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-2757] Remove wrong Fix it
suggestion in closure
#45361
Comments
Comment by Matthew Spear (JIRA) Pretty new to open source contribution, have a forked and built copy of Swift - Would this be an easy bug to jump in and attempt to do, where would you recommend I start? |
Comment by Matthew Spear (JIRA) kkoval (JIRA User) ^^ I'd like to attempt this, just need some help getting started! |
matthewspear (JIRA User): The diagnostic in question is emitted here: Lines 3690 to 3691 in a7b027d
(You can find this by searching for the error message, which leads you to one of the lists of diagnostics:
As for how to fix this, you could probably read the headers for the PatternBindingDecl, which is the particular declaration being examined there (which represents the General tips for jumping in / debugging: set lots of breakpoints, read lots of header comments, run lots of whole-project searches, and make test cases as small as possible! Also, if using Xcode, you'll want to edit your scheme so that ⌘R passes the right command line args to your Swift compiler. Probably |
I've confirmed that this issue still occurs on apple/swift master. Do you mind if I try to fix it, matthewspear (JIRA User)? |
Comment by Matthew Spear (JIRA) Go for it @modocache, I had a look but still getting my head round how Swift works internally with AST - Will be interesting to look at how you fix it. I'm only just getting into open source and sure I can find another starter bug to try fix! Thanks |
I enjoy keeping notes as I attempt to solve Swift bugs, so here are some from my work here so far. (I'm confident I'll be able to fix this eventually, so only chime in if you feel like it.) First of all, this problem can be reduced to: var x = 1
let y = { [x] in
x += 1
} This still produces an incorrect note: 2757.swift:3:5: error: left side of mutating operator isn't mutable: 'x' is a 'let' constant
x += 1
~ ^
2757.swift:2:12: note: change 'let' to 'var' to make it mutable
let y = { [x] in
^
var Dumping the parse tree for this source file shows that capture list elements are stored as (source_file
(top_level_code_decl
(brace_stmt
(pattern_binding_decl
(pattern_named 'x')
(integer_literal_expr type='<null>' value=1))
))
(var_decl "x" type='<null type>' storage_kind=stored)
(top_level_code_decl
(brace_stmt
(pattern_binding_decl
(pattern_named 'y')
(capture_list type='<null>'
(var_decl "x" type='<null type>' let storage_kind=stored)
(pattern_binding_decl
(pattern_named implicit 'x')
(unresolved_decl_ref_expr type='<null>' name=x specialized=no) function_ref=unapplied)
(closure_expr type='<null>' discriminator=0 single-expression
(parameter_list)
(sequence_expr type='<null>'
(declref_expr type='<null>' decl=main.(file).top-level code.x@2757.swift:2:12 function_ref=unapplied specialized=yes)
(unresolved_decl_ref_expr type='<null>' name=+= specialized=no) function_ref=unapplied
(integer_literal_expr type='<null>' value=1)))))
))
(var_decl "y" type='<null type>' let storage_kind=stored)) Capture list elements that are not marked as The fact that they're stored as
Reading the source code in the functions included in the backtrace, I found that once the TypeChecker has determined that the code is syntactically invalid (
So, how to fix this? Well, it seems unlikely that we can change how capture list variables are stored... they've been built to be stored as let constant VarDecls, and I assume the person who wrote it that way had a good reason to do so. I wonder instead if the solution is to differentiate capture list constants from let constants in some way. Maybe an additional method on VarDecl: it already has I'll probably have time to work on this over the holidays, so expect updates soon. |
Alternatively, if there were a way to see the "parent" of the |
A couple things off the top of my head:
|
Also, >> I ran I don't know if you use a Mac for development, but personally I find it much easier to use Xcode and its GUI debugging tools than lldb from the command line. You can set up your scheme so that it passes |
Here's my first attempt: #6490 – In the pull request I mention why I couldn't get implicit variables, or walking up the AST, to work. |
The pull request was merged, so I think this is done! |
Attachment: Download
Additional Detail from JIRA
md5: e7bf69726f57ea44fa1808ea28890522
Issue Description:
Remove wrong Fix it suggestion from closures, when trying to mutate an immutable constant that was captures in closure capture list.
Example:
The text was updated successfully, but these errors were encountered: