question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`final val`s that is implemented with a literal constant false positive in initialization checker

See original GitHub issue

Compiler version

3.0.0

Semantics of final val with literal constants

All final vals 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:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dwijnandcommented, Sep 23, 2021

Does everyone agree with @sjrd, (from https://github.com/lampepfl/dotty/issues/12570#issuecomment-857805134) that

The init checker should not be changed; it is correct to flag any use-before-initialization as unspecified behavior.

?

1reaction
olhotakcommented, Jun 9, 2021

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 preferring inline 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 of inline instead of final.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found