@@ -5,18 +5,15 @@ use super::compare_impl_item::check_type_bounds;
55use super :: compare_impl_item:: { compare_impl_method, compare_impl_ty} ;
66use super :: * ;
77use rustc_attr as attr;
8- use rustc_errors:: { Applicability , ErrorGuaranteed , MultiSpan } ;
8+ use rustc_errors:: { ErrorGuaranteed , MultiSpan } ;
99use rustc_hir as hir;
10- use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
10+ use rustc_hir:: def:: { CtorKind , DefKind } ;
1111use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId } ;
12- use rustc_hir:: intravisit:: Visitor ;
13- use rustc_hir:: { ItemKind , Node , PathSegment } ;
14- use rustc_infer:: infer:: opaque_types:: ConstrainOpaqueTypeRegionVisitor ;
12+ use rustc_hir:: Node ;
1513use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
1614use rustc_infer:: infer:: { RegionVariableOrigin , TyCtxtInferExt } ;
1715use rustc_infer:: traits:: { Obligation , TraitEngineExt as _} ;
1816use rustc_lint_defs:: builtin:: REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS ;
19- use rustc_middle:: hir:: nested_filter;
2017use rustc_middle:: middle:: stability:: EvalResult ;
2118use rustc_middle:: traits:: DefiningAnchor ;
2219use rustc_middle:: ty:: fold:: BottomUpFolder ;
@@ -218,9 +215,6 @@ fn check_opaque(tcx: TyCtxt<'_>, id: hir::ItemId) {
218215 let args = GenericArgs :: identity_for_item ( tcx, item. owner_id ) ;
219216 let span = tcx. def_span ( item. owner_id . def_id ) ;
220217
221- if !tcx. features ( ) . impl_trait_projections {
222- check_opaque_for_inheriting_lifetimes ( tcx, item. owner_id . def_id , span) ;
223- }
224218 if tcx. type_of ( item. owner_id . def_id ) . instantiate_identity ( ) . references_error ( ) {
225219 return ;
226220 }
@@ -231,129 +225,6 @@ fn check_opaque(tcx: TyCtxt<'_>, id: hir::ItemId) {
231225 let _ = check_opaque_meets_bounds ( tcx, item. owner_id . def_id , span, & origin) ;
232226}
233227
234- /// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result
235- /// in "inheriting lifetimes".
236- #[ instrument( level = "debug" , skip( tcx, span) ) ]
237- pub ( super ) fn check_opaque_for_inheriting_lifetimes (
238- tcx : TyCtxt < ' _ > ,
239- def_id : LocalDefId ,
240- span : Span ,
241- ) {
242- let item = tcx. hir ( ) . expect_item ( def_id) ;
243- debug ! ( ?item, ?span) ;
244-
245- struct ProhibitOpaqueVisitor < ' tcx > {
246- tcx : TyCtxt < ' tcx > ,
247- opaque_identity_ty : Ty < ' tcx > ,
248- parent_count : u32 ,
249- references_parent_regions : bool ,
250- selftys : Vec < ( Span , Option < String > ) > ,
251- }
252-
253- impl < ' tcx > ty:: visit:: TypeVisitor < TyCtxt < ' tcx > > for ProhibitOpaqueVisitor < ' tcx > {
254- type BreakTy = Ty < ' tcx > ;
255-
256- fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
257- debug ! ( ?t, "root_visit_ty" ) ;
258- if t == self . opaque_identity_ty {
259- ControlFlow :: Continue ( ( ) )
260- } else {
261- t. visit_with ( & mut ConstrainOpaqueTypeRegionVisitor {
262- tcx : self . tcx ,
263- op : |region| {
264- if let ty:: ReEarlyBound ( ty:: EarlyBoundRegion { index, .. } ) = * region
265- && index < self . parent_count
266- {
267- self . references_parent_regions = true ;
268- }
269- } ,
270- } ) ;
271- if self . references_parent_regions {
272- ControlFlow :: Break ( t)
273- } else {
274- ControlFlow :: Continue ( ( ) )
275- }
276- }
277- }
278- }
279-
280- impl < ' tcx > Visitor < ' tcx > for ProhibitOpaqueVisitor < ' tcx > {
281- type NestedFilter = nested_filter:: OnlyBodies ;
282-
283- fn nested_visit_map ( & mut self ) -> Self :: Map {
284- self . tcx . hir ( )
285- }
286-
287- fn visit_ty ( & mut self , arg : & ' tcx hir:: Ty < ' tcx > ) {
288- match arg. kind {
289- hir:: TyKind :: Path ( hir:: QPath :: Resolved ( None , path) ) => match & path. segments {
290- [ PathSegment { res : Res :: SelfTyParam { .. } , .. } ] => {
291- let impl_ty_name = None ;
292- self . selftys . push ( ( path. span , impl_ty_name) ) ;
293- }
294- [ PathSegment { res : Res :: SelfTyAlias { alias_to : def_id, .. } , .. } ] => {
295- let impl_ty_name = Some ( self . tcx . def_path_str ( * def_id) ) ;
296- self . selftys . push ( ( path. span , impl_ty_name) ) ;
297- }
298- _ => { }
299- } ,
300- _ => { }
301- }
302- hir:: intravisit:: walk_ty ( self , arg) ;
303- }
304- }
305-
306- if let ItemKind :: OpaqueTy ( & hir:: OpaqueTy {
307- origin : hir:: OpaqueTyOrigin :: AsyncFn ( ..) | hir:: OpaqueTyOrigin :: FnReturn ( ..) ,
308- ..
309- } ) = item. kind
310- {
311- let args = GenericArgs :: identity_for_item ( tcx, def_id) ;
312- let opaque_identity_ty = Ty :: new_opaque ( tcx, def_id. to_def_id ( ) , args) ;
313- let mut visitor = ProhibitOpaqueVisitor {
314- opaque_identity_ty,
315- parent_count : tcx. generics_of ( def_id) . parent_count as u32 ,
316- references_parent_regions : false ,
317- tcx,
318- selftys : vec ! [ ] ,
319- } ;
320- let prohibit_opaque = tcx
321- . explicit_item_bounds ( def_id)
322- . instantiate_identity_iter_copied ( )
323- . try_for_each ( |( predicate, _) | predicate. visit_with ( & mut visitor) ) ;
324-
325- if let Some ( ty) = prohibit_opaque. break_value ( ) {
326- visitor. visit_item ( & item) ;
327- let is_async = match item. kind {
328- ItemKind :: OpaqueTy ( hir:: OpaqueTy { origin, .. } ) => {
329- matches ! ( origin, hir:: OpaqueTyOrigin :: AsyncFn ( ..) )
330- }
331- _ => unreachable ! ( ) ,
332- } ;
333-
334- let mut err = feature_err (
335- & tcx. sess . parse_sess ,
336- sym:: impl_trait_projections,
337- span,
338- format ! (
339- "`{}` return type cannot contain a projection or `Self` that references \
340- lifetimes from a parent scope",
341- if is_async { "async fn" } else { "impl Trait" } ,
342- ) ,
343- ) ;
344- for ( span, name) in visitor. selftys {
345- err. span_suggestion (
346- span,
347- "consider spelling out the type instead" ,
348- name. unwrap_or_else ( || format ! ( "{ty:?}" ) ) ,
349- Applicability :: MaybeIncorrect ,
350- ) ;
351- }
352- err. emit ( ) ;
353- }
354- }
355- }
356-
357228/// Checks that an opaque type does not contain cycles.
358229pub ( super ) fn check_opaque_for_cycles < ' tcx > (
359230 tcx : TyCtxt < ' tcx > ,
0 commit comments