From 1613f830e6501255e41dd0f19dd1517b5b2c538b Mon Sep 17 00:00:00 2001 From: Cobalt Date: Mon, 28 Mar 2022 11:20:44 +0200 Subject: [PATCH 1/5] feat: Add visibilty to codegen of route struct --- actix-web-codegen/src/route.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index cb1ba1ef6..97aae1ec9 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -291,6 +291,7 @@ impl ToTokens for Route { resource_type, doc_attributes, } = self; + let visibility = &ast.vis; let resource_name = resource_name .as_ref() .map_or_else(|| name.to_string(), LitStr::value); @@ -316,7 +317,7 @@ impl ToTokens for Route { let stream = quote! { #(#doc_attributes)* #[allow(non_camel_case_types, missing_docs)] - pub struct #name; + #visibility struct #name; impl ::actix_web::dev::HttpServiceFactory for #name { fn register(self, __config: &mut actix_web::dev::AppService) { From b2cc95292dcee781cb4c5d3c9bd4405d75830726 Mon Sep 17 00:00:00 2001 From: Cobalt Date: Tue, 29 Mar 2022 00:07:39 +0200 Subject: [PATCH 2/5] test: Add trybuild tests for codegen visibility --- actix-web-codegen/tests/trybuild.rs | 3 +++ .../tests/trybuild/visibility-fail.rs | 20 +++++++++++++++++++ .../tests/trybuild/visibility-fail.stderr | 12 +++++++++++ .../tests/trybuild/visibility-ok.rs | 20 +++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 actix-web-codegen/tests/trybuild/visibility-fail.rs create mode 100644 actix-web-codegen/tests/trybuild/visibility-fail.stderr create mode 100644 actix-web-codegen/tests/trybuild/visibility-ok.rs diff --git a/actix-web-codegen/tests/trybuild.rs b/actix-web-codegen/tests/trybuild.rs index b2d9ce186..a39064ad7 100644 --- a/actix-web-codegen/tests/trybuild.rs +++ b/actix-web-codegen/tests/trybuild.rs @@ -6,6 +6,9 @@ fn compile_macros() { t.pass("tests/trybuild/simple.rs"); t.compile_fail("tests/trybuild/simple-fail.rs"); + t.pass("tests/trybuild/visibility-ok.rs"); + t.compile_fail("tests/trybuild/visibility-fail.rs"); + t.pass("tests/trybuild/route-ok.rs"); t.compile_fail("tests/trybuild/route-missing-method-fail.rs"); t.compile_fail("tests/trybuild/route-duplicate-method-fail.rs"); diff --git a/actix-web-codegen/tests/trybuild/visibility-fail.rs b/actix-web-codegen/tests/trybuild/visibility-fail.rs new file mode 100644 index 000000000..2b7cdfa08 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/visibility-fail.rs @@ -0,0 +1,20 @@ +use actix_web::App; + +mod config { + use actix_web_codegen::*; + use actix_web::{Responder, HttpResponse}; + + #[get("/config")] + async fn config() -> impl Responder { + HttpResponse::Ok() + } +} + +#[actix_web::main] +async fn main() { + let srv = actix_test::start(|| App::new().service(config::config)); + + let request = srv.get("/config"); + let response = request.send().await.unwrap(); + assert!(response.status().is_success()); +} diff --git a/actix-web-codegen/tests/trybuild/visibility-fail.stderr b/actix-web-codegen/tests/trybuild/visibility-fail.stderr new file mode 100644 index 000000000..e44acc010 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/visibility-fail.stderr @@ -0,0 +1,12 @@ +error[E0603]: unit struct `config` is private + --> tests/trybuild/visibility-fail.rs:15:63 + | +15 | let srv = actix_test::start(|| App::new().service(config::config)); + | ^^^^^^ private unit struct + | +note: the unit struct `config` is defined here + --> tests/trybuild/visibility-fail.rs:7:5 + | +7 | #[get("/config")] + | ^^^^^^^^^^^^^^^^^ + = note: this error originates in the attribute macro `get` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/actix-web-codegen/tests/trybuild/visibility-ok.rs b/actix-web-codegen/tests/trybuild/visibility-ok.rs new file mode 100644 index 000000000..2f531efe9 --- /dev/null +++ b/actix-web-codegen/tests/trybuild/visibility-ok.rs @@ -0,0 +1,20 @@ +use actix_web::App; + +mod config { + use actix_web_codegen::*; + use actix_web::{Responder, HttpResponse}; + + #[get("/config")] + pub async fn config() -> impl Responder { + HttpResponse::Ok() + } +} + +#[actix_web::main] +async fn main() { + let srv = actix_test::start(|| App::new().service(config::config)); + + let request = srv.get("/config"); + let response = request.send().await.unwrap(); + assert!(response.status().is_success()); +} From 0efafe03979de1b0f761437c5f94fb9eab46cb63 Mon Sep 17 00:00:00 2001 From: Cobalt Date: Tue, 29 Mar 2022 00:19:59 +0200 Subject: [PATCH 3/5] chore: Add changelog entry for PR --- actix-web-codegen/CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index 8ee787c0a..f1f39f92a 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -1,7 +1,7 @@ # Changes ## Unreleased - 2021-xx-xx - +- Preserve function visibility for routing macros ## 4.0.0 - 2022-02-24 - Version aligned with `actix-web` and will remain in sync going forward. From 90aa578e77e71a9e5c0456e66488a9b0a2eea19b Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 28 Mar 2022 23:21:46 +0100 Subject: [PATCH 4/5] Update CHANGES.md --- actix-web-codegen/CHANGES.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/actix-web-codegen/CHANGES.md b/actix-web-codegen/CHANGES.md index f1f39f92a..63595d923 100644 --- a/actix-web-codegen/CHANGES.md +++ b/actix-web-codegen/CHANGES.md @@ -1,7 +1,10 @@ # Changes ## Unreleased - 2021-xx-xx -- Preserve function visibility for routing macros +- Preserve function visibility for routing macros. [#2714] + +[#2714]: https://github.com/actix/actix-web/pull/2714 + ## 4.0.0 - 2022-02-24 - Version aligned with `actix-web` and will remain in sync going forward. From e9ca0b82968e1716dd09ff99631288f8906ea184 Mon Sep 17 00:00:00 2001 From: Cobalt Date: Tue, 29 Mar 2022 00:22:29 +0200 Subject: [PATCH 5/5] docs: Add doc comment about visibility inheritance --- actix-web-codegen/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index 5ca5616b6..e937bd161 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -19,7 +19,8 @@ //! //! # Single Method Handler //! There is a macro to set up a handler for each of the most common HTTP methods that also define -//! additional guards and route-specific middleware. +//! additional guards and route-specific middleware. This macros will inherit the visibility +//! of the underlying handler. //! //! See docs for: [GET], [POST], [PATCH], [PUT], [DELETE], [HEAD], [CONNECT], [OPTIONS], [TRACE] //!