Skip to content

Commit c3256b7

Browse files
jebrosenSergioBenitez
authored andcommitted
Allow catchers to be async fn.
1 parent 13273fe commit c3256b7

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

core/codegen/src/attribute/catch.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
5454
define_vars_and_mods!(req, catcher, Request, Response, ErrorHandlerFuture);
5555

5656
// Determine the number of parameters that will be passed in.
57-
let (fn_sig, inputs) = match catch.function.sig.inputs.len() {
57+
let (_fn_sig, inputs) = match catch.function.sig.inputs.len() {
5858
0 => (quote!(fn() -> _), quote!()),
5959
1 => (quote!(fn(&#Request) -> _), quote!(#req)),
6060
_ => return Err(catch.function.sig.inputs.span()
@@ -71,10 +71,22 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
7171
.map(|ty| ty.span().into())
7272
.unwrap_or(Span::call_site().into());
7373

74+
let responder_stmt = if catch.function.sig.asyncness.is_some() {
75+
quote_spanned! { return_type_span =>
76+
let ___responder = #catcher(#inputs).await;
77+
}
78+
} else {
79+
quote_spanned! { return_type_span =>
80+
let ___responder = #catcher(#inputs);
81+
}
82+
};
83+
7484
let catcher_response = quote_spanned!(return_type_span => {
75-
// Emit this to force a type signature check.
76-
let #catcher: #fn_sig = #user_catcher_fn_name;
77-
let ___responder = #catcher(#inputs);
85+
// TODO.async: fix this
86+
// // Emit this to force a type signature check.
87+
// let #catcher: #fn_sig = #user_catcher_fn_name;
88+
let #catcher = #user_catcher_fn_name;
89+
#responder_stmt
7890
::rocket::response::Responder::respond_to(___responder, #req).await?
7991
});
8092

core/codegen/tests/ui-fail/catch_type_errors.stderr

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@ error[E0277]: the trait bound `bool: rocket::response::Responder<'_>` is not sat
1515
= note: required by `rocket::response::Responder::respond_to`
1616

1717
error[E0308]: mismatched types
18-
--> $DIR/catch_type_errors.rs:18:7
18+
--> $DIR/catch_type_errors.rs:17:1
19+
|
20+
17 | #[catch(404)]
21+
| ^^^^^^^^^^^^^ expected bool, found reference
22+
|
23+
= note: expected type `bool`
24+
found type `&'_b rocket::Request<'_>`
25+
26+
error[E0277]: the trait bound `usize: rocket::response::Responder<'_>` is not satisfied
27+
--> $DIR/catch_type_errors.rs:18:26
1928
|
2029
17 | #[catch(404)]
2130
| ------------- expected due to this
2231
18 | fn f3(_request: bool) -> usize {
23-
| ^^^^^^^^^^^^^^ expected `&rocket::Request<'_>`, found `bool`
32+
| ^^^^^ the trait `rocket::response::Responder<'_>` is not implemented for `usize`
2433
|
25-
= note: expected fn pointer `for<'r, 's> fn(&'r rocket::Request<'s>) -> _`
26-
found fn item `fn(bool) -> usize {f3}`
34+
= note: required by `rocket::response::Responder::respond_to`
2735

2836
error[E0277]: the trait bound `usize: rocket::response::Responder<'_>` is not satisfied
2937
--> $DIR/catch_type_errors.rs:24:12
@@ -33,7 +41,7 @@ error[E0277]: the trait bound `usize: rocket::response::Responder<'_>` is not sa
3341
|
3442
= note: required by `rocket::response::Responder::respond_to`
3543

36-
error: aborting due to 4 previous errors
44+
error: aborting due to 5 previous errors
3745

3846
Some errors have detailed explanations: E0277, E0308.
3947
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)