Skip to content

Struct containing only associated type of generic may be too constraining variance wise #140109

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

Open
krtab opened this issue Apr 21, 2025 · 0 comments
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@krtab
Copy link
Contributor

krtab commented Apr 21, 2025

The following code is currently rejected by the compiler, because sub-typing for S is taken on the whole T and not only its associated type. Maybe this is something that could be improved?

trait TraitAssoc {
    type Assoc;
    fn assoc(self) -> Self::Assoc;
}

impl<T> TraitAssoc for &T {
    type Assoc = ();
    
    fn assoc(self) -> Self::Assoc {
        ()
    }
    
}

struct S<T: TraitAssoc> (
     T::Assoc
);

impl<T: TraitAssoc> S<T> {
    fn new(x : T) -> Self {
        Self(x.assoc())
    }
}

fn maybe_could_compile() {
    let v : Vec<u8> = vec![];
    let s = S::new(&v);
    drop(v);
    drop(s);
}

This also happens with nex-gen trait solver AFAICT.

@krtab krtab added the C-bug Category: This is a bug. label Apr 21, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 21, 2025
@krtab krtab changed the title Struct containing only associated type of generic may be to constraining variance wise Struct containing only associated type of generic may be too constraining variance wise Apr 21, 2025
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. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

2 participants