Skip to content

Commit 9c62890

Browse files
committed
Sema: Temporarily reject if #_hasSymbol in a closure context.
We'll need to implement appropriate constraint generation to support them in such a context.
1 parent ba46170 commit 9c62890

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6586,6 +6586,8 @@ WARNING(has_symbol_decl_must_be_weak,none,
65866586
(DescriptiveDeclKind, DeclName))
65876587
ERROR(has_symbol_invalid_expr,none,
65886588
"#_hasSymbol condition must refer to a declaration", ())
6589+
ERROR(has_symbol_unsupported_in_closures,none,
6590+
"#_hasSymbol is not supported in closures", ())
65896591

65906592
#define UNDEFINE_DIAGNOSTIC_MACROS
65916593
#include "DefineDiagnosticMacros.h"

lib/Sema/CSGen.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4365,10 +4365,16 @@ bool ConstraintSystem::generateConstraints(StmtCondition condition,
43654365
for (const auto &condElement : condition) {
43664366
switch (condElement.getKind()) {
43674367
case StmtConditionElement::CK_Availability:
4368-
case StmtConditionElement::CK_HasSymbol:
43694368
// Nothing to do here.
43704369
continue;
43714370

4371+
case StmtConditionElement::CK_HasSymbol: {
4372+
ASTContext &ctx = getASTContext();
4373+
ctx.Diags.diagnose(condElement.getStartLoc(),
4374+
diag::has_symbol_unsupported_in_closures);
4375+
return true;
4376+
}
4377+
43724378
case StmtConditionElement::CK_Boolean: {
43734379
Expr *condExpr = condElement.getBoolean();
43744380
setContextualType(condExpr, TypeLoc::withoutLoc(boolTy), CTP_Condition);

test/Sema/has_symbol.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/has_symbol_helper.swiftmodule -parse-as-library %S/Inputs/has_symbol_helper.swift -enable-library-evolution
3-
// RUN: %target-typecheck-verify-swift -I %t
3+
// RUN: %target-typecheck-verify-swift -disable-availability-checking -I %t
44

55
// UNSUPPORTED: OS=windows-msvc
66

@@ -100,3 +100,35 @@ func testInvalidExpressionsDiagnostics() {
100100
if #_hasSymbol(1 as Int) {} // expected-error {{#_hasSymbol condition must refer to a declaration}}
101101
if #_hasSymbol(1 as S) {} // expected-error {{cannot convert value of type 'Int' to type 'S' in coercion}}
102102
}
103+
104+
func testMultiStatementClosure() {
105+
let _: () -> Void = { // expected-error {{unable to infer closure type in the current context}}
106+
if #_hasSymbol(global) {} // expected-error 2 {{#_hasSymbol is not supported in closures}}
107+
}
108+
109+
let _: () -> Void = { // expected-error {{unable to infer closure type in the current context}}
110+
if #_hasSymbol(global) {} // expected-error 2 {{#_hasSymbol is not supported in closures}}
111+
localFunc()
112+
}
113+
}
114+
115+
protocol View {}
116+
117+
@resultBuilder struct ViewBuilder {
118+
static func buildBlock<Content>(_ content: Content) -> Content where Content : View { fatalError() }
119+
static func buildEither<Content>(first content: Content) -> Content where Content : View { fatalError() }
120+
static func buildEither<Content>(second content: Content) -> Content where Content : View { fatalError() }
121+
}
122+
123+
struct Image : View {
124+
}
125+
126+
struct MyView {
127+
@ViewBuilder var body: some View {
128+
if #_hasSymbol(global) { // expected-error {{#_hasSymbol is not supported in closures}}
129+
Image()
130+
} else {
131+
Image()
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)