Skip to content

Commit 8668662

Browse files
authored
Unrolled build for #148465
Rollup merge of #148465 - Jarcho:for_span, r=davidtwco,Zalathar Adjust spans into the `for` loops context before creating the new desugaring spans. When lowering `for` loops, the spans for the `into_iter` call and the `Some` pattern used the span of the provided pattern and head expression. If either of those came from a different `SyntaxContext` this would result in some very strange contexts. e.g.: ```rust macro_rules! m { ($e:expr) => { { $e } } } for _ in m!(expr) {} ``` This would result in the `into_iter` call have a context chain of `desugar => m!() => root` which is completely nonsensical; `m!()` does not have a `for` loop. The `into_iter` call also ends up located at `{ $e }` rather than inside the `for _ in _` part. This fixes that by walking the spans up to the `for` loop's context first. This will not handle adjusting the location of macro variable expansions (e.g. `for _ in $e`), but this does adjust the context to match the `for` loops. --- This ended up causing rust-lang/rust-clippy#16008. Clippy should be using a `debug_assert` rather than `unreachable`, but it still results in a bug either way.
2 parents 11339a0 + 76067c4 commit 8668662

21 files changed

+267
-171
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,8 +1771,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
17711771
let pat = self.lower_pat(pat);
17721772
let for_span =
17731773
self.mark_span_with_reason(DesugaringKind::ForLoop, self.lower_span(e.span), None);
1774-
let head_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
1775-
let pat_span = self.mark_span_with_reason(DesugaringKind::ForLoop, pat.span, None);
1774+
let for_ctxt = for_span.ctxt();
1775+
1776+
// Try to point both the head and pat spans to their position in the for loop
1777+
// rather than inside a macro.
1778+
let head_span =
1779+
head.span.find_ancestor_in_same_ctxt(e.span).unwrap_or(head.span).with_ctxt(for_ctxt);
1780+
let pat_span =
1781+
pat.span.find_ancestor_in_same_ctxt(e.span).unwrap_or(pat.span).with_ctxt(for_ctxt);
17761782

17771783
let loop_hir_id = self.lower_node_id(e.id);
17781784
let label = self.lower_label(opt_label, e.id, loop_hir_id);

tests/coverage/async_block.cov-map

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: async_block::main
2-
Raw bytes (41): 0x[01, 01, 01, 05, 01, 07, 01, 07, 01, 00, 0a, 02, 01, 09, 00, 0a, 05, 00, 0e, 00, 13, 02, 01, 0d, 00, 13, 02, 07, 09, 00, 1b, 02, 00, 1c, 00, 22, 01, 02, 01, 00, 02]
2+
Raw bytes (41): 0x[01, 01, 01, 05, 01, 07, 01, 07, 01, 00, 0a, 02, 01, 09, 00, 0a, 01, 00, 0e, 00, 13, 02, 01, 0d, 00, 13, 02, 07, 09, 00, 1b, 02, 00, 1c, 00, 22, 01, 02, 01, 00, 02]
33
Number of files: 1
44
- file 0 => $DIR/async_block.rs
55
Number of expressions: 1
@@ -8,15 +8,15 @@ Number of file 0 mappings: 7
88
- Code(Counter(0)) at (prev + 7, 1) to (start + 0, 10)
99
- Code(Expression(0, Sub)) at (prev + 1, 9) to (start + 0, 10)
1010
= (c1 - c0)
11-
- Code(Counter(1)) at (prev + 0, 14) to (start + 0, 19)
11+
- Code(Counter(0)) at (prev + 0, 14) to (start + 0, 19)
1212
- Code(Expression(0, Sub)) at (prev + 1, 13) to (start + 0, 19)
1313
= (c1 - c0)
1414
- Code(Expression(0, Sub)) at (prev + 7, 9) to (start + 0, 27)
1515
= (c1 - c0)
1616
- Code(Expression(0, Sub)) at (prev + 0, 28) to (start + 0, 34)
1717
= (c1 - c0)
1818
- Code(Counter(0)) at (prev + 2, 1) to (start + 0, 2)
19-
Highest counter ID seen: c1
19+
Highest counter ID seen: c0
2020

2121
Function name: async_block::main::{closure#0}
2222
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]

