Skip to content

Commit 8bdff20

Browse files
committed
[Diagnostics] Make same-shape requirement failure diagnostics less sensitive
to binding order.
1 parent 20ff52f commit 8bdff20

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ void RequirementFailure::maybeEmitRequirementNote(const Decl *anchor, Type lhs,
487487
return;
488488
}
489489

490+
if (req.getKind() == RequirementKind::SameShape) {
491+
// Same-shape requirements are broken down into two ShapeOf
492+
// constraints against the same type variable, so a failure is not
493+
// necessarily ordered to match the original requriement. For now,
494+
// don't emit a note.
495+
return;
496+
}
497+
490498
emitDiagnosticAt(anchor, diag::where_requirement_failure_both_subst,
491499
req.getFirstType(), lhs, req.getSecondType(), rhs);
492500
}

lib/Sema/CSSimplify.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,9 +2491,9 @@ ConstraintSystem::matchPackExpansionTypes(PackExpansionType *expansion1,
24912491
locator.withPathElement(ConstraintLocator::PackShape));
24922492
auto *shapeTypeVar = createTypeVariable(shapeLoc, TVO_CanBindToPack);
24932493
addConstraint(ConstraintKind::ShapeOf,
2494-
expansion1->getCountType(), shapeTypeVar, locator);
2494+
expansion1->getCountType(), shapeTypeVar, shapeLoc);
24952495
addConstraint(ConstraintKind::ShapeOf,
2496-
expansion2->getCountType(), shapeTypeVar, locator);
2496+
expansion2->getCountType(), shapeTypeVar, shapeLoc);
24972497

24982498
auto pattern1 = expansion1->getPatternType();
24992499
auto pattern2 = expansion2->getPatternType();
@@ -6011,6 +6011,16 @@ bool ConstraintSystem::repairFailures(
60116011

60126012
case ConstraintLocator::PackShape: {
60136013
auto *shapeLocator = getConstraintLocator(locator);
6014+
6015+
// FIXME: If the anchor isn't a pack expansion, this shape requirement
6016+
// came from a same-shape generic requirement, which will fail separately
6017+
// with an applied requirement fix. Currently, pack shapes can themselves be
6018+
// pack types with pack expansions, so matching shape types can recursively
6019+
// add ShapeOf constraints. For now, skip fixing the nested ones to avoid
6020+
// cascading diagnostics.
6021+
if (!isExpr<PackExpansionExpr>(shapeLocator->getAnchor()))
6022+
return true;
6023+
60146024
auto *fix = SkipSameShapeRequirement::create(*this, lhs, rhs, shapeLocator);
60156025
conversionsOrFixes.push_back(fix);
60166026
break;

test/Constraints/variadic_generic_constraints.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ takesParallelSequences(t: Array<String>(), Set<Int>(), u: Array<Int>(), Set<Stri
5858
// Same-shape requirements
5959

6060
func zip<T..., U...>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) {}
61-
// expected-note@-1 {{where 'T' = 'T', 'U' = 'U'}}
6261

6362
let _ = zip() // ok
6463
let _ = zip(t: 1, u: "hi") // ok
@@ -74,5 +73,5 @@ func goodCallToZip<T..., U...>(t: repeat each T, u: repeat each U) where (repeat
7473

7574
func badCallToZip<T..., U...>(t: repeat each T, u: repeat each U) {
7675
_ = zip(t: repeat each t, u: repeat each u)
77-
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'T' and 'U' have the same shape}}
76+
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'U' and 'T' have the same shape}}
7877
}

0 commit comments

Comments
 (0)