diff options
author | 2015-11-05 13:51:53 +0000 | |
---|---|---|
committer | 2015-11-05 16:51:58 +0000 | |
commit | a58dcd1d9db64032c2f4c25cb6d14225bc34f065 (patch) | |
tree | 1187bcb9f588bf143905a7ba50d80c75e6d541e7 /src | |
parent | cde721b9623d72e87bf643f6cf3b7dc8d05dc1a7 (diff) |
Improve error message for select objects ('+' operator for incompatible types)
--
MOS_MIGRATED_REVID=107131704
Diffstat (limited to 'src')
3 files changed, 28 insertions, 4 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java index 0967917af7..d284427219 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java @@ -265,6 +265,9 @@ public final class EvalUtils { } else if (object instanceof SkylarkNestedSet) { SkylarkNestedSet set = (SkylarkNestedSet) object; return "set" + (full ? " of " + set.getContentType() + "s" : ""); + } else if (object instanceof SelectorList) { + SelectorList list = (SelectorList) object; + return "select" + (full ? " of " + getDataTypeNameFromClass(list.getType()) : ""); } else { return getDataTypeNameFromClass(object.getClass()); } diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java b/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java index 60033c05b5..4d76524fe8 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/SelectorList.java @@ -60,7 +60,7 @@ public final class SelectorList { /** * Returns the native type contained by this expression. */ - private Class<?> getType() { + public Class<?> getType() { return type; } @@ -83,11 +83,16 @@ public final class SelectorList { Class<?> type1 = addValue(value1, builder); Class<?> type2 = addValue(value2, builder); if (!canConcatenate(type1, type2)) { - throw new EvalException(location, "'+' operator applied to incompatible types"); + throw new EvalException( + location, + String.format( + "'+' operator applied to incompatible types (%s, %s)", + EvalUtils.getDataTypeName(value1, true), + EvalUtils.getDataTypeName(value2, true))); } return new SelectorList(type1, builder.build()); } - + // TODO(bazel-team): match on the List interface, not the actual implementation. For now, // we verify this is the right class through test coverage. private static final Class<?> NATIVE_LIST_TYPE = ArrayList.class; @@ -126,4 +131,4 @@ public final class SelectorList { public String toString() { return Joiner.on(" + ").join(elements); } -}
\ No newline at end of file +} diff --git a/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java index 5a578e8e27..be1cf6d0db 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java @@ -530,6 +530,22 @@ public class EvaluationTest extends EvaluationTestCase { } @Test + public void testAddSelectIncompatibleType() throws Exception { + newTest() + .testIfErrorContains( + "'+' operator applied to incompatible types (select of list, int)", + "select({'foo': ['FOO'], 'bar': ['BAR']}) + 1"); + } + + @Test + public void testAddSelectIncompatibleType2() throws Exception { + newTest() + .testIfErrorContains( + "'+' operator applied to incompatible types (select of list, select of int)", + "select({'foo': ['FOO']}) + select({'bar': 2})"); + } + + @Test public void testListComprehensionFailsOnNonSequence() throws Exception { newTest().testIfErrorContains("type 'int' is not iterable", "[x + 1 for x in 123]"); } |