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

Method resolution fails when through a reference to a trait object on Self: Sized methods #82825

Open
guswynn opened this issue Mar 6, 2021 · 1 comment
Labels
A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@guswynn
Copy link
Contributor

guswynn commented Mar 6, 2021

trait Trait {
    fn static_call(&self) where Self: Sized;
    
    fn maybe_dynamic_call(&self) {
        unimplemented!("unsupported maybe_dynamic_call");
    }
}

impl<T: ?Sized + Trait> Trait for &T {
    fn static_call(&self) where Self: Sized {
        (**self).maybe_dynamic_call();
    }
}

fn foo(x: &dyn Trait) {
    // Works.
    (&x).static_call();
    
    // Doesn't work (goes through `dyn Trait: Trait`,
    // despite `static_call` not being object-safe)
    x.static_call();
}

(playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8d985c6ac7adc1a9835d4c7b5cd74f31)

To me it appears that method resolution should autoref and find the impl on the &T automatically, as the Self: Sized should be

Note that this is specific to &self/&mut self methods, and by-value receivers work, like MANY of the methods on Iterator:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4dbe34cf1ffec880d225d96bd2370907 (rustc even gives an extremely good diagnostic about & vs &mut), though its unclear to me why in that cause, next is callable in that case, but I think it's because it lacks the Self: Sized bound)

(note that the origin of this issue is me investigating how to stabilize the not-yet implemented std::stream::Stream::next https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7e9fe3092902bafe9e5ae607a12e9b4e)

Thanks @eddyb for helping me minimize this and suggesting that this may be possible to resolve in a simple way.

@Rua
Copy link
Contributor

Rua commented Feb 27, 2022

I ran into this issue, see also #51402.

@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. labels Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
4 participants