Skip to content

Commit fbe686d

Browse files
committed
SILGen: Fix an assert when emitting SIL for if #available.
Empty version numbers if availability specs are accepted by Sema prior to Swift 6, and when they occur in `if #available` queries they can make the query AST node invalid without aborting compilation. Adjust an assertion in SILGen to compensate.
1 parent 8dfff8c commit fbe686d

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

lib/SILGen/SILGenAvailability.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,17 @@ SILGenFunction::emitIfAvailableQuery(SILLocation loc,
265265
SILType i1 = SILType::getBuiltinIntegerType(1, ctx);
266266
auto query = availability->getAvailabilityQuery();
267267

268-
// The query might not have been computed by Sema if availability checking
269-
// was disabled. Otherwise, there's a bug in Sema.
270-
DEBUG_ASSERT(query || ctx.LangOpts.DisableAvailabilityChecking);
268+
// The query may not have been computed by Sema under the following
269+
// conditions:
270+
// - Availability checking was disabled (-disable-availabilty-checking).
271+
// - The query was marked invalid in the AST for a non-fatal reason.
272+
//
273+
// Otherwise, there's a bug in Sema.
274+
DEBUG_ASSERT(query || availability->isInvalid() ||
275+
ctx.LangOpts.DisableAvailabilityChecking);
271276

272-
// Treat the condition as if it always evaluates true if Sema skipped it.
277+
// If Sema skipped the query then treat the condition as if it always
278+
// evaluates true.
273279
if (!query)
274280
return B.createIntegerLiteral(loc, i1, !availability->isUnavailability());
275281

test/Parse/availability_query.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ if #available(OSX 0.0) { // expected-warning {{expected version number; this is
4545
if #available(OSX 51 { // expected-error {{expected ')'}} expected-note {{to match this opening '('}}
4646
}
4747

48+
if #available(iDishwasherOS 0) { // expected-warning {{expected version number; this is an error in the Swift 6 language mode}}
49+
}
50+
4851
if #available(iDishwasherOS 51) { // expected-warning {{unrecognized platform name 'iDishwasherOS'}}
4952
// expected-error@-1 {{condition required for target platform}}
5053
}

test/SILGen/availability_query.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ if #available(OSX 10, *) {
109109
if #unavailable(OSX 10) {
110110
}
111111

112+
// CHECK: [[TRUE:%.*]] = integer_literal $Builtin.Int1, -1
113+
// CHECK: cond_br [[TRUE]]
114+
if #available(macOS 0) { // expected-warning {{expected version number}}
115+
}
116+
112117
// CHECK: }
113118

114119
func doThing() {}

0 commit comments

Comments
 (0)