Skip to content

Commit 78bf0c4

Browse files
committed
delegation: Support renaming
1 parent c2fbe40 commit 78bf0c4

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,6 +3081,7 @@ pub struct Delegation {
30813081
/// Path resolution id.
30823082
pub id: NodeId,
30833083
pub qself: Option<P<QSelf>>,
3084+
pub rename: Option<Ident>,
30843085
pub path: Path,
30853086
pub body: Option<P<Block>>,
30863087
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,10 +1124,13 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
11241124
}
11251125
ItemKind::MacCall(m) => vis.visit_mac_call(m),
11261126
ItemKind::MacroDef(def) => vis.visit_macro_def(def),
1127-
ItemKind::Delegation(box Delegation { id, qself, path, body }) => {
1127+
ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
11281128
vis.visit_id(id);
11291129
vis.visit_qself(qself);
11301130
vis.visit_path(path);
1131+
if let Some(rename) = rename {
1132+
vis.visit_ident(rename);
1133+
}
11311134
if let Some(body) = body {
11321135
vis.visit_block(body);
11331136
}
@@ -1170,10 +1173,13 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11701173
visit_opt(ty, |ty| visitor.visit_ty(ty));
11711174
}
11721175
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
1173-
AssocItemKind::Delegation(box Delegation { id, qself, path, body }) => {
1176+
AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
11741177
visitor.visit_id(id);
11751178
visitor.visit_qself(qself);
11761179
visitor.visit_path(path);
1180+
if let Some(rename) = rename {
1181+
visitor.visit_ident(rename);
1182+
}
11771183
if let Some(body) = body {
11781184
visitor.visit_block(body);
11791185
}

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,12 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Resu
379379
}
380380
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
381381
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, item.id)),
382-
ItemKind::Delegation(box Delegation { id, qself, path, body }) => {
382+
ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
383383
if let Some(qself) = qself {
384384
try_visit!(visitor.visit_ty(&qself.ty));
385385
}
386386
try_visit!(visitor.visit_path(path, *id));
387+
visit_opt!(visitor, visit_ident, *rename);
387388
visit_opt!(visitor, visit_block, body);
388389
}
389390
}
@@ -753,11 +754,12 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
753754
AssocItemKind::MacCall(mac) => {
754755
try_visit!(visitor.visit_mac_call(mac));
755756
}
756-
AssocItemKind::Delegation(box Delegation { id, qself, path, body }) => {
757+
AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
757758
if let Some(qself) = qself {
758759
visitor.visit_ty(&qself.ty);
759760
}
760761
try_visit!(visitor.visit_path(path, *id));
762+
visit_opt!(visitor, visit_ident, *rename);
761763
visit_opt!(visitor, visit_block, body);
762764
}
763765
}

compiler/rustc_parse/src/parser/item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ impl<'a> Parser<'a> {
692692
(None, self.parse_path(PathStyle::Expr)?)
693693
};
694694

695+
let rename = if self.eat_keyword(kw::As) { Some(self.parse_ident()?) } else { None };
696+
695697
let body = if self.check(&token::OpenDelim(Delimiter::Brace)) {
696698
Some(self.parse_block()?)
697699
} else {
@@ -701,11 +703,9 @@ impl<'a> Parser<'a> {
701703
let span = span.to(self.prev_token.span);
702704
self.psess.gated_spans.gate(sym::fn_delegation, span);
703705

704-
let ident = path.segments.last().map(|seg| seg.ident).unwrap_or(Ident::empty());
705-
Ok((
706-
ident,
707-
ItemKind::Delegation(Box::new(Delegation { id: DUMMY_NODE_ID, qself, path, body })),
708-
))
706+
let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident);
707+
let deleg = Delegation { id: DUMMY_NODE_ID, qself, path, rename, body };
708+
Ok((ident, ItemKind::Delegation(Box::new(deleg))))
709709
}
710710

711711
fn parse_item_list<T>(

tests/ui/delegation/rename.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ check-pass
2+
3+
#![feature(fn_delegation)]
4+
#![allow(incomplete_features)]
5+
6+
mod to_reuse {
7+
pub fn a() {}
8+
}
9+
10+
reuse to_reuse::a as b;
11+
12+
struct S;
13+
impl S {
14+
reuse to_reuse::a as b;
15+
}
16+
17+
fn main() {
18+
b();
19+
S::b();
20+
}

0 commit comments

Comments
 (0)