Remove some vec clones in const-eval
This commit is contained in:
parent
5125063a21
commit
5ac0c14384
@ -515,7 +515,7 @@ fn render_const_scalar(
|
|||||||
TyKind::Dyn(_) => {
|
TyKind::Dyn(_) => {
|
||||||
let addr = usize::from_le_bytes(b[0..b.len() / 2].try_into().unwrap());
|
let addr = usize::from_le_bytes(b[0..b.len() / 2].try_into().unwrap());
|
||||||
let ty_id = usize::from_le_bytes(b[b.len() / 2..].try_into().unwrap());
|
let ty_id = usize::from_le_bytes(b[b.len() / 2..].try_into().unwrap());
|
||||||
let Ok(t) = memory_map.vtable.ty(ty_id) else {
|
let Ok(t) = memory_map.vtable_ty(ty_id) else {
|
||||||
return f.write_str("<ty-missing-in-vtable-map>");
|
return f.write_str("<ty-missing-in-vtable-map>");
|
||||||
};
|
};
|
||||||
let Ok(layout) = f.db.layout_of_ty(t.clone(), trait_env) else {
|
let Ok(layout) = f.db.layout_of_ty(t.clone(), trait_env) else {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Inference of closure parameter types based on the closure's expected type.
|
//! Inference of closure parameter types based on the closure's expected type.
|
||||||
|
|
||||||
use std::{cmp, collections::HashMap, convert::Infallible, mem};
|
use std::{cmp, convert::Infallible, mem};
|
||||||
|
|
||||||
use chalk_ir::{
|
use chalk_ir::{
|
||||||
cast::Cast,
|
cast::Cast,
|
||||||
@ -778,7 +778,7 @@ impl InferenceContext<'_> {
|
|||||||
|
|
||||||
fn minimize_captures(&mut self) {
|
fn minimize_captures(&mut self) {
|
||||||
self.current_captures.sort_by_key(|it| it.place.projections.len());
|
self.current_captures.sort_by_key(|it| it.place.projections.len());
|
||||||
let mut hash_map = HashMap::<HirPlace, usize>::new();
|
let mut hash_map = FxHashMap::<HirPlace, usize>::default();
|
||||||
let result = mem::take(&mut self.current_captures);
|
let result = mem::take(&mut self.current_captures);
|
||||||
for item in result {
|
for item in result {
|
||||||
let mut lookup_place = HirPlace { local: item.place.local, projections: vec![] };
|
let mut lookup_place = HirPlace { local: item.place.local, projections: vec![] };
|
||||||
|
@ -36,35 +36,6 @@ impl<T> std::ops::Deref for InternedWrapper<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq)]
|
|
||||||
pub struct PreHashedWrapper<T>(T, u64);
|
|
||||||
|
|
||||||
impl<T: fmt::Debug> fmt::Debug for PreHashedWrapper<T> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
fmt::Debug::fmt(&self.0, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: PartialEq> PartialEq for PreHashedWrapper<T> {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.1 == other.1 && self.0 == other.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> std::ops::Deref for PreHashedWrapper<T> {
|
|
||||||
type Target = T;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: std::hash::Hash> std::hash::Hash for PreHashedWrapper<T> {
|
|
||||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
||||||
state.write_u64(self.1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_internable!(
|
impl_internable!(
|
||||||
InternedWrapper<Vec<VariableKind>>,
|
InternedWrapper<Vec<VariableKind>>,
|
||||||
InternedWrapper<SmallVec<[GenericArg; 2]>>,
|
InternedWrapper<SmallVec<[GenericArg; 2]>>,
|
||||||
@ -76,7 +47,6 @@ impl_internable!(
|
|||||||
InternedWrapper<Vec<ProgramClause>>,
|
InternedWrapper<Vec<ProgramClause>>,
|
||||||
InternedWrapper<Vec<QuantifiedWhereClause>>,
|
InternedWrapper<Vec<QuantifiedWhereClause>>,
|
||||||
InternedWrapper<SmallVec<[Variance; 16]>>,
|
InternedWrapper<SmallVec<[Variance; 16]>>,
|
||||||
// InternedWrapper<PreHashedWrapper<Goals>>,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
impl chalk_ir::interner::Interner for Interner {
|
impl chalk_ir::interner::Interner for Interner {
|
||||||
@ -88,7 +58,6 @@ impl chalk_ir::interner::Interner for Interner {
|
|||||||
// We could do the following, but that saves "only" 20mb on self while increasing inferecene
|
// We could do the following, but that saves "only" 20mb on self while increasing inferecene
|
||||||
// time by ~2.5%
|
// time by ~2.5%
|
||||||
// type InternedGoal = Interned<InternedWrapper<GoalData>>;
|
// type InternedGoal = Interned<InternedWrapper<GoalData>>;
|
||||||
// type InternedGoal = Interned<InternedWrapper<PreHashedWrapper<GoalData>>>;
|
|
||||||
type InternedGoal = Arc<GoalData>;
|
type InternedGoal = Arc<GoalData>;
|
||||||
type InternedGoals = Vec<Goal>;
|
type InternedGoals = Vec<Goal>;
|
||||||
type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
|
type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
|
||||||
@ -97,7 +66,6 @@ impl chalk_ir::interner::Interner for Interner {
|
|||||||
type InternedQuantifiedWhereClauses = Interned<InternedWrapper<Vec<QuantifiedWhereClause>>>;
|
type InternedQuantifiedWhereClauses = Interned<InternedWrapper<Vec<QuantifiedWhereClause>>>;
|
||||||
type InternedVariableKinds = Interned<InternedWrapper<Vec<VariableKind>>>;
|
type InternedVariableKinds = Interned<InternedWrapper<Vec<VariableKind>>>;
|
||||||
type InternedCanonicalVarKinds = Interned<InternedWrapper<Vec<CanonicalVarKind>>>;
|
type InternedCanonicalVarKinds = Interned<InternedWrapper<Vec<CanonicalVarKind>>>;
|
||||||
// type InternedConstraints = SmallVec<[InEnvironment<Constraint>; 1]>;
|
|
||||||
type InternedConstraints = Vec<InEnvironment<Constraint>>;
|
type InternedConstraints = Vec<InEnvironment<Constraint>>;
|
||||||
type InternedVariances = SmallVec<[Variance; 16]>;
|
type InternedVariances = SmallVec<[Variance; 16]>;
|
||||||
type DefId = InternId;
|
type DefId = InternId;
|
||||||
|
@ -37,8 +37,8 @@ mod tests;
|
|||||||
mod test_db;
|
mod test_db;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::{hash_map::Entry, HashMap},
|
collections::hash_map::Entry,
|
||||||
hash::Hash,
|
hash::{BuildHasherDefault, Hash},
|
||||||
};
|
};
|
||||||
|
|
||||||
use chalk_ir::{
|
use chalk_ir::{
|
||||||
@ -52,7 +52,7 @@ use hir_def::{hir::ExprId, type_ref::Rawness, GeneralConstId, TypeOrConstParamId
|
|||||||
use hir_expand::name;
|
use hir_expand::name;
|
||||||
use la_arena::{Arena, Idx};
|
use la_arena::{Arena, Idx};
|
||||||
use mir::{MirEvalError, VTableMap};
|
use mir::{MirEvalError, VTableMap};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use syntax::ast::{make, ConstArg};
|
use syntax::ast::{make, ConstArg};
|
||||||
use traits::FnTrait;
|
use traits::FnTrait;
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
@ -171,24 +171,45 @@ pub type Variances = chalk_ir::Variances<Interner>;
|
|||||||
/// the necessary bits of memory of the const eval session to keep the constant
|
/// the necessary bits of memory of the const eval session to keep the constant
|
||||||
/// meaningful.
|
/// meaningful.
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||||
pub struct MemoryMap {
|
pub enum MemoryMap {
|
||||||
pub memory: HashMap<usize, Vec<u8>>,
|
#[default]
|
||||||
pub vtable: VTableMap,
|
Empty,
|
||||||
|
Simple(Box<[u8]>),
|
||||||
|
Complex(Box<ComplexMemoryMap>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MemoryMap {
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||||
fn insert(&mut self, addr: usize, x: Vec<u8>) {
|
pub struct ComplexMemoryMap {
|
||||||
|
memory: FxHashMap<usize, Box<[u8]>>,
|
||||||
|
vtable: VTableMap,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ComplexMemoryMap {
|
||||||
|
fn insert(&mut self, addr: usize, val: Box<[u8]>) {
|
||||||
match self.memory.entry(addr) {
|
match self.memory.entry(addr) {
|
||||||
Entry::Occupied(mut e) => {
|
Entry::Occupied(mut e) => {
|
||||||
if e.get().len() < x.len() {
|
if e.get().len() < val.len() {
|
||||||
e.insert(x);
|
e.insert(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Entry::Vacant(e) => {
|
Entry::Vacant(e) => {
|
||||||
e.insert(x);
|
e.insert(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MemoryMap {
|
||||||
|
pub fn vtable_ty(&self, id: usize) -> Result<&Ty, MirEvalError> {
|
||||||
|
match self {
|
||||||
|
MemoryMap::Empty | MemoryMap::Simple(_) => Err(MirEvalError::InvalidVTableId(id)),
|
||||||
|
MemoryMap::Complex(cm) => cm.vtable.ty(id),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn simple(v: Box<[u8]>) -> Self {
|
||||||
|
MemoryMap::Simple(v)
|
||||||
|
}
|
||||||
|
|
||||||
/// This functions convert each address by a function `f` which gets the byte intervals and assign an address
|
/// This functions convert each address by a function `f` which gets the byte intervals and assign an address
|
||||||
/// to them. It is useful when you want to load a constant with a memory map in a new memory. You can pass an
|
/// to them. It is useful when you want to load a constant with a memory map in a new memory. You can pass an
|
||||||
@ -196,22 +217,33 @@ impl MemoryMap {
|
|||||||
fn transform_addresses(
|
fn transform_addresses(
|
||||||
&self,
|
&self,
|
||||||
mut f: impl FnMut(&[u8], usize) -> Result<usize, MirEvalError>,
|
mut f: impl FnMut(&[u8], usize) -> Result<usize, MirEvalError>,
|
||||||
) -> Result<HashMap<usize, usize>, MirEvalError> {
|
) -> Result<FxHashMap<usize, usize>, MirEvalError> {
|
||||||
self.memory
|
let mut transform = |(addr, val): (&usize, &Box<[u8]>)| {
|
||||||
.iter()
|
let addr = *addr;
|
||||||
.map(|x| {
|
let align = if addr == 0 { 64 } else { (addr - (addr & (addr - 1))).min(64) };
|
||||||
let addr = *x.0;
|
f(val, align).and_then(|it| Ok((addr, it)))
|
||||||
let align = if addr == 0 { 64 } else { (addr - (addr & (addr - 1))).min(64) };
|
};
|
||||||
Ok((addr, f(x.1, align)?))
|
match self {
|
||||||
})
|
MemoryMap::Empty => Ok(Default::default()),
|
||||||
.collect()
|
MemoryMap::Simple(m) => transform((&0, m)).map(|(addr, val)| {
|
||||||
|
let mut map = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
|
||||||
|
map.insert(addr, val);
|
||||||
|
map
|
||||||
|
}),
|
||||||
|
MemoryMap::Complex(cm) => cm.memory.iter().map(transform).collect(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get<'a>(&'a self, addr: usize, size: usize) -> Option<&'a [u8]> {
|
fn get(&self, addr: usize, size: usize) -> Option<&[u8]> {
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
Some(&[])
|
Some(&[])
|
||||||
} else {
|
} else {
|
||||||
self.memory.get(&addr)?.get(0..size)
|
match self {
|
||||||
|
MemoryMap::Empty => Some(&[]),
|
||||||
|
MemoryMap::Simple(m) if addr == 0 => m.get(0..size),
|
||||||
|
MemoryMap::Simple(_) => None,
|
||||||
|
MemoryMap::Complex(cm) => cm.memory.get(&addr)?.get(0..size),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,6 @@
|
|||||||
//! This module provides a MIR interpreter, which is used in const eval.
|
//! This module provides a MIR interpreter, which is used in const eval.
|
||||||
|
|
||||||
use std::{
|
use std::{borrow::Cow, cell::RefCell, fmt::Write, iter, mem, ops::Range};
|
||||||
borrow::Cow,
|
|
||||||
cell::RefCell,
|
|
||||||
collections::{HashMap, HashSet},
|
|
||||||
fmt::Write,
|
|
||||||
iter, mem,
|
|
||||||
ops::Range,
|
|
||||||
};
|
|
||||||
|
|
||||||
use base_db::{CrateId, FileId};
|
use base_db::{CrateId, FileId};
|
||||||
use chalk_ir::{cast::Cast, Mutability};
|
use chalk_ir::{cast::Cast, Mutability};
|
||||||
@ -40,8 +33,8 @@ use crate::{
|
|||||||
name, static_lifetime,
|
name, static_lifetime,
|
||||||
traits::FnTrait,
|
traits::FnTrait,
|
||||||
utils::{detect_variant_from_bytes, ClosureSubst},
|
utils::{detect_variant_from_bytes, ClosureSubst},
|
||||||
CallableDefId, ClosureId, Const, ConstScalar, FnDefId, Interner, MemoryMap, Substitution,
|
CallableDefId, ClosureId, ComplexMemoryMap, Const, ConstScalar, FnDefId, Interner, MemoryMap,
|
||||||
TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
|
Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@ -98,6 +91,15 @@ impl VTableMap {
|
|||||||
let id = from_bytes!(usize, bytes);
|
let id = from_bytes!(usize, bytes);
|
||||||
self.ty(id)
|
self.ty(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn shrink_to_fit(&mut self) {
|
||||||
|
self.id_to_ty.shrink_to_fit();
|
||||||
|
self.ty_to_id.shrink_to_fit();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty(&self) -> bool {
|
||||||
|
self.id_to_ty.is_empty() && self.ty_to_id.is_empty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||||
@ -251,13 +253,6 @@ impl From<Interval> for IntervalOrOwned {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IntervalOrOwned {
|
impl IntervalOrOwned {
|
||||||
pub(crate) fn to_vec(self, memory: &Evaluator<'_>) -> Result<Vec<u8>> {
|
|
||||||
Ok(match self {
|
|
||||||
IntervalOrOwned::Owned(o) => o,
|
|
||||||
IntervalOrOwned::Borrowed(b) => b.get(memory)?.to_vec(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get<'a>(&'a self, memory: &'a Evaluator<'a>) -> Result<&'a [u8]> {
|
fn get<'a>(&'a self, memory: &'a Evaluator<'a>) -> Result<&'a [u8]> {
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
IntervalOrOwned::Owned(o) => o,
|
IntervalOrOwned::Owned(o) => o,
|
||||||
@ -291,8 +286,8 @@ impl Address {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&self) -> Vec<u8> {
|
fn to_bytes(&self) -> [u8; mem::size_of::<usize>()] {
|
||||||
usize::to_le_bytes(self.to_usize()).to_vec()
|
usize::to_le_bytes(self.to_usize())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_usize(&self) -> usize {
|
fn to_usize(&self) -> usize {
|
||||||
@ -510,6 +505,20 @@ struct Locals {
|
|||||||
drop_flags: DropFlags,
|
drop_flags: DropFlags,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct MirOutput {
|
||||||
|
stdout: Vec<u8>,
|
||||||
|
stderr: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MirOutput {
|
||||||
|
pub fn stdout(&self) -> Cow<'_, str> {
|
||||||
|
String::from_utf8_lossy(&self.stdout)
|
||||||
|
}
|
||||||
|
pub fn stderr(&self) -> Cow<'_, str> {
|
||||||
|
String::from_utf8_lossy(&self.stderr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn interpret_mir(
|
pub fn interpret_mir(
|
||||||
db: &dyn HirDatabase,
|
db: &dyn HirDatabase,
|
||||||
body: Arc<MirBody>,
|
body: Arc<MirBody>,
|
||||||
@ -520,7 +529,7 @@ pub fn interpret_mir(
|
|||||||
// (and probably should) do better here, for example by excluding bindings outside of the target expression.
|
// (and probably should) do better here, for example by excluding bindings outside of the target expression.
|
||||||
assert_placeholder_ty_is_unused: bool,
|
assert_placeholder_ty_is_unused: bool,
|
||||||
trait_env: Option<Arc<TraitEnvironment>>,
|
trait_env: Option<Arc<TraitEnvironment>>,
|
||||||
) -> (Result<Const>, String, String) {
|
) -> (Result<Const>, MirOutput) {
|
||||||
let ty = body.locals[return_slot()].ty.clone();
|
let ty = body.locals[return_slot()].ty.clone();
|
||||||
let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env);
|
let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env);
|
||||||
let it: Result<Const> = (|| {
|
let it: Result<Const> = (|| {
|
||||||
@ -534,14 +543,17 @@ pub fn interpret_mir(
|
|||||||
&ty,
|
&ty,
|
||||||
&Locals { ptr: ArenaMap::new(), body, drop_flags: DropFlags::default() },
|
&Locals { ptr: ArenaMap::new(), body, drop_flags: DropFlags::default() },
|
||||||
)?;
|
)?;
|
||||||
memory_map.vtable = evaluator.vtable_map.clone();
|
let bytes = bytes.into();
|
||||||
return Ok(intern_const_scalar(ConstScalar::Bytes(bytes.into(), memory_map), ty));
|
let memory_map = if memory_map.memory.is_empty() && evaluator.vtable_map.is_empty() {
|
||||||
|
MemoryMap::Empty
|
||||||
|
} else {
|
||||||
|
memory_map.vtable = mem::take(&mut evaluator.vtable_map);
|
||||||
|
memory_map.vtable.shrink_to_fit();
|
||||||
|
MemoryMap::Complex(Box::new(memory_map))
|
||||||
|
};
|
||||||
|
return Ok(intern_const_scalar(ConstScalar::Bytes(bytes, memory_map), ty));
|
||||||
})();
|
})();
|
||||||
(
|
(it, MirOutput { stdout: evaluator.stdout, stderr: evaluator.stderr })
|
||||||
it,
|
|
||||||
String::from_utf8_lossy(&evaluator.stdout).into_owned(),
|
|
||||||
String::from_utf8_lossy(&evaluator.stderr).into_owned(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -563,7 +575,7 @@ impl Evaluator<'_> {
|
|||||||
code_stack: vec![],
|
code_stack: vec![],
|
||||||
vtable_map: VTableMap::default(),
|
vtable_map: VTableMap::default(),
|
||||||
thread_local_storage: TlsData::default(),
|
thread_local_storage: TlsData::default(),
|
||||||
static_locations: HashMap::default(),
|
static_locations: Default::default(),
|
||||||
db,
|
db,
|
||||||
random_state: oorandom::Rand64::new(0),
|
random_state: oorandom::Rand64::new(0),
|
||||||
trait_env: trait_env.unwrap_or_else(|| db.trait_environment_for_body(owner)),
|
trait_env: trait_env.unwrap_or_else(|| db.trait_environment_for_body(owner)),
|
||||||
@ -574,11 +586,11 @@ impl Evaluator<'_> {
|
|||||||
stack_depth_limit: 100,
|
stack_depth_limit: 100,
|
||||||
execution_limit: EXECUTION_LIMIT,
|
execution_limit: EXECUTION_LIMIT,
|
||||||
memory_limit: 1000_000_000, // 2GB, 1GB for stack and 1GB for heap
|
memory_limit: 1000_000_000, // 2GB, 1GB for stack and 1GB for heap
|
||||||
layout_cache: RefCell::new(HashMap::default()),
|
layout_cache: RefCell::new(Default::default()),
|
||||||
projected_ty_cache: RefCell::new(HashMap::default()),
|
projected_ty_cache: RefCell::new(Default::default()),
|
||||||
not_special_fn_cache: RefCell::new(HashSet::default()),
|
not_special_fn_cache: RefCell::new(Default::default()),
|
||||||
mir_or_dyn_index_cache: RefCell::new(HashMap::default()),
|
mir_or_dyn_index_cache: RefCell::new(Default::default()),
|
||||||
unused_locals_store: RefCell::new(HashMap::default()),
|
unused_locals_store: RefCell::new(Default::default()),
|
||||||
cached_ptr_size: match db.target_data_layout(crate_id) {
|
cached_ptr_size: match db.target_data_layout(crate_id) {
|
||||||
Some(it) => it.pointer_size.bytes_usize(),
|
Some(it) => it.pointer_size.bytes_usize(),
|
||||||
None => 8,
|
None => 8,
|
||||||
@ -838,8 +850,8 @@ impl Evaluator<'_> {
|
|||||||
match &statement.kind {
|
match &statement.kind {
|
||||||
StatementKind::Assign(l, r) => {
|
StatementKind::Assign(l, r) => {
|
||||||
let addr = self.place_addr(l, &locals)?;
|
let addr = self.place_addr(l, &locals)?;
|
||||||
let result = self.eval_rvalue(r, &mut locals)?.to_vec(&self)?;
|
let result = self.eval_rvalue(r, &mut locals)?;
|
||||||
self.write_memory(addr, &result)?;
|
self.copy_from_interval_or_owned(addr, result)?;
|
||||||
locals
|
locals
|
||||||
.drop_flags
|
.drop_flags
|
||||||
.add_place(l.clone(), &locals.body.projection_store);
|
.add_place(l.clone(), &locals.body.projection_store);
|
||||||
@ -1051,7 +1063,7 @@ impl Evaluator<'_> {
|
|||||||
Rvalue::Use(it) => Borrowed(self.eval_operand(it, locals)?),
|
Rvalue::Use(it) => Borrowed(self.eval_operand(it, locals)?),
|
||||||
Rvalue::Ref(_, p) => {
|
Rvalue::Ref(_, p) => {
|
||||||
let (addr, _, metadata) = self.place_addr_and_ty_and_metadata(p, locals)?;
|
let (addr, _, metadata) = self.place_addr_and_ty_and_metadata(p, locals)?;
|
||||||
let mut r = addr.to_bytes();
|
let mut r = addr.to_bytes().to_vec();
|
||||||
if let Some(metadata) = metadata {
|
if let Some(metadata) = metadata {
|
||||||
r.extend(metadata.get(self)?);
|
r.extend(metadata.get(self)?);
|
||||||
}
|
}
|
||||||
@ -1284,7 +1296,7 @@ impl Evaluator<'_> {
|
|||||||
not_supported!("unsized box initialization");
|
not_supported!("unsized box initialization");
|
||||||
};
|
};
|
||||||
let addr = self.heap_allocate(size, align)?;
|
let addr = self.heap_allocate(size, align)?;
|
||||||
Owned(addr.to_bytes())
|
Owned(addr.to_bytes().to_vec())
|
||||||
}
|
}
|
||||||
Rvalue::CopyForDeref(_) => not_supported!("copy for deref"),
|
Rvalue::CopyForDeref(_) => not_supported!("copy for deref"),
|
||||||
Rvalue::Aggregate(kind, values) => {
|
Rvalue::Aggregate(kind, values) => {
|
||||||
@ -1716,7 +1728,18 @@ impl Evaluator<'_> {
|
|||||||
}
|
}
|
||||||
let addr = self.heap_allocate(size, align)?;
|
let addr = self.heap_allocate(size, align)?;
|
||||||
self.write_memory(addr, &v)?;
|
self.write_memory(addr, &v)?;
|
||||||
self.patch_addresses(&patch_map, &memory_map.vtable, addr, ty, locals)?;
|
self.patch_addresses(
|
||||||
|
&patch_map,
|
||||||
|
|bytes| match &memory_map {
|
||||||
|
MemoryMap::Empty | MemoryMap::Simple(_) => {
|
||||||
|
Err(MirEvalError::InvalidVTableId(from_bytes!(usize, bytes)))
|
||||||
|
}
|
||||||
|
MemoryMap::Complex(cm) => cm.vtable.ty_of_bytes(bytes),
|
||||||
|
},
|
||||||
|
addr,
|
||||||
|
ty,
|
||||||
|
locals,
|
||||||
|
)?;
|
||||||
Ok(Interval::new(addr, size))
|
Ok(Interval::new(addr, size))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1768,6 +1791,13 @@ impl Evaluator<'_> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn copy_from_interval_or_owned(&mut self, addr: Address, r: IntervalOrOwned) -> Result<()> {
|
||||||
|
match r {
|
||||||
|
IntervalOrOwned::Borrowed(r) => self.copy_from_interval(addr, r),
|
||||||
|
IntervalOrOwned::Owned(r) => self.write_memory(addr, &r),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn copy_from_interval(&mut self, addr: Address, r: Interval) -> Result<()> {
|
fn copy_from_interval(&mut self, addr: Address, r: Interval) -> Result<()> {
|
||||||
if r.size == 0 {
|
if r.size == 0 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@ -1888,13 +1918,18 @@ impl Evaluator<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_memory_map(&self, bytes: &[u8], ty: &Ty, locals: &Locals) -> Result<MemoryMap> {
|
fn create_memory_map(
|
||||||
|
&self,
|
||||||
|
bytes: &[u8],
|
||||||
|
ty: &Ty,
|
||||||
|
locals: &Locals,
|
||||||
|
) -> Result<ComplexMemoryMap> {
|
||||||
fn rec(
|
fn rec(
|
||||||
this: &Evaluator<'_>,
|
this: &Evaluator<'_>,
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
ty: &Ty,
|
ty: &Ty,
|
||||||
locals: &Locals,
|
locals: &Locals,
|
||||||
mm: &mut MemoryMap,
|
mm: &mut ComplexMemoryMap,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
match ty.kind(Interner) {
|
match ty.kind(Interner) {
|
||||||
TyKind::Ref(_, _, t) => {
|
TyKind::Ref(_, _, t) => {
|
||||||
@ -1904,7 +1939,7 @@ impl Evaluator<'_> {
|
|||||||
let addr_usize = from_bytes!(usize, bytes);
|
let addr_usize = from_bytes!(usize, bytes);
|
||||||
mm.insert(
|
mm.insert(
|
||||||
addr_usize,
|
addr_usize,
|
||||||
this.read_memory(Address::from_usize(addr_usize), size)?.to_vec(),
|
this.read_memory(Address::from_usize(addr_usize), size)?.into(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@ -1930,7 +1965,7 @@ impl Evaluator<'_> {
|
|||||||
let size = element_size * count;
|
let size = element_size * count;
|
||||||
let addr = Address::from_bytes(addr)?;
|
let addr = Address::from_bytes(addr)?;
|
||||||
let b = this.read_memory(addr, size)?;
|
let b = this.read_memory(addr, size)?;
|
||||||
mm.insert(addr.to_usize(), b.to_vec());
|
mm.insert(addr.to_usize(), b.into());
|
||||||
if let Some(ty) = check_inner {
|
if let Some(ty) = check_inner {
|
||||||
for i in 0..count {
|
for i in 0..count {
|
||||||
let offset = element_size * i;
|
let offset = element_size * i;
|
||||||
@ -2003,15 +2038,15 @@ impl Evaluator<'_> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
let mut mm = MemoryMap::default();
|
let mut mm = ComplexMemoryMap::default();
|
||||||
rec(self, bytes, ty, locals, &mut mm)?;
|
rec(&self, bytes, ty, locals, &mut mm)?;
|
||||||
Ok(mm)
|
Ok(mm)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn patch_addresses(
|
fn patch_addresses<'vtable>(
|
||||||
&mut self,
|
&mut self,
|
||||||
patch_map: &HashMap<usize, usize>,
|
patch_map: &FxHashMap<usize, usize>,
|
||||||
old_vtable: &VTableMap,
|
ty_of_bytes: impl Fn(&[u8]) -> Result<&'vtable Ty> + Copy,
|
||||||
addr: Address,
|
addr: Address,
|
||||||
ty: &Ty,
|
ty: &Ty,
|
||||||
locals: &Locals,
|
locals: &Locals,
|
||||||
@ -2038,7 +2073,7 @@ impl Evaluator<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TyKind::Function(_) => {
|
TyKind::Function(_) => {
|
||||||
let ty = old_vtable.ty_of_bytes(self.read_memory(addr, my_size)?)?.clone();
|
let ty = ty_of_bytes(self.read_memory(addr, my_size)?)?.clone();
|
||||||
let new_id = self.vtable_map.id(ty);
|
let new_id = self.vtable_map.id(ty);
|
||||||
self.write_memory(addr, &new_id.to_le_bytes())?;
|
self.write_memory(addr, &new_id.to_le_bytes())?;
|
||||||
}
|
}
|
||||||
@ -2049,7 +2084,7 @@ impl Evaluator<'_> {
|
|||||||
let ty = ty.clone().substitute(Interner, subst);
|
let ty = ty.clone().substitute(Interner, subst);
|
||||||
self.patch_addresses(
|
self.patch_addresses(
|
||||||
patch_map,
|
patch_map,
|
||||||
old_vtable,
|
ty_of_bytes,
|
||||||
addr.offset(offset),
|
addr.offset(offset),
|
||||||
&ty,
|
&ty,
|
||||||
locals,
|
locals,
|
||||||
@ -2071,7 +2106,7 @@ impl Evaluator<'_> {
|
|||||||
let ty = ty.clone().substitute(Interner, subst);
|
let ty = ty.clone().substitute(Interner, subst);
|
||||||
self.patch_addresses(
|
self.patch_addresses(
|
||||||
patch_map,
|
patch_map,
|
||||||
old_vtable,
|
ty_of_bytes,
|
||||||
addr.offset(offset),
|
addr.offset(offset),
|
||||||
&ty,
|
&ty,
|
||||||
locals,
|
locals,
|
||||||
@ -2084,7 +2119,7 @@ impl Evaluator<'_> {
|
|||||||
for (id, ty) in subst.iter(Interner).enumerate() {
|
for (id, ty) in subst.iter(Interner).enumerate() {
|
||||||
let ty = ty.assert_ty_ref(Interner); // Tuple only has type argument
|
let ty = ty.assert_ty_ref(Interner); // Tuple only has type argument
|
||||||
let offset = layout.fields.offset(id).bytes_usize();
|
let offset = layout.fields.offset(id).bytes_usize();
|
||||||
self.patch_addresses(patch_map, old_vtable, addr.offset(offset), ty, locals)?;
|
self.patch_addresses(patch_map, ty_of_bytes, addr.offset(offset), ty, locals)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TyKind::Array(inner, len) => {
|
TyKind::Array(inner, len) => {
|
||||||
@ -2096,7 +2131,7 @@ impl Evaluator<'_> {
|
|||||||
for i in 0..len {
|
for i in 0..len {
|
||||||
self.patch_addresses(
|
self.patch_addresses(
|
||||||
patch_map,
|
patch_map,
|
||||||
old_vtable,
|
ty_of_bytes,
|
||||||
addr.offset(i * size),
|
addr.offset(i * size),
|
||||||
inner,
|
inner,
|
||||||
locals,
|
locals,
|
||||||
@ -2167,7 +2202,7 @@ impl Evaluator<'_> {
|
|||||||
.map_err(|it| MirEvalError::MirLowerErrorForClosure(closure, it))?;
|
.map_err(|it| MirEvalError::MirLowerErrorForClosure(closure, it))?;
|
||||||
let closure_data = if mir_body.locals[mir_body.param_locals[0]].ty.as_reference().is_some()
|
let closure_data = if mir_body.locals[mir_body.param_locals[0]].ty.as_reference().is_some()
|
||||||
{
|
{
|
||||||
closure_data.addr.to_bytes()
|
closure_data.addr.to_bytes().to_vec()
|
||||||
} else {
|
} else {
|
||||||
closure_data.get(self)?.to_owned()
|
closure_data.get(self)?.to_owned()
|
||||||
};
|
};
|
||||||
@ -2553,7 +2588,7 @@ impl Evaluator<'_> {
|
|||||||
body,
|
body,
|
||||||
locals,
|
locals,
|
||||||
drop_fn,
|
drop_fn,
|
||||||
[IntervalOrOwned::Owned(addr.to_bytes())].into_iter(),
|
iter::once(IntervalOrOwned::Owned(addr.to_bytes().to_vec())),
|
||||||
span,
|
span,
|
||||||
Interval { addr: Address::Invalid(0), size: 0 },
|
Interval { addr: Address::Invalid(0), size: 0 },
|
||||||
None,
|
None,
|
||||||
|
@ -31,9 +31,9 @@ fn eval_main(db: &TestDB, file_id: FileId) -> Result<(String, String), MirEvalEr
|
|||||||
db.trait_environment(func_id.into()),
|
db.trait_environment(func_id.into()),
|
||||||
)
|
)
|
||||||
.map_err(|e| MirEvalError::MirLowerError(func_id.into(), e))?;
|
.map_err(|e| MirEvalError::MirLowerError(func_id.into(), e))?;
|
||||||
let (result, stdout, stderr) = interpret_mir(db, body, false, None);
|
let (result, output) = interpret_mir(db, body, false, None);
|
||||||
result?;
|
result?;
|
||||||
Ok((stdout, stderr))
|
Ok((output.stdout().into_owned(), output.stderr().into_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_pass(ra_fixture: &str) {
|
fn check_pass(ra_fixture: &str) {
|
||||||
|
@ -1403,31 +1403,27 @@ impl<'ctx> MirLowerCtx<'ctx> {
|
|||||||
const USIZE_SIZE: usize = mem::size_of::<usize>();
|
const USIZE_SIZE: usize = mem::size_of::<usize>();
|
||||||
let bytes: Box<[_]> = match l {
|
let bytes: Box<[_]> = match l {
|
||||||
hir_def::hir::Literal::String(b) => {
|
hir_def::hir::Literal::String(b) => {
|
||||||
let b = b.as_bytes();
|
let mut data = [0; { 2 * USIZE_SIZE }];
|
||||||
let mut data = Box::new([0; { 2 * USIZE_SIZE }]);
|
|
||||||
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
|
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
|
||||||
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
|
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
|
||||||
let mut mm = MemoryMap::default();
|
let mm = MemoryMap::simple(b.as_bytes().into());
|
||||||
mm.insert(0, b.to_vec());
|
return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
|
||||||
return Ok(Operand::from_concrete_const(data, mm, ty));
|
|
||||||
}
|
}
|
||||||
hir_def::hir::Literal::CString(b) => {
|
hir_def::hir::Literal::CString(b) => {
|
||||||
let bytes = b.iter().copied().chain(iter::once(0)).collect::<Vec<_>>();
|
let bytes = b.iter().copied().chain(iter::once(0)).collect::<Box<_>>();
|
||||||
|
|
||||||
let mut data = Box::new([0; { 2 * USIZE_SIZE }]);
|
let mut data = [0; { 2 * USIZE_SIZE }];
|
||||||
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
|
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
|
||||||
data[USIZE_SIZE..].copy_from_slice(&bytes.len().to_le_bytes());
|
data[USIZE_SIZE..].copy_from_slice(&bytes.len().to_le_bytes());
|
||||||
let mut mm = MemoryMap::default();
|
let mm = MemoryMap::simple(bytes);
|
||||||
mm.insert(0, bytes);
|
return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
|
||||||
return Ok(Operand::from_concrete_const(data, mm, ty));
|
|
||||||
}
|
}
|
||||||
hir_def::hir::Literal::ByteString(b) => {
|
hir_def::hir::Literal::ByteString(b) => {
|
||||||
let mut data = Box::new([0; { 2 * USIZE_SIZE }]);
|
let mut data = [0; { 2 * USIZE_SIZE }];
|
||||||
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
|
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
|
||||||
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
|
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
|
||||||
let mut mm = MemoryMap::default();
|
let mm = MemoryMap::simple(b.clone());
|
||||||
mm.insert(0, b.to_vec());
|
return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
|
||||||
return Ok(Operand::from_concrete_const(data, mm, ty));
|
|
||||||
}
|
}
|
||||||
hir_def::hir::Literal::Char(c) => Box::new(u32::from(*c).to_le_bytes()),
|
hir_def::hir::Literal::Char(c) => Box::new(u32::from(*c).to_le_bytes()),
|
||||||
hir_def::hir::Literal::Bool(b) => Box::new([*b as u8]),
|
hir_def::hir::Literal::Bool(b) => Box::new([*b as u8]),
|
||||||
|
@ -2183,7 +2183,7 @@ impl Function {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let (result, stdout, stderr) = interpret_mir(db, body, false, None);
|
let (result, output) = interpret_mir(db, body, false, None);
|
||||||
let mut text = match result {
|
let mut text = match result {
|
||||||
Ok(_) => "pass".to_string(),
|
Ok(_) => "pass".to_string(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@ -2192,10 +2192,12 @@ impl Function {
|
|||||||
r
|
r
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let stdout = output.stdout().into_owned();
|
||||||
if !stdout.is_empty() {
|
if !stdout.is_empty() {
|
||||||
text += "\n--------- stdout ---------\n";
|
text += "\n--------- stdout ---------\n";
|
||||||
text += &stdout;
|
text += &stdout;
|
||||||
}
|
}
|
||||||
|
let stderr = output.stdout().into_owned();
|
||||||
if !stderr.is_empty() {
|
if !stderr.is_empty() {
|
||||||
text += "\n--------- stderr ---------\n";
|
text += "\n--------- stderr ---------\n";
|
||||||
text += &stderr;
|
text += &stderr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user