Skip to content

Commit e9bec2f

Browse files
authored
Rollup merge of #102273 - woppopo:relax_const_bound, r=fee1-dead
Allow `~const` bounds on non-const functions Makes the behavior of bound of trait-associated functions and non-associated functions consistent.
2 parents 0857dde + e4b08ab commit e9bec2f

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14151415
if !self.is_tilde_const_allowed {
14161416
self.err_handler()
14171417
.struct_span_err(bound.span(), "`~const` is not allowed here")
1418-
.note("only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions")
1418+
.note("only allowed on bounds on functions, traits' associated types and functions, const impls and its associated functions")
14191419
.emit();
14201420
}
14211421
}
@@ -1523,9 +1523,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15231523
});
15241524
}
15251525

1526-
let tilde_const_allowed =
1527-
matches!(fk.header(), Some(FnHeader { constness: Const::Yes(_), .. }))
1528-
|| matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)));
1526+
let tilde_const_allowed = matches!(fk.header(), Some(FnHeader { .. }))
1527+
|| matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)));
15291528

15301529
self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk));
15311530
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// check-pass
2+
#![feature(const_trait_impl)]
3+
#![feature(generic_arg_infer)]
4+
#![feature(generic_const_exprs)]
5+
#![allow(incomplete_features)]
6+
7+
struct Foo<const N: usize>;
8+
9+
impl<const N: usize> Foo<N> {
10+
fn add<A: ~const Add42>(self) -> Foo<{ A::add(N) }> {
11+
Foo
12+
}
13+
}
14+
15+
#[const_trait]
16+
trait Add42 {
17+
fn add(a: usize) -> usize;
18+
}
19+
20+
impl const Add42 for () {
21+
fn add(a: usize) -> usize {
22+
a + 42
23+
}
24+
}
25+
26+
fn bar<A: ~const Add42, const N: usize>(_: Foo<N>) -> Foo<{ A::add(N) }> {
27+
Foo
28+
}
29+
30+
fn main() {
31+
let foo = Foo::<0>;
32+
let foo = bar::<(), _>(foo);
33+
let _foo = bar::<(), _>(foo);
34+
}

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

-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
1717
fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
1818
//~^ ERROR `~const` is not allowed
1919

20-
fn generic<P: ~const T>() {}
21-
//~^ ERROR `~const` is not allowed
22-
23-
fn where_clause<P>() where P: ~const T {}
24-
//~^ ERROR `~const` is not allowed
25-
2620
struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
2721
//~^ ERROR `~const` and `?` are mutually exclusive
2822

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

+6-22
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,37 @@ 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 and functions, const fns, const impls and its associated functions
7+
= note: only allowed on bounds on functions, traits' associated types and functions, 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 and functions, const fns, const impls and its associated functions
15+
= note: only allowed on bounds on functions, traits' associated types and functions, 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 and functions, const fns, const impls and its associated functions
23+
= note: only allowed on bounds on functions, traits' associated types and functions, 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 and functions, const fns, const impls and its associated functions
32-
33-
error: `~const` is not allowed here
34-
--> $DIR/tilde-const-invalid-places.rs:20:15
35-
|
36-
LL | fn generic<P: ~const T>() {}
37-
| ^^^^^^^^
38-
|
39-
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
40-
41-
error: `~const` is not allowed here
42-
--> $DIR/tilde-const-invalid-places.rs:23:31
43-
|
44-
LL | fn where_clause<P>() where P: ~const T {}
45-
| ^^^^^^^^
46-
|
47-
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
31+
= note: only allowed on bounds on functions, traits' associated types and functions, const impls and its associated functions
4832

4933
error: `~const` and `?` are mutually exclusive
50-
--> $DIR/tilde-const-invalid-places.rs:26:25
34+
--> $DIR/tilde-const-invalid-places.rs:20:25
5135
|
5236
LL | struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
5337
| ^^^^^^^^^^^^^
5438

55-
error: aborting due to 7 previous errors
39+
error: aborting due to 5 previous errors
5640

0 commit comments

Comments
 (0)