Skip to content

Commit 99b635e

Browse files
committed
delegation: Support renaming
1 parent c2f2db7 commit 99b635e

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3112,6 +3112,7 @@ pub struct Delegation {
31123112
/// Path resolution id.
31133113
pub id: NodeId,
31143114
pub qself: Option<P<QSelf>>,
3115+
pub rename: Option<Ident>,
31153116
pub path: Path,
31163117
pub body: Option<P<Block>>,
31173118
}

Diff for: compiler/rustc_ast/src/mut_visit.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1149,10 +1149,13 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
11491149
}
11501150
ItemKind::MacCall(m) => vis.visit_mac_call(m),
11511151
ItemKind::MacroDef(def) => vis.visit_macro_def(def),
1152-
ItemKind::Delegation(box Delegation { id, qself, path, body }) => {
1152+
ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
11531153
vis.visit_id(id);
11541154
vis.visit_qself(qself);
11551155
vis.visit_path(path);
1156+
if let Some(rename) = rename {
1157+
vis.visit_ident(rename);
1158+
}
11561159
if let Some(body) = body {
11571160
vis.visit_block(body);
11581161
}
@@ -1195,10 +1198,13 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11951198
visit_opt(ty, |ty| visitor.visit_ty(ty));
11961199
}
11971200
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
1198-
AssocItemKind::Delegation(box Delegation { id, qself, path, body }) => {
1201+
AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
11991202
visitor.visit_id(id);
12001203
visitor.visit_qself(qself);
12011204
visitor.visit_path(path);
1205+
if let Some(rename) = rename {
1206+
visitor.visit_ident(rename);
1207+
}
12021208
if let Some(body) = body {
12031209
visitor.visit_block(body);
12041210
}

Diff for: compiler/rustc_ast/src/visit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,12 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) -> V::Resu
382382
}
383383
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
384384
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, item.id)),
385-
ItemKind::Delegation(box Delegation { id, qself, path, body }) => {
385+
ItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
386386
if let Some(qself) = qself {
387387
try_visit!(visitor.visit_ty(&qself.ty));
388388
}
389389
try_visit!(visitor.visit_path(path, *id));
390+
visit_opt!(visitor, visit_ident, *rename);
390391
visit_opt!(visitor, visit_block, body);
391392
}
392393
}
@@ -782,11 +783,12 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(
782783
AssocItemKind::MacCall(mac) => {
783784
try_visit!(visitor.visit_mac_call(mac));
784785
}
785-
AssocItemKind::Delegation(box Delegation { id, qself, path, body }) => {
786+
AssocItemKind::Delegation(box Delegation { id, qself, path, rename, body }) => {
786787
if let Some(qself) = qself {
787788
try_visit!(visitor.visit_ty(&qself.ty));
788789
}
789790
try_visit!(visitor.visit_path(path, *id));
791+
visit_opt!(visitor, visit_ident, *rename);
790792
visit_opt!(visitor, visit_block, body);
791793
}
792794
}

Diff for: 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>(

Diff for: 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)