teal-language / tl

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

Error with custom (non-native) types in global variable declarations in .d.tl files #518

Closed PinocchioStrings closed 2 years ago

PinocchioStrings commented 2 years ago
-- somefile.d.tl:
local type Foobar = number -- Global or local same results, alias or record same results.
local MyLocalNumber: number -- Works
local MyLocalFoobar: Foobar -- Works
global MyGlobalNumber: number -- Works
global MyGlobalFoobar: Foobar -- Error: cannot redeclare global with a different type: previous type of MyGlobalFoobar is Foobar
-- Only generates the above error in .d.tl files, not in regular .tl files.
-- EDIT:
-- Despite the error, the declaration is still in effect for dependent .tl files (in my particular case this is a global_env_def).
-- The error won't show up when you run "tl build", but it shows up when you run "tl check somefile.d.tl"
-- It also shows up in vscode with the vscode-teal plugin, but again only for .d.tl files, not regular .tl files.
hishamhm commented 2 years ago

I'm unable to reproduce this on either the latest master or the latest release — I created the file somefile.d.tl with the above 5 lines, but tl check somefile.d.tl passes, it only produces warnings about the unused local variables, as expected.

PinocchioStrings commented 2 years ago

I did some further testing and it turns out that the problem only occurs when the same somefile is specified as global_env_def (using tlconfig.lua or command line option).

I guess that makes sense, tl would probably be processing the same file twice as though they were a single file? Alas the problem is that it makes "tl check" (and the teal vscode plugin) worse than useless for the global_env_def file.

If this is the case, then the solution would be to prevent tl from using the global_env_def file when it is the same as the file currently being checked.

(Quite aside from that, I already disabled the teal plugin because it tendsto crash and generate error popups during normal use).

hishamhm commented 2 years ago

@PinocchioStrings Could you test this quick hack and see if it solves the issue for you?

diff --git a/tl b/tl
index 42a7832..d291884 100755
--- a/tl
+++ b/tl
@@ -135,7 +135,7 @@ local function setup_env(tlconfig, filename)
    local gen_target = tlconfig["gen_target"]

    tlconfig._init_env_modules = tlconfig._init_env_modules or {}
-   if tlconfig.global_env_def then
+   if tlconfig.global_env_def and tlconfig.global_env_def ~= filename then
       table.insert(tlconfig._init_env_modules, 1, tlconfig.global_env_def)
    end

This might not be smart enough — e.g. for detecting absolute versus relative pathnames — but it might prove sufficient. @euclidianAce: do you think Cyan suffers from a similar issue?

PinocchioStrings commented 2 years ago

I'm on Windows with the precompiled executable.

PinocchioStrings commented 2 years ago

Alternatively you could allow redeclarations as long as they are the same.

I'm thinking you may want to do that anyway. In which case that would also solve this.

makspll commented 2 years ago

Facing same bug on linux

hishamhm commented 1 year ago

Ah, that makes sense! I missed the misleading error message.