Skip to content

Ommiting "C" in extern "C" fn in derive input causes spans to get lost. #64561

Closed

Description

For some reason ommiting the "C" in extern "C" fn causes the spans to get lost in error messages.

I've created this repository to demonstrate it,simply git clone it and try to build the using_proc_macro crate.

The proc_macro_crate crate:

extern crate proc_macro;

use proc_macro::TokenStream;

use syn::{
    visit::{self, Visit},
    Lifetime, Type, TypeBareFn, TypeReference,
};


#[proc_macro_derive(ProcMacro)]
pub fn stable_abi(input: TokenStream) -> TokenStream {
    syn::parse::<syn::DeriveInput>(input)
        .and_then(|di|{
            let mut res=Ok(());
            Visitor { err:&mut res }.visit_derive_input(&di);
            res.map(|_| quote::quote!() )
        })
        .unwrap_or_else(|e| e.to_compile_error() )
        .into()
}

pub(crate) struct Visitor<'a> {
    err:&'a mut Result<(),syn::Error>,
}

/////////////

impl<'a> Visit<'_> for Visitor<'a> {
    fn visit_type_reference(&mut self, ref_: &TypeReference) {
        *self.err=Err(syn::Error::new_spanned(ref_,format!("hello from proc macro")));
    }
    fn visit_lifetime(&mut self, lt: &Lifetime) {   
        *self.err=Err(syn::Error::new_spanned(lt,format!("hello from proc macro")));
    }
}

The using_proc_macro crate:

#[derive(proc_macro_crate::ProcMacro)]
struct Hello(
    &'a (),
    extern fn(),
);


#[derive(proc_macro_crate::ProcMacro)]
struct World(
    &'a (),
    extern "C" fn(),
);

The error message

error: hello from proc macro

error: hello from proc macro
  --> src/lib.rs:10:5
   |
10 |     &'a (),
   |     ^^^^^^

The first error is for Hello and the second is for World.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-parserArea: The parsing of Rust source code to an ASTC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions