Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip reborrows in AbstractConstBuilder #90529

Merged
merged 3 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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