tests/coverage/async_block.coverage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
LL| |extern crate executor;
66
LL| |
77
LL| 1|fn main() {
8-
LL| 17| for i in 0..16 {
9-
^16
8+
LL| 16| for i in 0..16 {
9+
^1
1010
LL| 16| let future = async {
1111
LL| 16| if i >= 12 {
1212
LL| 4| println!("big");

tests/coverage/continue.cov-map

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: continue::main
2-
Raw bytes (241): 0x[01, 01, 1a, 05, 01, 05, 13, 01, 09, 05, 13, 01, 09, 0d, 01, 0d, 27, 01, 11, 0d, 27, 01, 11, 15, 01, 15, 33, 01, 19, 1d, 01, 1d, 47, 01, 21, 1d, 47, 01, 21, 25, 01, 25, 53, 01, 29, 25, 01, 2d, 01, 63, 2d, 01, 31, 2d, 01, 25, 01, 03, 01, 00, 0a, 01, 01, 09, 00, 10, 01, 00, 13, 00, 2e, 01, 02, 09, 00, 0e, 01, 00, 11, 00, 12, 05, 01, 0e, 00, 13, 02, 01, 0f, 00, 16, 09, 02, 11, 00, 19, 0e, 02, 12, 02, 0e, 0e, 04, 09, 00, 0e, 0d, 02, 0e, 00, 13, 16, 01, 0f, 00, 16, 22, 01, 16, 02, 0e, 11, 04, 11, 00, 19, 22, 03, 09, 00, 0e, 15, 02, 0e, 00, 13, 2a, 01, 0f, 00, 16, 19, 01, 15, 02, 0e, 2e, 04, 11, 00, 19, 19, 03, 09, 00, 0e, 1d, 02, 0e, 00, 13, 36, 01, 0c, 00, 13, 21, 01, 0d, 00, 15, 42, 01, 09, 00, 0a, 42, 01, 09, 00, 0e, 25, 02, 0e, 00, 13, 56, 01, 0f, 00, 16, 4e, 01, 16, 02, 0e, 29, 03, 12, 02, 0e, 56, 04, 09, 00, 0e, 2d, 02, 0e, 00, 13, 31, 01, 0f, 00, 16, 66, 01, 16, 02, 0e, 5e, 04, 11, 00, 16, 66, 03, 09, 00, 0e, 01, 02, 0d, 00, 0e, 01, 01, 01, 00, 02]
2+
Raw bytes (241): 0x[01, 01, 1a, 05, 01, 05, 13, 01, 09, 05, 13, 01, 09, 0d, 01, 0d, 27, 01, 11, 0d, 27, 01, 11, 15, 01, 15, 33, 01, 19, 1d, 01, 1d, 47, 01, 21, 1d, 47, 01, 21, 25, 01, 25, 53, 01, 29, 25, 01, 31, 01, 63, 31, 01, 2d, 31, 01, 25, 01, 03, 01, 00, 0a, 01, 01, 09, 00, 10, 01, 00, 13, 00, 2e, 01, 02, 09, 00, 0e, 01, 00, 11, 00, 12, 01, 01, 0e, 00, 13, 02, 01, 0f, 00, 16, 09, 02, 11, 00, 19, 0e, 02, 12, 02, 0e, 0e, 04, 09, 00, 0e, 01, 02, 0e, 00, 13, 16, 01, 0f, 00, 16, 22, 01, 16, 02, 0e, 11, 04, 11, 00, 19, 22, 03, 09, 00, 0e, 01, 02, 0e, 00, 13, 2a, 01, 0f, 00, 16, 19, 01, 15, 02, 0e, 2e, 04, 11, 00, 19, 19, 03, 09, 00, 0e, 01, 02, 0e, 00, 13, 36, 01, 0c, 00, 13, 21, 01, 0d, 00, 15, 42, 01, 09, 00, 0a, 42, 01, 09, 00, 0e, 01, 02, 0e, 00, 13, 56, 01, 0f, 00, 16, 4e, 01, 16, 02, 0e, 29, 03, 12, 02, 0e, 56, 04, 09, 00, 0e, 01, 02, 0e, 00, 13, 2d, 01, 0f, 00, 16, 66, 01, 16, 02, 0e, 5e, 04, 11, 00, 16, 66, 03, 09, 00, 0e, 01, 02, 0d, 00, 0e, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => $DIR/continue.rs
55
Number of expressions: 26
@@ -25,64 +25,64 @@ Number of expressions: 26
2525
- expression 19 operands: lhs = Counter(9), rhs = Expression(20, Add)
2626
- expression 20 operands: lhs = Counter(0), rhs = Counter(10)
2727
- expression 21 operands: lhs = Counter(9), rhs = Counter(0)
28-
- expression 22 operands: lhs = Counter(11), rhs = Counter(0)
29-
- expression 23 operands: lhs = Expression(24, Add), rhs = Counter(11)
30-
- expression 24 operands: lhs = Counter(0), rhs = Counter(12)
31-
- expression 25 operands: lhs = Counter(11), rhs = Counter(0)
28+
- expression 22 operands: lhs = Counter(12), rhs = Counter(0)
29+
- expression 23 operands: lhs = Expression(24, Add), rhs = Counter(12)
30+
- expression 24 operands: lhs = Counter(0), rhs = Counter(11)
31+
- expression 25 operands: lhs = Counter(12), rhs = Counter(0)
3232
Number of file 0 mappings: 37
3333
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 10)
3434
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 16)
3535
- Code(Counter(0)) at (prev + 0, 19) to (start + 0, 46)
3636
- Code(Counter(0)) at (prev + 2, 9) to (start + 0, 14)
3737
- Code(Counter(0)) at (prev + 0, 17) to (start + 0, 18)
38-
- Code(Counter(1)) at (prev + 1, 14) to (start + 0, 19)
38+
- Code(Counter(0)) at (prev + 1, 14) to (start + 0, 19)
3939
- Code(Expression(0, Sub)) at (prev + 1, 15) to (start + 0, 22)
4040
= (c1 - c0)
4141
- Code(Counter(2)) at (prev + 2, 17) to (start + 0, 25)
4242
- Code(Expression(3, Sub)) at (prev + 2, 18) to (start + 2, 14)
4343
= (c1 - (c0 + c2))
4444
- Code(Expression(3, Sub)) at (prev + 4, 9) to (start + 0, 14)
4545
= (c1 - (c0 + c2))
46-
- Code(Counter(3)) at (prev + 2, 14) to (start + 0, 19)
46+
- Code(Counter(0)) at (prev + 2, 14) to (start + 0, 19)
4747
- Code(Expression(5, Sub)) at (prev + 1, 15) to (start + 0, 22)
4848
= (c3 - c0)
4949
- Code(Expression(8, Sub)) at (prev + 1, 22) to (start + 2, 14)
5050
= (c3 - (c0 + c4))
5151
- Code(Counter(4)) at (prev + 4, 17) to (start + 0, 25)
5252
- Code(Expression(8, Sub)) at (prev + 3, 9) to (start + 0, 14)
5353
= (c3 - (c0 + c4))
54-
- Code(Counter(5)) at (prev + 2, 14) to (start + 0, 19)
54+
- Code(Counter(0)) at (prev + 2, 14) to (start + 0, 19)
5555
- Code(Expression(10, Sub)) at (prev + 1, 15) to (start + 0, 22)
5656
= (c5 - c0)
5757
- Code(Counter(6)) at (prev + 1, 21) to (start + 2, 14)
5858
- Code(Expression(11, Sub)) at (prev + 4, 17) to (start + 0, 25)
5959
= (c5 - (c0 + c6))
6060
- Code(Counter(6)) at (prev + 3, 9) to (start + 0, 14)
61-
- Code(Counter(7)) at (prev + 2, 14) to (start + 0, 19)
61+
- Code(Counter(0)) at (prev + 2, 14) to (start + 0, 19)
6262
- Code(Expression(13, Sub)) at (prev + 1, 12) to (start + 0, 19)
6363
= (c7 - c0)
6464
- Code(Counter(8)) at (prev + 1, 13) to (start + 0, 21)
6565
- Code(Expression(16, Sub)) at (prev + 1, 9) to (start + 0, 10)
6666
= (c7 - (c0 + c8))
6767
- Code(Expression(16, Sub)) at (prev + 1, 9) to (start + 0, 14)
6868
= (c7 - (c0 + c8))
69-
- Code(Counter(9)) at (prev + 2, 14) to (start + 0, 19)
69+
- Code(Counter(0)) at (prev + 2, 14) to (start + 0, 19)
7070
- Code(Expression(21, Sub)) at (prev + 1, 15) to (start + 0, 22)
7171
= (c9 - c0)
7272
- Code(Expression(19, Sub)) at (prev + 1, 22) to (start + 2, 14)
7373
= (c9 - (c0 + c10))
7474
- Code(Counter(10)) at (prev + 3, 18) to (start + 2, 14)
7575
- Code(Expression(21, Sub)) at (prev + 4, 9) to (start + 0, 14)
7676
= (c9 - c0)
77-
- Code(Counter(11)) at (prev + 2, 14) to (start + 0, 19)
78-
- Code(Counter(12)) at (prev + 1, 15) to (start + 0, 22)
77+
- Code(Counter(0)) at (prev + 2, 14) to (start + 0, 19)
78+
- Code(Counter(11)) at (prev + 1, 15) to (start + 0, 22)
7979
- Code(Expression(25, Sub)) at (prev + 1, 22) to (start + 2, 14)
80-
= (c11 - c0)
80+
= (c12 - c0)
8181
- Code(Expression(23, Sub)) at (prev + 4, 17) to (start + 0, 22)
82-
= ((c0 + c12) - c11)
82+
= ((c0 + c11) - c12)
8383
- Code(Expression(25, Sub)) at (prev + 3, 9) to (start + 0, 14)
84-
= (c11 - c0)
84+
= (c12 - c0)
8585
- Code(Counter(0)) at (prev + 2, 13) to (start + 0, 14)
8686
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
87-
Highest counter ID seen: c12
87+
Highest counter ID seen: c11
8888

tests/coverage/continue.coverage

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
LL| 1| let is_true = std::env::args().len() == 1;
55
LL| |
66
LL| 1| let mut x = 0;
7-
LL| 11| for _ in 0..10 {
7+
LL| 1| for _ in 0..10 {
88
LL| 10| match is_true {
99
LL| | true => {
1010
LL| 10| continue;
@@ -15,7 +15,7 @@
1515
LL| | }
1616
LL| 0| x = 3;
1717
LL| | }
18-
LL| 11| for _ in 0..10 {
18+
LL| 1| for _ in 0..10 {
1919
LL| 10| match is_true {
2020
LL| 0| false => {
2121
LL| 0| x = 1;
@@ -26,7 +26,7 @@
2626
LL| | }
2727
LL| 0| x = 3;
2828
LL| | }
29-
LL| 11| for _ in 0..10 {
29+
LL| 1| for _ in 0..10 {
3030
LL| 10| match is_true {
3131
LL| 10| true => {
3232
LL| 10| x = 1;
@@ -37,13 +37,13 @@
3737
LL| | }
3838
LL| 10| x = 3;
3939
LL| | }
40-
LL| 11| for _ in 0..10 {
40+
LL| 1| for _ in 0..10 {
4141
LL| 10| if is_true {
4242
LL| 10| continue;
4343
LL| 0| }
4444
LL| 0| x = 3;
4545
LL| | }
46-
LL| 11| for _ in 0..10 {
46+
LL| 1| for _ in 0..10 {
4747
LL| 10| match is_true {
4848
LL| 0| false => {
4949
LL| 0| x = 1;

tests/coverage/inline.cov-map

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: inline::display::<char>
2-
Raw bytes (36): 0x[01, 01, 01, 05, 01, 06, 01, 29, 01, 00, 21, 02, 01, 09, 00, 0a, 05, 00, 0e, 00, 10, 02, 00, 11, 02, 06, 01, 03, 05, 00, 0d, 01, 01, 01, 00, 02]
2+
Raw bytes (36): 0x[01, 01, 01, 05, 01, 06, 01, 29, 01, 00, 21, 02, 01, 09, 00, 0a, 01, 00, 0e, 00, 10, 02, 00, 11, 02, 06, 01, 03, 05, 00, 0d, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => $DIR/inline.rs
55
Number of expressions: 1
@@ -8,12 +8,12 @@ Number of file 0 mappings: 6
88
- Code(Counter(0)) at (prev + 41, 1) to (start + 0, 33)
99
- Code(Expression(0, Sub)) at (prev + 1, 9) to (start + 0, 10)
1010
= (c1 - c0)
11-
- Code(Counter(1)) at (prev + 0, 14) to (start + 0, 16)
11+
- Code(Counter(0)) at (prev + 0, 14) to (start + 0, 16)
1212
- Code(Expression(0, Sub)) at (prev + 0, 17) to (start + 2, 6)
1313
= (c1 - c0)
1414
- Code(Counter(0)) at (prev + 3, 5) to (start + 0, 13)
1515
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
16-
Highest counter ID seen: c1
16+
Highest counter ID seen: c0
1717

1818
Function name: inline::error
1919
Raw bytes (14): 0x[01, 01, 00, 02, 01, 31, 01, 00, 0b, 01, 01, 05, 00, 0b]
@@ -50,7 +50,7 @@ Number of file 0 mappings: 4
5050
Highest counter ID seen: c0
5151

5252
Function name: inline::permutate::<char>
53-
Raw bytes (142): 0x[01, 01, 0e, 01, 05, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 01, 37, 05, 09, 16, 01, 0f, 01, 00, 38, 01, 01, 09, 00, 0a, 01, 00, 0d, 00, 13, 01, 00, 14, 00, 16, 01, 01, 08, 00, 0e, 05, 00, 0f, 02, 06, 02, 02, 0f, 00, 14, 2e, 01, 0d, 00, 0e, 09, 00, 12, 00, 13, 09, 00, 15, 00, 16, 2e, 00, 17, 04, 0a, 2e, 01, 0d, 00, 11, 2e, 00, 12, 00, 14, 2e, 00, 16, 00, 17, 2e, 00, 19, 00, 1a, 2e, 01, 0d, 00, 16, 2e, 00, 17, 00, 19, 2e, 00, 1b, 00, 20, 2e, 01, 0d, 00, 11, 2e, 00, 12, 00, 14, 32, 02, 0c, 02, 06, 01, 03, 01, 00, 02]
53+
Raw bytes (137): 0x[01, 01, 0e, 01, 05, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 0d, 09, 01, 37, 05, 09, 15, 01, 0f, 01, 00, 38, 01, 01, 09, 00, 0a, 01, 00, 0d, 00, 13, 01, 00, 14, 00, 16, 01, 01, 08, 00, 0e, 05, 00, 0f, 02, 06, 02, 02, 0f, 00, 14, 2e, 01, 0d, 00, 0e, 09, 00, 12, 00, 16, 2e, 00, 17, 04, 0a, 2e, 01, 0d, 00, 11, 2e, 00, 12, 00, 14, 2e, 00, 16, 00, 17, 2e, 00, 19, 00, 1a, 2e, 01, 0d, 00, 16, 2e, 00, 17, 00, 19, 2e, 00, 1b, 00, 20, 2e, 01, 0d, 00, 11, 2e, 00, 12, 00, 14, 32, 02, 0c, 02, 06, 01, 03, 01, 00, 02]
5454
Number of files: 1
5555
- file 0 => $DIR/inline.rs
5656
Number of expressions: 14
@@ -68,7 +68,7 @@ Number of expressions: 14
6868
- expression 11 operands: lhs = Counter(3), rhs = Counter(2)
6969
- expression 12 operands: lhs = Counter(0), rhs = Expression(13, Add)
7070
- expression 13 operands: lhs = Counter(1), rhs = Counter(2)
71-
Number of file 0 mappings: 22
71+
Number of file 0 mappings: 21
7272
- Code(Counter(0)) at (prev + 15, 1) to (start + 0, 56)
7373
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 10)
7474
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 19)
@@ -79,8 +79,7 @@ Number of file 0 mappings: 22
7979
= (c0 - c1)
8080
- Code(Expression(11, Sub)) at (prev + 1, 13) to (start + 0, 14)
8181
= (c3 - c2)
82-
- Code(Counter(2)) at (prev + 0, 18) to (start + 0, 19)
83-
- Code(Counter(2)) at (prev + 0, 21) to (start + 0, 22)
82+
- Code(Counter(2)) at (prev + 0, 18) to (start + 0, 22)
8483
- Code(Expression(11, Sub)) at (prev + 0, 23) to (start + 4, 10)
8584
= (c3 - c2)
8685
- Code(Expression(11, Sub)) at (prev + 1, 13) to (start + 0, 17)

tests/coverage/inline.coverage

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
LL| 6| display(xs);
1919
LL| 10| } else if k < n {
2020
LL| 15| for i in k..n {
21-
^10^10
21+
^10
2222
LL| 15| swap(xs, i, k);
2323
LL| 15| permutate(xs, k + 1);
2424
LL| 15| swap(xs, i, k);
@@ -40,8 +40,8 @@
4040
LL| 30|}
4141
LL| |
4242
LL| 6|fn display<T: Display>(xs: &[T]) {
43-
LL| 24| for x in xs {
44-
^18
43+
LL| 18| for x in xs {
44+
^6
4545
LL| 18| print!("{}", x);
4646
LL| 18| }
4747
LL| 6| println!();

0 commit comments

Comments
 (0)