Skip to content

Commit 6312987

Browse files
committed
delegation: Support renaming
1 parent af98101 commit 6312987

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

compiler/rustc_ast/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,7 @@ pub struct Delegation {
30913091
/// Path resolution id.
30923092
pub id: NodeId,
30933093
pub qself: Option<P<QSelf>>,
3094+
pub rename: Option<Ident>,
30943095
pub path: Path,
30953096
pub body: Option<P<Block>>,
30963097
}

compiler/rustc_ast/src/mut_visit.rs

+8-2
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

+4-2
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
}
@@ -756,11 +757,12 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
756757
AssocItemKind::MacCall(mac) => {
757758
try_visit!(visitor.visit_mac_call(mac));
758759
}
759-
AssocItemKind::Delegation(box Delegation { id, qself, path, body }) => {
760+
AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
760761
if let Some(qself) = qself {
761762
try_visit!(visitor.visit_ty(&qself.ty));
762763
}
763764
try_visit!(visitor.visit_path(path, *id));
765+
visit_opt!(visitor, visit_ident, *rename);
764766
visit_opt!(visitor, visit_block, body);
765767
}
766768
}

compiler/rustc_parse/src/parser/item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,8 @@ impl<'a> Parser<'a> {
686686
(None, self.parse_path(PathStyle::Expr)?)
687687
};
688688

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

698-
let ident = path.segments.last().map(|seg| seg.ident).unwrap_or(Ident::empty());
699-
Ok((
700-
ident,
701-
ItemKind::Delegation(Box::new(Delegation { id: DUMMY_NODE_ID, qself, path, body })),
702-
))
700+
let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident);
701+
let deleg = Delegation { id: DUMMY_NODE_ID, qself, path, rename, body };
702+
Ok((ident, ItemKind::Delegation(Box::new(deleg))))
703703
}
704704

705705
fn parse_item_list<T>(

tests/ui/delegation/rename.rs

+20
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)