allow complex expressions in assoc consts
This commit is contained in:
parent
0d54f571c1
commit
37c29adabc
@ -948,10 +948,14 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||||||
// Only impose the restrictions of `ConstRibKind` for an
|
// Only impose the restrictions of `ConstRibKind` for an
|
||||||
// actual constant expression in a provided default.
|
// actual constant expression in a provided default.
|
||||||
if let Some(expr) = default {
|
if let Some(expr) = default {
|
||||||
this.with_constant_rib(
|
// We allow arbitrary const expressions inside of associated consts,
|
||||||
expr.is_potential_trivial_const_param(),
|
// even if they are potentially not const evaluatable.
|
||||||
|this| this.visit_expr(expr),
|
//
|
||||||
);
|
// Type parameters can already be used and as associated consts are
|
||||||
|
// not used as part of the type system, this is far less surprising.
|
||||||
|
this.with_constant_rib(true, |this| {
|
||||||
|
this.visit_expr(expr)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AssocItemKind::Fn(_, _, generics, _) => {
|
AssocItemKind::Fn(_, _, generics, _) => {
|
||||||
@ -1225,7 +1229,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||||||
for item in impl_items {
|
for item in impl_items {
|
||||||
use crate::ResolutionError::*;
|
use crate::ResolutionError::*;
|
||||||
match &item.kind {
|
match &item.kind {
|
||||||
AssocItemKind::Const(_default, _ty, expr) => {
|
AssocItemKind::Const(_default, _ty, _expr) => {
|
||||||
debug!("resolve_implementation AssocItemKind::Const",);
|
debug!("resolve_implementation AssocItemKind::Const",);
|
||||||
// If this is a trait impl, ensure the const
|
// If this is a trait impl, ensure the const
|
||||||
// exists in trait
|
// exists in trait
|
||||||
@ -1236,18 +1240,14 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||||||
|n, s| ConstNotMemberOfTrait(n, s),
|
|n, s| ConstNotMemberOfTrait(n, s),
|
||||||
);
|
);
|
||||||
|
|
||||||
this.with_constant_rib(
|
// We allow arbitrary const expressions inside of associated consts,
|
||||||
expr.as_ref().map_or(false, |e| {
|
// even if they are potentially not const evaluatable.
|
||||||
e.is_potential_trivial_const_param()
|
//
|
||||||
}),
|
// Type parameters can already be used and as associated consts are
|
||||||
|this| {
|
// not used as part of the type system, this is far less surprising.
|
||||||
visit::walk_assoc_item(
|
this.with_constant_rib(true, |this| {
|
||||||
this,
|
visit::walk_assoc_item(this, item, AssocCtxt::Impl)
|
||||||
item,
|
});
|
||||||
AssocCtxt::Impl,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
AssocItemKind::Fn(_, _, generics, _) => {
|
AssocItemKind::Fn(_, _, generics, _) => {
|
||||||
// We also need a new scope for the impl item type parameters.
|
// We also need a new scope for the impl item type parameters.
|
||||||
|
18
src/test/ui/const-generics/min_const_generics/assoc_const.rs
Normal file
18
src/test/ui/const-generics/min_const_generics/assoc_const.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// check-pass
|
||||||
|
#![feature(min_const_generics)]
|
||||||
|
|
||||||
|
struct Foo<const N: usize>;
|
||||||
|
|
||||||
|
impl<const N: usize> Foo<N> {
|
||||||
|
const VALUE: usize = N * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait Bar {
|
||||||
|
const ASSOC: usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Bar for Foo<N> {
|
||||||
|
const ASSOC: usize = N * 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
@ -26,12 +26,4 @@ trait Foo {
|
|||||||
const ASSOC: usize;
|
const ASSOC: usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Foo for [u8; N] {
|
|
||||||
const ASSOC: usize = N + 1;
|
|
||||||
//~^ ERROR generic parameters must not be used inside of non trivial constant values
|
|
||||||
// FIXME(min_const_generics): We probably have to allow this as we can
|
|
||||||
// already allow referencing type parameters here on stable.
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -30,13 +30,5 @@ LL | let _ = [0; N + 1];
|
|||||||
|
|
|
|
||||||
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
|
||||||
|
|
||||||
error: generic parameters must not be used inside of non trivial constant values
|
error: aborting due to 4 previous errors
|
||||||
--> $DIR/complex-expression.rs:30:26
|
|
||||||
|
|
|
||||||
LL | const ASSOC: usize = N + 1;
|
|
||||||
| ^ non-trivial anonymous constants must not depend on the parameter `N`
|
|
||||||
|
|
|
||||||
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
|
|
||||||
|
|
||||||
error: aborting due to 5 previous errors
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user