@@ -8,20 +8,17 @@ use rustc_hir::{
8
8
self as hir, AmbigArg , GenericBound , GenericParam , GenericParamKind , Item , ItemKind , Lifetime ,
9
9
LifetimeName , LifetimeParamKind , MissingLifetimeKind , Node , TyKind ,
10
10
} ;
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 } ;
14
12
use rustc_span:: def_id:: LocalDefId ;
15
13
use rustc_span:: { Ident , Span } ;
16
14
use tracing:: debug;
17
15
18
16
use crate :: error_reporting:: infer:: nice_region_error:: NiceRegionError ;
19
17
use crate :: errors:: {
20
- ButCallingIntroduces , ButNeedsToSatisfy , DynTraitConstraintSuggestion , MoreTargeted ,
21
- ReqIntroducedLocations ,
18
+ ButNeedsToSatisfy , DynTraitConstraintSuggestion , MoreTargeted , ReqIntroducedLocations ,
22
19
} ;
23
20
use crate :: infer:: { RegionResolutionError , SubregionOrigin , TypeTrace } ;
24
- use crate :: traits:: { ObligationCauseCode , UnifyReceiverContext } ;
21
+ use crate :: traits:: ObligationCauseCode ;
25
22
26
23
impl < ' a , ' tcx > NiceRegionError < ' a , ' tcx > {
27
24
/// 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> {
39
36
sup_r,
40
37
spans,
41
38
) 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
- }
88
39
_ => return None ,
89
40
} ;
90
41
debug ! (
@@ -198,25 +149,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
198
149
let fn_returns = tcx. return_type_impl_or_dyn_traits ( anon_reg_sup. scope ) ;
199
150
200
151
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
- }
209
152
210
153
if let SubregionOrigin :: Subtype ( box TypeTrace { cause, .. } ) = & sub_origin
211
154
&& let code = match cause. code ( ) {
212
155
ObligationCauseCode :: MatchImpl ( parent, ..) => parent. code ( ) ,
213
156
_ => cause. code ( ) ,
214
157
}
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
220
160
{
221
161
// Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
222
162
// lifetime as above, but called using a fully-qualified path to the method:
@@ -230,7 +170,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
230
170
override_error_code = Some ( ident. name ) ;
231
171
}
232
172
}
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
+ {
234
176
// Provide a more targeted error code and description.
235
177
let retarget_subdiag = MoreTargeted { ident } ;
236
178
retarget_subdiag. add_to_diag ( & mut err) ;
@@ -495,8 +437,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
495
437
kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) , ..
496
438
} ) = tcx. hir_node_by_def_id ( impl_did)
497
439
&& 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
500
441
// this imperfect proxy. This will fail if there are
501
442
// multiple `impl`s for the same trait like
502
443
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
@@ -517,41 +458,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
517
458
}
518
459
}
519
460
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
-
555
461
fn suggest_constrain_dyn_trait_in_impl (
556
462
& self ,
557
463
err : & mut Diag < ' _ > ,
0 commit comments