Skip to content

Commit c669ee1

Browse files
authored
checker: detect recursive .str() method calls (#27926)
1 parent 556aa27 commit c669ee1

7 files changed

Lines changed: 2327 additions & 39 deletions

File tree

doc/docs.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3600,6 +3600,23 @@ red := Color{
36003600
println(red)
36013601
```
36023602

3603+
**Note:** Calling `.str()` on an unchanged receiver, or an unchanged alias of it,
3604+
inside a custom `str()` method is not allowed because it causes infinite recursion.
3605+
Use string interpolation of the individual fields instead, or for type aliases,
3606+
cast to the underlying type first:
3607+
3608+
```v failcompile
3609+
struct Color {
3610+
r int
3611+
g int
3612+
b int
3613+
}
3614+
3615+
fn (c Color) str() string {
3616+
return c.str() // error: cannot call `str()` method recursively
3617+
}
3618+
```
3619+
36033620
### Dumping expressions at runtime
36043621

36053622
You can dump/trace the value of any V expression using `dump(expr)`.

0 commit comments

Comments
 (0)