Skip to content

Commit 8955223

Browse files
kallentuCommit Queue
authored andcommitted
[analyzer] Dot shorthands: Infer type parameters in constructor tearoffs.
This CL fixes co19/LanguageFeatures/Static-access-shorthand /semantics_A05_t01.dart. While working on this, I realized the static type was wrong and we weren't doing any inference for the constructor references so I updated the logic. Unit tests passing, language test expectations are actually better than before and that particular co19 test passing. Bug: #59835 Change-Id: I7437079c33640f53f48a4445890fadfebc9ec158 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/430561 Reviewed-by: Erik Ernst <[email protected]> Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 785b38e commit 8955223

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer/src/dart/ast/ast.dart';
1212
import 'package:analyzer/src/dart/ast/extensions.dart';
1313
import 'package:analyzer/src/dart/element/element.dart';
1414
import 'package:analyzer/src/dart/element/extensions.dart';
15+
import 'package:analyzer/src/dart/element/member.dart';
1516
import 'package:analyzer/src/dart/element/type.dart';
1617
import 'package:analyzer/src/dart/element/type_schema.dart';
1718
import 'package:analyzer/src/dart/element/type_system.dart';
@@ -39,8 +40,9 @@ class PropertyElementResolver with ScopeHelpers {
3940
TypeSystemImpl get _typeSystem => _resolver.typeSystem;
4041

4142
PropertyElementResolverResult resolveDotShorthand(
42-
DotShorthandPropertyAccessImpl node,
43-
) {
43+
DotShorthandPropertyAccessImpl node, {
44+
required TypeImpl contextType,
45+
}) {
4446
if (_resolver.isDotShorthandContextEmpty) {
4547
assert(
4648
false,
@@ -76,6 +78,35 @@ class PropertyElementResolver with ScopeHelpers {
7678
}
7779
}
7880

81+
// Infer type parameters.
82+
var elementToInfer = _resolver.inferenceHelper
83+
.constructorElementToInfer(
84+
typeElement: context.element3,
85+
constructorName: identifier,
86+
definingLibrary: _resolver.definingLibrary,
87+
);
88+
if (elementToInfer != null &&
89+
elementToInfer.typeParameters2.isNotEmpty) {
90+
var inferred =
91+
_resolver.inferenceHelper.inferTearOff(
92+
node,
93+
identifier,
94+
elementToInfer.asType,
95+
contextType: contextType,
96+
)
97+
as FunctionType;
98+
var inferredType = inferred.returnType;
99+
var constructorElement = ConstructorMember.from2(
100+
elementToInfer.element2.baseElement,
101+
inferredType as InterfaceType,
102+
);
103+
node.propertyName.element = constructorElement.baseElement;
104+
return PropertyElementResolverResult(
105+
readElementRequested2: node.propertyName.element,
106+
getType: inferred.returnType,
107+
);
108+
}
109+
79110
return PropertyElementResolverResult(
80111
readElementRequested2: element,
81112
getType: element.returnType,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,10 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
25242524
}
25252525

25262526
checkUnreachableNode(node);
2527-
var result = _propertyElementResolver.resolveDotShorthand(node);
2527+
var result = _propertyElementResolver.resolveDotShorthand(
2528+
node,
2529+
contextType: contextType,
2530+
);
25282531
_resolvePropertyAccessRhs_common(
25292532
result,
25302533
node,
@@ -4462,7 +4465,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
44624465
DartType type;
44634466
if (element is MethodElement) {
44644467
type = element.type;
4465-
} else if (element is ConstructorElementImpl2) {
4468+
} else if (element is ConstructorElementMixin2) {
44664469
type = element.type;
44674470
} else if (element is GetterElement) {
44684471
type = resolverResult.getType!;

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,38 @@ Function fn() {
652652
);
653653
}
654654

655+
test_tearOff_constructor_generic() async {
656+
await assertNoErrorsInCode(r'''
657+
class C<T> {
658+
T t;
659+
C(this.t);
660+
C.id(this.t);
661+
}
662+
663+
void main() {
664+
Object? o = C<int>(0);
665+
if (o is C<int>) {
666+
o = .new;
667+
if (o is Function) {
668+
o(1).t;
669+
}
670+
}
671+
}
672+
''');
673+
674+
var dotShorthand = findNode.singleDotShorthandPropertyAccess;
675+
assertResolvedNodeText(dotShorthand, r'''
676+
DotShorthandPropertyAccess
677+
period: .
678+
propertyName: SimpleIdentifier
679+
token: new
680+
element: <testLibraryFragment>::@class::C::@constructor::new#element
681+
staticType: C<T> Function(T)
682+
correspondingParameter: <null>
683+
staticType: C<T> Function(T)
684+
''');
685+
}
686+
655687
test_tearOff_constructor_new() async {
656688
await assertNoErrorsInCode('''
657689
void main() {

tests/language/dot_shorthands/type_parameter/type_parameter_error_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ void main() {
5858
// ^^^^^
5959
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_TYPE_ARGUMENTS_FUNCTION
6060
ET e = .new<int>;
61-
// ^^^^
62-
// [analyzer] COMPILE_TIME_ERROR.DISALLOWED_TYPE_INSTANTIATION_EXPRESSION
61+
// ^^^^^^^^^
62+
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
6363
// ^
6464
// [cfe] A dot shorthand constructor invocation can't have type arguments.
65+
// ^^^^^
66+
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_TYPE_ARGUMENTS_FUNCTION
6567
}

0 commit comments

Comments
 (0)