Improve compile errors for unimplemented traits (#1436)

* Improve `debug_handler` to use the correct span for specific bounds

This results in better localised error messages, as they now point
directly to the corresponding argument instead of to the macro itself.

* Improve some error messages behind a `nightly-error-messages` feature
flag

This uses the nightly only `rustc_on_unimplemented` attribute to improve
some error messages when users try to use invalid handler functions.
This should be seen as prove of concept, not as full solution for all
potential error cases.

The underlying feature is currently marked as permanently unstable, but
I'm working on getting this specific attribute (or an attribute with
different name, similar functionality) ready to work on a stable compiler.

* Apply suggestions from code review

Co-authored-by: Jonas Platte <[email protected]>

* Enable the `nightly-error-messages` feature unconditionally for nightly compilers

* Use a nightly compiler to run the axum-marcos compile fail tests

* update to newer nightly

* Run axum-macros tests on nightly

* tweak compile error hints a bit

* more tweaks

* update test

Co-authored-by: Jonas Platte <[email protected]>
Co-authored-by: David Pedersen <[email protected]>
This commit is contained in:
Georg Semmler
2022-11-19 12:45:03 +01:00
committed by GitHub
co-authored by Jonas Platte David Pedersen
parent 2e3000f1a3
commit d5de3bc7e3
19 changed files with 88 additions and 19 deletions
@@ -4,6 +4,8 @@ error[E0277]: the trait bound `bool: FromRequestParts<()>` is not satisfied
4 | async fn handler(foo: bool) {}
| ^^^^ the trait `FromRequestParts<()>` is not implemented for `bool`
|
= note: Function argument is not a valid axum extractor.
See `https://docs.rs/axum/latest/axum/extract/index.html` for details
= help: the following other types implement trait `FromRequestParts<S>`:
<() as FromRequestParts<S>>
<(T1, T2) as FromRequestParts<S>>
@@ -16,10 +18,7 @@ error[E0277]: the trait bound `bool: FromRequestParts<()>` is not satisfied
and 25 others
= note: required for `bool` to implement `FromRequest<(), Body, axum_core::extract::private::ViaParts>`
note: required by a bound in `__axum_macros_check_handler_0_from_request_check`
--> tests/debug_handler/fail/argument_not_extractor.rs:3:1
--> tests/debug_handler/fail/argument_not_extractor.rs:4:23
|
3 | #[debug_handler]
| ^^^^^^^^^^^^^^^^ required by this bound in `__axum_macros_check_handler_0_from_request_check`
4 | async fn handler(foo: bool) {}
| ---- required by a bound in this
= note: this error originates in the attribute macro `debug_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
| ^^^^ required by this bound in `__axum_macros_check_handler_0_from_request_check`
@@ -1,9 +1,11 @@
error[E0277]: the trait bound `String: FromRequestParts<()>` is not satisfied
--> tests/debug_handler/fail/doesnt_implement_from_request_parts.rs:4:1
--> tests/debug_handler/fail/doesnt_implement_from_request_parts.rs:5:21
|
4 | #[debug_handler]
| ^^^^^^^^^^^^^^^^ the trait `FromRequestParts<()>` is not implemented for `String`
5 | async fn handler(_: String, _: Method) {}
| ^^^^^^ the trait `FromRequestParts<()>` is not implemented for `String`
|
= note: Function argument is not a valid axum extractor.
See `https://docs.rs/axum/latest/axum/extract/index.html` for details
= help: the following other types implement trait `FromRequestParts<S>`:
<() as FromRequestParts<S>>
<(T1, T2) as FromRequestParts<S>>
@@ -15,4 +17,4 @@ error[E0277]: the trait bound `String: FromRequestParts<()>` is not satisfied
<(T1, T2, T3, T4, T5, T6, T7, T8) as FromRequestParts<S>>
and 25 others
= help: see issue #48214
= note: this error originates in the attribute macro `debug_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
@@ -1,8 +1,11 @@
error: future cannot be sent between threads safely
--> tests/debug_handler/fail/not_send.rs:4:1
|
4 | async fn handler() {
| ^^^^^ future returned by `handler` is not `Send`
4 | / async fn handler() {
5 | | let rc = std::rc::Rc::new(());
6 | | async {}.await;
7 | | }
| |_^ future returned by `handler` is not `Send`
|
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `Rc<()>`
note: future is not `Send` as this value is used across an await
@@ -17,5 +20,8 @@ note: future is not `Send` as this value is used across an await
note: required by a bound in `check`
--> tests/debug_handler/fail/not_send.rs:4:1
|
4 | async fn handler() {
| ^^^^^ required by this bound in `check`
4 | / async fn handler() {
5 | | let rc = std::rc::Rc::new(());
6 | | async {}.await;
7 | | }
| |_^ required by this bound in `check`