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

Wrong Type Deduction for LEN #210

Open
XmiliaH opened this issue Feb 14, 2021 · 2 comments
Open

Wrong Type Deduction for LEN #210

XmiliaH opened this issue Feb 14, 2021 · 2 comments

Comments

@XmiliaH
Copy link
Contributor

XmiliaH commented Feb 14, 2021

The return type for LEN is wrong if the __len metamethod for floats or integers is overwritten and the length from an array element from a ravi array is taken. As in the following example which errors with src/lvm.c:2428: luaV_execute: Assertion `((((rb))->tt_) == (((3) | ((1) << 4))))' failed.

debug.setmetatable(1, {
    __len = function()
        return "123"
    end
})

local function f(x:integer[])
    return #(x[1]) + 1
end

print(f(table.intarray(2)))

The problem is that luaK_exp2anyreg can change the ravi_type if it is a load from an index as in the example above. The problematic place is

ravi/src/lcode.c

Line 1270 in 56a59a1

int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */

Following is the wrong bytecode with the unexpected ADDII

1       [7]     TOIARRAY        0
2       [8]     IARRAY_GET      1 0 -1  ; 1
3       [8]     LEN             1 1
4       [8]     ADDII           1 1 -1  ; - 1
5       [8]     RETURN          1 2
6       [9]     RETURN          0 1
@dibyendumajumdar
Copy link
Owner

Thanks for the report.

@dibyendumajumdar
Copy link
Owner

Hi thanks for the report and analysis - you are correct here. I have pushed a fix.

dibyendumajumdar added a commit that referenced this issue Feb 15, 2021
dibyendumajumdar added a commit that referenced this issue Feb 17, 2021
dibyendumajumdar added a commit that referenced this issue Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants