Skip to content

[SE-0458] Improve backward compatibility of generated Swift interfaces #79655

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
merged 2 commits into from
Feb 27, 2025
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
12 changes: 8 additions & 4 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2901,14 +2901,12 @@ void PrintAST::printInherited(const Decl *decl) {
Printer << "@preconcurrency ";
switch (inherited.getExplicitSafety()) {
case ExplicitSafety::Unspecified:
break;

case ExplicitSafety::Safe:
Printer << "@safe ";
break;

case ExplicitSafety::Unsafe:
Printer << "@unsafe ";
if (!llvm::is_contained(Options.ExcludeAttrList, TypeAttrKind::Unsafe))
Printer << "@unsafe ";
break;
}
if (inherited.isSuppressed())
Expand Down Expand Up @@ -3218,6 +3216,12 @@ suppressingFeatureMemorySafetyAttributes(PrintOptions &options,
llvm::function_ref<void()> action) {
ExcludeAttrRAII scope(options.ExcludeAttrList, DeclAttrKind::Unsafe);
ExcludeAttrRAII scope2(options.ExcludeAttrList, DeclAttrKind::Safe);
options.ExcludeAttrList.push_back(TypeAttrKind::Unsafe);
SWIFT_DEFER {
assert(options.ExcludeAttrList.back() == TypeAttrKind::Unsafe);
options.ExcludeAttrList.pop_back();
};

action();
}

Expand Down
29 changes: 27 additions & 2 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "swift/AST/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/ProtocolConformance.h"
#include "clang/AST/DeclObjC.h"
#include "swift/Basic/Assertions.h"

Expand Down Expand Up @@ -350,8 +351,32 @@ static bool usesFeatureIsolatedConformances(Decl *decl) {
}

static bool usesFeatureMemorySafetyAttributes(Decl *decl) {
return decl->getAttrs().hasAttribute<SafeAttr>() ||
decl->getAttrs().hasAttribute<UnsafeAttr>();
if (decl->getAttrs().hasAttribute<SafeAttr>() ||
decl->getAttrs().hasAttribute<UnsafeAttr>())
return true;

IterableDeclContext *idc;
if (auto nominal = dyn_cast<NominalTypeDecl>(decl))
idc = nominal;
else if (auto ext = dyn_cast<ExtensionDecl>(decl))
idc = ext;
else
idc = nullptr;

// Look for an @unsafe conformance ascribed to this declaration.
if (idc) {
auto conformances = idc->getLocalConformances();
for (auto conformance : conformances) {
auto rootConformance = conformance->getRootConformance();
if (auto rootNormalConformance =
dyn_cast<NormalProtocolConformance>(rootConformance)) {
if (rootNormalConformance->getExplicitSafety() == ExplicitSafety::Unsafe)
return true;
}
}
}

return false;
}

UNINTERESTING_FEATURE(StrictMemorySafety)
Expand Down
4 changes: 4 additions & 0 deletions lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ fileprivate class RemoveUnsafeExprSyntaxRewriter: SyntaxRewriter {
override func visit(_ node: UnsafeExprSyntax) -> ExprSyntax {
return node.expression.with(\.leadingTrivia, node.leadingTrivia)
}

override func visit(_ node: ForStmtSyntax) -> StmtSyntax {
return StmtSyntax(node.with(\.unsafeKeyword, nil))
}
}

extension SyntaxProtocol {
Expand Down
14 changes: 13 additions & 1 deletion test/Unsafe/module-interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,34 @@
// CHECK: #endif
@unsafe public func getIntUnsafely() -> Int { 0 }

public struct UnsafeIterator: @unsafe IteratorProtocol {
@unsafe public mutating func next() -> Int? { nil }
}

public struct SequenceWithUnsafeIterator: Sequence {
public init() { }
public func makeIterator() -> UnsafeIterator { UnsafeIterator() }
}

// CHECK: @inlinable public func useUnsafeCode()
@inlinable public func useUnsafeCode() {
// CHECK-NOT: unsafe
print( unsafe getIntUnsafely())

for unsafe _ in SequenceWithUnsafeIterator() { }
}

// CHECK: public protocol P
public protocol P {
func f()
}

// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
// CHECK: public struct X : @unsafe UserModule.P
public struct X: @unsafe P {
// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
// CHECK: @unsafe public func f()
// CHECK: #else
// CHECK: public struct X : UserModule.P
// CHECK: public func f()
// CHECK: #endif
@unsafe public func f() { }
Expand Down