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

parser: clean error when nesting unsafe (#6656)

This commit is contained in:
Enzo 2020-10-20 21:57:24 +02:00 committed by GitHub
parent d881185d79
commit 21db4b338b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 3 deletions

View File

@ -2012,7 +2012,9 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt {
p.error_with_pos('please use `unsafe {`', p.tok.position())
}
p.next()
assert !p.inside_unsafe
if p.inside_unsafe {
p.error_with_pos('already inside `unsafe` block', pos)
}
if p.tok.kind == .rcbr {
// `unsafe {}`
p.next()

View File

@ -92,9 +92,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
}
.key_unsafe {
// unsafe {
p.next()
pos := p.tok.position()
assert !p.inside_unsafe
p.next()
if p.inside_unsafe {
p.error_with_pos('already inside `unsafe` block', pos)
}
p.inside_unsafe = true
p.check(.lcbr)
node = ast.UnsafeExpr{

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/nested_unsafe_expr.vv:4:8: error: already inside `unsafe` block
2 | a := 0
3 | unsafe {
4 | a += unsafe{2}
| ~~~~~~
5 | }
6 | println(a)

View File

@ -0,0 +1,7 @@
fn main() {
a := 0
unsafe {
a += unsafe{2}
}
println(a)
}

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/nested_unsafe_stmt.vv:4:3: error: already inside `unsafe` block
2 | a := 0
3 | unsafe {
4 | unsafe {
| ~~~~~~
5 | a++
6 | }

View File

@ -0,0 +1,9 @@
fn main() {
a := 0
unsafe {
unsafe {
a++
}
}
println(a)
}