Skip to content

Commit a5704ea

Browse files
nshahancommit-bot@chromium.org
authored andcommitted
[dartdevc] Throw TypeError instead of CastError
Remove all `_check()` calls as they are now identical to `as()`. Fixes: #41008 Change-Id: I4e4dd1ff50840d3d1cbf0aae9b8cef0621016079 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139490 Reviewed-by: Mark Zhou <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]> Commit-Queue: Nicholas Shahan <[email protected]>
1 parent 584914b commit a5704ea

File tree

11 files changed

+63
-271
lines changed

11 files changed

+63
-271
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,13 +1055,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
10551055
body.add(js.statement('''
10561056
#.as = function as_FutureOr(o) {
10571057
if (#.is(o) || #.is(o)) return o;
1058-
return #.as(o, this, false);
1059-
}
1060-
''', [className, typeT, futureOfT, runtimeModule]));
1061-
body.add(js.statement('''
1062-
#._check = function check_FutureOr(o) {
1063-
if (#.is(o) || #.is(o)) return o;
1064-
return #.as(o, this, true);
1058+
return #.as(o, this);
10651059
}
10661060
''', [className, typeT, futureOfT, runtimeModule]));
10671061
return null;
@@ -1079,13 +1073,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
10791073
body.add(js.statement('''
10801074
#.as = function as_FutureOr(o) {
10811075
if (o == null || #.is(o) || #.is(o)) return o;
1082-
#.castError(o, this, false);
1083-
}
1084-
''', [className, typeT, futureOfT, runtimeModule]));
1085-
body.add(js.statement('''
1086-
#._check = function check_FutureOr(o) {
1087-
if (o == null || #.is(o) || #.is(o)) return o;
1088-
#.castError(o, this, true);
1076+
#.castError(o, this);
10891077
}
10901078
''', [className, typeT, futureOfT, runtimeModule]));
10911079
return null;
@@ -2296,8 +2284,6 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
22962284
switch (name) {
22972285
// Reserved for the compiler to do `x as T`.
22982286
case 'as':
2299-
// Reserved for the compiler to do implicit cast `T x = y`.
2300-
case '_check':
23012287
// Reserved for the SDK to compute `Type.toString()`.
23022288
case 'name':
23032289
// Reserved by JS, not a valid static member name.
@@ -5258,8 +5244,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
52585244
from.withNullability(Nullability.nonNullable) == to &&
52595245
_mustBeNonNullable(to)) {
52605246
// If the underlying type is the same, we only need a null check.
5261-
return runtimeCall('nullCast(#, #, #)',
5262-
[jsFrom, _emitType(to), js.boolean(isTypeError)]);
5247+
return runtimeCall('nullCast(#, #)', [jsFrom, _emitType(to)]);
52635248
}
52645249

52655250
// All Dart number types map to a JS double. We can specialize these
@@ -5278,8 +5263,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
52785263
if (from.nullability == Nullability.nonNullable) {
52795264
return jsFrom;
52805265
}
5281-
return runtimeCall('nullCast(#, #, #)',
5282-
[jsFrom, _emitType(to), js.boolean(isTypeError)]);
5266+
return runtimeCall('nullCast(#, #)', [jsFrom, _emitType(to)]);
52835267
}
52845268

52855269
// * -> int : asInt check
@@ -5294,16 +5278,11 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
52945278
}
52955279
}
52965280

5297-
return _emitCast(jsFrom, to, implicit: isTypeError);
5281+
return _emitCast(jsFrom, to);
52985282
}
52995283

5300-
js_ast.Expression _emitCast(js_ast.Expression expr, DartType type,
5301-
{bool implicit = true}) {
5302-
if (_types.isTop(type)) return expr;
5303-
5304-
var code = implicit ? '#._check(#)' : '#.as(#)';
5305-
return js.call(code, [_emitType(type), expr]);
5306-
}
5284+
js_ast.Expression _emitCast(js_ast.Expression expr, DartType type) =>
5285+
_types.isTop(type) ? expr : js.call('#.as(#)', [_emitType(type), expr]);
53075286

53085287
@override
53095288
js_ast.Expression visitSymbolLiteral(SymbolLiteral node) =>

sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ class Object {
7272

7373
@JSExportName('as')
7474
static Object _as_Object(Object o) => o;
75-
76-
@JSExportName('_check')
77-
static Object _check_Object(Object o) => o;
7875
}
7976

8077
@patch
@@ -89,14 +86,7 @@ class Null {
8986
static Object _as_Null(Object o) {
9087
// Avoid extra function call to core.Null.is() by manually inlining.
9188
if (o == null) return o;
92-
return dart.cast(o, dart.unwrapType(Null), false);
93-
}
94-
95-
@JSExportName('_check')
96-
static Object _check_Null(Object o) {
97-
// Avoid extra function call to core.Null.is() by manually inlining.
98-
if (o == null) return o;
99-
return dart.cast(o, dart.unwrapType(Null), true);
89+
return dart.cast(o, dart.unwrapType(Null));
10090
}
10191
}
10292

@@ -135,14 +125,7 @@ class Function {
135125
static Object _as_Function(Object o) {
136126
// Avoid extra function call to core.Function.is() by manually inlining.
137127
if (JS<Object>('!', 'typeof $o == "function"') || o == null) return o;
138-
return dart.cast(o, dart.unwrapType(Function), false);
139-
}
140-
141-
@JSExportName('_check')
142-
static Object _check_Function(Object o) {
143-
// Avoid extra function call to core.Function.is() by manually inlining.
144-
if (JS<Object>('!', 'typeof $o == "function"') || o == null) return o;
145-
return dart.cast(o, dart.unwrapType(Function), true);
128+
return dart.cast(o, dart.unwrapType(Function));
146129
}
147130
}
148131

@@ -217,17 +200,7 @@ class int {
217200
o == null) {
218201
return o;
219202
}
220-
return dart.cast(o, dart.unwrapType(int), false);
221-
}
222-
223-
@JSExportName('_check')
224-
static Object _check_int(Object o) {
225-
// Avoid extra function call to core.int.is() by manually inlining.
226-
if (JS<bool>('!', '(typeof $o == "number" && Math.floor($o) == $o)') ||
227-
o == null) {
228-
return o;
229-
}
230-
return dart.cast(o, dart.unwrapType(int), true);
203+
return dart.cast(o, dart.unwrapType(int));
231204
}
232205
}
233206

@@ -253,14 +226,7 @@ class double {
253226
static Object _as_double(o) {
254227
// Avoid extra function call to core.double.is() by manually inlining.
255228
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
256-
return dart.cast(o, dart.unwrapType(double), false);
257-
}
258-
259-
@JSExportName('_check')
260-
static Object _check_double(o) {
261-
// Avoid extra function call to core.double.is() by manually inlining.
262-
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
263-
return dart.cast(o, dart.unwrapType(double), true);
229+
return dart.cast(o, dart.unwrapType(double));
264230
}
265231
}
266232

@@ -275,14 +241,7 @@ abstract class num implements Comparable<num> {
275241
static Object _as_num(o) {
276242
// Avoid extra function call to core.num.is() by manually inlining.
277243
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
278-
return dart.cast(o, dart.unwrapType(num), false);
279-
}
280-
281-
@JSExportName('_check')
282-
static Object _check_num(o) {
283-
// Avoid extra function call to core.num.is() by manually inlining.
284-
if (JS<bool>('!', 'typeof $o == "number"') || o == null) return o;
285-
return dart.cast(o, dart.unwrapType(num), true);
244+
return dart.cast(o, dart.unwrapType(num));
286245
}
287246
}
288247

@@ -657,14 +616,7 @@ class String {
657616
static Object _as_String(Object o) {
658617
// Avoid extra function call to core.String.is() by manually inlining.
659618
if (JS<bool>('!', 'typeof $o == "string"') || o == null) return o;
660-
return dart.cast(o, dart.unwrapType(String), false);
661-
}
662-
663-
@JSExportName('_check')
664-
static Object _check_String(Object o) {
665-
// Avoid extra function call to core.String.is() by manually inlining.
666-
if (JS<bool>('!', 'typeof $o == "string"') || o == null) return o;
667-
return dart.cast(o, dart.unwrapType(String), true);
619+
return dart.cast(o, dart.unwrapType(String));
668620
}
669621
}
670622

