Skip to content

@objcMembers does not apply @objc to ObjC compatible enums or inner classes #63442

@mildm8nnered

Description

@mildm8nnered

Description

In a class declared with @objcMembers (and that inherits from NSObject, see #52325), inner classes and ObjectiveC compatible enums will not automatically behave as though declared with @objc

Steps to reproduce

Given the code

#!/usr/bin/env swift

import Foundation

// This WILL be visible to ObjC
@objcMembers
class Foo: NSObject {
    // This WILL be visible to ObjC
    var a: Int = 0

    // @objc is	redundant here - this could be a warning
    @objc var b: Int = 0

    // This will NOT be visible to ObjC
    class Foo2: NSObject {
        var x: Int = 0
    }

    // This will NOT be visible to ObjC. This means that inner classes effectively cannot use @objcMembers
    @objcMembers
    class Foo3: NSObject {
        var y: Int = 0
    }

    // This WILL be visible to ObjC
    @objc
    class Foo4: NSObject {
        var z: Int = 0
    }

    // This will NOT be visible to ObjC
    enum Bar: Int {
        case zero
    }

    // This WILL be visible to ObjC
    @objc enum Baz: Int {
        case zero
    }
}

swiftc -emit-objc-header-path header.h Example.swift

Will indicate that the various members are exposed in the ObjectiveC header as described in the comments.

Expected behavior

I would expect all of the above cases to appear in the ObjectiveC header, just as if they were declared with @objc

Ideally I would like a warning if @objc was redundant.

Environment

  • Swift compiler version info:
% swiftc --version
swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: arm64-apple-macosx12.0
  • Xcode version:
% xcodebuild -version
Xcode 14.2
Build version 14C18

Deployment target: MacOS 12.6.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    PrintAsClangArea → compiler: The PrintAsClang libraryattributesFeature: Declaration and type attributesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfobjective-c interopFeature: Interoperability with Objective-Cswift 5.7unexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions