Skip to content

Commit

Permalink
delegation: Implement glob delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Apr 19, 2024
1 parent 904d77f commit d365e9c
Show file tree
Hide file tree
Showing 35 changed files with 999 additions and 137 deletions.
33 changes: 22 additions & 11 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2951,6 +2951,7 @@ impl Item {
| ItemKind::MacCall(_)
| ItemKind::Delegation(_)
| ItemKind::DelegationList(_)
| ItemKind::DelegationGlob(_)
| ItemKind::MacroDef(_) => None,
ItemKind::Static(_) => None,
ItemKind::Const(i) => Some(&i.generics),
Expand Down Expand Up @@ -3108,20 +3109,14 @@ pub struct Fn {
pub body: Option<P<Block>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub enum DelegationKind {
Single,
List(ThinVec<Ident>),
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Delegation {
/// Path resolution id.
pub id: NodeId,
pub qself: Option<P<QSelf>>,
pub path: Path,
pub kind: DelegationKind,
pub body: Option<P<Block>>,
pub from_glob: bool,
}

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

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct DelegationGlob {
pub qself: Option<P<QSelf>>,
pub prefix: Path,
pub body: Option<P<Block>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StaticItem {
pub ty: P<Ty>,
Expand Down Expand Up @@ -3225,6 +3227,9 @@ pub enum ItemKind {
/// A list delegation item (`reuse prefix::{a, b, c}`).
/// Treated similarly to a macro call and expanded early.
DelegationList(Box<DelegationList>),
/// A glob delegation item (`reuse prefix::*`).
/// Treated similarly to a macro call and expanded early.
DelegationGlob(Box<DelegationGlob>),
}

impl ItemKind {
Expand All @@ -3233,7 +3238,7 @@ impl ItemKind {
match self {
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
| Delegation(..) | DelegationList(..) => "a",
| Delegation(..) | DelegationList(..) | DelegationGlob(..) => "a",
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
}
}
Expand All @@ -3259,6 +3264,7 @@ impl ItemKind {
ItemKind::Impl { .. } => "implementation",
ItemKind::Delegation(..) => "delegated function",
ItemKind::DelegationList(..) => "delegation list",
ItemKind::DelegationGlob(..) => "delegation glob",
}
}

Expand Down Expand Up @@ -3304,6 +3310,8 @@ pub enum AssocItemKind {
Delegation(Box<Delegation>),
/// An associated delegation item list.
DelegationList(Box<DelegationList>),
/// An associated delegation item glob.
DelegationGlob(Box<DelegationGlob>),
}

impl AssocItemKind {
Expand All @@ -3312,9 +3320,10 @@ impl AssocItemKind {
Self::Const(box ConstItem { defaultness, .. })
| Self::Fn(box Fn { defaultness, .. })
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..) | Self::Delegation(..) | Self::DelegationList(..) => {
Defaultness::Final
}
Self::MacCall(..)
| Self::Delegation(..)
| Self::DelegationList(..)
| Self::DelegationGlob(..) => Defaultness::Final,
}
}
}
Expand All @@ -3328,6 +3337,7 @@ impl From<AssocItemKind> for ItemKind {
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
AssocItemKind::DelegationList(delegation) => ItemKind::DelegationList(delegation),
AssocItemKind::DelegationGlob(delegation) => ItemKind::DelegationGlob(delegation),
}
}
}
Expand All @@ -3343,6 +3353,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
ItemKind::DelegationList(d) => AssocItemKind::DelegationList(d),
ItemKind::DelegationGlob(d) => AssocItemKind::DelegationGlob(d),
_ => return Err(item_kind),
})
}
Expand Down
35 changes: 17 additions & 18 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,18 +1149,10 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
}
ItemKind::MacCall(m) => vis.visit_mac_call(m),
ItemKind::MacroDef(def) => vis.visit_macro_def(def),
ItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
ItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
vis.visit_id(id);
vis.visit_qself(qself);
vis.visit_path(path);
match kind {
DelegationKind::Single => {}
DelegationKind::List(suffixes) => {
for ident in suffixes {
vis.visit_ident(ident);
}
}
}
if let Some(body) = body {
vis.visit_block(body);
}
Expand All @@ -1175,6 +1167,13 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
vis.visit_block(body);
}
}
ItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
vis.visit_qself(qself);
vis.visit_path(prefix);
if let Some(body) = body {
vis.visit_block(body);
}
}
}
}

Expand Down Expand Up @@ -1213,18 +1212,10 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visit_opt(ty, |ty| visitor.visit_ty(ty));
}
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
AssocItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
AssocItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
visitor.visit_id(id);
visitor.visit_qself(qself);
visitor.visit_path(path);
match kind {
DelegationKind::Single => {}
DelegationKind::List(suffixes) => {
for ident in suffixes {
visitor.visit_ident(ident);
}
}
}
if let Some(body) = body {
visitor.visit_block(body);
}
Expand All @@ -1240,6 +1231,14 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_block(body);
}
}
AssocItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
visitor.visit_id(id);
visitor.visit_qself(qself);
visitor.visit_path(prefix);
if let Some(body) = body {
visitor.visit_block(body);
}
}
}
visitor.visit_span(span);
visit_lazy_tts(tokens, visitor);
Expand Down
34 changes: 16 additions & 18 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,11 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Resu
}
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, item.id)),
ItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
ItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
if let Some(qself) = qself {
try_visit!(visitor.visit_ty(&qself.ty));
}
try_visit!(visitor.visit_path(path, *id));
match kind {
DelegationKind::Single => {}
DelegationKind::List(suffixes) => {
for ident in suffixes {
visitor.visit_ident(*ident);
}
}
}
visit_opt!(visitor, visit_block, body);
}
ItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
Expand All @@ -407,6 +399,13 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Resu
}
visit_opt!(visitor, visit_block, body);
}
ItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
if let Some(qself) = qself {
try_visit!(visitor.visit_ty(&qself.ty));
}
try_visit!(visitor.visit_path(prefix, item.id));
visit_opt!(visitor, visit_block, body);
}
}
walk_list!(visitor, visit_attribute, &item.attrs);
V::Result::output()
Expand Down Expand Up @@ -800,19 +799,11 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
AssocItemKind::MacCall(mac) => {
try_visit!(visitor.visit_mac_call(mac));
}
AssocItemKind::Delegation(box Delegation { id, qself, path, kind, body }) => {
AssocItemKind::Delegation(box Delegation { id, qself, path, body, from_glob: _ }) => {
if let Some(qself) = qself {
try_visit!(visitor.visit_ty(&qself.ty));
}
try_visit!(visitor.visit_path(path, *id));
match kind {
DelegationKind::Single => {}
DelegationKind::List(suffixes) => {
for ident in suffixes {
visitor.visit_ident(*ident);
}
}
}
visit_opt!(visitor, visit_block, body);
}
AssocItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
Expand All @@ -825,6 +816,13 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
}
visit_opt!(visitor, visit_block, body);
}
AssocItemKind::DelegationGlob(box DelegationGlob { qself, prefix, body }) => {
if let Some(qself) = qself {
try_visit!(visitor.visit_ty(&qself.ty));
}
try_visit!(visitor.visit_path(prefix, item.id));
visit_opt!(visitor, visit_block, body);
}
}
V::Result::output()
}
Expand Down
18 changes: 13 additions & 5 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
delegation_results.body_id,
)
}
ItemKind::MacCall(..) | ItemKind::DelegationList(..) => {
ItemKind::MacCall(..) | ItemKind::DelegationList(..) | ItemKind::DelegationGlob(..) => {
panic!("macros should have been expanded by now")
}
}
Expand Down Expand Up @@ -844,7 +844,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
(delegation_results.generics, item_kind, true)
}
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => {
AssocItemKind::MacCall(..)
| AssocItemKind::DelegationList(..)
| AssocItemKind::DelegationGlob(..) => {
panic!("macro item shouldn't exist at this point")
}
};
Expand All @@ -870,7 +872,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
has_self: self.delegation_has_self(i.id, delegation.id, i.span),
},
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => unreachable!(),
AssocItemKind::MacCall(..)
| AssocItemKind::DelegationList(..)
| AssocItemKind::DelegationGlob(..) => unreachable!(),
};
let id = hir::TraitItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } };
hir::TraitItemRef {
Expand Down Expand Up @@ -965,7 +969,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ImplItemKind::Fn(delegation_results.sig, delegation_results.body_id),
)
}
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => {
AssocItemKind::MacCall(..)
| AssocItemKind::DelegationList(..)
| AssocItemKind::DelegationGlob(..) => {
panic!("macros should have been expanded by now")
}
};
Expand Down Expand Up @@ -996,7 +1002,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
has_self: self.delegation_has_self(i.id, delegation.id, i.span),
},
AssocItemKind::MacCall(..) | AssocItemKind::DelegationList(..) => unreachable!(),
AssocItemKind::MacCall(..)
| AssocItemKind::DelegationList(..)
| AssocItemKind::DelegationGlob(..) => unreachable!(),
},
trait_item_def_id: self
.resolver
Expand Down
Loading

0 comments on commit d365e9c

Please sign in to comment.