diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 8a5099804ed26..3c87979224816 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -460,8 +460,6 @@ fn check_opaque_meets_bounds<'tcx>( return Err(guar); } match origin { - // Checked when type checking the function containing them. - hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {} // Nested opaque types occur only in associated types: // ` type Opaque = impl Trait<&'static T, AssocTy = impl Nested>; ` // They can only be referenced as ` as Trait<&'static T>>::AssocTy`. @@ -470,9 +468,11 @@ fn check_opaque_meets_bounds<'tcx>( hir::OpaqueTyOrigin::TyAlias { .. } if tcx.def_kind(tcx.parent(def_id.to_def_id())) == DefKind::OpaqueTy => {} // Can have different predicates to their defining use - hir::OpaqueTyOrigin::TyAlias { .. } => { - let wf_tys = ocx.assumed_wf_types_and_report_errors(param_env, def_id)?; - let implied_bounds = infcx.implied_bounds_tys(param_env, def_id, wf_tys); + hir::OpaqueTyOrigin::TyAlias { .. } + | hir::OpaqueTyOrigin::FnReturn(..) + | hir::OpaqueTyOrigin::AsyncFn(..) => { + let wf_tys = ocx.assumed_wf_types_and_report_errors(param_env, defining_use_anchor)?; + let implied_bounds = infcx.implied_bounds_tys(param_env, defining_use_anchor, wf_tys); let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds); ocx.resolve_regions_and_report_errors(defining_use_anchor, &outlives_env)?; } diff --git a/compiler/rustc_ty_utils/src/implied_bounds.rs b/compiler/rustc_ty_utils/src/implied_bounds.rs index 436f10a4f7b91..d9731c6811224 100644 --- a/compiler/rustc_ty_utils/src/implied_bounds.rs +++ b/compiler/rustc_ty_utils/src/implied_bounds.rs @@ -118,18 +118,7 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<' } }, DefKind::AssocConst | DefKind::AssocTy => tcx.assumed_wf_types(tcx.local_parent(def_id)), - DefKind::OpaqueTy => match tcx.def_kind(tcx.local_parent(def_id)) { - DefKind::TyAlias { .. } => ty::List::empty(), - DefKind::AssocTy => tcx.assumed_wf_types(tcx.local_parent(def_id)), - // Nested opaque types only occur in associated types: - // ` type Opaque = impl Trait<&'static T, AssocTy = impl Nested>; ` - // assumed_wf_types should include those of `Opaque`, `Opaque` itself - // and `&'static T`. - DefKind::OpaqueTy => bug!("unimplemented implied bounds for nested opaque types"), - def_kind => { - bug!("unimplemented implied bounds for opaque types with parent {def_kind:?}") - } - }, + DefKind::OpaqueTy => bug!("implied bounds are not defined for opaques"), DefKind::Mod | DefKind::Struct | DefKind::Union diff --git a/tests/ui/impl-trait/check-rpit-is-wf-regions.rs b/tests/ui/impl-trait/check-rpit-is-wf-regions.rs new file mode 100644 index 0000000000000..f0e12d3318ebd --- /dev/null +++ b/tests/ui/impl-trait/check-rpit-is-wf-regions.rs @@ -0,0 +1,21 @@ +type Static<'a> = &'static &'a (); + +trait Extend<'a> { + fn extend(self, _: &'a str) -> &'static str; +} + +impl<'a> Extend<'a> for Static<'a> { + fn extend(self, s: &'a str) -> &'static str { + s + } +} + +fn boom<'a>(arg: Static<'_>) -> impl Extend<'a> { + //~^ ERROR in type `&'static &'a ()`, reference has a longer lifetime than the data it references + arg +} + +fn main() { + let y = boom(&&()).extend(&String::from("temporary")); + println!("{}", y); +} diff --git a/tests/ui/impl-trait/check-rpit-is-wf-regions.stderr b/tests/ui/impl-trait/check-rpit-is-wf-regions.stderr new file mode 100644 index 0000000000000..5a7cce6449306 --- /dev/null +++ b/tests/ui/impl-trait/check-rpit-is-wf-regions.stderr @@ -0,0 +1,16 @@ +error[E0491]: in type `&'static &'a ()`, reference has a longer lifetime than the data it references + --> $DIR/check-rpit-is-wf-regions.rs:13:33 + | +LL | fn boom<'a>(arg: Static<'_>) -> impl Extend<'a> { + | ^^^^^^^^^^^^^^^ + | + = note: the pointer is valid for the static lifetime +note: but the referenced data is only valid for the lifetime `'a` as defined here + --> $DIR/check-rpit-is-wf-regions.rs:13:9 + | +LL | fn boom<'a>(arg: Static<'_>) -> impl Extend<'a> { + | ^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0491`.