Skip to content

[CS] Look at the fn's rvalue type in getCalleeLocator #26869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,12 @@ Type RequirementFailure::getOwnerType() const {
const GenericContext *RequirementFailure::getGenericContext() const {
if (auto *genericCtx = AffectedDecl->getAsGenericContext())
return genericCtx;
return AffectedDecl->getDeclContext()->getAsDecl()->getAsGenericContext();

auto parentDecl = AffectedDecl->getDeclContext()->getAsDecl();
if (!parentDecl)
return nullptr;

return parentDecl->getAsGenericContext();
}

const Requirement &RequirementFailure::getRequirement() const {
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ class RequirementFailure : public FailureDiagnostic {
assert(locator);
assert(isConditional() || Signature);
assert(AffectedDecl);
assert(getRequirementDC() &&
"Couldn't find where the requirement came from?");
assert(getGenericContext() &&
"Affected decl not within a generic context?");

auto reqElt = locator->castLastElementTo<LocatorPathElt::AnyRequirement>();
assert(reqElt.getRequirementKind() == kind);
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(Expr *expr) {
// For an apply of a metatype, we have a short-form constructor. Unlike
// other locators to callees, these are anchored on the apply expression
// rather than the function expr.
if (simplifyType(getType(fnExpr))->is<AnyMetatypeType>()) {
auto fnTy = getFixedTypeRecursive(getType(fnExpr), /*wantRValue*/ true);
if (fnTy->is<AnyMetatypeType>()) {
auto *fnLocator =
getConstraintLocator(applyExpr, ConstraintLocator::ApplyFunction);
return getConstraintLocator(fnLocator,
Expand Down
6 changes: 5 additions & 1 deletion test/Constraints/generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ protocol Q {
}

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

Expand All @@ -726,6 +726,10 @@ struct SR10694 {

type(of: q)(x) // expected-error {{initializer 'init(_:)' requires that 'T' conform to 'P'}}
// expected-error@-1 {{initializing from a metatype value must reference 'init' explicitly}}

var srTy = SR10694.self
srTy(x) // expected-error {{initializer 'init(_:)' requires that 'T' conform to 'P'}}
// expected-error@-1 {{initializing from a metatype value must reference 'init' explicitly}}
}
}

Expand Down