Skip to content

Commit 0796cdc

Browse files
authored
Merge pull request #25612 from rintaro/5.1-parse-ifconfig-evaluate-in-inactive-rdar50903021
[5.1][Parse] Don't evaluate IfConfig condtion inside inactive block
2 parents 3a6c5eb + 5b8812e commit 0796cdc

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/Parse/ParseIfConfig.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,12 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
584584
Parser::StructureMarkerRAII ParsingDecl(
585585
*this, Tok.getLoc(), Parser::StructureMarkerKind::IfConfig);
586586

587+
bool shouldEvaluate =
588+
// Don't evaluate if it's in '-parse' mode, etc.
589+
State->PerformConditionEvaluation &&
590+
// If it's in inactive #if ... #endif block, there's no point to do it.
591+
!getScopeInfo().isInactiveConfigBlock();
592+
587593
bool foundActive = false;
588594
bool isVersionCondition = false;
589595
while (1) {
@@ -598,7 +604,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
598604
// Parse the condition. Evaluate it to determine the active
599605
// clause unless we're doing a parse-only pass.
600606
if (isElse) {
601-
isActive = !foundActive && State->PerformConditionEvaluation;
607+
isActive = !foundActive && shouldEvaluate;
602608
} else {
603609
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true);
604610
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
@@ -613,7 +619,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
613619
// Error in the condition;
614620
isActive = false;
615621
isVersionCondition = false;
616-
} else if (!foundActive && State->PerformConditionEvaluation) {
622+
} else if (!foundActive && shouldEvaluate) {
617623
// Evaluate the condition only if we haven't found any active one and
618624
// we're not in parse-only mode.
619625
isActive = evaluateIfConfigCondition(Condition, Context);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t/Modules/SomeModule.swiftmodule
3+
// RUN: echo 'DUMMY' > %t/Modules/SomeModule.swiftmodule/i386.swiftmodule
4+
// RUN: not %target-swift-frontend -typecheck -I %t/Modules %s 2>&1 | %FileCheck %s
5+
6+
// REQUIRES: CPU=x86_64
7+
8+
// Testing 'canImport()' not emitting error in inactive #if .. #endif blocks.
9+
10+
#if false
11+
#if canImport(SomeModule) // Ok
12+
// CHECK-NOT: :[[@LINE-1]]:
13+
#endif
14+
#endif
15+
16+
#if true
17+
#else
18+
#if canImport(SomeModule) // Ok
19+
// CHECK-NOT: :[[@LINE-1]]:
20+
#endif
21+
#endif
22+
23+
#if canImport(SomeModule)
24+
// CHECK: :[[@LINE-1]]:5: error: could not find module 'SomeModule' for target '{{.*}}'; found: i386
25+
#endif
26+
27+
import SomeModule

0 commit comments

Comments
 (0)