bazel syntax: rationalize the type check operators
This change introduces {Dict,Sequence}.{,noneable}Cast.
These four conversion operators cast an arbitrary value
to a Sequence<T> or Dict<K,V>, replacing these 9 previous functions:
- Dict.{castSkylarkDictOrNoneToDict,getContents}
- Sequence.{castList,castSkylarkListOrNoneToList,getContents}
- SkylarkType.{cast,cast',checkType,castMap}
The functions don't allocate an unmodifiable wrapper,
as Dict and List are already unmodifiable through the java.util
interfaces, so this is just wasteful allocation and indirection.
Also, there is no a priori allocation of Formattable error messages.
A number of messes (e.g. unsound casts) were cleaned up throughout.
The new operators do not accept a Location, and report error
using Starlark.errorf. (This whole CL started as a subtask of a
subtask to eliminate the Location parameter of EvalException,
and this sub-sub-task is an experiment to see whether removing
Location parameters that don't correspond to program counter
locations is a UI regression. SRCTU.createTarget is the only
place where this appears to be a problem. For now I've added a
small kludge, but in a follow-up I will change it to report
events, not throw exceptions, so that it can report multiple
errors at arbitrary locations.
Depset might benefit from a similar {noneable,}Cast treatment,
but this too is left for a follow-up.
This is a breaking API change for copybara.
RELNOTES: N/A
PiperOrigin-RevId: 306925434
diff --git a/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java b/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java
index 302c856..7bc58b0 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/TargetUtils.java
@@ -275,15 +275,17 @@
* SkylarkSematicOptions#experimentalAllowTagsPropagation}
*/
public static ImmutableMap<String, String> getFilteredExecutionInfo(
- Object executionRequirementsUnchecked, Rule rule, boolean allowTagsPropagation)
+ @Nullable Object executionRequirementsUnchecked, Rule rule, boolean allowTagsPropagation)
throws EvalException {
Map<String, String> checkedExecutionRequirements =
TargetUtils.filter(
- Dict.castSkylarkDictOrNoneToDict(
- executionRequirementsUnchecked,
- String.class,
- String.class,
- "execution_requirements"));
+ executionRequirementsUnchecked == null
+ ? ImmutableMap.of()
+ : Dict.noneableCast(
+ executionRequirementsUnchecked,
+ String.class,
+ String.class,
+ "execution_requirements"));
Map<String, String> executionInfoBuilder = new HashMap<>();
// adding filtered execution requirements to the execution info map