Skip to content

Commit

Permalink
Instantiate to ? when joining with empty typemap, to avoid somewhat d…
Browse files Browse the repository at this point in the history
…ubious warnings.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159874119
  • Loading branch information
dimvar authored and blickly committed Jun 22, 2017
1 parent e1eb95d commit c20dc57
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/com/google/javascript/jscomp/newtypes/NominalType.java
Expand Up @@ -580,7 +580,7 @@ private boolean instantiationIsUnknownOrIdentity() {
return true;
}

private static ImmutableMap<String, JSType> joinTypeMaps(NominalType nt1, NominalType nt2) {
private static NominalType joinTypeMaps(NominalType nt1, NominalType nt2) {
checkState(nt1.rawType.equals(nt2.rawType));
ImmutableMap.Builder<String, JSType> builder = ImmutableMap.builder();
if (nt1.isIObject()) {
Expand All @@ -589,18 +589,20 @@ private static ImmutableMap<String, JSType> joinTypeMaps(NominalType nt1, Nomina
builder.put(indexTypevar, JSType.meet(nt1.getIndexType(), nt2.getIndexType()));
String indexedTypevar = nt1.rawType.getTypeParameters().get(1);
builder.put(indexedTypevar, JSType.join(nt1.getIndexedType(), nt2.getIndexedType()));
return builder.build();
return new NominalType(builder.build(), nt1.rawType);
}
if (nt1.typeMap.isEmpty() || nt2.typeMap.isEmpty()) {
return ImmutableMap.of();
return nt1.instantiateGenericsWithUnknown();
}
for (String typevar : nt1.typeMap.keySet()) {
builder.put(typevar, JSType.join(nt1.typeMap.get(typevar), nt2.typeMap.get(typevar)));
}
return builder.build();
return new NominalType(builder.build(), nt1.rawType);
}

// A special-case of join.
/**
* A special-case of join. If either argument is null, it returns null.
*/
static NominalType join(NominalType c1, NominalType c2) {
if (c1 == null || c2 == null) {
return null;
Expand All @@ -612,7 +614,7 @@ static NominalType join(NominalType c1, NominalType c2) {
return c1;
}
if (c1.rawType.equals(c2.rawType)) {
return c1.isGeneric() ? new NominalType(joinTypeMaps(c1, c2), c1.rawType) : c1;
return c1.isGeneric() ? joinTypeMaps(c1, c2) : c1;
}
// If c1.isRawSubtypeOf(c2) but not c1.isNominalSubtypeOf(c2), we would want to change
// joinTypeMaps to handle type maps with different domains. Basically, we want to go up
Expand Down
23 changes: 22 additions & 1 deletion test/com/google/javascript/jscomp/NewTypeInferenceTest.java
Expand Up @@ -19396,7 +19396,7 @@ public void testDontCrashWhenUnifyingNominalTypeWithEmptyTypemap() {
"};"));
}

public void testDontCrashWhenJoiningNominalTypeWithEmptyTypemap() {
public void testJoiningNominalTypeWithEmptyTypemap() {
typeCheck(LINE_JOINER.join(
"/**",
" * @constructor",
Expand All @@ -19406,6 +19406,27 @@ public void testDontCrashWhenJoiningNominalTypeWithEmptyTypemap() {
"Foo.prototype.conditional = function() {",
" return true ? /** @type {Foo<boolean>} */ (new Foo) : this;",
"};"));

typeCheck(LINE_JOINER.join(
"/**",
" * @constructor",
" * @template T",
" */",
"var Foo = function() {};",
"Foo.prototype.conditional = function() {",
" var /** !Foo<boolean> */ n = true ? /** @type {!Foo<boolean>} */ (new Foo) : this;",
"};"));

// The result of the join is Foo<?>
typeCheck(LINE_JOINER.join(
"/**",
" * @constructor",
" * @template T",
" */",
"var Foo = function() {};",
"Foo.prototype.conditional = function() {",
" var /** !Foo<number> */ n = true ? /** @type {!Foo<boolean>} */ (new Foo) : this;",
"};"));
}

public void testSpecializeFunctionReturnType() {
Expand Down

0 comments on commit c20dc57

Please sign in to comment.