Rollup merge of #117147 - DaniPopes:pphir-fn-variadic, r=compiler-errors
Print variadic argument pattern in HIR pretty printer Variadic argument name/pattern was ignored during HIR pretty printing. Could not figure out why it only works on normal functions (`va2`) and not in foreign ones (`va1`).
This commit is contained in:
commit
2915707622
4 changed files with 41 additions and 15 deletions
|
@ -1742,14 +1742,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] {
|
fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] {
|
||||||
// Skip the `...` (`CVarArgs`) trailing arguments from the AST,
|
self.arena.alloc_from_iter(decl.inputs.iter().map(|param| match param.pat.kind {
|
||||||
// as they are not explicit in HIR/Ty function signatures.
|
|
||||||
// (instead, the `c_variadic` flag is set to `true`)
|
|
||||||
let mut inputs = &decl.inputs[..];
|
|
||||||
if decl.c_variadic() {
|
|
||||||
inputs = &inputs[..inputs.len() - 1];
|
|
||||||
}
|
|
||||||
self.arena.alloc_from_iter(inputs.iter().map(|param| match param.pat.kind {
|
|
||||||
PatKind::Ident(_, ident, _) => self.lower_ident(ident),
|
PatKind::Ident(_, ident, _) => self.lower_ident(ident),
|
||||||
_ => Ident::new(kw::Empty, self.lower_span(param.pat.span)),
|
_ => Ident::new(kw::Empty, self.lower_span(param.pat.span)),
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -502,13 +502,13 @@ impl<'a> State<'a> {
|
||||||
self.word(";");
|
self.word(";");
|
||||||
self.end(); // end the outer cbox
|
self.end(); // end the outer cbox
|
||||||
}
|
}
|
||||||
hir::ItemKind::Fn(ref sig, param_names, body) => {
|
hir::ItemKind::Fn(ref sig, generics, body) => {
|
||||||
self.head("");
|
self.head("");
|
||||||
self.print_fn(
|
self.print_fn(
|
||||||
sig.decl,
|
sig.decl,
|
||||||
sig.header,
|
sig.header,
|
||||||
Some(item.ident.name),
|
Some(item.ident.name),
|
||||||
param_names,
|
generics,
|
||||||
&[],
|
&[],
|
||||||
Some(body),
|
Some(body),
|
||||||
);
|
);
|
||||||
|
@ -1948,11 +1948,10 @@ impl<'a> State<'a> {
|
||||||
self.print_generic_params(generics.params);
|
self.print_generic_params(generics.params);
|
||||||
|
|
||||||
self.popen();
|
self.popen();
|
||||||
let mut i = 0;
|
|
||||||
// Make sure we aren't supplied *both* `arg_names` and `body_id`.
|
// Make sure we aren't supplied *both* `arg_names` and `body_id`.
|
||||||
assert!(arg_names.is_empty() || body_id.is_none());
|
assert!(arg_names.is_empty() || body_id.is_none());
|
||||||
self.commasep(Inconsistent, decl.inputs, |s, ty| {
|
let mut i = 0;
|
||||||
s.ibox(INDENT_UNIT);
|
let mut print_arg = |s: &mut Self| {
|
||||||
if let Some(arg_name) = arg_names.get(i) {
|
if let Some(arg_name) = arg_names.get(i) {
|
||||||
s.word(arg_name.to_string());
|
s.word(arg_name.to_string());
|
||||||
s.word(":");
|
s.word(":");
|
||||||
|
@ -1963,11 +1962,17 @@ impl<'a> State<'a> {
|
||||||
s.space();
|
s.space();
|
||||||
}
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
|
};
|
||||||
|
self.commasep(Inconsistent, decl.inputs, |s, ty| {
|
||||||
|
s.ibox(INDENT_UNIT);
|
||||||
|
print_arg(s);
|
||||||
s.print_type(ty);
|
s.print_type(ty);
|
||||||
s.end()
|
s.end();
|
||||||
});
|
});
|
||||||
if decl.c_variadic {
|
if decl.c_variadic {
|
||||||
self.word(", ...");
|
self.word(", ");
|
||||||
|
print_arg(self);
|
||||||
|
self.word("...");
|
||||||
}
|
}
|
||||||
self.pclose();
|
self.pclose();
|
||||||
|
|
||||||
|
|
15
tests/pretty/hir-fn-variadic.pp
Normal file
15
tests/pretty/hir-fn-variadic.pp
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// pretty-compare-only
|
||||||
|
// pretty-mode:hir
|
||||||
|
// pp-exact:hir-fn-variadic.pp
|
||||||
|
|
||||||
|
#![feature(c_variadic)]
|
||||||
|
#[prelude_import]
|
||||||
|
use ::std::prelude::rust_2015::*;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate std;
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
fn foo(x: i32, va1: ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize { va2.arg::<usize>() }
|
13
tests/pretty/hir-fn-variadic.rs
Normal file
13
tests/pretty/hir-fn-variadic.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// pretty-compare-only
|
||||||
|
// pretty-mode:hir
|
||||||
|
// pp-exact:hir-fn-variadic.pp
|
||||||
|
|
||||||
|
#![feature(c_variadic)]
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
pub fn foo(x: i32, va1: ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize {
|
||||||
|
va2.arg::<usize>()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue