Skip to content
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

Improve some error messages for bevy_ecs generated by nightly rustc #5786

Closed
wants to merge 1 commit into from

Conversation

weiznich
Copy link

This commit introduces a new feature flags that let's user opt into the
nightly only #[rustc_on_unimplemented] attribute. This attribute can
be used to improve error messages generated by missing trait
implementations. This commit adds annotations to two locations in bevy
to improve error messages submitted as part of
weiznich/rust-foundation-community-grant#3

Addresses #1519

@weiznich weiznich force-pushed the improve_error_messages branch from 9e6f5e7 to c7cde57 Compare August 24, 2022 15:28
This commit introduces a new feature flags that let's user opt into the
nightly only `#[rustc_on_unimplemented]` attribute. This attribute can
be used to improve error messages generated by missing trait
implementations. This commit adds annotations to two locations in bevy
to improve error messages submitted as part of
weiznich/rust-foundation-community-grant#3

Addresses bevyengine#1519
@weiznich weiznich force-pushed the improve_error_messages branch from c7cde57 to 7adcdd2 Compare August 24, 2022 15:28
@weiznich
Copy link
Author

See weiznich/rust-foundation-community-grant@e69f4f7#diff-c059c07feb90f777392b96c4c59911c9b5895882682eff9b25f7f0539338b6b0 for the relevant output changes.

The exact wording of the error messages is up to discussion and can be easily changed.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Aug 24, 2022
@@ -252,6 +252,10 @@ impl_param_set!();
/// # schedule.add_system_to_stage("update", write_resource_system.after("first"));
/// # schedule.run_once(&mut world);
/// ```
#[cfg_attr(
feature = "nightly-error-messages",
rustc_on_unimplemented(note = "consider adding `#[derive(bevy::Resource)]` to `{Self}`")
Copy link
Member

Choose a reason for hiding this comment

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

We should match this for the Component trait :)

@alice-i-cecile
Copy link
Member

I am extremely excited to see the upstream work start to pay off :)

Is there any way to automatically detect whether the user is on a compatible nightly version? This is most useful for beginners, so it's a shame it has to be opt-in.

@alice-i-cecile
Copy link
Member

The IntoSystemDescriptor error message is the other critical pain point; would that be feasible to address in this PR or are there limitations that would block that?

@mockersf
Copy link
Member

mockersf commented Aug 24, 2022

It seems rustc_on_unimplemented is never going to be stabilised, and not meant to be used outside of rustc. If that's the case, I don't think it's usable for Bevy

@weiznich
Copy link
Author

Is there any way to automatically detect whether the user is on a compatible nightly version? This is most useful for beginners, so it's a shame it has to be opt-in.

I didn't check that, but I would expect that any nightly from the last few years is compatible. The attribute does not really change much as far as I know. ekuber states that there were maybe 3 changes in the last 5 years, all of them backward compatible. As for the opt-in part: See the last part of the comment. At least I as diesel maintainer would be quite interested in having something like that on stable. Maybe we can suggest something like that to the compiler team if there is demand in the ecosystem.

The IntoSystemDescriptor error message is the other critical pain point; would that be feasible to address in this PR or are there limitations that would block that?

I do not know the code base well enough to say if that would be feasible or not. Generally speaking if the error messages is a "… trait not implemented …" error message, then it's likely possible. I might have a look at that if you can provide an example.

It seems rustc_on_unimplemented is never going to be stabilised, and not meant to be used outside of rustc. If that's the case, I don't think it's usable for Bevy

That's partially correct. rustc_on_unimplemented will not be stabilized as far as I can tell. Although it can be used outside of rustc (as shown by this PR). There are considerations to stabilize basically the same thing without guaranteeing anything other than that it does not cause a compiler error if present. I personally want to investigate in some good use-cases outside of rustc for this attribute, so that there are exist a few examples of successful uses of this attribute. This hopefully gives some good arguments to stabilize it in some form. (My personal favorite would be using a tool attribute for this, so similar to the existing #[rustfmt::skip] there would be an #[rust::on_unimplemented] attribute, but that's up to future discussion.)

