teal-language / tl

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

spurious error when using the result of string.gsub #678

Closed fperrad closed 11 months ago

fperrad commented 12 months ago
$ cat gsub.tl 
global function translate (str: any)
     if type(str) == 'string' then
         -- type not narrowed, so there are error in 2 following lines
         str = str:gsub('a', 'A')
         str = str:gsub('b', 'B')
     end
     if str is string then
         -- type narrowed, so no error at the following line
         str = str:gsub('c', 'C')
         -- now spurious error at the following line, look likes the return of `gsub` is not a string
         str = str:gsub('d', 'D')
     end
     if type(str) == 'string' then
         -- type not narrowed, so there are error in 2 following lines
         str = string.gsub(str, 'e', 'E')
         str = string.gsub(str, 'f', 'F')
     end
     if str is string then
         -- type narrowed, so no error at the following line
         str = string.gsub(str, 'g', 'G')
         -- now spurious error at the following line, look likes the return of `gsub` is not a string
         str = string.gsub(str, 'h', 'H')
     end
end
$ tl gen --check gsub.tl 
Wrote: gsub.lua
========================================
6 errors:
gsub.tl:4:20: cannot index key 'gsub' in any 'str' of type <any type>
gsub.tl:5:20: cannot index key 'gsub' in any 'str' of type <any type>
gsub.tl:11:20: cannot index key 'gsub' in any 'str' of type <any type>
gsub.tl:15:28: argument 1: got <any type>, expected string
gsub.tl:16:28: argument 1: got <any type>, expected string
gsub.tl:22:28: argument 1: got <any type>, expected string
hishamhm commented 11 months ago
fperrad commented 11 months ago

many thanks for these clarifications.