Skip to content

Commit 58c658b

Browse files
sjamesrcopybara-github
authored andcommitted
fix infinite recursion in template type checking
PiperOrigin-RevId: 615186462
1 parent ec2c44f commit 58c658b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/com/google/javascript/rhino/jstype/SubtypeChecker.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ private boolean isSubtypeHelper(JSType subtype, JSType supertype) {
263263
private boolean isObjectSubtypeHelper(ObjectType subtype, ObjectType supertype) {
264264
TemplateTypeMap subtypeParams = subtype.getTemplateTypeMap();
265265
TemplateTypeMap supertypeParams = supertype.getTemplateTypeMap();
266+
boolean subAndSuperAreSameBaseType = false;
267+
if (subtype.isTemplatizedType() && supertype.isTemplatizedType()) {
268+
TemplatizedType templatizedSubType = subtype.toMaybeTemplatizedType();
269+
TemplatizedType templatizedSuperType = supertype.toMaybeTemplatizedType();
270+
ObjectType superTypeReferencedType = templatizedSuperType.getReferencedType();
271+
ObjectType subTypeReferencedType = templatizedSubType.getReferencedType();
272+
if (subTypeReferencedType.equals(superTypeReferencedType)) {
273+
subAndSuperAreSameBaseType = true;
274+
}
275+
}
266276
boolean bivarantMatch = false;
267277

268278
/*
@@ -284,7 +294,9 @@ private boolean isObjectSubtypeHelper(ObjectType subtype, ObjectType supertype)
284294
bivarantMatch = true;
285295
}
286296

287-
if (this.isUsingStructuralTyping && supertype.isStructuralType()) {
297+
if (!subAndSuperAreSameBaseType
298+
&& this.isUsingStructuralTyping
299+
&& supertype.isStructuralType()) {
288300
/*
289301
* Do this before considering templatization in general.
290302
*
@@ -293,7 +305,7 @@ private boolean isObjectSubtypeHelper(ObjectType subtype, ObjectType supertype)
293305
*/
294306
return this.isStructuralSubtypeHelper(
295307
subtype, supertype, PropertyOptionality.VOIDABLE_PROPS_ARE_OPTIONAL);
296-
} else if (supertype.isRecordType()) {
308+
} else if (!subAndSuperAreSameBaseType && supertype.isRecordType()) {
297309
/*
298310
* Anonymous record types are always considered structurally when supertypes.
299311
*

test/com/google/javascript/jscomp/integration/AdvancedOptimizationsIntegrationTest.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ public void testFoldSpread() {
216216

217217
@Test
218218
public void testBug303058080() {
219-
// TODO: b/303058080 - Actually fix the problem this test records.
220-
// The problem is a timeout, so as-written this test doesn't actually trigger the timeout.
221-
// See the comment below for how to modify this test case to cause the timeout for
222-
// investigation.
223-
224219
// Avoid including the transpilation library
225220
useNoninjectingCompiler = true;
226221
CompilerOptions options = createCompilerOptions();
@@ -259,11 +254,11 @@ public void testBug303058080() {
259254
"var Bar = function() {};",
260255
"",
261256
"/**",
262-
" * @return {!Foo<?>}", // Change `?` to `T` to trigger the timeout
257+
" * @return {!Foo<T>}",
263258
" */",
264259
"Bar.prototype.flatten = function() {};")
265260
.buildExternsFile("externs.js"));
266-
// The timeout happens while processing the externs, so there's no need to have any input
261+
// The timeout happened while processing the externs, so there's no need to have any input
267262
// code.
268263
test(options, "", "");
269264
}

0 commit comments

Comments
 (0)