@@ -200,6 +200,21 @@ pub enum MentionedItem<'tcx> {
200200 Closure ( Ty < ' tcx > ) ,
201201}
202202
203+ /// Track what typing mode a MIR body should use at current stage.
204+ /// A MIR body usually starts with `PostTypeckThroughBorrowck`. After borrowck is done, it's set
205+ /// to `PostBorrowck`. In `PostAnalysisNormalize` pass, we reveal all opaques types and set the
206+ /// typing mode to `PostAnalysis`.
207+ ///
208+ /// We can't directly use `ty::TypingMode` since it's not `TyEncodable/TyDecodable`.
209+ /// Previously we use MIR phase to derive this but it's inaccurate.
210+ #[ derive( Copy , Clone , PartialEq , Debug ) ]
211+ #[ derive( TyEncodable , TyDecodable , StableHash , TypeFoldable , TypeVisitable ) ]
212+ pub enum BodyTypingMode {
213+ PostTypeckThroughBorrowck ,
214+ PostBorrowck ,
215+ PostAnalysis ,
216+ }
217+
203218/// The lowered representation of a single function.
204219#[ derive( Clone , TyEncodable , TyDecodable , Debug , StableHash , TypeFoldable , TypeVisitable ) ]
205220pub struct Body < ' tcx > {
@@ -214,6 +229,9 @@ pub struct Body<'tcx> {
214229 /// us to see the difference and forego optimization on the inlined promoted items.
215230 pub phase : MirPhase ,
216231
232+ /// See [`BodyTypingMode`] for more info.
233+ pub typing_mode : BodyTypingMode ,
234+
217235 /// How many passes we have executed since starting the current phase. Used for debug output.
218236 pub pass_count : usize ,
219237
@@ -352,6 +370,7 @@ impl<'tcx> Body<'tcx> {
352370
353371 let mut body = Body {
354372 phase : MirPhase :: Built ,
373+ typing_mode : BodyTypingMode :: PostTypeckThroughBorrowck ,
355374 pass_count : 0 ,
356375 source,
357376 basic_blocks : BasicBlocks :: new ( basic_blocks) ,
@@ -383,6 +402,7 @@ impl<'tcx> Body<'tcx> {
383402 pub fn new_cfg_only ( basic_blocks : IndexVec < BasicBlock , BasicBlockData < ' tcx > > ) -> Self {
384403 let mut body = Body {
385404 phase : MirPhase :: Built ,
405+ typing_mode : BodyTypingMode :: PostTypeckThroughBorrowck ,
386406 pass_count : 0 ,
387407 source : MirSource :: item ( CRATE_DEF_ID . to_def_id ( ) ) ,
388408 basic_blocks : BasicBlocks :: new ( basic_blocks) ,
@@ -413,38 +433,35 @@ impl<'tcx> Body<'tcx> {
413433
414434 pub fn typing_env ( & self , tcx : TyCtxt < ' tcx > ) -> TypingEnv < ' tcx > {
415435 if tcx. use_typing_mode_post_typeck_through_borrowck ( ) {
416- match self . phase {
417- MirPhase :: Built | MirPhase :: Analysis ( AnalysisPhase :: Initial )
418- if let Some ( def_id ) = self . source . def_id ( ) . as_local ( ) =>
419- {
436+ match self . typing_mode {
437+ BodyTypingMode :: PostTypeckThroughBorrowck => {
438+ let local_def_id =
439+ self . source . def_id ( ) . as_local ( ) . expect ( "We only build MIR for local body" ) ;
420440 TypingEnv :: new (
421441 tcx. param_env ( self . source . def_id ( ) ) ,
422- ty:: TypingMode :: borrowck ( tcx, def_id ) ,
442+ ty:: TypingMode :: borrowck ( tcx, local_def_id ) ,
423443 )
424444 }
425- MirPhase :: Analysis ( AnalysisPhase :: PostCleanup )
426- if let Some ( def_id ) = self . source . def_id ( ) . as_local ( ) =>
427- {
445+ BodyTypingMode :: PostBorrowck => {
446+ let local_def_id =
447+ self . source . def_id ( ) . as_local ( ) . expect ( "We only build MIR for local body" ) ;
428448 TypingEnv :: new (
429449 tcx. param_env ( self . source . def_id ( ) ) ,
430- ty:: TypingMode :: post_borrowck_analysis ( tcx, def_id ) ,
450+ ty:: TypingMode :: post_borrowck_analysis ( tcx, local_def_id ) ,
431451 )
432452 }
433- MirPhase :: Built | MirPhase :: Analysis ( _) => {
434- // This branch happens for drop glue and fn ptr shims.
435- // FIXME: why do we do any of this analysis on drop glue etc?
436- // This should ideally all be skipped.
437- TypingEnv :: post_analysis ( tcx, self . source . def_id ( ) )
438- }
439- MirPhase :: Runtime ( _) => TypingEnv :: post_analysis ( tcx, self . source . def_id ( ) ) ,
453+ // We build shims like `drop_in_place` in post analysis typing mode.
454+ BodyTypingMode :: PostAnalysis => TypingEnv :: post_analysis ( tcx, self . source . def_id ( ) ) ,
440455 }
441456 } else {
442- match self . phase {
443- MirPhase :: Built | MirPhase :: Analysis ( _) => TypingEnv :: new (
444- tcx. param_env ( self . source . def_id ( ) ) ,
445- ty:: TypingMode :: non_body_analysis ( ) ,
446- ) ,
447- MirPhase :: Runtime ( _) => TypingEnv :: post_analysis ( tcx, self . source . def_id ( ) ) ,
457+ match self . typing_mode {
458+ BodyTypingMode :: PostTypeckThroughBorrowck | BodyTypingMode :: PostBorrowck => {
459+ TypingEnv :: new (
460+ tcx. param_env ( self . source . def_id ( ) ) ,
461+ ty:: TypingMode :: non_body_analysis ( ) ,
462+ )
463+ }
464+ BodyTypingMode :: PostAnalysis => TypingEnv :: post_analysis ( tcx, self . source . def_id ( ) ) ,
448465 }
449466 }
450467 }
0 commit comments