Skip to content

Commit

Permalink
[stable] [dart2wasm] Only share type parameter fields if nullability …
Browse files Browse the repository at this point in the history
…allows

Bug: #55741

Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/366940
Cherry-pick-request: #55895
Change-Id: I69b31dfff0eddd437becae79a5ae49683906088d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369241
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
  • Loading branch information
mkustermann authored and Commit Queue committed Jun 3, 2024
1 parent 7b7786a commit dc9ede5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ This is a patch release that:

- Adds support for `--enable-experiment` flag to `dart compile wasm.

- Fixes an issue in dart2wasm compiler that can result in incorrect nullability
of type parameter (see [#55741])

[#55767]: https://github.com/dart-lang/sdk/issues/55767
[#55872]: https://github.com/dart-lang/sdk/issues/55872
[#55876]: https://github.com/dart-lang/sdk/issues/55876
[#55741]: https://github.com/dart-lang/sdk/issues/55741

## 3.4.2 - 2024-05-29

Expand Down
6 changes: 4 additions & 2 deletions pkg/dart2wasm/lib/class_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,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;
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/dart2wasm/lib/transformers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,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;
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/language/regression_55741_test.dart
Original file line number Diff line number Diff line change
@@ -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<String>('a') : 1;
final baseNullable = kTrue ? Base<String?>('a') : 1;
final sub = kTrue ? Sub<String>('a') : 1;
final subNullable = kTrue ? Sub<String?>('a') : 1;

Expect.isTrue(base is Base<String>, 'is Base<String>');
Expect.isTrue(base is Base<String?>, 'is Base<String?>');
Expect.isTrue(baseNullable is! Base<String>, 'is! Base<String>');
Expect.isTrue(baseNullable is Base<String?>, 'is Base<String?>');
Expect.isTrue(sub is Sub<String>, 'is Sub<String>');
Expect.isTrue(sub is Sub<String?>, 'is Sub<String?>');
Expect.isTrue(subNullable is! Sub<String>, 'is! Sub<String>');
Expect.isTrue(subNullable is Sub<String?>, 'is Sub<String?>');
Expect.isTrue(sub is! Base<String>, 'is! Base<String>');
Expect.isTrue(sub is Base<String?>, 'is Base<String?>');
Expect.isTrue(subNullable is! Base<String>, 'is! Base<String>');
Expect.isTrue(subNullable is Base<String?>, 'is Base<String?>');
}

class Base<T> {
Base(this.data);
final T data;
}

class Sub<T> extends Base<T?> {
Sub(super.data);
}

0 comments on commit dc9ede5

Please sign in to comment.