Skip to content

Commit a84b53f

Browse files
authored
Merge pull request swiftlang#80248 from hamishknight/fix-error-type
[CS] Map caught error type into context
2 parents 241ffaf + c597023 commit a84b53f

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

Diff for: include/swift/Sema/ConstraintSystem.h

+4
Original file line numberDiff line numberDiff line change
@@ -3362,6 +3362,10 @@ class ConstraintSystem {
33623362
/// Undo the above change.
33633363
void removePotentialThrowSite(CatchNode catchNode);
33643364

3365+
/// Retrieve the explicit caught error type for the given catch node, without
3366+
/// attempting any inference.
3367+
Type getExplicitCaughtErrorType(CatchNode catchNode);
3368+
33653369
/// Determine the caught error type for the given catch node.
33663370
Type getCaughtErrorType(CatchNode node);
33673371

Diff for: lib/Sema/CSSyntacticElement.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ class SyntacticElementConstraintGenerator
969969
auto throwLoc = throwStmt->getThrowLoc();
970970
Type errorType;
971971
if (auto catchNode = ASTScope::lookupCatchNode(module, throwLoc))
972-
errorType = catchNode.getExplicitCaughtType(cs.getASTContext());
972+
errorType = cs.getExplicitCaughtErrorType(catchNode);
973973

974974
if (!errorType) {
975975
if (!cs.getASTContext().getErrorDecl()) {

Diff for: lib/Sema/ConstraintSystem.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ void ConstraintSystem::recordPotentialThrowSite(
511511
recordPotentialThrowSite(catchNode, site);
512512
}
513513

514-
Type ConstraintSystem::getCaughtErrorType(CatchNode catchNode) {
514+
Type ConstraintSystem::getExplicitCaughtErrorType(CatchNode catchNode) {
515515
ASTContext &ctx = getASTContext();
516516

517517
// If there is an explicit caught type for this node, use it.
@@ -522,6 +522,16 @@ Type ConstraintSystem::getCaughtErrorType(CatchNode catchNode) {
522522
return explicitCaughtType;
523523
}
524524

525+
return Type();
526+
}
527+
528+
Type ConstraintSystem::getCaughtErrorType(CatchNode catchNode) {
529+
ASTContext &ctx = getASTContext();
530+
531+
// If we have an explicit caught error type for this node, use it.
532+
if (auto explicitCaughtType = getExplicitCaughtErrorType(catchNode))
533+
return explicitCaughtType;
534+
525535
// Retrieve the thrown error type of a closure.
526536
// FIXME: This will need to change when we do inference of thrown error
527537
// types in closures.

Diff for: test/SILGen/issue-77295.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-emit-silgen %s -verify
2+
3+
// https://github.com/swiftlang/swift/issues/77295 - Make sure this compiles.
4+
extension Optional {
5+
func foo<E: Error>(orThrow error: @autoclosure () -> E) throws(E) -> Wrapped {
6+
switch self {
7+
case .none:
8+
throw error()
9+
case .some(let wrapped):
10+
wrapped
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)