Skip to content

Commit 987e2c6

Browse files
authored
Merge pull request #26869 from hamishknight/the-right-value
[CS] Look at the fn's rvalue type in getCalleeLocator
2 parents 9a5d6fe + ef1d5ab commit 987e2c6

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,12 @@ Type RequirementFailure::getOwnerType() const {
310310
const GenericContext *RequirementFailure::getGenericContext() const {
311311
if (auto *genericCtx = AffectedDecl->getAsGenericContext())
312312
return genericCtx;
313-
return AffectedDecl->getDeclContext()->getAsDecl()->getAsGenericContext();
313+
314+
auto parentDecl = AffectedDecl->getDeclContext()->getAsDecl();
315+
if (!parentDecl)
316+
return nullptr;
317+
318+
return parentDecl->getAsGenericContext();
314319
}
315320

316321
const Requirement &RequirementFailure::getRequirement() const {

lib/Sema/CSDiagnostics.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ class RequirementFailure : public FailureDiagnostic {
236236
assert(locator);
237237
assert(isConditional() || Signature);
238238
assert(AffectedDecl);
239+
assert(getRequirementDC() &&
240+
"Couldn't find where the requirement came from?");
241+
assert(getGenericContext() &&
242+
"Affected decl not within a generic context?");
239243

240244
auto reqElt = locator->castLastElementTo<LocatorPathElt::AnyRequirement>();
241245
assert(reqElt.getRequirementKind() == kind);

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(Expr *expr) {
425425
// For an apply of a metatype, we have a short-form constructor. Unlike
426426
// other locators to callees, these are anchored on the apply expression
427427
// rather than the function expr.
428-
if (simplifyType(getType(fnExpr))->is<AnyMetatypeType>()) {
428+
auto fnTy = getFixedTypeRecursive(getType(fnExpr), /*wantRValue*/ true);
429+
if (fnTy->is<AnyMetatypeType>()) {
429430
auto *fnLocator =
430431
getConstraintLocator(applyExpr, ConstraintLocator::ApplyFunction);
431432
return getConstraintLocator(fnLocator,

test/Constraints/generics.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ protocol Q {
714714
}
715715

716716
struct SR10694 {
717-
init<T : P>(_ x: T) {} // expected-note 2{{where 'T' = 'T'}}
717+
init<T : P>(_ x: T) {} // expected-note 3{{where 'T' = 'T'}}
718718
func bar<T>(_ x: T, _ s: SR10694, _ q: Q) {
719719
SR10694.self(x) // expected-error {{initializer 'init(_:)' requires that 'T' conform to 'P'}}
720720

@@ -726,6 +726,10 @@ struct SR10694 {
726726

727727
type(of: q)(x) // expected-error {{initializer 'init(_:)' requires that 'T' conform to 'P'}}
728728
// expected-error@-1 {{initializing from a metatype value must reference 'init' explicitly}}
729+
730+
var srTy = SR10694.self
731+
srTy(x) // expected-error {{initializer 'init(_:)' requires that 'T' conform to 'P'}}
732+
// expected-error@-1 {{initializing from a metatype value must reference 'init' explicitly}}
729733
}
730734
}
731735

0 commit comments

Comments
 (0)