From 2c1dbed4aa049d5bb5b7d83d3c856519ee27a1dc Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 25 Oct 2023 01:05:26 +0200 Subject: [PATCH 1/2] Print variadic argument pattern in HIR pretty printer --- compiler/rustc_hir_pretty/src/lib.rs | 19 ++++++++++++------- tests/pretty/hir-fn-variadic.pp | 15 +++++++++++++++ tests/pretty/hir-fn-variadic.rs | 13 +++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 tests/pretty/hir-fn-variadic.pp create mode 100644 tests/pretty/hir-fn-variadic.rs diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index ab6ab391484..1ce6bb6ca15 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -502,13 +502,13 @@ fn print_item(&mut self, item: &hir::Item<'_>) { 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), ); @@ -1948,11 +1948,10 @@ fn print_fn( 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(":"); @@ -1963,11 +1962,17 @@ fn print_fn( 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(); diff --git a/tests/pretty/hir-fn-variadic.pp b/tests/pretty/hir-fn-variadic.pp new file mode 100644 index 00000000000..d4fadb254a6 --- /dev/null +++ b/tests/pretty/hir-fn-variadic.pp @@ -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::() } diff --git a/tests/pretty/hir-fn-variadic.rs b/tests/pretty/hir-fn-variadic.rs new file mode 100644 index 00000000000..efb2754df62 --- /dev/null +++ b/tests/pretty/hir-fn-variadic.rs @@ -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::() +} From 6aead74ff276d249c2461962ebc3bad3ad93c955 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:44:17 +0200 Subject: [PATCH 2/2] Remove unnecessary CVarArgs name skipping logic --- compiler/rustc_ast_lowering/src/lib.rs | 9 +-------- tests/pretty/hir-fn-variadic.pp | 2 +- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 1e51449e70c..f48c5fc1af5 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1737,14 +1737,7 @@ fn lower_opaque_inner( } fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] { - // Skip the `...` (`CVarArgs`) trailing arguments from the AST, - // 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 { + self.arena.alloc_from_iter(decl.inputs.iter().map(|param| match param.pat.kind { PatKind::Ident(_, ident, _) => self.lower_ident(ident), _ => Ident::new(kw::Empty, self.lower_span(param.pat.span)), })) diff --git a/tests/pretty/hir-fn-variadic.pp b/tests/pretty/hir-fn-variadic.pp index d4fadb254a6..577d9400ad4 100644 --- a/tests/pretty/hir-fn-variadic.pp +++ b/tests/pretty/hir-fn-variadic.pp @@ -9,7 +9,7 @@ use ::std::prelude::rust_2015::*; extern crate std; extern "C" { - fn foo(x: i32, ...); + fn foo(x: i32, va1: ...); } unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize { va2.arg::() }