Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-13022] Support magic literals as enum case raw value #55467

Open
theblixguy opened this issue Jun 16, 2020 · 2 comments
Open

[SR-13022] Support magic literals as enum case raw value #55467

theblixguy opened this issue Jun 16, 2020 · 2 comments
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis

Comments

@theblixguy
Copy link
Collaborator

Previous ID SR-13022
Radar None
Original Reporter @theblixguy
Type Improvement
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Improvement, TypeChecker
Assignee None
Priority Medium

md5: 89b49aa4c8519ddc9542ad6e2571af4e

relates to:

  • SR-12998 Raw Value of literal String #file breaks enum with String Protocol

Issue 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).

@swift-ci
Copy link
Collaborator

Comment by Jonathan Pappas (JIRA)

This is incredible!

Would it be appropriate to allow magic literals within error and warning directives as well?

#error(#file)
#warning(#file)
#sourceLocation(file: #file, line: #line)

@theblixguy
Copy link
Collaborator Author

I think so, it makes sense to me.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants