Skip to content

Commit 440bfa0

Browse files
committed
Fix span info for mir::Operand
Fixes #19172
1 parent fe3eda7 commit 440bfa0

File tree

9 files changed

+149
-84
lines changed

9 files changed

+149
-84
lines changed

crates/hir-ty/src/mir.rs

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

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

106119
fn from_bytes(data: Box<[u8]>, ty: Ty) -> Self {
@@ -1075,11 +1088,11 @@ impl MirBody {
10751088
f: &mut impl FnMut(&mut Place, &mut ProjectionStore),
10761089
store: &mut ProjectionStore,
10771090
) {
1078-
match op {
1079-
Operand::Copy(p) | Operand::Move(p) => {
1091+
match &mut op.kind {
1092+
OperandKind::Copy(p) | OperandKind::Move(p) => {
10801093
f(p, store);
10811094
}
1082-
Operand::Constant(_) | Operand::Static(_) => (),
1095+
OperandKind::Constant(_) | OperandKind::Static(_) => (),
10831096
}
10841097
}
10851098
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
@@ -13,14 +13,14 @@ use triomphe::Arc;
1313

1414
use crate::{
1515
db::{HirDatabase, InternedClosure},
16-
mir::Operand,
16+
mir::OperandKind,
1717
utils::ClosureSubst,
1818
ClosureId, Interner, Substitution, Ty, TyExt, TypeFlags,
1919
};
2020

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

2626
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -119,8 +119,8 @@ fn make_fetch_closure_field(
119119

120120
fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef> {
121121
let mut result = vec![];
122-
let mut for_operand = |op: &Operand, span: MirSpan| match op {
123-
Operand::Copy(p) | Operand::Move(p) => {
122+
let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
123+
OperandKind::Copy(p) | OperandKind::Move(p) => {
124124
let mut ty: Ty = body.locals[p.local].ty.clone();
125125
let mut is_dereference_of_ref = false;
126126
for proj in p.projection.lookup(&body.projection_store) {
@@ -138,10 +138,10 @@ fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef>
138138
&& !ty.clone().is_copy(db, body.owner)
139139
&& !ty.data(Interner).flags.intersects(TypeFlags::HAS_ERROR)
140140
{
141-
result.push(MovedOutOfRef { span, ty });
141+
result.push(MovedOutOfRef { span: op.span.unwrap_or(span), ty });
142142
}
143143
}
144-
Operand::Constant(_) | Operand::Static(_) => (),
144+
OperandKind::Constant(_) | OperandKind::Static(_) => (),
145145
};
146146
for (_, block) in body.basic_blocks.iter() {
147147
db.unwind_if_cancelled();
@@ -214,8 +214,8 @@ fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef>
214214

215215
fn partially_moved(db: &dyn HirDatabase, body: &MirBody) -> Vec<PartiallyMoved> {
216216
let mut result = vec![];
217-
let mut for_operand = |op: &Operand, span: MirSpan| match op {
218-
Operand::Copy(p) | Operand::Move(p) => {
217+
let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
218+
OperandKind::Copy(p) | OperandKind::Move(p) => {
219219
let mut ty: Ty = body.locals[p.local].ty.clone();
220220
for proj in p.projection.lookup(&body.projection_store) {
221221
ty = proj.projected_ty(
@@ -231,7 +231,7 @@ fn partially_moved(db: &dyn HirDatabase, body: &MirBody) -> Vec<PartiallyMoved>
231231
result.push(PartiallyMoved { span, ty, local: p.local });
232232
}
233233
}
234-
Operand::Constant(_) | Operand::Static(_) => (),
234+
OperandKind::Constant(_) | OperandKind::Static(_) => (),
235235
};
236236
for (_, block) in body.basic_blocks.iter() {
237237
db.unwind_if_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

+10-10
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ use crate::{
4646

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

5353
mod shim;
@@ -864,10 +864,10 @@ impl Evaluator<'_> {
864864
}
865865

866866
fn operand_ty(&self, o: &Operand, locals: &Locals) -> Result<Ty> {
867-
Ok(match o {
868-
Operand::Copy(p) | Operand::Move(p) => self.place_ty(p, locals)?,
869-
Operand::Constant(c) => c.data(Interner).ty.clone(),
870-
&Operand::Static(s) => {
867+
Ok(match &o.kind {
868+
OperandKind::Copy(p) | OperandKind::Move(p) => self.place_ty(p, locals)?,
869+
OperandKind::Constant(c) => c.data(Interner).ty.clone(),
870+
&OperandKind::Static(s) => {
871871
let ty = self.db.infer(s.into())[self.db.body(s.into()).body_expr].clone();
872872
TyKind::Ref(Mutability::Not, static_lifetime(), ty).intern(Interner)
873873
}
@@ -1880,16 +1880,16 @@ impl Evaluator<'_> {
18801880
}
18811881

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

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

+46-26
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939
mir::{
4040
intern_const_scalar, return_slot, AggregateKind, Arena, BasicBlock, BasicBlockId, BinOp,
4141
BorrowKind, CastKind, ClosureId, ConstScalar, Either, Expr, FieldId, Idx, InferenceResult,
42-
Interner, Local, LocalId, MemoryMap, MirBody, MirSpan, Mutability, Operand, Place,
42+
Interner, Local, LocalId, MemoryMap, MirBody, MirSpan, Mutability, OperandKind, Place,
4343
PlaceElem, PointerCast, ProjectionElem, ProjectionStore, RawIdx, Rvalue, Statement,
4444
StatementKind, Substitution, SwitchTargets, Terminator, TerminatorKind, TupleFieldId, Ty,
4545
UnOp, VariantId,
@@ -50,6 +50,8 @@ use crate::{
5050
Adjust, Adjustment, AutoBorrow, CallableDefId, TyBuilder, TyExt,
5151
};
5252

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

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

332334
fn lower_expr_to_place_with_adjust(
@@ -349,7 +351,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
349351
else {
350352
return Ok(None);
351353
};
352-
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
354+
self.push_assignment(
355+
current,
356+
place,
357+
Operand { kind: OperandKind::Copy(p), span: None }.into(),
358+
expr_id.into(),
359+
);
353360
Ok(Some(current))
354361
}
355362
Adjust::Borrow(AutoBorrow::Ref(_, m) | AutoBorrow::RawPtr(m)) => {
@@ -373,7 +380,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
373380
place,
374381
Rvalue::Cast(
375382
CastKind::PointerCoercion(*cast),
376-
Operand::Copy(p),
383+
Operand { kind: OperandKind::Copy(p), span: None },
377384
last.target.clone(),
378385
),
379386
expr_id.into(),
@@ -479,7 +486,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
479486
self.push_assignment(
480487
current,
481488
place,
482-
Operand::Copy(temp).into(),
489+
Operand { kind: OperandKind::Copy(temp), span: None }.into(),
483490
expr_id.into(),
484491
);
485492
Ok(Some(current))
@@ -520,20 +527,23 @@ impl<'ctx> MirLowerCtx<'ctx> {
520527
self.push_assignment(
521528
current,
522529
place,
523-
Operand::Constant(
524-
ConstData {
525-
ty,
526-
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
527-
DebruijnIndex::INNERMOST,
528-
gen.type_or_const_param_idx(p.into()).ok_or(
529-
MirLowerError::TypeError(
530-
"fail to lower const generic param",
531-
),
532-
)?,
533-
)),
534-
}
535-
.intern(Interner),
536-
)
530+
Operand {
531+
kind: OperandKind::Constant(
532+
ConstData {
533+
ty,
534+
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
535+
DebruijnIndex::INNERMOST,
536+
gen.type_or_const_param_idx(p.into()).ok_or(
537+
MirLowerError::TypeError(
538+
"fail to lower const generic param",
539+
),
540+
)?,
541+
)),
542+
}
543+
.intern(Interner),
544+
),
545+
span: None,
546+
}
537547
.into(),
538548
expr_id.into(),
539549
);
@@ -879,7 +889,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
879889
})),
880890
&mut self.result.projection_store,
881891
);
882-
Operand::Copy(p)
892+
Operand { kind: OperandKind::Copy(p), span: None }
883893
}
884894
})
885895
.collect(),
@@ -981,7 +991,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
981991
else {
982992
return Ok(None);
983993
};
984-
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
994+
self.push_assignment(
995+
current,
996+
place,
997+
Operand { kind: OperandKind::Copy(p), span: None }.into(),
998+
expr_id.into(),
999+
);
9851000
Ok(Some(current))
9861001
}
9871002
Expr::UnaryOp {
@@ -1058,8 +1073,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
10581073
else {
10591074
return Ok(None);
10601075
};
1061-
let r_value =
1062-
Rvalue::CheckedBinaryOp(op.into(), Operand::Copy(lhs_place), rhs_op);
1076+
let r_value = Rvalue::CheckedBinaryOp(
1077+
op.into(),
1078+
Operand { kind: OperandKind::Copy(lhs_place), span: None },
1079+
rhs_op,
1080+
);
10631081
self.push_assignment(current, lhs_place, r_value, expr_id.into());
10641082
return Ok(Some(current));
10651083
}
@@ -1235,9 +1253,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
12351253
Rvalue::Ref(*bk, p),
12361254
capture_spans[0],
12371255
);
1238-
operands.push(Operand::Move(tmp));
1256+
operands.push(Operand { kind: OperandKind::Move(tmp), span: None });
1257+
}
1258+
CaptureKind::ByValue => {
1259+
operands.push(Operand { kind: OperandKind::Move(p), span: None })
12391260
}
1240-
CaptureKind::ByValue => operands.push(Operand::Move(p)),
12411261
}
12421262
}
12431263
self.push_assignment(
@@ -1472,7 +1492,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
14721492
.const_eval(const_id, subst, None)
14731493
.map_err(|e| MirLowerError::ConstEvalError(name.into(), Box::new(e)))?
14741494
};
1475-
Ok(Operand::Constant(c))
1495+
Ok(Operand { kind: OperandKind::Constant(c), span: None })
14761496
}
14771497

14781498
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};
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)