Skip to content

Commit aed6c5d

Browse files
committed
Use a more accurate span for the implicit block return assignment
1 parent 0df64c5 commit aed6c5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+560
-517
lines changed

compiler/rustc_mir_build/src/builder/block.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6464
// By doing so, we can be sure that even temporaries that receive extended lifetime
6565
// assignments are dropped, too.
6666
let mut last_remainder_scope = region_scope;
67-
67+
let mut last_span = None;
6868
let source_info = this.source_info(span);
6969
for stmt in stmts {
7070
let Stmt { ref kind } = this.thir[*stmt];
71+
last_span = match kind {
72+
StmtKind::Expr { expr, .. } => Some(this.thir[*expr].span),
73+
StmtKind::Let { span, .. } => Some(*span),
74+
};
75+
7176
match kind {
7277
StmtKind::Expr { scope, expr } => {
7378
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
@@ -334,6 +339,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
334339
{
335340
// We only want to assign an implicit `()` as the return value of the block if the
336341
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
342+
// Shrink the span to the tail of the block to avoid confusing diagnostics highlighting the entire block
343+
let source_info = if let Some(span) = last_span {
344+
SourceInfo { span: span.shrink_to_hi(), ..source_info }
345+
} else {
346+
source_info
347+
};
337348
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
338349
}
339350
}

tests/coverage-run-rustdoc/doctest.coverage

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ $DIR/doctest.rs:
4444
LL| |//! } else {
4545
LL| 1|//! if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
4646
LL| 1|//! println!("{:?}", res);
47-
LL| 1|//! }
48-
^0
47+
LL| 0|//! }
4948
LL| 1|//! if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
5049
LL| 1|//! res = Ok(1);
51-
LL| 1|//! }
52-
^0
50+
LL| 0|//! }
5351
LL| 1|//! res = Ok(0);
5452
LL| |//! }
5553
LL| |//! // need to be explicit because rustdoc cant infer the return type

tests/coverage/abort.cov-map

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: abort::main
2-
Raw bytes (98): 0x[01, 01, 07, 05, 01, 05, 0b, 01, 09, 05, 13, 01, 0d, 05, 1b, 01, 11, 10, 01, 0d, 01, 00, 1c, 01, 01, 09, 00, 16, 01, 00, 19, 00, 1b, 05, 01, 0b, 00, 18, 02, 01, 0c, 00, 19, 09, 00, 1a, 02, 0a, 06, 02, 09, 00, 0a, 02, 02, 0c, 00, 19, 0d, 00, 1a, 00, 31, 0e, 00, 30, 00, 31, 02, 04, 0c, 00, 19, 11, 00, 1a, 00, 31, 16, 00, 30, 00, 31, 02, 01, 09, 00, 17, 01, 02, 05, 00, 0b, 01, 01, 01, 00, 02]
2+
Raw bytes (98): 0x[01, 01, 07, 05, 01, 05, 0b, 01, 09, 05, 13, 01, 0d, 05, 1b, 01, 11, 10, 01, 0d, 01, 00, 1c, 01, 01, 09, 00, 16, 01, 00, 19, 00, 1b, 05, 01, 0b, 00, 18, 02, 01, 0c, 00, 19, 09, 01, 0d, 00, 18, 06, 01, 09, 00, 0a, 02, 02, 0c, 00, 19, 0d, 00, 1c, 00, 27, 0e, 00, 30, 00, 31, 02, 04, 0c, 00, 19, 11, 00, 1c, 00, 27, 16, 00, 30, 00, 31, 02, 01, 09, 00, 17, 01, 02, 05, 00, 0b, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => $DIR/abort.rs
55
Number of expressions: 7
@@ -17,17 +17,17 @@ Number of file 0 mappings: 16
1717
- Code(Counter(1)) at (prev + 1, 11) to (start + 0, 24)
1818
- Code(Expression(0, Sub)) at (prev + 1, 12) to (start + 0, 25)
1919
= (c1 - c0)
20-
- Code(Counter(2)) at (prev + 0, 26) to (start + 2, 10)
21-
- Code(Expression(1, Sub)) at (prev + 2, 9) to (start + 0, 10)
20+
- Code(Counter(2)) at (prev + 1, 13) to (start + 0, 24)
21+
- Code(Expression(1, Sub)) at (prev + 1, 9) to (start + 0, 10)
2222
= (c1 - (c0 + c2))
2323
- Code(Expression(0, Sub)) at (prev + 2, 12) to (start + 0, 25)
2424
= (c1 - c0)
25-
- Code(Counter(3)) at (prev + 0, 26) to (start + 0, 49)
25+
- Code(Counter(3)) at (prev + 0, 28) to (start + 0, 39)
2626
- Code(Expression(3, Sub)) at (prev + 0, 48) to (start + 0, 49)
2727
= (c1 - (c0 + c3))
2828
- Code(Expression(0, Sub)) at (prev + 4, 12) to (start + 0, 25)
2929
= (c1 - c0)
30-
- Code(Counter(4)) at (prev + 0, 26) to (start + 0, 49)
30+
- Code(Counter(4)) at (prev + 0, 28) to (start + 0, 39)
3131
- Code(Expression(5, Sub)) at (prev + 0, 48) to (start + 0, 49)
3232
= (c1 - (c0 + c4))
3333
- Code(Expression(0, Sub)) at (prev + 1, 9) to (start + 0, 23)
@@ -37,7 +37,7 @@ Number of file 0 mappings: 16
3737
Highest counter ID seen: c4
3838

3939
Function name: abort::might_abort
40-
Raw bytes (36): 0x[01, 01, 01, 01, 05, 06, 01, 03, 01, 00, 2e, 01, 01, 08, 00, 14, 05, 01, 09, 00, 11, 05, 01, 09, 00, 0f, 02, 01, 0c, 02, 06, 02, 03, 01, 00, 02]
40+
Raw bytes (36): 0x[01, 01, 01, 01, 05, 06, 01, 03, 01, 00, 2e, 01, 01, 08, 00, 14, 05, 01, 09, 00, 11, 05, 01, 09, 00, 0f, 02, 02, 09, 00, 11, 02, 02, 01, 00, 02]
4141
Number of files: 1
4242
- file 0 => $DIR/abort.rs
4343
Number of expressions: 1
@@ -47,9 +47,9 @@ Number of file 0 mappings: 6
4747
- Code(Counter(0)) at (prev + 1, 8) to (start + 0, 20)
4848
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 17)
4949
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 15)
50-
- Code(Expression(0, Sub)) at (prev + 1, 12) to (start + 2, 6)
50+
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 17)
5151
= (c0 - c1)
52-
- Code(Expression(0, Sub)) at (prev + 3, 1) to (start + 0, 2)
52+
- Code(Expression(0, Sub)) at (prev + 2, 1) to (start + 0, 2)
5353
= (c0 - c1)
5454
Highest counter ID seen: c1
5555

