Skip to content

Commit

Permalink
Flip the flag "--incompatible_static_name_resolution"
Browse files Browse the repository at this point in the history
This is an incompatible change, but it breaks in a single case:

  global = 2

  def fct(x):
    if x:
      global = 3

    print(global)

Before the change, `global` would refer to either the global variable
or the local variable, depending on the value of `x`. After the change,
it is either `3` or undefined (runtime error).

Fixes #5637

RELNOTES: --incompatible_static_name_resolution is no unable by default
PiperOrigin-RevId: 222242205
  • Loading branch information
laurentlb authored and Copybara-Service committed Nov 20, 2018
1 parent 1a8eb1e commit 3a979f7
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion site/docs/skylark/backward-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ for background and examples.
The proposal is not fully implemented yet.

* Flag: `--incompatible_static_name_resolution`
* Default: `false`
* Default: `true`
* Tracking issue: [#5637](https://github.com/bazelbuild/bazel/issues/5637)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public class SkylarkSemanticsOptions extends OptionsBase implements Serializable

@Option(
name = "incompatible_static_name_resolution",
defaultValue = "false",
defaultValue = "true",
documentationCategory = OptionDocumentationCategory.SKYLARK_SEMANTICS,
effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
metadataTags = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static Builder builderWithDefaults() {
.incompatibleRangeType(true)
.incompatibleRemoveNativeGitRepository(true)
.incompatibleRemoveNativeHttpArchive(true)
.incompatibleStaticNameResolution(false)
.incompatibleStaticNameResolution(true)
.incompatibleStricArgumentOrdering(false)
.incompatibleStringIsNotIterable(false)
.internalSkylarkFlagTestCanary(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void testVariableIsReferencedBeforeAssignment() throws Exception {
} catch (EvalExceptionWithStackTrace e) {
assertThat(e)
.hasMessageThat()
.contains("Variable 'global_var' is referenced before assignment.");
.contains("local variable 'global_var' is referenced before assignment.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,13 @@ public void testListComprehensionsWithFiltering() throws Exception {

@Test
public void testListComprehensionDefinitionOrder() throws Exception {
newTest().testIfErrorContains("name 'y' is not defined",
"[x for x in (1, 2) if y for y in (3, 4)]");
new BuildTest()
.testIfErrorContains("name 'y' is not defined", "[x for x in (1, 2) if y for y in (3, 4)]");

new SkylarkTest()
.testIfErrorContains(
"local variable 'y' is referenced before assignment",
"[x for x in (1, 2) if y for y in (3, 4)]");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public void testFunctionDefLocalGlobalScope() throws Exception {

@Test
public void testFunctionDefLocalVariableReferencedBeforeAssignment() throws Exception {
checkEvalErrorContains("Variable 'a' is referenced before assignment.",
checkEvalErrorContains(
"local variable 'a' is referenced before assignment.",
"a = 1",
"def func():",
" b = a",
Expand All @@ -116,7 +117,8 @@ public void testFunctionDefLocalVariableReferencedBeforeAssignment() throws Exce

@Test
public void testFunctionDefLocalVariableReferencedInCallBeforeAssignment() throws Exception {
checkEvalErrorContains("Variable 'a' is referenced before assignment.",
checkEvalErrorContains(
"local variable 'a' is referenced before assignment.",
"def dummy(x):",
" pass",
"a = 1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ public void testFunctionCallOrdering() throws Exception {
public void testFunctionCallBadOrdering() throws Exception {
new SkylarkTest()
.testIfErrorContains(
"name 'foo' is not defined",
"global variable 'foo' is referenced before assignment.",
"def func(): return foo() * 2",
"x = func()",
"def foo(): return 2");
Expand Down Expand Up @@ -2207,7 +2207,9 @@ public void testStructMethodNotDefined() throws Exception {
@Test
public void testListComprehensionsDoNotLeakVariables() throws Exception {
checkEvalErrorContains(
"name 'a' is not defined",
// TODO(laurentlb): This happens because the variable gets undefined after the list
// comprehension. We should do better.
"local variable 'a' is referenced before assignment.",
"def foo():",
" a = 10",
" b = [a for a in range(3)]",
Expand Down

0 comments on commit 3a979f7

Please sign in to comment.