Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Unified Diff: tool/input_sdk/private/ddc_runtime/operations.dart

Issue 2026133002: Throw TypeError instead of CastError for type coercions. (Closed) Base URL: https://github.com/dart-lang/dev_compiler@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tool/input_sdk/private/ddc_runtime/operations.dart
diff --git a/tool/input_sdk/private/ddc_runtime/operations.dart b/tool/input_sdk/private/ddc_runtime/operations.dart
index 92f62e2ba2a968c8ec46fb76989d01db13f42f22..1b5a9008d2a1f27277566d56f1b450da52548fe1 100644
--- a/tool/input_sdk/private/ddc_runtime/operations.dart
+++ b/tool/input_sdk/private/ddc_runtime/operations.dart
@@ -246,7 +246,7 @@ instanceOf(obj, type) => JS('', '''(() => {
let result = $strongInstanceOf($obj, $type);
if (result !== null) return result;
let actual = $getReifiedType($obj);
- $throwStrongModeError('Strong mode is check failure: ' +
+ $throwStrongModeError('Strong mode is-check failure: ' +
$typeName(actual) + ' does not soundly subtype ' +
$typeName($type));
})()''');
@@ -259,6 +259,13 @@ cast(obj, type) {
_throwCastError(obj, type, result);
}
+check(obj, type) {
+ if (JS('bool', '# == #', type, dynamic) || obj == null) return obj;
+ bool result = strongInstanceOf(obj, type, true);
+ if (JS('bool', '#', result)) return obj;
+ _throwTypeError(obj, type, result);
+}
+
bool test(obj) {
if (obj is bool) return obj;
return booleanConversionFailed(obj);
@@ -277,17 +284,23 @@ bool booleanConversionFailed(obj) {
void _throwCastError(obj, type, bool result) {
var actual = getReifiedType(obj);
- if (result == false) throwCastError(actual, type);
+ if (result == false) throwCastError(obj, actual, type);
+
+ throwStrongModeCastError(obj, actual, type);
+}
+
+void _throwTypeError(obj, type, bool result) {
+ var actual = getReifiedType(obj);
+ if (result == false) throwTypeError(obj, actual, type);
- throwStrongModeError('Strong mode cast failure from ' +
- typeName(actual) + ' to ' + typeName(type));
+ throwStrongModeTypeError(obj, actual, type);
}
asInt(obj) {
if (obj == null) return null;
if (JS('bool', 'Math.floor(#) != #', obj, obj)) {
- throwCastError(getReifiedType(obj), JS('', '#', int));
+ throwCastError(obj, getReifiedType(obj), JS('', '#', int));
}
return obj;
}

Powered by Google App Engine
This is Rietveld 408576698