Refactor: pass `TypeContext` to `StarlarkType.inferBinaryOperator`

Pure refactoring change; needed for `StarlarkBuiltinAutoType` to support more precise constraints for the `in` / `not in` operator.

Working towards #27370

PiperOrigin-RevId: 1035706866
Change-Id: Ibdd1998c0eb4057c3aa755e11aa226f21007807f
diff --git a/src/main/java/com/google/devtools/build/lib/packages/SelectorList.java b/src/main/java/com/google/devtools/build/lib/packages/SelectorList.java
index 1b6e241..e409257 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/SelectorList.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/SelectorList.java
@@ -34,6 +34,7 @@
 import net.starlark.java.annot.StarlarkBuiltin;
 import net.starlark.java.annot.StarlarkLibrary;
 import net.starlark.java.annot.StarlarkMethod;
+import net.starlark.java.eval.CallUtils;
 import net.starlark.java.eval.Dict;
 import net.starlark.java.eval.EvalException;
 import net.starlark.java.eval.HasBinary;
@@ -332,7 +333,8 @@
       } else {
         overallType =
             checkNotNull(
-                StarlarkType.inferBinaryOperator(overallType, operator, elementType),
+                StarlarkType.inferBinaryOperator(
+                    overallType, operator, elementType, CallUtils.getBuiltinManager(semantics)),
                 "In %s, cannot apply %s operator to element types '%s' and '%s'. This is a bug in"
                     + " SelectorList machinery.",
                 Starlark.repr(this, semantics),
diff --git a/src/main/java/com/google/devtools/build/lib/packages/SelectorValue.java b/src/main/java/com/google/devtools/build/lib/packages/SelectorValue.java
index 4c4d798..cc41a3e 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/SelectorValue.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/SelectorValue.java
@@ -189,7 +189,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       // The inferred type is a select of the type of the operator applied to the value types of the
       // operands (if selector types) or the operand types themselves (otherwise). The main
       // complication is that we need to unfold unions ourselves, replicating part of the logic of
@@ -217,7 +217,7 @@
           StarlarkType lhsValueType = thisLeft ? thisValueType : thatValueType;
           StarlarkType rhsValueType = thisLeft ? thatValueType : thisValueType;
           StarlarkType result =
-              StarlarkType.inferBinaryOperator(lhsValueType, operator, rhsValueType);
+              StarlarkType.inferBinaryOperator(lhsValueType, operator, rhsValueType, context);
           if (result != null) {
             resultTypes.add(result);
           } else {
diff --git a/src/main/java/net/starlark/java/syntax/StarlarkType.java b/src/main/java/net/starlark/java/syntax/StarlarkType.java
index 8ac6acc..5052c3b 100644
--- a/src/main/java/net/starlark/java/syntax/StarlarkType.java
+++ b/src/main/java/net/starlark/java/syntax/StarlarkType.java
@@ -209,7 +209,8 @@
    *     operators on certain built-in types (e.g. tuple multiplication).
    */
   @Nullable
-  public StarlarkType inferBinaryOperator(TokenKind operator, StarlarkType that, boolean thisLeft) {
+  public StarlarkType inferBinaryOperator(
+      TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
     return null;
   }
 
@@ -225,10 +226,10 @@
    */
   @Nullable
   public static StarlarkType inferBinaryOperator(
-      StarlarkType lhs, TokenKind operator, StarlarkType rhs) {
-    StarlarkType result = lhs.inferBinaryOperator(operator, rhs, true);
+      StarlarkType lhs, TokenKind operator, StarlarkType rhs, TypeContext context) {
+    StarlarkType result = lhs.inferBinaryOperator(operator, rhs, true, context);
     if (result == null) {
-      result = rhs.inferBinaryOperator(operator, lhs, false);
+      result = rhs.inferBinaryOperator(operator, lhs, false, context);
     }
     return result;
   }
diff --git a/src/main/java/net/starlark/java/syntax/TypeChecker.java b/src/main/java/net/starlark/java/syntax/TypeChecker.java
index 4f7de58..0b5c1b8 100644
--- a/src/main/java/net/starlark/java/syntax/TypeChecker.java
+++ b/src/main/java/net/starlark/java/syntax/TypeChecker.java
@@ -543,7 +543,7 @@
           for (StarlarkType yElemType : yTypes) {
             @Nullable
             StarlarkType resultType =
-                StarlarkType.inferBinaryOperator(xElemType, operator, yElemType);
+                StarlarkType.inferBinaryOperator(xElemType, operator, yElemType, typeContext);
             if (resultType == null && operator == TokenKind.STAR) {
               // Tuple repetition is the only case where we need to examine the expressions.
               // TODO: #28037 - We can get rid of the tuple repetition special case if we
diff --git a/src/main/java/net/starlark/java/syntax/Types.java b/src/main/java/net/starlark/java/syntax/Types.java
index 9cf4b06..4ce90e7 100644
--- a/src/main/java/net/starlark/java/syntax/Types.java
+++ b/src/main/java/net/starlark/java/syntax/Types.java
@@ -211,7 +211,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case IN, NOT_IN ->
             // If we are the LHS, fall through to RHS's inferBinaryOperator; RHS determines whether
@@ -375,7 +375,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case PLUS, MINUS, PERCENT, SLASH_SLASH -> NUMERIC.getTypes().contains(that) ? that : null;
         case SLASH -> NUMERIC.getTypes().contains(that) ? Types.FLOAT : null;
@@ -417,7 +417,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case PLUS, MINUS, PERCENT, SLASH, SLASH_SLASH, STAR ->
             NUMERIC.getTypes().contains(that) ? Types.FLOAT : null;
@@ -453,7 +453,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case PLUS -> that.equals(STR) ? STR : null;
         case PERCENT ->
@@ -1320,14 +1320,14 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case PLUS ->
             that instanceof BaseListType thatList
                 ? listRvalue(union(getElementType(), thatList.getElementType()))
                 : null;
         case STAR -> that.equals(Types.INT) ? this.toRvalue() : null;
-        default -> super.inferBinaryOperator(operator, that, thisLeft);
+        default -> super.inferBinaryOperator(operator, that, thisLeft, context);
       };
     }
 
@@ -1532,7 +1532,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case AMPERSAND, MINUS ->
             // TODO: #27370 - we may want to tighten the type of a set intersection, but it's
@@ -1542,7 +1542,7 @@
             that instanceof SetType thatSet
                 ? set(union(getElementType(), thatSet.getElementType()))
                 : null;
-        default -> super.inferBinaryOperator(operator, that, thisLeft);
+        default -> super.inferBinaryOperator(operator, that, thisLeft, context);
       };
     }
 
@@ -1579,12 +1579,12 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case PLUS -> that instanceof TupleType rhsTuple ? concatenate(rhsTuple) : null;
         // Special case handled by TypeChecker.inferTupleRepetition.
         case STAR -> null;
-        default -> super.inferBinaryOperator(operator, that, thisLeft);
+        default -> super.inferBinaryOperator(operator, that, thisLeft, context);
       };
     }
   }
