Skip to content

Commit 31f1aa5

Browse files
authored
Rollup merge of rust-lang#47529 - nikomatsakis:impl-trait-issue-38064, r=cramertj
track recursion limit when expanding existential impl trait r? @cramertj
2 parents 98b3754 + 072c3da commit 31f1aa5

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/librustc/infer/region_constraints/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub type VarOrigins = IndexVec<RegionVid, RegionVariableOrigin>;
8282
/// Describes constraints between the region variables and other
8383
/// regions, as well as other conditions that must be verified, or
8484
/// assumptions that can be made.
85-
#[derive(Default)]
85+
#[derive(Debug, Default)]
8686
pub struct RegionConstraintData<'tcx> {
8787
/// Constraints of the form `A <= B`, where either `A` or `B` can
8888
/// be a region variable (or neither, as it happens).

src/librustc/traits/project.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,23 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a,
293293
Reveal::UserFacing => ty,
294294

295295
Reveal::All => {
296+
let recursion_limit = self.tcx().sess.recursion_limit.get();
297+
if self.depth >= recursion_limit {
298+
let obligation = Obligation::with_depth(
299+
self.cause.clone(),
300+
recursion_limit,
301+
self.param_env,
302+
ty,
303+
);
304+
self.selcx.infcx().report_overflow_error(&obligation, true);
305+
}
306+
296307
let generic_ty = self.tcx().type_of(def_id);
297308
let concrete_ty = generic_ty.subst(self.tcx(), substs);
298-
self.fold_ty(concrete_ty)
309+
self.depth += 1;
310+
let folded_ty = self.fold_ty(concrete_ty);
311+
self.depth -= 1;
312+
folded_ty
299313
}
300314
}
301315
}

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,8 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
681681

682682
let data = self.infcx.take_and_reset_region_constraints();
683683
if !data.is_empty() {
684+
debug!("fully_perform_op: constraints generated at {:?} are {:#?}",
685+
locations, data);
684686
self.constraints
685687
.outlives_sets
686688
.push(OutlivesSet { locations, data });
@@ -1539,6 +1541,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
15391541
where
15401542
T: fmt::Debug + TypeFoldable<'tcx>,
15411543
{
1544+
debug!("normalize(value={:?}, location={:?})", value, location);
15421545
self.fully_perform_op(location.at_self(), |this| {
15431546
let mut selcx = traits::SelectionContext::new(this.infcx);
15441547
let cause = this.misc(this.last_span);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that attempts to construct infinite types via impl trait fail
12+
// in a graceful way.
13+
//
14+
// Regression test for #38064.
15+
16+
// error-pattern:overflow evaluating the requirement `impl Quux`
17+
18+
#![feature(conservative_impl_trait)]
19+
20+
trait Quux {}
21+
22+
fn foo() -> impl Quux {
23+
struct Foo<T>(T);
24+
impl<T> Quux for Foo<T> {}
25+
Foo(bar())
26+
}
27+
28+
fn bar() -> impl Quux {
29+
struct Bar<T>(T);
30+
impl<T> Quux for Bar<T> {}
31+
Bar(foo())
32+
}
33+
34+
// effectively:
35+
// struct Foo(Bar);
36+
// struct Bar(Foo);
37+
// should produce an error about infinite size
38+
39+
fn main() { foo(); }

0 commit comments

Comments
 (0)