Skip to content

Commit 37c29ad

Browse files
committed
allow complex expressions in assoc consts
1 parent 0d54f57 commit 37c29ad

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

src/librustc_resolve/late.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,14 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
948948
// Only impose the restrictions of `ConstRibKind` for an
949949
// actual constant expression in a provided default.
950950
if let Some(expr) = default {
951-
this.with_constant_rib(
952-
expr.is_potential_trivial_const_param(),
953-
|this| this.visit_expr(expr),
954-
);
951+
// We allow arbitrary const expressions inside of associated consts,
952+
// even if they are potentially not const evaluatable.
953+
//
954+
// Type parameters can already be used and as associated consts are
955+
// not used as part of the type system, this is far less surprising.
956+
this.with_constant_rib(true, |this| {
957+
this.visit_expr(expr)
958+
});
955959
}
956960
}
957961
AssocItemKind::Fn(_, _, generics, _) => {
@@ -1225,7 +1229,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
12251229
for item in impl_items {
12261230
use crate::ResolutionError::*;
12271231
match &item.kind {
1228-
AssocItemKind::Const(_default, _ty, expr) => {
1232+
AssocItemKind::Const(_default, _ty, _expr) => {
12291233
debug!("resolve_implementation AssocItemKind::Const",);
12301234
// If this is a trait impl, ensure the const
12311235
// exists in trait
@@ -1236,18 +1240,14 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
12361240
|n, s| ConstNotMemberOfTrait(n, s),
12371241
);
12381242

1239-
this.with_constant_rib(
1240-
expr.as_ref().map_or(false, |e| {
1241-
e.is_potential_trivial_const_param()
1242-
}),
1243-
|this| {
1244-
visit::walk_assoc_item(
1245-
this,
1246-
item,
1247-
AssocCtxt::Impl,
1248-
)
1249-
},
1250-
);
1243+
// We allow arbitrary const expressions inside of associated consts,
1244+
// even if they are potentially not const evaluatable.
1245+
//
1246+
// Type parameters can already be used and as associated consts are
1247+
// not used as part of the type system, this is far less surprising.
1248+
this.with_constant_rib(true, |this| {
1249+
visit::walk_assoc_item(this, item, AssocCtxt::Impl)
1250+
});
12511251
}
12521252
AssocItemKind::Fn(_, _, generics, _) => {
12531253
// We also need a new scope for the impl item type parameters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
#![feature(min_const_generics)]
3+
4+
struct Foo<const N: usize>;
5+
6+
impl<const N: usize> Foo<N> {
7+
const VALUE: usize = N * 2;
8+
}
9+
10+
trait Bar {
11+
const ASSOC: usize;
12+
}
13+
14+
impl<const N: usize> Bar for Foo<N> {
15+
const ASSOC: usize = N * 3;
16+
}
17+
18+
fn main() {}

src/test/ui/const-generics/min_const_generics/complex-expression.rs

-8
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,4 @@ trait Foo {
2626
const ASSOC: usize;
2727
}
2828

29-
impl<const N: usize> Foo for [u8; N] {
30-
const ASSOC: usize = N + 1;
31-
//~^ ERROR generic parameters must not be used inside of non trivial constant values
32-
// FIXME(min_const_generics): We probably have to allow this as we can
33-
// already allow referencing type parameters here on stable.
34-
}
35-
36-
3729
fn main() {}

src/test/ui/const-generics/min_const_generics/complex-expression.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,5 @@ LL | let _ = [0; N + 1];
3030
|
3131
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
3232

33-
error: generic parameters must not be used inside of non trivial constant values
34-
--> $DIR/complex-expression.rs:30:26
35-
|
36-
LL | const ASSOC: usize = N + 1;
37-
| ^ non-trivial anonymous constants must not depend on the parameter `N`
38-
|
39-
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
40-
41-
error: aborting due to 5 previous errors
33+
error: aborting due to 4 previous errors
4234

0 commit comments

Comments
 (0)