Skip to content

ModuleInterface: Improve TypedThrows feature guards #71800

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
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
22 changes: 20 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3758,8 +3758,26 @@ static bool usesFeatureFullTypedThrows(Decl *decl) {
}

static bool usesFeatureTypedThrows(Decl *decl) {
if (auto func = dyn_cast<AbstractFunctionDecl>(decl))
return func->getThrownTypeRepr() != nullptr;
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
struct Walker : public TypeWalker {
bool hasTypedThrows = false;

Action walkToTypePre(Type ty) override {
if (auto funcType = ty->getAs<AnyFunctionType>()) {
if (funcType->hasThrownError()) {
hasTypedThrows = true;
return Action::Stop;
}
}

return Action::Continue;
}
};

Walker walker;
func->getInterfaceType().walk(walker);
return walker.hasTypedThrows;
}

return false;
}
Expand Down
12 changes: 12 additions & 0 deletions test/ModuleInterface/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ public enum MyError: Error {
// CHECK-NEXT: public func throwsMyError() throws(Test.MyError)
// CHECK-NEXT: #endif
public func throwsMyError() throws(MyError) { }

// CHECK: #if compiler(>=5.3) && $TypedThrows
// CHECK-NEXT: public func takesClosureThrowingMyError(_ closure: () throws(Test.MyError) -> Swift.Void)
// CHECK-NEXT: #endif
public func takesClosureThrowingMyError(_ closure: () throws(MyError) -> Void) {}

public struct HasThrowingInit {
// CHECK: #if compiler(>=5.3) && $TypedThrows
// CHECK-NEXT: public init() throws(Test.MyError)
// CHECK-NEXT: #endif
public init() throws(MyError) { }
}