diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b594d03ff3..68cf2167ba 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1601,7 +1601,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { if v := scope.find_var(ident.name) { if left_first is ast.Ident { assigned_var := left_first - if !v.is_mut && assigned_var.is_mut { + if !v.is_mut && assigned_var.is_mut && !c.inside_unsafe { c.error('`$ident.name` is immutable, cannot have a mutable reference to it', node.pos) } diff --git a/vlib/v/tests/ptr_arithmetic_test.v b/vlib/v/tests/ptr_arithmetic_test.v index 0d6bc29c25..c06cd21659 100644 --- a/vlib/v/tests/ptr_arithmetic_test.v +++ b/vlib/v/tests/ptr_arithmetic_test.v @@ -1,18 +1,24 @@ fn test_ptr_arithmetic(){ - v := 4 - mut p := &v unsafe { + // Do NOT move this outside unsafe{}. + // It causes too much churn in CI when new checks are implemented. + // If you want to implement a specific failing test, do so inside + // vlib/v/checker/tests/ , NOT here. + v := 4 + mut p := &v p++ p += 2 p = p - 1 + assert p == &v + 2 + p = p + 1 + assert p == &v + 3 + r := p++ + assert r == &v + 3 + assert p == &v + 4 } - assert p == unsafe {&v + 2} - p = unsafe { p + 1 } - assert p == unsafe {&v + 3} - r := unsafe { p++ } - assert r == unsafe {&v + 3} - assert p == unsafe {&v + 4} - +} + +fn test_ptr_arithmetic_over_byteptr() { // byteptr, voidptr, charptr are handled differently mut q := byteptr(10) unsafe {