Skip to content

Commit e66aa61

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 61a850d commit e66aa61

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
@@ -2288,9 +2288,25 @@ namespace {
22882288
case PatternKind::Any: {
22892289
Type type;
22902290

2291+
// If this is a situation like `[let] _ = <expr>`, return
2292+
// initializer expression.
2293+
auto getInitializerExpr = [&locator]() -> Expr * {
2294+
auto last = locator.last();
2295+
if (!last)
2296+
return nullptr;
2297+
2298+
auto contextualTy = last->getAs<LocatorPathElt::ContextualType>();
2299+
return (contextualTy && contextualTy->isFor(CTP_Initialization))
2300+
? locator.trySimplifyToExpr()
2301+
: nullptr;
2302+
};
2303+
22912304
// Always prefer a contextual type when it's available.
22922305
if (externalPatternType) {
22932306
type = externalPatternType;
2307+
} else if (auto *initializer = getInitializerExpr()) {
2308+
// For initialization always assume a type of initializer.
2309+
type = CS.getType(initializer)->getRValueType();
22942310
} else {
22952311
type = CS.createTypeVariable(
22962312
CS.getConstraintLocator(pattern,

0 commit comments

Comments
 (0)