File tree Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change 28
28
#include " swift/AST/Initializer.h"
29
29
#include " swift/AST/MacroDiscriminatorContext.h"
30
30
#include " swift/AST/Module.h"
31
+ #include " swift/AST/NameLookup.h"
31
32
#include " swift/AST/ParameterList.h"
32
33
#include " swift/AST/Pattern.h"
33
34
#include " swift/AST/PrettyStackTrace.h"
@@ -1021,13 +1022,24 @@ class Verifier : public ASTWalker {
1021
1022
1022
1023
void verifyChecked (ThrowStmt *S) {
1023
1024
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
+
1029
+ if (!catchNode) {
1030
+ Out << " No catch context for throw statement\n " ;
1031
+ abort ();
1032
+ }
1033
+
1034
+ if (auto thrown = catchNode.getThrownErrorTypeInContext (Ctx)) {
1035
+ thrownError = *thrown;
1036
+ } else {
1037
+ thrownError = Ctx.getNeverType ();
1038
+ }
1039
+ } else {
1040
+ return ;
1027
1041
}
1028
1042
1029
- if (!thrownError)
1030
- thrownError = checkExceptionTypeExists (" throw expression" );
1031
1043
checkSameType (S->getSubExpr ()->getType (), thrownError, " throw operand" );
1032
1044
verifyCheckedBase (S);
1033
1045
}
Original file line number Diff line number Diff line change @@ -130,6 +130,23 @@ open class MyClass {
130
130
func f( ) throws { }
131
131
}
132
132
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
+
133
150
// CHECK-LABEL: sil_vtable MySubclass {
134
151
// CHECK-NEXT: #MyClass.init!allocator: <E where E : Error> (MyClass.Type) -> (() throws(E) -> ()) throws(E) -> MyClass : @$s12typed_throws10MySubclassC4bodyACyyxYKXE_txYKcs5ErrorRzlufC [override]
135
152
// CHECK-NEXT: #MyClass.f: (MyClass) -> () throws -> () : @$s12typed_throws10MySubclassC1fyyAA0C5ErrorOYKFAA0C5ClassCADyyKFTV [override]
You can’t perform that action at this time.
0 commit comments