Skip to content

Inconsistent borrow-checking errors based on function return type #113771

Open
@yakubin

Description

@yakubin

I tried this code:

struct Foo<'a, 'b>(&'a u8, &'b u8);

struct Bar<'a, 'b>(&'a u8, &'b u8);

impl<'a, 'b> Bar<'a, 'b> {
    fn f1() -> &'a [Foo<'a, 'b>] {
        &[]
    }
    
    fn f2() {
        Self::f1();
    }
    
    fn f3() -> &'a [Foo<'a, 'b>] {
        Self::f1();
        &[]
    }
}

I expected to see this happen: Either both f2() and f3() to build without errors, or both generate errors.

Instead, this happened: only f2() generates an error. f3() is built without errors:

   Compiling borrow-test v0.1.0 (/Users/yakubin/src/borrow-test)
error: lifetime may not live long enough
  --> src/lib.rs:11:9
   |
5  | impl<'a, 'b> Bar<'a, 'b> {
   |      --  -- lifetime `'b` defined here
   |      |
   |      lifetime `'a` defined here
...
11 |         Self::f1();
   |         ^^^^^^^^^^ requires that `'b` must outlive `'a`
   |
   = help: consider adding the following bound: `'b: 'a`

error: could not compile `borrow-test` due to previous error

Meta

rustc --version --verbose:

rustc 1.66.0 (69f9c33d7 2022-12-12)
binary: rustc
commit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943
commit-date: 2022-12-12
host: aarch64-apple-darwin
release: 1.66.0
LLVM version: 15.0.2

(Checked on Rust Playgrounds that it's also reproduced on Rust nightly.)

f2() building should imply f3() building and vice versa, since they only differ by their return types, but the error is in a line that has nothing to do with whatsoever being returned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-implied-boundsArea: Implied bounds / inferred outlives-boundsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions