Skip to content

Commit 5b8817f

Browse files
committed
Delegation implementation: step 1
1 parent 37340a1 commit 5b8817f

File tree

42 files changed

+1233
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1233
-51
lines changed

compiler/rustc_ast/src/ast.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,7 @@ impl Item {
28542854
| ItemKind::ForeignMod(_)
28552855
| ItemKind::GlobalAsm(_)
28562856
| ItemKind::MacCall(_)
2857+
| ItemKind::Delegation(_)
28572858
| ItemKind::MacroDef(_) => None,
28582859
ItemKind::Static(_) => None,
28592860
ItemKind::Const(i) => Some(&i.generics),
@@ -3000,6 +3001,14 @@ pub struct Fn {
30003001
pub body: Option<P<Block>>,
30013002
}
30023003

3004+
#[derive(Clone, Encodable, Decodable, Debug)]
3005+
pub struct Delegation {
3006+
pub id: NodeId,
3007+
pub path: (Option<P<QSelf>>, Path),
3008+
pub target_expr: Option<P<Expr>>,
3009+
pub span: Span,
3010+
}
3011+
30033012
#[derive(Clone, Encodable, Decodable, Debug)]
30043013
pub struct StaticItem {
30053014
pub ty: P<Ty>,
@@ -3085,14 +3094,20 @@ pub enum ItemKind {
30853094

30863095
/// A macro definition.
30873096
MacroDef(MacroDef),
3097+
3098+
/// A delegation item (`reuse`).
3099+
///
3100+
/// E.g. `reuse <Type as Trait>::name { target_expr_template }`.
3101+
Delegation(Box<Delegation>),
30883102
}
30893103

30903104
impl ItemKind {
30913105
pub fn article(&self) -> &'static str {
30923106
use ItemKind::*;
30933107
match self {
30943108
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
3095-
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a",
3109+
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
3110+
| Delegation(..) => "a",
30963111
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
30973112
}
30983113
}
@@ -3116,6 +3131,7 @@ impl ItemKind {
31163131
ItemKind::MacCall(..) => "item macro invocation",
31173132
ItemKind::MacroDef(..) => "macro definition",
31183133
ItemKind::Impl { .. } => "implementation",
3134+
ItemKind::Delegation(..) => "delegation",
31193135
}
31203136
}
31213137

@@ -3157,6 +3173,8 @@ pub enum AssocItemKind {
31573173
Type(Box<TyAlias>),
31583174
/// A macro expanding to associated items.
31593175
MacCall(P<MacCall>),
3176+
/// An associated delegation item.
3177+
Delegation(Box<Delegation>),
31603178
}
31613179

31623180
impl AssocItemKind {
@@ -3165,7 +3183,7 @@ impl AssocItemKind {
31653183
Self::Const(box ConstItem { defaultness, .. })
31663184
| Self::Fn(box Fn { defaultness, .. })
31673185
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
3168-
Self::MacCall(..) => Defaultness::Final,
3186+
Self::MacCall(..) | Self::Delegation(..) => Defaultness::Final,
31693187
}
31703188
}
31713189
}
@@ -3177,6 +3195,7 @@ impl From<AssocItemKind> for ItemKind {
31773195
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
31783196
AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
31793197
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
3198+
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
31803199
}
31813200
}
31823201
}
@@ -3190,6 +3209,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
31903209
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
31913210
ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
31923211
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
3212+
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
31933213
_ => return Err(item_kind),
31943214
})
31953215
}

