Skip to content

Commit 75161ce

Browse files
committed
RequirementMachine: Tweak fixit that turns 'T : Int' into 'T == Int' slightly to match GSB
1 parent 22f0f40 commit 75161ce

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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).
668683
bool 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);
@@ -902,7 +917,8 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
902917

903918
if (ctx.LangOpts.RequirementMachineProtocolSignatures ==
904919
RequirementMachineMode::Enabled) {
905-
diagnoseRequirementErrors(ctx, errors, /*allowConcreteGenericParams=*/false);
920+
diagnoseRequirementErrors(ctx, errors,
921+
AllowConcreteTypePolicy::NestedAssocTypes);
906922
}
907923

908924
return ctx.AllocateCopy(result);
@@ -1175,7 +1191,8 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
11751191

11761192
if (ctx.LangOpts.RequirementMachineProtocolSignatures ==
11771193
RequirementMachineMode::Enabled) {
1178-
diagnoseRequirementErrors(ctx, errors, /*allowConcreteGenericParams=*/false);
1194+
diagnoseRequirementErrors(ctx, errors,
1195+
AllowConcreteTypePolicy::NestedAssocTypes);
11791196
}
11801197

11811198
return ctx.AllocateCopy(result);

lib/AST/RequirementMachine/RequirementLowering.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,24 @@ void realizeInheritedRequirements(TypeDecl *decl, Type type,
5555
SmallVectorImpl<StructuralRequirement> &result,
5656
SmallVectorImpl<RequirementError> &errors);
5757

58+
/// Policy for the fixit that transforms 'T : S' where 'S' is not a protocol
59+
/// or a class into 'T == S'.
60+
enum AllowConcreteTypePolicy {
61+
/// Any type parameter can be concrete.
62+
All,
63+
64+
/// Only associated types can be concrete.
65+
AssocTypes,
66+
67+
/// Only nested associated types can be concrete. This is for protocols,
68+
/// where we don't want to suggest making an associated type member of
69+
/// 'Self' concrete.
70+
NestedAssocTypes
71+
};
72+
5873
bool diagnoseRequirementErrors(ASTContext &ctx,
5974
ArrayRef<RequirementError> errors,
60-
bool allowConcreteGenericParams);
75+
AllowConcreteTypePolicy concreteTypePolicy);
6176

6277
// Defined in ConcreteContraction.cpp.
6378
bool performConcreteContraction(

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ RequirementSignatureRequestRQM::evaluate(Evaluator &evaluator,
412412
SmallVector<RequirementError, 4> errors;
413413
machine->computeRequirementDiagnostics(errors, proto->getLoc());
414414
diagnoseRequirementErrors(ctx, errors,
415-
/*allowConcreteGenericParams=*/false);
415+
AllowConcreteTypePolicy::NestedAssocTypes);
416416
}
417417

418418
if (!machine->getErrors()) {
@@ -844,7 +844,10 @@ InferredGenericSignatureRequestRQM::evaluate(
844844
ctx.LangOpts.RequirementMachineInferredSignatures ==
845845
RequirementMachineMode::Enabled) {
846846
machine->computeRequirementDiagnostics(errors, loc);
847-
diagnoseRequirementErrors(ctx, errors, allowConcreteGenericParams);
847+
diagnoseRequirementErrors(ctx, errors,
848+
allowConcreteGenericParams
849+
? AllowConcreteTypePolicy::All
850+
: AllowConcreteTypePolicy::AssocTypes);
848851
}
849852

850853
// FIXME: Handle allowConcreteGenericParams

test/Constraints/generics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-objc-interop
1+
// RUN: %target-typecheck-verify-swift -enable-objc-interop -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
22

33
infix operator +++
44

0 commit comments

Comments
 (0)