Skip to content

Commit bf15a9e

Browse files
committed
Auto merge of rust-lang#101030 - woppopo:const_location, r=scottmcm
Constify `Location` methods Tracking issue: rust-lang#102911 Example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4789884c2f16ec4fb0e0405d86b794f5
2 parents 5819f41 + a53e3ac commit bf15a9e

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

library/core/src/panic/location.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ impl<'a> Location<'a> {
123123
/// ```
124124
#[must_use]
125125
#[stable(feature = "panic_hooks", since = "1.10.0")]
126+
#[rustc_const_unstable(feature = "const_location_fields", issue = "102911")]
126127
#[inline]
127-
pub fn file(&self) -> &str {
128+
pub const fn file(&self) -> &str {
128129
self.file
129130
}
130131

@@ -147,8 +148,9 @@ impl<'a> Location<'a> {
147148
/// ```
148149
#[must_use]
149150
#[stable(feature = "panic_hooks", since = "1.10.0")]
151+
#[rustc_const_unstable(feature = "const_location_fields", issue = "102911")]
150152
#[inline]
151-
pub fn line(&self) -> u32 {
153+
pub const fn line(&self) -> u32 {
152154
self.line
153155
}
154156

@@ -171,8 +173,9 @@ impl<'a> Location<'a> {
171173
/// ```
172174
#[must_use]
173175
#[stable(feature = "panic_col", since = "1.25.0")]
176+
#[rustc_const_unstable(feature = "const_location_fields", issue = "102911")]
174177
#[inline]
175-
pub fn column(&self) -> u32 {
178+
pub const fn column(&self) -> u32 {
176179
self.col
177180
}
178181
}

library/core/tests/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(const_assume)]
88
#![feature(const_black_box)]
99
#![feature(const_bool_to_option)]
10+
#![feature(const_caller_location)]
1011
#![feature(const_cell_into_inner)]
1112
#![feature(const_convert)]
1213
#![feature(const_heap)]
@@ -20,6 +21,7 @@
2021
#![feature(const_ptr_write)]
2122
#![feature(const_trait_impl)]
2223
#![feature(const_likely)]
24+
#![feature(const_location_fields)]
2325
#![feature(core_intrinsics)]
2426
#![feature(core_private_bignum)]
2527
#![feature(core_private_diy_float)]
@@ -131,6 +133,7 @@ mod nonzero;
131133
mod num;
132134
mod ops;
133135
mod option;
136+
mod panic;
134137
mod pattern;
135138
mod pin;
136139
mod pin_macro;

library/core/tests/panic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod location;

library/core/tests/panic/location.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use core::panic::Location;
2+
3+
// Note: Some of the following tests depend on the source location,
4+
// so please be careful when editing this file.
5+
6+
#[test]
7+
fn location_const_caller() {
8+
const _CALLER_REFERENCE: &Location<'static> = Location::caller();
9+
const _CALLER: Location<'static> = *Location::caller();
10+
}
11+
12+
#[test]
13+
fn location_const_file() {
14+
const CALLER: &Location<'static> = Location::caller();
15+
const FILE: &str = CALLER.file();
16+
assert_eq!(FILE, file!());
17+
}
18+
19+
#[test]
20+
fn location_const_line() {
21+
const CALLER: &Location<'static> = Location::caller();
22+
const LINE: u32 = CALLER.line();
23+
assert_eq!(LINE, 21);
24+
}
25+
26+
#[test]
27+
fn location_const_column() {
28+
const CALLER: &Location<'static> = Location::caller();
29+
const COLUMN: u32 = CALLER.column();
30+
assert_eq!(COLUMN, 40);
31+
}

0 commit comments

Comments
 (0)