Skip to content

[5.9] Sema: Diagnose @backDeployed on functions with opaque result types #66677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,15 @@ namespace swift {
/// until the next major language version.
InFlightDiagnostic &warnUntilSwiftVersion(unsigned majorVersion);

/// Limit the diagnostic behavior to warning if the context is a
/// swiftinterface.
///
/// This is useful for diagnostics for restrictions that may be lifted by a
/// future version of the compiler. In such cases, it may be helpful to
/// avoid failing to build a module from its interface if the interface was
/// emitted using a compiler that no longer has the restriction.
InFlightDiagnostic &warnInSwiftInterface(const DeclContext *context);

/// Conditionally limit the diagnostic behavior to warning until
/// the specified version. If the condition is false, no limit is
/// imposed, meaning (presumably) it is treated as an error.
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -6976,6 +6976,10 @@ ERROR(attr_incompatible_with_back_deploy,none,
"'%0' cannot be applied to a back deployed %1",
(DeclAttribute, DescriptiveDeclKind))

ERROR(backdeployed_opaque_result_not_supported,none,
"'%0' is unsupported on a %1 with a 'some' return type",
(DeclAttribute, DescriptiveDeclKind))

//------------------------------------------------------------------------------
// MARK: Implicit opening of existential types
//------------------------------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
return *this;
}

InFlightDiagnostic &
InFlightDiagnostic::warnInSwiftInterface(const DeclContext *context) {
auto sourceFile = context->getParentSourceFile();
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface) {
return limitBehavior(DiagnosticBehavior::Warning);
}

return *this;
}

InFlightDiagnostic &
InFlightDiagnostic::wrapIn(const Diagnostic &wrapper) {
// Save current active diagnostic into WrappedDiagnostics, ignoring state
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4324,6 +4324,14 @@ void AttributeChecker::checkBackDeployedAttrs(
}
}

if (VD->getOpaqueResultTypeDecl()) {
diagnoseAndRemoveAttr(Attr,
diag::backdeployed_opaque_result_not_supported,
Attr, D->getDescriptiveKind())
.warnInSwiftInterface(D->getDeclContext());
continue;
}

auto AtLoc = Attr->AtLoc;
auto Platform = Attr->Platform;

Expand Down
15 changes: 15 additions & 0 deletions test/attr/attr_backDeployed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ protocol CannotBackDeployProtocol {}
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' attribute cannot be applied to this declaration}}
public actor CannotBackDeployActor {}

public struct ConformsToTopLevelProtocol: TopLevelProtocol {
public init() {}
}

@available(SwiftStdlib 5.1, *)
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' is unsupported on a var with a 'some' return type}}
public var cannotBackDeployVarWithOpaqueResultType: some TopLevelProtocol {
return ConformsToTopLevelProtocol()
}

@available(SwiftStdlib 5.1, *)
@backDeployed(before: macOS 12.0) // expected-error {{'@backDeployed' is unsupported on a global function with a 'some' return type}}
public func cannotBackDeployFuncWithOpaqueResultType() -> some TopLevelProtocol {
return ConformsToTopLevelProtocol()
}

// MARK: - Function body diagnostics

Expand Down