Skip to content

Commit 247aa5e

Browse files
committed
add ptr_mask test
1 parent c8b310e commit 247aa5e

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/compile_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ run_test! {intrinsics,offset_of,unstable}
711711
run_test! {intrinsics,overflow_ops,stable}
712712
run_test! {intrinsics,pow_sqrt,stable}
713713
run_test! {intrinsics,printf,stable}
714+
run_test! {intrinsics,ptr_mask,stable}
714715
run_test! {intrinsics,ptr_offset_from_unsigned,stable}
715716
run_test! {intrinsics,round,stable}
716717
run_test! {intrinsics,size_of_val,stable}

test/intrinsics/is_val_statically_known.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ extern crate core;
1414
use core::intrinsics::is_val_statically_known;
1515

1616
fn main() {
17-
test_eq!(is_val_statically_known(42), true);
18-
test_eq!(is_val_statically_known([42]), false);
19-
test_eq!(is_val_statically_known("42"), false);
20-
test_eq!(is_val_statically_known(FortyTwo {}), false);
21-
test_eq!(is_val_statically_known(FortyTwo {}.forty_two()), false);
17+
test_eq!(is_val_statically_known(42), black_box(true));
18+
test_eq!(is_val_statically_known([42]), black_box(false));
19+
test_eq!(is_val_statically_known("42"), black_box(false));
20+
test_eq!(is_val_statically_known(FortyTwo {}), black_box(false));
21+
test_eq!(
22+
is_val_statically_known(FortyTwo {}.forty_two()),
23+
black_box(false)
24+
);
2225
}
2326

2427
#[derive(Copy, Clone)]

test/intrinsics/ptr_mask.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![feature(
2+
lang_items,
3+
adt_const_params,
4+
associated_type_defaults,
5+
core_intrinsics,
6+
start,
7+
unsized_const_params,
8+
ptr_mask,
9+
strict_provenance
10+
)]
11+
#![allow(internal_features, incomplete_features, unused_variables, dead_code)]
12+
#![no_std]
13+
include!("../common.rs");
14+
extern crate core;
15+
16+
use core::intrinsics::ptr_mask;
17+
18+
fn main() {
19+
let v = 17_u32;
20+
let ptr: *const u32 = &v;
21+
22+
// `u32` is 4 bytes aligned,
23+
// which means that lower 2 bits are always 0.
24+
let tag_mask = 0b11;
25+
let mask = !tag_mask;
26+
27+
// We can store something in these lower bits
28+
let tagged_ptr = ptr.map_addr(|a| a | 0b10);
29+
30+
// Get the "tag" back
31+
let tag = tagged_ptr.addr() & tag_mask;
32+
assert_eq!(tag, black_box(0b10));
33+
34+
// Note that `tagged_ptr` is unaligned, it's UB to read from it.
35+
// To get original pointer `mask` can be used:
36+
let masked_ptr = ptr_mask(tagged_ptr, mask);
37+
assert_eq!(unsafe { *masked_ptr }, black_box(17));
38+
}

0 commit comments

Comments
 (0)