Fix span placement on extern functions

This commit is contained in:
David Tolnay 2020-12-30 18:02:38 -08:00
parent 291c9350d6
commit 49f9df7923
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 23 additions and 13 deletions

View File

@ -632,22 +632,26 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
}
};
let mut dispatch = quote!(#setup #expr);
let visibility = efn.visibility;
let unsafety = &efn.sig.unsafety;
if unsafety.is_none() {
dispatch = quote!(unsafe { #dispatch });
}
let fn_token = efn.sig.fn_token;
let ident = &efn.name.rust;
let generics = &efn.generics;
let arg_list = quote_spanned!(efn.sig.paren_token.span=> (#(#all_args,)*));
let fn_body = quote_spanned!(efn.semi_token.span=> {
extern "C" {
#decl
}
#trampolines
#dispatch
});
let function_shim = quote! {
#doc
#attrs
pub #unsafety fn #ident #generics(#(#all_args,)*) #ret {
extern "C" {
#decl
}
#trampolines
#dispatch
}
#visibility #unsafety #fn_token #ident #generics #arg_list #ret #fn_body
};
match &efn.receiver {
None => function_shim,

View File

@ -113,6 +113,7 @@ pub struct ExternFn {
pub lang: Lang,
pub doc: Doc,
pub attrs: OtherAttrs,
pub visibility: Token![pub],
pub name: Pair,
pub sig: Signature,
pub semi_token: Token![;],

View File

@ -597,6 +597,12 @@ fn parse_extern_fn(
let mut throws_tokens = None;
let ret = parse_return_type(&foreign_fn.sig.output, &mut throws_tokens)?;
let throws = throws_tokens.is_some();
let visibility = Token![pub](match foreign_fn.vis {
Visibility::Public(vis) => vis.pub_token.span,
Visibility::Crate(vis) => vis.crate_token.span,
Visibility::Restricted(vis) => vis.pub_token.span,
Visibility::Inherited => foreign_fn.sig.ident.span(),
});
let unsafety = foreign_fn.sig.unsafety;
let fn_token = foreign_fn.sig.fn_token;
let name = pair(namespace, &foreign_fn.sig.ident, cxx_name, rust_name);
@ -611,6 +617,7 @@ fn parse_extern_fn(
lang,
doc,
attrs,
visibility,
name,
sig: Signature {
unsafety,

View File

@ -41,9 +41,7 @@ error: missing documentation for a type alias
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a function
--> $DIR/deny_missing_docs.rs:9:1
|
9 | #[cxx::bridge]
| ^^^^^^^^^^^^^^
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
--> $DIR/deny_missing_docs.rs:54:9
|
54 | pub fn undocumented_foreign_fn() -> u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^