Hey guys!
When switching from Swift 6.0 to new Swift version 6.2 (bundled with Xcode 26 beta 2) I noticed a weird error:
<unknown>:0: error: lifetime-dependent variable '$implicit_value' escapes its scope
Screenshot below:
It took me some time, but I found out the code part which makes this error:
/// An abstraction for `Optional<Wrapped>`.
public protocol OptionalProtocol: ExpressibleByNilLiteral {
associatedtype Wrapped
static var none: Self { get }
static func some(_ value: Wrapped) -> Self
/// Unwraps optional.
/// - Returns: Unwrapped value.
func unwrapped() throws -> Wrapped
}
and usage of this protocol in:
extension Optional: OptionalProtocol {
/// Unwraps optional and throws error when `nil` encountered.
/// - Returns: Unwrapped value.
public func unwrapped() throws -> Wrapped {
switch self {
case .none: throw Self.Error.nullValue
case .some(let wrapped): return wrapped
}
}
public enum Error: Swift.Error {
case nullValue
}
}
EDIT
I have noticed this line is causing the real problem:
static func some(_ value: Wrapped) -> Self
/EDIT
The OptionalProtocol
abstraction is used in many places in my code at foundation level of my app (like formatters, property wrappers)
I assume it might be related with ~Copyable
and ~Escapable
conformance, so I did changes but without success:
1st approach
Add constraints to Wrapped
of OptionalProtocol
:
public protocol OptionalProtocol: ExpressibleByNilLiteral where Wrapped: ~Copyable, Wrapped: ~Escapable { ...
This code has returned me followed error:
Cannot suppress 'Copyable' requirement of an associated type
2nd approach
Bypass previous error with protocol aggregator:
public protocol OptionalProtocol: ExpressibleByNilLiteral where Wrapped: OptionalWrapped {
associatedtype Wrapped
static var none: Self { get }
static func some(_ value: Wrapped) -> Self
/// Unwraps optional.
/// - Returns: Unwrapped value.
func unwrapped() throws -> Wrapped
}
public protocol OptionalWrapped: ~Copyable, ~Escapable { }
This code has returned me followed error:
Type 'Optional<Wrapped>' does not conform to protocol 'OptionalProtocol'
Candidate can not infer 'Wrapped' = 'Wrapped' because 'Wrapped' is not a nominal type and so can't conform to 'OptionalWrapped' (Swift.Optional.some)
Candidate can not infer 'Wrapped' = 'Wrapped' because 'Wrapped' is not a nominal type and so can't conform to 'OptionalWrapped'
Is it a bug?
If not, could you advise how to solve this problem to make it working back for Swift 6.2?