Skip to content

Implementing Reborrow prevents normally moving a value of that type. #156310

Description

@theemathas

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 63b1dfc.

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    F-reborrow`#![feature(reborrow)]`; see #145612
    on May 8, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 8, 2026
  3. added
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 8, 2026
  4. tvsfx commented on Jul 21, 2026

    @tvsfx

    Just ran into a variant of this: the same issue occurs when creating and returning Thing inside a function (playground)

    #![feature(reborrow)]
    
    use std::marker::Reborrow;
    
    struct Thing<'a>(&'a ());
    
    // Compiles when this line is commented out
    impl<'a> Reborrow for Thing<'a> {}
    
    fn make_thing(x: &'static ()) -> Thing<'static> {
        Thing(x)
    }
    
    fn make_up_thing() -> Thing<'static> {
        Thing(&())
    }
    Compiler error
    error[E0515]: cannot return value referencing temporary value
      --> src/lib.rs:9:49
       |
     9 |   fn make_thing(x: &'static ()) -> Thing<'static> {
       |  _________________________________________________^
    10 | |     Thing(x)
       | |     -------- temporary value created here
    11 | | }
       | |_^ returns a value referencing data owned by the current function
    
    error[E0515]: cannot return reference to temporary value
      --> src/lib.rs:9:49
       |
     9 |   fn make_thing(x: &'static ()) -> Thing<'static> {
       |  _________________________________________________^
    10 | |     Thing(x)
    11 | | }
       | |_^ returns a reference to data owned by the current function
    
    error[E0515]: cannot return value referencing temporary value
      --> src/lib.rs:13:38
       |
    13 |   fn make_up_thing() -> Thing<'static> {
       |  ______________________________________^
    14 | |     Thing(&())
       | |     ---------- temporary value created here
    15 | | }
       | |_^ returns a value referencing data owned by the current function
    
    error[E0515]: cannot return reference to temporary value
      --> src/lib.rs:13:38
       |
    13 |   fn make_up_thing() -> Thing<'static> {
       |  ______________________________________^
    14 | |     Thing(&())
    15 | | }
       | |_^ returns a reference to data owned by the current function
  5. added a commit that references this issue on Jul 21, 2026
  6. sleepycatcoding commented on Sep 6, 2026

    @sleepycatcoding
    Contributor

    This looks like to have been fixed by #159103.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.F-reborrow`#![feature(reborrow)]`; see #145612T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions