Skip to content

Commit

Permalink
Propagate attrs from extern mod onto all contents
Browse files Browse the repository at this point in the history
    #[cxx::bridge]
    mod ffi {
        #[allow(clippy::too_many_arguments)]
        extern "Rust" {
            fn repro(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32, h: i32) -> bool;
        }
    }

Before:

    warning: this function has too many arguments (8/7)
     --> src/main.rs:5:12
      |
    5 |         fn repro(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32, h: i32) -> bool;
      |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = note: `#[warn(clippy::too_many_arguments)]` on by default
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments

After: no lint.
  • Loading branch information
dtolnay committed Jul 4, 2022
1 parent 66ba9a0 commit 30427e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
5 changes: 5 additions & 0 deletions syntax/attrs.rs
Expand Up @@ -268,12 +268,17 @@ fn parse_rust_name_attribute(input: ParseStream) -> Result<Ident> {
}
}

#[derive(Clone)]
pub struct OtherAttrs(Vec<Attribute>);

impl OtherAttrs {
pub fn none() -> Self {
OtherAttrs(Vec::new())
}

pub fn extend(&mut self, other: Self) {
self.0.extend(other.0);
}
}

impl ToTokens for OtherAttrs {
Expand Down
55 changes: 34 additions & 21 deletions syntax/parse.rs
Expand Up @@ -360,7 +360,7 @@ fn parse_foreign_mod(

let mut cfg = CfgExpr::Unconditional;
let mut namespace = namespace.clone();
attrs::parse(
let attrs = attrs::parse(
cx,
foreign_mod.attrs,
attrs::Parser {
Expand All @@ -374,11 +374,11 @@ fn parse_foreign_mod(
for foreign in foreign_mod.items {
match foreign {
ForeignItem::Type(foreign) => {
let ety = parse_extern_type(cx, foreign, lang, trusted, &cfg, &namespace);
let ety = parse_extern_type(cx, foreign, lang, trusted, &cfg, &namespace, &attrs);
items.push(ety);
}
ForeignItem::Fn(foreign) => {
match parse_extern_fn(cx, foreign, lang, trusted, &cfg, &namespace) {
match parse_extern_fn(cx, foreign, lang, trusted, &cfg, &namespace, &attrs) {
Ok(efn) => items.push(efn),
Err(err) => cx.push(err),
}
Expand All @@ -393,7 +393,7 @@ fn parse_foreign_mod(
}
}
ForeignItem::Verbatim(tokens) => {
match parse_extern_verbatim(cx, tokens, lang, trusted, &cfg, &namespace) {
match parse_extern_verbatim(cx, tokens, lang, trusted, &cfg, &namespace, &attrs) {
Ok(api) => items.push(api),
Err(err) => cx.push(err),
}
Expand Down Expand Up @@ -463,14 +463,16 @@ fn parse_extern_type(
trusted: bool,
extern_block_cfg: &CfgExpr,
namespace: &Namespace,
attrs: &OtherAttrs,
) -> Api {
let mut cfg = extern_block_cfg.clone();
let mut doc = Doc::new();
let mut derives = Vec::new();
let mut namespace = namespace.clone();
let mut cxx_name = None;
let mut rust_name = None;
let attrs = attrs::parse(
let mut attrs = attrs.clone();
attrs.extend(attrs::parse(
cx,
foreign_type.attrs,
attrs::Parser {
Expand All @@ -482,7 +484,7 @@ fn parse_extern_type(
rust_name: Some(&mut rust_name),
..Default::default()
},
);
));

let type_token = foreign_type.type_token;
let visibility = visibility_pub(&foreign_type.vis, type_token.span);
Expand Down Expand Up @@ -523,13 +525,15 @@ fn parse_extern_fn(
trusted: bool,
extern_block_cfg: &CfgExpr,
namespace: &Namespace,
attrs: &OtherAttrs,
) -> Result<Api> {
let mut cfg = extern_block_cfg.clone();
let mut doc = Doc::new();
let mut namespace = namespace.clone();
let mut cxx_name = None;
let mut rust_name = None;
let attrs = attrs::parse(
let mut attrs = attrs.clone();
attrs.extend(attrs::parse(
cx,
mem::take(&mut foreign_fn.attrs),
attrs::Parser {
Expand All @@ -540,7 +544,7 @@ fn parse_extern_fn(
rust_name: Some(&mut rust_name),
..Default::default()
},
);
));

let generics = &foreign_fn.sig.generics;
if generics.where_clause.is_some()
Expand Down Expand Up @@ -708,20 +712,22 @@ fn parse_extern_verbatim(
trusted: bool,
extern_block_cfg: &CfgExpr,
namespace: &Namespace,
attrs: &OtherAttrs,
) -> Result<Api> {
|input: ParseStream| -> Result<Api> {
let attrs = input.call(Attribute::parse_outer)?;
let unparsed_attrs = input.call(Attribute::parse_outer)?;
let visibility: Visibility = input.parse()?;
if input.peek(Token![type]) {
parse_extern_verbatim_type(
cx,
attrs,
unparsed_attrs,
visibility,
input,
lang,
trusted,
extern_block_cfg,
namespace,
attrs,
)
} else if input.peek(Token![fn]) {
parse_extern_verbatim_fn(input)
Expand All @@ -738,13 +744,14 @@ fn parse_extern_verbatim(

fn parse_extern_verbatim_type(
cx: &mut Errors,
attrs: Vec<Attribute>,
unparsed_attrs: Vec<Attribute>,
visibility: Visibility,
input: ParseStream,
lang: Lang,
trusted: bool,
extern_block_cfg: &CfgExpr,
namespace: &Namespace,
attrs: &OtherAttrs,
) -> Result<Api> {
let type_token: Token![type] = input.parse()?;
let ident: Ident = input.parse()?;
Expand Down Expand Up @@ -791,7 +798,7 @@ fn parse_extern_verbatim_type(
// type Alias = crate::path::to::Type;
parse_type_alias(
cx,
attrs,
unparsed_attrs,
visibility,
type_token,
ident,
Expand All @@ -800,12 +807,13 @@ fn parse_extern_verbatim_type(
lang,
extern_block_cfg,
namespace,
attrs,
)
} else if lookahead.peek(Token![:]) || lookahead.peek(Token![;]) {
// type Opaque: Bound2 + Bound2;
parse_extern_type_bounded(
cx,
attrs,
unparsed_attrs,
visibility,
type_token,
ident,
Expand All @@ -815,6 +823,7 @@ fn parse_extern_verbatim_type(
trusted,
extern_block_cfg,
namespace,
attrs,
)
} else {
Err(lookahead.error())
Expand All @@ -829,7 +838,7 @@ fn parse_extern_verbatim_fn(input: ParseStream) -> Result<Api> {

fn parse_type_alias(
cx: &mut Errors,
attrs: Vec<Attribute>,
unparsed_attrs: Vec<Attribute>,
visibility: Visibility,
type_token: Token![type],
ident: Ident,
Expand All @@ -838,6 +847,7 @@ fn parse_type_alias(
lang: Lang,
extern_block_cfg: &CfgExpr,
namespace: &Namespace,
attrs: &OtherAttrs,
) -> Result<Api> {
let eq_token: Token![=] = input.parse()?;
let ty: RustType = input.parse()?;
Expand All @@ -849,9 +859,10 @@ fn parse_type_alias(
let mut namespace = namespace.clone();
let mut cxx_name = None;
let mut rust_name = None;
let attrs = attrs::parse(
let mut attrs = attrs.clone();
attrs.extend(attrs::parse(
cx,
attrs,
unparsed_attrs,
attrs::Parser {
cfg: Some(&mut cfg),
doc: Some(&mut doc),
Expand All @@ -861,7 +872,7 @@ fn parse_type_alias(
rust_name: Some(&mut rust_name),
..Default::default()
},
);
));

if lang == Lang::Rust {
let span = quote!(#type_token #semi_token);
Expand Down Expand Up @@ -889,7 +900,7 @@ fn parse_type_alias(

fn parse_extern_type_bounded(
cx: &mut Errors,
attrs: Vec<Attribute>,
unparsed_attrs: Vec<Attribute>,
visibility: Visibility,
type_token: Token![type],
ident: Ident,
Expand All @@ -899,6 +910,7 @@ fn parse_extern_type_bounded(
trusted: bool,
extern_block_cfg: &CfgExpr,
namespace: &Namespace,
attrs: &OtherAttrs,
) -> Result<Api> {
let mut bounds = Vec::new();
let colon_token: Option<Token![:]> = input.parse()?;
Expand Down Expand Up @@ -939,9 +951,10 @@ fn parse_extern_type_bounded(
let mut namespace = namespace.clone();
let mut cxx_name = None;
let mut rust_name = None;
let attrs = attrs::parse(
let mut attrs = attrs.clone();
attrs.extend(attrs::parse(
cx,
attrs,
unparsed_attrs,
attrs::Parser {
cfg: Some(&mut cfg),
doc: Some(&mut doc),
Expand All @@ -951,7 +964,7 @@ fn parse_extern_type_bounded(
rust_name: Some(&mut rust_name),
..Default::default()
},
);
));

let visibility = visibility_pub(&visibility, type_token.span);
let name = pair(namespace, &ident, cxx_name, rust_name);
Expand Down

0 comments on commit 30427e0

Please sign in to comment.