Skip to content

[Parse] Don't evaluate IfConfig condtion inside inactive block #25593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,12 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
Parser::StructureMarkerRAII ParsingDecl(
*this, Tok.getLoc(), Parser::StructureMarkerKind::IfConfig);

bool shouldEvaluate =
// Don't evaluate if it's in '-parse' mode, etc.
State->PerformConditionEvaluation &&
// If it's in inactive #if ... #endif block, there's no point to do it.
!getScopeInfo().isInactiveConfigBlock();

bool foundActive = false;
bool isVersionCondition = false;
while (1) {
Expand All @@ -604,7 +610,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
// Parse the condition. Evaluate it to determine the active
// clause unless we're doing a parse-only pass.
if (isElse) {
isActive = !foundActive && State->PerformConditionEvaluation;
isActive = !foundActive && shouldEvaluate;
} else {
llvm::SaveAndRestore<bool> S(InPoundIfEnvironment, true);
ParserResult<Expr> Result = parseExprSequence(diag::expected_expr,
Expand All @@ -619,7 +625,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
// Error in the condition;
isActive = false;
isVersionCondition = false;
} else if (!foundActive && State->PerformConditionEvaluation) {
} else if (!foundActive && shouldEvaluate) {
// Evaluate the condition only if we haven't found any active one and
// we're not in parse-only mode.
isActive = evaluateIfConfigCondition(Condition, Context);
Expand Down
27 changes: 27 additions & 0 deletions test/Parse/ConditionalCompilation/canimport_in_inactive.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %empty-directory(%t)
// RUN: mkdir -p %t/Modules/SomeModule.swiftmodule
// RUN: echo 'DUMMY' > %t/Modules/SomeModule.swiftmodule/i386.swiftmodule
// RUN: not %target-swift-frontend -typecheck -I %t/Modules %s 2>&1 | %FileCheck %s

// REQUIRES: CPU=x86_64

// Testing 'canImport()' not emitting error in inactive #if .. #endif blocks.

#if false
#if canImport(SomeModule) // Ok
// CHECK-NOT: :[[@LINE-1]]:
#endif
#endif

#if true
#else
#if canImport(SomeModule) // Ok
// CHECK-NOT: :[[@LINE-1]]:
#endif
#endif

#if canImport(SomeModule)
// CHECK: :[[@LINE-1]]:5: error: could not find module 'SomeModule' for target '{{.*}}'; found: i386
#endif

import SomeModule