Skip to content

Commit d242750

Browse files
authored
Merge pull request #82269 from atrick/62-infer-implicit-read
[6.2] LifetimeDependence type check: infer trivial _read accessor
2 parents 7bb7326 + b15d308 commit d242750

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

lib/AST/LifetimeDependence.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -932,16 +932,19 @@ class LifetimeDependenceChecker {
932932
if (!useLazyInference() && afd->getParameters()->size() > 0) {
933933
return;
934934
}
935-
if (!nonEscapableSelf && isBitwiseCopyable(selfTypeInContext, ctx)) {
936-
diagnose(returnLoc,
937-
diag::lifetime_dependence_cannot_infer_bitwisecopyable,
938-
diagnosticQualifier(), "self");
939-
return;
940-
}
941-
if (!useLazyInference()) {
942-
// Do not infer LifetimeDependenceKind::Inherit unless this is an implicit
943-
// getter, which simply returns a stored property.
944-
if (nonEscapableSelf && !isImplicitOrSIL()) {
935+
// Allow inference for implicit getters, which simply return a stored,
936+
// property, and for implicit _read/_modify, which cannot be defined
937+
// explicitly alongside a regular getter.
938+
if (!useLazyInference() && !isImplicitOrSIL()) {
939+
// Require explicit @_lifetime(borrow self) for UnsafePointer-like self.
940+
if (!nonEscapableSelf && isBitwiseCopyable(selfTypeInContext, ctx)) {
941+
diagnose(returnLoc,
942+
diag::lifetime_dependence_cannot_infer_bitwisecopyable,
943+
diagnosticQualifier(), "self");
944+
return;
945+
}
946+
// Require explicit @_lifetime(copy or borrow) for non-Escapable self.
947+
if (nonEscapableSelf) {
945948
diagnose(returnLoc, diag::lifetime_dependence_cannot_infer_kind,
946949
diagnosticQualifier(), "self");
947950
return;

test/Sema/lifetime_depend_infer.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,36 @@ struct Accessors {
318318
}
319319
}
320320

321+
struct TrivialAccessors {
322+
let p: UnsafeRawPointer
323+
324+
// The implicit _read/_modify accessors must be inferred. They cannot be written explicitly because a getter is
325+
// already defined.
326+
var neComputed: NEImmortal {
327+
@_lifetime(borrow self)
328+
get { // OK
329+
NEImmortal()
330+
}
331+
332+
@_lifetime(&self)
333+
set { // OK (no dependency)
334+
}
335+
}
336+
337+
var neYielded: NEImmortal {
338+
@_lifetime(borrow self)
339+
_read { // OK
340+
yield NEImmortal()
341+
}
342+
343+
@_lifetime(&self)
344+
_modify { // OK
345+
var ne = NEImmortal()
346+
yield &ne
347+
}
348+
}
349+
}
350+
321351
struct NonescapableSelfAccessors: ~Escapable {
322352
var ne: NE
323353

0 commit comments

Comments
 (0)