From 8295e4a6cfb12ce74c480172c2854f76d1428b8b Mon Sep 17 00:00:00 2001 From: Ellen Date: Thu, 9 Sep 2021 15:43:59 +0100 Subject: [PATCH] add test for builtin types N + N unifying with fn call --- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- .../src/traits/const_evaluatable.rs | 13 ++++++++++--- .../generic_const_exprs/unify-op-with-fn-call.rs | 13 ++++++++++++- .../unify-op-with-fn-call.stderr | 10 ++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.stderr diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 60d7f0f85bb..97c0bca5d91 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1305,7 +1305,7 @@ impl EncodeContext<'a, 'tcx> { if encode_const { record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- self.tcx.mir_for_ctfe(def_id)); - // FIXME this feels wrong to have in `encode_mir` + // FIXME(generic_const_exprs): this feels wrong to have in `encode_mir` let abstract_const = self.tcx.thir_abstract_const(def_id); if let Ok(Some(abstract_const)) = abstract_const { record!(self.tables.thir_abstract_consts[def_id.to_def_id()] <- abstract_const); diff --git a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs index 4f178f777af..24fa5007f1e 100644 --- a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs +++ b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs @@ -267,10 +267,16 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) { self.is_poly |= expr.ty.definitely_has_param_types_or_consts(self.tcx); - if self.is_poly { - return; + if self.is_poly == false { + visit::walk_expr(self, expr) + } + } + + fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) { + self.is_poly |= pat.ty.definitely_has_param_types_or_consts(self.tcx); + if self.is_poly == false { + visit::walk_pat(self, pat); } - visit::walk_expr(self, expr); } fn visit_const(&mut self, ct: &'tcx ty::Const<'tcx>) { @@ -280,6 +286,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body, tcx }; visit::walk_expr(&mut is_poly_vis, &body[body_id]); + debug!("AbstractConstBuilder: is_poly={}", is_poly_vis.is_poly); if is_poly_vis.is_poly == false { return Ok(None); } diff --git a/src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.rs b/src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.rs index bb054bec6b6..c0404d35b08 100644 --- a/src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.rs +++ b/src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.rs @@ -1,7 +1,7 @@ -// check-pass #![feature(generic_const_exprs, adt_const_params, const_trait_impl)] #![allow(incomplete_features)] +// test `N + N` unifies with explicit function calls for non-builtin-types #[derive(PartialEq, Eq)] struct Foo(u8); @@ -21,4 +21,15 @@ fn foo(a: Evaluatable<{ N + N }>) { fn bar() {} +// test that `N + N` unifies with explicit function calls for builin-types +struct Evaluatable2; + +fn foo2(a: Evaluatable2<{ N + N }>) { + bar2::<{ std::ops::Add::add(N, N) }>(); + //~^ error: unconstrained generic constant + // FIXME(generic_const_exprs) make this not an error +} + +fn bar2() {} + fn main() {} diff --git a/src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.stderr b/src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.stderr new file mode 100644 index 00000000000..d18c7916f5f --- /dev/null +++ b/src/test/ui/const-generics/generic_const_exprs/unify-op-with-fn-call.stderr @@ -0,0 +1,10 @@ +error: unconstrained generic constant + --> $DIR/unify-op-with-fn-call.rs:28:12 + | +LL | bar2::<{ std::ops::Add::add(N, N) }>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: try adding a `where` bound using this expression: `where [(); { std::ops::Add::add(N, N) }]:` + +error: aborting due to previous error +