Skip to content

Commit

Permalink
Fix C codegen when accessing a casted record pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 30, 2020
1 parent 2e07d1c commit 6f7ef7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nelua/cemitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@ function CEmitter:add_val2type(type, val, valtype, checkcast)
self:add_builtin('nelua_narrow_cast_', type, valtype)
self:add('(', val, ')')
else
local innertype = type.is_pointer and type.subtype or type
local surround = innertype.is_record or innertype.is_array
if surround then self:add('(') end
self:add_ctypecast(type)
self:add(val)
if surround then self:add(')') end
end
end
else
Expand Down
7 changes: 7 additions & 0 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ it("type cast", function()
n = 0.0;
f = 0.0f;
r = (R){0};]])

assert.generate_c([[
local recA = @record{a: integer}
local recB = @record{b: integer}
local x: recA
local y = ((@*recB)(&x).b)
]], "y = ((recB_ptr)(&x))->b;")
end)

it("string", function()
Expand Down

0 comments on commit 6f7ef7b

Please sign in to comment.