Cleanup error variants in MIR code slightly

This commit is contained in:
Lukas Wirth 2024-01-30 09:51:28 +01:00
parent 8a5829cf28
commit 5860763f18
17 changed files with 598 additions and 434 deletions

View file

@ -1,4 +1,4 @@
//! Proc Macro Expander stub
//! Proc Macro Expander stuff
use core::fmt;
use std::{panic::RefUnwindSafe, sync};

View file

@ -118,7 +118,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
fn layout_of_ty(&self, ty: Ty, env: Arc<TraitEnvironment>) -> Result<Arc<Layout>, LayoutError>;
#[salsa::invoke(crate::layout::target_data_layout_query)]
fn target_data_layout(&self, krate: CrateId) -> Option<Arc<TargetDataLayout>>;
fn target_data_layout(&self, krate: CrateId) -> Result<Arc<TargetDataLayout>, Arc<str>>;
#[salsa::invoke(crate::method_resolution::lookup_impl_method_query)]
fn lookup_impl_method(

View file

@ -605,8 +605,11 @@ fn render_const_scalar(
write!(f, "{}", f.db.union_data(u).name.display(f.db.upcast()))
}
hir_def::AdtId::EnumId(e) => {
let Ok(target_data_layout) = f.db.target_data_layout(trait_env.krate) else {
return f.write_str("<target-layout-not-available>");
};
let Some((var_id, var_layout)) =
detect_variant_from_bytes(&layout, f.db, trait_env, b, e)
detect_variant_from_bytes(&layout, f.db, &target_data_layout, b, e)
else {
return f.write_str("<failed-to-detect-variant>");
};

View file

@ -198,7 +198,7 @@ pub fn layout_of_ty_query(
trait_env: Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError> {
let krate = trait_env.krate;
let Some(target) = db.target_data_layout(krate) else {
let Ok(target) = db.target_data_layout(krate) else {
return Err(LayoutError::TargetLayoutNotAvailable);
};
let cx = LayoutCx { target: &target };

View file

@ -32,7 +32,7 @@ pub fn layout_of_adt_query(
trait_env: Arc<TraitEnvironment>,
) -> Result<Arc<Layout>, LayoutError> {
let krate = trait_env.krate;
let Some(target) = db.target_data_layout(krate) else {
let Ok(target) = db.target_data_layout(krate) else {
return Err(LayoutError::TargetLayoutNotAvailable);
};
let cx = LayoutCx { target: &target };

View file

@ -2,6 +2,7 @@
use base_db::CrateId;
use hir_def::layout::TargetDataLayout;
use ra_ap_rustc_abi::{AlignFromBytesError, TargetDataLayoutErrors};
use triomphe::Arc;
use crate::db::HirDatabase;
@ -9,15 +10,40 @@ use crate::db::HirDatabase;
pub fn target_data_layout_query(
db: &dyn HirDatabase,
krate: CrateId,
) -> Option<Arc<TargetDataLayout>> {
) -> Result<Arc<TargetDataLayout>, Arc<str>> {
let crate_graph = db.crate_graph();
let target_layout = crate_graph[krate].target_layout.as_ref().ok()?;
let res = TargetDataLayout::parse_from_llvm_datalayout_string(target_layout);
if let Err(_e) = &res {
// FIXME: Print the error here once it implements debug/display
// also logging here is somewhat wrong, but unfortunately this is the earliest place we can
// parse that doesn't impose a dependency to the rust-abi crate for project-model
tracing::error!("Failed to parse target data layout for {krate:?}");
let res = crate_graph[krate].target_layout.as_deref();
match res {
Ok(it) => match TargetDataLayout::parse_from_llvm_datalayout_string(it) {
Ok(it) => Ok(Arc::new(it)),
Err(e) => {
Err(match e {
TargetDataLayoutErrors::InvalidAddressSpace { addr_space, cause, err } => {
format!(
r#"invalid address space `{addr_space}` for `{cause}` in "data-layout": {err}"#
)
}
TargetDataLayoutErrors::InvalidBits { kind, bit, cause, err } => format!(r#"invalid {kind} `{bit}` for `{cause}` in "data-layout": {err}"#),
TargetDataLayoutErrors::MissingAlignment { cause } => format!(r#"missing alignment for `{cause}` in "data-layout""#),
TargetDataLayoutErrors::InvalidAlignment { cause, err } => format!(
r#"invalid alignment for `{cause}` in "data-layout": `{align}` is {err_kind}"#,
align = err.align(),
err_kind = match err {
AlignFromBytesError::NotPowerOfTwo(_) => "not a power of two",
AlignFromBytesError::TooLarge(_) => "too large",
}
),
TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => {
format!(r#"inconsistent target specification: "data-layout" claims architecture is {dl}-endian, while "target-endian" is `{target}`"#)
}
TargetDataLayoutErrors::InconsistentTargetPointerWidth {
pointer_size,
target,
} => format!(r#"inconsistent target specification: "data-layout" claims pointers are {pointer_size}-bit, while "target-pointer-width" is `{target}`"#),
TargetDataLayoutErrors::InvalidBitsSize { err } => err,
}.into())
}
},
Err(e) => Err(Arc::from(&**e)),
}
res.ok().map(Arc::new)
}

View file

@ -444,7 +444,7 @@ fn mutability_of_locals(
}
if destination.projection.lookup(&body.projection_store).is_empty() {
if ever_init_map.get(destination.local).copied().unwrap_or_default() {
push_mut_span(destination.local, MirSpan::Unknown, &mut result);
push_mut_span(destination.local, terminator.span, &mut result);
} else {
ever_init_map.insert(destination.local, true);
}

View file

@ -17,6 +17,7 @@ use hir_def::{
use hir_expand::{mod_path::ModPath, HirFileIdExt, InFile};
use intern::Interned;
use la_arena::ArenaMap;
use ra_ap_rustc_abi::TargetDataLayout;
use rustc_hash::{FxHashMap, FxHashSet};
use stdx::never;
use syntax::{SyntaxNodePtr, TextRange};
@ -51,7 +52,7 @@ macro_rules! from_bytes {
($ty:tt, $value:expr) => {
($ty::from_le_bytes(match ($value).try_into() {
Ok(it) => it,
Err(_) => return Err(MirEvalError::TypeError(stringify!(mismatched size in constructing $ty))),
Err(_) => return Err(MirEvalError::InternalError(stringify!(mismatched size in constructing $ty).into())),
}))
};
}
@ -145,6 +146,7 @@ enum MirOrDynIndex {
pub struct Evaluator<'a> {
db: &'a dyn HirDatabase,
trait_env: Arc<TraitEnvironment>,
target_data_layout: Arc<TargetDataLayout>,
stack: Vec<u8>,
heap: Vec<u8>,
code_stack: Vec<StackFrame>,
@ -316,12 +318,12 @@ impl Address {
pub enum MirEvalError {
ConstEvalError(String, Box<ConstEvalError>),
LayoutError(LayoutError, Ty),
/// Means that code had type errors (or mismatched args) and we shouldn't generate mir in first place.
TypeError(&'static str),
TargetDataLayoutNotAvailable(Arc<str>),
/// Means that code had undefined behavior. We don't try to actively detect UB, but if it was detected
/// then use this type of error.
UndefinedBehavior(String),
Panic(String),
// FIXME: This should be folded into ConstEvalError?
MirLowerError(FunctionId, MirLowerError),
MirLowerErrorForClosure(ClosureId, MirLowerError),
TypeIsUnsized(Ty, &'static str),
@ -330,11 +332,12 @@ pub enum MirEvalError {
InFunction(Box<MirEvalError>, Vec<(Either<FunctionId, ClosureId>, MirSpan, DefWithBodyId)>),
ExecutionLimitExceeded,
StackOverflow,
TargetDataLayoutNotAvailable,
/// FIXME: Fold this into InternalError
InvalidVTableId(usize),
/// ?
CoerceUnsizedError(Ty),
LangItemNotFound(LangItem),
BrokenLayout(Box<Layout>),
/// These should not occur, usually indicates a bug in mir lowering.
InternalError(Box<str>),
}
impl MirEvalError {
@ -406,8 +409,8 @@ impl MirEvalError {
span_formatter,
)?;
}
MirEvalError::TypeError(_)
| MirEvalError::UndefinedBehavior(_)
MirEvalError::UndefinedBehavior(_)
| MirEvalError::TargetDataLayoutNotAvailable(_)
| MirEvalError::Panic(_)
| MirEvalError::MirLowerErrorForClosure(_, _)
| MirEvalError::TypeIsUnsized(_, _)
@ -415,10 +418,8 @@ impl MirEvalError {
| MirEvalError::InvalidConst(_)
| MirEvalError::ExecutionLimitExceeded
| MirEvalError::StackOverflow
| MirEvalError::TargetDataLayoutNotAvailable
| MirEvalError::CoerceUnsizedError(_)
| MirEvalError::LangItemNotFound(_)
| MirEvalError::BrokenLayout(_)
| MirEvalError::InternalError(_)
| MirEvalError::InvalidVTableId(_) => writeln!(f, "{:?}", err)?,
}
Ok(())
@ -431,16 +432,16 @@ impl std::fmt::Debug for MirEvalError {
Self::ConstEvalError(arg0, arg1) => {
f.debug_tuple("ConstEvalError").field(arg0).field(arg1).finish()
}
Self::LangItemNotFound(arg0) => f.debug_tuple("LangItemNotFound").field(arg0).finish(),
Self::LayoutError(arg0, arg1) => {
f.debug_tuple("LayoutError").field(arg0).field(arg1).finish()
}
Self::TypeError(arg0) => f.debug_tuple("TypeError").field(arg0).finish(),
Self::UndefinedBehavior(arg0) => {
f.debug_tuple("UndefinedBehavior").field(arg0).finish()
}
Self::Panic(msg) => write!(f, "Panic with message:\n{msg:?}"),
Self::TargetDataLayoutNotAvailable => write!(f, "TargetDataLayoutNotAvailable"),
Self::TargetDataLayoutNotAvailable(arg0) => {
f.debug_tuple("TargetDataLayoutNotAvailable").field(arg0).finish()
}
Self::TypeIsUnsized(ty, it) => write!(f, "{ty:?} is unsized. {it} should be sized."),
Self::ExecutionLimitExceeded => write!(f, "execution limit exceeded"),
Self::StackOverflow => write!(f, "stack overflow"),
@ -453,7 +454,7 @@ impl std::fmt::Debug for MirEvalError {
Self::CoerceUnsizedError(arg0) => {
f.debug_tuple("CoerceUnsizedError").field(arg0).finish()
}
Self::BrokenLayout(arg0) => f.debug_tuple("BrokenLayout").field(arg0).finish(),
Self::InternalError(arg0) => f.debug_tuple("InternalError").field(arg0).finish(),
Self::InvalidVTableId(arg0) => f.debug_tuple("InvalidVTableId").field(arg0).finish(),
Self::NotSupported(arg0) => f.debug_tuple("NotSupported").field(arg0).finish(),
Self::InvalidConst(arg0) => {
@ -530,7 +531,11 @@ pub fn interpret_mir(
trait_env: Option<Arc<TraitEnvironment>>,
) -> (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 mut evaluator =
match Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env) {
Ok(it) => it,
Err(e) => return (Err(e), MirOutput { stdout: vec![], stderr: vec![] }),
};
let it: Result<Const> = (|| {
if evaluator.ptr_size() != std::mem::size_of::<usize>() {
not_supported!("targets with different pointer size from host");
@ -566,9 +571,15 @@ impl Evaluator<'_> {
owner: DefWithBodyId,
assert_placeholder_ty_is_unused: bool,
trait_env: Option<Arc<TraitEnvironment>>,
) -> Evaluator<'_> {
) -> Result<Evaluator<'_>> {
let crate_id = owner.module(db.upcast()).krate();
Evaluator {
let target_data_layout = match db.target_data_layout(crate_id) {
Ok(target_data_layout) => target_data_layout,
Err(e) => return Err(MirEvalError::TargetDataLayoutNotAvailable(e)),
};
let cached_ptr_size = target_data_layout.pointer_size.bytes_usize();
Ok(Evaluator {
target_data_layout,
stack: vec![0],
heap: vec![0],
code_stack: vec![],
@ -590,10 +601,7 @@ impl Evaluator<'_> {
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,
},
cached_ptr_size,
cached_fn_trait_func: db
.lang_item(crate_id, LangItem::Fn)
.and_then(|x| x.as_trait())
@ -606,7 +614,7 @@ impl Evaluator<'_> {
.lang_item(crate_id, LangItem::FnOnce)
.and_then(|x| x.as_trait())
.and_then(|x| db.trait_data(x).method_by_name(&name![call_once])),
}
})
}
fn place_addr(&self, p: &Place, locals: &Locals) -> Result<Address> {
@ -754,8 +762,8 @@ impl Evaluator<'_> {
RustcEnumVariantIdx(it.lookup(self.db.upcast()).index as usize)
}
_ => {
return Err(MirEvalError::TypeError(
"Multivariant layout only happens for enums",
return Err(MirEvalError::InternalError(
"mismatched layout".into(),
))
}
}]
@ -993,12 +1001,12 @@ impl Evaluator<'_> {
IntervalOrOwned::Borrowed(value) => interval.write_from_interval(self, value)?,
}
if remain_args == 0 {
return Err(MirEvalError::TypeError("more arguments provided"));
return Err(MirEvalError::InternalError("too many arguments".into()));
}
remain_args -= 1;
}
if remain_args > 0 {
return Err(MirEvalError::TypeError("not enough arguments provided"));
return Err(MirEvalError::InternalError("too few arguments".into()));
}
Ok(())
}
@ -1071,8 +1079,8 @@ impl Evaluator<'_> {
match metadata {
Some(m) => m,
None => {
return Err(MirEvalError::TypeError(
"type without metadata is used for Rvalue::Len",
return Err(MirEvalError::InternalError(
"type without metadata is used for Rvalue::Len".into(),
));
}
}
@ -1312,7 +1320,7 @@ impl Evaluator<'_> {
}
AggregateKind::Tuple(ty) => {
let layout = self.layout(ty)?;
Owned(self.make_by_layout(
Owned(self.construct_with_layout(
layout.size.bytes_usize(),
&layout,
None,
@ -1334,7 +1342,7 @@ impl Evaluator<'_> {
AggregateKind::Adt(it, subst) => {
let (size, variant_layout, tag) =
self.layout_of_variant(*it, subst.clone(), locals)?;
Owned(self.make_by_layout(
Owned(self.construct_with_layout(
size,
&variant_layout,
tag,
@ -1343,7 +1351,7 @@ impl Evaluator<'_> {
}
AggregateKind::Closure(ty) => {
let layout = self.layout(ty)?;
Owned(self.make_by_layout(
Owned(self.construct_with_layout(
layout.size.bytes_usize(),
&layout,
None,
@ -1415,10 +1423,7 @@ impl Evaluator<'_> {
Ok(r)
}
Variants::Multiple { tag, tag_encoding, variants, .. } => {
let Some(target_data_layout) = self.db.target_data_layout(self.crate_id) else {
not_supported!("missing target data layout");
};
let size = tag.size(&*target_data_layout).bytes_usize();
let size = tag.size(&*self.target_data_layout).bytes_usize();
let offset = layout.fields.offset(0).bytes_usize(); // The only field on enum variants is the tag field
match tag_encoding {
TagEncoding::Direct => {
@ -1458,9 +1463,8 @@ impl Evaluator<'_> {
if let TyKind::Adt(id, subst) = kind {
if let AdtId::StructId(struct_id) = id.0 {
let field_types = self.db.field_types(struct_id.into());
let mut field_types = field_types.iter();
if let Some(ty) =
field_types.next().map(|it| it.1.clone().substitute(Interner, subst))
field_types.iter().last().map(|it| it.1.clone().substitute(Interner, subst))
{
return self.coerce_unsized_look_through_fields(&ty, goal);
}
@ -1578,10 +1582,6 @@ impl Evaluator<'_> {
Ok(match &layout.variants {
Variants::Single { .. } => (layout.size.bytes_usize(), layout, None),
Variants::Multiple { variants, tag, tag_encoding, .. } => {
let cx = self
.db
.target_data_layout(self.crate_id)
.ok_or(MirEvalError::TargetDataLayoutNotAvailable)?;
let enum_variant_id = match it {
VariantId::EnumVariantId(it) => it,
_ => not_supported!("multi variant layout for non-enums"),
@ -1612,7 +1612,7 @@ impl Evaluator<'_> {
if have_tag {
Some((
layout.fields.offset(0).bytes_usize(),
tag.size(&*cx).bytes_usize(),
tag.size(&*self.target_data_layout).bytes_usize(),
discriminant,
))
} else {
@ -1623,7 +1623,7 @@ impl Evaluator<'_> {
})
}
fn make_by_layout(
fn construct_with_layout(
&mut self,
size: usize, // Not necessarily equal to variant_layout.size
variant_layout: &Layout,
@ -1634,7 +1634,14 @@ impl Evaluator<'_> {
if let Some((offset, size, value)) = tag {
match result.get_mut(offset..offset + size) {
Some(it) => it.copy_from_slice(&value.to_le_bytes()[0..size]),
None => return Err(MirEvalError::BrokenLayout(Box::new(variant_layout.clone()))),
None => {
return Err(MirEvalError::InternalError(
format!(
"encoded tag ({offset}, {size}, {value}) is out of bounds 0..{size}"
)
.into(),
))
}
}
}
for (i, op) in values.enumerate() {
@ -1642,7 +1649,11 @@ impl Evaluator<'_> {
let op = op.get(self)?;
match result.get_mut(offset..offset + op.len()) {
Some(it) => it.copy_from_slice(op),
None => return Err(MirEvalError::BrokenLayout(Box::new(variant_layout.clone()))),
None => {
return Err(MirEvalError::InternalError(
format!("field offset ({offset}) is out of bounds 0..{size}").into(),
))
}
}
}
Ok(result)
@ -1695,28 +1706,29 @@ impl Evaluator<'_> {
}
ConstScalar::Unknown => not_supported!("evaluating unknown const"),
};
let mut v: Cow<'_, [u8]> = Cow::Borrowed(v);
let patch_map = memory_map.transform_addresses(|b, align| {
let addr = self.heap_allocate(b.len(), align)?;
self.write_memory(addr, b)?;
Ok(addr.to_usize())
})?;
let (size, align) = self.size_align_of(ty, locals)?.unwrap_or((v.len(), 1));
if size != v.len() {
let v: Cow<'_, [u8]> = if size != v.len() {
// Handle self enum
if size == 16 && v.len() < 16 {
v = Cow::Owned(pad16(&v, false).to_vec());
Cow::Owned(pad16(&v, false).to_vec())
} else if size < 16 && v.len() == 16 {
v = Cow::Owned(v[0..size].to_vec());
Cow::Borrowed(&v[0..size])
} else {
return Err(MirEvalError::InvalidConst(konst.clone()));
}
}
} else {
Cow::Borrowed(v)
};
let addr = self.heap_allocate(size, align)?;
self.write_memory(addr, &v)?;
self.patch_addresses(
&patch_map,
|bytes| match &memory_map {
|bytes| match memory_map {
MemoryMap::Empty | MemoryMap::Simple(_) => {
Err(MirEvalError::InvalidVTableId(from_bytes!(usize, bytes)))
}
@ -2000,7 +2012,7 @@ impl Evaluator<'_> {
if let Some((v, l)) = detect_variant_from_bytes(
&layout,
this.db,
this.trait_env.clone(),
&this.target_data_layout,
bytes,
e,
) {
@ -2079,7 +2091,7 @@ impl Evaluator<'_> {
if let Some((ev, layout)) = detect_variant_from_bytes(
&layout,
self.db,
self.trait_env.clone(),
&self.target_data_layout,
self.read_memory(addr, layout.size.bytes_usize())?,
e,
) {
@ -2153,14 +2165,14 @@ impl Evaluator<'_> {
) -> Result<Option<StackFrame>> {
let id = from_bytes!(usize, bytes.get(self)?);
let next_ty = self.vtable_map.ty(id)?.clone();
match &next_ty.kind(Interner) {
match next_ty.kind(Interner) {
TyKind::FnDef(def, generic_args) => {
self.exec_fn_def(*def, generic_args, destination, args, locals, target_bb, span)
}
TyKind::Closure(id, subst) => {
self.exec_closure(*id, bytes.slice(0..0), subst, destination, args, locals, span)
}
_ => Err(MirEvalError::TypeError("function pointer to non function")),
_ => Err(MirEvalError::InternalError("function pointer to non function".into())),
}
}
@ -2241,7 +2253,7 @@ impl Evaluator<'_> {
CallableDefId::StructId(id) => {
let (size, variant_layout, tag) =
self.layout_of_variant(id.into(), generic_args, locals)?;
let result = self.make_by_layout(
let result = self.construct_with_layout(
size,
&variant_layout,
tag,
@ -2253,7 +2265,7 @@ impl Evaluator<'_> {
CallableDefId::EnumVariantId(id) => {
let (size, variant_layout, tag) =
self.layout_of_variant(id.into(), generic_args, locals)?;
let result = self.make_by_layout(
let result = self.construct_with_layout(
size,
&variant_layout,
tag,
@ -2407,7 +2419,9 @@ impl Evaluator<'_> {
target_bb: Option<BasicBlockId>,
span: MirSpan,
) -> Result<Option<StackFrame>> {
let func = args.first().ok_or(MirEvalError::TypeError("fn trait with no arg"))?;
let func = args
.first()
.ok_or_else(|| MirEvalError::InternalError("fn trait with no arg".into()))?;
let mut func_ty = func.ty.clone();
let mut func_data = func.interval;
while let TyKind::Ref(_, _, z) = func_ty.kind(Interner) {
@ -2450,7 +2464,7 @@ impl Evaluator<'_> {
)
.intern(Interner);
let layout = self.layout(&ty)?;
let result = self.make_by_layout(
let result = self.construct_with_layout(
layout.size.bytes_usize(),
&layout,
None,
@ -2634,7 +2648,7 @@ pub fn render_const_using_debug_impl(
owner: ConstId,
c: &Const,
) -> Result<String> {
let mut evaluator = Evaluator::new(db, owner.into(), false, None);
let mut evaluator = Evaluator::new(db, owner.into(), false, None)?;
let locals = &Locals {
ptr: ArenaMap::new(),
body: db
@ -2699,12 +2713,7 @@ pub fn render_const_using_debug_impl(
pub fn pad16(it: &[u8], is_signed: bool) -> [u8; 16] {
let is_negative = is_signed && it.last().unwrap_or(&0) > &127;
let fill_with = if is_negative { 255 } else { 0 };
it.iter()
.copied()
.chain(iter::repeat(fill_with))
.take(16)
.collect::<Vec<u8>>()
.try_into()
.expect("iterator take is not working")
let mut res = [if is_negative { 255 } else { 0 }; 16];
res[..it.len()].copy_from_slice(it);
res
}

View file

@ -18,7 +18,7 @@ macro_rules! from_bytes {
($ty:tt, $value:expr) => {
($ty::from_le_bytes(match ($value).try_into() {
Ok(it) => it,
Err(_) => return Err(MirEvalError::TypeError("mismatched size")),
Err(_) => return Err(MirEvalError::InternalError("mismatched size".into())),
}))
};
}
@ -249,7 +249,9 @@ impl Evaluator<'_> {
match alloc_fn {
"rustc_allocator_zeroed" | "rustc_allocator" => {
let [size, align] = args else {
return Err(MirEvalError::TypeError("rustc_allocator args are not provided"));
return Err(MirEvalError::InternalError(
"rustc_allocator args are not provided".into(),
));
};
let size = from_bytes!(usize, size.get(self)?);
let align = from_bytes!(usize, align.get(self)?);
@ -259,7 +261,9 @@ impl Evaluator<'_> {
"rustc_deallocator" => { /* no-op for now */ }
"rustc_reallocator" => {
let [ptr, old_size, align, new_size] = args else {
return Err(MirEvalError::TypeError("rustc_allocator args are not provided"));
return Err(MirEvalError::InternalError(
"rustc_allocator args are not provided".into(),
));
};
let old_size = from_bytes!(usize, old_size.get(self)?);
let new_size = from_bytes!(usize, new_size.get(self)?);
@ -339,22 +343,22 @@ impl Evaluator<'_> {
Err(MirEvalError::Panic(message))
}
SliceLen => {
let arg = args
.next()
.ok_or(MirEvalError::TypeError("argument of <[T]>::len() is not provided"))?;
let arg = args.next().ok_or(MirEvalError::InternalError(
"argument of <[T]>::len() is not provided".into(),
))?;
let ptr_size = arg.len() / 2;
Ok(arg[ptr_size..].into())
}
DropInPlace => {
let ty =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner)).ok_or(
MirEvalError::TypeError(
"generic argument of drop_in_place is not provided",
MirEvalError::InternalError(
"generic argument of drop_in_place is not provided".into(),
),
)?;
let arg = args
.next()
.ok_or(MirEvalError::TypeError("argument of drop_in_place is not provided"))?;
let arg = args.next().ok_or(MirEvalError::InternalError(
"argument of drop_in_place is not provided".into(),
))?;
self.run_drop_glue_deep(
ty.clone(),
locals,
@ -380,7 +384,9 @@ impl Evaluator<'_> {
318 => {
// SYS_getrandom
let [buf, len, _flags] = args else {
return Err(MirEvalError::TypeError("SYS_getrandom args are not provided"));
return Err(MirEvalError::InternalError(
"SYS_getrandom args are not provided".into(),
));
};
let addr = Address::from_bytes(buf.get(self)?)?;
let size = from_bytes!(usize, len.get(self)?);
@ -408,7 +414,7 @@ impl Evaluator<'_> {
match as_str {
"memcmp" => {
let [ptr1, ptr2, size] = args else {
return Err(MirEvalError::TypeError("memcmp args are not provided"));
return Err(MirEvalError::InternalError("memcmp args are not provided".into()));
};
let addr1 = Address::from_bytes(ptr1.get(self)?)?;
let addr2 = Address::from_bytes(ptr2.get(self)?)?;
@ -424,7 +430,9 @@ impl Evaluator<'_> {
}
"write" => {
let [fd, ptr, len] = args else {
return Err(MirEvalError::TypeError("libc::write args are not provided"));
return Err(MirEvalError::InternalError(
"libc::write args are not provided".into(),
));
};
let fd = u128::from_le_bytes(pad16(fd.get(self)?, false));
let interval = Interval {
@ -446,14 +454,16 @@ impl Evaluator<'_> {
"pthread_key_create" => {
let key = self.thread_local_storage.create_key();
let Some(arg0) = args.first() else {
return Err(MirEvalError::TypeError("pthread_key_create arg0 is not provided"));
return Err(MirEvalError::InternalError(
"pthread_key_create arg0 is not provided".into(),
));
};
let arg0_addr = Address::from_bytes(arg0.get(self)?)?;
let key_ty = if let Some((ty, ..)) = arg0.ty.as_reference_or_ptr() {
ty
} else {
return Err(MirEvalError::TypeError(
"pthread_key_create arg0 is not a pointer",
return Err(MirEvalError::InternalError(
"pthread_key_create arg0 is not a pointer".into(),
));
};
let arg0_interval = Interval::new(
@ -467,8 +477,8 @@ impl Evaluator<'_> {
}
"pthread_getspecific" => {
let Some(arg0) = args.first() else {
return Err(MirEvalError::TypeError(
"pthread_getspecific arg0 is not provided",
return Err(MirEvalError::InternalError(
"pthread_getspecific arg0 is not provided".into(),
));
};
let key = from_bytes!(usize, &pad16(arg0.get(self)?, false)[0..8]);
@ -478,14 +488,14 @@ impl Evaluator<'_> {
}
"pthread_setspecific" => {
let Some(arg0) = args.first() else {
return Err(MirEvalError::TypeError(
"pthread_setspecific arg0 is not provided",
return Err(MirEvalError::InternalError(
"pthread_setspecific arg0 is not provided".into(),
));
};
let key = from_bytes!(usize, &pad16(arg0.get(self)?, false)[0..8]);
let Some(arg1) = args.get(1) else {
return Err(MirEvalError::TypeError(
"pthread_setspecific arg1 is not provided",
return Err(MirEvalError::InternalError(
"pthread_setspecific arg1 is not provided".into(),
));
};
let value = from_bytes!(u128, pad16(arg1.get(self)?, false));
@ -502,14 +512,16 @@ impl Evaluator<'_> {
}
"syscall" => {
let Some((id, rest)) = args.split_first() else {
return Err(MirEvalError::TypeError("syscall arg1 is not provided"));
return Err(MirEvalError::InternalError("syscall arg1 is not provided".into()));
};
let id = from_bytes!(i64, id.get(self)?);
self.exec_syscall(id, rest, destination, locals, span)
}
"sched_getaffinity" => {
let [_pid, _set_size, set] = args else {
return Err(MirEvalError::TypeError("libc::write args are not provided"));
return Err(MirEvalError::InternalError(
"libc::write args are not provided".into(),
));
};
let set = Address::from_bytes(set.get(self)?)?;
// Only enable core 0 (we are single threaded anyway), which is bitset 0x0000001
@ -520,7 +532,9 @@ impl Evaluator<'_> {
}
"getenv" => {
let [name] = args else {
return Err(MirEvalError::TypeError("libc::write args are not provided"));
return Err(MirEvalError::InternalError(
"libc::write args are not provided".into(),
));
};
let mut name_buf = vec![];
let name = {
@ -586,8 +600,8 @@ impl Evaluator<'_> {
"sqrt" | "sin" | "cos" | "exp" | "exp2" | "log" | "log10" | "log2" | "fabs"
| "floor" | "ceil" | "trunc" | "rint" | "nearbyint" | "round" | "roundeven" => {
let [arg] = args else {
return Err(MirEvalError::TypeError(
"f64 intrinsic signature doesn't match fn (f64) -> f64",
return Err(MirEvalError::InternalError(
"f64 intrinsic signature doesn't match fn (f64) -> f64".into(),
));
};
let arg = from_bytes!(f64, arg.get(self)?);
@ -614,8 +628,8 @@ impl Evaluator<'_> {
}
"pow" | "minnum" | "maxnum" | "copysign" => {
let [arg1, arg2] = args else {
return Err(MirEvalError::TypeError(
"f64 intrinsic signature doesn't match fn (f64, f64) -> f64",
return Err(MirEvalError::InternalError(
"f64 intrinsic signature doesn't match fn (f64, f64) -> f64".into(),
));
};
let arg1 = from_bytes!(f64, arg1.get(self)?);
@ -630,8 +644,8 @@ impl Evaluator<'_> {
}
"powi" => {
let [arg1, arg2] = args else {
return Err(MirEvalError::TypeError(
"powif64 signature doesn't match fn (f64, i32) -> f64",
return Err(MirEvalError::InternalError(
"powif64 signature doesn't match fn (f64, i32) -> f64".into(),
));
};
let arg1 = from_bytes!(f64, arg1.get(self)?);
@ -640,8 +654,8 @@ impl Evaluator<'_> {
}
"fma" => {
let [arg1, arg2, arg3] = args else {
return Err(MirEvalError::TypeError(
"fmaf64 signature doesn't match fn (f64, f64, f64) -> f64",
return Err(MirEvalError::InternalError(
"fmaf64 signature doesn't match fn (f64, f64, f64) -> f64".into(),
));
};
let arg1 = from_bytes!(f64, arg1.get(self)?);
@ -658,8 +672,8 @@ impl Evaluator<'_> {
"sqrt" | "sin" | "cos" | "exp" | "exp2" | "log" | "log10" | "log2" | "fabs"
| "floor" | "ceil" | "trunc" | "rint" | "nearbyint" | "round" | "roundeven" => {
let [arg] = args else {
return Err(MirEvalError::TypeError(
"f32 intrinsic signature doesn't match fn (f32) -> f32",
return Err(MirEvalError::InternalError(
"f32 intrinsic signature doesn't match fn (f32) -> f32".into(),
));
};
let arg = from_bytes!(f32, arg.get(self)?);
@ -686,8 +700,8 @@ impl Evaluator<'_> {
}
"pow" | "minnum" | "maxnum" | "copysign" => {
let [arg1, arg2] = args else {
return Err(MirEvalError::TypeError(
"f32 intrinsic signature doesn't match fn (f32, f32) -> f32",
return Err(MirEvalError::InternalError(
"f32 intrinsic signature doesn't match fn (f32, f32) -> f32".into(),
));
};
let arg1 = from_bytes!(f32, arg1.get(self)?);
@ -702,8 +716,8 @@ impl Evaluator<'_> {
}
"powi" => {
let [arg1, arg2] = args else {
return Err(MirEvalError::TypeError(
"powif32 signature doesn't match fn (f32, i32) -> f32",
return Err(MirEvalError::InternalError(
"powif32 signature doesn't match fn (f32, i32) -> f32".into(),
));
};
let arg1 = from_bytes!(f32, arg1.get(self)?);
@ -712,8 +726,8 @@ impl Evaluator<'_> {
}
"fma" => {
let [arg1, arg2, arg3] = args else {
return Err(MirEvalError::TypeError(
"fmaf32 signature doesn't match fn (f32, f32, f32) -> f32",
return Err(MirEvalError::InternalError(
"fmaf32 signature doesn't match fn (f32, f32, f32) -> f32".into(),
));
};
let arg1 = from_bytes!(f32, arg1.get(self)?);
@ -730,7 +744,9 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("size_of generic arg is not provided"));
return Err(MirEvalError::InternalError(
"size_of generic arg is not provided".into(),
));
};
let size = self.size_of_sized(ty, locals, "size_of arg")?;
destination.write_from_bytes(self, &size.to_le_bytes()[0..destination.size])
@ -739,7 +755,9 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("align_of generic arg is not provided"));
return Err(MirEvalError::InternalError(
"align_of generic arg is not provided".into(),
));
};
let align = self.layout(ty)?.align.abi.bytes();
destination.write_from_bytes(self, &align.to_le_bytes()[0..destination.size])
@ -748,10 +766,14 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("size_of_val generic arg is not provided"));
return Err(MirEvalError::InternalError(
"size_of_val generic arg is not provided".into(),
));
};
let [arg] = args else {
return Err(MirEvalError::TypeError("size_of_val args are not provided"));
return Err(MirEvalError::InternalError(
"size_of_val args are not provided".into(),
));
};
if let Some((size, _)) = self.size_align_of(ty, locals)? {
destination.write_from_bytes(self, &size.to_le_bytes())
@ -765,12 +787,14 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError(
"min_align_of_val generic arg is not provided",
return Err(MirEvalError::InternalError(
"min_align_of_val generic arg is not provided".into(),
));
};
let [arg] = args else {
return Err(MirEvalError::TypeError("min_align_of_val args are not provided"));
return Err(MirEvalError::InternalError(
"min_align_of_val args are not provided".into(),
));
};
if let Some((_, align)) = self.size_align_of(ty, locals)? {
destination.write_from_bytes(self, &align.to_le_bytes())
@ -784,7 +808,9 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("type_name generic arg is not provided"));
return Err(MirEvalError::InternalError(
"type_name generic arg is not provided".into(),
));
};
let ty_name = match ty.display_source_code(
self.db,
@ -808,7 +834,9 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("size_of generic arg is not provided"));
return Err(MirEvalError::InternalError(
"size_of generic arg is not provided".into(),
));
};
let result = !ty.clone().is_copy(self.db, locals.body.owner);
destination.write_from_bytes(self, &[u8::from(result)])
@ -817,14 +845,18 @@ impl Evaluator<'_> {
// FIXME: this is wrong for const eval, it should return 2 in some
// cases.
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("wrapping_add args are not provided"));
return Err(MirEvalError::InternalError(
"wrapping_add args are not provided".into(),
));
};
let ans = lhs.get(self)? == rhs.get(self)?;
destination.write_from_bytes(self, &[u8::from(ans)])
}
"saturating_add" | "saturating_sub" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("saturating_add args are not provided"));
return Err(MirEvalError::InternalError(
"saturating_add args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -844,7 +876,9 @@ impl Evaluator<'_> {
}
"wrapping_add" | "unchecked_add" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("wrapping_add args are not provided"));
return Err(MirEvalError::InternalError(
"wrapping_add args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -853,7 +887,9 @@ impl Evaluator<'_> {
}
"ptr_offset_from_unsigned" | "ptr_offset_from" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("wrapping_sub args are not provided"));
return Err(MirEvalError::InternalError(
"wrapping_sub args are not provided".into(),
));
};
let lhs = i128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = i128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -861,8 +897,8 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError(
"ptr_offset_from generic arg is not provided",
return Err(MirEvalError::InternalError(
"ptr_offset_from generic arg is not provided".into(),
));
};
let size = self.size_of_sized(ty, locals, "ptr_offset_from arg")? as i128;
@ -871,7 +907,9 @@ impl Evaluator<'_> {
}
"wrapping_sub" | "unchecked_sub" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("wrapping_sub args are not provided"));
return Err(MirEvalError::InternalError(
"wrapping_sub args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -880,7 +918,9 @@ impl Evaluator<'_> {
}
"wrapping_mul" | "unchecked_mul" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("wrapping_mul args are not provided"));
return Err(MirEvalError::InternalError(
"wrapping_mul args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -890,7 +930,9 @@ impl Evaluator<'_> {
"wrapping_shl" | "unchecked_shl" => {
// FIXME: signed
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("unchecked_shl args are not provided"));
return Err(MirEvalError::InternalError(
"unchecked_shl args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -900,7 +942,9 @@ impl Evaluator<'_> {
"wrapping_shr" | "unchecked_shr" => {
// FIXME: signed
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("unchecked_shr args are not provided"));
return Err(MirEvalError::InternalError(
"unchecked_shr args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -910,7 +954,9 @@ impl Evaluator<'_> {
"unchecked_rem" => {
// FIXME: signed
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("unchecked_rem args are not provided"));
return Err(MirEvalError::InternalError(
"unchecked_rem args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -922,7 +968,9 @@ impl Evaluator<'_> {
"unchecked_div" | "exact_div" => {
// FIXME: signed
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("unchecked_div args are not provided"));
return Err(MirEvalError::InternalError(
"unchecked_div args are not provided".into(),
));
};
let lhs = u128::from_le_bytes(pad16(lhs.get(self)?, false));
let rhs = u128::from_le_bytes(pad16(rhs.get(self)?, false));
@ -933,7 +981,9 @@ impl Evaluator<'_> {
}
"add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("const_eval_select args are not provided"));
return Err(MirEvalError::InternalError(
"const_eval_select args are not provided".into(),
));
};
let result_ty = TyKind::Tuple(
2,
@ -954,7 +1004,7 @@ impl Evaluator<'_> {
|| ans.to_le_bytes()[op_size..].iter().any(|&it| it != 0 && it != 255);
let is_overflow = vec![u8::from(is_overflow)];
let layout = self.layout(&result_ty)?;
let result = self.make_by_layout(
let result = self.construct_with_layout(
layout.size.bytes_usize(),
&layout,
None,
@ -966,15 +1016,15 @@ impl Evaluator<'_> {
}
"copy" | "copy_nonoverlapping" => {
let [src, dst, offset] = args else {
return Err(MirEvalError::TypeError(
"copy_nonoverlapping args are not provided",
return Err(MirEvalError::InternalError(
"copy_nonoverlapping args are not provided".into(),
));
};
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError(
"copy_nonoverlapping generic arg is not provided",
return Err(MirEvalError::InternalError(
"copy_nonoverlapping generic arg is not provided".into(),
));
};
let src = Address::from_bytes(src.get(self)?)?;
@ -988,18 +1038,22 @@ impl Evaluator<'_> {
}
"offset" | "arith_offset" => {
let [ptr, offset] = args else {
return Err(MirEvalError::TypeError("offset args are not provided"));
return Err(MirEvalError::InternalError("offset args are not provided".into()));
};
let ty = if name == "offset" {
let Some(ty0) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("offset generic arg is not provided"));
return Err(MirEvalError::InternalError(
"offset generic arg is not provided".into(),
));
};
let Some(ty1) =
generic_args.as_slice(Interner).get(1).and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("offset generic arg is not provided"));
return Err(MirEvalError::InternalError(
"offset generic arg is not provided".into(),
));
};
if !matches!(
ty1.as_builtin(),
@ -1008,15 +1062,15 @@ impl Evaluator<'_> {
| BuiltinType::Uint(BuiltinUint::Usize)
)
) {
return Err(MirEvalError::TypeError(
"offset generic arg is not usize or isize",
return Err(MirEvalError::InternalError(
"offset generic arg is not usize or isize".into(),
));
}
match ty0.as_raw_ptr() {
Some((ty, _)) => ty,
None => {
return Err(MirEvalError::TypeError(
"offset generic arg is not a raw pointer",
return Err(MirEvalError::InternalError(
"offset generic arg is not a raw pointer".into(),
));
}
}
@ -1024,8 +1078,8 @@ impl Evaluator<'_> {
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError(
"arith_offset generic arg is not provided",
return Err(MirEvalError::InternalError(
"arith_offset generic arg is not provided".into(),
));
};
ty
@ -1046,19 +1100,21 @@ impl Evaluator<'_> {
}
"transmute" => {
let [arg] = args else {
return Err(MirEvalError::TypeError("transmute arg is not provided"));
return Err(MirEvalError::InternalError(
"transmute arg is not provided".into(),
));
};
destination.write_from_interval(self, arg.interval)
}
"likely" | "unlikely" => {
let [arg] = args else {
return Err(MirEvalError::TypeError("likely arg is not provided"));
return Err(MirEvalError::InternalError("likely arg is not provided".into()));
};
destination.write_from_interval(self, arg.interval)
}
"ctpop" => {
let [arg] = args else {
return Err(MirEvalError::TypeError("ctpop arg is not provided"));
return Err(MirEvalError::InternalError("ctpop arg is not provided".into()));
};
let result = u128::from_le_bytes(pad16(arg.get(self)?, false)).count_ones();
destination
@ -1066,7 +1122,7 @@ impl Evaluator<'_> {
}
"ctlz" | "ctlz_nonzero" => {
let [arg] = args else {
return Err(MirEvalError::TypeError("ctlz arg is not provided"));
return Err(MirEvalError::InternalError("ctlz arg is not provided".into()));
};
let result =
u128::from_le_bytes(pad16(arg.get(self)?, false)).leading_zeros() as usize;
@ -1076,7 +1132,7 @@ impl Evaluator<'_> {
}
"cttz" | "cttz_nonzero" => {
let [arg] = args else {
return Err(MirEvalError::TypeError("cttz arg is not provided"));
return Err(MirEvalError::InternalError("cttz arg is not provided".into()));
};
let result = u128::from_le_bytes(pad16(arg.get(self)?, false)).trailing_zeros();
destination
@ -1084,7 +1140,9 @@ impl Evaluator<'_> {
}
"rotate_left" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("rotate_left args are not provided"));
return Err(MirEvalError::InternalError(
"rotate_left args are not provided".into(),
));
};
let lhs = &lhs.get(self)?[0..destination.size];
let rhs = rhs.get(self)?[0] as u32;
@ -1114,7 +1172,9 @@ impl Evaluator<'_> {
}
"rotate_right" => {
let [lhs, rhs] = args else {
return Err(MirEvalError::TypeError("rotate_right args are not provided"));
return Err(MirEvalError::InternalError(
"rotate_right args are not provided".into(),
));
};
let lhs = &lhs.get(self)?[0..destination.size];
let rhs = rhs.get(self)?[0] as u32;
@ -1144,13 +1204,15 @@ impl Evaluator<'_> {
}
"discriminant_value" => {
let [arg] = args else {
return Err(MirEvalError::TypeError("discriminant_value arg is not provided"));
return Err(MirEvalError::InternalError(
"discriminant_value arg is not provided".into(),
));
};
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError(
"discriminant_value generic arg is not provided",
return Err(MirEvalError::InternalError(
"discriminant_value generic arg is not provided".into(),
));
};
let addr = Address::from_bytes(arg.get(self)?)?;
@ -1161,11 +1223,15 @@ impl Evaluator<'_> {
}
"const_eval_select" => {
let [tuple, const_fn, _] = args else {
return Err(MirEvalError::TypeError("const_eval_select args are not provided"));
return Err(MirEvalError::InternalError(
"const_eval_select args are not provided".into(),
));
};
let mut args = vec![const_fn.clone()];
let TyKind::Tuple(_, fields) = tuple.ty.kind(Interner) else {
return Err(MirEvalError::TypeError("const_eval_select arg[0] is not a tuple"));
return Err(MirEvalError::InternalError(
"const_eval_select arg[0] is not a tuple".into(),
));
};
let layout = self.layout(&tuple.ty)?;
for (i, field) in fields.iter(Interner).enumerate() {
@ -1196,21 +1262,25 @@ impl Evaluator<'_> {
}
"read_via_copy" | "volatile_load" => {
let [arg] = args else {
return Err(MirEvalError::TypeError("read_via_copy args are not provided"));
return Err(MirEvalError::InternalError(
"read_via_copy args are not provided".into(),
));
};
let addr = Address::from_bytes(arg.interval.get(self)?)?;
destination.write_from_interval(self, Interval { addr, size: destination.size })
}
"write_via_move" => {
let [ptr, val] = args else {
return Err(MirEvalError::TypeError("write_via_move args are not provided"));
return Err(MirEvalError::InternalError(
"write_via_move args are not provided".into(),
));
};
let dst = Address::from_bytes(ptr.get(self)?)?;
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError(
"write_via_copy generic arg is not provided",
return Err(MirEvalError::InternalError(
"write_via_copy generic arg is not provided".into(),
));
};
let size = self.size_of_sized(ty, locals, "write_via_move ptr type")?;
@ -1219,14 +1289,18 @@ impl Evaluator<'_> {
}
"write_bytes" => {
let [dst, val, count] = args else {
return Err(MirEvalError::TypeError("write_bytes args are not provided"));
return Err(MirEvalError::InternalError(
"write_bytes args are not provided".into(),
));
};
let count = from_bytes!(usize, count.get(self)?);
let val = from_bytes!(u8, val.get(self)?);
let Some(ty) =
generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("write_bytes generic arg is not provided"));
return Err(MirEvalError::InternalError(
"write_bytes generic arg is not provided".into(),
));
};
let dst = Address::from_bytes(dst.get(self)?)?;
let size = self.size_of_sized(ty, locals, "copy_nonoverlapping ptr type")?;
@ -1310,10 +1384,14 @@ impl Evaluator<'_> {
let Some(ty) = generic_args.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("atomic intrinsic generic arg is not provided"));
return Err(MirEvalError::InternalError(
"atomic intrinsic generic arg is not provided".into(),
));
};
let Some(arg0) = args.first() else {
return Err(MirEvalError::TypeError("atomic intrinsic arg0 is not provided"));
return Err(MirEvalError::InternalError(
"atomic intrinsic arg0 is not provided".into(),
));
};
let arg0_addr = Address::from_bytes(arg0.get(self)?)?;
let arg0_interval =
@ -1322,7 +1400,9 @@ impl Evaluator<'_> {
return destination.write_from_interval(self, arg0_interval);
}
let Some(arg1) = args.get(1) else {
return Err(MirEvalError::TypeError("atomic intrinsic arg1 is not provided"));
return Err(MirEvalError::InternalError(
"atomic intrinsic arg1 is not provided".into(),
));
};
if name.starts_with("store_") {
return arg0_interval.write_from_interval(self, arg1.interval);
@ -1374,7 +1454,9 @@ impl Evaluator<'_> {
return arg0_interval.write_from_bytes(self, &ans.to_le_bytes()[0..destination.size]);
}
let Some(arg2) = args.get(2) else {
return Err(MirEvalError::TypeError("atomic intrinsic arg2 is not provided"));
return Err(MirEvalError::InternalError(
"atomic intrinsic arg2 is not provided".into(),
));
};
if name.starts_with("cxchg_") || name.starts_with("cxchgweak_") {
let dest = if arg1.get(self)? == arg0_interval.get(self)? {
@ -1389,7 +1471,7 @@ impl Evaluator<'_> {
)
.intern(Interner);
let layout = self.layout(&result_ty)?;
let result = self.make_by_layout(
let result = self.construct_with_layout(
layout.size.bytes_usize(),
&layout,
None,

View file

@ -10,7 +10,7 @@ macro_rules! from_bytes {
($ty:tt, $value:expr) => {
($ty::from_le_bytes(match ($value).try_into() {
Ok(it) => it,
Err(_) => return Err(MirEvalError::TypeError("mismatched size")),
Err(_) => return Err(MirEvalError::InternalError("mismatched size".into())),
}))
};
}
@ -40,7 +40,9 @@ impl Evaluator<'_> {
.substitute(Interner, subst);
return Ok((fields.len(), field_ty));
}
return Err(MirEvalError::TypeError("simd type with no len param"));
return Err(MirEvalError::InternalError(
"simd type with no len param".into(),
));
}
};
match try_const_usize(self.db, len) {
@ -48,14 +50,18 @@ impl Evaluator<'_> {
let Some(ty) =
subst.as_slice(Interner).first().and_then(|it| it.ty(Interner))
else {
return Err(MirEvalError::TypeError("simd type with no ty param"));
return Err(MirEvalError::InternalError(
"simd type with no ty param".into(),
));
};
Ok((len as usize, ty.clone()))
}
None => Err(MirEvalError::TypeError("simd type with unevaluatable len param")),
None => Err(MirEvalError::InternalError(
"simd type with unevaluatable len param".into(),
)),
}
}
_ => Err(MirEvalError::TypeError("simd type which is not a struct")),
_ => Err(MirEvalError::InternalError("simd type which is not a struct".into())),
}
}
@ -71,7 +77,9 @@ impl Evaluator<'_> {
match name {
"and" | "or" | "xor" => {
let [left, right] = args else {
return Err(MirEvalError::TypeError("simd bit op args are not provided"));
return Err(MirEvalError::InternalError(
"simd bit op args are not provided".into(),
));
};
let result = left
.get(self)?
@ -88,7 +96,7 @@ impl Evaluator<'_> {
}
"eq" | "ne" | "lt" | "le" | "gt" | "ge" => {
let [left, right] = args else {
return Err(MirEvalError::TypeError("simd args are not provided"));
return Err(MirEvalError::InternalError("simd args are not provided".into()));
};
let (len, ty) = self.detect_simd_ty(&left.ty)?;
let is_signed = matches!(ty.as_builtin(), Some(BuiltinType::Int(_)));
@ -125,7 +133,9 @@ impl Evaluator<'_> {
}
"bitmask" => {
let [op] = args else {
return Err(MirEvalError::TypeError("simd_bitmask args are not provided"));
return Err(MirEvalError::InternalError(
"simd_bitmask args are not provided".into(),
));
};
let (op_len, _) = self.detect_simd_ty(&op.ty)?;
let op_count = op.interval.size / op_len;
@ -139,18 +149,20 @@ impl Evaluator<'_> {
}
"shuffle" => {
let [left, right, index] = args else {
return Err(MirEvalError::TypeError("simd_shuffle args are not provided"));
return Err(MirEvalError::InternalError(
"simd_shuffle args are not provided".into(),
));
};
let TyKind::Array(_, index_len) = index.ty.kind(Interner) else {
return Err(MirEvalError::TypeError(
"simd_shuffle index argument has non-array type",
return Err(MirEvalError::InternalError(
"simd_shuffle index argument has non-array type".into(),
));
};
let index_len = match try_const_usize(self.db, index_len) {
Some(it) => it as usize,
None => {
return Err(MirEvalError::TypeError(
"simd type with unevaluatable len param",
return Err(MirEvalError::InternalError(
"simd type with unevaluatable len param".into(),
))
}
};
@ -164,8 +176,8 @@ impl Evaluator<'_> {
let val = match vector.clone().nth(index) {
Some(it) => it,
None => {
return Err(MirEvalError::TypeError(
"out of bound access in simd shuffle",
return Err(MirEvalError::InternalError(
"out of bound access in simd shuffle".into(),
))
}
};

View file

@ -939,7 +939,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
Ok(Some(current))
}
Expr::BinaryOp { lhs, rhs, op } => {
let op = op.ok_or(MirLowerError::IncompleteExpr)?;
let op: BinaryOp = op.ok_or(MirLowerError::IncompleteExpr)?;
let is_builtin = 'b: {
// Without adjust here is a hack. We assume that we know every possible adjustment
// for binary operator, and use without adjust to simplify our conditions.

View file

@ -24,18 +24,18 @@ use hir_def::{
};
use hir_expand::name::Name;
use intern::Interned;
use ra_ap_rustc_abi::TargetDataLayout;
use rustc_hash::FxHashSet;
use smallvec::{smallvec, SmallVec};
use stdx::never;
use triomphe::Arc;
use crate::{
consteval::unknown_const,
db::HirDatabase,
layout::{Layout, TagEncoding},
mir::pad16,
ChalkTraitId, Const, ConstScalar, GenericArg, Interner, Substitution, TraitEnvironment,
TraitRef, TraitRefExt, Ty, WhereClause,
ChalkTraitId, Const, ConstScalar, GenericArg, Interner, Substitution, TraitRef, TraitRefExt,
Ty, WhereClause,
};
pub(crate) fn fn_traits(
@ -431,17 +431,15 @@ impl FallibleTypeFolder<Interner> for UnevaluatedConstEvaluatorFolder<'_> {
pub(crate) fn detect_variant_from_bytes<'a>(
layout: &'a Layout,
db: &dyn HirDatabase,
trait_env: Arc<TraitEnvironment>,
target_data_layout: &TargetDataLayout,
b: &[u8],
e: EnumId,
) -> Option<(EnumVariantId, &'a Layout)> {
let krate = trait_env.krate;
let (var_id, var_layout) = match &layout.variants {
hir_def::layout::Variants::Single { index } => {
(db.enum_data(e).variants[index.0].0, layout)
}
hir_def::layout::Variants::Multiple { tag, tag_encoding, variants, .. } => {
let target_data_layout = db.target_data_layout(krate)?;
let size = tag.size(&*target_data_layout).bytes_usize();
let offset = layout.fields.offset(0).bytes_usize(); // The only field on enum variants is the tag field
let tag = i128::from_le_bytes(pad16(&b[offset..offset + size], false));

View file

@ -4,11 +4,12 @@
//! This probably isn't the best way to do this -- ideally, diagnostics should
//! be expressed in terms of hir types themselves.
pub use hir_ty::diagnostics::{CaseType, IncorrectCase};
use hir_ty::{db::HirDatabase, diagnostics::BodyValidationDiagnostic, InferenceDiagnostic};
use base_db::CrateId;
use cfg::{CfgExpr, CfgOptions};
use either::Either;
use hir_def::{path::ModPath, AssocItemId};
use hir_def::{body::SyntheticSyntax, hir::ExprOrPatId, path::ModPath, AssocItemId, DefWithBodyId};
use hir_expand::{name::Name, HirFileId, InFile};
use syntax::{ast, AstPtr, SyntaxError, SyntaxNodePtr, TextRange};
@ -30,14 +31,28 @@ macro_rules! diagnostics {
)*
};
}
// FIXME Accept something like the following in the macro call instead
// diagnostics![
// pub struct BreakOutsideOfLoop {
// pub expr: InFile<AstPtr<ast::Expr>>,
// pub is_break: bool,
// pub bad_value_break: bool,
// }, ...
// or more concisely
// BreakOutsideOfLoop {
// expr: InFile<AstPtr<ast::Expr>>,
// is_break: bool,
// bad_value_break: bool,
// }, ...
// ]
diagnostics![
BreakOutsideOfLoop,
ExpectedFunction,
InactiveCode,
IncoherentImpl,
IncorrectCase,
InvalidDeriveTarget,
IncoherentImpl,
MacroDefError,
MacroError,
MacroExpansionParseError,
@ -55,8 +70,8 @@ diagnostics![
ReplaceFilterMapNextWithFindMap,
TraitImplIncorrectSafety,
TraitImplMissingAssocItems,
TraitImplRedundantAssocItems,
TraitImplOrphan,
TraitImplRedundantAssocItems,
TypedHole,
TypeMismatch,
UndeclaredLabel,
@ -326,3 +341,219 @@ pub struct TraitImplRedundantAssocItems {
pub impl_: AstPtr<ast::Impl>,
pub assoc_item: (Name, AssocItem),
}
impl AnyDiagnostic {
pub(crate) fn body_validation_diagnostic(
db: &dyn HirDatabase,
diagnostic: BodyValidationDiagnostic,
source_map: &hir_def::body::BodySourceMap,
) -> Option<AnyDiagnostic> {
match diagnostic {
BodyValidationDiagnostic::RecordMissingFields { record, variant, missed_fields } => {
let variant_data = variant.variant_data(db.upcast());
let missed_fields = missed_fields
.into_iter()
.map(|idx| variant_data.fields()[idx].name.clone())
.collect();
match record {
Either::Left(record_expr) => match source_map.expr_syntax(record_expr) {
Ok(source_ptr) => {
let root = source_ptr.file_syntax(db.upcast());
if let ast::Expr::RecordExpr(record_expr) =
source_ptr.value.to_node(&root)
{
if record_expr.record_expr_field_list().is_some() {
let field_list_parent_path =
record_expr.path().map(|path| AstPtr::new(&path));
return Some(
MissingFields {
file: source_ptr.file_id,
field_list_parent: AstPtr::new(&Either::Left(
record_expr,
)),
field_list_parent_path,
missed_fields,
}
.into(),
);
}
}
}
Err(SyntheticSyntax) => (),
},
Either::Right(record_pat) => match source_map.pat_syntax(record_pat) {
Ok(source_ptr) => {
if let Some(ptr) = source_ptr.value.cast::<ast::RecordPat>() {
let root = source_ptr.file_syntax(db.upcast());
let record_pat = ptr.to_node(&root);
if record_pat.record_pat_field_list().is_some() {
let field_list_parent_path =
record_pat.path().map(|path| AstPtr::new(&path));
return Some(
MissingFields {
file: source_ptr.file_id,
field_list_parent: AstPtr::new(&Either::Right(
record_pat,
)),
field_list_parent_path,
missed_fields,
}
.into(),
);
}
}
}
Err(SyntheticSyntax) => (),
},
}
}
BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { method_call_expr } => {
if let Ok(next_source_ptr) = source_map.expr_syntax(method_call_expr) {
return Some(
ReplaceFilterMapNextWithFindMap {
file: next_source_ptr.file_id,
next_expr: next_source_ptr.value,
}
.into(),
);
}
}
BodyValidationDiagnostic::MissingMatchArms { match_expr, uncovered_patterns } => {
match source_map.expr_syntax(match_expr) {
Ok(source_ptr) => {
let root = source_ptr.file_syntax(db.upcast());
if let ast::Expr::MatchExpr(match_expr) = &source_ptr.value.to_node(&root) {
match match_expr.expr() {
Some(scrut_expr) if match_expr.match_arm_list().is_some() => {
return Some(
MissingMatchArms {
scrutinee_expr: InFile::new(
source_ptr.file_id,
AstPtr::new(&scrut_expr),
),
uncovered_patterns,
}
.into(),
);
}
_ => {}
}
}
}
Err(SyntheticSyntax) => (),
}
}
}
None
}
pub(crate) fn inference_diagnostic(
db: &dyn HirDatabase,
def: DefWithBodyId,
d: &InferenceDiagnostic,
source_map: &hir_def::body::BodySourceMap,
) -> Option<AnyDiagnostic> {
let expr_syntax = |expr| source_map.expr_syntax(expr).expect("unexpected synthetic");
let pat_syntax = |pat| source_map.pat_syntax(pat).expect("unexpected synthetic");
Some(match d {
&InferenceDiagnostic::NoSuchField { field: expr, private } => {
let expr_or_pat = match expr {
ExprOrPatId::ExprId(expr) => {
source_map.field_syntax(expr).map(AstPtr::wrap_left)
}
ExprOrPatId::PatId(pat) => {
source_map.pat_field_syntax(pat).map(AstPtr::wrap_right)
}
};
NoSuchField { field: expr_or_pat, private }.into()
}
&InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
MismatchedArgCount { call_expr: expr_syntax(call_expr), expected, found }.into()
}
&InferenceDiagnostic::PrivateField { expr, field } => {
let expr = expr_syntax(expr);
let field = field.into();
PrivateField { expr, field }.into()
}
&InferenceDiagnostic::PrivateAssocItem { id, item } => {
let expr_or_pat = match id {
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
ExprOrPatId::PatId(pat) => pat_syntax(pat).map(AstPtr::wrap_right),
};
let item = item.into();
PrivateAssocItem { expr_or_pat, item }.into()
}
InferenceDiagnostic::ExpectedFunction { call_expr, found } => {
let call_expr = expr_syntax(*call_expr);
ExpectedFunction { call: call_expr, found: Type::new(db, def, found.clone()) }
.into()
}
InferenceDiagnostic::UnresolvedField {
expr,
receiver,
name,
method_with_same_name_exists,
} => {
let expr = expr_syntax(*expr);
UnresolvedField {
expr,
name: name.clone(),
receiver: Type::new(db, def, receiver.clone()),
method_with_same_name_exists: *method_with_same_name_exists,
}
.into()
}
InferenceDiagnostic::UnresolvedMethodCall {
expr,
receiver,
name,
field_with_same_name,
assoc_func_with_same_name,
} => {
let expr = expr_syntax(*expr);
UnresolvedMethodCall {
expr,
name: name.clone(),
receiver: Type::new(db, def, receiver.clone()),
field_with_same_name: field_with_same_name
.clone()
.map(|ty| Type::new(db, def, ty)),
assoc_func_with_same_name: *assoc_func_with_same_name,
}
.into()
}
&InferenceDiagnostic::UnresolvedAssocItem { id } => {
let expr_or_pat = match id {
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
ExprOrPatId::PatId(pat) => pat_syntax(pat).map(AstPtr::wrap_right),
};
UnresolvedAssocItem { expr_or_pat }.into()
}
&InferenceDiagnostic::BreakOutsideOfLoop { expr, is_break, bad_value_break } => {
let expr = expr_syntax(expr);
BreakOutsideOfLoop { expr, is_break, bad_value_break }.into()
}
InferenceDiagnostic::TypedHole { expr, expected } => {
let expr = expr_syntax(*expr);
TypedHole { expr, expected: Type::new(db, def, expected.clone()) }.into()
}
&InferenceDiagnostic::MismatchedTupleStructPatArgCount { pat, expected, found } => {
let expr_or_pat = match pat {
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
ExprOrPatId::PatId(pat) => {
let InFile { file_id, value } =
source_map.pat_syntax(pat).expect("unexpected synthetic");
// cast from Either<Pat, SelfParam> -> Either<_, Pat>
let Some(ptr) = AstPtr::try_from_raw(value.syntax_node_ptr()) else {
return None;
};
InFile { file_id, value: ptr }
}
};
MismatchedTupleStructPatArgCount { expr_or_pat, expected, found }.into()
}
})
}
}

View file

@ -61,7 +61,7 @@ use hir_def::{
use hir_expand::{attrs::collect_attrs, name::name, proc_macro::ProcMacroKind, MacroCallKind};
use hir_ty::{
all_super_traits, autoderef, check_orphan_rules,
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
consteval::{try_const_usize, unknown_const_as_generic, ConstExt},
diagnostics::BodyValidationDiagnostic,
known_const_to_ast,
layout::{Layout as TyLayout, RustcEnumVariantIdx, RustcFieldIdx, TagEncoding},
@ -70,9 +70,9 @@ use hir_ty::{
primitive::UintTy,
traits::FnTrait,
AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId, GenericArg,
GenericArgData, InferenceDiagnostic, Interner, ParamKind, QuantifiedWhereClause, Scalar,
Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind,
ValueTyDefId, WhereClause,
GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution,
TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, ValueTyDefId,
WhereClause,
};
use itertools::Itertools;
use nameres::diagnostics::DefDiagnosticKind;
@ -131,8 +131,10 @@ pub use {
MacroFileIdExt,
},
hir_ty::{
consteval::ConstEvalError,
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite},
layout::LayoutError,
mir::{MirEvalError, MirLowerError},
PointerCast, Safety,
},
// FIXME: Properly encapsulate mir
@ -1626,116 +1628,8 @@ impl DefWithBody {
}
let infer = db.infer(self.into());
let expr_syntax = |expr| source_map.expr_syntax(expr).expect("unexpected synthetic");
let pat_syntax = |pat| source_map.pat_syntax(pat).expect("unexpected synthetic");
for d in &infer.diagnostics {
acc.push(match d {
&InferenceDiagnostic::NoSuchField { field: expr, private } => {
let expr_or_pat = match expr {
ExprOrPatId::ExprId(expr) => {
source_map.field_syntax(expr).map(AstPtr::wrap_left)
}
ExprOrPatId::PatId(pat) => {
source_map.pat_field_syntax(pat).map(AstPtr::wrap_right)
}
};
NoSuchField { field: expr_or_pat, private }.into()
}
&InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
MismatchedArgCount { call_expr: expr_syntax(call_expr), expected, found }.into()
}
&InferenceDiagnostic::PrivateField { expr, field } => {
let expr = expr_syntax(expr);
let field = field.into();
PrivateField { expr, field }.into()
}
&InferenceDiagnostic::PrivateAssocItem { id, item } => {
let expr_or_pat = match id {
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
ExprOrPatId::PatId(pat) => pat_syntax(pat).map(AstPtr::wrap_right),
};
let item = item.into();
PrivateAssocItem { expr_or_pat, item }.into()
}
InferenceDiagnostic::ExpectedFunction { call_expr, found } => {
let call_expr = expr_syntax(*call_expr);
ExpectedFunction {
call: call_expr,
found: Type::new(db, DefWithBodyId::from(self), found.clone()),
}
.into()
}
InferenceDiagnostic::UnresolvedField {
expr,
receiver,
name,
method_with_same_name_exists,
} => {
let expr = expr_syntax(*expr);
UnresolvedField {
expr,
name: name.clone(),
receiver: Type::new(db, DefWithBodyId::from(self), receiver.clone()),
method_with_same_name_exists: *method_with_same_name_exists,
}
.into()
}
InferenceDiagnostic::UnresolvedMethodCall {
expr,
receiver,
name,
field_with_same_name,
assoc_func_with_same_name,
} => {
let expr = expr_syntax(*expr);
UnresolvedMethodCall {
expr,
name: name.clone(),
receiver: Type::new(db, DefWithBodyId::from(self), receiver.clone()),
field_with_same_name: field_with_same_name
.clone()
.map(|ty| Type::new(db, DefWithBodyId::from(self), ty)),
assoc_func_with_same_name: *assoc_func_with_same_name,
}
.into()
}
&InferenceDiagnostic::UnresolvedAssocItem { id } => {
let expr_or_pat = match id {
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
ExprOrPatId::PatId(pat) => pat_syntax(pat).map(AstPtr::wrap_right),
};
UnresolvedAssocItem { expr_or_pat }.into()
}
&InferenceDiagnostic::BreakOutsideOfLoop { expr, is_break, bad_value_break } => {
let expr = expr_syntax(expr);
BreakOutsideOfLoop { expr, is_break, bad_value_break }.into()
}
InferenceDiagnostic::TypedHole { expr, expected } => {
let expr = expr_syntax(*expr);
TypedHole {
expr,
expected: Type::new(db, DefWithBodyId::from(self), expected.clone()),
}
.into()
}
&InferenceDiagnostic::MismatchedTupleStructPatArgCount { pat, expected, found } => {
let expr_or_pat = match pat {
ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
ExprOrPatId::PatId(pat) => {
let InFile { file_id, value } =
source_map.pat_syntax(pat).expect("unexpected synthetic");
// cast from Either<Pat, SelfParam> -> Either<_, Pat>
let Some(ptr) = AstPtr::try_from_raw(value.syntax_node_ptr()) else {
continue;
};
InFile { file_id, value: ptr }
}
};
MismatchedTupleStructPatArgCount { expr_or_pat, expected, found }.into()
}
});
acc.extend(AnyDiagnostic::inference_diagnostic(db, self.into(), d, &source_map));
}
for (pat_or_expr, mismatch) in infer.type_mismatches() {
let expr_or_pat = match pat_or_expr {
@ -1857,109 +1751,7 @@ impl DefWithBody {
}
for diagnostic in BodyValidationDiagnostic::collect(db, self.into()) {
match diagnostic {
BodyValidationDiagnostic::RecordMissingFields {
record,
variant,
missed_fields,
} => {
let variant_data = variant.variant_data(db.upcast());
let missed_fields = missed_fields
.into_iter()
.map(|idx| variant_data.fields()[idx].name.clone())
.collect();
match record {
Either::Left(record_expr) => match source_map.expr_syntax(record_expr) {
Ok(source_ptr) => {
let root = source_ptr.file_syntax(db.upcast());
if let ast::Expr::RecordExpr(record_expr) =
source_ptr.value.to_node(&root)
{
if record_expr.record_expr_field_list().is_some() {
let field_list_parent_path =
record_expr.path().map(|path| AstPtr::new(&path));
acc.push(
MissingFields {
file: source_ptr.file_id,
field_list_parent: AstPtr::new(&Either::Left(
record_expr,
)),
field_list_parent_path,
missed_fields,
}
.into(),
)
}
}
}
Err(SyntheticSyntax) => (),
},
Either::Right(record_pat) => match source_map.pat_syntax(record_pat) {
Ok(source_ptr) => {
if let Some(ptr) = source_ptr.value.cast::<ast::RecordPat>() {
let root = source_ptr.file_syntax(db.upcast());
let record_pat = ptr.to_node(&root);
if record_pat.record_pat_field_list().is_some() {
let field_list_parent_path =
record_pat.path().map(|path| AstPtr::new(&path));
acc.push(
MissingFields {
file: source_ptr.file_id,
field_list_parent: AstPtr::new(&Either::Right(
record_pat,
)),
field_list_parent_path,
missed_fields,
}
.into(),
)
}
}
}
Err(SyntheticSyntax) => (),
},
}
}
BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { method_call_expr } => {
if let Ok(next_source_ptr) = source_map.expr_syntax(method_call_expr) {
acc.push(
ReplaceFilterMapNextWithFindMap {
file: next_source_ptr.file_id,
next_expr: next_source_ptr.value,
}
.into(),
);
}
}
BodyValidationDiagnostic::MissingMatchArms { match_expr, uncovered_patterns } => {
match source_map.expr_syntax(match_expr) {
Ok(source_ptr) => {
let root = source_ptr.file_syntax(db.upcast());
if let ast::Expr::MatchExpr(match_expr) =
&source_ptr.value.to_node(&root)
{
match match_expr.expr() {
Some(scrut_expr) if match_expr.match_arm_list().is_some() => {
acc.push(
MissingMatchArms {
scrutinee_expr: InFile::new(
source_ptr.file_id,
AstPtr::new(&scrut_expr),
),
uncovered_patterns,
}
.into(),
);
}
_ => {}
}
}
}
Err(SyntheticSyntax) => (),
}
}
}
acc.extend(AnyDiagnostic::body_validation_diagnostic(db, diagnostic, &source_map));
}
let def: ModuleDef = match self {
@ -1975,7 +1767,6 @@ impl DefWithBody {
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Function {
pub(crate) id: FunctionId,

View file

@ -7196,8 +7196,8 @@ impl Iterator for S {
file_id: FileId(
1,
),
full_range: 6012..6220,
focus_range: 6077..6083,
full_range: 6156..6364,
focus_range: 6221..6227,
name: "Future",
kind: Trait,
container_name: "future",
@ -7210,8 +7210,8 @@ impl Iterator for S {
file_id: FileId(
1,
),
full_range: 6850..7316,
focus_range: 6894..6902,
full_range: 6994..7460,
focus_range: 7038..7046,
name: "Iterator",
kind: Trait,
container_name: "iterator",

View file

@ -302,13 +302,13 @@ impl flags::AnalysisStats {
let mut fail = 0;
for &c in consts {
all += 1;
let Err(e) = c.render_eval(db) else {
let Err(error) = c.render_eval(db) else {
continue;
};
if verbosity.is_spammy() {
let full_name =
full_name_of_item(db, c.module(db), c.name(db).unwrap_or(Name::missing()));
println!("Const eval for {full_name} failed due {e:?}");
println!("Const eval for {full_name} failed due {error:?}");
}
fail += 1;
}

View file

@ -28,7 +28,7 @@
//! env: option
//! eq: sized
//! error: fmt
//! fmt: result, transmute, coerce_unsized
//! fmt: option, result, transmute, coerce_unsized
//! fn:
//! from: sized
//! future: pin
@ -987,6 +987,10 @@ pub mod fmt {
Arguments { pieces, fmt: None, args }
}
pub const fn new_const(pieces: &'a [&'static str]) -> Arguments<'a> {
Arguments { pieces, fmt: None, args: &[] }
}
pub fn new_v1_formatted(
pieces: &'a [&'static str],
args: &'a [rt::Argument<'a>],
@ -1346,6 +1350,9 @@ pub mod iter {
// region:panic
mod panic {
pub macro panic_2021 {
() => (
$crate::panicking::panic("explicit panic")
),
($($t:tt)+) => (
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+))
),
@ -1357,6 +1364,11 @@ mod panicking {
pub const fn panic_fmt(_fmt: crate::fmt::Arguments<'_>) -> ! {
loop {}
}
#[lang = "panic"]
pub const fn panic(expr: &'static str) -> ! {
panic_fmt(crate::fmt::Arguments::new_const(&[expr]))
}
}
// endregion:panic