@@ -654,20 +654,35 @@ void swift::rewriting::realizeInheritedRequirements(
654654 }
655655}
656656
657+ static bool shouldSuggestConcreteTypeFixit (
658+ Type type, AllowConcreteTypePolicy concreteTypePolicy) {
659+ switch (concreteTypePolicy) {
660+ case AllowConcreteTypePolicy::All:
661+ return true ;
662+
663+ case AllowConcreteTypePolicy::AssocTypes:
664+ return type->is <DependentMemberType>();
665+
666+ case AllowConcreteTypePolicy::NestedAssocTypes:
667+ if (auto *memberType = type->getAs <DependentMemberType>())
668+ return memberType->getBase ()->is <DependentMemberType>();
669+
670+ return false ;
671+ }
672+ }
673+
657674// / Emit diagnostics for the given \c RequirementErrors.
658675// /
659676// / \param ctx The AST context in which to emit diagnostics.
660677// / \param errors The set of requirement diagnostics to be emitted.
661- // / \param allowConcreteGenericParams Whether concrete type parameters
662- // / are permitted in the generic signature. If true, diagnostics will
663- // / offer fix-its to turn invalid type requirements, e.g. T: Int, into
664- // / same-type requirements.
678+ // / \param concreteTypePolicy Whether fix-its should be offered to turn
679+ // / invalid type requirements, e.g. T: Int, into same-type requirements.
665680// /
666681// / \returns true if any errors were emitted, and false otherwise (including
667682// / when only warnings were emitted).
668683bool swift::rewriting::diagnoseRequirementErrors (
669684 ASTContext &ctx, ArrayRef<RequirementError> errors,
670- bool allowConcreteGenericParams ) {
685+ AllowConcreteTypePolicy concreteTypePolicy ) {
671686 bool diagnosedError = false ;
672687
673688 for (auto error : errors) {
@@ -697,7 +712,7 @@ bool swift::rewriting::diagnoseRequirementErrors(
697712 return subjectTypeName;
698713 };
699714
700- if (allowConcreteGenericParams ) {
715+ if (shouldSuggestConcreteTypeFixit (subjectType, concreteTypePolicy) ) {
701716 auto options = PrintOptions::forDiagnosticArguments ();
702717 auto subjectTypeName = subjectType.getString (options);
703718 auto subjectTypeNameWithoutSelf = getNameWithoutSelf (subjectTypeName);
@@ -725,16 +740,25 @@ bool swift::rewriting::diagnoseRequirementErrors(
725740 auto requirement = error.requirement ;
726741 auto conflict = error.conflictingRequirement ;
727742
743+ if (requirement.getFirstType ()->hasError () ||
744+ (requirement.getKind () != RequirementKind::Layout &&
745+ requirement.getSecondType ()->hasError ())) {
746+ // Don't emit a cascading error.
747+ break ;
748+ }
749+
728750 if (!conflict) {
729- if (requirement.getFirstType ()->hasError () ||
730- requirement.getSecondType ()->hasError ()) {
731- // Don't emit a cascading error.
732- break ;
733- }
734751 ctx.Diags .diagnose (loc, diag::requires_same_concrete_type,
735752 requirement.getFirstType (),
736753 requirement.getSecondType ());
737754 } else {
755+ if (conflict->getFirstType ()->hasError () ||
756+ (conflict->getKind () != RequirementKind::Layout &&
757+ conflict->getSecondType ()->hasError ())) {
758+ // Don't emit a cascading error.
759+ break ;
760+ }
761+
738762 auto options = PrintOptions::forDiagnosticArguments ();
739763 std::string requirements;
740764 llvm::raw_string_ostream OS (requirements);
@@ -754,6 +778,13 @@ bool swift::rewriting::diagnoseRequirementErrors(
754778
755779 case RequirementError::Kind::RedundantRequirement: {
756780 auto requirement = error.requirement ;
781+ if (requirement.getFirstType ()->hasError () ||
782+ (requirement.getKind () != RequirementKind::Layout &&
783+ requirement.getSecondType ()->hasError ())) {
784+ // Don't emit a cascading error.
785+ break ;
786+ }
787+
757788 switch (requirement.getKind ()) {
758789 case RequirementKind::SameType:
759790 ctx.Diags .diagnose (loc, diag::redundant_same_type_to_concrete,
@@ -886,7 +917,8 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
886917
887918 if (ctx.LangOpts .RequirementMachineProtocolSignatures ==
888919 RequirementMachineMode::Enabled) {
889- diagnoseRequirementErrors (ctx, errors, /* allowConcreteGenericParams=*/ false );
920+ diagnoseRequirementErrors (ctx, errors,
921+ AllowConcreteTypePolicy::NestedAssocTypes);
890922 }
891923
892924 return ctx.AllocateCopy (result);
@@ -1159,7 +1191,8 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
11591191
11601192 if (ctx.LangOpts .RequirementMachineProtocolSignatures ==
11611193 RequirementMachineMode::Enabled) {
1162- diagnoseRequirementErrors (ctx, errors, /* allowConcreteGenericParams=*/ false );
1194+ diagnoseRequirementErrors (ctx, errors,
1195+ AllowConcreteTypePolicy::NestedAssocTypes);
11631196 }
11641197
11651198 return ctx.AllocateCopy (result);
0 commit comments