teal-language / tl

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

"tl check" does not warn if function foo():string returns a nil #703

Closed houseofmercy-github closed 9 months ago

houseofmercy-github commented 9 months ago

For context, I've been discussing a problem I experienced with Lua some years ago with a fellow on reddit https://old.reddit.com/r/programming/comments/16zmdvy/strong_typing_a_hill_im_willing_to_die_on/k3g2ndf/

I think it would be good if a typechecker for Lua could report an error in a function like

local function foo(x: number): string 
  if x == 2 then return nil else return "ok" end
end

much like Typescript does with this one:

function foo(x: number): string
{
  if (x == 2) { return null; } else { return "ok"; }
}

On a whim I tried installing tl and seeing what it did. Sadly it didn't warn or prevent the problem in this example:

% cat foo.tl

  local function foo(x: number): string 
    if x == 2 then return nil else return "ok" end
  end

  local x = {
      foo(1),
      foo(2),
      foo(3)
  }

  for i,v in ipairs(x) do print(i,v) end

% tl check foo.tl
  ========================================
  Type checked foo.tl
  0 errors detected -- you can use:

     tl run foo.tl

         to run foo.tl as a program

     tl gen foo.tl

         to generate foo.lua

% tl run foo.tl
  1 ok

% tl --version
  0.15.2+dev

Is there a way to have tl report an error in this case like my Typescript example does?

houseofmercy-github commented 9 months ago

Looking at other issues this might be same as https://github.com/teal-language/tl/issues/598

hishamhm commented 9 months ago

Yes, it is a duplicate of that one!