Skip to content

Commit

Permalink
[CFE] Fix crash when metadata references locals in extension type
Browse files Browse the repository at this point in the history
Having metadata on several variables means it has to be cloned which
(used to) crash.

Change-Id: I0ed1d4b51cec98adb23674d144d3970d3db9d69d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398060
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Jens Johansen <[email protected]>
  • Loading branch information
jensjoha authored and Commit Queue committed Nov 28, 2024
1 parent eb54636 commit 3572482
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/front_end/lib/src/kernel/body_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3288,7 +3288,7 @@ class BodyBuilder extends StackListenerImpl
VariableBuilder variableBuilder = declaration as VariableBuilder;
if (constantContext != ConstantContext.none &&
!variableBuilder.isConst &&
!_context.isConstructor &&
!(_context.isConstructor && inFieldInitializer) &&
!libraryFeatures.constFunctions.isEnabled) {
return new IncompleteErrorGenerator(
this, nameToken, cfe.messageNotAConstantExpression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

extension type Foo._(int _x) {
Foo(this._x, int bar){
@bar
int a1, a2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: Not a constant expression.
// @bar
// ^^^
//
// pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
// @bar
// ^
//
import self as self;
import "dart:core" as core;

extension type Foo(core::int _x) {
abstract extension-type-member representation-field get _x() → core::int;
constructor _ = self::Foo|constructor#_;
constructor tearoff _ = self::Foo|constructor#_#_#tearOff;
constructor • = self::Foo|constructor#;
constructor tearoff • = self::Foo|constructor#_#new#tearOff;
}
static extension-type-member method Foo|constructor#_(core::int _x) → self::Foo% /* erasure=core::int, declared=! */ {
lowered final self::Foo% /* erasure=core::int, declared=! */ #this = _x;
return #this;
}
static extension-type-member method Foo|constructor#_#_#tearOff(core::int _x) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#_(_x);
static extension-type-member method Foo|constructor#(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */ {
lowered final self::Foo% /* erasure=core::int, declared=! */ #this = _x;
{
@invalid-expression "pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
@bar
^" core::int a1;
@invalid-expression "pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
@bar
^" core::int a2;
}
return #this;
}
static extension-type-member method Foo|constructor#_#new#tearOff(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#(_x, bar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: Not a constant expression.
// @bar
// ^^^
//
// pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
// @bar
// ^
//
import self as self;
import "dart:core" as core;

extension type Foo(core::int _x) {
abstract extension-type-member representation-field get _x() → core::int;
constructor _ = self::Foo|constructor#_;
constructor tearoff _ = self::Foo|constructor#_#_#tearOff;
constructor • = self::Foo|constructor#;
constructor tearoff • = self::Foo|constructor#_#new#tearOff;
}
static extension-type-member method Foo|constructor#_(core::int _x) → self::Foo% /* erasure=core::int, declared=! */ {
lowered final self::Foo% /* erasure=core::int, declared=! */ #this = _x;
return #this;
}
static extension-type-member method Foo|constructor#_#_#tearOff(core::int _x) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#_(_x);
static extension-type-member method Foo|constructor#(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */ {
lowered final self::Foo% /* erasure=core::int, declared=! */ #this = _x;
{
@invalid-expression "pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
@bar
^" core::int a1;
@invalid-expression "pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
@bar
^" core::int a2;
}
return #this;
}
static extension-type-member method Foo|constructor#_#new#tearOff(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#(_x, bar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library;
import self as self;
import "dart:core" as core;

extension type Foo(core::int _x) {
abstract extension-type-member representation-field get _x() → core::int;
constructor _ = self::Foo|constructor#_;
constructor tearoff _ = self::Foo|constructor#_#_#tearOff;
constructor • = self::Foo|constructor#;
constructor tearoff • = self::Foo|constructor#_#new#tearOff;
}
static extension-type-member method Foo|constructor#_(core::int _x) → self::Foo% /* erasure=core::int, declared=! */
;
static extension-type-member method Foo|constructor#_#_#tearOff(core::int _x) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#_(_x);
static extension-type-member method Foo|constructor#(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */
;
static extension-type-member method Foo|constructor#_#new#tearOff(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#(_x, bar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
library;
//
// Problems in library:
//
// pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: Not a constant expression.
// @bar
// ^^^
//
// pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
// @bar
// ^
//
import self as self;
import "dart:core" as core;

extension type Foo(core::int _x) {
abstract extension-type-member representation-field get _x() → core::int;
constructor _ = self::Foo|constructor#_;
constructor tearoff _ = self::Foo|constructor#_#_#tearOff;
constructor • = self::Foo|constructor#;
constructor tearoff • = self::Foo|constructor#_#new#tearOff;
}
static extension-type-member method Foo|constructor#_(core::int _x) → self::Foo% /* erasure=core::int, declared=! */ {
lowered final self::Foo% /* erasure=core::int, declared=! */ #this = _x;
return #this;
}
static extension-type-member method Foo|constructor#_#_#tearOff(core::int _x) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#_(_x);
static extension-type-member method Foo|constructor#(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */ {
lowered final self::Foo% /* erasure=core::int, declared=! */ #this = _x;
{
@invalid-expression "pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
@bar
^" core::int a1;
@invalid-expression "pkg/front_end/testcases/regress/extension_type_metadata_references_variable.dart:7:6: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor.
@bar
^" core::int a2;
}
return #this;
}
static extension-type-member method Foo|constructor#_#new#tearOff(core::int _x, core::int bar) → self::Foo% /* erasure=core::int, declared=! */
return self::Foo|constructor#(_x, bar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extension type Foo._(int _x) {
Foo(this._x, int bar) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extension type Foo._(int _x) {
Foo(this._x, int bar) {}
}

0 comments on commit 3572482

Please sign in to comment.