Skip to content

Commit 4770078

Browse files
authored
Merge pull request #25593 from rintaro/parse-ifconfig-evaluate-in-inactive-rdar50903021
[Parse] Don't evaluate IfConfig condtion inside inactive block
2 parents 00d7861 + 7b37072 commit 4770078

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) {
@@ -604,7 +610,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
604610
// Parse the condition. Evaluate it to determine the active
605611
// clause unless we're doing a parse-only pass.
606612
if (isElse) {
607-
isActive = !foundActive && State->PerformConditionEvaluation;
613+
isActive = !foundActive && shouldEvaluate;
608614
} else {
609615
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true);
610616
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
@@ -619,7 +625,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
619625
// Error in the condition;
620626
isActive = false;
621627
isVersionCondition = false;
622-
} else if (!foundActive && State->PerformConditionEvaluation) {
628+
} else if (!foundActive && shouldEvaluate) {
623629
// Evaluate the condition only if we haven't found any active one and
624630
// we're not in parse-only mode.
625631
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)