Skip to content

Commit ba678b5

Browse files
authored
Merge pull request #81893 from meg-gupta/lifetimediagnostics
Fix diagnostic messages for some cases of invalid lifetime dependencies
2 parents 57b56a0 + cbe2533 commit ba678b5

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8293,8 +8293,8 @@ ERROR(lifetime_dependence_immortal_alone, none,
82938293
"cannot specify any other dependence source along with immortal", ())
82948294
ERROR(lifetime_dependence_invalid_inherit_escapable_type, none,
82958295
"cannot copy the lifetime of an Escapable type, use "
8296-
"'@lifetime(borrow %0)' instead",
8297-
(StringRef))
8296+
"'@lifetime(%1%0)' instead",
8297+
(StringRef, StringRef))
82988298
ERROR(lifetime_dependence_cannot_use_parsed_borrow_consuming, none,
82998299
"invalid use of %0 dependence with %1 ownership",
83008300
(StringRef, StringRef))

lib/AST/LifetimeDependence.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,18 @@ class LifetimeDependenceChecker {
665665
case ParsedLifetimeDependenceKind::Inherit:
666666
// @lifetime(copy x) is only invalid for Escapable types.
667667
if (type->isEscapable()) {
668-
diagnose(loc, diag::lifetime_dependence_invalid_inherit_escapable_type,
669-
descriptor.getString());
668+
if (loweredOwnership == ValueOwnership::Shared) {
669+
diagnose(loc, diag::lifetime_dependence_invalid_inherit_escapable_type,
670+
descriptor.getString(), "borrow ");
671+
} else if (loweredOwnership == ValueOwnership::InOut) {
672+
diagnose(loc, diag::lifetime_dependence_invalid_inherit_escapable_type,
673+
descriptor.getString(), "&");
674+
} else {
675+
diagnose(
676+
loc,
677+
diag::lifetime_dependence_cannot_use_default_escapable_consuming,
678+
getOwnershipSpelling(loweredOwnership));
679+
}
670680
return std::nullopt;
671681
}
672682
return LifetimeDependenceKind::Inherit;

test/Sema/lifetime_attr.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,18 @@ func inoutLifetimeDependence(_ ne: inout NE) -> NE {
6969
ne
7070
}
7171

72+
@lifetime(copy k) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(&k)' instead}}
73+
func dependOnEscapable(_ k: inout Klass) -> NE {
74+
NE()
75+
}
76+
77+
@lifetime(copy k) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(borrow k)' instead}}
78+
func dependOnEscapable(_ k: borrowing Klass) -> NE {
79+
NE()
80+
}
81+
82+
@lifetime(copy k) // expected-error{{invalid lifetime dependence on an Escapable value with consuming ownership}}
83+
func dependOnEscapable(_ k: consuming Klass) -> NE {
84+
NE()
85+
}
86+

test/Sema/lifetime_depend_infer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct EscapableTrivialSelf {
8484
@lifetime(self) // OK
8585
mutating func mutatingMethodNoParamLifetime() -> NEImmortal { NEImmortal() }
8686

87-
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(borrow self)' instead}}
87+
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(&self)' instead}}
8888
mutating func mutatingMethodNoParamCopy() -> NEImmortal { NEImmortal() }
8989

9090
@lifetime(borrow self)
@@ -106,7 +106,7 @@ struct EscapableTrivialSelf {
106106
@lifetime(self)
107107
mutating func mutatingMethodOneParamLifetime(_: Int) -> NEImmortal { NEImmortal() }
108108

109-
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(borrow self)' instead}}
109+
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(&self)' instead}}
110110
mutating func mutatingMethodOneParamCopy(_: Int) -> NEImmortal { NEImmortal() }
111111

112112
@lifetime(borrow self)
@@ -138,7 +138,7 @@ struct EscapableNonTrivialSelf {
138138
@lifetime(self)
139139
mutating func mutatingMethodNoParamLifetime() -> NEImmortal { NEImmortal() }
140140

141-
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(borrow self)' instead}}
141+
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(&self)' instead}}
142142
mutating func mutatingMethodNoParamCopy() -> NEImmortal { NEImmortal() }
143143

144144
@lifetime(&self)
@@ -160,7 +160,7 @@ struct EscapableNonTrivialSelf {
160160
@lifetime(self)
161161
mutating func mutatingMethodOneParamLifetime(_: Int) -> NEImmortal { NEImmortal() }
162162

163-
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(borrow self)' instead}}
163+
@lifetime(copy self) // expected-error{{cannot copy the lifetime of an Escapable type, use '@lifetime(&self)' instead}}
164164
mutating func mutatingMethodOneParamCopy(_: Int) -> NEImmortal { NEImmortal() }
165165

166166
@lifetime(&self)

0 commit comments

Comments
 (0)