Skip to content

Commit

Permalink
[stable] Fixing DDC record subtype checks crashing with numerics.
Browse files Browse the repository at this point in the history
Bug: #52480
Change-Id: Iad9f6c986d4c445558c105abc66ad9b2a2812f25
Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/305560
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/306307
Reviewed-by: Sigmund Cherem <[email protected]>
Reviewed-by: Nicholas Shahan <[email protected]>
Commit-Queue: Mark Zhou <[email protected]>
  • Loading branch information
Markzipan authored and Commit Queue committed May 31, 2023
1 parent 892ba4a commit 7add114
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2497,11 +2497,17 @@ class RecordType extends DartType {

@JSExportName('is')
bool is_T(obj) {
if (obj is _RecordImpl) {
var actual = getReifiedType(obj);
return actual != null && isSubtypeOf(actual, this);
if (!(obj is _RecordImpl)) return false;
if (shape != obj.shape) return false;
if (types.length != obj.values.length) {
return false;
}
return false;
for (var i = 0; i < types.length; i++) {
if (!JS<bool>('!', '#.is(#)', types[i], obj.values[i])) {
return false;
}
}
return true;
}

@JSExportName('as')
Expand Down
36 changes: 36 additions & 0 deletions tests/web/record_numbers_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2023, 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.

// Tests record numeric subtyping rules with web semantics.
// Regression test for https://github.com/dart-lang/sdk/issues/52480

import "package:expect/expect.dart";

main() {
Expect.notSubtype<(int, int), (double, double)>();
Expect.notSubtype<(int, double), (double, double)>();
Expect.notSubtype<(double, double), (int, int)>();
Expect.notSubtype<(int, double), (int, int)>();

Object mixedTuple = (0, 0.1);
(double, double) doubleTuple = (0.0, 0.1);
Object someTuple = doubleTuple;

Expect.type<(double, double)>(mixedTuple);
Expect.type<(int, double)>(mixedTuple);
Expect.type<(double, double)>(someTuple);
Expect.type<(int, double)>(someTuple);

dynamic dynamicIntTuple = (4, 4);
dynamic dynamicDoubleTuple = doubleTuple;

Expect.type<(int, int)>(dynamicIntTuple);
Expect.type<(int, double)>(dynamicIntTuple);
Expect.type<(double, int)>(dynamicIntTuple);
Expect.type<(double, double)>(dynamicIntTuple);
Expect.notType<(int, int)>(dynamicDoubleTuple);
Expect.type<(int, double)>(dynamicDoubleTuple);
Expect.notType<(double, int)>(dynamicDoubleTuple);
Expect.type<(double, double)>(dynamicDoubleTuple);
}

0 comments on commit 7add114

Please sign in to comment.