Skip to content

Commit 991c91f

Browse files
committed
Reduce P<T> to a typedef of Box<T>.
Keep the `P` constructor function for now, to minimize immediate churn. All the `into_inner` calls are removed, which is nice.
1 parent c42d1fc commit 991c91f

File tree

15 files changed

+48
-131
lines changed

15 files changed

+48
-131
lines changed

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: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +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};
21-
use std::ops::{Deref, DerefMut};
22-
23-
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
24-
25-
/// An owned smart pointer.
1+
/// A pointer type that uniquely owns a heap allocation of type T.
262
///
27-
/// See the [module level documentation][crate::ptr] for details.
28-
pub struct P<T: ?Sized> {
29-
ptr: Box<T>,
30-
}
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>;
316

327
/// Construct a `P<T>` from a `T` value.
338
#[allow(non_snake_case)]
349
pub fn P<T>(value: T) -> P<T> {
35-
P { ptr: Box::new(value) }
36-
}
37-
38-
impl<T> P<T> {
39-
/// Consume the `P` and return the wrapped value.
40-
pub fn into_inner(self) -> T {
41-
*self.ptr
42-
}
43-
}
44-
45-
impl<T: ?Sized> Deref for P<T> {
46-
type Target = T;
47-
48-
fn deref(&self) -> &T {
49-
&self.ptr
50-
}
51-
}
52-
53-
impl<T: ?Sized> DerefMut for P<T> {
54-
fn deref_mut(&mut self) -> &mut T {
55-
&mut self.ptr
56-
}
57-
}
58-
59-
impl<T: Clone> Clone for P<T> {
60-
fn clone(&self) -> P<T> {
61-
P((**self).clone())
62-
}
63-
}
64-
65-
impl<T: ?Sized + Debug> Debug for P<T> {
66-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67-
Debug::fmt(&self.ptr, f)
68-
}
69-
}
70-
71-
impl<D: Decoder, T: Decodable<D>> Decodable<D> for P<T> {
72-
fn decode(d: &mut D) -> P<T> {
73-
P(Decodable::decode(d))
74-
}
75-
}
76-
77-
impl<S: Encoder, T: Encodable<S>> Encodable<S> for P<T> {
78-
fn encode(&self, s: &mut S) {
79-
(**self).encode(s);
80-
}
10+
Box::new(value)
8111
}

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/test.rs

Lines changed: 2 additions & 6 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);

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
}

compiler/rustc_expand/src/expand.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,9 +1188,8 @@ impl InvocationCollectorNode for P<ast::Item> {
11881188
matches!(self.kind, ItemKind::MacCall(..))
11891189
}
11901190
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1191-
let node = self.into_inner();
1192-
match node.kind {
1193-
ItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1191+
match self.kind {
1192+
ItemKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
11941193
_ => unreachable!(),
11951194
}
11961195
}
@@ -1344,7 +1343,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
13441343
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
13451344
}
13461345
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1347-
let item = self.wrapped.into_inner();
1346+
let item = self.wrapped;
13481347
match item.kind {
13491348
AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
13501349
_ => unreachable!(),
@@ -1385,7 +1384,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
13851384
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
13861385
}
13871386
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1388-
let item = self.wrapped.into_inner();
1387+
let item = self.wrapped;
13891388
match item.kind {
13901389
AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
13911390
_ => unreachable!(),
@@ -1426,7 +1425,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitImplItem
14261425
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
14271426
}
14281427
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1429-
let item = self.wrapped.into_inner();
1428+
let item = self.wrapped;
14301429
match item.kind {
14311430
AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
14321431
_ => unreachable!(),
@@ -1464,9 +1463,8 @@ impl InvocationCollectorNode for P<ast::ForeignItem> {
14641463
matches!(self.kind, ForeignItemKind::MacCall(..))
14651464
}
14661465
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1467-
let node = self.into_inner();
1468-
match node.kind {
1469-
ForeignItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1466+
match self.kind {
1467+
ForeignItemKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
14701468
_ => unreachable!(),
14711469
}
14721470
}
@@ -1601,16 +1599,16 @@ impl InvocationCollectorNode for ast::Stmt {
16011599
// `StmtKind`s and treat them as statement macro invocations, not as items or expressions.
16021600
let (add_semicolon, mac, attrs) = match self.kind {
16031601
StmtKind::MacCall(mac) => {
1604-
let ast::MacCallStmt { mac, style, attrs, .. } = mac.into_inner();
1602+
let ast::MacCallStmt { mac, style, attrs, .. } = *mac;
16051603
(style == MacStmtStyle::Semicolon, mac, attrs)
16061604
}
1607-
StmtKind::Item(item) => match item.into_inner() {
1605+
StmtKind::Item(item) => match *item {
16081606
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
16091607
(mac.args.need_semicolon(), mac, attrs)
16101608
}
16111609
_ => unreachable!(),
16121610
},
1613-
StmtKind::Semi(expr) => match expr.into_inner() {
1611+
StmtKind::Semi(expr) => match *expr {
16141612
ast::Expr { kind: ExprKind::MacCall(mac), attrs, .. } => {
16151613
(mac.args.need_semicolon(), mac, attrs)
16161614
}
@@ -1691,8 +1689,7 @@ impl InvocationCollectorNode for P<ast::Ty> {
16911689
matches!(self.kind, ast::TyKind::MacCall(..))
16921690
}
16931691
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1694-
let node = self.into_inner();
1695-
match node.kind {
1692+
match self.kind {
16961693
TyKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
16971694
_ => unreachable!(),
16981695
}
@@ -1715,8 +1712,7 @@ impl InvocationCollectorNode for P<ast::Pat> {
17151712
matches!(self.kind, PatKind::MacCall(..))
17161713
}
17171714
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1718-
let node = self.into_inner();
1719-
match node.kind {
1715+
match self.kind {
17201716
PatKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
17211717
_ => unreachable!(),
17221718
}
@@ -1742,9 +1738,8 @@ impl InvocationCollectorNode for P<ast::Expr> {
17421738
matches!(self.kind, ExprKind::MacCall(..))
17431739
}
17441740
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1745-
let node = self.into_inner();
1746-
match node.kind {
1747-
ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
1741+
match self.kind {
1742+
ExprKind::MacCall(mac) => (mac, self.attrs, AddSemicolon::No),
17481743
_ => unreachable!(),
17491744
}
17501745
}
@@ -1768,7 +1763,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
17681763
matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
17691764
}
17701765
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1771-
let node = self.wrapped.into_inner();
1766+
let node = self.wrapped;
17721767
match node.kind {
17731768
ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
17741769
_ => unreachable!(),
@@ -1806,7 +1801,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag>
18061801
matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
18071802
}
18081803
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
1809-
let node = self.wrapped.into_inner();
1804+
let node = self.wrapped;
18101805
match node.kind {
18111806
ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
18121807
_ => unreachable!(),

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,9 +2273,9 @@ impl<'a> Parser<'a> {
22732273
),
22742274
// Also catches `fn foo(&a)`.
22752275
PatKind::Ref(ref inner_pat, mutab)
2276-
if matches!(inner_pat.clone().into_inner().kind, PatKind::Ident(..)) =>
2276+
if matches!(inner_pat.clone().kind, PatKind::Ident(..)) =>
22772277
{
2278-
match inner_pat.clone().into_inner().kind {
2278+
match inner_pat.clone().kind {
22792279
PatKind::Ident(_, ident, _) => {
22802280
let mutab = mutab.prefix_str();
22812281
(

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,10 +3450,8 @@ impl<'a> Parser<'a> {
34503450
// Detect and recover from `($pat if $cond) => $arm`.
34513451
// FIXME(guard_patterns): convert this to a normal guard instead
34523452
let span = pat.span;
3453-
let ast::PatKind::Paren(subpat) = pat.into_inner().kind else { unreachable!() };
3454-
let ast::PatKind::Guard(_, mut cond) = subpat.into_inner().kind else {
3455-
unreachable!()
3456-
};
3453+
let ast::PatKind::Paren(subpat) = pat.kind else { unreachable!() };
3454+
let ast::PatKind::Guard(_, mut cond) = subpat.kind else { unreachable!() };
34573455
self.psess.gated_spans.ungate_last(sym::guard_patterns, cond.span);
34583456
CondChecker::new(self, LetChainsPolicy::AlwaysAllowed).visit_expr(&mut cond);
34593457
let right = self.prev_token.span;

compiler/rustc_parse/src/parser/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a> Parser<'a> {
145145
{
146146
let mut item = item.expect("an actual item");
147147
attrs.prepend_to_nt_inner(&mut item.attrs);
148-
return Ok(Some(item.into_inner()));
148+
return Ok(Some(*item));
149149
}
150150

151151
self.collect_tokens(None, attrs, force_collect, |this, mut attrs| {
@@ -637,7 +637,7 @@ impl<'a> Parser<'a> {
637637
self.dcx().emit_err(errors::MissingForInTraitImpl { span: missing_for_span });
638638
}
639639

640-
let ty_first = ty_first.into_inner();
640+
let ty_first = *ty_first;
641641
let path = match ty_first.kind {
642642
// This notably includes paths passed through `ty` macro fragments (#46438).
643643
TyKind::Path(None, path) => path,

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ impl<'a> Parser<'a> {
10861086
if matches!(pat.kind, PatKind::Ident(BindingMode(ByRef::Yes(_), Mutability::Mut), ..)) {
10871087
self.psess.gated_spans.gate(sym::mut_ref, pat.span);
10881088
}
1089-
Ok(pat.into_inner().kind)
1089+
Ok(pat.kind)
10901090
}
10911091

10921092
/// Turn all by-value immutable bindings in a pattern into mutable bindings.

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'a> Parser<'a> {
404404
})?;
405405

406406
if ts.len() == 1 && matches!(trailing, Trailing::No) {
407-
let ty = ts.into_iter().next().unwrap().into_inner();
407+
let ty = ts.into_iter().next().unwrap();
408408
let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
409409
match ty.kind {
410410
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
@@ -420,7 +420,7 @@ impl<'a> Parser<'a> {
420420
self.parse_remaining_bounds(bounds, true)
421421
}
422422
// `(TYPE)`
423-
_ => Ok(TyKind::Paren(P(ty))),
423+
_ => Ok(TyKind::Paren(ty)),
424424
}
425425
} else {
426426
Ok(TyKind::Tup(ts))
@@ -1295,7 +1295,7 @@ impl<'a> Parser<'a> {
12951295
) -> PResult<'a, ()> {
12961296
let fn_path_segment = fn_path.segments.last_mut().unwrap();
12971297
let generic_args = if let Some(p_args) = &fn_path_segment.args {
1298-
p_args.clone().into_inner()
1298+
*p_args.clone()
12991299
} else {
13001300
// Normally it wouldn't come here because the upstream should have parsed
13011301
// generic parameters (otherwise it's impossible to call this function).

src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fn drain_matching(
426426
// Check if we should extract, but only if `idx >= start`.
427427
if idx > start && predicate(&alternatives[i].kind) {
428428
let pat = alternatives.remove(i);
429-
tail_or.push(extract(pat.into_inner().kind));
429+
tail_or.push(extract(pat.kind));
430430
} else {
431431
i += 1;
432432
}

src/tools/rustfmt/src/modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
174174
) -> Result<(), ModuleResolutionError> {
175175
for item in items {
176176
if is_cfg_if(&item) {
177-
self.visit_cfg_if(Cow::Owned(item.into_inner()))?;
177+
self.visit_cfg_if(Cow::Owned(*item))?;
178178
continue;
179179
}
180180

src/tools/rustfmt/src/parse/macros/cfg_if.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn parse_cfg_if_inner<'a>(
6262

6363
while parser.token != TokenKind::CloseBrace && parser.token.kind != TokenKind::Eof {
6464
let item = match parser.parse_item(ForceCollect::No) {
65-
Ok(Some(item_ptr)) => item_ptr.into_inner(),
65+
Ok(Some(item_ptr)) => *item_ptr,
6666
Ok(None) => continue,
6767
Err(err) => {
6868
err.cancel();

0 commit comments

Comments
 (0)