Sema: Clean up leading dot fix logic in simplifyConformsToConstraint()

Tests still pass without this code, including the tests I just
added in the previous commit.
This commit is contained in:
Slava Pestov
2025-10-20 15:09:06 -04:00
parent 8d247100d8
commit 1b6ac2c1d8
2 changed files with 33 additions and 37 deletions

View File

@@ -8859,7 +8859,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
return SolutionKind::Solved; return SolutionKind::Solved;
} }
auto formUnsolved = [&](bool activate = false) { auto formUnsolved = [&]() {
// If we're supposed to generate constraints, do so. // If we're supposed to generate constraints, do so.
if (flags.contains(TMF_GenerateConstraints)) { if (flags.contains(TMF_GenerateConstraints)) {
auto *conformance = Constraint::create( auto *conformance = Constraint::create(
@@ -8867,9 +8867,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
getConstraintLocator(locator)); getConstraintLocator(locator));
addUnsolvedConstraint(conformance); addUnsolvedConstraint(conformance);
if (activate)
activateConstraint(conformance);
return SolutionKind::Solved; return SolutionKind::Solved;
} }
@@ -9243,46 +9240,41 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
if (isExpr<UnresolvedMemberExpr>(anchor) && if (isExpr<UnresolvedMemberExpr>(anchor) &&
req->is<LocatorPathElt::TypeParameterRequirement>()) { req->is<LocatorPathElt::TypeParameterRequirement>()) {
auto *memberLoc = getConstraintLocator(anchor, path.front());
auto signature = path[path.size() - 2] auto signature = path[path.size() - 2]
.castTo<LocatorPathElt::OpenedGeneric>() .castTo<LocatorPathElt::OpenedGeneric>()
.getSignature(); .getSignature();
auto requirement = signature.getRequirements()[req->getIndex()]; auto requirement = signature.getRequirements()[req->getIndex()];
auto *memberLoc = getConstraintLocator(anchor, path.front()); auto attemptInvalidStaticMemberRefOnMetatypeFix = [&]() {
auto overload = findSelectedOverloadFor(memberLoc); // If the failed requirement isn't the first generic parameter,
// it can't be a static member reference on a protocol metatype.
if (!requirement.getFirstType()->isEqual(getASTContext().TheSelfType))
return false;
// To figure out what is going on here we need to wait until // If we don't know the overload yet, conservatively assume it's
// member overload is set in the constraint system. // a static member reference on a protocol metatype.
if (!overload) { auto overload = findSelectedOverloadFor(memberLoc);
// If it's not allowed to generate new constraints if (!overload)
// there is no way to control re-activation, so this return true;
// check has to fail.
if (!flags.contains(TMF_GenerateConstraints))
return SolutionKind::Error;
return formUnsolved(/*activate=*/true); auto *decl = overload->choice.getDeclOrNull();
} if (!decl)
return true;
auto *memberRef = overload->choice.getDeclOrNull(); // Otherwise, we can do a precise check.
if (!memberRef) if (!decl->isStatic())
return SolutionKind::Error; return false;
// If this is a `Self` conformance requirement from a static member return decl->getDeclContext()->getSelfProtocolDecl() != nullptr;
// reference on a protocol metatype, let's produce a tailored diagnostic. };
if (memberRef->isStatic()) {
if (hasFixFor(memberLoc,
FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype))
return SolutionKind::Solved;
if (auto *protocolDecl = if (attemptInvalidStaticMemberRefOnMetatypeFix()) {
memberRef->getDeclContext()->getSelfProtocolDecl()) { auto *fix = AllowInvalidStaticMemberRefOnProtocolMetatype::create(
auto selfTy = protocolDecl->getSelfInterfaceType(); *this, memberLoc);
if (selfTy->isEqual(requirement.getFirstType())) {
auto *fix = AllowInvalidStaticMemberRefOnProtocolMetatype::create( return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
*this, memberLoc);
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
}
}
} }
} }

View File

@@ -177,7 +177,7 @@ test_combo(.genericFn(42)) // expected-error {{global function 'test_combo' requ
extension P { // expected-note 13 {{missing same-type requirement on 'Self'}} {{12-12= where Self == <#Type#>}} extension P { // expected-note 13 {{missing same-type requirement on 'Self'}} {{12-12= where Self == <#Type#>}}
static func generic<T>(_: T) -> T { fatalError() } static func generic<T>(_: T) -> T { fatalError() }
static func genericWithReqs<T: Collection, Q>(_: T) -> Q where T.Element == Q { // expected-note {{required by static method 'genericWithReqs' where 'T' = '()'}} static func genericWithReqs<T: Collection, Q>(_: T) -> Q where T.Element == Q { // expected-note 3 {{required by static method 'genericWithReqs' where 'T' = '()'}}
fatalError() fatalError()
} }
} }
@@ -246,7 +246,9 @@ test(.genericWithReqs([S()])) // expected-error {{contextual member reference to
test(.genericWithReqs([42])) test(.genericWithReqs([42]))
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}} // expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
test(.genericWithReqs(())) test(.genericWithReqs(()))
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}} // expected-error@-1 {{type '()' cannot conform to 'Collection'}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-error@-3 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
test_combo(.doesntExist) // expected-error {{reference to member 'doesntExist' cannot be resolved without a contextual type}} test_combo(.doesntExist) // expected-error {{reference to member 'doesntExist' cannot be resolved without a contextual type}}
test_combo(.doesnt.exist()) // expected-error {{reference to member 'doesnt' cannot be resolved without a contextual type}} test_combo(.doesnt.exist()) // expected-error {{reference to member 'doesnt' cannot be resolved without a contextual type}}
@@ -262,7 +264,9 @@ test_combo(.genericWithReqs([S()])) // expected-error {{contextual member refere
test_combo(.genericWithReqs([42])) test_combo(.genericWithReqs([42]))
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}} // expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
test_combo(.genericWithReqs(())) test_combo(.genericWithReqs(()))
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}} // expected-error@-1 {{type '()' cannot conform to 'Collection'}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-error@-3 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
protocol TestWithAssoc { protocol TestWithAssoc {
associatedtype U associatedtype U