teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.03k stars 101 forks source link

Add new warning when nil is assigned to a non-nilable type #649

Closed Hazematman closed 1 year ago

Hazematman commented 1 year ago

This pull request add a new warning when you assign nil to a type that is not-nilable. For example the following code

local x: number = 10
x = nil

will now return an warning that looks like

========================================
1 warning:
test.tl:18:1: assigning nil to a non-nilable type number

hinting that the correct way to write this code is (assuming that you want assigning nil to be valid)

local x: number | nil = 10
x = nil

This change causes the tl compiler to throw a lot of warning when building. If required I could modify the tl compiler to explicitly mark types as nilable where required.

This change also required a test update to the record function spec tests, as one of the tests would assign nil to a boolean value and then expect zero warning to be generated.

github-actions[bot] commented 1 year ago

Teal Playground URL: https://649--teal-playground-preview.netlify.app

Hazematman commented 1 year ago

Closing this PR as it only address one issue surrounding nil ability of types. Proper nil checking would require more changes to the type system.