diff --git a/src/com/google/javascript/jscomp/AbstractCompiler.java b/src/com/google/javascript/jscomp/AbstractCompiler.java index 9c2f358604c..f08ed6e8c24 100644 --- a/src/com/google/javascript/jscomp/AbstractCompiler.java +++ b/src/com/google/javascript/jscomp/AbstractCompiler.java @@ -20,7 +20,6 @@ import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; -import com.google.javascript.jscomp.TypeValidator.TypeMismatch; import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.parsing.Config; import com.google.javascript.jscomp.parsing.parser.trees.Comment; diff --git a/src/com/google/javascript/jscomp/AmbiguateProperties.java b/src/com/google/javascript/jscomp/AmbiguateProperties.java index 0efd7e1ac5c..ecc086110a8 100644 --- a/src/com/google/javascript/jscomp/AmbiguateProperties.java +++ b/src/com/google/javascript/jscomp/AmbiguateProperties.java @@ -22,7 +22,6 @@ import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; -import com.google.javascript.jscomp.TypeValidator.TypeMismatch; import com.google.javascript.jscomp.graph.AdjacencyGraph; import com.google.javascript.jscomp.graph.Annotation; import com.google.javascript.jscomp.graph.GraphColoring; @@ -30,6 +29,7 @@ import com.google.javascript.jscomp.graph.GraphNode; import com.google.javascript.jscomp.graph.SubGraph; import com.google.javascript.rhino.Node; +import com.google.javascript.rhino.TypeI; import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSTypeNative; @@ -119,7 +119,7 @@ public int compare(Property p1, Property p2) { private Map relatedBitsets = new HashMap<>(); /** A set of types that invalidate properties from ambiguation. */ - private final Set invalidatingTypes; + private final Set invalidatingTypes; /** * Prefix of properties to skip renaming. These should be renamed in the @@ -137,7 +137,7 @@ public int compare(Property p1, Property p2) { this.reservedNonFirstCharacters = reservedNonFirstCharacters; JSTypeRegistry r = compiler.getTypeRegistry(); - invalidatingTypes = new HashSet<>(ImmutableSet.of( + invalidatingTypes = new HashSet<>(ImmutableSet.of( r.getNativeType(JSTypeNative.ALL_TYPE), r.getNativeType(JSTypeNative.FUNCTION_FUNCTION_TYPE), r.getNativeType(JSTypeNative.FUNCTION_INSTANCE_TYPE), @@ -174,16 +174,16 @@ static AmbiguateProperties makePassForTesting( /** * Invalidates the given type, so that no properties on it will be renamed. */ - private void addInvalidatingType(JSType type) { + private void addInvalidatingType(TypeI type) { type = type.restrictByNotNullOrUndefined(); if (type.isUnionType()) { - for (JSType alt : type.toMaybeUnionType().getAlternatesWithoutStructuralTyping()) { + for (TypeI alt : type.getUnionMembers()) { addInvalidatingType(alt); } } invalidatingTypes.add(type); - ObjectType objType = ObjectType.cast(type); + ObjectType objType = (ObjectType) type.toMaybeObjectType(); if (objType != null && objType.isInstanceType()) { invalidatingTypes.add(objType.getImplicitPrototype()); } diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 6852c280281..83bf94036a1 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -31,7 +31,6 @@ import com.google.debugging.sourcemap.proto.Mapping.OriginalMapping; import com.google.javascript.jscomp.CompilerOptions.DevMode; import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; -import com.google.javascript.jscomp.TypeValidator.TypeMismatch; import com.google.javascript.jscomp.WarningsGuard.DiagnosticGroupState; import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.deps.SortedDependencies.MissingProvideException; diff --git a/src/com/google/javascript/jscomp/DisambiguateProperties.java b/src/com/google/javascript/jscomp/DisambiguateProperties.java index 8c865a58178..65969068184 100644 --- a/src/com/google/javascript/jscomp/DisambiguateProperties.java +++ b/src/com/google/javascript/jscomp/DisambiguateProperties.java @@ -25,7 +25,6 @@ import com.google.common.collect.Multimap; import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage; import com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback; -import com.google.javascript.jscomp.TypeValidator.TypeMismatch; import com.google.javascript.jscomp.graph.StandardUnionFind; import com.google.javascript.jscomp.graph.UnionFind; import com.google.javascript.rhino.FunctionTypeI; diff --git a/src/com/google/javascript/jscomp/InlineProperties.java b/src/com/google/javascript/jscomp/InlineProperties.java index 9682711c518..2ed2d6e3f7c 100644 --- a/src/com/google/javascript/jscomp/InlineProperties.java +++ b/src/com/google/javascript/jscomp/InlineProperties.java @@ -18,16 +18,15 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; -import com.google.javascript.jscomp.TypeValidator.TypeMismatch; import com.google.javascript.rhino.IR; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.Node; +import com.google.javascript.rhino.TypeI; import com.google.javascript.rhino.TypeIRegistry; import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSTypeNative; import com.google.javascript.rhino.jstype.ObjectType; - import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -65,7 +64,7 @@ static class PropertyInfo { private final Map props = new HashMap<>(); - private Set invalidatingTypes; + private Set invalidatingTypes; InlineProperties(AbstractCompiler compiler) { this.compiler = compiler; @@ -78,7 +77,7 @@ static class PropertyInfo { // we should move it to a common location. private void buildInvalidatingTypeSet() { TypeIRegistry registry = compiler.getTypeIRegistry(); - invalidatingTypes = new HashSet<>(ImmutableSet.of( + invalidatingTypes = new HashSet<>(ImmutableSet.of( (JSType) registry.getNativeType(JSTypeNative.ALL_TYPE), (JSType) registry.getNativeType(JSTypeNative.NO_OBJECT_TYPE), (JSType) registry.getNativeType(JSTypeNative.NO_TYPE), @@ -110,16 +109,16 @@ private void invalidateExternProperties() { /** * Invalidates the given type, so that no properties on it will be renamed. */ - private void addInvalidatingType(JSType type) { + private void addInvalidatingType(TypeI type) { type = type.restrictByNotNullOrUndefined(); if (type.isUnionType()) { - for (JSType alt : type.toMaybeUnionType().getAlternatesWithoutStructuralTyping()) { + for (TypeI alt : type.getUnionMembers()) { addInvalidatingType(alt); } } invalidatingTypes.add(type); - ObjectType objType = ObjectType.cast(type); + ObjectType objType = (ObjectType) type.toMaybeObjectType(); if (objType != null && objType.isInstanceType()) { invalidatingTypes.add(objType.getImplicitPrototype()); } diff --git a/src/com/google/javascript/jscomp/TypeMismatch.java b/src/com/google/javascript/jscomp/TypeMismatch.java new file mode 100644 index 00000000000..e2e30b86d6a --- /dev/null +++ b/src/com/google/javascript/jscomp/TypeMismatch.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 The Closure Compiler Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.javascript.jscomp; + +import com.google.javascript.rhino.TypeI; +import java.util.Objects; + +/** + * Signals that the first type and the second type have been + * used interchangeably. + * + * Type-based optimizations should take this into account + * so that they don't wreck code with type warnings. + */ +class TypeMismatch { + final TypeI typeA; + final TypeI typeB; + final JSError src; + + /** + * It's the responsibility of the class that creates the + * {@code TypeMismatch} to ensure that {@code a} and {@code b} are + * non-matching types. + */ + TypeMismatch(TypeI a, TypeI b, JSError src) { + this.typeA = a; + this.typeB = b; + this.src = src; + } + + @Override public boolean equals(Object object) { + if (object instanceof TypeMismatch) { + TypeMismatch that = (TypeMismatch) object; + return (that.typeA.equals(this.typeA) && that.typeB.equals(this.typeB)) + || (that.typeB.equals(this.typeA) && that.typeA.equals(this.typeB)); + } + return false; + } + + @Override public int hashCode() { + return Objects.hash(typeA, typeB); + } + + @Override public String toString() { + return "(" + typeA + ", " + typeB + ")"; + } +} diff --git a/src/com/google/javascript/jscomp/TypeValidator.java b/src/com/google/javascript/jscomp/TypeValidator.java index 15e6073f92b..a1903f90537 100644 --- a/src/com/google/javascript/jscomp/TypeValidator.java +++ b/src/com/google/javascript/jscomp/TypeValidator.java @@ -48,7 +48,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import javax.annotation.Nullable; /** @@ -912,47 +911,4 @@ private JSError report(JSError error) { compiler.report(error); return error; } - - /** - * Signals that the first type and the second type have been - * used interchangeably. - * - * Type-based optimizations should take this into account - * so that they don't wreck code with type warnings. - */ - static class TypeMismatch { - final JSType typeA; - final JSType typeB; - final JSError src; - - /** - * It's the responsibility of the class that creates the - * {@code TypeMismatch} to ensure that {@code a} and {@code b} are - * non-matching types. - */ - TypeMismatch(JSType a, JSType b, JSError src) { - this.typeA = a; - this.typeB = b; - this.src = src; - } - - @Override public boolean equals(Object object) { - if (object instanceof TypeMismatch) { - TypeMismatch that = (TypeMismatch) object; - return (that.typeA.isEquivalentTo(this.typeA) - && that.typeB.isEquivalentTo(this.typeB)) - || (that.typeB.isEquivalentTo(this.typeA) - && that.typeA.isEquivalentTo(this.typeB)); - } - return false; - } - - @Override public int hashCode() { - return Objects.hash(typeA, typeB); - } - - @Override public String toString() { - return "(" + typeA + ", " + typeB + ")"; - } - } } diff --git a/test/com/google/javascript/jscomp/TypeValidatorTest.java b/test/com/google/javascript/jscomp/TypeValidatorTest.java index 64e4ab20c82..e02fdd0ee97 100644 --- a/test/com/google/javascript/jscomp/TypeValidatorTest.java +++ b/test/com/google/javascript/jscomp/TypeValidatorTest.java @@ -22,12 +22,10 @@ import static com.google.javascript.rhino.jstype.JSTypeNative.STRING_TYPE; import com.google.common.collect.ImmutableList; -import com.google.javascript.jscomp.TypeValidator.TypeMismatch; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSTypeNative; import com.google.javascript.rhino.jstype.JSTypeRegistry; - import java.util.Collections; import java.util.List;