Skip to content

Commit c0d1583

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot shorthands: Report error when instantiating abstract class.
This CL reports `INSTANTIATE_ABSTRACT_CLASS` when we are trying to instantiate an abstract class (with a non-factory constructor). co19 tests and unit tests passing. Bug: #59835 Change-Id: I5eded14f558d528265d82fd63cc0af3d9b052eac Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426682 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 24f594a commit c0d1583

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ class InstanceCreationExpressionResolver {
7373

7474
// TODO(kallentu): Support other context types
7575
if (dotShorthandContextType is InterfaceTypeImpl) {
76+
InterfaceElementImpl2? contextElement = dotShorthandContextType.element3;
7677
// This branch will be true if we're resolving an explicitly marked
7778
// const constructor invocation. It's completely unresolved, unlike a
7879
// rewritten [DotShorthandConstructorInvocation] that resulted from
7980
// resolving a [DotShorthandInvocation].
8081
if (node.element == null) {
81-
var contextElement = dotShorthandContextType.element3;
8282
if (contextElement.getNamedConstructor2(node.constructorName.name)
8383
case ConstructorElementImpl2 element?
8484
when element.isAccessibleIn2(_resolver.definingLibrary)) {
@@ -93,7 +93,15 @@ class InstanceCreationExpressionResolver {
9393
}
9494

9595
var typeArguments = node.typeArguments;
96-
if (typeArguments != null) {
96+
if (contextElement is ClassElementImpl2 && contextElement.isAbstract) {
97+
var constructorElement = node.element;
98+
if (constructorElement != null && !constructorElement.isFactory) {
99+
_resolver.errorReporter.atNode(
100+
node,
101+
CompileTimeErrorCode.INSTANTIATE_ABSTRACT_CLASS,
102+
);
103+
}
104+
} else if (typeArguments != null) {
97105
_resolver.errorReporter.atNode(
98106
typeArguments,
99107
CompileTimeErrorCode

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

+46
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,52 @@ main() {
1818
@reflectiveTest
1919
class DotShorthandConstructorInvocationResolutionTest
2020
extends PubPackageResolutionTest {
21+
test_abstract_instantiation() async {
22+
await assertErrorsInCode(
23+
r'''
24+
Function getFunction() {
25+
return .new();
26+
}
27+
''',
28+
[error(CompileTimeErrorCode.INSTANTIATE_ABSTRACT_CLASS, 34, 6)],
29+
);
30+
}
31+
32+
test_abstract_instantiation_factory() async {
33+
await assertNoErrorsInCode(r'''
34+
void main() async {
35+
var iter = [1, 2];
36+
await for (var x in .fromIterable(iter)) {
37+
print(x);
38+
}
39+
}
40+
''');
41+
42+
var node = findNode.singleDotShorthandConstructorInvocation;
43+
assertResolvedNodeText(node, r'''
44+
DotShorthandConstructorInvocation
45+
period: .
46+
constructorName: SimpleIdentifier
47+
token: fromIterable
48+
element: ConstructorMember
49+
baseElement: dart:async::@fragment::dart:async/stream.dart::@class::Stream::@constructor::fromIterable#element
50+
substitution: {T: int}
51+
staticType: null
52+
argumentList: ArgumentList
53+
leftParenthesis: (
54+
arguments
55+
SimpleIdentifier
56+
token: iter
57+
correspondingParameter: ParameterMember
58+
baseElement: dart:async::@fragment::dart:async/stream.dart::@class::Stream::@constructor::fromIterable::@parameter::data#element
59+
substitution: {T: int}
60+
element: iter@26
61+
staticType: List<int>
62+
rightParenthesis: )
63+
staticType: Stream<int>
64+
''');
65+
}
66+
2167
test_chain_method() async {
2268
await assertNoErrorsInCode(r'''
2369
class C {

0 commit comments

Comments
 (0)