Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No error if a non-constant is used in a constant constructor initializer list #59804

Open
sgrekhov opened this issue Dec 25, 2024 · 1 comment
Assignees
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-dysfunctionalities Issues for the CFE not behaving as intended type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@sgrekhov
Copy link
Contributor

The code below produces an error in the analyzer but works in VM.

class A {
  int id;
  A(this.id);
  static A answer = A(42);
}

class C {
  final A a;
  const C(this.a);
  const C.fromAnswer() : a = A.answer; // Analyzer: Invalid constant value.
}

main() {
  print(C.fromAnswer().a.id); // prints 42
}

There should be an error in VM as well.

Dart SDK version: 3.7.0-274.0.dev (dev) (Thu Dec 19 20:06:50 2024 -0800) on "windows_x64"

@sgrekhov sgrekhov added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label Dec 25, 2024
@lrhn lrhn added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Dec 25, 2024
@a-siva a-siva added area-front-end Use area-front-end for front end / CFE / kernel format related issues. and removed area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. labels Jan 8, 2025
@johnniwinther johnniwinther added the cfe-dysfunctionalities Issues for the CFE not behaving as intended label Jan 9, 2025
@lrhn
Copy link
Member

lrhn commented Jan 9, 2025

At least the front-end constant evaluator does recognize that A.answer is not valid if the constructor is invoked with const, so we won't have mutable constants. (Phew!)

It is a specified error. A.answer is a reference to a getter which isn't a constant getter, and isn't .length of a String, so it's not a potentially constant expression.

Not a big problem, you can't invoke the constructor using const. The rules are there to avoid people writing const constructors that can't actually be invoked as const, because it's not useful to allow something that the compiler can easily see will never work.
The fix here is to remove const from the C.fromAnswer constructor and then every existing successful program would still work, since there are no successful constant invocations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-dysfunctionalities Issues for the CFE not behaving as intended type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants