-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
checker: detect recursive .str() method calls
#27926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3590,6 +3590,12 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool) | |
| && c.table.register_fn_concrete_types(method.fkey(), concrete_types) { | ||
| c.need_recheck_generic_fns = true | ||
| } | ||
| if method_name == 'str' && c.table.cur_fn != unsafe { nil } && c.table.cur_fn.is_method | ||
| && c.table.cur_fn.name == 'str' && node.left is ast.Ident | ||
| && (node.left as ast.Ident).name == c.table.cur_fn.receiver.name | ||
| && left_type.idx() == c.table.cur_fn.receiver.typ.idx() { | ||
| c.error('cannot call `str()` method recursively', node.pos) | ||
|
Comment on lines
+3593
to
+3597
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This condition also fires while checking alias receivers that implement an interface by providing Useful? React with 👍 / 👎. |
||
| } | ||
| node.is_noreturn = method.is_noreturn | ||
| node.is_expand_simple_interpolation = method.is_expand_simple_interpolation | ||
| node.is_ctor_new = method.is_ctor_new | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| vlib/v/checker/tests/sum_type_recursive_str_err.vv:6:12: error: cannot call `str()` method recursively | ||
| 4 | return match n { | ||
| 5 | u8 { n.str() } | ||
| 6 | else { n.str() } | ||
| | ~~~~~ | ||
| 7 | } | ||
| 8 | } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| type Num = u8 | u16 | ||
|
|
||
| fn (n Num) str() string { | ||
| return match n { | ||
| u8 { n.str() } | ||
| else { n.str() } | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| println(Num(u16(0))) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a
strmethod,(n).str()is semantically the same recursive call asn.str(), butnode.leftremains anast.ParExpr, so thisnode.left is ast.Identguard skips the new diagnostic and still allows the runtime recursion/stack overflow that this check is meant to prevent.method_callalready computes an unwrappedleft_expr := node.left.remove_par()earlier, so using that here would catch both forms.Useful? React with 👍 / 👎.