Skip to content

Commit

Permalink
Implement equals() and hashcode() for StarlarkSubrule
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 675039189
Change-Id: I8d1a3180f7b16d10447485ee100adf4b2b9a97b1
  • Loading branch information
hvadehra authored and copybara-github committed Sep 16, 2024
1 parent 7b8d072 commit 6c1fe0b
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkSubruleApi;
import com.google.devtools.build.lib.starlarkbuildapi.platform.ToolchainContextApi;
import com.google.devtools.build.lib.util.Pair;
import java.util.Objects;
import javax.annotation.Nullable;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class StarlarkSubrule implements StarlarkExportable, StarlarkCallable, St
private final ImmutableSet<StarlarkSubrule> subrules;

// following fields are set on export
@Nullable private Label extensionLabel = null;
@Nullable private String exportedName = null;
private ImmutableList<SubruleAttribute> attributes;

Expand Down Expand Up @@ -107,6 +109,26 @@ public void repr(Printer printer) {
printer.append("<subrule ").append(getName()).append(">");
}

@Override
public boolean equals(Object other) {
if (!(other instanceof StarlarkSubrule)) {
return false;
}
if (isExported()) {
return this.extensionLabel.equals(((StarlarkSubrule) other).extensionLabel)
&& this.exportedName.equals(((StarlarkSubrule) other).exportedName);
}
return this == other;
}

@Override
public int hashCode() {
if (isExported()) {
return Objects.hash(this.extensionLabel, this.exportedName);
}
return System.identityHashCode(this);
}

@Override
public Object call(StarlarkThread thread, Tuple args, Dict<String, Object> kwargs)
throws EvalException, InterruptedException {
Expand Down Expand Up @@ -221,12 +243,13 @@ private void checkExported() throws EvalException {

@Override
public boolean isExported() {
return this.exportedName != null;
return this.extensionLabel != null && this.exportedName != null;
}

@Override
public void export(EventHandler handler, Label extensionLabel, String exportedName) {
Preconditions.checkState(!isExported());
this.extensionLabel = extensionLabel;
this.exportedName = exportedName;
this.attributes =
SubruleAttribute.transformOnExport(attributes, extensionLabel, exportedName, handler);
Expand Down Expand Up @@ -343,7 +366,7 @@ public ToolchainContextApi toolchains() throws EvalException {
if (ruleContext.useAutoExecGroups()) {
return StarlarkToolchainContext.create(
/* targetDescription= */ ruleContext.getToolchainContext().targetDescription(),
/* resolveToolchainInfoFunc= */ ruleContext::getToolchainInfo,
/* resolveToolchainDataFunc= */ ruleContext::getToolchainInfo,
/* resolvedToolchainTypeLabels= */ getAutomaticExecGroupLabels());
} else {
throw Starlark.errorf("subrules using toolchains must enable automatic exec-groups");
Expand Down

0 comments on commit 6c1fe0b

Please sign in to comment.