{"record":{"id":"7d6646eb387c6cbe","repo":"bazelbuild/bazel","slug":"unsupported-comparison-s-s-7d6646","errorCode":null,"errorMessage":"unsupported comparison: %s <=> %s","messagePattern":"unsupported comparison: (.+?) <=> (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/eval/Starlark.java","lineNumber":616,"sourceCode":"                  Starlark.reprForErrors(x), Starlark.reprForErrors(y)));\n        }\n      }\n\n    } else {\n      // different types\n\n      if (x instanceof StarlarkFloat && y instanceof StarlarkInt) {\n        // float < int\n        double xf = ((StarlarkFloat) x).toDouble();\n        return Double.isNaN(xf) ? +1 : -StarlarkInt.compareIntAndDouble((StarlarkInt) y, xf);\n      } else if (x instanceof StarlarkInt && y instanceof StarlarkFloat) {\n        // int < float\n        double yf = ((StarlarkFloat) y).toDouble();\n        return Double.isNaN(yf) ? -1 : StarlarkInt.compareIntAndDouble((StarlarkInt) x, yf);\n      }\n    }\n\n    throw new ClassCastException(\n        String.format(\"unsupported comparison: %s <=> %s\", Starlark.type(x), Starlark.type(y)));\n  }\n\n  /**\n   * Returns true if the given values are equal. Safe to use for potentially self-referential\n   * Starlark data structures.\n   *\n   * @throws EvalException if x and/or y is a self-referential data structures (signaled by {@link\n   *     Object#equals} overflowing the stack)\n   */\n  public static boolean checkedEquals(@Nullable Object x, @Nullable Object y) throws EvalException {\n    if (x == null) {\n      return y == null;\n    }\n    try {\n      return x.equals(y);\n    } catch (StackOverflowError unused) {\n      throw Starlark.errorf(","sourceCodeStart":598,"sourceCodeEnd":634,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/eval/Starlark.java#L598-L634","documentation":"The underlying throw inside Starlark.compareUnchecked: after same-type comparison and the int/float special cases, the values are mutually unordered, so it raises ClassCastException('unsupported comparison: T1 <=> T2'). Higher-level callers translate this: EvalUtils.compare wraps it into EvalException (error 100) and MethodLibrary's min/max rethrow its getMessage() (error 101). You only see the raw ClassCastException if you call compareUnchecked or an ordering built on it directly from Java.","triggerScenarios":"Calling Starlark.compareUnchecked from Java embedding code (e.g. custom StarlarkCallable implementations or orderings) with e.g. a String and an Integer; using Guava Ordering.arbitrary()/natural() on mixed Starlark values outside MethodLibrary's guarded path.","commonSituations":"Embedding the Starlark interpreter in a host application and reusing compareUnchecked for sorting; custom builtins that sort their arguments without catching ClassCastException.","solutions":["In host code, call the guarded wrapper (EvalUtils.compare semantics) or catch ClassCastException and translate it to your error type.","Sort with an explicit key/ordering restricted to one type.","Validate/normalize types before comparing (see error 100 fixes)."],"exampleFix":"// before\nint cmp = Starlark.compareUnchecked(x, y); // raw ClassCastException on mismatch\n\n// after\nint cmp;\ntry {\n  cmp = Starlark.compareUnchecked(x, y);\n} catch (ClassCastException e) {\n  throw new IllegalArgumentException(\"cannot order \" + Starlark.type(x), e);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Java host code calling compareUnchecked directly\ntry {\n  int cmp = Starlark.compareUnchecked(x, y);\n} catch (ClassCastException e) {\n  throw new IllegalArgumentException(\"unsupported comparison: \" + Starlark.type(x) + \" <=> \" + Starlark.type(y), e);\n}","preventionTips":["Prefer the guarded EvalUtils.compare path in host code over raw compareUnchecked.","Catch StackOverflowError-turned-ClassCastException too if data may be cyclic (same exception channel).","Restrict custom orderings to values of one known type."],"tags":["starlark","comparison","java-api","classcast","embedding"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}