1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix detection of invalid insertions, fixes #3600 (#11945)

This commit is contained in:
Alexander Ivanov 2021-09-23 11:59:43 +03:00 committed by GitHub
parent 27f5c35bde
commit 4d0f835548
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -1586,8 +1586,13 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
}
// []T << T or []T << []T
unwrapped_right_type := c.unwrap_generic(right_type)
if c.check_types(unwrapped_right_type, left_value_type)
|| c.check_types(unwrapped_right_type, c.unwrap_generic(left_type)) {
if c.check_types(unwrapped_right_type, left_value_type) {
// []&T << T is wrong: we check for that, !(T.is_ptr()) && ?(&T).is_ptr()
if !(!unwrapped_right_type.is_ptr() && left_value_type.is_ptr()
&& left_value_type.share() == .mut_t) {
return ast.void_type
}
} else if c.check_types(unwrapped_right_type, c.unwrap_generic(left_type)) {
return ast.void_type
}
c.error('cannot append `$right_sym.name` to `$left_sym.name`', right_pos)

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/invalid_insert_references_test.vv:8:7: error: cannot append `int literal` to `[]&int`
6 | a << &c
7 | c = 2
8 | a << 1
| ^
9 | println(a)
10 | }

View File

@ -0,0 +1,14 @@
// fixes https://github.com/vlang/v/issues/3600, test based on a simplified version of example by https://github.com/radare
fn test_invalid_insert_references() {
b := 0
mut a := [&b]
mut c := 1
a << &c
c = 2
a << 1
println(a)
}
fn main() {
test_invalid_insert_references()
}