Incorrect borrow checker error
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Whe I try to compile this code:
#![feature(ptr_metadata)]
// tested at: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024
pub trait Device {
}
pub struct DeviceMetadata {
metadata: std::ptr::DynMetadata<dyn Device>,
}
impl DeviceMetadata {
pub fn new(device: &dyn Device) -> Self {
let device = device as *const dyn Device;
//let device = device as *const dyn Device;
Self {
metadata: std::ptr::metadata(device),
}
}
}
fn main() {
}
I get error:
error[E0521]: borrowed data escapes outside of associated function
--> src/main.rs:17:23
|
13 | pub fn new(device: &dyn Device) -> Self {
| ------ - let's call the lifetime of this reference `'1`
| |
| `device` is a reference that is only valid in the associated function body
...
17 | metadata: std::ptr::metadata(device),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `device` escapes the associated function body here
| argument requires that `'1` must outlive `'static`
For more information about this error, try `rustc --explain E0521`.
When I uncomment line 15, it works. device is pointer in both situations, so it should work in both cases.
Meta
Tested with nightly here: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024
Also at my computer:
rustc 1.91.0-nightly (6ba0ce409 2025-08-21)
binary: rustc
commit-hash: 6ba0ce40941eee1ca02e9ba49c791ada5158747a
commit-date: 2025-08-21
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example in src/main.rs or the linked Rust Playground with the reported nightly compiler. Compare the diagnostics for the two pointer-cast forms and read the guidance from rustc --explain E0521. Done means the behavior is explained or corrected and covered by an appropriate compiler regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100