diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 4ca75d24a5..a7ea12aab2 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -256,7 +256,8 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.error('receiver cannot be `mut` for operator overloading', node.receiver_pos) } else if node.params[1].is_mut { c.error('argument cannot be `mut` for operator overloading', node.pos) - } else if node.receiver.typ != node.params[1].typ { + } else if !c.check_same_type_ignoring_pointers(node.receiver.typ, + node.params[1].typ) { c.error('expected `$receiver_sym.name` not `$param_sym.name` - both operands must be the same type for operator overloading', node.params[1].type_pos) } else if node.name in ['<', '=='] && node.return_type != ast.bool_type { @@ -322,6 +323,22 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { node.source_file = c.file } +// FIXME: if the optimization will go after the checker, we can safely remove +// this util function +// check_same_type_ignoring_pointers util function to check if the Type are the same, included all the corner case +fn (c Checker) check_same_type_ignoring_pointers(type_a ast.Type, type_b ast.Type) bool { + // FIXME: if is possible pass the ast.Node and check + // the propriety `is_auto_rec` + if type_a != type_b { + // before failing we must be sure that + // the parser didn't optimize the function + clean_type_a := type_a.set_nr_muls(0) + clean_type_b := type_b.set_nr_muls(0) + return clean_type_a == clean_type_b + } + return true +} + fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type { keep_fn := c.table.cur_fn keep_inside_anon := c.inside_anon_fn diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 8b5ce48a0c..295c095dbb 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -608,6 +608,15 @@ fn (mut p Parser) fn_receiver(mut params []ast.Param, mut rec ReceiverParsingInf p.check_for_impure_v(rec.language, rec.type_pos) } + p.check(.rpar) + + if is_auto_rec && p.tok.kind != .name { + // Disable the auto-reference conversion for methodlike operators like ==, <=, > etc, + // since their parameters and receivers, *must* always be of the same type. + is_auto_rec = false + rec.typ = rec.typ.deref() + } + params << ast.Param{ pos: rec_start_pos name: rec.name @@ -616,9 +625,6 @@ fn (mut p Parser) fn_receiver(mut params []ast.Param, mut rec ReceiverParsingInf typ: rec.typ type_pos: rec.type_pos } - p.check(.rpar) - - return } fn (mut p Parser) anon_fn() ast.AnonFn { diff --git a/vlib/v/tests/operator_overloading_on_struct_with_too_many_fields_test.v b/vlib/v/tests/operator_overloading_on_struct_with_too_many_fields_test.v new file mode 100644 index 0000000000..ed495d287a --- /dev/null +++ b/vlib/v/tests/operator_overloading_on_struct_with_too_many_fields_test.v @@ -0,0 +1,30 @@ +// NB: the struct here deliberately has 9 member fields, to trigger +// the V heuristic, that decides to pass a Foo receiver automatically +// by reference, when a struct is too large. +struct Foo { + one int + two int + three int + four int + five int + six int + seven int + eight int + nine int +} + +fn (_ Foo) == (_ Foo) bool { + return true +} + +fn test_op() { + a := Foo{ + one: 1 + } + b := Foo{ + two: 2 + } + assert a == b + dump(a) + dump(b) +}