Skip to content

Commit

Permalink
Raise a compile error when trying to assign comptime records
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 31, 2023
1 parent 596fcca commit 3343daa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lualib/nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2320,9 +2320,14 @@ function visitors.VarDecl(context, node)
valtype = primtypes.niltype
end
end
if varnode.attr.comptime and not (valnode.attr.comptime and valtype) then
varnode:raisef("compile time variables can only assign to compile time expressions")
elseif vartype and vartype.is_auto then
if varnode.attr.comptime then
if not (valnode.attr.comptime and valtype) then
varnode:raisef("compile time variables can only assign to compile time expressions")
elseif (valnode.attr.value == nil and valnode.attr.type ~= primtypes.niltype) then
varnode:raisef("compile time variables cannot be of type '%s'", vartype)
end
end
if vartype and vartype.is_auto then
if not valtype then
varnode:raisef("auto variables must be assigned to expressions where type is known ahead")
elseif valtype.is_nolvalue then
Expand Down
4 changes: 4 additions & 0 deletions spec/analyzer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,10 @@ it("records", function()
local Record
local a <comptime> = Record{}
]], "can only assign to compile time expressions")
expect.analyze_error([[
local Box = @record{n: number}
local b: Box <comptime> = {n=3}
]], "compile time variables cannot be of type")
expect.ast_type_equals(
"local a: record{x: boolean}; local b = a.x",
"local a: record{x: boolean}; local b: boolean = a.x")
Expand Down

0 comments on commit 3343daa

Please sign in to comment.