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-8753] wrong error message is given when you attempt to mutate parameters inside a reduce block #51261

Closed
swift-ci opened this issue Sep 14, 2018 · 3 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 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8753
Radar rdar://problem/44476380
Original Reporter swiftHoney (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Swift 4.1.2

Xcode 9.4.1

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

md5: 2e445a28e7ba51855d170331b7ee7e6a

Issue Description:

As recorded on stackoverflow:

{code:none}
var nums = [1,2,3]
let emptyArray : [Int] = []
let sum1 = nums.reduce(emptyArray){ $0.append($1)}

would result in the following error:

{quote}Type of expression ambiguous without more context
{quote}

But that error is incorrect. 

If I wrote as: 

{code:none}
func append<T>(_ element: T, to array: [T]) -> [T] {
  let newArray = array.append(element)
  return newArray
}

Then the error would correctly come as:

Cannot use mutating member on immutable value: 'array' is a 'let' constant

@belkadan
Copy link
Contributor

Reproduces on master. cc @xedin

@xedin
Copy link
Member

xedin commented Sep 15, 2018

@swift-ci create

@hborla
Copy link
Member

hborla commented Feb 6, 2020

This is resolved in Swift 5.2. The compiler now reports two error messages:

error: cannot convert value of type '()' to closure result type '[Int]'
let sum1 = nums.reduce(emptyArray){ $0.append($1) }
                                    ~~~^~~~~~~~~~
error: cannot use mutating member on immutable value: '$0' is immutable
let sum1 = nums.reduce(emptyArray){ $0.append($1) }
                                    ~~ ^

Could you please verify using a recent 5.2 snapshot, or the latest Xcode beta? Thank you!

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants