Skip to content

Commit

Permalink
Sync from rust faae5f1
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Mar 30, 2024
2 parents 29b2d42 + b2f6349 commit 362caf7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
30 changes: 30 additions & 0 deletions example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,36 @@ pub fn panic(_msg: &'static str) -> ! {
}
}

macro_rules! panic_const {
($($lang:ident = $message:expr,)+) => {
#[cfg(not(bootstrap))]
pub mod panic_const {
use super::*;

$(
#[track_caller]
#[lang = stringify!($lang)]
pub fn $lang() -> ! {
panic($message);
}
)+
}
}
}

panic_const! {
panic_const_add_overflow = "attempt to add with overflow",
panic_const_sub_overflow = "attempt to subtract with overflow",
panic_const_mul_overflow = "attempt to multiply with overflow",
panic_const_div_overflow = "attempt to divide with overflow",
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
panic_const_neg_overflow = "attempt to negate with overflow",
panic_const_shr_overflow = "attempt to shift right with overflow",
panic_const_shl_overflow = "attempt to shift left with overflow",
panic_const_div_by_zero = "attempt to divide by zero",
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
}

#[lang = "panic_bounds_check"]
#[track_caller]
fn panic_bounds_check(index: usize, len: usize) -> ! {
Expand Down
24 changes: 8 additions & 16 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,14 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
);
}
_ => {
let msg_str = msg.description();
codegen_panic(fx, msg_str, source_info);
let location = fx.get_caller_location(source_info).load_scalar(fx);

codegen_panic_inner(
fx,
msg.panic_function(),
&[location],
Some(source_info.span),
);
}
}
}
Expand Down Expand Up @@ -957,20 +963,6 @@ pub(crate) fn codegen_operand<'tcx>(
}
}

pub(crate) fn codegen_panic<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
msg_str: &str,
source_info: mir::SourceInfo,
) {
let location = fx.get_caller_location(source_info).load_scalar(fx);

let msg_ptr = fx.anonymous_str(msg_str);
let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
let args = [msg_ptr, msg_len, location];

codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, Some(source_info.span));
}

pub(crate) fn codegen_panic_nounwind<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
msg_str: &str,
Expand Down
15 changes: 2 additions & 13 deletions src/debuginfo/line_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ impl DebugContext {
match &source_file.name {
FileName::Real(path) => {
let (dir_path, file_name) =
split_path_dir_and_file(if self.should_remap_filepaths {
path.remapped_path_if_available()
} else {
path.local_path_if_available()
});
split_path_dir_and_file(path.to_path(self.filename_display_preference));
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
let file_name = osstr_as_utf8_bytes(file_name);

Expand All @@ -115,14 +111,7 @@ impl DebugContext {
filename => {
let dir_id = line_program.default_directory();
let dummy_file_name = LineString::new(
filename
.display(if self.should_remap_filepaths {
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
})
.to_string()
.into_bytes(),
filename.display(self.filename_display_preference).to_string().into_bytes(),
line_program.encoding(),
line_strings,
);
Expand Down
24 changes: 10 additions & 14 deletions src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) struct DebugContext {
namespace_map: DefIdMap<UnitEntryId>,
array_size_type: UnitEntryId,

should_remap_filepaths: bool,
filename_display_preference: FileNameDisplayPreference,
}

pub(crate) struct FunctionDebugContext {
Expand Down Expand Up @@ -84,22 +84,18 @@ impl DebugContext {

let mut dwarf = DwarfUnit::new(encoding);

let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
use rustc_session::config::RemapPathScopeComponents;

let filename_display_preference =
tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);

let producer = producer(tcx.sess);
let comp_dir = tcx
.sess
.opts
.working_dir
.to_string_lossy(if should_remap_filepaths {
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
})
.into_owned();
let comp_dir =
tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string();

let (name, file_info) = match tcx.sess.local_crate_source_file() {
Some(path) => {
let name = path.to_string_lossy().into_owned();
let name = path.to_string_lossy(filename_display_preference).to_string();
(name, None)
}
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
Expand Down Expand Up @@ -156,7 +152,7 @@ impl DebugContext {
stack_pointer_register,
namespace_map: DefIdMap::default(),
array_size_type,
should_remap_filepaths,
filename_display_preference,
}
}

Expand Down

0 comments on commit 362caf7

Please sign in to comment.