checker: check number of fields in short struct inits (#6280)
							parent
							
								
									60a9d49382
								
							
						
					
					
						commit
						bd304f1141
					
				| 
						 | 
				
			
			@ -411,8 +411,14 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
 | 
			
		|||
			} else {
 | 
			
		||||
				info = type_sym.info as table.Struct
 | 
			
		||||
			}
 | 
			
		||||
			if struct_init.is_short && struct_init.fields.len > info.fields.len {
 | 
			
		||||
				c.error('too many fields', struct_init.pos)
 | 
			
		||||
			if struct_init.is_short {
 | 
			
		||||
				exp_len := info.fields.len
 | 
			
		||||
				got_len := struct_init.fields.len
 | 
			
		||||
				if exp_len != got_len {
 | 
			
		||||
					amount := if exp_len < got_len { 'many' } else { 'few' }
 | 
			
		||||
					c.error('too $amount fields in `$type_sym.source_name` literal (expecting $exp_len, got $got_len)',
 | 
			
		||||
						struct_init.pos)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			mut inited_fields := []string{}
 | 
			
		||||
			for i, field in struct_init.fields {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +0,0 @@
 | 
			
		|||
vlib/v/checker/tests/short_struct_too_many.vv:6:7: error: too many fields
 | 
			
		||||
    4 |
 | 
			
		||||
    5 | fn main() {
 | 
			
		||||
    6 |     t := Test{true, false}
 | 
			
		||||
      |          ~~~~~~~~~~~~~~~~~
 | 
			
		||||
    7 |     _ = t
 | 
			
		||||
    8 | }
 | 
			
		||||
| 
						 | 
				
			
			@ -1,8 +0,0 @@
 | 
			
		|||
struct Test {
 | 
			
		||||
	foo bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
	t := Test{true, false}
 | 
			
		||||
	_ = t
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,13 @@
 | 
			
		|||
vlib/v/checker/tests/short_struct_wrong_number.vv:7:6: error: too few fields in `Test` literal (expecting 2, got 1)
 | 
			
		||||
    5 |
 | 
			
		||||
    6 | fn main() {
 | 
			
		||||
    7 |     _ = Test{true}
 | 
			
		||||
      |         ~~~~~~~~~~
 | 
			
		||||
    8 |     _ = Test{true, false, true}
 | 
			
		||||
    9 | }
 | 
			
		||||
vlib/v/checker/tests/short_struct_wrong_number.vv:8:6: error: too many fields in `Test` literal (expecting 2, got 3)
 | 
			
		||||
    6 | fn main() {
 | 
			
		||||
    7 |     _ = Test{true}
 | 
			
		||||
    8 |     _ = Test{true, false, true}
 | 
			
		||||
      |         ~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
    9 | }
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
struct Test {
 | 
			
		||||
	foo bool
 | 
			
		||||
	bar bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
	_ = Test{true}
 | 
			
		||||
	_ = Test{true, false, true}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,15 +1,15 @@
 | 
			
		|||
vlib/v/checker/tests/unknown_method_suggest_name.vv:7:2: error: unknown type `hash.crc32.Crc33`.
 | 
			
		||||
vlib/v/checker/tests/unknown_method_suggest_name.vv:13:2: error: unknown type `hash.crc32.Crc33`.
 | 
			
		||||
Did you mean `crc32.Crc32`?
 | 
			
		||||
    5 |     y   int
 | 
			
		||||
    6 |     z   int
 | 
			
		||||
    7 |     ccc crc32.Crc33
 | 
			
		||||
   11 |     y int
 | 
			
		||||
   12 |     z int
 | 
			
		||||
   13 |     ccc crc32.Crc33
 | 
			
		||||
      |     ~~~~~~~~~~~~~~~
 | 
			
		||||
    8 | }
 | 
			
		||||
    9 |
 | 
			
		||||
   14 | }
 | 
			
		||||
   15 |
 | 
			
		||||
vlib/v/checker/tests/unknown_method_suggest_name.vv:27:9: error: unknown method: `Point.tranzlate`.
 | 
			
		||||
Did you mean `translate`?
 | 
			
		||||
   25 |     p := Point{1, 2, 3}
 | 
			
		||||
   26 |     v := Vector{5, 5, 10}
 | 
			
		||||
   26 |     v := Vector{x: 5, y: 5, z: 10}
 | 
			
		||||
   27 |     z := p.tranzlate(v)
 | 
			
		||||
      |            ~~~~~~~~~~~~
 | 
			
		||||
   28 |     println('p: $p')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,13 +4,13 @@ struct Point {
 | 
			
		|||
	x   int
 | 
			
		||||
	y   int
 | 
			
		||||
	z   int
 | 
			
		||||
	ccc crc32.Crc33
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Vector {
 | 
			
		||||
	x int
 | 
			
		||||
	y int
 | 
			
		||||
	z int
 | 
			
		||||
	ccc crc32.Crc33
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn (p Point) translate(v Vector) Point {
 | 
			
		||||
| 
						 | 
				
			
			@ -23,7 +23,7 @@ fn (p Point) identity() Point {
 | 
			
		|||
 | 
			
		||||
fn main() {
 | 
			
		||||
	p := Point{1, 2, 3}
 | 
			
		||||
	v := Vector{5, 5, 10}
 | 
			
		||||
	v := Vector{x: 5, y: 5, z: 10}
 | 
			
		||||
	z := p.tranzlate(v)
 | 
			
		||||
	println('p: $p')
 | 
			
		||||
	println('v: $v')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,7 +32,7 @@ pub fn dial_udp(laddr, raddr string) ?UdpConn {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	return UdpConn {
 | 
			
		||||
		sock
 | 
			
		||||
		sock: sock
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue