cgen: fix typeof variadic type (#7063)

pull/7070/head
yuyi 2020-12-01 23:11:37 +08:00 committed by GitHub
parent 4f540e6ac3
commit d3deaa1f59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 13 deletions

View File

@ -2663,6 +2663,8 @@ fn (mut g Gen) typeof_expr(node ast.TypeOf) {
repr += ' ${util.strip_main_name(g.table.get_type_name(fn_info.return_type))}'
}
g.write('tos_lit("$repr")')
} else if node.expr_type.has_flag(.variadic) {
g.write('tos_lit("...${util.strip_main_name(sym.name)}")')
} else {
g.write('tos_lit("${util.strip_main_name(sym.name)}")')
}

View File

@ -47,7 +47,7 @@ fn test_typeof_on_structs() {
assert typeof(astruct_dynamic).name == '[]FooBar'
}
type MySumType = int | f32 | FooBar
type MySumType = FooBar | f32 | int
pub fn (ms MySumType) str() string {
match ms {
@ -61,7 +61,9 @@ pub fn (ms MySumType) str() string {
fn test_typeof_on_sumtypes() {
a := MySumType(int(32))
b := MySumType(f32(123.0))
c := MySumType(FooBar{x:43})
c := MySumType(FooBar{
x: 43
})
assert typeof(a) == 'int'
assert typeof(b) == 'f32'
assert typeof(c) == 'FooBar'
@ -75,11 +77,20 @@ fn test_typeof_on_sumtypes() {
}
//
struct UnaryExpr {
a string
}
struct UnaryExpr { a string }
struct BinExpr { a string b string }
struct BoolExpr { z int }
type ExprType = BoolExpr | BinExpr | UnaryExpr
struct BinExpr {
a string
b string
}
struct BoolExpr {
z int
}
type ExprType = BinExpr | BoolExpr | UnaryExpr
fn fexpr(k int) ExprType {
match k {
@ -108,10 +119,14 @@ fn test_typeof_on_sumtypes_of_structs() {
fn myfn(i int) int {
return i
}
fn myfn2() {}
fn myfn2() {
}
fn myfn3(i int, s string) byte {
return byte(0)
}
fn myfn4() i8 {
return -1
}
@ -144,8 +159,25 @@ fn test_generic_type() {
assert type_name([v]) == '[]int'
assert type_name([[v]]) == '[][]int'
assert type_name(FooBar{}) == 'FooBar'
assert array_item_type([v]) == 'int'
assert array_item_type([[v]]) == '[]int'
// assert array_item_type([&v]) == '&int'
}
fn variadic_int(x ...int) string {
return typeof(x)
}
fn variadic_bool(x ...bool) string {
return typeof(x)
}
fn variadic_f64(x ...f64) string {
return typeof(x)
}
fn test_variadic_type() {
assert variadic_int(1, 2, 3) == '...int'
assert variadic_bool(true, false) == '...bool'
assert variadic_f64(3.1, 3.2) == '...f64'
}