Skip to content

Commit 7329f26

Browse files
committed
Auto merge of rust-lang#118947 - Bryanskiy:delegStep1, r=petrochenkov,lcnr
Delegation implementation: step 1 See rust-lang#118212 for more details. r? `@petrochenkov`
2 parents 174e73a + d69cd64 commit 7329f26

Some content is hidden

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

50 files changed

+1634
-93
lines changed

compiler/rustc_ast/src/ast.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,7 @@ impl Item {
28732873
| ItemKind::ForeignMod(_)
28742874
| ItemKind::GlobalAsm(_)
28752875
| ItemKind::MacCall(_)
2876+
| ItemKind::Delegation(_)
28762877
| ItemKind::MacroDef(_) => None,
28772878
ItemKind::Static(_) => None,
28782879
ItemKind::Const(i) => Some(&i.generics),
@@ -3019,6 +3020,15 @@ pub struct Fn {
30193020
pub body: Option<P<Block>>,
30203021
}
30213022

3023+
#[derive(Clone, Encodable, Decodable, Debug)]
3024+
pub struct Delegation {
3025+
/// Path resolution id.
3026+
pub id: NodeId,
3027+
pub qself: Option<P<QSelf>>,
3028+
pub path: Path,
3029+
pub body: Option<P<Block>>,
3030+
}
3031+
30223032
#[derive(Clone, Encodable, Decodable, Debug)]
30233033
pub struct StaticItem {
30243034
pub ty: P<Ty>,
@@ -3104,14 +3114,20 @@ pub enum ItemKind {
31043114

31053115
/// A macro definition.
31063116
MacroDef(MacroDef),
3117+
3118+
/// A delegation item (`reuse`).
3119+
///
3120+
/// E.g. `reuse <Type as Trait>::name { target_expr_template }`.
3121+
Delegation(Box<Delegation>),
31073122
}
31083123

31093124
impl ItemKind {
31103125
pub fn article(&self) -> &'static str {
31113126
use ItemKind::*;
31123127
match self {
31133128
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
3114-
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a",
3129+
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
3130+
| Delegation(..) => "a",
31153131
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
31163132
}
31173133
}
@@ -3135,6 +3151,7 @@ impl ItemKind {
31353151
ItemKind::MacCall(..) => "item macro invocation",
31363152
ItemKind::MacroDef(..) => "macro definition",
31373153
ItemKind::Impl { .. } => "implementation",
3154+
ItemKind::Delegation(..) => "delegated function",
31383155
}
31393156
}
31403157

@@ -3176,6 +3193,8 @@ pub enum AssocItemKind {
31763193
Type(Box<TyAlias>),
31773194
/// A macro expanding to associated items.
31783195
MacCall(P<MacCall>),
3196+
/// An associated delegation item.
3197+
Delegation(Box<Delegation>),
31793198
}
31803199

31813200
impl AssocItemKind {
@@ -3184,7 +3203,7 @@ impl AssocItemKind {
31843203
Self::Const(box ConstItem { defaultness, .. })
31853204
| Self::Fn(box Fn { defaultness, .. })
31863205
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
3187-
Self::MacCall(..) => Defaultness::Final,
3206+
Self::MacCall(..) | Self::Delegation(..) => Defaultness::Final,
31883207
}
31893208
}
31903209
}
@@ -3196,6 +3215,7 @@ impl From<AssocItemKind> for ItemKind {
31963215
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
31973216
AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
31983217
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
3218+
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
31993219
}
32003220
}
32013221
}
@@ -3209,6 +3229,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
32093229
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
32103230
ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
32113231
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
3232+
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
32123233
_ => return Err(item_kind),
32133234
})
32143235
}

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 { id, qself, path, body }) => {
1121+
vis.visit_id(id);
1122+
vis.visit_qself(qself);
1123+
vis.visit_path(path);
1124+
if let Some(body) = body {
1125+
vis.visit_block(body);
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 { id, qself, path, body }) => {
1167+
visitor.visit_id(id);
1168+
visitor.visit_qself(qself);
1169+
visitor.visit_path(path);
1170+
if let Some(body) = body {
1171+
visitor.visit_block(body);
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 { id: _, qself, path, body }) => {
379+
if let Some(qself) = qself {
380+
visitor.visit_ty(&qself.ty);
381+
}
382+
walk_path(visitor, path);
383+
if let Some(body) = body {
384+
visitor.visit_block(body);
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 { id: _, qself, path, body }) => {
717+
if let Some(qself) = qself {
718+
visitor.visit_ty(&qself.ty);
719+
}
720+
walk_path(visitor, path);
721+
if let Some(body) = body {
722+
visitor.visit_block(body);
723+
}
724+
}
707725
}
708726
}
709727

0 commit comments

Comments
 (0)