Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type identifying where clause behaviour changes when using interfaces loaded via require #853

Closed
svermeulen opened this issue Nov 11, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@svermeulen
Copy link
Contributor

local type A <const> = require("a")

-- local interface A
-- 	get_type: function(A): string
-- end

local type B <const> = require("b")

-- local interface B is A where self:get_type() == "b"
-- end

local b: A = {
	get_type = function(): string return "b" end
}

local c: A = {
	get_type = function(): string return "c" end
}

if c is B then
   print("is B")
else
   print("is not B")
end

The above code prints "is B" when it should print "is not B". If we stop using 'require' and place the contents of the files directly in the above code, then we correctly get "is not B". It seems that when included via files, the compiled lua code is this:

if type(c) == "table" then
   print("is B")
else
   print("is not B")
end

And when not using require it is this:

if c:get_type() == "b" then
   print("is B")
else
   print("is not B")
end
@hishamhm hishamhm added the bug Something isn't working label Nov 11, 2024
@catwell
Copy link
Contributor

catwell commented Jan 8, 2025

I tried reproducing this but could not.

Here are exactly the files I used:

-- a.tl

local interface A
    get_type: function(A): string
end

return A
-- b.tl

local type A <const> = require "a"

local interface B is A where self:get_type() == "b"
end

return B
local type A <const> = require "a"
local type B <const> = require "b"

local b: A = {
    get_type = function(): string return "b" end
}

local c: A = {
    get_type = function(): string return "c" end
}

if c is B then
    print("is B")
else
    print("is not B")
end

I tried Teal master, 0.24.0 and 0.24.1 and I always get the correct generation.

@svermeulen
Copy link
Contributor Author

Thanks for attempting to repro. I tried to narrow down repro steps more, and I realized that it actually does work, as long as I either run teal in the same directory as all these files, or a directory where it can find a tlconfig.lua file that includes the necessary directories such that it can locate all the files by require path.

You can repro the issue I had originally by running "tl gen [absolute path to main.tl]" from a directory that is not the one containing main.tl

So I assume this is working as intended. Maybe I should always run something like "tl check" before "tl gen" to ensure it doesn't generate invalid lua next time

@svermeulen
Copy link
Contributor Author

svermeulen commented Jan 8, 2025

I also found another way to fix this is to provide tl with the necessary include directories before running gen via the -I command line switch

@hishamhm
Copy link
Member

hishamhm commented Jan 8, 2025

I think we should change the behavior so that tl gen does tl gen --check by default (i.e. check and gen), and then introduce tl gen --no-check if someone wants to generate a file with type errors (which could be useful during the process of converting a codebase).

@hishamhm
Copy link
Member

hishamhm commented Jan 8, 2025

I'm closing this bug and opening #899, but I think I'm only going to merge that for Teal 0.25

@hishamhm hishamhm closed this as completed Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants