Skip to content

Commit c09b338

Browse files
author
John Messerly
committed
fix inference of object members when a method has no target
we incorrectly thought `toString()` was a call to `Object.toString()` See dart-archive/dev_compiler#349 for more context [email protected] Review URL: https://codereview.chromium.org//1368793004 .
1 parent 6416a29 commit c09b338

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pkg/analyzer/lib/src/generated/static_type_analyzer.dart

+9
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,15 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
17621762
* being called is one of the object methods.
17631763
*/
17641764
bool _inferMethodInvocationObject(MethodInvocation node) {
1765+
// If we have a call like `toString()` don't infer it.
1766+
//
1767+
// Either: it's really `this.toString()` and we shouldn't need inference,
1768+
// or it's a call to a function that happens to be called `toString()` and
1769+
// we shouldn't infer.
1770+
if (node.target == null) {
1771+
return false;
1772+
}
1773+
17651774
// Object methods called on dynamic targets can have their types improved.
17661775
String name = node.methodName.name;
17671776
MethodElement inferredElement =

pkg/analyzer/test/generated/resolver_test.dart

+14
Original file line numberDiff line numberDiff line change
@@ -12172,6 +12172,20 @@ main() {
1217212172
code, typeProvider.dynamicType, typeProvider.intType);
1217312173
}
1217412174

12175+
void test_localVariableInference_declaredType_disabled_for_toString() {
12176+
String name = 'toString';
12177+
String code = '''
12178+
main() {
12179+
dynamic $name = () => null;
12180+
$name(); // marker
12181+
}''';
12182+
SimpleIdentifier identifier = _findMarkedIdentifier(code, "$name = ");
12183+
expect(identifier.staticType, typeProvider.dynamicType);
12184+
SimpleIdentifier call = _findMarkedIdentifier(code, "(); // marker");
12185+
expect(call.staticType, typeProvider.dynamicType);
12186+
expect((call.parent as Expression).staticType, typeProvider.dynamicType);
12187+
}
12188+
1217512189
void test_localVariableInference_noInitializer_disabled() {
1217612190
String code = r'''
1217712191
main() {

0 commit comments

Comments
 (0)