Skip to content

Commit eba95ba

Browse files
committed
[CS] Delay type resolution of variables for variables in named pattern
1 parent e14fa72 commit eba95ba

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

include/swift/Sema/ConstraintLocatorPathElts.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ CUSTOM_LOCATOR_PATH_ELT(SyntacticElement)
239239
/// The element of the pattern binding declaration.
240240
CUSTOM_LOCATOR_PATH_ELT(PatternBindingElement)
241241

242+
/// The variable declared by a named pattern.
243+
SIMPLE_LOCATOR_PATH_ELT(NamedPatternDecl)
244+
242245
#undef LOCATOR_PATH_ELT
243246
#undef CUSTOM_LOCATOR_PATH_ELT
244247
#undef SIMPLE_LOCATOR_PATH_ELT

lib/Sema/CSBindings.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ bool BindingSet::isDelayed() const {
109109
if (locator->directlyAt<NilLiteralExpr>())
110110
return true;
111111

112+
// When inferring the type of a variable in a pattern, delay its resolution
113+
// so that we resolve type variables inside the expression as placeholders
114+
// instead of marking the type of the variable itself as a placeholder. This
115+
// allows us to produce more specific errors because the type variable in
116+
// the expression that introduced the placeholder might be diagnosable using
117+
// fixForHole.
118+
if (locator->isLastElement<LocatorPathElt::NamedPatternDecl>()) {
119+
return true;
120+
}
121+
112122
// It's possible that type of member couldn't be determined,
113123
// and if so it would be beneficial to bind member to a hole
114124
// early to propagate that information down to arguments,

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,9 +2327,10 @@ namespace {
23272327
}
23282328

23292329
if (!varType) {
2330-
varType =
2331-
CS.createTypeVariable(CS.getConstraintLocator(locator),
2332-
TVO_CanBindToNoEscape | TVO_CanBindToHole);
2330+
varType = CS.createTypeVariable(
2331+
CS.getConstraintLocator(pattern,
2332+
LocatorPathElt::NamedPatternDecl()),
2333+
TVO_CanBindToNoEscape | TVO_CanBindToHole);
23332334

23342335
// If this is either a `weak` declaration or capture e.g.
23352336
// `weak var ...` or `[weak self]`. Let's wrap type variable

lib/Sema/ConstraintLocator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ unsigned LocatorPathElt::getNewSummaryFlags() const {
9797
case ConstraintLocator::PackType:
9898
case ConstraintLocator::PackElement:
9999
case ConstraintLocator::PatternBindingElement:
100+
case ConstraintLocator::NamedPatternDecl:
100101
return 0;
101102

102103
case ConstraintLocator::FunctionArgument:
@@ -441,6 +442,11 @@ void LocatorPathElt::dump(raw_ostream &out) const {
441442
<< llvm::utostr(patternBindingElt.getIndex());
442443
break;
443444
}
445+
446+
case ConstraintLocator::NamedPatternDecl: {
447+
out << "named pattern decl";
448+
break;
449+
}
444450
}
445451
}
446452

lib/Sema/ConstraintSystem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5256,6 +5256,13 @@ void constraints::simplifyLocator(ASTNode &anchor,
52565256
continue;
52575257
}
52585258

5259+
case ConstraintLocator::NamedPatternDecl: {
5260+
auto pattern = cast<NamedPattern>(anchor.get<Pattern *>());
5261+
anchor = pattern->getDecl();
5262+
path = path.slice(1);
5263+
break;
5264+
}
5265+
52595266
case ConstraintLocator::ImplicitConversion:
52605267
break;
52615268

0 commit comments

Comments
 (0)