Skip to content

Commit 5da1a04

Browse files
committed
Allow impl ~const Trait opaque types
1 parent b70baa4 commit 5da1a04

File tree

4 files changed

+37
-58
lines changed

4 files changed

+37
-58
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ enum SelfSemantic {
4242
/// What is the context that prevents using `~const`?
4343
enum DisallowTildeConstContext<'a> {
4444
TraitObject,
45-
ImplTrait,
4645
Fn(FnKind<'a>),
4746
}
4847

@@ -187,11 +186,7 @@ impl<'a> AstValidator<'a> {
187186

188187
fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
189188
let old = mem::replace(&mut self.outer_impl_trait, outer);
190-
if outer.is_some() {
191-
self.with_banned_tilde_const(DisallowTildeConstContext::ImplTrait, f);
192-
} else {
193-
f(self);
194-
}
189+
f(self);
195190
self.outer_impl_trait = old;
196191
}
197192

@@ -1384,7 +1379,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13841379
let mut err = self.err_handler().struct_span_err(bound.span(), "`~const` is not allowed here");
13851380
match reason {
13861381
DisallowTildeConstContext::TraitObject => err.note("trait objects cannot have `~const` trait bounds"),
1387-
DisallowTildeConstContext::ImplTrait => err.note("`impl Trait`s cannot have `~const` trait bounds"),
13881382
DisallowTildeConstContext::Fn(FnKind::Closure(..)) => err.note("closures cannot have `~const` trait bounds"),
13891383
DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => err.span_note(ident.span, "this function is not `const`, so it cannot have `~const` trait bounds"),
13901384
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// check-pass
2+
#![feature(associated_type_bounds, const_trait_impl, const_cmp)]
3+
4+
use std::marker::Destruct;
5+
6+
const fn cmp(a: &impl ~const PartialEq) -> bool {
7+
a == a
8+
}
9+
10+
const fn wrap(x: impl ~const PartialEq + ~const Destruct) -> impl ~const PartialEq + ~const Destruct {
11+
x
12+
}
13+
14+
const _: () = {
15+
assert!(cmp(&0xDEADBEEFu32));
16+
assert!(cmp(&()));
17+
assert!(wrap(123) == wrap(123));
18+
assert!(wrap(123) != wrap(456));
19+
};
20+
21+
#[const_trait]
22+
trait T {}
23+
struct S;
24+
impl const T for S {}
25+
26+
const fn rpit() -> impl ~const T { S }
27+
28+
const fn apit(_: impl ~const T + ~const Destruct) {}
29+
30+
const fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
31+
32+
const fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T> + ~const Destruct) {}
33+
34+
fn main() {}

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
#![feature(const_trait_impl)]
22
#![feature(associated_type_bounds)]
33

4-
#[const_trait]
5-
trait T {}
6-
struct S;
7-
impl T for S {}
8-
9-
fn rpit() -> impl ~const T { S }
10-
//~^ ERROR `~const` is not allowed
11-
12-
fn apit(_: impl ~const T) {}
13-
//~^ ERROR `~const` is not allowed
14-
15-
fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
16-
//~^ ERROR `~const` is not allowed
17-
18-
fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
19-
//~^ ERROR `~const` is not allowed
20-
214
struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
225
//~^ ERROR `~const` and `?` are mutually exclusive
236

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,8 @@
1-
error: `~const` is not allowed here
2-
--> $DIR/tilde-const-invalid-places.rs:9:19
3-
|
4-
LL | fn rpit() -> impl ~const T { S }
5-
| ^^^^^^^^
6-
|
7-
= note: `impl Trait`s cannot have `~const` trait bounds
8-
9-
error: `~const` is not allowed here
10-
--> $DIR/tilde-const-invalid-places.rs:12:17
11-
|
12-
LL | fn apit(_: impl ~const T) {}
13-
| ^^^^^^^^
14-
|
15-
= note: `impl Trait`s cannot have `~const` trait bounds
16-
17-
error: `~const` is not allowed here
18-
--> $DIR/tilde-const-invalid-places.rs:15:50
19-
|
20-
LL | fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
21-
| ^^^^^^^^
22-
|
23-
= note: `impl Trait`s cannot have `~const` trait bounds
24-
25-
error: `~const` is not allowed here
26-
--> $DIR/tilde-const-invalid-places.rs:18:48
27-
|
28-
LL | fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T>) {}
29-
| ^^^^^^^^
30-
|
31-
= note: `impl Trait`s cannot have `~const` trait bounds
32-
331
error: `~const` and `?` are mutually exclusive
34-
--> $DIR/tilde-const-invalid-places.rs:21:25
2+
--> $DIR/tilde-const-invalid-places.rs:4:25
353
|
364
LL | struct TildeQuestion<T: ~const ?Sized>(std::marker::PhantomData<T>);
375
| ^^^^^^^^^^^^^
386

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

0 commit comments

Comments
 (0)