@@ -1224,8 +1224,16 @@ fn escape_utf8(string: &str, repr: &mut String) {
1224
1224
1225
1225
#[ cfg( feature = "proc-macro" ) ]
1226
1226
pub ( crate ) trait FromStr2 : FromStr < Err = proc_macro:: LexError > {
1227
+ #[ cfg( wrap_proc_macro) ]
1228
+ fn validate ( src : & str ) -> Result < ( ) , LexError > ;
1229
+
1227
1230
#[ cfg( wrap_proc_macro) ]
1228
1231
fn from_str_checked ( src : & str ) -> Result < Self , imp:: LexError > {
1232
+ // Validate using fallback parser, because rustc is incapable of
1233
+ // returning a recoverable Err for certain invalid token streams, and
1234
+ // will instead permanently poison the compilation.
1235
+ Self :: validate ( src) ?;
1236
+
1229
1237
// Catch panic to work around https://github.com/rust-lang/rust/issues/58736.
1230
1238
match panic:: catch_unwind ( || Self :: from_str ( src) ) {
1231
1239
Ok ( Ok ( ok) ) => Ok ( ok) ,
@@ -1240,7 +1248,17 @@ pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
1240
1248
}
1241
1249
1242
1250
#[ cfg( feature = "proc-macro" ) ]
1243
- impl FromStr2 for proc_macro:: TokenStream { }
1251
+ impl FromStr2 for proc_macro:: TokenStream {
1252
+ #[ cfg( wrap_proc_macro) ]
1253
+ fn validate ( src : & str ) -> Result < ( ) , LexError > {
1254
+ TokenStream :: from_str_checked ( src) . map ( drop)
1255
+ }
1256
+ }
1244
1257
1245
1258
#[ cfg( feature = "proc-macro" ) ]
1246
- impl FromStr2 for proc_macro:: Literal { }
1259
+ impl FromStr2 for proc_macro:: Literal {
1260
+ #[ cfg( wrap_proc_macro) ]
1261
+ fn validate ( src : & str ) -> Result < ( ) , LexError > {
1262
+ Literal :: from_str_checked ( src) . map ( drop)
1263
+ }
1264
+ }
0 commit comments