Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Add MIR argument for #[track_caller] (RFC 2091 3/N) #65258

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcore/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl fmt::Display for PanicInfo<'_> {
///
/// panic!("Normal panic");
/// ```
#[cfg_attr(not(bootstrap), lang = "panic_location")]
#[derive(Debug)]
#[stable(feature = "panic_hooks", since = "1.10.0")]
pub struct Location<'a> {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ language_item_table! {
PanicFnLangItem, "panic", panic_fn, Target::Fn;
PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn, Target::Fn;
PanicInfoLangItem, "panic_info", panic_info, Target::Struct;
PanicLocationLangItem, "panic_location", panic_location, Target::Struct;
PanicImplLangItem, "panic_impl", panic_impl, Target::Fn;
// Libstd panic entry point. Necessary for const eval to be able to catch it
BeginPanicFnLangItem, "begin_panic", begin_panic_fn, Target::Fn;
Expand Down
22 changes: 22 additions & 0 deletions src/librustc/ty/instance.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::hir::CodegenFnAttrFlags;
use crate::hir::Mutability;
use crate::hir::Unsafety;
use crate::hir::def::Namespace;
use crate::hir::def_id::DefId;
use crate::ty::{self, Ty, PolyFnSig, TypeFoldable, SubstsRef, TyCtxt};
use crate::ty::print::{FmtPrinter, Printer};
use crate::traits;
use crate::middle::lang_items::DropInPlaceFnLangItem;
use rustc::middle::lang_items::PanicLocationLangItem;
use rustc_target::spec::abi::Abi;
use rustc_macros::HashStable;

Expand Down Expand Up @@ -121,9 +123,29 @@ impl<'tcx> Instance<'tcx> {
fn_sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
fn_sig
});
} else if let InstanceDef::ReifyShim(..) = self.def {
// Modify fn(...) to fn(_location: &core::panic::Location, ...)
fn_sig = fn_sig.map_bound(|mut fn_sig| {
let mut inputs_and_output = fn_sig.inputs_and_output.to_vec();
inputs_and_output.insert(0, Self::track_caller_ty(tcx));
fn_sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
fn_sig
});
}
fn_sig
}

/// Returns `&'static core::panic::Location`, for args of functions with #[track_caller].
pub fn track_caller_ty(tcx: TyCtxt<'_>) -> Ty<'_> {
let panic_loc_item = tcx.require_lang_item(PanicLocationLangItem, None);
tcx.mk_ref(
tcx.mk_region(ty::RegionKind::ReStatic),
ty::TypeAndMut {
mutbl: Mutability::MutImmutable,
ty: tcx.type_of(panic_loc_item),
},
)
}
}

impl<'tcx> InstanceDef<'tcx> {
Expand Down
15 changes: 14 additions & 1 deletion src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ pub fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
ArgInfo(ty, opt_ty_info, Some(&arg), self_arg)
});

let arguments = implicit_argument.into_iter().chain(explicit_arguments);
let arguments = implicit_argument.into_iter()
.chain(track_caller_argument(tcx, def_id))
.chain(explicit_arguments);

let (yield_ty, return_ty) = if body.generator_kind.is_some() {
let gen_sig = match ty.kind {
Expand Down Expand Up @@ -185,6 +187,17 @@ pub fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
})
}

/// Returns the appropriate `ArgInfo` if the provided function has #[track_caller].
fn track_caller_argument(tcx: TyCtxt<'_>, fn_def_id: DefId) -> Option<ArgInfo<'_>> {
let codegen_flags = tcx.codegen_fn_attrs(fn_def_id).flags;
let has_track_caller = codegen_flags.contains(hir::CodegenFnAttrFlags::TRACK_CALLER);
if has_track_caller {
Some(ArgInfo(ty::Instance::track_caller_ty(tcx), None, None, None))
} else {
None
}
}

///////////////////////////////////////////////////////////////////////////
// BuildMir -- walks a crate, looking for fn items and methods to build MIR from

Expand Down
9 changes: 0 additions & 9 deletions src/test/ui/rfc-2091-track-caller/pass.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/rfc-2091-track-caller/pass.stderr

This file was deleted.

12 changes: 12 additions & 0 deletions src/test/ui/rfc-2091-track-caller/should-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// failure-status: 101
// normalize-stderr-test "note: rustc 1.* running on .*" -> "note: rustc VERSION running on TARGET"
// normalize-stderr-test "note: compiler flags: .*" -> "note: compiler flags: FLAGS"

#![feature(track_caller)] //~ WARN the feature `track_caller` is incomplete

#[track_caller]
fn f() {}

fn main() {
f();
}
21 changes: 21 additions & 0 deletions src/test/ui/rfc-2091-track-caller/should-pass.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
warning: the feature `track_caller` is incomplete and may cause the compiler to crash
--> $DIR/should-pass.rs:5:12
|
LL | #![feature(track_caller)]
| ^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', $SRC_DIR/libcore/slice/mod.rs:LL:COL
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc VERSION running on TARGET

note: compiler flags: FLAGS