Skip to content

Commit d8cae13

Browse files
authored
Rollup merge of rust-lang#110272 - Ezrashaw:fix-unconned-lt-in-implbounds, r=aliemjay
fix: skip implied bounds if unconstrained lifetime exists Fixes rust-lang#110161 r? ```@aliemjay```
2 parents b6abbed + 4c80f58 commit d8cae13

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

compiler/rustc_trait_selection/src/traits/outlives_bounds.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ 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 do not expect existential variables in implied bounds.
60+
// We may however encounter unconstrained lifetime variables in invalid
61+
// code. See #110161 for context.
62+
assert!(!ty.has_non_region_infer());
63+
if ty.needs_infer() {
64+
self.tcx.sess.delay_span_bug(
65+
self.tcx.def_span(body_id),
66+
"skipped implied_outlives_bounds due to unconstrained lifetimes",
67+
);
68+
return vec![];
69+
}
5970

6071
let span = self.tcx.def_span(body_id);
6172
let result = param_env
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ICE regression relating to unconstrained lifetimes in implied
2+
// bounds. See #110161.
3+
4+
// compile-flags: --crate-type=lib
5+
6+
trait LtTrait {
7+
type Ty;
8+
}
9+
10+
// erroneous `Ty` impl
11+
impl LtTrait 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> LtTrait for Box<T>
17+
where
18+
T: LtTrait<Ty = &'lt ()>,
19+
{
20+
type Ty = &'lt ();
21+
}
22+
23+
// unconstrained lifetime appears in implied bounds
24+
fn test(_: <Box<()> as LtTrait>::Ty) {}
25+
26+
fn test2<'x>(_: &'x <Box<()> as LtTrait>::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 LtTrait 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)