Skip to content

Commit 89cdc6d

Browse files
authored
Merge pull request #70160 from DougGregor/ast-verifier-thrown-error-contexts
Teach AST verifier about thrown error contexts
2 parents 6843e03 + 965b970 commit 89cdc6d

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

include/swift/Basic/MacroRoles.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
// This file defines all of the kinds of macro roles. Clients should define
1414
// either MACRO_ROLE or both ATTACHED_MACRO_ROLE and FREESTANDING_MACRO_ROLE.
1515
//
16+
// The order of the macro roles in this file is significant for the
17+
// serialization of module files. Please do not re-order the entries without
18+
// also bumping the module format version. When introducing new macro roles,
19+
// please add them to the end of the file.
20+
//
1621
//===----------------------------------------------------------------------===//
1722

1823
#ifndef ATTACHED_MACRO_ROLE

lib/AST/ASTVerifier.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/AST/Initializer.h"
2929
#include "swift/AST/MacroDiscriminatorContext.h"
3030
#include "swift/AST/Module.h"
31+
#include "swift/AST/NameLookup.h"
3132
#include "swift/AST/ParameterList.h"
3233
#include "swift/AST/Pattern.h"
3334
#include "swift/AST/PrettyStackTrace.h"
@@ -1021,13 +1022,22 @@ class Verifier : public ASTWalker {
10211022

10221023
void verifyChecked(ThrowStmt *S) {
10231024
Type thrownError;
1024-
if (!Functions.empty()) {
1025-
if (auto fn = AnyFunctionRef::fromDeclContext(Functions.back()))
1026-
thrownError = fn->getThrownErrorType();
1025+
SourceLoc loc = S->getThrowLoc();
1026+
if (loc.isValid()) {
1027+
auto catchNode = ASTScope::lookupCatchNode(getModuleContext(), loc);
1028+
if (catchNode) {
1029+
if (auto thrown = catchNode.getThrownErrorTypeInContext(Ctx)) {
1030+
thrownError = *thrown;
1031+
} else {
1032+
thrownError = Ctx.getNeverType();
1033+
}
1034+
} else {
1035+
thrownError = checkExceptionTypeExists("throw expression");
1036+
}
1037+
} else {
1038+
return;
10271039
}
10281040

1029-
if (!thrownError)
1030-
thrownError = checkExceptionTypeExists("throw expression");
10311041
checkSameType(S->getSubExpr()->getType(), thrownError, "throw operand");
10321042
verifyCheckedBase(S);
10331043
}

test/SILGen/typed_throws.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,23 @@ open class MyClass {
130130
func f() throws { }
131131
}
132132

133+
134+
struct Foo: Error { }
135+
struct Bar: Error { }
136+
137+
// CHECK-LABEL: sil hidden [ossa] @$s12typed_throws0B22DifferentFromEnclosingyyAA3FooVYKF : $@convention(thin) () -> @error Foo
138+
func throwsDifferentFromEnclosing() throws(Foo) {
139+
do {
140+
throw Bar()
141+
} catch {
142+
print("Bar was barred")
143+
}
144+
145+
// CHECK: throw [[ERROR:%.*]] : $Foo
146+
throw Foo()
147+
}
148+
149+
133150
// CHECK-LABEL: sil_vtable MySubclass {
134151
// CHECK-NEXT: #MyClass.init!allocator: <E where E : Error> (MyClass.Type) -> (() throws(E) -> ()) throws(E) -> MyClass : @$s12typed_throws10MySubclassC4bodyACyyxYKXE_txYKcs5ErrorRzlufC [override]
135152
// CHECK-NEXT: #MyClass.f: (MyClass) -> () throws -> () : @$s12typed_throws10MySubclassC1fyyAA0C5ErrorOYKFAA0C5ClassCADyyKFTV [override]

0 commit comments

Comments
 (0)