teal-language / tl

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

Bug in "tl check" (cannot redeclare global with a different type) #805

Open b0ryakha opened 3 days ago

b0ryakha commented 3 days ago

When I use tl check on a project, it finds an incomprehensible error: "sources\oop.tl:5:8: cannot redeclare global with a different type: previous type of extends is function(Child, Base, ...: ): Child"

The error began to appear only after I moved some files (which use oop.tl) to the widgets subfolder. At the same time, tl gen correctly generates lua files and everything works.

Example of the project structure:

sources/
├── oop.tl
├── 1.tl
├── 2.tl
├── ...
├── tlconfig.lua
└── widgets/
    ├── 3.tl
    ├── 3.tl
    └── ...

tlconfig.lua:

return {
    include_dir = {
        "sources", "sources/widgets"
    },
}

oop.tl:

local record Base
    new: function<Self>(Self, ...: any)
end

global function extends<Child>(child: Child, base: Base, ...: any): Child
    local index = {}

    for k, v in pairs(base as table) do index[k] = v end
    for k, v in pairs(child as table) do index[k] = v end

    return setmetatable(base:new(...), { __index = index })
end

building:

tl check sources/*.tl &&
tl check sources/widgets/*.tl &&
tl gen sources/*.tl &&
tl gen sources/widgets/*.tl
hishamhm commented 3 days ago

Do you have an example of the code of the module that requires oop.tl?

b0ryakha commented 2 days ago

Simplified example:

require("widget")
require("oop")

global record Label
    text: string
end

function Label:new(parent: Widget, text: string): Label
    local self = extends(Label, Widget, parent)

    self.text = text or ""

    return self
end

widget.tl in 'sources' folder.