-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Reduce ast::ptr::P
to a typedef of Box
#141603
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a35675d
Remove `'static` bounds on `P`.
nnethercote 6973fa0
Remove `P::map`.
nnethercote 0f285e3
Remove the one use of `P<[T]>`.
nnethercote 6859750
Remove support for `P<[T]>`.
nnethercote c42d1fc
Remove unused `P` stuff.
nnethercote 991c91f
Reduce `P<T>` to a typedef of `Box<T>`.
nnethercote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -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) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be fixed. I'll do so in a followup.