[ddc] Add library URIs to cast failure messages

When the two types have the same name but are from different libraries
showing the library URI will help users understand the failure better.

Change-Id: I5ab4412e676272111d41f688ef2d1cc83afbe627
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194116
Reviewed-by: Mark Zhou <markzipan@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
This commit is contained in:
Nicholas Shahan 2021-04-09 22:24:43 +00:00 committed by commit-bot@chromium.org
parent 0b175ed534
commit 4e25e499c8
7 changed files with 156 additions and 2 deletions

View file

@ -102,8 +102,23 @@ String _castErrorMessage(from, to) {
// }
// }
// }
return "Expected a value of type '${typeName(to)}', "
"but got one of type '${typeName(from)}'";
var fromName = "'${typeName(from)}'";
var toName = "'${typeName(to)}'";
var toType = to;
if (_jsInstanceOf(to, NullableType) || _jsInstanceOf(to, LegacyType)) {
toType = to.type;
}
var fromType = from;
if (_jsInstanceOf(from, NullableType) || _jsInstanceOf(from, LegacyType)) {
fromType = from.type;
}
if (typeName(fromType) == typeName(toType)) {
fromName += ' (in ${getLibraryUri(fromType)})';
toName += ' (in ${getLibraryUri(toType)})';
}
return 'Expected a value of type $toName, but got one of type $fromName';
}
/// The symbol that references the thrown Dart Object (typically but not

View file

@ -0,0 +1,5 @@
// Copyright (c) 2021, 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.
class Animal {}

View file

@ -0,0 +1,5 @@
// Copyright (c) 2021, 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.
class Animal {}

View file

@ -0,0 +1,66 @@
// Copyright (c) 2021, 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.
import "../utils.dart";
import "lib_a.dart" as libA;
import "lib_b.dart" as libB;
void main() {
// When the name of the class is the same the error message should include
// library URIs for the two classes.
var a = libA.Animal();
try {
a as libB.Animal;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_b.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_a.dart)",
message);
}
// Verify the libraries are properly ordered.
var b = libB.Animal();
try {
b as libA.Animal;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_a.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_b.dart)",
message);
}
// Shows library URIs when one of the types is nullable.
try {
b as libA.Animal?;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal?' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_a.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_b.dart)",
message);
}
// URIs are not displayed when the class names are different.
try {
a as String;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'String', but got one of type 'Animal'",
message);
}
}

View file

@ -0,0 +1,5 @@
// Copyright (c) 2021, 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.
class Animal {}

View file

@ -0,0 +1,5 @@
// Copyright (c) 2021, 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.
class Animal {}

View file

@ -0,0 +1,53 @@
// Copyright (c) 2021, 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.
// @dart = 2.9
import "../utils.dart";
import "lib_a.dart" as libA;
import "lib_b.dart" as libB;
void main() {
// When the name of the class is the same the error message should include
// library URIs for the two classes.
var a = libA.Animal();
try {
a as libB.Animal;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc_2/cast_error/lib_b.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc_2/cast_error/lib_a.dart)",
message);
}
// Verify the libraries are properly ordered.
var b = libB.Animal();
try {
b as libA.Animal;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc_2/cast_error/lib_a.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc_2/cast_error/lib_b.dart)",
message);
}
// URIs are not displayed when the class names are different.
try {
a as String;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'String', but got one of type 'Animal'",
message);
}
}