Skip to content

Commit 2915707

Browse files
authored
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`).
2 parents 91bbdd9 + 6aead74 commit 2915707

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1742,14 +1742,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17421742
}
17431743

17441744
fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] {
1745-
// Skip the `...` (`CVarArgs`) trailing arguments from the AST,
1746-
// as they are not explicit in HIR/Ty function signatures.
1747-
// (instead, the `c_variadic` flag is set to `true`)
1748-
let mut inputs = &decl.inputs[..];
1749-
if decl.c_variadic() {
1750-
inputs = &inputs[..inputs.len() - 1];
1751-
}
1752-
self.arena.alloc_from_iter(inputs.iter().map(|param| match param.pat.kind {
1745+
self.arena.alloc_from_iter(decl.inputs.iter().map(|param| match param.pat.kind {
17531746
PatKind::Ident(_, ident, _) => self.lower_ident(ident),
17541747
_ => Ident::new(kw::Empty, self.lower_span(param.pat.span)),
17551748
}))

compiler/rustc_hir_pretty/src/lib.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ impl<'a> State<'a> {
502502
self.word(";");
503503
self.end(); // end the outer cbox
504504
}
505-
hir::ItemKind::Fn(ref sig, param_names, body) => {
505+
hir::ItemKind::Fn(ref sig, generics, body) => {
506506
self.head("");
507507
self.print_fn(
508508
sig.decl,
509509
sig.header,
510510
Some(item.ident.name),
511-
param_names,
511+
generics,
512512
&[],
513513
Some(body),
514514
);
@@ -1948,11 +1948,10 @@ impl<'a> State<'a> {
19481948
self.print_generic_params(generics.params);
19491949

19501950
self.popen();
1951-
let mut i = 0;
19521951
// Make sure we aren't supplied *both* `arg_names` and `body_id`.
19531952
assert!(arg_names.is_empty() || body_id.is_none());
1954-
self.commasep(Inconsistent, decl.inputs, |s, ty| {
1955-
s.ibox(INDENT_UNIT);
1953+
let mut i = 0;
1954+
let mut print_arg = |s: &mut Self| {
19561955
if let Some(arg_name) = arg_names.get(i) {
19571956
s.word(arg_name.to_string());
19581957
s.word(":");
@@ -1963,11 +1962,17 @@ impl<'a> State<'a> {
19631962
s.space();
19641963
}
19651964
i += 1;
1965+
};
1966+
self.commasep(Inconsistent, decl.inputs, |s, ty| {
1967+
s.ibox(INDENT_UNIT);
1968+
print_arg(s);
19661969
s.print_type(ty);
1967-
s.end()
1970+
s.end();
19681971
});
19691972
if decl.c_variadic {
1970-
self.word(", ...");
1973+
self.word(", ");
1974+
print_arg(self);
1975+
self.word("...");
19711976
}
19721977
self.pclose();
19731978

tests/pretty/hir-fn-variadic.pp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// pretty-compare-only
2+
// pretty-mode:hir
3+
// pp-exact:hir-fn-variadic.pp
4+
5+
#![feature(c_variadic)]
6+
#[prelude_import]
7+
use ::std::prelude::rust_2015::*;
8+
#[macro_use]
9+
extern crate std;
10+
11+
extern "C" {
12+
fn foo(x: i32, va1: ...);
13+
}
14+
15+
unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize { va2.arg::<usize>() }

tests/pretty/hir-fn-variadic.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// pretty-compare-only
2+
// pretty-mode:hir
3+
// pp-exact:hir-fn-variadic.pp
4+
5+
#![feature(c_variadic)]
6+
7+
extern "C" {
8+
pub fn foo(x: i32, va1: ...);
9+
}
10+
11+
pub unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize {
12+
va2.arg::<usize>()
13+
}

0 commit comments

Comments
 (0)