Skip to content

Commit 22797ef

Browse files
authored
Rollup merge of #105725 - fee1-dead-contrib:allow-impl-const-trait, r=oli-obk
Allow `impl ~const Trait` opaque types
2 parents 01ef4b2 + a8b9e00 commit 22797ef

File tree

4 files changed

+58
-58
lines changed

4 files changed

+58
-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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// check-pass
2+
#![allow(incomplete_features)]
3+
#![feature(
4+
associated_type_bounds,
5+
const_trait_impl,
6+
const_cmp,
7+
return_position_impl_trait_in_trait,
8+
)]
9+
10+
use std::marker::Destruct;
11+
12+
const fn cmp(a: &impl ~const PartialEq) -> bool {
13+
a == a
14+
}
15+
16+
const fn wrap(x: impl ~const PartialEq + ~const Destruct)
17+
-> impl ~const PartialEq + ~const Destruct
18+
{
19+
x
20+
}
21+
22+
#[const_trait]
23+
trait Foo {
24+
fn huh() -> impl ~const PartialEq + ~const Destruct + Copy;
25+
}
26+
27+
impl const Foo for () {
28+
fn huh() -> impl ~const PartialEq + ~const Destruct + Copy {
29+
123
30+
}
31+
}
32+
33+
const _: () = {
34+
assert!(cmp(&0xDEADBEEFu32));
35+
assert!(cmp(&()));
36+
assert!(wrap(123) == wrap(123));
37+
assert!(wrap(123) != wrap(456));
38+
let x = <() as Foo>::huh();
39+
assert!(x == x);
40+
};
41+
42+
#[const_trait]
43+
trait T {}
44+
struct S;
45+
impl const T for S {}
46+
47+
const fn rpit() -> impl ~const T { S }
48+
49+
const fn apit(_: impl ~const T + ~const Destruct) {}
50+
51+
const fn rpit_assoc_bound() -> impl IntoIterator<Item: ~const T> { Some(S) }
52+
53+
const fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T> + ~const Destruct) {}
54+
55+
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)