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

Improve diagnostic for read-only properties



    • Type: Improvement
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:


      If you write some code like

      protocol P {
        let x : Int // error: immutable property requirement must be declared as 'var' with a '{ get }' specifier

      This diagnostic is not quite accurate. There is 1 of 2 things the user is trying to do:

      1. They really want an immutable property. This is impossible given current restrictions, because `let` implies that the property is a stored property (it is not possible today to determine if the computation is pure, if computed `let` properties were to be allowed) and protocols cannot enforce whether the witnesses are stored or computed.
      2. They want a read-only property. This can be solved with `var { get }`.

      The diagnostic is saying that if you want to get 1., use the solution for 2., which is not quite right. Instead, we should be clearer about potential things they want and what the solutions are. At the very least, mixing those two things up doesn’t feel right.

      We should change the diagnostic to something like:

      protocol P {
        let x : Int // protocols cannot require properties to be immutable; declare read-only properties by using 'var' with a '{ get }' specifier

      You can grep the codebase to find where the error text is and then replace it with the suggested text (or if you have a better idea, feel free to suggest that ).

      Please make sure that you update any test cases or add one if we are missing a test case for this diagnostic.

      You can @ me (varungandhi-apple) on GitHub for review.




            hassaneldesouky Hassan ElDesouky
            theindigamer Varun Gandhi
            0 Vote for this issue
            3 Start watching this issue