From 95209252f534ccb5bd15766234a992f53737fccf Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 3 Nov 2021 15:36:38 +0100 Subject: [PATCH 1/3] add test --- src/test/ui/const-generics/issues/issue-90455.rs | 12 ++++++++++++ src/test/ui/const-generics/issues/issue-90455.stderr | 10 ++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/test/ui/const-generics/issues/issue-90455.rs create mode 100644 src/test/ui/const-generics/issues/issue-90455.stderr diff --git a/src/test/ui/const-generics/issues/issue-90455.rs b/src/test/ui/const-generics/issues/issue-90455.rs new file mode 100644 index 0000000000000..a580410cf37ef --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-90455.rs @@ -0,0 +1,12 @@ +#![feature(generic_const_exprs, adt_const_params)] +#![allow(incomplete_features)] + +struct FieldElement { + n: [u64; num_limbs(N)], + //~^ ERROR unconstrained generic constant +} +const fn num_limbs(_: &str) -> usize { + 0 +} + +fn main() {} diff --git a/src/test/ui/const-generics/issues/issue-90455.stderr b/src/test/ui/const-generics/issues/issue-90455.stderr new file mode 100644 index 0000000000000..724d7f42e69d1 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-90455.stderr @@ -0,0 +1,10 @@ +error: unconstrained generic constant + --> $DIR/issue-90455.rs:5:8 + | +LL | n: [u64; num_limbs(N)], + | ^^^^^^^^^^^^^^^^^^^ + | + = help: try adding a `where` bound using this expression: `where [(); num_limbs(N)]:` + +error: aborting due to previous error + From 8ff50fe2736d8d33e2c51105cbabf023eff93218 Mon Sep 17 00:00:00 2001 From: b-naber Date: Thu, 2 Dec 2021 12:43:36 +0100 Subject: [PATCH 2/3] skip reborrows during AbstractConst building --- .../src/traits/const_evaluatable.rs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs index 8edb7069fc45f..be09434771235 100644 --- a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs +++ b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs @@ -399,13 +399,25 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { let arg = self.recurse_build(source)?; self.nodes.push(Node::Cast(abstract_const::CastKind::As, arg, node.ty)) } - + ExprKind::Borrow{ arg, ..} => { + let arg_node = &self.body.exprs[*arg]; + + // Skip reborrows for now until we allow Deref/Borrow/AddressOf + // expressions. + // FIXME(generic_const_exprs): Verify/explain why this is sound + if let ExprKind::Deref {arg} = arg_node.kind { + self.recurse_build(arg)? + } else { + self.maybe_supported_error( + node.span, + "borrowing is not supported in generic constants", + )? + } + } // FIXME(generic_const_exprs): We may want to support these. - ExprKind::AddressOf { .. } - | ExprKind::Borrow { .. } - | ExprKind::Deref { .. } => self.maybe_supported_error( + ExprKind::AddressOf { .. } | ExprKind::Deref {..}=> self.maybe_supported_error( node.span, - "dereferencing is not supported in generic constants", + "dereferencing or taking the address is not supported in generic constants", )?, ExprKind::Repeat { .. } | ExprKind::Array { .. } => self.maybe_supported_error( node.span, From 1777f431ad3afa1c19ceed8b11b211dbe18b558c Mon Sep 17 00:00:00 2001 From: b-naber Date: Sun, 5 Dec 2021 11:47:07 +0100 Subject: [PATCH 3/3] bless tests --- src/test/ui/const-generics/generic_const_exprs/closures.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/const-generics/generic_const_exprs/closures.stderr b/src/test/ui/const-generics/generic_const_exprs/closures.stderr index 0dfd804be41b4..18010413b9394 100644 --- a/src/test/ui/const-generics/generic_const_exprs/closures.stderr +++ b/src/test/ui/const-generics/generic_const_exprs/closures.stderr @@ -4,7 +4,7 @@ error: overly complex generic constant LL | fn test() -> [u8; N + (|| 42)()] {} | ^^^^-------^^ | | - | dereferencing is not supported in generic constants + | borrowing is not supported in generic constants | = help: consider moving this anonymous constant into a `const` function = note: this operation may be supported in the future