Skip to content

Commit b32fc44

Browse files
committed
Auto merge of rust-lang#138676 - compiler-errors:overflow-implied-bounds, r=<try>
Implement overflow for infinite implied lifetime bounds Not a great error message, but better than a hang r? lcnr
2 parents 75530e9 + 7ed922a commit b32fc44

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub(super) fn infer_predicates(
2424

2525
// If new predicates were added then we need to re-calculate
2626
// all crates since there could be new implied predicates.
27-
loop {
28-
let mut predicates_added = false;
27+
for i in 0.. {
28+
let mut predicates_added: Option<Vec<_>> = None;
2929

3030
// Visit all the crates and infer predicates
3131
for id in tcx.hir_free_items() {
@@ -83,13 +83,34 @@ pub(super) fn infer_predicates(
8383
.get(&item_did.to_def_id())
8484
.map_or(0, |p| p.as_ref().skip_binder().len());
8585
if item_required_predicates.len() > item_predicates_len {
86-
predicates_added = true;
86+
predicates_added.get_or_insert_default().push(item_did);
8787
global_inferred_outlives
8888
.insert(item_did.to_def_id(), ty::EarlyBinder::bind(item_required_predicates));
8989
}
9090
}
9191

92-
if !predicates_added {
92+
if let Some(ids) = predicates_added {
93+
if !tcx.recursion_limit().value_within_limit(i) {
94+
let msg = if let &[id] = &ids[..] {
95+
format!(
96+
"overflow computing implied lifetime bounds for `{}`",
97+
tcx.def_path_str(id),
98+
)
99+
} else {
100+
"overflow computing implied lifetime bounds".to_string()
101+
};
102+
tcx.dcx()
103+
.struct_span_err(
104+
ids.iter().map(|id| tcx.def_span(*id)).collect::<Vec<_>>(),
105+
msg,
106+
)
107+
.emit();
108+
for id in ids {
109+
global_inferred_outlives.shift_remove(&id.to_def_id());
110+
}
111+
break;
112+
}
113+
} else {
93114
break;
94115
}
95116
}

tests/ui/implied-bounds/overflow.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trait Tailed<'a>: 'a {
2+
type Tail: Tailed<'a>;
3+
}
4+
5+
struct List<'a, T: Tailed<'a>> {
6+
//~^ ERROR overflow computing implied lifetime bounds for `List`
7+
next: Box<List<'a, T::Tail>>,
8+
node: &'a T,
9+
}
10+
11+
fn main() {}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: overflow computing implied lifetime bounds for `List`
2+
--> $DIR/overflow.rs:5:1
3+
|
4+
LL | struct List<'a, T: Tailed<'a>> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)