From 8696dd94aff3e6be441e90f14b0fc8a5830c9e9e Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 6 Feb 2020 21:04:01 -0800 Subject: [PATCH] Make references to core types absolute in codegen. Prior to this commit, codegen emitted tokens containing bare types like 'Result' and 'Box' as well as presumed imported variants such as 'None' and 'Ok'. However, users are free to shadow these, and if they do, the generated code will fail to compile, or worse, be incorrect. To avoid this, this commit makes all references to these core types and imports absolute. --- core/codegen/src/attribute/catch.rs | 4 ++-- core/codegen/src/attribute/route.rs | 4 ++-- core/codegen/src/derive/from_form_value.rs | 3 ++- core/codegen/src/derive/responder.rs | 6 +++--- core/codegen/src/lib.rs | 7 +++++++ 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/core/codegen/src/attribute/catch.rs b/core/codegen/src/attribute/catch.rs index 71d09455..ec8846ba 100644 --- a/core/codegen/src/attribute/catch.rs +++ b/core/codegen/src/attribute/catch.rs @@ -51,7 +51,7 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result { let status_code = status.0.code; // Variables names we'll use and reuse. - define_vars_and_mods!(req, catcher, Request, Response, ErrorHandlerFuture); + define_vars_and_mods!(req, catcher, _Box, Request, Response, ErrorHandlerFuture); // Determine the number of parameters that will be passed in. let (_fn_sig, inputs) = match catch.function.sig.inputs.len() { @@ -97,7 +97,7 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result { /// Rocket code generated wrapping catch function. #[doc(hidden)] #vis fn #generated_fn_name<'_b>(#req: &'_b #Request) -> #ErrorHandlerFuture<'_b> { - Box::pin(async move { + #_Box::pin(async move { let __response = #catcher_response; #Response::build() .status(#status) diff --git a/core/codegen/src/attribute/route.rs b/core/codegen/src/attribute/route.rs index c98d401e..1bb7d1d3 100644 --- a/core/codegen/src/attribute/route.rs +++ b/core/codegen/src/attribute/route.rs @@ -413,7 +413,7 @@ fn codegen_route(route: Route) -> Result { } // Gather everything we need. - define_vars_and_mods!(req, data, Request, Data, StaticRouteInfo, HandlerFuture); + define_vars_and_mods!(req, data, _Box, Request, Data, StaticRouteInfo, HandlerFuture); let (vis, user_handler_fn) = (&route.function.vis, &route.function); let user_handler_fn_name = &user_handler_fn.sig.ident; let generated_fn_name = user_handler_fn_name.prepend(ROUTE_FN_PREFIX); @@ -435,7 +435,7 @@ fn codegen_route(route: Route) -> Result { #req: &'_b #Request, #data: #Data ) -> #HandlerFuture<'_b> { - Box::pin(async move { + #_Box::pin(async move { #(#req_guard_definitions)* #(#parameter_definitions)* #data_stmt diff --git a/core/codegen/src/derive/from_form_value.rs b/core/codegen/src/derive/from_form_value.rs index f83c7383..aa796d63 100644 --- a/core/codegen/src/derive/from_form_value.rs +++ b/core/codegen/src/derive/from_form_value.rs @@ -38,7 +38,8 @@ pub fn derive_from_form_value(input: TokenStream) -> TokenStream { } }) .try_map_enum(null_enum_mapper) - .try_map_variant(move |_, variant| { + .try_map_variant(|_, variant| { + define_vars_and_mods!(_Ok); let variant_str = Form::from_attrs("form", &variant.attrs) .unwrap_or_else(|| Ok(Form { value: variant.ident.to_string() }))? .value; diff --git a/core/codegen/src/derive/responder.rs b/core/codegen/src/derive/responder.rs index d80c86c4..0067f8e8 100644 --- a/core/codegen/src/derive/responder.rs +++ b/core/codegen/src/derive/responder.rs @@ -40,7 +40,7 @@ pub fn derive_responder(input: TokenStream) -> TokenStream { } }) .try_map_fields(|_, fields| { - define_vars_and_mods!(_Ok); + define_vars_and_mods!(_Ok, _Box); fn set_header_tokens(item: T) -> TokenStream2 { quote_spanned!(item.span().into() => __res.set_header(#item);) } @@ -73,12 +73,12 @@ pub fn derive_responder(input: TokenStream) -> TokenStream { }); Ok(quote! { - Box::pin(async move { + #_Box::pin(async move { #responder #(#headers)* #content_type #status - Ok(__res) + #_Ok(__res) }) }) }) diff --git a/core/codegen/src/lib.rs b/core/codegen/src/lib.rs index 1e88de22..29392020 100644 --- a/core/codegen/src/lib.rs +++ b/core/codegen/src/lib.rs @@ -97,6 +97,13 @@ vars_and_mods! { SmallVec => rocket::http::private::SmallVec, HandlerFuture => rocket::handler::HandlerFuture, ErrorHandlerFuture => rocket::handler::ErrorHandlerFuture, + _Option => ::std::option::Option, + _Result => ::std::result::Result, + _Some => ::std::option::Option::Some, + _None => ::std::option::Option::None, + _Ok => ::std::result::Result::Ok, + _Err => ::std::result::Result::Err, + _Box => ::std::boxed::Box, } macro_rules! define_vars_and_mods {