@@ -1939,12 +1939,23 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
1939
1939
// Check the running OS version to determine whether it is in the range
1940
1940
// specified by elt.
1941
1941
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 ();
1943
1954
1944
1955
// The OS version might be left empty if availability checking was
1945
1956
// 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 );
1948
1959
1949
1960
if (getASTContext ().LangOpts .TargetVariant &&
1950
1961
!getASTContext ().LangOpts .DisableAvailabilityChecking ) {
@@ -1954,25 +1965,34 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
1954
1965
// into. In a macOS process it will use the macOS version; in an
1955
1966
// macCatalyst process it will use the iOS version.
1956
1967
1957
- VersionRange VariantOSVersion =
1968
+ auto variantVersionRange =
1958
1969
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
+ }
1962
1980
break ;
1963
1981
}
1964
1982
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 );
1971
1991
} else {
1972
1992
bool isMacCatalyst =
1973
1993
tripleIsMacCatalystEnvironment (getASTContext ().LangOpts .Target );
1974
- booleanTestValue = emitOSVersionRangeCheck (loc, OSVersion,
1975
- isMacCatalyst);
1994
+ booleanTestValue =
1995
+ emitOSVersionRangeCheck (loc, versionRange. value (), isMacCatalyst);
1976
1996
if (availability->isUnavailability ()) {
1977
1997
// If this is an unavailability check, invert the result
1978
1998
// by emitting a call to Builtin.xor_Int1(lhs, -1).
0 commit comments