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

Causing stack overflow by calling to_string in Display trait implementation #63652

Closed
lukaseller opened this issue Aug 17, 2019 · 2 comments
Closed
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@lukaseller
Copy link

lukaseller commented Aug 17, 2019

As a mentor on exercism.io I came across the following code:

impl Display for Clock {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(f, "{}", self.to_string())
    }
}

This causes a stack overflow: The reason for that being that to_string is automatically implemented by the Display trait - which generates a loop. At least that is my explaination.

Is there a way for the compiler to catch mistakes like this? This is actually the first time i really saw someone generate a runtime error with safe rust (except for panics of course).

@Centril Centril added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Aug 17, 2019
@jonas-schievink
Copy link
Contributor

#57965 would fix this

@jonas-schievink
Copy link
Contributor

This also looks like a direct duplicate of #45838, so closing in favor of that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants