Skip to content

Commit f9fdf40

Browse files
authored
Rollup merge of #140878 - nnethercote:two-expand-cleanups, r=compiler-errors
Two expand-related cleanups Minor improvements I found while looking at this code. Best reviewed one commit at a time. r? `@BoxyUwU`
2 parents 53f88f9 + 5ebcbfc commit f9fdf40

File tree

5 files changed

+45
-71
lines changed

5 files changed

+45
-71
lines changed

compiler/rustc_ast/src/ast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ impl ParenthesizedArgs {
308308
}
309309
}
310310

311-
use crate::AstDeref;
312311
pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};
313312

314313
/// Modifiers on a trait bound like `~const`, `?` and `!`.
@@ -2349,7 +2348,7 @@ impl Ty {
23492348
pub fn is_maybe_parenthesised_infer(&self) -> bool {
23502349
match &self.kind {
23512350
TyKind::Infer => true,
2352-
TyKind::Paren(inner) => inner.ast_deref().is_maybe_parenthesised_infer(),
2351+
TyKind::Paren(inner) => inner.is_maybe_parenthesised_infer(),
23532352
_ => false,
23542353
}
23552354
}

compiler/rustc_ast/src/ast_traits.rs

+28-47
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,6 @@ use crate::{
1313
Ty, Variant, Visibility, WherePredicate,
1414
};
1515

16-
/// A utility trait to reduce boilerplate.
17-
/// Standard `Deref(Mut)` cannot be reused due to coherence.
18-
pub trait AstDeref {
19-
type Target;
20-
fn ast_deref(&self) -> &Self::Target;
21-
fn ast_deref_mut(&mut self) -> &mut Self::Target;
22-
}
23-
24-
macro_rules! impl_not_ast_deref {
25-
($($T:ty),+ $(,)?) => {
26-
$(
27-
impl !AstDeref for $T {}
28-
)+
29-
};
30-
}
31-
32-
impl_not_ast_deref!(AssocItem, Expr, ForeignItem, Item, Stmt);
33-
34-
impl<T> AstDeref for P<T> {
35-
type Target = T;
36-
fn ast_deref(&self) -> &Self::Target {
37-
self
38-
}
39-
fn ast_deref_mut(&mut self) -> &mut Self::Target {
40-
self
41-
}
42-
}
43-
4416
/// A trait for AST nodes having an ID.
4517
pub trait HasNodeId {
4618
fn node_id(&self) -> NodeId;
@@ -81,12 +53,12 @@ impl_has_node_id!(
8153
WherePredicate,
8254
);
8355

84-
impl<T: AstDeref<Target: HasNodeId>> HasNodeId for T {
56+
impl<T: HasNodeId> HasNodeId for P<T> {
8557
fn node_id(&self) -> NodeId {
86-
self.ast_deref().node_id()
58+
(**self).node_id()
8759
}
8860
fn node_id_mut(&mut self) -> &mut NodeId {
89-
self.ast_deref_mut().node_id_mut()
61+
(**self).node_id_mut()
9062
}
9163
}
9264

@@ -138,21 +110,21 @@ impl_has_tokens_none!(
138110
WherePredicate
139111
);
140112

141-
impl<T: AstDeref<Target: HasTokens>> HasTokens for T {
113+
impl<T: HasTokens> HasTokens for Option<T> {
142114
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
143-
self.ast_deref().tokens()
115+
self.as_ref().and_then(|inner| inner.tokens())
144116
}
145117
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
146-
self.ast_deref_mut().tokens_mut()
118+
self.as_mut().and_then(|inner| inner.tokens_mut())
147119
}
148120
}
149121

150-
impl<T: HasTokens> HasTokens for Option<T> {
122+
impl<T: HasTokens> HasTokens for P<T> {
151123
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
152-
self.as_ref().and_then(|inner| inner.tokens())
124+
(**self).tokens()
153125
}
154126
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
155-
self.as_mut().and_then(|inner| inner.tokens_mut())
127+
(**self).tokens_mut()
156128
}
157129
}
158130

@@ -273,13 +245,13 @@ impl_has_attrs!(
273245
);
274246
impl_has_attrs_none!(Attribute, AttrItem, Block, Pat, Path, Ty, Visibility);
275247

276-
impl<T: AstDeref<Target: HasAttrs>> HasAttrs for T {
277-
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = T::Target::SUPPORTS_CUSTOM_INNER_ATTRS;
248+
impl<T: HasAttrs> HasAttrs for P<T> {
249+
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = T::SUPPORTS_CUSTOM_INNER_ATTRS;
278250
fn attrs(&self) -> &[Attribute] {
279-
self.ast_deref().attrs()
251+
(**self).attrs()
280252
}
281253
fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
282-
self.ast_deref_mut().visit_attrs(f)
254+
(**self).visit_attrs(f);
283255
}
284256
}
285257

@@ -343,13 +315,22 @@ impl<Wrapped, Tag> AstNodeWrapper<Wrapped, Tag> {
343315
}
344316
}
345317

