Skip to content

Commit 7f483c0

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Set TypeName type of unresolved class with prefixed identifier in instance creation.
It was crashing prefer_void_to_null lint. [email protected] Change-Id: Ie9301d53267f264907ecc9e84d0c9252532172e6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137827 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent dc366a5 commit 7f483c0

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

pkg/analyzer/lib/src/dart/resolver/type_name_resolver.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class TypeNameResolver {
8181
TypeArgumentList argumentList = node.typeArguments;
8282
Element element = nameScope.lookup(typeName, definingLibrary);
8383
if (element == null) {
84+
node.type = dynamicType;
8485
if (nameScope.shouldIgnoreUndefined(typeName)) {
85-
node.type = dynamicType;
8686
return;
8787
}
8888
//
@@ -102,7 +102,6 @@ class TypeNameResolver {
102102
element = nameScope.lookup(prefix, definingLibrary);
103103
if (element is PrefixElement) {
104104
if (nameScope.shouldIgnoreUndefined(typeName)) {
105-
node.type = dynamicType;
106105
return;
107106
}
108107
AstNode grandParent = parent.parent;
@@ -143,7 +142,6 @@ class TypeNameResolver {
143142
}
144143
}
145144
if (nameScope.shouldIgnoreUndefined(typeName)) {
146-
node.type = dynamicType;
147145
return;
148146
}
149147
}

pkg/analyzer/test/src/dart/resolution/type_name_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/features.dart';
6+
import 'package:analyzer/src/error/codes.dart';
67
import 'package:analyzer/src/generated/engine.dart';
78
import 'package:analyzer/src/test_utilities/find_element.dart';
89
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -133,6 +134,57 @@ f(F<int> a) {}
133134
);
134135
}
135136

137+
test_instanceCreation_explicitNew_prefix_unresolvedClass() async {
138+
await assertErrorsInCode(r'''
139+
import 'dart:math' as math;
140+
141+
main() {
142+
new math.A();
143+
}
144+
''', [
145+
error(StaticWarningCode.NEW_WITH_NON_TYPE, 49, 1),
146+
]);
147+
148+
assertTypeName(
149+
findNode.typeName('A();'),
150+
null,
151+
'dynamic',
152+
expectedPrefix: findElement.prefix('math'),
153+
);
154+
}
155+
156+
test_instanceCreation_explicitNew_resolvedClass() async {
157+
await assertNoErrorsInCode(r'''
158+
class A {}
159+
160+
main() {
161+
new A();
162+
}
163+
''');
164+
165+
assertTypeName(
166+
findNode.typeName('A();'),
167+
findElement.class_('A'),
168+
typeStr('A', 'A*'),
169+
);
170+
}
171+
172+
test_instanceCreation_explicitNew_unresolvedClass() async {
173+
await assertErrorsInCode(r'''
174+
main() {
175+
new A();
176+
}
177+
''', [
178+
error(CompileTimeErrorCode.UNDEFINED_CLASS, 15, 1),
179+
]);
180+
181+
assertTypeName(
182+
findNode.typeName('A();'),
183+
null,
184+
'dynamic',
185+
);
186+
}
187+
136188
test_never() async {
137189
await assertNoErrorsInCode(r'''
138190
f(Never a) {}

0 commit comments

Comments
 (0)