Skip to content

Commit 86e55b1

Browse files
committed
Always use consteval to codegen caller_location.
1 parent 017877c commit 86e55b1

File tree

11 files changed

+66
-63
lines changed

11 files changed

+66
-63
lines changed

src/librustc/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ rustc_queries! {
466466
no_force
467467
desc { "extract field of const" }
468468
}
469+
470+
query const_caller_location(key: (syntax_pos::Symbol, u32, u32)) -> &'tcx ty::Const<'tcx> {
471+
eval_always
472+
no_force
473+
desc { "get a &core::panic::Location referring to a span" }
474+
}
469475
}
470476

471477
TypeChecking {

src/librustc/ty/query/keys.rs

+10
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,13 @@ impl<'tcx, T> Key for Canonical<'tcx, T> {
208208
DUMMY_SP
209209
}
210210
}
211+
212+
impl Key for (Symbol, u32, u32) {
213+
fn query_crate(&self) -> CrateNum {
214+
LOCAL_CRATE
215+
}
216+
217+
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
218+
DUMMY_SP
219+
}
220+
}

src/librustc_codegen_llvm/builder.rs

-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope};
22
use crate::llvm::{self, False, BasicBlock};
33
use crate::common::Funclet;
44
use crate::context::CodegenCx;
5-
use crate::syntax_pos::Pos;
65
use crate::type_::Type;
76
use crate::type_of::LayoutLlvmExt;
87
use crate::value::Value;
@@ -24,7 +23,6 @@ use std::ffi::CStr;
2423
use std::ops::{Deref, Range};
2524
use std::ptr;
2625
use std::iter::TrustedLen;
27-
use syntax::symbol::Symbol;
2826

2927
// All Builders must have an llfn associated with them
3028
#[must_use]
@@ -1068,20 +1066,6 @@ impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
10681066
// Forward to the `get_static` method of `CodegenCx`
10691067
self.cx().get_static(def_id)
10701068
}
1071-
1072-
fn static_panic_location(&mut self, loc: &syntax::source_map::Loc) -> Self::Value {
1073-
let filename = Symbol::intern(&loc.file.name.to_string());
1074-
let filename = self.const_str(filename);
1075-
let line = self.const_u32(loc.line as u32);
1076-
let col = self.const_u32(loc.col.to_usize() as u32 + 1);
1077-
let struct_ = self.const_struct(&[filename.0, filename.1, line, col], false);
1078-
1079-
let align = self.tcx.data_layout.aggregate_align.abi
1080-
.max(self.tcx.data_layout.i32_align.abi)
1081-
.max(self.tcx.data_layout.pointer_align.abi);
1082-
// FIXME(eddyb) move this into miri, it can be correct if e.g. field order changes
1083-
self.static_addr_of(struct_, align, Some("panic_loc"))
1084-
}
10851069
}
10861070

10871071
impl Builder<'a, 'll, 'tcx> {

src/librustc_codegen_ssa/mir/block.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::traits::*;
1515

1616
use std::borrow::Cow;
1717

18-
use syntax::symbol::Symbol;
18+
use syntax::{source_map::Span, symbol::Symbol};
1919

2020
use super::{FunctionCx, LocalRef};
2121
use super::place::PlaceRef;
@@ -420,8 +420,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
420420
self.set_debug_loc(&mut bx, terminator.source_info);
421421

422422
// Get the location information.
423-
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
424-
let location = bx.static_panic_location(&loc);
423+
let location = self.get_caller_location(&mut bx, span).immediate();
425424

