Skip to content

Commit

Permalink
Fix span info for mir::Operand
Browse files Browse the repository at this point in the history
  • Loading branch information
alibektas committed Feb 27, 2025
1 parent fe3eda7 commit 440bfa0
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 84 deletions.
23 changes: 18 additions & 5 deletions crates/hir-ty/src/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ pub struct Local {
/// currently implements it, but it seems like this may be something to check against in the
/// validator.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Operand {
pub struct Operand {
kind: OperandKind,
// FIXME : This should actually just be of type `MirSpan`.
span: Option<MirSpan>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum OperandKind {
/// Creates a value by loading the given place.
///
/// Before drop elaboration, the type of the place must be `Copy`. After drop elaboration there
Expand All @@ -100,7 +107,13 @@ pub enum Operand {

impl Operand {
fn from_concrete_const(data: Box<[u8]>, memory_map: MemoryMap, ty: Ty) -> Self {
Operand::Constant(intern_const_scalar(ConstScalar::Bytes(data, memory_map), ty))
Operand {
kind: OperandKind::Constant(intern_const_scalar(
ConstScalar::Bytes(data, memory_map),
ty,
)),
span: None,
}
}

fn from_bytes(data: Box<[u8]>, ty: Ty) -> Self {
Expand Down Expand Up @@ -1075,11 +1088,11 @@ impl MirBody {
f: &mut impl FnMut(&mut Place, &mut ProjectionStore),
store: &mut ProjectionStore,
) {
match op {
Operand::Copy(p) | Operand::Move(p) => {
match &mut op.kind {
OperandKind::Copy(p) | OperandKind::Move(p) => {
f(p, store);
}
Operand::Constant(_) | Operand::Static(_) => (),
OperandKind::Constant(_) | OperandKind::Static(_) => (),
}
}
for (_, block) in self.basic_blocks.iter_mut() {
Expand Down
22 changes: 11 additions & 11 deletions crates/hir-ty/src/mir/borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use triomphe::Arc;

use crate::{
db::{HirDatabase, InternedClosure},
mir::Operand,
mir::OperandKind,
utils::ClosureSubst,
ClosureId, Interner, Substitution, Ty, TyExt, TypeFlags,
};

use super::{
BasicBlockId, BorrowKind, LocalId, MirBody, MirLowerError, MirSpan, MutBorrowKind, Place,
ProjectionElem, Rvalue, StatementKind, TerminatorKind,
BasicBlockId, BorrowKind, LocalId, MirBody, MirLowerError, MirSpan, MutBorrowKind, Operand,
Place, ProjectionElem, Rvalue, StatementKind, TerminatorKind,
};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -119,8 +119,8 @@ fn make_fetch_closure_field(

fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef> {
let mut result = vec![];
let mut for_operand = |op: &Operand, span: MirSpan| match op {
Operand::Copy(p) | Operand::Move(p) => {
let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
OperandKind::Copy(p) | OperandKind::Move(p) => {
let mut ty: Ty = body.locals[p.local].ty.clone();
let mut is_dereference_of_ref = false;
for proj in p.projection.lookup(&body.projection_store) {
Expand All @@ -138,10 +138,10 @@ fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef>
&& !ty.clone().is_copy(db, body.owner)
&& !ty.data(Interner).flags.intersects(TypeFlags::HAS_ERROR)
{
result.push(MovedOutOfRef { span, ty });
result.push(MovedOutOfRef { span: op.span.unwrap_or(span), ty });
}
}
Operand::Constant(_) | Operand::Static(_) => (),
OperandKind::Constant(_) | OperandKind::Static(_) => (),
};
for (_, block) in body.basic_blocks.iter() {
db.unwind_if_cancelled();
Expand Down Expand Up @@ -214,8 +214,8 @@ fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef>

fn partially_moved(db: &dyn HirDatabase, body: &MirBody) -> Vec<PartiallyMoved> {
let mut result = vec![];
let mut for_operand = |op: &Operand, span: MirSpan| match op {
Operand::Copy(p) | Operand::Move(p) => {
let mut for_operand = |op: &Operand, span: MirSpan| match op.kind {
OperandKind::Copy(p) | OperandKind::Move(p) => {
let mut ty: Ty = body.locals[p.local].ty.clone();
for proj in p.projection.lookup(&body.projection_store) {
ty = proj.projected_ty(
Expand All @@ -231,7 +231,7 @@ fn partially_moved(db: &dyn HirDatabase, body: &MirBody) -> Vec<PartiallyMoved>
result.push(PartiallyMoved { span, ty, local: p.local });
}
}
Operand::Constant(_) | Operand::Static(_) => (),
OperandKind::Constant(_) | OperandKind::Static(_) => (),
};
for (_, block) in body.basic_blocks.iter() {
db.unwind_if_cancelled();
Expand Down Expand Up @@ -500,7 +500,7 @@ fn record_usage(local: LocalId, result: &mut ArenaMap<LocalId, MutabilityReason>
}

fn record_usage_for_operand(arg: &Operand, result: &mut ArenaMap<LocalId, MutabilityReason>) {
if let Operand::Copy(p) | Operand::Move(p) = arg {
if let OperandKind::Copy(p) | OperandKind::Move(p) = arg.kind {
record_usage(p.local, result);
}
}
Expand Down
20 changes: 10 additions & 10 deletions crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ use crate::{

use super::{
return_slot, AggregateKind, BasicBlockId, BinOp, CastKind, LocalId, MirBody, MirLowerError,
MirSpan, Operand, Place, PlaceElem, ProjectionElem, ProjectionStore, Rvalue, StatementKind,
TerminatorKind, UnOp,
MirSpan, Operand, OperandKind, Place, PlaceElem, ProjectionElem, ProjectionStore, Rvalue,
StatementKind, TerminatorKind, UnOp,
};

mod shim;
Expand Down Expand Up @@ -864,10 +864,10 @@ impl Evaluator<'_> {
}

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

fn eval_operand(&mut self, it: &Operand, locals: &mut Locals) -> Result<Interval> {
Ok(match it {
Operand::Copy(p) | Operand::Move(p) => {
Ok(match &it.kind {
OperandKind::Copy(p) | OperandKind::Move(p) => {
locals.drop_flags.remove_place(p, &locals.body.projection_store);
self.eval_place(p, locals)?
}
Operand::Static(st) => {
OperandKind::Static(st) => {
let addr = self.eval_static(*st, locals)?;
Interval::new(addr, self.ptr_size())
}
Operand::Constant(konst) => self.allocate_const_in_heap(locals, konst)?,
OperandKind::Constant(konst) => self.allocate_const_in_heap(locals, konst)?,
})
}

Expand Down
72 changes: 46 additions & 26 deletions crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
mir::{
intern_const_scalar, return_slot, AggregateKind, Arena, BasicBlock, BasicBlockId, BinOp,
BorrowKind, CastKind, ClosureId, ConstScalar, Either, Expr, FieldId, Idx, InferenceResult,
Interner, Local, LocalId, MemoryMap, MirBody, MirSpan, Mutability, Operand, Place,
Interner, Local, LocalId, MemoryMap, MirBody, MirSpan, Mutability, OperandKind, Place,
PlaceElem, PointerCast, ProjectionElem, ProjectionStore, RawIdx, Rvalue, Statement,
StatementKind, Substitution, SwitchTargets, Terminator, TerminatorKind, TupleFieldId, Ty,
UnOp, VariantId,
Expand All @@ -50,6 +50,8 @@ use crate::{
Adjust, Adjustment, AutoBorrow, CallableDefId, TyBuilder, TyExt,
};

use super::Operand;

mod as_place;
mod pattern_matching;

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

fn lower_expr_to_place_with_adjust(
Expand All @@ -349,7 +351,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
else {
return Ok(None);
};
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
self.push_assignment(
current,
place,
Operand { kind: OperandKind::Copy(p), span: None }.into(),
expr_id.into(),
);
Ok(Some(current))
}
Adjust::Borrow(AutoBorrow::Ref(_, m) | AutoBorrow::RawPtr(m)) => {
Expand All @@ -373,7 +380,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
place,
Rvalue::Cast(
CastKind::PointerCoercion(*cast),
Operand::Copy(p),
Operand { kind: OperandKind::Copy(p), span: None },
last.target.clone(),
),
expr_id.into(),
Expand Down Expand Up @@ -479,7 +486,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.push_assignment(
current,
place,
Operand::Copy(temp).into(),
Operand { kind: OperandKind::Copy(temp), span: None }.into(),
expr_id.into(),
);
Ok(Some(current))
Expand Down Expand Up @@ -520,20 +527,23 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.push_assignment(
current,
place,
Operand::Constant(
ConstData {
ty,
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
DebruijnIndex::INNERMOST,
gen.type_or_const_param_idx(p.into()).ok_or(
MirLowerError::TypeError(
"fail to lower const generic param",
),
)?,
)),
}
.intern(Interner),
)
Operand {
kind: OperandKind::Constant(
ConstData {
ty,
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
DebruijnIndex::INNERMOST,
gen.type_or_const_param_idx(p.into()).ok_or(
MirLowerError::TypeError(
"fail to lower const generic param",
),
)?,
)),
}
.intern(Interner),
),
span: None,
}
.into(),
expr_id.into(),
);
Expand Down Expand Up @@ -879,7 +889,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
})),
&mut self.result.projection_store,
);
Operand::Copy(p)
Operand { kind: OperandKind::Copy(p), span: None }
}
})
.collect(),
Expand Down Expand Up @@ -981,7 +991,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
else {
return Ok(None);
};
self.push_assignment(current, place, Operand::Copy(p).into(), expr_id.into());
self.push_assignment(
current,
place,
Operand { kind: OperandKind::Copy(p), span: None }.into(),
expr_id.into(),
);
Ok(Some(current))
}
Expr::UnaryOp {
Expand Down Expand Up @@ -1058,8 +1073,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
else {
return Ok(None);
};
let r_value =
Rvalue::CheckedBinaryOp(op.into(), Operand::Copy(lhs_place), rhs_op);
let r_value = Rvalue::CheckedBinaryOp(
op.into(),
Operand { kind: OperandKind::Copy(lhs_place), span: None },
rhs_op,
);
self.push_assignment(current, lhs_place, r_value, expr_id.into());
return Ok(Some(current));
}
Expand Down Expand Up @@ -1235,9 +1253,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
Rvalue::Ref(*bk, p),
capture_spans[0],
);
operands.push(Operand::Move(tmp));
operands.push(Operand { kind: OperandKind::Move(tmp), span: None });
}
CaptureKind::ByValue => {
operands.push(Operand { kind: OperandKind::Move(p), span: None })
}
CaptureKind::ByValue => operands.push(Operand::Move(p)),
}
}
self.push_assignment(
Expand Down Expand Up @@ -1472,7 +1492,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
.const_eval(const_id, subst, None)
.map_err(|e| MirLowerError::ConstEvalError(name.into(), Box::new(e)))?
};
Ok(Operand::Constant(c))
Ok(Operand { kind: OperandKind::Constant(c), span: None })
}

fn write_bytes_to_place(
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-ty/src/mir/lower/as_place.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! MIR lowering for places
use crate::mir::MutBorrowKind;
use crate::mir::{MutBorrowKind, Operand};

use super::*;
use hir_def::FunctionId;
Expand Down Expand Up @@ -156,7 +156,7 @@ impl MirLowerCtx<'_> {
self.push_assignment(
current,
temp,
Operand::Static(s).into(),
Operand { kind: OperandKind::Static(s), span: None }.into(),
expr_id.into(),
);
Ok(Some((
Expand Down Expand Up @@ -306,7 +306,7 @@ impl MirLowerCtx<'_> {
);
let Some(current) = self.lower_call(
index_fn_op,
Box::new([Operand::Copy(place), index_operand]),
Box::new([Operand { kind: OperandKind::Copy(place), span: None }, index_operand]),
result,
current,
false,
Expand Down Expand Up @@ -366,7 +366,7 @@ impl MirLowerCtx<'_> {
let mut result: Place = self.temp(target_ty_ref, current, span)?.into();
let Some(current) = self.lower_call(
deref_fn_op,
Box::new([Operand::Copy(ref_place)]),
Box::new([Operand { kind: OperandKind::Copy(ref_place), span: None }]),
result,
current,
false,
Expand Down
Loading

0 comments on commit 440bfa0

Please sign in to comment.