forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#120354 - lukas-code:metadata-normalize, r=lcnr improve normalization of `Pointee::Metadata` This PR makes it so that `<Wrapper<Tail> as Pointee>::Metadata` is normalized to `<Tail as Pointee>::Metadata` if we don't know `Wrapper<Tail>: Sized`. With that, the trait solver can prove projection predicates like `<Wrapper<Tail> as Pointee>::Metadata == <Tail as Pointee>::Metadata`, which makes it possible to use the metadata APIs to cast between the tail and the wrapper: ```rust #![feature(ptr_metadata)] use std::ptr::{self, Pointee}; fn cast_same_meta<T: ?Sized, U: ?Sized>(ptr: *const T) -> *const U where T: Pointee<Metadata = <U as Pointee>::Metadata>, { let (thin, meta) = ptr.to_raw_parts(); ptr::from_raw_parts(thin, meta) } struct Wrapper<T: ?Sized>(T); fn cast_to_wrapper<T: ?Sized>(ptr: *const T) -> *const Wrapper<T> { cast_same_meta(ptr) } ``` Previously, this failed to compile: ``` error[E0271]: type mismatch resolving `<Wrapper<T> as Pointee>::Metadata == <T as Pointee>::Metadata` --> src/lib.rs:16:5 | 15 | fn cast_to_wrapper<T: ?Sized>(ptr: *const T) -> *const Wrapper<T> { | - found this type parameter 16 | cast_same_meta(ptr) | ^^^^^^^^^^^^^^ expected `Wrapper<T>`, found type parameter `T` | = note: expected associated type `<Wrapper<T> as Pointee>::Metadata` found associated type `<T as Pointee>::Metadata` = note: an associated type was expected, but a different one was found ``` (Yes, you can already do this with `as` casts. But using functions is so much :sparkles: *safer* :sparkles:, because you can't change the metadata on accident.) --- This PR essentially changes the built-in impls of `Pointee` from this: ```rust // before impl Pointee for u8 { type Metadata = (); } impl Pointee for [u8] { type Metadata = usize; } // ... impl Pointee for Wrapper<u8> { type Metadata = (); } impl Pointee for Wrapper<[u8]> { type Metadata = usize; } // ... // This impl is only selected if `T` is a type parameter or unnormalizable projection or opaque type. fallback impl<T: ?Sized> Pointee for Wrapper<T> where Wrapper<T>: Sized { type Metadata = (); } // This impl is only selected if `T` is a type parameter or unnormalizable projection or opaque type. fallback impl<T /*: Sized */> Pointee for T { type Metadata = (); } ``` to this: ```rust // after impl Pointee for u8 { type Metadata = (); } impl Pointee for [u8] { type Metadata = usize; } // ... impl<T: ?Sized> Pointee for Wrapper<T> { // in the old solver this will instead project to the "deep" tail directly, // e.g. `Wrapper<Wrapper<T>>::Metadata = T::Metadata` type Metadata = <T as Pointee>::Metadata; } // ... // This impl is only selected if `T` is a type parameter or unnormalizable projection or opaque type. fallback impl<T /*: Sized */> Pointee for T { type Metadata = (); } ```
- Loading branch information
Showing
7 changed files
with
148 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// check-pass | ||
// revisions: old next | ||
//[next] compile-flags: -Znext-solver | ||
|
||
#![feature(ptr_metadata)] | ||
|
||
use std::ptr::{self, Pointee}; | ||
|
||
fn cast_same_meta<T: ?Sized, U: ?Sized>(ptr: *const T) -> *const U | ||
where | ||
T: Pointee<Metadata = <U as Pointee>::Metadata>, | ||
{ | ||
let (thin, meta) = ptr.to_raw_parts(); | ||
ptr::from_raw_parts(thin, meta) | ||
} | ||
|
||
struct Wrapper<T: ?Sized>(T); | ||
|
||
// normalize `Wrapper<T>::Metadata` -> `T::Metadata` | ||
fn wrapper_to_tail<T: ?Sized>(ptr: *const T) -> *const Wrapper<T> { | ||
cast_same_meta(ptr) | ||
} | ||
|
||
// normalize `Wrapper<T>::Metadata` -> `T::Metadata` -> `()` | ||
fn wrapper_to_unit<T>(ptr: *const ()) -> *const Wrapper<T> { | ||
cast_same_meta(ptr) | ||
} | ||
|
||
trait Project { | ||
type Assoc: ?Sized; | ||
} | ||
|
||
struct WrapperProject<T: ?Sized + Project>(T::Assoc); | ||
|
||
// normalize `WrapperProject<T>::Metadata` -> `T::Assoc::Metadata` | ||
fn wrapper_project_tail<T: ?Sized + Project>(ptr: *const T::Assoc) -> *const WrapperProject<T> { | ||
cast_same_meta(ptr) | ||
} | ||
|
||
// normalize `WrapperProject<T>::Metadata` -> `T::Assoc::Metadata` -> `()` | ||
fn wrapper_project_unit<T: ?Sized + Project>(ptr: *const ()) -> *const WrapperProject<T> | ||
where | ||
T::Assoc: Sized, | ||
{ | ||
cast_same_meta(ptr) | ||
} | ||
|
||
// normalize `<[T] as Pointee>::Metadata` -> `usize`, even if `[T]: Sized` | ||
fn sized_slice<T>(ptr: *const [T]) -> *const str | ||
where | ||
[T]: Sized, | ||
{ | ||
cast_same_meta(ptr) | ||
} | ||
|
||
fn main() {} |