@literal support for extension types

See: https://github.com/dart-lang/sdk/issues/53434

Change-Id: I2668b74e2037176d0ac6e6da22300be29ba14d8d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/325444
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
pq 2023-09-12 19:04:19 +00:00 committed by Commit Queue
parent 83b27b8bcf
commit ba70bed261
4 changed files with 68 additions and 1 deletions

View file

@ -197,6 +197,10 @@ class AnnotationVerifier {
/// `@literal` annotation.
void _checkLiteral(AstNode node) {
var parent = node.parent;
// Primary constructor of an extension type declaration.
if (parent is ExtensionTypeDeclaration && parent.constKeyword != null) {
return;
}
if (parent is! ConstructorDeclaration || parent.constKeyword == null) {
_errorReporter
.reportErrorForNode(WarningCode.INVALID_LITERAL_ANNOTATION, node, []);

View file

@ -1172,8 +1172,11 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
if (constructor == null) {
return;
}
var enclosingElement = constructor.enclosingElement;
if (!node.isConst &&
constructor.hasLiteral &&
(constructor.hasLiteral ||
(enclosingElement is ExtensionTypeElement &&
enclosingElement.hasLiteral)) &&
_linterContext.canBeConst(node)) {
// Echoing jwren's TODO from _checkForDeprecatedMemberUse:
// TODO(jwren) We should modify ConstructorElement.getDisplayName(), or

View file

@ -31,6 +31,24 @@ class A {
''');
}
test_extensionType_constConstructor() async {
await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
extension type const E(int i) {
@literal
const E.zero(): this(0);
}
''');
}
test_extensionType_primaryConstructor_const() async {
await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
@literal
extension type const E(int i) { }
''');
}
test_nonConstConstructor() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';

View file

@ -42,6 +42,15 @@ const a = A();
''');
}
test_constContextCreation_extensionType() async {
await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
@literal
extension type const E(int i) { }
const e = E(1);
''');
}
test_constCreation() async {
await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
@ -53,6 +62,15 @@ const a = const A();
''');
}
test_constCreation_extensionType() async {
await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
@literal
extension type const E(int i) { }
const e = const E(1);
''');
}
test_namedConstructor() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
@ -102,4 +120,28 @@ var a = new A();
error(WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR_USING_NEW, 77, 7),
]);
}
test_usingNew_extensionType() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
extension type const E(int i) {
@literal
const E.zero(): this(0);
}
E e = E.zero();
''', [
error(WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 112, 8),
]);
}
test_usingNew_extensionType_primaryConstructor() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
@literal
extension type const E(int i) { }
E e = E(1);
''', [
error(WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 82, 4),
]);
}
}