mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Allow impl ~const Trait
opaque types
This commit is contained in:
parent
b70baa4f92
commit
5da1a04278
@ -42,7 +42,6 @@ enum SelfSemantic {
|
|||||||
/// What is the context that prevents using `~const`?
|
/// What is the context that prevents using `~const`?
|
||||||
enum DisallowTildeConstContext<'a> {
|
enum DisallowTildeConstContext<'a> {
|
||||||
TraitObject,
|
TraitObject,
|
||||||
ImplTrait,
|
|
||||||
Fn(FnKind<'a>),
|
Fn(FnKind<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,11 +186,7 @@ impl<'a> AstValidator<'a> {
|
|||||||
|
|
||||||
fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
|
fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
|
||||||
let old = mem::replace(&mut self.outer_impl_trait, outer);
|
let old = mem::replace(&mut self.outer_impl_trait, outer);
|
||||||
if outer.is_some() {
|
f(self);
|
||||||
self.with_banned_tilde_const(DisallowTildeConstContext::ImplTrait, f);
|
|
||||||
} else {
|
|
||||||
f(self);
|
|
||||||
}
|
|
||||||
self.outer_impl_trait = old;
|
self.outer_impl_trait = old;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1384,7 +1379,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
|||||||
let mut err = self.err_handler().struct_span_err(bound.span(), "`~const` is not allowed here");
|
let mut err = self.err_handler().struct_span_err(bound.span(), "`~const` is not allowed here");
|
||||||
match reason {
|
match reason {
|
||||||
DisallowTildeConstContext::TraitObject => err.note("trait objects cannot have `~const` trait bounds"),
|
DisallowTildeConstContext::TraitObject => err.note("trait objects cannot have `~const` trait bounds"),
|
||||||
DisallowTildeConstContext::ImplTrait => err.note("`impl Trait`s cannot have `~const` trait bounds"),
|
|
||||||
DisallowTildeConstContext::Fn(FnKind::Closure(..)) => err.note("closures cannot have `~const` trait bounds"),
|
DisallowTildeConstContext::Fn(FnKind::Closure(..)) => err.note("closures cannot have `~const` trait bounds"),
|
||||||
DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => err.span_note(ident.span, "this function is not `const`, so it cannot have `~const` trait bounds"),
|
DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => err.span_note(ident.span, "this function is not `const`, so it cannot have `~const` trait bounds"),
|
||||||
};
|
};
|
||||||
|
34
src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs
Normal file
34
src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// check-pass
|
||||||
|
#![feature(associated_type_bounds, const_trait_impl, const_cmp)]
|
||||||
|
|
||||||
|
use std::marker::Destruct;
|
||||||
|
|
||||||
|
const fn cmp(a: &impl ~const PartialEq) -> bool {
|
||||||
|
a == a
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn wrap(x: impl ~const PartialEq + ~const Destruct) -> impl ~const PartialEq + ~const Destruct {
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
|
const _: () = {
|
||||||
|
assert!(cmp(&0xDEADBEEFu32));
|
||||||
|
assert!(cmp(&()));
|
||||||
|
assert!(wrap(123) == wrap(123));
|
||||||
|
assert!(wrap(123) != wrap(456));
|
||||||
|
};
|
||||||
|
|
||||||
|
#[const_trait]
|
||||||
|
trait T {}
|
||||||
|
struct S;
|
||||||
|
impl const T for S {}
|
||||||
|
|
||||||
|
const fn rpit() -> impl ~const T { S }
|
||||||
|
|
||||||
|
const fn apit(_: impl ~const T + ~const Destruct) {}
|
||||||
|
|
||||||
|
const fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
|
||||||
|
|
||||||
|
const fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T> + ~const Destruct) {}
|
||||||
|
|
||||||
|
fn main() {}
|
@ -1,23 +1,6 @@
|
|||||||
#![feature(const_trait_impl)]
|
#![feature(const_trait_impl)]
|
||||||
#![feature(associated_type_bounds)]
|
#![feature(associated_type_bounds)]
|
||||||
|
|
||||||
#[const_trait]
|
|
||||||
trait T {}
|
|
||||||
struct S;
|
|
||||||
impl T for S {}
|
|
||||||
|
|
||||||
fn rpit() -> impl ~const T { S }
|
|
||||||
//~^ ERROR `~const` is not allowed
|
|
||||||
|
|
||||||
fn apit(_: impl ~const T) {}
|
|
||||||
//~^ ERROR `~const` is not allowed
|
|
||||||
|
|
||||||
fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
|
|
||||||
//~^ ERROR `~const` is not allowed
|
|
||||||
|
|
||||||
fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
|
|
||||||
//~^ ERROR `~const` is not allowed
|
|
||||||
|
|
||||||
struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
|
struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
|
||||||
//~^ ERROR `~const` and `?` are mutually exclusive
|
//~^ ERROR `~const` and `?` are mutually exclusive
|
||||||
|
|
||||||
|
@ -1,40 +1,8 @@
|
|||||||
error: `~const` is not allowed here
|
|
||||||
--> $DIR/tilde-const-invalid-places.rs:9:19
|
|
||||||
|
|
|
||||||
LL | fn rpit() -> impl ~const T { S }
|
|
||||||
| ^^^^^^^^
|
|
||||||
|
|
|
||||||
= note: `impl Trait`s cannot have `~const` trait bounds
|
|
||||||
|
|
||||||
error: `~const` is not allowed here
|
|
||||||
--> $DIR/tilde-const-invalid-places.rs:12:17
|
|
||||||
|
|
|
||||||
LL | fn apit(_: impl ~const T) {}
|
|
||||||
| ^^^^^^^^
|
|
||||||
|
|
|
||||||
= note: `impl Trait`s cannot have `~const` trait bounds
|
|
||||||
|
|
||||||
error: `~const` is not allowed here
|
|
||||||
--> $DIR/tilde-const-invalid-places.rs:15:50
|
|
||||||
|
|
|
||||||
LL | fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
|
|
||||||
| ^^^^^^^^
|
|
||||||
|
|
|
||||||
= note: `impl Trait`s cannot have `~const` trait bounds
|
|
||||||
|
|
||||||
error: `~const` is not allowed here
|
|
||||||
--> $DIR/tilde-const-invalid-places.rs:18:48
|
|
||||||
|
|
|
||||||
LL | fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
|
|
||||||
| ^^^^^^^^
|
|
||||||
|
|
|
||||||
= note: `impl Trait`s cannot have `~const` trait bounds
|
|
||||||
|
|
||||||
error: `~const` and `?` are mutually exclusive
|
error: `~const` and `?` are mutually exclusive
|
||||||
--> $DIR/tilde-const-invalid-places.rs:21:25
|
--> $DIR/tilde-const-invalid-places.rs:4:25
|
||||||
|
|
|
|
||||||
LL | struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
|
LL | struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
|
||||||
| ^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 5 previous errors
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user