Skip to content

Teach AST verifier about thrown error contexts #70160

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
5 changes: 5 additions & 0 deletions include/swift/Basic/MacroRoles.def
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
// This file defines all of the kinds of macro roles. Clients should define
// either MACRO_ROLE or both ATTACHED_MACRO_ROLE and FREESTANDING_MACRO_ROLE.
//
// The order of the macro roles in this file is significant for the
// serialization of module files. Please do not re-order the entries without
// also bumping the module format version. When introducing new macro roles,
// please add them to the end of the file.
//
//===----------------------------------------------------------------------===//

#ifndef ATTACHED_MACRO_ROLE
Expand Down
20 changes: 15 additions & 5 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "swift/AST/Initializer.h"
#include "swift/AST/MacroDiscriminatorContext.h"
#include "swift/AST/Module.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/PrettyStackTrace.h"
Expand Down Expand Up @@ -1021,13 +1022,22 @@ class Verifier : public ASTWalker {

void verifyChecked(ThrowStmt *S) {
Type thrownError;
if (!Functions.empty()) {
if (auto fn = AnyFunctionRef::fromDeclContext(Functions.back()))
thrownError = fn->getThrownErrorType();
SourceLoc loc = S->getThrowLoc();
if (loc.isValid()) {
auto catchNode = ASTScope::lookupCatchNode(getModuleContext(), loc);
if (catchNode) {
if (auto thrown = catchNode.getThrownErrorTypeInContext(Ctx)) {
thrownError = *thrown;
} else {
thrownError = Ctx.getNeverType();
}
} else {
thrownError = checkExceptionTypeExists("throw expression");
}
} else {
return;
}

if (!thrownError)
thrownError = checkExceptionTypeExists("throw expression");
checkSameType(S->getSubExpr()->getType(), thrownError, "throw operand");
verifyCheckedBase(S);
}
Expand Down
17 changes: 17 additions & 0 deletions test/SILGen/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ open class MyClass {
func f() throws { }
}


struct Foo: Error { }
struct Bar: Error { }

// CHECK-LABEL: sil hidden [ossa] @$s12typed_throws0B22DifferentFromEnclosingyyAA3FooVYKF : $@convention(thin) () -> @error Foo
func throwsDifferentFromEnclosing() throws(Foo) {
do {
throw Bar()
} catch {
print("Bar was barred")
}

// CHECK: throw [[ERROR:%.*]] : $Foo
throw Foo()
}


// CHECK-LABEL: sil_vtable MySubclass {
// CHECK-NEXT: #MyClass.init!allocator: <E where E : Error> (MyClass.Type) -> (() throws(E) -> ()) throws(E) -> MyClass : @$s12typed_throws10MySubclassC4bodyACyyxYKXE_txYKcs5ErrorRzlufC [override]
// CHECK-NEXT: #MyClass.f: (MyClass) -> () throws -> () : @$s12typed_throws10MySubclassC1fyyAA0C5ErrorOYKFAA0C5ClassCADyyKFTV [override]
Expand Down