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