Skip to content

Commit

Permalink
Rollup merge of #90529 - b-naber:reborrows-consts, r=lcnr
Browse files Browse the repository at this point in the history
Skip reborrows in AbstractConstBuilder

Fixes #90455

Temporary fix to prevent confusing diagnostics that refer to implicit borrows and derefs until we allow borrows and derefs on constant expressions.

r? `@oli-obk`
  • Loading branch information
matthiaskrgr authored Dec 5, 2021
2 parents cafc458 + 1777f43 commit 214b2a1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
22 changes: 17 additions & 5 deletions compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: overly complex generic constant
LL | fn test<const N: usize>() -> [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
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/const-generics/issues/issue-90455.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(generic_const_exprs, adt_const_params)]
#![allow(incomplete_features)]

struct FieldElement<const N: &'static str> {
n: [u64; num_limbs(N)],
//~^ ERROR unconstrained generic constant
}
const fn num_limbs(_: &str) -> usize {
0
}

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/const-generics/issues/issue-90455.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 214b2a1

Please sign in to comment.