Skip to content

Commit 2d8f002

Browse files
committed
[CSDiagnostics] Adjacent diagnostics for the new rules of static member lookup in generic context
1 parent c44a92a commit 2d8f002

File tree

5 files changed

+113
-51
lines changed

5 files changed

+113
-51
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,9 +1902,8 @@ ERROR(type_does_not_conform_owner,none,
19021902
ERROR(type_does_not_conform_in_decl_ref,none,
19031903
"referencing %0 %1 on %2 requires that %3 conform to %4",
19041904
(DescriptiveDeclKind, DeclName, Type, Type, Type))
1905-
ERROR(type_does_not_conform_in_member_ref_on_protocol_type,none,
1906-
"cannot reference %0 %1 on %2 with non-conforming result type %3",
1907-
(DescriptiveDeclKind, DeclName, Type, Type))
1905+
NOTE(missing_sametype_requirement_on_self,none,
1906+
"missing same-type requirement on 'Self'", ())
19081907
ERROR(type_does_not_conform_anyobject_in_decl_ref,none,
19091908
"referencing %0 %1 on %2 requires that %3 be a class type",
19101909
(DescriptiveDeclKind, DeclName, Type, Type, Type))

lib/Sema/CSDiagnostics.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7199,25 +7199,29 @@ bool InvalidMemberRefOnProtocolMetatype::diagnoseAsError() {
71997199
if (!overload)
72007200
return false;
72017201

7202-
auto resultTy = resolveType(overload->openedType);
7203-
if (auto *fnType = resultTy->getAs<FunctionType>())
7204-
resultTy = fnType->getResult();
7205-
72067202
auto *member = overload->choice.getDeclOrNull();
72077203
assert(member);
72087204

7209-
auto *DC = member->getDeclContext()->getSelfProtocolDecl();
7210-
auto protocolTy = DC->getDeclaredInterfaceType();
7205+
emitDiagnostic(diag::cannot_infer_base_of_unresolved_member,
7206+
DeclNameRef(member->getName()));
7207+
7208+
auto *extension = dyn_cast<ExtensionDecl>(member->getDeclContext());
7209+
7210+
// If this was a protocol requirement we can't suggest a fix-it.
7211+
if (!extension)
7212+
return true;
72117213

7212-
emitDiagnostic(diag::type_does_not_conform_in_member_ref_on_protocol_type,
7213-
member->getDescriptiveKind(), member->getName(),
7214-
MetatypeType::get(protocolTy), resultTy);
7214+
auto note =
7215+
emitDiagnosticAt(extension, diag::missing_sametype_requirement_on_self);
72157216

7216-
if (resultTy->is<FunctionType>() || resultTy->is<TupleType>() ||
7217-
resultTy->isExistentialType() || resultTy->is<AnyMetatypeType>())
7218-
emitDiagnostic(diag::only_concrete_types_conform_to_protocols);
7217+
if (auto *whereClause = extension->getTrailingWhereClause()) {
7218+
auto sourceRange = whereClause->getSourceRange();
7219+
note.fixItInsertAfter(sourceRange.End, ", Self == <#Type#> ");
7220+
} else {
7221+
auto nameRepr = extension->getExtendedTypeRepr();
7222+
note.fixItInsertAfter(nameRepr->getEndLoc(), " where Self == <#Type#>");
7223+
}
72197224

7220-
emitDiagnosticAt(member, diag::decl_declared_here, member->getName());
72217225
return true;
72227226
}
72237227

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,6 +5972,10 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
59725972
// If this is a `Self` conformance requirement from a static member
59735973
// reference on a protocol metatype, let's produce a tailored diagnostic.
59745974
if (memberRef->isStatic()) {
5975+
if (hasFixFor(memberLoc,
5976+
FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype))
5977+
return SolutionKind::Solved;
5978+
59755979
if (auto *protocolDecl =
59765980
memberRef->getDeclContext()->getSelfProtocolDecl()) {
59775981
auto selfTy = protocolDecl->getProtocolSelfType();
@@ -6869,8 +6873,9 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
68696873
if (getConcreteReplacementForProtocolSelfType(decl)) {
68706874
result.addViable(candidate);
68716875
} else {
6872-
result.addUnviable(candidate,
6873-
MemberLookupResult::UR_TypeMemberOnInstance);
6876+
result.addUnviable(
6877+
candidate,
6878+
MemberLookupResult::UR_InvalidStaticMemberOnProtocolMetatype);
68746879
}
68756880

68766881
return;

0 commit comments

Comments
 (0)