mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
parent
725e25991d
commit
e995d991f1
9 changed files with 101 additions and 4 deletions
|
@ -899,6 +899,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
|
||||||
// Check `x := &y` and `mut x := <-ch`
|
// Check `x := &y` and `mut x := <-ch`
|
||||||
if mut right_first is ast.PrefixExpr {
|
if mut right_first is ast.PrefixExpr {
|
||||||
mut right_node := right_first
|
mut right_node := right_first
|
||||||
|
is_amp := right_first.op == .amp
|
||||||
left_first := node.left[0]
|
left_first := node.left[0]
|
||||||
if left_first is ast.Ident {
|
if left_first is ast.Ident {
|
||||||
assigned_var := left_first
|
assigned_var := left_first
|
||||||
|
@ -920,6 +921,10 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
|
||||||
v := expr.obj
|
v := expr.obj
|
||||||
right_first_type = v.typ
|
right_first_type = v.typ
|
||||||
}
|
}
|
||||||
|
if is_amp && !node.left[0].is_blank_ident() && expr.obj is ast.ConstField {
|
||||||
|
c.error('cannot have mutable reference to const `${expr.name}`',
|
||||||
|
right_node.pos)
|
||||||
|
}
|
||||||
if !c.inside_unsafe && assigned_var.is_mut() && !expr.is_mut() {
|
if !c.inside_unsafe && assigned_var.is_mut() && !expr.is_mut() {
|
||||||
c.error('`${expr.name}` is immutable, cannot have a mutable reference to it',
|
c.error('`${expr.name}` is immutable, cannot have a mutable reference to it',
|
||||||
right_node.pos)
|
right_node.pos)
|
||||||
|
|
|
@ -7,3 +7,10 @@ vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv:4:8: error: cannot assi
|
||||||
6 | }
|
6 | }
|
||||||
Details: Specify the type for the constant value. Example:
|
Details: Specify the type for the constant value. Example:
|
||||||
`const a = int(3)`
|
`const a = int(3)`
|
||||||
|
vlib/v/checker/tests/assign_const_ptr_int_literal_err.vv:4:7: error: cannot have mutable reference to const `a`
|
||||||
|
2 |
|
||||||
|
3 | fn main() {
|
||||||
|
4 | b := &a
|
||||||
|
| ^
|
||||||
|
5 | dump(b)
|
||||||
|
6 | }
|
||||||
|
|
|
@ -12,6 +12,13 @@ vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:1
|
||||||
| ~~~~~~~~~~~~~~
|
| ~~~~~~~~~~~~~~
|
||||||
13 | println(a)
|
13 | println(a)
|
||||||
14 | println(b)
|
14 | println(b)
|
||||||
|
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:10:11: error: cannot have mutable reference to const `a_char`
|
||||||
|
8 |
|
||||||
|
9 | fn foo() {
|
||||||
|
10 | mut a := &(a_char)
|
||||||
|
| ^
|
||||||
|
11 | mut b := &((a_char))
|
||||||
|
12 | mut c := &((((a_char))))
|
||||||
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:10:11: error: `a_char` is immutable, cannot have a mutable reference to it
|
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:10:11: error: `a_char` is immutable, cannot have a mutable reference to it
|
||||||
8 |
|
8 |
|
||||||
9 | fn foo() {
|
9 | fn foo() {
|
||||||
|
@ -19,6 +26,13 @@ vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:10:1
|
||||||
| ^
|
| ^
|
||||||
11 | mut b := &((a_char))
|
11 | mut b := &((a_char))
|
||||||
12 | mut c := &((((a_char))))
|
12 | mut c := &((((a_char))))
|
||||||
|
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:11:11: error: cannot have mutable reference to const `a_char`
|
||||||
|
9 | fn foo() {
|
||||||
|
10 | mut a := &(a_char)
|
||||||
|
11 | mut b := &((a_char))
|
||||||
|
| ^
|
||||||
|
12 | mut c := &((((a_char))))
|
||||||
|
13 | println(a)
|
||||||
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:11:11: error: `a_char` is immutable, cannot have a mutable reference to it
|
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:11:11: error: `a_char` is immutable, cannot have a mutable reference to it
|
||||||
9 | fn foo() {
|
9 | fn foo() {
|
||||||
10 | mut a := &(a_char)
|
10 | mut a := &(a_char)
|
||||||
|
@ -26,6 +40,13 @@ vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:11:1
|
||||||
| ^
|
| ^
|
||||||
12 | mut c := &((((a_char))))
|
12 | mut c := &((((a_char))))
|
||||||
13 | println(a)
|
13 | println(a)
|
||||||
|
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:11: error: cannot have mutable reference to const `a_char`
|
||||||
|
10 | mut a := &(a_char)
|
||||||
|
11 | mut b := &((a_char))
|
||||||
|
12 | mut c := &((((a_char))))
|
||||||
|
| ^
|
||||||
|
13 | println(a)
|
||||||
|
14 | println(b)
|
||||||
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:11: error: `a_char` is immutable, cannot have a mutable reference to it
|
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:11: error: `a_char` is immutable, cannot have a mutable reference to it
|
||||||
10 | mut a := &(a_char)
|
10 | mut a := &(a_char)
|
||||||
11 | mut b := &((a_char))
|
11 | mut b := &((a_char))
|
||||||
|
|
14
vlib/v/checker/tests/const_reference_write_err.out
Normal file
14
vlib/v/checker/tests/const_reference_write_err.out
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
vlib/v/checker/tests/const_reference_write_err.vv:14:10: error: cannot have mutable reference to const `a_string`
|
||||||
|
12 | }
|
||||||
|
13 | unsafe {
|
||||||
|
14 | ptr := &a_string
|
||||||
|
| ^
|
||||||
|
15 | *ptr = f
|
||||||
|
16 |
|
||||||
|
vlib/v/checker/tests/const_reference_write_err.vv:17:11: error: cannot have mutable reference to const `a_struct`
|
||||||
|
15 | *ptr = f
|
||||||
|
16 |
|
||||||
|
17 | ptr2 := &a_struct
|
||||||
|
| ^
|
||||||
|
18 | *ptr2 = a
|
||||||
|
19 | }
|
23
vlib/v/checker/tests/const_reference_write_err.vv
Normal file
23
vlib/v/checker/tests/const_reference_write_err.vv
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
const a_string = ''
|
||||||
|
const a_struct = A1{}
|
||||||
|
|
||||||
|
struct A1 {
|
||||||
|
a_string string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
f := '123'
|
||||||
|
a := A1{
|
||||||
|
a_string: f
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
ptr := &a_string
|
||||||
|
*ptr = f
|
||||||
|
|
||||||
|
ptr2 := &a_struct
|
||||||
|
*ptr2 = a
|
||||||
|
}
|
||||||
|
println('f: ${f} vs. a_string: ${a_string} vs. a_struct.a_string: ${a_struct.a_string}')
|
||||||
|
println('f == a_string : ${a_string == f}')
|
||||||
|
println('f == a_struct.a_string : ${a_struct.a_string == f}')
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/checker/tests/immutable_deref.vv:6:7: error: cannot have mutable reference to const `ctx_ptr`
|
||||||
|
4 |
|
||||||
|
5 | fn main() {
|
||||||
|
6 | x := &ctx_ptr
|
||||||
|
| ^
|
||||||
|
7 | unsafe {
|
||||||
|
8 | *x = &Context{}
|
|
@ -3,7 +3,6 @@ struct Context {}
|
||||||
const ctx_ptr = &Context(unsafe { nil })
|
const ctx_ptr = &Context(unsafe { nil })
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// TODO: unsafe bug, having this declaration inside `unsafe` results in an error
|
|
||||||
x := &ctx_ptr
|
x := &ctx_ptr
|
||||||
unsafe {
|
unsafe {
|
||||||
*x = &Context{}
|
*x = &Context{}
|
||||||
|
|
|
@ -12,6 +12,13 @@ vlib/v/checker/tests/mut_assign_ref.vv:11:7: error: `f` is immutable, cannot hav
|
||||||
| ^
|
| ^
|
||||||
12 | p := &f
|
12 | p := &f
|
||||||
13 | _ = p
|
13 | _ = p
|
||||||
|
vlib/v/checker/tests/mut_assign_ref.vv:15:11: error: cannot have mutable reference to const `constant`
|
||||||
|
13 | _ = p
|
||||||
|
14 |
|
||||||
|
15 | mut c := &constant
|
||||||
|
| ^
|
||||||
|
16 | c.value = 200
|
||||||
|
17 | c = &constant
|
||||||
vlib/v/checker/tests/mut_assign_ref.vv:15:11: error: `constant` is immutable, cannot have a mutable reference to it
|
vlib/v/checker/tests/mut_assign_ref.vv:15:11: error: `constant` is immutable, cannot have a mutable reference to it
|
||||||
13 | _ = p
|
13 | _ = p
|
||||||
14 |
|
14 |
|
||||||
|
@ -19,10 +26,24 @@ vlib/v/checker/tests/mut_assign_ref.vv:15:11: error: `constant` is immutable, ca
|
||||||
| ^
|
| ^
|
||||||
16 | c.value = 200
|
16 | c.value = 200
|
||||||
17 | c = &constant
|
17 | c = &constant
|
||||||
|
vlib/v/checker/tests/mut_assign_ref.vv:17:6: error: cannot have mutable reference to const `constant`
|
||||||
|
15 | mut c := &constant
|
||||||
|
16 | c.value = 200
|
||||||
|
17 | c = &constant
|
||||||
|
| ^
|
||||||
|
18 | ic := &constant
|
||||||
|
19 | _ = ic
|
||||||
vlib/v/checker/tests/mut_assign_ref.vv:17:6: error: `constant` is immutable, cannot have a mutable reference to it
|
vlib/v/checker/tests/mut_assign_ref.vv:17:6: error: `constant` is immutable, cannot have a mutable reference to it
|
||||||
15 | mut c := &constant
|
15 | mut c := &constant
|
||||||
16 | c.value = 200
|
16 | c.value = 200
|
||||||
17 | c = &constant
|
17 | c = &constant
|
||||||
| ^
|
| ^
|
||||||
18 | ic := &constant // ok
|
18 | ic := &constant
|
||||||
19 | _ = ic
|
19 | _ = ic
|
||||||
|
vlib/v/checker/tests/mut_assign_ref.vv:18:8: error: cannot have mutable reference to const `constant`
|
||||||
|
16 | c.value = 200
|
||||||
|
17 | c = &constant
|
||||||
|
18 | ic := &constant
|
||||||
|
| ^
|
||||||
|
19 | _ = ic
|
||||||
|
20 |
|
||||||
|
|
|
@ -15,7 +15,7 @@ fn main() {
|
||||||
mut c := &constant
|
mut c := &constant
|
||||||
c.value = 200
|
c.value = 200
|
||||||
c = &constant
|
c = &constant
|
||||||
ic := &constant // ok
|
ic := &constant
|
||||||
_ = ic
|
_ = ic
|
||||||
|
|
||||||
mut mf := f
|
mut mf := f
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue