@@ -8,20 +8,17 @@ use rustc_hir::{
88 self as hir, AmbigArg , GenericBound , GenericParam , GenericParamKind , Item , ItemKind , Lifetime ,
99 LifetimeName , LifetimeParamKind , MissingLifetimeKind , Node , TyKind ,
1010} ;
11- use rustc_middle:: ty:: {
12- self , AssocItemContainer , StaticLifetimeVisitor , Ty , TyCtxt , TypeSuperVisitable , TypeVisitor ,
13- } ;
11+ use rustc_middle:: ty:: { self , StaticLifetimeVisitor , Ty , TyCtxt , TypeSuperVisitable , TypeVisitor } ;
1412use rustc_span:: def_id:: LocalDefId ;
1513use rustc_span:: { Ident , Span } ;
1614use tracing:: debug;
1715
1816use crate :: error_reporting:: infer:: nice_region_error:: NiceRegionError ;
1917use crate :: errors:: {
20- ButCallingIntroduces , ButNeedsToSatisfy , DynTraitConstraintSuggestion , MoreTargeted ,
21- ReqIntroducedLocations ,
18+ ButNeedsToSatisfy , DynTraitConstraintSuggestion , MoreTargeted , ReqIntroducedLocations ,
2219} ;
2320use crate :: infer:: { RegionResolutionError , SubregionOrigin , TypeTrace } ;
24- use crate :: traits:: { ObligationCauseCode , UnifyReceiverContext } ;
21+ use crate :: traits:: ObligationCauseCode ;
2522
2623impl < ' a , ' tcx > NiceRegionError < ' a , ' tcx > {
2724 /// Print the error message for lifetime errors when the return type is a static `impl Trait`,
@@ -39,52 +36,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3936 sup_r,
4037 spans,
4138 ) if sub_r. is_static ( ) => ( var_origin, sub_origin, sub_r, sup_origin, sup_r, spans) ,
42- RegionResolutionError :: ConcreteFailure (
43- SubregionOrigin :: Subtype ( box TypeTrace { cause, .. } ) ,
44- sub_r,
45- sup_r,
46- ) if sub_r. is_static ( ) => {
47- // This is for an implicit `'static` requirement coming from `impl dyn Trait {}`.
48- if let ObligationCauseCode :: UnifyReceiver ( ctxt) = cause. code ( ) {
49- // This may have a closure and it would cause ICE
50- // through `find_param_with_region` (#78262).
51- let anon_reg_sup = tcx. is_suitable_region ( self . generic_param_scope , * sup_r) ?;
52- let fn_returns = tcx. return_type_impl_or_dyn_traits ( anon_reg_sup. scope ) ;
53- if fn_returns. is_empty ( ) {
54- return None ;
55- }
56-
57- let param = self . find_param_with_region ( * sup_r, * sub_r) ?;
58- let simple_ident = param. param . pat . simple_ident ( ) ;
59-
60- let ( has_impl_path, impl_path) = match ctxt. assoc_item . container {
61- AssocItemContainer :: Trait => {
62- let id = ctxt. assoc_item . container_id ( tcx) ;
63- ( true , tcx. def_path_str ( id) )
64- }
65- AssocItemContainer :: Impl => ( false , String :: new ( ) ) ,
66- } ;
67-
68- let mut err = self . tcx ( ) . dcx ( ) . create_err ( ButCallingIntroduces {
69- param_ty_span : param. param_ty_span ,
70- cause_span : cause. span ,
71- has_param_name : simple_ident. is_some ( ) ,
72- param_name : simple_ident. map ( |x| x. to_string ( ) ) . unwrap_or_default ( ) ,
73- has_lifetime : sup_r. has_name ( ) ,
74- lifetime : sup_r. to_string ( ) ,
75- assoc_item : ctxt. assoc_item . name ,
76- has_impl_path,
77- impl_path,
78- } ) ;
79- if self . find_impl_on_dyn_trait ( & mut err, param. param_ty , ctxt) {
80- let reported = err. emit ( ) ;
81- return Some ( reported) ;
82- } else {
83- err. cancel ( )
84- }
85- }
86- return None ;
87- }
8839 _ => return None ,
8940 } ;
9041 debug ! (
@@ -198,25 +149,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
198149 let fn_returns = tcx. return_type_impl_or_dyn_traits ( anon_reg_sup. scope ) ;
199150
200151 let mut override_error_code = None ;
201- if let SubregionOrigin :: Subtype ( box TypeTrace { cause, .. } ) = & sup_origin
202- && let ObligationCauseCode :: UnifyReceiver ( ctxt) = cause. code ( )
203- // Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
204- // `'static` lifetime when called as a method on a binding: `bar.qux()`.
205- && self . find_impl_on_dyn_trait ( & mut err, param. param_ty , ctxt)
206- {
207- override_error_code = Some ( ctxt. assoc_item . name ) ;
208- }
209152
210153 if let SubregionOrigin :: Subtype ( box TypeTrace { cause, .. } ) = & sub_origin
211154 && let code = match cause. code ( ) {
212155 ObligationCauseCode :: MatchImpl ( parent, ..) => parent. code ( ) ,
213156 _ => cause. code ( ) ,
214157 }
215- && let (
216- & ObligationCauseCode :: WhereClause ( item_def_id, _)
217- | & ObligationCauseCode :: WhereClauseInExpr ( item_def_id, ..) ,
218- None ,
219- ) = ( code, override_error_code)
158+ && let & ObligationCauseCode :: WhereClause ( item_def_id, _)
159+ | & ObligationCauseCode :: WhereClauseInExpr ( item_def_id, ..) = code
220160 {
221161 // Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
222162 // lifetime as above, but called using a fully-qualified path to the method:
@@ -230,7 +170,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
230170 override_error_code = Some ( ident. name ) ;
231171 }
232172 }
233- if let ( Some ( ident) , true ) = ( override_error_code, fn_returns. is_empty ( ) ) {
173+ if let Some ( ident) = override_error_code
174+ && fn_returns. is_empty ( )
175+ {
234176 // Provide a more targeted error code and description.
235177 let retarget_subdiag = MoreTargeted { ident } ;
236178 retarget_subdiag. add_to_diag ( & mut err) ;
@@ -495,8 +437,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
495437 kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) , ..
496438 } ) = tcx. hir_node_by_def_id ( impl_did)
497439 && trait_objects. iter ( ) . all ( |did| {
498- // FIXME: we should check `self_ty` against the receiver
499- // type in the `UnifyReceiver` context, but for now, use
440+ // FIXME: we should check `self_ty`, but for now, use
500441 // this imperfect proxy. This will fail if there are
501442 // multiple `impl`s for the same trait like
502443 // `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
@@ -517,41 +458,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
517458 }
518459 }
519460
520- /// When we call a method coming from an `impl Foo for dyn Bar`, `dyn Bar` introduces a default
521- /// `'static` obligation. Suggest relaxing that implicit bound.
522- fn find_impl_on_dyn_trait (
523- & self ,
524- err : & mut Diag < ' _ > ,
525- ty : Ty < ' _ > ,
526- ctxt : & UnifyReceiverContext < ' tcx > ,
527- ) -> bool {
528- let tcx = self . tcx ( ) ;
529-
530- // Find the method being called.
531- let Ok ( Some ( instance) ) = ty:: Instance :: try_resolve (
532- tcx,
533- self . cx . typing_env ( ctxt. param_env ) ,
534- ctxt. assoc_item . def_id ,
535- self . cx . resolve_vars_if_possible ( ctxt. args ) ,
536- ) else {
537- return false ;
538- } ;
539-
540- let mut v = TraitObjectVisitor ( FxIndexSet :: default ( ) ) ;
541- v. visit_ty ( ty) ;
542-
543- // Get the `Ident` of the method being called and the corresponding `impl` (to point at
544- // `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
545- let Some ( ( ident, self_ty) ) =
546- NiceRegionError :: get_impl_ident_and_self_ty_from_trait ( tcx, instance. def_id ( ) , & v. 0 )
547- else {
548- return false ;
549- } ;
550-
551- // Find the trait object types in the argument, so we point at *only* the trait object.
552- self . suggest_constrain_dyn_trait_in_impl ( err, & v. 0 , ident, self_ty)
553- }
554-
555461 fn suggest_constrain_dyn_trait_in_impl (
556462 & self ,
557463 err : & mut Diag < ' _ > ,
0 commit comments