Skip to content

Commit ef4328b

Browse files
authored
Merge pull request #8263 from DougGregor/redundant-same-type-constraints
2 parents 631c7d8 + 7763e96 commit ef4328b

File tree

7 files changed

+458
-190
lines changed

7 files changed

+458
-190
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,13 @@ NOTE(previous_layout_constraint, none,
16011601
"layout constraint constraint %1 : %2 %select{written here|implied here}0",
16021602
(bool, Type, LayoutConstraint))
16031603

1604+
WARNING(redundant_same_type_constraint,none,
1605+
"redundant same-type constraint %0 == %1", (Type, Type))
1606+
NOTE(previous_same_type_constraint, none,
1607+
"previous same-type constraint %1 == %2 "
1608+
"%select{written here|implied here}0",
1609+
(bool, Type, Type))
1610+
16041611
ERROR(generic_param_access,none,
16051612
"%0 %select{must be declared %select{"
16061613
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ class GenericSignatureBuilder {
137137
/// The members of the equivalence class.
138138
TinyPtrVector<PotentialArchetype *> members;
139139

140+
/// Describes a component within the graph of same-type constraints within
141+
/// the equivalence class that is held together by derived constraints.
142+
struct DerivedSameTypeComponent {
143+
/// The potential archetype that acts as the anchor for this component.
144+
PotentialArchetype *anchor;
145+
146+
/// The (best) requirement source within the component that makes the
147+
/// potential archetypes in this component equivalent to the concrete
148+
/// type.
149+
const RequirementSource *concreteTypeSource;
150+
};
151+
152+
/// The set of connected components within this equivalence class, using
153+
/// only the derived same-type constraints in the graph.
154+
std::vector<DerivedSameTypeComponent> derivedSameTypeComponents;
155+
140156
/// Construct a new equivalence class containing only the given
141157
/// potential archetype (which represents itself).
142158
EquivalenceClass(PotentialArchetype *representative);
@@ -462,6 +478,32 @@ class GenericSignatureBuilder {
462478
Diag<Type, T> redundancyDiag,
463479
Diag<bool, Type, T> otherNoteDiag);
464480

481+
/// Check a list of constraints, removing self-derived constraints
482+
/// and diagnosing redundant constraints.
483+
///
484+
/// \param isSuitableRepresentative Determines whether the given constraint
485+
/// is a suitable representative.
486+
///
487+
/// \param checkConstraint Checks the given constraint against the
488+
/// canonical constraint to determine which diagnostics (if any) should be
489+
/// emitted.
490+
///
491+
/// \returns the representative constraint.
492+
template<typename T, typename DiagT>
493+
Constraint<T> checkConstraintList(
494+
ArrayRef<GenericTypeParamType *> genericParams,
495+
std::vector<Constraint<T>> &constraints,
496+
llvm::function_ref<bool(const Constraint<T> &)>
497+
isSuitableRepresentative,
498+
llvm::function_ref<ConstraintRelation(const T&)>
499+
checkConstraint,
500+
Optional<Diag<unsigned, Type, DiagT, DiagT>>
501+
conflictingDiag,
502+
Diag<Type, DiagT> redundancyDiag,
503+
Diag<bool, Type, DiagT> otherNoteDiag,
504+
llvm::function_ref<DiagT(const T&)> diagValue,
505+
bool removeSelfDerived);
506+
465507
/// Check the concrete type constraints within the equivalence
466508
/// class of the given potential archetype.
467509
void checkConcreteTypeConstraints(

0 commit comments

Comments
 (0)