about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_infer/src/infer/combine.rs2
-rw-r--r--compiler/rustc_trait_selection/src/solve/eval_ctxt.rs17
-rw-r--r--compiler/rustc_trait_selection/src/solve/project_goals.rs64
-rw-r--r--tests/ui/traits/new-solver/array-default.rs8
-rw-r--r--tests/ui/traits/new-solver/structural-resolve-field.rs26
-rw-r--r--tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.fails.stderr18
-rw-r--r--tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.rs22
7 files changed, 115 insertions, 42 deletions
diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs
index 94a72e5a4bd..ed532aa2e8b 100644
--- a/compiler/rustc_infer/src/infer/combine.rs
+++ b/compiler/rustc_infer/src/infer/combine.rs
@@ -227,7 +227,7 @@ impl<'tcx> InferCtxt<'tcx> {
                 return self.unify_const_variable(vid, a, relation.param_env());
             }
             (ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..))
-                if self.tcx.features().generic_const_exprs =>
+                if self.tcx.features().generic_const_exprs || self.tcx.trait_solver_next() =>
             {
                 relation.register_const_equate_obligation(a, b);
                 return Ok(b);
diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs
index f91c6727753..a004e903b5e 100644
--- a/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs
+++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs
@@ -772,4 +772,21 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
         }
         values
     }
+
+    // Try to evaluate a const, or return `None` if the const is too generic.
+    // This doesn't mean the const isn't evaluatable, though, and should be treated
+    // as an ambiguity rather than no-solution.
+    pub(super) fn try_const_eval_resolve(
+        &self,
+        param_env: ty::ParamEnv<'tcx>,
+        unevaluated: ty::UnevaluatedConst<'tcx>,
+        ty: Ty<'tcx>,
+    ) -> Option<ty::Const<'tcx>> {
+        use rustc_middle::mir::interpret::ErrorHandled;
+        match self.infcx.try_const_eval_resolve(param_env, unevaluated, ty, None) {
+            Ok(ct) => Some(ct),
+            Err(ErrorHandled::Reported(e)) => Some(self.tcx().const_error(ty, e.into())),
+            Err(ErrorHandled::TooGeneric) => None,
+        }
+    }
 }
diff --git a/compiler/rustc_trait_selection/src/solve/project_goals.rs b/compiler/rustc_trait_selection/src/solve/project_goals.rs
index 7d7dfa2c837..99ed9ac7b62 100644
--- a/compiler/rustc_trait_selection/src/solve/project_goals.rs
+++ b/compiler/rustc_trait_selection/src/solve/project_goals.rs
@@ -22,25 +22,55 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
         &mut self,
         goal: Goal<'tcx, ProjectionPredicate<'tcx>>,
     ) -> QueryResult<'tcx> {
-        match goal.predicate.projection_ty.kind(self.tcx()) {
-            ty::AliasKind::Projection => {
-                // To only compute normalization once for each projection we only
-                // normalize if the expected term is an unconstrained inference variable.
-                //
-                // E.g. for `<T as Trait>::Assoc == u32` we recursively compute the goal
-                // `exists<U> <T as Trait>::Assoc == U` and then take the resulting type for
-                // `U` and equate it with `u32`. This means that we don't need a separate
-                // projection cache in the solver.
-                if self.term_is_fully_unconstrained(goal) {
-                    let candidates = self.assemble_and_evaluate_candidates(goal);
-                    self.merge_candidates(candidates)
-                } else {
-                    self.set_normalizes_to_hack_goal(goal);
-                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
+        let def_id = goal.predicate.def_id();
+        match self.tcx().def_kind(def_id) {
+            DefKind::AssocTy | DefKind::AssocConst => {
+                match self.tcx().associated_item(def_id).container {
+                    ty::AssocItemContainer::TraitContainer => {
+                        // To only compute normalization once for each projection we only
+                        // normalize if the expected term is an unconstrained inference variable.
+                        //
+                        // E.g. for `<T as Trait>::Assoc == u32` we recursively compute the goal
+                        // `exists<U> <T as Trait>::Assoc == U` and then take the resulting type for
+                        // `U` and equate it with `u32`. This means that we don't need a separate
+                        // projection cache in the solver.
+                        if self.term_is_fully_unconstrained(goal) {
+                            let candidates = self.assemble_and_evaluate_candidates(goal);
+                            self.merge_candidates(candidates)
+                        } else {
+                            self.set_normalizes_to_hack_goal(goal);
+                            self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
+                        }
+                    }
+                    ty::AssocItemContainer::ImplContainer => bug!("IATs not supported here yet"),
                 }
             }
-            ty::AliasKind::Opaque => self.normalize_opaque_type(goal),
-            ty::AliasKind::Inherent => bug!("IATs not supported here yet"),
+            DefKind::AnonConst => self.normalize_anon_const(goal),
+            DefKind::OpaqueTy => self.normalize_opaque_type(goal),
+            kind => bug!("uknown DefKind {} in projection goal: {goal:#?}", kind.descr(def_id)),
+        }
+    }
+
+    #[instrument(level = "debug", skip(self), ret)]
+    fn normalize_anon_const(
+        &mut self,
+        goal: Goal<'tcx, ty::ProjectionPredicate<'tcx>>,
+    ) -> QueryResult<'tcx> {
+        if let Some(normalized_const) = self.try_const_eval_resolve(
+            goal.param_env,
+            ty::UnevaluatedConst::new(
+                goal.predicate.projection_ty.def_id,
+                goal.predicate.projection_ty.substs,
+            ),
+            self.tcx()
+                .type_of(goal.predicate.projection_ty.def_id)
+                .no_bound_vars()
+                .expect("const ty should not rely on other generics"),
+        ) {
+            self.eq(goal.param_env, normalized_const, goal.predicate.term.ct().unwrap())?;
+            self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
+        } else {
+            self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
         }
     }
 }
