Skip to content

fix: Correct span info for mir::Operand #19247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions crates/hir-ty/src/mir.rs
Original file line number Diff line number Diff line change
@@ -77,7 +77,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
@@ -101,7 +108,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 {
@@ -1076,11 +1089,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() {
22 changes: 11 additions & 11 deletions crates/hir-ty/src/mir/borrowck.rs
Original file line number Diff line number Diff line change
@@ -15,13 +15,13 @@ use crate::{
ClosureId, Interner, Substitution, Ty, TyExt, TypeFlags,
db::{HirDatabase, InternedClosure},
display::DisplayTarget,
mir::Operand,
mir::OperandKind,
utils::ClosureSubst,
};

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)]
@@ -120,8 +120,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) {
@@ -139,10 +139,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_revision_cancelled();
@@ -215,8 +215,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(
@@ -232,7 +232,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_revision_cancelled();
@@ -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);
}
}
18 changes: 9 additions & 9 deletions crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ use crate::{

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

@@ -867,10 +867,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)
}
@@ -1884,16 +1884,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)?,
})
}

71 changes: 45 additions & 26 deletions crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
@@ -50,6 +50,8 @@ use crate::{
utils::ClosureSubst,
};

use super::OperandKind;

mod as_place;
mod pattern_matching;

@@ -329,7 +331,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(
@@ -352,7 +354,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)) => {
@@ -376,7 +383,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(),
@@ -482,7 +489,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))
@@ -523,21 +530,23 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.push_assignment(
current,
place,
Operand::Constant(
ConstData {
ty,
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
DebruijnIndex::INNERMOST,
generics.type_or_const_param_idx(p.into()).ok_or(
MirLowerError::TypeError(
"fail to lower const generic param",
),
)?,
)),
}
.intern(Interner),
)
.into(),
Rvalue::from(Operand {
kind: OperandKind::Constant(
ConstData {
ty,
value: chalk_ir::ConstValue::BoundVar(BoundVar::new(
DebruijnIndex::INNERMOST,
generics.type_or_const_param_idx(p.into()).ok_or(
MirLowerError::TypeError(
"fail to lower const generic param",
),
)?,
)),
}
.intern(Interner),
),
span: None,
}),
expr_id.into(),
);
Ok(Some(current))
@@ -882,7 +891,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
})),
&mut self.result.projection_store,
);
Operand::Copy(p)
Operand { kind: OperandKind::Copy(p), span: None }
}
})
.collect(),
@@ -984,7 +993,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 {
@@ -1061,8 +1075,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));
}
@@ -1237,9 +1254,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(
@@ -1481,7 +1500,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(
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, OperandKind};

use super::*;
use hir_def::FunctionId;
@@ -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((
@@ -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,
@@ -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,
Loading