Give caret position as well as line number on errors

Given that Lua is not giving me any help in the form of compile time type checking, it would be really great if I didn’t have to guess what part of this line of code was wrong:

A VALID MEMBER OF WHAT? TELLLL MEEEEEEE

Useful output would look like this:

"CFrame is not a valid member of s (s is a CFrame)"

Which was the problem in my case that I only figured out after doing a binary search commenting out clauses.

1 Like

If it was an instance, it would’ve said “not a valid member of Part or whatever”.
This undescriptive errors happens because you’re indexing a non-instance userdata.
That could be a CFrame, a Vector3, …
(your ‘s’ isn’t a part, which is what you want I assume)
Line numbers are displayed in the stacktrace.

(no idea what you mean with “caret position”, I assume “that character there”)

I wonder if that can’t be changed. I’ll do some digging when I have some free time.

Just having “CFrame is not a valid member of Vector3” would already help a lot.
At least you know you’re trying to index a Vector3 instead of an Instance or table or whatever you wanted.

Ideally I would get the types of all the variables in the line and then it would be obvious what I am doing wrong.

For instance, a useful error would be “CFrame is not a valid member of s (s is a CFrame)”

That would be slightly more helpful, but I think it might be possible to get the name of the variable out of the stack. I haven’t done any Lua API stuff since December though.

Well, atempt to index local 's' (a nil value) is a possible error, so it isn’t too difficult.

In the meantime, why don’t you split that equation across multiple lines (temporarily) to quickly find your problem (if you haven’t already)?

Yeah, that is built into Lua. I would be surprised if I didn’t have access to the same information, but you never know until you look!

EDIT: Upon review, I’m not sure it is possible. Especially given the case where the object being indexed is an rvalue (by that I mean “Color3.new(1,1,1).x”, what happens then?) If I figure out another way, I’ll try it. Spoke too soon. Will check it out later.

I did this.

I’m not sure if it works. In my case the errors were both on line 17.

Hmph, I don’t think getting the name of the variable is possible. It turns out that Lua actually parses the bytecode or something when it actually gets you the name of the variable you failed to index.

However, it would be slightly more helpful if the error messages included the typename. I’ll check into that, it would be very easy.

If only we were using a language that generated an AST.

Maybe we could bolt one on just for syntax checking. GitHub - andremm/lua-parser: A Lua 5.3 parser written with LPegLabel

Is that how the current syntax checker works?

It uses the debug data in the bytecode.
If you can replicate the “get variable type/name” that happens on indexing a nil/number value, it’ll work fine.

That would be best, as doing print(game.Banana) errors “Banana is not a valid member of DataModel”.
(“attempt to index/call …” is for “pure Lua errors”, not something “C-sided” like Instance methods)

Hm, I just don’t think there’s a feasible way. Most uses will be on a temporary anyway, like part.CFrame.badIndex. I did, however, add a typename for all of the ROBLOX userdata types just now!