rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.49k stars 12.6k forks source link

Emit errors or warning to user about recursion of display #125292

Open gftea opened 4 months ago

gftea commented 4 months ago

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

  1. 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
  2. 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?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=15e6928ea61ea7e8ac7072fa82875e97

workingjubilee commented 4 months ago

Macros, like the one you call there, are pretty good at obscuring these impl details, but yeah we should probably warn.

estebank commented 4 months ago

I could have sworn there was a similar report about using to_string within an impl Display for from maybe 2017/2018, but can't find it 😅

@workingjubilee in the wild I've seen that simple cases where the chain repeats after just one or two jumps represent the bulk of them, so beyond the obfuscation issues, I'm convincing myself that a very limited call-graph analysis to look for unconditional recursivity is warranted. It's funny that it's always involving Display or Debug though 😄

Edit: found it https://github.com/rust-lang/rust/issues/45838

so-schen commented 4 months ago

And regarding the 2nd suggestion, what is your view

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 miss ? by mistake,

tbu- commented 4 months ago

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 miss ? by mistake,

Unlikely to work, there's probably a crate out there that uses it, so it can't be removed due to backward compatibility guarantees.

gftea commented 4 months ago

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 miss ? by mistake,

Unlikely to work, there's probably a crate out there that uses it, so it can't be removed due to backward compatibility guarantees.

can be a warnings?

workingjubilee commented 4 months ago

Yes, we can lint on that. In fact we already lint on a few bad format_args! inputs.