bazel syntax: remove Location from calling convention
This change removes the Location parameter from StarlarkCallable.call
(built-in functions) and the SkylarkCallable.useLocation feature from
annotated built-in functions.
Lately, the thread's call stack records the position of the program
counter of each active frame, so clients may access it through
StarlarkThread.getCallerLocation.
Numerous parameters that pass Locations merely to construct an
EvalException have been deleted. (This is the bulk of the change.)
Many places that directly constructed an EvalException now call
Starlark.errorf instead. (A few places in Bazel formerly specified a
location other than thread.getCallerLocation, or equivalently, null.)
The evaluator ensures that any exception thrown by Starlark.call
is augmented with the location of the call.
PiperOrigin-RevId: 291009378
diff --git a/src/main/java/com/google/devtools/build/lib/packages/StructProvider.java b/src/main/java/com/google/devtools/build/lib/packages/StructProvider.java
index 998f632..ca75eb8 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/StructProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/StructProvider.java
@@ -18,6 +18,8 @@
import com.google.devtools.build.lib.skylarkbuildapi.core.StructApi;
import com.google.devtools.build.lib.syntax.Dict;
import com.google.devtools.build.lib.syntax.EvalException;
+import com.google.devtools.build.lib.syntax.Starlark;
+import com.google.devtools.build.lib.syntax.StarlarkThread;
import java.util.Map;
/**
@@ -36,26 +38,34 @@
}
@Override
- public StructImpl createStruct(Dict<?, ?> kwargs, Location loc) throws EvalException {
- Map<String, Object> kwargsMap = kwargs.getContents(String.class, Object.class, "kwargs");
- if (kwargsMap.containsKey("to_json")) {
- throw new EvalException(loc, "cannot override built-in struct function 'to_json'");
+ public StructImpl createStruct(Dict<String, Object> kwargs, StarlarkThread thread)
+ throws EvalException {
+ return create(kwargs, thread.getCallerLocation());
+ }
+
+ // Called from SkylarkRepositoryContext. TODO(adonovan): eliminate.
+ public StructImpl createWithBuiltinLocation(Dict<String, Object> kwargs) throws EvalException {
+ return create(kwargs, Location.BUILTIN);
+ }
+
+ private StructImpl create(Dict<String, Object> kwargs, Location location) throws EvalException {
+ if (kwargs.containsKey("to_json")) {
+ throw Starlark.errorf("cannot override built-in struct function 'to_json'");
}
- if (kwargsMap.containsKey("to_proto")) {
- throw new EvalException(loc, "cannot override built-in struct function 'to_proto'");
+ if (kwargs.containsKey("to_proto")) {
+ throw Starlark.errorf("cannot override built-in struct function 'to_proto'");
}
- return SkylarkInfo.create(this, kwargsMap, loc);
+ return SkylarkInfo.create(this, kwargs, location);
}
/**
* Creates a struct with the given field values and message format for unknown fields.
*
* <p>The custom message is useful for objects that have fields but aren't exactly used as
- * providers, such as the {@code native} object, and the struct fields of {@code ctx} like
- * {@code ctx.attr}.
- * */
- public SkylarkInfo create(
- Map<String, Object> values, String errorMessageFormatForUnknownField) {
+ * providers, such as the {@code native} object, and the struct fields of {@code ctx} like {@code
+ * ctx.attr}.
+ */
+ public SkylarkInfo create(Map<String, Object> values, String errorMessageFormatForUnknownField) {
return SkylarkInfo.createWithCustomMessage(this, values, errorMessageFormatForUnknownField);
}
}