mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 11:58:13 +00:00
@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:
parent
83b27b8bcf
commit
ba70bed261
|
@ -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, []);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue