Auto merge of #16290 - Veykril:chalk, r=Veykril

internal: Reduce vec cloning in mir lowering/eval
This commit is contained in:
bors 2024-01-06 22:13:09 +00:00
commit f595e60b6d
12 changed files with 308 additions and 256 deletions

View File

@ -142,15 +142,15 @@ pub fn intern_const_ref(
LiteralConstRef::Int(i) => {
// FIXME: We should handle failure of layout better.
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
ConstScalar::Bytes(i.to_le_bytes()[0..size].to_vec(), MemoryMap::default())
ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
}
LiteralConstRef::UInt(i) => {
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
ConstScalar::Bytes(i.to_le_bytes()[0..size].to_vec(), MemoryMap::default())
ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
}
LiteralConstRef::Bool(b) => ConstScalar::Bytes(vec![*b as u8], MemoryMap::default()),
LiteralConstRef::Bool(b) => ConstScalar::Bytes(Box::new([*b as u8]), MemoryMap::default()),
LiteralConstRef::Char(c) => {
ConstScalar::Bytes((*c as u32).to_le_bytes().to_vec(), MemoryMap::default())
ConstScalar::Bytes((*c as u32).to_le_bytes().into(), MemoryMap::default())
}
LiteralConstRef::Unknown => ConstScalar::Unknown,
};

View File

