1
1
use crate :: infer:: { InferCtxt , TyCtxtInferExt } ;
2
2
use crate :: traits:: ObligationCause ;
3
- use crate :: traits:: { self , ConstPatternStructural , TraitEngine } ;
3
+ use crate :: traits:: { self , TraitEngine } ;
4
4
5
5
use rustc_data_structures:: fx:: FxHashSet ;
6
6
use rustc_hir as hir;
7
7
use rustc_hir:: lang_items:: { StructuralPeqTraitLangItem , StructuralTeqTraitLangItem } ;
8
+ use rustc_middle:: ty:: query:: Providers ;
8
9
use rustc_middle:: ty:: { self , AdtDef , Ty , TyCtxt , TypeFoldable , TypeVisitor } ;
9
10
use rustc_span:: Span ;
10
11
@@ -41,14 +42,14 @@ pub enum NonStructuralMatchTy<'tcx> {
41
42
/// that arose when the requirement was not enforced completely, see
42
43
/// Rust RFC 1445, rust-lang/rust#61188, and rust-lang/rust#62307.
43
44
pub fn search_for_structural_match_violation < ' tcx > (
44
- id : hir:: HirId ,
45
- span : Span ,
45
+ _id : hir:: HirId ,
46
+ _span : Span ,
46
47
tcx : TyCtxt < ' tcx > ,
47
48
ty : Ty < ' tcx > ,
48
49
) -> Option < NonStructuralMatchTy < ' tcx > > {
49
50
// FIXME: we should instead pass in an `infcx` from the outside.
50
51
tcx. infer_ctxt ( ) . enter ( |infcx| {
51
- let mut search = Search { id , span , infcx, found : None , seen : FxHashSet :: default ( ) } ;
52
+ let mut search = Search { infcx, found : None , seen : FxHashSet :: default ( ) } ;
52
53
ty. visit_with ( & mut search) ;
53
54
search. found
54
55
} )
@@ -62,26 +63,25 @@ pub fn search_for_structural_match_violation<'tcx>(
62
63
/// Note that this does *not* recursively check if the substructure of `adt_ty`
63
64
/// implements the traits.
64
65
pub fn type_marked_structural (
65
- id : hir:: HirId ,
66
- span : Span ,
67
66
infcx : & InferCtxt < ' _ , ' tcx > ,
68
67
adt_ty : Ty < ' tcx > ,
68
+ cause : ObligationCause < ' tcx > ,
69
69
) -> bool {
70
70
let mut fulfillment_cx = traits:: FulfillmentContext :: new ( ) ;
71
- let cause = ObligationCause :: new ( span, id, ConstPatternStructural ) ;
72
71
// require `#[derive(PartialEq)]`
73
- let structural_peq_def_id = infcx. tcx . require_lang_item ( StructuralPeqTraitLangItem , Some ( span) ) ;
72
+ let structural_peq_def_id =
73
+ infcx. tcx . require_lang_item ( StructuralPeqTraitLangItem , Some ( cause. span ) ) ;
74
74
fulfillment_cx. register_bound (
75
75
infcx,
76
76
ty:: ParamEnv :: empty ( ) ,
77
77
adt_ty,
78
78
structural_peq_def_id,
79
- cause,
79
+ cause. clone ( ) ,
80
80
) ;
81
81
// for now, require `#[derive(Eq)]`. (Doing so is a hack to work around
82
82
// the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.)
83
- let cause = ObligationCause :: new ( span , id , ConstPatternStructural ) ;
84
- let structural_teq_def_id = infcx. tcx . require_lang_item ( StructuralTeqTraitLangItem , Some ( span) ) ;
83
+ let structural_teq_def_id =
84
+ infcx. tcx . require_lang_item ( StructuralTeqTraitLangItem , Some ( cause . span ) ) ;
85
85
fulfillment_cx. register_bound (
86
86
infcx,
87
87
ty:: ParamEnv :: empty ( ) ,
@@ -106,9 +106,6 @@ pub fn type_marked_structural(
106
106
/// find instances of ADTs (specifically structs or enums) that do not implement
107
107
/// the structural-match traits (`StructuralPartialEq` and `StructuralEq`).
108
108
struct Search < ' a , ' tcx > {
109
- id : hir:: HirId ,
110
- span : Span ,
111
-
112
109
infcx : InferCtxt < ' a , ' tcx > ,
113
110
114
111
/// Records first ADT that does not implement a structural-match trait.
@@ -125,7 +122,7 @@ impl Search<'a, 'tcx> {
125
122
}
126
123
127
124
fn type_marked_structural ( & self , adt_ty : Ty < ' tcx > ) -> bool {
128
- type_marked_structural ( self . id , self . span , & self . infcx , adt_ty )
125
+ adt_ty . is_structural_eq_shallow ( self . tcx ( ) )
129
126
}
130
127
}
131
128
@@ -220,3 +217,12 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
220
217
false
221
218
}
222
219
}
220
+
221
+ pub fn provide ( providers : & mut Providers < ' _ > ) {
222
+ providers. is_structural_eq_raw = |tcx, ty| {
223
+ tcx. infer_ctxt ( ) . enter ( |infcx| {
224
+ let cause = ObligationCause :: dummy ( ) ;
225
+ type_marked_structural ( & infcx, ty, cause)
226
+ } )
227
+ } ;
228
+ }
0 commit comments