Skip to content

Commit 71ba777

Browse files
authored
Update Environment.wrappedValue for RELEASE_2023 (#77)
* Update Environment.wrappedValue for RELEASE_2023 * Fix internal import issue for OPENSWIFTUI_SWIFT_LOG=1
1 parent 0d22c7e commit 71ba777

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

Sources/OpenSwiftUI/Core/Log/Log.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Status: Complete
77

88
#if OPENSWIFTUI_SWIFT_LOG
9-
internal import Logging
9+
import Logging
1010
#else
1111
import os
1212
#if DEBUG
@@ -27,6 +27,7 @@ public let dso = { () -> UnsafeMutableRawPointer in
2727
#endif
2828
#endif
2929

30+
@usableFromInline
3031
enum Log {
3132
static func internalWarning(
3233
_ message: @autoclosure () -> String,
@@ -37,31 +38,32 @@ enum Log {
3738
}
3839

3940
#if OPENSWIFTUI_SWIFT_LOG
41+
@usableFromInline
4042
static let runtimeIssuesLog = Logger(label: "OpenSwiftUI")
4143

4244
@_transparent
43-
@inline(__always)
4445
static func runtimeIssues(
4546
_ message: @autoclosure () -> StaticString,
4647
_ args: @autoclosure () -> [CVarArg] = []
4748
) {
4849
runtimeIssuesLog.log(level: .critical, "\(message())")
4950
}
5051
#else
51-
static let runtimeIssuesLog = OSLog(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI")
52-
52+
53+
// Audited for RELEASE_2023
54+
@usableFromInline
55+
static var runtimeIssuesLog = OSLog(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI")
56+
5357
@_transparent
54-
@inline(__always)
5558
static func runtimeIssues(
5659
_ message: @autoclosure () -> StaticString,
5760
_ args: @autoclosure () -> [CVarArg] = []
5861
) {
5962
#if DEBUG
60-
let message = message()
6163
unsafeBitCast(
6264
os_log as (OSLogType, UnsafeRawPointer, OSLog, StaticString, CVarArg...) -> Void,
6365
to: ((OSLogType, UnsafeRawPointer, OSLog, StaticString, [CVarArg]) -> Void).self
64-
)(.fault, dso, runtimeIssuesLog, message, args())
66+
)(.fault, dso, runtimeIssuesLog, message(), args())
6567
#else
6668
os_log(.fault, log: runtimeIssuesLog, message(), args())
6769
#endif

Sources/OpenSwiftUI/Data/Environment/Environment.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
// ID: 7B48F30970137591804EEB8D0D309152
88

99
internal import OpenGraphShims
10+
#if OPENSWIFTUI_SWIFT_LOG
11+
import Logging
12+
#else
13+
import os
14+
#endif
1015

1116
/// A property wrapper that reads a value from a view's environment.
1217
///
@@ -189,12 +194,38 @@ public struct Environment<Value>: DynamicProperty {
189194
/// }
190195
/// }
191196
///
197+
// Audited for RELEASE_2023
192198
@inlinable
193199
public var wrappedValue: Value {
194200
switch content {
195201
case let .value(value):
196202
return value
197203
case let .keyPath(keyPath):
204+
#if OPENSWIFTUI_SWIFT_LOG
205+
Log.runtimeIssuesLog.log(level: .critical, """
206+
Accessing Environment<\(Value.self)>'s value outside of \
207+
being installed on a View. \
208+
This will always read the default value \
209+
and will not update.
210+
""")
211+
#else
212+
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) {
213+
os_log(.fault, log: Log.runtimeIssuesLog, """
214+
Accessing Environment<\(Value.self)>'s value outside of \
215+
being installed on a View. \
216+
This will always read the default value \
217+
and will not update.
218+
""")
219+
} else {
220+
os_log(.fault, log: Log.runtimeIssuesLog, """
221+
Accessing Environment's value outside of being \
222+
installed on a View. \
223+
This will always read the default value \
224+
and will not update.
225+
""")
226+
}
227+
#endif
228+
// not bound to a view, return the default value.
198229
return EnvironmentValues()[keyPath: keyPath]
199230
}
200231
}

0 commit comments

Comments
 (0)