Skip to content

Commit 7b37838

Browse files
committed
[dart2wasm] Split --omit-type-checks into --omit-{implicit,explicit}-checks
This aligns the behavior of --omit-implicit-type-checks with dart2js Change-Id: I453652339f23b89873d070422cf61eca77a2b68d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/348164 Reviewed-by: Slava Egorov <[email protected]>
1 parent 7901612 commit 7b37838

File tree

6 files changed

+81
-11
lines changed

6 files changed

+81
-11
lines changed

pkg/dart2wasm/lib/code_generator.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ class CodeGenerator extends ExpressionVisitor1<w.ValueType, w.ValueType>
488488

489489
void generateTypeChecks(List<TypeParameter> typeParameters,
490490
FunctionNode function, ParameterInfo paramInfo) {
491-
if (translator.options.omitTypeChecks) {
491+
if (translator.options.omitImplicitTypeChecks) {
492492
return;
493493
}
494494

@@ -3047,7 +3047,7 @@ class CodeGenerator extends ExpressionVisitor1<w.ValueType, w.ValueType>
30473047

30483048
@override
30493049
w.ValueType visitAsExpression(AsExpression node, w.ValueType expectedType) {
3050-
if (translator.options.omitTypeChecks || node.isUnchecked) {
3050+
if (translator.options.omitExplicitTypeChecks || node.isUnchecked) {
30513051
return wrap(node.operand, expectedType);
30523052
}
30533053

@@ -3425,7 +3425,7 @@ class CodeGenerator extends ExpressionVisitor1<w.ValueType, w.ValueType>
34253425
}
34263426
}
34273427

3428-
if (!translator.options.omitTypeChecks) {
3428+
if (!translator.options.omitImplicitTypeChecks) {
34293429
// Check type parameter bounds
34303430
for (TypeParameter typeParameter in memberTypeParams) {
34313431
if (typeParameter.bound != translator.coreTypes.objectNullableRawType) {

pkg/dart2wasm/lib/dart2wasm.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ final List<Option> options = [
4242
Flag(
4343
"enable-asserts", (o, value) => o.translatorOptions.enableAsserts = value,
4444
defaultsTo: _d.translatorOptions.enableAsserts),
45-
Flag("omit-type-checks",
46-
(o, value) => o.translatorOptions.omitTypeChecks = value,
47-
defaultsTo: _d.translatorOptions.omitTypeChecks),
45+
Flag("omit-explicit-checks",
46+
(o, value) => o.translatorOptions.omitExplicitTypeChecks = value,
47+
defaultsTo: _d.translatorOptions.omitExplicitTypeChecks),
48+
Flag("omit-implicit-checks",
49+
(o, value) => o.translatorOptions.omitImplicitTypeChecks = value,
50+
defaultsTo: _d.translatorOptions.omitImplicitTypeChecks),
51+
// TODO(http://dartbug.com/54675): Deprecate & Remove this one.
52+
Flag("omit-type-checks", (o, value) {
53+
o.translatorOptions.omitImplicitTypeChecks = value;
54+
o.translatorOptions.omitExplicitTypeChecks = value;
55+
},
56+
defaultsTo: _d.translatorOptions.omitImplicitTypeChecks &&
57+
_d.translatorOptions.omitExplicitTypeChecks),
4858
Flag("verbose", (o, value) => o.translatorOptions.verbose = value,
4959
defaultsTo: _d.translatorOptions.verbose),
5060
Flag("verify-type-checks",

pkg/dart2wasm/lib/dispatch_table.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ class SelectorInfo {
131131
// parameter.
132132
return translator.coreTypes.objectNullableRawType;
133133
}
134-
if (!translator.options.omitTypeChecks) {
134+
if (!translator.options.omitImplicitTypeChecks) {
135135
// A runtime type check of the parameter will be generated.
136-
// The value therefore must be boxed.
136+
// The value must therefore be boxed.
137137
ensureBoxed[index] = true;
138138
}
139139
}

pkg/dart2wasm/lib/dynamic_forwarders.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ void generateDynamicFunctionCall(
712712
b.br_if(noSuchMethodBlock);
713713

714714
// Shape check passed, check types
715-
if (!translator.options.omitTypeChecks) {
715+
if (!translator.options.omitImplicitTypeChecks) {
716716
b.local_get(functionTypeLocal);
717717
b.local_get(typeArgsLocal);
718718
b.local_get(posArgsLocal);

pkg/dart2wasm/lib/translator.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class TranslatorOptions {
3434
bool inlining = true;
3535
bool jsCompatibility = false;
3636
bool nameSection = true;
37-
bool omitTypeChecks = false;
37+
bool omitImplicitTypeChecks = false;
38+
bool omitExplicitTypeChecks = false;
3839
bool polymorphicSpecialization = false;
3940
bool printKernel = false;
4041
bool printWasm = false;
@@ -450,7 +451,7 @@ class Translator with KernelNodes {
450451
final namedParameters = List.of(staticType.namedParameters);
451452
assert(namedParameters.length == method.function.namedParameters.length);
452453

453-
if (!options.omitTypeChecks) {
454+
if (!options.omitImplicitTypeChecks) {
454455
for (int i = 0; i < positionalParameters.length; i++) {
455456
final param = method.function.positionalParameters[i];
456457
if (param.isCovariantByDeclaration || param.isCovariantByClass) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2024, 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+
// dart2jsOptions=--omit-implicit-checks
6+
// dart2wasmOptions=--omit-implicit-checks
7+
8+
import 'package:expect/expect.dart';
9+
import 'package:expect/config.dart';
10+
11+
final kTrue = int.parse('1') == 1;
12+
13+
void main() {
14+
// This test is specific to testing dart2wasm & dart2js.
15+
if (!isDart2jsConfiguration && !isDart2WasmConfiguration) return;
16+
17+
testExplicitAsCheck();
18+
19+
testCovariantMethodCheck();
20+
testDynamicCall();
21+
testCovariantKeyword();
22+
}
23+
24+
void testExplicitAsCheck() {
25+
final dynamic x = kTrue ? A() : B();
26+
Expect.throws(() => x as B);
27+
}
28+
29+
void testCovariantMethodCheck() {
30+
final List<dynamic> list = kTrue ? <String>['a'] : <int>[1];
31+
list.add('b');
32+
list.add(3);
33+
Expect.equals('a', list[0]);
34+
Expect.equals('b', list[1]);
35+
Expect.equals(3, list[2]);
36+
}
37+
38+
void testDynamicCall() {
39+
final dynamic a = kTrue ? (String a) => 'closure($a)' : A();
40+
Expect.equals('closure(B)', a(B()));
41+
}
42+
43+
void testCovariantKeyword() {
44+
final object = kTrue ? B() : A();
45+
46+
// Normally the `covariant String` would cause a type error to be thrown, but
47+
// due to --omit-implicit-checks it works just fine.
48+
Expect.equals('B.foo(42)', object.foo(42));
49+
}
50+
51+
class A {
52+
String foo(Object arg) => 'A.foo($arg)';
53+
}
54+
55+
class B extends A {
56+
String foo(covariant String arg) => 'B.foo($arg)';
57+
58+
String toString() => 'B';
59+
}

0 commit comments

Comments
 (0)