Skip to content

Commit ba8fffe

Browse files
natebiggsCommit Queue
authored and
Commit Queue
committed
[stable] Fix Dart2JS type inference for record accesses on invalid indices.
Switches can generate record accesses on receivers that don't support the generated selector. Add an if guarding against this case. Bug: 52438 Change-Id: I2b715c7a8d83b67a503d56d20b595bd90a81a59b Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/304380 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/304420 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 45799c3 commit ba8fffe

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 3.0.2
2+
3+
This is a patch release that:
4+
5+
- Fixes a dart2js crash when using a switch case expression on a record where the fields don't match the cases. (issue [#52438]).
6+
17
## 3.0.1
28

39
This is a patch release that:

pkg/compiler/lib/src/inferrer/typemasks/masks.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,10 +917,15 @@ class CommonMasks with AbstractValueDomain {
917917

918918
@override
919919
AbstractValue getGetterTypeInRecord(AbstractValue value, String getterName) {
920-
final type = value is RecordTypeMask
921-
? value.types[value.shape.indexOfGetterName(getterName)]
922-
: null;
923-
return type ?? dynamicType;
920+
if (value is RecordTypeMask) {
921+
final getterIndex = value.shape.indexOfGetterName(getterName);
922+
// Generated code can sometimes contain record accesses for invalid
923+
// getters.
924+
if (getterIndex >= 0) {
925+
return value.types[getterIndex];
926+
}
927+
}
928+
return dynamicType;
924929
}
925930

926931
@override
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Switches geenrate record accesses on non-existent fields. These accesses
6+
// should be guarded by a type check but Dart2JS does not always promote
7+
// correctly after the type check.
8+
9+
void main() {
10+
Object r = (1, 2);
11+
switch (r) {
12+
case (int _, int _, int c):
13+
print('Skip, no match');
14+
}
15+
}

0 commit comments

Comments
 (0)