checker: allow ++, -- on byteptr, charptr (#7218)
parent
92a8db5b2b
commit
cf755d40b7
|
@ -2668,7 +2668,7 @@ Examples of potentially memory-unsafe operations are:
|
||||||
|
|
||||||
To mark potentially memory-unsafe operations, enclose them in an `unsafe` block:
|
To mark potentially memory-unsafe operations, enclose them in an `unsafe` block:
|
||||||
|
|
||||||
```v failcompile
|
```v wip
|
||||||
// allocate 2 uninitialized bytes & return a reference to them
|
// allocate 2 uninitialized bytes & return a reference to them
|
||||||
mut p := unsafe { malloc(2) }
|
mut p := unsafe { malloc(2) }
|
||||||
p[0] = `h` // Error: pointer indexing is only allowed in `unsafe` blocks
|
p[0] = `h` // Error: pointer indexing is only allowed in `unsafe` blocks
|
||||||
|
|
|
@ -4135,14 +4135,13 @@ fn (c &Checker) has_return(stmts []ast.Stmt) ?bool {
|
||||||
pub fn (mut c Checker) postfix_expr(mut node ast.PostfixExpr) table.Type {
|
pub fn (mut c Checker) postfix_expr(mut node ast.PostfixExpr) table.Type {
|
||||||
typ := c.expr(node.expr)
|
typ := c.expr(node.expr)
|
||||||
typ_sym := c.table.get_type_symbol(typ)
|
typ_sym := c.table.get_type_symbol(typ)
|
||||||
// if !typ.is_number() {
|
if !typ_sym.is_number() && typ_sym.kind !in [.byteptr, .charptr] {
|
||||||
if !typ_sym.is_number() {
|
|
||||||
c.error('invalid operation: $node.op.str() (non-numeric type `$typ_sym.name`)',
|
c.error('invalid operation: $node.op.str() (non-numeric type `$typ_sym.name`)',
|
||||||
node.pos)
|
node.pos)
|
||||||
} else {
|
} else {
|
||||||
node.auto_locked, _ = c.fail_if_immutable(node.expr)
|
node.auto_locked, _ = c.fail_if_immutable(node.expr)
|
||||||
}
|
}
|
||||||
if (typ.is_ptr() || typ_sym.is_pointer()) && !c.inside_unsafe {
|
if !c.inside_unsafe && (typ.is_ptr() || typ_sym.is_pointer()) {
|
||||||
c.warn('pointer arithmetic is only allowed in `unsafe` blocks', node.pos)
|
c.warn('pointer arithmetic is only allowed in `unsafe` blocks', node.pos)
|
||||||
}
|
}
|
||||||
return typ
|
return typ
|
||||||
|
|
|
@ -28,4 +28,7 @@ fn test_ptr_arithmetic_over_byteptr() {
|
||||||
assert q == byteptr(9)
|
assert q == byteptr(9)
|
||||||
s := unsafe { q - 1 }
|
s := unsafe { q - 1 }
|
||||||
assert s == byteptr(8)
|
assert s == byteptr(8)
|
||||||
|
|
||||||
|
unsafe {q++ q++ q--}
|
||||||
|
assert q == byteptr(10)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue