bazel syntax: add FileOptions
FileOptions is a set of per-file options that control the front-end ("static")
processing of a single Starlark file: scanning, parsing, validation,
and, eventually, compilation.
FileOptions determine language dialect and "code generation",
like the options to a compiler. Different files may have different options,
as in the case of BUILD and .bzl files, for example.
By contrast, a StarlarkSemantics affects only dynamic behavior,
and is carried down the thread, possibly through many files that
vary in their FileOptions.
FileOptions are specified at the construction of a StarlarkFile (parsing)
and are retained by it thereafter. This reduces the burden of ensuring
that consistent options are provided to the operations of parsing and validation
when these steps are widely separated in the source, as is often the case
in Bazel.
This change allows Copybara do validation on all its files.
It was previously unable because it could not select validation
options "a la carte". This will soon allow us to assume that all
files are resolved before execution.
The API for creating a Starlark thread is getting more verbose.
This is temporary; to "refactor" a term in an equation one must
first expand it out.
Details:
- Add FileOptions parameters to scanner, parser, and their wrappers.
- Remove the StarlarkSemantics parameter from getUndeclaredNameError.
It was only used for an assertion.
- Move StarlakSemantics into the evaluator package.
ValidationTest no longer depends on semantics or lib.packages.
- Decompose the validator's previous isBuildFile parameter into
separate features: top-level rebinding, whether to call setScope, etc.
- Simplify the "restrict string escapes" feature by pushing it
from the validator to the scanner.
- Simplify the "load disregards privacy" feature by pushing it
from the syntax tree into the validator. Delete parseVirtualBuildFile.
- Add a placeholder for a "load binds globally" feature,
for load in the REPL and for upcoming changes to the implementation
of the prelude. Its implementation will come later.
- Move the "load of private symbol" check from Eval to Validator.
This is a breaking API change for Copybara.
PiperOrigin-RevId: 302691302
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
index 7728813..b1ec447 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
@@ -53,6 +53,8 @@
import com.google.devtools.build.lib.syntax.Dict;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.EvalUtils;
+import com.google.devtools.build.lib.syntax.FileOptions;
+import com.google.devtools.build.lib.syntax.Module;
import com.google.devtools.build.lib.syntax.Mutability;
import com.google.devtools.build.lib.syntax.ParserInput;
import com.google.devtools.build.lib.syntax.StarlarkFile;
@@ -729,12 +731,11 @@
assertThat(c.hasAttr("a1", Type.STRING)).isTrue();
}
- // TODO(adonovan): rename execAndExport
private void evalAndExport(String... lines) throws Exception {
ParserInput input = ParserInput.fromLines(lines);
StarlarkThread thread = ev.getStarlarkThread();
- StarlarkFile file =
- EvalUtils.parseAndValidate(input, thread.getGlobals(), thread.getSemantics());
+ Module module = thread.getGlobals();
+ StarlarkFile file = EvalUtils.parseAndValidate(input, FileOptions.DEFAULT, module);
if (!file.ok()) {
throw new SyntaxError(file.errors());
}