gen: fix `arr.last().field` (#7076)
							parent
							
								
									ff26f0539c
								
							
						
					
					
						commit
						d8b8aca51e
					
				| 
						 | 
					@ -1002,6 +1002,27 @@ fn test_array_string_pop() {
 | 
				
			||||||
	assert a.cap == 3
 | 
						assert a.cap == 3
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn test_array_first() {
 | 
				
			||||||
 | 
						a := [3]
 | 
				
			||||||
 | 
						assert a.first() == 3
 | 
				
			||||||
 | 
						b := [1, 2, 3, 4]
 | 
				
			||||||
 | 
						assert b.first() == 1
 | 
				
			||||||
 | 
						c := ['abc', 'def']
 | 
				
			||||||
 | 
						assert c.first()[0] == `a`
 | 
				
			||||||
 | 
						s := [Chunk{'a'}]
 | 
				
			||||||
 | 
						assert s.first().val == 'a'
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn test_array_last() {
 | 
				
			||||||
 | 
						a := [3]
 | 
				
			||||||
 | 
						assert a.last() == 3
 | 
				
			||||||
 | 
						b := [1, 2, 3, 4]
 | 
				
			||||||
 | 
						assert b.last() == 4
 | 
				
			||||||
 | 
						c := ['abc', 'def']
 | 
				
			||||||
 | 
						assert c.last()[0] == `d`
 | 
				
			||||||
 | 
						s := [Chunk{'a'}]
 | 
				
			||||||
 | 
						assert s.last().val == 'a'
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[direct_array_access]
 | 
					[direct_array_access]
 | 
				
			||||||
fn test_direct_array_access() {
 | 
					fn test_direct_array_access() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -365,6 +365,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
 | 
				
			||||||
	if node.name == 'str' {
 | 
						if node.name == 'str' {
 | 
				
			||||||
		g.gen_str_for_type(node.receiver_type)
 | 
							g.gen_str_for_type(node.receiver_type)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						mut has_cast := false
 | 
				
			||||||
	// TODO performance, detect `array` method differently
 | 
						// TODO performance, detect `array` method differently
 | 
				
			||||||
	if left_sym.kind == .array && node.name in
 | 
						if left_sym.kind == .array && node.name in
 | 
				
			||||||
		['repeat', 'sort_with_compare', 'free', 'push_many', 'trim', 'first', 'last', 'pop', 'clone', 'reverse', 'slice'] {
 | 
							['repeat', 'sort_with_compare', 'free', 'push_many', 'trim', 'first', 'last', 'pop', 'clone', 'reverse', 'slice'] {
 | 
				
			||||||
| 
						 | 
					@ -374,7 +375,8 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
 | 
				
			||||||
		receiver_type_name = 'array'
 | 
							receiver_type_name = 'array'
 | 
				
			||||||
		if node.name in ['last', 'first', 'pop'] {
 | 
							if node.name in ['last', 'first', 'pop'] {
 | 
				
			||||||
			return_type_str := g.typ(node.return_type)
 | 
								return_type_str := g.typ(node.return_type)
 | 
				
			||||||
			g.write('*($return_type_str*)')
 | 
								has_cast = true
 | 
				
			||||||
 | 
								g.write('(*($return_type_str*)')
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	mut name := util.no_dots('${receiver_type_name}_$node.name')
 | 
						mut name := util.no_dots('${receiver_type_name}_$node.name')
 | 
				
			||||||
| 
						 | 
					@ -428,6 +430,9 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		g.expr(node.left)
 | 
							g.expr(node.left)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if has_cast {
 | 
				
			||||||
 | 
							g.write(')')
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	is_variadic := node.expected_arg_types.len > 0 && node.expected_arg_types[node.expected_arg_types.len -
 | 
						is_variadic := node.expected_arg_types.len > 0 && node.expected_arg_types[node.expected_arg_types.len -
 | 
				
			||||||
		1].has_flag(.variadic)
 | 
							1].has_flag(.variadic)
 | 
				
			||||||
	if node.args.len > 0 || is_variadic {
 | 
						if node.args.len > 0 || is_variadic {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue