Skip to content

[6.2] LifetimeDependence type check: infer trivial _read accessor #82269

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
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions lib/AST/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,16 +932,19 @@ class LifetimeDependenceChecker {
if (!useLazyInference() && afd->getParameters()->size() > 0) {
return;
}
if (!nonEscapableSelf && isBitwiseCopyable(selfTypeInContext, ctx)) {
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_bitwisecopyable,
diagnosticQualifier(), "self");
return;
}
if (!useLazyInference()) {
// Do not infer LifetimeDependenceKind::Inherit unless this is an implicit
// getter, which simply returns a stored property.
if (nonEscapableSelf && !isImplicitOrSIL()) {
// Allow inference for implicit getters, which simply return a stored,
// property, and for implicit _read/_modify, which cannot be defined
// explicitly alongside a regular getter.
if (!useLazyInference() && !isImplicitOrSIL()) {
// Require explicit @_lifetime(borrow self) for UnsafePointer-like self.
if (!nonEscapableSelf && isBitwiseCopyable(selfTypeInContext, ctx)) {
diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_bitwisecopyable,
diagnosticQualifier(), "self");
return;
}
// Require explicit @_lifetime(copy or borrow) for non-Escapable self.
if (nonEscapableSelf) {
diagnose(returnLoc, diag::lifetime_dependence_cannot_infer_kind,
diagnosticQualifier(), "self");
return;
Expand Down
30 changes: 30 additions & 0 deletions test/Sema/lifetime_depend_infer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,36 @@ struct Accessors {
}
}

struct TrivialAccessors {
let p: UnsafeRawPointer

// The implicit _read/_modify accessors must be inferred. They cannot be written explicitly because a getter is
// already defined.
var neComputed: NEImmortal {
@_lifetime(borrow self)
get { // OK
NEImmortal()
}

@_lifetime(&self)
set { // OK (no dependency)
}
}

var neYielded: NEImmortal {
@_lifetime(borrow self)
_read { // OK
yield NEImmortal()
}

@_lifetime(&self)
_modify { // OK
var ne = NEImmortal()
yield &ne
}
}
}

struct NonescapableSelfAccessors: ~Escapable {
var ne: NE

Expand Down