-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Restrictive AssetPath From impls cause hard-to-diagnose compile errors #10478
Description
Bevy version
0.12 or git
What you did
Use AssetServer methods that take impl Into<AssetPath<'a>> as parameters.
What went wrong
error[E0597]: `fileset` does not live long enough
--> crates/artifice_editor/src/files.rs:120:30
|
96 | .show(egui_context, |ui| {
| ---- value captured here
...
120 | for path in &fileset.files {
| ^^^^^^^ borrowed value does not live long enough
...
133 | let handle = server.load_untyped(relpath);
| ---------------------------- argument requires that `fileset` is borrowed for `'static`
...
145 | }
| - `fileset` dropped here while still borrowed
error[E0521]: borrowed data escapes outside of function
--> crates/artifice_editor/src/files.rs:133:38
|
55 | mut fileset: Local<FileSet>,
| -----------
| |
| `fileset` is a reference that is only valid in the function body
| has type `bevy::prelude::Local<'1, FileSet>`
...
133 | let handle = server.load_untyped(relpath);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `fileset` escapes the function body here
| argument requires that `'1` must outlive `'static`
This diagnostic is difficult to decipher: the impl Into<AssetPath<'a>> argument makes it look like the method wouldn't have any lifetime constraints, and the diagnostics don't refer to the actual reason those constraints exist (so arguably this is a rustc bug, but bevy might want to fix it too, since what it does is somewhat odd here).
The real cause of these errors is that AssetPath has several From impls that are restricted to 'static lifetimes:
impl From<&'static Path> for AssetPath<'static>impl From<&'static str> for AssetPath<'static>
Those are chosen by rustc and are what cause the argument to be forced to &'static Path, instead of &'a Path.
It is possible to fix this by making the From impls fully generic, and by moving the "static lifetime optimization" to dedicated methods (right now the AssetPath API works exactly the other way around).