Skip to content

Commit

Permalink
[RFC 2091] Add #[track_caller] attribute.
Browse files Browse the repository at this point in the history
- The attribute is behind a feature gate.
- Error if both #[naked] and #[track_caller] are applied to the same function.
- Error if #[track_caller] is applied to a non-function item.
- Error if ABI is not "rust"
- Error if #[track_caller] is applied to a trait function.

Error codes and descriptions are pending.
  • Loading branch information
ayosec authored and anp committed Oct 3, 2019
1 parent 2daa404 commit a8b2fe0
Show file tree
Hide file tree
Showing 22 changed files with 199 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/doc/unstable-book/src/language-features/track-caller.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `track_caller`

The tracking issue for this feature is: [#47809](https://github.com/rust-lang/rust/issues/47809).

------------------------
10 changes: 10 additions & 0 deletions src/librustc/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,16 @@ each method; it is not possible to annotate the entire impl with an `#[inline]`
attribute.
"##,

E0900: r##"
TODO: change error number
TODO: track_caller: invalid syntax
"##,

E0901: r##"
TODO: change error number
TODO: track_caller: no naked functions
"##,

E0522: r##"
The lang attribute is intended for marking special items that are built-in to
Rust itself. This includes special traits (like `Copy` and `Sized`) that affect
Expand Down
34 changes: 34 additions & 0 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl CheckAttrVisitor<'tcx> {
/// Checks any attribute.
fn check_attributes(&self, item: &hir::Item, target: Target) {
let mut is_valid = true;
let mut track_caller_span = None;
for attr in &item.attrs {
is_valid &= if attr.check_name(sym::inline) {
self.check_inline(attr, &item.span, target)
Expand All @@ -103,6 +104,9 @@ impl CheckAttrVisitor<'tcx> {
self.check_marker(attr, item, target)
} else if attr.check_name(sym::target_feature) {
self.check_target_feature(attr, item, target)
} else if attr.check_name(sym::track_caller) {
track_caller_span = Some(attr.span);
self.check_track_caller(attr, &item, target)
} else {
true
};
Expand All @@ -118,6 +122,19 @@ impl CheckAttrVisitor<'tcx> {

self.check_repr(item, target);
self.check_used(item, target);

// Checks if `#[track_caller]` and `#[naked]` are both used.
if let Some(span) = track_caller_span {
if item.attrs.iter().any(|attr| attr.check_name(sym::naked)) {
struct_span_err!(
self.tcx.sess,
span,
E0901,
"cannot use `#[track_caller]` with `#[naked]`",
)
.emit();
}
}
}

/// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
Expand All @@ -135,6 +152,23 @@ impl CheckAttrVisitor<'tcx> {
}
}

/// Checks if a `#[target_feature]` can be applied.
fn check_track_caller(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) -> bool {
if target != Target::Fn {
struct_span_err!(
self.tcx.sess,
attr.span,
E0900,
"attribute should be applied to function"
)
.span_label(item.span, "not a function")
.emit();
false
} else {
true
}
}

/// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
fn check_non_exhaustive(
&self,
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2687,7 +2687,9 @@ bitflags! {
const USED = 1 << 9;
/// #[ffi_returns_twice], indicates that an extern function can return
/// multiple times
const FFI_RETURNS_TWICE = 1 << 10;
const FFI_RETURNS_TWICE = 1 << 10;
/// #[track_caller]: allow access to the caller location
const TRACK_CALLER = 1 << 11;
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/librustc_typeck/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: DefId) {
_ => None
};
check_associated_item(tcx, trait_item.hir_id, trait_item.span, method_sig);

// Prohibits applying `#[track_caller]` to trait methods
for attr in &trait_item.attrs {
if attr.check_name(sym::track_caller) {
struct_span_err!(
tcx.sess,
attr.span,
E0903,
"`#[track_caller]` is not supported for trait items yet."
).emit();
}
}
}

pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: DefId) {
Expand Down
10 changes: 10 additions & 0 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,16 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
} else if attr.check_name(sym::thread_local) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
} else if attr.check_name(sym::track_caller) {
if tcx.fn_sig(id).abi() != abi::Abi::Rust {
struct_span_err!(
tcx.sess,
attr.span,
E0902,
"rust ABI is required to use `#[track_caller]`"
).emit();
}
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
} else if attr.check_name(sym::export_name) {
if let Some(s) = attr.value_str() {
if s.as_str().contains("\0") {
Expand Down
11 changes: 11 additions & 0 deletions src/librustc_typeck/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4907,6 +4907,17 @@ fn foo_recursive(n: usize) -> Pin<Box<dyn Future<Output = ()>>> {
The `Box<...>` ensures that the result is of known size,
and the pin is required to keep it in the same place in memory.
"##,

E0902: r##"
TODO: change error number
TODO: track_caller: require Rust ABI to use track_caller
"##,

E0903: r##"
TODO: change error number
TODO: track_caller: can't apply in traits
"##,

;
// E0035, merged into E0087/E0089
// E0036, merged into E0087/E0089
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/feature_gate/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ declare_features! (
/// Allows the use of or-patterns (e.g., `0 | 1`).
(active, or_patterns, "1.38.0", Some(54883), None),

/// Enable accurate caller location reporting during panic (RFC 2091).
(active, track_caller, "1.37.0", Some(47809), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/libsyntax/feature_gate/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
cfg_fn!(no_debug)
)
),
gated!(
track_caller, Whitelisted, template!(Word),
"the `#[track_caller]` attribute is an experimental feature",
),
gated!(
// Used in resolve:
prelude_import, Whitelisted, template!(Word),
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ symbols! {
tool_attributes,
tool_lints,
trace_macros,
track_caller,
trait_alias,
transmute,
transparent,
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/feature-gates/feature-gate-track_caller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#[track_caller]
fn f() {}
//~^^ ERROR the `#[track_caller]` attribute is an experimental feature

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/feature-gates/feature-gate-track_caller.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: the `#[track_caller]` attribute is an experimental feature
--> $DIR/feature-gate-track_caller.rs:2:1
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/47809
= help: add `#![feature(track_caller)]` to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
7 changes: 7 additions & 0 deletions src/test/ui/rfc-2091-track-caller/error-odd-syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(track_caller)]

#[track_caller(1)]
fn f() {}
//~^^ ERROR malformed `track_caller` attribute input

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/rfc-2091-track-caller/error-odd-syntax.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: malformed `track_caller` attribute input
--> $DIR/error-odd-syntax.rs:3:1
|
LL | #[track_caller(1)]
| ^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[track_caller]`

error: aborting due to previous error

7 changes: 7 additions & 0 deletions src/test/ui/rfc-2091-track-caller/error-with-invalid-abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(track_caller)]

#[track_caller]
extern "C" fn f() {}
//~^^ ERROR rust ABI is required to use `#[track_caller]`

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0902]: rust ABI is required to use `#[track_caller]`
--> $DIR/error-with-invalid-abi.rs:3:1
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0902`.
8 changes: 8 additions & 0 deletions src/test/ui/rfc-2091-track-caller/error-with-naked.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(naked_functions, track_caller)]

#[track_caller]
#[naked]
fn f() {}
//~^^^ ERROR cannot use `#[track_caller]` with `#[naked]`

fn main() {}
9 changes: 9 additions & 0 deletions src/test/ui/rfc-2091-track-caller/error-with-naked.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0901]: cannot use `#[track_caller]` with `#[naked]`
--> $DIR/error-with-naked.rs:3:1
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0901`.
13 changes: 13 additions & 0 deletions src/test/ui/rfc-2091-track-caller/error-with-trait-fns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(track_caller)]

trait Trait {
#[track_caller]
fn unwrap(&self);
//~^^ ERROR: `#[track_caller]` is not supported for trait items yet.
}

impl Trait for u64 {
fn unwrap(&self) {}
}

fn main() {}
9 changes: 9 additions & 0 deletions src/test/ui/rfc-2091-track-caller/error-with-trait-fns.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0903]: `#[track_caller]` is not supported for trait items yet.
--> $DIR/error-with-trait-fns.rs:4:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0903`.
7 changes: 7 additions & 0 deletions src/test/ui/rfc-2091-track-caller/only-for-fns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(track_caller)]

#[track_caller]
struct S;
//~^^ ERROR attribute should be applied to function

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/rfc-2091-track-caller/only-for-fns.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0900]: attribute should be applied to function
--> $DIR/only-for-fns.rs:3:1
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^
LL | struct S;
| --------- not a function

error: aborting due to previous error

For more information about this error, try `rustc --explain E0900`.

0 comments on commit a8b2fe0

Please sign in to comment.