Skip to content

Commit 4425446

Browse files
authored
cgen: fix print interface when arg0 is function call (fix #26184) (#26188)
1 parent 12f04b5 commit 4425446

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎vlib/v/gen/c/fn.v‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2143,7 +2143,9 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
21432143
}
21442144
}
21452145
if !print_auto_str {
2146-
if is_print {
2146+
if is_print && node.args[0].expr !is ast.CallExpr {
2147+
// only need for `println(err)`
2148+
// not need for `println(err.msg())`
21472149
g.inside_interface_deref = true
21482150
defer(fn) {
21492151
g.inside_interface_deref = false
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
struct MyError {
2+
code int
3+
state string
4+
}
5+
6+
pub fn (e MyError) msg() string {
7+
return 'something went wrong'
8+
}
9+
10+
pub fn (e MyError) code() int {
11+
return e.code
12+
}
13+
14+
fn foo() ! {
15+
return MyError{}
16+
}
17+
18+
fn test_interface_err_msg_print() {
19+
foo() or {
20+
if err is MyError {
21+
eprintln(err.msg())
22+
exit(err.code())
23+
} else {
24+
panic(err)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)