`final val`s that is implemented with a literal constant false positive in initialization checker
See original GitHub issueCompiler version
3.0.0
Semantics of final val
with literal constants
All final val
s that is implemented with a literal constant do not generate a field in the bytecode. Their accessors simply return the value and the field is elided.
This optimization makes the bytecode smaller and helps the JVM JIT to inline this value.
Initialization checker
The initialization checker is currently unaware of how code is generated for these fields. It believes that we do generate the fields and that there is a potential for uninitialized values. The reality is that these will always be seen as initialized as they always return the correct value.
We need to teach the initialization checker about this field encoding.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Confusing semantics of uninitialized `final val` containing literal ...
When a final val is implemented with a literal constant it can overcome this initialization issue. The initialization checker is there to protect...
Read more >Custom pluggable types for ... - The Checker Framework Manual
The Checker Framework comes with checkers for specific types of errors: Nullness Checker for null pointer errors (see Chapter 3); Initialization ...
Read more >V730. Not all members of a class are initialized inside the ...
The analyzer has detected a constructor that doesn′t initialize some of ... Another way is to use a special database for false positives....
Read more >Java Basics - Java Programming Tutorial
Java introduces a new binary type called " boolean ", which takes a literal value of either true or false . boolean s...
Read more >Pointer to literal value - Stack Overflow
For what it's worth, the type of character literals in C is int (char in C++ but this question is tagged C). 'A'...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Does everyone agree with @sjrd, (from https://github.com/lampepfl/dotty/issues/12570#issuecomment-857805134) that
?
It appears that the semantics that the compiler should implement are still being debated in https://github.com/lampepfl/dotty/issues/12570.
Once the semantics are decided there, my opinion is that the initialization checker should follow the actual semantics as closely as possible rather than trying to also be a linter, trying to enforce a “better”, “cleaner” semantics than the compiler actually implements. If something is safe but only due to ugly hacks, I think the initialization checker should not report it unsafe. Many use the type checker as an oracle to improve their understanding of the specification of the type system. Similarly, many will use the initialization checker to improve their understanding of the details of initialization. Inconsistency between the init checker and the actual init semantics will be confusing to that understanding.
It sounds like the #12570 is converging towards preserving the semantics of
final
for backward compatibility with Scala 2 and Java but preferringinline
to better express the intended semantics. In that case, there could be a lint-style deprecation/migration warning, separate from the initialization checker, that suggests the use ofinline
instead offinal
.In summary, as a general rule, I think the initialization checker should match actual semantics as closely as possible and lint/style warnings should be separate and marked as such without suggesting that a runtime error will occur.