Skip to content

Commit

Permalink
[8.1.0] Mark built-in providers as hashable (#25230)
Browse files Browse the repository at this point in the history
Built-in providers always have an immutable `hashCode`. Making them
hashable makes it easier to deduplicate a list of providers, which is
sometimes required as the list of provider instances returned by a rule
must not contain two instances for a given provider.

Starlark-defined providers are already hashable, so this brings the
behavior of built-in providers in line with them.

Closes #24848.

PiperOrigin-RevId: 724370319
Change-Id: I68147b1f707249a8b09ad170c5fc5b3da4776ccf

Commit
c94a9bd

Co-authored-by: Fabian Meumertzheim <[email protected]>
  • Loading branch information
bazel-io and fmeum authored Feb 11, 2025
1 parent 9209fd5 commit 3b0d75a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public final int hashCode() {
return getClass().hashCode();
}

@Override
public void checkHashable() {
// The hash code is based on the class, so it is hashable.
}

@Override
public boolean isExported() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import java.util.regex.Pattern;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Mutability;
import net.starlark.java.eval.Printer;
Expand Down Expand Up @@ -4154,4 +4155,11 @@ def _impl(ctx):
assertThat(a2.getRoot().getExecPathString())
.matches(getRelativeOutputPath() + "/[\\w\\-]+\\-exec/bin");
}

@Test
public void testHashableProviders() throws Exception {
ev.execAndExport("p = provider()");
Dict<?, ?> dict = (Dict<?, ?>) ev.eval("{k: None for k in [DefaultInfo, p, DefaultInfo, p]}");
assertThat(dict.size()).isEqualTo(2);
}
}

0 comments on commit 3b0d75a

Please sign in to comment.