diff --git a/tests/ui/traits/new-solver/array-default.rs b/tests/ui/traits/new-solver/array-default.rs
new file mode 100644
index 00000000000..5077137b09b
--- /dev/null
+++ b/tests/ui/traits/new-solver/array-default.rs
@@ -0,0 +1,8 @@
+// compile-flags: -Ztrait-solver=next
+// check-pass
+
+fn has_default<const N: usize>() where [(); N]: Default {}
+
+fn main() {
+    has_default::<1>();
+}
diff --git a/tests/ui/traits/new-solver/structural-resolve-field.rs b/tests/ui/traits/new-solver/structural-resolve-field.rs
index c492d927696..01899c9ad64 100644
--- a/tests/ui/traits/new-solver/structural-resolve-field.rs
+++ b/tests/ui/traits/new-solver/structural-resolve-field.rs
@@ -1,35 +1,13 @@
 // compile-flags: -Ztrait-solver=next
 // check-pass
 
+#[derive(Default)]
 struct Foo {
     x: i32,
 }
 
-impl MyDefault for Foo {
-    fn my_default() -> Self {
-        Self {
-            x: 0,
-        }
-    }
-}
-
-trait MyDefault {
-    fn my_default() -> Self;
-}
-
-impl MyDefault for [Foo; 0]  {
-    fn my_default() -> Self {
-        []
-    }
-}
-impl MyDefault for [Foo; 1] {
-    fn my_default() -> Self {
-        [Foo::my_default(); 1]
-    }
-}
-
 fn main() {
-    let mut xs = <[Foo; 1]>::my_default();
+    let mut xs = <[Foo; 1]>::default();
     xs[0].x = 1;
     (&mut xs[0]).x = 2;
 }
diff --git a/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.fails.stderr b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.fails.stderr
new file mode 100644
index 00000000000..072ac32a5de
--- /dev/null
+++ b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.fails.stderr
@@ -0,0 +1,18 @@
+error[E0277]: the trait bound `(): Trait<1>` is not satisfied
+  --> $DIR/unevaluated-const-impl-trait-ref.rs:20:13
+   |
+LL |     needs::<1>();
+   |             ^ the trait `Trait<1>` is not implemented for `()`
+   |
+   = help: the following other types implement trait `Trait<N>`:
+             <() as Trait<0>>
+             <() as Trait<2>>
+note: required by a bound in `needs`
+  --> $DIR/unevaluated-const-impl-trait-ref.rs:10:38
+   |
+LL | fn needs<const N: usize>() where (): Trait<N> {}
+   |                                      ^^^^^^^^ required by this bound in `needs`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.rs b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.rs
new file mode 100644
index 00000000000..26c595bc974
--- /dev/null
+++ b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.rs
@@ -0,0 +1,22 @@
+// compile-flags: -Ztrait-solver=next
+// revisions: works fails
+//[works] check-pass
+
+trait Trait<const N: usize> {}
+
+impl Trait<{ 1 - 1 }> for () {}
+impl Trait<{ 1 + 1 }> for () {}
+
+fn needs<const N: usize>() where (): Trait<N> {}
+
+#[cfg(works)]
+fn main() {
+    needs::<0>();
+    needs::<2>();
+}
+
+#[cfg(fails)]
+fn main() {
+    needs::<1>();
+    //[fails]~^ ERROR the trait bound `(): Trait<1>` is not satisfied
+}