Skip to content

Commit ebda3e2

Browse files
committed
Sema: Diagnose @backDeployed functions with missing bodies in swiftinterfaces.
A `@backDeployed` function printed in a `.swiftinterface` must have a function body so that SILGen can emit a fallback copy to call when the back deployed function is unavailable. Previously, the compiler would crash in SILGen when compiling an interface containing a back deployed function without a body. Resolves rdar://141593108.
1 parent e87c1c3 commit ebda3e2

File tree

6 files changed

+83
-29
lines changed

6 files changed

+83
-29
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7570,13 +7570,17 @@ ERROR(cannot_convert_default_value_type_to_argument_type, none,
75707570
// MARK: Back deployment
75717571
//------------------------------------------------------------------------------
75727572

7573-
ERROR(attr_incompatible_with_back_deploy,none,
7574-
"'%0' cannot be applied to a back deployed %1",
7575-
(DeclAttribute, DescriptiveDeclKind))
7573+
ERROR(attr_incompatible_with_back_deployed,none,
7574+
"'%0' cannot be applied to a back deployed %kind1",
7575+
(DeclAttribute, const Decl *))
75767576

7577-
WARNING(backdeployed_opaque_result_not_supported,none,
7578-
"'%0' is unsupported on a %1 with a 'some' return type",
7579-
(DeclAttribute, DescriptiveDeclKind))
7577+
WARNING(back_deployed_opaque_result_not_supported,none,
7578+
"'%0' cannot be applied to %kind1 because it has a 'some' return type",
7579+
(DeclAttribute, const ValueDecl *))
7580+
7581+
ERROR(back_deployed_requires_body,none,
7582+
"'%0' requires that %kind1 have a body",
7583+
(DeclAttribute, const ValueDecl *))
75807584

75817585
//------------------------------------------------------------------------------
75827586
// MARK: Implicit opening of existential types

lib/Sema/TypeCheckAttr.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4719,13 +4719,13 @@ void AttributeChecker::checkBackDeployedAttrs(
47194719
// back deployment, which is to use the ABI version of the declaration when it
47204720
// is available.
47214721
if (auto *AEICA = D->getAttrs().getAttribute<AlwaysEmitIntoClientAttr>()) {
4722-
diagnoseAndRemoveAttr(AEICA, diag::attr_incompatible_with_back_deploy,
4723-
AEICA, D->getDescriptiveKind());
4722+
diagnoseAndRemoveAttr(AEICA, diag::attr_incompatible_with_back_deployed,
4723+
AEICA, D);
47244724
}
47254725

47264726
if (auto *TA = D->getAttrs().getAttribute<TransparentAttr>()) {
4727-
diagnoseAndRemoveAttr(TA, diag::attr_incompatible_with_back_deploy, TA,
4728-
D->getDescriptiveKind());
4727+
diagnoseAndRemoveAttr(TA, diag::attr_incompatible_with_back_deployed, TA,
4728+
D);
47294729
}
47304730

47314731
// Only functions, methods, computed properties, and subscripts are
@@ -4777,19 +4777,9 @@ void AttributeChecker::checkBackDeployedAttrs(
47774777
continue;
47784778
}
47794779

4780-
if (auto *VarD = dyn_cast<VarDecl>(D)) {
4781-
// There must be a function body to back deploy so for vars we require
4782-
// that they be computed in order to allow back deployment.
4783-
if (VarD->hasStorageOrWrapsStorage()) {
4784-
diagnoseAndRemoveAttr(Attr, diag::attr_not_on_stored_properties, Attr);
4785-
continue;
4786-
}
4787-
}
4788-
47894780
if (VD->getOpaqueResultTypeDecl()) {
4790-
diagnoseAndRemoveAttr(Attr,
4791-
diag::backdeployed_opaque_result_not_supported,
4792-
Attr, D->getDescriptiveKind())
4781+
diagnoseAndRemoveAttr(
4782+
Attr, diag::back_deployed_opaque_result_not_supported, Attr, VD)
47934783
.warnInSwiftInterface(D->getDeclContext());
47944784
continue;
47954785
}
@@ -4806,12 +4796,29 @@ void AttributeChecker::checkBackDeployedAttrs(
48064796
continue;
48074797
}
48084798

4809-
if (Ctx.LangOpts.DisableAvailabilityChecking)
4799+
// The remaining diagnostics can only be diagnosed for attributes that
4800+
// apply to the active platform.
4801+
if (Attr != ActiveAttr)
48104802
continue;
48114803

4812-
// Availability conflicts can only be diagnosed for attributes that apply
4813-
// to the active platform.
4814-
if (Attr != ActiveAttr)
4804+
if (auto *VarD = dyn_cast<VarDecl>(D)) {
4805+
// There must be a function body to back deploy so for vars we require
4806+
// that they be computed in order to allow back deployment.
4807+
if (VarD->hasStorageOrWrapsStorage()) {
4808+
diagnoseAndRemoveAttr(Attr, diag::attr_not_on_stored_properties, Attr);
4809+
continue;
4810+
}
4811+
}
4812+
4813+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
4814+
if (!AFD->hasBody()) {
4815+
diagnoseAndRemoveAttr(Attr, diag::back_deployed_requires_body, Attr,
4816+
VD);
4817+
continue;
4818+
}
4819+
}
4820+
4821+
if (Ctx.LangOpts.DisableAvailabilityChecking)
48154822
continue;
48164823

48174824
auto availability =
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
// RUN: not %target-swift-typecheck-module-from-interface(%t/Test.swiftinterface) -module-name Test 2>&1 | %FileCheck %s
4+
5+
// REQUIRES: OS=macosx || OS=ios || OS=tvos || OS=watchos || OS=visionos
6+
7+
// This test uses split-file because the check lines cannot appear as comments
8+
// in the interface (they'd match themselves in the diagnostic output).
9+
// FIXME: -verify should work for -typecheck-module-from-interface
10+
11+
// CHECK: Test.swiftinterface:5:2: error: '@backDeployed' requires that global function 'backDeployedFuncWithoutBody()' have a body
12+
// CHECK: Test.swiftinterface:9:2: error: '@backDeployed' must not be used on stored properties
13+
14+
//--- Test.swiftinterface
15+
// swift-interface-format-version: 1.0
16+
// swift-module-flags:
17+
18+
@available(macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.1, *)
19+
@backDeployed(before: macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
20+
public func backDeployedFuncWithoutBody()
21+
22+
@available(macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.1, *)
23+
@backDeployed(before: macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0)
24+
public var backDeployedVarWithoutBody: Int
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags:
3+
4+
// RUN: %target-swift-typecheck-module-from-interface(%s) -module-name Test
5+
// REQUIRES: OS=macosx
6+
7+
// Since the following declarations are only back deployed on iOS, their bodies
8+
// should be missing in a `.swiftinterface` compiled for macOS
9+
10+
@available(iOS 17.4, *)
11+
@backDeployed(before: iOS 18.0)
12+
public func backDeployedFuncOniOSWithoutBody()
13+
14+
@available(iOS 17.4, *)
15+
@backDeployed(before: iOS 18.0)
16+
public var backDeployedVarWithoutBody: Int

test/Serialization/ignore-opaque-underlying-type-back-deploy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct EV : V {
5757
@available(SwiftStdlib 5.1, *)
5858
public extension V {
5959
// CHECK: Loading underlying information for opaque type of 'backdeployedOpaqueFunc()'
60-
@backDeployed(before: SwiftStdlib 5.1) // expected-warning 4 {{'@backDeployed' is unsupported on a instance method with a 'some' return type}}
60+
@backDeployed(before: SwiftStdlib 5.1) // expected-warning 4 {{'@backDeployed' cannot be applied to instance method 'backdeployedOpaqueFunc()' because it has a 'some' return type}}
6161
func backdeployedOpaqueFunc() -> some V { EV() }
6262
}
6363

test/attr/attr_backDeployed.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ public enum CannotBackDeployEnum {
251251
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' must not be used on stored properties}}
252252
public var cannotBackDeployTopLevelVar = 79
253253

254+
@backDeployed(before: iOS 15.0) // OK, this can only be diagnosed when compiling for iOS
255+
public var cannotBackDeployTopLevelVarOniOS = 79
256+
254257
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' attribute cannot be applied to this declaration}}
255258
extension TopLevelStruct {}
256259

@@ -266,13 +269,13 @@ public struct ConformsToTopLevelProtocol: TopLevelProtocol {
266269
}
267270

268271
@available(SwiftStdlib 5.1, *)
269-
@backDeployed(before: macOS 12.0) // expected-warning {{'@backDeployed' is unsupported on a var with a 'some' return type}}
272+
@backDeployed(before: macOS 12.0) // expected-warning {{'@backDeployed' cannot be applied to var 'cannotBackDeployVarWithOpaqueResultType' because it has a 'some' return type}}
270273
public var cannotBackDeployVarWithOpaqueResultType: some TopLevelProtocol {
271274
return ConformsToTopLevelProtocol()
272275
}
273276

274277
@available(SwiftStdlib 5.1, *)
275-
@backDeployed(before: macOS 12.0) // expected-warning {{'@backDeployed' is unsupported on a global function with a 'some' return type}}
278+
@backDeployed(before: macOS 12.0) // expected-warning {{'@backDeployed' cannot be applied to global function 'cannotBackDeployFuncWithOpaqueResultType()' because it has a 'some' return type}}
276279
public func cannotBackDeployFuncWithOpaqueResultType() -> some TopLevelProtocol {
277280
return ConformsToTopLevelProtocol()
278281
}

0 commit comments

Comments
 (0)