Skip to content

Commit

Permalink
Allow to use len operator on pointer to records
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Feb 16, 2020
1 parent 6715118 commit 69a67ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions nelua/analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1784,15 +1784,24 @@ function visitors.UnaryOp(context, node)
local argtype = argattr.type
local type
if argtype then
if argtype.is_record and not blocked_metamethod_operators[opname] then
local mtname = '__' .. opname
local mtsym = argtype:get_metafield(mtname)
if mtsym then
visitor_Call(context, node, {}, mtsym.type, mtsym, argnode)
else
argnode:raisef("no metamethod `%s` for record '%s'", mtname, argtype:prettyname())
local overridden = false
if not blocked_metamethod_operators[opname] then
local objtype = argtype
if argtype.is_pointer and argtype.subtype then
objtype = argtype.subtype
end
else
if objtype.is_record then
local mtname = '__' .. opname
local mtsym = objtype:get_metafield(mtname)
if mtsym then
visitor_Call(context, node, {}, mtsym.type, mtsym, argnode)
overridden = true
else
argnode:raisef("no metamethod `%s` for record '%s'", mtname, objtype:prettyname())
end
end
end
if not overridden then
local value, err
type, value, err = argtype:unary_operator(opname, argattr)
if err then
Expand Down
1 change: 0 additions & 1 deletion nelua/cgenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,6 @@ function visitors.UnaryOp(context, node, emitter)
local op = cdefs.unary_ops[opname]
assert(op)
if attr.calleesym then
assert(argnode.attr.type.is_record)
visitor_Call(context, node, emitter, {}, nil, argnode)
else
local surround = not node.attr.inconditional
Expand Down
1 change: 1 addition & 0 deletions spec/05-cgenerator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,7 @@ it("record metametods", function()
assert(a:__len() == 100)
local pa = &a
assert(#$pa == 100)
assert(#pa == 100)
local R = @record {
x: integer
Expand Down

0 comments on commit 69a67ec

Please sign in to comment.