Skip to content

Update Environment.wrappedValue for RELEASE_2023 #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Sources/OpenSwiftUI/Core/Log/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Status: Complete

#if OPENSWIFTUI_SWIFT_LOG
internal import Logging
import Logging
#else
import os
#if DEBUG
Expand All @@ -27,6 +27,7 @@ public let dso = { () -> UnsafeMutableRawPointer in
#endif
#endif

@usableFromInline
enum Log {
static func internalWarning(
_ message: @autoclosure () -> String,
Expand All @@ -37,31 +38,32 @@ enum Log {
}

#if OPENSWIFTUI_SWIFT_LOG
@usableFromInline
static let runtimeIssuesLog = Logger(label: "OpenSwiftUI")

@_transparent
@inline(__always)
static func runtimeIssues(
_ message: @autoclosure () -> StaticString,
_ args: @autoclosure () -> [CVarArg] = []
) {
runtimeIssuesLog.log(level: .critical, "\(message())")
}
#else
static let runtimeIssuesLog = OSLog(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI")


// Audited for RELEASE_2023
@usableFromInline
static var runtimeIssuesLog = OSLog(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI")

@_transparent
@inline(__always)
static func runtimeIssues(
_ message: @autoclosure () -> StaticString,
_ args: @autoclosure () -> [CVarArg] = []
) {
#if DEBUG
let message = message()
unsafeBitCast(
os_log as (OSLogType, UnsafeRawPointer, OSLog, StaticString, CVarArg...) -> Void,
to: ((OSLogType, UnsafeRawPointer, OSLog, StaticString, [CVarArg]) -> Void).self
)(.fault, dso, runtimeIssuesLog, message, args())
)(.fault, dso, runtimeIssuesLog, message(), args())
#else
os_log(.fault, log: runtimeIssuesLog, message(), args())
#endif
Expand Down
31 changes: 31 additions & 0 deletions Sources/OpenSwiftUI/Data/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
// ID: 7B48F30970137591804EEB8D0D309152

internal import OpenGraphShims
#if OPENSWIFTUI_SWIFT_LOG
import Logging
#else
import os
#endif

/// A property wrapper that reads a value from a view's environment.
///
Expand Down Expand Up @@ -189,12 +194,38 @@ public struct Environment<Value>: DynamicProperty {
/// }
/// }
///
// Audited for RELEASE_2023
@inlinable
public var wrappedValue: Value {
switch content {
case let .value(value):
return value
case let .keyPath(keyPath):
#if OPENSWIFTUI_SWIFT_LOG
Log.runtimeIssuesLog.log(level: .critical, """
Accessing Environment<\(Value.self)>'s value outside of \
being installed on a View. \
This will always read the default value \
and will not update.
""")
#else
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) {
os_log(.fault, log: Log.runtimeIssuesLog, """
Accessing Environment<\(Value.self)>'s value outside of \
being installed on a View. \
This will always read the default value \
and will not update.
""")
} else {
os_log(.fault, log: Log.runtimeIssuesLog, """
Accessing Environment's value outside of being \
installed on a View. \
This will always read the default value \
and will not update.
""")
}
#endif
// not bound to a view, return the default value.
return EnvironmentValues()[keyPath: keyPath]
}
}
Expand Down