Skip to content

Cleanup region hierarchy code, especially around closures #3696

Closed

Description

@nikomatsakis

This is a big that's been on the back of my mind for a while but I don't think it has a bug associated with it. When we build the region hierarchy, we assume that any ||-closure argument is limited to the lifetime of the call where it appears. However, since the more general form of fn& was implemented, I don't believe this is enforced. We should place an upper-bound on the || lifetime and add some tests. In an ideal world, maybe we would always infer the lifetime of an ||-closure, but this is tricky to do because the region hierarchy is built before region inference (and indeed it must be, in order to compute LUB/GLB and so forth).

Activity

  1. bblum commented on Oct 11, 2012

    @bblum
    Contributor

    I had a weird idea a while ago, for a higher-order function involving returning a stack closure something like:

    fn a(blk: &a/fn(c: &blk/fn() -> &blk/fn())) {
        let d = || { ... };
        do blk {
            return d;
        }
    }
    

    And then use it like:

    do a |c| {
        let d = c();
        d(); // 'd' cannot escape this block
    }
    

    I recall that I couldn't make it compile. This sounds like it might enable this (crazy, but cool) sort of thing.

  2. nikomatsakis commented on Mar 29, 2013

    @nikomatsakis
    ContributorAuthor

    An example from #3283 that should not build but does (leading to segfaults):

    fn foo(+blk: fn(+p: &a/fn() -> &a/fn())) {
        let mut state = 0;
        let statep = &mut state;
        do blk {
            || { *statep = 1; }
        }
    }
    fn main() {
        do foo |p| { p()() }
    }
    
  3. emberian commented on Jul 21, 2013

    @emberian
    Contributor

    Could not get to compile with:

    fn foo<'a>(blk: &fn(p: &'a fn() -> &'a fn())) {
        let mut state = 0;
        let statep = &mut state;
        do blk {
            || { *statep = 1; }
        }
    }
    fn main() {
        do foo |p| { p()() }
    }
  4. nikomatsakis commented on Jul 24, 2013

    @nikomatsakis
    ContributorAuthor

    I did fix some problems in this area though this is still an issue. I have to come up with a good representative test case.

  5. catamorphism commented on Oct 14, 2013

    @catamorphism
    Contributor

    @nikomatsakis Does #2202 subsume this, or is there still something left to do?

  6. added a commit that references this issue on Feb 4, 2014
  7. edwardw commented on Mar 13, 2014

    @edwardw
    Contributor

    The PR #12828 tries to apply variance rule to self type substitution, also part of #5781, but the following begins to fail:

    pub fn main() {
        let a = ~[~""];
        let b: ~[~[&str]] = a.map(|s| s.lines().collect()); // error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
    }

    First of all, should this compile as it currently does? If yes, then how is the self type substitution intertwined with with closure lifetime inference?

  8. nikomatsakis commented on Oct 14, 2014

    @nikomatsakis
    ContributorAuthor

    @bkoropoff I'd love to talk over this issue with you if you're interested in looking at something else after #17403 :) -- I think the two are related, or at least touching pretty similar code.

  9. nikomatsakis commented on Dec 9, 2014

    @nikomatsakis
    ContributorAuthor

    @pnkfelix has been working on this

  10. steveklabnik commented on Feb 14, 2015

    @steveklabnik
    Contributor

    @nikomatsakis what's the state of this with the new closure stuff? is it still relevant?

  11. added a commit that references this issue on Apr 1, 2015
  12. changed the title [-]Define region hierarchy for closures[/-] [+]Cleanup region hierarchy code, especially around closures[/+] on Jul 31, 2015
  13. nikomatsakis commented on Jul 31, 2015

    @nikomatsakis
    ContributorAuthor

    I'm updating the title on this issue and repurposing it to also cover the fact that it would be nice if the representation of "free" regions did not always have them attached to a scope. Instead, a free region should just have a number, and we should indicate the relationship that A >= X for some scope X as an in-scope where clause.

  14. added
    P-lowLow priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jan 26, 2017
  15. brson commented on Jan 26, 2017

    @brson
    Contributor

    @nikomatsakis Still a bug?

  16. nikomatsakis commented on Jan 26, 2017

    @nikomatsakis
    ContributorAuthor

    So this is still a bug, but I hope that we will fix it for good once we fully move borrow/region-checking to MIR. The idea is that MIR has fully desugared closures. I am going to close this bug though because it's kind of vague and I don't see how having it open is helpful in any particular way.

  17. added a commit that references this issue on Jun 29, 2024
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-closuresArea: Closures (`|…| { … }`)A-lifetimesArea: Lifetimes / regionsA-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priorityT-compilerRelevant to the compiler 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