From 9ab065dcda070680e18ca0da4366ad45e1665f0f Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Mon, 11 Nov 2019 08:45:52 -0800 Subject: Implement #[track_caller] in const. --- src/librustc/ty/instance.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/librustc') diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index 801dfa81ef1..366951bc9f4 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -116,6 +116,10 @@ impl<'tcx> InstanceDef<'tcx> { } tcx.codegen_fn_attrs(self.def_id()).requests_inline() } + + pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool { + tcx.codegen_fn_attrs(self.def_id()).flags.contains(CodegenFnAttrFlags::TRACK_CALLER) + } } impl<'tcx> fmt::Display for Instance<'tcx> { @@ -255,11 +259,8 @@ impl<'tcx> Instance<'tcx> { ) -> Option> { debug!("resolve(def_id={:?}, substs={:?})", def_id, substs); Instance::resolve(tcx, param_env, def_id, substs).map(|mut resolved| { - let has_track_caller = |def| tcx.codegen_fn_attrs(def).flags - .contains(CodegenFnAttrFlags::TRACK_CALLER); - match resolved.def { - InstanceDef::Item(def_id) if has_track_caller(def_id) => { + InstanceDef::Item(def_id) if resolved.def.requires_caller_location(tcx) => { debug!(" => fn pointer created for function with #[track_caller]"); resolved.def = InstanceDef::ReifyShim(def_id); } -- cgit 1.4.1-3-g733a5 From 4773ded15ba23518cd3ba23ab30a2eaf69d9c20f Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Mon, 28 Oct 2019 20:02:41 -0700 Subject: Generate &core::panic::Location type in a single place. --- src/librustc/ty/context.rs | 10 ++++++++++ src/librustc_mir/const_eval.rs | 7 +------ src/librustc_typeck/check/intrinsic.rs | 11 +---------- 3 files changed, 12 insertions(+), 16 deletions(-) (limited to 'src/librustc') diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 6a0002cd80f..bf6c10dd3c9 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -8,6 +8,7 @@ use crate::session::Session; use crate::session::config::{BorrowckMode, OutputFilenames}; use crate::session::config::CrateType; use crate::middle; +use crate::middle::lang_items::PanicLocationLangItem; use crate::hir::{self, TraitCandidate, HirId, ItemKind, ItemLocalId, Node}; use crate::hir::def::{Res, DefKind, Export}; use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE}; @@ -1588,6 +1589,15 @@ impl<'tcx> TyCtxt<'tcx> { pub fn has_strict_asm_symbol_naming(&self) -> bool { self.sess.target.target.arch.contains("nvptx") } + + /// Returns `&'static core::panic::Location<'static>`. + pub fn caller_location_ty(&self) -> Ty<'tcx> { + self.mk_imm_ref( + self.lifetimes.re_static, + self.type_of(self.require_lang_item(PanicLocationLangItem, None)) + .subst(*self, self.mk_substs([self.lifetimes.re_static.into()].iter())), + ) + } } impl<'tcx> GlobalCtxt<'tcx> { diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 968a8a71ba0..b6a2cc0a9ff 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -9,7 +9,6 @@ use std::convert::TryInto; use rustc::hir::def::DefKind; use rustc::hir::def_id::DefId; -use rustc::middle::lang_items::PanicLocationLangItem; use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef}; use rustc::mir; use rustc::ty::{self, Ty, TyCtxt, subst::Subst}; @@ -559,11 +558,7 @@ pub fn const_caller_location<'tcx>( trace!("const_caller_location: {}:{}:{}", file, line, col); let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all()); - let loc_ty = tcx.mk_imm_ref( - tcx.lifetimes.re_static, - tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None)) - .subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())), - ); + let loc_ty = tcx.caller_location_ty(); let loc_place = ecx.alloc_caller_location(file, line, col); intern_const_alloc_recursive(&mut ecx, None, loc_place).unwrap(); let loc_const = ty::Const { diff --git a/src/librustc_typeck/check/intrinsic.rs b/src/librustc_typeck/check/intrinsic.rs index 9f034e65b6e..2e19ee653c3 100644 --- a/src/librustc_typeck/check/intrinsic.rs +++ b/src/librustc_typeck/check/intrinsic.rs @@ -1,7 +1,6 @@ //! Type-checking for the rust-intrinsic and platform-intrinsic //! intrinsics that the compiler exposes. -use rustc::middle::lang_items::PanicLocationLangItem; use rustc::traits::{ObligationCause, ObligationCauseCode}; use rustc::ty::{self, TyCtxt, Ty}; use rustc::ty::subst::Subst; @@ -148,15 +147,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) { ], tcx.types.usize) } "rustc_peek" => (1, vec![param(0)], param(0)), - "caller_location" => ( - 0, - vec![], - tcx.mk_imm_ref( - tcx.lifetimes.re_static, - tcx.type_of(tcx.require_lang_item(PanicLocationLangItem, None)) - .subst(tcx, tcx.mk_substs([tcx.lifetimes.re_static.into()].iter())), - ), - ), + "caller_location" => (0, vec![], tcx.caller_location_ty()), "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()), "init" => (1, Vec::new(), param(0)), "uninit" => (1, Vec::new(), param(0)), -- cgit 1.4.1-3-g733a5 From fa6bb398553200c0bc6ddba488eec43d5a497aef Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Tue, 5 Nov 2019 21:13:17 -0800 Subject: Add caller_location paramter to FnAbi::new_internal. We pass it in `of_instance` when the instance requires caller location. --- src/librustc/ty/layout.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/librustc') diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 7f93e8c91e9..6c921134ab8 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -2434,6 +2434,7 @@ where cx: &C, sig: ty::PolyFnSig<'tcx>, extra_args: &[Ty<'tcx>], + caller_location: Option>, mk_arg_type: impl Fn(Ty<'tcx>, Option) -> ArgAbi<'tcx, Ty<'tcx>>, ) -> Self; fn adjust_for_abi(&mut self, cx: &C, abi: SpecAbi); @@ -2448,13 +2449,19 @@ where + HasParamEnv<'tcx>, { fn of_fn_ptr(cx: &C, sig: ty::PolyFnSig<'tcx>, extra_args: &[Ty<'tcx>]) -> Self { - call::FnAbi::new_internal(cx, sig, extra_args, |ty, _| ArgAbi::new(cx.layout_of(ty))) + call::FnAbi::new_internal(cx, sig, extra_args, None, |ty, _| ArgAbi::new(cx.layout_of(ty))) } fn of_instance(cx: &C, instance: ty::Instance<'tcx>, extra_args: &[Ty<'tcx>]) -> Self { let sig = instance.fn_sig_for_fn_abi(cx.tcx()); - call::FnAbi::new_internal(cx, sig, extra_args, |ty, arg_idx| { + let caller_location = if instance.def.requires_caller_location(cx.tcx()) { + Some(cx.tcx().caller_location_ty()) + } else { + None + }; + + call::FnAbi::new_internal(cx, sig, extra_args, caller_location, |ty, arg_idx| { let mut layout = cx.layout_of(ty); // Don't pass the vtable, it's not an argument of the virtual fn. // Instead, pass just the data pointer, but give it the type `*const/mut dyn Trait` @@ -2512,6 +2519,7 @@ where cx: &C, sig: ty::PolyFnSig<'tcx>, extra_args: &[Ty<'tcx>], + caller_location: Option>, mk_arg_type: impl Fn(Ty<'tcx>, Option) -> ArgAbi<'tcx, Ty<'tcx>>, ) -> Self { debug!("FnAbi::new_internal({:?}, {:?})", sig, extra_args); @@ -2684,6 +2692,7 @@ where .iter() .cloned() .chain(extra_args) + .chain(caller_location) .enumerate() .map(|(i, ty)| arg_of(ty, Some(i))) .collect(), -- cgit 1.4.1-3-g733a5