Skip to content

Commit d365e9c

Browse files
committed
delegation: Implement glob delegation
1 parent 904d77f commit d365e9c

35 files changed

+999
-137
lines changed

compiler/rustc_ast/src/ast.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -2951,6 +2951,7 @@ impl Item {
29512951
| ItemKind::MacCall(_)
29522952
| ItemKind::Delegation(_)
29532953
| ItemKind::DelegationList(_)
2954+
| ItemKind::DelegationGlob(_)
29542955
| ItemKind::MacroDef(_) => None,
29552956
ItemKind::Static(_) => None,
29562957
ItemKind::Const(i) => Some(&i.generics),
@@ -3108,20 +3109,14 @@ pub struct Fn {
31083109
pub body: Option<P<Block>>,
31093110
}
31103111

3111-
#[derive(Clone, Encodable, Decodable, Debug)]
3112-
pub enum DelegationKind {
3113-
Single,
3114-
List(ThinVec<Ident>),
3115-
}
3116-
31173112
#[derive(Clone, Encodable, Decodable, Debug)]
31183113
pub struct Delegation {
31193114
/// Path resolution id.
31203115
pub id: NodeId,
31213116
pub qself: Option<P<QSelf>>,
31223117
pub path: Path,
3123-
pub kind: DelegationKind,
31243118
pub body: Option<P<Block>>,
3119+
pub from_glob: bool,
31253120
}
31263121

31273122
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3132,6 +3127,13 @@ pub struct DelegationList {
31323127
pub body: Option<P<Block>>,
31333128
}
31343129

3130+
#[derive(Clone, Encodable, Decodable, Debug)]
3131+
pub struct DelegationGlob {
3132+
pub qself: Option<P<QSelf>>,
3133+
pub prefix: Path,
3134+
pub body: Option<P<Block>>,
3135+
}
3136+
31353137
#[derive(Clone, Encodable, Decodable, Debug)]
31363138
pub struct StaticItem {
31373139
pub ty: P<Ty>,
@@ -3225,6 +3227,9 @@ pub enum ItemKind {
32253227
/// A list delegation item (`reuse prefix::{a, b, c}`).
32263228
/// Treated similarly to a macro call and expanded early.
32273229
DelegationList(Box<DelegationList>),
3230+
/// A glob delegation item (`reuse prefix::*`).
3231+
/// Treated similarly to a macro call and expanded early.
3232+
DelegationGlob(Box<DelegationGlob>),
32283233
}
32293234

32303235
impl ItemKind {
@@ -3233,7 +3238,7 @@ impl ItemKind {
32333238
match self {
32343239
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
32353240
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
3236-
| Delegation(..) | DelegationList(..) => "a",
3241+
| Delegation(..) | DelegationList(..) | DelegationGlob(..) => "a",
32373242
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
32383243
}
32393244
}
@@ -3259,6 +3264,7 @@ impl ItemKind {
32593264
ItemKind::Impl { .. } => "implementation",
32603265
ItemKind::Delegation(..) => "delegated function",
32613266
ItemKind::DelegationList(..) => "delegation list",
3267+
ItemKind::DelegationGlob(..) => "delegation glob",
32623268
}
32633269
}
32643270

@@ -3304,6 +3310,8 @@ pub enum AssocItemKind {
33043310
Delegation(Box<Delegation>),
33053311
/// An associated delegation item list.
33063312
DelegationList(Box<DelegationList>),
3313+
/// An associated delegation item glob.
3314+
DelegationGlob(Box<DelegationGlob>),
33073315
}
33083316

33093317
impl AssocItemKind {
@@ -3312,9 +3320,10 @@ impl AssocItemKind {
33123320
Self::Const(box ConstItem { defaultness, .. })
33133321
| Self::Fn(box Fn { defaultness, .. })
33143322
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
3315-
Self::MacCall(..) | Self::Delegation(..) | Self::DelegationList(..) => {
3316-
Defaultness::Final
3317-
}
3323+
Self::MacCall(..)
3324+
| Self::Delegation(..)
3325+
| Self::DelegationList(..)
3326+
| Self::DelegationGlob(..) => Defaultness::Final,
33183327
}
33193328
}
33203329
}
@@ -3328,6 +3337,7 @@ impl From<AssocItemKind> for ItemKind {
33283337
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
33293338
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
33303339
AssocItemKind::DelegationList(delegation) => ItemKind::DelegationList(delegation),
3340+
AssocItemKind::DelegationGlob(delegation) => ItemKind::DelegationGlob(delegation),
33313341
}
33323342
}
33333343
}
@@ -3343,6 +3353,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
33433353
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
33443354
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
33453355
ItemKind::DelegationList(d) => AssocItemKind::DelegationList(d),
3356+
ItemKind::DelegationGlob(d) => AssocItemKind::DelegationGlob(d),
33463357
_ => return Err(item_kind),
33473358
})
33483359
}

compiler/rustc_ast/src/mut_visit.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -1149,18 +1149,10 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
11491149
}
11501150
ItemKind::MacCall(m) => vis.visit_mac_call(m),
11511151
ItemKind::MacroDef(def) => vis.visit_macro_def(def),
1152-
ItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
1152+
ItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
11531153
vis.visit_id(id);
11541154
vis.visit_qself(qself);
11551155
vis.visit_path(path);
1156-
match kind {
1157-
DelegationKind::Single => {}
1158-
DelegationKind::List(suffixes) => {
1159-
for ident in suffixes {
1160-
vis.visit_ident(ident);
1161-
}
1162-
}
1163-
}
11641156
if let Some(body) = body {
11651157
vis.visit_block(body);
11661158
}
@@ -1175,6 +1167,13 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
11751167
vis.visit_block(body);
11761168
}
11771169
}
1170+
ItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
1171+
vis.visit_qself(qself);
1172+
vis.visit_path(prefix);
1173+
if let Some(body) = body {
1174+
vis.visit_block(body);
1175+
}
1176+
}
11781177
}
11791178
}
11801179

@@ -1213,18 +1212,10 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
12131212
visit_opt(ty, |ty| visitor.visit_ty(ty));
12141213
}
12151214
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
1216-
AssocItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
1215+
AssocItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
12171216
visitor.visit_id(id);
12181217
visitor.visit_qself(qself);
12191218
visitor.visit_path(path);
1220-
match kind {
1221-
DelegationKind::Single => {}
1222-
DelegationKind::List(suffixes) => {
1223-
for ident in suffixes {
1224-
visitor.visit_ident(ident);
1225-
}
1226-
}
1227-
}
12281219
if let Some(body) = body {
12291220
visitor.visit_block(body);
12301221
}
@@ -1240,6 +1231,14 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
12401231
visitor.visit_block(body);
12411232
}
12421233
}
1234+
AssocItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
1235+
visitor.visit_id(id);
1236+
visitor.visit_qself(qself);
1237+
visitor.visit_path(prefix);
1238+
if let Some(body) = body {
1239+
visitor.visit_block(body);
1240+
}
1241+
}
12431242
}
12441243
visitor.visit_span(span);
12451244
visit_lazy_tts(tokens, visitor);

compiler/rustc_ast/src/visit.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,11 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Resu
382382
}
383383
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
384384
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, item.id)),
385-
ItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
385+
ItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
386386
if let Some(qself) = qself {
387387
try_visit!(visitor.visit_ty(&qself.ty));
388388
}
389389
try_visit!(visitor.visit_path(path, *id));
390-
match kind {
391-
DelegationKind::Single => {}
392-
DelegationKind::List(suffixes) => {
393-
for ident in suffixes {
394-
visitor.visit_ident(*ident);
395-
}
396-
}
397-
}
398390
visit_opt!(visitor, visit_block, body);
399391
}
400392
ItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
@@ -407,6 +399,13 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Resu
407399
}
408400
visit_opt!(visitor, visit_block, body);
409401
}
402+
ItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
403+
if let Some(qself) = qself {
404+
try_visit!(visitor.visit_ty(&qself.ty));
405+
}
406+
try_visit!(visitor.visit_path(prefix, item.id));
407+
visit_opt!(visitor, visit_block, body);
408+
}
410409
}
411410
walk_list!(visitor, visit_attribute, &item.attrs);
412411
V::Result::output()
@@ -800,19 +799,11 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
800799
AssocItemKind::MacCall(mac) => {
801800
try_visit!(visitor.visit_mac_call(mac));
802801
}
803-
AssocItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
802+
AssocItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
804803
if let Some(qself) = qself {
805804
try_visit!(visitor.visit_ty(&qself.ty));
806805
}
807806
try_visit!(visitor.visit_path(path, *id));
808-
match kind {
809-
DelegationKind::Single => {}
810-
DelegationKind::List(suffixes) => {
811-
for ident in suffixes {
812-
visitor.visit_ident(*ident);
813-
}
814-
}
815-
}
816807
visit_opt!(visitor, visit_block, body);
817808
}
818809
AssocItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
@@ -825,6 +816,13 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
825816
}
826817
visit_opt!(visitor, visit_block, body);
827818
}
819+
AssocItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
820+
if let Some(qself) = qself {
821+
try_visit!(visitor.visit_ty(&qself.ty));
822+
}
823+
try_visit!(visitor.visit_path(prefix, item.id));
824+
visit_opt!(visitor, visit_block, body);
825+
}
828826
}
829827
V::Result::output()
830828
}

compiler/rustc_ast_lowering/src/item.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
459459
delegation_results.body_id,
460460
)
461461
}
462-
ItemKind::MacCall(..) | ItemKind::DelegationList(..) => {
462+
ItemKind::MacCall(..) | ItemKind::DelegationList(..) | ItemKind::DelegationGlob(..) => {
463463
panic!("macros should have been expanded by now")
464464
}
465465
}
@@ -844,7 +844,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
844844
);
845845
(delegation_results.generics, item_kind, true)
846846
}
847-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => {
847+
AssocItemKind::MacCall(..)
848+
| AssocItemKind::DelegationList(..)
849+
| AssocItemKind::DelegationGlob(..) => {
848850
panic!("macro item shouldn't exist at this point")
849851
}
850852
};
@@ -870,7 +872,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
870872
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
871873
has_self: self.delegation_has_self(i.id, delegation.id, i.span),
872874
},
873-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => unreachable!(),
875+
AssocItemKind::MacCall(..)
876+
| AssocItemKind::DelegationList(..)
877+
| AssocItemKind::DelegationGlob(..) => unreachable!(),
874878
};
875879
let id = hir::TraitItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } };
876880
hir::TraitItemRef {
@@ -965,7 +969,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
965969
hir::ImplItemKind::Fn(delegation_results.sig, delegation_results.body_id),
966970
)
967971
}
968-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => {
972+
AssocItemKind::MacCall(..)
973+
| AssocItemKind::DelegationList(..)
974+
| AssocItemKind::DelegationGlob(..) => {
969975
panic!("macros should have been expanded by now")
970976
}
971977
};
@@ -996,7 +1002,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
9961002
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
9971003
has_self: self.delegation_has_self(i.id, delegation.id, i.span),
9981004
},
999-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => unreachable!(),
1005+
AssocItemKind::MacCall(..)
1006+
| AssocItemKind::DelegationList(..)
1007+
| AssocItemKind::DelegationGlob(..) => unreachable!(),
10001008
},
10011009
trait_item_def_id: self
10021010
.resolver

0 commit comments

Comments
 (0)