Skip to content

Instantly share code, notes, and snippets.

@Techcable
Last active September 26, 2025 07:23
Show Gist options
  • Save Techcable/a80e3a4c4d928a2038dc45bd6d91f9e4 to your computer and use it in GitHub Desktop.
Save Techcable/a80e3a4c4d928a2038dc45bd6d91f9e4 to your computer and use it in GitHub Desktop.
Demonstrating an unfortunate interaction between `#[track_caller]` and const-evaluation.
#![allow(dead_code)]
use std::panic::Location;
#[track_caller]
fn runtime_line() -> &'static u32 {
Box::leak(Box::new(core::panic::Location::caller().line()))
}
#[track_caller]
fn inline_const_line() -> &'static u32 {
const { &core::panic::Location::caller().line() }
}
#[track_caller]
fn decl_const_line() -> &'static u32 {
const LINE: u32 = Location::caller().line();
&LINE
}
#[test]
fn runtime() {
let line = runtime_line();
assert_eq!(*line, line!() - 1);
}
#[test]
fn inline_const() {
let line = inline_const_line();
assert_eq!(*line, line!() - 1);
}
#[test]
fn decl_const() {
let line = decl_const_line();
assert_eq!(*line, line!() - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment