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

Should warn or error on unregistered handlers #114

Closed
david-crespo opened this issue Jun 2, 2021 · 3 comments · Fixed by #116
Closed

Should warn or error on unregistered handlers #114

david-crespo opened this issue Jun 2, 2021 · 3 comments · Fixed by #116
Assignees

Comments

@david-crespo
Copy link
Contributor

If you write an endpoint function and forget to api.register it, the server will 404 on those routes, but it's not easy to figure out the problem. It would be helpful if there was a compiler error or warning for unregistered route handlers.

@ahl ahl self-assigned this Jun 2, 2021
@ahl
Copy link
Collaborator

ahl commented Jun 3, 2021

When I suggested filing this, I thought fixing it would be pretty straightforward. As it turns out, it's not straightforward to cause normal unused warnings from items generated from proc macros. I asked in discord so perhaps I'll track down someone who knows: https://discord.com/channels/273534239310479360/512792629516173323/849808892103229521

@dtolnay if you can hear me, I'd love your input: with in a proc_macro we generate a token stream that includes this:

const FOO: FooType = FooType {};

We'd ideally like it to be a warning if the program never uses FOO.

@dtolnay
Copy link
Contributor

dtolnay commented Jun 3, 2021

Seems like rustc throws out the warning if it's spanned with call_site (not sure why).

Tested with:

// src/lib.rs

use proc_macro::TokenStream;
use proc_macro2::{Ident, Punct, Spacing};
use quote::{quote, quote_spanned};
use syn::{parse_macro_input, ItemFn};

#[proc_macro_attribute]
pub fn ahl0(_args: TokenStream, _input: TokenStream) -> TokenStream {
    TokenStream::from(quote! {
        const FOO: () = ();
    })
}

#[proc_macro_attribute]
pub fn ahl1(_args: TokenStream, input: TokenStream) -> TokenStream {
    let item = parse_macro_input!(input as ItemFn);
    let span = item.sig.ident.span();

    TokenStream::from(quote_spanned! {span=>
        const FOO: () = ();
    })
}

#[proc_macro_attribute]
pub fn ahl2(_args: TokenStream, input: TokenStream) -> TokenStream {
    let mut iter = input.into_iter();
    let begin = iter.next().unwrap().span();
    let end = iter.last().unwrap().span();
    let const_token = Ident::new("const", begin.into());
    let mut semi_token = Punct::new(';', Spacing::Alone);
    semi_token.set_span(end.into());

    TokenStream::from(quote! {
        #const_token FOO: () = () #semi_token
    })
}
// src/main.rs

pub mod f {
    #[repro::ahl0]
    pub fn test() {}
}

pub mod g {
    #[repro::ahl1]
    pub fn test() {}
}

pub mod h {
    #[repro::ahl2]
    pub fn test() {}
}

fn main() {}
warning: constant is never used: `FOO`
 --> src/main.rs:8:12
  |
8 |     pub fn test() {}
  |            ^^^^

warning: constant is never used: `FOO`
  --> src/main.rs:13:5
   |
13 |     pub fn test() {}
   |     ^^^^^^^^^^^^^^^^

@ahl
Copy link
Collaborator

ahl commented Jun 4, 2021

Thanks @dtolnay and @Kestrer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants