Skip to content

Commit

Permalink
Auto merge of #16447 - Veykril:mir-errors, r=Veykril
Browse files Browse the repository at this point in the history
intenral: Cleanup error variants in MIR code a bit
  • Loading branch information
bors committed Jan 30, 2024
2 parents 22b6f96 + 5860763 commit 11b401d
Show file tree
Hide file tree
Showing 17 changed files with 598 additions and 434 deletions.
2 changes: 1 addition & 1 deletion crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Proc Macro Expander stub
//! Proc Macro Expander stuff

use core::fmt;
use std::{panic::RefUnwindSafe, sync};
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>");
};
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/layout/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
44 changes: 35 additions & 9 deletions crates/hir-ty/src/layout/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,48 @@

use base_db::CrateId;
use hir_def::layout::TargetDataLayout;
use ra_ap_rustc_abi::{AlignFromBytesError, TargetDataLayoutErrors};
use triomphe::Arc;

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)
}
2 changes: 1 addition & 1 deletion crates/hir-ty/src/mir/borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 11b401d

Please sign in to comment.