@@ -695,14 +647,7 @@ class bool {
695647
static Object _as_bool(Object o) {
696648
// Avoid extra function call to core.bool.is() by manually inlining.
697649
if (JS<bool>("!", '$o === true || $o === false') || o == null) return o;
698-
return dart.cast(o, dart.unwrapType(bool), false);
699-
}
700-
701-
@JSExportName('_check')
702-
static Object _check_bool(Object o) {
703-
// Avoid extra function call to core.bool.is() by manually inlining.
704-
if (JS<bool>("!", '$o === true || $o === false') || o == null) return o;
705-
return dart.cast(o, dart.unwrapType(bool), true);
650+
return dart.cast(o, dart.unwrapType(bool));
706651
}
707652
}
708653

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/classes.dart

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,7 @@ addTypeTests(ctor, isClass) {
535535
'',
536536
'''#.as = function as_C(obj) {
537537
if (obj == null || obj[#]) return obj;
538-
return #(obj, this, false);
539-
}''',
540-
ctor,
541-
isClass,
542-
cast);
543-
JS(
544-
'',
545-
'''#._check = function check_C(obj) {
546-
if (obj == null || obj[#]) return obj;
547-
return #(obj, this, true);
538+
return #(obj, this);
548539
}''',
549540
ctor,
550541
isClass,

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ throwNullValueError() {
3838
null, Symbol('<Unexpected Null Value>'), null, null, null);
3939
}
4040

