Skip to content

Commit daa5a9b

Browse files
committed
Use statement span for statement scope
1 parent b4e2c4e commit daa5a9b

13 files changed

+53
-40
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,22 @@ impl<'hir> Map<'hir> {
818818
self.tcx.hir_attrs(id.owner).get(id.local_id)
819819
}
820820

821+
pub fn stmt_span(&self, hir_id: HirId) -> Span {
822+
match self.find(hir_id).unwrap() {
823+
Node::Local(_) | Node::Item(_) | Node::Expr(_) => {
824+
if let Some(Node::Block(block)) = self.find(self.get_parent_node(hir_id)) {
825+
for stmt in block.stmts {
826+
if stmt.kind.hir_id() == hir_id {
827+
return stmt.span;
828+
}
829+
}
830+
}
831+
}
832+
_ => {}
833+
}
834+
self.span(hir_id)
835+
}
836+
821837
/// Gets the span of the definition of the specified HIR node.
822838
/// This is used by `tcx.get_span`
823839
pub fn span(&self, hir_id: HirId) -> Span {

compiler/rustc_middle/src/middle/region.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ impl Scope {
175175
Some(hir_id) => hir_id,
176176
None => return DUMMY_SP,
177177
};
178-
let span = tcx.hir().span(hir_id);
178+
let span = if self.for_stmt {
179+
tcx.hir().stmt_span(hir_id)
180+
} else {
181+
tcx.hir().span(hir_id)
182+
};
183+
179184
if let ScopeData::Remainder(first_statement_index) = self.data {
180185
if let Node::Block(ref blk) = tcx.hir().get(hir_id) {
181186
// Want span for scope starting after the

src/test/ui/async-await/issue-70935-complex-spans.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ LL | | foo(tx.clone());
1313
LL | | }).await;
1414
| |________________^ first, await occurs here, with the value maybe used later...
1515
note: the value is later dropped here
16-
--> $DIR/issue-70935-complex-spans.rs:15:16
16+
--> $DIR/issue-70935-complex-spans.rs:15:17
1717
|
1818
LL | }).await;
19-
| ^
19+
| ^
2020
note: this has type `[closure@$DIR/issue-70935-complex-spans.rs:13:13: 15:10]` which is not `Send`
2121
--> $DIR/issue-70935-complex-spans.rs:13:13
2222
|

src/test/ui/async-await/issues/issue-65436-raw-ptr-not-send.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ note: future is not `Send` as this value is used across an await
1414
LL | bar(Foo(std::ptr::null())).await;
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ first, await occurs here, with `std::ptr::null()` maybe used later...
1616
note: `std::ptr::null()` is later dropped here
17-
--> $DIR/issue-65436-raw-ptr-not-send.rs:14:40
17+
--> $DIR/issue-65436-raw-ptr-not-send.rs:14:41
1818
|
1919
LL | bar(Foo(std::ptr::null())).await;
20-
| ---------------- ^
20+
| ---------------- ^
2121
| |
2222
| has type `*const u8` which is not `Send`
2323
help: consider moving this into a `let` binding to create a shorter lived borrow

src/test/ui/borrowck/borrowck-borrowed-uniq-rvalue.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0716]: temporary value dropped while borrowed
22
--> $DIR/borrowck-borrowed-uniq-rvalue.rs:10:28
33
|
44
LL | buggy_map.insert(42, &*Box::new(1));
5-
| ^^^^^^^^^^^- temporary value is freed at the end of this statement
5+
| ^^^^^^^^^^^ - temporary value is freed at the end of this statement
66
| |
77
| creates a temporary which is freed while still in use
88
...

src/test/ui/borrowck/issue-17545.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ LL | / bar.call((
77
LL | | &id(()),
88
| | ^^^^^^ creates a temporary which is freed while still in use
99
LL | | ));
10-
| | -
11-
| | |
12-
| |______temporary value is freed at the end of this statement
10+
| | -- temporary value is freed at the end of this statement
11+
| |______|
1312
| argument requires that borrow lasts for `'a`
1413

1514
error: aborting due to previous error

src/test/ui/issues/issue-47646.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | println!("{:?}", heap);
1111
| ^^^^ immutable borrow occurs here
1212
...
1313
LL | };
14-
| - ... and the mutable borrow might be used here, when that temporary is dropped and runs the destructor for type `(Option<PeekMut<'_, i32>>, ())`
14+
| - ... and the mutable borrow might be used here, when that temporary is dropped and runs the destructor for type `(Option<PeekMut<'_, i32>>, ())`
1515

1616
error: aborting due to previous error
1717

src/test/ui/nll/issue-54382-use-span-of-tail-of-block.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ LL | D("other").next(&_thing1)
88
| a temporary with access to the borrow is created here ...
99
...
1010
LL | }
11-
| -
12-
| |
13-
| `_thing1` dropped here while still borrowed
14-
| ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
11+
| - `_thing1` dropped here while still borrowed
12+
LL |
13+
LL | ;
14+
| - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
1515
|
1616
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
1717
|

src/test/ui/nll/issue-54556-temps-in-tail-diagnostic.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | D(&_thing1).end()
77
| | borrowed value does not live long enough
88
| a temporary with access to the borrow is created here ...
99
LL | }
10-
| -
11-
| |
12-
| `_thing1` dropped here while still borrowed
13-
| ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
10+
| - `_thing1` dropped here while still borrowed
11+
LL |
12+
LL | ;
13+
| - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
1414
|
1515
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
1616
|

src/test/ui/nll/issue-54556-used-vs-unused-tails.stderr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ error[E0597]: `_t1` does not live long enough
22
--> $DIR/issue-54556-used-vs-unused-tails.rs:10:55
33
|
44
LL | { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // suggest `;`
5-
| --^^^^- -
5+
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
66
| | | |
77
| | | `_t1` dropped here while still borrowed
8-
| | | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
98
| | borrowed value does not live long enough
109
| a temporary with access to the borrow is created here ...
1110
|
@@ -18,7 +17,7 @@ error[E0597]: `_t1` does not live long enough
1817
--> $DIR/issue-54556-used-vs-unused-tails.rs:13:55
1918
|
2019
LL | { { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } } ; // suggest `;`
21-
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
20+
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
2221
| | | |
2322
| | | `_t1` dropped here while still borrowed
2423
| | borrowed value does not live long enough
@@ -33,10 +32,9 @@ error[E0597]: `_t1` does not live long enough
3332
--> $DIR/issue-54556-used-vs-unused-tails.rs:16:55
3433
|
3534
LL | { { let mut _t1 = D(Box::new("t1")); D(&_t1).end() }; } // suggest `;`
36-
| --^^^^- -
35+
| --^^^^- -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
3736
| | | |
3837
| | | `_t1` dropped here while still borrowed
39-
| | | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
4038
| | borrowed value does not live long enough
4139
| a temporary with access to the borrow is created here ...
4240
|
@@ -96,10 +94,9 @@ error[E0597]: `_t1` does not live long enough
9694
--> $DIR/issue-54556-used-vs-unused-tails.rs:30:55
9795
|
9896
LL | _y = { let mut _t1 = D(Box::new("t1")); D(&_t1).end() } ; // `let x = ...; x`
99-
| --^^^^- -
97+
| --^^^^- - - ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
10098
| | | |
10199
| | | `_t1` dropped here while still borrowed
102-
| | | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
103100
| | borrowed value does not live long enough
104101
| a temporary with access to the borrow is created here ...
105102
|

src/test/ui/regions/regions-var-type-out-of-scope.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0716]: temporary value dropped while borrowed
22
--> $DIR/regions-var-type-out-of-scope.rs:9:14
33
|
44
LL | x = &id(3);
5-
| ^^^^-
6-
| | |
7-
| | temporary value is freed at the end of this statement
5+
| ^^^^^- temporary value is freed at the end of this statement
6+
| |
87
| creates a temporary which is freed while still in use
98
LL | assert_eq!(*x, 3);
109
| ------------------ borrow later used here

src/test/ui/span/borrowck-let-suggestion-suffixes.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0716]: temporary value dropped while borrowed
1414
--> $DIR/borrowck-let-suggestion-suffixes.rs:19:14
1515
|
1616
LL | v3.push(&id('x')); // statement 6
17-
| ^^^^^^^- temporary value is freed at the end of this statement
17+
| ^^^^^^^ - temporary value is freed at the end of this statement
1818
| |
1919
| creates a temporary which is freed while still in use
2020
...
@@ -27,7 +27,7 @@ error[E0716]: temporary value dropped while borrowed
2727
--> $DIR/borrowck-let-suggestion-suffixes.rs:29:18
2828
|
2929
LL | v4.push(&id('y'));
30-
| ^^^^^^^- temporary value is freed at the end of this statement
30+
| ^^^^^^^ - temporary value is freed at the end of this statement
3131
| |
3232
| creates a temporary which is freed while still in use
3333
...
@@ -40,7 +40,7 @@ error[E0716]: temporary value dropped while borrowed
4040
--> $DIR/borrowck-let-suggestion-suffixes.rs:40:14
4141
|
4242
LL | v5.push(&id('z'));
43-
| ^^^^^^^- temporary value is freed at the end of this statement
43+
| ^^^^^^^ - temporary value is freed at the end of this statement
4444
| |
4545
| creates a temporary which is freed while still in use
4646
...

src/test/ui/static/static-reference-to-fn-2.stderr

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ error[E0716]: temporary value dropped while borrowed
44
LL | fn state1(self_: &mut StateMachineIter) -> Option<&'static str> {
55
| ----- has type `&mut StateMachineIter<'1>`
66
LL | self_.statefn = &id(state2 as StateMachineFunc);
7-
| -----------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
8-
| | | |
9-
| | | temporary value is freed at the end of this statement
7+
| -----------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
8+
| | |
109
| | creates a temporary which is freed while still in use
1110
| assignment requires that borrow lasts for `'1`
1211

@@ -16,9 +15,8 @@ error[E0716]: temporary value dropped while borrowed
1615
LL | fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> {
1716
| ----- has type `&mut StateMachineIter<'1>`
1817
LL | self_.statefn = &id(state3 as StateMachineFunc);
19-
| -----------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
20-
| | | |
21-
| | | temporary value is freed at the end of this statement
18+
| -----------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
19+
| | |
2220
| | creates a temporary which is freed while still in use
2321
| assignment requires that borrow lasts for `'1`
2422

@@ -28,9 +26,8 @@ error[E0716]: temporary value dropped while borrowed
2826
LL | fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> {
2927
| ----- has type `&mut StateMachineIter<'1>`
3028
LL | self_.statefn = &id(finished as StateMachineFunc);
31-
| -----------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
32-
| | | |
33-
| | | temporary value is freed at the end of this statement
29+
| -----------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
30+
| | |
3431
| | creates a temporary which is freed while still in use
3532
| assignment requires that borrow lasts for `'1`
3633

0 commit comments

Comments
 (0)