Skip to content

[GSB] Diagnose redundant same-type constraints. #8263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,13 @@ NOTE(previous_layout_constraint, none,
"layout constraint constraint %1 : %2 %select{written here|implied here}0",
(bool, Type, LayoutConstraint))

WARNING(redundant_same_type_constraint,none,
"redundant same-type constraint %0 == %1", (Type, Type))
NOTE(previous_same_type_constraint, none,
"previous same-type constraint %1 == %2 "
"%select{written here|implied here}0",
(bool, Type, Type))

ERROR(generic_param_access,none,
"%0 %select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
Expand Down
42 changes: 42 additions & 0 deletions include/swift/AST/GenericSignatureBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ class GenericSignatureBuilder {
/// The members of the equivalence class.
TinyPtrVector<PotentialArchetype *> members;

/// Describes a component within the graph of same-type constraints within
/// the equivalence class that is held together by derived constraints.
struct DerivedSameTypeComponent {
/// The potential archetype that acts as the anchor for this component.
PotentialArchetype *anchor;

/// The (best) requirement source within the component that makes the
/// potential archetypes in this component equivalent to the concrete
/// type.
const RequirementSource *concreteTypeSource;
};

/// The set of connected components within this equivalence class, using
/// only the derived same-type constraints in the graph.
std::vector<DerivedSameTypeComponent> derivedSameTypeComponents;

/// Construct a new equivalence class containing only the given
/// potential archetype (which represents itself).
EquivalenceClass(PotentialArchetype *representative);
Expand Down Expand Up @@ -462,6 +478,32 @@ class GenericSignatureBuilder {
Diag<Type, T> redundancyDiag,
Diag<bool, Type, T> otherNoteDiag);

/// Check a list of constraints, removing self-derived constraints
/// and diagnosing redundant constraints.
///
/// \param isSuitableRepresentative Determines whether the given constraint
/// is a suitable representative.
///
/// \param checkConstraint Checks the given constraint against the
/// canonical constraint to determine which diagnostics (if any) should be
/// emitted.
///
/// \returns the representative constraint.
template<typename T, typename DiagT>
Constraint<T> checkConstraintList(
ArrayRef<GenericTypeParamType *> genericParams,
std::vector<Constraint<T>> &constraints,
llvm::function_ref<bool(const Constraint<T> &)>
isSuitableRepresentative,
llvm::function_ref<ConstraintRelation(const T&)>
checkConstraint,
Optional<Diag<unsigned, Type, DiagT, DiagT>>
conflictingDiag,
Diag<Type, DiagT> redundancyDiag,
Diag<bool, Type, DiagT> otherNoteDiag,
llvm::function_ref<DiagT(const T&)> diagValue,
bool removeSelfDerived);

/// Check the concrete type constraints within the equivalence
/// class of the given potential archetype.
void checkConcreteTypeConstraints(
Expand Down
Loading