Skip to content

Conversation

@slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Sep 18, 2020

This was motivated by getting self in capture lists working without parse-time name lookup.

@slavapestov slavapestov force-pushed the astscope-self-dc-cleanup branch from bcc7eb1 to a00b5cd Compare September 18, 2020 06:58
…Expr

We'll need this to get the right 'selfDC' when name lookup
finds a 'self' declaration in a capture list, eg

class C {
  func bar() {}
  func foo() {
    _ = { [self] in bar() }
  }
}
Copy link
Contributor

@davidungar davidungar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this direction. Didn't look deeply enough to assure correctness, but very nice to shift the complexity as this is doing. It wouldn't be a review from me without a request to break up a long function, where I think clarity could be improved.

lib/AST/Expr.cpp Outdated
Comment on lines +1254 to +1268
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer putting the for loop before the CaptureListExpr constructor to compute the nullable CaptureListExpr, then passing it in to the constructor. That way the instance variable could be a const and it would be a bit clearer when reading the code that it is immutable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand. The for loop references the newly-constructed CaptureListExpr instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my mistake. Thanks for pointing it out so gently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the real issue is not knowing where the var is captured when the var is created. Would be great if the var could be immutable somehow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can Parent be a const or do VarDecls move from family to family?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clean up.

Comment on lines -465 to -527
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely!

Comment on lines -596 to -652
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Comment on lines +644 to +672
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new comments are great!

…ut parse-time lookup

The DeclRefExpr here was resolved by parse-time lookup.
Without it, it's an UnresolvedDeclRefExpr.
@slavapestov slavapestov force-pushed the astscope-self-dc-cleanup branch from 1a73364 to e115b2f Compare September 18, 2020 17:35
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

This centralizes some invariants around the 'self' parameter.
While all ConstructorDecls and DestructorDecls have a 'self',
even if they're invalid because they're not nested inside a type,
we don't want to consider this as the base 'self' for lookups.

Eg, consider this invalid code:

class C {
  func f() {
    init() {
      x
    }
  }
}

The base for the lookup should be f.self, not f.init.self.
…fiedLookup

Instead of having ASTScope compute 'selfDC', the lookup
consumer can compute it on its own, by looking for
bindings named 'self'.
@slavapestov slavapestov force-pushed the astscope-self-dc-cleanup branch from e115b2f to f4083ba Compare September 18, 2020 19:06
Copy link
Contributor

@davidungar davidungar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The selfDC computation is better, but maybe in a future PR it could be even better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you've refactored--thanks!--I can see more clearly what is going on with candidateSelfDC. Is it a fragile piece of mutable state? Could we pass a piece of immutable state through the lookup recursion instead? It seems to be a bit odd that a consume method has a side effect. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually going to try to refactor the lookup to use a loop that walks the parents instead of recursing. This will allow us to implement the one-level lookup we need to do re-declaration checking for locals defined in the same scope. Maybe then the selfDC computation can go there and be totally explicit, instead of being part of consume().

@slavapestov slavapestov force-pushed the astscope-self-dc-cleanup branch from f4083ba to 0712d0b Compare September 18, 2020 19:48
@slavapestov slavapestov force-pushed the astscope-self-dc-cleanup branch from 0712d0b to d4cc35a Compare September 18, 2020 20:11
@slavapestov
Copy link
Contributor Author

swiftlang/llvm-project#1820
@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov slavapestov merged commit 1e0a9ca into swiftlang:master Sep 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants