rustc_const_eval/util/
caller_location.rs

1use rustc_hir::LangItem;
2use rustc_middle::ty::layout::LayoutOf;
3use rustc_middle::ty::{self, TyCtxt};
4use rustc_middle::{bug, mir};
5use rustc_span::Symbol;
6use tracing::trace;
7
8use crate::const_eval::{CanAccessMutGlobal, CompileTimeInterpCx, mk_eval_cx_to_read_const_val};
9use crate::interpret::*;
10
11/// Allocate a `const core::panic::Location` with the provided filename and line/column numbers.
12fn alloc_caller_location<'tcx>(
13    ecx: &mut CompileTimeInterpCx<'tcx>,
14    filename: Symbol,
15    line: u32,
16    col: u32,
17) -> MPlaceTy<'tcx> {
18    // Ensure that the filename itself does not contain nul bytes.
19    // This isn't possible via POSIX or Windows, but we should ensure no one
20    // ever does such a thing.
21    assert!(!filename.as_str().as_bytes().contains(&0));
22
23    let loc_details = ecx.tcx.sess.opts.unstable_opts.location_detail;
24    let file_wide_ptr = {
25        let filename = if loc_details.file { filename.as_str() } else { "<redacted>" };
26        let filename_with_nul = filename.to_owned() + "\0";
27        // This can fail if rustc runs out of memory right here. Trying to emit an error would be
28        // pointless, since that would require allocating more memory than these short strings.
29        let file_ptr = ecx.allocate_bytes_dedup(filename_with_nul.as_bytes()).unwrap();
30        Immediate::new_slice(file_ptr.into(), filename_with_nul.len().try_into().unwrap(), ecx)
31    };
32    let line = if loc_details.line { Scalar::from_u32(line) } else { Scalar::from_u32(0) };
33    let col = if loc_details.column { Scalar::from_u32(col) } else { Scalar::from_u32(0) };
34
35    // Allocate memory for `CallerLocation` struct.
36    let loc_ty = ecx
37        .tcx
38        .type_of(ecx.tcx.require_lang_item(LangItem::PanicLocation, ecx.tcx.span))
39        .instantiate(*ecx.tcx, ecx.tcx.mk_args(&[ecx.tcx.lifetimes.re_erased.into()]));
40    let loc_layout = ecx.layout_of(loc_ty).unwrap();
41    let location = ecx.allocate(loc_layout, MemoryKind::CallerLocation).unwrap();
42
43    // Initialize fields.
44    ecx.write_immediate(file_wide_ptr, &ecx.project_field(&location, 0).unwrap())
45        .expect("writing to memory we just allocated cannot fail");
46    ecx.write_scalar(line, &ecx.project_field(&location, 1).unwrap())
47        .expect("writing to memory we just allocated cannot fail");
48    ecx.write_scalar(col, &ecx.project_field(&location, 2).unwrap())
49        .expect("writing to memory we just allocated cannot fail");
50
51    location
52}
53
54pub(crate) fn const_caller_location_provider(
55    tcx: TyCtxt<'_>,
56    file: Symbol,
57    line: u32,
58    col: u32,
59) -> mir::ConstValue<'_> {
60    trace!("const_caller_location: {}:{}:{}", file, line, col);
61    let mut ecx = mk_eval_cx_to_read_const_val(
62        tcx,
63        rustc_span::DUMMY_SP, // FIXME: use a proper span here?
64        ty::TypingEnv::fully_monomorphized(),
65        CanAccessMutGlobal::No,
66    );
67
68    let loc_place = alloc_caller_location(&mut ecx, file, line, col);
69    if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
70        bug!("intern_const_alloc_recursive should not error in this case")
71    }
72    mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
73}