Shrink some stuff

This commit is contained in:
Lukas Wirth 2023-09-09 22:45:01 +02:00
parent 994df3d6a3
commit ccff704c25
7 changed files with 18 additions and 16 deletions

View file

@ -24,7 +24,7 @@ pub use self::{
macro_rules! user_error { macro_rules! user_error {
($it: expr) => { ($it: expr) => {
return Err(LayoutError::UserError(format!($it))) return Err(LayoutError::UserError(format!($it).into()))
}; };
} }
@ -50,7 +50,7 @@ pub type Variants = hir_def::layout::Variants<RustcEnumVariantIdx>;
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum LayoutError { pub enum LayoutError {
UserError(String), UserError(Box<str>),
SizeOverflow, SizeOverflow,
TargetLayoutNotAvailable, TargetLayoutNotAvailable,
HasPlaceholder, HasPlaceholder,
@ -234,9 +234,9 @@ pub fn layout_of_ty_query(
cx.univariant(dl, &fields, &ReprOptions::default(), kind).ok_or(LayoutError::Unknown)? cx.univariant(dl, &fields, &ReprOptions::default(), kind).ok_or(LayoutError::Unknown)?
} }
TyKind::Array(element, count) => { TyKind::Array(element, count) => {
let count = try_const_usize(db, &count).ok_or(LayoutError::UserError( let count = try_const_usize(db, &count).ok_or(LayoutError::UserError(Box::from(
"unevaluated or mistyped const generic parameter".to_string(), "unevaluated or mistyped const generic parameter",
))? as u64; )))? as u64;
let element = db.layout_of_ty(element.clone(), trait_env.clone())?; let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?; let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?;

View file

@ -163,7 +163,7 @@ fn repr_discr(
return Err(LayoutError::UserError( return Err(LayoutError::UserError(
"Integer::repr_discr: `#[repr]` hint too small for \ "Integer::repr_discr: `#[repr]` hint too small for \
discriminant range of enum " discriminant range of enum "
.to_string(), .into(),
)); ));
} }
return Ok((discr, ity.is_signed())); return Ok((discr, ity.is_signed()));

View file

@ -212,14 +212,14 @@ fn recursive() {
} }
check_fail( check_fail(
r#"struct Goal(Goal);"#, r#"struct Goal(Goal);"#,
LayoutError::UserError("infinite sized recursive type".to_string()), LayoutError::UserError("infinite sized recursive type".into()),
); );
check_fail( check_fail(
r#" r#"
struct Foo<T>(Foo<T>); struct Foo<T>(Foo<T>);
struct Goal(Foo<i32>); struct Goal(Foo<i32>);
"#, "#,
LayoutError::UserError("infinite sized recursive type".to_string()), LayoutError::UserError("infinite sized recursive type".into()),
); );
} }

View file

@ -179,6 +179,7 @@ fn moved_out_of_ref(db: &dyn HirDatabase, body: &MirBody) -> Vec<MovedOutOfRef>
None => (), None => (),
} }
} }
result.shrink_to_fit();
result result
} }

View file

@ -339,7 +339,7 @@ pub enum MirEvalError {
InvalidVTableId(usize), InvalidVTableId(usize),
CoerceUnsizedError(Ty), CoerceUnsizedError(Ty),
LangItemNotFound(LangItem), LangItemNotFound(LangItem),
BrokenLayout(Layout), BrokenLayout(Box<Layout>),
} }
impl MirEvalError { impl MirEvalError {
@ -408,7 +408,7 @@ impl MirEvalError {
err.pretty_print(f, db, span_formatter)?; err.pretty_print(f, db, span_formatter)?;
} }
MirEvalError::ConstEvalError(name, err) => { MirEvalError::ConstEvalError(name, err) => {
MirLowerError::ConstEvalError(name.clone(), err.clone()).pretty_print( MirLowerError::ConstEvalError((**name).into(), err.clone()).pretty_print(
f, f,
db, db,
span_formatter, span_formatter,
@ -1632,7 +1632,7 @@ impl Evaluator<'_> {
if let Some((offset, size, value)) = tag { if let Some((offset, size, value)) = tag {
match result.get_mut(offset..offset + size) { match result.get_mut(offset..offset + size) {
Some(it) => it.copy_from_slice(&value.to_le_bytes()[0..size]), Some(it) => it.copy_from_slice(&value.to_le_bytes()[0..size]),
None => return Err(MirEvalError::BrokenLayout(variant_layout.clone())), None => return Err(MirEvalError::BrokenLayout(Box::new(variant_layout.clone()))),
} }
} }
for (i, op) in values.enumerate() { for (i, op) in values.enumerate() {
@ -1640,7 +1640,7 @@ impl Evaluator<'_> {
let op = op.get(&self)?; let op = op.get(&self)?;
match result.get_mut(offset..offset + op.len()) { match result.get_mut(offset..offset + op.len()) {
Some(it) => it.copy_from_slice(op), Some(it) => it.copy_from_slice(op),
None => return Err(MirEvalError::BrokenLayout(variant_layout.clone())), None => return Err(MirEvalError::BrokenLayout(Box::new(variant_layout.clone()))),
} }
} }
Ok(result) Ok(result)

View file

@ -71,7 +71,7 @@ struct MirLowerCtx<'a> {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum MirLowerError { pub enum MirLowerError {
ConstEvalError(String, Box<ConstEvalError>), ConstEvalError(Box<str>, Box<ConstEvalError>),
LayoutError(LayoutError), LayoutError(LayoutError),
IncompleteExpr, IncompleteExpr,
IncompletePattern, IncompletePattern,
@ -84,7 +84,7 @@ pub enum MirLowerError {
UnsizedTemporary(Ty), UnsizedTemporary(Ty),
MissingFunctionDefinition(DefWithBodyId, ExprId), MissingFunctionDefinition(DefWithBodyId, ExprId),
TypeMismatch(TypeMismatch), TypeMismatch(TypeMismatch),
/// This should be never happen. Type mismatch should catch everything. /// This should never happen. Type mismatch should catch everything.
TypeError(&'static str), TypeError(&'static str),
NotSupported(String), NotSupported(String),
ContinueWithoutLoop, ContinueWithoutLoop,
@ -1456,7 +1456,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
let name = const_id.name(self.db.upcast()); let name = const_id.name(self.db.upcast());
self.db self.db
.const_eval(const_id.into(), subst, None) .const_eval(const_id.into(), subst, None)
.map_err(|e| MirLowerError::ConstEvalError(name, Box::new(e)))? .map_err(|e| MirLowerError::ConstEvalError(name.into(), Box::new(e)))?
}; };
Ok(Operand::Constant(c)) Ok(Operand::Constant(c))
} }
@ -1853,7 +1853,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
data.name.display(self.db.upcast()), data.name.display(self.db.upcast()),
data.variants[variant.local_id].name.display(self.db.upcast()) data.variants[variant.local_id].name.display(self.db.upcast())
); );
Err(MirLowerError::ConstEvalError(name, Box::new(e))) Err(MirLowerError::ConstEvalError(name.into(), Box::new(e)))
} }
} }
} }

View file

@ -66,6 +66,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
None => format!("{}", krate.into_raw()), None => format!("{}", krate.into_raw()),
}; };
format_to!(buf, "Crate: {}\n", display_crate(krate)); format_to!(buf, "Crate: {}\n", display_crate(krate));
format_to!(buf, "Enabled cfgs: {:?}\n", crate_graph[krate].cfg_options);
let deps = crate_graph[krate] let deps = crate_graph[krate]
.dependencies .dependencies
.iter() .iter()