From 57f10c791119606e1813b8adefd06a93471147d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 9 Oct 2018 15:53:37 -0700 Subject: [PATCH] Point to variable in `asm!` macro when failing borrowck --- src/librustc/hir/lowering.rs | 1 + src/librustc/hir/mod.rs | 1 + src/librustc/ich/impls_hir.rs | 3 ++- src/librustc/middle/expr_use_visitor.rs | 23 +++++++++++-------- src/librustc_mir/borrow_check/mod.rs | 6 ++--- src/test/ui/asm/asm-out-assign-imm.nll.stderr | 4 ++-- src/test/ui/asm/asm-out-assign-imm.stderr | 4 ++-- .../ui/borrowck/borrowck-asm.ast.nll.stderr | 16 ++++++------- src/test/ui/borrowck/borrowck-asm.ast.stderr | 16 ++++++------- src/test/ui/borrowck/borrowck-asm.mir.stderr | 16 ++++++------- 10 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 7309358091056..4d51126621d7d 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -3953,6 +3953,7 @@ impl<'a> LoweringContext<'a> { constraint: out.constraint.clone(), is_rw: out.is_rw, is_indirect: out.is_indirect, + span: out.expr.span, }) .collect(), asm: asm.asm.clone(), diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index c57c26434e32a..1a97c678ef1c0 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -1812,6 +1812,7 @@ pub struct InlineAsmOutput { pub constraint: Symbol, pub is_rw: bool, pub is_indirect: bool, + pub span: Span, } #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index da9604702dfa3..9f4ac77fa1fe1 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -983,7 +983,8 @@ impl<'a> ToStableHashKey> for hir::BodyId { impl_stable_hash_for!(struct hir::InlineAsmOutput { constraint, is_rw, - is_indirect + is_indirect, + span }); impl_stable_hash_for!(struct hir::GlobalAsm { diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index d6b43ffe6da62..7e9b26bbf729c 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -364,11 +364,12 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { } fn mutate_expr(&mut self, + span: Span, assignment_expr: &hir::Expr, expr: &hir::Expr, mode: MutateMode) { let cmt = return_if_err!(self.mc.cat_expr(expr)); - self.delegate.mutate(assignment_expr.id, assignment_expr.span, &cmt, mode); + self.delegate.mutate(assignment_expr.id, span, &cmt, mode); self.walk_expr(expr); } @@ -472,12 +473,16 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { if o.is_indirect { self.consume_expr(output); } else { - self.mutate_expr(expr, output, - if o.is_rw { - MutateMode::WriteAndRead - } else { - MutateMode::JustWrite - }); + self.mutate_expr( + output.span, + expr, + output, + if o.is_rw { + MutateMode::WriteAndRead + } else { + MutateMode::JustWrite + }, + ); } } self.consume_exprs(inputs); @@ -515,7 +520,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { } hir::ExprKind::Assign(ref lhs, ref rhs) => { - self.mutate_expr(expr, &lhs, MutateMode::JustWrite); + self.mutate_expr(expr.span, expr, &lhs, MutateMode::JustWrite); self.consume_expr(&rhs); } @@ -527,7 +532,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { if self.mc.tables.is_method_call(expr) { self.consume_expr(lhs); } else { - self.mutate_expr(expr, &lhs, MutateMode::WriteAndRead); + self.mutate_expr(expr.span, expr, &lhs, MutateMode::WriteAndRead); } self.consume_expr(&rhs); } diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 0943b36440aa6..9cbaf35acd33f 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -544,7 +544,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx // be encoeded through MIR place derefs instead. self.access_place( context, - (output, span), + (output, o.span), (Deep, Read(ReadKind::Copy)), LocalMutationIsAllowed::No, flow_state, @@ -552,13 +552,13 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx self.check_if_path_or_subpath_is_moved( context, InitializationRequiringAction::Use, - (output, span), + (output, o.span), flow_state, ); } else { self.mutate_place( context, - (output, span), + (output, o.span), if o.is_rw { Deep } else { Shallow(None) }, if o.is_rw { WriteAndRead } else { JustWrite }, flow_state, diff --git a/src/test/ui/asm/asm-out-assign-imm.nll.stderr b/src/test/ui/asm/asm-out-assign-imm.nll.stderr index 7fefb6672c76b..40a36dd895f77 100644 --- a/src/test/ui/asm/asm-out-assign-imm.nll.stderr +++ b/src/test/ui/asm/asm-out-assign-imm.nll.stderr @@ -1,5 +1,5 @@ error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/asm-out-assign-imm.rs:34:9 + --> $DIR/asm-out-assign-imm.rs:34:34 | LL | let x: isize; | - help: make this binding mutable: `mut x` @@ -7,7 +7,7 @@ LL | x = 1; | ----- first assignment to `x` ... LL | asm!("mov $1, $0" : "=r"(x) : "r"(5)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error: aborting due to previous error diff --git a/src/test/ui/asm/asm-out-assign-imm.stderr b/src/test/ui/asm/asm-out-assign-imm.stderr index 83cb8092e16a2..51933cac39692 100644 --- a/src/test/ui/asm/asm-out-assign-imm.stderr +++ b/src/test/ui/asm/asm-out-assign-imm.stderr @@ -1,11 +1,11 @@ error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/asm-out-assign-imm.rs:34:9 + --> $DIR/asm-out-assign-imm.rs:34:34 | LL | x = 1; | ----- first assignment to `x` ... LL | asm!("mov $1, $0" : "=r"(x) : "r"(5)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error: aborting due to previous error diff --git a/src/test/ui/borrowck/borrowck-asm.ast.nll.stderr b/src/test/ui/borrowck/borrowck-asm.ast.nll.stderr index b0f0535b9f6c7..0cec1975db880 100644 --- a/src/test/ui/borrowck/borrowck-asm.ast.nll.stderr +++ b/src/test/ui/borrowck/borrowck-asm.ast.nll.stderr @@ -22,7 +22,7 @@ LL | let z = y; | - borrow later used here error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/borrowck-asm.rs:54:13 + --> $DIR/borrowck-asm.rs:54:31 | LL | let x = 3; | - @@ -31,10 +31,10 @@ LL | let x = 3; | help: make this binding mutable: `mut x` LL | unsafe { LL | asm!("nop" : "=r"(x)); //[ast]~ ERROR cannot assign twice - | ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/borrowck-asm.rs:70:13 + --> $DIR/borrowck-asm.rs:70:31 | LL | let x = 3; | - @@ -43,22 +43,22 @@ LL | let x = 3; | help: make this binding mutable: `mut x` LL | unsafe { LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign twice - | ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error[E0381]: use of possibly uninitialized variable: `x` - --> $DIR/borrowck-asm.rs:78:13 + --> $DIR/borrowck-asm.rs:78:32 | LL | asm!("nop" : "=*r"(x)); //[ast]~ ERROR use of possibly uninitialized variable - | ^^^^^^^^^^^^^^^^^^^^^^^ use of possibly uninitialized `x` + | ^ use of possibly uninitialized `x` error[E0506]: cannot assign to `x` because it is borrowed - --> $DIR/borrowck-asm.rs:87:13 + --> $DIR/borrowck-asm.rs:87:31 | LL | let y = &*x; | --- borrow of `x` occurs here LL | unsafe { LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign to `x` because it is borrowed - | ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `x` occurs here + | ^ assignment to borrowed `x` occurs here ... LL | let z = y; | - borrow later used here diff --git a/src/test/ui/borrowck/borrowck-asm.ast.stderr b/src/test/ui/borrowck/borrowck-asm.ast.stderr index e2e54aa9b8ad9..5856a1b0790b2 100644 --- a/src/test/ui/borrowck/borrowck-asm.ast.stderr +++ b/src/test/ui/borrowck/borrowck-asm.ast.stderr @@ -19,31 +19,31 @@ LL | asm!("nop" : : "r"(x)); //[ast]~ ERROR cannot use | ^ use of borrowed `x` error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/borrowck-asm.rs:54:13 + --> $DIR/borrowck-asm.rs:54:31 | LL | let x = 3; | - first assignment to `x` LL | unsafe { LL | asm!("nop" : "=r"(x)); //[ast]~ ERROR cannot assign twice - | ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error[E0506]: cannot assign to `a` because it is borrowed - --> $DIR/borrowck-asm.rs:60:13 + --> $DIR/borrowck-asm.rs:60:31 | LL | let b = &*a; | -- borrow of `a` occurs here LL | unsafe { LL | asm!("nop" : "=r"(a)); //[ast]~ ERROR cannot assign to `a` because it is borrowed - | ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `a` occurs here + | ^ assignment to borrowed `a` occurs here error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/borrowck-asm.rs:70:13 + --> $DIR/borrowck-asm.rs:70:31 | LL | let x = 3; | - first assignment to `x` LL | unsafe { LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign twice - | ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error[E0381]: use of possibly uninitialized variable: `x` --> $DIR/borrowck-asm.rs:78:32 @@ -52,13 +52,13 @@ LL | asm!("nop" : "=*r"(x)); //[ast]~ ERROR use of possibly uninitia | ^ use of possibly uninitialized `x` error[E0506]: cannot assign to `x` because it is borrowed - --> $DIR/borrowck-asm.rs:87:13 + --> $DIR/borrowck-asm.rs:87:31 | LL | let y = &*x; | -- borrow of `x` occurs here LL | unsafe { LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign to `x` because it is borrowed - | ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `x` occurs here + | ^ assignment to borrowed `x` occurs here error[E0382]: use of moved value: `x` --> $DIR/borrowck-asm.rs:96:40 diff --git a/src/test/ui/borrowck/borrowck-asm.mir.stderr b/src/test/ui/borrowck/borrowck-asm.mir.stderr index b0f0535b9f6c7..0cec1975db880 100644 --- a/src/test/ui/borrowck/borrowck-asm.mir.stderr +++ b/src/test/ui/borrowck/borrowck-asm.mir.stderr @@ -22,7 +22,7 @@ LL | let z = y; | - borrow later used here error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/borrowck-asm.rs:54:13 + --> $DIR/borrowck-asm.rs:54:31 | LL | let x = 3; | - @@ -31,10 +31,10 @@ LL | let x = 3; | help: make this binding mutable: `mut x` LL | unsafe { LL | asm!("nop" : "=r"(x)); //[ast]~ ERROR cannot assign twice - | ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error[E0384]: cannot assign twice to immutable variable `x` - --> $DIR/borrowck-asm.rs:70:13 + --> $DIR/borrowck-asm.rs:70:31 | LL | let x = 3; | - @@ -43,22 +43,22 @@ LL | let x = 3; | help: make this binding mutable: `mut x` LL | unsafe { LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign twice - | ^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable + | ^ cannot assign twice to immutable variable error[E0381]: use of possibly uninitialized variable: `x` - --> $DIR/borrowck-asm.rs:78:13 + --> $DIR/borrowck-asm.rs:78:32 | LL | asm!("nop" : "=*r"(x)); //[ast]~ ERROR use of possibly uninitialized variable - | ^^^^^^^^^^^^^^^^^^^^^^^ use of possibly uninitialized `x` + | ^ use of possibly uninitialized `x` error[E0506]: cannot assign to `x` because it is borrowed - --> $DIR/borrowck-asm.rs:87:13 + --> $DIR/borrowck-asm.rs:87:31 | LL | let y = &*x; | --- borrow of `x` occurs here LL | unsafe { LL | asm!("nop" : "+r"(x)); //[ast]~ ERROR cannot assign to `x` because it is borrowed - | ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `x` occurs here + | ^ assignment to borrowed `x` occurs here ... LL | let z = y; | - borrow later used here