Skip to content

Commit 77784de

Browse files
committed
Consolidate type generation for #[track_caller].
1 parent 9961fa6 commit 77784de

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

src/librustc/ty/instance.rs

+27-17
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,36 @@ impl<'tcx> Instance<'tcx> {
123123
});
124124
} else if let InstanceDef::ReifyShim(..) = self.def {
125125
// Modify fn(...) to fn(_location: &core::panic::Location, ...)
126-
#[cfg(not(bootstrap))]
127-
{
128-
use rustc::middle::lang_items::PanicLocationLangItem;
129-
let panic_loc_item = tcx.require_lang_item(PanicLocationLangItem, None);
130-
let panic_loc_ty = tcx.type_of(panic_loc_item);
131-
132-
fn_sig = fn_sig.map_bound(|mut fn_sig| {
133-
let mut inputs_and_output = fn_sig.inputs_and_output.to_vec();
134-
inputs_and_output.insert(0, panic_loc_ty);
135-
fn_sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
136-
fn_sig
137-
});
138-
}
139-
#[cfg(bootstrap)]
140-
{
141-
bug!("#[track_caller] isn't supported during bootstrap (yet).");
142-
}
126+
fn_sig = fn_sig.map_bound(|mut fn_sig| {
127+
let mut inputs_and_output = fn_sig.inputs_and_output.to_vec();
128+
inputs_and_output.insert(0, Self::track_caller_ty(tcx));
129+
fn_sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
130+
fn_sig
131+
});
143132
}
144133
fn_sig
145134
}
135+
136+
/// Returns `&'static core::panic::Location`, for args of functions with #[track_caller].
137+
pub fn track_caller_ty(_tcx: TyCtxt<'_>) -> Ty<'_> {
138+
#[cfg(bootstrap)]
139+
{ bug!("#[track_caller] isn't supported during bootstrap (yet)."); }
140+
141+
#[cfg(not(bootstrap))]
142+
{
143+
use crate::hir::Mutability;
144+
use rustc::middle::lang_items::PanicLocationLangItem;
145+
146+
let panic_loc_item = _tcx.require_lang_item(PanicLocationLangItem, None);
147+
_tcx.mk_ref(
148+
_tcx.mk_region(ty::RegionKind::ReStatic),
149+
ty::TypeAndMut {
150+
mutbl: Mutability::MutImmutable,
151+
ty: _tcx.type_of(panic_loc_item),
152+
},
153+
)
154+
}
155+
}
146156
}
147157

148158
impl<'tcx> InstanceDef<'tcx> {

src/librustc_mir/build/mod.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,6 @@ pub fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
8989
_ => None,
9090
};
9191

92-
// if this fn has #[track_caller], it will receive an implicit argument with a location
93-
let has_track_caller = tcx.codegen_fn_attrs(def_id).flags
94-
.contains(hir::CodegenFnAttrFlags::TRACK_CALLER);
95-
let location_argument = if has_track_caller {
96-
#[cfg(not(bootstrap))]
97-
{
98-
use rustc::middle::lang_items::PanicLocationLangItem;
99-
let panic_loc_item = tcx.require_lang_item(PanicLocationLangItem, None);
100-
let panic_loc_ty = tcx.type_of(panic_loc_item);
101-
Some(ArgInfo(panic_loc_ty, None, None, None))
102-
}
103-
#[cfg(bootstrap)]
104-
{
105-
bug!("#[track_caller] can't be used during a bootstrap build (yet).");
106-
}
107-
} else {
108-
None
109-
};
110-
11192
let safety = match fn_sig.unsafety {
11293
hir::Unsafety::Normal => Safety::Safe,
11394
hir::Unsafety::Unsafe => Safety::FnUnsafe,
@@ -161,7 +142,7 @@ pub fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
161142
});
162143

163144
let arguments = implicit_argument.into_iter()
164-
.chain(location_argument)
145+
.chain(track_caller_argument(tcx, def_id))
165146
.chain(explicit_arguments);
166147

167148
let (yield_ty, return_ty) = if body.generator_kind.is_some() {
@@ -206,6 +187,17 @@ pub fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
206187
})
207188
}
208189

190+
/// Returns the appropriate `ArgInfo` if the provided function has #[track_caller].
191+
fn track_caller_argument(tcx: TyCtxt<'_>, fn_def_id: DefId) -> Option<ArgInfo<'_>> {
192+
let codegen_flags = tcx.codegen_fn_attrs(fn_def_id).flags;
193+
let has_track_caller = codegen_flags.contains(hir::CodegenFnAttrFlags::TRACK_CALLER);
194+
if has_track_caller {
195+
Some(ArgInfo(ty::Instance::track_caller_ty(tcx), None, None, None))
196+
} else {
197+
None
198+
}
199+
}
200+
209201
///////////////////////////////////////////////////////////////////////////
210202
// BuildMir -- walks a crate, looking for fn items and methods to build MIR from
211203

0 commit comments

Comments
 (0)