Skip to content

Commit 7e0e180

Browse files
committed
[Diagnostics] Fix requirement note to properly handle layout requirements
Layout requirement doesn't have a second type since it's always `AnyObject`, requirement failure note should handle that, otherwise in no assertion builds this would crash in diagnostic engine trying to emit the note. Resolves: rdar://79672230 (cherry picked from commit 2b58a75)
1 parent b51c0ea commit 7e0e180

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,16 +1979,16 @@ NOTE(wrapped_type_satisfies_requirement,none,
19791979
"wrapped type %0 satisfies this requirement; did you mean to unwrap?", (Type))
19801980
NOTE(candidate_types_conformance_requirement,none,
19811981
"candidate requires that %0 conform to %1 "
1982-
"(requirement specified as %2 == %3%4)",
1983-
(Type, Type, Type, Type, StringRef))
1982+
"(requirement specified as %2 : %3)",
1983+
(Type, Type, Type, Type))
19841984
NOTE(candidate_types_equal_requirement,none,
19851985
"candidate requires that the types %0 and %1 be equivalent "
1986-
"(requirement specified as %2 == %3%4)",
1987-
(Type, Type, Type, Type, StringRef))
1986+
"(requirement specified as %2 == %3)",
1987+
(Type, Type, Type, Type))
19881988
NOTE(candidate_types_inheritance_requirement,none,
19891989
"candidate requires that %1 inherit from %2 "
1990-
"(requirement specified as %2 : %3%4)",
1991-
(Type, Type, Type, Type, StringRef))
1990+
"(requirement specified as %2 : %3)",
1991+
(Type, Type, Type, Type))
19921992
NOTE(types_not_equal_requirement,none,
19931993
"requirement specified as %0 == %1%2", (Type, Type, StringRef))
19941994
ERROR(type_is_not_a_class,none,

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,14 @@ bool RequirementFailure::diagnoseAsNote() {
388388
const auto &req = getRequirement();
389389
const auto *reqDC = getRequirementDC();
390390

391+
// Layout requirement doesn't have a second type, let's always
392+
// `AnyObject`.
393+
auto requirementTy = req.getKind() == RequirementKind::Layout
394+
? getASTContext().getAnyObjectType()
395+
: req.getSecondType();
396+
391397
emitDiagnosticAt(reqDC->getAsDecl(), getDiagnosticAsNote(), getLHS(),
392-
getRHS(), req.getFirstType(), req.getSecondType(), "");
398+
getRHS(), req.getFirstType(), requirementTy);
393399
return true;
394400
}
395401

lib/Sema/CSDiagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class RequirementFailure : public FailureDiagnostic {
231231
using PathEltKind = ConstraintLocator::PathElementKind;
232232
using DiagOnDecl = Diag<DescriptiveDeclKind, DeclName, Type, Type>;
233233
using DiagInReference = Diag<DescriptiveDeclKind, DeclName, Type, Type, Type>;
234-
using DiagAsNote = Diag<Type, Type, Type, Type, StringRef>;
234+
using DiagAsNote = Diag<Type, Type, Type, Type>;
235235

236236
/// If this failure associated with one of the conditional requirements,
237237
/// this field would represent conformance where requirement comes from.

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ func f11<T : P2>(_ n: T, _ f: @escaping (T) -> T) {} // expected-note {{where '
12541254
f11(3, f4) // expected-error {{global function 'f11' requires that 'Int' conform to 'P2'}}
12551255

12561256
let f12: (Int) -> Void = { _ in } // expected-note {{candidate '(Int) -> Void' requires 1 argument, but 2 were provided}}
1257-
func f12<T : P2>(_ n: T, _ f: @escaping (T) -> T) {} // expected-note {{candidate requires that 'Int' conform to 'P2' (requirement specified as 'T' == 'P2')}}
1257+
func f12<T : P2>(_ n: T, _ f: @escaping (T) -> T) {} // expected-note {{candidate requires that 'Int' conform to 'P2' (requirement specified as 'T' : 'P2')}}
12581258
f12(3, f4)// expected-error {{no exact matches in call to global function 'f12'}}
12591259

12601260
// SR-12242

test/Constraints/overload.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,14 @@ func test_no_hole_propagation() {
246246
return arguments.reduce(0, +) // expected-error {{cannot convert value of type 'Int' to expected argument type 'String'}}
247247
}
248248
}
249+
250+
// rdar://79672230 - crash due to unsatisfied `: AnyObject` requirement
251+
func rdar79672230() {
252+
struct MyType {}
253+
254+
func test(_ representation: MyType) -> Bool {} // expected-note {{found candidate with type 'MyType'}}
255+
func test<T>(_ object: inout T) -> Bool where T : AnyObject {} // expected-note {{candidate requires that 'MyType' conform to 'AnyObject' (requirement specified as 'T' : 'AnyObject')}}
256+
257+
var t: MyType = MyType()
258+
test(&t) // expected-error {{no exact matches in call to local function 'test'}}
259+
}

test/decl/ext/protocol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ extension AnotherBazProtocol where BazValue: AnotherBazProtocol {} // ok, does n
197197
// Protocol extensions with additional requirements
198198
// ----------------------------------------------------------------------------
199199
extension P4 where Self.AssocP4 : P1 {
200-
// expected-note@-1 {{candidate requires that 'Int' conform to 'P1' (requirement specified as 'Self.AssocP4' == 'P1')}}
201-
// expected-note@-2 {{candidate requires that 'S4aHelper' conform to 'P1' (requirement specified as 'Self.AssocP4' == 'P1')}}
200+
// expected-note@-1 {{candidate requires that 'Int' conform to 'P1' (requirement specified as 'Self.AssocP4' : 'P1')}}
201+
// expected-note@-2 {{candidate requires that 'S4aHelper' conform to 'P1' (requirement specified as 'Self.AssocP4' : 'P1')}}
202202
func extP4a() {
203203
acceptsP1(reqP4a())
204204
}

0 commit comments

Comments
 (0)