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

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