Skip to content

Commit ac9d710

Browse files
aoeuicopybara-github
authored andcommitted
Add identityToken to StarlarkDefinedAspect via its StarlarkAspectClass.
PiperOrigin-RevId: 638277768 Change-Id: Ib1e2a570fb1ea86e998ef50beb6096fac87d14de
1 parent bb702b6 commit ac9d710

File tree

7 files changed

+60
-29
lines changed

7 files changed

+60
-29
lines changed

src/main/java/com/google/devtools/build/lib/analysis/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ java_library(
656656
"//src/main/java/com/google/devtools/build/lib/profiler",
657657
"//src/main/java/com/google/devtools/build/lib/skyframe:aspect_key_creator",
658658
"//src/main/java/com/google/devtools/build/lib/skyframe:build_result_listener",
659+
"//src/main/java/com/google/devtools/build/lib/skyframe:bzl_load_value",
659660
"//src/main/java/com/google/devtools/build/lib/skyframe:configured_target_key",
660661
"//src/main/java/com/google/devtools/build/lib/skyframe:coverage_report_value",
661662
"//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_value",

src/main/java/com/google/devtools/build/lib/analysis/BuildView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.google.devtools.build.lib.analysis;
1515

1616
import static com.google.common.collect.ImmutableList.toImmutableList;
17+
import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild;
1718

1819
import com.google.common.annotations.VisibleForTesting;
1920
import com.google.common.base.Preconditions;
@@ -515,7 +516,8 @@ private ImmutableList<TopLevelAspectsKey> createTopLevelAspectKeys(
515516
e);
516517
}
517518
String starlarkFunctionName = aspect.substring(delimiterPosition + 1);
518-
aspectClassesBuilder.add(new StarlarkAspectClass(starlarkFileLabel, starlarkFunctionName));
519+
aspectClassesBuilder.add(
520+
new StarlarkAspectClass(keyForBuild(starlarkFileLabel), starlarkFunctionName));
519521
} else {
520522
final NativeAspectClass aspectFactoryClass =
521523
ruleClassProvider.getNativeAspectClassMap().get(aspect);

src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,22 @@ public StarlarkCallable macro(
356356
builder.addAttribute(attr);
357357
}
358358

359+
return new MacroFunction(
360+
builder,
361+
Starlark.toJavaOptional(doc, String.class).map(Starlark::trimDocString),
362+
getBzlKeyToken(thread, "Macros"));
363+
}
364+
365+
private static Symbol<BzlLoadValue.Key> getBzlKeyToken(StarlarkThread thread, String onBehalfOf) {
359366
Symbol<?> untypedToken = thread.getNextIdentityToken();
360367
checkState(
361368
untypedToken.getOwner() instanceof BzlLoadValue.Key,
362-
"Macros may only be owned by .bzl files (owner=%s)",
369+
"%s may only be owned by .bzl files (owner=%s)",
370+
onBehalfOf,
363371
untypedToken);
364372
@SuppressWarnings("unchecked")
365373
var typedToken = (Symbol<BzlLoadValue.Key>) untypedToken;
366-
367-
return new MacroFunction(
368-
builder,
369-
Starlark.toJavaOptional(doc, String.class).map(Starlark::trimDocString),
370-
typedToken);
374+
return typedToken;
371375
}
372376

373377
// TODO(bazel-team): implement attribute copy and other rule properties
@@ -1103,7 +1107,8 @@ public StarlarkAspect aspect(
11031107
applyToGeneratingRules,
11041108
execCompatibleWith,
11051109
execGroups,
1106-
ImmutableSet.copyOf(subrules));
1110+
ImmutableSet.copyOf(subrules),
1111+
getBzlKeyToken(thread, "Aspects"));
11071112
}
11081113

11091114
private static ImmutableSet<String> getLegacyAnyTypeAttrs(RuleClass ruleClass) {

src/main/java/com/google/devtools/build/lib/packages/StarlarkAspectClass.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@
1414

1515
package com.google.devtools.build.lib.packages;
1616

17+
import static com.google.devtools.build.lib.util.HashCodes.hashObjects;
18+
1719
import com.google.devtools.build.lib.cmdline.Label;
1820
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
21+
import com.google.devtools.build.lib.skyframe.BzlLoadValue;
1922

2023
/** {@link AspectClass} for aspects defined in Starlark. */
2124
@Immutable
2225
public final class StarlarkAspectClass implements AspectClass {
23-
private final Label extensionLabel;
26+
private final BzlLoadValue.Key extensionKey;
2427
private final String exportedName;
2528
private final String name;
2629

27-
public StarlarkAspectClass(Label extensionLabel, String exportedName) {
28-
this.extensionLabel = extensionLabel;
30+
public StarlarkAspectClass(BzlLoadValue.Key extensionKey, String exportedName) {
31+
this.extensionKey = extensionKey;
2932
this.exportedName = exportedName;
30-
this.name = extensionLabel + "%" + exportedName;
33+
this.name = extensionKey.getLabel() + "%" + exportedName;
34+
}
35+
36+
BzlLoadValue.Key getExtensionKey() {
37+
return extensionKey;
3138
}
3239

3340
public Label getExtensionLabel() {
34-
return extensionLabel;
41+
return extensionKey.getLabel();
3542
}
3643

3744
public String getExportedName() {
@@ -54,15 +61,12 @@ public boolean equals(Object o) {
5461
}
5562

5663
StarlarkAspectClass that = (StarlarkAspectClass) o;
57-
58-
return extensionLabel.equals(that.extensionLabel)
59-
&& exportedName.equals(that.exportedName);
64+
return extensionKey.equals(that.extensionKey) && exportedName.equals(that.exportedName);
6065
}
6166

6267
@Override
6368
public int hashCode() {
64-
// Inlines the implementation of Objects.hashCode to avoid generating garbage.
65-
return 31 * extensionLabel.hashCode() + exportedName.hashCode();
69+
return hashObjects(extensionKey, exportedName);
6670
}
6771

6872
@Override

src/main/java/com/google/devtools/build/lib/packages/StarlarkDefinedAspect.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.devtools.build.lib.packages;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
18+
1719
import com.github.benmanes.caffeine.cache.Caffeine;
1820
import com.github.benmanes.caffeine.cache.LoadingCache;
1921
import com.google.common.base.Ascii;
@@ -25,6 +27,7 @@
2527
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
2628
import com.google.devtools.build.lib.cmdline.Label;
2729
import com.google.devtools.build.lib.events.EventHandler;
30+
import com.google.devtools.build.lib.skyframe.BzlLoadValue;
2831
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkSubruleApi;
2932
import java.io.Serializable;
3033
import java.util.Objects;
@@ -35,6 +38,7 @@
3538
import net.starlark.java.eval.Starlark;
3639
import net.starlark.java.eval.StarlarkCallable;
3740
import net.starlark.java.eval.StarlarkInt;
41+
import net.starlark.java.eval.SymbolGenerator.Symbol;
3842

3943
/** A Starlark value that is a result of an 'aspect(..)' function call. */
4044
public final class StarlarkDefinedAspect implements StarlarkExportable, StarlarkAspect {
@@ -58,7 +62,8 @@ public final class StarlarkDefinedAspect implements StarlarkExportable, Starlark
5862
private final ImmutableMap<String, ExecGroup> execGroups;
5963
private final ImmutableSet<? extends StarlarkSubruleApi> subrules;
6064

61-
private StarlarkAspectClass aspectClass;
65+
/** {@link Symbol} before {@link #export} and a {@link StarlarkAspectClass} after. */
66+
private Object aspectClassOrIdentityToken;
6267

6368
private static final ImmutableSet<String> TRUE_REPS =
6469
ImmutableSet.of("true", "1", "yes", "t", "y");
@@ -81,7 +86,8 @@ public StarlarkDefinedAspect(
8186
boolean applyToGeneratingRules,
8287
ImmutableSet<Label> execCompatibleWith,
8388
ImmutableMap<String, ExecGroup> execGroups,
84-
ImmutableSet<? extends StarlarkSubruleApi> subrules) {
89+
ImmutableSet<? extends StarlarkSubruleApi> subrules,
90+
Symbol<BzlLoadValue.Key> identityToken) {
8591
this.implementation = implementation;
8692
this.documentation = documentation.orElse(null);
8793
this.attributeAspects = attributeAspects;
@@ -97,6 +103,7 @@ public StarlarkDefinedAspect(
97103
this.execCompatibleWith = execCompatibleWith;
98104
this.execGroups = execGroups;
99105
this.subrules = subrules;
106+
this.aspectClassOrIdentityToken = identityToken;
100107
}
101108

102109
public StarlarkCallable getImplementation() {
@@ -138,7 +145,7 @@ public String getName() {
138145
@Override
139146
public StarlarkAspectClass getAspectClass() {
140147
Preconditions.checkState(isExported());
141-
return aspectClass;
148+
return (StarlarkAspectClass) aspectClassOrIdentityToken;
142149
}
143150

144151
@Override
@@ -149,7 +156,16 @@ public ImmutableSet<String> getParamAttributes() {
149156
@Override
150157
public void export(EventHandler handler, Label extensionLabel, String name) {
151158
Preconditions.checkArgument(!isExported());
152-
this.aspectClass = new StarlarkAspectClass(extensionLabel, name);
159+
@SuppressWarnings("unchecked")
160+
var identityToken = (Symbol<BzlLoadValue.Key>) aspectClassOrIdentityToken;
161+
BzlLoadValue.Key owner = identityToken.getOwner();
162+
checkArgument(
163+
owner.getLabel().equals(extensionLabel),
164+
"Exporting aspect as (%s, %s) but label did not match owner=%s",
165+
extensionLabel,
166+
name,
167+
owner);
168+
this.aspectClassOrIdentityToken = new StarlarkAspectClass(owner, name);
153169
}
154170

155171
private static final ImmutableList<String> ALL_ATTR_ASPECTS = ImmutableList.of("*");
@@ -173,7 +189,8 @@ public AspectDefinition getDefinition(AspectParameters aspectParams) {
173189
}
174190

175191
private AspectDefinition buildDefinition(AspectParameters aspectParams) {
176-
AspectDefinition.Builder builder = new AspectDefinition.Builder(aspectClass);
192+
AspectDefinition.Builder builder =
193+
new AspectDefinition.Builder((StarlarkAspectClass) aspectClassOrIdentityToken);
177194
if (ALL_ATTR_ASPECTS.equals(attributeAspects)) {
178195
builder.propagateAlongAllAttributes();
179196
} else {
@@ -254,7 +271,7 @@ private static Attribute addAttrValue(Attribute attr, String attrValue) {
254271

255272
@Override
256273
public boolean isExported() {
257-
return aspectClass != null;
274+
return aspectClassOrIdentityToken instanceof StarlarkAspectClass;
258275
}
259276

260277
@Override
@@ -406,7 +423,7 @@ public boolean equals(Object o) {
406423
&& Objects.equals(requiredAspects, that.requiredAspects)
407424
&& Objects.equals(fragments, that.fragments)
408425
&& Objects.equals(toolchainTypes, that.toolchainTypes)
409-
&& Objects.equals(aspectClass, that.aspectClass);
426+
&& Objects.equals(aspectClassOrIdentityToken, that.aspectClassOrIdentityToken);
410427
}
411428

412429
@Override
@@ -422,6 +439,6 @@ public int hashCode() {
422439
requiredAspects,
423440
fragments,
424441
toolchainTypes,
425-
aspectClass);
442+
aspectClassOrIdentityToken);
426443
}
427444
}

src/test/java/com/google/devtools/build/lib/skyframe/TargetCycleReporterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.google.devtools.build.lib.skyframe;
1515

1616
import static com.google.common.truth.Truth.assertThat;
17+
import static com.google.devtools.build.lib.skyframe.BzlLoadValue.keyForBuild;
1718

1819
import com.google.common.collect.ImmutableList;
1920
import com.google.common.collect.ImmutableMap;
@@ -93,7 +94,7 @@ public void loadingPhaseCycleWithDifferentTopLevelKeyTypes() throws Exception {
9394
AspectKeyCreator.createTopLevelAspectsKey(
9495
ImmutableList.of(
9596
new StarlarkAspectClass(
96-
Label.parseCanonicalUnchecked("//foo:b"), "my Starlark key")),
97+
keyForBuild(Label.parseCanonicalUnchecked("//foo:b")), "my Starlark key")),
9798
Label.parseCanonicalUnchecked("//foo:a"),
9899
targetConfig,
99100
/* topLevelAspectsParameters= */ ImmutableMap.of());

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ def _impl(name, xyz): pass
607607
@Test
608608
public void testSymbolicMacro_macroFunctionApi() throws Exception {
609609
ev.setSemantics("--experimental_enable_first_class_macros");
610-
ev.setThreadOwner(keyForBuild(FAKE_LABEL));
611610

612611
evalAndExport(
613612
ev,
@@ -1018,6 +1017,7 @@ public void testLabelWithAspects() throws Exception {
10181017

10191018
@Test
10201019
public void testLabelListWithAspectsError() throws Exception {
1020+
ev.setThreadOwner(keyForBuild(FAKE_LABEL));
10211021
ev.checkEvalErrorContains(
10221022
"at index 1 of aspects, got element of type int, want Aspect",
10231023
"def _impl(target, ctx):",
@@ -1618,6 +1618,7 @@ public void testRuleAddAttribute() throws Exception {
16181618
}
16191619

16201620
private static void evalAndExport(BazelEvaluationTestCase ev, String... lines) throws Exception {
1621+
ev.setThreadOwner(keyForBuild(FAKE_LABEL));
16211622
ev.execAndExport(FAKE_LABEL, lines);
16221623
}
16231624

@@ -2906,7 +2907,7 @@ public void fancyExports() throws Exception {
29062907
assertThat(p.getKey()).isEqualTo(new StarlarkProvider.Key(keyForBuild(FAKE_LABEL), "p"));
29072908
assertThat(p1.getPrintableName()).isEqualTo("p1");
29082909
assertThat(p1.getKey()).isEqualTo(new StarlarkProvider.Key(keyForBuild(FAKE_LABEL), "p1"));
2909-
assertThat(a.getAspectClass()).isEqualTo(new StarlarkAspectClass(FAKE_LABEL, "a"));
2910+
assertThat(a.getAspectClass()).isEqualTo(new StarlarkAspectClass(keyForBuild(FAKE_LABEL), "a"));
29102911
}
29112912

29122913
@Test

0 commit comments

Comments
 (0)