diff --git a/pkg/dart2wasm/lib/class_info.dart b/pkg/dart2wasm/lib/class_info.dart index a81d55a66d4f..6f6b1642c9a0 100644 --- a/pkg/dart2wasm/lib/class_info.dart +++ b/pkg/dart2wasm/lib/class_info.dart @@ -318,8 +318,10 @@ class ClassInfoCollector { : cls.implementedTypes.single; for (TypeParameter parameter in cls.typeParameters) { for (int i = 0; i < supertype.typeArguments.length; i++) { - DartType arg = supertype.typeArguments[i]; - if (arg is TypeParameterType && arg.parameter == parameter) { + DartType superTypeArg = supertype.typeArguments[i]; + if (superTypeArg is TypeParameterType && + superTypeArg.parameter == parameter && + superTypeArg.nullability != Nullability.nullable) { typeParameterMatch[parameter] = superInfo.cls!.typeParameters[i]; break; } diff --git a/pkg/dart2wasm/lib/transformers.dart b/pkg/dart2wasm/lib/transformers.dart index d4141d0b153e..bdfcc9043b35 100644 --- a/pkg/dart2wasm/lib/transformers.dart +++ b/pkg/dart2wasm/lib/transformers.dart @@ -149,8 +149,10 @@ class _WasmTransformer extends Transformer { } for (int i = 0; i < cls.typeParameters.length; i++) { TypeParameter parameter = cls.typeParameters[i]; - DartType arg = supertype.typeArguments[i]; - if (arg is! TypeParameterType || arg.parameter != parameter) { + DartType superTypeArg = supertype.typeArguments[i]; + if (superTypeArg is! TypeParameterType || + superTypeArg.parameter != parameter || + superTypeArg.nullability == Nullability.nullable) { return false; } } diff --git a/tests/language/regression_55741_test.dart b/tests/language/regression_55741_test.dart new file mode 100644 index 000000000000..f86fd51819ae --- /dev/null +++ b/tests/language/regression_55741_test.dart @@ -0,0 +1,36 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:expect/expect.dart'; + +final kTrue = int.parse('1') == 1; + +void main() { + final base = kTrue ? Base('a') : 1; + final baseNullable = kTrue ? Base('a') : 1; + final sub = kTrue ? Sub('a') : 1; + final subNullable = kTrue ? Sub('a') : 1; + + Expect.isTrue(base is Base, 'is Base'); + Expect.isTrue(base is Base, 'is Base'); + Expect.isTrue(baseNullable is! Base, 'is! Base'); + Expect.isTrue(baseNullable is Base, 'is Base'); + Expect.isTrue(sub is Sub, 'is Sub'); + Expect.isTrue(sub is Sub, 'is Sub'); + Expect.isTrue(subNullable is! Sub, 'is! Sub'); + Expect.isTrue(subNullable is Sub, 'is Sub'); + Expect.isTrue(sub is! Base, 'is! Base'); + Expect.isTrue(sub is Base, 'is Base'); + Expect.isTrue(subNullable is! Base, 'is! Base'); + Expect.isTrue(subNullable is Base, 'is Base'); +} + +class Base { + Base(this.data); + final T data; +} + +class Sub extends Base { + Sub(super.data); +}