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

checker: fix immutable deref check

This commit is contained in:
Alexander Medvednikov 2023-04-10 04:59:36 +02:00
parent 88f89bde4e
commit 83afa1009e
3 changed files with 18 additions and 1 deletions

View File

@ -707,7 +707,12 @@ fn (mut c Checker) fail_if_immutable(expr_ ast.Expr) (string, token.Pos) {
to_lock, pos = c.fail_if_immutable(expr.expr)
}
ast.PrefixExpr {
to_lock, pos = c.fail_if_immutable(expr.right)
if expr.op == .mul && expr.right is ast.Ident {
// Do not fail if dereference is immutable:
// `*x = foo()` doesn't modify `x`
} else {
to_lock, pos = c.fail_if_immutable(expr.right)
}
}
ast.PostfixExpr {
to_lock, pos = c.fail_if_immutable(expr.expr)

View File

View File

@ -0,0 +1,12 @@
struct Context {}
const ctx_ptr = &Context(unsafe { nil })
fn main() {
// TODO unsafe bug, having this declaration inside `unsafe` results in an erro
x := &ctx_ptr
unsafe {
*x = &Context{}
_ = x
}
}