Skip to content

Commit 7adcdd2

Browse files
committed
Improve some error messages generated by nightly rustc
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
1 parent 3d194a2 commit 7adcdd2

File tree

6 files changed

+16
-0
lines changed

6 files changed

+16
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ debug_asset_server = ["bevy_internal/debug_asset_server"]
114114

115115
# Enable animation support, and glTF animation loading
116116
animation = ["bevy_internal/animation"]
117+
nightly-error-messages = ["bevy_internal/nightly-error-messages"]
117118

118119
[dependencies]
119120
bevy_dylib = { path = "crates/bevy_dylib", version = "0.9.0-dev", default-features = false, optional = true }

crates/bevy_ecs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ categories = ["game-engines", "data-structures"]
1212
[features]
1313
trace = []
1414
default = ["bevy_reflect"]
15+
nightly-error-messages = []
1516

1617
[dependencies]
1718
bevy_ptr = { path = "../bevy_ptr", version = "0.9.0-dev" }

crates/bevy_ecs/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::undocumented_unsafe_blocks)]
22
#![doc = include_str!("../README.md")]
3+
#![cfg_attr(feature = "nightly-error-messages", feature(rustc_attrs))]
34

45
#[cfg(target_pointer_width = "16")]
56
compile_error!("bevy_ecs cannot safely compile for a 16-bit platform.");

crates/bevy_ecs/src/query/fetch.rs

+8
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ use std::{cell::UnsafeCell, marker::PhantomData};
325325
/// [`WorldQuery::update_archetype_component_access`] exactly reflects the results of
326326
/// [`WorldQuery::matches_component_set`], [`WorldQuery::archetype_fetch`], and
327327
/// [`WorldQuery::table_fetch`].
328+
#[cfg_attr(
329+
feature = "nightly-error-messages",
330+
rustc_on_unimplemented(on(
331+
not(any(_Self = "& _", _Self = "&mut _", _Self = "()", _Self = "(_, _)")),
332+
message = "Using a `WorldQuery` object as parameter to `Query` requires the usage of a reference",
333+
label = "consider using `& {Self}` here"
334+
),)
335+
)]
328336
pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> {
329337
/// The read-only variant of this [`WorldQuery`], which satisfies the [`ReadOnlyWorldQuery`] trait.
330338
type ReadOnly: ReadOnlyWorldQuery<State = Self::State>;

crates/bevy_ecs/src/system/system_param.rs

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ impl_param_set!();
252252
/// # schedule.add_system_to_stage("update", write_resource_system.after("first"));
253253
/// # schedule.run_once(&mut world);
254254
/// ```
255+
#[cfg_attr(
256+
feature = "nightly-error-messages",
257+
rustc_on_unimplemented(note = "consider adding `#[derive(bevy::Resource)]` to `{Self}`")
258+
)]
255259
pub trait Resource: Send + Sync + 'static {}
256260

257261
/// Shared borrow of a [`Resource`].

crates/bevy_internal/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ trace_chrome = [ "bevy_log/tracing-chrome" ]
2222
trace_tracy = ["bevy_render?/tracing-tracy", "bevy_log/tracing-tracy" ]
2323
wgpu_trace = ["bevy_render/wgpu_trace"]
2424
debug_asset_server = ["bevy_asset/debug_asset_server"]
25+
nightly-error-messages = ["bevy_ecs/nightly-error-messages"]
2526

2627
# Image format support for texture loading (PNG and HDR are enabled by default)
2728
hdr = ["bevy_render/hdr"]

0 commit comments

Comments
 (0)