compiler/rustc_ast/src/ast_traits.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::ptr::P;
66
use crate::token::Nonterminal;
77
use crate::tokenstream::LazyAttrTokenStream;
8-
use crate::{Arm, Crate, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
8+
use crate::{Arm, Crate, Delegation, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
99
use crate::{AssocItem, Expr, ForeignItem, Item, NodeId};
1010
use crate::{AttrItem, AttrKind, Block, Pat, Path, Ty, Visibility};
1111
use crate::{AttrVec, Attribute, Stmt, StmtKind};
@@ -80,6 +80,7 @@ impl_has_node_id!(
8080
Stmt,
8181
Ty,
8282
Variant,
83+
Delegation
8384
);
8485

8586
impl<T: AstDeref<Target: HasNodeId>> HasNodeId for T {
@@ -108,7 +109,19 @@ macro_rules! impl_has_span {
108109
};
109110
}
110111

111-
impl_has_span!(AssocItem, Block, Expr, ForeignItem, Item, Pat, Path, Stmt, Ty, Visibility);
112+
impl_has_span!(
113+
AssocItem,
114+
Block,
115+
Expr,
116+
ForeignItem,
117+
Item,
118+
Pat,
119+
Path,
120+
Stmt,
121+
Ty,
122+
Visibility,
123+
Delegation
124+
);
112125

113126
impl<T: AstDeref<Target: HasSpan>> HasSpan for T {
114127
fn span(&self) -> Span {
@@ -159,7 +172,7 @@ macro_rules! impl_has_tokens_none {
159172
}
160173

161174
impl_has_tokens!(AssocItem, AttrItem, Block, Expr, ForeignItem, Item, Pat, Path, Ty, Visibility);
162-
impl_has_tokens_none!(Arm, ExprField, FieldDef, GenericParam, Param, PatField, Variant);
175+
impl_has_tokens_none!(Arm, ExprField, FieldDef, GenericParam, Param, PatField, Variant, Delegation);
163176

164177
impl<T: AstDeref<Target: HasTokens>> HasTokens for T {
165178
fn tokens(&self) -> Option<&LazyAttrTokenStream> {

compiler/rustc_ast/src/mut_visit.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,14 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
11171117
}
11181118
ItemKind::MacCall(m) => vis.visit_mac_call(m),
11191119
ItemKind::MacroDef(def) => vis.visit_macro_def(def),
1120+
ItemKind::Delegation(box delegation) => {
1121+
vis.visit_id(&mut delegation.id);
1122+
vis.visit_qself(&mut delegation.path.0);
1123+
vis.visit_path(&mut delegation.path.1);
1124+
if let Some(target_expr) = &mut delegation.target_expr {
1125+
vis.visit_expr(target_expr);
1126+
}
1127+
}
11201128
}
11211129
}
11221130

@@ -1155,6 +1163,14 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11551163
visit_opt(ty, |ty| visitor.visit_ty(ty));
11561164
}
11571165
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
1166+
AssocItemKind::Delegation(box delegation) => {
1167+
visitor.visit_id(&mut delegation.id);
1168+
visitor.visit_qself(&mut delegation.path.0);
1169+
visitor.visit_path(&mut delegation.path.1);
1170+
if let Some(target_expr) = &mut delegation.target_expr {
1171+
visitor.visit_expr(target_expr);
1172+
}
1173+
}
11581174
}
11591175
visitor.visit_span(span);
11601176
visit_lazy_tts(tokens, visitor);

compiler/rustc_ast/src/visit.rs

+18
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
375375
}
376376
ItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
377377
ItemKind::MacroDef(ts) => visitor.visit_mac_def(ts, item.id),
378+
ItemKind::Delegation(box delegation) => {
379+
if let Some(qself) = &delegation.path.0 {
380+
visitor.visit_ty(&qself.ty);
381+
}
382+
walk_path(visitor, &delegation.path.1);
383+
if let Some(target_expr) = &delegation.target_expr {
384+
visitor.visit_expr(target_expr);
385+
}
386+
}
378387
}
379388
walk_list!(visitor, visit_attribute, &item.attrs);
380389
}
@@ -704,6 +713,15 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
704713
AssocItemKind::MacCall(mac) => {
705714
visitor.visit_mac_call(mac);
706715
}
716+
AssocItemKind::Delegation(box delegation) => {
717+
if let Some(qself) = &delegation.path.0 {
718+
visitor.visit_ty(&qself.ty);
719+
}
720+
walk_path(visitor, &delegation.path.1);
721+
if let Some(target_expr) = &delegation.target_expr {
722+
visitor.visit_expr(target_expr);
723+
}
724+
}
707725
}
708726
}
709727

0 commit comments

Comments
 (0)