Skip to content

Commit

Permalink
delegation: impl step 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryanskiy committed Dec 14, 2023
1 parent 529047c commit 6204a46
Show file tree
Hide file tree
Showing 34 changed files with 1,156 additions and 28 deletions.
23 changes: 21 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,14 @@ pub struct Fn {
pub body: Option<P<Block>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Delegation {
pub id: NodeId,
pub path: (Option<P<QSelf>>, Path),
pub target_expr: Option<P<Expr>>,
pub span: Span,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StaticItem {
pub ty: P<Ty>,
Expand Down Expand Up @@ -3063,14 +3071,20 @@ pub enum ItemKind {

/// A macro definition.
MacroDef(MacroDef),

/// A delegation item (`reuse`).
///
/// E.g. reuse <Type as Trait>::name { target_expr_template }
Delegation(Box<Delegation>),
}

impl ItemKind {
pub fn article(&self) -> &'static str {
use ItemKind::*;
match self {
Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..) => "a",
| Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
| Delegation(..) => "a",
ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
}
}
Expand All @@ -3094,6 +3108,7 @@ impl ItemKind {
ItemKind::MacCall(..) => "item macro invocation",
ItemKind::MacroDef(..) => "macro definition",
ItemKind::Impl { .. } => "implementation",
ItemKind::Delegation(..) => "delegation",
}
}

Expand Down Expand Up @@ -3135,6 +3150,8 @@ pub enum AssocItemKind {
Type(Box<TyAlias>),
/// A macro expanding to associated items.
MacCall(P<MacCall>),
/// An associated delegation item.
Delegation(Box<Delegation>),
}

impl AssocItemKind {
Expand All @@ -3143,7 +3160,7 @@ impl AssocItemKind {
Self::Const(box ConstItem { defaultness, .. })
| Self::Fn(box Fn { defaultness, .. })
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..) => Defaultness::Final,
Self::MacCall(..) | Self::Delegation(..) => Defaultness::Final,
}
}
}
Expand All @@ -3155,6 +3172,7 @@ impl From<AssocItemKind> for ItemKind {
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
}
}
}
Expand All @@ -3168,6 +3186,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
_ => return Err(item_kind),
})
}
Expand Down
19 changes: 16 additions & 3 deletions compiler/rustc_ast/src/ast_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::ptr::P;
use crate::token::Nonterminal;
use crate::tokenstream::LazyAttrTokenStream;
use crate::{Arm, Crate, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
use crate::{Arm, Crate, Delegation, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
use crate::{AssocItem, Expr, ForeignItem, Item, NodeId};
use crate::{AttrItem, AttrKind, Block, Pat, Path, Ty, Visibility};
use crate::{AttrVec, Attribute, Stmt, StmtKind};
Expand Down Expand Up @@ -80,6 +80,7 @@ impl_has_node_id!(
Stmt,
Ty,
Variant,
Delegation
);

impl<T: AstDeref<Target: HasNodeId>> HasNodeId for T {
Expand Down Expand Up @@ -108,7 +109,19 @@ macro_rules! impl_has_span {
};
}

impl_has_span!(AssocItem, Block, Expr, ForeignItem, Item, Pat, Path, Stmt, Ty, Visibility);
impl_has_span!(
AssocItem,
Block,
Expr,
ForeignItem,
Item,
Pat,
Path,
Stmt,
Ty,
Visibility,
Delegation
);

impl<T: AstDeref<Target: HasSpan>> HasSpan for T {
fn span(&self) -> Span {
Expand Down Expand Up @@ -159,7 +172,7 @@ macro_rules! impl_has_tokens_none {
}

impl_has_tokens!(AssocItem, AttrItem, Block, Expr, ForeignItem, Item, Pat, Path, Ty, Visibility);
impl_has_tokens_none!(Arm, ExprField, FieldDef, GenericParam, Param, PatField, Variant);
impl_has_tokens_none!(Arm, ExprField, FieldDef, GenericParam, Param, PatField, Variant, Delegation);

impl<T: AstDeref<Target: HasTokens>> HasTokens for T {
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,14 @@ 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) => {
vis.visit_id(&mut delegation.id);
vis.visit_qself(&mut delegation.path.0);
vis.visit_path(&mut delegation.path.1);
if let Some(target_expr) = &mut delegation.target_expr {
vis.visit_expr(target_expr);
}
}
}
}

Expand Down Expand Up @@ -1155,6 +1163,14 @@ 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) => {
visitor.visit_id(&mut delegation.id);
visitor.visit_qself(&mut delegation.path.0);
visitor.visit_path(&mut delegation.path.1);
if let Some(target_expr) = &mut delegation.target_expr {
visitor.visit_expr(target_expr);
}
}
}
visitor.visit_span(span);
visit_lazy_tts(tokens, visitor);
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
}
ItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
ItemKind::MacroDef(ts) => visitor.visit_mac_def(ts, item.id),
ItemKind::Delegation(box delegation) => {
if let Some(qself) = &delegation.path.0 {
visitor.visit_ty(&qself.ty);
}
walk_path(visitor, &delegation.path.1);
if let Some(target_expr) = &delegation.target_expr {
visitor.visit_expr(target_expr);
}
}
}
walk_list!(visitor, visit_attribute, &item.attrs);
}
Expand Down Expand Up @@ -704,6 +713,15 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
AssocItemKind::MacCall(mac) => {
visitor.visit_mac_call(mac);
}
AssocItemKind::Delegation(box delegation) => {
if let Some(qself) = &delegation.path.0 {
visitor.visit_ty(&qself.ty);
}
walk_path(visitor, &delegation.path.1);
if let Some(target_expr) = &delegation.target_expr {
visitor.visit_expr(target_expr);
}
}
}
}

Expand Down
Loading

0 comments on commit 6204a46

Please sign in to comment.