Skip to content

Commit 4c6f647

Browse files
authored
Unrolled build for #141603
Rollup merge of #141603 - nnethercote:reduce-P, r=fee1-dead Reduce `ast::ptr::P` to a typedef of `Box` As per the MCP at rust-lang/compiler-team#878. r? `@fee1-dead`
2 parents 868bf2d + 991c91f commit 4c6f647

File tree

17 files changed

+72
-289
lines changed

17 files changed

+72
-289
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,10 +1119,9 @@ impl Stmt {
11191119
pub fn add_trailing_semicolon(mut self) -> Self {
11201120
self.kind = match self.kind {
11211121
StmtKind::Expr(expr) => StmtKind::Semi(expr),
1122-
StmtKind::MacCall(mac) => {
1123-
StmtKind::MacCall(mac.map(|MacCallStmt { mac, style: _, attrs, tokens }| {
1124-
MacCallStmt { mac, style: MacStmtStyle::Semicolon, attrs, tokens }
1125-
}))
1122+
StmtKind::MacCall(mut mac) => {
1123+
mac.style = MacStmtStyle::Semicolon;
1124+
StmtKind::MacCall(mac)
11261125
}
11271126
kind => kind,
11281127
};
@@ -1724,7 +1723,7 @@ pub enum ExprKind {
17241723
///
17251724
/// Usually not written directly in user code but
17261725
/// indirectly via the macro `core::mem::offset_of!(...)`.
1727-
OffsetOf(P<Ty>, P<[Ident]>),
1726+
OffsetOf(P<Ty>, Vec<Ident>),
17281727

17291728
/// A macro invocation; pre-expansion.
17301729
MacCall(P<MacCall>),

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Attribute {
6363

6464
pub fn unwrap_normal_item(self) -> AttrItem {
6565
match self.kind {
66-
AttrKind::Normal(normal) => normal.into_inner().item,
66+
AttrKind::Normal(normal) => normal.item,
6767
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
6868
}
6969
}

compiler/rustc_ast/src/ptr.rs

Lines changed: 6 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -1,209 +1,11 @@
1-
//! The AST pointer.
2-
//!
3-
//! Provides [`P<T>`][struct@P], an owned smart pointer.
4-
//!
5-
//! # Motivations and benefits
6-
//!
7-
//! * **Identity**: sharing AST nodes is problematic for the various analysis
8-
//! passes (e.g., one may be able to bypass the borrow checker with a shared
9-
//! `ExprKind::AddrOf` node taking a mutable borrow).
10-
//!
11-
//! * **Efficiency**: folding can reuse allocation space for `P<T>` and `Vec<T>`,
12-
//! the latter even when the input and output types differ (as it would be the
13-
//! case with arenas or a GADT AST using type parameters to toggle features).
14-
//!
15-
//! * **Maintainability**: `P<T>` provides an interface, which can remain fully
16-
//! functional even if the implementation changes (using a special thread-local
17-
//! heap, for example). Moreover, a switch to, e.g., `P<'a, T>` would be easy
18-
//! and mostly automated.
19-
20-
use std::fmt::{self, Debug, Display};
21-
use std::ops::{Deref, DerefMut};
22-
use std::{slice, vec};
23-
24-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
25-
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
26-
/// An owned smart pointer.
1+
/// A pointer type that uniquely owns a heap allocation of type T.
272
///
28-
/// See the [module level documentation][crate::ptr] for details.
29-
pub struct P<T: ?Sized> {
30-
ptr: Box<T>,
31-
}
3+
/// This used to be its own type, but now it's just a typedef for `Box` and we are planning to
4+
/// remove it soon.
5+
pub type P<T> = Box<T>;
326

337
/// Construct a `P<T>` from a `T` value.
348
#[allow(non_snake_case)]
35-
pub fn P<T: 'static>(value: T) -> P<T> {
36-
P { ptr: Box::new(value) }
37-
}
38-
39-
impl<T: 'static> P<T> {
40-
/// Move out of the pointer.
41-
/// Intended for chaining transformations not covered by `map`.
42-
pub fn and_then<U, F>(self, f: F) -> U
43-
where
44-
F: FnOnce(T) -> U,
45-
{
46-
f(*self.ptr)
47-
}
48-
49-
/// Equivalent to `and_then(|x| x)`.
50-
pub fn into_inner(self) -> T {
51-
*self.ptr
52-
}
53-
54-
/// Produce a new `P<T>` from `self` without reallocating.
55-
pub fn map<F>(mut self, f: F) -> P<T>
56-
where
57-
F: FnOnce(T) -> T,
58-
{
59-
let x = f(*self.ptr);
60-
*self.ptr = x;
61-
62-
self
63-
}
64-
65-
/// Optionally produce a new `P<T>` from `self` without reallocating.
66-
pub fn filter_map<F>(mut self, f: F) -> Option<P<T>>
67-
where
68-
F: FnOnce(T) -> Option<T>,
69-
{
70-
*self.ptr = f(*self.ptr)?;
71-
Some(self)
72-
}
73-
}
74-
75-
impl<T: ?Sized> Deref for P<T> {
76-
type Target = T;
77-
78-
fn deref(&self) -> &T {
79-
&self.ptr
80-
}
81-
}
82-
83-
impl<T: ?Sized> DerefMut for P<T> {
84-
fn deref_mut(&mut self) -> &mut T {
85-
&mut self.ptr
86-
}
87-
}
88-
89-
impl<T: 'static + Clone> Clone for P<T> {
90-
fn clone(&self) -> P<T> {
91-
P((**self).clone())
92-
}
93-
}
94-
95-
impl<T: ?Sized + Debug> Debug for P<T> {
96-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97-
Debug::fmt(&self.ptr, f)
98-
}
99-
}
100-
101-
impl<T: Display> Display for P<T> {
102-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103-
Display::fmt(&**self, f)
104-
}
105-
}
106-
107-
impl<T> fmt::Pointer for P<T> {
108-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109-
fmt::Pointer::fmt(&self.ptr, f)
110-
}
111-
}
112-
113-
impl<D: Decoder, T: 'static + Decodable<D>> Decodable<D> for P<T> {
114-
fn decode(d: &mut D) -> P<T> {
115-
P(Decodable::decode(d))
116-
}
117-
}
118-
119-
impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<T> {
120-
fn encode(&self, s: &mut S) {
121-
(**self).encode(s);
122-
}
123-
}
124-
125-
impl<T> P<[T]> {
126-
// FIXME(const-hack) make this const again
127-
pub fn new() -> P<[T]> {
128-
P { ptr: Box::default() }
129-
}
130-
131-
#[inline(never)]
132-
pub fn from_vec(v: Vec<T>) -> P<[T]> {
133-
P { ptr: v.into_boxed_slice() }
134-
}
135-
136-
#[inline(never)]
137-
pub fn into_vec(self) -> Vec<T> {
138-
self.ptr.into_vec()
139-
}
140-
}
141-
142-
impl<T> Default for P<[T]> {
143-
/// Creates an empty `P<[T]>`.
144-
fn default() -> P<[T]> {
145-
P::new()
146-
}
147-
}
148-
149-
impl<T: Clone> Clone for P<[T]> {
150-
fn clone(&self) -> P<[T]> {
151-
P::from_vec(self.to_vec())
152-
}
153-
}
154-
155-
impl<T> From<Vec<T>> for P<[T]> {
156-
fn from(v: Vec<T>) -> Self {
157-
P::from_vec(v)
158-
}
159-
}
160-
161-
impl<T> From<P<[T]>> for Vec<T> {
162-
fn from(val: P<[T]>) -> Self {
163-
val.into_vec()
164-
}
165-
}
166-
167-
impl<T> FromIterator<T> for P<[T]> {
168-
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> P<[T]> {
169-
P::from_vec(iter.into_iter().collect())
170-
}
171-
}
172-
173-
impl<T> IntoIterator for P<[T]> {
174-
type Item = T;
175-
type IntoIter = vec::IntoIter<T>;
176-
177-
fn into_iter(self) -> Self::IntoIter {
178-
self.into_vec().into_iter()
179-
}
180-
}
181-
182-
impl<'a, T> IntoIterator for &'a P<[T]> {
183-
type Item = &'a T;
184-
type IntoIter = slice::Iter<'a, T>;
185-
fn into_iter(self) -> Self::IntoIter {
186-
self.ptr.iter()
187-
}
188-
}
189-
190-
impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<[T]> {
191-
fn encode(&self, s: &mut S) {
192-
Encodable::encode(&**self, s);
193-
}
194-
}
195-
196-
impl<D: Decoder, T: Decodable<D>> Decodable<D> for P<[T]> {
197-
fn decode(d: &mut D) -> P<[T]> {
198-
P::from_vec(Decodable::decode(d))
199-
}
200-
}
201-
202-
impl<CTX, T> HashStable<CTX> for P<T>
203-
where
204-
T: ?Sized + HashStable<CTX>,
205-
{
206-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
207-
(**self).hash_stable(hcx, hasher);
208-
}
9+
pub fn P<T>(value: T) -> P<T> {
10+
Box::new(value)
20911
}

