aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar laurentlb <laurentlb@google.com>2017-06-23 16:03:00 +0200
committerGravatar Marcel Hlopko <hlopko@google.com>2017-06-26 18:31:16 +0200
commitc32efc8375cf482e795a43ecf4f009971722e71a (patch)
tree6d52f53c789b0d5437d78ebf922bbcf271ef695e /src
parent1d8cd59173e9c1e2fd7fd03dd4b2a0ae8a35ef4b (diff)
Make len(depset()) fail when --incompatible_depset_is_not_iterable is set
RELNOTES: None. PiperOrigin-RevId: 159945244
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java12
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java6
3 files changed, 16 insertions, 6 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 1e3b13486e..6cc14f295e 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
@@ -337,8 +337,8 @@ public final class EvalUtils {
if (env != null && env.getSemantics().incompatibleDepsetIsNotIterable) {
throw new EvalException(
loc,
- "type 'depset' is not iterable. Use the `to_list()` method to get a list. "
- + "Use --incompatible_depset_is_not_iterable to temporarily disable this check.");
+ "type 'depset' is not iterable. Use the `to_list()` method to get a list. Use "
+ + "--incompatible_depset_is_not_iterable=false to temporarily disable this check.");
}
return set.toCollection();
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
index afa4afd388..2f25c3b1f9 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
@@ -1607,11 +1607,19 @@ public class MethodLibrary {
returnType = Integer.class,
doc = "Returns the length of a string, list, tuple, depset, or dictionary.",
parameters = {@Param(name = "x", doc = "The object to check length of.")},
- useLocation = true
+ useLocation = true,
+ useEnvironment = true
)
private static final BuiltinFunction len =
new BuiltinFunction("len") {
- public Integer invoke(Object x, Location loc) throws EvalException {
+ public Integer invoke(Object x, Location loc, Environment env) throws EvalException {
+ if (env.getSemantics().incompatibleDepsetIsNotIterable && x instanceof SkylarkNestedSet) {
+ throw new EvalException(
+ loc,
+ EvalUtils.getDataTypeName(x)
+ + " is not iterable. Use --incompatible_depset_is_not_iterable=false to "
+ + "temporarily disable this check.");
+ }
int l = EvalUtils.size(x);
if (l == -1) {
throw new EvalException(loc, EvalUtils.getDataTypeName(x) + " is not iterable");
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
index a8248eaa1a..e6224fa44c 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
@@ -886,7 +886,8 @@ public class SkylarkEvaluationTest extends EvaluationTest {
.testIfErrorContains("not iterable", "max(depset([1, 2, 3]))")
.testIfErrorContains("not iterable", "sorted(depset(['a', 'b']))")
.testIfErrorContains("not iterable", "tuple(depset(['a', 'b']))")
- .testIfErrorContains("not iterable", "[x for x in depset()]");
+ .testIfErrorContains("not iterable", "[x for x in depset()]")
+ .testIfErrorContains("not iterable", "len(depset(['a']))");
}
@Test
@@ -896,7 +897,8 @@ public class SkylarkEvaluationTest extends EvaluationTest {
.testStatement("max(depset([1, 2, 3]))", 3)
.testStatement("str(sorted(depset(['b', 'a'])))", "[\"a\", \"b\"]")
.testStatement("str(tuple(depset(['a', 'b'])))", "(\"a\", \"b\")")
- .testStatement("str([x for x in depset()])", "[]");
+ .testStatement("str([x for x in depset()])", "[]")
+ .testStatement("len(depset(['a']))", 1);
}
@Test