Combine definite initialization and variable usage checks?

These 2 checks are done in different sections of the compiler:

// Definite initialization, part of swiftSILOptimizer
var x: Int
print(x) // error: variable 'x' used before being initialized

// Variable usage, part of swiftSema
var y: Int = 1 // warning: variable 'y' was never mutated; consider changing to 'let' constant
print(x)

But these really seem to me like they ought to be the same thing. Both cases track reads and writes to a variable. Based on the results, different diagnostics may be emitted:

  • Read before initialized? Error
  • A var is never written to after initialization? Warning
  • Never read at all? Warning

And while the definite initialization check is comprehensive, the variable usage checks are not. Even simple deviations may not emit a warning:

// This doesn't warn because the assignment is counted as a write
var y: Int
y = 1
print(y)

Am I correct in thinking that it would be a good idea to merge these checks?

If they should merge, which should subsume the other?

It does seem reasonable, but I suspect it's a fairly complex undertaking. Definite initialization is definitely (pun intended) the place to go rather than the other way around, since SIL has a much better understanding of control flow than Sema, and DI is needed for correctness.

2 Likes