Skip to content

Commit 0d95a84

Browse files
committed
[Diagnostics] Improve the error message for when a type fails to satisfy
`AnyObject` layout requirement.
1 parent ec864fc commit 0d95a84

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,12 +1716,18 @@ ERROR(type_does_not_conform_owner,none,
17161716
ERROR(type_does_not_conform_in_decl_ref,none,
17171717
"referencing %0 %1 on %2 requires that %3 conform to %4",
17181718
(DescriptiveDeclKind, DeclName, Type, Type, Type))
1719+
ERROR(type_does_not_conform_anyobject_in_decl_ref,none,
1720+
"referencing %0 %1 on %2 requires that %3 be a class type",
1721+
(DescriptiveDeclKind, DeclName, Type, Type, Type))
17191722
ERROR(type_does_not_conform_decl_owner,none,
17201723
"%0 %1 requires that %2 conform to %3",
17211724
(DescriptiveDeclKind, DeclName, Type, Type))
1722-
ERROR(type_does_not_conform_in_opaque_return,none,
1723-
"return type of %0 %1 requires that %2 conform to %3",
1725+
ERROR(type_does_not_conform_anyobject_decl_owner,none,
1726+
"%0 %1 requires that %2 be a class type",
17241727
(DescriptiveDeclKind, DeclName, Type, Type))
1728+
ERROR(type_does_not_conform_in_opaque_return,none,
1729+
"return type of %0 %1 requires that %2 %select{conform to %3|be a class type}4",
1730+
(DescriptiveDeclKind, DeclName, Type, Type, bool))
17251731
ERROR(types_not_equal_decl,none,
17261732
"%0 %1 requires the types %2 and %3 be equivalent",
17271733
(DescriptiveDeclKind, DeclName, Type, Type))
@@ -1741,7 +1747,7 @@ NOTE(where_requirement_failure_both_subst,none,
17411747
NOTE(requirement_implied_by_conditional_conformance,none,
17421748
"requirement from conditional conformance of %0 to %1", (Type, Type))
17431749
NOTE(wrapped_type_satisfies_requirement,none,
1744-
"wrapped type %0 satisfies this requirement", (Type))
1750+
"wrapped type %0 satisfies this requirement; did you mean to unwrap?", (Type))
17451751
NOTE(candidate_types_conformance_requirement,none,
17461752
"candidate requires that %0 conform to %1 "
17471753
"(requirement specified as %2 == %3%4)",

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ bool RequirementFailure::diagnoseAsError() {
478478
auto *namingDecl = OTD->getNamingDecl();
479479
emitDiagnostic(
480480
anchor->getLoc(), diag::type_does_not_conform_in_opaque_return,
481-
namingDecl->getDescriptiveKind(), namingDecl->getFullName(), lhs, rhs);
481+
namingDecl->getDescriptiveKind(), namingDecl->getFullName(), lhs, rhs,
482+
rhs->isAnyObject());
482483

483484
if (auto *repr = namingDecl->getOpaqueResultTypeRepr()) {
484485
emitDiagnostic(repr->getLoc(), diag::opaque_return_type_declared_here)

lib/Sema/CSDiagnostics.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,15 @@ class MissingConformanceFailure final : public RequirementFailure {
434434
bool diagnoseAsAmbiguousOperatorRef();
435435

436436
DiagOnDecl getDiagnosticOnDecl() const override {
437-
return diag::type_does_not_conform_decl_owner;
437+
return (getRequirement().getKind() == RequirementKind::Layout ?
438+
diag::type_does_not_conform_anyobject_decl_owner :
439+
diag::type_does_not_conform_decl_owner);
438440
}
439441

440442
DiagInReference getDiagnosticInRereference() const override {
441-
return diag::type_does_not_conform_in_decl_ref;
443+
return (getRequirement().getKind() == RequirementKind::Layout ?
444+
diag::type_does_not_conform_anyobject_in_decl_ref :
445+
diag::type_does_not_conform_in_decl_ref);
442446
}
443447

444448
DiagAsNote getDiagnosticAsNote() const override {

test/APINotes/versioned-objc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ProtoWithVersionedUnavailableMemberImpl: ProtoWithVersionedUnavailableMemb
1616
func testNonGeneric() {
1717
// CHECK-DIAGS-4:[[@LINE+1]]:{{[0-9]+}}: error: cannot convert value of type 'Any' to specified type 'Int'
1818
let _: Int = NewlyGenericSub.defaultElement()
19-
// CHECK-DIAGS-5:[[@LINE-1]]:{{[0-9]+}}: error: generic class 'NewlyGenericSub' requires that 'Int' conform to 'AnyObject'
19+
// CHECK-DIAGS-5:[[@LINE-1]]:{{[0-9]+}}: error: generic class 'NewlyGenericSub' requires that 'Int' be a class type
2020

2121
// CHECK-DIAGS-4:[[@LINE+1]]:{{[0-9]+}}: error: cannot specialize non-generic type 'NewlyGenericSub'
2222
let _: Int = NewlyGenericSub<Base>.defaultElement()

test/Constraints/closures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ struct S<T> {
281281

282282
func subscribe<Object: AnyObject>(object: Object?, method: (Object, T) -> ()) where Object: Hashable {
283283
let wrappedMethod = { (object: AnyObject, value: T) in }
284-
// expected-error @+2 {{instance method 'append(value:forKey:)' requires that 'Object?' conform to 'AnyObject'}}
284+
// expected-error @+2 {{instance method 'append(value:forKey:)' requires that 'Object?' be a class type}}
285285
// expected-note @+1 {{wrapped type 'Object' satisfies this requirement}}
286286
cs.forEach { $0.w.append(value: wrappedMethod, forKey: object) }
287287
}
@@ -295,7 +295,7 @@ func simplified1069() {
295295
// expected-note@-1 {{where 'T' = 'Optional<C>'}}
296296

297297
func f(_ a: C?, _ b: C?, _ c: C) {
298-
genericallyNonOptional(a, b, c) // expected-error {{instance method 'genericallyNonOptional' requires that 'Optional<C>' conform to 'AnyObject'}}
298+
genericallyNonOptional(a, b, c) // expected-error {{instance method 'genericallyNonOptional' requires that 'Optional<C>' be a class type}}
299299
// expected-note @-1 {{wrapped type 'C' satisfies this requirement}}
300300
}
301301
}

test/decl/var/property_wrappers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ extension SR_11288_P4 where Self: AnyObject { // expected-note 2 {{where 'Self'
15871587

15881588
struct SR_11288_S4: SR_11288_P4 {
15891589
// FIXME: We shouldn't diagnose the arg-to-param mismatch (rdar://problem/56345248)
1590-
@SR_11288_Wrapper4 var answer = 42 // expected-error 2 {{referencing type alias 'SR_11288_Wrapper4' on 'SR_11288_P4' requires that 'SR_11288_S4' conform to 'AnyObject'}}
1590+
@SR_11288_Wrapper4 var answer = 42 // expected-error 2 {{referencing type alias 'SR_11288_Wrapper4' on 'SR_11288_P4' requires that 'SR_11288_S4' be a class type}}
15911591
// expected-error @-1 {{cannot convert value of type '<<error type>>' to expected argument type 'Int'}}
15921592
}
15931593

0 commit comments

Comments
 (0)