cgen: check unknown sizeof type (#8815)

pull/8819/head
yuyi 2021-02-18 15:38:57 +08:00 committed by GitHub
parent bf6e9ff95a
commit a119affeba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/unknown_sizeof_type_err_a.vv:14:34: cgen error: unknown type `T`
12 | println("size of Abc: ${sizeof(Abc)}")
13 | println("size of Xyz: ${sizeof(Xyz)}")
14 | println("size of Test: ${sizeof(T)}")
| ^
15 | }

View File

@ -0,0 +1,15 @@
struct Abc {
i int
}
struct Xyz {
f f64
}
type Test = Abc | Xyz
fn main() {
println("size of Abc: ${sizeof(Abc)}")
println("size of Xyz: ${sizeof(Xyz)}")
println("size of Test: ${sizeof(T)}")
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/unknown_sizeof_type_err_b.vv:14:34: cgen error: unknown type `Zabc`
12 | println("size of Abc: ${sizeof(Abc)}")
13 | println("size of Xyz: ${sizeof(Xyz)}")
14 | println("size of Test: ${sizeof(Zabc)}")
| ~~~~
15 | }

View File

@ -0,0 +1,15 @@
struct Abc {
i int
}
struct Xyz {
f f64
}
type Test = Abc | Xyz
fn main() {
println("size of Abc: ${sizeof(Abc)}")
println("size of Xyz: ${sizeof(Xyz)}")
println("size of Test: ${sizeof(Zabc)}")
}

View File

@ -2786,6 +2786,10 @@ fn (mut g Gen) expr(node ast.Expr) {
} }
ast.SizeOf { ast.SizeOf {
node_typ := g.unwrap_generic(node.typ) node_typ := g.unwrap_generic(node.typ)
sym := g.table.get_type_symbol(node_typ)
if sym.language == .v && sym.kind in [.placeholder, .any] {
g.error('unknown type `$sym.name`', node.pos)
}
styp := g.typ(node_typ) styp := g.typ(node_typ)
g.write('/*SizeOf*/ sizeof(${util.no_dots(styp)})') g.write('/*SizeOf*/ sizeof(${util.no_dots(styp)})')
} }

View File

@ -163,9 +163,9 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
} }
} }
.key_sizeof { .key_sizeof {
pos := p.tok.position()
p.next() // sizeof p.next() // sizeof
p.check(.lpar) p.check(.lpar)
pos := p.tok.position()
is_known_var := p.mark_var_as_used(p.tok.lit) is_known_var := p.mark_var_as_used(p.tok.lit)
// assume mod. prefix leads to a type // assume mod. prefix leads to a type
if is_known_var || !(p.known_import(p.tok.lit) || p.tok.kind.is_start_of_type()) { if is_known_var || !(p.known_import(p.tok.lit) || p.tok.kind.is_start_of_type()) {