Skip to content

Allow lazy property initializers to reference instance members without explicit 'self.' #9920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2017

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented May 25, 2017

This is a common source of confusion. Fixes rdar://problem/16888679, rdar://problem/31762378, https://bugs.swift.org/browse/SR-48,
https://bugs.swift.org/browse/SR-2203,
https://bugs.swift.org/browse/SR-4663, and the countless other
dupes of this issue.

Allow instance properties and methods to be referenced from
within a lazy property initializer, with or without explicit
'self.' qualification.

The old behavior in Swift 3 was an incredible combination
of odd quirks:

- If the lazy property had an explicitly-written type, it was
  possible to reference instance members from the initializer
  expression by explicitly prefixing 'self.'.

- However, if the lazy property type is inferred, it would
  first be type checked in the initializer context, which
  has no 'self' available.

- Unqualified references to instance members did not work
  at all, because name lookup thought the "location" of the
  lookup was outside of the body of the getter.

- Unqualified references to static properties worked, however
  unqualified references to static methods did not, and
  produced a bogus diagnostic, because one part of the name
  lookup code thought that initializers were "instance
  context" and another thought they were "static context".

This patch improves on the old behavior with the following
fixes:

- Give PatternBindingInitializers associated with lazy
  properties an implicit 'self' declaration for use by
  name lookup.

- In order to allow "re-parenting" the initializer after it
  has been type checked into the body of the getter, "steal"
  the initializer's 'self' when buiding the getter.

- Fix up name lookup and make it aware of the implicit
  'self' decl of a PatternBindingInitializer.

This improves upon an earlier fix for this issue by Doug Gregor
which only worked with ASTScope enabled; the new fix is more
general and shares logic between the two name lookup
implementations.

Fixes <rdar://problem/16888679>, <https://bugs.swift.org/browse/SR-48>,
<https://bugs.swift.org/browse/SR-2203>,
<https://bugs.swift.org/browse/SR-4663>, and the countless other
dupes of this issue.
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@NachoSoto
Copy link
Contributor

🎉

@slavapestov slavapestov merged commit fd80aa0 into swiftlang:master May 25, 2017
@harlanhaskins
Copy link
Contributor

Thank you!

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.

3 participants