Emit errors or warning to user about recursion of display
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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
Anything else?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the supplied Rust example with rustc or the linked Playground and confirm the stack overflow behavior. Investigate whether the compiler can identify this formatter recursion or the standalone : format syntax without changing valid formatting behavior. Done means an appropriate diagnostic or warning points to the problematic recursion, with coverage for the reported example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100