tests/coverage/abort.coverage

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
LL| 12| if should_abort {
55
LL| 0| println!("aborting...");
66
LL| 0| panic!("panics and aborts");
7-
LL| 12| } else {
7+
LL| | } else {
88
LL| 12| println!("Don't Panic");
9-
LL| 12| }
9+
LL| | }
1010
LL| 12|}
1111
LL| |
1212
LL| |#[rustfmt::skip]
@@ -18,12 +18,12 @@
1818
LL| 6| }
1919
LL| | // See discussion (below the `Notes` section) on coverage results for the closing brace.
2020
LL| 10| if countdown < 5 { might_abort(false); } // Counts for different regions on one line.
21-
^4 ^6
21+
^4 ^6
2222
LL| | // For the following example, the closing brace is the last character on the line.
2323
LL| | // This shows the character after the closing brace is highlighted, even if that next
2424
LL| | // character is a newline.
2525
LL| 10| if countdown < 5 { might_abort(false); }
26-
^4 ^6
26+
^4 ^6
2727
LL| 10| countdown -= 1;
2828
LL| | }
2929
LL| 1| Ok(())

tests/coverage/assert.cov-map

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: assert::main
2-
Raw bytes (76): 0x[01, 01, 06, 05, 01, 05, 17, 01, 09, 05, 13, 17, 0d, 01, 09, 0c, 01, 09, 01, 00, 1c, 01, 01, 09, 00, 16, 01, 00, 19, 00, 1b, 05, 01, 0b, 00, 18, 02, 01, 0c, 00, 1a, 09, 00, 1b, 02, 0a, 06, 02, 13, 00, 20, 0d, 00, 21, 02, 0a, 0e, 02, 09, 00, 0a, 02, 01, 09, 00, 17, 01, 02, 05, 00, 0b, 01, 01, 01, 00, 02]
2+
Raw bytes (76): 0x[01, 01, 06, 05, 01, 05, 17, 01, 09, 05, 13, 17, 0d, 01, 09, 0c, 01, 09, 01, 00, 1c, 01, 01, 09, 00, 16, 01, 00, 19, 00, 1b, 05, 01, 0b, 00, 18, 02, 01, 0c, 00, 1a, 09, 01, 0d, 00, 1e, 06, 01, 13, 00, 20, 0d, 01, 0d, 00, 1e, 0e, 01, 09, 00, 0a, 02, 01, 09, 00, 17, 01, 02, 05, 00, 0b, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => $DIR/assert.rs
55
Number of expressions: 6
@@ -16,11 +16,11 @@ Number of file 0 mappings: 12
1616
- Code(Counter(1)) at (prev + 1, 11) to (start + 0, 24)
1717
- Code(Expression(0, Sub)) at (prev + 1, 12) to (start + 0, 26)
1818
= (c1 - c0)
19-
- Code(Counter(2)) at (prev + 0, 27) to (start + 2, 10)
20-
- Code(Expression(1, Sub)) at (prev + 2, 19) to (start + 0, 32)
19+
- Code(Counter(2)) at (prev + 1, 13) to (start + 0, 30)
20+
- Code(Expression(1, Sub)) at (prev + 1, 19) to (start + 0, 32)
2121
= (c1 - (c0 + c2))
22-
- Code(Counter(3)) at (prev + 0, 33) to (start + 2, 10)
23-
- Code(Expression(3, Sub)) at (prev + 2, 9) to (start + 0, 10)
22+
- Code(Counter(3)) at (prev + 1, 13) to (start + 0, 30)
23+
- Code(Expression(3, Sub)) at (prev + 1, 9) to (start + 0, 10)
2424
= (c1 - ((c0 + c2) + c3))
2525
- Code(Expression(0, Sub)) at (prev + 1, 9) to (start + 0, 23)
2626
= (c1 - c0)

tests/coverage/async.cov-map

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Number of file 0 mappings: 13
193193
Highest counter ID seen: c5
194194

195195
Function name: async::j
196-
Raw bytes (65): 0x[01, 01, 03, 01, 05, 01, 0b, 05, 09, 0b, 01, 37, 01, 00, 0c, 01, 0b, 0b, 00, 0c, 05, 01, 09, 00, 0a, 01, 00, 0e, 00, 0f, 01, 00, 0e, 00, 1b, 05, 00, 1f, 00, 27, 09, 01, 09, 00, 0a, 02, 00, 0e, 00, 1a, 09, 00, 1e, 00, 20, 06, 01, 0e, 00, 10, 01, 02, 01, 00, 02]
196+
Raw bytes (65): 0x[01, 01, 03, 01, 05, 01, 0b, 05, 09, 0b, 01, 37, 01, 00, 0c, 01, 0b, 0b, 00, 0c, 05, 01, 09, 00, 0a, 01, 00, 0e, 00, 0f, 01, 00, 0e, 00, 1b, 05, 00, 21, 00, 22, 09, 01, 09, 00, 0a, 02, 00, 0e, 00, 1a, 09, 00, 1e, 00, 20, 06, 01, 0e, 00, 10, 01, 02, 01, 00, 02]
197197
Number of files: 1
198198
- file 0 => $DIR/async.rs
199199
Number of expressions: 3
@@ -206,7 +206,7 @@ Number of file 0 mappings: 11
206206
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 10)
207207
- Code(Counter(0)) at (prev + 0, 14) to (start + 0, 15)
208208
- Code(Counter(0)) at (prev + 0, 14) to (start + 0, 27)
209-
- Code(Counter(1)) at (prev + 0, 31) to (start + 0, 39)
209+
- Code(Counter(1)) at (prev + 0, 33) to (start + 0, 34)
210210
- Code(Counter(2)) at (prev + 1, 9) to (start + 0, 10)
211211
- Code(Expression(0, Sub)) at (prev + 0, 14) to (start + 0, 26)
212212
= (c0 - c1)

