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

Removing an identity pointer cast can cause vtable kind unification for projected types to fail #103384

Closed
CAD97 opened this issue Oct 22, 2022 · 2 comments · Fixed by #103390
Closed
Labels
C-bug Category: This is a bug.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Oct 22, 2022

[playground]

use core::marker::PhantomData;

pub trait TypeTag<'a>: 'static {
    type Type: ?Sized;
}

pub unsafe trait PointerTag<'a>: TypeTag<'a>
where
    Self::Type: Sized,
{
    type Pointee: TypeTag<'a>;

    fn into_raw(this: Self::Type) -> *mut <Self::Pointee as TypeTag<'a>>::Type;
}

pub struct Rc<I>(PhantomData<I>);

impl<'a, I: TypeTag<'a>> TypeTag<'a> for Rc<I> {
    type Type = std::rc::Rc<I::Type>;
}

unsafe impl<'a, I: TypeTag<'a>> PointerTag<'a> for Rc<I> {
    type Pointee = I;

    fn into_raw(this: Self::Type) -> *mut I::Type {
        std::rc::Rc::into_raw(this) /* as *const I::Type */ as *mut I::Type
    }
}

results in the error

error[[E0606]](https://doc.rust-lang.org/nightly/error-index.html#E0606): casting `*const <I as TypeTag<'_>>::Type` as `*mut <I as TypeTag<'a>>::Type` is invalid
  --> src/lib.rs:26:9
   |
26 |         std::rc::Rc::into_raw(this) /* as *const I::Type */ as *mut I::Type
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: vtable kinds may not match

For more information about this error, try `rustc --explain E0606`.

Uncommenting the as *const I::Type lets this compile without error.

Meta

playground 1.66.0-nightly (2022-10-20 dcb3761)

@CAD97 CAD97 added the C-bug Category: This is a bug. label Oct 22, 2022
@compiler-errors
Copy link
Member

We should probably be doing this cast check modulo regions, inspired by #101520.

@compiler-errors
Copy link
Member

Minimized a bit, just to demonstrate the underlying issue:

trait Tag<'a> {
    type Type: ?Sized;
}

trait IntoRaw: for<'a> Tag<'a> {
    fn into_raw(this: *const <Self as Tag<'_>>::Type) -> *mut <Self as Tag<'_>>::Type;
}

impl<T: for<'a> Tag<'a>> IntoRaw for T {
    fn into_raw(this: *const <Self as Tag<'_>>::Type) -> *mut <Self as Tag<'_>>::Type {
        this as *mut T::Type
    }
}

fn main() {}

@bors bors closed this as completed in 9cdfe03 Nov 20, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…, r=eholk

Check fat pointer metadata compatibility modulo regions

Regions don't really mean anything anyways during hir typeck.

If this `erase_regions` makes anyone nervous, it's probably equally valid to just equate the types using a type relation, but regardless we should _not_ be using strict type equality while region variables are present.

Fixes rust-lang#103384
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

Successfully merging a pull request may close this issue.

2 participants