Skip to content

Commit 34d9170

Browse files
committed
parse: refactor fun ret ty & param ty
1 parent 404013e commit 34d9170

File tree

12 files changed

+77
-107
lines changed

12 files changed

+77
-107
lines changed

src/librustc/hir/lowering.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,29 +2092,19 @@ impl<'a> LoweringContext<'a> {
20922092
.iter()
20932093
.map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed()))
20942094
.collect();
2095+
let output_ty = match output {
2096+
FunctionRetTy::Ty(ty) => this.lower_ty(&ty, ImplTraitContext::disallowed()),
2097+
FunctionRetTy::Default(_) => P(this.ty_tup(span, hir::HirVec::new())),
2098+
};
2099+
let args = hir_vec![GenericArg::Type(this.ty_tup(span, inputs))];
2100+
let binding = hir::TypeBinding {
2101+
hir_id: this.next_id(),
2102+
ident: Ident::with_dummy_span(FN_OUTPUT_NAME),
2103+
span: output_ty.span,
2104+
kind: hir::TypeBindingKind::Equality { ty: output_ty },
2105+
};
20952106
(
2096-
hir::GenericArgs {
2097-
args: hir_vec![GenericArg::Type(this.ty_tup(span, inputs))],
2098-
bindings: hir_vec![
2099-
hir::TypeBinding {
2100-
hir_id: this.next_id(),
2101-
ident: Ident::with_dummy_span(FN_OUTPUT_NAME),
2102-
kind: hir::TypeBindingKind::Equality {
2103-
ty: output
2104-
.as_ref()
2105-
.map(|ty| this.lower_ty(
2106-
&ty,
2107-
ImplTraitContext::disallowed()
2108-
))
2109-
.unwrap_or_else(||
2110-
P(this.ty_tup(span, hir::HirVec::new()))
2111-
),
2112-
},
2113-
span: output.as_ref().map_or(span, |ty| ty.span),
2114-
}
2115-
],
2116-
parenthesized: true,
2117-
},
2107+
hir::GenericArgs { args, bindings: hir_vec![binding], parenthesized: true },
21182108
false,
21192109
)
21202110
}

src/librustc_interface/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,8 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
712712
ret
713713
}
714714

715-
fn should_ignore_fn(ret_ty: &ast::FnDecl) -> bool {
716-
if let ast::FunctionRetTy::Ty(ref ty) = ret_ty.output {
715+
fn should_ignore_fn(ret_ty: &ast::FunctionRetTy) -> bool {
716+
if let ast::FunctionRetTy::Ty(ref ty) = ret_ty {
717717
fn involves_impl_trait(ty: &ast::Ty) -> bool {
718718
match ty.kind {
719719
ast::TyKind::ImplTrait(..) => true,
@@ -742,7 +742,7 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
742742
},
743743
Some(&ast::GenericArgs::Parenthesized(ref data)) => {
744744
any_involves_impl_trait(data.inputs.iter()) ||
745-
any_involves_impl_trait(data.output.iter())
745+
ReplaceBodyWithLoop::should_ignore_fn(&data.output)
746746
}
747747
}
748748
}),
@@ -762,7 +762,7 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
762762

763763
fn is_sig_const(sig: &ast::FnSig) -> bool {
764764
sig.header.constness.node == ast::Constness::Const ||
765-
ReplaceBodyWithLoop::should_ignore_fn(&sig.decl)
765+
ReplaceBodyWithLoop::should_ignore_fn(&sig.decl.output)
766766
}
767767
}
768768

src/librustc_parse/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ impl<'a> Parser<'a> {
13811381
args
13821382
}
13831383
};
1384-
let output = self.parse_ret_ty(true)?;
1384+
let output = self.parse_ret_ty(true, true)?;
13851385

