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

Using into() in the From trait without having implemented Into should generate a compilation error. #11150

Closed
ThibaultEbroussard opened this issue Jul 10, 2023 · 3 comments · Fixed by #12459
Labels
A-lint Area: New lints

Comments

@ThibaultEbroussard
Copy link

ThibaultEbroussard commented Jul 10, 2023

Code

fn main() {
    #[derive(Debug)]
    struct Number(f32);

    impl From<f32> for Number {
        fn from(value: f32) -> Self {
            value.into()
        }
    }

    let value: Number = (10.0).into();
    println!("{value:?}");
}

Current output

Compiling number v0.1.0 (/home/number)
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/number`

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
[1]    25988 IOT instruction (core dumped)  cargo run

Desired output

The compiler should detect that we are calling Into for f32 trait which is automatically implemented by From for Number trait in our implementation of the From for Number trait.

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@riking
Copy link

riking commented Jul 12, 2023

Implementation would be a deny-by-default lint. Not sure whether to recommend Clippy first or straight into rustc.

@Noratrieb
Copy link
Member

I agree that this should live in clippy instead of rustc, @matthiaskrgr do you want to move the issue :)?

@Noratrieb Noratrieb added the A-lint Area: New lints label Jul 13, 2023
@matthiaskrgr matthiaskrgr transferred this issue from rust-lang/rust Jul 13, 2023
@y21
Copy link
Member

y21 commented Sep 6, 2023

I think a more general version of this is #11032. The blanket Into impl just has U::from(self) in its implementation, so this is basically the same idea: from -> into -> from -> ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants