From 99b635eafa372d3b8522e978262095f60a8b4e56 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 14 Mar 2024 11:41:38 +0300 Subject: [PATCH] delegation: Support renaming --- compiler/rustc_ast/src/ast.rs | 1 + compiler/rustc_ast/src/mut_visit.rs | 10 ++++++++-- compiler/rustc_ast/src/visit.rs | 6 ++++-- compiler/rustc_parse/src/parser/item.rs | 10 +++++----- tests/ui/delegation/rename.rs | 20 ++++++++++++++++++++ 5 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 tests/ui/delegation/rename.rs diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index bddb50568d4..727c6bc82fe 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -3112,6 +3112,7 @@ pub struct Delegation { /// Path resolution id. pub id: NodeId, pub qself: Option>, + pub rename: Option, pub path: Path, pub body: Option>, } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index c4e49d7dbea..7dbfb7c7364 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1149,10 +1149,13 @@ pub fn noop_visit_item_kind(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, body }) => { + ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => { vis.visit_id(id); vis.visit_qself(qself); vis.visit_path(path); + if let Some(rename) = rename { + vis.visit_ident(rename); + } if let Some(body) = body { vis.visit_block(body); } @@ -1195,10 +1198,13 @@ pub fn noop_flat_map_assoc_item( visit_opt(ty, |ty| visitor.visit_ty(ty)); } AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac), - AssocItemKind::Delegation(box Delegation { id, qself, path, body }) => { + AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => { visitor.visit_id(id); visitor.visit_qself(qself); visitor.visit_path(path); + if let Some(rename) = rename { + visitor.visit_ident(rename); + } if let Some(body) = body { visitor.visit_block(body); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 968d10ad487..d9740928f8d 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -382,11 +382,12 @@ 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, body }) => { + ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => { if let Some(qself) = qself { try_visit!(visitor.visit_ty(&qself.ty)); } try_visit!(visitor.visit_path(path, *id)); + visit_opt!(visitor, visit_ident, *rename); visit_opt!(visitor, visit_block, body); } } @@ -782,11 +783,12 @@ 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, body }) => { + AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => { if let Some(qself) = qself { try_visit!(visitor.visit_ty(&qself.ty)); } try_visit!(visitor.visit_path(path, *id)); + visit_opt!(visitor, visit_ident, *rename); visit_opt!(visitor, visit_block, body); } } diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 84ecd0a0de5..ed51710564a 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -686,6 +686,8 @@ impl<'a> Parser<'a> { (None, self.parse_path(PathStyle::Expr)?) }; + let rename = if self.eat_keyword(kw::As) { Some(self.parse_ident()?) } else { None }; + let body = if self.check(&token::OpenDelim(Delimiter::Brace)) { Some(self.parse_block()?) } else { @@ -695,11 +697,9 @@ impl<'a> Parser<'a> { let span = span.to(self.prev_token.span); self.psess.gated_spans.gate(sym::fn_delegation, span); - let ident = path.segments.last().map(|seg| seg.ident).unwrap_or(Ident::empty()); - Ok(( - ident, - ItemKind::Delegation(Box::new(Delegation { id: DUMMY_NODE_ID, qself, path, body })), - )) + let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident); + let deleg = Delegation { id: DUMMY_NODE_ID, qself, path, rename, body }; + Ok((ident, ItemKind::Delegation(Box::new(deleg)))) } fn parse_item_list( diff --git a/tests/ui/delegation/rename.rs b/tests/ui/delegation/rename.rs new file mode 100644 index 00000000000..f4b3da76c56 --- /dev/null +++ b/tests/ui/delegation/rename.rs @@ -0,0 +1,20 @@ +//@ check-pass + +#![feature(fn_delegation)] +#![allow(incomplete_features)] + +mod to_reuse { + pub fn a() {} +} + +reuse to_reuse::a as b; + +struct S; +impl S { + reuse to_reuse::a as b; +} + +fn main() { + b(); + S::b(); +}