Emit a dummy 'Origin' in failed 'uri!' expansions.

This significantly reduces the number of spurious, fallout compiler
errors when the 'uri!' macros fails.
This commit is contained in:
Sergio Benitez 2021-04-07 19:49:30 -07:00
parent 3535d08ff8
commit fd03417188
2 changed files with 14 additions and 9 deletions

View File

@ -41,7 +41,9 @@ pub fn catchers_macro(input: proc_macro::TokenStream) -> TokenStream {
pub fn uri_macro(input: proc_macro::TokenStream) -> TokenStream {
uri::_uri_macro(input.into())
.unwrap_or_else(|diag| diag.emit_as_expr_tokens())
.unwrap_or_else(|diag| diag.emit_as_expr_tokens_or(quote! {
rocket::http::uri::Origin::dummy()
}))
}
pub fn uri_internal_macro(input: proc_macro::TokenStream) -> TokenStream {
@ -50,13 +52,10 @@ pub fn uri_internal_macro(input: proc_macro::TokenStream) -> TokenStream {
// invocation of `uri!` without access to `span.parent()`, and
// `Span::call_site()` here points to the `#[route]`, immediate caller,
// generate a rather confusing error message when there's a type-mismatch.
// uri::_uri_internal_macro(input.into())
// .unwrap_or_else(|diag| diag.emit_as_expr_tokens_or(quote_spanned! { span =>
// rocket::http::uri::Origin::dummy()
// }))
uri::_uri_internal_macro(input.into())
.unwrap_or_else(|diag| diag.emit_as_expr_tokens())
.unwrap_or_else(|diag| diag.emit_as_expr_tokens_or(quote! {
rocket::http::uri::Origin::dummy()
}))
}
pub fn guide_tests_internal(input: proc_macro::TokenStream) -> TokenStream {

View File

@ -61,19 +61,25 @@ fn extract_exprs<'a>(internal: &'a InternalUriParams) -> Result<(
Ok((path_exprs, query_exprs, types))
}
Validation::NamedIgnored(_) => {
let mut route_name = quote!(#route_name).to_string();
route_name.retain(|c| !c.is_whitespace());
let diag = internal.uri_params.args_span()
.error("expected unnamed arguments due to ignored parameters")
.note(format!("uri for route `{}` ignores path parameters: \"{}\"",
quote!(#route_name), internal.route_uri));
route_name, internal.route_uri));
Err(diag)
}
Validation::Unnamed(expected, actual) => {
let mut route_name = quote!(#route_name).to_string();
route_name.retain(|c| !c.is_whitespace());
let diag = internal.uri_params.args_span()
.error(format!("expected {} but {} supplied",
p!(expected, "parameter"), p!(actual, "was")))
.note(format!("route `{}` has uri \"{}\"",
quote!(#route_name), internal.route_uri));
route_name, internal.route_uri));
Err(diag)
}