Synthesized Code and Source Locations

Hi all,

I've been looking into SR-9094 recently, where an implicitly RawRepresentable enum will cause a compiler error if one of its cases is potentially unavailable. This happens because the synthesized init(rawValue:) will have a switch case which unconditionally returns the potentially unavailable enum case.

I've been experimenting with a solution to this which inserts matching #available checks in the initializer as needed, but have run into some roadblocks related to the fact that synthesized AST nodes don't have valid source locations. More specifically, I can't construct the proper TypeRefinementContexts for the added nodes. Additionally, the lack of source locations makes existing diagnostics confusing at times. For example:

   enum Test: Int {
       case one
       @available(macOS, introduced: 10.20) case two
       case three
   }

Reports the following error:

<unknown>:0: error: 'two' is only available on OS X 10.20 or newer

which is likely confusing, as the programmer never attempted to use "two" directly.

While looking into these issues I also came across this post, which seems to further justify somehow mapping synthesized code to a location in the source file.

Apologies for the wall of text, but I guess my question is, does assigning valid locations to synthesized code seem like an appropriate long-term solution to these types of problems? I'm still new to compiler development and want to make sure I'm not overthinking this, especially since it seems like it would be a nontrivial effort.

Thanks!

We've recently silenced analogous warnings here by the way. Synthesized code must not trigger errors or expose any other diagnostics to the user as in your example, so it's OK to have invalid locations in that aspect. From my experience, invalid locations aren't a problem if you can guarantee the compiler or tools won't attempt to use them, i.e. the implicit source will not be involved in highlighting, code completion, diagnostics etc. – you should be fine passing an invalid one. I'm not entirely sure that holds generally though. I'll leave any details to the core team.

Terms of Service

Privacy Policy

Cookie Policy