@@ -21,6 +21,8 @@ use crate::session_diagnostics::{BorrowMutUpvarLable, FnMutBumpFn, OnModifyTy};
21
21
use crate :: util:: FindAssignments ;
22
22
use crate :: MirBorrowckCtxt ;
23
23
24
+ use super :: DescribedPlace ;
25
+
24
26
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
25
27
pub ( crate ) enum AccessKind {
26
28
MutableBorrow ,
@@ -30,17 +32,17 @@ pub(crate) enum AccessKind {
30
32
#[ derive( Debug ) ]
31
33
pub ( crate ) enum PlaceAndReason < ' a > {
32
34
// place, name
33
- DeclaredImmute ( String , Option < String > ) ,
35
+ DeclaredImmute ( DescribedPlace , Option < String > ) ,
34
36
// place
35
- InPatternGuard ( String ) ,
37
+ InPatternGuard ( DescribedPlace ) ,
36
38
// place, name
37
- StaticItem ( String , Option < String > ) ,
39
+ StaticItem ( DescribedPlace , Option < String > ) ,
38
40
// place
39
- UpvarCaptured ( String ) ,
41
+ UpvarCaptured ( DescribedPlace ) ,
40
42
// place
41
- SelfCaptured ( String ) ,
43
+ SelfCaptured ( DescribedPlace ) ,
42
44
// place, pointer_type, name
43
- BehindPointer ( Option < String > , BorrowedContentSource < ' a > , Option < String > ) ,
45
+ BehindPointer ( DescribedPlace , BorrowedContentSource < ' a > , Option < String > ) ,
44
46
}
45
47
46
48
impl < ' a , ' tcx > MirBorrowckCtxt < ' a , ' tcx > {
@@ -60,20 +62,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
60
62
) ;
61
63
62
64
let mut err;
63
- let item_msg;
64
65
let diagnostic: PlaceAndReason < ' _ > ;
65
66
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) ;
68
71
69
72
match the_place_err {
70
73
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 )
74
76
} else {
75
77
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 ( ) ) )
77
79
} ;
78
80
}
79
81
@@ -101,34 +103,31 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
101
103
// If we deref an immutable ref then the suggestion here doesn't help.
102
104
return ;
103
105
} else {
104
- item_msg = access_place_desc;
105
106
diagnostic = if self . is_upvar_field_projection ( access_place. as_ref ( ) ) . is_some ( )
106
107
{
107
- PlaceAndReason :: DeclaredImmute ( item_msg . clone ( ) , None )
108
+ PlaceAndReason :: DeclaredImmute ( access_place_desc , None )
108
109
} else {
109
110
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) )
111
112
}
112
113
}
113
114
}
114
115
115
116
PlaceRef { local, projection : [ ProjectionElem :: Deref ] }
116
117
if self . body . local_decls [ local] . is_ref_for_guard ( ) =>
117
118
{
118
- item_msg = access_place_desc;
119
119
diagnostic = PlaceAndReason :: InPatternGuard ( access_place_desc)
120
120
}
121
121
PlaceRef { local, projection : [ ProjectionElem :: Deref ] }
122
122
if self . body . local_decls [ local] . is_ref_to_static ( ) =>
123
123
{
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 )
126
126
} else {
127
- item_msg = access_place_desc;
128
127
let local_info = self . body . local_decls [ local] . local_info ( ) ;
129
128
if let LocalInfo :: StaticRef { def_id, .. } = * local_info {
130
129
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 ( ) ) )
132
131
} else {
133
132
bug ! ( "is_ref_to_static return true, but not ref to static?" ) ;
134
133
}
@@ -139,32 +138,29 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
139
138
&& proj_base. is_empty ( )
140
139
&& !self . upvars . is_empty ( )
141
140
{
142
- item_msg = access_place_desc;
143
141
debug_assert ! ( self . body. local_decls[ ty:: CAPTURE_STRUCT_LOCAL ] . ty. is_ref( ) ) ;
144
142
debug_assert ! ( is_closure_or_coroutine(
145
143
the_place_err. ty( self . body, self . infcx. tcx) . ty
146
144
) ) ;
147
145
148
146
diagnostic = if self . is_upvar_field_projection ( access_place. as_ref ( ) ) . is_some ( )
149
147
{
150
- PlaceAndReason :: SelfCaptured ( item_msg . clone ( ) )
148
+ PlaceAndReason :: SelfCaptured ( access_place_desc )
151
149
} else {
152
- PlaceAndReason :: UpvarCaptured ( item_msg . clone ( ) )
150
+ PlaceAndReason :: UpvarCaptured ( access_place_desc )
153
151
}
154
152
} else {
155
- let source = self . borrowed_content_source ( PlaceRef {
153
+ let ptr_source = self . borrowed_content_source ( PlaceRef {
156
154
local : the_place_err. local ,
157
155
projection : proj_base,
158
156
} ) ;
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) ;
168
164
}
169
165
}
170
166
@@ -184,8 +180,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
184
180
}
185
181
186
182
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
189
185
) ;
190
186
191
187
// `act` and `acted_on` are strings that let us abstract over
0 commit comments