Skip to content

Remove most uses of uncheckedNN#26239

Merged
SolalPirelli merged 2 commits into
scala:mainfrom
dotty-staging:solal/no-uncheckednn
Jun 8, 2026
Merged

Remove most uses of uncheckedNN#26239
SolalPirelli merged 2 commits into
scala:mainfrom
dotty-staging:solal/no-uncheckednn

Conversation

@SolalPirelli
Copy link
Copy Markdown
Contributor

Removed all of the ones that are locally easy to remove, i.e., no complex invariants across methods or fields.

Remaining:

scala3 % grep --include=\*.scala -Fr 'uncheckedNN' compiler
compiler/src/dotty/tools/dotc/typer/Implicits.scala:      if isImport then if ctx.importInfo.uncheckedNN.isWildcardImport then WildImport else NamedImport else Definition
compiler/src/dotty/tools/dotc/typer/Implicits.scala:        val outer = outerImplicits.uncheckedNN
compiler/src/dotty/tools/dotc/typer/Typer.scala:          ctx.isImportContext && namedImportRef(ctx.importInfo.uncheckedNN).exists
compiler/src/dotty/tools/dotc/typer/Typer.scala:          val curImport = ctx.importInfo.uncheckedNN

These are all more complex invariants.

Plus the def:

compiler/src/dotty/tools/package.scala:     *  occurrences of uncheckedNN.
compiler/src/dotty/tools/package.scala:    transparent inline def uncheckedNN: T = x.asInstanceOf[T]

How much have you relied on LLM-based tools in this contribution?

Not at all

How was the solution tested?

Covered by existing tests (this is a refactoring)

if (ctx.settings.YretainTrees.value) {
if (myTrees == null) myTrees = computeRootTrees
myTrees.uncheckedNN
initialize(myTrees, myTrees = _, computeRootTrees)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(scroll to the last file in this diff view to see the definition of this -- it's as you'd expect, hopefully)

protected def computeRootTrees(using Context): List[Tree]

private var myTrees: List[Tree] | Null = uninitialized
private var myTrees: List[Tree] | Null = null
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no point in uninitialized for explicitly nullable stuff

@SolalPirelli SolalPirelli force-pushed the solal/no-uncheckednn branch from 7bc072e to 79e6ca5 Compare June 5, 2026 13:47
@SolalPirelli SolalPirelli requested a review from sjrd June 5, 2026 14:23
@SolalPirelli SolalPirelli marked this pull request as ready for review June 5, 2026 14:23
Comment thread compiler/src/dotty/tools/package.scala Outdated
Comment on lines +29 to +34
inline def initialize[T](getter: T | Null, setter: T => Unit, inline value: T): T = getter match
case v: T => v
case null =>
val res = value
setter(res)
res
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not produce good code because it performs a type test on : T rather than a single null test. The best code is obtained with

inline def initialize[T](getter: T | Null, inline setter: T => Unit, inline value: => T): T =
  if getter != null then
    getter
  else
    val res = value
    setter(res)
    res

(note also the signature. setter must be inline)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks. I wonder if that's something we could improve in the compiler.

end comparing

@sharable val NoContext: Context = new FreshContext((null: ContextBase | Null).uncheckedNN) {
@sharable val NoContext: Context = new FreshContext(null.asInstanceOf[ContextBase]) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. This is just wrong :(
(not that it was better before your PR)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know 😭

Comment thread compiler/src/dotty/tools/dotc/core/Scopes.scala Outdated
@SolalPirelli SolalPirelli force-pushed the solal/no-uncheckednn branch from 93a1f6d to 6258af3 Compare June 8, 2026 06:59
@SolalPirelli SolalPirelli requested a review from sjrd June 8, 2026 07:28
@SolalPirelli SolalPirelli enabled auto-merge (squash) June 8, 2026 07:53
@SolalPirelli SolalPirelli merged commit bcf39f6 into scala:main Jun 8, 2026
60 of 64 checks passed
@SolalPirelli SolalPirelli deleted the solal/no-uncheckednn branch June 8, 2026 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants