Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Apr 19, 2024
1 parent d365e9c commit 2737bcf
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 178 deletions.
42 changes: 13 additions & 29 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2950,8 +2950,7 @@ impl Item {
| ItemKind::GlobalAsm(_)
| ItemKind::MacCall(_)
| ItemKind::Delegation(_)
| ItemKind::DelegationList(_)
| ItemKind::DelegationGlob(_)
| ItemKind::DelegationMac(_)
| ItemKind::MacroDef(_) => None,
ItemKind::Static(_) => None,
ItemKind::Const(i) => Some(&i.generics),
Expand Down Expand Up @@ -3120,17 +3119,11 @@ pub struct Delegation {
}

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

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct DelegationGlob {
pub struct DelegationMac {
pub qself: Option<P<QSelf>>,
pub prefix: Path,
// Some for list delegation, and None for glob delegation
pub suffixes: Option<ThinVec<Ident>>,
pub body: Option<P<Block>>,
}

Expand Down Expand Up @@ -3224,12 +3217,9 @@ pub enum ItemKind {
///
/// E.g. `reuse <Type as Trait>::name { target_expr_template }`.
Delegation(Box<Delegation>),
/// 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::*`).
/// A list or glob delegation item (`reuse prefix::{a, b, c}`, `reuse prefix::*`).
/// Treated similarly to a macro call and expanded early.
DelegationGlob(Box<DelegationGlob>),
DelegationMac(Box<DelegationMac>),
}

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

Expand Down Expand Up @@ -3308,10 +3297,8 @@ pub enum AssocItemKind {
MacCall(P<MacCall>),
/// An associated delegation item.
Delegation(Box<Delegation>),
/// An associated delegation item list.
DelegationList(Box<DelegationList>),
/// An associated delegation item glob.
DelegationGlob(Box<DelegationGlob>),
/// An associated list or glob delegation item.
DelegationMac(Box<DelegationMac>),
}

impl AssocItemKind {
Expand All @@ -3322,8 +3309,7 @@ impl AssocItemKind {
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..)
| Self::Delegation(..)
| Self::DelegationList(..)
| Self::DelegationGlob(..) => Defaultness::Final,
| Self::DelegationMac(..) => Defaultness::Final,
}
}
}
Expand All @@ -3336,8 +3322,7 @@ impl From<AssocItemKind> for ItemKind {
AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
AssocItemKind::DelegationList(delegation) => ItemKind::DelegationList(delegation),
AssocItemKind::DelegationGlob(delegation) => ItemKind::DelegationGlob(delegation),
AssocItemKind::DelegationMac(delegation) => ItemKind::DelegationMac(delegation),
}
}
}
Expand All @@ -3352,8 +3337,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
ItemKind::DelegationList(d) => AssocItemKind::DelegationList(d),
ItemKind::DelegationGlob(d) => AssocItemKind::DelegationGlob(d),
ItemKind::DelegationMac(d) => AssocItemKind::DelegationMac(d),
_ => return Err(item_kind),
})
}
Expand Down
31 changes: 10 additions & 21 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,19 +1157,14 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
vis.visit_block(body);
}
}
ItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
ItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
vis.visit_qself(qself);
vis.visit_path(prefix);
for ident in suffixes {
vis.visit_ident(ident);
}
if let Some(body) = body {
vis.visit_block(body);
if let Some(suffixes) = suffixes {
for ident in suffixes {
vis.visit_ident(ident);
}
}
}
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 @@ -1220,21 +1215,15 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_block(body);
}
}
AssocItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
AssocItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
visitor.visit_id(id);
visitor.visit_qself(qself);
visitor.visit_path(prefix);
for ident in suffixes {
visitor.visit_ident(ident);
}
if let Some(body) = body {
visitor.visit_block(body);
if let Some(suffixes) = suffixes {
for ident in suffixes {
visitor.visit_ident(ident);
}
}
}
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);
}
Expand Down
30 changes: 10 additions & 20 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,21 +389,16 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Resu
try_visit!(visitor.visit_path(path, *id));
visit_opt!(visitor, visit_block, body);
}
ItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
ItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
if let Some(qself) = qself {
try_visit!(visitor.visit_ty(&qself.ty));
}
try_visit!(visitor.visit_path(prefix, item.id));
for suffix in suffixes {
visitor.visit_ident(*suffix);
}
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));
if let Some(suffixes) = suffixes {
for suffix in suffixes {
visitor.visit_ident(*suffix);
}
}
try_visit!(visitor.visit_path(prefix, item.id));
visit_opt!(visitor, visit_block, body);
}
}
Expand Down Expand Up @@ -806,21 +801,16 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
try_visit!(visitor.visit_path(path, *id));
visit_opt!(visitor, visit_block, body);
}
AssocItemKind::DelegationList(box DelegationList { qself, prefix, suffixes, body }) => {
AssocItemKind::DelegationMac(box DelegationMac { qself, prefix, suffixes, body }) => {
if let Some(qself) = qself {
try_visit!(visitor.visit_ty(&qself.ty));
}
try_visit!(visitor.visit_path(prefix, item.id));
for suffix in suffixes {
visitor.visit_ident(*suffix);
}
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));
if let Some(suffixes) = suffixes {
for suffix in suffixes {
visitor.visit_ident(*suffix);
}
}
try_visit!(visitor.visit_path(prefix, item.id));
visit_opt!(visitor, visit_block, body);
}
}
Expand Down
18 changes: 5 additions & 13 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::DelegationGlob(..) => {
ItemKind::MacCall(..) | ItemKind::DelegationMac(..) => {
panic!("macros should have been expanded by now")
}
}
Expand Down Expand Up @@ -844,9 +844,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
(delegation_results.generics, item_kind, true)
}
AssocItemKind::MacCall(..)
| AssocItemKind::DelegationList(..)
| AssocItemKind::DelegationGlob(..) => {
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
panic!("macro item shouldn't exist at this point")
}
};
Expand All @@ -872,9 +870,7 @@ 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(..)
| AssocItemKind::DelegationGlob(..) => unreachable!(),
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => unreachable!(),
};
let id = hir::TraitItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } };
hir::TraitItemRef {
Expand Down Expand Up @@ -969,9 +965,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ImplItemKind::Fn(delegation_results.sig, delegation_results.body_id),
)
}
AssocItemKind::MacCall(..)
| AssocItemKind::DelegationList(..)
| AssocItemKind::DelegationGlob(..) => {
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
panic!("macros should have been expanded by now")
}
};
Expand Down Expand Up @@ -1002,9 +996,7 @@ 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(..)
| AssocItemKind::DelegationGlob(..) => unreachable!(),
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => unreachable!(),
},
trait_item_def_id: self
.resolver
Expand Down
24 changes: 4 additions & 20 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,12 @@ impl<'a> State<'a> {
DelegationKind::Single,
&deleg.body,
),
ast::ItemKind::DelegationList(deleg) => self.print_delegation(
ast::ItemKind::DelegationMac(deleg) => self.print_delegation(
&item.attrs,
&item.vis,
&deleg.qself,
&deleg.prefix,
DelegationKind::List(&deleg.suffixes),
&deleg.body,
),
ast::ItemKind::DelegationGlob(deleg) => self.print_delegation(
&item.attrs,
&item.vis,
&deleg.qself,
&deleg.prefix,
DelegationKind::Glob,
deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
&deleg.body,
),
}
Expand Down Expand Up @@ -586,20 +578,12 @@ impl<'a> State<'a> {
DelegationKind::Single,
&deleg.body,
),
ast::AssocItemKind::DelegationList(deleg) => self.print_delegation(
&item.attrs,
vis,
&deleg.qself,
&deleg.prefix,
DelegationKind::List(&deleg.suffixes),
&deleg.body,
),
ast::AssocItemKind::DelegationGlob(deleg) => self.print_delegation(
ast::AssocItemKind::DelegationMac(deleg) => self.print_delegation(
&item.attrs,
vis,
&deleg.qself,
&deleg.prefix,
DelegationKind::Glob,
deleg.suffixes.as_ref().map_or(DelegationKind::Glob, |s| DelegationKind::List(s)),
&deleg.body,
),
}
Expand Down
Loading

0 comments on commit 2737bcf

Please sign in to comment.