diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 308d533e68991..91c52b19fcdfd 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -635,9 +635,18 @@ pub fn try_evaluate_const<'tcx>( return Err(EvaluateConstErr::HasGenericsOrInfers); } - let typing_env = infcx + let mut typing_env; + + typing_env = infcx .typing_env(tcx.erase_and_anonymize_regions(param_env)) .with_post_analysis_normalized(tcx); + + // Since there is no generic parameter, we can just drop the environment + // to prevent query cycle. + if !uv.args.has_non_region_param() { + typing_env = infcx.typing_env(ty::ParamEnv::empty()); + } + (uv.args, typing_env) } }; diff --git a/tests/ui/traits/next-solver/unevaluated_const_query_cycle.rs b/tests/ui/traits/next-solver/unevaluated_const_query_cycle.rs new file mode 100644 index 0000000000000..be7024dcbf180 --- /dev/null +++ b/tests/ui/traits/next-solver/unevaluated_const_query_cycle.rs @@ -0,0 +1,20 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Regression test for https://github.com/rust-lang/trait-system-refactor-initiative/issues/249 + +const CONST: &str = "hi"; + +trait ToUnit { + type Assoc; +} +impl ToUnit for T { + type Assoc = (); +} + +fn foo() +where + <[u8; CONST.len()] as ToUnit>::Assoc: Sized, +{} + +fn main(){}