Merge commit 'af40101841' into sync-from-ra

This commit is contained in:
Laurențiu Nicola 2024-01-08 11:29:25 +02:00
parent 79fa976864
commit 3afeb24198
115 changed files with 3106 additions and 3623 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 {
@ -609,7 +609,7 @@ fn render_const_scalar(
}
hir_def::AdtId::EnumId(e) => {
let Some((var_id, var_layout)) =
detect_variant_from_bytes(&layout, f.db, trait_env.clone(), b, e)
detect_variant_from_bytes(&layout, f.db, trait_env, b, e)
else {
return f.write_str("<failed-to-detect-variant>");
};

View file

@ -41,9 +41,10 @@ use hir_def::{
resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs},
type_ref::TypeRef,
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, ItemContainerId, Lookup,
TraitId, TypeAliasId, VariantId,
TraitId, TupleFieldId, TupleId, TypeAliasId, VariantId,
};
use hir_expand::name::{name, Name};
use indexmap::IndexSet;
use la_arena::{ArenaMap, Entry};
use rustc_hash::{FxHashMap, FxHashSet};
use stdx::{always, never};
@ -403,11 +404,15 @@ pub struct InferenceResult {
/// For each method call expr, records the function it resolves to.
method_resolutions: FxHashMap<ExprId, (FunctionId, Substitution)>,
/// For each field access expr, records the field it resolves to.
field_resolutions: FxHashMap<ExprId, FieldId>,
field_resolutions: FxHashMap<ExprId, Either<FieldId, TupleFieldId>>,
/// For each struct literal or pattern, records the variant it resolves to.
variant_resolutions: FxHashMap<ExprOrPatId, VariantId>,
/// For each associated item record what it resolves to
assoc_resolutions: FxHashMap<ExprOrPatId, (AssocItemId, Substitution)>,
/// Whenever a tuple field expression access a tuple field, we allocate a tuple id in
/// [`InferenceContext`] and store the tuples substitution there. This map is the reverse of
/// that which allows us to resolve a [`TupleFieldId`]s type.
pub tuple_field_access_types: FxHashMap<TupleId, Substitution>,
pub diagnostics: Vec<InferenceDiagnostic>,
pub type_of_expr: ArenaMap<ExprId, Ty>,
/// For each pattern record the type it resolves to.
@ -447,7 +452,7 @@ impl InferenceResult {
pub fn method_resolution(&self, expr: ExprId) -> Option<(FunctionId, Substitution)> {
self.method_resolutions.get(&expr).cloned()
}
pub fn field_resolution(&self, expr: ExprId) -> Option<FieldId> {
pub fn field_resolution(&self, expr: ExprId) -> Option<Either<FieldId, TupleFieldId>> {
self.field_resolutions.get(&expr).copied()
}
pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantId> {
@ -517,6 +522,8 @@ pub(crate) struct InferenceContext<'a> {
/// The traits in scope, disregarding block modules. This is used for caching purposes.
traits_in_scope: FxHashSet<TraitId>,
pub(crate) result: InferenceResult,
tuple_field_accesses_rev:
IndexSet<Substitution, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>,
/// The return type of the function being inferred, the closure or async block if we're
/// currently within one.
///
@ -598,6 +605,7 @@ impl<'a> InferenceContext<'a> {
InferenceContext {
result: InferenceResult::default(),
table: unify::InferenceTable::new(db, trait_env),
tuple_field_accesses_rev: Default::default(),
return_ty: TyKind::Error.intern(Interner), // set in collect_* calls
resume_yield_tys: None,
return_coercion: None,
@ -621,7 +629,13 @@ impl<'a> InferenceContext<'a> {
// used this function for another workaround, mention it here. If you really need this function and believe that
// there is no problem in it being `pub(crate)`, remove this comment.
pub(crate) fn resolve_all(self) -> InferenceResult {
let InferenceContext { mut table, mut result, deferred_cast_checks, .. } = self;
let InferenceContext {
mut table,
mut result,
deferred_cast_checks,
tuple_field_accesses_rev,
..
} = self;
// Destructure every single field so whenever new fields are added to `InferenceResult` we
// don't forget to handle them here.
let InferenceResult {
@ -645,6 +659,7 @@ impl<'a> InferenceContext<'a> {
// to resolve them here.
closure_info: _,
mutated_bindings_in_closure: _,
tuple_field_access_types: _,
} = &mut result;
table.fallback_if_possible();
@ -720,6 +735,11 @@ impl<'a> InferenceContext<'a> {
for adjustment in pat_adjustments.values_mut().flatten() {
*adjustment = table.resolve_completely(adjustment.clone());
}
result.tuple_field_access_types = tuple_field_accesses_rev
.into_iter()
.enumerate()
.map(|(idx, subst)| (TupleId(idx as u32), table.resolve_completely(subst)))
.collect();
result
}

View file

@ -1,18 +1,19 @@
//! 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,
fold::{FallibleTypeFolder, TypeFoldable},
AliasEq, AliasTy, BoundVar, DebruijnIndex, FnSubst, Mutability, TyKind, WhereClause,
};
use either::Either;
use hir_def::{
data::adt::VariantData,
hir::{Array, BinaryOp, BindingId, CaptureBy, Expr, ExprId, Pat, PatId, Statement, UnaryOp},
lang_item::LangItem,
resolver::{resolver_for_expr, ResolveValueResult, ValueNs},
DefWithBodyId, FieldId, HasModule, VariantId,
DefWithBodyId, FieldId, HasModule, TupleFieldId, TupleId, VariantId,
};
use hir_expand::name;
use rustc_hash::FxHashMap;
@ -129,7 +130,7 @@ impl HirPlace {
ctx.owner.module(ctx.db.upcast()).krate(),
);
}
ty.clone()
ty
}
fn capture_kind_of_truncated_place(
@ -186,7 +187,7 @@ impl CapturedItem {
result = format!("*{result}");
field_need_paren = true;
}
ProjectionElem::Field(f) => {
ProjectionElem::Field(Either::Left(f)) => {
if field_need_paren {
result = format!("({result})");
}
@ -207,7 +208,15 @@ impl CapturedItem {
result = format!("{result}.{field}");
field_need_paren = false;
}
&ProjectionElem::TupleOrClosureField(field) => {
ProjectionElem::Field(Either::Right(f)) => {
let field = f.index;
if field_need_paren {
result = format!("({result})");
}
result = format!("{result}.{field}");
field_need_paren = false;
}
&ProjectionElem::ClosureField(field) => {
if field_need_paren {
result = format!("({result})");
}
@ -236,7 +245,7 @@ pub(crate) struct CapturedItemWithoutTy {
impl CapturedItemWithoutTy {
fn with_ty(self, ctx: &mut InferenceContext<'_>) -> CapturedItem {
let ty = self.place.ty(ctx).clone();
let ty = self.place.ty(ctx);
let ty = match &self.kind {
CaptureKind::ByValue => ty,
CaptureKind::ByRef(bk) => {
@ -329,15 +338,10 @@ impl InferenceContext<'_> {
}
}
}
Expr::Field { expr, name } => {
Expr::Field { expr, name: _ } => {
let mut place = self.place_of_expr(*expr)?;
if let TyKind::Tuple(..) = self.expr_ty(*expr).kind(Interner) {
let index = name.as_tuple_index()?;
place.projections.push(ProjectionElem::TupleOrClosureField(index))
} else {
let field = self.result.field_resolution(tgt_expr)?;
place.projections.push(ProjectionElem::Field(field));
}
let field = self.result.field_resolution(tgt_expr)?;
place.projections.push(ProjectionElem::Field(field));
return Some(place);
}
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
@ -392,7 +396,7 @@ impl InferenceContext<'_> {
fn consume_place(&mut self, place: HirPlace, span: MirSpan) {
if self.is_upvar(&place) {
let ty = place.ty(self).clone();
let ty = place.ty(self);
let kind = if self.is_ty_copy(ty) {
CaptureKind::ByRef(BorrowKind::Shared)
} else {
@ -598,7 +602,7 @@ impl InferenceContext<'_> {
self.consume_expr(expr);
}
}
Expr::Index { base, index } => {
Expr::Index { base, index, is_assignee_expr: _ } => {
self.select_from_expr(*base);
self.consume_expr(*index);
}
@ -774,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![] };
@ -825,7 +829,10 @@ impl InferenceContext<'_> {
let it = al.iter().zip(fields.clone()).chain(ar.iter().rev().zip(fields.rev()));
for (arg, i) in it {
let mut p = place.clone();
p.projections.push(ProjectionElem::TupleOrClosureField(i));
p.projections.push(ProjectionElem::Field(Either::Right(TupleFieldId {
tuple: TupleId(!0), // dummy this, as its unused anyways
index: i as u32,
})));
self.consume_with_pat(p, *arg);
}
}
@ -850,10 +857,10 @@ impl InferenceContext<'_> {
continue;
};
let mut p = place.clone();
p.projections.push(ProjectionElem::Field(FieldId {
p.projections.push(ProjectionElem::Field(Either::Left(FieldId {
parent: variant.into(),
local_id,
}));
})));
self.consume_with_pat(p, arg);
}
}
@ -894,10 +901,10 @@ impl InferenceContext<'_> {
al.iter().zip(fields.clone()).chain(ar.iter().rev().zip(fields.rev()));
for (arg, (i, _)) in it {
let mut p = place.clone();
p.projections.push(ProjectionElem::Field(FieldId {
p.projections.push(ProjectionElem::Field(Either::Left(FieldId {
parent: variant.into(),
local_id: i,
}));
})));
self.consume_with_pat(p, *arg);
}
}

View file

@ -6,6 +6,7 @@ use std::{
};
use chalk_ir::{cast::Cast, fold::Shift, DebruijnIndex, Mutability, TyVariableKind};
use either::Either;
use hir_def::{
generics::TypeOrConstParamData,
hir::{
@ -13,7 +14,7 @@ use hir_def::{
},
lang_item::{LangItem, LangItemTarget},
path::{GenericArg, GenericArgs},
BlockId, ConstParamId, FieldId, ItemContainerId, Lookup,
BlockId, ConstParamId, FieldId, ItemContainerId, Lookup, TupleFieldId, TupleId,
};
use hir_expand::name::{name, Name};
use stdx::always;
@ -744,7 +745,7 @@ impl InferenceContext<'_> {
(RangeOp::Inclusive, _, None) => self.err_ty(),
}
}
Expr::Index { base, index } => {
Expr::Index { base, index, is_assignee_expr } => {
let base_ty = self.infer_expr_inner(*base, &Expectation::none());
let index_ty = self.infer_expr(*index, &Expectation::none());
@ -772,11 +773,24 @@ impl InferenceContext<'_> {
.build();
self.write_method_resolution(tgt_expr, func, substs);
}
self.resolve_associated_type_with_params(
self_ty,
self.resolve_ops_index_output(),
&[index_ty.cast(Interner)],
)
let assoc = self.resolve_ops_index_output();
let res = self.resolve_associated_type_with_params(
self_ty.clone(),
assoc,
&[index_ty.clone().cast(Interner)],
);
if *is_assignee_expr {
if let Some(index_trait) = self.resolve_lang_trait(LangItem::IndexMut) {
let trait_ref = TyBuilder::trait_ref(self.db, index_trait)
.push(self_ty)
.fill(|_| index_ty.clone().cast(Interner))
.build();
self.push_obligation(trait_ref.cast(Interner));
}
}
res
} else {
self.err_ty()
}
@ -964,7 +978,7 @@ impl InferenceContext<'_> {
.push(callee_ty.clone())
.push(TyBuilder::tuple_with(params.iter().cloned()))
.build();
self.write_method_resolution(tgt_expr, func, subst.clone());
self.write_method_resolution(tgt_expr, func, subst);
}
}
@ -1393,7 +1407,7 @@ impl InferenceContext<'_> {
&mut self,
receiver_ty: &Ty,
name: &Name,
) -> Option<(Ty, Option<FieldId>, Vec<Adjustment>, bool)> {
) -> Option<(Ty, Either<FieldId, TupleFieldId>, Vec<Adjustment>, bool)> {
let mut autoderef = Autoderef::new(&mut self.table, receiver_ty.clone(), false);
let mut private_field = None;
let res = autoderef.by_ref().find_map(|(derefed_ty, _)| {
@ -1405,7 +1419,20 @@ impl InferenceContext<'_> {
.get(idx)
.map(|a| a.assert_ty_ref(Interner))
.cloned()
.map(|ty| (None, ty))
.map(|ty| {
(
Either::Right(TupleFieldId {
tuple: TupleId(
self.tuple_field_accesses_rev
.insert_full(substs.clone())
.0
as u32,
),
index: idx as u32,
}),
ty,
)
})
});
}
TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => {
@ -1431,7 +1458,7 @@ impl InferenceContext<'_> {
let ty = self.db.field_types(field_id.parent)[field_id.local_id]
.clone()
.substitute(Interner, &parameters);
Some((Some(field_id), ty))
Some((Either::Left(field_id), ty))
});
Some(match res {
@ -1451,7 +1478,7 @@ impl InferenceContext<'_> {
let ty = self.insert_type_vars(ty);
let ty = self.normalize_associated_types_in(ty);
(ty, Some(field_id), adjustments, false)
(ty, Either::Left(field_id), adjustments, false)
}
})
}
@ -1474,11 +1501,9 @@ impl InferenceContext<'_> {
match self.lookup_field(&receiver_ty, name) {
Some((ty, field_id, adjustments, is_public)) => {
self.write_expr_adj(receiver, adjustments);
if let Some(field_id) = field_id {
self.result.field_resolutions.insert(tgt_expr, field_id);
}
self.result.field_resolutions.insert(tgt_expr, field_id);
if !is_public {
if let Some(field) = field_id {
if let Either::Left(field) = field_id {
// FIXME: Merge this diagnostic into UnresolvedField?
self.result
.diagnostics
@ -1568,9 +1593,7 @@ impl InferenceContext<'_> {
{
Some((ty, field_id, adjustments, _public)) => {
self.write_expr_adj(receiver, adjustments);
if let Some(field_id) = field_id {
self.result.field_resolutions.insert(tgt_expr, field_id);
}
self.result.field_resolutions.insert(tgt_expr, field_id);
Some(ty)
}
None => None,

View file

@ -96,7 +96,7 @@ impl InferenceContext<'_> {
Expr::RecordLit { path: _, fields, spread, ellipsis: _, is_assignee_expr: _ } => {
self.infer_mut_not_expr_iter(fields.iter().map(|it| it.expr).chain(*spread))
}
&Expr::Index { base, index } => {
&Expr::Index { base, index, is_assignee_expr: _ } => {
if mutability == Mutability::Mut {
if let Some((f, _)) = self.result.method_resolutions.get_mut(&tgt_expr) {
if let Some(index_trait) = self

View file

@ -233,7 +233,6 @@ impl InferenceContext<'_> {
};
let mut expectations_iter = expectations
.iter()
.cloned()
.map(|a| a.assert_ty_ref(Interner).clone())
.chain(repeat_with(|| self.table.new_type_var()));
@ -336,7 +335,7 @@ impl InferenceContext<'_> {
&Pat::Lit(expr) => {
// Don't emit type mismatches again, the expression lowering already did that.
let ty = self.infer_lit_pat(expr, &expected);
self.write_pat_ty(pat, ty.clone());
self.write_pat_ty(pat, ty);
return self.pat_ty_after_adjustment(pat);
}
Pat::Box { inner } => match self.resolve_boxed_box() {

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

@ -164,7 +164,7 @@ fn layout_of_simd_ty(
};
// Compute the ABI of the element type:
let e_ly = db.layout_of_ty(e_ty, env.clone())?;
let e_ly = db.layout_of_ty(e_ty, env)?;
let Abi::Scalar(e_abi) = e_ly.abi else {
return Err(LayoutError::Unknown);
};
@ -204,17 +204,17 @@ pub fn layout_of_ty_query(
};
let cx = LayoutCx { target: &target };
let dl = &*cx.current_data_layout();
let ty = normalize(db, trait_env.clone(), ty.clone());
let ty = normalize(db, trait_env.clone(), ty);
let result = match ty.kind(Interner) {
TyKind::Adt(AdtId(def), subst) => {
if let hir_def::AdtId::StructId(s) = def {
let data = db.struct_data(*s);
let repr = data.repr.unwrap_or_default();
if repr.simd() {
return layout_of_simd_ty(db, *s, subst, trait_env.clone(), &target);
return layout_of_simd_ty(db, *s, subst, trait_env, &target);
}
};
return db.layout_of_adt(*def, subst.clone(), trait_env.clone());
return db.layout_of_adt(*def, subst.clone(), trait_env);
}
TyKind::Scalar(s) => match s {
chalk_ir::Scalar::Bool => Layout::scalar(
@ -280,7 +280,7 @@ pub fn layout_of_ty_query(
}
TyKind::Array(element, count) => {
let count = try_const_usize(db, &count).ok_or(LayoutError::HasErrorConst)? as u64;
let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
let element = db.layout_of_ty(element.clone(), trait_env)?;
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?;
let abi = if count != 0 && matches!(element.abi, Abi::Uninhabited) {
@ -303,7 +303,7 @@ pub fn layout_of_ty_query(
}
}
TyKind::Slice(element) => {
let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
let element = db.layout_of_ty(element.clone(), trait_env)?;
Layout {
variants: Variants::Single { index: struct_variant_idx() },
fields: FieldsShape::Array { stride: element.size, count: 0 },
@ -345,7 +345,7 @@ pub fn layout_of_ty_query(
}))
.intern(Interner);
}
unsized_part = normalize(db, trait_env.clone(), unsized_part);
unsized_part = normalize(db, trait_env, unsized_part);
let metadata = match unsized_part.kind(Interner) {
TyKind::Slice(_) | TyKind::Str => {
scalar_unit(dl, Primitive::Int(dl.ptr_sized_integer(), false))
@ -384,7 +384,7 @@ pub fn layout_of_ty_query(
match impl_trait_id {
crate::ImplTraitId::ReturnTypeImplTrait(func, idx) => {
let infer = db.infer(func.into());
return db.layout_of_ty(infer.type_of_rpit[idx].clone(), trait_env.clone());
return db.layout_of_ty(infer.type_of_rpit[idx].clone(), trait_env);
}
crate::ImplTraitId::AsyncBlockTypeImplTrait(_, _) => {
return Err(LayoutError::NotImplemented)

View file

@ -220,6 +220,36 @@ fn recursive() {
);
}
#[test]
fn repr_packed() {
size_and_align! {
#[repr(packed)]
struct Goal;
}
size_and_align! {
#[repr(packed(2))]
struct Goal;
}
size_and_align! {
#[repr(packed(4))]
struct Goal;
}
size_and_align! {
#[repr(packed)]
struct Goal(i32);
}
size_and_align! {
#[repr(packed(2))]
struct Goal(i32);
}
size_and_align! {
#[repr(packed(4))]
struct Goal(i32);
}
check_size_and_align("#[repr(packed(5))] struct Goal(i32);", "", 4, 1);
}
#[test]
fn generic() {
size_and_align! {

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

@ -1350,7 +1350,7 @@ pub(crate) fn resolve_indexing_op(
ty: Canonical<Ty>,
index_trait: TraitId,
) -> Option<ReceiverAdjustments> {
let mut table = InferenceTable::new(db, env.clone());
let mut table = InferenceTable::new(db, env);
let ty = table.instantiate_canonical(ty);
let deref_chain = autoderef_method_receiver(&mut table, ty);
for (ty, adj) in deref_chain {

View file

@ -14,9 +14,10 @@ use crate::{
};
use base_db::CrateId;
use chalk_ir::Mutability;
use either::Either;
use hir_def::{
hir::{BindingId, Expr, ExprId, Ordering, PatId},
DefWithBodyId, FieldId, StaticId, UnionId, VariantId,
DefWithBodyId, FieldId, StaticId, TupleFieldId, UnionId, VariantId,
};
use la_arena::{Arena, ArenaMap, Idx, RawIdx};
@ -97,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(
@ -117,16 +118,16 @@ 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)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ProjectionElem<V, T> {
Deref,
Field(FieldId),
Field(Either<FieldId, TupleFieldId>),
// FIXME: get rid of this, and use FieldId for tuples and closures
TupleOrClosureField(usize),
ClosureField(usize),
Index(V),
ConstantIndex { offset: u64, from_end: bool },
Subslice { from: u64, to: u64 },
@ -161,7 +162,7 @@ impl<V, T> ProjectionElem<V, T> {
return TyKind::Error.intern(Interner);
}
},
ProjectionElem::Field(f) => match &base.kind(Interner) {
ProjectionElem::Field(Either::Left(f)) => match &base.kind(Interner) {
TyKind::Adt(_, subst) => {
db.field_types(f.parent)[f.local_id].clone().substitute(Interner, subst)
}
@ -170,19 +171,25 @@ impl<V, T> ProjectionElem<V, T> {
return TyKind::Error.intern(Interner);
}
},
ProjectionElem::TupleOrClosureField(f) => match &base.kind(Interner) {
ProjectionElem::Field(Either::Right(f)) => match &base.kind(Interner) {
TyKind::Tuple(_, subst) => subst
.as_slice(Interner)
.get(*f)
.get(f.index as usize)
.map(|x| x.assert_ty_ref(Interner))
.cloned()
.unwrap_or_else(|| {
never!("Out of bound tuple field");
TyKind::Error.intern(Interner)
}),
_ => {
never!("Only tuple has tuple field");
return TyKind::Error.intern(Interner);
}
},
ProjectionElem::ClosureField(f) => match &base.kind(Interner) {
TyKind::Closure(id, subst) => closure_field(*id, subst, *f),
_ => {
never!("Only tuple or closure has tuple or closure field");
never!("Only closure has closure field");
return TyKind::Error.intern(Interner);
}
},

View file

@ -205,7 +205,7 @@ fn place_case(db: &dyn HirDatabase, body: &MirBody, lvalue: &Place) -> Projectio
| ProjectionElem::ConstantIndex { .. }
| ProjectionElem::Subslice { .. }
| ProjectionElem::Field(_)
| ProjectionElem::TupleOrClosureField(_)
| ProjectionElem::ClosureField(_)
| ProjectionElem::Index(_) => {
is_part_of = true;
}

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 {
@ -391,7 +386,7 @@ impl MirEvalError {
write!(
f,
"Layout for type `{}` is not available due {err:?}",
ty.display(db).with_closure_style(ClosureStyle::ClosureWithId).to_string()
ty.display(db).with_closure_style(ClosureStyle::ClosureWithId)
)?;
}
MirEvalError::MirLowerError(func, err) => {
@ -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,
@ -720,13 +733,19 @@ impl Evaluator<'_> {
self.size_of_sized(&inner_ty, locals, "array inner type should be sized")?;
addr = addr.offset(ty_size * (from as usize));
}
&ProjectionElem::TupleOrClosureField(f) => {
&ProjectionElem::ClosureField(f) => {
let layout = self.layout(&prev_ty)?;
let offset = layout.fields.offset(f).bytes_usize();
addr = addr.offset(offset);
metadata = None; // tuple field is always sized
metadata = None;
}
ProjectionElem::Field(f) => {
ProjectionElem::Field(Either::Right(f)) => {
let layout = self.layout(&prev_ty)?;
let offset = layout.fields.offset(f.index as usize).bytes_usize();
addr = addr.offset(offset);
metadata = None; // tuple field is always sized FIXME: This is wrong, the tail can be unsized
}
ProjectionElem::Field(Either::Left(f)) => {
let layout = self.layout(&prev_ty)?;
let variant_layout = match &layout.variants {
Variants::Single { .. } => &layout,
@ -797,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 {
@ -831,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);
@ -951,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.
@ -1044,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)?);
}
@ -1277,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) => {
@ -1514,7 +1533,7 @@ impl Evaluator<'_> {
}
},
TyKind::Dyn(_) => {
let vtable = self.vtable_map.id(current_ty.clone());
let vtable = self.vtable_map.id(current_ty);
let mut r = Vec::with_capacity(16);
let addr = addr.get(self)?;
r.extend(addr.iter().copied());
@ -1709,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))
}
@ -1761,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(());
@ -1881,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) => {
@ -1897,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 => {
@ -1923,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;
@ -1996,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,
@ -2031,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())?;
}
@ -2042,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,
@ -2064,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,
@ -2077,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) => {
@ -2089,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,
@ -2160,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(
@ -2175,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)
}
@ -2368,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
})
}
@ -2546,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,
@ -2674,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

@ -15,7 +15,7 @@ use hir_def::{
path::Path,
resolver::{resolver_for_expr, HasResolver, ResolveValueResult, ValueNs},
AdtId, DefWithBodyId, EnumVariantId, GeneralConstId, HasModule, ItemContainerId, LocalFieldId,
Lookup, TraitId, TypeOrConstParamId,
Lookup, TraitId, TupleId, TypeOrConstParamId,
};
use hir_expand::name::Name;
use la_arena::ArenaMap;
@ -177,7 +177,7 @@ impl MirLowerError {
)?;
writeln!(f, "Provided args: [")?;
for g in subst.iter(Interner) {
write!(f, " {},", g.display(db).to_string())?;
write!(f, " {},", g.display(db))?;
}
writeln!(f, "]")?;
}
@ -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(),
@ -828,12 +828,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
Some(it) => it,
None => {
let p = sp.project(
ProjectionElem::Field(FieldId {
ProjectionElem::Field(Either::Left(FieldId {
parent: variant_id,
local_id: LocalFieldId::from_raw(RawIdx::from(
i as u32,
)),
}),
})),
&mut self.result.projection_store,
);
Operand::Copy(p)
@ -855,7 +855,10 @@ impl<'ctx> MirLowerCtx<'ctx> {
let local_id =
variant_data.field(name).ok_or(MirLowerError::UnresolvedField)?;
let place = place.project(
PlaceElem::Field(FieldId { parent: union_id.into(), local_id }),
PlaceElem::Field(Either::Left(FieldId {
parent: union_id.into(),
local_id,
})),
&mut self.result.projection_store,
);
self.lower_expr_to_place(*expr, place, current)
@ -1110,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,
};
@ -1142,8 +1145,8 @@ impl<'ctx> MirLowerCtx<'ctx> {
.map(|it| match it {
ProjectionElem::Deref => ProjectionElem::Deref,
ProjectionElem::Field(it) => ProjectionElem::Field(it),
ProjectionElem::TupleOrClosureField(it) => {
ProjectionElem::TupleOrClosureField(it)
ProjectionElem::ClosureField(it) => {
ProjectionElem::ClosureField(it)
}
ProjectionElem::ConstantIndex { offset, from_end } => {
ProjectionElem::ConstantIndex { offset, from_end }
@ -1273,7 +1276,10 @@ impl<'ctx> MirLowerCtx<'ctx> {
Expr::Tuple { exprs, is_assignee_expr: _ } => {
for (i, expr) in exprs.iter().enumerate() {
let rhs = rhs.project(
ProjectionElem::TupleOrClosureField(i),
ProjectionElem::Field(Either::Right(TupleFieldId {
tuple: TupleId(!0), // Dummy this as its unused
index: i as u32,
})),
&mut self.result.projection_store,
);
let Some(c) = self.lower_destructing_assignment(current, *expr, rhs, span)?
@ -1337,11 +1343,14 @@ impl<'ctx> MirLowerCtx<'ctx> {
fn push_field_projection(&mut self, place: &mut Place, expr_id: ExprId) -> Result<()> {
if let Expr::Field { expr, name } = &self.body[expr_id] {
if let TyKind::Tuple(..) = self.expr_ty_after_adjustments(*expr).kind(Interner) {
let index = name
.as_tuple_index()
.ok_or(MirLowerError::TypeError("named field on tuple"))?;
let index =
name.as_tuple_index().ok_or(MirLowerError::TypeError("named field on tuple"))?
as u32;
*place = place.project(
ProjectionElem::TupleOrClosureField(index),
ProjectionElem::Field(Either::Right(TupleFieldId {
tuple: TupleId(!0), // dummy as its unused
index,
})),
&mut self.result.projection_store,
)
} else {
@ -1386,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"))
}
@ -1474,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<()> {
@ -2041,10 +2047,11 @@ pub fn mir_body_for_closure_query(
match (it, y) {
(ProjectionElem::Deref, ProjectionElem::Deref) => (),
(ProjectionElem::Field(it), ProjectionElem::Field(y)) if it == y => (),
(
ProjectionElem::TupleOrClosureField(it),
ProjectionElem::TupleOrClosureField(y),
) if it == y => (),
(ProjectionElem::ClosureField(it), ProjectionElem::ClosureField(y))
if it == y =>
{
()
}
_ => return false,
}
}
@ -2054,7 +2061,7 @@ pub fn mir_body_for_closure_query(
Some(it) => {
p.local = closure_local;
let mut next_projs = closure_projection.clone();
next_projs.push(PlaceElem::TupleOrClosureField(it.1));
next_projs.push(PlaceElem::ClosureField(it.1));
let prev_projs = p.projection;
if it.0.kind != CaptureKind::ByValue {
next_projs.push(ProjectionElem::Deref);
@ -2063,8 +2070,8 @@ pub fn mir_body_for_closure_query(
prev_projs
.lookup(&store)
.iter()
.cloned()
.skip(it.0.place.projections.len()),
.skip(it.0.place.projections.len())
.cloned(),
);
p.projection = store.intern(next_projs.into());
}

View file

@ -218,7 +218,7 @@ impl MirLowerCtx<'_> {
self.push_field_projection(&mut r, expr_id)?;
Ok(Some((r, current)))
}
Expr::Index { base, index } => {
Expr::Index { base, index, is_assignee_expr: _ } => {
let base_ty = self.expr_ty_after_adjustments(*base);
let index_ty = self.expr_ty_after_adjustments(*index);
if index_ty != TyBuilder::usize()

View file

@ -108,7 +108,12 @@ impl MirLowerCtx<'_> {
current_else,
args,
*ellipsis,
(0..subst.len(Interner)).map(|i| PlaceElem::TupleOrClosureField(i)),
(0..subst.len(Interner)).map(|i| {
PlaceElem::Field(Either::Right(TupleFieldId {
tuple: TupleId(!0), // Dummy as it is unused
index: i as u32,
}))
}),
&(&mut cond_place),
mode,
)?
@ -239,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(),
);
@ -566,7 +571,10 @@ impl MirLowerCtx<'_> {
let field_id =
variant_data.field(&x.name).ok_or(MirLowerError::UnresolvedField)?;
Ok((
PlaceElem::Field(FieldId { parent: v.into(), local_id: field_id }),
PlaceElem::Field(Either::Left(FieldId {
parent: v.into(),
local_id: field_id,
})),
x.pat,
))
})
@ -574,10 +582,9 @@ impl MirLowerCtx<'_> {
self.pattern_match_adt(current, current_else, it.into_iter(), cond_place, mode)?
}
AdtPatternShape::Tuple { args, ellipsis } => {
let fields = variant_data
.fields()
.iter()
.map(|(x, _)| PlaceElem::Field(FieldId { parent: v.into(), local_id: x }));
let fields = variant_data.fields().iter().map(|(x, _)| {
PlaceElem::Field(Either::Left(FieldId { parent: v.into(), local_id: x }))
});
self.pattern_match_tuple_like(
current,
current_else,

View file

@ -5,6 +5,7 @@ use std::{
mem,
};
use either::Either;
use hir_def::{body::Body, hir::BindingId};
use hir_expand::name::Name;
use la_arena::ArenaMap;
@ -298,7 +299,7 @@ impl<'a> MirPrettyCtx<'a> {
f(this, local, head);
w!(this, ")");
}
ProjectionElem::Field(field) => {
ProjectionElem::Field(Either::Left(field)) => {
let variant_data = field.parent.variant_data(this.db.upcast());
let name = &variant_data.fields()[field.local_id].name;
match field.parent {
@ -320,7 +321,11 @@ impl<'a> MirPrettyCtx<'a> {
}
}
}
ProjectionElem::TupleOrClosureField(it) => {
ProjectionElem::Field(Either::Right(field)) => {
f(this, local, head);
w!(this, ".{}", field.index);
}
ProjectionElem::ClosureField(it) => {
f(this, local, head);
w!(this, ".{}", it);
}

View file

@ -4506,3 +4506,50 @@ fn ttt() {
"#,
);
}
#[test]
fn infer_borrow() {
check_types(
r#"
//- minicore: index
pub struct SomeMap<K>;
pub trait Borrow<Borrowed: ?Sized> {
fn borrow(&self) -> &Borrowed;
}
impl<T: ?Sized> Borrow<T> for T {
fn borrow(&self) -> &T {
self
}
}
impl<T: ?Sized> Borrow<T> for &T {
fn borrow(&self) -> &T {
&**self
}
}
impl<K, KB: Borrow<K>> core::ops::Index<KB> for SomeMap<K> {
type Output = ();
fn index(&self, _: KB) -> &() {
&()
}
}
impl<K> core::ops::IndexMut<K> for SomeMap<K> {
fn index_mut(&mut self, _: K) -> &mut () {
&mut ()
}
}
fn foo() {
let mut map = SomeMap;
map["a"] = ();
map;
//^^^ SomeMap<&str>
}
"#,
);
}