From 4ce6e663bfc3415d2f38fbcbf75807529b44b42e Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 10 Jan 2022 18:03:50 +0800 Subject: [PATCH] cgen: check error for comptime call method argument (#13115) --- .../tests/comptime_call_method_args_err.out | 7 +++++++ .../tests/comptime_call_method_args_err.vv | 15 +++++++++++++++ vlib/v/gen/c/comptime.v | 17 +++++++++++------ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 vlib/v/checker/tests/comptime_call_method_args_err.out create mode 100644 vlib/v/checker/tests/comptime_call_method_args_err.vv diff --git a/vlib/v/checker/tests/comptime_call_method_args_err.out b/vlib/v/checker/tests/comptime_call_method_args_err.out new file mode 100644 index 0000000000..18ab038d9b --- /dev/null +++ b/vlib/v/checker/tests/comptime_call_method_args_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/comptime_call_method_args_err.vv:9:14: cgen error: expected 0 arguments to method S1.method_hello, but got 1 + 7 | + 8 | $for method in S1.methods { + 9 | println(s1.$method('yo')) + | ~~~~~~~~~~~~~ + 10 | } + 11 | } diff --git a/vlib/v/checker/tests/comptime_call_method_args_err.vv b/vlib/v/checker/tests/comptime_call_method_args_err.vv new file mode 100644 index 0000000000..c5ba115cf1 --- /dev/null +++ b/vlib/v/checker/tests/comptime_call_method_args_err.vv @@ -0,0 +1,15 @@ +module main + +struct S1 {} + +fn main() { + s1 := S1{} + + $for method in S1.methods { + println(s1.$method('yo')) + } +} + +fn (t S1) method_hello() string { + return 'Hello' +} diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 2b9868311f..ac4443e9c6 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -99,12 +99,17 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { } // check argument length and types if m.params.len - 1 != node.args.len && !expand_strs { - // do not generate anything if the argument lengths don't match - g.writeln('/* skipping ${sym.name}.$m.name due to mismatched arguments list */') - // g.writeln('println(_SLIT("skipping ${node.sym.name}.$m.name due to mismatched arguments list"));') - // eprintln('info: skipping ${node.sym.name}.$m.name due to mismatched arguments list\n' + - //'method.params: $m.params, args: $node.args\n\n') - // verror('expected ${m.params.len-1} arguments to method ${node.sym.name}.$m.name, but got $node.args.len') + if g.inside_call { + g.error('expected ${m.params.len - 1} arguments to method ${sym.name}.$m.name, but got $node.args.len', + node.pos) + } else { + // do not generate anything if the argument lengths don't match + g.writeln('/* skipping ${sym.name}.$m.name due to mismatched arguments list */') + // g.writeln('println(_SLIT("skipping ${node.sym.name}.$m.name due to mismatched arguments list"));') + // eprintln('info: skipping ${node.sym.name}.$m.name due to mismatched arguments list\n' + + //'method.params: $m.params, args: $node.args\n\n') + // verror('expected ${m.params.len-1} arguments to method ${node.sym.name}.$m.name, but got $node.args.len') + } return } // TODO: check argument types