Last active
September 26, 2025 07:23
-
-
Save Techcable/a80e3a4c4d928a2038dc45bd6d91f9e4 to your computer and use it in GitHub Desktop.
Demonstrating an unfortunate interaction between `#[track_caller]` and const-evaluation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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