diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2018-01-25 12:48:51 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-25 12:48:51 -0600 |
| commit | 31f1aa57067403ded803cf72b196d6ddcf989fbb (patch) | |
| tree | 50d89b8e779173f36a27c11703b28f88e3431607 | |
| parent | 98b375483c44c68009d699a4cd4b7b0a3d5d97a3 (diff) | |
| parent | 072c3daa4cb6e1a8958ad9d61ebc7e062d570eac (diff) | |
| download | rust-31f1aa57067403ded803cf72b196d6ddcf989fbb.tar.gz rust-31f1aa57067403ded803cf72b196d6ddcf989fbb.zip | |
Rollup merge of #47529 - nikomatsakis:impl-trait-issue-38064, r=cramertj
track recursion limit when expanding existential impl trait r? @cramertj
| -rw-r--r-- | src/librustc/infer/region_constraints/mod.rs | 2 | ||||
| -rw-r--r-- | src/librustc/traits/project.rs | 16 | ||||
| -rw-r--r-- | src/librustc_mir/borrow_check/nll/type_check/mod.rs | 3 | ||||
| -rw-r--r-- | src/test/compile-fail/impl-trait/infinite-impl-trait-issue-38064.rs | 39 |
4 files changed, 58 insertions, 2 deletions
diff --git a/src/librustc/infer/region_constraints/mod.rs b/src/librustc/infer/region_constraints/mod.rs index 72740dd40be..68d81a2dee3 100644 --- a/src/librustc/infer/region_constraints/mod.rs +++ b/src/librustc/infer/region_constraints/mod.rs @@ -82,7 +82,7 @@ pub type VarOrigins = IndexVec<RegionVid, RegionVariableOrigin>; /// Describes constraints between the region variables and other /// regions, as well as other conditions that must be verified, or /// assumptions that can be made. -#[derive(Default)] +#[derive(Debug, Default)] pub struct RegionConstraintData<'tcx> { /// Constraints of the form `A <= B`, where either `A` or `B` can /// be a region variable (or neither, as it happens). diff --git a/src/librustc/traits/project.rs b/src/librustc/traits/project.rs index 3342d13dd6e..d34649782ba 100644 --- a/src/librustc/traits/project.rs +++ b/src/librustc/traits/project.rs @@ -293,9 +293,23 @@ impl<'a, 'b, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for AssociatedTypeNormalizer<'a, Reveal::UserFacing => ty, Reveal::All => { + let recursion_limit = self.tcx().sess.recursion_limit.get(); + if self.depth >= recursion_limit { + let obligation = Obligation::with_depth( + self.cause.clone(), + recursion_limit, + self.param_env, + ty, + ); + self.selcx.infcx().report_overflow_error(&obligation, true); + } + let generic_ty = self.tcx().type_of(def_id); let concrete_ty = generic_ty.subst(self.tcx(), substs); - self.fold_ty(concrete_ty) + self.depth += 1; + let folded_ty = self.fold_ty(concrete_ty); + self.depth -= 1; + folded_ty } } } diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index 901b73c610e..9dcd4435580 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -681,6 +681,8 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { let data = self.infcx.take_and_reset_region_constraints(); if !data.is_empty() { + debug!("fully_perform_op: constraints generated at {:?} are {:#?}", + locations, data); self.constraints .outlives_sets .push(OutlivesSet { locations, data }); @@ -1539,6 +1541,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { where T: fmt::Debug + TypeFoldable<'tcx>, { + debug!("normalize(value={:?}, location={:?})", value, location); self.fully_perform_op(location.at_self(), |this| { let mut selcx = traits::SelectionContext::new(this.infcx); let cause = this.misc(this.last_span); diff --git a/src/test/compile-fail/impl-trait/infinite-impl-trait-issue-38064.rs b/src/test/compile-fail/impl-trait/infinite-impl-trait-issue-38064.rs new file mode 100644 index 00000000000..abde9689bd6 --- /dev/null +++ b/src/test/compile-fail/impl-trait/infinite-impl-trait-issue-38064.rs @@ -0,0 +1,39 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that attempts to construct infinite types via impl trait fail +// in a graceful way. +// +// Regression test for #38064. + +// error-pattern:overflow evaluating the requirement `impl Quux` + +#![feature(conservative_impl_trait)] + +trait Quux {} + +fn foo() -> impl Quux { + struct Foo<T>(T); + impl<T> Quux for Foo<T> {} + Foo(bar()) +} + +fn bar() -> impl Quux { + struct Bar<T>(T); + impl<T> Quux for Bar<T> {} + Bar(foo()) +} + +// effectively: +// struct Foo(Bar); +// struct Bar(Foo); +// should produce an error about infinite size + +fn main() { foo(); } |
