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

Analyzer doesn't allow ! selector in constants #59800

Open
sgrekhov opened this issue Dec 24, 2024 · 6 comments
Open

Analyzer doesn't allow ! selector in constants #59800

sgrekhov opened this issue Dec 24, 2024 · 6 comments
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 below works well in VM but produces an error in the analyzer.

const answer = "42"!; // Analyzer error. Const variables must be initialized with a constant value.

main() {
  print(answer);
}

Constant expression allows ! selector, therefore, I believe, it's an analyzer issue.

@lrhn @eernstg please confirm.

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-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Dec 24, 2024
@lrhn
Copy link
Member

lrhn commented Dec 25, 2024

I'd be fine with ! being a constant expression. However, I didn't find it in the "Constant Objects" section of the NNBD specification. Was it added later @eernstg?

(I did find a section saying that the section on as checks being covariant should be changed to'

\item An expression of the form \code{$e$\,\,as\,\,$T$} or \code{$e$\,\,is\,\,$T$} is potentially constant if $e$ is a potentially constant expression and $T$ is a potentially constant type expression, and it is further constant if $e$ is constant.

That should also say "and $T$ is constant" at the end.)

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Dec 27, 2024
@keertip keertip added analyzer-spec Issues with the analyzer's implementation of the language spec P3 A lower priority bug or feature request labels Dec 30, 2024
@eernstg
Copy link
Member

eernstg commented Jan 8, 2025

Same here, I can't see any justification for the claim that a constant expression can contain a selector of the form !, neither in the language specification, nor in the NNBD feature specification, nor in any other specification document that I could come up with. ! is only used as a boolean negation operator, in !=, and in is!.

@sgrekhov, did you find anything to the contrary somewhere?

This means that the CFE should be modified to report an error.

@eernstg eernstg added area-front-end Use area-front-end for front end / CFE / kernel format related issues. and removed area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request analyzer-spec Issues with the analyzer's implementation of the language spec labels Jan 8, 2025
@eernstg
Copy link
Member

eernstg commented Jan 8, 2025

@keertip, I adjusted the labels. PTAL if you disagree.

@eernstg
Copy link
Member

eernstg commented Jan 8, 2025

We could consider generalizing constant expressions to include this case, but that would be a fresh language repo issue, so I won't proceed with anything of that nature here.

@johnniwinther johnniwinther added the cfe-dysfunctionalities Issues for the CFE not behaving as intended label Jan 8, 2025
@sgrekhov
Copy link
Contributor Author

sgrekhov commented Jan 8, 2025

@eernstg
Copy link
Member

eernstg commented Jan 8, 2025

Good catch! dart-lang/language#4226.

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

7 participants