Skip to content

Cache failure when environment contains predicates with free lifetimes #22019

Closed
@nikomatsakis

Description

@nikomatsakis

The following example fails to compile in a nonsense way:

#![allow(missing_copy_implementations)]
#![allow(unused_variables)]

use std::borrow::ToOwned;

pub struct CFGNode;

pub type Node<'a> = &'a CFGNode;

pub trait GraphWalk<'c, N> {
    /// Returns all the nodes in this graph.
    fn nodes(&'c self) where [N]:ToOwned<Vec<N>>;
}

impl<'g> GraphWalk<'g, Node<'g>> for u32
{
    fn nodes(&'g self) where [Node<'g>]:ToOwned<Vec<Node<'g>>>
    { loop { } }
}

impl<'h> GraphWalk<'h, Node<'h>> for u64
{
    fn nodes(&'h self) where [Node<'h>]:ToOwned<Vec<Node<'h>>>
    { loop { } }
}

fn main()  { }

Running this code yields the following errors:



<anon>:23:5: 24:17 error: mismatched types:
 expected `core::borrow::ToOwned<collections::vec::Vec<&'h CFGNode>>`,
    found `core::borrow::ToOwned<collections::vec::Vec<&'g CFGNode>>`
(lifetime mismatch) [E0308]
<anon>:23     fn nodes(&'h self) where [Node<'h>]:ToOwned<Vec<Node<'h>>>
<anon>:24     { loop { } }
<anon>:24:5: 24:17 note: the lifetime 'h as defined on the block at 24:4...
<anon>:24     { loop { } }
              ^~~~~~~~~~~~
<anon>:18:5: 18:17 note: ...does not necessarily outlive the lifetime 'g as defined on the block at 18:4
<anon>:18     { loop { } }
              ^~~~~~~~~~~~
<anon>:23:5: 24:17 error: mismatched types:
 expected `core::borrow::ToOwned<collections::vec::Vec<&'h CFGNode>>`,
    found `core::borrow::ToOwned<collections::vec::Vec<&'g CFGNode>>`
(lifetime mismatch) [E0308]
<anon>:23     fn nodes(&'h self) where [Node<'h>]:ToOwned<Vec<Node<'h>>>
<anon>:24     { loop { } }
<anon>:18:5: 18:17 note: the lifetime 'g as defined on the block at 18:4...
<anon>:18     { loop { } }
              ^~~~~~~~~~~~
<anon>:24:5: 24:17 note: ...does not necessarily outlive the lifetime 'h as defined on the block at 24:4
<anon>:24     { loop { } }
              ^~~~~~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
Program ended.

These are nonsense because you can see the compiler is comparing two lifetimes ('g and 'h) that are never simultaneously in scope. This arises due to errors in caching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions