diff --git a/src/com/google/javascript/jscomp/AbstractCompiler.java b/src/com/google/javascript/jscomp/AbstractCompiler.java index c175225d829..5198ef3274d 100644 --- a/src/com/google/javascript/jscomp/AbstractCompiler.java +++ b/src/com/google/javascript/jscomp/AbstractCompiler.java @@ -190,8 +190,6 @@ static enum MostRecentTypechecker { */ abstract CompilerPass getSymbolTable(); - abstract void setSymbolTable(CompilerPass symbolTable); - /** * Used by three passes that run in sequence (optimize-returns, * optimize-parameters, remove-unused-variables), to avoid having them diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index d0a6ad1a1c5..b4eb1f4c9e6 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -211,6 +211,9 @@ public SourceFile apply(String filename) { // Used by optimize-returns, optimize-parameters and remove-unused-variables private DefinitionUseSiteFinder defFinder = null; + // Types that have been forward declared + private final Set forwardDeclaredTypes = new HashSet<>(); + // For use by the new type inference private GlobalTypeInfo symbolTable; @@ -1237,20 +1240,14 @@ public TypeIRegistry getTypeIRegistry() { @Override public JSTypeRegistry getTypeRegistry() { if (typeRegistry == null) { - typeRegistry = new JSTypeRegistry(oldErrorReporter); + typeRegistry = new JSTypeRegistry(oldErrorReporter, forwardDeclaredTypes); } return typeRegistry; } @Override void forwardDeclareType(String typeName) { - // Always add it to the old type registry, since OTI runs after NTI to - // provide types for the remaining passes. - // TODO(dimvar): change this when we stop running OTI after NTI. - getTypeRegistry().forwardDeclareType(typeName); - if (this.options.getNewTypeInference()) { - getSymbolTable().addUnknownTypeName(typeName); - } + forwardDeclaredTypes.add(typeName); } @Override @@ -1352,18 +1349,11 @@ Iterable getImplicitInterfaceUses() { @Override GlobalTypeInfo getSymbolTable() { if (this.symbolTable == null) { - this.symbolTable = new GlobalTypeInfo(this); + this.symbolTable = new GlobalTypeInfo(this, forwardDeclaredTypes); } return this.symbolTable; } - @Override - void setSymbolTable(CompilerPass symbolTable) { - Preconditions.checkArgument( - symbolTable == null || symbolTable instanceof GlobalTypeInfo); - this.symbolTable = (GlobalTypeInfo) symbolTable; - } - @Override DefinitionUseSiteFinder getDefinitionFinder() { return this.defFinder; @@ -1531,7 +1521,7 @@ void orderInputs() { // Forward-declare all the provided types, so that they // are not flagged even if they are dropped from the process. for (String provide : input.getProvides()) { - getTypeRegistry().forwardDeclareType(provide); + forwardDeclareType(provide); } } diff --git a/src/com/google/javascript/jscomp/GlobalTypeInfo.java b/src/com/google/javascript/jscomp/GlobalTypeInfo.java index 74b492dba55..62d4f437f57 100644 --- a/src/com/google/javascript/jscomp/GlobalTypeInfo.java +++ b/src/com/google/javascript/jscomp/GlobalTypeInfo.java @@ -275,11 +275,12 @@ class GlobalTypeInfo implements CompilerPass, TypeIRegistry { private Map declaredObjLitProps = new LinkedHashMap<>(); private JSTypes commonTypes; - private Set unknownTypeNames = new LinkedHashSet<>(); + private final Set unknownTypeNames; - GlobalTypeInfo(AbstractCompiler compiler) { + GlobalTypeInfo(AbstractCompiler compiler, Set unknownTypeNames) { this.warnings = new WarningReporter(compiler); this.compiler = compiler; + this.unknownTypeNames = unknownTypeNames; this.convention = compiler.getCodingConvention(); this.varNameGen = new UniqueNameGenerator(); this.funNameGen = new DefaultNameGenerator(ImmutableSet.of(), "", null); @@ -310,10 +311,6 @@ JSType getPropDeclaredType(Node n) { return declaredObjLitProps.get(n); } - void addUnknownTypeName(String name) { - this.unknownTypeNames.add(name); - } - // Differs from the similar method in NTIScope class on how it treats qnames. String getFunInternalName(Node n) { Preconditions.checkArgument(n.isFunction()); @@ -337,7 +334,6 @@ public void process(Node externs, Node root) { this.globalScope = new NTIScope(root, null, ImmutableList.of(), commonTypes); this.globalScope.addUnknownTypeNames(this.unknownTypeNames); - this.unknownTypeNames = null; // Don't retain the LinkedHashSet scopes.add(this.globalScope); // Processing of a scope is split into many separate phases, and it's not diff --git a/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java b/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java index 374e9ec6989..7f5d1d47a6a 100644 --- a/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java +++ b/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java @@ -48,6 +48,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; import com.google.javascript.rhino.ErrorReporter; @@ -62,7 +63,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -140,7 +140,7 @@ public class JSTypeRegistry implements TypeIRegistry, Serializable { // Types that have been "forward-declared." // If these types are not declared anywhere in the binary, we shouldn't // try to type-check them at all. - private final Set forwardDeclaredTypes = new HashSet<>(); + private final Set forwardDeclaredTypes; // A map of properties to the types on which those properties have been // declared. @@ -178,12 +178,14 @@ public class JSTypeRegistry implements TypeIRegistry, Serializable { // there are no template types. private final TemplateTypeMap emptyTemplateTypeMap; - /** - * Constructs a new type registry populated with the built-in types. - */ - public JSTypeRegistry( - ErrorReporter reporter) { + public JSTypeRegistry(ErrorReporter reporter) { + this(reporter, ImmutableSet.of()); + } + + /** Constructs a new type registry populated with the built-in types. */ + public JSTypeRegistry(ErrorReporter reporter, Set forwardDeclaredTypes) { this.reporter = reporter; + this.forwardDeclaredTypes = forwardDeclaredTypes; this.emptyTemplateTypeMap = new TemplateTypeMap( this, ImmutableList.of(), ImmutableList.of()); nativeTypes = new JSType[JSTypeNative.values().length]; @@ -911,14 +913,6 @@ public void overwriteDeclaredType(String name, JSType t) { register(t, name); } - /** - * Records a forward-declared type name. We will not emit errors if this - * type name never resolves to anything. - */ - public void forwardDeclareType(String name) { - forwardDeclaredTypes.add(name); - } - /** * Whether this is a forward-declared type name. */ diff --git a/src/com/google/javascript/rhino/testing/BaseJSTypeTestCase.java b/src/com/google/javascript/rhino/testing/BaseJSTypeTestCase.java index b00578f0f3f..ac7a666d8f7 100644 --- a/src/com/google/javascript/rhino/testing/BaseJSTypeTestCase.java +++ b/src/com/google/javascript/rhino/testing/BaseJSTypeTestCase.java @@ -41,6 +41,7 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.FunctionBuilder; @@ -51,7 +52,6 @@ import com.google.javascript.rhino.jstype.ObjectType; import com.google.javascript.rhino.jstype.RecordTypeBuilder; import com.google.javascript.rhino.jstype.TemplatizedType; - import junit.framework.TestCase; public abstract class BaseJSTypeTestCase extends TestCase { @@ -119,7 +119,7 @@ public abstract class BaseJSTypeTestCase extends TestCase { protected void setUp() throws Exception { super.setUp(); errorReporter = new TestErrorReporter(null, null); - registry = new JSTypeRegistry(errorReporter); + registry = new JSTypeRegistry(errorReporter, ImmutableSet.of("forwardDeclared")); initTypes(); } diff --git a/test/com/google/javascript/rhino/jstype/JSTypeTest.java b/test/com/google/javascript/rhino/jstype/JSTypeTest.java index 5a9afe51de7..6c50387401b 100644 --- a/test/com/google/javascript/rhino/jstype/JSTypeTest.java +++ b/test/com/google/javascript/rhino/jstype/JSTypeTest.java @@ -168,7 +168,6 @@ public StaticTypedSlot getSlot(String name) { forwardDeclaredNamedType = new NamedType(registry, "forwardDeclared", "source", 1, 0); - registry.forwardDeclareType("forwardDeclared"); forwardDeclaredNamedType.resolve( new SimpleErrorReporter(), EMPTY_SCOPE); @@ -4974,9 +4973,8 @@ public void testNamedTypeEquals2() { */ public void testForwardDeclaredNamedTypeEquals() { // test == if references are equal - NamedType a = new NamedType(registry, "typeA", "source", 1, 0); - NamedType b = new NamedType(registry, "typeA", "source", 1, 0); - registry.forwardDeclareType("typeA"); + NamedType a = new NamedType(registry, "forwardDeclared", "source", 1, 0); + NamedType b = new NamedType(registry, "forwardDeclared", "source", 1, 0); assertTypeEquals(a, b); @@ -4995,8 +4993,7 @@ public void testForwardDeclaredNamedTypeEquals() { } public void testForwardDeclaredNamedType() { - NamedType a = new NamedType(registry, "typeA", "source", 1, 0); - registry.forwardDeclareType("typeA"); + NamedType a = new NamedType(registry, "forwardDeclared", "source", 1, 0); assertTypeEquals(UNKNOWN_TYPE, a.getLeastSupertype(UNKNOWN_TYPE)); assertTypeEquals(CHECKED_UNKNOWN_TYPE,