Skip to content

Commit 808911b

Browse files
scheglovCommit Queue
authored andcommitted
Issue 60172. Fix for resolving metadata with extension types.
Bug: #60172 Bug: #60174 Change-Id: I0e4fb54fdc8419cf0eef7fd095a94fc10e5ec9dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411081 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 2aa4dad commit 808911b

File tree

4 files changed

+113
-13
lines changed

4 files changed

+113
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AnnotationResolver {
3434

3535
void _classConstructorInvocation(
3636
AnnotationImpl node,
37-
ClassElement classElement,
37+
InterfaceElement classElement,
3838
SimpleIdentifierImpl? constructorName,
3939
ArgumentListImpl argumentList,
4040
List<WhyNotPromotedGetter> whyNotPromotedArguments,
@@ -237,7 +237,7 @@ class AnnotationResolver {
237237

238238
// Class(args) or Class.CONST
239239
if (element1 is InterfaceElement) {
240-
if (element1 is ClassElement && argumentList != null) {
240+
if (argumentList != null) {
241241
_classConstructorInvocation(
242242
node, element1, name2, argumentList, whyNotPromotedArguments);
243243
} else {

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,84 @@ D
657657
''');
658658
}
659659

660+
test_value_extensionType_namedConstructor() async {
661+
await assertNoErrorsInCode(r'''
662+
extension type const A.named(int it) {}
663+
664+
@A.named(42)
665+
void f() {}
666+
''');
667+
668+
var node = findNode.singleAnnotation;
669+
assertResolvedNodeText(node, r'''
670+
Annotation
671+
atSign: @
672+
name: PrefixedIdentifier
673+
prefix: SimpleIdentifier
674+
token: A
675+
staticElement: <testLibraryFragment>::@extensionType::A
676+
element: <testLibrary>::@extensionType::A
677+
staticType: null
678+
period: .
679+
identifier: SimpleIdentifier
680+
token: named
681+
staticElement: <testLibraryFragment>::@extensionType::A::@constructor::named
682+
element: <testLibraryFragment>::@extensionType::A::@constructor::named#element
683+
staticType: null
684+
staticElement: <testLibraryFragment>::@extensionType::A::@constructor::named
685+
element: <testLibraryFragment>::@extensionType::A::@constructor::named#element
686+
staticType: null
687+
arguments: ArgumentList
688+
leftParenthesis: (
689+
arguments
690+
IntegerLiteral
691+
literal: 42
692+
parameter: <testLibraryFragment>::@extensionType::A::@constructor::named::@parameter::it
693+
staticType: int
694+
rightParenthesis: )
695+
element: <testLibraryFragment>::@extensionType::A::@constructor::named
696+
element2: <testLibraryFragment>::@extensionType::A::@constructor::named#element
697+
''');
698+
699+
_assertAnnotationValueText(node, r'''
700+
int 42
701+
''');
702+
}
703+
704+
test_value_extensionType_unnamedConstructor() async {
705+
await assertNoErrorsInCode(r'''
706+
extension type const A(int it) {}
707+
708+
@A(42)
709+
void f() {}
710+
''');
711+
712+
var node = findNode.singleAnnotation;
713+
assertResolvedNodeText(node, r'''
714+
Annotation
715+
atSign: @
716+
name: SimpleIdentifier
717+
token: A
718+
staticElement: <testLibraryFragment>::@extensionType::A
719+
element: <testLibrary>::@extensionType::A
720+
staticType: null
721+
arguments: ArgumentList
722+
leftParenthesis: (
723+
arguments
724+
IntegerLiteral
725+
literal: 42
726+
parameter: <testLibraryFragment>::@extensionType::A::@constructor::new::@parameter::it
727+
staticType: int
728+
rightParenthesis: )
729+
element: <testLibraryFragment>::@extensionType::A::@constructor::new
730+
element2: <testLibraryFragment>::@extensionType::A::@constructor::new#element
731+
''');
732+
733+
_assertAnnotationValueText(node, r'''
734+
int 42
735+
''');
736+
}
737+
660738
test_value_genericClass_downwards_inference_namedConstructor() async {
661739
await assertNoErrorsInCode(r'''
662740
class A<T> {

pkg/analyzer/test/src/diagnostics/argument_type_not_assignable_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ f() {
3737
expect(message.contains("_A"), isTrue);
3838
}
3939

40+
test_annotation_extensionType() async {
41+
await assertErrorsInCode('''
42+
extension type const A(String _) {}
43+
44+
@A(0)
45+
void f() {}
46+
''', [
47+
error(CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, 40, 1),
48+
]);
49+
}
50+
4051
test_annotation_namedConstructor() async {
4152
await assertErrorsInCode('''
4253
class A {

pkg/analyzer/test/src/diagnostics/extra_positional_arguments_test.dart

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ main() {
1717
@reflectiveTest
1818
class ExtraPositionalArgumentsCouldBeNamedTest
1919
extends PubPackageResolutionTest {
20-
test_constConstructor() async {
20+
test_class_constConstructor() async {
2121
await assertErrorsInCode(r'''
2222
class A {
2323
const A({int x = 0});
@@ -31,7 +31,7 @@ main() {
3131
]);
3232
}
3333

34-
test_constConstructor_super() async {
34+
test_class_constConstructor_super() async {
3535
await assertErrorsInCode(r'''
3636
class A {
3737
const A({int x = 0});
@@ -45,7 +45,7 @@ class B extends A {
4545
]);
4646
}
4747

48-
test_constConstructor_typedef() async {
48+
test_class_constConstructor_typedef() async {
4949
await assertErrorsInCode(r'''
5050
class A {
5151
const A({int x = 0});
@@ -77,7 +77,18 @@ main() {
7777
findNode.methodInvocation('f()').typeArgumentTypes!.single, 'dynamic');
7878
}
7979

80-
test_enumConstant() async {
80+
test_functionExpressionInvocation() async {
81+
await assertErrorsInCode('''
82+
main() {
83+
(int x, {int y = 0}) {} (0, 1);
84+
}
85+
''', [
86+
error(CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS_COULD_BE_NAMED, 39,
87+
1),
88+
]);
89+
}
90+
91+
test_metadata_enumConstant() async {
8192
await assertErrorsInCode(r'''
8293
enum E {
8394
v(0);
@@ -90,14 +101,14 @@ enum E {
90101
]);
91102
}
92103

93-
test_functionExpressionInvocation() async {
94-
await assertErrorsInCode('''
95-
main() {
96-
(int x, {int y = 0}) {} (0, 1);
97-
}
104+
test_metadata_extensionType() async {
105+
await assertErrorsInCode(r'''
106+
extension type const A(int it) {}
107+
108+
@A(0, 1)
109+
void f() {}
98110
''', [
99-
error(CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS_COULD_BE_NAMED, 39,
100-
1),
111+
error(CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS, 41, 1),
101112
]);
102113
}
103114

0 commit comments

Comments
 (0)