Open
Description
The following snippet crashes:
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted (core dumped)
use std::fmt;
struct Badger;
impl Badger {
fn to_string<'a>(self) -> &'a str {
"badger"
}
}
impl fmt::Display for Badger {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(&self.to_string())
}
}
fn main() {
let b = Badger {};
println!("{}", b);
}
fmt()
calls the blanket implementation of ToString
, which is using Display
, instead of our own implementation resulting in an infinite recursion.
We wouldn't have the problem if our function was taking &self
but that's still pretty confusing and prone to errors. It would be good to have at least a warning (or a clippy lint?) for such patterns.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
jonas-schievink commentedon Jun 18, 2019
This is effectively #57965