parser: fix parsing interface methods with varargs (#8229)

pull/8237/head
Enzo 2021-01-20 22:15:02 +01:00 committed by GitHub
parent 94b5e47ba8
commit 55efd8309a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -0,0 +1,4 @@
interface Element {
unnamed_method(...f64)
named_method(params ...f64)
}

View File

@ -476,7 +476,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
return ast.InterfaceDecl{}
}
// field_names << name
args2, _, _ := p.fn_args() // TODO merge table.Param and ast.Arg to avoid this
args2, _, is_variadic := p.fn_args() // TODO merge table.Param and ast.Arg to avoid this
mut args := [table.Param{
name: 'x'
typ: typ
@ -489,6 +489,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
params: args
file: p.file_name
return_type: table.void_type
is_variadic: is_variadic
is_pub: true
pos: method_start_pos.extend(p.prev_tok.position())
scope: p.scope
@ -502,7 +503,13 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
method.next_comments = mnext_comments
methods << method
// println('register method $name')
ts.register_method(name: name, params: args, return_type: method.return_type, is_pub: true)
ts.register_method(
name: name
params: args
return_type: method.return_type
is_variadic: is_variadic
is_pub: true
)
}
p.top_level_statement_end()
p.check(.rcbr)

View File

@ -0,0 +1,25 @@
interface Element {
method(params ...f64) string
}
struct Foo {}
fn (f &Foo) method(params ...f64) string {
return params.str()
}
fn test_variadic_array_decompose() {
mut a := []Element{}
a << Foo{}
input := [0.0, 1.0]
assert a[0].method(...input) == '[0, 1]'
assert a[0].method(...[0.0, 1.0]) == '[0, 1]'
}
fn test_variadic_multiple_args() {
mut a := []Element{}
a << Foo{}
assert a[0].method(0.0, 1.0) == '[0, 1]'
}