Skip to content

[Parse] Enable self rebinding (to self) #15306

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 1 commit into from
Mar 26, 2018
Merged

Conversation

dduan
Copy link
Contributor

@dduan dduan commented Mar 16, 2018

As discussed in this thread, this patch allows "self" to be an identifier bound to self (see added tests for examples), and is useful in closures where self is weakly captured to avoid retain cycles. The current behavior of allowing

guard let `self` = self else { ... }

remains for source compatibility.

Instead of parsing identifiers and fallthrough to parsing self, treat kw_self the same as identifiers.

@dduan dduan changed the title Enable self rebinding (to self) [Parse] Enable self rebinding (to self) Mar 16, 2018
@rintaro
Copy link
Member

rintaro commented Mar 17, 2018

Could you add a test cases for implicit method/property references?

class MyCls {
  func something() {}

  func test() {
    let self = OtherCls() // Even if `self` is shadowed,
    something() // this should still refer `MyCls.something`.
  }
}

I know this currently works. But it prevents accidental breakage in the future.

@dduan
Copy link
Contributor Author

dduan commented Mar 17, 2018

@rintaro added

@swiftlang swiftlang deleted a comment from swift-ci Mar 17, 2018
@swiftlang swiftlang deleted a comment from swift-ci Mar 17, 2018
@dduan
Copy link
Contributor Author

dduan commented Mar 17, 2018

@swift-ci please test

@swiftlang swiftlang deleted a comment from swift-ci Mar 17, 2018
@swiftlang swiftlang deleted a comment from swift-ci Mar 17, 2018
@dduan
Copy link
Contributor Author

dduan commented Mar 20, 2018

@rintaro does everything else look mergeable to you? cc @DougGregor (owner)

@rintaro
Copy link
Member

rintaro commented Mar 20, 2018

No more objection from me. Defer to @DougGregor (or other core team member) on the decision.

func something() {}

func test() {
// expected-warning @+1 {{}}
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to have some part of the warning text here, for others that read the test later on. Is the warning about rebinding self to an unrelated type (which would be a really good warning, IMO!), or is it something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in this case it's just about the variable being unused. I included the text.

}
}

Writer().nonStop()
Copy link
Member

Choose a reason for hiding this comment

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

You can delete this line; it doesn't add anything.

Instead of parisng identifiers and fallthrough to self, treat `kw_self` the same
as identifiers. This enables binding to self.
@dduan
Copy link
Contributor Author

dduan commented Mar 26, 2018

@swift-ci please test

@swiftlang swiftlang deleted a comment from swift-ci Mar 26, 2018
@swiftlang swiftlang deleted a comment from swift-ci Mar 26, 2018
@dduan dduan merged commit def71e0 into swiftlang:master Mar 26, 2018
@dduan dduan deleted the self-rebinding branch March 26, 2018 21:24
@acecilia
Copy link

acecilia commented Jun 9, 2018

Is this PR linked to an evolution proposal?

There is an old evolution proposal here already merged, but in Deferred status. Seen the general acceptance shown in the discussion thread + the merge of this PR, would it make sense to consider updating the evolution proposal and taking it back to life?

@lattner
Copy link
Contributor

lattner commented Aug 30, 2018

@dduan and @DougGregor can you two look at revising and updating SE-0079? It is related to this patch and is causing confusion for people who think that this behavior is not supposed to be supported.

@dduan
Copy link
Contributor Author

dduan commented Aug 30, 2018

Will a simple update of status suffice? swiftlang/swift-evolution#900

@mdiep
Copy link
Contributor

mdiep commented Aug 31, 2018

Unfortunately, it seems like this may still not work in LLDB: https://bugs.swift.org/browse/SR-6156 😞

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.

6 participants