Skip to content

Commit cc3fdc6

Browse files
committed
Avoid a bogus THIR span for let x = offset_of!(..)
1 parent fb5a4dc commit cc3fdc6

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

compiler/rustc_mir_build/src/thir/cx/block.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ impl<'tcx> ThirBuildCx<'tcx> {
7575
debug!(?pattern);
7676

7777
let span = match local.init {
78-
Some(init) => local.span.with_hi(init.span.hi()),
79-
None => local.span,
78+
Some(init)
79+
if let Some(init_span) =
80+
init.span.find_ancestor_inside_same_ctxt(local.span) =>
81+
{
82+
local.span.with_hi(init_span.hi())
83+
}
84+
Some(_) | None => local.span,
8085
};
8186
let stmt = Stmt {
8287
kind: StmtKind::Let {

tests/ui/span/let-offset-of.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | if let x = offset_of!(Foo, field) {}
99
= note: `#[warn(irrefutable_let_patterns)]` on by default
1010

1111
warning: irrefutable `let...else` pattern
12+
--> $DIR/let-offset-of.rs:17:5
13+
|
14+
LL | let x = offset_of!(Foo, field) else { return; };
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1216
|
1317
= note: this pattern will always match, so the `else` clause is useless
1418
= help: consider removing the `else` clause

tests/ui/thir-print/offset_of.stdout

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ body:
6868
)
6969
else_block: None
7070
hir_id: HirId(DefId(offset_of::concrete).10)
71-
span: $DIR/offset_of.rs:37:5: 1445:57 (#0)
71+
span: $DIR/offset_of.rs:37:5: 37:33 (#0)
7272
}
7373
}
7474
Stmt {
@@ -117,7 +117,7 @@ body:
117117
)
118118
else_block: None
119119
hir_id: HirId(DefId(offset_of::concrete).20)
120-
span: $DIR/offset_of.rs:38:5: 1445:57 (#0)
120+
span: $DIR/offset_of.rs:38:5: 38:33 (#0)
121121
}
122122
}
123123
Stmt {
@@ -166,7 +166,7 @@ body:
166166
)
167167
else_block: None
168168
hir_id: HirId(DefId(offset_of::concrete).30)
169-
span: $DIR/offset_of.rs:39:5: 1445:57 (#0)
169+
span: $DIR/offset_of.rs:39:5: 39:34 (#0)
170170
}
171171
}
172172
Stmt {
@@ -215,7 +215,7 @@ body:
215215
)
216216
else_block: None
217217
hir_id: HirId(DefId(offset_of::concrete).40)
218-
span: $DIR/offset_of.rs:40:5: 1445:57 (#0)
218+
span: $DIR/offset_of.rs:40:5: 40:36 (#0)
219219
}
220220
}
221221
Stmt {
@@ -264,7 +264,7 @@ body:
264264
)
265265
else_block: None
266266
hir_id: HirId(DefId(offset_of::concrete).50)
267-
span: $DIR/offset_of.rs:41:5: 1445:57 (#0)
267+
span: $DIR/offset_of.rs:41:5: 41:36 (#0)
268268
}
269269
}
270270
]
@@ -864,7 +864,7 @@ body:
864864
)
865865
else_block: None
866866
hir_id: HirId(DefId(offset_of::generic).12)
867-
span: $DIR/offset_of.rs:45:5: 1445:57 (#0)
867+
span: $DIR/offset_of.rs:45:5: 45:37 (#0)
868868
}
869869
}
870870
Stmt {
@@ -913,7 +913,7 @@ body:
913913
)
914914
else_block: None
915915
hir_id: HirId(DefId(offset_of::generic).24)
916-
span: $DIR/offset_of.rs:46:5: 1445:57 (#0)
916+
span: $DIR/offset_of.rs:46:5: 46:37 (#0)
917917
}
918918
}
919919
Stmt {
@@ -962,7 +962,7 @@ body:
962962
)
963963
else_block: None
964964
hir_id: HirId(DefId(offset_of::generic).36)
965-
span: $DIR/offset_of.rs:47:5: 1445:57 (#0)
965+
span: $DIR/offset_of.rs:47:5: 47:37 (#0)
966966
}
967967
}
968968
Stmt {
@@ -1011,7 +1011,7 @@ body:
10111011
)
10121012
else_block: None
10131013
hir_id: HirId(DefId(offset_of::generic).48)
1014-
span: $DIR/offset_of.rs:48:5: 1445:57 (#0)
1014+
span: $DIR/offset_of.rs:48:5: 48:37 (#0)
10151015
}
10161016
}
10171017
]

0 commit comments

Comments
 (0)