Skip to content

SILGen: Fix an assert when emitting SIL for if #available #83035

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 1 commit into from
Jul 15, 2025
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
14 changes: 10 additions & 4 deletions lib/SILGen/SILGenAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,17 @@ SILGenFunction::emitIfAvailableQuery(SILLocation loc,
SILType i1 = SILType::getBuiltinIntegerType(1, ctx);
auto query = availability->getAvailabilityQuery();

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

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

Expand Down
3 changes: 3 additions & 0 deletions test/Parse/availability_query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ if #available(OSX 0.0) { // expected-warning {{expected version number; this is
if #available(OSX 51 { // expected-error {{expected ')'}} expected-note {{to match this opening '('}}
}

if #available(iDishwasherOS 0) { // expected-warning {{expected version number; this is an error in the Swift 6 language mode}}
}

if #available(iDishwasherOS 51) { // expected-warning {{unrecognized platform name 'iDishwasherOS'}}
// expected-error@-1 {{condition required for target platform}}
}
Expand Down
5 changes: 5 additions & 0 deletions test/SILGen/availability_query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ if #available(OSX 10, *) {
if #unavailable(OSX 10) {
}

// CHECK: [[TRUE:%.*]] = integer_literal $Builtin.Int1, -1
// CHECK: cond_br [[TRUE]]
if #available(macOS 0) { // expected-warning {{expected version number}}
}

// CHECK: }

func doThing() {}
Expand Down