From 2e7556c1449ae26de4e6454af542c12043bef883 Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Thu, 3 Jul 2025 15:20:04 -0700 Subject: [PATCH] Fix a lifetime dependence diagnostic `LifetimeDescriptor::getName()` can crash if the descriptor had a `self`. Replace with `LifetimeDescriptor::getString()` (cherry picked from commit 6d0a6d2760409263cba28dda8f46c8a4afaf639c) --- include/swift/AST/DiagnosticsSema.def | 2 +- lib/AST/LifetimeDependence.cpp | 2 +- test/Sema/lifetime_attr.swift | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index 46b92335f7f4..8dbf8e8014c5 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -8249,7 +8249,7 @@ ERROR(lifetime_dependence_cannot_use_parsed_borrow_inout, none, ERROR(lifetime_dependence_duplicate_target, none, "invalid duplicate target lifetime dependencies on function", ()) ERROR(lifetime_parameter_requires_inout, none, - "lifetime-dependent parameter must be 'inout'", (Identifier)) + "lifetime-dependent parameter '%0' must be 'inout'", (StringRef)) //------------------------------------------------------------------------------ // MARK: Lifetime Dependence Requirements diff --git a/lib/AST/LifetimeDependence.cpp b/lib/AST/LifetimeDependence.cpp index 10d57fa1e0db..502c46580040 100644 --- a/lib/AST/LifetimeDependence.cpp +++ b/lib/AST/LifetimeDependence.cpp @@ -880,7 +880,7 @@ class LifetimeDependenceChecker { if (!targetDeclAndIndex->first->isInOut()) { diagnose(targetDeclAndIndex->first, diag::lifetime_parameter_requires_inout, - targetDescriptor->getName()); + targetDescriptor->getString()); } targetIndex = targetDeclAndIndex->second; } else { diff --git a/test/Sema/lifetime_attr.swift b/test/Sema/lifetime_attr.swift index ea56b423f177..ef8b16fffe08 100644 --- a/test/Sema/lifetime_attr.swift +++ b/test/Sema/lifetime_attr.swift @@ -91,7 +91,7 @@ do { // rdar://146401190 ([nonescapable] implement non-inout parameter dependencies) @_lifetime(span: borrow holder) -func testParameterDep(holder: AnyObject, span: Span) {} // expected-error{{lifetime-dependent parameter must be 'inout'}} +func testParameterDep(holder: AnyObject, span: Span) {} // expected-error{{lifetime-dependent parameter 'span' must be 'inout'}} @_lifetime(&ne) func inoutLifetimeDependence(_ ne: inout NE) -> NE { @@ -113,3 +113,16 @@ func dependOnEscapable(_ k: consuming Klass) -> NE { NE() } +struct Wrapper : ~Escapable { + var _ne: NE + + var ne: NE { + @_lifetime(copy self) + get { + _ne + } + @_lifetime(self: &self) + nonmutating _modify {// expected-error{{lifetime-dependent parameter 'self' must be 'inout'}} + } + } +}