Open
Description
Previous ID | SR-13142 |
Radar | rdar://problem/65115564 |
Original Reporter | akaralar (JIRA User) |
Type | Bug |
Environment
Apple Swift version 5.3 (swiftlang-1200.0.16.9 clang-1200.0.22.5)
Target: x86_64-apple-darwin19.5.0
macOS Catalina 10.15.5 (19F101)
Xcode Version 12.0 beta (12A6159)
Additional Detail from JIRA
Votes | 1 |
Component/s | |
Labels | Bug |
Assignee | None |
Priority | Medium |
md5: 1c30f84090b7a2a317091e8ff055868b
Issue Description:
I'm trying to implement a property wrapper `LazyConstant` using `Lazy` property wrapper in the SE proposal.
Lazy
@propertyWrapper
public enum Lazy<Value> {
case uninitialized(() -> Value)
case initialized(Value)
public init(wrappedValue: @autoclosure @escaping () -> Value) {
self = .uninitialized(wrappedValue)
}
public var wrappedValue: Value {
mutating get {
switch self {
case let .uninitialized(initializer):
let value = initializer()
self = .initialized(value)
return value
case let .initialized(value):
return value
}
}
set {
self = .initialized(newValue)
}
}
}
LazyConstant
@propertyWrapper
public struct LazyConstant<Value> {
@Lazy private var storage: Value
public init(wrappedValue: @autoclosure @escaping () -> Value) {
storage = wrappedValue() // -> compile error here, 1- Return from initializer without initializing all stored properties, 2- 'self' used before all stored properties are initialized
}
public var wrappedValue: Value {
mutating get {
return storage
}
}
}
However if I change the line with the error to be
_storage = Lazy(wrappedValue: wrappedValue())
it compiles and functions correctly.
Metadata
Metadata
Assignees
Labels
Feature → attributes: the @autoclosure type attributeArea → compiler: The SIL generation stageFeature: Declaration and type attributesA deviation from expected or documented behavior. Also: expected but undesirable behavior.The Swift compiler itselfArea → compiler → SIL: Definite initializationFeature: property wrappersBug: Unexpected error