swiftlang / swiftlang/swift-java
Compilation error for generic NSObject: Required init
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 1.2k
- Forks
- 123
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 16
Description
Commit hash: 0dd772b8fcae9efd5bda701f1a0be95f9deb2836
Context:
While testing swiftly's support of existing Swift code, noticed that a code using generic NSObject (or NSObject subclass) is not compiling. The same code is compiling just fine in Xcode, is it because Foundation library in Xcode treats NSObject as having a required init?
import Foundation
protocol Test: NSObject /* or an NSObject subclass */ { }
func test<T: Test>() -> T {
return T()
}
Results in the following compilation error:
.../Sample.swift:21:12: error: constructing an object of class type 'T' with a metatype value must use a 'required' initializer
19 |
20 | func test<T: Test>() -> T {
21 | return T()
| `- error: constructing an object of class type 'T' with a metatype value must use a 'required' initializer
22 | }
23 |
Foundation.NSObject.init:2:8: note: selected non-required initializer 'init()'
1 | class NSObject {
2 | public init()}
| `- note: selected non-required initializer 'init()'
Environment: Swift 6.3.3 on macOS.
$ swiftly run swift --version
Apple Swift version 6.3.3 (swift-6.3.3-RELEASE)
Target: arm64-apple-macosx26.0
$ uname -a
Darwin it-C01241 25.6.0 Darwin Kernel Version 25.6.0: Sat Jul 11 15:26:21 PDT 2026; root:xnu-12377.161.13~4/RELEASE_ARM64_T6000 arm64
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided generic NSObject reproducer and run it using the recorded Swift 6.3.3 environment. Compare its behavior with Xcode, focusing on why Foundation.NSObject.init is treated as non-required. Done means the reproducer compiles consistently with the expected Xcode behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100