From 988c3011096fcab0bc26706a33e765497923897c Mon Sep 17 00:00:00 2001 From: Kallen Tu Date: Thu, 2 Nov 2023 20:46:20 +0000 Subject: [PATCH] [analyzer] Issue 53927: Disallow final fields to be used in a const context. Context: Added a field check in https://dart-review.googlesource.com/c/sdk/+/312347 which the goal back then was to cut down on more unnecessary errors, but this seemed to have caused a regression elsewhere. Reverting this part of the change and other related tests. Bug: https://github.com/dart-lang/sdk/issues/53927 Change-Id: I0774e050c73e677347caad836dd59c9cba71d044 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333240 Reviewed-by: Brian Wilkerson Commit-Queue: Kallen Tu --- .../lib/src/dart/constant/evaluation.dart | 4 +--- .../src/dart/constant/evaluation_test.dart | 22 +++++++++++++++++++ .../initializer_instance_reference_test.dart | 2 ++ .../initializer_instance_reference_test.dart | 2 ++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart index b5903765a22..56f41fd13a3 100644 --- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart +++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart @@ -1684,9 +1684,7 @@ class ConstantVisitor extends UnifyingAstVisitor { // already computed values of all dependencies first (or detect a cycle), // so the value has already been computed and we can just return it. var evaluationResult = variableElement.evaluationResult; - var isConstField = variableElement is FieldElement && - (variableElement.isConst || variableElement.isFinal); - if (isConstField || variableElement.isConst) { + if (variableElement.isConst) { switch (evaluationResult) { case null: // The constant value isn't computed yet, or there is an error while diff --git a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart index 52d02f81311..49884ffd1d4 100644 --- a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart +++ b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart @@ -2017,6 +2017,28 @@ List '''); } + test_visitListLiteral_listElement_field_final() async { + await assertErrorsInCode(r''' +class A { + final String bar = ''; + const A(); + List foo() => const [bar]; +} +''', [ + error(CompileTimeErrorCode.NON_CONSTANT_LIST_ELEMENT, 79, 3), + ]); + } + + test_visitListLiteral_listElement_field_static() async { + await assertNoErrorsInCode(r''' +class A { + static const String bar = ''; + const A(); + List foo() => const [bar]; +} +'''); + } + test_visitListLiteral_listElement_simple() async { await assertNoErrorsInCode(r''' const x = ['a', 'b', 'c']; diff --git a/tests/language/final/initializer_instance_reference_test.dart b/tests/language/final/initializer_instance_reference_test.dart index e0bf3f2544d..be6ea475952 100644 --- a/tests/language/final/initializer_instance_reference_test.dart +++ b/tests/language/final/initializer_instance_reference_test.dart @@ -7,6 +7,8 @@ class C { const C(); +//^^^^^ +// [analyzer] COMPILE_TIME_ERROR.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZED_BY_NON_CONST final x = 1; final y = x; diff --git a/tests/language_2/final/initializer_instance_reference_test.dart b/tests/language_2/final/initializer_instance_reference_test.dart index 8edb007c7eb..be02d1ec0a6 100644 --- a/tests/language_2/final/initializer_instance_reference_test.dart +++ b/tests/language_2/final/initializer_instance_reference_test.dart @@ -9,6 +9,8 @@ class C { const C(); +//^^^^^ +// [analyzer] COMPILE_TIME_ERROR.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZED_BY_NON_CONST final x = 1; final y = x;