Skip to content

Drop tracking: handle invalid assignments better #97031

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

Merged
merged 2 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
diag_expr_id: HirId,
) {
debug!("mutate {assignee_place:?}; diag_expr_id={diag_expr_id:?}");

if assignee_place.place.base == PlaceBase::Rvalue
&& assignee_place.place.projections.is_empty()
{
// Assigning to an Rvalue is illegal unless done through a dereference. We would have
// already gotten a type error, so we will just return here.
return;
}

// If the type being assigned needs dropped, then the mutation counts as a borrow
// since it is essentially doing `Drop::drop(&mut x); x = new_value;`.
if assignee_place.place.base_ty.needs_drop(self.tcx, self.param_env) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/async-await/issue-73741-type-err-drop-tracking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// edition:2018
// compile-flags: -Zdrop-tracking
// Regression test for issue #73741
// Ensures that we don't emit spurious errors when
// a type error ocurrs in an `async fn`

async fn weird() {
1 = 2; //~ ERROR invalid left-hand side

let mut loop_count = 0;
async {}.await
}

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/async-await/issue-73741-type-err-drop-tracking.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0070]: invalid left-hand side of assignment
--> $DIR/issue-73741-type-err-drop-tracking.rs:8:7
|
LL | 1 = 2;
| - ^
| |
| cannot assign to this expression

error: aborting due to previous error

For more information about this error, try `rustc --explain E0070`.