From 5a6a3e0fc4e930de498817bfbca0196f5ab5ab88 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 21 Apr 2024 19:15:33 +0800 Subject: [PATCH 1/2] Update Environment.wrappedValue for RELEASE_2023 --- Sources/OpenSwiftUI/Core/Log/Log.swift | 11 ++++--- .../Data/Environment/Environment.swift | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Sources/OpenSwiftUI/Core/Log/Log.swift b/Sources/OpenSwiftUI/Core/Log/Log.swift index e8737f07..1b8660bf 100644 --- a/Sources/OpenSwiftUI/Core/Log/Log.swift +++ b/Sources/OpenSwiftUI/Core/Log/Log.swift @@ -27,6 +27,7 @@ public let dso = { () -> UnsafeMutableRawPointer in #endif #endif +@usableFromInline enum Log { static func internalWarning( _ message: @autoclosure () -> String, @@ -48,8 +49,11 @@ enum Log { 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( @@ -57,11 +61,10 @@ enum Log { _ 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 diff --git a/Sources/OpenSwiftUI/Data/Environment/Environment.swift b/Sources/OpenSwiftUI/Data/Environment/Environment.swift index 2fe799a8..edda9159 100644 --- a/Sources/OpenSwiftUI/Data/Environment/Environment.swift +++ b/Sources/OpenSwiftUI/Data/Environment/Environment.swift @@ -7,6 +7,11 @@ // ID: 7B48F30970137591804EEB8D0D309152 internal import OpenGraphShims +#if OPENSWIFTUI_SWIFT_LOG +internal import Logging +#else +import os +#endif /// A property wrapper that reads a value from a view's environment. /// @@ -189,12 +194,38 @@ public struct Environment: 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] } } From eb2900165a707f2b43817579b9711e378df18355 Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 22 Apr 2024 13:24:22 +0800 Subject: [PATCH 2/2] Fix internal import issue for OPENSWIFTUI_SWIFT_LOG=1 --- Sources/OpenSwiftUI/Core/Log/Log.swift | 5 ++--- Sources/OpenSwiftUI/Data/Environment/Environment.swift | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Sources/OpenSwiftUI/Core/Log/Log.swift b/Sources/OpenSwiftUI/Core/Log/Log.swift index 1b8660bf..76b56872 100644 --- a/Sources/OpenSwiftUI/Core/Log/Log.swift +++ b/Sources/OpenSwiftUI/Core/Log/Log.swift @@ -6,7 +6,7 @@ // Status: Complete #if OPENSWIFTUI_SWIFT_LOG -internal import Logging +import Logging #else import os #if DEBUG @@ -38,10 +38,10 @@ 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] = [] @@ -55,7 +55,6 @@ enum Log { static var runtimeIssuesLog = OSLog(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI") @_transparent - @inline(__always) static func runtimeIssues( _ message: @autoclosure () -> StaticString, _ args: @autoclosure () -> [CVarArg] = [] diff --git a/Sources/OpenSwiftUI/Data/Environment/Environment.swift b/Sources/OpenSwiftUI/Data/Environment/Environment.swift index edda9159..d5c9dd69 100644 --- a/Sources/OpenSwiftUI/Data/Environment/Environment.swift +++ b/Sources/OpenSwiftUI/Data/Environment/Environment.swift @@ -8,7 +8,7 @@ internal import OpenGraphShims #if OPENSWIFTUI_SWIFT_LOG -internal import Logging +import Logging #else import os #endif