Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gftea opened this issue May 19, 2024 · 6 comments
Open

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

gftea opened this issue May 19, 2024 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-traits Area: Trait system L-unconditional_recursion Lint: unconditional_recursion T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gftea
Copy link
Contributor

gftea commented May 19, 2024

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

@gftea gftea added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 19, 2024
@workingjubilee
Copy link
Contributor

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

@workingjubilee workingjubilee added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label May 20, 2024
@gftea gftea changed the title Emit errors or wranings to user about recursion of display Emit errors or warning to user about recursion of display May 20, 2024
@estebank
Copy link
Contributor

estebank commented May 20, 2024

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 #45838

@estebank estebank added A-traits Area: Trait system L-unconditional_recursion Lint: unconditional_recursion labels May 20, 2024
@so-schen
Copy link

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-
Copy link
Contributor

tbu- commented May 21, 2024

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
Copy link
Contributor Author

gftea commented May 21, 2024

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-traits Area: Trait system L-unconditional_recursion Lint: unconditional_recursion T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants