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

Should warn when calling blanket implementation of a method rather than our own #61930

Open
gdesmott opened this issue Jun 18, 2019 · 1 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.

Comments

@gdesmott
Copy link

The following snippet crashes:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted (core dumped)
use std::fmt;

struct Badger;

impl Badger {
    fn to_string<'a>(self) -> &'a str {
        "badger"
    }
}

impl fmt::Display for Badger {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.write_str(&self.to_string())
    }
}

fn main() {
    let b = Badger {};
    println!("{}", b);
}

fmt() calls the blanket implementation of ToString, which is using Display, instead of our own implementation resulting in an infinite recursion.

We wouldn't have the problem if our function was taking &self but that's still pretty confusing and prone to errors. It would be good to have at least a warning (or a clippy lint?) for such patterns.

@jonas-schievink
Copy link
Contributor

This is effectively #57965

@jonas-schievink jonas-schievink added 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. labels Jun 18, 2019
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.
Projects
None yet
Development

No branches or pull requests

2 participants