Open
Description
Code
code cause recursion stack overflow
struct Hello;
impl std::fmt::Debug for Hello {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl std::fmt::Display for Hello {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:}", self)
}
}
fn main() {
let h = Hello;
println!("{}", h);
}
Current output
Runtime error instead of compile time error at present
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Desired output
In a large code base, such small mistake is hard to locate because it does not point out which recursion call cause stack overflow.
Rationale and extra context
- It is calling Display in a implementation for Display, this seems to be abvious recursion can be detected by compiler. I guess there may be general effort to detect recursion call, but if such obvious recursion can be prevented by compiler earlier
- there is no use case for using
:
as standalone formatter, and it is better the compiler to emit error to use:
as standalone formatter because it is likely user want to use:?
but drop?
by mistake,
Other cases
No response
Rust Version
rustc v1.78.0