Skip to content

Fix indexing crasher with implicit objcImpl inits #79207

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
Feb 11, 2025
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
2 changes: 2 additions & 0 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ class IterableDeclContext {
return LastDeclAndKind.getInt();
}

SourceRange getBraces() const;

bool hasUnparsedMembers() const;

void setDeserializedMembers(bool deserialized) { DeserializedMembers = deserialized; }
Expand Down
12 changes: 12 additions & 0 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,18 @@ ASTContext &IterableDeclContext::getASTContext() const {
return getDecl()->getASTContext();
}

SourceRange IterableDeclContext::getBraces() const {
switch (getIterableContextKind()) {
case IterableDeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(this)->getBraces();
break;

case IterableDeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(this)->getBraces();
break;
}
}

DeclRange IterableDeclContext::getCurrentMembersWithoutLoading() const {
return DeclRange(FirstDeclAndLazyMembers.getPointer(), nullptr);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,16 +818,16 @@ createDesignatedInitOverride(ClassDecl *classDecl,

// Create the initializer declaration, inheriting the name,
// failability, and throws from the superclass initializer.
auto implCtx = classDecl->getImplementationContext()->getAsGenericContext();
auto implCtx = classDecl->getImplementationContext();
auto ctor = new (ctx) ConstructorDecl(
superclassCtor->getName(), classDecl->getBraces().Start,
superclassCtor->getName(), implCtx->getBraces().Start,
superclassCtor->isFailable(),
/*FailabilityLoc=*/SourceLoc(),
/*Async=*/superclassCtor->hasAsync(),
/*AsyncLoc=*/SourceLoc(),
/*Throws=*/superclassCtor->hasThrows(),
/*ThrowsLoc=*/SourceLoc(), TypeLoc::withoutLoc(thrownType), bodyParams,
genericParams, implCtx,
genericParams, implCtx->getAsGenericContext(),
/*LifetimeDependentTypeRepr*/ nullptr);

ctor->setImplicit();
Expand Down
10 changes: 8 additions & 2 deletions test/Index/index_objc_impl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
// RUN: %empty-directory(%t/mods)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -emit-module -o %t/mods %t/ObjcImpl.swift -import-objc-header %t/objc_impl.h -disable-objc-attr-requires-foundation-module -target %target-stable-abi-triple
// We use `-index-store-path` to exercise a code path that used to crash; we
// aren't checking its output.
// RUN: %target-swift-frontend -emit-module -o %t/mods %t/ObjcImpl.swift -import-objc-header %t/objc_impl.h -target %target-stable-abi-triple -disable-objc-attr-requires-foundation-module -index-store-path %t/index-store
// RUN: %target-swift-ide-test -print-indexed-symbols -module-to-print ObjcImpl -source-filename none -I %t/mods -target %target-stable-abi-triple | %FileCheck %s

//--- objc_impl.h
@interface NSObject
- (instancetype)init;
@end

@interface ObjCClass : NSObject
Expand All @@ -20,5 +23,8 @@
// CHECK: class/Swift | ObjCClass | c:objc(cs)ObjCClass | Ref
@_objcImplementation public extension ObjCClass {
// CHECK: instance-property/Swift | someObjCDeclaredVar | c:@CM@ObjcImpl@@objc(cs)ObjCClass(py)someObjCDeclaredVar | Def
@objc var someObjCDeclaredVar: CInt
@objc var someObjCDeclaredVar: CInt = 1

// Implicitly synthesized `override init()`:
// CHECK: constructor/Swift | init() | c:@CM@ObjcImpl@@objc(cs)ObjCClass(im)init | Def
}