Skip to content

Commit

Permalink
Rollback of 'If a generic type is instantiated with fewer than expect…
Browse files Browse the repository at this point in the history
…ed parameters, fill in the rest with ?. Otherwise, the uninstantiated type variables cause i...'

This needs some fixups before it can be rolled forward.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=128857212
  • Loading branch information
dimvar authored and blickly committed Aug 1, 2016
1 parent 5b368d9 commit 58902a9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 102 deletions.
23 changes: 5 additions & 18 deletions src/com/google/javascript/rhino/jstype/JSTypeRegistry.java
Expand Up @@ -51,7 +51,6 @@
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.javascript.rhino.ErrorReporter;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SimpleErrorReporter;
Expand Down Expand Up @@ -1665,27 +1664,19 @@ private JSType createFromTypeNodesInternal(Node n, String sourceName,
!(nonNullableTypeNames.contains(n.getString()))) {
Node typeList = n.getFirstChild();
int nAllowedTypes = namedType.getTemplateTypeMap().numUnfilledTemplateKeys();
String typeName = n.getString();
if (!namedType.isUnknownType() && (typeList != null || nAllowedTypes > 0)) {
if (typeList == null) {
// Object and Array generics are different from other types.
// Don't fill in missing generics for them.
if (typeName.equals("Object") || typeName.equals("Array")) {
return createDefaultObjectUnion(namedType);
}
typeList = IR.empty();
}
if (!namedType.isUnknownType() && typeList != null) {
// Templatized types.
ImmutableList.Builder<JSType> templateTypes = ImmutableList.builder();
ImmutableList.Builder<JSType> templateTypes =
ImmutableList.builder();

// Special case for Object, where Object.<X> implies Object.<?,X>.
if ((typeName.equals("Object") || typeName.equals("window.Object"))
if ((n.getString().equals("Object") || n.getString().equals("window.Object"))
&& typeList.getFirstChild() == typeList.getLastChild()) {
templateTypes.add(getNativeType(UNKNOWN_TYPE));
}

int templateNodeIndex = 0;
for (Node templateNode : typeList.children()) {
for (Node templateNode : typeList.getFirstChild().siblings()) {
// Don't parse more templatized type nodes than the type can
// accommodate. This is because some existing clients have
// template annotations on non-templatized classes, for instance:
Expand All @@ -1705,10 +1696,6 @@ private JSType createFromTypeNodesInternal(Node n, String sourceName,
templateTypes.add(createFromTypeNodesInternal(
templateNode, sourceName, scope));
}
// If fewer than nAllowedTypes types are provided, fill in the rest with ?
for (int i = typeList.getChildCount(); i < nAllowedTypes; i++) {
templateTypes.add(getNativeType(UNKNOWN_TYPE));
}
namedType = createTemplatizedType(
(ObjectType) namedType, templateTypes.build());
Preconditions.checkNotNull(namedType);
Expand Down
83 changes: 0 additions & 83 deletions test/com/google/javascript/jscomp/TypeCheckTest.java
Expand Up @@ -10336,89 +10336,6 @@ public void testInheritSameGenericInterfaceFromDifferentPaths() {
"ns.Low = function() {};"));
}

public void testFillInMissingGenerics1() {
testTypes(
LINE_JOINER.join(
"/**",
" * @constructor",
" * @template T",
" */",
"function Foo() {}",
"/**",
" * @constructor",
" * @template T",
" */",
"function Bar() {}",
"/** @type {!Foo<T>} */",
"Bar.prototype.myprop;",
"/** @param {!Bar} x */",
"function f(x) {",
" var /** null */ n = x.myprop;",
"}"),
LINE_JOINER.join(
"initializing variable",
"found : Foo<?>",
"required: null"));
}

public void testFillInMissingGenerics2() {
testTypes(
LINE_JOINER.join(
"/**",
" * @constructor",
" * @template T",
" */",
"function Foo() {}",
"/**",
" * @constructor",
" * @template T,U",
" */",
"function Bar() {}",
"/** @type {!Foo<U>} */",
"Bar.prototype.myprop;",
"/** @param {!Bar<number>} x */",
"function f(x) {",
" var /** null */ n = x.myprop;",
"}"),
LINE_JOINER.join(
"initializing variable",
"found : Foo<?>",
"required: null"));
}

public void testFillInMissingGenerics3() {
// If we don't fill in the missing generics here, we get a spurious
// warning that Low gets incompatible types for myprop from its
// super-interfaces.
testTypes(
LINE_JOINER.join(
"/**",
" * @constructor",
" * @template T",
" */",
"function Foo() {}",
"/**",
" * @interface",
" * @template T",
" */",
"function High1() {}",
"/** @type {!Foo<T>} */",
"High1.prototype.myprop;",
"/**",
" * @interface",
" * @template T",
" */",
"function High2() {}",
"/** @type {!Foo<T>} */",
"High2.prototype.myprop;",
"/**",
" * @interface",
" * @extends {High1}",
" * @extends {High2}",
" */",
"function Low() {}"));
}

public void testConversionFromInterfaceToRecursiveConstructor() {
testClosureTypesMultipleWarnings(
suppressMissingProperty("foo") +
Expand Down
Expand Up @@ -1743,7 +1743,7 @@ public void testClassTemplateInheritance5() {
"var result3 = z.method1();\n");
assertEquals("string", findNameType("result1", globalScope).toString());
assertEquals("boolean", findNameType("result2", globalScope).toString());
assertEquals("?", findNameType("result3", globalScope).toString());
assertEquals("T", findNameType("result3", globalScope).toString());
}

public void testClosureParameterTypesWithoutJSDoc() {
Expand Down

0 comments on commit 58902a9

Please sign in to comment.