Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/mir/eval.rs')
| -rw-r--r-- | crates/hir-ty/src/mir/eval.rs | 136 |
1 files changed, 58 insertions, 78 deletions
diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs index 551446962d..f8e4c6e43d 100644 --- a/crates/hir-ty/src/mir/eval.rs +++ b/crates/hir-ty/src/mir/eval.rs @@ -291,12 +291,11 @@ impl Address { } fn to_usize(&self) -> usize { - let as_num = match self { + match self { Stack(it) => *it + STACK_OFFSET, Heap(it) => *it + HEAP_OFFSET, Invalid(it) => *it, - }; - as_num + } } fn map(&self, f: impl FnOnce(usize) -> usize) -> Address { @@ -485,8 +484,7 @@ impl DropFlags { fn remove_place(&mut self, p: &Place, store: &ProjectionStore) -> bool { // FIXME: replace parents with parts - if let Some(parent) = p.iterate_over_parents(store).find(|it| self.need_drop.contains(&it)) - { + if let Some(parent) = p.iterate_over_parents(store).find(|it| self.need_drop.contains(it)) { self.need_drop.remove(&parent); return true; } @@ -551,7 +549,7 @@ pub fn interpret_mir( memory_map.vtable.shrink_to_fit(); MemoryMap::Complex(Box::new(memory_map)) }; - return Ok(intern_const_scalar(ConstScalar::Bytes(bytes, memory_map), ty)); + Ok(intern_const_scalar(ConstScalar::Bytes(bytes, memory_map), ty)) })(); (it, MirOutput { stdout: evaluator.stdout, stderr: evaluator.stderr }) } @@ -562,12 +560,12 @@ const EXECUTION_LIMIT: usize = 100_000; const EXECUTION_LIMIT: usize = 10_000_000; impl Evaluator<'_> { - pub fn new<'a>( - db: &'a dyn HirDatabase, + pub fn new( + db: &dyn HirDatabase, owner: DefWithBodyId, assert_placeholder_ty_is_unused: bool, trait_env: Option<Arc<TraitEnvironment>>, - ) -> Evaluator<'a> { + ) -> Evaluator<'_> { let crate_id = owner.module(db.upcast()).krate(); Evaluator { stack: vec![0], @@ -585,7 +583,7 @@ impl Evaluator<'_> { assert_placeholder_ty_is_unused, stack_depth_limit: 100, execution_limit: EXECUTION_LIMIT, - memory_limit: 1000_000_000, // 2GB, 1GB for stack and 1GB for heap + memory_limit: 1_000_000_000, // 2GB, 1GB for stack and 1GB for heap layout_cache: RefCell::new(Default::default()), projected_ty_cache: RefCell::new(Default::default()), not_special_fn_cache: RefCell::new(Default::default()), @@ -816,8 +814,8 @@ impl Evaluator<'_> { }) } - fn interpret_mir<'slf>( - &'slf mut self, + fn interpret_mir( + &mut self, body: Arc<MirBody>, args: impl Iterator<Item = IntervalOrOwned>, ) -> Result<Interval> { @@ -837,7 +835,7 @@ impl Evaluator<'_> { not_supported!("missing stack frame"); }; let e = (|| { - let mut locals = &mut my_stack_frame.locals; + let locals = &mut my_stack_frame.locals; let body = locals.body.clone(); loop { let current_block = &body.basic_blocks[current_block_idx]; @@ -849,12 +847,10 @@ impl Evaluator<'_> { for statement in ¤t_block.statements { match &statement.kind { StatementKind::Assign(l, r) => { - let addr = self.place_addr(l, &locals)?; - let result = self.eval_rvalue(r, &mut locals)?; + let addr = self.place_addr(l, locals)?; + let result = self.eval_rvalue(r, locals)?; self.copy_from_interval_or_owned(addr, result)?; - locals - .drop_flags - .add_place(l.clone(), &locals.body.projection_store); + locals.drop_flags.add_place(*l, &locals.body.projection_store); } StatementKind::Deinit(_) => not_supported!("de-init statement"), StatementKind::StorageLive(_) @@ -878,20 +874,20 @@ impl Evaluator<'_> { cleanup: _, from_hir_call: _, } => { - let destination_interval = self.place_interval(destination, &locals)?; - let fn_ty = self.operand_ty(func, &locals)?; + let destination_interval = self.place_interval(destination, locals)?; + let fn_ty = self.operand_ty(func, locals)?; let args = args .iter() - .map(|it| self.operand_ty_and_eval(it, &mut locals)) + .map(|it| self.operand_ty_and_eval(it, locals)) .collect::<Result<Vec<_>>>()?; let stack_frame = match &fn_ty.kind(Interner) { TyKind::Function(_) => { - let bytes = self.eval_operand(func, &mut locals)?; + let bytes = self.eval_operand(func, locals)?; self.exec_fn_pointer( bytes, destination_interval, &args, - &locals, + locals, *target, terminator.span, )? @@ -901,7 +897,7 @@ impl Evaluator<'_> { generic_args, destination_interval, &args, - &locals, + locals, *target, terminator.span, )?, @@ -909,7 +905,7 @@ impl Evaluator<'_> { }; locals .drop_flags - .add_place(destination.clone(), &locals.body.projection_store); + .add_place(*destination, &locals.body.projection_store); if let Some(stack_frame) = stack_frame { self.code_stack.push(my_stack_frame); current_block_idx = stack_frame.locals.body.start_block; @@ -924,7 +920,7 @@ impl Evaluator<'_> { } TerminatorKind::SwitchInt { discr, targets } => { let val = u128::from_le_bytes(pad16( - self.eval_operand(discr, &mut locals)?.get(&self)?, + self.eval_operand(discr, locals)?.get(self)?, false, )); current_block_idx = targets.target_for_value(val); @@ -938,7 +934,7 @@ impl Evaluator<'_> { )); } TerminatorKind::Drop { place, target, unwind: _ } => { - self.drop_place(place, &mut locals, terminator.span)?; + self.drop_place(place, locals, terminator.span)?; current_block_idx = *target; } _ => not_supported!("unknown terminator"), @@ -1081,7 +1077,7 @@ impl Evaluator<'_> { } } Rvalue::UnaryOp(op, val) => { - let mut c = self.eval_operand(val, locals)?.get(&self)?; + let mut c = self.eval_operand(val, locals)?.get(self)?; let mut ty = self.operand_ty(val, locals)?; while let TyKind::Ref(_, _, z) = ty.kind(Interner) { ty = z.clone(); @@ -1124,8 +1120,8 @@ impl Evaluator<'_> { Rvalue::CheckedBinaryOp(op, lhs, rhs) => 'binary_op: { let lc = self.eval_operand(lhs, locals)?; let rc = self.eval_operand(rhs, locals)?; - let mut lc = lc.get(&self)?; - let mut rc = rc.get(&self)?; + let mut lc = lc.get(self)?; + let mut rc = rc.get(self)?; let mut ty = self.operand_ty(lhs, locals)?; while let TyKind::Ref(_, _, z) = ty.kind(Interner) { ty = z.clone(); @@ -1277,12 +1273,12 @@ impl Evaluator<'_> { } Rvalue::Discriminant(p) => { let ty = self.place_ty(p, locals)?; - let bytes = self.eval_place(p, locals)?.get(&self)?; + let bytes = self.eval_place(p, locals)?.get(self)?; let result = self.compute_discriminant(ty, bytes)?; Owned(result.to_le_bytes().to_vec()) } Rvalue::Repeat(it, len) => { - let len = match try_const_usize(self.db, &len) { + let len = match try_const_usize(self.db, len) { Some(it) => it as usize, None => not_supported!("non evaluatable array len in repeat Rvalue"), }; @@ -1308,13 +1304,13 @@ impl Evaluator<'_> { AggregateKind::Array(_) => { let mut r = vec![]; for it in values { - let value = it.get(&self)?; + let value = it.get(self)?; r.extend(value); } Owned(r) } AggregateKind::Tuple(ty) => { - let layout = self.layout(&ty)?; + let layout = self.layout(ty)?; Owned(self.make_by_layout( layout.size.bytes_usize(), &layout, @@ -1329,7 +1325,7 @@ impl Evaluator<'_> { .fields .offset(u32::from(f.local_id.into_raw()) as usize) .bytes_usize(); - let op = values[0].get(&self)?; + let op = values[0].get(self)?; let mut result = vec![0; layout.size.bytes_usize()]; result[offset..offset + op.len()].copy_from_slice(op); Owned(result) @@ -1345,7 +1341,7 @@ impl Evaluator<'_> { )?) } AggregateKind::Closure(ty) => { - let layout = self.layout(&ty)?; + let layout = self.layout(ty)?; Owned(self.make_by_layout( layout.size.bytes_usize(), &layout, @@ -1397,7 +1393,7 @@ impl Evaluator<'_> { }, _ => false, }; - let current = pad16(self.eval_operand(operand, locals)?.get(&self)?, is_signed); + let current = pad16(self.eval_operand(operand, locals)?.get(self)?, is_signed); let dest_size = self.size_of_sized(target_ty, locals, "destination of int to int cast")?; Owned(current[0..dest_size].to_vec()) @@ -1513,7 +1509,7 @@ impl Evaluator<'_> { let mut r = Vec::with_capacity(16); let addr = addr.get(self)?; r.extend(addr.iter().copied()); - r.extend(len.to_le_bytes().into_iter()); + r.extend(len.to_le_bytes()); Owned(r) } t => { @@ -1525,7 +1521,7 @@ impl Evaluator<'_> { let mut r = Vec::with_capacity(16); let addr = addr.get(self)?; r.extend(addr.iter().copied()); - r.extend(vtable.to_le_bytes().into_iter()); + r.extend(vtable.to_le_bytes()); Owned(r) } TyKind::Adt(id, target_subst) => match ¤t_ty.kind(Interner) { @@ -1539,7 +1535,7 @@ impl Evaluator<'_> { AdtId::EnumId(_) => not_supported!("unsizing enums"), }; let Some((last_field, _)) = - self.db.struct_data(id).variant_data.fields().iter().rev().next() + self.db.struct_data(id).variant_data.fields().iter().next_back() else { not_supported!("unsizing struct without field"); }; @@ -1645,7 +1641,7 @@ impl Evaluator<'_> { } for (i, op) in values.enumerate() { let offset = variant_layout.fields.offset(i).bytes_usize(); - let op = op.get(&self)?; + let op = op.get(self)?; match result.get_mut(offset..offset + op.len()) { Some(it) => it.copy_from_slice(op), None => return Err(MirEvalError::BrokenLayout(Box::new(variant_layout.clone()))), @@ -1686,7 +1682,7 @@ impl Evaluator<'_> { } result_owner = self .db - .const_eval(const_id.into(), subst, Some(self.trait_env.clone())) + .const_eval(const_id, subst, Some(self.trait_env.clone())) .map_err(|e| { let name = const_id.name(self.db.upcast()); MirEvalError::ConstEvalError(name, Box::new(e)) @@ -1769,9 +1765,8 @@ impl Evaluator<'_> { ))); } }; - Ok(mem.get_mut(pos..pos + size).ok_or_else(|| { - MirEvalError::UndefinedBehavior("out of bound memory write".to_string()) - })?) + mem.get_mut(pos..pos + size) + .ok_or_else(|| MirEvalError::UndefinedBehavior("out of bound memory write".to_string())) } fn write_memory(&mut self, addr: Address, r: &[u8]) -> Result<()> { @@ -1960,14 +1955,14 @@ impl Evaluator<'_> { if let Some(ty) = check_inner { for i in 0..count { let offset = element_size * i; - rec(this, &b[offset..offset + element_size], &ty, locals, mm)?; + rec(this, &b[offset..offset + element_size], ty, locals, mm)?; } } } } } chalk_ir::TyKind::Array(inner, len) => { - let len = match try_const_usize(this.db, &len) { + let len = match try_const_usize(this.db, len) { Some(it) => it as usize, None => not_supported!("non evaluatable array len in patching addresses"), }; @@ -2028,7 +2023,7 @@ impl Evaluator<'_> { Ok(()) } let mut mm = ComplexMemoryMap::default(); - rec(&self, bytes, ty, locals, &mut mm)?; + rec(self, bytes, ty, locals, &mut mm)?; Ok(mm) } @@ -2111,7 +2106,7 @@ impl Evaluator<'_> { } } TyKind::Array(inner, len) => { - let len = match try_const_usize(self.db, &len) { + let len = match try_const_usize(self.db, len) { Some(it) => it as usize, None => not_supported!("non evaluatable array len in patching addresses"), }; @@ -2161,7 +2156,7 @@ impl Evaluator<'_> { let next_ty = self.vtable_map.ty(id)?.clone(); match &next_ty.kind(Interner) { TyKind::FnDef(def, generic_args) => { - self.exec_fn_def(*def, generic_args, destination, args, &locals, target_bb, span) + self.exec_fn_def(*def, generic_args, destination, args, locals, target_bb, span) } TyKind::Closure(id, subst) => { self.exec_closure(*id, bytes.slice(0..0), subst, destination, args, locals, span) @@ -2195,7 +2190,7 @@ impl Evaluator<'_> { closure_data.get(self)?.to_owned() }; let arg_bytes = iter::once(Ok(closure_data)) - .chain(args.iter().map(|it| Ok(it.get(&self)?.to_owned()))) + .chain(args.iter().map(|it| Ok(it.get(self)?.to_owned()))) .collect::<Result<Vec<_>>>()?; let interval = self .interpret_mir(mir_body, arg_bytes.into_iter().map(IntervalOrOwned::Owned)) @@ -2246,7 +2241,7 @@ impl Evaluator<'_> { } CallableDefId::StructId(id) => { let (size, variant_layout, tag) = - self.layout_of_variant(id.into(), generic_args, &locals)?; + self.layout_of_variant(id.into(), generic_args, locals)?; let result = self.make_by_layout( size, &variant_layout, @@ -2258,7 +2253,7 @@ impl Evaluator<'_> { } CallableDefId::EnumVariantId(id) => { let (size, variant_layout, tag) = - self.layout_of_variant(id.into(), generic_args, &locals)?; + self.layout_of_variant(id.into(), generic_args, locals)?; let result = self.make_by_layout( size, &variant_layout, @@ -2353,7 +2348,7 @@ impl Evaluator<'_> { } }), ); - return self.exec_fn_with_args( + self.exec_fn_with_args( def, &args_for_target, generics_for_target, @@ -2361,7 +2356,7 @@ impl Evaluator<'_> { destination, target_bb, span, - ); + ) } MirOrDynIndex::Mir(body) => self.exec_looked_up_function( body, @@ -2413,7 +2408,7 @@ impl Evaluator<'_> { target_bb: Option<BasicBlockId>, span: MirSpan, ) -> Result<Option<StackFrame>> { - let func = args.get(0).ok_or(MirEvalError::TypeError("fn trait with no arg"))?; + let func = args.first().ok_or(MirEvalError::TypeError("fn trait with no arg"))?; let mut func_ty = func.ty.clone(); let mut func_data = func.interval; while let TyKind::Ref(_, _, z) = func_ty.kind(Interner) { @@ -2429,25 +2424,10 @@ impl Evaluator<'_> { } match &func_ty.kind(Interner) { TyKind::FnDef(def, subst) => { - return self.exec_fn_def( - *def, - subst, - destination, - &args[1..], - locals, - target_bb, - span, - ); + self.exec_fn_def(*def, subst, destination, &args[1..], locals, target_bb, span) } TyKind::Function(_) => { - return self.exec_fn_pointer( - func_data, - destination, - &args[1..], - locals, - target_bb, - span, - ); + self.exec_fn_pointer(func_data, destination, &args[1..], locals, target_bb, span) } TyKind::Closure(closure, subst) => { return self.exec_closure( @@ -2483,7 +2463,7 @@ impl Evaluator<'_> { self.write_memory(addr, &result)?; IntervalAndTy { interval: Interval { addr, size }, ty } }; - return self.exec_fn_with_args( + self.exec_fn_with_args( def, &[arg0.clone(), arg1], generic_args, @@ -2491,7 +2471,7 @@ impl Evaluator<'_> { destination, target_bb, span, - ); + ) } } } @@ -2511,7 +2491,7 @@ impl Evaluator<'_> { self.allocate_const_in_heap(locals, &konst)? } else { let ty = &self.db.infer(st.into())[self.db.body(st.into()).body_expr]; - let Some((size, align)) = self.size_align_of(&ty, locals)? else { + let Some((size, align)) = self.size_align_of(ty, locals)? else { not_supported!("unsized extern static"); }; let addr = self.heap_allocate(size, align)?; @@ -2669,7 +2649,7 @@ pub fn render_const_using_debug_impl( db.upcast(), &hir_def::path::Path::from_known_path_with_no_generic(ModPath::from_segments( hir_expand::mod_path::PathKind::Abs, - [name![core], name![fmt], name![Debug]].into_iter(), + [name![core], name![fmt], name![Debug]], )), ) else { not_supported!("core::fmt::Debug not found"); @@ -2701,7 +2681,7 @@ pub fn render_const_using_debug_impl( db.upcast(), &hir_def::path::Path::from_known_path_with_no_generic(ModPath::from_segments( hir_expand::mod_path::PathKind::Abs, - [name![std], name![fmt], name![format]].into_iter(), + [name![std], name![fmt], name![format]], )), ) else { not_supported!("std::fmt::format not found"); |