@ -515,7 +515,7 @@ fn render_const_scalar(
TyKind::Dyn(_) => {
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 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>");
};
let Ok(layout) = f.db.layout_of_ty(t.clone(), trait_env) else {

View File

@ -1,6 +1,6 @@
//! 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::{
cast::Cast,
@ -778,7 +778,7 @@ impl InferenceContext<'_> {
fn minimize_captures(&mut self) {
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);
for item in result {
let mut lookup_place = HirPlace { local: item.place.local, projections: vec![] };

View File

@ -1,9 +1,15 @@
//! Implementation of the Chalk `Interner` trait, which allows customizing the
//! representation of the various objects Chalk deals with (types, goals etc.).
use crate::{chalk_db, tls, ConstScalar, GenericArg};
use crate::{
chalk_db, tls, AliasTy, CanonicalVarKind, CanonicalVarKinds, ClosureId, Const, ConstData,
ConstScalar, Constraint, Constraints, FnDefId, GenericArg, GenericArgData, Goal, GoalData,
Goals, InEnvironment, Lifetime, LifetimeData, OpaqueTy, OpaqueTyId, ProgramClause,
ProgramClauseData, ProgramClauses, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
Substitution, Ty, TyData, TyKind, VariableKind, VariableKinds,
};
use base_db::salsa::InternId;
use chalk_ir::{Goal, GoalData};
use chalk_ir::{ProgramClauseImplication, SeparatorTraitRef, Variance};
use hir_def::TypeAliasId;
use intern::{impl_internable, Interned};
use smallvec::SmallVec;
@ -31,36 +37,37 @@ impl<T> std::ops::Deref for InternedWrapper<T> {
}
impl_internable!(
InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>,
InternedWrapper<Vec<VariableKind>>,
InternedWrapper<SmallVec<[GenericArg; 2]>>,
InternedWrapper<chalk_ir::TyData<Interner>>,
InternedWrapper<chalk_ir::LifetimeData<Interner>>,
InternedWrapper<chalk_ir::ConstData<Interner>>,
InternedWrapper<TyData>,
InternedWrapper<LifetimeData>,
InternedWrapper<ConstData>,
InternedWrapper<ConstScalar>,
InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Interner>>>,
InternedWrapper<Vec<chalk_ir::ProgramClause<Interner>>>,
InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Interner>>>,
InternedWrapper<Vec<chalk_ir::Variance>>,
InternedWrapper<Vec<CanonicalVarKind>>,
InternedWrapper<Vec<ProgramClause>>,
InternedWrapper<Vec<QuantifiedWhereClause>>,
InternedWrapper<SmallVec<[Variance; 16]>>,
);
impl chalk_ir::interner::Interner for Interner {
type InternedType = Interned<InternedWrapper<chalk_ir::TyData<Self>>>;
type InternedLifetime = Interned<InternedWrapper<chalk_ir::LifetimeData<Self>>>;
type InternedConst = Interned<InternedWrapper<chalk_ir::ConstData<Self>>>;
type InternedType = Interned<InternedWrapper<TyData>>;
type InternedLifetime = Interned<InternedWrapper<LifetimeData>>;
type InternedConst = Interned<InternedWrapper<ConstData>>;
type InternedConcreteConst = ConstScalar;
type InternedGenericArg = chalk_ir::GenericArgData<Self>;
type InternedGoal = Arc<GoalData<Self>>;
type InternedGoals = Vec<Goal<Self>>;
type InternedGenericArg = GenericArgData;
// We could do the following, but that saves "only" 20mb on self while increasing inferecene
// time by ~2.5%
// type InternedGoal = Interned<InternedWrapper<GoalData>>;
type InternedGoal = Arc<GoalData>;
type InternedGoals = Vec<Goal>;
type InternedSubstitution = Interned<InternedWrapper<SmallVec<[GenericArg; 2]>>>;
type InternedProgramClauses = Interned<InternedWrapper<Vec<chalk_ir::ProgramClause<Self>>>>;
type InternedProgramClause = chalk_ir::ProgramClauseData<Self>;
type InternedQuantifiedWhereClauses =
Interned<InternedWrapper<Vec<chalk_ir::QuantifiedWhereClause<Self>>>>;
type InternedVariableKinds = Interned<InternedWrapper<Vec<chalk_ir::VariableKind<Interner>>>>;
type InternedCanonicalVarKinds =
Interned<InternedWrapper<Vec<chalk_ir::CanonicalVarKind<Self>>>>;
type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>;
type InternedVariances = Interned<InternedWrapper<Vec<chalk_ir::Variance>>>;
type InternedProgramClauses = Interned<InternedWrapper<Vec<ProgramClause>>>;
type InternedProgramClause = ProgramClauseData;
type InternedQuantifiedWhereClauses = Interned<InternedWrapper<Vec<QuantifiedWhereClause>>>;
type InternedVariableKinds = Interned<InternedWrapper<Vec<VariableKind>>>;
type InternedCanonicalVarKinds = Interned<InternedWrapper<Vec<CanonicalVarKind>>>;
type InternedConstraints = Vec<InEnvironment<Constraint>>;
type InternedVariances = SmallVec<[Variance; 16]>;
type DefId = InternId;
type InternedAdtId = hir_def::AdtId;
type Identifier = TypeAliasId;
@ -88,68 +95,51 @@ impl chalk_ir::interner::Interner for Interner {
}
fn debug_opaque_ty_id(
opaque_ty_id: chalk_ir::OpaqueTyId<Self>,
opaque_ty_id: OpaqueTyId,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "OpaqueTy#{}", opaque_ty_id.0))
}
fn debug_fn_def_id(
fn_def_id: chalk_ir::FnDefId<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_fn_def_id(fn_def_id: FnDefId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt)))
}
fn debug_closure_id(
_fn_def_id: chalk_ir::ClosureId<Self>,
_fn_def_id: ClosureId,
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
fn debug_alias(
alias: &chalk_ir::AliasTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_alias(alias: &AliasTy, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
use std::fmt::Debug;
match alias {
chalk_ir::AliasTy::Projection(projection_ty) => {
Interner::debug_projection_ty(projection_ty, fmt)
}
chalk_ir::AliasTy::Opaque(opaque_ty) => Some(opaque_ty.fmt(fmt)),
AliasTy::Projection(projection_ty) => Interner::debug_projection_ty(projection_ty, fmt),
AliasTy::Opaque(opaque_ty) => Some(opaque_ty.fmt(fmt)),
}
}
fn debug_projection_ty(
proj: &chalk_ir::ProjectionTy<Interner>,
proj: &ProjectionTy,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt)))
}
fn debug_opaque_ty(
opaque_ty: &chalk_ir::OpaqueTy<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_opaque_ty(opaque_ty: &OpaqueTy, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", opaque_ty.opaque_ty_id))
}
fn debug_ty(ty: &chalk_ir::Ty<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
fn debug_ty(ty: &Ty, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", ty.data(Interner)))
}
fn debug_lifetime(
lifetime: &chalk_ir::Lifetime<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_lifetime(lifetime: &Lifetime, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", lifetime.data(Interner)))
}
fn debug_const(
constant: &chalk_ir::Const<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_const(constant: &Const, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", constant.data(Interner)))
}
@ -161,102 +151,99 @@ impl chalk_ir::interner::Interner for Interner {
}
fn debug_variable_kinds(
variable_kinds: &chalk_ir::VariableKinds<Self>,
variable_kinds: &VariableKinds,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", variable_kinds.as_slice(Interner)))
}
fn debug_variable_kinds_with_angles(
variable_kinds: &chalk_ir::VariableKinds<Self>,
variable_kinds: &VariableKinds,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", variable_kinds.inner_debug(Interner)))
}
fn debug_canonical_var_kinds(
canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Self>,
canonical_var_kinds: &CanonicalVarKinds,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", canonical_var_kinds.as_slice(Interner)))
}
fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
fn debug_goal(goal: &Goal, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
let goal_data = goal.data(Interner);
Some(write!(fmt, "{goal_data:?}"))
}
fn debug_goals(
goals: &chalk_ir::Goals<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
fn debug_goals(goals: &Goals, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", goals.debug(Interner)))
}
fn debug_program_clause_implication(
pci: &chalk_ir::ProgramClauseImplication<Interner>,
pci: &ProgramClauseImplication<Self>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", pci.debug(Interner)))
}
fn debug_program_clause(
clause: &chalk_ir::ProgramClause<Self>,
clause: &ProgramClause,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clause.data(Interner)))
}
fn debug_program_clauses(
clauses: &chalk_ir::ProgramClauses<Self>,
clauses: &ProgramClauses,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
}
fn debug_substitution(
substitution: &chalk_ir::Substitution<Interner>,
substitution: &Substitution,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", substitution.debug(Interner)))
}
fn debug_separator_trait_ref(
separator_trait_ref: &chalk_ir::SeparatorTraitRef<'_, Interner>,
separator_trait_ref: &SeparatorTraitRef<'_, Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", separator_trait_ref.debug(Interner)))
}
fn debug_quantified_where_clauses(
clauses: &chalk_ir::QuantifiedWhereClauses<Self>,
clauses: &QuantifiedWhereClauses,
fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
Some(write!(fmt, "{:?}", clauses.as_slice(Interner)))
}
fn debug_constraints(
_clauses: &chalk_ir::Constraints<Self>,
_clauses: &Constraints,
_fmt: &mut fmt::Formatter<'_>,
) -> Option<fmt::Result> {
None
}
fn intern_ty(self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType {
fn intern_ty(self, kind: TyKind) -> Self::InternedType {
let flags = kind.compute_flags(self);
Interned::new(InternedWrapper(chalk_ir::TyData { kind, flags }))
Interned::new(InternedWrapper(TyData { kind, flags }))
}
fn ty_data(self, ty: &Self::InternedType) -> &chalk_ir::TyData<Self> {
fn ty_data(self, ty: &Self::InternedType) -> &TyData {
&ty.0
}
fn intern_lifetime(self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime {
fn intern_lifetime(self, lifetime: LifetimeData) -> Self::InternedLifetime {
Interned::new(InternedWrapper(lifetime))
}
fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &chalk_ir::LifetimeData<Self> {
fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &LifetimeData {
&lifetime.0
}
fn intern_const(self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst {
fn intern_const(self, constant: ConstData) -> Self::InternedConst {
Interned::new(InternedWrapper(constant))
}
fn const_data(self, constant: &Self::InternedConst) -> &chalk_ir::ConstData<Self> {
fn const_data(self, constant: &Self::InternedConst) -> &ConstData {
&constant.0
}
@ -269,36 +256,33 @@ impl chalk_ir::interner::Interner for Interner {
!matches!(c1, ConstScalar::Bytes(..)) || !matches!(c2, ConstScalar::Bytes(..)) || (c1 == c2)
}
fn intern_generic_arg(
self,
parameter: chalk_ir::GenericArgData<Self>,
) -> Self::InternedGenericArg {
fn intern_generic_arg(self, parameter: GenericArgData) -> Self::InternedGenericArg {
parameter
}
fn generic_arg_data(
self,
parameter: &Self::InternedGenericArg,
) -> &chalk_ir::GenericArgData<Self> {
fn generic_arg_data(self, parameter: &Self::InternedGenericArg) -> &GenericArgData {
parameter
}
fn intern_goal(self, goal: GoalData<Self>) -> Self::InternedGoal {
fn intern_goal(self, goal: GoalData) -> Self::InternedGoal {
Arc::new(goal)
}
fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData<Self> {
fn goal_data(self, goal: &Self::InternedGoal) -> &GoalData {
goal
}
fn intern_goals<E>(
self,
data: impl IntoIterator<Item = Result<Goal<Self>, E>>,
data: impl IntoIterator<Item = Result<Goal, E>>,
) -> Result<Self::InternedGoals, E> {
// let hash =
// std::hash::BuildHasher::hash_one(&BuildHasherDefault::<FxHasher>::default(), &goal);
// Interned::new(InternedWrapper(PreHashedWrapper(goal, hash)))
data.into_iter().collect()
}
fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal<Interner>] {
fn goals_data(self, goals: &Self::InternedGoals) -> &[Goal] {
goals
}
@ -313,37 +297,28 @@ impl chalk_ir::interner::Interner for Interner {
&substitution.as_ref().0
}
fn intern_program_clause(
self,
data: chalk_ir::ProgramClauseData<Self>,
) -> Self::InternedProgramClause {
fn intern_program_clause(self, data: ProgramClauseData) -> Self::InternedProgramClause {
data
}
fn program_clause_data(
self,
clause: &Self::InternedProgramClause,
) -> &chalk_ir::ProgramClauseData<Self> {
fn program_clause_data(self, clause: &Self::InternedProgramClause) -> &ProgramClauseData {
clause
}
fn intern_program_clauses<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>,
data: impl IntoIterator<Item = Result<ProgramClause, E>>,
) -> Result<Self::InternedProgramClauses, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
fn program_clauses_data(
self,
clauses: &Self::InternedProgramClauses,
) -> &[chalk_ir::ProgramClause<Self>] {
fn program_clauses_data(self, clauses: &Self::InternedProgramClauses) -> &[ProgramClause] {
clauses
}
fn intern_quantified_where_clauses<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>,
data: impl IntoIterator<Item = Result<QuantifiedWhereClause, E>>,
) -> Result<Self::InternedQuantifiedWhereClauses, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
@ -351,27 +326,24 @@ impl chalk_ir::interner::Interner for Interner {
fn quantified_where_clauses_data(
self,
clauses: &Self::InternedQuantifiedWhereClauses,
) -> &[chalk_ir::QuantifiedWhereClause<Self>] {
) -> &[QuantifiedWhereClause] {
clauses
}
fn intern_generic_arg_kinds<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>,
data: impl IntoIterator<Item = Result<VariableKind, E>>,
) -> Result<Self::InternedVariableKinds, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
fn variable_kinds_data(
self,
parameter_kinds: &Self::InternedVariableKinds,
) -> &[chalk_ir::VariableKind<Self>] {
fn variable_kinds_data(self, parameter_kinds: &Self::InternedVariableKinds) -> &[VariableKind] {
&parameter_kinds.as_ref().0
}
fn intern_canonical_var_kinds<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>,
data: impl IntoIterator<Item = Result<CanonicalVarKind, E>>,
) -> Result<Self::InternedCanonicalVarKinds, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
}
@ -379,30 +351,30 @@ impl chalk_ir::interner::Interner for Interner {
fn canonical_var_kinds_data(
self,
canonical_var_kinds: &Self::InternedCanonicalVarKinds,
) -> &[chalk_ir::CanonicalVarKind<Self>] {
) -> &[CanonicalVarKind] {
canonical_var_kinds
}
fn intern_constraints<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>,
data: impl IntoIterator<Item = Result<InEnvironment<Constraint>, E>>,
) -> Result<Self::InternedConstraints, E> {
data.into_iter().collect()
}
fn constraints_data(
self,
constraints: &Self::InternedConstraints,
) -> &[chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] {
) -> &[InEnvironment<Constraint>] {
constraints
}
fn intern_variances<E>(
self,
data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>,
data: impl IntoIterator<Item = Result<Variance, E>>,
) -> Result<Self::InternedVariances, E> {
Ok(Interned::new(InternedWrapper(data.into_iter().collect::<Result<_, _>>()?)))
data.into_iter().collect::<Result<_, _>>()
}
fn variances_data(self, variances: &Self::InternedVariances) -> &[chalk_ir::Variance] {
fn variances_data(self, variances: &Self::InternedVariances) -> &[Variance] {
variances
}
}

View File

@ -37,22 +37,22 @@ mod tests;
mod test_db;
use std::{
collections::{hash_map::Entry, HashMap},
hash::Hash,
collections::hash_map::Entry,
hash::{BuildHasherDefault, Hash},
};
use chalk_ir::{
fold::{Shift, TypeFoldable},
interner::HasInterner,
visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor},
NoSolution, TyData,
NoSolution,
};
use either::Either;
use hir_def::{hir::ExprId, type_ref::Rawness, GeneralConstId, TypeOrConstParamId};
use hir_expand::name;
use la_arena::{Arena, Idx};
use mir::{MirEvalError, VTableMap};
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};
use syntax::ast::{make, ConstArg};
use traits::FnTrait;
use triomphe::Arc;
@ -152,32 +152,64 @@ pub type DomainGoal = chalk_ir::DomainGoal<Interner>;
pub type Goal = chalk_ir::Goal<Interner>;
pub type AliasEq = chalk_ir::AliasEq<Interner>;
pub type Solution = chalk_solve::Solution<Interner>;
pub type Constraint = chalk_ir::Constraint<Interner>;
pub type Constraints = chalk_ir::Constraints<Interner>;
pub type ConstrainedSubst = chalk_ir::ConstrainedSubst<Interner>;
pub type Guidance = chalk_solve::Guidance<Interner>;
pub type WhereClause = chalk_ir::WhereClause<Interner>;
pub type CanonicalVarKind = chalk_ir::CanonicalVarKind<Interner>;
pub type GoalData = chalk_ir::GoalData<Interner>;
pub type Goals = chalk_ir::Goals<Interner>;
pub type ProgramClauseData = chalk_ir::ProgramClauseData<Interner>;
pub type ProgramClause = chalk_ir::ProgramClause<Interner>;
pub type ProgramClauses = chalk_ir::ProgramClauses<Interner>;
pub type TyData = chalk_ir::TyData<Interner>;
pub type Variances = chalk_ir::Variances<Interner>;
/// A constant can have reference to other things. Memory map job is holding
/// the necessary bits of memory of the const eval session to keep the constant
/// meaningful.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct MemoryMap {
pub memory: HashMap<usize, Vec<u8>>,
pub vtable: VTableMap,
pub enum MemoryMap {
#[default]
Empty,
Simple(Box<[u8]>),
Complex(Box<ComplexMemoryMap>),
}
impl MemoryMap {
fn insert(&mut self, addr: usize, x: Vec<u8>) {
#[derive(Debug, Default, Clone, PartialEq, Eq)]
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) {
Entry::Occupied(mut e) => {
if e.get().len() < x.len() {
e.insert(x);
if e.get().len() < val.len() {
e.insert(val);
}
}
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
/// to them. It is useful when you want to load a constant with a memory map in a new memory. You can pass an
@ -185,22 +217,33 @@ impl MemoryMap {
fn transform_addresses(
&self,
mut f: impl FnMut(&[u8], usize) -> Result<usize, MirEvalError>,
) -> Result<HashMap<usize, usize>, MirEvalError> {
self.memory
.iter()
.map(|x| {
let addr = *x.0;
let align = if addr == 0 { 64 } else { (addr - (addr & (addr - 1))).min(64) };
Ok((addr, f(x.1, align)?))
})
.collect()
) -> Result<FxHashMap<usize, usize>, MirEvalError> {
let mut transform = |(addr, val): (&usize, &Box<[u8]>)| {
let addr = *addr;
let align = if addr == 0 { 64 } else { (addr - (addr & (addr - 1))).min(64) };
f(val, align).and_then(|it| Ok((addr, it)))
};
match self {
MemoryMap::Empty => Ok(Default::default()),
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 {
Some(&[])
} 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),
}
}
}
}
@ -208,7 +251,7 @@ impl MemoryMap {
/// A concrete constant value
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConstScalar {
Bytes(Vec<u8>, MemoryMap),
Bytes(Box<[u8]>, MemoryMap),
// FIXME: this is a hack to get around chalk not being able to represent unevaluatable
// constants
UnevaluatedConst(GeneralConstId, Substitution),

View File

@ -98,16 +98,16 @@ pub enum Operand {
}
impl Operand {
fn from_concrete_const(data: Vec<u8>, memory_map: MemoryMap, ty: Ty) -> Self {
fn from_concrete_const(data: Box<[u8]>, memory_map: MemoryMap, ty: Ty) -> Self {
Operand::Constant(intern_const_scalar(ConstScalar::Bytes(data, memory_map), ty))
}
fn from_bytes(data: Vec<u8>, ty: Ty) -> Self {
fn from_bytes(data: Box<[u8]>, ty: Ty) -> Self {
Operand::from_concrete_const(data, MemoryMap::default(), ty)
}
fn const_zst(ty: Ty) -> Operand {
Self::from_bytes(vec![], ty)
Self::from_bytes(Box::default(), ty)
}
fn from_fn(
@ -118,7 +118,7 @@ impl Operand {
let ty =
chalk_ir::TyKind::FnDef(CallableDefId::FunctionId(func_id).to_chalk(db), generic_args)
.intern(Interner);
Operand::from_bytes(vec![], ty)
Operand::from_bytes(Box::default(), ty)
}
}

View File

@ -1,13 +1,6 @@
//! This module provides a MIR interpreter, which is used in const eval.
use std::{
borrow::Cow,
cell::RefCell,
collections::{HashMap, HashSet},
fmt::Write,
iter, mem,
ops::Range,
};
use std::{borrow::Cow, cell::RefCell, fmt::Write, iter, mem, ops::Range};
use base_db::{CrateId, FileId};
use chalk_ir::{cast::Cast, Mutability};
@ -40,8 +33,8 @@ use crate::{
name, static_lifetime,
traits::FnTrait,
utils::{detect_variant_from_bytes, ClosureSubst},
CallableDefId, ClosureId, Const, ConstScalar, FnDefId, Interner, MemoryMap, Substitution,
TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
CallableDefId, ClosureId, ComplexMemoryMap, Const, ConstScalar, FnDefId, Interner, MemoryMap,
Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
};
use super::{
@ -98,6 +91,15 @@ impl VTableMap {
let id = from_bytes!(usize, bytes);
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)]
@ -251,13 +253,6 @@ impl From<Interval> for 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]> {
Ok(match self {
IntervalOrOwned::Owned(o) => o,
@ -291,8 +286,8 @@ impl Address {
}
}
fn to_bytes(&self) -> Vec<u8> {
usize::to_le_bytes(self.to_usize()).to_vec()
fn to_bytes(&self) -> [u8; mem::size_of::<usize>()] {
usize::to_le_bytes(self.to_usize())
}
fn to_usize(&self) -> usize {
@ -510,6 +505,20 @@ struct Locals {
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(
db: &dyn HirDatabase,
body: Arc<MirBody>,
@ -520,27 +529,31 @@ pub fn interpret_mir(
// (and probably should) do better here, for example by excluding bindings outside of the target expression.
assert_placeholder_ty_is_unused: bool,
trait_env: Option<Arc<TraitEnvironment>>,
) -> (Result<Const>, String, String) {
) -> (Result<Const>, MirOutput) {
let ty = body.locals[return_slot()].ty.clone();
let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env);
let it: Result<Const> = (|| {
if evaluator.ptr_size() != std::mem::size_of::<usize>() {
not_supported!("targets with different pointer size from host");
}
let bytes = evaluator.interpret_mir(body.clone(), None.into_iter())?;
let interval = evaluator.interpret_mir(body.clone(), None.into_iter())?;
let bytes = interval.get(&evaluator)?;
let mut memory_map = evaluator.create_memory_map(
&bytes,
bytes,
&ty,
&Locals { ptr: ArenaMap::new(), body, drop_flags: DropFlags::default() },
)?;
memory_map.vtable = evaluator.vtable_map.clone();
let bytes = bytes.into();
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,
String::from_utf8_lossy(&evaluator.stdout).into_owned(),
String::from_utf8_lossy(&evaluator.stderr).into_owned(),
)
(it, MirOutput { stdout: evaluator.stdout, stderr: evaluator.stderr })
}
#[cfg(test)]
@ -562,7 +575,7 @@ impl Evaluator<'_> {
code_stack: vec![],
vtable_map: VTableMap::default(),
thread_local_storage: TlsData::default(),
static_locations: HashMap::default(),
static_locations: Default::default(),
db,
random_state: oorandom::Rand64::new(0),
trait_env: trait_env.unwrap_or_else(|| db.trait_environment_for_body(owner)),
@ -573,11 +586,11 @@ impl Evaluator<'_> {
stack_depth_limit: 100,
execution_limit: EXECUTION_LIMIT,
memory_limit: 1000_000_000, // 2GB, 1GB for stack and 1GB for heap
layout_cache: RefCell::new(HashMap::default()),
projected_ty_cache: RefCell::new(HashMap::default()),
not_special_fn_cache: RefCell::new(HashSet::default()),
mir_or_dyn_index_cache: RefCell::new(HashMap::default()),
unused_locals_store: RefCell::new(HashMap::default()),
layout_cache: RefCell::new(Default::default()),
projected_ty_cache: RefCell::new(Default::default()),
not_special_fn_cache: RefCell::new(Default::default()),
mir_or_dyn_index_cache: RefCell::new(Default::default()),
unused_locals_store: RefCell::new(Default::default()),
cached_ptr_size: match db.target_data_layout(crate_id) {
Some(it) => it.pointer_size.bytes_usize(),
None => 8,
@ -803,11 +816,11 @@ impl Evaluator<'_> {
})
}
fn interpret_mir(
&mut self,
fn interpret_mir<'slf>(
&'slf mut self,
body: Arc<MirBody>,
args: impl Iterator<Item = IntervalOrOwned>,
) -> Result<Vec<u8>> {
) -> Result<Interval> {
if let Some(it) = self.stack_depth_limit.checked_sub(1) {
self.stack_depth_limit = it;
} else {
@ -837,8 +850,8 @@ impl Evaluator<'_> {
match &statement.kind {
StatementKind::Assign(l, r) => {
let addr = self.place_addr(l, &locals)?;
let result = self.eval_rvalue(r, &mut locals)?.to_vec(&self)?;
self.write_memory(addr, &result)?;
let result = self.eval_rvalue(r, &mut locals)?;
self.copy_from_interval_or_owned(addr, result)?;
locals
.drop_flags
.add_place(l.clone(), &locals.body.projection_store);
@ -957,7 +970,7 @@ impl Evaluator<'_> {
None => {
self.code_stack = prev_code_stack;
self.stack_depth_limit += 1;
return Ok(return_interval.get(self)?.to_vec());
return Ok(return_interval);
}
Some(bb) => {
// We don't support const promotion, so we can't truncate the stack yet.
@ -1050,7 +1063,7 @@ impl Evaluator<'_> {
Rvalue::Use(it) => Borrowed(self.eval_operand(it, locals)?),
Rvalue::Ref(_, p) => {
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 {
r.extend(metadata.get(self)?);
}
@ -1283,7 +1296,7 @@ impl Evaluator<'_> {
not_supported!("unsized box initialization");
};
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::Aggregate(kind, values) => {
@ -1715,7 +1728,18 @@ impl Evaluator<'_> {
}
let addr = self.heap_allocate(size, align)?;
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))
}
@ -1767,6 +1791,13 @@ impl Evaluator<'_> {
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<()> {
if r.size == 0 {
return Ok(());
@ -1887,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(
this: &Evaluator<'_>,
bytes: &[u8],
ty: &Ty,
locals: &Locals,
mm: &mut MemoryMap,
mm: &mut ComplexMemoryMap,
) -> Result<()> {
match ty.kind(Interner) {
TyKind::Ref(_, _, t) => {
@ -1903,7 +1939,7 @@ impl Evaluator<'_> {
let addr_usize = from_bytes!(usize, bytes);
mm.insert(
addr_usize,
this.read_memory(Address::from_usize(addr_usize), size)?.to_vec(),
this.read_memory(Address::from_usize(addr_usize), size)?.into(),
)
}
None => {
@ -1929,7 +1965,7 @@ impl Evaluator<'_> {
let size = element_size * count;
let addr = Address::from_bytes(addr)?;
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 {
for i in 0..count {
let offset = element_size * i;
@ -2002,15 +2038,15 @@ impl Evaluator<'_> {
}
Ok(())
}
let mut mm = MemoryMap::default();
rec(self, bytes, ty, locals, &mut mm)?;
let mut mm = ComplexMemoryMap::default();
rec(&self, bytes, ty, locals, &mut mm)?;
Ok(mm)
}
fn patch_addresses(
fn patch_addresses<'vtable>(
&mut self,
patch_map: &HashMap<usize, usize>,
old_vtable: &VTableMap,
patch_map: &FxHashMap<usize, usize>,
ty_of_bytes: impl Fn(&[u8]) -> Result<&'vtable Ty> + Copy,
addr: Address,
ty: &Ty,
locals: &Locals,
@ -2037,7 +2073,7 @@ impl Evaluator<'_> {
}
}
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);
self.write_memory(addr, &new_id.to_le_bytes())?;
}
@ -2048,7 +2084,7 @@ impl Evaluator<'_> {
let ty = ty.clone().substitute(Interner, subst);
self.patch_addresses(
patch_map,
old_vtable,
ty_of_bytes,
addr.offset(offset),
&ty,
locals,
@ -2070,7 +2106,7 @@ impl Evaluator<'_> {
let ty = ty.clone().substitute(Interner, subst);
self.patch_addresses(
patch_map,
old_vtable,
ty_of_bytes,
addr.offset(offset),
&ty,
locals,
@ -2083,7 +2119,7 @@ impl Evaluator<'_> {
for (id, ty) in subst.iter(Interner).enumerate() {
let ty = ty.assert_ty_ref(Interner); // Tuple only has type argument
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) => {
@ -2095,7 +2131,7 @@ impl Evaluator<'_> {
for i in 0..len {
self.patch_addresses(
patch_map,
old_vtable,
ty_of_bytes,
addr.offset(i * size),
inner,
locals,
@ -2166,14 +2202,14 @@ impl Evaluator<'_> {
.map_err(|it| MirEvalError::MirLowerErrorForClosure(closure, it))?;
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 {
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())))
.collect::<Result<Vec<_>>>()?;
let bytes = self
let interval = self
.interpret_mir(mir_body, arg_bytes.into_iter().map(IntervalOrOwned::Owned))
.map_err(|e| {
MirEvalError::InFunction(
@ -2181,7 +2217,7 @@ impl Evaluator<'_> {
vec![(Either::Right(closure), span, locals.body.owner)],
)
})?;
destination.write_from_bytes(self, &bytes)?;
destination.write_from_interval(self, interval)?;
Ok(None)
}
@ -2374,7 +2410,7 @@ impl Evaluator<'_> {
vec![(Either::Left(def), span, locals.body.owner)],
)
})?;
destination.write_from_bytes(self, &result)?;
destination.write_from_interval(self, result)?;
None
})
}
@ -2552,7 +2588,7 @@ impl Evaluator<'_> {
body,
locals,
drop_fn,
[IntervalOrOwned::Owned(addr.to_bytes())].into_iter(),
iter::once(IntervalOrOwned::Owned(addr.to_bytes().to_vec())),
span,
Interval { addr: Address::Invalid(0), size: 0 },
None,
@ -2680,11 +2716,12 @@ pub fn render_const_using_debug_impl(
) else {
not_supported!("std::fmt::format not found");
};
let message_string = evaluator.interpret_mir(
let interval = evaluator.interpret_mir(
db.mir_body(format_fn.into()).map_err(|e| MirEvalError::MirLowerError(format_fn, e))?,
[IntervalOrOwned::Borrowed(Interval { addr: a3, size: evaluator.ptr_size() * 6 })]
.into_iter(),
)?;
let message_string = interval.get(&evaluator)?;
let addr =
Address::from_bytes(&message_string[evaluator.ptr_size()..2 * evaluator.ptr_size()])?;
let size = from_bytes!(usize, message_string[2 * evaluator.ptr_size()..]);

View File

@ -322,12 +322,13 @@ impl Evaluator<'_> {
let hir_def::resolver::ValueNs::FunctionId(format_fn) = format_fn else {
not_supported!("std::fmt::format is not a function")
};
let message_string = self.interpret_mir(
let interval = self.interpret_mir(
self.db
.mir_body(format_fn.into())
.map_err(|e| MirEvalError::MirLowerError(format_fn, e))?,
args.map(|x| IntervalOrOwned::Owned(x.clone())),
)?;
let message_string = interval.get(self)?;
let addr =
Address::from_bytes(&message_string[self.ptr_size()..2 * self.ptr_size()])?;
let size = from_bytes!(usize, message_string[2 * self.ptr_size()..]);

View File

@ -31,9 +31,9 @@ fn eval_main(db: &TestDB, file_id: FileId) -> Result<(String, String), MirEvalEr
db.trait_environment(func_id.into()),
)
.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?;
Ok((stdout, stderr))
Ok((output.stdout().into_owned(), output.stderr().into_owned()))
}
fn check_pass(ra_fixture: &str) {

View File

@ -540,7 +540,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.write_bytes_to_place(
then_target,
place.clone(),
vec![1],
Box::new([1]),
TyBuilder::bool(),
MirSpan::Unknown,
)?;
@ -548,7 +548,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.write_bytes_to_place(
else_target,
place,
vec![0],
Box::new([0]),
TyBuilder::bool(),
MirSpan::Unknown,
)?;
@ -602,7 +602,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
generic_args,
)
.intern(Interner);
let func = Operand::from_bytes(vec![], ty);
let func = Operand::from_bytes(Box::default(), ty);
return self.lower_call_and_args(
func,
iter::once(*callee).chain(args.iter().copied()),
@ -615,7 +615,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
let callee_ty = self.expr_ty_after_adjustments(*callee);
match &callee_ty.kind(Interner) {
chalk_ir::TyKind::FnDef(..) => {
let func = Operand::from_bytes(vec![], callee_ty.clone());
let func = Operand::from_bytes(Box::default(), callee_ty.clone());
self.lower_call_and_args(
func,
args.iter().copied(),
@ -1113,7 +1113,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
Some("start") => lp.take(),
Some("end") => rp.take(),
Some("exhausted") => {
Some(Operand::from_bytes(vec![0], TyBuilder::bool()))
Some(Operand::from_bytes(Box::new([0]), TyBuilder::bool()))
}
_ => None,
};
@ -1395,46 +1395,43 @@ impl<'ctx> MirLowerCtx<'ctx> {
}
fn lower_literal_to_operand(&mut self, ty: Ty, l: &Literal) -> Result<Operand> {
let size = self
.db
.layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner))?
.size
.bytes_usize();
let bytes = match l {
let size = || {
self.db
.layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner))
.map(|it| it.size.bytes_usize())
};
const USIZE_SIZE: usize = mem::size_of::<usize>();
let bytes: Box<[_]> = match l {
hir_def::hir::Literal::String(b) => {
let b = b.as_bytes();
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
data.extend(0usize.to_le_bytes());
data.extend(b.len().to_le_bytes());
let mut mm = MemoryMap::default();
mm.insert(0, b.to_vec());
return Ok(Operand::from_concrete_const(data, mm, ty));
let mut data = [0; { 2 * USIZE_SIZE }];
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
let mm = MemoryMap::simple(b.as_bytes().into());
return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
}
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 = Vec::with_capacity(mem::size_of::<usize>() * 2);
data.extend(0usize.to_le_bytes());
data.extend(bytes.len().to_le_bytes());
let mut mm = MemoryMap::default();
mm.insert(0, bytes);
return Ok(Operand::from_concrete_const(data, mm, ty));
let mut data = [0; { 2 * USIZE_SIZE }];
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
data[USIZE_SIZE..].copy_from_slice(&bytes.len().to_le_bytes());
let mm = MemoryMap::simple(bytes);
return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
}
hir_def::hir::Literal::ByteString(b) => {
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
data.extend(0usize.to_le_bytes());
data.extend(b.len().to_le_bytes());
let mut mm = MemoryMap::default();
mm.insert(0, b.to_vec());
return Ok(Operand::from_concrete_const(data, mm, ty));
let mut data = [0; { 2 * USIZE_SIZE }];
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
let mm = MemoryMap::simple(b.clone());
return Ok(Operand::from_concrete_const(Box::new(data), mm, ty));
}
hir_def::hir::Literal::Char(c) => u32::from(*c).to_le_bytes().into(),
hir_def::hir::Literal::Bool(b) => vec![*b as u8],
hir_def::hir::Literal::Int(it, _) => it.to_le_bytes()[0..size].into(),
hir_def::hir::Literal::Uint(it, _) => it.to_le_bytes()[0..size].into(),
hir_def::hir::Literal::Float(f, _) => match size {
8 => f.into_f64().to_le_bytes().into(),
4 => f.into_f32().to_le_bytes().into(),
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::Int(it, _) => Box::from(&it.to_le_bytes()[0..size()?]),
hir_def::hir::Literal::Uint(it, _) => Box::from(&it.to_le_bytes()[0..size()?]),
hir_def::hir::Literal::Float(f, _) => match size()? {
8 => Box::new(f.into_f64().to_le_bytes()),
4 => Box::new(f.into_f32().to_le_bytes()),
_ => {
return Err(MirLowerError::TypeError("float with size other than 4 or 8 bytes"))
}
@ -1483,7 +1480,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
&mut self,
prev_block: BasicBlockId,
place: Place,
cv: Vec<u8>,
cv: Box<[u8]>,
ty: Ty,
span: MirSpan,
) -> Result<()> {

View File

@ -244,7 +244,7 @@ impl MirLowerCtx<'_> {
);
} else {
let c = Operand::from_concrete_const(
pattern_len.to_le_bytes().to_vec(),
pattern_len.to_le_bytes().into(),
MemoryMap::default(),
TyBuilder::usize(),
);

View File

@ -2183,7 +2183,7 @@ impl Function {
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 {
Ok(_) => "pass".to_string(),
Err(e) => {
@ -2192,10 +2192,12 @@ impl Function {
r
}
};
let stdout = output.stdout().into_owned();
if !stdout.is_empty() {
text += "\n--------- stdout ---------\n";
text += &stdout;
}
let stderr = output.stdout().into_owned();
if !stderr.is_empty() {
text += "\n--------- stderr ---------\n";
text += &stderr;