@@ -15,7 +15,7 @@ use rustc_infer::infer::{DefineOpaqueTypes, TyCtxtInferExt};
1515use rustc_infer:: traits:: Obligation ;
1616use rustc_middle:: ty:: adjustment:: CoerceUnsizedInfo ;
1717use rustc_middle:: ty:: { self , suggest_constraining_type_params, Ty , TyCtxt , TypeVisitableExt } ;
18- use rustc_span:: Span ;
18+ use rustc_span:: { Span , DUMMY_SP } ;
1919use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt ;
2020use rustc_trait_selection:: traits:: misc:: {
2121 type_allowed_to_implement_const_param_ty, type_allowed_to_implement_copy,
@@ -25,13 +25,14 @@ use rustc_trait_selection::traits::ObligationCtxt;
2525use rustc_trait_selection:: traits:: { self , ObligationCause } ;
2626use std:: collections:: BTreeMap ;
2727
28- pub fn check_trait (
29- tcx : TyCtxt < ' _ > ,
28+ pub fn check_trait < ' tcx > (
29+ tcx : TyCtxt < ' tcx > ,
3030 trait_def_id : DefId ,
3131 impl_def_id : LocalDefId ,
32+ impl_header : ty:: ImplTraitHeader < ' tcx > ,
3233) -> Result < ( ) , ErrorGuaranteed > {
3334 let lang_items = tcx. lang_items ( ) ;
34- let checker = Checker { tcx, trait_def_id, impl_def_id } ;
35+ let checker = Checker { tcx, trait_def_id, impl_def_id, impl_header } ;
3536 let mut res = checker. check ( lang_items. drop_trait ( ) , visit_implementation_of_drop) ;
3637 res = res. and ( checker. check ( lang_items. copy_trait ( ) , visit_implementation_of_copy) ) ;
3738 res = res. and (
@@ -50,24 +51,25 @@ struct Checker<'tcx> {
5051 tcx : TyCtxt < ' tcx > ,
5152 trait_def_id : DefId ,
5253 impl_def_id : LocalDefId ,
54+ impl_header : ty:: ImplTraitHeader < ' tcx > ,
5355}
5456
5557impl < ' tcx > Checker < ' tcx > {
5658 fn check (
5759 & self ,
5860 trait_def_id : Option < DefId > ,
59- f : impl FnOnce ( TyCtxt < ' tcx > , LocalDefId ) -> Result < ( ) , ErrorGuaranteed > ,
61+ f : impl FnOnce ( & Self ) -> Result < ( ) , ErrorGuaranteed > ,
6062 ) -> Result < ( ) , ErrorGuaranteed > {
61- if Some ( self . trait_def_id ) == trait_def_id { f ( self . tcx , self . impl_def_id ) } else { Ok ( ( ) ) }
63+ if Some ( self . trait_def_id ) == trait_def_id { f ( self ) } else { Ok ( ( ) ) }
6264 }
6365}
6466
65- fn visit_implementation_of_drop (
66- tcx : TyCtxt < ' _ > ,
67- impl_did : LocalDefId ,
68- ) -> Result < ( ) , ErrorGuaranteed > {
67+ fn visit_implementation_of_drop ( checker : & Checker < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
68+ let tcx = checker . tcx ;
69+ let header = checker . impl_header ;
70+ let impl_did = checker . impl_def_id ;
6971 // Destructors only work on local ADT types.
70- match tcx . type_of ( impl_did ) . instantiate_identity ( ) . kind ( ) {
72+ match header . trait_ref . self_ty ( ) . kind ( ) {
7173 ty:: Adt ( def, _) if def. did ( ) . is_local ( ) => return Ok ( ( ) ) ,
7274 ty:: Error ( _) => return Ok ( ( ) ) ,
7375 _ => { }
@@ -78,70 +80,72 @@ fn visit_implementation_of_drop(
7880 Err ( tcx. dcx ( ) . emit_err ( errors:: DropImplOnWrongItem { span : impl_. self_ty . span } ) )
7981}
8082
81- fn visit_implementation_of_copy (
82- tcx : TyCtxt < ' _ > ,
83- impl_did : LocalDefId ,
84- ) -> Result < ( ) , ErrorGuaranteed > {
83+ fn visit_implementation_of_copy ( checker : & Checker < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
84+ let tcx = checker . tcx ;
85+ let impl_header = checker . impl_header ;
86+ let impl_did = checker . impl_def_id ;
8587 debug ! ( "visit_implementation_of_copy: impl_did={:?}" , impl_did) ;
8688
87- let self_type = tcx . type_of ( impl_did ) . instantiate_identity ( ) ;
89+ let self_type = impl_header . trait_ref . self_ty ( ) ;
8890 debug ! ( "visit_implementation_of_copy: self_type={:?} (bound)" , self_type) ;
8991
9092 let param_env = tcx. param_env ( impl_did) ;
9193 assert ! ( !self_type. has_escaping_bound_vars( ) ) ;
9294
9395 debug ! ( "visit_implementation_of_copy: self_type={:?} (free)" , self_type) ;
9496
95- if let ty:: ImplPolarity :: Negative = tcx . impl_polarity ( impl_did ) {
97+ if let ty:: ImplPolarity :: Negative = impl_header . polarity {
9698 return Ok ( ( ) ) ;
9799 }
98- let span = tcx. hir ( ) . expect_item ( impl_did) . expect_impl ( ) . self_ty . span ;
99100
100- let cause = traits:: ObligationCause :: misc ( span , impl_did) ;
101+ let cause = traits:: ObligationCause :: misc ( DUMMY_SP , impl_did) ;
101102 match type_allowed_to_implement_copy ( tcx, param_env, self_type, cause) {
102103 Ok ( ( ) ) => Ok ( ( ) ) ,
103104 Err ( CopyImplementationError :: InfringingFields ( fields) ) => {
105+ let span = tcx. hir ( ) . expect_item ( impl_did) . expect_impl ( ) . self_ty . span ;
104106 Err ( infringing_fields_error ( tcx, fields, LangItem :: Copy , impl_did, span) )
105107 }
106108 Err ( CopyImplementationError :: NotAnAdt ) => {
109+ let span = tcx. hir ( ) . expect_item ( impl_did) . expect_impl ( ) . self_ty . span ;
107110 Err ( tcx. dcx ( ) . emit_err ( errors:: CopyImplOnNonAdt { span } ) )
108111 }
109112 Err ( CopyImplementationError :: HasDestructor ) => {
113+ let span = tcx. hir ( ) . expect_item ( impl_did) . expect_impl ( ) . self_ty . span ;
110114 Err ( tcx. dcx ( ) . emit_err ( errors:: CopyImplOnTypeWithDtor { span } ) )
111115 }
112116 }
113117}
114118
115- fn visit_implementation_of_const_param_ty (
116- tcx : TyCtxt < ' _ > ,
117- impl_did : LocalDefId ,
118- ) -> Result < ( ) , ErrorGuaranteed > {
119- let self_type = tcx . type_of ( impl_did ) . instantiate_identity ( ) ;
119+ fn visit_implementation_of_const_param_ty ( checker : & Checker < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
120+ let tcx = checker . tcx ;
121+ let header = checker . impl_header ;
122+ let impl_did = checker . impl_def_id ;
123+ let self_type = header . trait_ref . self_ty ( ) ;
120124 assert ! ( !self_type. has_escaping_bound_vars( ) ) ;
121125
122126 let param_env = tcx. param_env ( impl_did) ;
123127
124- if let ty:: ImplPolarity :: Negative = tcx . impl_polarity ( impl_did ) {
128+ if let ty:: ImplPolarity :: Negative = header . polarity {
125129 return Ok ( ( ) ) ;
126130 }
127- let span = tcx. hir ( ) . expect_item ( impl_did) . expect_impl ( ) . self_ty . span ;
128131
129- let cause = traits:: ObligationCause :: misc ( span , impl_did) ;
132+ let cause = traits:: ObligationCause :: misc ( DUMMY_SP , impl_did) ;
130133 match type_allowed_to_implement_const_param_ty ( tcx, param_env, self_type, cause) {
131134 Ok ( ( ) ) => Ok ( ( ) ) ,
132135 Err ( ConstParamTyImplementationError :: InfrigingFields ( fields) ) => {
136+ let span = tcx. hir ( ) . expect_item ( impl_did) . expect_impl ( ) . self_ty . span ;
133137 Err ( infringing_fields_error ( tcx, fields, LangItem :: ConstParamTy , impl_did, span) )
134138 }
135139 Err ( ConstParamTyImplementationError :: NotAnAdtOrBuiltinAllowed ) => {
140+ let span = tcx. hir ( ) . expect_item ( impl_did) . expect_impl ( ) . self_ty . span ;
136141 Err ( tcx. dcx ( ) . emit_err ( errors:: ConstParamTyImplOnNonAdt { span } ) )
137142 }
138143 }
139144}
140145
141- fn visit_implementation_of_coerce_unsized (
142- tcx : TyCtxt < ' _ > ,
143- impl_did : LocalDefId ,
144- ) -> Result < ( ) , ErrorGuaranteed > {
146+ fn visit_implementation_of_coerce_unsized ( checker : & Checker < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
147+ let tcx = checker. tcx ;
148+ let impl_did = checker. impl_def_id ;
145149 debug ! ( "visit_implementation_of_coerce_unsized: impl_did={:?}" , impl_did) ;
146150
147151 // Just compute this for the side-effects, in particular reporting
@@ -151,20 +155,20 @@ fn visit_implementation_of_coerce_unsized(
151155 tcx. at ( span) . ensure ( ) . coerce_unsized_info ( impl_did)
152156}
153157
154- fn visit_implementation_of_dispatch_from_dyn (
155- tcx : TyCtxt < ' _ > ,
156- impl_did : LocalDefId ,
157- ) -> Result < ( ) , ErrorGuaranteed > {
158+ fn visit_implementation_of_dispatch_from_dyn ( checker : & Checker < ' _ > ) -> Result < ( ) , ErrorGuaranteed > {
159+ let tcx = checker. tcx ;
160+ let header = checker. impl_header ;
161+ let impl_did = checker. impl_def_id ;
162+ let trait_ref = header. trait_ref ;
158163 debug ! ( "visit_implementation_of_dispatch_from_dyn: impl_did={:?}" , impl_did) ;
159164
160165 let span = tcx. def_span ( impl_did) ;
161166
162167 let dispatch_from_dyn_trait = tcx. require_lang_item ( LangItem :: DispatchFromDyn , Some ( span) ) ;
163168
164- let source = tcx . type_of ( impl_did ) . instantiate_identity ( ) ;
169+ let source = trait_ref . self_ty ( ) ;
165170 assert ! ( !source. has_escaping_bound_vars( ) ) ;
166171 let target = {
167- let trait_ref = tcx. impl_trait_ref ( impl_did) . unwrap ( ) . instantiate_identity ( ) ;
168172 assert_eq ! ( trait_ref. def_id, dispatch_from_dyn_trait) ;
169173
170174 trait_ref. args . type_at ( 1 )
0 commit comments