Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print attributes in print_arg #63212

Merged
merged 3 commits into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2639,27 +2639,23 @@ impl<'a> State<'a> {
self.s.word("<");

self.commasep(Inconsistent, &generic_params, |s, param| {
s.print_outer_attributes_inline(&param.attrs);

match param.kind {
ast::GenericParamKind::Lifetime => {
s.print_outer_attributes_inline(&param.attrs);
let lt = ast::Lifetime { id: param.id, ident: param.ident };
s.print_lifetime_bounds(lt, &param.bounds)
}
ast::GenericParamKind::Type { ref default } => {
s.print_outer_attributes_inline(&param.attrs);
s.print_ident(param.ident);
s.print_type_bounds(":", &param.bounds);
match default {
Some(ref default) => {
s.s.space();
s.word_space("=");
s.print_type(default)
}
_ => {}
if let Some(ref default) = default {
s.s.space();
s.word_space("=");
s.print_type(default)
}
}
ast::GenericParamKind::Const { ref ty } => {
s.print_outer_attributes_inline(&param.attrs);
s.word_space("const");
s.print_ident(param.ident);
s.s.space();
Expand Down Expand Up @@ -2760,6 +2756,9 @@ impl<'a> State<'a> {

crate fn print_arg(&mut self, input: &ast::Arg, is_closure: bool) {
self.ibox(INDENT_UNIT);

self.print_outer_attributes_inline(&input.attrs);

match input.ty.node {
ast::TyKind::Infer if is_closure => self.print_pat(&input.pat),
_ => {
Expand Down
34 changes: 34 additions & 0 deletions src/test/ui/rfc-2565-param-attrs/auxiliary/param-attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// force-host
// no-prefer-dynamic

#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

macro_rules! checker {
($attr_name:ident, $expected:literal) => {
#[proc_macro_attribute]
pub fn $attr_name(attr: TokenStream, input: TokenStream) -> TokenStream {
assert!(attr.to_string().is_empty());
assert_eq!(input.to_string(), $expected);
TokenStream::new()
}
}
}

checker!(attr_extern, r#"extern "C" {
fn ffi(#[a1] arg1: i32, #[a2] ...);
}"#);
checker!(attr_extern_cvar, r#"unsafe extern "C" fn cvar(arg1: i32, #[a1] mut args: ...) { }"#);
checker!(attr_alias, "type Alias = fn(#[a1] u8, #[a2] ...);");
checker!(attr_free, "fn free(#[a1] arg1: u8) { let lam = |#[a2] W(x), #[a3] y| (); }");
checker!(attr_inherent_1, "fn inherent1(#[a1] self, #[a2] arg1: u8) { }");
checker!(attr_inherent_2, "fn inherent2(#[a1] &self, #[a2] arg1: u8) { }");
checker!(attr_inherent_3, "fn inherent3<'a>(#[a1] &'a mut self, #[a2] arg1: u8) { }");
checker!(attr_inherent_4, "fn inherent4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8) { }");
checker!(attr_trait_1, "fn trait1(#[a1] self, #[a2] arg1: u8);");
checker!(attr_trait_2, "fn trait2(#[a1] &self, #[a2] arg1: u8);");
checker!(attr_trait_3, "fn trait3<'a>(#[a1] &'a mut self, #[a2] arg1: u8);");
checker!(attr_trait_4, "fn trait4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8, #[a3] Vec<u8>);");
56 changes: 56 additions & 0 deletions src/test/ui/rfc-2565-param-attrs/param-attrs-pretty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// aux-build:param-attrs.rs

// check-pass

#![feature(param_attrs)]
#![feature(c_variadic)]

extern crate param_attrs;

use param_attrs::*;

struct W(u8);

#[attr_extern]
extern "C" { fn ffi(#[a1] arg1: i32, #[a2] ...); }

#[attr_extern_cvar]
unsafe extern "C" fn cvar(arg1: i32, #[a1] mut args: ...) {}

#[attr_alias]
type Alias = fn(#[a1] u8, #[a2] ...);

#[attr_free]
fn free(#[a1] arg1: u8) {
let lam = |#[a2] W(x), #[a3] y| ();
}

impl W {
#[attr_inherent_1]
fn inherent1(#[a1] self, #[a2] arg1: u8) {}

#[attr_inherent_2]
fn inherent2(#[a1] &self, #[a2] arg1: u8) {}

#[attr_inherent_3]
fn inherent3<'a>(#[a1] &'a mut self, #[a2] arg1: u8) {}

#[attr_inherent_4]
fn inherent4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8) {}
}

trait A {
#[attr_trait_1]
fn trait1(#[a1] self, #[a2] arg1: u8);

#[attr_trait_2]
fn trait2(#[a1] &self, #[a2] arg1: u8);

#[attr_trait_3]
fn trait3<'a>(#[a1] &'a mut self, #[a2] arg1: u8);

#[attr_trait_4]
fn trait4<'a>(#[a1] self: Box<Self>, #[a2] arg1: u8, #[a3] Vec<u8>);
}

fn main() {}