Skip to content

Commit 569859a

Browse files
committed
Add hasFeature(X) to check for future features
1 parent a317fad commit 569859a

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/Parse/ParseIfConfig.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ class ValidateIfConfigCondition :
329329
return E;
330330
}
331331

332+
if (*KindName == "hasFeature") {
333+
if (!getDeclRefStr(Arg, DeclRefKind::Ordinary)) {
334+
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_argument,
335+
"feature name");
336+
return nullptr;
337+
}
338+
339+
return E;
340+
}
341+
332342
// ( 'os' | 'arch' | '_endian' | '_runtime' ) '(' identifier ')''
333343
auto Kind = getPlatformConditionKind(*KindName);
334344
if (!Kind.hasValue()) {
@@ -531,6 +541,9 @@ class EvaluateIfConfigCondition :
531541
ImportPath::Module::Builder builder(Ctx, Str, /*separator=*/'.',
532542
Arg->getStartLoc());
533543
return Ctx.canImportModule(builder.get(), version, underlyingModule);
544+
} else if (KindName == "hasFeature") {
545+
auto featureName = getDeclRefStr(Arg);
546+
return Ctx.LangOpts.hasFeature(featureName);
534547
}
535548

536549
auto Val = getDeclRefStr(Arg);

test/Frontend/upcoming_feature.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
// Make sure that hasFeature(ConciseMagicFile) evaluates true when provided
2+
// explicitly.
3+
// RUN: %target-swift-frontend -typecheck -enable-upcoming-feature ConciseMagicFile %s
4+
5+
// Make sure that hasFeature(ConciseMagicFile) evaluates true in Swift 6.
6+
// RUN: %target-swift-frontend -typecheck -swift-version 6 %s
7+
8+
// Make sure that hasFeature(ConciseMagicFile) is off prior to Swift 6
9+
// RUN: %target-typecheck-verify-swift %s
110

211
// It's fine to provide a feature that we don't know about
3-
// RUN: %target-swift-frontend -typecheck -enable-upcoming-feature UnknownFeature %s
12+
// RUN: %target-swift-frontend -typecheck -enable-upcoming-feature ConciseMagicFile -enable-upcoming-feature UnknownFeature %s
13+
// RUN: %target-swift-frontend -typecheck -enable-upcoming-feature UnknownFeature -enable-upcoming-feature ConciseMagicFile %s
14+
415

516
// It's not fine to provide a feature that's in the specified language version.
617
// RUN: not %target-swift-frontend -typecheck -enable-upcoming-feature ConciseMagicFile -swift-version 6 %s 2>&1 | %FileCheck %s
718
// REQUIRES: asserts
819

920
// CHECK: error: upcoming feature 'ConciseMagicFile' is already enabled as of Swift version 6
21+
22+
#if hasFeature(ConciseMagicFile)
23+
let x = 0
24+
#else
25+
let y = boom // expected-error{{'boom'}}
26+
#endif

test/Parse/upcoming_feature.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// expected-error@+1{{unexpected platform condition argument: expected feature name}}
4+
#if hasFeature(17)
5+
#endif

0 commit comments

Comments
 (0)