checker: disallow type casting to `none` (#6635)
							parent
							
								
									9309a07438
								
							
						
					
					
						commit
						acc85be5ae
					
				| 
						 | 
					@ -2670,6 +2670,9 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			} else if node.typ == table.bool_type {
 | 
								} else if node.typ == table.bool_type {
 | 
				
			||||||
				c.error('cannot cast to bool - use e.g. `some_int != 0` instead', node.pos)
 | 
									c.error('cannot cast to bool - use e.g. `some_int != 0` instead', node.pos)
 | 
				
			||||||
 | 
								} else if node.expr_type == table.none_type {
 | 
				
			||||||
 | 
									type_name := c.table.type_to_str(node.typ)
 | 
				
			||||||
 | 
									c.error('cannot cast `none` to `$type_name`', node.pos)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if node.has_arg {
 | 
								if node.has_arg {
 | 
				
			||||||
				c.expr(node.arg)
 | 
									c.expr(node.arg)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,75 @@
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:2:14: error: cannot cast `none` to `string`
 | 
				
			||||||
 | 
					    1 | fn main() {
 | 
				
			||||||
 | 
					    2 |     _ := string(none)
 | 
				
			||||||
 | 
					      |                 ~~~~
 | 
				
			||||||
 | 
					    3 |     _ := int(none)
 | 
				
			||||||
 | 
					    4 |     _ := i8(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:3:11: error: cannot cast `none` to `int`
 | 
				
			||||||
 | 
					    1 | fn main() {
 | 
				
			||||||
 | 
					    2 |     _ := string(none)
 | 
				
			||||||
 | 
					    3 |     _ := int(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					    4 |     _ := i8(none)
 | 
				
			||||||
 | 
					    5 |     _ := i16(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:4:10: error: cannot cast `none` to `i8`
 | 
				
			||||||
 | 
					    2 |     _ := string(none)
 | 
				
			||||||
 | 
					    3 |     _ := int(none)
 | 
				
			||||||
 | 
					    4 |     _ := i8(none)
 | 
				
			||||||
 | 
					      |             ~~~~
 | 
				
			||||||
 | 
					    5 |     _ := i16(none)
 | 
				
			||||||
 | 
					    6 |     _ := i64(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:5:11: error: cannot cast `none` to `i16`
 | 
				
			||||||
 | 
					    3 |     _ := int(none)
 | 
				
			||||||
 | 
					    4 |     _ := i8(none)
 | 
				
			||||||
 | 
					    5 |     _ := i16(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					    6 |     _ := i64(none)
 | 
				
			||||||
 | 
					    7 |     _ := u16(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:6:11: error: cannot cast `none` to `i64`
 | 
				
			||||||
 | 
					    4 |     _ := i8(none)
 | 
				
			||||||
 | 
					    5 |     _ := i16(none)
 | 
				
			||||||
 | 
					    6 |     _ := i64(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					    7 |     _ := u16(none)
 | 
				
			||||||
 | 
					    8 |     _ := u32(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:7:11: error: cannot cast `none` to `u16`
 | 
				
			||||||
 | 
					    5 |     _ := i16(none)
 | 
				
			||||||
 | 
					    6 |     _ := i64(none)
 | 
				
			||||||
 | 
					    7 |     _ := u16(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					    8 |     _ := u32(none)
 | 
				
			||||||
 | 
					    9 |     _ := u64(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:8:11: error: cannot cast `none` to `u32`
 | 
				
			||||||
 | 
					    6 |     _ := i64(none)
 | 
				
			||||||
 | 
					    7 |     _ := u16(none)
 | 
				
			||||||
 | 
					    8 |     _ := u32(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					    9 |     _ := u64(none)
 | 
				
			||||||
 | 
					   10 |     _ := rune(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:9:11: error: cannot cast `none` to `u64`
 | 
				
			||||||
 | 
					    7 |     _ := u16(none)
 | 
				
			||||||
 | 
					    8 |     _ := u32(none)
 | 
				
			||||||
 | 
					    9 |     _ := u64(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					   10 |     _ := rune(none)
 | 
				
			||||||
 | 
					   11 |     _ := f32(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:10:12: error: cannot cast `none` to `rune`
 | 
				
			||||||
 | 
					    8 |     _ := u32(none)
 | 
				
			||||||
 | 
					    9 |     _ := u64(none)
 | 
				
			||||||
 | 
					   10 |     _ := rune(none)
 | 
				
			||||||
 | 
					      |               ~~~~
 | 
				
			||||||
 | 
					   11 |     _ := f32(none)
 | 
				
			||||||
 | 
					   12 |     _ := f64(none)
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:11:11: error: cannot cast `none` to `f32`
 | 
				
			||||||
 | 
					    9 |     _ := u64(none)
 | 
				
			||||||
 | 
					   10 |     _ := rune(none)
 | 
				
			||||||
 | 
					   11 |     _ := f32(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					   12 |     _ := f64(none)
 | 
				
			||||||
 | 
					   13 | }
 | 
				
			||||||
 | 
					vlib/v/checker/tests/none_type_cast_err.vv:12:11: error: cannot cast `none` to `f64`
 | 
				
			||||||
 | 
					   10 |     _ := rune(none)
 | 
				
			||||||
 | 
					   11 |     _ := f32(none)
 | 
				
			||||||
 | 
					   12 |     _ := f64(none)
 | 
				
			||||||
 | 
					      |              ~~~~
 | 
				
			||||||
 | 
					   13 | }
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,13 @@
 | 
				
			||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
						_ := string(none)
 | 
				
			||||||
 | 
						_ := int(none)
 | 
				
			||||||
 | 
						_ := i8(none)
 | 
				
			||||||
 | 
						_ := i16(none)
 | 
				
			||||||
 | 
						_ := i64(none)
 | 
				
			||||||
 | 
						_ := u16(none)
 | 
				
			||||||
 | 
						_ := u32(none)
 | 
				
			||||||
 | 
						_ := u64(none)
 | 
				
			||||||
 | 
						_ := rune(none)
 | 
				
			||||||
 | 
						_ := f32(none)
 | 
				
			||||||
 | 
						_ := f64(none)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue