Skip to content

Commit 766e623

Browse files
authored
Merge pull request #71800 from tshortli/module-interface-typed-throws-closure
ModuleInterface: Improve TypedThrows feature guards
2 parents 7f9b184 + 1b7d8b6 commit 766e623

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,8 +3758,26 @@ static bool usesFeatureFullTypedThrows(Decl *decl) {
37583758
}
37593759

37603760
static bool usesFeatureTypedThrows(Decl *decl) {
3761-
if (auto func = dyn_cast<AbstractFunctionDecl>(decl))
3762-
return func->getThrownTypeRepr() != nullptr;
3761+
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
3762+
struct Walker : public TypeWalker {
3763+
bool hasTypedThrows = false;
3764+
3765+
Action walkToTypePre(Type ty) override {
3766+
if (auto funcType = ty->getAs<AnyFunctionType>()) {
3767+
if (funcType->hasThrownError()) {
3768+
hasTypedThrows = true;
3769+
return Action::Stop;
3770+
}
3771+
}
3772+
3773+
return Action::Continue;
3774+
}
3775+
};
3776+
3777+
Walker walker;
3778+
func->getInterfaceType().walk(walker);
3779+
return walker.hasTypedThrows;
3780+
}
37633781

37643782
return false;
37653783
}

test/ModuleInterface/typed_throws.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ public enum MyError: Error {
1111
// CHECK-NEXT: public func throwsMyError() throws(Test.MyError)
1212
// CHECK-NEXT: #endif
1313
public func throwsMyError() throws(MyError) { }
14+
15+
// CHECK: #if compiler(>=5.3) && $TypedThrows
16+
// CHECK-NEXT: public func takesClosureThrowingMyError(_ closure: () throws(Test.MyError) -> Swift.Void)
17+
// CHECK-NEXT: #endif
18+
public func takesClosureThrowingMyError(_ closure: () throws(MyError) -> Void) {}
19+
20+
public struct HasThrowingInit {
21+
// CHECK: #if compiler(>=5.3) && $TypedThrows
22+
// CHECK-NEXT: public init() throws(Test.MyError)
23+
// CHECK-NEXT: #endif
24+
public init() throws(MyError) { }
25+
}

0 commit comments

Comments
 (0)