Skip to content

Commit 6204a46

Browse files
committed
delegation: impl step 1
1 parent 529047c commit 6204a46

File tree

34 files changed

+1156
-28
lines changed

34 files changed

+1156
-28
lines changed

compiler/rustc_ast/src/ast.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -2978,6 +2978,14 @@ pub struct Fn {
29782978
pub body: Option<P<Block>>,
29792979
}
29802980

2981+
#[derive(Clone, Encodable, Decodable, Debug)]
2982+
pub struct Delegation {
2983+
pub id: NodeId,
2984+
pub path: (Option<P<QSelf>>, Path),
2985+
pub target_expr: Option<P<Expr>>,
2986+
pub span: Span,
2987+
}
2988+
29812989
#[derive(Clone, Encodable, Decodable, Debug)]
29822990
pub struct StaticItem {
29832991
pub ty: P<Ty>,
@@ -3063,14 +3071,20 @@ pub enum ItemKind {
30633071

30643072
/// A macro definition.
30653073
MacroDef(MacroDef),
3074+
3075+
/// A delegation item (`reuse`).
3076+
///
3077+
/// E.g. reuse <Type as Trait>::name { target_expr_template }
3078+
Delegation(Box<Delegation>),
30663079
}
30673080

30683081
impl ItemKind {
30693082
pub fn article(&self) -> &'static str {
30703083
use ItemKind::*;
30713084
match self {
30723085
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
3073-
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a",
3086+
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
3087+
| Delegation(..) => "a",
30743088
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
30753089
}
30763090
}
@@ -3094,6 +3108,7 @@ impl ItemKind {
30943108
ItemKind::MacCall(..) => "item macro invocation",
30953109
ItemKind::MacroDef(..) => "macro definition",
30963110
ItemKind::Impl { .. } => "implementation",
3111+
ItemKind::Delegation(..) => "delegation",
30973112
}
30983113
}
30993114

@@ -3135,6 +3150,8 @@ pub enum AssocItemKind {
31353150
Type(Box<TyAlias>),
31363151
/// A macro expanding to associated items.
31373152
MacCall(P<MacCall>),
3153+
/// An associated delegation item.
3154+
Delegation(Box<Delegation>),
31383155
}
31393156

31403157
impl AssocItemKind {
@@ -3143,7 +3160,7 @@ impl AssocItemKind {
31433160
Self::Const(box ConstItem { defaultness, .. })
31443161
| Self::Fn(box Fn { defaultness, .. })
31453162
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
3146-
Self::MacCall(..) => Defaultness::Final,
3163+
Self::MacCall(..) | Self::Delegation(..) => Defaultness::Final,
31473164
}
31483165
}
31493166
}
@@ -3155,6 +3172,7 @@ impl From<AssocItemKind> for ItemKind {
31553172
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
31563173
AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
31573174
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
3175+
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
31583176
}
31593177
}
31603178
}
@@ -3168,6 +3186,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
31683186
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
31693187
ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
31703188
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
3189+
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
31713190
_ => return Err(item_kind),
31723191
})
31733192
}

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)