Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constify Location methods #101030

Merged
merged 7 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/core/src/panic/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ impl<'a> Location<'a> {
/// ```
#[must_use]
#[stable(feature = "panic_hooks", since = "1.10.0")]
#[rustc_const_unstable(feature = "const_location_fields", issue = "102911")]
#[inline]
pub fn file(&self) -> &str {
pub const fn file(&self) -> &str {
self.file
}

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

Expand All @@ -171,8 +173,9 @@ impl<'a> Location<'a> {
/// ```
#[must_use]
#[stable(feature = "panic_col", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_location_fields", issue = "102911")]
#[inline]
pub fn column(&self) -> u32 {
pub const fn column(&self) -> u32 {
self.col
}
}
Expand Down
3 changes: 3 additions & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![feature(const_assume)]
#![feature(const_black_box)]
#![feature(const_bool_to_option)]
#![feature(const_caller_location)]
#![feature(const_cell_into_inner)]
#![feature(const_convert)]
#![feature(const_heap)]
Expand All @@ -20,6 +21,7 @@
#![feature(const_ptr_write)]
#![feature(const_trait_impl)]
#![feature(const_likely)]
#![feature(const_location_fields)]
#![feature(core_intrinsics)]
#![feature(core_private_bignum)]
#![feature(core_private_diy_float)]
Expand Down Expand Up @@ -130,6 +132,7 @@ mod nonzero;
mod num;
mod ops;
mod option;
mod panic;
mod pattern;
mod pin;
mod pin_macro;
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod location;
31 changes: 31 additions & 0 deletions library/core/tests/panic/location.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use core::panic::Location;

// Note: Some of the following tests depend on the source location,
// so please be careful when editing this file.

#[test]
fn location_const_caller() {
const _CALLER_REFERENCE: &Location<'static> = Location::caller();
const _CALLER: Location<'static> = *Location::caller();
}

#[test]
fn location_const_file() {
const CALLER: &Location<'static> = Location::caller();
const FILE: &str = CALLER.file();
assert_eq!(FILE, file!());
}

#[test]
fn location_const_line() {
const CALLER: &Location<'static> = Location::caller();
const LINE: u32 = CALLER.line();
assert_eq!(LINE, 21);
}

#[test]
fn location_const_column() {
const CALLER: &Location<'static> = Location::caller();
const COLUMN: u32 = CALLER.column();
assert_eq!(COLUMN, 40);
}