Skip to content

Require matching lifetimes in protocol conformance checking#87281

Draft
aidan-hall wants to merge 4 commits intoswiftlang:mainfrom
aidan-hall:lifedep-protocol-check
Draft

Require matching lifetimes in protocol conformance checking#87281
aidan-hall wants to merge 4 commits intoswiftlang:mainfrom
aidan-hall:lifedep-protocol-check

Conversation

@aidan-hall
Copy link
Contributor

rdar://160970376

@aidan-hall aidan-hall force-pushed the lifedep-protocol-check branch 2 times, most recently from 73da449 to 15c3b8c Compare February 17, 2026 17:43
@aidan-hall aidan-hall marked this pull request as ready for review February 17, 2026 17:43
@aidan-hall
Copy link
Contributor Author

@swift-ci smoke test

@aidan-hall
Copy link
Contributor Author

@swift-ci test

@aidan-hall
Copy link
Contributor Author

@swift-ci Please Test Source Compatibility

@aidan-hall aidan-hall force-pushed the lifedep-protocol-check branch from 15c3b8c to 9a15561 Compare February 17, 2026 17:50
@aidan-hall
Copy link
Contributor Author

@swift-ci test

@aidan-hall
Copy link
Contributor Author

@swift-ci smoke test

@aidan-hall
Copy link
Contributor Author

@swift-ci test source compatibility

namespace swift {
bool checkLifetimeConformanceApproximately(
const ArrayRef<LifetimeDependenceInfo> from,
const ArrayRef<LifetimeDependenceInfo> to) {
Copy link
Contributor

Choose a reason for hiding this comment

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

checkLifetimeConformanceApproximately doesn't seem to be needed in addition to checkLifetimeConformanceCarefully. Why not just have checkLifetimeConformanceCarefully ?

Copy link
Contributor

@meg-gupta meg-gupta Feb 17, 2026

Choose a reason for hiding this comment

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

checkLifetimeConformanceCarefully should probably be renamed to something on the lines of matchLifetimeDependencies.

The early exit condition above should also be added here:

if (from.size() != to.size())
    return false;

Another early exit with:

if (from == to)
    return true;

Copy link
Contributor Author

@aidan-hall aidan-hall Feb 18, 2026

Choose a reason for hiding this comment

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

if (from == to)

I believe ArrayRef::operator== performs element-wise equality testing. Did you mean that we could do a base-pointer comparison?
I.e.

if (from.data() == to.data()) return true;

That would be an effective and cheap early exit check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (from.size() != to.size()) return false;

👍

// If a dependency is present for the same target in both types, then
// the dependency must match.
if (fromDep != *toDep) {
if (!fromDep.convertibleTo(*toDep)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the entire for loop can be collapsed into checkLifetimeConformanceCarefully

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This loop does have a similar structure to checkLifetimeConformanceCarefully, but it needs to add constraints for each fromDep that doesn't have a matching toDep, so it can't exit early.

@aidan-hall
Copy link
Contributor Author

aidan-hall commented Feb 18, 2026

This test fails because a function with an immortal lifetime dependence is used to conform to the protocol. This should be safe to do.
https://github.com/aidan-hall/swift/blob/lifedep-protocol-check/test/SIL/lifetime_dependence_generics.swift#L28

To maintain source compatibility, we will need to introduce a lifetime subtyping relationship, as described in #87187.

@aidan-hall aidan-hall force-pushed the lifedep-protocol-check branch from 8557dca to 1a64dc6 Compare February 18, 2026 17:30
@aidan-hall aidan-hall force-pushed the lifedep-protocol-check branch from 1a64dc6 to 10aa07f Compare February 18, 2026 19:30
@aidan-hall aidan-hall marked this pull request as draft February 18, 2026 21:30
@aidan-hall aidan-hall force-pushed the lifedep-protocol-check branch from e2e7bce to eca9e8c Compare February 18, 2026 21:51
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

Comments