Skip to content

Add a map in the TypeckTables to get all Upvars given the a closure ID #56905

Closed
@blitzerr

Description

@blitzerr
Contributor

It is a recurring pattern in the rustc compiler base to iterate thorough all the free-variables (or Upvars as they are also known as) using with_freevars. It might be better to have a list of all the Upvars given a closure id.

Activity

added
C-cleanupCategory: PRs that clean code up or issues documenting cleanup.
on Dec 18, 2018
added a commit that references this issue on Dec 20, 2018

Rollup merge of rust-lang#56906 - blitzerr:master, r=nikomatsakis

f46a757
added a commit that references this issue on Dec 21, 2018

Rollup merge of rust-lang#56906 - blitzerr:master, r=nikomatsakis

fafd715
nikomatsakis

nikomatsakis commented on Jan 2, 2019

@nikomatsakis
Contributor

Leaving some notes here:

It's clear that we want to remove the use of freevars wherever possible, since all that code is basically assuming that the set of captures for a closure is equal to the set of variables referenced from within that closure.

In most of these cases, it should be fairly straightforward: typeck can instead produce a list of captures and the code can use that list.

However, there are a few cases that are going to be more complex. The most notable is the place where we determine the "generics" (set of generic parameters) for a closure type. The problem is that, currently, the set of generic parameters include one generic parameter for each free variable.

I'm not really sure how best to fix this. I suppose the ideal would be if we could avoid generating TyClosure types until the initial type check is done. This may be plausible.

Hmm, perhaps an alternative could be to make the "upvar types" always be a tuple of the actual upvar types, with the arity not known until after type-check is done. During type-check, this would just be an inference variable (just as, today, all the types of the upvars are inference variables). After upvar inference, we would unify that variable with a tuple with the types of each upvar (just as, today, we unify each of the individual upvar variables with its final type).

A quick inspection of the users of upvar_tys suggests that this could work. We could do this refactoring today.

nikomatsakis

nikomatsakis commented on Jan 2, 2019

@nikomatsakis
Contributor

I suppose we could even, but we probably don't want to, make closures always store their upvars in a tuple, so that we access field #x as closure.upvars.x instead of closure.#x. Hmm. This might turn out more convenient, though it would also complicate some of the code in the borrow-checker which would now have to work harder to figure out if a given field reference is an "upvar field" reference.

(It would have the advantage of mapping the closure more closely to something a user could have written)

blitzerr

blitzerr commented on Jan 9, 2019

@blitzerr
ContributorAuthor

@nikomatsakis Can we close this issue as we can treat this as a logical wrapup. For the tuple introduction, I created another issue with the next steps ?

8 remaining items

Loading
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

    C-cleanupCategory: PRs that clean code up or issues documenting cleanup.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nikomatsakis@varkor@blitzerr

        Issue actions

          Add a map in the TypeckTables to get all Upvars given the a closure ID · Issue #56905 · rust-lang/rust