teal-language / tl

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

0.14 regression: expression not recognized as enum #553

Closed lewis6991 closed 2 years ago

lewis6991 commented 2 years ago
local enum Type
  "add"
  "change"
  "delete"
end

local function foo(a: integer, b: integer): Type
  return a == 0 and 'delete' or
         b == 0 and 'add'    or
                    'change'
end

Reports: in return value: string is not a Type ()

lenscas commented 2 years ago

https://teal-playground.netlify.app/?c=CmxvY2FsIGVudW0gVHlwZQogICJhZGQiCiAgImNoYW5nZSIKICAiZGVsZXRlIgplbmQKCmxvY2FsIGZ1bmN0aW9uIGZvbyhhOiBpbnRlZ2VyLCBiOiBpbnRlZ2VyKTogVHlwZQogIHJldHVybiBhID09IDAgYW5kICdkZWxldGUnIG9yCiAgICAgICAgIGIgPT0gMCBhbmQgJ2FkZCcgICAgb3IKICAgICAgICAgICAgICAgICAgICAnY2hhbmdlJwplbmQ%3D

Looks like the error is always flagged at the last or

local enum Type
  "add"
  "change"
  "delete"
end

local function foo(a: integer, b: integer): Type
  return a == 0 and 'delete' or
         b == 0 and ('add' as Type) or
                    'change'
end

this however passes the typechecker.

hishamhm commented 2 years ago

@lewis6991 should be fixed now! can you test master in your codebase and check if things are working now?

lewis6991 commented 2 years ago

Thanks a lot.

... however, I've now got a different error:

local enum TypeA
  "a"
  "b"
end

local record A
  type: TypeA
end

local enum TypeB
  "a"
  "b"
  "c"
end

local record B
  type: TypeB
end

function M.foo(a: A): {B}
  local bs = {}
  bs[1] = {
    type = 'c' or a.type,
  }
  return bs
end

in return value: got {record (type: string)} (inferred at ...), expected {B}

hishamhm commented 2 years ago

@lewis6991 for this second example, I think the inference engine is just not powerful enough to handle it, currently. I believe you didn't get an error before because the return type wasn't being checked enough. A solution is to declare the array type explicitly:

local bs: {B} = {}

and then everything else flows nicely.

lewis6991 commented 2 years ago

No problem.

So with that, everything is now clean.