checker: fix operator overloads (for large structs with > 8 fields, the method receiver is normally auto converted to a reference) (#13889)
							parent
							
								
									38853568b4
								
							
						
					
					
						commit
						51c1d666c2
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue