diff --git a/compiler/java/com/google/dart/compiler/backend/js/GenerateNamesAndScopes.java b/compiler/java/com/google/dart/compiler/backend/js/GenerateNamesAndScopes.java index 84fd758ba57..771f984ca04 100644 --- a/compiler/java/com/google/dart/compiler/backend/js/GenerateNamesAndScopes.java +++ b/compiler/java/com/google/dart/compiler/backend/js/GenerateNamesAndScopes.java @@ -49,7 +49,8 @@ class GenerateNamesAndScopes extends NormalizedVisitor { private final Deque scopes = new LinkedList(); private DartClass currentClass = null; - private int nextLabelId = 0; + private int labelUniqifier = 0; // to resolve label name collisions. + private int varUniqifier = 0; // to resolve variable name collisions. private final TranslationContext translationContext; private final LibraryElement unitLibrary; @@ -124,20 +125,20 @@ class GenerateNamesAndScopes extends NormalizedVisitor { public boolean visit(DartParameter x, DartContext ctx) { // TODO(ngeoffray): A parameter in a function type does not have a symbol. if (x.getSymbol() != null) { - declare(x.getSymbol(), x.getParameterName()); + declareExclusively(x.getSymbol(), x.getParameterName()); } return true; } @Override public boolean visit(DartVariable x, DartContext ctx) { - declare(x.getSymbol(), x.getVariableName()); + declareExclusively(x.getSymbol(), x.getVariableName()); return true; } @Override public boolean visit(DartLabel x, DartContext ctx) { - declare(x.getSymbol(), "L" + nextLabelId++); + declareExclusively(x.getSymbol(), String.format("L%X", labelUniqifier++)); return true; } @@ -163,7 +164,7 @@ class GenerateNamesAndScopes extends NormalizedVisitor { } private JsName function(Symbol symbol, String name, String originalName, DartFunction func) { - JsName jsName = name != null ? declare(symbol, name, originalName) : null; + JsName jsName = name != null ? declareExclusively(symbol, name, originalName) : null; JsFunction jsFunc = new JsFunction(scopes.peek(), jsName); jsFunc.setFromDart(true); scopes.push(jsFunc.getScope()); @@ -175,8 +176,32 @@ class GenerateNamesAndScopes extends NormalizedVisitor { return declareInScope(scopes.peek(), x, name, originalName); } - private JsName declare(Symbol x, String name) { - return declareInScope(scopes.peek(), x, name, name); + private JsName declareExclusively(Symbol x, String name, String originalName) { + return declareExclusivelyInScope(scopes.peek(), x, name, originalName); + } + + private JsName declareExclusively(Symbol x, String name) { + return declareExclusivelyInScope(scopes.peek(), x, name, name); + } + + private static final int BIG_PRIME_UNDER_0XFFFFF = 985531; + + /** + * Create a unique name for this variable in this scope. + * + * Try to keep this from being a linear scan of the namespace, and keep + * it under 5 hex digits (over 1,000,000 unique suffixes). + * + */ + private JsName declareExclusivelyInScope(JsScope scope, Symbol x, + String name, String originalName) { + String mappedName = name; + int offset = 0; + while (scope.findExistingName(mappedName) != null) { + mappedName = String.format("%s_%X", mappedName, varUniqifier); + varUniqifier = (varUniqifier + offset++) % BIG_PRIME_UNDER_0XFFFFF; + } + return declareInScope(scope, x, mappedName, originalName); } private JsName declareInScope(JsScope scope, Symbol x, String name, String originalName) { diff --git a/tests/co19/co19-compiler.status b/tests/co19/co19-compiler.status index 2f011641b5b..04ec4d44541 100644 --- a/tests/co19/co19-compiler.status +++ b/tests/co19/co19-compiler.status @@ -137,9 +137,7 @@ LangGuideTest/02_Language_Constructs/02_1_Class/02_1_Class_Construction/A13/t01: LangGuideTest/02_Language_Constructs/02_1_Class/02_1_Class_Construction/A18/t01: Fail # Bug 5371670. LangGuideTest/02_Language_Constructs/02_2_Interface/A02/t03: Fail # Bug 5371670. LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/Examples/A02/t01: Fail # Bug 5371670. -LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/Shadowing_and_Hiding_Names/A01/t01: Fail # Bug 5371670. -LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/Shadowing_and_Hiding_Names/A02/t01: Fail # Bug 5371670. -LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/Shadowing_and_Hiding_Names/A01/t02: Fail # Bug 5371670. + LangGuideTest/02_Language_Constructs/02_7_Abstract_Methods/A02/t01: Fail # Bug 5371670. LangGuideTest/02_Language_Constructs/02_8_Static_Methods/A02/t02: Fail # Bug 5371670. LangGuideTest/07_Overriding/A02/t01: Fail # Bug 5371670. @@ -253,7 +251,9 @@ LangGuideTest/02_Language_Constructs/02_11_Exceptions/A05/t01: Fail LangGuideTest/02_Language_Constructs/02_1_Class/02_1_Class_Construction/A05/t01: Fail LangGuideTest/02_Language_Constructs/02_1_Class/02_1_Class_Construction/A12/t02: Fail LangGuideTest/02_Language_Constructs/02_1_Class/A02/t04: Skip # Times out. -LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/A02/t02: Fail +LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/Shadowing_and_Hiding_Names/A01/t01: Fail +LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/Shadowing_and_Hiding_Names/A02/t01: Fail +LangGuideTest/02_Language_Constructs/02_5_Meaning_of_Names/Shadowing_and_Hiding_Names/A01/t02: Fail LangGuideTest/02_Language_Constructs/02_6_Functions/A04/t01: Fail LangGuideTest/02_Language_Constructs/02_6_Functions/A06/t01: Fail LangGuideTest/02_Language_Constructs/02_6_Functions/A06/t02: Fail diff --git a/tests/language/language.status b/tests/language/language.status index f887d584cd5..87625558b30 100644 --- a/tests/language/language.status +++ b/tests/language/language.status @@ -67,7 +67,6 @@ BadNamedParametersTest: Fail # Bug Bug 4202974 - release mode is not throwing Prefix11NegativeTest: Fail # Bug 5406175 Prefix12NegativeTest: Fail # Bug 5406175 FunctionTypeParameterNegativeTest: Fail # Bug 4568007 -ImplicitScopeTest: FAIL # Nested statements can be declarations ConstConstructor1NegativeTest: FAIL # 5142545 ConstConstructor2NegativeTest: FAIL # 5142545 MathTest: FAIL # 5165080 @@ -80,7 +79,6 @@ NamedParameters2NegativeTest: Fail # Implementation in progress. NamedParameters3NegativeTest: Fail # Implementation in progress. NamedParameters4NegativeTest: Fail # Implementation in progress. NamedParameters6NegativeTest: Crash # Implementation in progress. -ScopeVariableTest: Fail # 5244704 InstFieldInitializerTest: Fail # Cannot deal with static final values in const expression. RegExp3Test: Fail # 5299683 InterfaceFactory3NegativeTest: Fail # 5387405