41-
castError(obj, expectedType, [@notNull bool isImplicit = false]) {
41+
castError(obj, expectedType) {
4242
var actualType = getReifiedType(obj);
4343
var message = _castErrorMessage(actualType, expectedType);
44-
var error = isImplicit ? TypeErrorImpl(message) : CastErrorImpl(message);
45-
throw error;
44+
throw TypeErrorImpl(message);
4645
}
4746

4847
String _castErrorMessage(from, to) {

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ dput(obj, field, value) {
134134
if (f != null) {
135135
var setterType = getSetterType(getType(obj), f);
136136
if (setterType != null) {
137-
return JS('', '#[#] = #._check(#)', obj, f, setterType, value);
137+
return JS('', '#[#] = #.as(#)', obj, f, setterType, value);
138138
}
139139
// Always allow for JS interop objects.
140140
if (isJsInterop(obj)) return JS('', '#[#] = #', obj, f, value);
@@ -179,14 +179,14 @@ String _argumentErrors(FunctionType type, List actuals, namedActuals) {
179179
}
180180
// Now that we know the signature matches, we can perform type checks.
181181
for (var i = 0; i < requiredCount; ++i) {
182-
JS('', '#[#]._check(#[#])', required, i, actuals, i);
182+
JS('', '#[#].as(#[#])', required, i, actuals, i);
183183
}
184184
for (var i = 0; i < extras; ++i) {
185-
JS('', '#[#]._check(#[#])', optionals, i, actuals, i + requiredCount);
185+
JS('', '#[#].as(#[#])', optionals, i, actuals, i + requiredCount);
186186
}
187187
if (names != null) {
188188
for (var name in names) {
189-
JS('', '#[#]._check(#[#])', named, name, namedActuals, name);
189+
JS('', '#[#].as(#[#])', named, name, namedActuals, name);
190190
}
191191
}
192192
return null;
@@ -397,13 +397,13 @@ bool instanceOf(obj, type) {
397397
}
398398

399399
@JSExportName('as')
400-
cast(obj, type, @notNull bool isImplicit) {
400+
cast(obj, type) {
401401
if (obj == null) return obj;
402402
var actual = getReifiedType(obj);
403403
if (isSubtypeOf(actual, type)) {
404404
return obj;
405405
}
406-
return castError(obj, type, isImplicit);
406+
return castError(obj, type);
407407
}
408408

409409
bool test(bool obj) {
@@ -427,7 +427,7 @@ asInt(obj) {
427427
if (obj == null) return null;
428428

429429
if (JS('!', 'Math.floor(#) != #', obj, obj)) {
430-
castError(obj, JS('', '#', int), false);
430+
castError(obj, JS('', '#', int));
431431
}
432432
return obj;
433433
}
@@ -448,7 +448,7 @@ _notNull(x) {
448448
}
449449

450450
/// No-op without null safety enabled.
451-
nullCast(x, type, [@notNull bool isImplicit = false]) => x;
451+
nullCast(x, type) => x;
452452

453453
/// The global constant map table.
454454
final constantMaps = JS('', 'new Map()');

sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ final metadata = JS('', 'Symbol("metadata")');
4646
///
4747
/// T.is(o): Implements 'o is T'.
4848
/// T.as(o): Implements 'o as T'.
49-
/// T._check(o): Implements the type assertion of 'T x = o;'
5049
///
5150
/// By convention, we used named JavaScript functions for these methods with the
52-
/// name 'is_X', 'as_X' and 'check_X' for various X to indicate the type or the
51+
/// name 'is_X' and 'as_X' for various X to indicate the type or the
5352
/// implementation strategy for the test (e.g 'is_String', 'is_G' for generic
5453
/// types, etc.)
5554
// TODO(jmesserly): we shouldn't implement Type here. It should be moved down
@@ -63,10 +62,7 @@ class DartType implements Type {
6362
bool is_T(object) => instanceOf(object, this);
6463

6564
@JSExportName('as')
66-
as_T(object) => cast(object, this, false);
67-
68-
@JSExportName('_check')
69-
check_T(object) => cast(object, this, true);
65+
as_T(object) => cast(object, this);
7066
}
7167

7268
class DynamicType extends DartType {
@@ -77,9 +73,6 @@ class DynamicType extends DartType {
7773

7874
@JSExportName('as')
7975
as_T(object) => object;
80-
81-
@JSExportName('_check')
82-
check_T(object) => object;
8376
}
8477

8578
@notNull
@@ -178,10 +171,7 @@ class LazyJSType extends DartType {
178171
bool is_T(obj) => isRawJSType(obj) || instanceOf(obj, this);
179172

180173
@JSExportName('as')
181-
as_T(obj) => obj == null || is_T(obj) ? obj : castError(obj, this, false);
182-
183-
@JSExportName('_check')
184-
check_T(obj) => obj == null || is_T(obj) ? obj : castError(obj, this, true);
174+
as_T(obj) => obj == null || is_T(obj) ? obj : castError(obj, this);
185175
}
186176

187177
/// An anonymous JS type
@@ -196,10 +186,7 @@ class AnonymousJSType extends DartType {
196186
bool is_T(obj) => _isJsObject(obj) || instanceOf(obj, this);
197187

198188
@JSExportName('as')
199-
as_T(obj) => obj == null || _isJsObject(obj) ? obj : cast(obj, this, false);
200-
201-
@JSExportName('_check')
202-
check_T(obj) => obj == null || _isJsObject(obj) ? obj : cast(obj, this, true);
189+
as_T(obj) => obj == null || _isJsObject(obj) ? obj : cast(obj, this);
203190
}
204191

205192
void _warn(arg) {
@@ -486,7 +473,7 @@ class FunctionType extends AbstractFunctionType {
486473
}
487474

488475
@JSExportName('as')
489-
as_T(obj, [@notNull bool isImplicit = false]) {
476+
as_T(obj) {
490477
if (obj == null) return obj;
491478
if (JS('!', 'typeof # == "function"', obj)) {
492479
var actual = JS('', '#[#]', obj, _runtimeType);
@@ -496,11 +483,8 @@ class FunctionType extends AbstractFunctionType {
496483
return obj;
497484
}
498485
}
499-
return castError(obj, this, isImplicit);
486+
return castError(obj, this);
500487
}
501-
502-
@JSExportName('_check')
503-
check_T(obj) => as_T(obj, true);
504488
}
505489

506490
/// A type variable, used by [GenericFunctionType] to represent a type formal.
@@ -677,13 +661,7 @@ class GenericFunctionType extends AbstractFunctionType {
677661
@JSExportName('as')
678662
as_T(obj) {
679663
if (obj == null || is_T(obj)) return obj;
680-
return castError(obj, this, false);
681-
}
682-
683-
@JSExportName('_check')
684-
check_T(obj) {
685-
if (obj == null || is_T(obj)) return obj;
686-
return castError(obj, this, true);
664+
return castError(obj, this);
687665
}
688666
}
689667

0 commit comments

Comments
 (0)