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

Presence of Add impl for unrelated type prevents compilation #77143

Closed
djc opened this issue Sep 24, 2020 · 8 comments
Closed

Presence of Add impl for unrelated type prevents compilation #77143

djc opened this issue Sep 24, 2020 · 8 comments
Labels
C-bug Category: This is a bug.

Comments

@djc
Copy link
Contributor

djc commented Sep 24, 2020

Simplified version of bodil/smartstring#7.

I tried this code:

fn main() {
    println!("{}", "Hello".to_string() + &(" World".to_string()));
}

struct FakeString {}

impl std::ops::Add<FakeString> for String {
    type Output = Self;
    fn add(mut self, rhs: FakeString) -> Self::Output {
        unreachable!()
    }
}

I expected to see this happen: this should compile correctly.

Instead, it fails like this:

error[E0277]: cannot add `&std::string::String` to `std::string::String`
 --> src/main.rs:2:40
  |
2 |     println!("{}", "Hello".to_string() + &(" World".to_string()));
  |                                        ^ no implementation for `std::string::String + &std::string::String`
  |
  = help: the trait `std::ops::Add<&std::string::String>` is not implemented for `std::string::String`

Removing the Add impl makes it work.

Meta

This is on current stable, 1.46.0.

@djc djc added the C-bug Category: This is a bug. label Sep 24, 2020
@djc djc changed the title Presence of Deref impl prevents selection of proper operator impl Presence of Add + Deref impl for unrelated type prevents compilation Sep 24, 2020
@djc djc changed the title Presence of Add + Deref impl for unrelated type prevents compilation Presence of Add impl for unrelated type prevents compilation Sep 24, 2020
@jonas-schievink
Copy link
Contributor

This seems like expected behavior

@djc
Copy link
Contributor Author

djc commented Sep 24, 2020

I'm confused -- why would String + &String start failing in the presence of an impl for String + FakeString?

@jonas-schievink
Copy link
Contributor

Because the trait system then has two impls available to select from, impl Add<&str> for String and impl Add<FakeString> for String, which means that it doesn't know to apply autoderef to the &String argument to get the &str.

@djc
Copy link
Contributor Author

djc commented Sep 24, 2020

Hmm, okay. So I guess this is a duplicate for #44619.

@djc djc closed this as completed Sep 24, 2020
@andy128k
Copy link

@jonas-schievink It doesn't compile even if a FakeString and an Add implementation are in a separated private module.

mod x {
    struct FakeString {}

    impl std::ops::Add<FakeString> for String {
        type Output = Self;
        fn add(mut self, rhs: FakeString) -> Self::Output {
            unreachable!()
        }
    }
}

mod y {
    pub fn hw() {
        println!("{}", "Hello".to_string() + &(" World".to_string()));
    }
}

@jonas-schievink
Copy link
Contributor

Yeah, that doesn't affect trait selection to my knowledge

@erenon
Copy link

erenon commented Jul 1, 2022

Is there a workaround for this issue, short of removing the offending Add implementations? (as they can be in dependencies. e.g: I'm unable to add prometheus_client, as it breaks other code in the project)

erenon added a commit to erenon/client_rust that referenced this issue Jul 4, 2022
They interact with builtin implementation in confusing ways:
rust-lang/rust#77143

Fixes prometheus#68
erenon added a commit to erenon/client_rust that referenced this issue Jul 4, 2022
They interact with builtin implementation in confusing ways:
rust-lang/rust#77143

Fixes prometheus#68

Signed-off-by: Benedek Thaler <[email protected]>
erenon added a commit to erenon/client_rust that referenced this issue Jul 10, 2022
They interact with builtin implementation in confusing ways:
rust-lang/rust#77143

Fixes prometheus#68

Signed-off-by: Benedek Thaler <[email protected]>
mxinden pushed a commit to prometheus/client_rust that referenced this issue Jul 11, 2022
They conflict with builtin implementations, see
rust-lang/rust#77143

Signed-off-by: Benedek Thaler <[email protected]>
@mattfbacon
Copy link
Contributor

ackintosh pushed a commit to ackintosh/client_rust that referenced this issue Aug 27, 2022
They conflict with builtin implementations, see
rust-lang/rust#77143

Signed-off-by: Benedek Thaler <[email protected]>
Signed-off-by: ackintosh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

5 participants