Skip to content

Commit

Permalink
Print variadic argument pattern in HIR pretty printer
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Oct 25, 2023
1 parent b66fe58 commit 2c1dbed
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
19 changes: 12 additions & 7 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,13 @@ impl<'a> State<'a> {
self.word(";");
self.end(); // end the outer cbox
}
hir::ItemKind::Fn(ref sig, param_names, body) => {
hir::ItemKind::Fn(ref sig, generics, body) => {
self.head("");
self.print_fn(
sig.decl,
sig.header,
Some(item.ident.name),
param_names,
generics,
&[],
Some(body),
);
Expand Down Expand Up @@ -1948,11 +1948,10 @@ impl<'a> State<'a> {
self.print_generic_params(generics.params);

self.popen();
let mut i = 0;
// Make sure we aren't supplied *both* `arg_names` and `body_id`.
assert!(arg_names.is_empty() || body_id.is_none());
self.commasep(Inconsistent, decl.inputs, |s, ty| {
s.ibox(INDENT_UNIT);
let mut i = 0;
let mut print_arg = |s: &mut Self| {
if let Some(arg_name) = arg_names.get(i) {
s.word(arg_name.to_string());
s.word(":");
Expand All @@ -1963,11 +1962,17 @@ impl<'a> State<'a> {
s.space();
}
i += 1;
};
self.commasep(Inconsistent, decl.inputs, |s, ty| {
s.ibox(INDENT_UNIT);
print_arg(s);
s.print_type(ty);
s.end()
s.end();
});
if decl.c_variadic {
self.word(", ...");
self.word(", ");
print_arg(self);
self.word("...");
}
self.pclose();

Expand Down
15 changes: 15 additions & 0 deletions tests/pretty/hir-fn-variadic.pp
Original file line number Diff line number Diff line change
@@ -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, ...);
}

unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize { va2.arg::<usize>() }
13 changes: 13 additions & 0 deletions tests/pretty/hir-fn-variadic.rs
Original file line number Diff line number Diff line change
@@ -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>()
}

0 comments on commit 2c1dbed

Please sign in to comment.