File tree Expand file tree Collapse file tree 4 files changed +26
-0
lines changed Expand file tree Collapse file tree 4 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ mod diagnostic;
4545#[ unstable( feature = "proc_macro_diagnostic" , issue = "54140" ) ]
4646pub use diagnostic:: { Diagnostic , Level , MultiSpan } ;
4747
48+ use std:: ffi:: CStr ;
4849use std:: ops:: { Range , RangeBounds } ;
4950use std:: path:: PathBuf ;
5051use std:: str:: FromStr ;
@@ -1351,6 +1352,15 @@ impl Literal {
13511352 Literal :: new ( bridge:: LitKind :: ByteStr , & string, None )
13521353 }
13531354
1355+ /// C string literal.
1356+ #[ stable( feature = "c_str_literals" , since = "1.76.0" ) ]
1357+ pub fn c_string ( string : & CStr ) -> Literal {
1358+ let quoted = format ! ( "{:?}" , string) ;
1359+ assert ! ( quoted. starts_with( '"' ) && quoted. ends_with( '"' ) ) ;
1360+ let symbol = & quoted[ 1 ..quoted. len ( ) - 1 ] ;
1361+ Literal :: new ( bridge:: LitKind :: CStr , symbol, None )
1362+ }
1363+
13541364 /// Returns the span encompassing this literal.
13551365 #[ stable( feature = "proc_macro_lib2" , since = "1.29.0" ) ]
13561366 pub fn span ( & self ) -> Span {
Original file line number Diff line number Diff line change 1+ // force-host
2+ #![ crate_type = "proc-macro" ]
3+
4+ extern crate proc_macro;
5+
6+ use proc_macro:: Literal ;
7+
8+ fn test ( ) {
9+ Literal :: c_string ( c"a" ) ; //~ ERROR use of unstable library feature 'c_str_literals'
10+ }
Original file line number Diff line number Diff line change 55#![ crate_name = "proc_macro_api_tests" ]
66#![ feature( proc_macro_span) ]
77#![ feature( proc_macro_byte_character) ]
8+ #![ feature( c_str_literals) ]
89#![ deny( dead_code) ] // catch if a test function is never called
910
1011extern crate proc_macro;
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ fn test_display_literal() {
2323 Literal :: string( "a \t ❤ ' \" \u{1} " ) . to_string( ) ,
2424 "\" a \\ t ❤ ' \\ \" \\ u{1}\" " ,
2525 ) ;
26+ assert_eq ! (
27+ Literal :: c_string( c"a \t ❤ ' \" \u{1} " ) . to_string( ) ,
28+ "\" a \\ t ❤ ' \\ \" \\ u{1}\" " ,
29+ ) ;
2630 assert_eq ! ( Literal :: character( 'a' ) . to_string( ) , "'a'" ) ;
2731 assert_eq ! ( Literal :: character( '\t' ) . to_string( ) , "'\\ t'" ) ;
2832 assert_eq ! ( Literal :: character( '❤' ) . to_string( ) , "'❤'" ) ;
@@ -41,6 +45,7 @@ fn test_parse_literal() {
4145 assert_eq ! ( "b'a'" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "b'a'" ) ;
4246 assert_eq ! ( "\" \n \" " . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "\" \n \" " ) ;
4347 assert_eq ! ( "b\" \" " . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "b\" \" " ) ;
48+ assert_eq ! ( "c\" \" " . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "c\" \" " ) ;
4449 assert_eq ! ( "r##\" \" ##" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "r##\" \" ##" ) ;
4550 assert_eq ! ( "10ulong" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "10ulong" ) ;
4651 assert_eq ! ( "-10ulong" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "-10ulong" ) ;
You can’t perform that action at this time.
0 commit comments