# Emitting warning when creating instance of type conforming to protocol

**URL:** <https://forums.swift.org/t/emitting-warning-when-creating-instance-of-type-conforming-to-protocol/79298>\
**Category:** Using Swift\
**Tags:** diagnostics\
**Created:** [April 16, 2025, 10:54am UTC](https://forums.swift.org/t/emitting-warning-when-creating-instance-of-type-conforming-to-protocol/79298 "2025-04-16T10:54:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erithacus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erithacus/32/20377_2.png) [@erithacus](https://forums.swift.org/u/erithacus)\
**Post date:** [April 16, 2025, 10:54am UTC](https://forums.swift.org/t/emitting-warning-when-creating-instance-of-type-conforming-to-protocol/79298/1 "2025-04-16T10:54:48Z")

</div>

Hi all

I've been looking at whether it is possible to emit a warning into Xcode when instantiating a type that conforms to a specific protocol.

I can see that using SwiftSyntax, I could emit a warning when declaring the type by inspecting the inheritance syntax node. This is potentially better than nothing, but what I would really like to achieve is emitting a warning when instantiating something that conforms to a specific protocol.

For example:

```swift
protocol HandleWithCare { }

struct HereBeDragons: HandleWithCare { }

... 

// in some other library or target

let item = HereBeDragons() // ⚠️ warning displayed here in Xcode

```

I've done a bit of reading around this and have yet to identify a way to achieve what I'm after but it feels like it should be possible.

I'm not wedded to the idea of attaching the warning emitter via protocol conformance. If there's another way that achieves the same effect that would be good to explore.

Do any of you have any suggestions for approaches to take and things to investigate?

---

<div class="post-metadata">

**Author:** ![Genaro-Chris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genaro-chris/32/24997_2.png) [@Genaro-Chris](https://forums.swift.org/u/Genaro-Chris)\
**Post date:** [April 30, 2025, 9:18am UTC](https://forums.swift.org/t/emitting-warning-when-creating-instance-of-type-conforming-to-protocol/79298/2 "2025-04-30T09:18:54Z")

</div>

You could try [@\_spi](https://github.com/swiftlang/swift/blob/main/docs/ReferenceGuides/UnderscoredAttributes.md#_spispiname) but not sure it exactly what you are searching for 🤔

---

<div class="post-metadata">

**Author:** ![erithacus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erithacus/32/20377_2.png) [@erithacus](https://forums.swift.org/u/erithacus)\
**Post date:** [May 11, 2025, 1:42pm UTC](https://forums.swift.org/t/emitting-warning-when-creating-instance-of-type-conforming-to-protocol/79298/3 "2025-05-11T13:42:38Z")

</div>

Thanks for this. It is interesting; I've not used it before. It does part of what I want which is to force people to knowingly use something that's potentially problematic or has an unstable API.

It's good for the initial use but there's no on-going warning about using it for the consuming developer(s).

I suppose a custom SwiftLint rule or such like could deal with that.

Do you have any experience of using `@_spi` in production apps? I'm slightly concerned about App review not liking the underscore.

---

<div class="post-metadata">

**Author:** ![Genaro-Chris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genaro-chris/32/24997_2.png) [@Genaro-Chris](https://forums.swift.org/u/Genaro-Chris)\
**Post date:** [May 12, 2025, 4:34pm UTC](https://forums.swift.org/t/emitting-warning-when-creating-instance-of-type-conforming-to-protocol/79298/4 "2025-05-12T16:34:05Z")

</div>

For me none but I think the swift syntax package uses it
