@@ -5,7 +5,7 @@ use crate::module::DirOwnership;
55
66use rustc_ast:: attr:: MarkedAttrs ;
77use rustc_ast:: ptr:: P ;
8- use rustc_ast:: token:: Nonterminal ;
8+ use rustc_ast:: token:: NonterminalKind ;
99use rustc_ast:: tokenstream:: TokenStream ;
1010use rustc_ast:: visit:: { AssocCtxt , Visitor } ;
1111use rustc_ast:: { self as ast, AttrVec , Attribute , HasAttrs , Item , NodeId , PatKind } ;
@@ -15,7 +15,8 @@ use rustc_data_structures::sync::{self, Lrc};
1515use rustc_errors:: { DiagCtxtHandle , ErrorGuaranteed , PResult } ;
1616use rustc_feature:: Features ;
1717use rustc_lint_defs:: { BufferedEarlyLint , RegisteredTools } ;
18- use rustc_parse:: { parser:: Parser , MACRO_ARGUMENTS } ;
18+ use rustc_parse:: parser:: { ParseNtResult , Parser } ;
19+ use rustc_parse:: MACRO_ARGUMENTS ;
1920use rustc_session:: config:: CollapseMacroDebuginfo ;
2021use rustc_session:: { parse:: ParseSess , Limit , Session } ;
2122use rustc_span:: def_id:: { CrateNum , DefId , LocalDefId } ;
@@ -1377,13 +1378,13 @@ pub fn parse_macro_name_and_helper_attrs(
13771378/// If this item looks like a specific enums from `rental`, emit a fatal error.
13781379/// See #73345 and #83125 for more details.
13791380/// FIXME(#73933): Remove this eventually.
1380- fn pretty_printing_compatibility_hack ( item : & Item , sess : & Session ) {
1381+ fn pretty_printing_compatibility_hack ( item : & Item , psess : & ParseSess ) {
13811382 let name = item. ident . name ;
13821383 if name == sym:: ProceduralMasqueradeDummyType
13831384 && let ast:: ItemKind :: Enum ( enum_def, _) = & item. kind
13841385 && let [ variant] = & * enum_def. variants
13851386 && variant. ident . name == sym:: Input
1386- && let FileName :: Real ( real) = sess . source_map ( ) . span_to_filename ( item. ident . span )
1387+ && let FileName :: Real ( real) = psess . source_map ( ) . span_to_filename ( item. ident . span )
13871388 && let Some ( c) = real
13881389 . local_path ( )
13891390 . unwrap_or ( Path :: new ( "" ) )
@@ -1403,15 +1404,15 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) {
14031404 if crate_matches {
14041405 // FIXME: make this translatable
14051406 #[ allow( rustc:: untranslatable_diagnostic) ]
1406- sess . dcx ( ) . emit_fatal ( errors:: ProcMacroBackCompat {
1407+ psess . dcx ( ) . emit_fatal ( errors:: ProcMacroBackCompat {
14071408 crate_name : "rental" . to_string ( ) ,
14081409 fixed_version : "0.5.6" . to_string ( ) ,
14091410 } ) ;
14101411 }
14111412 }
14121413}
14131414
1414- pub ( crate ) fn ann_pretty_printing_compatibility_hack ( ann : & Annotatable , sess : & Session ) {
1415+ pub ( crate ) fn ann_pretty_printing_compatibility_hack ( ann : & Annotatable , psess : & ParseSess ) {
14151416 let item = match ann {
14161417 Annotatable :: Item ( item) => item,
14171418 Annotatable :: Stmt ( stmt) => match & stmt. kind {
@@ -1420,17 +1421,34 @@ pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, sess: &S
14201421 } ,
14211422 _ => return ,
14221423 } ;
1423- pretty_printing_compatibility_hack ( item, sess )
1424+ pretty_printing_compatibility_hack ( item, psess )
14241425}
14251426
1426- pub ( crate ) fn nt_pretty_printing_compatibility_hack ( nt : & Nonterminal , sess : & Session ) {
1427- let item = match nt {
1428- Nonterminal :: NtItem ( item) => item,
1429- Nonterminal :: NtStmt ( stmt) => match & stmt. kind {
1430- ast:: StmtKind :: Item ( item) => item,
1431- _ => return ,
1432- } ,
1427+ pub ( crate ) fn stream_pretty_printing_compatibility_hack (
1428+ kind : NonterminalKind ,
1429+ stream : & TokenStream ,
1430+ psess : & ParseSess ,
1431+ ) {
1432+ let item = match kind {
1433+ NonterminalKind :: Item => {
1434+ let mut parser = Parser :: new ( psess, stream. clone ( ) , None ) ;
1435+ let Ok ( ParseNtResult :: Item ( item) ) = parser. parse_nonterminal ( kind) else {
1436+ panic ! ( "failed to reparse" ) ;
1437+ } ;
1438+ item
1439+ }
1440+ NonterminalKind :: Stmt => {
1441+ // njn: reparsing and then checking for StmtKind::Item sucks, hmm
1442+ let mut parser = Parser :: new ( psess, stream. clone ( ) , None ) ;
1443+ let Ok ( ParseNtResult :: Stmt ( stmt) ) = parser. parse_nonterminal ( kind) else {
1444+ panic ! ( "failed to reparse" ) ;
1445+ } ;
1446+ match & stmt. kind {
1447+ ast:: StmtKind :: Item ( item) => item. clone ( ) ,
1448+ _ => return ,
1449+ }
1450+ }
14331451 _ => return ,
14341452 } ;
1435- pretty_printing_compatibility_hack ( item, sess )
1453+ pretty_printing_compatibility_hack ( & item, psess )
14361454}
0 commit comments