Skip to content

Commit

Permalink
checker: fix cgen error on sliced references (#13736)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrel committed Mar 14, 2022
1 parent ea3c016 commit 34dd4f3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions vlib/strings/builder.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn (mut b Builder) go_back(n int) {
// cut_last cuts the last `n` bytes from the buffer and returns them
pub fn (mut b Builder) cut_last(n int) string {
cut_pos := b.len - n
x := unsafe { (&[]byte(b))[cut_pos..] }
x := unsafe { (*&[]byte(b))[cut_pos..] }
res := x.bytestr()
b.trim(cut_pos)
return res
Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn (b &Builder) last_n(n int) string {
if n > b.len {
return ''
}
x := unsafe { (&[]byte(b))[b.len - n..] }
x := unsafe { (*&[]byte(b))[b.len - n..] }
return x.bytestr()
}

Expand All @@ -157,7 +157,7 @@ pub fn (b &Builder) after(n int) string {
if n >= b.len {
return ''
}
x := unsafe { (&[]byte(b))[n..] }
x := unsafe { (*&[]byte(b))[n..] }
return x.bytestr()
}

Expand Down
9 changes: 6 additions & 3 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3770,16 +3770,19 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
'(note, that variables may be mutable but string values are always immutable, like in Go and Java)',
node.pos)
}
if !c.inside_unsafe && ((typ.is_ptr() && !typ.has_flag(.shared_f)
&& !node.left.is_auto_deref_var()) || typ.is_pointer()) {
if (typ.is_ptr() && !typ.has_flag(.shared_f) && !node.left.is_auto_deref_var())
|| typ.is_pointer() {
mut is_ok := false
if mut node.left is ast.Ident {
if mut node.left.obj is ast.Var {
// `mut param []T` function parameter
is_ok = node.left.obj.is_mut && node.left.obj.is_arg && !typ.deref().is_ptr()
}
}
if !is_ok && !c.pref.translated && !c.file.is_translated {
if !is_ok && node.index is ast.RangeExpr {
s := c.table.type_to_str(typ)
c.error('type `$s` does not support slicing', node.pos)
} else if !c.inside_unsafe && !is_ok && !c.pref.translated && !c.file.is_translated {
c.warn('pointer indexing is only allowed in `unsafe` blocks', node.pos)
}
}
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/ptr_slice.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vlib/v/checker/tests/ptr_slice.vv:9:17: error: type `&Foo` does not support slicing
7 |
8 | fn main() {
9 | fs := jeje()[1..]
| ~~~~~
10 | println(fs)
11 | vs := byteptr(0)[..3]
vlib/v/checker/tests/ptr_slice.vv:11:21: error: type `byteptr` does not support slicing
9 | fs := jeje()[1..]
10 | println(fs)
11 | vs := byteptr(0)[..3]
| ~~~~~
12 | println(vs)
13 | }
13 changes: 13 additions & 0 deletions vlib/v/checker/tests/ptr_slice.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct Foo {
}

fn jeje() &Foo {
return &Foo{}
}

fn main() {
fs := jeje()[1..]
println(fs)
vs := byteptr(0)[..3]
println(vs)
}

0 comments on commit 34dd4f3

Please sign in to comment.