Suppressing uninhabited enum warnings?

I have a macro that generates code that involves embedding a value in a case, but this can lead to a warning when the value is an uninhabited enum, like Never. Effectively, it produces this warning:

enum Bar {}
enum Foo {
  case bar(Bar)
func bar(_ bar: Bar) -> Foo {
  .bar(bar)  // ⚠️

:warning: Will never be executed

:information_source: 'bar' is of type 'Bar' which cannot be constructed because it is an enum with no cases

Is there a way to suppress this warning? It's not possible for the macro to know that Bar is uninhabited, so all we can do is generate the code in the abstract.

1 Like

I don't know if this solution is applicable to your use case, but I had a similar scenario and I managed to work around it by defining a protocol for each generated type:

@_marker protocol _Bar { }
enum Bar: _Bar {}

protocol _Foo {
    associatedtype Bar: _Bar
    static func bar(_ : Bar) -> Self
enum Foo: _Foo {
  case bar(_Bar)

func bar(_ bar: some _Bar) -> Foo {

You need the marker annotation for every uninhabited type, otherwise Foo cannot conform to _Foo.

I hope this helps!

Unfortunately, the Bar type could be anything and is not something the macro can extend, so I think I'd need an alternate solution.

AFAICT there isn't a way to suppress these, since Swift doesn't really seem to support granular control over the errors & warnings produced by the compiler. these diagnostics appear come from SILGen. it seems fairly intentional that any unreachable source code produce a warning, so if there were a way to avoid these, it would likely indicate a bug.

not a very satisfying answer to the issue at hand... it would seem best if the code generator could be aware of the 'inhabitedness' of the type to just conditionally avoid generation, but idk how realistic that change may be.