@@ -8,6 +8,7 @@ use rustc_index::Idx;
88use rustc_middle:: bug;
99use rustc_middle:: ty:: layout:: { LayoutError , SizeSkeleton } ;
1010use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
11+ use rustc_span:: ErrorGuaranteed ;
1112use rustc_span:: def_id:: LocalDefId ;
1213use tracing:: trace;
1314
@@ -75,7 +76,7 @@ fn check_transmute<'tcx>(
7576 from : Ty < ' tcx > ,
7677 to : Ty < ' tcx > ,
7778 hir_id : HirId ,
78- ) {
79+ ) -> Result < ( ) , ErrorGuaranteed > {
7980 let span = || tcx. hir_span ( hir_id) ;
8081 let normalize = |ty| {
8182 if let Ok ( ty) = tcx. try_normalize_erasing_regions ( typing_env, ty) {
@@ -95,7 +96,7 @@ fn check_transmute<'tcx>(
9596
9697 // Transmutes that are only changing lifetimes are always ok.
9798 if from == to {
98- return ;
99+ return Ok ( ( ) ) ;
99100 }
100101
101102 let sk_from = SizeSkeleton :: compute ( from, tcx, typing_env) ;
@@ -107,7 +108,7 @@ fn check_transmute<'tcx>(
107108 && let Ok ( sk_to) = sk_to
108109 {
109110 if sk_from. same_size ( sk_to) {
110- return ;
111+ return Ok ( ( ) ) ;
111112 }
112113
113114 // Special-case transmuting from `typeof(function)` and
@@ -122,7 +123,7 @@ fn check_transmute<'tcx>(
122123 . with_note ( format ! ( "target type: {to}" ) )
123124 . with_help ( "cast with `as` to a pointer instead" )
124125 . emit ( ) ;
125- return ;
126+ return Ok ( ( ) ) ;
126127 }
127128 }
128129
@@ -134,21 +135,25 @@ fn check_transmute<'tcx>(
134135 ) ;
135136 if from == to {
136137 err. note ( format ! ( "`{from}` does not have a fixed size" ) ) ;
137- err. emit ( ) ;
138+ Err ( err. emit ( ) )
138139 } else {
139140 err. note ( format ! ( "source type: `{}` ({})" , from, skeleton_string( from, sk_from) ) ) ;
140141 err. note ( format ! ( "target type: `{}` ({})" , to, skeleton_string( to, sk_to) ) ) ;
141- err. emit ( ) ;
142+ Err ( err. emit ( ) )
142143 }
143144}
144145
145- pub ( crate ) fn check_transmutes ( tcx : TyCtxt < ' _ > , owner : LocalDefId ) {
146+ pub ( crate ) fn check_transmutes ( tcx : TyCtxt < ' _ > , owner : LocalDefId ) -> Result < ( ) , ErrorGuaranteed > {
146147 assert ! ( !tcx. is_typeck_child( owner. to_def_id( ) ) ) ;
147148 let typeck_results = tcx. typeck ( owner) ;
148- let None = typeck_results. tainted_by_errors else { return } ;
149+ if let Some ( e) = typeck_results. tainted_by_errors {
150+ return Err ( e) ;
151+ } ;
149152
150153 let typing_env = ty:: TypingEnv :: post_analysis ( tcx, owner) ;
154+ let mut result = Ok ( ( ) ) ;
151155 for & ( from, to, hir_id) in & typeck_results. transmutes_to_check {
152- check_transmute ( tcx, typing_env, from, to, hir_id) ;
156+ result = check_transmute ( tcx, typing_env, from, to, hir_id) ;
153157 }
158+ result
154159}
0 commit comments