# How to get access level of a type a macro is attached to?

**URL:** <https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876>\
**Category:** Macros\
**Created:** [September 23, 2024, 11:42am UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876 "2024-09-23T11:42:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dev42](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Dev42](https://forums.swift.org/u/Dev42)\
**Post date:** [September 23, 2024, 11:42am UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876/1 "2024-09-23T11:42:25Z")

</div>

Let's look at the following case where I want to use macros to extend an enum type as follows:

Source:

```swift
enum MyEnum {
    case intValue(Int)
    case stringValue(string: String)
}

```

Destination:

```swift
enum MyEnum {
    case intValue(Int)
    case stringValue(string: String)

    enum CaseLabel {
        case intValue, stringValue
    }

    var caseLabel: CaseLabel {
        switch self {
        case .intValue:
            .intValue
        case .stringValue:
            .stringValue
        }
    }
}

extension MyEnum: CaseLabeled {}

```

While `CaseLabeled` is a public protocol

```swift
public protocol CaseLabeled {
    associatedtype CaseLabel: Equatable
    var caseLabel: CaseLabel { get }
}

```

I have implemented this with a `member` and an `extension` macro.

If `MyEnum` is public, the `CaseLabel` and `caseLabel` declarations must also be public due to the public protocol conformance:

```swift
public enum MyEnum {
    case intValue(Int)
    case stringValue(string: String)

    public enum CaseLabel {
        case intValue, stringValue
    }

    public var caseLabel: CaseLabel {
        switch self {
        case .intValue:
            .intValue
        case .stringValue:
            .stringValue
        }
    }
}

```

What I can do in the `MemberMacro` implementation is to retrieve the modifiers of the enum to find out if `CaseLabel` and `caseLabel` need to be made public.

So sth. like

```swift
// 'declaration' is an EnumDeclSyntax
let isPublicEnum = declaration.modifiers.contains { modifier in
    modifier.name.text == "public"
}

```

But this only works if the developer doesn't put the enum declaration into a public extension like this:

```swift
public extension SomeType {
    @CaseLabeled
    enum MyEnum {
        case intValue(Int)
        case stringValue(string: String)
    }
}

```

This results in `MyEnum` also being `public`. But in this case I do not have a corresponding modifier in the macro code at the node, as it is present on the enclosing element.  
As a result, `CaseLabel` and `caseLabel` on the expanded enum do not have the correct access level.

_ **So the question is:** _  
How do I get the **effective** access level of the type to be extended by the macro?

It doesn't seem like you can get information about the parent node hierarchy in a macro.

Of course, you can ask the developers to make sure that they always define the access level at the enum itself when using the macro, but that doesn't seem like a good solution to me.

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [September 23, 2024, 11:48am UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876/2 "2024-09-23T11:48:51Z")

</div>

> [@Dev42](#):
>
> How do I get the **effective** access level of the type to be extended by the macro?

Macros operate on raw syntax and not on AST nodes annotated with inferred types or access levels.

This semantic information is available after macro's output is produced. What you're asking for is not available at the early compilation stage when macros are still evaluated.

---

<div class="post-metadata">

**Author:** ![Dev42](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Dev42](https://forums.swift.org/u/Dev42)\
**Post date:** [September 23, 2024, 12:16pm UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876/3 "2024-09-23T12:16:38Z")

</div>

Let me perhaps rephrase my question:  
How would you solve the problem described?

Via a macro parameter that the developer can set and that sets the appropriate access level in the expanded code?

---

<div class="post-metadata">

**Author:** ![AlexisQapa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexisqapa/32/7159_2.png) [@AlexisQapa](https://forums.swift.org/u/AlexisQapa)\
**Post date:** [September 23, 2024, 12:17pm UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876/4 "2024-09-23T12:17:56Z")

</div>

I think you can always output public, those won't be visible anyway if the type visibility is less than public

---

<div class="post-metadata">

**Author:** ![Dev42](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Dev42](https://forums.swift.org/u/Dev42)\
**Post date:** [September 23, 2024, 2:57pm UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876/5 "2024-09-23T14:57:11Z")

</div>

This actually seems to be a simple and effective option. I would have expected the compiler to at least issue a warning if you make a public declaration on a private type. But that does not seem to be the case.

I think that's a solution I can live with for the time being. Thank you very much for that!

In the meantime, I have also found out that work is being done to make the parent context available to macros:

> <https://github.com/swiftlang/swift-syntax/pull/1554>
>
> The lexical context of a particular macro expansion involves all of the
> enclosi…ng contexts, including functions, types and extensions,
> properties and subscripts, and so on. The lexical context stack can be
> used to, for example, determine which type a macro was expanded
> within, or gather the parameters of the enclosing function for an
> assertion, etc.

> <https://github.com/swiftlang/swift/pull/71362/files>

However, I have not yet taken a closer look at the new `lexicalContext`.

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [September 23, 2024, 7:48pm UTC](https://forums.swift.org/t/how-to-get-access-level-of-a-type-a-macro-is-attached-to/74876/6 "2024-09-23T19:48:34Z")

</div>

It's a deliberate design decision that access levels of members inside of a type are clamped to the types access level. This is to reduce churn when increasing the visibility of a type.
