@@ -33,6 +33,7 @@ use std::ascii::Char;
3333use std:: assert_matches:: assert_matches;
3434use std:: cmp:: { max, min} ;
3535use std:: collections:: HashMap ;
36+ use std:: ffi:: CStr ;
3637use std:: io:: Write ;
3738use std:: ops:: ControlFlow ;
3839
@@ -45,6 +46,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
4546 check_foo ( * get_item ( & items, ( ItemKind :: Static , "FOO" ) ) . unwrap ( ) ) ;
4647 check_bar ( * get_item ( & items, ( ItemKind :: Static , "BAR" ) ) . unwrap ( ) ) ;
4748 check_len ( * get_item ( & items, ( ItemKind :: Static , "LEN" ) ) . unwrap ( ) ) ;
49+ check_cstr ( * get_item ( & items, ( ItemKind :: Static , "C_STR" ) ) . unwrap ( ) ) ;
4850 check_other_consts ( * get_item ( & items, ( ItemKind :: Fn , "other_consts" ) ) . unwrap ( ) ) ;
4951 check_type_id ( * get_item ( & items, ( ItemKind :: Fn , "check_type_id" ) ) . unwrap ( ) ) ;
5052 ControlFlow :: Continue ( ( ) )
@@ -86,6 +88,24 @@ fn check_bar(item: CrateItem) {
8688 assert_eq ! ( std:: str :: from_utf8( & allocation. raw_bytes( ) . unwrap( ) ) , Ok ( "Bar" ) ) ;
8789}
8890
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+
89109/// Check the allocation data for constants used in `other_consts` function.
90110fn check_other_consts ( item : CrateItem ) {
91111 // Instance body will force constant evaluation.
@@ -206,6 +226,7 @@ fn main() {
206226 generate_input ( & path) . unwrap ( ) ;
207227 let args = vec ! [
208228 "rustc" . to_string( ) ,
229+ "--edition=2021" . to_string( ) ,
209230 "--crate-name" . to_string( ) ,
210231 CRATE_NAME . to_string( ) ,
211232 path. to_string( ) ,
@@ -224,6 +245,7 @@ fn generate_input(path: &str) -> std::io::Result<()> {
224245 static LEN: usize = 2;
225246 static FOO: [&str; 2] = ["hi", "there"];
226247 static BAR: &str = "Bar";
248+ static C_STR: &std::ffi::CStr = c"cstr";
227249 const NULL: *const u8 = std::ptr::null();
228250 const TUPLE: (u32, u32) = (10, u32::MAX);
229251
0 commit comments