Skip to content

Commit 2c52f05

Browse files
authored
Merge pull request swiftlang#78414 from tshortli/back-deployed-without-body-redux
Sema: Diagnose `@backDeployed` functions with missing bodies in swiftinterfaces
2 parents e252cbb + 7ea778f commit 2c52f05

File tree

6 files changed

+86
-30
lines changed

6 files changed

+86
-30
lines changed

include/swift/AST/DiagnosticsSema.def

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

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

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

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

lib/Sema/TypeCheckAttr.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4874,21 +4874,23 @@ void AttributeChecker::checkBackDeployedAttrs(
48744874
// back deployment, which is to use the ABI version of the declaration when it
48754875
// is available.
48764876
if (auto *AEICA = D->getAttrs().getAttribute<AlwaysEmitIntoClientAttr>()) {
4877-
diagnoseAndRemoveAttr(AEICA, diag::attr_incompatible_with_back_deploy,
4878-
AEICA, D->getDescriptiveKind());
4877+
diagnoseAndRemoveAttr(AEICA, diag::attr_incompatible_with_back_deployed,
4878+
AEICA, D);
48794879
}
48804880

48814881
if (auto *TA = D->getAttrs().getAttribute<TransparentAttr>()) {
4882-
diagnoseAndRemoveAttr(TA, diag::attr_incompatible_with_back_deploy, TA,
4883-
D->getDescriptiveKind());
4882+
diagnoseAndRemoveAttr(TA, diag::attr_incompatible_with_back_deployed, TA,
4883+
D);
48844884
}
48854885

48864886
// Only functions, methods, computed properties, and subscripts are
48874887
// back-deployable, so D should be ValueDecl.
48884888
auto *VD = cast<ValueDecl>(D);
48894889
std::map<PlatformKind, SourceLoc> seenPlatforms;
48904890

4891-
auto *ActiveAttr = D->getAttrs().getBackDeployed(Ctx, false);
4891+
const BackDeployedAttr *ActiveAttr = nullptr;
4892+
if (D->getBackDeployedBeforeOSVersion(Ctx))
4893+
ActiveAttr = D->getAttrs().getBackDeployed(Ctx, false);
48924894

48934895
for (auto *Attr : Attrs) {
48944896
// Back deployment only makes sense for public declarations.
@@ -4932,19 +4934,9 @@ void AttributeChecker::checkBackDeployedAttrs(
49324934
continue;
49334935
}
49344936

4935-
if (auto *VarD = dyn_cast<VarDecl>(D)) {
4936-
// There must be a function body to back deploy so for vars we require
4937-
// that they be computed in order to allow back deployment.
4938-
if (VarD->hasStorageOrWrapsStorage()) {
4939-
diagnoseAndRemoveAttr(Attr, diag::attr_not_on_stored_properties, Attr);
4940-
continue;
4941-
}
4942-
}
4943-
49444937
if (VD->getOpaqueResultTypeDecl()) {
4945-
diagnoseAndRemoveAttr(Attr,
4946-
diag::backdeployed_opaque_result_not_supported,
4947-
Attr, D->getDescriptiveKind())
4938+
diagnoseAndRemoveAttr(
4939+
Attr, diag::back_deployed_opaque_result_not_supported, Attr, VD)
49484940
.warnInSwiftInterface(D->getDeclContext());
49494941
continue;
49504942
}
@@ -4961,12 +4953,29 @@ void AttributeChecker::checkBackDeployedAttrs(
49614953
continue;
49624954
}
49634955

4964-
if (Ctx.LangOpts.DisableAvailabilityChecking)
4956+
// The remaining diagnostics can only be diagnosed for attributes that
4957+
// apply to the active platform.
4958+
if (Attr != ActiveAttr)
49654959
continue;
49664960

4967-
// Availability conflicts can only be diagnosed for attributes that apply
4968-
// to the active platform.
4969-
if (Attr != ActiveAttr)
4961+
if (auto *VarD = dyn_cast<VarDecl>(D)) {
4962+
// There must be a function body to back deploy so for vars we require
4963+
// that they be computed in order to allow back deployment.
4964+
if (VarD->hasStorageOrWrapsStorage()) {
4965+
diagnoseAndRemoveAttr(Attr, diag::attr_not_on_stored_properties, Attr);
4966+
continue;
4967+
}
4968+
}
4969+
4970+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
4971+
if (!AFD->hasBody()) {
4972+
diagnoseAndRemoveAttr(Attr, diag::back_deployed_requires_body, Attr,
4973+
VD);
4974+
continue;
4975+
}
4976+
}
4977+
4978+
if (Ctx.LangOpts.DisableAvailabilityChecking)
49704979
continue;
49714980

49724981
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)