Skip to content

lazy_type_alias causes lifetime error #114221

Closed
@matthiaskrgr

Description

@matthiaskrgr

I tried this code:
tests/ui/regions/regions-scope-chain-example.rs

// run-pass
#![allow(dead_code)]
#![allow(unused_variables)]
// This is an example where the older inference algorithm failed. The
// specifics of why it failed are somewhat, but not entirely, tailed
// to the algorithm. Ultimately the problem is that when computing the
// mutual supertype of both sides of the `if` it would be faced with a
// choice of tightening bounds or unifying variables and it took the
// wrong path. The new algorithm avoids this problem and hence this
// example typechecks correctly.

// pretty-expanded FIXME #23616

enum ScopeChain<'a> {
    Link(Scope<'a>),
    End
}

type Scope<'a> = &'a ScopeChain<'a>;

struct OuterContext;

struct Context<'a> {
    foo: &'a OuterContext
}

impl<'a> Context<'a> {
    fn foo(&mut self, scope: Scope) {
        let link = if 1 < 2 {
            let l = ScopeChain::Link(scope);
            self.take_scope(&l);
            l
        } else {
            ScopeChain::Link(scope)
        };
        self.take_scope(&link);
    }

    fn take_scope(&mut self, x: Scope) {
    }
}

fn main() { }

without `-Zcrate-attr=feature(lazy_type_alias)´: no warnings

with -Zcrate-attr=feature(lazy_type_alias):

error[E0597]: `l` does not live long enough
  --> tests/ui/regions/regions-scope-chain-example.rs:31:29
   |
28 |     fn foo(&mut self, scope: Scope) {
   |                       ----- has type `Scope<'1>`
29 |         let link = if 1 < 2 {
30 |             let l = ScopeChain::Link(scope);
   |                 - binding `l` declared here
31 |             self.take_scope(&l);
   |             ----------------^^-
   |             |               |
   |             |               borrowed value does not live long enough
   |             argument requires that `l` is borrowed for `'1`
32 |             l
33 |         } else {
   |         - `l` dropped here while still borrowed

error[E0505]: cannot move out of `l` because it is borrowed
  --> tests/ui/regions/regions-scope-chain-example.rs:32:13
   |
28 |     fn foo(&mut self, scope: Scope) {
   |                       ----- has type `Scope<'1>`
29 |         let link = if 1 < 2 {
30 |             let l = ScopeChain::Link(scope);
   |                 - binding `l` declared here
31 |             self.take_scope(&l);
   |             -------------------
   |             |               |
   |             |               borrow of `l` occurs here
   |             argument requires that `l` is borrowed for `'1`
32 |             l
   |             ^ move out of `l` occurs here

error[E0597]: `link` does not live long enough
  --> tests/ui/regions/regions-scope-chain-example.rs:36:25
   |
28 |     fn foo(&mut self, scope: Scope) {
   |                       ----- has type `Scope<'1>`
29 |         let link = if 1 < 2 {
   |             ---- binding `link` declared here
...
36 |         self.take_scope(&link);
   |         ----------------^^^^^-
   |         |               |
   |         |               borrowed value does not live long enough
   |         argument requires that `link` is borrowed for `'1`
37 |     }
   |     - `link` dropped here while still borrowed

error: aborting due to 3 previous errors
rustc 1.73.0-nightly (04abc370b 2023-07-28)
binary: rustc
commit-hash: 04abc370b9f3855b28172b65a7f7d5a433f41412
commit-date: 2023-07-28
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Metadata

Metadata

Assignees

Labels

A-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)C-bugCategory: This is a bug.F-lazy_type_alias`#![feature(lazy_type_alias)]`S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions