@@ -49,6 +49,7 @@ use std::cmp::max;
49
49
use std:: collections:: BTreeMap ;
50
50
use std:: env;
51
51
use std:: ffi:: OsString ;
52
+ use std:: fmt:: Write as _;
52
53
use std:: fs;
53
54
use std:: io:: { self , IsTerminal , Read , Write } ;
54
55
use std:: panic:: { self , catch_unwind} ;
@@ -65,6 +66,11 @@ macro do_not_use_print($($t:tt)*) {
65
66
)
66
67
}
67
68
69
+ #[ allow( unused_macros) ]
70
+ macro do_not_use_safe_print( $( $t: tt) * ) {
71
+ std:: compile_error!( "Don't use `safe_print` or `safe_println` here, use `println_info` instead" )
72
+ }
73
+
68
74
// This import blocks the use of panicking `print` and `println` in all the code
69
75
// below. Please use `safe_print` and `safe_println` to avoid ICE when
70
76
// encountering an I/O error during print.
@@ -713,6 +719,13 @@ fn print_crate_info(
713
719
parse_attrs: bool ,
714
720
) -> Compilation {
715
721
use rustc_session:: config:: PrintKind :: * ;
722
+
723
+ // This import prevents the following code from using the printing macros
724
+ // used by the rest of the module. Within this function, we only write to
725
+ // the output specified by `sess.io.output_file`.
726
+ #[ allow( unused_imports) ]
727
+ use { do_not_use_safe_print as safe_print, do_not_use_safe_print as safe_println} ;
728
+
716
729
// NativeStaticLibs and LinkArgs are special - printed during linking
717
730
// (empty iterator returns true)
718
731
if sess. opts. prints. iter( ) . all( |p| p. kind == NativeStaticLibs || p. kind == LinkArgs ) {
@@ -731,17 +744,23 @@ fn print_crate_info(
731
744
} else {
732
745
None
733
746
} ;
747
+
734
748
for req in & sess. opts. prints {
749
+ let mut crate_info = String :: new( ) ;
750
+ macro println_info( $( $arg: tt) * ) {
751
+ crate_info. write_fmt( format_args!( "{}\n " , format_args!( $( $arg) * ) ) ) . unwrap( )
752
+ }
753
+
735
754
match req. kind {
736
755
TargetList => {
737
756
let mut targets = rustc_target:: spec:: TARGETS . to_vec( ) ;
738
757
targets. sort_unstable( ) ;
739
- safe_println ! ( "{}" , targets. join( "\n " ) ) ;
758
+ println_info !( "{}" , targets. join( "\n " ) ) ;
740
759
}
741
- Sysroot => safe_println ! ( "{}" , sess. sysroot. display( ) ) ,
742
- TargetLibdir => safe_println ! ( "{}" , sess. target_tlib_path. dir. display( ) ) ,
760
+ Sysroot => println_info !( "{}" , sess. sysroot. display( ) ) ,
761
+ TargetLibdir => println_info !( "{}" , sess. target_tlib_path. dir. display( ) ) ,
743
762
TargetSpec => {
744
- safe_println ! ( "{}" , serde_json:: to_string_pretty( & sess. target. to_json( ) ) . unwrap( ) ) ;
763
+ println_info !( "{}" , serde_json:: to_string_pretty( & sess. target. to_json( ) ) . unwrap( ) ) ;
745
764
}
746
765
AllTargetSpecs => {
747
766
let mut targets = BTreeMap :: new( ) ;
@@ -750,7 +769,7 @@ fn print_crate_info(
750
769
let target = Target :: expect_builtin( & triple) ;
751
770
targets. insert( name, target. to_json( ) ) ;
752
771
}
753
- safe_println ! ( "{}" , serde_json:: to_string_pretty( & targets) . unwrap( ) ) ;
772
+ println_info !( "{}" , serde_json:: to_string_pretty( & targets) . unwrap( ) ) ;
754
773
}
755
774
FileNames | CrateName => {
756
775
let Some ( attrs) = attrs. as_ref( ) else {
@@ -760,14 +779,14 @@ fn print_crate_info(
760
779
let t_outputs = rustc_interface:: util:: build_output_filenames( attrs, sess) ;
761
780
let id = rustc_session:: output:: find_crate_name( sess, attrs) ;
762
781
if req. kind == CrateName {
763
- safe_println ! ( "{id}" ) ;
764
- continue ;
765
- }
766
- let crate_types = collect_crate_types ( sess , attrs ) ;
767
- for & style in & crate_types {
768
- let fname =
769
- rustc_session :: output :: filename_for_input ( sess , style , id , & t_outputs ) ;
770
- safe_println ! ( "{}" , fname . as_path ( ) . file_name ( ) . unwrap ( ) . to_string_lossy ( ) ) ;
782
+ println_info !( "{id}" ) ;
783
+ } else {
784
+ let crate_types = collect_crate_types ( sess , attrs ) ;
785
+ for & style in & crate_types {
786
+ let fname =
787
+ rustc_session :: output :: filename_for_input ( sess , style , id , & t_outputs ) ;
788
+ println_info! ( "{}" , fname . as_path ( ) . file_name ( ) . unwrap ( ) . to_string_lossy ( ) ) ;
789
+ }
771
790
}
772
791
}
773
792
Cfg => {
@@ -801,13 +820,13 @@ fn print_crate_info(
801
820
802
821
cfgs. sort( ) ;
803
822
for cfg in cfgs {
804
- safe_println ! ( "{cfg}" ) ;
823
+ println_info !( "{cfg}" ) ;
805
824
}
806
825
}
807
826
CallingConventions => {
808
827
let mut calling_conventions = rustc_target:: spec:: abi:: all_names( ) ;
809
828
calling_conventions. sort_unstable( ) ;
810
- safe_println ! ( "{}" , calling_conventions. join( "\n " ) ) ;
829
+ println_info !( "{}" , calling_conventions. join( "\n " ) ) ;
811
830
}
812
831
RelocationModels
813
832
| CodeModels
@@ -825,15 +844,15 @@ fn print_crate_info(
825
844
826
845
for split in & [ Off , Packed , Unpacked ] {
827
846
if sess. target. options. supported_split_debuginfo. contains( split) {
828
- safe_println ! ( "{split}" ) ;
847
+ println_info !( "{split}" ) ;
829
848
}
830
849
}
831
850
}
832
851
DeploymentTarget => {
833
852
use rustc_target:: spec:: current_apple_deployment_target;
834
853
835
854
if sess. target. is_like_osx {
836
- safe_println ! (
855
+ println_info !(
837
856
"deployment_target={}" ,
838
857
current_apple_deployment_target( & sess. target)
839
858
. expect( "unknown Apple target OS" )
@@ -844,6 +863,8 @@ fn print_crate_info(
844
863
}
845
864
}
846
865
}
866
+
867
+ req. out. overwrite( & crate_info, sess) ;
847
868
}
848
869
Compilation :: Stop
849
870
}
0 commit comments