# Combine definite initialization and variable usage checks?

**URL:** <https://forums.swift.org/t/combine-definite-initialization-and-variable-usage-checks/14030>\
**Category:** Compiler\
**Created:** [June 27, 2018, 12:18pm UTC](https://forums.swift.org/t/combine-definite-initialization-and-variable-usage-checks/14030 "2018-06-27T12:18:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdiep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mdiep/32/500_2.png) [@mdiep](https://forums.swift.org/u/mdiep)\
**Post date:** [June 27, 2018, 12:18pm UTC](https://forums.swift.org/t/combine-definite-initialization-and-variable-usage-checks/14030/1 "2018-06-27T12:18:29Z")

</div>

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

```swift
// 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](https://bugs.swift.org/browse/SR-1658). Even simple deviations may not emit a warning:

```swift
// 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?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [June 27, 2018, 6:38pm UTC](https://forums.swift.org/t/combine-definite-initialization-and-variable-usage-checks/14030/2 "2018-06-27T18:38:08Z")

</div>

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.
