Skip to content

[CSDiag] Fix crash related to failures in contextual type requirements #20028

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
Oct 25, 2018
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
3 changes: 2 additions & 1 deletion lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,8 @@ diagnoseUnresolvedDotExprTypeRequirementFailure(ConstraintSystem &cs,
// If we actually resolved the member to use, use it.
auto loc = cs.getConstraintLocator(UDE, ConstraintLocator::Member);
auto *member = cs.findResolvedMemberRef(loc);
if (!member)
// If the problem is contextual it's diagnosed elsewhere.
if (!member || !member->getAsGenericContext())
return false;

auto req = member->getAsGenericContext()
Expand Down
23 changes: 19 additions & 4 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,23 @@ class RequirementFailure : public FailureDiagnostic {
const ApplyExpr *Apply = nullptr;

public:
RequirementFailure(Expr *expr, ConstraintSystem &cs,
RequirementFailure(ConstraintSystem &cs, Expr *expr, RequirementKind kind,
ConstraintLocator *locator)
: FailureDiagnostic(expr, cs, locator), AffectedDecl(getDeclRef()) {
assert(locator);
assert(AffectedDecl);

auto path = locator->getPath();
assert(!path.empty());

auto &last = path.back();
assert(last.getKind() == ConstraintLocator::TypeParameterRequirement);
assert(static_cast<RequirementKind>(last.getValue2()) == kind);

// It's possible sometimes not to have no base expression.
if (!expr)
return;

auto *anchor = getAnchor();
expr->forEachChildExpr([&](Expr *subExpr) -> Expr * {
auto *AE = dyn_cast<ApplyExpr>(subExpr);
Expand Down Expand Up @@ -246,7 +259,7 @@ class MissingConformanceFailure final : public RequirementFailure {
MissingConformanceFailure(Expr *expr, ConstraintSystem &cs,
ConstraintLocator *locator,
std::pair<Type, ProtocolDecl *> conformance)
: RequirementFailure(expr, cs, locator),
: RequirementFailure(cs, expr, RequirementKind::Conformance, locator),
NonConformingType(conformance.first), Protocol(conformance.second) {}

bool diagnoseAsError() override;
Expand Down Expand Up @@ -294,7 +307,8 @@ class SameTypeRequirementFailure final : public RequirementFailure {
public:
SameTypeRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
Type rhs, ConstraintLocator *locator)
: RequirementFailure(expr, cs, locator), LHS(lhs), RHS(rhs) {}
: RequirementFailure(cs, expr, RequirementKind::SameType, locator),
LHS(lhs), RHS(rhs) {}

Type getLHS() const override { return LHS; }
Type getRHS() const override { return RHS; }
Expand Down Expand Up @@ -332,7 +346,8 @@ class SuperclassRequirementFailure final : public RequirementFailure {
public:
SuperclassRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
Type rhs, ConstraintLocator *locator)
: RequirementFailure(expr, cs, locator), LHS(lhs), RHS(rhs) {}
: RequirementFailure(cs, expr, RequirementKind::Superclass, locator),
LHS(lhs), RHS(rhs) {}

Type getLHS() const override { return LHS; }
Type getRHS() const override { return RHS; }
Expand Down
23 changes: 23 additions & 0 deletions test/Constraints/rdar45511837.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
// REQUIRES: objc_interop

import Foundation

protocol A: RawRepresentable {}

extension A {
static func +(lhs: RawValue, rhs: Self) -> Self {
fatalError()
}
}

class Foo<Bar: NSObject> {
var foobar: Bar {
fatalError()
}

lazy var foo: () -> Void = {
// TODO: improve diagnostic message
_ = self.foobar + nil // expected-error {{'Foo<Bar>' requires that 'Bar' inherit from 'NSObject'}}
}
}