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

Different behavior of CFE and the analyzer when constant depends on itself #59945

Open
sgrekhov opened this issue Jan 21, 2025 · 3 comments
Open
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@sgrekhov
Copy link
Contributor

The following code produces an error in the analyzer but no issues in VM.

const int? one = 2 > 1 ? null : one; // Analyzer error: The compile-time constant expression depends on itself

main() {
  print(one); // prints null
}

If to change the example to really invoke the constant then VM produces an error as well.

const int? two = 2 > 1 ? two: null; // Error: Constant evaluation error. Context: Constant expression depends on itself.

main() {
  print(two);
}

@eernstg @lrhn does in the first case the constant really depends on itself?

@sgrekhov sgrekhov added the type-question A question about expected behavior or functionality label Jan 21, 2025
@dart-github-bot
Copy link
Collaborator

Summary: Analyzer detects self-referential constant one, but the VM doesn't, though a modified example (two) triggers VM error. Inconsistency needs investigation.

@dart-github-bot dart-github-bot added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Jan 21, 2025
@sgrekhov sgrekhov changed the title Differenf bahavior of VM and the analyzer when constant depends on itself Different behavior of VM and the analyzer when constant depends on itself Jan 21, 2025
@mraleph mraleph changed the title Different behavior of VM and the analyzer when constant depends on itself Different behavior of CFE and the analyzer when constant depends on itself Jan 21, 2025
@mraleph mraleph 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. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Jan 21, 2025
@mraleph
Copy link
Member

mraleph commented Jan 21, 2025

cc @eernstg

@bwilkerson bwilkerson added the P2 A bug or feature request we're likely to work on label Jan 21, 2025
@eernstg
Copy link
Member

eernstg commented Jan 23, 2025

The language team decided that cyclic dependencies should be computed based on a simple syntactic criterion: If a non-local variable declaration D1 has an initializing expression e where the name that resolves to a declaration D2 occurs then D1 depends on D2. A compile-time error occurs if the dependency graph has a cycle.

This means that a syntactically cyclic set of declarations will be reported as a compile-time error even in the case where some analysis can prove that no infinite loop will occur during initialization.

The implementation was handled in #50383. There was no need to change the specification.

We could try to introduce special rules about constant expressions because they have a control flow which is known at compile time (or link time, depending on the treatment of some constructors whose name includes fromEnvironment), but this should be done in a new issue in the language repository.

So I'd say that the analyzer is right and the CFE should be adjusted such that all syntactic circularities are reported. I changed the labels accordingly.

@eernstg eernstg added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-question A question about expected behavior or functionality labels Jan 23, 2025
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. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants