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

Record tag error message includes record name instead of only "userdata" #595

Merged
merged 4 commits into from
Apr 7, 2024

Conversation

Vipul-Cariappa
Copy link
Contributor

Fixes: #151

Code:

-- main.lua

Mod = require "Shapes"

C1 = Mod.Circle_New(3.0)
R1 = Mod.Rectangle_New(3.0, 4.0)

print(Mod.Circle_Area(R1))
print(Mod.Circle_Area(1)) 
-- Shapes.pln

local Shapes: module = {}

record Circle
    radius: float
end

record Rectangle
    length: float
    width: float
end

function Shapes.Circle_New(r: float): Circle
    local c: Circle = {radius = r}
    return c
end

function Shapes.Circle_Area(c: Circle): float
    return c.radius * c.radius * 3.1415
end

function Shapes.Rectangle_New(l: float, w: float): Rectangle
    local c: Rectangle = {length = l, width = w}
    return c
end

function Shapes.Rectangle_Area(c: Rectangle): float
    return c.length * c.width
end

return Shapes

Old behaviour

❯ lua main.lua                                  
lua: main.lua:8: file Shapes.pln: line 17: wrong type for argument 'c', expected userdata but found userdata
stack traceback:
        [C]: in function 'Shapes.Circle_Area'
        main.lua:8: in main chunk
        [C]: in ?

❯ lua main.lua
lua: main.lua:9: file Shapes.pln: line 17: wrong type for argument 'c', expected userdata but found integer
stack traceback:
        [C]: in function 'Shapes.Circle_Area'
        main.lua:9: in main chunk
        [C]: in ?

New behaviour

❯ lua main.lua
lua: main.lua:8: file Shapes.pln: line 17: wrong type for argument 'c', expected Circle but found Rectangle
stack traceback:
        [C]: in function 'Shapes.Circle_Area'
        main.lua:8: in main chunk
        [C]: in ?

❯ lua main.lua
lua: main.lua:9: file Shapes.pln: line 17: wrong type for argument 'c', expected Circle but found integer
stack traceback:
        [C]: in function 'Shapes.Circle_Area'
        main.lua:9: in main chunk
        [C]: in ?

Copy link
Member

@hugomg hugomg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Vipul! Looks like the PR is not passing the test suite. Can you please look into that?

if expected_type == "LUA_TUSERDATA" then
expected_type = C.string(typ.name)
else
expected_type = "pallene_tag_name(" .. expected_type .. ")"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need pallene_tag_name, now that we have pallene_type_name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would require both pallene_tag_name and pallene_type_name. They are different in two ways.

  1. Difference in the function signature const char *pallene_tag_name(int raw_tag) and const char *pallene_type_name(lua_State *L, const TValue *v)
  2. expected_type is known at the compile time (and uses pallene_tag_name) i.e. the argument's expected type. Whereas the argument const TValue *v passed to pallene_type_name cannot be known at compile time. v is the type of the actual argument passed at runtime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. IIRC, there aren't any other places that call pallene_tag_name. The tag name function was there only for the error messages and pallene_type_name now does that job. Notice how both functions are currently quite similar.

  2. If your goal was that the expected_type be known at compile time, then calling pallene_tag_name is not the way to do that. That C function call will only happen at run-time.

  3. One last thing: In the coder.lua, please use different variable names for the type tag returned by pallene_type_tag and for the type name that you compute inside the if statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look at the recent changes. Hope the latest commit resolves the issues/suggestions you mentioned.

src/pallene/pallenelib.lua Outdated Show resolved Hide resolved
src/pallene/coder.lua Outdated Show resolved Hide resolved
@hugomg
Copy link
Member

hugomg commented Apr 7, 2024

Hmm, why is our CI failing? 🤔
The tests are passing on my machine. Is that also the case for you, Vipul?

Copy link
Member

@hugomg hugomg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's passing now, go figure...
Thanks again!

@hugomg hugomg merged commit 48104cb into pallene-lang:master Apr 7, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve record tag error messages to include the record name
2 participants