Skip to content

Commit 523490e

Browse files
committed
Allow ~const bounds on trait assoc functions
1 parent ac50a53 commit 523490e

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14421442
if !self.is_tilde_const_allowed {
14431443
self.err_handler()
14441444
.struct_span_err(bound.span(), "`~const` is not allowed here")
1445-
.note("only allowed on bounds on traits' associated types, const fns, const impls and its associated functions")
1445+
.note("only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions")
14461446
.emit();
14471447
}
14481448
}
@@ -1616,7 +1616,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16161616
walk_list!(self, visit_ty, ty);
16171617
}
16181618
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body))
1619-
if self.in_const_trait_impl =>
1619+
if self.in_const_trait_impl || ctxt == AssocCtxt::Trait =>
16201620
{
16211621
self.visit_vis(&item.vis);
16221622
self.visit_ident(item.ident);

Diff for: src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,47 @@ error: `~const` is not allowed here
44
LL | fn rpit() -> impl ~const T { S }
55
| ^^^^^^^^
66
|
7-
= note: only allowed on bounds on traits' associated types, const fns, const impls and its associated functions
7+
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
88

99
error: `~const` is not allowed here
1010
--> $DIR/tilde-const-invalid-places.rs:11:17
1111
|
1212
LL | fn apit(_: impl ~const T) {}
1313
| ^^^^^^^^
1414
|
15-
= note: only allowed on bounds on traits' associated types, const fns, const impls and its associated functions
15+
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
1616

1717
error: `~const` is not allowed here
1818
--> $DIR/tilde-const-invalid-places.rs:14:50
1919
|
2020
LL | fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
2121
| ^^^^^^^^
2222
|
23-
= note: only allowed on bounds on traits' associated types, const fns, const impls and its associated functions
23+
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
2424

2525
error: `~const` is not allowed here
2626
--> $DIR/tilde-const-invalid-places.rs:17:48
2727
|
2828
LL | fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
2929
| ^^^^^^^^
3030
|
31-
= note: only allowed on bounds on traits' associated types, const fns, const impls and its associated functions
31+
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
3232

3333
error: `~const` is not allowed here
3434
--> $DIR/tilde-const-invalid-places.rs:20:15
3535
|
3636
LL | fn generic<P: ~const T>() {}
3737
| ^^^^^^^^
3838
|
39-
= note: only allowed on bounds on traits' associated types, const fns, const impls and its associated functions
39+
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
4040

4141
error: `~const` is not allowed here
4242
--> $DIR/tilde-const-invalid-places.rs:23:31
4343
|
4444
LL | fn where_clause<P>() where P: ~const T {}
4545
| ^^^^^^^^
4646
|
47-
= note: only allowed on bounds on traits' associated types, const fns, const impls and its associated functions
47+
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
4848

4949
error: `~const` and `?` are mutually exclusive
5050
--> $DIR/tilde-const-invalid-places.rs:26:25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![feature(const_fn_trait_bound)]
2+
#![feature(const_trait_impl)]
3+
4+
trait Bar {}
5+
6+
trait Foo {
7+
fn a();
8+
fn b() where Self: ~const Bar;
9+
}
10+
11+
const fn test1<T: ~const Foo + Bar>() {
12+
T::a();
13+
T::b();
14+
//~^ ERROR the trait bound
15+
}
16+
17+
const fn test2<T: ~const Foo + ~const Bar>() {
18+
T::a();
19+
T::b();
20+
}
21+
22+
fn test3<T: Foo>() {
23+
T::a();
24+
T::b();
25+
//~^ ERROR the trait bound
26+
}
27+
28+
fn test4<T: Foo + Bar>() {
29+
T::a();
30+
T::b();
31+
}
32+
33+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0277]: the trait bound `T: Bar` is not satisfied
2+
--> $DIR/trait-where-clause.rs:13:5
3+
|
4+
LL | T::b();
5+
| ^^^^ the trait `Bar` is not implemented for `T`
6+
|
7+
note: required by `Foo::b`
8+
--> $DIR/trait-where-clause.rs:8:5
9+
|
10+
LL | fn b() where Self: ~const Bar;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
help: consider further restricting this bound
13+
|
14+
LL | const fn test1<T: ~const Foo + Bar + Bar>() {
15+
| +++++
16+
17+
error[E0277]: the trait bound `T: Bar` is not satisfied
18+
--> $DIR/trait-where-clause.rs:24:5
19+
|
20+
LL | T::b();
21+
| ^^^^ the trait `Bar` is not implemented for `T`
22+
|
23+
note: required by `Foo::b`
24+
--> $DIR/trait-where-clause.rs:8:5
25+
|
26+
LL | fn b() where Self: ~const Bar;
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
help: consider further restricting this bound
29+
|
30+
LL | fn test3<T: Foo + Bar>() {
31+
| +++++
32+
33+
error: aborting due to 2 previous errors
34+
35+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)