Skip to content

Commit 5a6a3e0

Browse files
committed
Update Environment.wrappedValue for RELEASE_2023
1 parent 2da57e3 commit 5a6a3e0

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

Sources/OpenSwiftUI/Core/Log/Log.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -48,20 +49,22 @@ enum Log {
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
5458
@inline(__always)
5559
static func runtimeIssues(
5660
_ message: @autoclosure () -> StaticString,
5761
_ args: @autoclosure () -> [CVarArg] = []
5862
) {
5963
#if DEBUG
60-
let message = message()
6164
unsafeBitCast(
6265
os_log as (OSLogType, UnsafeRawPointer, OSLog, StaticString, CVarArg...) -> Void,
6366
to: ((OSLogType, UnsafeRawPointer, OSLog, StaticString, [CVarArg]) -> Void).self
64-
)(.fault, dso, runtimeIssuesLog, message, args())
67+
)(.fault, dso, runtimeIssuesLog, message(), args())
6568
#else
6669
os_log(.fault, log: runtimeIssuesLog, message(), args())
6770
#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+
internal 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)