Skip to content

Commit 1ad31ef

Browse files
authored
Merge pull request #19247 from alibektas/19172_very_new
fix: Correct span info for mir::Operand
2 parents afc7486 + 9f333a6 commit 1ad31ef

File tree

9 files changed

+146
-82
lines changed

9 files changed

+146
-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();
@@ -492,7 +492,7 @@ fn record_usage(local: LocalId, result: &mut ArenaMap<LocalId, MutabilityReason>
492492
}
493493

494494
fn record_usage_for_operand(arg: &Operand, result: &mut ArenaMap<LocalId, MutabilityReason>) {
495-
if let Operand::Copy(p) | Operand::Move(p) = arg {
495+
if let OperandKind::Copy(p) | OperandKind::Move(p) = arg.kind {
496496
record_usage(p.local, result);
497497
}
498498
}

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::{
4747

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

@@ -856,10 +856,10 @@ impl Evaluator<'_> {
856856
}
857857

858858
fn operand_ty(&self, o: &Operand, locals: &Locals) -> Result<Ty> {
859-
Ok(match o {
860-
Operand::Copy(p) | Operand::Move(p) => self.place_ty(p, locals)?,
861-
Operand::Constant(c) => c.data(Interner).ty.clone(),
862-
&Operand::Static(s) => {
859+
Ok(match &o.kind {
860+
OperandKind::Copy(p) | OperandKind::Move(p) => self.place_ty(p, locals)?,
861+
OperandKind::Constant(c) => c.data(Interner).ty.clone(),
862+
&OperandKind::Static(s) => {
863863
let ty = self.db.infer(s.into())[self.db.body(s.into()).body_expr].clone();
864864
TyKind::Ref(Mutability::Not, static_lifetime(), ty).intern(Interner)
865865
}
@@ -1873,16 +1873,16 @@ impl Evaluator<'_> {
18731873
}
18741874

18751875
fn eval_operand(&mut self, it: &Operand, locals: &mut Locals) -> Result<Interval> {
1876-
Ok(match it {
1877-
Operand::Copy(p) | Operand::Move(p) => {
1876+
Ok(match &it.kind {
1877+
OperandKind::Copy(p) | OperandKind::Move(p) => {
18781878
locals.drop_flags.remove_place(p, &locals.body.projection_store);
18791879
self.eval_place(p, locals)?
18801880
}
1881-
Operand::Static(st) => {
1881+
OperandKind::Static(st) => {
18821882
let addr = self.eval_static(*st, locals)?;
18831883
Interval::new(addr, self.ptr_size())
18841884
}
1885-
Operand::Constant(konst) => self.allocate_const_in_heap(locals, konst)?,
1885+
OperandKind::Constant(konst) => self.allocate_const_in_heap(locals, konst)?,
18861886
})
18871887
}
18881888

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

+45-26
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ use crate::{
4848
utils::ClosureSubst,
4949
};
5050

51+
use super::OperandKind;
52+
5153
mod as_place;
5254
mod pattern_matching;
5355

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

330332
fn lower_expr_to_place_with_adjust(
@@ -347,7 +349,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
347349
else {
348350
return Ok(None);
349351
};
350-
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
352+
self.push_assignment(
353+
current,
354+
place,
355+
Operand { kind: OperandKind::Copy(p), span: None }.into(),
356+
expr_id.into(),
357+
);
351358
Ok(Some(current))
352359
}
353360
Adjust::Borrow(AutoBorrow::Ref(_, m) | AutoBorrow::RawPtr(m)) => {
@@ -371,7 +378,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
371378
place,
372379
Rvalue::Cast(
373380
CastKind::PointerCoercion(*cast),
374-
Operand::Copy(p),
381+
Operand { kind: OperandKind::Copy(p), span: None },
375382
last.target.clone(),
376383
),
377384
expr_id.into(),
@@ -476,7 +483,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
476483
self.push_assignment(
477484
current,
478485
place,
479-
Operand::Copy(temp).into(),
486+
Operand { kind: OperandKind::Copy(temp), span: None }.into(),
480487
expr_id.into(),
481488
);
482489
Ok(Some(current))
@@ -517,21 +524,23 @@ impl<'ctx> MirLowerCtx<'ctx> {
517524
self.push_assignment(
518525
current,
519526
place,
520-
Operand::Constant(
521-
ConstData {
522-
ty,
523-
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
524-
DebruijnIndex::INNERMOST,
525-
generics.type_or_const_param_idx(p.into()).ok_or(
526-
MirLowerError::TypeError(
527-
"fail to lower const generic param",
528-
),
529-
)?,
530-
)),
531-
}
532-
.intern(Interner),
533-
)
534-
.into(),
527+
Rvalue::from(Operand {
528+
kind: OperandKind::Constant(
529+
ConstData {
530+
ty,
531+
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
532+
DebruijnIndex::INNERMOST,
533+
generics.type_or_const_param_idx(p.into()).ok_or(
534+
MirLowerError::TypeError(
535+
"fail to lower const generic param",
536+
),
537+
)?,
538+
)),
539+
}
540+
.intern(Interner),
541+
),
542+
span: None,
543+
}),
535544
expr_id.into(),
536545
);
537546
Ok(Some(current))
@@ -876,7 +885,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
876885
})),
877886
&mut self.result.projection_store,
878887
);
879-
Operand::Copy(p)
888+
Operand { kind: OperandKind::Copy(p), span: None }
880889
}
881890
})
882891
.collect(),
@@ -979,7 +988,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
979988
else {
980989
return Ok(None);
981990
};
982-
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
991+
self.push_assignment(
992+
current,
993+
place,
994+
Operand { kind: OperandKind::Copy(p), span: None }.into(),
995+
expr_id.into(),
996+
);
983997
Ok(Some(current))
984998
}
985999
Expr::UnaryOp {
@@ -1056,8 +1070,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
10561070
else {
10571071
return Ok(None);
10581072
};
1059-
let r_value =
1060-
Rvalue::CheckedBinaryOp(op.into(), Operand::Copy(lhs_place), rhs_op);
1073+
let r_value = Rvalue::CheckedBinaryOp(
1074+
op.into(),
1075+
Operand { kind: OperandKind::Copy(lhs_place), span: None },
1076+
rhs_op,
1077+
);
10611078
self.push_assignment(current, lhs_place, r_value, expr_id.into());
10621079
return Ok(Some(current));
10631080
}
@@ -1232,9 +1249,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
12321249
Rvalue::Ref(*bk, p),
12331250
capture_spans[0],
12341251
);
1235-
operands.push(Operand::Move(tmp));
1252+
operands.push(Operand { kind: OperandKind::Move(tmp), span: None });
1253+
}
1254+
CaptureKind::ByValue => {
1255+
operands.push(Operand { kind: OperandKind::Move(p), span: None })
12361256
}
1237-
CaptureKind::ByValue => operands.push(Operand::Move(p)),
12381257
}
12391258
}
12401259
self.push_assignment(
@@ -1476,7 +1495,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
14761495
.const_eval(const_id, subst, None)
14771496
.map_err(|e| MirLowerError::ConstEvalError(name.into(), Box::new(e)))?
14781497
};
1479-
Ok(Operand::Constant(c))
1498+
Ok(Operand { kind: OperandKind::Constant(c), span: None })
14801499
}
14811500

14821501
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;
@@ -155,7 +155,7 @@ impl MirLowerCtx<'_> {
155155
self.push_assignment(
156156
current,
157157
temp,
158-
Operand::Static(s).into(),
158+
Operand { kind: OperandKind::Static(s), span: None }.into(),
159159
expr_id.into(),
160160
);
161161
Ok(Some((
@@ -305,7 +305,7 @@ impl MirLowerCtx<'_> {
305305
);
306306
let Some(current) = self.lower_call(
307307
index_fn_op,
308-
Box::new([Operand::Copy(place), index_operand]),
308+
Box::new([Operand { kind: OperandKind::Copy(place), span: None }, index_operand]),
309309
result,
310310
current,
311311
false,
@@ -365,7 +365,7 @@ impl MirLowerCtx<'_> {
365365
let mut result: Place = self.temp(target_ty_ref, current, span)?.into();
366366
let Some(current) = self.lower_call(
367367
deref_fn_op,
368-
Box::new([Operand::Copy(ref_place)]),
368+
Box::new([Operand { kind: OperandKind::Copy(ref_place), span: None }]),
369369
result,
370370
current,
371371
false,

0 commit comments

Comments
 (0)