13861386
Ok(P(FnDecl {
13871387
inputs: inputs_captures,

src/librustc_parse/parser/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,7 @@ impl<'a> Parser<'a> {
19001900
) -> PResult<'a, P<FnDecl>> {
19011901
Ok(P(FnDecl {
19021902
inputs: self.parse_fn_params(cfg)?,
1903-
output: self.parse_ret_ty(ret_allow_plus)?,
1903+
output: self.parse_ret_ty(ret_allow_plus, true)?,
19041904
}))
19051905
}
19061906

@@ -2002,12 +2002,12 @@ impl<'a> Parser<'a> {
20022002
}
20032003

20042004
self.eat_incorrect_doc_comment_for_param_type();
2005-
(pat, self.parse_ty_common(true, true, cfg.allow_c_variadic)?)
2005+
(pat, self.parse_ty_for_param(cfg.allow_c_variadic)?)
20062006
} else {
20072007
debug!("parse_param_general ident_to_pat");
20082008
let parser_snapshot_before_ty = self.clone();
20092009
self.eat_incorrect_doc_comment_for_param_type();
2010-
let mut ty = self.parse_ty_common(true, true, cfg.allow_c_variadic);
2010+
let mut ty = self.parse_ty_for_param(cfg.allow_c_variadic);
20112011
if ty.is_ok() && self.token != token::Comma &&
20122012
self.token != token::CloseDelim(token::Paren) {
20132013
// This wasn't actually a type, but a pattern looking like a type,

src/librustc_parse/parser/path.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,7 @@ impl<'a> Parser<'a> {
182182
// `(T, U) -> R`
183183
let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
184184
let span = ident.span.to(self.prev_span);
185-
let output = if self.eat(&token::RArrow) {
186-
Some(self.parse_ty_common(false, false, false)?)
187-
} else {
188-
None
189-
};
185+
let output = self.parse_ret_ty(false, false)?;
190186
ParenthesizedArgs { inputs, output, span }.into()
191187
};
192188

src/librustc_parse/parser/ty.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ impl<'a> Parser<'a> {
3030
self.parse_ty_common(true, true, false)
3131
}
3232

33+
/// Parse a type suitable for a function or function pointer parameter.
34+
/// The difference from `parse_ty` is that this version allows `...`
35+
/// (`CVarArgs`) at the top level of the the type.
36+
pub(super) fn parse_ty_for_param(&mut self, allow_c_variadic: bool) -> PResult<'a, P<Ty>> {
37+
self.parse_ty_common(true, true, allow_c_variadic)
38+
}
39+
3340
/// Parses a type in restricted contexts where `+` is not permitted.
3441
///
3542
/// Example 1: `&'a TYPE`
@@ -41,17 +48,26 @@ impl<'a> Parser<'a> {
4148
}
4249

4350
/// Parses an optional return type `[ -> TY ]` in a function declaration.
44-
pub(super) fn parse_ret_ty(&mut self, allow_plus: bool) -> PResult<'a, FunctionRetTy> {
51+
pub(super) fn parse_ret_ty(
52+
&mut self,
53+
allow_plus: bool,
54+
allow_qpath_recovery: bool,
55+
) -> PResult<'a, FunctionRetTy> {
4556
Ok(if self.eat(&token::RArrow) {
4657
// FIXME(Centril): Can we unconditionally `allow_plus`?
47-
FunctionRetTy::Ty(self.parse_ty_common(allow_plus, true, false)?)
58+
FunctionRetTy::Ty(self.parse_ty_common(allow_plus, allow_qpath_recovery, false)?)
4859
} else {
4960
FunctionRetTy::Default(self.token.span.shrink_to_lo())
5061
})
5162
}
5263

53-
pub(super) fn parse_ty_common(&mut self, allow_plus: bool, allow_qpath_recovery: bool,
54-
allow_c_variadic: bool) -> PResult<'a, P<Ty>> {
64+
fn parse_ty_common(
65+
&mut self,
66+
allow_plus: bool,
67+
allow_qpath_recovery: bool,
68+
// Is `...` (`CVarArgs`) legal in the immediate top level call?
69+
allow_c_variadic: bool,
70+
) -> PResult<'a, P<Ty>> {
5571
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
5672
maybe_whole!(self, NtTy, |x| x);
5773

@@ -198,6 +214,8 @@ impl<'a> Parser<'a> {
198214
self.eat(&token::DotDotDot);
199215
TyKind::CVarArgs
200216
} else {
217+
// FIXME(Centril): Should we just allow `...` syntactically
218+
// anywhere in a type and use semantic restrictions instead?
201219
return Err(struct_span_fatal!(
202220
self.sess.span_diagnostic,
203221
self.token.span,

src/librustc_passes/ast_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
674674
}
675675
GenericArgs::Parenthesized(ref data) => {
676676
walk_list!(self, visit_ty, &data.inputs);
677-
if let Some(ref type_) = data.output {
677+
if let FunctionRetTy::Ty(ty) = &data.output {
678678
// `-> Foo` syntax is essentially an associated type binding,
679679
// so it is also allowed to contain nested `impl Trait`.
680-
self.with_impl_trait(None, |this| this.visit_ty(type_));
680+
self.with_impl_trait(None, |this| this.visit_ty(ty));
681681
}
682682
}
683683
}

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -811,18 +811,17 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
811811
match **generic_args {
812812
ast::GenericArgs::AngleBracketed(ref data) => {
813813
for arg in &data.args {
814-
match arg {
815-
ast::GenericArg::Type(ty) => self.visit_ty(ty),
816-
_ => {}
814+
if let ast::GenericArg::Type(ty) = arg {
815+
self.visit_ty(ty);
817816
}
818817
}
819818
}
820819
ast::GenericArgs::Parenthesized(ref data) => {
821820
for t in &data.inputs {
822821
self.visit_ty(t);
823822
}
824-
if let Some(ref t) = data.output {
825-
self.visit_ty(t);
823+
if let ast::FunctionRetTy::Ty(ty) = &data.output {
824+
self.visit_ty(ty);
826825
}
827826
}
828827
}

src/libsyntax/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub struct ParenthesizedArgs {
254254
pub inputs: Vec<P<Ty>>,
255255

256256
/// `C`
257-
pub output: Option<P<Ty>>,
257+
pub output: FunctionRetTy,
258258
}
259259

260260
impl ParenthesizedArgs {
@@ -2185,7 +2185,7 @@ impl fmt::Debug for ImplPolarity {
21852185
}
21862186

21872187
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2188-
pub enum FunctionRetTy {
2188+
pub enum FunctionRetTy { // FIXME(Centril): Rename to `FnRetTy` and in HIR also.
21892189
/// Returns type is not specified.
21902190
///
21912191
/// Functions default to `()` and closures default to inference.

src/libsyntax/mut_visit.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ pub fn noop_visit_parenthesized_parameter_data<T: MutVisitor>(args: &mut Parenth
553553
vis: &mut T) {
554554
let ParenthesizedArgs { inputs, output, span } = args;
555555
visit_vec(inputs, |input| vis.visit_ty(input));
556-
visit_opt(output, |output| vis.visit_ty(output));
556+
noop_visit_fn_ret_ty(output, vis);
557557
vis.visit_span(span);
558558
}
559559

@@ -742,7 +742,11 @@ pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut IsAsync, vis: &mut T)
742742
pub fn noop_visit_fn_decl<T: MutVisitor>(decl: &mut P<FnDecl>, vis: &mut T) {
743743
let FnDecl { inputs, output } = decl.deref_mut();
744744
inputs.flat_map_in_place(|param| vis.flat_map_param(param));
745-
match output {
745+
noop_visit_fn_ret_ty(output, vis);
746+
}
747+
748+
pub fn noop_visit_fn_ret_ty<T: MutVisitor>(fn_ret_ty: &mut FunctionRetTy, vis: &mut T) {
749+
match fn_ret_ty {
746750
FunctionRetTy::Default(span) => vis.visit_span(span),
747751
FunctionRetTy::Ty(ty) => vis.visit_ty(ty),
748752
}

src/libsyntax/print/pprust.rs

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -887,17 +887,9 @@ impl<'a> PrintState<'a> for State<'a> {
887887

888888
ast::GenericArgs::Parenthesized(ref data) => {
889889
self.s.word("(");
890-
self.commasep(
891-
Inconsistent,
892-
&data.inputs,
893-
|s, ty| s.print_type(ty));
890+
self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty));
894891
self.s.word(")");
895-
896-
if let Some(ref ty) = data.output {
897-
self.space_if_not_bol();
898-
self.word_space("->");
899-
self.print_type(ty);
900-
}
892+
self.print_fn_ret_ty(&data.output);
901893
}
902894
}
903895
}
@@ -1579,6 +1571,7 @@ impl<'a> State<'a> {
15791571
self.ann.post(self, AnnNode::SubItem(ti.id))
15801572
}
15811573

1574+
// FIXME(Centril): merge with function above.
15821575
crate fn print_impl_item(&mut self, ii: &ast::ImplItem) {
15831576
self.ann.pre(self, AnnNode::SubItem(ii.id));
15841577
self.hardbreak_if_not_bol();
@@ -2104,7 +2097,7 @@ impl<'a> State<'a> {
21042097
self.print_asyncness(asyncness);
21052098
self.print_capture_clause(capture_clause);
21062099

2107-
self.print_fn_block_params(decl);
2100+
self.print_fn_params_and_ret(decl, true);
21082101
self.s.space();
21092102
self.print_expr(body);
21102103
self.end(); // need to close a box
@@ -2535,36 +2528,16 @@ impl<'a> State<'a> {
25352528
self.print_ident(name);
25362529
}
25372530
self.print_generic_params(&generics.params);
2538-
self.print_fn_params_and_ret(decl);
2531+
self.print_fn_params_and_ret(decl, false);
25392532
self.print_where_clause(&generics.where_clause)
25402533
}
25412534

2542-
crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl) {
2543-
self.popen();
2544-
self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, false));
2545-
self.pclose();
2546-
2547-
self.print_fn_output(decl)
2548-
}
2549-
2550-
crate fn print_fn_block_params(&mut self, decl: &ast::FnDecl) {
2551-
self.s.word("|");
2552-
self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, true));
2553-
self.s.word("|");
2554-
2555-
if let ast::FunctionRetTy::Default(..) = decl.output {
2556-
return;
2557-
}
2558-
2559-
self.space_if_not_bol();
2560-
self.word_space("->");
2561-
match decl.output {
2562-
ast::FunctionRetTy::Ty(ref ty) => {
2563-
self.print_type(ty);
2564-
self.maybe_print_comment(ty.span.lo())
2565-
}
2566-
ast::FunctionRetTy::Default(..) => unreachable!(),
2567-
}
2535+
crate fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) {
2536+
let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") };
2537+
self.word(open);
2538+
self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure));
2539+
self.word(close);
2540+
self.print_fn_ret_ty(&decl.output)
25682541
}
25692542

25702543
crate fn print_movability(&mut self, movability: ast::Movability) {
@@ -2786,24 +2759,14 @@ impl<'a> State<'a> {
27862759
self.end();
27872760
}
27882761

2789-
crate fn print_fn_output(&mut self, decl: &ast::FnDecl) {
2790-
if let ast::FunctionRetTy::Default(..) = decl.output {
2791-
return;
2792-
}
2793-
2794-
self.space_if_not_bol();
2795-
self.ibox(INDENT_UNIT);
2796-
self.word_space("->");
2797-
match decl.output {
2798-
ast::FunctionRetTy::Default(..) => unreachable!(),
2799-
ast::FunctionRetTy::Ty(ref ty) =>
2800-
self.print_type(ty),
2801-
}
2802-
self.end();
2803-
2804-
match decl.output {
2805-
ast::FunctionRetTy::Ty(ref output) => self.maybe_print_comment(output.span.lo()),
2806-
_ => {}
2762+
crate fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FunctionRetTy) {
2763+
if let ast::FunctionRetTy::Ty(ty) = fn_ret_ty {
2764+
self.space_if_not_bol();
2765+
self.ibox(INDENT_UNIT);
2766+
self.word_space("->");
2767+
self.print_type(ty);
2768+
self.end();
2769+
self.maybe_print_comment(ty.span.lo());
28072770
}
28082771
}
28092772

src/libsyntax/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ pub fn walk_generic_args<'a, V>(visitor: &mut V,
420420
}
421421
GenericArgs::Parenthesized(ref data) => {
422422
walk_list!(visitor, visit_ty, &data.inputs);
423-
walk_list!(visitor, visit_ty, &data.output);
423+
walk_fn_ret_ty(visitor, &data.output);
424424
}
425425
}
426426
}

0 commit comments

Comments
 (0)