Report INVALID_ANNOTATION for not constant variable, resolve arguments.

Change-Id: Ie6109d253d825527c545d9129dca3b3a6896ca9f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219921
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-11-11 16:38:01 +00:00 committed by commit-bot@chromium.org
parent c12ef90b23
commit 009020db39
2 changed files with 48 additions and 1 deletions

View file

@ -263,6 +263,19 @@ class AnnotationResolver {
_visitArguments(node, whyNotPromotedList);
}
void _localVariable(
AnnotationImpl node,
VariableElement element,
List<WhyNotPromotedGetter> whyNotPromotedList,
) {
if (!element.isConst || node.arguments != null) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INVALID_ANNOTATION, node);
}
_visitArguments(node, whyNotPromotedList);
}
void _propertyAccessorElement(
AnnotationImpl node,
SimpleIdentifierImpl name,
@ -393,8 +406,8 @@ class AnnotationResolver {
return;
}
// TODO(scheglov) Must be const.
if (element1 is VariableElement) {
_localVariable(node, element1, whyNotPromotedList);
return;
}

View file

@ -110,6 +110,40 @@ main() {
]);
}
test_localVariable_const() async {
await assertNoErrorsInCode(r'''
void f() {
const a = 0;
@a
var b; // ignore:unused_local_variable
}
''');
}
test_localVariable_const_withArguments() async {
await assertErrorsInCode(r'''
void f() {
const a = 0;
@a(0)
var b; // ignore:unused_local_variable
}
''', [
error(CompileTimeErrorCode.INVALID_ANNOTATION, 28, 5),
]);
}
test_localVariable_final() async {
await assertErrorsInCode(r'''
void f() {
final a = 0;
@a
var b; // ignore:unused_local_variable
}
''', [
error(CompileTimeErrorCode.INVALID_ANNOTATION, 28, 2),
]);
}
test_notClass_importWithPrefix() async {
newFile('$testPackageLibPath/annotations.dart', content: r'''
class Property {