rust-lang / rust-lang/rust

Implementing `Reborrow` prevents normally moving a value of that type.

Open
#156,310 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker C-bug F-reborrow T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(reborrow)]

use std::marker::Reborrow;

struct Thing<'a>(&'a ());

impl<'a> Reborrow for Thing<'a> {}

fn foo<'a>(x: Thing<'a>) -> Thing<'a> {
    x
}

I expected this code to compile, since it's just an identity function. Instead, I got the following error:

    Checking foo v0.1.0 (/Users/timch/foo)
error[E0515]: cannot return reference to temporary value
  --> src/lib.rs:9:39
   |
 9 |   fn foo<'a>(x: Thing<'a>) -> Thing<'a> {
   |  _______________________________________^
10 | |     x
11 | | }
   | |_^ returns a reference to data owned by the current function

error[E0515]: cannot return value referencing function parameter `x`
  --> src/lib.rs:9:39
   |
 9 |   fn foo<'a>(x: Thing<'a>) -> Thing<'a> {
   |  _______________________________________^
10 | |     x
   | |     - `x` is borrowed here
11 | | }
   | |_^ returns a value referencing data owned by the current function

For more information about this error, try `rustc --explain E0515`.
error: could not compile `foo` (lib) due to 2 previous errors

cc @aapoalas

Meta

rustc --version --verbose:

rustc 1.97.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: aarch64-apple-darwin
release: 1.97.0-dev
LLVM version: 22.1.4

Compiled from the main branch at commit 63b1dfc0e00fd6f8ad7cd8817fc712e7d9b7be59.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the example in src/lib.rs with the reborrow feature enabled, then trace how the compiler handles Reborrow implementations during borrow checking. Compare the result with the expected identity-function behavior. Done means the example compiles without the E0515 diagnostics.

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
Active
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.