Skip to content

Commit cacb295

Browse files
authored
Fix build failure when building with pre-6.2 toolchain due to unrecognized unsafe keyword (#1093)
This fixes another bit of fallout from #1069 when building this project's test targets with a 6.1 (or any pre-6.2) toolchain. The `unsafe` keyword was introduced in 6.2 as part of [SE-0458: Opt-in Strict Memory Safety Checking](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0458-strict-memory-safety.md). Older toolchains are not aware of it, so the fix is to avoid emitting expressions involving that keyword when the macro plugin has been built using an older toolchain. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 696d9b9 commit cacb295

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/TestingMacros/Support/EffectfulExpressionHandling.swift

+10
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp
111111

112112
let needAwait = effectfulKeywords.contains(.await) && !expr.is(AwaitExprSyntax.self)
113113
let needTry = effectfulKeywords.contains(.try) && !expr.is(TryExprSyntax.self)
114+
115+
// The 'unsafe' keyword was introduced in 6.2 as part of SE-0458. Older
116+
// toolchains are not aware of it, so avoid emitting expressions involving
117+
// that keyword when the macro has been built using an older toolchain.
118+
#if compiler(>=6.2)
114119
let needUnsafe = effectfulKeywords.contains(.unsafe) && !expr.is(UnsafeExprSyntax.self)
120+
#endif
115121

116122
// First, add thunk function calls.
117123
if needAwait {
@@ -120,9 +126,11 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp
120126
if needTry {
121127
expr = _makeCallToEffectfulThunk(.identifier("__requiringTry"), passing: expr)
122128
}
129+
#if compiler(>=6.2)
123130
if needUnsafe {
124131
expr = _makeCallToEffectfulThunk(.identifier("__requiringUnsafe"), passing: expr)
125132
}
133+
#endif
126134

127135
// Then add keyword expressions. (We do this separately so we end up writing
128136
// `try await __r(__r(self))` instead of `try __r(await __r(self))` which is
@@ -143,6 +151,7 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp
143151
)
144152
)
145153
}
154+
#if compiler(>=6.2)
146155
if needUnsafe {
147156
expr = ExprSyntax(
148157
UnsafeExprSyntax(
@@ -151,6 +160,7 @@ func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some Exp
151160
)
152161
)
153162
}
163+
#endif
154164

155165
expr.leadingTrivia = originalExpr.leadingTrivia
156166
expr.trailingTrivia = originalExpr.trailingTrivia

0 commit comments

Comments
 (0)