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.
This commit is contained in:
Matthias Krüger 2022-09-26 19:19:21 +02:00 committed by GitHub
commit e9bec2fdd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 32 deletions

View file

@ -0,0 +1,34 @@
// check-pass
#![feature(const_trait_impl)]
#![feature(generic_arg_infer)]
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]
struct Foo<const N: usize>;
impl<const N: usize> Foo<N> {
fn add<A: ~const Add42>(self) -> Foo<{ A::add(N) }> {
Foo
}
}
#[const_trait]
trait Add42 {
fn add(a: usize) -> usize;
}
impl const Add42 for () {
fn add(a: usize) -> usize {
a + 42
}
}
fn bar<A: ~const Add42, const N: usize>(_: Foo<N>) -> Foo<{ A::add(N) }> {
Foo
}
fn main() {
let foo = Foo::<0>;
let foo = bar::<(), _>(foo);
let _foo = bar::<(), _>(foo);
}

View file

@ -17,12 +17,6 @@ fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
//~^ ERROR `~const` is not allowed
fn generic<P: ~const T>() {}
//~^ ERROR `~const` is not allowed
fn where_clause<P>() where P: ~const T {}
//~^ ERROR `~const` is not allowed
struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
//~^ ERROR `~const` and `?` are mutually exclusive

View file

@ -4,7 +4,7 @@ error: `~const` is not allowed here
LL | fn rpit() -> impl ~const T { S }
| ^^^^^^^^
|
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
= note: only allowed on bounds on functions, traits' associated types and functions, const impls and its associated functions
error: `~const` is not allowed here
--> $DIR/tilde-const-invalid-places.rs:11:17
@ -12,7 +12,7 @@ error: `~const` is not allowed here
LL | fn apit(_: impl ~const T) {}
| ^^^^^^^^
|
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
= note: only allowed on bounds on functions, traits' associated types and functions, const impls and its associated functions
error: `~const` is not allowed here
--> $DIR/tilde-const-invalid-places.rs:14:50
@ -20,7 +20,7 @@ error: `~const` is not allowed here
LL | fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
| ^^^^^^^^
|
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
= note: only allowed on bounds on functions, traits' associated types and functions, const impls and its associated functions
error: `~const` is not allowed here
--> $DIR/tilde-const-invalid-places.rs:17:48
@ -28,29 +28,13 @@ error: `~const` is not allowed here
LL | fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
| ^^^^^^^^
|
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
error: `~const` is not allowed here
--> $DIR/tilde-const-invalid-places.rs:20:15
|
LL | fn generic<P: ~const T>() {}
| ^^^^^^^^
|
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
error: `~const` is not allowed here
--> $DIR/tilde-const-invalid-places.rs:23:31
|
LL | fn where_clause<P>() where P: ~const T {}
| ^^^^^^^^
|
= note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
= note: only allowed on bounds on functions, traits' associated types and functions, const impls and its associated functions
error: `~const` and `?` are mutually exclusive
--> $DIR/tilde-const-invalid-places.rs:26:25
--> $DIR/tilde-const-invalid-places.rs:20:25
|
LL | struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
| ^^^^^^^^^^^^^
error: aborting due to 7 previous errors
error: aborting due to 5 previous errors