Skip to content

Initial language support for lifetime dependence #71069

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 5 commits into from
Jan 25, 2024

Conversation

meg-gupta
Copy link
Contributor

No description provided.

@meg-gupta
Copy link
Contributor Author

meg-gupta commented Jan 22, 2024

Swift Parser changes and support for lifetime dependence in initializers and function types are pending.

@meg-gupta meg-gupta requested review from atrick and xedin January 22, 2024 20:33
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test windows platform

@atrick atrick requested a review from gottesmm January 23, 2024 22:34
Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

LGTM! I left a few comments inline. @ahoppen and/or @rintaro could you please take a look at the parser changes?

Value(Identifier name) : Named({name}) {}
Value(unsigned index) : Ordered({index}) {}
Value() {}
} value;
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of doing this could we take advantage of inline bit fields here? We'll get a NamedSpecifier, OrderedSpecifier or SelfSpecifier sublcasses and share trailing storage to store name/id similar to #70998?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can I do this as a follow on commit ?

static inline llvm::StringRef getOwnershipSpelling(ValueOwnership ownership) {
switch (ownership) {
case ValueOwnership::Default:
return "";
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be something more concrete i.e. "default"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Leaving the spelling as is, because it matched both swift and sil.

@atrick
Copy link
Contributor

atrick commented Jan 24, 2024

The diagnostics on valid dependence vs. ownership specifiers looks great to me.

Please also disallow a mutate dependence when there is no ownership specifier!

As discussed, we should

  • implement inference in Sema based on explicit parameter ownership.
  • mangle function types based on lifetime dependence regardless of whether explicit or inferred.

But those can be follow-up PRs.

@meg-gupta meg-gupta force-pushed the lifetimedependencelangattr branch 2 times, most recently from c9220e6 to d7b974d Compare January 24, 2024 03:26
@meg-gupta
Copy link
Contributor Author

@swift-ci smoke test

if (returnTypeRepr) {
if (auto *lifetimeDependentRepr =
dyn_cast<LifetimeDependentReturnTypeRepr>(returnTypeRepr)) {
lifetimeDependenceInfo = validateLifetimeDependenceInfo(
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be in a separate request evaluator request

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can I do that in a follow up PR ?

@slavapestov
Copy link
Contributor

You might want to consider landing some of these changes right away, to avoid merge conflicts.

@slavapestov
Copy link
Contributor

(Assuming the existing tests still pass)

@meg-gupta meg-gupta force-pushed the lifetimedependencelangattr branch 2 times, most recently from 7f2ec44 to abdade5 Compare January 24, 2024 20:21
@meg-gupta
Copy link
Contributor Author

@swift-ci smoke test

Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

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

Parser changes look good. Nothing serious.

break;
}
// consume the lifetime dependence kind
consumeToken();
Copy link
Member

@rintaro rintaro Jan 24, 2024

Choose a reason for hiding this comment

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

Not remembering the location of kind token makes getStartLoc() incorrect, but I don't think it's serious for now. applyAttributesToType() changes the order of specifiers anyway. We should fix them, but not necessary in this PR.

Also, representing single _copy(x, y) as separate LifetimeDependenceSpecifier make it impossible to restore the textual representation of the specifier (when printing), but I think that's also not serious for now.


void LifetimeDependentReturnTypeRepr::printImpl(
ASTPrinter &Printer, const PrintOptions &Opts) const {
printTypeRepr(getBase(), Printer, Opts);
Copy link
Member

Choose a reason for hiding this comment

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

I assume you're going to implement printing specifiers in followups.

())

ERROR(expected_rparen_after_lifetime_dependence, PointsToFirstBadToken,
"expected ')' after param list in lifetime dependence specifier", ())
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"expected ')' after param list in lifetime dependence specifier", ())
"expected ')' after parameter list in lifetime dependence specifier", ())

Also: I don’ t think we have a test case for this diagnostic.

func use(_ x: borrowing BufferView) {}

func deriveMultiView1(_ x: borrowing BufferView, _ y: borrowing BufferView) -> _borrow(x, y) BufferView {
if (Int.random(in: 1..<100) % 2 == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: The parentheses aren’t needed in Swift.

return BufferView(y.ptr)
}

func deriveMultiView2(_ x: borrowing BufferView, _ y: borrowing BufferView) -> _borrow(x) _borrow(y) BufferView {
Copy link
Member

Choose a reason for hiding this comment

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

Is it intentional that it’s allowed to specify _borrow twice? I think we should error here and only allow _borrow(x, y).

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 had the same opinion. @atrick suggested not to diagnose this previously.

If it's going to be a problematic grammar, we can ban it.

cc @atrick

@meg-gupta meg-gupta force-pushed the lifetimedependencelangattr branch from abdade5 to 1894b11 Compare January 24, 2024 23:01
@meg-gupta
Copy link
Contributor Author

@swift-ci smoke test

@meg-gupta
Copy link
Contributor Author

@swift-ci smoke test Linux

@meg-gupta
Copy link
Contributor Author

@swift-ci test Linux platform

meg-gupta added a commit to meg-gupta/swift that referenced this pull request Jan 25, 2024
@meg-gupta meg-gupta merged commit 28f27c3 into swiftlang:main Jan 25, 2024
meg-gupta added a commit to meg-gupta/swift that referenced this pull request Jan 25, 2024
@meg-gupta
Copy link
Contributor Author

meg-gupta commented Jan 25, 2024

@rintaro @ahoppen Follow up to comments on parser changes - #71147. I have left unaddressed ones open to handle later.

meg-gupta added a commit that referenced this pull request Jan 25, 2024
carlos4242 pushed a commit to carlos4242/swift that referenced this pull request May 31, 2024
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.

7 participants