Make len(depset()) fail when --incompatible_depset_is_not_iterable is set

RELNOTES: None.
PiperOrigin-RevId: 159945244
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 1e3b134..6cc14f2 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 @@
     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 afa4afd..2f25c3b 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 @@
     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");