File tree 4 files changed +26
-0
lines changed
4 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ mod diagnostic;
45
45
#[ unstable( feature = "proc_macro_diagnostic" , issue = "54140" ) ]
46
46
pub use diagnostic:: { Diagnostic , Level , MultiSpan } ;
47
47
48
+ use std:: ffi:: CStr ;
48
49
use std:: ops:: { Range , RangeBounds } ;
49
50
use std:: path:: PathBuf ;
50
51
use std:: str:: FromStr ;
@@ -1351,6 +1352,15 @@ impl Literal {
1351
1352
Literal :: new ( bridge:: LitKind :: ByteStr , & string, None )
1352
1353
}
1353
1354
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
+
1354
1364
/// Returns the span encompassing this literal.
1355
1365
#[ stable( feature = "proc_macro_lib2" , since = "1.29.0" ) ]
1356
1366
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 5
5
#![ crate_name = "proc_macro_api_tests" ]
6
6
#![ feature( proc_macro_span) ]
7
7
#![ feature( proc_macro_byte_character) ]
8
+ #![ feature( c_str_literals) ]
8
9
#![ deny( dead_code) ] // catch if a test function is never called
9
10
10
11
extern crate proc_macro;
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ fn test_display_literal() {
23
23
Literal :: string( "a \t ❤ ' \" \u{1} " ) . to_string( ) ,
24
24
"\" a \\ t ❤ ' \\ \" \\ u{1}\" " ,
25
25
) ;
26
+ assert_eq ! (
27
+ Literal :: c_string( c"a \t ❤ ' \" \u{1} " ) . to_string( ) ,
28
+ "\" a \\ t ❤ ' \\ \" \\ u{1}\" " ,
29
+ ) ;
26
30
assert_eq ! ( Literal :: character( 'a' ) . to_string( ) , "'a'" ) ;
27
31
assert_eq ! ( Literal :: character( '\t' ) . to_string( ) , "'\\ t'" ) ;
28
32
assert_eq ! ( Literal :: character( '❤' ) . to_string( ) , "'❤'" ) ;
@@ -41,6 +45,7 @@ fn test_parse_literal() {
41
45
assert_eq ! ( "b'a'" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "b'a'" ) ;
42
46
assert_eq ! ( "\" \n \" " . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "\" \n \" " ) ;
43
47
assert_eq ! ( "b\" \" " . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "b\" \" " ) ;
48
+ assert_eq ! ( "c\" \" " . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "c\" \" " ) ;
44
49
assert_eq ! ( "r##\" \" ##" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "r##\" \" ##" ) ;
45
50
assert_eq ! ( "10ulong" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "10ulong" ) ;
46
51
assert_eq ! ( "-10ulong" . parse:: <Literal >( ) . unwrap( ) . to_string( ) , "-10ulong" ) ;
You can’t perform that action at this time.
0 commit comments