@@ -1939,12 +1939,23 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
19391939 // Check the running OS version to determine whether it is in the range
19401940 // specified by elt.
19411941 PoundAvailableInfo *availability = elt.getAvailability ();
1942- VersionRange OSVersion = availability->getAvailableRange ();
1942+
1943+ // Creates a boolean literal for availability conditions that have been
1944+ // evaluated at compile time. Automatically inverts the value for
1945+ // `#unavailable` queries.
1946+ auto createBooleanTestLiteral = [&](bool value) {
1947+ SILType i1 = SILType::getBuiltinIntegerType (1 , getASTContext ());
1948+ if (availability->isUnavailability ())
1949+ value = !value;
1950+ return B.createIntegerLiteral (loc, i1, value);
1951+ };
1952+
1953+ auto versionRange = availability->getAvailableRange ();
19431954
19441955 // The OS version might be left empty if availability checking was
19451956 // disabled. Treat it as always-true in that case.
1946- assert (!OSVersion. isEmpty ()
1947- || getASTContext ().LangOpts .DisableAvailabilityChecking );
1957+ assert (versionRange. has_value () ||
1958+ getASTContext ().LangOpts .DisableAvailabilityChecking );
19481959
19491960 if (getASTContext ().LangOpts .TargetVariant &&
19501961 !getASTContext ().LangOpts .DisableAvailabilityChecking ) {
@@ -1954,25 +1965,34 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
19541965 // into. In a macOS process it will use the macOS version; in an
19551966 // macCatalyst process it will use the iOS version.
19561967
1957- VersionRange VariantOSVersion =
1968+ auto variantVersionRange =
19581969 elt.getAvailability ()->getVariantAvailableRange ();
1959- assert (!VariantOSVersion.isEmpty ());
1960- booleanTestValue =
1961- emitZipperedOSVersionRangeCheck (loc, OSVersion, VariantOSVersion);
1970+ assert (variantVersionRange.has_value ());
1971+
1972+ if (versionRange && variantVersionRange) {
1973+ booleanTestValue = emitZipperedOSVersionRangeCheck (
1974+ loc, *versionRange, *variantVersionRange);
1975+ } else {
1976+ // Type checking did not fill in versions so as a fallback treat this
1977+ // condition as trivially true.
1978+ booleanTestValue = createBooleanTestLiteral (true );
1979+ }
19621980 break ;
19631981 }
19641982
1965- if (OSVersion.isEmpty () || OSVersion.isAll ()) {
1966- // If there's no check for the current platform, this condition is
1967- // trivially true (or false, for unavailability).
1968- SILType i1 = SILType::getBuiltinIntegerType (1 , getASTContext ());
1969- bool value = !availability->isUnavailability ();
1970- booleanTestValue = B.createIntegerLiteral (loc, i1, value);
1983+ if (!versionRange) {
1984+ // Type checking did not fill in version so as a fallback treat this
1985+ // condition as trivially true.
1986+ booleanTestValue = createBooleanTestLiteral (true );
1987+ } else if (versionRange->isAll ()) {
1988+ booleanTestValue = createBooleanTestLiteral (true );
1989+ } else if (versionRange->isEmpty ()) {
1990+ booleanTestValue = createBooleanTestLiteral (false );
19711991 } else {
19721992 bool isMacCatalyst =
19731993 tripleIsMacCatalystEnvironment (getASTContext ().LangOpts .Target );
1974- booleanTestValue = emitOSVersionRangeCheck (loc, OSVersion,
1975- isMacCatalyst);
1994+ booleanTestValue =
1995+ emitOSVersionRangeCheck (loc, versionRange. value (), isMacCatalyst);
19761996 if (availability->isUnavailability ()) {
19771997 // If this is an unavailability check, invert the result
19781998 // by emitting a call to Builtin.xor_Int1(lhs, -1).
0 commit comments