compiler/rustc_builtin_macros/src/deriving/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl MultiItemModifier for BuiltinDerive {
5757
let mut items = Vec::new();
5858
match item {
5959
Annotatable::Stmt(stmt) => {
60-
if let ast::StmtKind::Item(item) = stmt.into_inner().kind {
60+
if let ast::StmtKind::Item(item) = stmt.kind {
6161
(self.0)(
6262
ecx,
6363
span,

compiler/rustc_builtin_macros/src/pattern_type.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
3030

3131
let pat = pat_to_ty_pat(
3232
cx,
33-
parser
34-
.parse_pat_no_top_guard(
35-
None,
36-
RecoverComma::No,
37-
RecoverColon::No,
38-
CommaRecoveryMode::EitherTupleOrPipe,
39-
)?
40-
.into_inner(),
33+
*parser.parse_pat_no_top_guard(
34+
None,
35+
RecoverComma::No,
36+
RecoverColon::No,
37+
CommaRecoveryMode::EitherTupleOrPipe,
38+
)?,
4139
);
4240

4341
if parser.token != token::Eof {
@@ -58,9 +56,9 @@ fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> P<TyPat> {
5856
end.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
5957
include_end,
6058
),
61-
ast::PatKind::Or(variants) => TyPatKind::Or(
62-
variants.into_iter().map(|pat| pat_to_ty_pat(cx, pat.into_inner())).collect(),
63-
),
59+
ast::PatKind::Or(variants) => {
60+
TyPatKind::Or(variants.into_iter().map(|pat| pat_to_ty_pat(cx, *pat)).collect())
61+
}
6462
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
6563
_ => TyPatKind::Err(cx.dcx().span_err(pat.span, "pattern not supported in pattern types")),
6664
};

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -354,30 +354,28 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
354354
})
355355
.collect();
356356

357-
let decls_static = cx
358-
.item_static(
357+
let mut decls_static = cx.item_static(
358+
span,
359+
Ident::new(sym::_DECLS, span),
360+
cx.ty_ref(
359361
span,
360-
Ident::new(sym::_DECLS, span),
361-
cx.ty_ref(
362+
cx.ty(
362363
span,
363-
cx.ty(
364-
span,
365-
ast::TyKind::Slice(
366-
cx.ty_path(cx.path(span, vec![proc_macro, bridge, client, proc_macro_ty])),
367-
),
364+
ast::TyKind::Slice(
365+
cx.ty_path(cx.path(span, vec![proc_macro, bridge, client, proc_macro_ty])),
368366
),
369-
None,
370-
ast::Mutability::Not,
371367
),
368+
None,
372369
ast::Mutability::Not,
373-
cx.expr_array_ref(span, decls),
374-
)
375-
.map(|mut i| {
376-
i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span));
377-
i.attrs.push(cx.attr_word(sym::used, span));
378-
i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span));
379-
i
380-
});
370+
),
371+
ast::Mutability::Not,
372+
cx.expr_array_ref(span, decls),
373+
);
374+
decls_static.attrs.extend([
375+
cx.attr_word(sym::rustc_proc_macro_decls, span),
376+
cx.attr_word(sym::used, span),
377+
cx.attr_nested_word(sym::allow, sym::deprecated, span),
378+
]);
381379

382380
let block = cx.expr_block(
383381
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) fn expand_test_case(
4040
let (mut item, is_stmt) = match anno_item {
4141
Annotatable::Item(item) => (item, false),
4242
Annotatable::Stmt(stmt) if let ast::StmtKind::Item(_) = stmt.kind => {
43-
if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
43+
if let ast::StmtKind::Item(i) = stmt.kind {
4444
(i, true)
4545
} else {
4646
unreachable!()
@@ -120,11 +120,7 @@ pub(crate) fn expand_test_or_bench(
120120
Annotatable::Item(i) => (i, false),
121121
Annotatable::Stmt(stmt) if matches!(stmt.kind, ast::StmtKind::Item(_)) => {
122122
// FIXME: Use an 'if let' guard once they are implemented
123-
if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
124-
(i, true)
125-
} else {
126-
unreachable!()
127-
}
123+
if let ast::StmtKind::Item(i) = stmt.kind { (i, true) } else { unreachable!() }
128124
}
129125
other => {
130126
not_testable_error(cx, attr_sp, None);
@@ -381,10 +377,7 @@ pub(crate) fn expand_test_or_bench(
381377
.into(),
382378
),
383379
);
384-
test_const = test_const.map(|mut tc| {
385-
tc.vis.kind = ast::VisibilityKind::Public;
386-
tc
387-
});
380+
test_const.vis.kind = ast::VisibilityKind::Public;
388381

389382
// extern crate test
390383
let test_extern =

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl Annotatable {
167167

168168
pub fn expect_stmt(self) -> ast::Stmt {
169169
match self {
170-
Annotatable::Stmt(stmt) => stmt.into_inner(),
170+
Annotatable::Stmt(stmt) => *stmt,
171171
_ => panic!("expected statement"),
172172
}
173173
}

0 commit comments

Comments
 (0)