Skip to content

Commit fa1e4ba

Browse files
Check RPIT and AFIT hidden types are well-formed considering regions
1 parent b03864d commit fa1e4ba

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,6 @@ fn check_opaque_meets_bounds<'tcx>(
460460
return Err(guar);
461461
}
462462
match origin {
463-
// Checked when type checking the function containing them.
464-
hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {}
465463
// Nested opaque types occur only in associated types:
466464
// ` type Opaque<T> = impl Trait<&'static T, AssocTy = impl Nested>; `
467465
// They can only be referenced as `<Opaque<T> as Trait<&'static T>>::AssocTy`.
@@ -470,9 +468,11 @@ fn check_opaque_meets_bounds<'tcx>(
470468
hir::OpaqueTyOrigin::TyAlias { .. }
471469
if tcx.def_kind(tcx.parent(def_id.to_def_id())) == DefKind::OpaqueTy => {}
472470
// Can have different predicates to their defining use
473-
hir::OpaqueTyOrigin::TyAlias { .. } => {
474-
let wf_tys = ocx.assumed_wf_types_and_report_errors(param_env, def_id)?;
475-
let implied_bounds = infcx.implied_bounds_tys(param_env, def_id, wf_tys);
471+
hir::OpaqueTyOrigin::TyAlias { .. }
472+
| hir::OpaqueTyOrigin::FnReturn(..)
473+
| hir::OpaqueTyOrigin::AsyncFn(..) => {
474+
let wf_tys = ocx.assumed_wf_types_and_report_errors(param_env, defining_use_anchor)?;
475+
let implied_bounds = infcx.implied_bounds_tys(param_env, defining_use_anchor, wf_tys);
476476
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
477477
ocx.resolve_regions_and_report_errors(defining_use_anchor, &outlives_env)?;
478478
}

compiler/rustc_ty_utils/src/implied_bounds.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,7 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'
118118
}
119119
},
120120
DefKind::AssocConst | DefKind::AssocTy => tcx.assumed_wf_types(tcx.local_parent(def_id)),
121-
DefKind::OpaqueTy => match tcx.def_kind(tcx.local_parent(def_id)) {
122-
DefKind::TyAlias { .. } => ty::List::empty(),
123-
DefKind::AssocTy => tcx.assumed_wf_types(tcx.local_parent(def_id)),
124-
// Nested opaque types only occur in associated types:
125-
// ` type Opaque<T> = impl Trait<&'static T, AssocTy = impl Nested>; `
126-
// assumed_wf_types should include those of `Opaque<T>`, `Opaque<T>` itself
127-
// and `&'static T`.
128-
DefKind::OpaqueTy => bug!("unimplemented implied bounds for nested opaque types"),
129-
def_kind => {
130-
bug!("unimplemented implied bounds for opaque types with parent {def_kind:?}")
131-
}
132-
},
121+
DefKind::OpaqueTy => bug!("implied bounds are not defined for opaques"),
133122
DefKind::Mod
134123
| DefKind::Struct
135124
| DefKind::Union
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
type Static<'a> = &'static &'a ();
2+
3+
trait Extend<'a> {
4+
fn extend(self, _: &'a str) -> &'static str;
5+
}
6+
7+
impl<'a> Extend<'a> for Static<'a> {
8+
fn extend(self, s: &'a str) -> &'static str {
9+
s
10+
}
11+
}
12+
13+
fn boom<'a>(arg: Static<'_>) -> impl Extend<'a> {
14+
//~^ ERROR in type `&'static &'a ()`, reference has a longer lifetime than the data it references
15+
arg
16+
}
17+
18+
fn main() {
19+
let y = boom(&&()).extend(&String::from("temporary"));
20+
println!("{}", y);
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0491]: in type `&'static &'a ()`, reference has a longer lifetime than the data it references
2+
--> $DIR/check-rpit-is-wf-regions.rs:13:33
3+
|
4+
LL | fn boom<'a>(arg: Static<'_>) -> impl Extend<'a> {
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: the pointer is valid for the static lifetime
8+
note: but the referenced data is only valid for the lifetime `'a` as defined here
9+
--> $DIR/check-rpit-is-wf-regions.rs:13:9
10+
|
11+
LL | fn boom<'a>(arg: Static<'_>) -> impl Extend<'a> {
12+
| ^^
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0491`.

0 commit comments

Comments
 (0)