Skip to content

Commit ecf2a9b

Browse files
committed
fix: skip implied bounds if unconstrained lifetime exists
1 parent a29dada commit ecf2a9b

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

compiler/rustc_trait_selection/src/traits/outlives_bounds.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
5555
) -> Vec<OutlivesBound<'tcx>> {
5656
let ty = self.resolve_vars_if_possible(ty);
5757
let ty = OpportunisticRegionResolver::new(self).fold_ty(ty);
58-
assert!(!ty.needs_infer());
58+
59+
// We must avoid processing constrained lifetime variables in implied
60+
// bounds. See #110161 for context.
61+
if ty.needs_infer() {
62+
self.tcx.sess.delay_span_bug(
63+
self.tcx.source_span_untracked(body_id),
64+
"skipped implied_outlives_bounds due to unconstrained lifetimes",
65+
);
66+
return vec![];
67+
}
5968

6069
let span = self.tcx.def_span(body_id);
6170
let result = param_env
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ICE regression relating to unconstrained lifetimes in implied
2+
// bounds. See #110161.
3+
4+
// compile-flags: --crate-type=lib
5+
6+
trait Trait {
7+
type Ty;
8+
}
9+
10+
// erroneous `Ty` impl
11+
impl Trait for () {
12+
//~^ ERROR not all trait items implemented, missing: `Ty` [E0046]
13+
}
14+
15+
// `'lt` is not constrained by the erroneous `Ty`
16+
impl<'lt, T> Trait for Box<T>
17+
where
18+
T: Trait<Ty = &'lt ()>,
19+
{
20+
type Ty = &'lt ();
21+
}
22+
23+
// unconstrained lifetime appears in implied bounds
24+
fn test(_: <Box<()> as Trait>::Ty) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0046]: not all trait items implemented, missing: `Ty`
2+
--> $DIR/issue-110161.rs:11:1
3+
|
4+
LL | type Ty;
5+
| ------- `Ty` from trait
6+
...
7+
LL | impl Trait for () {
8+
| ^^^^^^^^^^^^^^^^^ missing `Ty` in implementation
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)