Skip to content

Commit e61bf20

Browse files
committed
[ConstraintSystem] Make types of implicit result builder variables locatable
This would aid `FailureDiagnostic` in anchoring conformance failures to `View`.
1 parent 0fe4cea commit e61bf20

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3516,6 +3516,8 @@ class ConstraintSystem {
35163516
return !solverState || solverState->recordFixes;
35173517
}
35183518

3519+
bool inSalvageMode() const { return solverState && solverState->recordFixes; }
3520+
35193521
ArrayRef<ConstraintFix *> getFixes() const { return Fixes.getArrayRef(); }
35203522

35213523
bool shouldSuppressDiagnostics() const {

lib/Sema/CSGen.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,21 @@ namespace {
26872687
varType = TypeChecker::getOptionalType(var->getLoc(), varType);
26882688
}
26892689

2690+
auto makeTypeLocatableIfPossible = [&var](Type type) -> Type {
2691+
if (auto loc = var->getLoc()) {
2692+
return LocatableType::get(loc, type);
2693+
}
2694+
return type;
2695+
};
2696+
2697+
auto useLocatableTypes = [&]() -> bool {
2698+
if (!CS.inSalvageMode())
2699+
return false;
2700+
2701+
return var->isImplicit() &&
2702+
var->getNameStr().starts_with("$__builder");
2703+
};
2704+
26902705
// When we are supposed to bind pattern variables, create a fresh
26912706
// type variable and a one-way constraint to assign it to either the
26922707
// deduced type or the externally-imposed type.
@@ -2711,6 +2726,9 @@ namespace {
27112726

27122727
CS.addConstraint(ConstraintKind::OneWayEqual, oneWayVarType,
27132728
varType, locator);
2729+
2730+
if (useLocatableTypes())
2731+
oneWayVarType = makeTypeLocatableIfPossible(oneWayVarType);
27142732
}
27152733

27162734
// Ascribe a type to the declaration so it's always available to
@@ -2772,6 +2790,9 @@ namespace {
27722790
CS.getConstraintLocator(locator));
27732791
}
27742792

2793+
if (useLocatableTypes())
2794+
declTy = makeTypeLocatableIfPossible(declTy);
2795+
27752796
CS.setType(var, declTy);
27762797
}
27772798

0 commit comments

Comments
 (0)