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

MIR-borrowck: immutable unique closure upvars can be mutated #46236

Merged
merged 4 commits into from
Dec 1, 2017
Merged

MIR-borrowck: immutable unique closure upvars can be mutated #46236

merged 4 commits into from
Dec 1, 2017

Conversation

davidtwco
Copy link
Member

@davidtwco davidtwco commented Nov 24, 2017

Fixes #46023 and #46160 (see this comment).

@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@davidtwco
Copy link
Member Author

davidtwco commented Nov 24, 2017

This isn't quite working yet and I'm not quite sure why. I think I've followed the instructions left on the issue correctly.

r? @nikomatsakis @arielb1

@rust-highfive rust-highfive assigned arielb1 and unassigned pnkfelix Nov 24, 2017
@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 24, 2017
@@ -544,8 +544,8 @@ impl<'tcx> LocalDecl<'tcx> {
pub struct UpvarDecl {
pub debug_name: Name,

/// If true, the capture is behind a reference.
pub by_ref: bool
/// If None then the capture is by-value.
Copy link
Contributor

@arielb1 arielb1 Nov 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename it to pub capture_mode: Option<BorrowKind>?


if let Some(field) = field_projection {
let decl = &self.mir.upvar_decls[field.index()];
if let Some(borrow_kind) = decl.by_ref {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if let Some(BorrowKind::Unique) = decl.by_ref {

@arielb1
Copy link
Contributor

arielb1 commented Nov 25, 2017

I think you also need to change check_if_reassignment_to_immutable_state to use the new is_mutable logic - it handles error reporting for unique-immutable assignments. Otherwise assignments will not quite work. I think that's why the test is broken.

@davidtwco
Copy link
Member Author

Pushed an alternate approach after speaking with @arielb1 on Gitter - it isn't working yet but I think I've implemented the mutability field in UpvarDecl as per our conversation.

};

if let Lvalue::Static(_) = lvalue {
self.report_assignment_to_static(context, (lvalue, span));
}

if let Some(mpi) = self.move_path_for_lvalue(lvalue) {
Copy link
Contributor

@arielb1 arielb1 Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need the move_path_closest_to change here. Also, for assignments you want to move_path_closest_to the original lvalue, not the result of is_mutable, in order to allow

fn main() {
    let x: (u32, u32);
    x.0 = 0;
    x.1 = 1;
}

@arielb1
Copy link
Contributor

arielb1 commented Nov 27, 2017

@davidtwco

BTW, if you're already at this, you could also fix #46160 by marking the closure argument (argument _1 of a closure) as mutable

@davidtwco
Copy link
Member Author

I've not done much testing of what I just pushed, but I've added work from all the instruction provided over the past few days. It includes a rebase onto current master which included #46022 which @arielb1 said I might need.

@bors
Copy link
Contributor

bors commented Nov 30, 2017

☔ The latest upstream changes (presumably #46041) made this pull request unmergeable. Please resolve the merge conflicts.

@davidtwco
Copy link
Member Author

Pushed a compiling version of the last changes and rebased ontop of the current master. Wasn't sure what to use for the new parameter to is_mutable.

@davidtwco davidtwco changed the title WIP: MIR-borrowck: immutable unique closure upvars can be mutated MIR-borrowck: immutable unique closure upvars can be mutated Nov 30, 2017
@arielb1
Copy link
Contributor

arielb1 commented Nov 30, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 30, 2017

📌 Commit c6b1ba5 has been approved by arielb1

@bors
Copy link
Contributor

bors commented Dec 1, 2017

⌛ Testing commit c6b1ba5 with merge e3ed212...

bors added a commit that referenced this pull request Dec 1, 2017
MIR-borrowck: immutable unique closure upvars can be mutated

Fixes #46023 and #46160 (see [this comment](#46236 (comment))).
@bors
Copy link
Contributor

bors commented Dec 1, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: arielb1
Pushing e3ed212 to master...

@eddyb
Copy link
Member

eddyb commented May 16, 2018

This should have been merged like this, UpvarDecl was "supposed" to be debuginfo-only 😞.
But I'm not sure where to put this information - presumably it belongs attached to the closure type.

@nikomatsakis
Copy link
Contributor

@eddyb

But I'm not sure where to put this information - presumably it belongs attached to the closure type

Hmm. Maybe, but I'm not sure. I guess it depends on how you view the mut information. I don't think of it as part of the type system, but rather a kind of lint that sits on top. But I suppose we do issue hard errors and so forth, so it's reasonable to view it as information that belongs in the closure type. It's sort of nice though that the "closure subst" is just types right now, and I'm not sure I want to change that. Another option would be to add some closure_upvar_info query that takes a closure def-id and gives further information, though — sort of like the existing free_vars calculation?

(On the other hand, maybe it's nice to have all the upvar information collected in one place...)

( <grumpy> I still think we should just drop the notion of declaring local variables as mut altogether. =) </grumpy> )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants