parser: fix parsing interface methods with varargs (#8229)
parent
94b5e47ba8
commit
55efd8309a
|
@ -0,0 +1,4 @@
|
||||||
|
interface Element {
|
||||||
|
unnamed_method(...f64)
|
||||||
|
named_method(params ...f64)
|
||||||
|
}
|
|
@ -476,7 +476,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
||||||
return ast.InterfaceDecl{}
|
return ast.InterfaceDecl{}
|
||||||
}
|
}
|
||||||
// field_names << name
|
// 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{
|
mut args := [table.Param{
|
||||||
name: 'x'
|
name: 'x'
|
||||||
typ: typ
|
typ: typ
|
||||||
|
@ -489,6 +489,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
||||||
params: args
|
params: args
|
||||||
file: p.file_name
|
file: p.file_name
|
||||||
return_type: table.void_type
|
return_type: table.void_type
|
||||||
|
is_variadic: is_variadic
|
||||||
is_pub: true
|
is_pub: true
|
||||||
pos: method_start_pos.extend(p.prev_tok.position())
|
pos: method_start_pos.extend(p.prev_tok.position())
|
||||||
scope: p.scope
|
scope: p.scope
|
||||||
|
@ -502,7 +503,13 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
||||||
method.next_comments = mnext_comments
|
method.next_comments = mnext_comments
|
||||||
methods << method
|
methods << method
|
||||||
// println('register method $name')
|
// 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.top_level_statement_end()
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
|
|
|
@ -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]'
|
||||||
|
}
|
Loading…
Reference in New Issue