diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-06-06 23:53:16 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-06 23:53:16 +0200 | 
| commit | 93ca0af08ca056e9e38d172e9b224d1e607a8e99 (patch) | |
| tree | 2a42a877715e20a48d65748511b18d23e6b3e8c8 /compiler/rustc_ast/src | |
| parent | 15d9b9619c0db448552b092c5259ea6c18cd32fc (diff) | |
| parent | 991c91fdaa5862835799abfd9c2349e06277097e (diff) | |
| download | rust-93ca0af08ca056e9e38d172e9b224d1e607a8e99.tar.gz rust-93ca0af08ca056e9e38d172e9b224d1e607a8e99.zip | |
Rollup merge of #141603 - nnethercote:reduce-P, r=fee1-dead
Reduce `ast::ptr::P` to a typedef of `Box` As per the MCP at https://github.com/rust-lang/compiler-team/issues/878. r? `@fee1-dead`
Diffstat (limited to 'compiler/rustc_ast/src')
| -rw-r--r-- | compiler/rustc_ast/src/ast.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/attr/mod.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/ptr.rs | 210 | 
3 files changed, 11 insertions, 210 deletions
| diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index c9a8adec31a..cf40c3f7f6f 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -1119,10 +1119,9 @@ impl Stmt { pub fn add_trailing_semicolon(mut self) -> Self { self.kind = match self.kind { StmtKind::Expr(expr) => StmtKind::Semi(expr), - StmtKind::MacCall(mac) => { - StmtKind::MacCall(mac.map(|MacCallStmt { mac, style: _, attrs, tokens }| { - MacCallStmt { mac, style: MacStmtStyle::Semicolon, attrs, tokens } - })) + StmtKind::MacCall(mut mac) => { + mac.style = MacStmtStyle::Semicolon; + StmtKind::MacCall(mac) } kind => kind, }; @@ -1724,7 +1723,7 @@ pub enum ExprKind { /// /// Usually not written directly in user code but /// indirectly via the macro `core::mem::offset_of!(...)`. - OffsetOf(P<Ty>, P<[Ident]>), + OffsetOf(P<Ty>, Vec<Ident>), /// A macro invocation; pre-expansion. MacCall(P<MacCall>), diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index f165c4ddcdd..621e3042b62 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -63,7 +63,7 @@ impl Attribute { pub fn unwrap_normal_item(self) -> AttrItem { match self.kind { - AttrKind::Normal(normal) => normal.into_inner().item, + AttrKind::Normal(normal) => normal.item, AttrKind::DocComment(..) => panic!("unexpected doc comment"), } } diff --git a/compiler/rustc_ast/src/ptr.rs b/compiler/rustc_ast/src/ptr.rs index dd923305cdf..fffeab8bbca 100644 --- a/compiler/rustc_ast/src/ptr.rs +++ b/compiler/rustc_ast/src/ptr.rs @@ -1,209 +1,11 @@ -//! The AST pointer. -//! -//! Provides [`P<T>`][struct@P], an owned smart pointer. -//! -//! # Motivations and benefits -//! -//! * **Identity**: sharing AST nodes is problematic for the various analysis -//! passes (e.g., one may be able to bypass the borrow checker with a shared -//! `ExprKind::AddrOf` node taking a mutable borrow). -//! -//! * **Efficiency**: folding can reuse allocation space for `P<T>` and `Vec<T>`, -//! the latter even when the input and output types differ (as it would be the -//! case with arenas or a GADT AST using type parameters to toggle features). -//! -//! * **Maintainability**: `P<T>` provides an interface, which can remain fully -//! functional even if the implementation changes (using a special thread-local -//! heap, for example). Moreover, a switch to, e.g., `P<'a, T>` would be easy -//! and mostly automated. - -use std::fmt::{self, Debug, Display}; -use std::ops::{Deref, DerefMut}; -use std::{slice, vec}; - -use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; -/// An owned smart pointer. +/// A pointer type that uniquely owns a heap allocation of type T. /// -/// See the [module level documentation][crate::ptr] for details. -pub struct P<T: ?Sized> { - ptr: Box<T>, -} +/// This used to be its own type, but now it's just a typedef for `Box` and we are planning to +/// remove it soon. +pub type P<T> = Box<T>; /// Construct a `P<T>` from a `T` value. #[allow(non_snake_case)] -pub fn P<T: 'static>(value: T) -> P<T> { - P { ptr: Box::new(value) } -} - -impl<T: 'static> P<T> { - /// Move out of the pointer. - /// Intended for chaining transformations not covered by `map`. - pub fn and_then<U, F>(self, f: F) -> U - where - F: FnOnce(T) -> U, - { - f(*self.ptr) - } - - /// Equivalent to `and_then(|x| x)`. - pub fn into_inner(self) -> T { - *self.ptr - } - - /// Produce a new `P<T>` from `self` without reallocating. - pub fn map<F>(mut self, f: F) -> P<T> - where - F: FnOnce(T) -> T, - { - let x = f(*self.ptr); - *self.ptr = x; - - self - } - - /// Optionally produce a new `P<T>` from `self` without reallocating. - pub fn filter_map<F>(mut self, f: F) -> Option<P<T>> - where - F: FnOnce(T) -> Option<T>, - { - *self.ptr = f(*self.ptr)?; - Some(self) - } -} - -impl<T: ?Sized> Deref for P<T> { - type Target = T; - - fn deref(&self) -> &T { - &self.ptr - } -} - -impl<T: ?Sized> DerefMut for P<T> { - fn deref_mut(&mut self) -> &mut T { - &mut self.ptr - } -} - -impl<T: 'static + Clone> Clone for P<T> { - fn clone(&self) -> P<T> { - P((**self).clone()) - } -} - -impl<T: ?Sized + Debug> Debug for P<T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - Debug::fmt(&self.ptr, f) - } -} - -impl<T: Display> Display for P<T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - Display::fmt(&**self, f) - } -} - -impl<T> fmt::Pointer for P<T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Pointer::fmt(&self.ptr, f) - } -} - -impl<D: Decoder, T: 'static + Decodable<D>> Decodable<D> for P<T> { - fn decode(d: &mut D) -> P<T> { - P(Decodable::decode(d)) - } -} - -impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<T> { - fn encode(&self, s: &mut S) { - (**self).encode(s); - } -} - -impl<T> P<[T]> { - // FIXME(const-hack) make this const again - pub fn new() -> P<[T]> { - P { ptr: Box::default() } - } - - #[inline(never)] - pub fn from_vec(v: Vec<T>) -> P<[T]> { - P { ptr: v.into_boxed_slice() } - } - - #[inline(never)] - pub fn into_vec(self) -> Vec<T> { - self.ptr.into_vec() - } -} - -impl<T> Default for P<[T]> { - /// Creates an empty `P<[T]>`. - fn default() -> P<[T]> { - P::new() - } -} - -impl<T: Clone> Clone for P<[T]> { - fn clone(&self) -> P<[T]> { - P::from_vec(self.to_vec()) - } -} - -impl<T> From<Vec<T>> for P<[T]> { - fn from(v: Vec<T>) -> Self { - P::from_vec(v) - } -} - -impl<T> From<P<[T]>> for Vec<T> { - fn from(val: P<[T]>) -> Self { - val.into_vec() - } -} - -impl<T> FromIterator<T> for P<[T]> { - fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> P<[T]> { - P::from_vec(iter.into_iter().collect()) - } -} - -impl<T> IntoIterator for P<[T]> { - type Item = T; - type IntoIter = vec::IntoIter<T>; - - fn into_iter(self) -> Self::IntoIter { - self.into_vec().into_iter() - } -} - -impl<'a, T> IntoIterator for &'a P<[T]> { - type Item = &'a T; - type IntoIter = slice::Iter<'a, T>; - fn into_iter(self) -> Self::IntoIter { - self.ptr.iter() - } -} - -impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<[T]> { - fn encode(&self, s: &mut S) { - Encodable::encode(&**self, s); - } -} - -impl<D: Decoder, T: Decodable<D>> Decodable<D> for P<[T]> { - fn decode(d: &mut D) -> P<[T]> { - P::from_vec(Decodable::decode(d)) - } -} - -impl<CTX, T> HashStable<CTX> for P<T> -where - T: ?Sized + HashStable<CTX>, -{ - fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { - (**self).hash_stable(hcx, hasher); - } +pub fn P<T>(value: T) -> P<T> { + Box::new(value) } | 
