Skip to content

[5.5][Diagnostics] Fix requirement note to properly handle layout requirements #38137

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 1 commit into from
Jun 29, 2021
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
12 changes: 6 additions & 6 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1979,16 +1979,16 @@ NOTE(wrapped_type_satisfies_requirement,none,
"wrapped type %0 satisfies this requirement; did you mean to unwrap?", (Type))
NOTE(candidate_types_conformance_requirement,none,
"candidate requires that %0 conform to %1 "
"(requirement specified as %2 == %3%4)",
(Type, Type, Type, Type, StringRef))
"(requirement specified as %2 : %3)",
(Type, Type, Type, Type))
NOTE(candidate_types_equal_requirement,none,
"candidate requires that the types %0 and %1 be equivalent "
"(requirement specified as %2 == %3%4)",
(Type, Type, Type, Type, StringRef))
"(requirement specified as %2 == %3)",
(Type, Type, Type, Type))
NOTE(candidate_types_inheritance_requirement,none,
"candidate requires that %1 inherit from %2 "
"(requirement specified as %2 : %3%4)",
(Type, Type, Type, Type, StringRef))
"(requirement specified as %2 : %3)",
(Type, Type, Type, Type))
NOTE(types_not_equal_requirement,none,
"requirement specified as %0 == %1%2", (Type, Type, StringRef))
ERROR(type_is_not_a_class,none,
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,14 @@ bool RequirementFailure::diagnoseAsNote() {
const auto &req = getRequirement();
const auto *reqDC = getRequirementDC();

// Layout requirement doesn't have a second type, let's always
// `AnyObject`.
auto requirementTy = req.getKind() == RequirementKind::Layout
? getASTContext().getAnyObjectType()
: req.getSecondType();

emitDiagnosticAt(reqDC->getAsDecl(), getDiagnosticAsNote(), getLHS(),
getRHS(), req.getFirstType(), req.getSecondType(), "");
getRHS(), req.getFirstType(), requirementTy);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class RequirementFailure : public FailureDiagnostic {
using PathEltKind = ConstraintLocator::PathElementKind;
using DiagOnDecl = Diag<DescriptiveDeclKind, DeclName, Type, Type>;
using DiagInReference = Diag<DescriptiveDeclKind, DeclName, Type, Type, Type>;
using DiagAsNote = Diag<Type, Type, Type, Type, StringRef>;
using DiagAsNote = Diag<Type, Type, Type, Type>;

/// If this failure associated with one of the conditional requirements,
/// this field would represent conformance where requirement comes from.
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ func f11<T : P2>(_ n: T, _ f: @escaping (T) -> T) {} // expected-note {{where '
f11(3, f4) // expected-error {{global function 'f11' requires that 'Int' conform to 'P2'}}

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

// SR-12242
Expand Down
11 changes: 11 additions & 0 deletions test/Constraints/overload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,14 @@ func test_no_hole_propagation() {
return arguments.reduce(0, +) // expected-error {{cannot convert value of type 'Int' to expected argument type 'String'}}
}
}

// rdar://79672230 - crash due to unsatisfied `: AnyObject` requirement
func rdar79672230() {
struct MyType {}

func test(_ representation: MyType) -> Bool {} // expected-note {{found candidate with type 'MyType'}}
func test<T>(_ object: inout T) -> Bool where T : AnyObject {} // expected-note {{candidate requires that 'MyType' conform to 'AnyObject' (requirement specified as 'T' : 'AnyObject')}}

var t: MyType = MyType()
test(&t) // expected-error {{no exact matches in call to local function 'test'}}
}
4 changes: 2 additions & 2 deletions test/decl/ext/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ extension AnotherBazProtocol where BazValue: AnotherBazProtocol {} // ok, does n
// Protocol extensions with additional requirements
// ----------------------------------------------------------------------------
extension P4 where Self.AssocP4 : P1 {
// expected-note@-1 {{candidate requires that 'Int' conform to 'P1' (requirement specified as 'Self.AssocP4' == 'P1')}}
// expected-note@-2 {{candidate requires that 'S4aHelper' conform to 'P1' (requirement specified as 'Self.AssocP4' == 'P1')}}
// expected-note@-1 {{candidate requires that 'Int' conform to 'P1' (requirement specified as 'Self.AssocP4' : 'P1')}}
// expected-note@-2 {{candidate requires that 'S4aHelper' conform to 'P1' (requirement specified as 'Self.AssocP4' : 'P1')}}
func extP4a() {
acceptsP1(reqP4a())
}
Expand Down