#[cfg_attr(
feature = "nightly-error-messages",
rustc_on_unimplemented(on(
not(any(_Self = "& _", _Self = "&mut _", _Self = "()", _Self = "(_, _)")),
Copy link
Author

Choose a reason for hiding this comment

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

Note to myself: I need to check if that actually does something or if it's unnecessary. In the second case I likely need to find a solution for false positives if Self is a tuple or something like that.

@alice-i-cecile
Copy link
Member

I do not know the code base well enough to say if that would be feasible or not. Generally speaking if the error messages is a "… trait not implemented …" error message, then it's likely possible. I might have a look at that if you can provide an example.

Sure! Here's a simple failing snippet:

use bevy::prelude::*;

// Should be `mut commands: Commands` as `&mut Commands` doesn't implement `SystemParam`
fn broken_system(commands: &mut Commands) {}

fn main(){
    App::new().add_system(broken_system).run();
}

Maybe we can suggest something like that to the compiler team if there is demand in the ecosystem.

I'd be happy to help advocate for this; this style of work was mentioned in the lang team's roadmap as an area they'd like to move forward with and something I've personally discussed in the past with both Niko and Josh Triplett.

I agree that this work is valuable as a proof of concept for how this feature could improve end user UX.

I'm personally a bit nervous to merge this PR using rustc_on_unimplemented, but if @cart decides that this feature flag approach is acceptable I would want to insist on a step in CI that verifies that this continues to work as intended.

@alice-i-cecile
Copy link
Member

I didn't check that, but I would expect that any nightly from the last few years is compatible. The attribute does not really change much as far as I know

@weiznich to clarify, I was wondering if there was a way to detect at runtime if the rustc version was nightly or stable, so we could avoid having a feature flag at all :)

@weiznich
Copy link
Author

I'd be happy to help advocate for this; this style of work was mentioned in the lang team's roadmap as an area they'd like to move forward with and something I've personally discussed in the past with both Niko and Josh Triplett.

I should probably link this zulip discussion here as well as this seems to be highly relevant and currently ongoing.

@weiznich to clarify, I was wondering if there was a way to detect at runtime if the rustc version was nightly or stable, so we could avoid having a feature flag at all :)

As far as I know there is currently no way to detect which rustc version is used to compile the current crate. At least not without using a build.rs file and something like version_check or a similar approach. I personally prefer feature flags for such things as this gives users a finer control whether they want to use something that's not considered to be stable.

@cart
Copy link
Member

cart commented Aug 24, 2022

I'm personally a bit nervous to merge this PR using rustc_on_unimplemented, but if @cart decides that this feature flag approach is acceptable I would want to insist on a step in CI that verifies that this continues to work as intend

If this is totally opt-in and the implementation scope is small (and it looks that way to me), I'm down to merge something in this vein if it helps move this compiler effort forward / prove these features are valuable.

Probably worth adding some comment explaining the rationale behind this / the scope of this effort.

That being said, if merging this (as opposed to just testing this branch ourselves and leaving feedback) doesn't directly benefit the effort to stabilize (ex: help answer unanswered questions), then I think we should wait for a stable api.

@weiznich
Copy link
Author

weiznich commented Sep 1, 2022

I had some discussions with ekuber about how to stabilize the corresponding attribute. He tweeted about that here. The summary of that discussion is that we both agree on that it should be possible to stabilize this "soon". Maybe even without going through a language team decision by using the existing tool attribute RFC. I'm on holiday for the next weeks,after that I plan to fill a compiler MCP for this.

For this PR: It's up to you if you want to provide this improved error messages as early as possible or wait for a final compiler/language team decision. For diesel I recently shipped at least some uses of the corresponding attribute behind a feature flag, as I believe that can help users.

@cart
Copy link
Member

cart commented Sep 1, 2022

Given that this message is intended to help new users, I think the added complexity of requiring nightly / enabling a feature counteracts the improved UX of the better error messages. I think we should probably wait until it stabilizes.

@alice-i-cecile alice-i-cecile added S-Blocked This cannot move forward until something else changes X-Controversial There is active debate or serious implications around merging this PR and removed X-Controversial There is active debate or serious implications around merging this PR labels Sep 2, 2022
@alice-i-cecile alice-i-cecile added the X-Controversial There is active debate or serious implications around merging this PR label Jan 11, 2023
@alice-i-cecile
Copy link
Member

Closing this out: we'll redo this properly with the stable equivalent soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Blocked This cannot move forward until something else changes X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants