Skip to content

[CS] Map caught error type into context #80248

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 1 commit into from
Mar 25, 2025
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
4 changes: 4 additions & 0 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3362,6 +3362,10 @@ class ConstraintSystem {
/// Undo the above change.
void removePotentialThrowSite(CatchNode catchNode);

/// Retrieve the explicit caught error type for the given catch node, without
/// attempting any inference.
Type getExplicitCaughtErrorType(CatchNode catchNode);

/// Determine the caught error type for the given catch node.
Type getCaughtErrorType(CatchNode node);

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ class SyntacticElementConstraintGenerator
auto throwLoc = throwStmt->getThrowLoc();
Type errorType;
if (auto catchNode = ASTScope::lookupCatchNode(module, throwLoc))
errorType = catchNode.getExplicitCaughtType(cs.getASTContext());
errorType = cs.getExplicitCaughtErrorType(catchNode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only note here is use of DC in existing code. Maybe we should propagate dc down explicitly to avoid problems in the future because DC in ConstraintSystem during solving could be difference from DC during constraint generation unfortunately...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, fortunately in this case being in a closure doesn't affect the result of mapping into context, but yeah I agree we ought to be passing the DeclContext down


if (!errorType) {
if (!cs.getASTContext().getErrorDecl()) {
Expand Down
12 changes: 11 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ void ConstraintSystem::recordPotentialThrowSite(
recordPotentialThrowSite(catchNode, site);
}

Type ConstraintSystem::getCaughtErrorType(CatchNode catchNode) {
Type ConstraintSystem::getExplicitCaughtErrorType(CatchNode catchNode) {
ASTContext &ctx = getASTContext();

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

return Type();
}

Type ConstraintSystem::getCaughtErrorType(CatchNode catchNode) {
ASTContext &ctx = getASTContext();

// If we have an explicit caught error type for this node, use it.
if (auto explicitCaughtType = getExplicitCaughtErrorType(catchNode))
return explicitCaughtType;

// Retrieve the thrown error type of a closure.
// FIXME: This will need to change when we do inference of thrown error
// types in closures.
Expand Down
13 changes: 13 additions & 0 deletions test/SILGen/issue-77295.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %target-swift-emit-silgen %s -verify

// https://github.com/swiftlang/swift/issues/77295 - Make sure this compiles.
extension Optional {
func foo<E: Error>(orThrow error: @autoclosure () -> E) throws(E) -> Wrapped {
switch self {
case .none:
throw error()
case .some(let wrapped):
wrapped
}
}
}