Skip to content

Commit 75506a1

Browse files
authored
Merge pull request #85413 from kubamracek/section-convention-c
Allow function conversions to `@convention(c)` in `@section` expressions
2 parents de51fed + 4e919fb commit 75506a1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/Sema/LegalConstExprVerifier.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,18 @@ checkSupportedWithSectionAttribute(const Expr *expr,
226226
if (isa<AbstractClosureExpr>(expr))
227227
return std::make_pair(expr, Closure);
228228

229-
// Function conversions are not allowed in constant expressions
230-
if (isa<FunctionConversionExpr>(expr))
229+
// Function conversions are allowed if the conversion is to '@convention(c)'
230+
if (auto functionConvExpr = dyn_cast<FunctionConversionExpr>(expr)) {
231+
if (auto targetFnTy =
232+
functionConvExpr->getType()->getAs<AnyFunctionType>()) {
233+
if (targetFnTy->getExtInfo().getRepresentation() ==
234+
FunctionTypeRepresentation::CFunctionPointer) {
235+
expressionsToCheck.push_back(functionConvExpr->getSubExpr());
236+
continue;
237+
}
238+
}
231239
return std::make_pair(expr, Default);
240+
}
232241

233242
// Direct references to non-generic functions are allowed
234243
if (const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(expr)) {

test/ConstValues/SectionSyntactic.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func bar(x: Int) -> String { return "test" }
4646
// function references
4747
@section("mysection") let funcRef1 = foo // ok
4848
@section("mysection") let funcRef2 = bar // ok
49+
@section("mysection") let funcRef3: ()->Int = foo // ok
50+
@section("mysection") let funcRef4: @convention(c) ()->Int = foo // ok
4951

5052
// invalid function references (should be rejected)
5153
@section("mysection") let invalidFuncRef1 = foo()

0 commit comments

Comments
 (0)