Skip to content

Commit acc4c33

Browse files
committed
Fix span info for mir::Operand
Fixes #19172
1 parent 0c1b483 commit acc4c33

File tree

9 files changed

+147
-82
lines changed

9 files changed

+147
-82
lines changed

crates/hir-ty/src/mir.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ pub struct Local {
7777
/// currently implements it, but it seems like this may be something to check against in the
7878
/// validator.
7979
#[derive(Debug, PartialEq, Eq, Clone)]
80-
pub enum Operand {
80+
pub struct Operand {
81+
kind: OperandKind,
82+
// FIXME : This should actually just be of type `MirSpan`.
83+
span: Option<MirSpan>,
84+
}
85+
86+
#[derive(Debug, PartialEq, Eq, Clone)]
87+
pub enum OperandKind {
8188
/// Creates a value by loading the given place.
8289
///
8390
/// Before drop elaboration, the type of the place must be `Copy`. After drop elaboration there
@@ -101,7 +108,13 @@ pub enum Operand {
101108

102109
impl Operand {
103110
fn from_concrete_const(data: Box<[u8]>, memory_map: MemoryMap, ty: Ty) -> Self {
104-
Operand::Constant(intern_const_scalar(ConstScalar::Bytes(data, memory_map), ty))
111+
Operand {
112+
kind: OperandKind::Constant(intern_const_scalar(
113+
ConstScalar::Bytes(data, memory_map),
114+
ty,
115+
)),
116+
span: None,
117+
}
105118
}
106119

107120
fn from_bytes(data: Box<[u8]>, ty: Ty) -> Self {
@@ -1076,11 +1089,11 @@ impl MirBody {
10761089
f: &mut impl FnMut(&mut Place, &mut ProjectionStore),
10771090
store: &mut ProjectionStore,
10781091
) {
1079-
match op {
1080-
Operand::Copy(p) | Operand::Move(p) => {
1092+
match &mut op.kind {
1093+
OperandKind::Copy(p) | OperandKind::Move(p) => {
10811094
f(p, store);
10821095
}
1083-
Operand::Constant(_) | Operand::Static(_) => (),
1096+
OperandKind::Constant(_) | OperandKind::Static(_) => (),
10841097
}
10851098
}
10861099
for (_, block) in self.basic_blocks.iter_mut() {

crates/hir-ty/src/mir/borrowck.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ use crate::{
1515
ClosureId, Interner, Substitution, Ty, TyExt, TypeFlags,
1616
db::{HirDatabase, InternedClosure},
1717
display::DisplayTarget,
18-
mir::Operand,
18+
mir::OperandKind,
1919
utils::ClosureSubst,
2020
};
2121

2222
use super::{
23-
BasicBlockId, BorrowKind, LocalId, MirBody, MirLowerError, MirSpan, MutBorrowKind, Place,
24-
ProjectionElem, Rvalue, StatementKind, TerminatorKind,
23+
BasicBlockId, BorrowKind, LocalId, MirBody, MirLowerError, MirSpan, MutBorrowKind, Operand,
24+
Place, ProjectionElem, Rvalue, StatementKind, TerminatorKind,
2525
};
2626

2727
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -120,8 +120,8 @@ fn make_fetch_closure_field(
120120

121121
fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef> {
122122
let mut result = vec![];
123-
let mut for_operand = |op: &Operand, span: MirSpan| match op {
124-
Operand::Copy(p) | Operand::Move(p) => {
123+
let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
124+
OperandKind::Copy(p) | OperandKind::Move(p) => {
125125
let mut ty: Ty = body.locals[p.local].ty.clone();
126126
let mut is_dereference_of_ref = false;
127127
for proj in p.projection.lookup(&body.projection_store) {
@@ -139,10 +139,10 @@ fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef>
139139
&& !ty.clone().is_copy(db, body.owner)
140140
&& !ty.data(Interner).flags.intersects(TypeFlags::HAS_ERROR)
141141
{
142-
result.push(MovedOutOfRef { span, ty });
142+
result.push(MovedOutOfRef { span: op.span.unwrap_or(span), ty });
143143
}
144144
}
145-
Operand::Constant(_) | Operand::Static(_) => (),
145+
OperandKind::Constant(_) | OperandKind::Static(_) => (),
146146
};
147147
for (_, block) in body.basic_blocks.iter() {
148148
db.unwind_if_revision_cancelled();
@@ -215,8 +215,8 @@ fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef>
215215

216216
fn partially_moved(db: &dyn HirDatabase, body: &MirBody) -> Vec<PartiallyMoved> {
217217
let mut result = vec![];
218-
let mut for_operand = |op: &Operand, span: MirSpan| match op {
219-
Operand::Copy(p) | Operand::Move(p) => {
218+
let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
219+
OperandKind::Copy(p) | OperandKind::Move(p) => {
220220
let mut ty: Ty = body.locals[p.local].ty.clone();
221221
for proj in p.projection.lookup(&body.projection_store) {
222222
ty = proj.projected_ty(
@@ -232,7 +232,7 @@ fn partially_moved(db: &dyn HirDatabase, body: &MirBody) -> Vec<PartiallyMoved>
232232
result.push(PartiallyMoved { span, ty, local: p.local });
233233
}
234234
}
235-
Operand::Constant(_) | Operand::Static(_) => (),
235+
OperandKind::Constant(_) | OperandKind::Static(_) => (),
236236
};
237237
for (_, block) in body.basic_blocks.iter() {
238238
db.unwind_if_revision_cancelled();
@@ -500,7 +500,7 @@ fn record_usage(local: LocalId, result: &mut ArenaMap<LocalId, MutabilityReason>
500500
}
501501

502502
fn record_usage_for_operand(arg: &Operand, result: &mut ArenaMap<LocalId, MutabilityReason>) {
503-
if let Operand::Copy(p) | Operand::Move(p) = arg {
503+
if let OperandKind::Copy(p) | OperandKind::Move(p) = arg.kind {
504504
record_usage(p.local, result);
505505
}
506506
}

crates/hir-ty/src/mir/eval.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::{
4646

4747
use super::{
4848
AggregateKind, BasicBlockId, BinOp, CastKind, LocalId, MirBody, MirLowerError, MirSpan,
49-
Operand, Place, PlaceElem, ProjectionElem, ProjectionStore, Rvalue, StatementKind,
49+
Operand, OperandKind, Place, PlaceElem, ProjectionElem, ProjectionStore, Rvalue, StatementKind,
5050
TerminatorKind, UnOp, return_slot,
5151
};
5252

@@ -867,10 +867,10 @@ impl Evaluator<'_> {
867867
}
868868

869869
fn operand_ty(&self, o: &Operand, locals: &Locals) -> Result<Ty> {
870-
Ok(match o {
871-
Operand::Copy(p) | Operand::Move(p) => self.place_ty(p, locals)?,
872-
Operand::Constant(c) => c.data(Interner).ty.clone(),
873-
&Operand::Static(s) => {
870+
Ok(match &o.kind {
871+
OperandKind::Copy(p) | OperandKind::Move(p) => self.place_ty(p, locals)?,
872+
OperandKind::Constant(c) => c.data(Interner).ty.clone(),
873+
&OperandKind::Static(s) => {
874874
let ty = self.db.infer(s.into())[self.db.body(s.into()).body_expr].clone();
875875
TyKind::Ref(Mutability::Not, static_lifetime(), ty).intern(Interner)
876876
}
@@ -1884,16 +1884,16 @@ impl Evaluator<'_> {
18841884
}
18851885

18861886
fn eval_operand(&mut self, it: &Operand, locals: &mut Locals) -> Result<Interval> {
1887-
Ok(match it {
1888-
Operand::Copy(p) | Operand::Move(p) => {
1887+
Ok(match &it.kind {
1888+
OperandKind::Copy(p) | OperandKind::Move(p) => {
18891889
locals.drop_flags.remove_place(p, &locals.body.projection_store);
18901890
self.eval_place(p, locals)?
18911891
}
1892-
Operand::Static(st) => {
1892+
OperandKind::Static(st) => {
18931893
let addr = self.eval_static(*st, locals)?;
18941894
Interval::new(addr, self.ptr_size())
18951895
}
1896-
Operand::Constant(konst) => self.allocate_const_in_heap(locals, konst)?,
1896+
OperandKind::Constant(konst) => self.allocate_const_in_heap(locals, konst)?,
18971897
})
18981898
}
18991899

crates/hir-ty/src/mir/lower.rs

+46-26
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ use crate::{
5050
utils::ClosureSubst,
5151
};
5252

53+
use super::OperandKind;
54+
5355
mod as_place;
5456
mod pattern_matching;
5557

@@ -329,7 +331,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
329331
let Some((p, current)) = self.lower_expr_as_place(current, expr_id, true)? else {
330332
return Ok(None);
331333
};
332-
Ok(Some((Operand::Copy(p), current)))
334+
Ok(Some((Operand { kind: OperandKind::Copy(p), span: Some(expr_id.into()) }, current)))
333335
}
334336

335337
fn lower_expr_to_place_with_adjust(
@@ -352,7 +354,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
352354
else {
353355
return Ok(None);
354356
};
355-
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
357+
self.push_assignment(
358+
current,
359+
place,
360+
Operand { kind: OperandKind::Copy(p), span: None }.into(),
361+
expr_id.into(),
362+
);
356363
Ok(Some(current))
357364
}
358365
Adjust::Borrow(AutoBorrow::Ref(_, m) | AutoBorrow::RawPtr(m)) => {
@@ -376,7 +383,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
376383
place,
377384
Rvalue::Cast(
378385
CastKind::PointerCoercion(*cast),
379-
Operand::Copy(p),
386+
Operand { kind: OperandKind::Copy(p), span: None },
380387
last.target.clone(),
381388
),
382389
expr_id.into(),
@@ -482,7 +489,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
482489
self.push_assignment(
483490
current,
484491
place,
485-
Operand::Copy(temp).into(),
492+
Operand { kind: OperandKind::Copy(temp), span: None }.into(),
486493
expr_id.into(),
487494
);
488495
Ok(Some(current))
@@ -523,21 +530,24 @@ impl<'ctx> MirLowerCtx<'ctx> {
523530
self.push_assignment(
524531
current,
525532
place,
526-
Operand::Constant(
527-
ConstData {
528-
ty,
529-
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
530-
DebruijnIndex::INNERMOST,
531-
generics.type_or_const_param_idx(p.into()).ok_or(
532-
MirLowerError::TypeError(
533-
"fail to lower const generic param",
534-
),
535-
)?,
536-
)),
537-
}
538-
.intern(Interner),
539-
)
540-
.into(),
533+
Rvalue::from(Operand {
534+
kind: OperandKind::Constant(
535+
ConstData {
536+
ty,
537+
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
538+
DebruijnIndex::INNERMOST,
539+
generics.type_or_const_param_idx(p.into()).ok_or(
540+
MirLowerError::TypeError(
541+
"fail to lower const generic param",
542+
),
543+
)?,
544+
)),
545+
}
546+
.intern(Interner),
547+
)
548+
.into(),
549+
span: None,
550+
}),
541551
expr_id.into(),
542552
);
543553
Ok(Some(current))
@@ -882,7 +892,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
882892
})),
883893
&mut self.result.projection_store,
884894
);
885-
Operand::Copy(p)
895+
Operand { kind: OperandKind::Copy(p), span: None }
886896
}
887897
})
888898
.collect(),
@@ -984,7 +994,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
984994
else {
985995
return Ok(None);
986996
};
987-
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
997+
self.push_assignment(
998+
current,
999+
place,
1000+
Operand { kind: OperandKind::Copy(p), span: None }.into(),
1001+
expr_id.into(),
1002+
);
9881003
Ok(Some(current))
9891004
}
9901005
Expr::UnaryOp {
@@ -1061,8 +1076,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
10611076
else {
10621077
return Ok(None);
10631078
};
1064-
let r_value =
1065-
Rvalue::CheckedBinaryOp(op.into(), Operand::Copy(lhs_place), rhs_op);
1079+
let r_value = Rvalue::CheckedBinaryOp(
1080+
op.into(),
1081+
Operand { kind: OperandKind::Copy(lhs_place), span: None },
1082+
rhs_op,
1083+
);
10661084
self.push_assignment(current, lhs_place, r_value, expr_id.into());
10671085
return Ok(Some(current));
10681086
}
@@ -1237,9 +1255,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
12371255
Rvalue::Ref(*bk, p),
12381256
capture_spans[0],
12391257
);
1240-
operands.push(Operand::Move(tmp));
1258+
operands.push(Operand { kind: OperandKind::Move(tmp), span: None });
1259+
}
1260+
CaptureKind::ByValue => {
1261+
operands.push(Operand { kind: OperandKind::Move(p), span: None })
12411262
}
1242-
CaptureKind::ByValue => operands.push(Operand::Move(p)),
12431263
}
12441264
}
12451265
self.push_assignment(
@@ -1481,7 +1501,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
14811501
.const_eval(const_id, subst, None)
14821502
.map_err(|e| MirLowerError::ConstEvalError(name.into(), Box::new(e)))?
14831503
};
1484-
Ok(Operand::Constant(c))
1504+
Ok(Operand { kind: OperandKind::Constant(c), span: None })
14851505
}
14861506

14871507
fn write_bytes_to_place(

crates/hir-ty/src/mir/lower/as_place.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! MIR lowering for places
22
3-
use crate::mir::MutBorrowKind;
3+
use crate::mir::{MutBorrowKind, Operand, OperandKind};
44

55
use super::*;
66
use hir_def::FunctionId;
@@ -156,7 +156,7 @@ impl MirLowerCtx<'_> {
156156
self.push_assignment(
157157
current,
158158
temp,
159-
Operand::Static(s).into(),
159+
Operand { kind: OperandKind::Static(s), span: None }.into(),
160160
expr_id.into(),
161161
);
162162
Ok(Some((
@@ -306,7 +306,7 @@ impl MirLowerCtx<'_> {
306306
);
307307
let Some(current) = self.lower_call(
308308
index_fn_op,
309-
Box::new([Operand::Copy(place), index_operand]),
309+
Box::new([Operand { kind: OperandKind::Copy(place), span: None }, index_operand]),
310310
result,
311311
current,
312312
false,
@@ -366,7 +366,7 @@ impl MirLowerCtx<'_> {
366366
let mut result: Place = self.temp(target_ty_ref, current, span)?.into();
367367
let Some(current) = self.lower_call(
368368
deref_fn_op,
369-
Box::new([Operand::Copy(ref_place)]),
369+
Box::new([Operand { kind: OperandKind::Copy(ref_place), span: None }]),
370370
result,
371371
current,
372372
false,

0 commit comments

Comments
 (0)