Skip to content

unsizing fails when associated types are involved #50213

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
glandium opened this issue Apr 25, 2018 · 2 comments
Open

unsizing fails when associated types are involved #50213

glandium opened this issue Apr 25, 2018 · 2 comments
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

I am trying to implement something very generic, and in the course of doing so, am using trait associated types. That, in turn, apparently doesn't allow the unsizing rules to kick in.

The following compiles fine:

#![crate_type="lib"]
use std::cell::UnsafeCell;

struct Foo<T: ?Sized>(UnsafeCell<T>);

impl<T> Foo<T> {
    fn new(t: T) -> Foo<T> {
        Foo(UnsafeCell::new(t))
    }
}

pub fn foo() {
    let foo: &Foo<[i32]> = &Foo::new([1,2,3]);
}

Now, adding some nesting in the above fails:

#![crate_type="lib"]
use std::cell::UnsafeCell;

trait Wrapper {
    type Type: ?Sized;
}

struct Wrap<T: ?Sized>(T);

impl<T: ?Sized> Wrapper for Wrap<T> {
    type Type = T;
}

struct Foo<T: Wrapper + ?Sized>(UnsafeCell<T::Type>);

impl<T> Foo<Wrap<T>> {
    fn new(t: T) -> Self {
        Foo(UnsafeCell::new(t))
    }
}

pub fn foo() {
    let foo: &Foo<Wrap<[i32]>> = &Foo::new([1,2,3]);
}

That fails with:

error[E0308]: mismatched types
  --> src/lib.rs:23:34
   |
23 |     let foo: &Foo<Wrap<[i32]>> = &Foo::new([1,2,3]);
   |                                  ^^^^^^^^^^^^^^^^^^ expected slice, found array of 3 elements
   |
   = note: expected type `&Foo<Wrap<[i32]>>`
              found type `&Foo<Wrap<[i32; 3]>>`

In fact, it fails similarly with a simplified version that differs even less with the original:

#![crate_type="lib"]
use std::cell::UnsafeCell;

trait Wrapper {
    type Type: ?Sized;
}

impl<T: ?Sized> Wrapper for T {
    type Type = T;
}

struct Foo<T: Wrapper + ?Sized>(UnsafeCell<T::Type>);

impl<T> Foo<T> {
    fn new(t: T) -> Self {
        Foo(UnsafeCell::new(t))
    }
}

pub fn foo() {
    let foo: &Foo<[i32]> = &Foo::new([1,2,3]);
}
error[E0308]: mismatched types
  --> src/lib.rs:21:28
   |
21 |     let foo: &Foo<[i32]> = &Foo::new([1,2,3]);
   |                            ^^^^^^^^^^^^^^^^^^ expected slice, found array of 3 elements
   |
   = note: expected type `&Foo<[i32]>`
              found type `&Foo<[i32; 3]>`

(In fact, the UnsafeCell is irrelevant, and can be removed too)

#![crate_type="lib"]
trait Wrapper {
    type Type: ?Sized;
}

impl<T: ?Sized> Wrapper for T {
    type Type = T;
}

struct Foo<T: Wrapper + ?Sized>(T::Type);

impl<T> Foo<T> {
    fn new(t: T) -> Self {
        Foo(t)
    }
}

pub fn foo() {
    let foo: &Foo<[i32]> = &Foo::new([1,2,3]);
}
error[E0308]: mismatched types
  --> src/lib.rs:19:28
   |
19 |     let foo: &Foo<[i32]> = &Foo::new([1,2,3]);
   |                            ^^^^^^^^^^^^^^^^^^ expected slice, found array of 3 elements
   |
   = note: expected type `&Foo<[i32]>`
              found type `&Foo<[i32; 3]>`
@glandium glandium changed the title unsizing fails when some nesting is involved unsizing fails when associated types are involved Apr 25, 2018
@pietroalbini pietroalbini added the A-trait-system Area: Trait system label Apr 26, 2018
@XAMPPRocky XAMPPRocky added T-lang Relevant to the language team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Oct 2, 2018
@SoniEx2
Copy link
Contributor

SoniEx2 commented Aug 19, 2022

ran into this issue with GATs as well. see: https://users.rust-lang.org/t/how-can-we-make-this-crate-support-sized/80078

@SoniEx2
Copy link
Contributor

SoniEx2 commented Sep 16, 2022

we believe this can be closed now

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 C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants