# Add name of calling function to \`std::panic::Location\`

**URL:** <https://internals.rust-lang.org/t/add-name-of-calling-function-to-std-location/13013>\
**Category:** language design\
**Created:** [September 7, 2020, 3:19am UTC](https://internals.rust-lang.org/t/add-name-of-calling-function-to-std-location/13013 "2020-09-07T03:19:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/michael-f-bryan/32/2815_2.png) [@Michael-F-Bryan](https://internals.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [September 7, 2020, 3:19am UTC](https://internals.rust-lang.org/t/add-name-of-calling-function-to-std-location/13013/1 "2020-09-07T03:19:37Z")

</div>

You can currently use `#[track_caller]` and [`std::panic::Location`](https://doc.rust-lang.org/std/panic/struct.Location.html) to record where a function is called, however `Location` only exposes the caller's location in source code.

What would be needed to extend this to also expose the calling function's name?

My use case is to provide better error messages when something goes wrong. A native library I'm using only provides a boolean success code which I then turn into an empty `CallFailed` struct who's message is _"The call failed"_. I'd prefer to have a more useful message like _"The call to `set_watchdog()` failed"_.

```rust
use std::panic::Location;

struct CallFailed {
  caller: &'static Location<'static>,  
}

impl CallFailed {
  #[track_caller]
  pub(crate) fn new() -> Self {
    CallFailed { caller: Location::caller() }
  }
}

impl Display for CallFailed {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "The call to `{}()` failed", self.caller.function_name())
  }
}

impl Error for CallFailed {}

```

I could then create an extension trait for automatically transforming the returned true/false into a `Result<(), CallFailed>`, again using `#[track_caller]` to propagate the caller's `Location`.

```rust
pub(crate) trait CallFailedResult {
  fn into_call_failed(self) -> Result<(), CallFailed>;
}

impl CallFailedResult for std::os::raw::c_int {
  #[track_caller]
  fn into_call_failed(self) -> Result<(), CallFailed> {
    if self == 0 {
      Ok(())
    } else {
      Err(CallFailed::new())
    }
  }
}

```

Without `#[track_caller]` I'd be forced to use a macro or write out the function name every time `CallFailed` is created.

* * *

@comex made [a similar comment](https://github.com/rust-lang/rfcs/pull/2818#issuecomment-554506563) in [_A macro to get the current function name_](https://github.com/rust-lang/rfcs/pull/2818), but it seems that PR was closed. The workaround proposed (`stdext::function_name!()`) doesn't work with `#[track_caller]` because I'd need to call the macro directly, not as part of my error type's constructor.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [December 6, 2020, 3:19am UTC](https://internals.rust-lang.org/t/add-name-of-calling-function-to-std-location/13013/2 "2020-12-06T03:19:39Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
