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

var list = const [3];list[0] = 9;should throw error in compile time #45886

Closed
LuoJimmy opened this issue Apr 29, 2021 · 2 comments
Closed

var list = const [3];list[0] = 9;should throw error in compile time #45886

LuoJimmy opened this issue Apr 29, 2021 · 2 comments
Labels
analyzer-constants analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@LuoJimmy
Copy link

var list = const [3];
list[0] = 9; // error in runtime, but this can analyze in compile time.

var list2 = List.unmodifiable([3]);
list2[0] = 99; // this should error in runtime not in compile time.

@lrhn lrhn transferred this issue from dart-lang/sdk May 3, 2021
@lrhn
Copy link
Member

lrhn commented May 3, 2021

This is a restriction that the type system doesn't capture. There is no way to look at the variable list or list2 of type List<int> and see that it's unmodifiable.

The analyzer can likely detect that the only possible values of that variable at that point are unmodifiable lists (and have the ability to know about unmodifiable lists to begin with, something the language specification doesn't even know about), and could possibly give a warning here.

So, not possible as a compile-time error. Might be possible to get a warning, but then it's not a language issue.

@lrhn lrhn transferred this issue from dart-lang/language May 3, 2021
@lrhn lrhn added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-enhancement A request for a change that isn't a bug labels May 3, 2021
@jcollins-g jcollins-g added the P3 A lower priority bug or feature request label May 13, 2021
@srawlins srawlins added analyzer-warning Issues with the analyzer's Warning codes analyzer-constants labels Jul 22, 2024
@srawlins
Copy link
Member

#48551 is a duplicate request. It is newer, but has more discussion, so I'm closing this in favor of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-constants analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants