@@ -33,6 +33,7 @@ use std::ascii::Char;
33
33
use std:: assert_matches:: assert_matches;
34
34
use std:: cmp:: { max, min} ;
35
35
use std:: collections:: HashMap ;
36
+ use std:: ffi:: CStr ;
36
37
use std:: io:: Write ;
37
38
use std:: ops:: ControlFlow ;
38
39
@@ -45,6 +46,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
45
46
check_foo ( * get_item ( & items, ( ItemKind :: Static , "FOO" ) ) . unwrap ( ) ) ;
46
47
check_bar ( * get_item ( & items, ( ItemKind :: Static , "BAR" ) ) . unwrap ( ) ) ;
47
48
check_len ( * get_item ( & items, ( ItemKind :: Static , "LEN" ) ) . unwrap ( ) ) ;
49
+ check_cstr ( * get_item ( & items, ( ItemKind :: Static , "C_STR" ) ) . unwrap ( ) ) ;
48
50
check_other_consts ( * get_item ( & items, ( ItemKind :: Fn , "other_consts" ) ) . unwrap ( ) ) ;
49
51
check_type_id ( * get_item ( & items, ( ItemKind :: Fn , "check_type_id" ) ) . unwrap ( ) ) ;
50
52
ControlFlow :: Continue ( ( ) )
@@ -86,6 +88,24 @@ fn check_bar(item: CrateItem) {
86
88
assert_eq ! ( std:: str :: from_utf8( & allocation. raw_bytes( ) . unwrap( ) ) , Ok ( "Bar" ) ) ;
87
89
}
88
90
91
+ /// Check the allocation data for static `C_STR`.
92
+ ///
93
+ /// ```no_run
94
+ /// static C_STR: &core::ffi::cstr = c"cstr";
95
+ /// ```
96
+ fn check_cstr ( item : CrateItem ) {
97
+ let def = StaticDef :: try_from ( item) . unwrap ( ) ;
98
+ let alloc = def. eval_initializer ( ) . unwrap ( ) ;
99
+ assert_eq ! ( alloc. provenance. ptrs. len( ) , 1 ) ;
100
+ let deref = item. ty ( ) . kind ( ) . builtin_deref ( true ) . unwrap ( ) ;
101
+ assert ! ( deref. ty. kind( ) . is_cstr( ) , "Expected CStr, but got: {:?}" , item. ty( ) ) ;
102
+
103
+ let alloc_id_0 = alloc. provenance . ptrs [ 0 ] . 1 . 0 ;
104
+ let GlobalAlloc :: Memory ( allocation) = GlobalAlloc :: from ( alloc_id_0) else { unreachable ! ( ) } ;
105
+ assert_eq ! ( allocation. bytes. len( ) , 5 ) ;
106
+ assert_eq ! ( CStr :: from_bytes_until_nul( & allocation. raw_bytes( ) . unwrap( ) ) , Ok ( c"cstr" ) ) ;
107
+ }
108
+
89
109
/// Check the allocation data for constants used in `other_consts` function.
90
110
fn check_other_consts ( item : CrateItem ) {
91
111
// Instance body will force constant evaluation.
@@ -206,6 +226,7 @@ fn main() {
206
226
generate_input ( & path) . unwrap ( ) ;
207
227
let args = vec ! [
208
228
"rustc" . to_string( ) ,
229
+ "--edition=2021" . to_string( ) ,
209
230
"--crate-name" . to_string( ) ,
210
231
CRATE_NAME . to_string( ) ,
211
232
path. to_string( ) ,
@@ -224,6 +245,7 @@ fn generate_input(path: &str) -> std::io::Result<()> {
224
245
static LEN: usize = 2;
225
246
static FOO: [&str; 2] = ["hi", "there"];
226
247
static BAR: &str = "Bar";
248
+ static C_STR: &std::ffi::CStr = c"cstr";
227
249
const NULL: *const u8 = std::ptr::null();
228
250
const TUPLE: (u32, u32) = (10, u32::MAX);
229
251
0 commit comments