Skip to content

Commit 79616c4

Browse files
authored
Merge pull request #66673 from tshortli/diagnose-back-deployed-opaque-result-types
Sema: Diagnose `@backDeployed` on functions with opaque result types
2 parents de31b13 + c8f4dab commit 79616c4

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,15 @@ namespace swift {
564564
/// until the next major language version.
565565
InFlightDiagnostic &warnUntilSwiftVersion(unsigned majorVersion);
566566

567+
/// Limit the diagnostic behavior to warning if the context is a
568+
/// swiftinterface.
569+
///
570+
/// This is useful for diagnostics for restrictions that may be lifted by a
571+
/// future version of the compiler. In such cases, it may be helpful to
572+
/// avoid failing to build a module from its interface if the interface was
573+
/// emitted using a compiler that no longer has the restriction.
574+
InFlightDiagnostic &warnInSwiftInterface(const DeclContext *context);
575+
567576
/// Conditionally limit the diagnostic behavior to warning until
568577
/// the specified version. If the condition is false, no limit is
569578
/// imposed, meaning (presumably) it is treated as an error.

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7053,6 +7053,10 @@ ERROR(attr_incompatible_with_back_deploy,none,
70537053
"'%0' cannot be applied to a back deployed %1",
70547054
(DeclAttribute, DescriptiveDeclKind))
70557055

7056+
ERROR(backdeployed_opaque_result_not_supported,none,
7057+
"'%0' is unsupported on a %1 with a 'some' return type",
7058+
(DeclAttribute, DescriptiveDeclKind))
7059+
70567060
//------------------------------------------------------------------------------
70577061
// MARK: Implicit opening of existential types
70587062
//------------------------------------------------------------------------------

lib/AST/DiagnosticEngine.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,16 @@ InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
334334
return *this;
335335
}
336336

337+
InFlightDiagnostic &
338+
InFlightDiagnostic::warnInSwiftInterface(const DeclContext *context) {
339+
auto sourceFile = context->getParentSourceFile();
340+
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface) {
341+
return limitBehavior(DiagnosticBehavior::Warning);
342+
}
343+
344+
return *this;
345+
}
346+
337347
InFlightDiagnostic &
338348
InFlightDiagnostic::wrapIn(const Diagnostic &wrapper) {
339349
// Save current active diagnostic into WrappedDiagnostics, ignoring state

lib/Sema/TypeCheckAttr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,6 +4349,14 @@ void AttributeChecker::checkBackDeployedAttrs(
43494349
}
43504350
}
43514351

4352+
if (VD->getOpaqueResultTypeDecl()) {
4353+
diagnoseAndRemoveAttr(Attr,
4354+
diag::backdeployed_opaque_result_not_supported,
4355+
Attr, D->getDescriptiveKind())
4356+
.warnInSwiftInterface(D->getDeclContext());
4357+
continue;
4358+
}
4359+
43524360
auto AtLoc = Attr->AtLoc;
43534361
auto Platform = Attr->Platform;
43544362

test/attr/attr_backDeployed.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,21 @@ protocol CannotBackDeployProtocol {}
261261
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' attribute cannot be applied to this declaration}}
262262
public actor CannotBackDeployActor {}
263263

264+
public struct ConformsToTopLevelProtocol: TopLevelProtocol {
265+
public init() {}
266+
}
267+
268+
@available(SwiftStdlib 5.1, *)
269+
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' is unsupported on a var with a 'some' return type}}
270+
public var cannotBackDeployVarWithOpaqueResultType: some TopLevelProtocol {
271+
return ConformsToTopLevelProtocol()
272+
}
273+
274+
@available(SwiftStdlib 5.1, *)
275+
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' is unsupported on a global function with a 'some' return type}}
276+
public func cannotBackDeployFuncWithOpaqueResultType() -> some TopLevelProtocol {
277+
return ConformsToTopLevelProtocol()
278+
}
264279

265280
// MARK: - Function body diagnostics
266281

0 commit comments

Comments
 (0)