Skip to content

Commit a3bad80

Browse files
committed
[CSGen] Allow _ pattern to assume type of initializer expression
`_` pattern doesn't have a type, it's always contextual, so let's allow it to assume a type of initializer expression instead of creating a new type variable. This helps to makes sure that initializer would never get a placeholder type from `_` pattern it's associated with.
1 parent 99ec458 commit a3bad80

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/Sema/CSGen.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,9 +2292,25 @@ namespace {
22922292
case PatternKind::Any: {
22932293
Type type;
22942294

2295+
// If this is a situation like `[let] _ = <expr>`, return
2296+
// initializer expression.
2297+
auto getInitializerExpr = [&locator]() -> Expr * {
2298+
auto last = locator.last();
2299+
if (!last)
2300+
return nullptr;
2301+
2302+
auto contextualTy = last->getAs<LocatorPathElt::ContextualType>();
2303+
return (contextualTy && contextualTy->isFor(CTP_Initialization))
2304+
? locator.trySimplifyToExpr()
2305+
: nullptr;
2306+
};
2307+
22952308
// Always prefer a contextual type when it's available.
22962309
if (externalPatternType) {
22972310
type = externalPatternType;
2311+
} else if (auto *initializer = getInitializerExpr()) {
2312+
// For initialization always assume a type of initializer.
2313+
type = CS.getType(initializer)->getRValueType();
22982314
} else {
22992315
type = CS.createTypeVariable(
23002316
CS.getConstraintLocator(pattern,

0 commit comments

Comments
 (0)