Skip to content

Commit 75b1c5c

Browse files
authored
Merge pull request #20028 from xedin/rdar-45511837
[CSDiag] Fix crash related to failures in contextual type requirements
2 parents e34a6a1 + 17d46d1 commit 75b1c5c

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,8 @@ diagnoseUnresolvedDotExprTypeRequirementFailure(ConstraintSystem &cs,
12891289
// If we actually resolved the member to use, use it.
12901290
auto loc = cs.getConstraintLocator(UDE, ConstraintLocator::Member);
12911291
auto *member = cs.findResolvedMemberRef(loc);
1292-
if (!member)
1292+
// If the problem is contextual it's diagnosed elsewhere.
1293+
if (!member || !member->getAsGenericContext())
12931294
return false;
12941295

12951296
auto req = member->getAsGenericContext()

lib/Sema/CSDiagnostics.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,23 @@ class RequirementFailure : public FailureDiagnostic {
160160
const ApplyExpr *Apply = nullptr;
161161

162162
public:
163-
RequirementFailure(Expr *expr, ConstraintSystem &cs,
163+
RequirementFailure(ConstraintSystem &cs, Expr *expr, RequirementKind kind,
164164
ConstraintLocator *locator)
165165
: FailureDiagnostic(expr, cs, locator), AffectedDecl(getDeclRef()) {
166+
assert(locator);
166167
assert(AffectedDecl);
168+
169+
auto path = locator->getPath();
170+
assert(!path.empty());
171+
172+
auto &last = path.back();
173+
assert(last.getKind() == ConstraintLocator::TypeParameterRequirement);
174+
assert(static_cast<RequirementKind>(last.getValue2()) == kind);
175+
176+
// It's possible sometimes not to have no base expression.
177+
if (!expr)
178+
return;
179+
167180
auto *anchor = getAnchor();
168181
expr->forEachChildExpr([&](Expr *subExpr) -> Expr * {
169182
auto *AE = dyn_cast<ApplyExpr>(subExpr);
@@ -246,7 +259,7 @@ class MissingConformanceFailure final : public RequirementFailure {
246259
MissingConformanceFailure(Expr *expr, ConstraintSystem &cs,
247260
ConstraintLocator *locator,
248261
std::pair<Type, ProtocolDecl *> conformance)
249-
: RequirementFailure(expr, cs, locator),
262+
: RequirementFailure(cs, expr, RequirementKind::Conformance, locator),
250263
NonConformingType(conformance.first), Protocol(conformance.second) {}
251264

252265
bool diagnoseAsError() override;
@@ -294,7 +307,8 @@ class SameTypeRequirementFailure final : public RequirementFailure {
294307
public:
295308
SameTypeRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
296309
Type rhs, ConstraintLocator *locator)
297-
: RequirementFailure(expr, cs, locator), LHS(lhs), RHS(rhs) {}
310+
: RequirementFailure(cs, expr, RequirementKind::SameType, locator),
311+
LHS(lhs), RHS(rhs) {}
298312

299313
Type getLHS() const override { return LHS; }
300314
Type getRHS() const override { return RHS; }
@@ -332,7 +346,8 @@ class SuperclassRequirementFailure final : public RequirementFailure {
332346
public:
333347
SuperclassRequirementFailure(Expr *expr, ConstraintSystem &cs, Type lhs,
334348
Type rhs, ConstraintLocator *locator)
335-
: RequirementFailure(expr, cs, locator), LHS(lhs), RHS(rhs) {}
349+
: RequirementFailure(cs, expr, RequirementKind::Superclass, locator),
350+
LHS(lhs), RHS(rhs) {}
336351

337352
Type getLHS() const override { return LHS; }
338353
Type getRHS() const override { return RHS; }

test/Constraints/rdar45511837.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
6+
protocol A: RawRepresentable {}
7+
8+
extension A {
9+
static func +(lhs: RawValue, rhs: Self) -> Self {
10+
fatalError()
11+
}
12+
}
13+
14+
class Foo<Bar: NSObject> {
15+
var foobar: Bar {
16+
fatalError()
17+
}
18+
19+
lazy var foo: () -> Void = {
20+
// TODO: improve diagnostic message
21+
_ = self.foobar + nil // expected-error {{'Foo<Bar>' requires that 'Bar' inherit from 'NSObject'}}
22+
}
23+
}

0 commit comments

Comments
 (0)