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-11334] Misleading error message when a mutating getter is used in the definition of a getter #53735

Closed
swift-ci opened this issue Aug 20, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11334
Radar None
Original Reporter apaszke (JIRA User)
Type Bug
Status Resolved
Resolution Cannot Reproduce
Environment

Apple Swift version 5.1 (swiftlang-1100.0.257.2 clang-1100.0.31.3)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee apaszke (JIRA)
Priority Medium

md5: e1dfed439a8936e45650002009419c47

Issue Description:

Consider this piece of code

struct HasMutableGetter {
  subscript(_ k: Int) -> Int {
    mutating get { 0 }
  }
}

struct MyStruct {
  var d: HasMutableGetter!
  subscript(_ v: Int) -> Int {
    get { d[v] }
  }
}

Running this through swiftc will produces a highly confusing error message, complaining that d needs to be explicitly unwrapped, even though it has been force unwrapped at the definition:

error.swift:28:11: error: value of optional type 'HasMutableGetter?' must be unwrapped to refer to member 'subscript' of wrapped base type 'HasMutableGetter'
    get { d[v] }
          ^
error.swift:28:11: note: chain the optional using '?' to access member 'subscript' only for non-'nil' base values
    get { d[v] }
          ^
           ?
error.swift:28:11: note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
    get { d[v] }
          ^
           !

Only once d[v] is changed to d![v] (which seems redundant), the error message reveals the true cause of failure:

error.swift:28:12: error: cannot use mutating getter on immutable value: 'self' is immutable
    get { d![v] }
          ~^
error.swift:28:5: note: mark accessor 'mutating' to make 'self' mutable
    get { d![v] }
    ^
    mutating 
@belkadan
Copy link
Contributor

cc @xedin, @hborla

@xedin
Copy link
Member

xedin commented Aug 28, 2019

Looks like this issue has been recently solved on master and now doesn't report unrelated "force unwrap" error. Please use the latest master snapshot to verify and close this issue.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

3 participants