Skip to content

Commit

Permalink
Fix toolchains to support type lookup.
Browse files Browse the repository at this point in the history
Fixes bazelbuild#13320.

PiperOrigin-RevId: 367624002
  • Loading branch information
katre authored and copybara-github committed Apr 9, 2021
1 parent d51a88f commit b120d4f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/main/java/net/starlark/java/eval/Eval.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private static Object inplaceBinaryOp(StarlarkThread.Frame fr, TokenKind op, Obj
list.extend(y);
return list;
}
return EvalUtils.binaryOp(op, x, y, fr.thread.getSemantics(), fr.thread.mutability());
return EvalUtils.binaryOp(op, x, y, fr.thread);
}

// ---- expressions ----
Expand Down Expand Up @@ -532,8 +532,7 @@ private static Object evalBinaryOperator(StarlarkThread.Frame fr, BinaryOperator
default:
Object y = eval(fr, binop.getY());
try {
return EvalUtils.binaryOp(
binop.getOperator(), x, y, fr.thread.getSemantics(), fr.thread.mutability());
return EvalUtils.binaryOp(binop.getOperator(), x, y, fr.thread);
} catch (EvalException ex) {
fr.setErrorLocation(binop.getOperatorLocation());
throw ex;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/net/starlark/java/eval/EvalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ static int toIndex(int index, int length) {
}

/** Evaluates an eager binary operation, {@code x op y}. (Excludes AND and OR.) */
static Object binaryOp(
TokenKind op, Object x, Object y, StarlarkSemantics semantics, Mutability mu)
static Object binaryOp(TokenKind op, Object x, Object y, StarlarkThread starlarkThread)
throws EvalException {
StarlarkSemantics semantics = starlarkThread.getSemantics();
Mutability mu = starlarkThread.mutability();
switch (op) {
case PLUS:
if (x instanceof StarlarkInt) {
Expand Down Expand Up @@ -339,6 +340,8 @@ static Object binaryOp(
case IN:
if (y instanceof StarlarkIndexable) {
return ((StarlarkIndexable) y).containsKey(semantics, x);
} else if (y instanceof StarlarkIndexable.Threaded) {
return ((StarlarkIndexable.Threaded) y).containsKey(starlarkThread, semantics, x);
} else if (y instanceof String) {
if (!(x instanceof String)) {
throw Starlark.errorf(
Expand All @@ -349,7 +352,7 @@ static Object binaryOp(
break;

case NOT_IN:
Object z = binaryOp(TokenKind.IN, x, y, semantics, mu);
Object z = binaryOp(TokenKind.IN, x, y, starlarkThread);
if (z != null) {
return !Starlark.truth(z);
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/shell/bazel/toolchain_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ function write_test_rule() {
mkdir -p toolchain
cat >> toolchain/rule_${rule_name}.bzl <<EOF
def _impl(ctx):
if '//toolchain:${toolchain_name}' not in ctx.toolchains:
fail('Toolchain type //toolchain:${toolchain_name} not found')
toolchain = ctx.toolchains['//toolchain:${toolchain_name}']
message = ctx.attr.message
print(
Expand Down

0 comments on commit b120d4f

Please sign in to comment.