Skip to content

Commit 214b2a1

Browse files
authored
Rollup merge of #90529 - b-naber:reborrows-consts, r=lcnr
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`
2 parents cafc458 + 1777f43 commit 214b2a1

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,25 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
399399
let arg = self.recurse_build(source)?;
400400
self.nodes.push(Node::Cast(abstract_const::CastKind::As, arg, node.ty))
401401
}
402-
402+
ExprKind::Borrow{ arg, ..} => {
403+
let arg_node = &self.body.exprs[*arg];
404+
405+
// Skip reborrows for now until we allow Deref/Borrow/AddressOf
406+
// expressions.
407+
// FIXME(generic_const_exprs): Verify/explain why this is sound
408+
if let ExprKind::Deref {arg} = arg_node.kind {
409+
self.recurse_build(arg)?
410+
} else {
411+
self.maybe_supported_error(
412+
node.span,
413+
"borrowing is not supported in generic constants",
414+
)?
415+
}
416+
}
403417
// FIXME(generic_const_exprs): We may want to support these.
404-
ExprKind::AddressOf { .. }
405-
| ExprKind::Borrow { .. }
406-
| ExprKind::Deref { .. } => self.maybe_supported_error(
418+
ExprKind::AddressOf { .. } | ExprKind::Deref {..}=> self.maybe_supported_error(
407419
node.span,
408-
"dereferencing is not supported in generic constants",
420+
"dereferencing or taking the address is not supported in generic constants",
409421
)?,
410422
ExprKind::Repeat { .. } | ExprKind::Array { .. } => self.maybe_supported_error(
411423
node.span,

src/test/ui/const-generics/generic_const_exprs/closures.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: overly complex generic constant
44
LL | fn test<const N: usize>() -> [u8; N + (|| 42)()] {}
55
| ^^^^-------^^
66
| |
7-
| dereferencing is not supported in generic constants
7+
| borrowing is not supported in generic constants
88
|
99
= help: consider moving this anonymous constant into a `const` function
1010
= note: this operation may be supported in the future
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(generic_const_exprs, adt_const_params)]
2+
#![allow(incomplete_features)]
3+
4+
struct FieldElement<const N: &'static str> {
5+
n: [u64; num_limbs(N)],
6+
//~^ ERROR unconstrained generic constant
7+
}
8+
const fn num_limbs(_: &str) -> usize {
9+
0
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: unconstrained generic constant
2+
--> $DIR/issue-90455.rs:5:8
3+
|
4+
LL | n: [u64; num_limbs(N)],
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: try adding a `where` bound using this expression: `where [(); num_limbs(N)]:`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)