Skip to content

Commit b2007cf

Browse files
author
Aniket Ray
committed
[AST] #SR-13906 (Improvement)
Updated Error message for types conforming to AnyObject or its Composition
1 parent a6b8552 commit b2007cf

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,9 +2633,9 @@ WARNING(anyobject_class_inheritance_deprecated,none,
26332633
ERROR(multiple_inheritance,none,
26342634
"multiple inheritance from classes %0 and %1", (Type, Type))
26352635
ERROR(inheritance_from_non_protocol_or_class,none,
2636-
"inheritance from non-protocol, non-class type %0", (Type))
2636+
"%0 type cannot inherit from non-protocol, non-class type %1", (DescriptiveDeclKind, Type))
26372637
ERROR(inheritance_from_non_protocol,none,
2638-
"inheritance from non-protocol type %0", (Type))
2638+
"%0 type cannot inherit from non-protocol type %1", (DescriptiveDeclKind, Type))
26392639
ERROR(superclass_not_first,none,
26402640
"superclass %0 must appear first in the inheritance clause", (Type))
26412641
ERROR(superclass_not_open,none,
@@ -2662,6 +2662,11 @@ ERROR(inheritance_from_cf_class,none,
26622662
ERROR(inheritance_from_objc_runtime_visible_class,none,
26632663
"cannot inherit from class %0 because it is only visible via the "
26642664
"Objective-C runtime", (Identifier))
2665+
ERROR(composition_of_protocol_and_anyobject,none,
2666+
"non-protocol type %0 cannot conform to composition type %1; "
2667+
"%0 must be a protocol type",
2668+
(Identifier, Type))
2669+
26652670

26662671
// Enums
26672672
ERROR(enum_case_access,none,

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,19 @@ static void checkInheritanceClause(
235235
// AnyObject is not allowed except on protocols.
236236
if (layout.hasExplicitAnyObject &&
237237
!isa<ProtocolDecl>(decl)) {
238-
decl->diagnose(canHaveSuperclass
239-
? diag::inheritance_from_non_protocol_or_class
240-
: diag::inheritance_from_non_protocol,
241-
inheritedTy);
238+
// Check if the inherited type is exclusively AnyObject
239+
// If not, we can be sure of the composition of Protocol(s) with
240+
// AnyObject
241+
if (!layout.isAnyObject()) {
242+
decl->diagnose(diag::composition_of_protocol_and_anyobject,
243+
typeDecl->getName(), inheritedTy);
244+
} else {
245+
decl->diagnose(canHaveSuperclass
246+
? diag::inheritance_from_non_protocol_or_class
247+
: diag::inheritance_from_non_protocol,
248+
decl->getDescriptiveKind(), inheritedTy);
249+
}
250+
242251
continue;
243252
}
244253

@@ -342,6 +351,7 @@ static void checkInheritanceClause(
342351
decl->diagnose(canHaveSuperclass
343352
? diag::inheritance_from_non_protocol_or_class
344353
: diag::inheritance_from_non_protocol,
354+
decl->getDescriptiveKind(),
345355
inheritedTy);
346356
// FIXME: Note pointing to the declaration 'inheritedTy' references?
347357
}

0 commit comments

Comments
 (0)