rust/compiler
bors d1462d8558 Auto merge of #81172 - SimonSapin:ptr-metadata, r=oli-obk
Implement RFC 2580: Pointer metadata & VTable

RFC: https://github.com/rust-lang/rfcs/pull/2580

~~Before merging this PR:~~

* [x] Wait for the end of the RFC’s [FCP to merge](https://github.com/rust-lang/rfcs/pull/2580#issuecomment-759145278).
* [x] Open a tracking issue: https://github.com/rust-lang/rust/issues/81513
* [x] Update `#[unstable]` attributes in the PR with the tracking issue number

----

This PR extends the language with a new lang item for the `Pointee` trait which is special-cased in trait resolution to implement it for all types. Even in generic contexts, parameters can be assumed to implement it without a corresponding bound.

For this I mostly imitated what the compiler was already doing for the `DiscriminantKind` trait. I’m very unfamiliar with compiler internals, so careful review is appreciated.

This PR also extends the standard library with new unstable APIs in `core::ptr` and `std::ptr`:

```rust
pub trait Pointee {
    /// One of `()`, `usize`, or `DynMetadata<dyn SomeTrait>`
    type Metadata: Copy + Send + Sync + Ord + Hash + Unpin;
}

pub trait Thin = Pointee<Metadata = ()>;

pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {}

pub const fn from_raw_parts<T: ?Sized>(*const (), <T as Pointee>::Metadata) -> *const T {}
pub const fn from_raw_parts_mut<T: ?Sized>(*mut (),<T as Pointee>::Metadata) -> *mut T {}

impl<T: ?Sized> NonNull<T> {
    pub const fn from_raw_parts(NonNull<()>, <T as Pointee>::Metadata) -> NonNull<T> {}

    /// Convenience for `(ptr.cast(), metadata(ptr))`
    pub const fn to_raw_parts(self) -> (NonNull<()>, <T as Pointee>::Metadata) {}
}

impl<T: ?Sized> *const T {
    pub const fn to_raw_parts(self) -> (*const (), <T as Pointee>::Metadata) {}
}

impl<T: ?Sized> *mut T {
    pub const fn to_raw_parts(self) -> (*mut (), <T as Pointee>::Metadata) {}
}

/// `<dyn SomeTrait as Pointee>::Metadata == DynMetadata<dyn SomeTrait>`
pub struct DynMetadata<Dyn: ?Sized> {
    // Private pointer to vtable
}

impl<Dyn: ?Sized> DynMetadata<Dyn> {
    pub fn size_of(self) -> usize {}
    pub fn align_of(self) -> usize {}
    pub fn layout(self) -> crate::alloc::Layout {}
}

unsafe impl<Dyn: ?Sized> Send for DynMetadata<Dyn> {}
unsafe impl<Dyn: ?Sized> Sync for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> Debug for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> Unpin for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> Copy for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> Clone for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> Eq for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> PartialEq for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> Ord for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> PartialOrd for DynMetadata<Dyn> {}
impl<Dyn: ?Sized> Hash for DynMetadata<Dyn> {}
```

API differences from the RFC, in areas noted as unresolved questions in the RFC:

* Module-level functions instead of associated `from_raw_parts` functions on `*const T` and `*mut T`, following the precedent of `null`, `slice_from_raw_parts`, etc.
* Added `to_raw_parts`
2021-02-18 04:22:16 +00:00
..
rustc
rustc_apfloat bumped smallvec deps 2021-02-14 18:03:11 +03:00
rustc_arena Rollup merge of #82077 - pierwill:edit-droparena, r=lcnr 2021-02-15 16:07:00 +01:00
rustc_ast Auto merge of #82103 - Dylan-DPC:rollup-5wv8rid, r=Dylan-DPC 2021-02-14 22:26:21 +00:00
rustc_ast_lowering Auto merge of #81611 - cjgillot:meowner, r=estebank 2021-02-16 22:14:32 +00:00
rustc_ast_passes Auto merge of #81346 - hug-dev:nonsecure-call-abi, r=jonas-schievink 2021-02-03 06:00:43 +00:00
rustc_ast_pretty avoid full-slicing slices 2021-02-16 00:31:11 +01:00
rustc_attr Never MIR inline functions with a different instruction set 2021-02-05 00:00:00 +00:00
rustc_builtin_macros avoid full-slicing slices 2021-02-16 00:31:11 +01:00
rustc_codegen_cranelift Use an ItemId inside mir::GlobalAsm. 2021-02-15 19:24:58 +01:00
rustc_codegen_llvm Rollup merge of #82105 - nagisa:nagisa/ensure-removed, r=petrochenkov 2021-02-17 20:37:57 +01:00
rustc_codegen_ssa Rollup merge of #81898 - nanguye2496:nanguye2496/fix_str_and_slice_visualization, r=varkor 2021-02-17 23:51:17 +01:00
rustc_data_structures Auto merge of #81855 - cjgillot:ensure-cache, r=oli-obk 2021-02-15 12:11:59 +00:00
rustc_driver Rollup merge of #82174 - est31:master, r=oli-obk 2021-02-17 20:38:01 +01:00
rustc_error_codes Add long explanation for E0543 2021-02-17 18:08:30 +01:00
rustc_errors Make sure all fields are accounted for in encode_fields! 2021-02-07 14:54:22 -08:00
rustc_expand Rollup merge of #81869 - mark-i-m:leading-vert, r=petrochenkov 2021-02-17 23:51:16 +01:00
rustc_feature Feature gate macro attributes in #[derive] output 2021-02-07 20:08:45 +03:00
rustc_fs_util Optimize away a fs::metadata call. 2021-01-06 08:33:15 -08:00
rustc_graphviz remove redundant closures (clippy::redundant_closure) 2021-01-03 13:34:24 +01:00
rustc_hir Auto merge of #81172 - SimonSapin:ptr-metadata, r=oli-obk 2021-02-18 04:22:16 +00:00
rustc_hir_pretty Auto merge of #81611 - cjgillot:meowner, r=estebank 2021-02-16 22:14:32 +00:00
rustc_incremental Use less HirId when referring to items. 2021-02-15 19:36:12 +01:00
rustc_index Auto merge of #81498 - thomaseizinger:ice-workaround-56935-rustc-index, r=matthewjasper 2021-02-07 08:09:58 +00:00
rustc_infer Rollup merge of #81972 - matthewjasper:hrtb-error-cleanup, r=nikomatsakis 2021-02-17 23:51:18 +01:00
rustc_interface Index Modules using their LocalDefId. 2021-02-15 19:32:30 +01:00
rustc_lexer Return EOF_CHAR constant instead of magic char. 2021-01-07 13:20:04 +01:00
rustc_lint Rollup merge of #79981 - camelid:overflowing_literals-inference-error, r=lcnr 2021-02-17 20:37:48 +01:00
rustc_lint_defs Add --extern-loc to augment unused crate dependency diagnostics 2021-02-07 14:54:20 -08:00
rustc_llvm HWASan support 2021-02-07 23:48:58 -08:00
rustc_macros Switch query descriptions to just String 2021-02-08 17:20:41 -05:00
rustc_metadata Auto merge of #81611 - cjgillot:meowner, r=estebank 2021-02-16 22:14:32 +00:00
rustc_middle Auto merge of #81172 - SimonSapin:ptr-metadata, r=oli-obk 2021-02-18 04:22:16 +00:00
rustc_mir Auto merge of #82235 - GuillaumeGomez:rollup-oflxc08, r=GuillaumeGomez 2021-02-17 19:39:58 +00:00
rustc_mir_build Rollup merge of #82029 - tmiasko:debug, r=matthewjasper 2021-02-14 16:54:52 +01:00
rustc_parse Simplify pattern grammar by allowing nested leading vert 2021-02-15 12:07:54 -06:00
rustc_parse_format parse_format: treat r" as a literal 2021-02-06 15:01:07 +00:00
rustc_passes Use less HirId when referring to items. 2021-02-15 19:36:12 +01:00
rustc_plugin_impl Only store a LocalDefId in hir::Item. 2021-02-15 19:32:10 +01:00
rustc_privacy Only store a LocalDefId in hir::MacroDef. 2021-02-15 19:35:55 +01:00
rustc_query_system Inline try_get_cached 2021-02-16 00:00:00 +00:00
rustc_resolve Auto merge of #81611 - cjgillot:meowner, r=estebank 2021-02-16 22:14:32 +00:00
rustc_save_analysis Only store a LocalDefId in hir::ForeignItem. 2021-02-15 19:32:29 +01:00
rustc_serialize bumped smallvec deps 2021-02-14 18:03:11 +03:00
rustc_session rustdoc: treat edition 2021 as unstable 2021-02-16 19:17:01 -08:00
rustc_span Auto merge of #81172 - SimonSapin:ptr-metadata, r=oli-obk 2021-02-18 04:22:16 +00:00
rustc_symbol_mangling Use less HirId when referring to items. 2021-02-15 19:36:12 +01:00
rustc_target avoid full-slicing slices 2021-02-16 00:31:11 +01:00
rustc_trait_selection Auto merge of #81172 - SimonSapin:ptr-metadata, r=oli-obk 2021-02-18 04:22:16 +00:00
rustc_traits Rollup merge of #82029 - tmiasko:debug, r=matthewjasper 2021-02-14 16:54:52 +01:00
rustc_ty_utils Auto merge of #81172 - SimonSapin:ptr-metadata, r=oli-obk 2021-02-18 04:22:16 +00:00
rustc_type_ir Move a few more types to rustc_type_ir 2021-01-18 21:06:12 +01:00
rustc_typeck Auto merge of #81172 - SimonSapin:ptr-metadata, r=oli-obk 2021-02-18 04:22:16 +00:00