@@ -240,6 +240,11 @@ pub fn print_crate<'a>(
240
240
let mut s =
241
241
State { s : pp:: Printer :: new ( ) , comments : Some ( Comments :: new ( sm, filename, input) ) , ann } ;
242
242
243
+ // We need to print shebang before anything else
244
+ // otherwise the resulting code will not compile
245
+ // and shebang will be useless.
246
+ s. maybe_print_shebang ( ) ;
247
+
243
248
if is_expanded && !krate. attrs . iter ( ) . any ( |attr| attr. has_name ( sym:: no_core) ) {
244
249
// We need to print `#![no_std]` (and its feature gate) so that
245
250
// compiling pretty-printed source won't inject libstd again.
@@ -560,6 +565,20 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
560
565
self . word ( st)
561
566
}
562
567
568
+ fn maybe_print_shebang ( & mut self ) {
569
+ if let Some ( cmnt) = self . peek_comment ( ) {
570
+ // Comment is a shebang if it's:
571
+ // Isolated, starts with #! and doesn't continue with `[`
572
+ // See [rustc_lexer::strip_shebang] and [gather_comments] from pprust/state.rs for details
573
+ if cmnt. style == CommentStyle :: Isolated
574
+ && cmnt. lines . first ( ) . map_or ( false , |l| l. starts_with ( "#!" ) )
575
+ {
576
+ let cmnt = self . next_comment ( ) . unwrap ( ) ;
577
+ self . print_comment ( cmnt) ;
578
+ }
579
+ }
580
+ }
581
+
563
582
fn print_inner_attributes ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
564
583
self . print_either_attributes ( attrs, ast:: AttrStyle :: Inner , false , true )
565
584
}
0 commit comments