Skip to content

Commit

Permalink
Mark built-in providers as hashable
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.
  • Loading branch information
fmeum committed Jan 7, 2025
1 parent eca313a commit 480f8fb
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 @@ -78,6 +78,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 @@ -4025,4 +4026,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 480f8fb

Please sign in to comment.