Allow #[track_caller] in traits.

The codegen implementation already works for this, so we're:

* propagating track_caller attr from trait def to impl
* relaxing errors
* adding tests

Approved in a recent lang team meeting:
https://github.com/rust-lang/lang-team/blob/master/minutes/2020-01-09.md
This commit is contained in:
Adam Perry 2020-02-17 15:36:36 -08:00 committed by Adam Perry
parent d1e81ef234
commit 97da6dae41
15 changed files with 215 additions and 107 deletions

View file

@ -0,0 +1,9 @@
#![feature(track_caller)]
#![allow(dead_code)]
extern "Rust" {
#[track_caller] //~ ERROR: `#[track_caller]` is not supported on foreign functions
fn bar();
}
fn main() {}

View file

@ -1,5 +1,5 @@
error[E0738]: `#[track_caller]` may not be used on trait methods
--> $DIR/error-with-trait-decl.rs:4:5
error[E0738]: `#[track_caller]` is not supported on foreign functions
--> $DIR/error-extern-fn.rs:5:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^

View file

@ -1,12 +0,0 @@
#![feature(track_caller)]
trait Trait {
#[track_caller] //~ ERROR: `#[track_caller]` may not be used on trait methods
fn unwrap(&self);
}
impl Trait for u64 {
fn unwrap(&self) {}
}
fn main() {}

View file

@ -1,8 +0,0 @@
#![feature(track_caller)]
trait Trait {
#[track_caller] //~ ERROR: `#[track_caller]` may not be used on trait methods
fn unwrap(&self) {}
}
fn main() {}

View file

@ -1,9 +0,0 @@
error[E0738]: `#[track_caller]` may not be used on trait methods
--> $DIR/error-with-trait-default-impl.rs:4:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0738`.

View file

@ -1,21 +0,0 @@
// check-fail
#![feature(track_caller)]
trait Trait {
fn unwrap(&self);
}
impl Trait for u64 {
#[track_caller] //~ ERROR: `#[track_caller]` may not be used on trait methods
fn unwrap(&self) {}
}
struct S;
impl S {
#[track_caller] // ok
fn foo() {}
}
fn main() {}

View file

@ -1,9 +0,0 @@
error[E0738]: `#[track_caller]` may not be used on trait methods
--> $DIR/error-with-trait-fn-impl.rs:10:5
|
LL | #[track_caller]
| ^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0738`.

View file

@ -14,6 +14,49 @@ fn tracked_unit(_: ()) {
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
trait Trait {
fn trait_tracked_unit(_: ());
}
impl Trait for () {
#[track_caller]
fn trait_tracked_unit(_: ()) {
let expected_line = line!() - 1;
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
}
trait TrackedTrait {
#[track_caller]
fn trait_tracked_unit_default(_: ()) {
let expected_line = line!() - 1;
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
}
impl TrackedTrait for () {}
trait BlanketTrackedTrait {
#[track_caller]
fn tracked_blanket(_: ());
}
impl BlanketTrackedTrait for () {
fn tracked_blanket(_: ()) {
let expected_line = line!() - 1;
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
}
fn main() {
pass_to_ptr_call(tracked_unit, ());
pass_to_ptr_call(<() as Trait>::trait_tracked_unit, ());
pass_to_ptr_call(<() as TrackedTrait>::trait_tracked_unit_default, ());
pass_to_ptr_call(<() as BlanketTrackedTrait>::tracked_blanket, ());
}

View file

@ -14,6 +14,49 @@ fn tracked() {
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
trait Trait {
fn trait_tracked();
}
impl Trait for () {
#[track_caller]
fn trait_tracked() {
let expected_line = line!() - 1;
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
}
trait TrackedTrait {
#[track_caller]
fn trait_tracked_default() {
let expected_line = line!() - 1;
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
}
impl TrackedTrait for () {}
trait TraitBlanketTracked {
#[track_caller]
fn tracked_blanket();
}
impl TraitBlanketTracked for () {
fn tracked_blanket() {
let expected_line = line!() - 1;
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_eq!(location.line(), expected_line, "call shims report location as fn definition");
}
}
fn main() {
ptr_call(tracked);
ptr_call(<() as Trait>::trait_tracked);
ptr_call(<() as TrackedTrait>::trait_tracked_default);
ptr_call(<() as TraitBlanketTracked>::tracked_blanket);
}

View file

@ -0,0 +1,79 @@
// run-pass
#![feature(track_caller)]
macro_rules! assert_expansion_site_is_tracked {
() => {{
let location = std::panic::Location::caller();
assert_eq!(location.file(), file!());
assert_ne!(location.line(), line!(), "line should be outside this fn");
}}
}
trait Tracked {
fn local_tracked(&self);
#[track_caller]
fn blanket_tracked(&self);
#[track_caller]
fn default_tracked(&self) {
assert_expansion_site_is_tracked!();
}
}
impl Tracked for () {
#[track_caller]
fn local_tracked(&self) {
assert_expansion_site_is_tracked!();
}
fn blanket_tracked(&self) {
assert_expansion_site_is_tracked!();
}
}
impl Tracked for bool {
#[track_caller]
fn local_tracked(&self) {
assert_expansion_site_is_tracked!();
}
fn blanket_tracked(&self) {
assert_expansion_site_is_tracked!();
}
fn default_tracked(&self) {
assert_expansion_site_is_tracked!();
}
}
impl Tracked for u8 {
#[track_caller]
fn local_tracked(&self) {
assert_expansion_site_is_tracked!();
}
fn blanket_tracked(&self) {
assert_expansion_site_is_tracked!();
}
#[track_caller]
fn default_tracked(&self) {
assert_expansion_site_is_tracked!();
}
}
fn main() {
().local_tracked();
().default_tracked();
().blanket_tracked();
true.local_tracked();
true.default_tracked();
true.blanket_tracked();
0u8.local_tracked();
0u8.default_tracked();
0u8.blanket_tracked();
}