Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e8d5008

Browse files
scheglovCommit Bot
authored andcommitted
Update validation for final fields with const enum constructor.
For now just making it to not crash. Change-Id: I415f0c7e17cc959f26f87acd2d662e7f6de3b39b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229780 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 1708703 commit e8d5008

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

pkg/analyzer/lib/src/dart/ast/extensions.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ DartType? _writeType(AstNode node) {
7676
return null;
7777
}
7878

79+
extension AstNodeNullableExtension on AstNode? {
80+
List<ClassMember> get classMembers {
81+
final self = this;
82+
if (self is ClassOrMixinDeclaration) {
83+
return self.members;
84+
} else if (self is EnumDeclaration) {
85+
return self.members;
86+
} else if (self is ExtensionDeclaration) {
87+
return self.members;
88+
} else {
89+
throw UnimplementedError('(${self.runtimeType}) $self');
90+
}
91+
}
92+
}
93+
7994
extension ConstructorDeclarationExtension on ConstructorDeclaration {
8095
bool get isNonRedirectingGenerative {
8196
// Must be generative.

pkg/analyzer/lib/src/dart/constant/constant_verifier.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
103103
if (constKeyword != null) {
104104
_validateConstructorInitializers(node);
105105
if (node.factoryKeyword == null) {
106-
_validateFieldInitializers(
107-
node.parent as ClassOrMixinDeclaration, constKeyword);
106+
_validateFieldInitializers(node.parent.classMembers, constKeyword);
108107
}
109108
}
110109
_validateDefaultValues(node.parameters);
@@ -545,12 +544,11 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
545544
}
546545

547546
/// Validates that the expressions of any field initializers in
548-
/// [classDeclaration] are all compile-time constants. Since this is only
547+
/// [members] are all compile-time constants. Since this is only
549548
/// required if the class has a constant constructor, the error is reported at
550549
/// [constKeyword], the const keyword on such a constant constructor.
551550
void _validateFieldInitializers(
552-
ClassOrMixinDeclaration classDeclaration, Token constKeyword) {
553-
NodeList<ClassMember> members = classDeclaration.members;
551+
List<ClassMember> members, Token constKeyword) {
554552
for (ClassMember member in members) {
555553
if (member is FieldDeclaration && !member.isStatic) {
556554
for (VariableDeclaration variableDeclaration

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
202202

203203
/// The class containing the AST nodes being visited, or `null` if we are not
204204
/// in the scope of a class.
205-
ClassElementImpl? _enclosingClass;
206-
207-
/// The enum containing the AST nodes being visited, or `null` if we are not
208-
/// in the scope of an enum.
209-
ClassElement? _enclosingEnum;
205+
ClassElement? _enclosingClass;
210206

211207
/// The element of the extension being visited, or `null` if we are not
212208
/// in the scope of an extension.
@@ -281,11 +277,8 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
281277
/// should not be modified in the middle of visiting a tree and requires an
282278
/// analyzer-provided Impl instance to work.
283279
set enclosingClass(ClassElement? classElement) {
284-
assert(classElement is ClassElementImpl);
285280
assert(_enclosingClass == null);
286-
assert(_enclosingEnum == null);
287281
assert(_enclosingExecutable.element == null);
288-
_enclosingClass = classElement as ClassElementImpl;
289282
}
290283

291284
/// The language team is thinking about adding abstract fields, or external
@@ -563,13 +556,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
563556

564557
@override
565558
void visitEnumDeclaration(EnumDeclaration node) {
566-
var outerEnum = _enclosingEnum;
559+
var outerClass = _enclosingClass;
567560
try {
568-
_enclosingEnum = node.declaredElement;
561+
_enclosingClass = node.declaredElement;
569562
_duplicateDefinitionVerifier.checkEnum(node);
570563
super.visitEnumDeclaration(node);
571564
} finally {
572-
_enclosingEnum = outerEnum;
565+
_enclosingClass = outerClass;
573566
}
574567
}
575568

@@ -4408,9 +4401,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
44084401
if (identical(enclosingElement, _enclosingClass)) {
44094402
return;
44104403
}
4411-
if (identical(enclosingElement, _enclosingEnum)) {
4412-
return;
4413-
}
44144404
if (enclosingElement is! ClassElement) {
44154405
return;
44164406
}

0 commit comments

Comments
 (0)