From 5a219cba9563dc869577f7b7a9e318a1565dd4da Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 20 Sep 2024 14:17:08 +0000 Subject: [PATCH 1/3] bail if there are too many non-region infer vars --- .../src/solve/eval_ctxt/canonical.rs | 7 +++++++ tests/ui/traits/coherence-alias-hang.rs | 2 ++ 2 files changed, 9 insertions(+) diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs index 5acfec3dee33c..3887f9c529cdb 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs @@ -157,6 +157,13 @@ where }, ); + let num_non_region_vars = canonical.variables.iter().filter(|c| !c.is_region()).count(); + if num_non_region_vars > self.cx().recursion_limit() { + return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow { + suggest_increasing_limit: true, + })); + } + Ok(canonical) } diff --git a/tests/ui/traits/coherence-alias-hang.rs b/tests/ui/traits/coherence-alias-hang.rs index 37b8073958994..c2b4d2e42d214 100644 --- a/tests/ui/traits/coherence-alias-hang.rs +++ b/tests/ui/traits/coherence-alias-hang.rs @@ -1,4 +1,6 @@ //@ check-pass +//@ revisions: current next +//[next]@ compile-flags: -Znext-solver // Regression test for nalgebra hang . From 9613f487ae0e8c563caffad4ffdede3296002457 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 20 Sep 2024 14:49:03 +0000 Subject: [PATCH 2/3] add comment --- .../rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs index 3887f9c529cdb..4d743c0519022 100644 --- a/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs +++ b/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs @@ -157,6 +157,10 @@ where }, ); + // HACK: We bail with overflow if the response would have too many non-region + // inference variables. This tends to only happen if we encounter a lot of + // ambiguous alias types which get replaced with fresh inference variables + // during generalization. This prevents a hang in nalgebra. let num_non_region_vars = canonical.variables.iter().filter(|c| !c.is_region()).count(); if num_non_region_vars > self.cx().recursion_limit() { return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow { From a6aeba83beac8173bcc1eb819441e9bdc54d0561 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 20 Sep 2024 14:57:58 +0000 Subject: [PATCH 3/3] update tests --- .../coinduction/fixpoint-exponential-growth.stderr | 1 + .../overflow/recursion-limit-zero-issue-115351.rs | 3 ++- .../overflow/recursion-limit-zero-issue-115351.stderr | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.stderr diff --git a/tests/ui/traits/next-solver/cycles/coinduction/fixpoint-exponential-growth.stderr b/tests/ui/traits/next-solver/cycles/coinduction/fixpoint-exponential-growth.stderr index 8d7d8cee08ae3..150100f2c531c 100644 --- a/tests/ui/traits/next-solver/cycles/coinduction/fixpoint-exponential-growth.stderr +++ b/tests/ui/traits/next-solver/cycles/coinduction/fixpoint-exponential-growth.stderr @@ -4,6 +4,7 @@ error[E0275]: overflow evaluating the requirement `W<_>: Trait` LL | impls::>(); | ^^^^ | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`fixpoint_exponential_growth`) note: required by a bound in `impls` --> $DIR/fixpoint-exponential-growth.rs:30:13 | diff --git a/tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.rs b/tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.rs index 1b80287d9da3b..86d428cc0f059 100644 --- a/tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.rs +++ b/tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.rs @@ -1,6 +1,7 @@ +//~ ERROR overflow evaluating the requirement `Self: Trait` +//~^ ERROR overflow evaluating the requirement `Self well-formed` // This is a non-regression test for issue #115351, where a recursion limit of 0 caused an ICE. //@ compile-flags: -Znext-solver --crate-type=lib -//@ check-pass #![recursion_limit = "0"] trait Trait {} diff --git a/tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.stderr b/tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.stderr new file mode 100644 index 0000000000000..2eed7e8f7233e --- /dev/null +++ b/tests/ui/traits/next-solver/overflow/recursion-limit-zero-issue-115351.stderr @@ -0,0 +1,11 @@ +error[E0275]: overflow evaluating the requirement `Self: Trait` + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "2"]` attribute to your crate (`recursion_limit_zero_issue_115351`) + +error[E0275]: overflow evaluating the requirement `Self well-formed` + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "2"]` attribute to your crate (`recursion_limit_zero_issue_115351`) + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0275`.