teal-language / tl

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

Records used as table indices are checked structurally? #533

Closed shviller closed 2 years ago

shviller commented 2 years ago

Code currently in master doesn't error on this code, but as far as my understanding goes, it should:

local type IndexType = record
  x: number
end
local type WrongType = record
  --y: number -- uncomment this for Teal to finally throw the expected type error:
  -- "wrong index type: WrongType, expected IndexType"
end
local type MapType = {IndexType: number}

local wrong_var: WrongType = {}
local index_var: IndexType = {}
local map: MapType = {[index_var] = 42}

print(map[wrong_var])

As long as WrongType is structurally a subtype of IndexType (that is, all of its fields are present in IndexType and have the same type they do in IndexType; I haven't tested for anything more complex), this code won't error.

hishamhm commented 2 years ago

@shviller Thank you for the report and test case! It's now fixed in git.