Skip to content

Commit ab37e49

Browse files
authored
Rollup merge of rust-lang#88418 - fee1-dead:trait-assoc-tilde-const, r=oli-obk
Allow `~const` bounds on trait assoc functions r? `@oli-obk`
2 parents 4d08908 + ee02c8e commit ab37e49

File tree

6 files changed

+180
-8
lines changed

6 files changed

+180
-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,41 @@
1+
// run-pass
2+
3+
#![feature(const_trait_impl)]
4+
#![feature(const_fn_trait_bound)]
5+
6+
trait Bar {
7+
fn bar() -> u8;
8+
}
9+
10+
trait Foo {
11+
#[default_method_body_is_const]
12+
fn foo() -> u8 where Self: ~const Bar {
13+
<Self as Bar>::bar() * 6
14+
}
15+
}
16+
17+
struct NonConst;
18+
struct Const;
19+
20+
impl Bar for NonConst {
21+
fn bar() -> u8 {
22+
3
23+
}
24+
}
25+
26+
impl Foo for NonConst {}
27+
28+
impl const Bar for Const {
29+
fn bar() -> u8 {
30+
4
31+
}
32+
}
33+
34+
impl const Foo for Const {}
35+
36+
fn main() {
37+
const ANS1: u8 = Const::foo();
38+
let ans2 = NonConst::foo();
39+
40+
assert_eq!(ANS1 + ans2, 42);
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// check-pass
2+
3+
#![feature(const_trait_impl)]
4+
#![feature(const_fn_trait_bound)]
5+
6+
trait Foo {
7+
fn bar() where Self: ~const Foo;
8+
}
9+
10+
struct S;
11+
12+
impl Foo for S {
13+
fn bar() {}
14+
}
15+
16+
fn baz<T: Foo>() {
17+
T::bar();
18+
}
19+
20+
const fn qux<T: ~const Foo>() {
21+
T::bar();
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
fn c<T: ~const Bar>();
10+
}
11+
12+
const fn test1<T: ~const Foo + Bar>() {
13+
T::a();
14+
T::b();
15+
//~^ ERROR the trait bound
16+
T::c::<T>();
17+
//~^ ERROR the trait bound
18+
}
19+
20+
const fn test2<T: ~const Foo + ~const Bar>() {
21+
T::a();
22+
T::b();
23+
T::c::<T>();
24+
}
25+
26+
fn test3<T: Foo>() {
27+
T::a();
28+
T::b();
29+
//~^ ERROR the trait bound
30+
T::c::<T>();
31+
//~^ ERROR the trait bound
32+
}
33+
34+
fn test4<T: Foo + Bar>() {
35+
T::a();
36+
T::b();
37+
T::c::<T>();
38+
}
39+
40+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
error[E0277]: the trait bound `T: Bar` is not satisfied
2+
--> $DIR/trait-where-clause.rs:14: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:16:5
19+
|
20+
LL | T::c::<T>();
21+
| ^^^^^^^^^ the trait `Bar` is not implemented for `T`
22+
|
23+
note: required by `Foo::c`
24+
--> $DIR/trait-where-clause.rs:9:5
25+
|
26+
LL | fn c<T: ~const Bar>();
27+
| ^^^^^^^^^^^^^^^^^^^^^^
28+
help: consider further restricting this bound
29+
|
30+
LL | const fn test1<T: ~const Foo + Bar + Bar>() {
31+
| +++++
32+
33+
error[E0277]: the trait bound `T: Bar` is not satisfied
34+
--> $DIR/trait-where-clause.rs:28:5
35+
|
36+
LL | T::b();
37+
| ^^^^ the trait `Bar` is not implemented for `T`
38+
|
39+
note: required by `Foo::b`
40+
--> $DIR/trait-where-clause.rs:8:5
41+
|
42+
LL | fn b() where Self: ~const Bar;
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
help: consider further restricting this bound
45+
|
46+
LL | fn test3<T: Foo + Bar>() {
47+
| +++++
48+
49+
error[E0277]: the trait bound `T: Bar` is not satisfied
50+
--> $DIR/trait-where-clause.rs:30:5
51+
|
52+
LL | T::c::<T>();
53+
| ^^^^^^^^^ the trait `Bar` is not implemented for `T`
54+
|
55+
note: required by `Foo::c`
56+
--> $DIR/trait-where-clause.rs:9:5
57+
|
58+
LL | fn c<T: ~const Bar>();
59+
| ^^^^^^^^^^^^^^^^^^^^^^
60+
help: consider further restricting this bound
61+
|
62+
LL | fn test3<T: Foo + Bar>() {
63+
| +++++
64+
65+
error: aborting due to 4 previous errors
66+
67+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)