tests/coverage/async.coverage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
LL| 1| fn f() -> u8 { 1 }
7070
LL| 1| match x {
7171
LL| 1| y if c(x) == y + 1 => { d(); }
72-
^0 ^0
72+
^0 ^0
7373
LL| 1| y if f() == y + 1 => (),
7474
^0 ^0
7575
LL| 1| _ => (),

tests/coverage/async2.cov-map

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Number of file 0 mappings: 1
88
Highest counter ID seen: c0
99

1010
Function name: async2::async_func::{closure#0}
11-
Raw bytes (44): 0x[01, 01, 00, 08, 01, 0f, 17, 00, 18, 01, 01, 05, 00, 0d, 01, 01, 09, 00, 0a, 01, 00, 0d, 00, 11, 01, 01, 08, 00, 09, 01, 00, 0a, 02, 06, 00, 02, 05, 00, 06, 01, 01, 01, 00, 02]
11+
Raw bytes (44): 0x[01, 01, 00, 08, 01, 0f, 17, 00, 18, 01, 01, 05, 00, 0d, 01, 01, 09, 00, 0a, 01, 00, 0d, 00, 11, 01, 01, 08, 00, 09, 01, 01, 09, 00, 11, 00, 01, 05, 00, 06, 01, 01, 01, 00, 02]
1212
Number of files: 1
1313
- file 0 => $DIR/async2.rs
1414
Number of expressions: 0
@@ -18,8 +18,8 @@ Number of file 0 mappings: 8
1818
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 10)
1919
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 17)
2020
- Code(Counter(0)) at (prev + 1, 8) to (start + 0, 9)
21-
- Code(Counter(0)) at (prev + 0, 10) to (start + 2, 6)
22-
- Code(Zero) at (prev + 2, 5) to (start + 0, 6)
21+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 17)
22+
- Code(Zero) at (prev + 1, 5) to (start + 0, 6)
2323
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
2424
Highest counter ID seen: c0
2525

