Skip to content

Commit c755369

Browse files
committed
Add hasFeature(X) to check for future features
1 parent c96da08 commit c755369

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-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/future_feature.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11

2+
// Make sure that hasFeature(ConciseMagicFile) evaluates true when provided
3+
// explicitly.
4+
// RUN: %target-swift-frontend -typecheck -enable-future-feature ConciseMagicFile %s
5+
6+
// Make sure that hasFeature(ConciseMagicFile) evaluates true in Swift 6.
7+
// RUN: %target-swift-frontend -typecheck -swift-version 6 %s
8+
9+
// Make sure that hasFeature(ConciseMagicFile) is off prior to Swift 6
10+
// RUN: %target-typecheck-verify-swift %s
11+
212
// It's fine to provide a feature that we don't know about
3-
// RUN: %target-swift-frontend -typecheck -enable-future-feature UnknownFeature %s
13+
// RUN: %target-swift-frontend -typecheck -enable-future-feature ConciseMagicFile -enable-future-feature UnknownFeature %s
14+
// RUN: %target-swift-frontend -typecheck -enable-future-feature UnknownFeature -enable-future-feature ConciseMagicFile %s
15+
416

517
// It's not fine to provide a feature that's in the specified language version.
618
// RUN: not %target-swift-frontend -typecheck -enable-future-feature ConciseMagicFile -swift-version 6 %s 2>&1 | %FileCheck %s
719
// REQUIRES: asserts
820

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

test/Parse/future_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)