426425
// Put together the arguments to the panic entry point.
427426
let (lang_item, args) = match msg {
@@ -534,11 +533,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
534533
let ty = instance.unwrap().substs.type_at(0);
535534
let layout = bx.layout_of(ty);
536535
if layout.abi.is_uninhabited() {
537-
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
538-
539536
let msg_str = format!("Attempted to instantiate uninhabited type {}", ty);
540537
let msg = bx.const_str(Symbol::intern(&msg_str));
541-
let location = bx.static_panic_location(&loc);
538+
let location = self.get_caller_location(&mut bx, span).immediate();
542539

543540
// Obtain the panic entry point.
544541
let def_id =
@@ -584,13 +581,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
584581

585582
if intrinsic == Some("caller_location") {
586583
if let Some((_, target)) = destination.as_ref() {
587-
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
588-
let location = bx.static_panic_location(&loc);
584+
let location = self.get_caller_location(&mut bx, span);
589585

590586
if let ReturnDest::IndirectOperand(tmp, _) = ret_dest {
591-
Immediate(location).store(&mut bx, tmp);
587+
location.val.store(&mut bx, tmp);
592588
}
593-
self.store_return(&mut bx, ret_dest, &fn_ty.ret, location);
589+
self.store_return(&mut bx, ret_dest, &fn_ty.ret, location.immediate());
594590

595591
helper.maybe_sideeffect(self.mir, &mut bx, &[*target]);
596592
helper.funclet_br(self, &mut bx, *target);
@@ -994,6 +990,20 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
994990
}
995991
}
996992

993+
fn get_caller_location(
994+
&mut self,
995+
bx: &mut Bx,
996+
span: Span,
997+
) -> OperandRef<'tcx, Bx::Value> {
998+
let caller = bx.tcx().sess.source_map().lookup_char_pos(span.lo());
999+
let const_loc = bx.tcx().const_caller_location((
1000+
Symbol::intern(&caller.file.name.to_string()),
1001+
caller.line as u32,
1002+
caller.col_display as u32 + 1,
1003+
));
1004+
OperandRef::from_const(bx, const_loc)
1005+
}
1006+
9971007
fn get_personality_slot(
9981008
&mut self,
9991009
bx: &mut Bx

src/librustc_codegen_ssa/traits/statics.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::BackendTypes;
2-
use syntax::source_map::Loc;
32
use rustc::hir::def_id::DefId;
43
use rustc::ty::layout::Align;
54

@@ -10,5 +9,4 @@ pub trait StaticMethods: BackendTypes {
109

1110
pub trait StaticBuilderMethods: BackendTypes {
1211
fn get_static(&mut self, def_id: DefId) -> Self::Value;
13-
fn static_panic_location(&mut self, loc: &Loc) -> Self::Value;
1412
}

src/librustc_mir/const_eval.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::traits::Reveal;
1818
use rustc_data_structures::fx::FxHashMap;
1919
use crate::interpret::eval_nullary_intrinsic;
2020

21-
use syntax::source_map::{Span, DUMMY_SP};
21+
use syntax::{source_map::{Span, DUMMY_SP}, symbol::Symbol};
2222

2323
use crate::interpret::{self,
2424
PlaceTy, MPlaceTy, OpTy, ImmTy, Immediate, Scalar, Pointer,
@@ -159,11 +159,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
159159
ecx.run()?;
160160

161161
// Intern the result
162-
intern_const_alloc_recursive(
163-
ecx,
164-
cid.instance.def_id(),
165-
ret,
166-
)?;
162+
intern_const_alloc_recursive(ecx, tcx.static_mutability(cid.instance.def_id()), ret)?;
167163

168164
debug!("eval_body_using_ecx done: {:?}", *ret);
169165
Ok(ret)

src/librustc_mir/interpret/intern.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use rustc::ty::{Ty, self};
77
use rustc::mir::interpret::{InterpResult, ErrorHandled};
88
use rustc::hir;
9-
use rustc::hir::def_id::DefId;
109
use super::validity::RefTracking;
1110
use rustc_data_structures::fx::FxHashSet;
1211

@@ -270,12 +269,12 @@ for
270269

271270
pub fn intern_const_alloc_recursive(
272271
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
273-
def_id: DefId,
272+
// The `mutability` of the place, ignoring the type.
273+
place_mut: Option<hir::Mutability>,
274274
ret: MPlaceTy<'tcx>,
275275
) -> InterpResult<'tcx> {
276276
let tcx = ecx.tcx;
277-
// this `mutability` is the mutability of the place, ignoring the type
278-
let (base_mutability, base_intern_mode) = match tcx.static_mutability(def_id) {
277+
let (base_mutability, base_intern_mode) = match place_mut {
279278
Some(hir::Mutability::MutImmutable) => (Mutability::Immutable, InternMode::Static),
280279
// `static mut` doesn't care about interior mutability, it's mutable anyway
281280
Some(hir::Mutability::MutMutable) => (Mutability::Mutable, InternMode::Static),

src/librustc_mir/interpret/intrinsics.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9898
let intrinsic_name = &self.tcx.item_name(instance.def_id()).as_str()[..];
9999
match intrinsic_name {
100100
"caller_location" => {
101-
self.write_caller_location(span, dest)?;
101+
let caller = self.tcx.sess.source_map().lookup_char_pos(span.lo());
102+
let location = self.alloc_caller_location(
103+
Symbol::intern(&caller.file.name.to_string()),
104+
caller.line as u32,
105+
caller.col_display as u32 + 1,
106+
)?;
107+
self.write_scalar(location.ptr, dest)?;
102108
}
103109

104110
"min_align_of" |

src/librustc_mir/interpret/intrinsics/caller_location.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@ use rustc::middle::lang_items::PanicLocationLangItem;
22
use rustc::mir::interpret::{Pointer, PointerArithmetic, Scalar};
33
use rustc::ty::subst::Subst;
44
use rustc_target::abi::{LayoutOf, Size};
5-
use syntax_pos::Span;
5+
use syntax_pos::Symbol;
66

7-
use crate::interpret::{
8-
MemoryKind,
9-
intrinsics::{InterpCx, InterpResult, Machine, PlaceTy},
10-
};
7+
use crate::interpret::{MemoryKind, MPlaceTy, intrinsics::{InterpCx, InterpResult, Machine}};
118

129
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
13-
pub fn write_caller_location(
10+
pub fn alloc_caller_location(
1411
&mut self,
15-
span: Span,
16-
dest: PlaceTy<'tcx, M::PointerTag>,
17-
) -> InterpResult<'tcx> {
18-
let caller = self.tcx.sess.source_map().lookup_char_pos(span.lo());
19-
let filename = caller.file.name.to_string();
20-
let line = Scalar::from_u32(caller.line as u32);
21-
let col = Scalar::from_u32(caller.col_display as u32 + 1);
12+
filename: Symbol,
13+
line: u32,
14+
col: u32,
15+
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
16+
let line = Scalar::from_u32(line);
17+
let col = Scalar::from_u32(col);
2218

2319
let ptr_size = self.pointer_size();
2420
let u32_size = Size::from_bits(32);
@@ -27,10 +23,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2723
.subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_static.into()].iter()));
2824
let loc_layout = self.layout_of(loc_ty)?;
2925

30-
let file_alloc = self.tcx.allocate_bytes(filename.as_bytes());
26+
let file_alloc = self.tcx.allocate_bytes(filename.as_str().as_bytes());
3127
let file_ptr = Pointer::new(file_alloc, Size::ZERO);
3228
let file = Scalar::Ptr(self.tag_static_base_pointer(file_ptr));
33-
let file_len = Scalar::from_uint(filename.len() as u128, ptr_size);
29+
let file_len = Scalar::from_uint(filename.as_str().len() as u128, ptr_size);
3430

3531
let location = self.allocate(loc_layout, MemoryKind::Stack);
3632

@@ -48,7 +44,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
4844
alloc.write_scalar(layout, line_out, line.into(), u32_size)?;
4945
alloc.write_scalar(layout, col_out, col.into(), u32_size)?;
5046

51-
self.write_scalar(location.ptr, dest)?;
52-
Ok(())
47+
Ok(location)
5348
}
5449
}

src/librustc_mir/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub fn provide(providers: &mut Providers<'_>) {
5858
providers.const_eval = const_eval::const_eval_provider;
5959
providers.const_eval_raw = const_eval::const_eval_raw_provider;
6060
providers.check_match = hair::pattern::check_match;
61+
providers.const_caller_location = const_eval::const_caller_location;
6162
providers.const_field = |tcx, param_env_and_value| {
6263
let (param_env, (value, field)) = param_env_and_value.into_parts();
6364
const_eval::const_field(tcx, param_env, None, field, value)

src/librustc_typeck/check/intrinsic.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Type-checking for the rust-intrinsic and platform-intrinsic
22
//! intrinsics that the compiler exposes.
33
4-
use rustc::hir::{self, Mutability};
54
use rustc::middle::lang_items::PanicLocationLangItem;
65
use rustc::traits::{ObligationCause, ObligationCauseCode};
76
use rustc::ty::{self, TyCtxt, Ty};
@@ -11,6 +10,8 @@ use crate::require_same_types;
1110
use rustc_target::spec::abi::Abi;
1211
use syntax::symbol::Symbol;
1312

13+
use rustc::hir;
14+
1415
use std::iter;
1516

1617
fn equate_intrinsic_type<'tcx>(
@@ -146,13 +147,10 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
146147
"caller_location" => (
147148
0,
148149
vec![],
149-
tcx.mk_ref(
150+
tcx.mk_imm_ref(
150151
tcx.lifetimes.re_static,
151-
ty::TypeAndMut {
152-
mutbl: Mutability::MutImmutable,
153-
ty: tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None))
154-
.subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())),
155-
},
152+
tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None))
153+
.subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())),
156154
),
157155
),
158156
"panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),

0 commit comments

Comments
 (0)