Skip to content

Commit 6fcb47d

Browse files
authored
Merge pull request #38111 from xedin/rdar-79672230
[Diagnostics] Fix requirement note to properly handle layout requirements
2 parents 6a50c87 + 2b58a75 commit 6fcb47d

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
@@ -1988,16 +1988,16 @@ NOTE(wrapped_type_satisfies_requirement,none,
19881988
"wrapped type %0 satisfies this requirement; did you mean to unwrap?", (Type))
19891989
NOTE(candidate_types_conformance_requirement,none,
19901990
"candidate requires that %0 conform to %1 "
1991-
"(requirement specified as %2 == %3%4)",
1992-
(Type, Type, Type, Type, StringRef))
1991+
"(requirement specified as %2 : %3)",
1992+
(Type, Type, Type, Type))
19931993
NOTE(candidate_types_equal_requirement,none,
19941994
"candidate requires that the types %0 and %1 be equivalent "
1995-
"(requirement specified as %2 == %3%4)",
1996-
(Type, Type, Type, Type, StringRef))
1995+
"(requirement specified as %2 == %3)",
1996+
(Type, Type, Type, Type))
19971997
NOTE(candidate_types_inheritance_requirement,none,
19981998
"candidate requires that %1 inherit from %2 "
1999-
"(requirement specified as %2 : %3%4)",
2000-
(Type, Type, Type, Type, StringRef))
1999+
"(requirement specified as %2 : %3)",
2000+
(Type, Type, Type, Type))
20012001
NOTE(types_not_equal_requirement,none,
20022002
"requirement specified as %0 == %1%2", (Type, Type, StringRef))
20032003
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
@@ -399,8 +399,14 @@ bool RequirementFailure::diagnoseAsNote() {
399399
const auto &req = getRequirement();
400400
const auto *reqDC = getRequirementDC();
401401

402+
// Layout requirement doesn't have a second type, let's always
403+
// `AnyObject`.
404+
auto requirementTy = req.getKind() == RequirementKind::Layout
405+
? getASTContext().getAnyObjectType()
406+
: req.getSecondType();
407+
402408
emitDiagnosticAt(reqDC->getAsDecl(), getDiagnosticAsNote(), getLHS(),
403-
getRHS(), req.getFirstType(), req.getSecondType(), "");
409+
getRHS(), req.getFirstType(), requirementTy);
404410
return true;
405411
}
406412

lib/Sema/CSDiagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class RequirementFailure : public FailureDiagnostic {
235235
using PathEltKind = ConstraintLocator::PathElementKind;
236236
using DiagOnDecl = Diag<DescriptiveDeclKind, DeclName, Type, Type>;
237237
using DiagInReference = Diag<DescriptiveDeclKind, DeclName, Type, Type, Type>;
238-
using DiagAsNote = Diag<Type, Type, Type, Type, StringRef>;
238+
using DiagAsNote = Diag<Type, Type, Type, Type>;
239239

240240
/// If this failure associated with one of the conditional requirements,
241241
/// 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
@@ -1262,7 +1262,7 @@ func f11<T : P2>(_ n: T, _ f: @escaping (T) -> T) {} // expected-note {{where '
12621262
f11(3, f4) // expected-error {{global function 'f11' requires that 'Int' conform to 'P2'}}
12631263

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

12681268
// 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)