@@ -1796,7 +1796,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         // `in` and `not in` are always valid for collections on the RHS.
         case IN, NOT_IN -> thisLeft ? null : BOOL;
@@ -1957,7 +1957,7 @@
     @Override
     @Nullable
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType rhs, boolean thisLeft) {
+        TokenKind operator, StarlarkType rhs, boolean thisLeft, TypeContext context) {
       return switch (operator) {
         case PIPE ->
             // TODO: #27370 - mypy supports dict | dict, but doesn't support the | operator for
@@ -1969,7 +1969,7 @@
                     union(getKeyType(), rhsMapping.getKeyType()),
                     union(getValueType(), rhsMapping.getValueType()))
                 : null;
-        default -> super.inferBinaryOperator(operator, rhs, thisLeft);
+        default -> super.inferBinaryOperator(operator, rhs, thisLeft, context);
       };
     }
   }
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkTypesTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkTypesTest.java
index ec81d3f..d80f38a 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkTypesTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkTypesTest.java
@@ -825,7 +825,7 @@
 
     @Override
     public StarlarkType inferBinaryOperator(
-        TokenKind operator, StarlarkType that, boolean thisLeft) {
+        TokenKind operator, StarlarkType that, boolean thisLeft, TypeContext context) {
       if (operator == TokenKind.PLUS && addsTo.test(that) && thisLeft == this.thisLeft) {
         return result;
       }
@@ -843,31 +843,35 @@
       TokenKind operator,
       StarlarkType rhsArgToSelect,
       StarlarkType expectedArgToSelect) {
+    TypeContext typeContext = getTypeContext();
     assertThat(
             StarlarkType.inferBinaryOperator(
                 SelectorValue.Type.of(lhsArgToSelec),
                 operator,
-                SelectorValue.Type.of(rhsArgToSelect)))
+                SelectorValue.Type.of(rhsArgToSelect),
+                typeContext))
         .isEqualTo(SelectorValue.Type.of(expectedArgToSelect));
     assertThat(
             StarlarkType.inferBinaryOperator(
-                SelectorValue.Type.of(lhsArgToSelec), operator, rhsArgToSelect))
+                SelectorValue.Type.of(lhsArgToSelec), operator, rhsArgToSelect, typeContext))
         .isEqualTo(SelectorValue.Type.of(expectedArgToSelect));
     assertThat(
             StarlarkType.inferBinaryOperator(
-                lhsArgToSelec, operator, SelectorValue.Type.of(rhsArgToSelect)))
+                lhsArgToSelec, operator, SelectorValue.Type.of(rhsArgToSelect), typeContext))
         .isEqualTo(SelectorValue.Type.of(expectedArgToSelect));
   }
 
   @Test
   public void select_inferBinaryOperator() throws Exception {
+    TypeContext typeContext = getTypeContext();
     // Basic cases
     assertThat(
-            SelectorValue.Type.of(Types.INT).inferBinaryOperator(TokenKind.PLUS, Types.INT, true))
+            SelectorValue.Type.of(Types.INT)
+                .inferBinaryOperator(TokenKind.PLUS, Types.INT, true, typeContext))
         .isEqualTo(SelectorValue.Type.of(Types.INT));
     assertThat(
             SelectorValue.Type.of(Types.list(Types.INT))
-                .inferBinaryOperator(TokenKind.PLUS, Types.list(Types.STR), true))
+                .inferBinaryOperator(TokenKind.PLUS, Types.list(Types.STR), true, typeContext))
         .isEqualTo(SelectorValue.Type.of(Types.list(Types.union(Types.INT, Types.STR))));
 
     // Distinct types which add to str on the left, producing int and float respectively.
@@ -886,11 +890,11 @@
     // error, so we forbid it statically too.
     assertThat(
             SelectorValue.Type.of(Types.FLOAT)
-                .inferBinaryOperator(TokenKind.PLUS, Types.INT, false))
+                .inferBinaryOperator(TokenKind.PLUS, Types.INT, false, typeContext))
         .isNull();
     assertThat(
             SelectorValue.Type.of(Types.INT)
-                .inferBinaryOperator(TokenKind.PLUS, Types.FLOAT, false))
+                .inferBinaryOperator(TokenKind.PLUS, Types.FLOAT, false, typeContext))
         .isNull();
   }