@@ -60,7 +60,7 @@ Number of file 0 mappings: 8
6060
Highest counter ID seen: c0
6161

6262
Function name: async2::non_async_func
63-
Raw bytes (44): 0x[01, 01, 00, 08, 01, 07, 01, 00, 14, 01, 01, 05, 00, 0d, 01, 01, 09, 00, 0a, 01, 00, 0d, 00, 11, 01, 01, 08, 00, 09, 01, 00, 0a, 02, 06, 00, 02, 05, 00, 06, 01, 01, 01, 00, 02]
63+
Raw bytes (44): 0x[01, 01, 00, 08, 01, 07, 01, 00, 14, 01, 01, 05, 00, 0d, 01, 01, 09, 00, 0a, 01, 00, 0d, 00, 11, 01, 01, 08, 00, 09, 01, 01, 09, 00, 11, 00, 01, 05, 00, 06, 01, 01, 01, 00, 02]
6464
Number of files: 1
6565
- file 0 => $DIR/async2.rs
6666
Number of expressions: 0
@@ -70,8 +70,8 @@ Number of file 0 mappings: 8
7070
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 10)
7171
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 17)
7272
- Code(Counter(0)) at (prev + 1, 8) to (start + 0, 9)
73-
- Code(Counter(0)) at (prev + 0, 10) to (start + 2, 6)
74-
- Code(Zero) at (prev + 2, 5) to (start + 0, 6)
73+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 17)
74+
- Code(Zero) at (prev + 1, 5) to (start + 0, 6)
7575
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
7676
Highest counter ID seen: c0
7777

tests/coverage/async2.coverage

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
LL| 1| let b = true;
1010
LL| 1| if b {
1111
LL| 1| println!("non_async_func println in block");
12-
LL| 1| }
13-
^0
12+
LL| 0| }
1413
LL| 1|}
1514
LL| |
1615
LL| 1|async fn async_func() {
1716
LL| 1| println!("async_func was covered");
1817
LL| 1| let b = true;
1918
LL| 1| if b {
2019
LL| 1| println!("async_func println in block");
21-
LL| 1| }
22-
^0
20+
LL| 0| }
2321
LL| 1|}
2422
LL| |
2523
LL| 1|async fn async_func_just_println() {

tests/coverage/async_block.cov-map

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ Number of file 0 mappings: 7
1919
Highest counter ID seen: c0
2020

2121
Function name: async_block::main::{closure#0}
22-
Raw bytes (31): 0x[01, 01, 01, 01, 05, 05, 01, 09, 1c, 00, 1d, 01, 01, 10, 00, 17, 05, 00, 18, 02, 0e, 02, 02, 14, 02, 0e, 01, 03, 09, 00, 0a]
22+
Raw bytes (31): 0x[01, 01, 01, 01, 05, 05, 01, 09, 1c, 00, 1d, 01, 01, 10, 00, 17, 05, 01, 11, 00, 19, 02, 02, 11, 00, 19, 01, 02, 09, 00, 0a]
2323
Number of files: 1
2424
- file 0 => $DIR/async_block.rs
2525
Number of expressions: 1
2626
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
2727
Number of file 0 mappings: 5
2828
- Code(Counter(0)) at (prev + 9, 28) to (start + 0, 29)
2929
- Code(Counter(0)) at (prev + 1, 16) to (start + 0, 23)
30-
- Code(Counter(1)) at (prev + 0, 24) to (start + 2, 14)
31-
- Code(Expression(0, Sub)) at (prev + 2, 20) to (start + 2, 14)
30+
- Code(Counter(1)) at (prev + 1, 17) to (start + 0, 25)
31+
- Code(Expression(0, Sub)) at (prev + 2, 17) to (start + 0, 25)
3232
= (c0 - c1)
33-
- Code(Counter(0)) at (prev + 3, 9) to (start + 0, 10)
33+
- Code(Counter(0)) at (prev + 2, 9) to (start + 0, 10)
3434
Highest counter ID seen: c1
3535

0 commit comments

Comments
 (0)