Skip to content

Improve compile errors for unimplemented traits #1436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 19, 2022

Conversation

weiznich
Copy link
Contributor

These two commits try to improve error messages generated by rustc if some trait bound is not fulfilled.

The first commit simply improves the #[debug_handler] helper proc-macro attribute to set spans ins certain situation. This results in the corresponding error messages always pointing to a concrete handler argument instead of the attribute itself (see the updated compile tests).

The second commit introduces a rust nightly only nightly-error-messages feature flag + uses unstable rust attributes to improve some other error messages as well. It currently mostly points the users to use the #[debug_handler] attribute. In specific situation it additionally adds more information.. This is mostly meant as demonstration of what's possible. I currently try to push for stabilization of the underlying #[rustc_on_unimplemented] attribute.

#[cfg_attr(
feature = "nightly-error-messages",
rustc_on_unimplemented(
note = "Consider using `#[axum_macros::debug_handler]` to improve the error message"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 😄

Can you maybe add one or two tests for the on_unimplemented errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is where and how such tests can be added? Running them would always require a nightly compiler + trybuild/compiletests. I'm not that familiar with the axum project setup to answer this question, but if someone points me into the right direction I'm happy to add them there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already feature-gating the existing trybuild tests to a single rust version because stable and nightly often disagree. Have a look at the end of axum-macros/src/lib.rs. I think you can copy that to make a nightly-only trybuild test for the axum crate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although if you're doing this anyways, it's probably more sensible to always compile the test itself, and ignore it on other compiler versions using #[rustversion::attr(not(nightly), ignore)]. (instead of having the test do nothing)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weiznich We can also merge this as-is and add those tests in a separate PR, if you prefer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a good idea. I'm currently quite busy with other stuff, so I cannot address this comment now (although I've planed to do that next week or the week after).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Looks like CI needs to be updated though (we're using --all-features which is now trying to activate the nightly-only stuff on stable / beta).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also fine with making the required changes later this month.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a new version that enables this by default on nightly compilers and removes the explicit feature flag. Also this is now integrated in the axum-macros test suite output, although it's not clear for me if that's run on CI yet?

weiznich and others added 5 commits October 21, 2022 14:06
This results in better localised error messages, as they now point
directly to the corresponding argument instead of to the macro itself.
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.
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
@weiznich weiznich force-pushed the improve_error_messages branch from 1be6236 to fe94ab1 Compare October 21, 2022 12:10
@jplatte jplatte requested a review from davidpdrsn November 11, 2022 11:01
Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long to review this. I think this looks really good! Getting on stable would be awesome ✨

Comment on lines 42 to 52
#[cfg_attr(
nightly_error_messages,
rustc_on_unimplemented(
on(
_Self = "axum::http::Request<axum::body::Body>",
message = "Request must always be the last argument to a handler function",
label = "Move this argument to the end of the argument list",
),
note = "Function argument is no valid axum extractor. \nSee `https://docs.rs/axum/latest/axum/extract/index.html` for details",
)
)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be tweaked a bit and also need one for FromRequest. But we can play around with that once this is merged.

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait a second. Doesn't seem like the nightly tests are run on CI. Need to fix that before we can merge this. I'll look at that later.

I'm also getting an odd error from running this locally:

$ cargo +nightly-2022-10-20 check --tests --all-features
...
error: unreachable `pub` item
   --> axum/src/extract/mod.rs:56:9
    |
56  | pub use self::matched_path::MatchedPath;
    | ---     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | |
    | help: consider restricting its visibility: `pub(crate)`
    |
    = help: or consider exporting it for use by other crates
note: the lint level is defined here
   --> axum/src/lib.rs:429:9
    |
429 | #![deny(unreachable_pub, private_in_public)]
    |         ^^^^^^^^^^^^^^^

@jplatte
Copy link
Member

jplatte commented Nov 18, 2022

Maybe try a newer nightly and bump the toolchain file if things work? I don't get that error locally with a current nightly.

@davidpdrsn davidpdrsn changed the title Improve error messages Improve compile errors for unimplemented traits Nov 19, 2022
Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weiznich thanks a lot of working on this!

@davidpdrsn davidpdrsn merged commit d5de3bc into tokio-rs:main Nov 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants