[cfe] Adjust initializer's parent in ConstantsTransformer

The parent of the initializer is set to the `VariableInitialization`
node, while previously it was set to the corresponding variable. There
is no observable change for the ouptut for the old variables. For the
new variables, the adjustments restores the expected parent relation
and addresses some verification issues.

Part of https://github.com/dart-lang/sdk/issues/61572

Change-Id: Ie81b1a06078867076c4aad42fb213fd09e4a3f73
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/504960
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Chloe Stefantsova
2026-05-20 06:22:16 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent bc16ca4d51
commit e7461ea9e7
10 changed files with 65 additions and 3 deletions
@@ -2264,10 +2264,26 @@ class ConstantsTransformer extends RemovingTransformer {
TreeNode visitVariableGet(VariableGet node, TreeNode? removalSentinel) {
final VariableDeclaration variable = node.variable;
if (variable.isConst) {
// TODO(63414): Obtain the initializer parent uniformly rather than using
// the 'switch'.
TreeNode? initializerParent = switch (variable) {
LegacyVariable() => variable,
LocalVariable() => variable.variableInitialization,
// Coverage-ignore(suite): Not run.
CatchVariable() => variable.variableInitialization,
// Coverage-ignore(suite): Not run.
ThisVariable() => variable.variableInitialization,
// Coverage-ignore(suite): Not run.
SyntheticVariable() => variable.variableInitialization,
// Coverage-ignore(suite): Not run.
PositionalParameter() => variable.variableInitialization,
// Coverage-ignore(suite): Not run.
NamedParameter() => variable.variableInitialization,
};
variable.initializer = evaluateAndTransformWithContext(
variable,
variable.initializer!,
)..parent = variable;
)..parent = initializerParent;
if (shouldInline(variable.initializer!)) {
return evaluateAndTransformWithContext(node, node);
}
@@ -3361,6 +3361,7 @@ unhelpful
unicode
unified
uniformity
uniformly
unifying
unimplemented
uninitialized
@@ -0,0 +1,4 @@
test() {
const x = "";
return x;
}
@@ -0,0 +1,16 @@
library;
import self as self;
import "dart:core" as core;
static method test() → dynamic /* scope=[
#ctx1: not-captured VariableContext([
local-variable const x;
]),
] */ {
x := #C1;
return #C1;
}
constants {
#C1 = ""
}
@@ -0,0 +1,16 @@
library;
import self as self;
import "dart:core" as core;
static method test() → dynamic /* scope=[
#ctx1: not-captured VariableContext([
local-variable const x;
]),
] */ {
x := #C1;
return #C1;
}
constants {
#C1 = ""
}
@@ -0,0 +1,5 @@
library;
import self as self;
static method test() → dynamic
;
+2 -1
View File
@@ -36,4 +36,5 @@ closure_context_lowering/assert_captured_variables: ExpectationFileMismatchSeria
closure_context_lowering/constructor_initializers: ExpectationFileMismatchSerialized
closure_context_lowering/super_initializing_formal: ExpectationFileMismatchSerialized
closure_context_lowering/late_field_initializers: Crash
closure_context_lowering/redirecting_factories: ExpectationFileMismatchSerialized
closure_context_lowering/redirecting_factories: ExpectationFileMismatchSerialized
closure_context_lowering/constant_local_variable: Crash
+2 -1
View File
@@ -274,4 +274,5 @@ closure_context_lowering/assert_captured_variables: ExpectationFileMismatchSeria
closure_context_lowering/constructor_initializers: ExpectationFileMismatchSerialized
closure_context_lowering/super_initializing_formal: ExpectationFileMismatchSerialized
closure_context_lowering/late_field_initializers: Crash
closure_context_lowering/redirecting_factories: ExpectationFileMismatchSerialized
closure_context_lowering/redirecting_factories: ExpectationFileMismatchSerialized
closure_context_lowering/constant_local_variable: Crash