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

Support marking handler functions as deprecated #2262

Closed
matt-phylum opened this issue Jul 8, 2022 · 0 comments
Closed

Support marking handler functions as deprecated #2262

matt-phylum opened this issue Jul 8, 2022 · 0 comments
Labels
suggestion A suggestion to change functionality

Comments

@matt-phylum
Copy link

Existing Functionality

The following code generates a warning about using a deprecated function.

use rocket::{get, Build, Rocket, routes};

#[get("/")]
#[deprecated]
fn handler_without_docs() -> &'static str {
    "ok"
}

#[rocket::launch]
#[allow(deprecated)]
fn launch() -> Rocket<Build> {
    rocket::build()
        .mount("/", routes![handler_without_docs])
}

The deprecation warning is emitted on line 5 where the function is declared. This appears to be because Rocket's code generation is producing a type which has a function that calls the handler. This doesn't seem useful. The warning is emitted whenever a deprecated handler exists, whether it's used or not.

Annoyingly, adding #[allow(deprecated)] to the handler function has no effect on this warning. That allows the handler function to call deprecated functions, but has no effect on the generated struct calling the function.

For contrast, the following code does not generate a warning about using a deprecated function even though it looks like it should.

use rocket::{get, Build, Rocket, routes};

#[allow(deprecated)]
mod handlers {
    use super::*;

    #[get("/")]
    #[deprecated]
    pub fn handler_without_docs() -> &'static str {
        "ok"
    }
}

#[rocket::launch]
fn launch() -> Rocket<Build> {
    rocket::build()
        .mount("/", routes![handlers::handler_without_docs])
}

Suggested Changes

When #[get] or similar is used on a deprecated function,

  1. the generated struct has #[allow(deprecated)] where it uses the handler function so a warning is not emitted where the function is declared.
  2. the generated struct may have #[deprecated] itself, such that mounting the route generates a deprecated warning. I'm not sure if this is useful, but it seems like an expected behavior since the user is using the struct as if it were the function and the function is deprecated.

Additional Context

This would allow crates like rocket_okapi to use the #[deprecated] attribute for setting the deprecated property on the API spec.

@matt-phylum matt-phylum added the suggestion A suggestion to change functionality label Jul 8, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
suggestion A suggestion to change functionality
Projects
None yet
Development

No branches or pull requests

1 participant