Skip to content

Commit f6063e5

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot shorthands: Error - constructors with type parameters.
Add `WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR` for dot shorthand constructor invocations with type parameters. Unit tests added and co19 tests + language tests such as `type_parameter_error_test.dart`. Bug: #59835 Change-Id: Ie8314f1678248f168744646dc0c3d2622cf61551 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426283 Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 52fae0a commit f6063e5

File tree

11 files changed

+134
-17
lines changed

11 files changed

+134
-17
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,8 @@ CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_ANONYMOUS_FUNCTION:
16711671
since: 2.15
16721672
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR:
16731673
status: hasFix
1674+
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR:
1675+
status: needsEvaluation
16741676
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_ENUM:
16751677
status: noFix
16761678
since: 2.17

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

+13
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ class InstanceCreationExpressionResolver {
9191
);
9292
}
9393
}
94+
95+
var typeArguments = node.typeArguments;
96+
if (typeArguments != null) {
97+
_resolver.errorReporter.atNode(
98+
typeArguments,
99+
CompileTimeErrorCode
100+
.WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR,
101+
arguments: [
102+
dotShorthandContextType.getDisplayString(),
103+
node.constructorName.name,
104+
],
105+
);
106+
}
94107
} else {
95108
_resolver.errorReporter.atNode(
96109
node,

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

+10
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ class DotShorthandConstructorInvocationInferrer
160160
@override
161161
TypeArgumentListImpl? get _typeArguments => node.typeArguments;
162162

163+
@override
164+
void _reportWrongNumberOfTypeArguments(
165+
TypeArgumentList typeArgumentList,
166+
FunctionType rawType,
167+
List<TypeParameterElement> typeParameters,
168+
) {
169+
// Error reporting for dot shorthand constructor invocations is done
170+
// within the [InstanceCreationExpressionResolver].
171+
}
172+
163173
@override
164174
List<FormalParameterElement>? _storeResult(
165175
List<DartType>? typeArgumentTypes,

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

-2
Original file line numberDiff line numberDiff line change
@@ -1293,8 +1293,6 @@ class MethodInvocationResolver with ScopeHelpers {
12931293
if (receiver.getNamedConstructor2(name)
12941294
case ConstructorElementImpl2 element?
12951295
when element.isAccessibleIn2(_resolver.definingLibrary)) {
1296-
// TODO(kallentu): Produce an error if there are type arguments for this
1297-
// constructor.
12981296
var replacement = DotShorthandConstructorInvocationImpl(
12991297
constKeyword: null,
13001298
period: node.period,

pkg/analyzer/lib/src/error/codes.g.dart

+14
Original file line numberDiff line numberDiff line change
@@ -5916,6 +5916,20 @@ class CompileTimeErrorCode extends DiagnosticCode {
59165916
hasPublishedDocs: true,
59175917
);
59185918

5919+
/// Parameters:
5920+
/// 0: the name of the class being instantiated
5921+
/// 1: the name of the constructor being invoked
5922+
static const CompileTimeErrorCode
5923+
WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR = CompileTimeErrorCode(
5924+
'WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR',
5925+
"The constructor '{0}.{1}` doesn't have type parameters.",
5926+
correctionMessage:
5927+
"Try removing the type arguments, or adding a class name, followed by "
5928+
"the type arguments, then the constructor name.",
5929+
hasPublishedDocs: true,
5930+
uniqueName: 'WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR',
5931+
);
5932+
59195933
/// Parameters:
59205934
/// 0: the number of type parameters that were declared
59215935
/// 1: the number of type arguments provided

pkg/analyzer/lib/src/error/error_code_values.g.dart

+1
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ const List<DiagnosticCode> errorCodeValues = [
587587
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS,
588588
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_ANONYMOUS_FUNCTION,
589589
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR,
590+
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR,
590591
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_ENUM,
591592
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_EXTENSION,
592593
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS_FUNCTION,

pkg/analyzer/lib/src/fasta/ast_builder.dart

-1
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,6 @@ class AstBuilder extends StackListener {
16201620
}
16211621

16221622
var dotShorthand = pop() as DotShorthandInvocationImpl;
1623-
// TODO(kallentu): Report error if there are type arguments here.
16241623
push(
16251624
DotShorthandConstructorInvocationImpl(
16261625
constKeyword: token,

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

+2
Original file line numberDiff line numberDiff line change
@@ -4781,6 +4781,8 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
47814781
nameNodeName is PrefixedIdentifier
47824782
? nameNodeName.identifier.name
47834783
: '${nameNodeName.name}.new';
4784+
} else if (nameNode is DotShorthandConstructorInvocation) {
4785+
name = nameNode.constructorName.name;
47844786
} else if (nameNode is DotShorthandInvocation) {
47854787
name = nameNode.memberName.name;
47864788
} else {

pkg/analyzer/messages.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -18392,6 +18392,15 @@ CompileTimeErrorCode:
1839218392
}
1839318393
C f() => C.named();
1839418394
```
18395+
WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR:
18396+
sharedName: WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR
18397+
problemMessage: "The constructor '{0}.{1}` doesn't have type parameters."
18398+
correctionMessage: Try removing the type arguments, or adding a class name, followed by the type arguments, then the constructor name.
18399+
hasPublishedDocs: true
18400+
comment: |-
18401+
Parameters:
18402+
0: the name of the class being instantiated
18403+
1: the name of the constructor being invoked
1839518404
WRONG_NUMBER_OF_TYPE_ARGUMENTS_ENUM:
1839618405
problemMessage: The enum is declared with {0} type parameters, but {1} type arguments were given.
1839718406
correctionMessage: Try adjusting the number of type arguments.

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

+58
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,62 @@ DotShorthandConstructorInvocation
519519
staticType: C
520520
''');
521521
}
522+
523+
test_typeParameters() async {
524+
await assertErrorsInCode(
525+
r'''
526+
class C {
527+
C();
528+
}
529+
530+
void main() {
531+
C c = .new<int>();
532+
print(c);
533+
}
534+
''',
535+
[
536+
error(
537+
CompileTimeErrorCode
538+
.WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR,
539+
46,
540+
5,
541+
),
542+
],
543+
);
544+
}
545+
546+
test_typeParameters_const() async {
547+
await assertErrorsInCode(
548+
r'''
549+
class C {
550+
const C();
551+
}
552+
553+
void main() {
554+
C c = const .new<int>();
555+
print(c);
556+
}
557+
''',
558+
[
559+
error(
560+
CompileTimeErrorCode
561+
.WRONG_NUMBER_OF_TYPE_ARGUMENTS_DOT_SHORTHAND_CONSTRUCTOR,
562+
58,
563+
5,
564+
),
565+
],
566+
);
567+
}
568+
569+
test_typeParameters_missingContext() async {
570+
await assertErrorsInCode(
571+
r'''
572+
void main() {
573+
var c = const .new<int>();
574+
print(c);
575+
}
576+
''',
577+
[error(CompileTimeErrorCode.DOT_SHORTHAND_MISSING_CONTEXT, 24, 17)],
578+
);
579+
}
522580
}

tests/language/dot_shorthands/type_parameter/type_parameter_error_test.dart

+25-14
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,48 @@ extension type ET<T>(T v) {}
1818

1919
void main() {
2020
StaticMember<int> s = .memberType<String, String>('s');
21-
// ^
22-
// [analyzer] unspecified
21+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
2323
// [cfe] A value of type 'StaticMember<String>' can't be assigned to a variable of type 'StaticMember<int>'.
2424

2525
// Constructors doesn't have type parameters.
2626
StaticMember<int> constructorTypeParameter = .constNamed<int>(1);
27-
// ^^^^^^^^^^
28-
// [analyzer] unspecified
27+
// ^
2928
// [cfe] A dot shorthand constructor invocation can't have type arguments.
29+
// ^^^^^
30+
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR
3031

3132
// `.new<type-args>()` and `.new<type-args>` are a compile-time error.
3233
UnnamedConstructorTypeParameters typeParameters = .new<int>();
33-
// ^^^
34-
// [analyzer] unspecified
34+
// ^
3535
// [cfe] A dot shorthand constructor invocation can't have type arguments.
36+
// ^^^^^
37+
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_TYPE_ARGUMENTS_CONSTRUCTOR
3638

3739
UnnamedConstructorTypeParameters Function() tearOff = .new<int>;
38-
// ^^^
39-
// [analyzer] unspecified
40+
// ^^^^
41+
// [analyzer] COMPILE_TIME_ERROR.DISALLOWED_TYPE_INSTANTIATION_EXPRESSION
42+
// [analyzer] COMPILE_TIME_ERROR.DOT_SHORTHAND_MISSING_CONTEXT
43+
// ^
4044
// [cfe] The static getter or field 'new' isn't defined for the type 'UnnamedConstructorTypeParameters<dynamic> Function()'.
4145

4246
C newTearoff = .new<int>;
43-
// ^^^
44-
// [analyzer] unspecified
47+
// ^^^^^^^^^
48+
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
49+
// ^
4550
// [cfe] A dot shorthand constructor invocation can't have type arguments.
51+
// ^^^^^
52+
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_TYPE_ARGUMENTS_FUNCTION
4653
C namedTearoff = .new<int>;
47-
// ^^^
48-
// [analyzer] unspecified
54+
// ^^^^^^^^^
55+
// [analyzer] COMPILE_TIME_ERROR.INVALID_ASSIGNMENT
56+
// ^
4957
// [cfe] A dot shorthand constructor invocation can't have type arguments.
58+
// ^^^^^
59+
// [analyzer] COMPILE_TIME_ERROR.WRONG_NUMBER_OF_TYPE_ARGUMENTS_FUNCTION
5060
ET e = .new<int>;
51-
// ^^^
52-
// [analyzer] unspecified
61+
// ^^^^
62+
// [analyzer] COMPILE_TIME_ERROR.DISALLOWED_TYPE_INSTANTIATION_EXPRESSION
63+
// ^
5364
// [cfe] A dot shorthand constructor invocation can't have type arguments.
5465
}

0 commit comments

Comments
 (0)