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
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. 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

[Edit: The first version of this comment was not quite careful enough: It referred to #50383 which is about inference only, but the example in this issue does not rely on type inference. So I've basically rewritten this comment from scratch, reaching the opposite conclusion.]

The language specification says that

It is a compile-time error if the value of a constant expression depends on itself.

The null safety feature specification does not modify or add anything to this rule, nor does any other accepted feature specification as far as I can see.

It is not possible to refer to the value of a constant expression (or any expression) directly, it only occurs when that expression is the initializing expression in a constant variable declaration (or some subexpression thereof), such that we can refer to the value (or to some object that indirectly provides access to the value of that subexpression). This means that the above rule must give rise to an error at or in the same declarations as the following rule:

'It is a compile-time error if the value of a constant variable depends on the value of that same constant variable.'

This means that it is not sufficient to have a syntactic cycle (in the example: The declaration of the variable one contains a reference to one). Instead, it is required that the evaluation of the value can only be completed if that same value can be obtained during this evaluation.

This is not true when it comes to the example in this issue, because the value of one is not needed in order to evaluate 2 > 1 ? null : one.

This means that no error should be reported, and the analyzer should be adjusted accordingly.

@eernstg eernstg added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-spec Issues with the analyzer's implementation of the language spec 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 area-front-end Use area-front-end for front end / CFE / kernel format related issues. labels Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. 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