1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: check comptime call method (no value) used as value (#15241)

This commit is contained in:
yuyi 2022-07-28 02:32:00 +08:00 committed by GitHub
parent 8af87a9e98
commit 60094d95e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/comptime_call_method_void_err.vv:17:20: cgen error: method `sample1()` (no value) used as value
15 | $for method in Dummy.methods {
16 | if os.args.len >= 1 {
17 | println(Dummy{}.$method(os.args[0]))
| ~~~~~~~~~~~~~~~~~~~
18 | }
19 | }

View File

@ -0,0 +1,20 @@
import os
struct Dummy {}
fn (d Dummy) sample2(file_name string) int {
println(file_name)
return 22
}
fn (d Dummy) sample1(file_name string) {
println(file_name)
}
fn main() {
$for method in Dummy.methods {
if os.args.len >= 1 {
println(Dummy{}.$method(os.args[0]))
}
}
}

View File

@ -93,6 +93,9 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
for val in vals {
}
*/
if g.inside_call && m.return_type == ast.void_type {
g.error('method `${m.name}()` (no value) used as value', node.pos)
}
expand_strs := if node.args.len > 0 && m.params.len - 1 >= node.args.len {
arg := node.args[node.args.len - 1]
param := m.params[node.args.len]