Studio highlights error in wrong place

Reproduction Steps
Put the following code into a ModuleScript, even though the error is on line 4 (2x do), studio will show the error being on line 9

local Module = {}

function Module.DoSomething()
	for Key = 1, 5 do do
		print(Key)
	end
end

return Module

image

Expected Behavior
Line 4 to be underlined in red

Actual Behavior
Line 9 is underlined in red

Issue Area: Studio
Issue Type: Display
Impact: Moderate
Frequency: Constantly

5 posts were split to a new topic: Non-useful posts on “Studio highlights error in wrong place”

That’s because that’s technically speaking where the error is. Whitespace doesn’t do anything in Luaᶧ, so this is what the engine sees rather than what you intended:

local Module = {}

function Module.DoSomething()
    for Key = 1, 5 do
        do
            print(Key)
        end
    end
    return Module
--> Missing end, hence the error

Roblox could try to make the parser use whitespace as a hint on what errors to return, but that could be problematic because a lot of Roblox scripts don’t actually indent their stuff correctly.

ᶧExcept in some rare ambiguous syntax cases involving function call expressions.

4 Likes

Roblox thinks there should be three ends instead of two, because of the second do statement. But, when Roblox reaches the end of the module code, it sees there isn’t an end, so it highlights the last line with "Expected end, got <eof>".

We actually already use indentation as a hint to improve the error message, which is this in this case:

Note that we’re correctly attributing the error to failure to close the do statement instead of failing to close the function – this hint is driven by indentation. However this is a heuristic so we don’t actually highlight the mismatch, as the editor doesn’t have this extra context.

This topic was automatically closed after 7 days. New replies are no longer allowed.