@@ -21,6 +21,8 @@ use crate::session_diagnostics::{BorrowMutUpvarLable, FnMutBumpFn, OnModifyTy};
2121use crate :: util:: FindAssignments ;
2222use crate :: MirBorrowckCtxt ;
2323
24+ use super :: DescribedPlace ;
25+
2426#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
2527pub ( crate ) enum AccessKind {
2628 MutableBorrow ,
@@ -30,17 +32,17 @@ pub(crate) enum AccessKind {
3032#[ derive( Debug ) ]
3133pub ( crate ) enum PlaceAndReason < ' a > {
3234 // place, name
33- DeclaredImmute ( String , Option < String > ) ,
35+ DeclaredImmute ( DescribedPlace , Option < String > ) ,
3436 // place
35- InPatternGuard ( String ) ,
37+ InPatternGuard ( DescribedPlace ) ,
3638 // place, name
37- StaticItem ( String , Option < String > ) ,
39+ StaticItem ( DescribedPlace , Option < String > ) ,
3840 // place
39- UpvarCaptured ( String ) ,
41+ UpvarCaptured ( DescribedPlace ) ,
4042 // place
41- SelfCaptured ( String ) ,
43+ SelfCaptured ( DescribedPlace ) ,
4244 // place, pointer_type, name
43- BehindPointer ( Option < String > , BorrowedContentSource < ' a > , Option < String > ) ,
45+ BehindPointer ( DescribedPlace , BorrowedContentSource < ' a > , Option < String > ) ,
4446}
4547
4648impl < ' a , ' tcx > MirBorrowckCtxt < ' a , ' tcx > {
@@ -60,20 +62,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
6062 ) ;
6163
6264 let mut err;
63- let item_msg;
6465 let diagnostic: PlaceAndReason < ' _ > ;
6566 let mut opt_source = None ;
66- let access_place_desc = self . describe_any_place ( access_place. as_ref ( ) ) ;
67- debug ! ( "report_mutability_error: access_place_desc={:?}" , access_place_desc) ;
67+ let access_place_desc = self . describe_place_typed ( access_place. as_ref ( ) ) ;
68+
69+ let access_place_desc_dbg = self . describe_any_place ( access_place. as_ref ( ) ) ;
70+ debug ! ( "report_mutability_error: access_place_desc={:?}" , access_place_desc_dbg) ;
6871
6972 match the_place_err {
7073 PlaceRef { local, projection : [ ] } => {
71- item_msg = access_place_desc;
72- diagnosing = if access_place. as_local ( ) . is_some ( ) {
73- PlaceAndReason :: DeclaredImmute ( item_msg. clone ( ) , None )
74+ diagnostic = if access_place. as_local ( ) . is_some ( ) {
75+ PlaceAndReason :: DeclaredImmute ( access_place_desc, None )
7476 } else {
7577 let name = self . local_names [ local] . expect ( "immutable unnamed local" ) ;
76- PlaceAndReason :: DeclaredImmute ( item_msg . clone ( ) , Some ( name. to_string ( ) ) )
78+ PlaceAndReason :: DeclaredImmute ( access_place_desc , Some ( name. to_string ( ) ) )
7779 } ;
7880 }
7981
@@ -101,34 +103,31 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
101103 // If we deref an immutable ref then the suggestion here doesn't help.
102104 return ;
103105 } else {
104- item_msg = access_place_desc;
105106 diagnostic = if self . is_upvar_field_projection ( access_place. as_ref ( ) ) . is_some ( )
106107 {
107- PlaceAndReason :: DeclaredImmute ( item_msg . clone ( ) , None )
108+ PlaceAndReason :: DeclaredImmute ( access_place_desc , None )
108109 } else {
109110 let name = self . upvars [ upvar_index. index ( ) ] . place . to_string ( self . infcx . tcx ) ;
110- PlaceAndReason :: DeclaredImmute ( item_msg . clone ( ) , Some ( name) )
111+ PlaceAndReason :: DeclaredImmute ( access_place_desc , Some ( name) )
111112 }
112113 }
113114 }
114115
115116 PlaceRef { local, projection : [ ProjectionElem :: Deref ] }
116117 if self . body . local_decls [ local] . is_ref_for_guard ( ) =>
117118 {
118- item_msg = access_place_desc;
119119 diagnostic = PlaceAndReason :: InPatternGuard ( access_place_desc)
120120 }
121121 PlaceRef { local, projection : [ ProjectionElem :: Deref ] }
122122 if self . body . local_decls [ local] . is_ref_to_static ( ) =>
123123 {
124- diagnosing = if access_place. projection . len ( ) == 1 {
125- PlaceAndReason :: StaticItem ( access_place_desc. clone ( ) , None )
124+ diagnostic = if access_place. projection . len ( ) == 1 {
125+ PlaceAndReason :: StaticItem ( access_place_desc, None )
126126 } else {
127- item_msg = access_place_desc;
128127 let local_info = self . body . local_decls [ local] . local_info ( ) ;
129128 if let LocalInfo :: StaticRef { def_id, .. } = * local_info {
130129 let static_name = & self . infcx . tcx . item_name ( def_id) ;
131- PlaceAndReason :: StaticItem ( item_msg . clone ( ) , Some ( static_name. to_string ( ) ) )
130+ PlaceAndReason :: StaticItem ( access_place_desc , Some ( static_name. to_string ( ) ) )
132131 } else {
133132 bug ! ( "is_ref_to_static return true, but not ref to static?" ) ;
134133 }
@@ -139,32 +138,29 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
139138 && proj_base. is_empty ( )
140139 && !self . upvars . is_empty ( )
141140 {
142- item_msg = access_place_desc;
143141 debug_assert ! ( self . body. local_decls[ ty:: CAPTURE_STRUCT_LOCAL ] . ty. is_ref( ) ) ;
144142 debug_assert ! ( is_closure_or_coroutine(
145143 the_place_err. ty( self . body, self . infcx. tcx) . ty
146144 ) ) ;
147145
148146 diagnostic = if self . is_upvar_field_projection ( access_place. as_ref ( ) ) . is_some ( )
149147 {
150- PlaceAndReason :: SelfCaptured ( item_msg . clone ( ) )
148+ PlaceAndReason :: SelfCaptured ( access_place_desc )
151149 } else {
152- PlaceAndReason :: UpvarCaptured ( item_msg . clone ( ) )
150+ PlaceAndReason :: UpvarCaptured ( access_place_desc )
153151 }
154152 } else {
155- let source = self . borrowed_content_source ( PlaceRef {
153+ let ptr_source = self . borrowed_content_source ( PlaceRef {
156154 local : the_place_err. local ,
157155 projection : proj_base,
158156 } ) ;
159- let diag_msg = source. describe_for_immutable_place ( self . infcx . tcx ) ;
160- diagnosing = if let Some ( desc) = self . describe_place ( access_place. as_ref ( ) ) {
161- item_msg = format ! ( "`{desc}`" ) ;
162- PlaceAndReason :: BehindPointer ( Some ( item_msg. clone ( ) ) , source, diag_msg)
163- } else {
164- PlaceAndReason :: BehindPointer ( None , source, diag_msg)
165- } ;
166-
167- opt_source = Some ( source) ;
157+ let name = ptr_source. describe_for_immutable_place ( self . infcx . tcx ) ;
158+ diagnostic = PlaceAndReason :: BehindPointer (
159+ self . describe_place_typed ( access_place. as_ref ( ) ) ,
160+ ptr_source,
161+ name,
162+ ) ;
163+ opt_source = Some ( ptr_source) ;
168164 }
169165 }
170166
@@ -184,8 +180,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
184180 }
185181
186182 debug ! (
187- "report_mutability_error: item_msg and reason is moved to a struct due to diagnosing migration: {:?}" ,
188- & diagnosing
183+ "report_mutability_error: item_msg and reason is moved to a struct due to diagnosing migration: [E0594] & [E0596], now they are typed value: {:?}" ,
184+ & diagnostic
189185 ) ;
190186
191187 // `act` and `acted_on` are strings that let us abstract over
0 commit comments