Skip to content

Commit

Permalink
Fix a bug when type casting on unresolved types
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 2, 2020
1 parent cc328ed commit f736933
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
21 changes: 11 additions & 10 deletions nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,6 @@ local function visitor_Call_type_cast(context, node, argnodes, type)
type, #argnodes)
end
local argnode = argnodes[1]
local done = true
if argnode then
argnode.desiredtype = type
context:traverse_node(argnode)
Expand All @@ -924,17 +923,19 @@ local function visitor_Call_type_cast(context, node, argnodes, type)
attr.comptime = true
end
end
attr.type = type
attr.calleetype = primtypes.type
attr.sideeffect = argnode.attr.sideeffect
attr.lvalue = argnode.attr.lvalue
if argnode.done then
node.done = true
end
end
attr.sideeffect = argnode.attr.sideeffect
attr.lvalue = argnode.attr.lvalue
if not argnode.done then
done = nil
end
else -- zero initializer
attr.type = type
attr.calleetype = primtypes.type
node.done = true
end
attr.typecast = true
attr.type = type
attr.calleetype = primtypes.type
node.done = done
end

local function visitor_Call(context, node, argnodes, calleetype, calleesym, calleeobjnode)
Expand Down
8 changes: 8 additions & 0 deletions spec/03-typechecker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,14 @@ it("type construction", function()
assert.analyze_error("local a = (@integer)(nil)", "no viable type conversion")
end)

it("type casting", function()
assert.analyze_error([[
local R = @record{}
local a = 'b'
local b = (@R)(a)
]], "no viable type conversion")
end)

it("annotations", function()
assert.analyze_ast("local r: record{x: integer} <aligned(8)>")
assert.analyze_ast("local Record <aligned(8)> = @record{x: integer}")
Expand Down
25 changes: 25 additions & 0 deletions tests/string_test.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ do -- initialization, len, eq and reset
assert('a' == 'a')
end

do -- casting
local sstr = (@string)("hello")
local scstr = (@cstring)("hello")
local svstr = (@stringview)("hello")
local dstr: string
local dcstr: cstring
local dvstr: stringview

-- implicit
dstr = scstr assert(dstr == "hello") -- cstring -> string
dstr = svstr assert(dstr == "hello") -- stringview -> string
dcstr = sstr assert(dcstr == "hello") -- string -> cstring
dcstr = svstr assert(dcstr == "hello") -- stringview -> cstring
dvstr = scstr assert(dvstr == "hello") -- cstring -> stringview
dvstr = sstr assert(dvstr == "hello") -- string -> stringview

-- explicit
dstr = (@string)(scstr) assert(dstr == "hello") -- cstring -> string
dstr = (@string)(svstr) assert(dstr == "hello") -- stringview -> string
dcstr = (@cstring)(sstr) assert(dcstr == "hello") -- string -> cstring
dcstr = (@cstring)(svstr) assert(dcstr == "hello") -- stringview -> cstring
dvstr = (@stringview)(scstr) assert(dvstr == "hello") -- cstring -> stringview
dvstr = (@stringview)(sstr) assert(dvstr == "hello") -- string -> stringview
end

do -- len
local a: string = 'hello'
local e: string
Expand Down

0 comments on commit f736933

Please sign in to comment.