346-
impl<Wrapped, Tag> AstDeref for AstNodeWrapper<Wrapped, Tag> {
347-
type Target = Wrapped;
348-
fn ast_deref(&self) -> &Self::Target {
349-
&self.wrapped
318+
impl<Wrapped: HasNodeId, Tag> HasNodeId for AstNodeWrapper<Wrapped, Tag> {
319+
fn node_id(&self) -> NodeId {
320+
self.wrapped.node_id()
321+
}
322+
fn node_id_mut(&mut self) -> &mut NodeId {
323+
self.wrapped.node_id_mut()
324+
}
325+
}
326+
327+
impl<Wrapped: HasAttrs, Tag> HasAttrs for AstNodeWrapper<Wrapped, Tag> {
328+
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = Wrapped::SUPPORTS_CUSTOM_INNER_ATTRS;
329+
fn attrs(&self) -> &[Attribute] {
330+
self.wrapped.attrs()
350331
}
351-
fn ast_deref_mut(&mut self) -> &mut Self::Target {
352-
&mut self.wrapped
332+
fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
333+
self.wrapped.visit_attrs(f);
353334
}
354335
}
355336

compiler/rustc_ast/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub mod tokenstream;
4646
pub mod visit;
4747

4848
pub use self::ast::*;
49-
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
49+
pub use self::ast_traits::{AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
5050

5151
/// Requirements for a `StableHashingContext` to be used in this crate.
5252
/// This is a hack to allow using the `HashStable_Generic` derive macro

compiler/rustc_builtin_macros/src/env.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::env::VarError;
88

99
use rustc_ast::token::{self, LitKind};
1010
use rustc_ast::tokenstream::TokenStream;
11-
use rustc_ast::{AstDeref, ExprKind, GenericArg, Mutability};
11+
use rustc_ast::{ExprKind, GenericArg, Mutability};
1212
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
1313
use rustc_span::{Ident, Span, Symbol, kw, sym};
1414
use thin_vec::thin_vec;
@@ -148,13 +148,13 @@ pub(crate) fn expand_env<'cx>(
148148
cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVar {
149149
span,
150150
var: *symbol,
151-
var_expr: var_expr.ast_deref(),
151+
var_expr: &var_expr,
152152
})
153153
} else {
154154
cx.dcx().emit_err(errors::EnvNotDefined::CustomEnvVar {
155155
span,
156156
var: *symbol,
157-
var_expr: var_expr.ast_deref(),
157+
var_expr: &var_expr,
158158
})
159159
}
160160
}

