Skip to content

Commit 74cc627

Browse files
committed
[RequirementMachine] Diagnose same-element requirements.
The rewrite rules are not quite right yet for same-element requirements, so let's ban them for now.
1 parent e7f82bc commit 74cc627

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,9 @@ ERROR(requires_not_suitable_archetype,none,
27332733
ERROR(invalid_shape_requirement,none,
27342734
"invalid same-shape requirement between %0 and %1",
27352735
(Type, Type))
2736+
ERROR(unsupported_same_element,none,
2737+
"same-element requirements are not yet supported",
2738+
())
27362739

27372740
ERROR(requires_generic_params_made_equal,none,
27382741
"same-type requirement makes generic parameters %0 and %1 equivalent",

lib/AST/RequirementMachine/Diagnostics.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ bool swift::rewriting::diagnoseRequirementErrors(
209209

210210
break;
211211
}
212+
213+
case RequirementError::Kind::UnsupportedSameElement: {
214+
if (error.requirement.hasError())
215+
break;
216+
217+
ctx.Diags.diagnose(loc, diag::unsupported_same_element);
218+
diagnosedError = true;
219+
break;
220+
}
212221
}
213222
}
214223

lib/AST/RequirementMachine/Diagnostics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ struct RequirementError {
4242
RecursiveRequirement,
4343
/// A redundant requirement, e.g. T == T.
4444
RedundantRequirement,
45+
/// A not-yet-supported same-element requirement, e.g. each T == Int.
46+
UnsupportedSameElement,
4547
} kind;
4648

4749
/// The invalid requirement.
@@ -100,6 +102,10 @@ struct RequirementError {
100102
SourceLoc loc) {
101103
return {Kind::RecursiveRequirement, req, loc};
102104
}
105+
106+
static RequirementError forSameElement(Requirement req, SourceLoc loc) {
107+
return {Kind::UnsupportedSameElement, req, loc};
108+
}
103109
};
104110

105111
/// Policy for the fixit that transforms 'T : S' where 'S' is not a protocol

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ static void desugarSameTypeRequirement(Type lhs, Type rhs, SourceLoc loc,
194194

195195
bool mismatch(TypeBase *firstType, TypeBase *secondType,
196196
Type sugaredFirstType) {
197+
// If one side is a parameter pack, this is a same-element requirement, which
198+
// is not yet supported.
199+
if (firstType->isParameterPack() != secondType->isParameterPack()) {
200+
errors.push_back(RequirementError::forSameElement(
201+
{RequirementKind::SameType, sugaredFirstType, secondType}, loc));
202+
recordedErrors = true;
203+
return true;
204+
}
205+
197206
if (firstType->isTypeParameter() && secondType->isTypeParameter()) {
198207
result.emplace_back(RequirementKind::SameType,
199208
sugaredFirstType, secondType);

test/Constraints/pack-expansion-expressions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,20 @@ func outerArchetype<each T, U>(t: repeat each T, u: U) where each T: P {
5656
}
5757

5858
func sameElement<each T, U>(t: repeat each T, u: U) where each T: P, each T == U {
59+
// expected-error@-1{{same-element requirements are not yet supported}}
60+
61+
// FIXME: Opened element archetypes in diagnostics
5962
let _: repeat each T = repeat (each t).f(u)
63+
// expected-error@-1 {{cannot convert value of type 'U' to expected argument type 'τ_1_0'}}
6064
}
6165

6266
func forEachEach<each C, U>(c: repeat each C, function: (U) -> Void)
6367
where each C: Collection, each C.Element == U {
68+
// expected-error@-1{{same-element requirements are not yet supported}}
69+
70+
// FIXME: Opened element archetypes in diagnostics
6471
_ = repeat (each c).forEach(function)
72+
// expected-error@-1 {{cannot convert value of type '(U) -> Void' to expected argument type '(τ_1_0.Element) throws -> Void'}}
6573
}
6674

6775
func typeReprPacks<each T>(_ t: repeat each T) where each T: ExpressibleByIntegerLiteral {

0 commit comments

Comments
 (0)