Uploaded image for project: 'Swift'
  1. Swift
  2. SR-13022

Support magic literals as enum case raw value

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:

      Description

      Currently, the use of a magic literal (such as #file) as a raw value for an enum case:

      enum Foo: String {
        case bar = #file
      }
      

      triggers an error: use of '#file' literal as raw value for enum case is not supported.

      We should support use of a limited set of magic literals as raw values. The eligible literals would be #file, #filePath, #function, #line and #column since their raw value types can be used as the raw type for an enum:

      enum FooString: String {
        case bar = #file // okay, #file is String which matches the raw type
      }
      
      enum FooInt: Int {
        case bar = #line // okay, #line is Int which matches the raw type
      }
      

      #line and #column can be easily fetched from the SourceManager, and the same with #file and #filePath I believe. #function name can be fetched by moving getMagicFunctionName from SILGen into AST (as a method on DeclContext).

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              suyashsrijan Suyash Srijan
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: