-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reject hashing of frozen mutable types #8980
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,7 +222,7 @@ public Runtime.NoneType update( | |
SkylarkDict<K, V> dict = | ||
(args instanceof SkylarkDict) ? (SkylarkDict<K, V>) args : getDictFromArgs(args, loc, env); | ||
dict = SkylarkDict.plus(dict, (SkylarkDict<K, V>) kwargs, env); | ||
putAll(dict, loc, env.mutability()); | ||
putAll(dict, loc, env.mutability(), env); | ||
return Runtime.NONE; | ||
} | ||
|
||
|
@@ -355,7 +355,7 @@ protected Map<K, V> getContentsUnsafe() { | |
*/ | ||
public void put(K key, V value, Location loc, Mutability mutability) throws EvalException { | ||
checkMutable(loc, mutability); | ||
EvalUtils.checkValidDictKey(key); | ||
EvalUtils.checkValidDictKey(key, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this function (put) is only used here: https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/analysis/skylark/FunctionTransitionUtil.java#L178 |
||
contents.put(key, value); | ||
} | ||
|
||
|
@@ -365,7 +365,9 @@ public void put(K key, V value, Location loc, Mutability mutability) throws Eval | |
*/ | ||
// TODO(bazel-team): Decide whether to eliminate this overload. | ||
public void put(K key, V value, Location loc, Environment env) throws EvalException { | ||
put(key, value, loc, env.mutability()); | ||
checkMutable(loc, mutability); | ||
EvalUtils.checkValidDictKey(key, env); | ||
contents.put(key, value); | ||
} | ||
|
||
/** | ||
|
@@ -377,11 +379,11 @@ public void put(K key, V value, Location loc, Environment env) throws EvalExcept | |
* @throws EvalException if some key is invalid or the dict is frozen | ||
*/ | ||
public <KK extends K, VV extends V> void putAll( | ||
Map<KK, VV> map, Location loc, Mutability mutability) throws EvalException { | ||
Map<KK, VV> map, Location loc, Mutability mutability, Environment env) throws EvalException { | ||
checkMutable(loc, mutability); | ||
for (Map.Entry<KK, VV> e : map.entrySet()) { | ||
KK k = e.getKey(); | ||
EvalUtils.checkValidDictKey(k); | ||
EvalUtils.checkValidDictKey(k, env); | ||
contents.put(k, e.getValue()); | ||
} | ||
} | ||
|
@@ -508,7 +510,7 @@ public final boolean containsKey(Object key, Location loc, StarlarkContext conte | |
@Override | ||
public final boolean containsKey(Object key, Location loc, Environment env) throws EvalException { | ||
if (env.getSemantics().incompatibleDisallowDictLookupUnhashableKeys()) { | ||
EvalUtils.checkValidDictKey(key); | ||
EvalUtils.checkValidDictKey(key, env); | ||
} | ||
return this.containsKey(key); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3160,6 +3160,52 @@ public void testSplitEmptySeparator() throws Exception { | |
getConfiguredTarget("//test:r"); | ||
} | ||
|
||
@Test | ||
public void testHashFrozenList() throws Exception { | ||
setSkylarkSemanticsOptions("--incompatible_disallow_hashing_frozen_mutables=false"); | ||
scratch.file("test/extension.bzl", "y = []"); | ||
|
||
scratch.file( | ||
"test/BUILD", | ||
"load('//test:extension.bzl', 'y')", | ||
"{y: 1}", | ||
"cc_library(name = 'r')"); | ||
|
||
getConfiguredTarget("//test:r"); | ||
} | ||
|
||
@Test | ||
public void testHashFrozenListForbidden() throws Exception { | ||
setSkylarkSemanticsOptions("--incompatible_disallow_hashing_frozen_mutables=true"); | ||
scratch.file("test/extension.bzl", "y = []"); | ||
|
||
scratch.file( | ||
"test/BUILD", | ||
"load('//test:extension.bzl', 'y')", | ||
"{y: 1}", | ||
"cc_library(name = 'r')"); | ||
|
||
reporter.removeHandler(failFastHandler); | ||
getConfiguredTarget("//test:r"); | ||
assertContainsEvent("unhashable type: 'list'"); | ||
} | ||
|
||
@Test | ||
public void testHashFrozenDeepMutableForbidden() throws Exception { | ||
setSkylarkSemanticsOptions("--incompatible_disallow_hashing_frozen_mutables=true"); | ||
scratch.file("test/extension.bzl", "y = {}"); | ||
|
||
scratch.file( | ||
"test/BUILD", | ||
"load('//test:extension.bzl', 'y')", | ||
"{('a', (y,), True): None}", | ||
"cc_library(name = 'r')"); | ||
|
||
reporter.removeHandler(failFastHandler); | ||
getConfiguredTarget("//test:r"); | ||
assertContainsEvent("unhashable type: 'tuple'"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error message is a bit confusing, implying that tuples are never hashable. Would it be feasible to change it to mention the actual unhashable type within this PR? The similar error in Python is "unhashable type: 'dict'". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to submit this PR first (and hopefully be included in Bazel 0.29) and iterate later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is really the object that is unhashable rather than the type, how about changing the message to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw the same happens, regardless of this PR, if you try something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like @aiuto's suggestion. |
||
} | ||
|
||
@Test | ||
public void testNoOutputsError() throws Exception { | ||
scratch.file( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi, I'm removing this page. Instead we just use GitHub issues to track the changes.