From 85df6c22538ec7761d6ecf2e7dad22d698d13672 Mon Sep 17 00:00:00 2001 From: Stuart Montgomery Date: Wed, 23 Apr 2025 10:43:39 -0500 Subject: [PATCH] Fix build failure when building with pre-6.2 toolchain due to unrecognized `unsafe` keyword --- .../Support/EffectfulExpressionHandling.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/TestingMacros/Support/EffectfulExpressionHandling.swift b/Sources/TestingMacros/Support/EffectfulExpressionHandling.swift index f67ca40ee..a70f4ae0f 100644 --- a/Sources/TestingMacros/Support/EffectfulExpressionHandling.swift +++ b/Sources/TestingMacros/Support/EffectfulExpressionHandling.swift @@ -111,7 +111,13 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set, to expr: some Exp let needAwait = effectfulKeywords.contains(.await) && !expr.is(AwaitExprSyntax.self) let needTry = effectfulKeywords.contains(.try) && !expr.is(TryExprSyntax.self) + + // The 'unsafe' keyword was introduced in 6.2 as part of SE-0458. Older + // toolchains are not aware of it, so avoid emitting expressions involving + // that keyword when the macro has been built using an older toolchain. +#if compiler(>=6.2) let needUnsafe = effectfulKeywords.contains(.unsafe) && !expr.is(UnsafeExprSyntax.self) +#endif // First, add thunk function calls. if needAwait { @@ -120,9 +126,11 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set, to expr: some Exp if needTry { expr = _makeCallToEffectfulThunk(.identifier("__requiringTry"), passing: expr) } +#if compiler(>=6.2) if needUnsafe { expr = _makeCallToEffectfulThunk(.identifier("__requiringUnsafe"), passing: expr) } +#endif // Then add keyword expressions. (We do this separately so we end up writing // `try await __r(__r(self))` instead of `try __r(await __r(self))` which is @@ -143,6 +151,7 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set, to expr: some Exp ) ) } +#if compiler(>=6.2) if needUnsafe { expr = ExprSyntax( UnsafeExprSyntax( @@ -151,6 +160,7 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set, to expr: some Exp ) ) } +#endif expr.leadingTrivia = originalExpr.leadingTrivia expr.trailingTrivia = originalExpr.trailingTrivia