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.
This commit is contained in:
Sergio Benitez 2020-02-06 21:04:01 -08:00
parent 71b888c2fa
commit 8696dd94af
5 changed files with 16 additions and 8 deletions

View File

@ -51,7 +51,7 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
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<TokenStream> {
/// 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)

View File

@ -413,7 +413,7 @@ fn codegen_route(route: Route) -> Result<TokenStream> {
}
// 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<TokenStream> {
#req: &'_b #Request,
#data: #Data
) -> #HandlerFuture<'_b> {
Box::pin(async move {
#_Box::pin(async move {
#(#req_guard_definitions)*
#(#parameter_definitions)*
#data_stmt

View File

@ -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;

View File

@ -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<T: ToTokens + Spanned>(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)
})
})
})

View File

@ -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 {