@@ -7,7 +7,7 @@ use rustc_hir as hir;
7
7
use rustc_hir:: def_id:: DefId ;
8
8
use rustc_infer:: infer:: TyCtxtInferExt ;
9
9
use rustc_infer:: traits:: { ImplSource , Obligation , ObligationCause } ;
10
- use rustc_middle:: mir;
10
+ use rustc_middle:: mir:: { self , CallSource } ;
11
11
use rustc_middle:: ty:: print:: with_no_trimmed_paths;
12
12
use rustc_middle:: ty:: subst:: { GenericArgKind , SubstsRef } ;
13
13
use rustc_middle:: ty:: { suggest_constraining_type_param, Adt , Closure , FnDef , FnPtr , Param , Ty } ;
@@ -100,7 +100,7 @@ pub struct FnCallNonConst<'tcx> {
100
100
pub callee : DefId ,
101
101
pub substs : SubstsRef < ' tcx > ,
102
102
pub span : Span ,
103
- pub from_hir_call : bool ,
103
+ pub call_source : CallSource ,
104
104
pub feature : Option < Symbol > ,
105
105
}
106
106
@@ -110,7 +110,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
110
110
ccx : & ConstCx < ' _ , ' tcx > ,
111
111
_: Span ,
112
112
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
113
- let FnCallNonConst { caller, callee, substs, span, from_hir_call , feature } = * self ;
113
+ let FnCallNonConst { caller, callee, substs, span, call_source , feature } = * self ;
114
114
let ConstCx { tcx, param_env, .. } = * ccx;
115
115
116
116
let diag_trait = |err, self_ty : Ty < ' _ > , trait_id| {
@@ -157,7 +157,8 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
157
157
}
158
158
} ;
159
159
160
- let call_kind = call_kind ( tcx, ccx. param_env , callee, substs, span, from_hir_call, None ) ;
160
+ let call_kind =
161
+ call_kind ( tcx, ccx. param_env , callee, substs, span, call_source. from_hir_call ( ) , None ) ;
161
162
162
163
debug ! ( ?call_kind) ;
163
164
@@ -219,48 +220,59 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
219
220
err
220
221
}
221
222
CallKind :: Operator { trait_id, self_ty, .. } => {
222
- let mut sugg = None ;
223
-
224
- if Some ( trait_id) == ccx. tcx . lang_items ( ) . eq_trait ( ) {
225
- match ( substs[ 0 ] . unpack ( ) , substs[ 1 ] . unpack ( ) ) {
226
- ( GenericArgKind :: Type ( self_ty) , GenericArgKind :: Type ( rhs_ty) )
227
- if self_ty == rhs_ty
228
- && self_ty. is_ref ( )
229
- && self_ty. peel_refs ( ) . is_primitive ( ) =>
230
- {
231
- let mut num_refs = 0 ;
232
- let mut tmp_ty = self_ty;
233
- while let rustc_middle:: ty:: Ref ( _, inner_ty, _) = tmp_ty. kind ( ) {
234
- num_refs += 1 ;
235
- tmp_ty = * inner_ty;
236
- }
237
- let deref = "*" . repeat ( num_refs) ;
238
-
239
- if let Ok ( call_str) = ccx. tcx . sess . source_map ( ) . span_to_snippet ( span) {
240
- if let Some ( eq_idx) = call_str. find ( "==" ) {
241
- if let Some ( rhs_idx) =
242
- call_str[ ( eq_idx + 2 ) ..] . find ( |c : char | !c. is_whitespace ( ) )
243
- {
244
- let rhs_pos =
245
- span. lo ( ) + BytePos :: from_usize ( eq_idx + 2 + rhs_idx) ;
246
- let rhs_span = span. with_lo ( rhs_pos) . with_hi ( rhs_pos) ;
247
- sugg = Some ( errors:: ConsiderDereferencing {
248
- deref,
249
- span : span. shrink_to_lo ( ) ,
250
- rhs_span,
251
- } ) ;
223
+ let mut err = if let CallSource :: MatchCmp = call_source {
224
+ tcx. sess . create_err ( errors:: NonConstMatchEq {
225
+ span,
226
+ kind : ccx. const_kind ( ) ,
227
+ ty : self_ty,
228
+ } )
229
+ } else {
230
+ let mut sugg = None ;
231
+
232
+ if Some ( trait_id) == ccx. tcx . lang_items ( ) . eq_trait ( ) {
233
+ match ( substs[ 0 ] . unpack ( ) , substs[ 1 ] . unpack ( ) ) {
234
+ ( GenericArgKind :: Type ( self_ty) , GenericArgKind :: Type ( rhs_ty) )
235
+ if self_ty == rhs_ty
236
+ && self_ty. is_ref ( )
237
+ && self_ty. peel_refs ( ) . is_primitive ( ) =>
238
+ {
239
+ let mut num_refs = 0 ;
240
+ let mut tmp_ty = self_ty;
241
+ while let rustc_middle:: ty:: Ref ( _, inner_ty, _) = tmp_ty. kind ( ) {
242
+ num_refs += 1 ;
243
+ tmp_ty = * inner_ty;
244
+ }
245
+ let deref = "*" . repeat ( num_refs) ;
246
+
247
+ if let Ok ( call_str) =
248
+ ccx. tcx . sess . source_map ( ) . span_to_snippet ( span)
249
+ {
250
+ if let Some ( eq_idx) = call_str. find ( "==" ) {
251
+ if let Some ( rhs_idx) = call_str[ ( eq_idx + 2 ) ..]
252
+ . find ( |c : char | !c. is_whitespace ( ) )
253
+ {
254
+ let rhs_pos = span. lo ( )
255
+ + BytePos :: from_usize ( eq_idx + 2 + rhs_idx) ;
256
+ let rhs_span = span. with_lo ( rhs_pos) . with_hi ( rhs_pos) ;
257
+ sugg = Some ( errors:: ConsiderDereferencing {
258
+ deref,
259
+ span : span. shrink_to_lo ( ) ,
260
+ rhs_span,
261
+ } ) ;
262
+ }
252
263
}
253
264
}
254
265
}
266
+ _ => { }
255
267
}
256
- _ => { }
257
268
}
258
- }
259
- let mut err = tcx. sess . create_err ( errors:: NonConstOperator {
260
- span,
261
- kind : ccx. const_kind ( ) ,
262
- sugg,
263
- } ) ;
269
+ tcx. sess . create_err ( errors:: NonConstOperator {
270
+ span,
271
+ kind : ccx. const_kind ( ) ,
272
+ sugg,
273
+ } )
274
+ } ;
275
+
264
276
diag_trait ( & mut err, self_ty, trait_id) ;
265
277
err
266
278
}
0 commit comments