Skip to content

Commit f6d8977

Browse files
authored
Rollup merge of #63212 - Centril:param-attrs-pretty, r=davidtwco
Pretty print attributes in `print_arg` Fixes #63210. cc #60406 r? @petrochenkov
2 parents 109b21f + d1c89d6 commit f6d8977

File tree

3 files changed

+99
-10
lines changed

3 files changed

+99
-10
lines changed

Diff for: src/libsyntax/print/pprust.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -2622,27 +2622,23 @@ impl<'a> State<'a> {
26222622
self.s.word("<");
26232623

26242624
self.commasep(Inconsistent, &generic_params, |s, param| {
2625+
s.print_outer_attributes_inline(&param.attrs);
2626+
26252627
match param.kind {
26262628
ast::GenericParamKind::Lifetime => {
2627-
s.print_outer_attributes_inline(&param.attrs);
26282629
let lt = ast::Lifetime { id: param.id, ident: param.ident };
26292630
s.print_lifetime_bounds(lt, &param.bounds)
26302631
}
26312632
ast::GenericParamKind::Type { ref default } => {
2632-
s.print_outer_attributes_inline(&param.attrs);
26332633
s.print_ident(param.ident);
26342634
s.print_type_bounds(":", &param.bounds);
2635-
match default {
2636-
Some(ref default) => {
2637-
s.s.space();
2638-
s.word_space("=");
2639-
s.print_type(default)
2640-
}
2641-
_ => {}
2635+
if let Some(ref default) = default {
2636+
s.s.space();
2637+
s.word_space("=");
2638+
s.print_type(default)
26422639
}
26432640
}
26442641
ast::GenericParamKind::Const { ref ty } => {
2645-
s.print_outer_attributes_inline(&param.attrs);
26462642
s.word_space("const");
26472643
s.print_ident(param.ident);
26482644
s.s.space();
@@ -2743,6 +2739,9 @@ impl<'a> State<'a> {
27432739

27442740
crate fn print_arg(&mut self, input: &ast::Arg, is_closure: bool) {
27452741
self.ibox(INDENT_UNIT);
2742+
2743+
self.print_outer_attributes_inline(&input.attrs);
2744+
27462745
match input.ty.node {
27472746
ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
27482747
_ => {
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
8+
use proc_macro::TokenStream;
9+
10+
macro_rules! checker {
11+
($attr_name:ident, $expected:literal) => {
12+
#[proc_macro_attribute]
13+
pub fn $attr_name(attr: TokenStream, input: TokenStream) -> TokenStream {
14+
assert!(attr.to_string().is_empty());
15+
assert_eq!(input.to_string(), $expected);
16+
TokenStream::new()
17+
}
18+
}
19+
}
20+
21+
checker!(attr_extern, r#"extern "C" {
22+
fn ffi(#[a1] arg1: i32, #[a2] ...);
23+
}"#);
24+
checker!(attr_extern_cvar, r#"unsafe extern "C" fn cvar(arg1: i32, #[a1] mut args: ...) { }"#);
25+
checker!(attr_alias, "type Alias = fn(#[a1] u8, #[a2] ...);");
26+
checker!(attr_free, "fn free(#[a1] arg1: u8) { let lam = |#[a2] W(x), #[a3] y| (); }");
27+
checker!(attr_inherent_1, "fn inherent1(#[a1] self, #[a2] arg1: u8) { }");
28+
checker!(attr_inherent_2, "fn inherent2(#[a1] &self, #[a2] arg1: u8) { }");
29+
checker!(attr_inherent_3, "fn inherent3<'a>(#[a1] &'a mut self, #[a2] arg1: u8) { }");
30+
checker!(attr_inherent_4, "fn inherent4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8) { }");
31+
checker!(attr_trait_1, "fn trait1(#[a1] self, #[a2] arg1: u8);");
32+
checker!(attr_trait_2, "fn trait2(#[a1] &self, #[a2] arg1: u8);");
33+
checker!(attr_trait_3, "fn trait3<'a>(#[a1] &'a mut self, #[a2] arg1: u8);");
34+
checker!(attr_trait_4, "fn trait4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8, #[a3] Vec<u8>);");
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// aux-build:param-attrs.rs
2+
3+
// check-pass
4+
5+
#![feature(param_attrs)]
6+
#![feature(c_variadic)]
7+
8+
extern crate param_attrs;
9+
10+
use param_attrs::*;
11+
12+
struct W(u8);
13+
14+
#[attr_extern]
15+
extern "C" { fn ffi(#[a1] arg1: i32, #[a2] ...); }
16+
17+
#[attr_extern_cvar]
18+
unsafe extern "C" fn cvar(arg1: i32, #[a1] mut args: ...) {}
19+
20+
#[attr_alias]
21+
type Alias = fn(#[a1] u8, #[a2] ...);
22+
23+
#[attr_free]
24+
fn free(#[a1] arg1: u8) {
25+
let lam = |#[a2] W(x), #[a3] y| ();
26+
}
27+
28+
impl W {
29+
#[attr_inherent_1]
30+
fn inherent1(#[a1] self, #[a2] arg1: u8) {}
31+
32+
#[attr_inherent_2]
33+
fn inherent2(#[a1] &self, #[a2] arg1: u8) {}
34+
35+
#[attr_inherent_3]
36+
fn inherent3<'a>(#[a1] &'a mut self, #[a2] arg1: u8) {}
37+
38+
#[attr_inherent_4]
39+
fn inherent4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8) {}
40+
}
41+
42+
trait A {
43+
#[attr_trait_1]
44+
fn trait1(#[a1] self, #[a2] arg1: u8);
45+
46+
#[attr_trait_2]
47+
fn trait2(#[a1] &self, #[a2] arg1: u8);
48+
49+
#[attr_trait_3]
50+
fn trait3<'a>(#[a1] &'a mut self, #[a2] arg1: u8);
51+
52+
#[attr_trait_4]
53+
fn trait4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8, #[a3] Vec<u8>);
54+
}
55+
56+
fn main() {}

0 commit comments

Comments
 (0)