Skip to content

Commit

Permalink
Skylark: Allow builtin functions to be shadowed.
Browse files Browse the repository at this point in the history
This change makes them consistent with global variables.
e.g.
  def foo(len): return len + 1  # now allowed

Redefinition is still forbidden.

--
MOS_MIGRATED_REVID=89383535
  • Loading branch information
laurentlb authored and hanwen committed Mar 24, 2015
1 parent adeef73 commit 8fc603c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,10 @@ public ValidationEnvironment clone() {
* Creates a local ValidationEnvironment to validate user defined function bodies.
*/
public ValidationEnvironment(ValidationEnvironment parent, SkylarkFunctionType currentFunction) {
// Don't copy readOnlyVariables: Variables may shadow global values.
this.parent = parent;
this.variableTypes.put(SkylarkType.GLOBAL, new HashMap<String, SkylarkType>());
this.currentFunction = currentFunction;
for (String var : parent.readOnlyVariables) {
if (!parent.variableLocations.containsKey(var)) {
// Mark built in global vars readonly. Variables defined in Skylark may be shadowed locally.
readOnlyVariables.add(var);
}
}
this.clonable = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,22 +624,19 @@ public void testReadOnlyWorksAfterNestedBranch2() {

@Test
public void testModulesReadOnlyInFuncDefBody() {
checkError("Variable cmd_helper is read only",
"def func():",
parse("def func():",
" cmd_helper = set()");
}

@Test
public void testBuiltinGlobalFunctionsReadOnlyInFuncDefBody() {
checkError("Variable rule is read only",
"def func():",
parse("def func():",
" rule = 'abc'");
}

@Test
public void testBuiltinGlobalFunctionsReadOnlyAsFuncDefArg() {
checkError("Variable rule is read only",
"def func(rule):",
parse("def func(rule):",
" return rule");
}

Expand Down

0 comments on commit 8fc603c

Please sign in to comment.