From e7461ea9e7de9bdf8052def1320011254cebfdef Mon Sep 17 00:00:00 2001 From: Chloe Stefantsova Date: Wed, 20 May 2026 06:22:16 -0700 Subject: [PATCH] [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 Commit-Queue: Chloe Stefantsova --- .../lib/src/kernel/constant_evaluator.dart | 18 +++++++++++++++++- .../test/spell_checking_list_common.txt | 1 + .../constant_local_variable.dart | 4 ++++ .../constant_local_variable.dart.strong.expect | 16 ++++++++++++++++ ...t_local_variable.dart.strong.modular.expect | 16 ++++++++++++++++ ...t_local_variable.dart.strong.outline.expect | 5 +++++ ..._local_variable.dart.textual_outline.expect | 1 + ...riable.dart.textual_outline_modelled.expect | 1 + pkg/front_end/testcases/modular.status | 3 ++- pkg/front_end/testcases/strong.status | 3 ++- 10 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart create mode 100644 pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.expect create mode 100644 pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.modular.expect create mode 100644 pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.outline.expect create mode 100644 pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline.expect create mode 100644 pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline_modelled.expect diff --git a/pkg/front_end/lib/src/kernel/constant_evaluator.dart b/pkg/front_end/lib/src/kernel/constant_evaluator.dart index 8c453e38ad3..111070f3fcc 100644 --- a/pkg/front_end/lib/src/kernel/constant_evaluator.dart +++ b/pkg/front_end/lib/src/kernel/constant_evaluator.dart @@ -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); } diff --git a/pkg/front_end/test/spell_checking_list_common.txt b/pkg/front_end/test/spell_checking_list_common.txt index 14b670d13bd..572be2bcb4a 100644 --- a/pkg/front_end/test/spell_checking_list_common.txt +++ b/pkg/front_end/test/spell_checking_list_common.txt @@ -3361,6 +3361,7 @@ unhelpful unicode unified uniformity +uniformly unifying unimplemented uninitialized diff --git a/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart new file mode 100644 index 00000000000..52114a12742 --- /dev/null +++ b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart @@ -0,0 +1,4 @@ +test() { + const x = ""; + return x; +} diff --git a/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.expect b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.expect new file mode 100644 index 00000000000..b1548e0e714 --- /dev/null +++ b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.expect @@ -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 = "" +} diff --git a/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.modular.expect b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.modular.expect new file mode 100644 index 00000000000..b1548e0e714 --- /dev/null +++ b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.modular.expect @@ -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 = "" +} diff --git a/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.outline.expect b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.outline.expect new file mode 100644 index 00000000000..531df3f6ed5 --- /dev/null +++ b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.strong.outline.expect @@ -0,0 +1,5 @@ +library; +import self as self; + +static method test() → dynamic + ; diff --git a/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline.expect b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline.expect new file mode 100644 index 00000000000..a9f9e5f44ab --- /dev/null +++ b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline.expect @@ -0,0 +1 @@ +test() {} diff --git a/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline_modelled.expect b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline_modelled.expect new file mode 100644 index 00000000000..a9f9e5f44ab --- /dev/null +++ b/pkg/front_end/testcases/closure_context_lowering/constant_local_variable.dart.textual_outline_modelled.expect @@ -0,0 +1 @@ +test() {} diff --git a/pkg/front_end/testcases/modular.status b/pkg/front_end/testcases/modular.status index 7a635f042c7..3d90f5db388 100644 --- a/pkg/front_end/testcases/modular.status +++ b/pkg/front_end/testcases/modular.status @@ -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 \ No newline at end of file +closure_context_lowering/redirecting_factories: ExpectationFileMismatchSerialized +closure_context_lowering/constant_local_variable: Crash \ No newline at end of file diff --git a/pkg/front_end/testcases/strong.status b/pkg/front_end/testcases/strong.status index 18e12cddf25..2657d760ea7 100644 --- a/pkg/front_end/testcases/strong.status +++ b/pkg/front_end/testcases/strong.status @@ -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 \ No newline at end of file +closure_context_lowering/redirecting_factories: ExpectationFileMismatchSerialized +closure_context_lowering/constant_local_variable: Crash \ No newline at end of file