[clang] [clang] [ObjC] Warn that using parent direct initializer could leave child instance incomplete (PR #236951)

Daniel Rodríguez Troitiño via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 17:09:39 PST 2026


================
@@ -2959,6 +2959,54 @@ static bool isMethodDeclaredInRootProtocol(Sema &S, const ObjCMethodDecl *M) {
   return false;
 }
 
+/// Whether \p M is a designated initializer the way Swift imports it: marked
+/// as one, or its class marks no designated initializers at all.
+static bool isDesignatedInitializer(const ObjCMethodDecl *M) {
+  if (M->getMethodFamily() != OMF_init)
+    return false;
+  const ObjCInterfaceDecl *Class = M->getClassInterface();
+  if (Class)
+    Class = Class->getDefinition();
+  return Class && (!Class->hasDesignatedInitializers() ||
+                   M->isDesignatedInitializerForTheInterface());
+}
+
+/// Warn when a subclass instance is initialized with its superclass's direct
+/// designated initializer, e.g. [[Child alloc] initWithParent:1]. The subclass
+/// can't override a direct method, so its own initialization never runs.
+static void checkDirectInitOfSubclass(Sema &S, Expr *Receiver,
+                                      QualType ReceiverType,
+                                      const ObjCMethodDecl *Method,
+                                      SourceLocation SelLoc,
+                                      SourceRange RecRange) {
+  if (!isDesignatedInitializer(Method))
+    return;
+
+  // Use the receiver's static type such that a cast to the superclass
+  // silences the warning.
+  const auto *OPT = ReceiverType->getAsObjCInterfacePointerType();
+  if (!OPT)
+    return;
+  const ObjCInterfaceDecl *Class = Method->getClassInterface();
+  const ObjCInterfaceDecl *Subclass = OPT->getInterfaceDecl();
+  if (!Subclass || declaresSameEntity(Class, Subclass) ||
+      !Class->isSuperClassOf(Subclass))
+    return;
+
+  // A designated initializer of the subclass has to chain up through self,
+  // since messaging super with a direct method is an error.
+  if (S.ObjC().isSelfExpr(Receiver)) {
+    const ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
+    if (CurMethod && isDesignatedInitializer(CurMethod))
+      return;
+  }
----------------
drodriguez wrote:

I might misunderstand the comment/code, but if you have three levels of inheritance, doesn't this leave the door open for skipping the middle one by calling through `self` the designated initializer of the super-super-type?

```
@interface Level1 : NSObject
- (instancetype)initLevel1 NS_DESIGNATED_INITIALIZER;
@end
@interface Level2 : Level1
- (instancetype)initLevel2 NS_DESIGNATED_INITIALIZER;
@end
@interface Level3 : Level2
- (instancetype)initLevel3 NS_DESIGNATED_INITIALIZER;
@end
@implementation Level3
- (instancetype)initLevel3 {
  if ((self = [self initLevel1])) { /* initialize Level3, with Level1 initialized?, but not Level2? */ } return self;
}
@end
```

My problem with the comment is that it goes against what the good practices with initializers should do. From https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Initialization/Initialization.html under epigraph "Implementing an Initializer": "There are several critical rules to follow when implementing an init... method that serves as a class’s sole initializer or, if there are multiple initializers, its designated initializer [...] Always invoke the superclass (super) initializer first."

So what we are asking here goes against the good practices that have been in place for quite some time.

Not sure if "since messaging super with a direct method is an error" is part of the Swift parts that landed recently, or has always been there since Obj-C Direct was implemented, but it feels that this might have been a hole. A subtype should be able to (at least) call `super` when overriding a method, and from the good practices rules, allow a designated initializer to call its (immediate) superclass designated initializer. At the same time, if a subclass inherits a direct designated initializer, but has a different designated initializer, maybe it has to force the user to provide an overriden initializer which calls its designated one.

https://github.com/llvm/llvm-project/pull/236951


More information about the cfe-commits mailing list