@@ -3,8 +3,6 @@ use rustc::traits::auto_trait as auto;
3
3
use rustc:: ty:: { self , TypeFoldable } ;
4
4
use std:: fmt:: Debug ;
5
5
6
- use self :: def_ctor:: { get_def_from_def_id, get_def_from_hir_id} ;
7
-
8
6
use super :: * ;
9
7
10
8
pub struct AutoTraitFinder < ' a , ' tcx > {
@@ -19,103 +17,71 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
19
17
AutoTraitFinder { cx, f }
20
18
}
21
19
22
- pub fn get_with_def_id ( & self , def_id : DefId ) -> Vec < Item > {
23
- get_def_from_def_id ( & self . cx , def_id, & |def_ctor| {
24
- self . get_auto_trait_impls ( def_id, & def_ctor, None )
25
- } )
26
- }
27
-
28
- pub fn get_with_hir_id ( & self , id : hir:: HirId , name : String ) -> Vec < Item > {
29
- get_def_from_hir_id ( & self . cx , id, name, & |def_ctor, name| {
30
- let did = self . cx . tcx . hir ( ) . local_def_id_from_hir_id ( id) ;
31
- self . get_auto_trait_impls ( did, & def_ctor, Some ( name) )
32
- } )
33
- }
34
-
35
- pub fn get_auto_trait_impls < F > (
20
+ // FIXME(eddyb) figure out a better way to pass information about
21
+ // parametrization of `ty` than `param_env_def_id`.
22
+ pub fn get_auto_trait_impls (
36
23
& self ,
37
- def_id : DefId ,
38
- def_ctor : & F ,
39
- name : Option < String > ,
40
- ) -> Vec < Item >
41
- where F : Fn ( DefId ) -> Def {
42
- if self . cx
43
- . tcx
44
- . get_attrs ( def_id)
45
- . lists ( "doc" )
46
- . has_word ( "hidden" )
47
- {
48
- debug ! (
49
- "get_auto_trait_impls(def_id={:?}, def_ctor=...): item has doc('hidden'), \
50
- aborting",
51
- def_id
52
- ) ;
53
- return Vec :: new ( ) ;
54
- }
55
-
56
- let tcx = self . cx . tcx ;
57
- let generics = self . cx . tcx . generics_of ( def_id) ;
24
+ ty : Ty < ' tcx > ,
25
+ param_env_def_id : DefId ,
26
+ ) -> Vec < Item > {
27
+ let generics = self . cx . tcx . generics_of ( param_env_def_id) ;
58
28
59
29
debug ! (
60
- "get_auto_trait_impls(def_id ={:?}, def_ctor=... , generics={:?}" ,
61
- def_id , generics
30
+ "get_auto_trait_impls(param_env_def_id ={:?}, generics={:?}" ,
31
+ param_env_def_id , generics
62
32
) ;
63
33
let auto_traits: Vec < _ > = self . cx
64
34
. send_trait
65
35
. and_then ( |send_trait| {
66
36
self . get_auto_trait_impl_for (
67
- def_id,
68
- name. clone ( ) ,
69
- generics. clone ( ) ,
70
- def_ctor,
37
+ ty,
38
+ param_env_def_id,
39
+ generics,
71
40
send_trait,
72
41
)
73
42
} )
74
43
. into_iter ( )
75
44
. chain ( self . get_auto_trait_impl_for (
76
- def_id,
77
- name,
78
- generics. clone ( ) ,
79
- def_ctor,
80
- tcx. require_lang_item ( lang_items:: SyncTraitLangItem ) ,
45
+ ty,
46
+ param_env_def_id,
47
+ generics,
48
+ self . cx . tcx . require_lang_item ( lang_items:: SyncTraitLangItem ) ,
81
49
) . into_iter ( ) )
82
50
. collect ( ) ;
83
51
84
52
debug ! (
85
53
"get_auto_traits: type {:?} auto_traits {:?}" ,
86
- def_id , auto_traits
54
+ param_env_def_id , auto_traits
87
55
) ;
88
56
auto_traits
89
57
}
90
58
91
- fn get_auto_trait_impl_for < F > (
59
+ fn get_auto_trait_impl_for (
92
60
& self ,
93
- def_id : DefId ,
94
- name : Option < String > ,
95
- generics : ty:: Generics ,
96
- def_ctor : & F ,
61
+ ty : Ty < ' tcx > ,
62
+ param_env_def_id : DefId ,
63
+ generics : & ty:: Generics ,
97
64
trait_def_id : DefId ,
98
- ) -> Option < Item >
99
- where F : Fn ( DefId ) -> Def {
65
+ ) -> Option < Item > {
100
66
if !self . cx
101
67
. generated_synthetics
102
68
. borrow_mut ( )
103
- . insert ( ( def_id , trait_def_id) )
69
+ . insert ( ( param_env_def_id , trait_def_id) )
104
70
{
105
71
debug ! (
106
- "get_auto_trait_impl_for(def_id ={:?}, generics={:?}, def_ctor=... , \
72
+ "get_auto_trait_impl_for(param_env_def_id ={:?}, generics={:?}, \
107
73
trait_def_id={:?}): already generated, aborting",
108
- def_id , generics, trait_def_id
74
+ param_env_def_id , generics, trait_def_id
109
75
) ;
110
76
return None ;
111
77
}
112
78
113
- let result = self . find_auto_trait_generics ( def_id , trait_def_id, & generics) ;
79
+ let result = self . find_auto_trait_generics ( ty , param_env_def_id , trait_def_id, & generics) ;
114
80
115
81
if result. is_auto ( ) {
116
- let trait_ = hir :: TraitRef {
117
- path : get_path_for_type ( self . cx . tcx , trait_def_id, hir :: def :: Def :: Trait ) ,
118
- hir_ref_id : hir :: DUMMY_HIR_ID ,
82
+ let trait_ref = ty :: TraitRef {
83
+ def_id : trait_def_id,
84
+ substs : self . cx . tcx . mk_substs_trait ( ty , & [ ] ) ,
119
85
} ;
120
86
121
87
let polarity;
@@ -140,7 +106,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
140
106
// Instead, we generate `impl !Send for Foo<T>`, which better
141
107
// expresses the fact that `Foo<T>` never implements `Send`,
142
108
// regardless of the choice of `T`.
143
- let real_generics = ( & generics, & Default :: default ( ) ) ;
109
+ let real_generics = ( generics, & Default :: default ( ) ) ;
144
110
145
111
// Clean the generics, but ignore the '?Sized' bounds generated
146
112
// by the `Clean` impl
@@ -153,22 +119,20 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
153
119
}
154
120
_ => unreachable ! ( ) ,
155
121
} ;
156
- let real_name = name. map ( |name| Ident :: from_str ( & name) ) ;
157
- let ty = self . cx . get_real_ty ( def_id, def_ctor, & real_name, & generics) ;
158
122
159
123
return Some ( Item {
160
124
source : Span :: empty ( ) ,
161
125
name : None ,
162
126
attrs : Default :: default ( ) ,
163
127
visibility : None ,
164
- def_id : self . cx . next_def_id ( def_id . krate ) ,
128
+ def_id : self . cx . next_def_id ( param_env_def_id . krate ) ,
165
129
stability : None ,
166
130
deprecation : None ,
167
131
inner : ImplItem ( Impl {
168
132
unsafety : hir:: Unsafety :: Normal ,
169
133
generics : new_generics,
170
134
provided_trait_methods : Default :: default ( ) ,
171
- trait_ : Some ( trait_ . clean ( self . cx ) ) ,
135
+ trait_ : Some ( trait_ref . clean ( self . cx ) . get_trait_type ( ) . unwrap ( ) ) ,
172
136
for_ : ty. clean ( self . cx ) ,
173
137
items : Vec :: new ( ) ,
174
138
polarity,
@@ -182,11 +146,12 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
182
146
183
147
fn find_auto_trait_generics (
184
148
& self ,
185
- did : DefId ,
149
+ ty : Ty < ' tcx > ,
150
+ param_env_def_id : DefId ,
186
151
trait_did : DefId ,
187
152
generics : & ty:: Generics ,
188
153
) -> AutoTraitResult {
189
- match self . f . find_auto_trait_generics ( did , trait_did, generics,
154
+ match self . f . find_auto_trait_generics ( ty , param_env_def_id , trait_did, generics,
190
155
|infcx, mut info| {
191
156
let region_data = info. region_data ;
192
157
let names_map =
@@ -198,17 +163,17 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
198
163
self . handle_lifetimes ( & region_data, & names_map) ;
199
164
let new_generics = self . param_env_to_generics (
200
165
infcx. tcx ,
201
- did ,
166
+ param_env_def_id ,
202
167
info. full_user_env ,
203
- generics. clone ( ) ,
168
+ generics,
204
169
lifetime_predicates,
205
170
info. vid_to_region ,
206
171
) ;
207
172
208
173
debug ! (
209
- "find_auto_trait_generics(did ={:?}, trait_did={:?}, generics={:?}): \
174
+ "find_auto_trait_generics(ty ={:?}, trait_did={:?}, generics={:?}): \
210
175
finished with {:?}",
211
- did , trait_did, generics, new_generics
176
+ ty , trait_did, generics, new_generics
212
177
) ;
213
178
214
179
new_generics
@@ -530,16 +495,16 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
530
495
fn param_env_to_generics < ' b , ' c , ' cx > (
531
496
& self ,
532
497
tcx : TyCtxt < ' b , ' c , ' cx > ,
533
- did : DefId ,
498
+ param_env_def_id : DefId ,
534
499
param_env : ty:: ParamEnv < ' cx > ,
535
- type_generics : ty:: Generics ,
500
+ type_generics : & ty:: Generics ,
536
501
mut existing_predicates : Vec < WherePredicate > ,
537
502
vid_to_region : FxHashMap < ty:: RegionVid , ty:: Region < ' cx > > ,
538
503
) -> Generics {
539
504
debug ! (
540
- "param_env_to_generics(did ={:?}, param_env={:?}, type_generics={:?}, \
505
+ "param_env_to_generics(param_env_def_id ={:?}, param_env={:?}, type_generics={:?}, \
541
506
existing_predicates={:?})",
542
- did , param_env, type_generics, existing_predicates
507
+ param_env_def_id , param_env, type_generics, existing_predicates
543
508
) ;
544
509
545
510
// The `Sized` trait must be handled specially, since we only display it when
@@ -553,7 +518,8 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
553
518
tcx,
554
519
} ;
555
520
556
- let orig_bounds: FxHashSet < _ > = self . cx . tcx . param_env ( did) . caller_bounds . iter ( ) . collect ( ) ;
521
+ let orig_bounds: FxHashSet < _ > =
522
+ self . cx . tcx . param_env ( param_env_def_id) . caller_bounds . iter ( ) . collect ( ) ;
557
523
let clean_where_predicates = param_env
558
524
. caller_bounds
559
525
. iter ( )
@@ -568,7 +534,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
568
534
( replaced. clone ( ) , replaced. clean ( self . cx ) )
569
535
} ) ;
570
536
571
- let full_generics = ( & type_generics, & tcx. explicit_predicates_of ( did ) ) ;
537
+ let full_generics = ( type_generics, & tcx. explicit_predicates_of ( param_env_def_id ) ) ;
572
538
let Generics {
573
539
params : mut generic_params,
574
540
..
@@ -757,10 +723,14 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
757
723
. or_default ( )
758
724
. insert ( * trait_. clone ( ) ) ;
759
725
}
760
- _ => panic ! ( "Unexpected trait {:?} for {:?}" , trait_, did) ,
726
+ _ => panic ! (
727
+ "Unexpected trait {:?} for {:?}" ,
728
+ trait_,
729
+ param_env_def_id,
730
+ ) ,
761
731
}
762
732
}
763
- _ => panic ! ( "Unexpected LHS {:?} for {:?}" , lhs, did ) ,
733
+ _ => panic ! ( "Unexpected LHS {:?} for {:?}" , lhs, param_env_def_id ) ,
764
734
}
765
735
}
766
736
} ;
0 commit comments