compiler/rustc_expand/src/expand.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::ops::Deref;
21
use std::path::PathBuf;
32
use std::rc::Rc;
43
use std::sync::Arc;
@@ -1117,7 +1116,6 @@ enum AddSemicolon {
11171116
/// of functionality used by `InvocationCollector`.
11181117
trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
11191118
type OutputTy = SmallVec<[Self; 1]>;
1120-
type AttrsTy: Deref<Target = [ast::Attribute]> = ast::AttrVec;
11211119
type ItemKind = ItemKind;
11221120
const KIND: AstFragmentKind;
11231121
fn to_annotatable(self) -> Annotatable;
@@ -1134,7 +1132,7 @@ trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
11341132
fn is_mac_call(&self) -> bool {
11351133
false
11361134
}
1137-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1135+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
11381136
unreachable!()
11391137
}
11401138
fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
@@ -1189,7 +1187,7 @@ impl InvocationCollectorNode for P<ast::Item> {
11891187
fn is_mac_call(&self) -> bool {
11901188
matches!(self.kind, ItemKind::MacCall(..))
11911189
}
1192-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1190+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
11931191
let node = self.into_inner();
11941192
match node.kind {
11951193
ItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
@@ -1345,7 +1343,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
13451343
fn is_mac_call(&self) -> bool {
13461344
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
13471345
}
1348-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1346+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
13491347
let item = self.wrapped.into_inner();
13501348
match item.kind {
13511349
AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
@@ -1386,7 +1384,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
13861384
fn is_mac_call(&self) -> bool {
13871385
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
13881386
}
1389-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1387+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
13901388
let item = self.wrapped.into_inner();
13911389
match item.kind {
13921390
AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
@@ -1427,7 +1425,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitImplItem
14271425
fn is_mac_call(&self) -> bool {
14281426
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
14291427
}
1430-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1428+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
14311429
let item = self.wrapped.into_inner();
14321430
match item.kind {
14331431
AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No),
@@ -1465,7 +1463,7 @@ impl InvocationCollectorNode for P<ast::ForeignItem> {
14651463
fn is_mac_call(&self) -> bool {
14661464
matches!(self.kind, ForeignItemKind::MacCall(..))
14671465
}
1468-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1466+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
14691467
let node = self.into_inner();
14701468
match node.kind {
14711469
ForeignItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
@@ -1579,7 +1577,6 @@ impl InvocationCollectorNode for ast::Arm {
15791577
}
15801578

15811579
impl InvocationCollectorNode for ast::Stmt {
1582-
type AttrsTy = ast::AttrVec;
15831580
const KIND: AstFragmentKind = AstFragmentKind::Stmts;
15841581
fn to_annotatable(self) -> Annotatable {
15851582
Annotatable::Stmt(P(self))
@@ -1599,7 +1596,7 @@ impl InvocationCollectorNode for ast::Stmt {
15991596
StmtKind::Let(..) | StmtKind::Empty => false,
16001597
}
16011598
}
1602-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1599+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
16031600
// We pull macro invocations (both attributes and fn-like macro calls) out of their
16041601
// `StmtKind`s and treat them as statement macro invocations, not as items or expressions.
16051602
let (add_semicolon, mac, attrs) = match self.kind {
@@ -1693,7 +1690,7 @@ impl InvocationCollectorNode for P<ast::Ty> {
16931690
fn is_mac_call(&self) -> bool {
16941691
matches!(self.kind, ast::TyKind::MacCall(..))
16951692
}
1696-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1693+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
16971694
let node = self.into_inner();
16981695
match node.kind {
16991696
TyKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
@@ -1717,7 +1714,7 @@ impl InvocationCollectorNode for P<ast::Pat> {
17171714
fn is_mac_call(&self) -> bool {
17181715
matches!(self.kind, PatKind::MacCall(..))
17191716
}
1720-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1717+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
17211718
let node = self.into_inner();
17221719
match node.kind {
17231720
PatKind::MacCall(mac) => (mac, AttrVec::new(), AddSemicolon::No),
@@ -1728,7 +1725,6 @@ impl InvocationCollectorNode for P<ast::Pat> {
17281725

17291726
impl InvocationCollectorNode for P<ast::Expr> {
17301727
type OutputTy = P<ast::Expr>;
1731-
type AttrsTy = ast::AttrVec;
17321728
const KIND: AstFragmentKind = AstFragmentKind::Expr;
17331729
fn to_annotatable(self) -> Annotatable {
17341730
Annotatable::Expr(self)
@@ -1745,7 +1741,7 @@ impl InvocationCollectorNode for P<ast::Expr> {
17451741
fn is_mac_call(&self) -> bool {
17461742
matches!(self.kind, ExprKind::MacCall(..))
17471743
}
1748-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1744+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
17491745
let node = self.into_inner();
17501746
match node.kind {
17511747
ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
@@ -1757,7 +1753,6 @@ impl InvocationCollectorNode for P<ast::Expr> {
17571753
struct OptExprTag;
17581754
impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
17591755
type OutputTy = Option<P<ast::Expr>>;
1760-
type AttrsTy = ast::AttrVec;
17611756
const KIND: AstFragmentKind = AstFragmentKind::OptExpr;
17621757
fn to_annotatable(self) -> Annotatable {
17631758
Annotatable::Expr(self.wrapped)
@@ -1772,7 +1767,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
17721767
fn is_mac_call(&self) -> bool {
17731768
matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
17741769
}
1775-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1770+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
17761771
let node = self.wrapped.into_inner();
17771772
match node.kind {
17781773
ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),
@@ -1794,7 +1789,6 @@ impl DummyAstNode for MethodReceiverTag {
17941789
}
17951790
impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag> {
17961791
type OutputTy = Self;
1797-
type AttrsTy = ast::AttrVec;
17981792
const KIND: AstFragmentKind = AstFragmentKind::MethodReceiverExpr;
17991793
fn descr() -> &'static str {
18001794
"an expression"
@@ -1811,7 +1805,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, MethodReceiverTag>
18111805
fn is_mac_call(&self) -> bool {
18121806
matches!(self.wrapped.kind, ast::ExprKind::MacCall(..))
18131807
}
1814-
fn take_mac_call(self) -> (P<ast::MacCall>, Self::AttrsTy, AddSemicolon) {
1808+
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
18151809
let node = self.wrapped.into_inner();
18161810
match node.kind {
18171811
ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No),

0 commit comments

Comments
 (0)