# SE-0367: Conditional compilation for attributes

**URL:** <https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380>\
**Category:** Proposal Reviews\
**Created:** [August 1, 2022, 8:15pm UTC](https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380 "2022-08-01T20:15:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [August 1, 2022, 8:15pm UTC](https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380/1 "2022-08-01T20:15:54Z")

</div>

Hi everyone. The review of [SE-0367, _Conditional compilation for attributes_](https://github.com/apple/swift-evolution/blob/main/proposals/0367-conditional-attributes.md), begins now and runs through August 15st, 2022.

Reviews are an important part of the Swift evolution process. All review feedback should either be on this forum thread or, if you would like to keep your feedback private, directly to the review manager by DM. When messaging the review manager directly, please keep the proposal link at the top of the message and the evolution identifier in the subject line.

## What goes into a review?

The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:

- What is your evaluation of the proposal?
- Is the problem being addressed significant enough to warrant a change to Swift?
- Does this proposal fit well with the feel and direction of Swift?
- If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
- How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

More information about the Swift evolution process is available at: [https://github.com/apple/swift-evolution/blob/main/process.md](https://github.com/apple/swift-evolution/blob/main/process.md)

Thank you for helping review this proposal.

Joe Groff  
Review Manager

---

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [August 1, 2022, 8:24pm UTC](https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380/2 "2022-08-01T20:24:55Z")

</div>

+1 on the proposal, it seems like a very natural solution to the problem.

I have one minor suggestion regarding the interaction with custom attributes:

> While the built-in attribute that enables the feature will be recognized by `hasAttribute` (e.g., `hasAttribute(propertyWrapper)` will evaluate `true` ), the custom attribute will not (e.g., `hasAttribute(MyWrapper)` will evaluate `false` ).

Would it make sense to explicitly diagnose attempts to use `hasAttribute(MyCustomAttribute)` when it is possible to do so? If there is a visible definition for the custom attribute with the name then the compiler could reject the `hasAttribute` condition to ensure that the developer understands that it will not work for property wrappers etc.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [August 1, 2022, 8:51pm UTC](https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380/3 "2022-08-01T20:51:46Z")

</div>

> [@tshortli](#):
>
> Would it make sense to explicitly diagnose attempts to use `hasAttribute(MyCustomAttribute)` when it is possible to do so?

Additionally, I wonder if—sorry, bikeshedding—we should name it `hasBuiltinAttribute(…)`: it would be self-documenting why such a query returns `false` for a custom attribute.

By contrast, it’s explainable why `hasAttribute` would return `false` in that scenario but still awkward because at face value it’s inaccurate—one actually _has_ the queried attribute.

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [August 2, 2022, 5:12am UTC](https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380/4 "2022-08-02T05:12:05Z")

</div>

> [@tshortli](#):
>
> Would it make sense to explicitly diagnose attempts to use `hasAttribute(MyCustomAttribute)` when it is possible to do so? If there is a visible definition for the custom attribute with the name then the compiler could reject the `hasAttribute` condition to ensure that the developer understands that it will not work for property wrappers etc.

Such a warning would be a bit of a mess to implement, because compiler conditionals like `hasAttribute` are evaluated early in the parser and are then thrown away. However, you'd need to retain that information somewhere so that a much later semantic phase capable of doing name lookup on `MyCustomAttribute` could do the check.

Doug

---

<div class="post-metadata">

**Author:** ![lorentey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorentey/32/1299_2.png) [@lorentey](https://forums.swift.org/u/lorentey)\
**Post date:** [August 8, 2022, 11:09pm UTC](https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380/5 "2022-08-08T23:09:08Z")

</div>

This proposal is most welcome -- conditional compilation for attributes fixes a long-standing papercut for maintainers of Swift libraries, and reduces the need for external code generation tools. `hasAttribute()` reduces the need to deal with error-prone and opaque version numbers, which will also be very useful.

While we will still need to carefully copy-and-paste the same set of complex conditional attribute incantations over a multitude of declarations, but at least we will no longer need to duplicate the declarations themselves. Huge thumbs up -- thank you very much for doing this work!

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [August 23, 2022, 8:11pm UTC](https://forums.swift.org/t/se-0367-conditional-compilation-for-attributes/59380/6 "2022-08-23T20:11:08Z")

</div>

Thanks all for participating in the review! As @Joe_Groff has announced, the review has concluded and SE-0367 has been [accepted](https://forums.swift.org/t/accepted-se-0367-conditional-compilation-for-attributes/59756).
