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-9897] '.Type' property on a type can't be escaped with backticks #52303

Open
AliSoftware opened this issue Feb 10, 2019 · 0 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@AliSoftware
Copy link
Contributor

AliSoftware commented Feb 10, 2019

Previous ID SR-9897
Radar None
Original Reporter @AliSoftware
Type Bug
Environment

Xcode 10.1, Swift 4.2

(Still true in Swift 5.0 early beta so far)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: e89a888a494ee4ac9e32ecde9fdbb1b6

duplicates:

  • SR-1072 Nested type named Type should be better dealt with

relates to:

  • SR-2172 Compiler mistreats static Type property

Issue Description:

Issue Description

If a type has a type property or subtype named "self", you can access it using backticks (MyClassOrEnum.`self`) to disambiguate and reference the property/subtype instead of the reserved keyword.

That behaviour is not true for subtypes named "Type" though: even when adding backticks around it (MyClassOrEnum.`Type`) when trying to access the subtype, the compiler will still access the meta-type (MyClassOrEnum.Type) instead of the inner subtype.

This is both a problem as we then have no way of referencing any subtype named `Type` but also because this behaviour is inconsistent with the rest of the reserved keywords like `self`.

Example code 1: subtypes named "self" & "Type"

public enum Constants {
  public enum `self` {
    public static let control = "Self-Control"
    public static let service = "Self-Service"
  }
  public enum `Type` {
    public static let safety = "Type-safety"
  }
}

print(Constants.self.control) // Type 'Constants' has no member 'control' (expected)
print(Constants.`self`.control) // This works fine
print(Constants.Type.safety) // Type 'Constants.Type' has no member 'safety' (expected)
print(Constants.`Type`.safety) // Type 'Constants.Type' has no member 'safety' (what?)

Example code 2: type properties named "self" & "Type"

public enum Constants {
  static let `self` = "me"
  static let `Type` = "kind"
}

// Trying to print "me"
print(Constants.self) // Compiles, print "Constants" (the name of the type itself)
print(Constants.`self`) // Works as expected, prints "me"

// Trying to print "kind":
print(Constants.Type) // expected error: "Expected member name or constructor call after type name". Fix-it: "use '.self' to reference the type object"
print(Constants.Type.self) // applying the fixit: prints "Constants.Type" (the name of the metatype), valid but not what we want
print(Constants.`Type`) // unexpectedly, same error as above (while we expected to escape Type and reference its value "kind")

// So… no way to print the value "kind" from Constants.`Type` then
@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant