Skip to content

Commit b2facd0

Browse files
authored
Merge pull request #30258 from DougGregor/constraint-pattern-locators
[Constraint system] Clean up constraints associated with patterns.
2 parents 8d5d381 + 1a981f9 commit b2facd0

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

lib/Sema/CSGen.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,7 +2315,8 @@ namespace {
23152315
cast<TypedPattern>(pattern)->getSubPattern(), locator,
23162316
Type(), bindPatternVarsOneWay);
23172317
CS.addConstraint(
2318-
ConstraintKind::Conversion, subPatternType, openedType, locator);
2318+
ConstraintKind::Conversion, subPatternType, openedType,
2319+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
23192320
return setType(openedType);
23202321
}
23212322

@@ -2352,7 +2353,7 @@ namespace {
23522353

23532354
Type eltTy = getTypeForPattern(
23542355
tupleElt.getPattern(),
2355-
locator.withPathElement(LocatorPathElt::TupleElement(i)),
2356+
locator,
23562357
externalEltType,
23572358
bindPatternVarsOneWay);
23582359
tupleTypeElts.push_back(TupleTypeElt(eltTy, tupleElt.getLabel()));
@@ -2368,7 +2369,8 @@ namespace {
23682369
CS.getConstraintLocator(locator), TVO_CanBindToNoEscape);
23692370
CS.addConstraint(
23702371
ConstraintKind::OptionalObject, externalPatternType,
2371-
objVar, locator);
2372+
objVar,
2373+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
23722374

23732375
externalPatternType = objVar;
23742376
}
@@ -2386,7 +2388,9 @@ namespace {
23862388

23872389
Type castType =
23882390
resolveTypeReferenceInExpression(isPattern->getCastTypeLoc());
2389-
castType = CS.openUnboundGenericType(castType, locator);
2391+
castType = CS.openUnboundGenericType(
2392+
castType,
2393+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
23902394

23912395
Type subPatternType =
23922396
getTypeForPattern(isPattern->getSubPattern(), locator, castType,
@@ -2395,7 +2399,8 @@ namespace {
23952399
// Make sure we can cast from the subpattern type to the type we're
23962400
// checking; if it's impossible, fail.
23972401
CS.addConstraint(
2398-
ConstraintKind::CheckedCast, subPatternType, castType, locator);
2402+
ConstraintKind::CheckedCast, subPatternType, castType,
2403+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
23992404

24002405
return setType(subPatternType);
24012406
}
@@ -2422,7 +2427,9 @@ namespace {
24222427
// Resolve the parent type.
24232428
Type parentType =
24242429
resolveTypeReferenceInExpression(enumPattern->getParentType());
2425-
parentType = CS.openUnboundGenericType(parentType, locator);
2430+
parentType = CS.openUnboundGenericType(
2431+
parentType,
2432+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
24262433

24272434
// Perform member lookup into the parent's metatype.
24282435
Type parentMetaType = MetatypeType::get(parentType);
@@ -2433,8 +2440,9 @@ namespace {
24332440

24342441
// Parent type needs to be convertible to the pattern type; this
24352442
// accounts for cases where the pattern type is existential.
2436-
CS.addConstraint(ConstraintKind::Conversion, parentType, patternType,
2437-
locator);
2443+
CS.addConstraint(
2444+
ConstraintKind::Conversion, parentType, patternType,
2445+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
24382446

24392447
baseType = parentType;
24402448
} else {
@@ -2470,8 +2478,9 @@ namespace {
24702478
ConstraintKind::Equal, functionType, memberType,
24712479
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
24722480

2473-
CS.addConstraint(ConstraintKind::Conversion, outputType, baseType,
2474-
locator);
2481+
CS.addConstraint(
2482+
ConstraintKind::Conversion, outputType, baseType,
2483+
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
24752484
}
24762485

24772486
return setType(patternType);
@@ -4265,10 +4274,18 @@ bool ConstraintSystem::generateConstraints(
42654274
// For any pattern variable that has a parent variable (i.e., another
42664275
// pattern variable with the same name in the same case), require that
42674276
// the types be equivalent.
4268-
pattern->forEachVariable([&](VarDecl *var) {
4277+
pattern->forEachNode([&](Pattern *pattern) {
4278+
auto namedPattern = dyn_cast<NamedPattern>(pattern);
4279+
if (!namedPattern)
4280+
return;
4281+
4282+
auto var = namedPattern->getDecl();
42694283
if (auto parentVar = var->getParentVarDecl()) {
42704284
addConstraint(
4271-
ConstraintKind::Equal, getType(parentVar), getType(var), locator);
4285+
ConstraintKind::Equal, getType(parentVar), getType(var),
4286+
getConstraintLocator(
4287+
locator,
4288+
LocatorPathElt::PatternMatch(namedPattern)));
42724289
}
42734290
});
42744291
}

test/Constraints/function_builder_diags.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,17 @@ func testCaseMutabilityMismatches(e: E3) {
466466
}
467467
}
468468
}
469+
470+
// Check for type equivalence among different case variables with the same name.
471+
func testCaseVarTypes(e: E3) {
472+
// FIXME: Terrible diagnostic
473+
tuplify(true) { c in // expected-error{{type of expression is ambiguous without more context}}
474+
"testSwitch"
475+
switch e {
476+
case .a(let x, let y),
477+
.c(let x, let y):
478+
x
479+
y + "a"
480+
}
481+
}
482+
}

0 commit comments

Comments
 (0)