Skip to content

Commit

Permalink
Move TypeMismatch to its own file and make it use TypeI. Needed so th…
Browse files Browse the repository at this point in the history
…at the new type inference can generate type mismatches.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149481216
  • Loading branch information
dimvar committed Mar 8, 2017
1 parent 43b0c7b commit 0108582
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 62 deletions.
1 change: 0 additions & 1 deletion src/com/google/javascript/jscomp/AbstractCompiler.java
Expand Up @@ -20,7 +20,6 @@
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; 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.deps.ModuleLoader;
import com.google.javascript.jscomp.parsing.Config; import com.google.javascript.jscomp.parsing.Config;
import com.google.javascript.jscomp.parsing.parser.trees.Comment; import com.google.javascript.jscomp.parsing.parser.trees.Comment;
Expand Down
12 changes: 6 additions & 6 deletions src/com/google/javascript/jscomp/AmbiguateProperties.java
Expand Up @@ -22,14 +22,14 @@
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; 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.AdjacencyGraph;
import com.google.javascript.jscomp.graph.Annotation; import com.google.javascript.jscomp.graph.Annotation;
import com.google.javascript.jscomp.graph.GraphColoring; import com.google.javascript.jscomp.graph.GraphColoring;
import com.google.javascript.jscomp.graph.GraphColoring.GreedyGraphColoring; import com.google.javascript.jscomp.graph.GraphColoring.GreedyGraphColoring;
import com.google.javascript.jscomp.graph.GraphNode; import com.google.javascript.jscomp.graph.GraphNode;
import com.google.javascript.jscomp.graph.SubGraph; import com.google.javascript.jscomp.graph.SubGraph;
import com.google.javascript.rhino.Node; 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.FunctionType;
import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative; import com.google.javascript.rhino.jstype.JSTypeNative;
Expand Down Expand Up @@ -119,7 +119,7 @@ public int compare(Property p1, Property p2) {
private Map<JSType, JSTypeBitSet> relatedBitsets = new HashMap<>(); private Map<JSType, JSTypeBitSet> relatedBitsets = new HashMap<>();


/** A set of types that invalidate properties from ambiguation. */ /** A set of types that invalidate properties from ambiguation. */
private final Set<JSType> invalidatingTypes; private final Set<TypeI> invalidatingTypes;


/** /**
* Prefix of properties to skip renaming. These should be renamed in the * Prefix of properties to skip renaming. These should be renamed in the
Expand All @@ -137,7 +137,7 @@ public int compare(Property p1, Property p2) {
this.reservedNonFirstCharacters = reservedNonFirstCharacters; this.reservedNonFirstCharacters = reservedNonFirstCharacters;


JSTypeRegistry r = compiler.getTypeRegistry(); JSTypeRegistry r = compiler.getTypeRegistry();
invalidatingTypes = new HashSet<>(ImmutableSet.of( invalidatingTypes = new HashSet<>(ImmutableSet.<TypeI>of(
r.getNativeType(JSTypeNative.ALL_TYPE), r.getNativeType(JSTypeNative.ALL_TYPE),
r.getNativeType(JSTypeNative.FUNCTION_FUNCTION_TYPE), r.getNativeType(JSTypeNative.FUNCTION_FUNCTION_TYPE),
r.getNativeType(JSTypeNative.FUNCTION_INSTANCE_TYPE), r.getNativeType(JSTypeNative.FUNCTION_INSTANCE_TYPE),
Expand Down Expand Up @@ -174,16 +174,16 @@ static AmbiguateProperties makePassForTesting(
/** /**
* Invalidates the given type, so that no properties on it will be renamed. * 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(); type = type.restrictByNotNullOrUndefined();
if (type.isUnionType()) { if (type.isUnionType()) {
for (JSType alt : type.toMaybeUnionType().getAlternatesWithoutStructuralTyping()) { for (TypeI alt : type.getUnionMembers()) {
addInvalidatingType(alt); addInvalidatingType(alt);
} }
} }


invalidatingTypes.add(type); invalidatingTypes.add(type);
ObjectType objType = ObjectType.cast(type); ObjectType objType = (ObjectType) type.toMaybeObjectType();
if (objType != null && objType.isInstanceType()) { if (objType != null && objType.isInstanceType()) {
invalidatingTypes.add(objType.getImplicitPrototype()); invalidatingTypes.add(objType.getImplicitPrototype());
} }
Expand Down
1 change: 0 additions & 1 deletion src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -31,7 +31,6 @@
import com.google.debugging.sourcemap.proto.Mapping.OriginalMapping; import com.google.debugging.sourcemap.proto.Mapping.OriginalMapping;
import com.google.javascript.jscomp.CompilerOptions.DevMode; import com.google.javascript.jscomp.CompilerOptions.DevMode;
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; 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.WarningsGuard.DiagnosticGroupState;
import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.deps.SortedDependencies.MissingProvideException; import com.google.javascript.jscomp.deps.SortedDependencies.MissingProvideException;
Expand Down
Expand Up @@ -25,7 +25,6 @@
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage; import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage;
import com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback; 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.StandardUnionFind;
import com.google.javascript.jscomp.graph.UnionFind; import com.google.javascript.jscomp.graph.UnionFind;
import com.google.javascript.rhino.FunctionTypeI; import com.google.javascript.rhino.FunctionTypeI;
Expand Down
13 changes: 6 additions & 7 deletions src/com/google/javascript/jscomp/InlineProperties.java
Expand Up @@ -18,16 +18,15 @@
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; 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.IR;
import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.TypeI;
import com.google.javascript.rhino.TypeIRegistry; import com.google.javascript.rhino.TypeIRegistry;
import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative; import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.ObjectType; import com.google.javascript.rhino.jstype.ObjectType;

import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
Expand Down Expand Up @@ -65,7 +64,7 @@ static class PropertyInfo {


private final Map<String, PropertyInfo> props = new HashMap<>(); private final Map<String, PropertyInfo> props = new HashMap<>();


private Set<JSType> invalidatingTypes; private Set<TypeI> invalidatingTypes;


InlineProperties(AbstractCompiler compiler) { InlineProperties(AbstractCompiler compiler) {
this.compiler = compiler; this.compiler = compiler;
Expand All @@ -78,7 +77,7 @@ static class PropertyInfo {
// we should move it to a common location. // we should move it to a common location.
private void buildInvalidatingTypeSet() { private void buildInvalidatingTypeSet() {
TypeIRegistry registry = compiler.getTypeIRegistry(); TypeIRegistry registry = compiler.getTypeIRegistry();
invalidatingTypes = new HashSet<>(ImmutableSet.of( invalidatingTypes = new HashSet<>(ImmutableSet.<TypeI>of(
(JSType) registry.getNativeType(JSTypeNative.ALL_TYPE), (JSType) registry.getNativeType(JSTypeNative.ALL_TYPE),
(JSType) registry.getNativeType(JSTypeNative.NO_OBJECT_TYPE), (JSType) registry.getNativeType(JSTypeNative.NO_OBJECT_TYPE),
(JSType) registry.getNativeType(JSTypeNative.NO_TYPE), (JSType) registry.getNativeType(JSTypeNative.NO_TYPE),
Expand Down Expand Up @@ -110,16 +109,16 @@ private void invalidateExternProperties() {
/** /**
* Invalidates the given type, so that no properties on it will be renamed. * 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(); type = type.restrictByNotNullOrUndefined();
if (type.isUnionType()) { if (type.isUnionType()) {
for (JSType alt : type.toMaybeUnionType().getAlternatesWithoutStructuralTyping()) { for (TypeI alt : type.getUnionMembers()) {
addInvalidatingType(alt); addInvalidatingType(alt);
} }
} }


invalidatingTypes.add(type); invalidatingTypes.add(type);
ObjectType objType = ObjectType.cast(type); ObjectType objType = (ObjectType) type.toMaybeObjectType();
if (objType != null && objType.isInstanceType()) { if (objType != null && objType.isInstanceType()) {
invalidatingTypes.add(objType.getImplicitPrototype()); invalidatingTypes.add(objType.getImplicitPrototype());
} }
Expand Down
60 changes: 60 additions & 0 deletions 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 + ")";
}
}
44 changes: 0 additions & 44 deletions src/com/google/javascript/jscomp/TypeValidator.java
Expand Up @@ -48,7 +48,6 @@
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable; import javax.annotation.Nullable;


/** /**
Expand Down Expand Up @@ -912,47 +911,4 @@ private JSError report(JSError error) {
compiler.report(error); compiler.report(error);
return 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 + ")";
}
}
} }
2 changes: 0 additions & 2 deletions test/com/google/javascript/jscomp/TypeValidatorTest.java
Expand Up @@ -22,12 +22,10 @@
import static com.google.javascript.rhino.jstype.JSTypeNative.STRING_TYPE; import static com.google.javascript.rhino.jstype.JSTypeNative.STRING_TYPE;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.TypeValidator.TypeMismatch;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative; import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.JSTypeRegistry; import com.google.javascript.rhino.jstype.JSTypeRegistry;

import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;


Expand Down

0 comments on commit 0108582

Please sign in to comment.