Skip to content

Commit 79ae722

Browse files
committed
Apply changes as per @fee1-dead's comments in rust-lang#116210.
1 parent 38698c7 commit 79ae722

File tree

5 files changed

+34
-55
lines changed

5 files changed

+34
-55
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ struct AstValidator<'a> {
5252
/// Are we inside a trait impl?
5353
in_trait_impl: bool,
5454

55-
in_const_trait_impl: bool,
56-
57-
/// Are we inside a const trait defn?
58-
in_const_trait_defn: bool,
55+
/// Are we inside a const trait defn or impl?
56+
in_const_trait: bool,
5957

6058
has_proc_macro_decls: bool,
6159

@@ -82,16 +80,16 @@ impl<'a> AstValidator<'a> {
8280
) {
8381
let old = mem::replace(&mut self.in_trait_impl, is_in);
8482
let old_const =
85-
mem::replace(&mut self.in_const_trait_impl, matches!(constness, Some(Const::Yes(_))));
83+
mem::replace(&mut self.in_const_trait, matches!(constness, Some(Const::Yes(_))));
8684
f(self);
8785
self.in_trait_impl = old;
88-
self.in_const_trait_impl = old_const;
86+
self.in_const_trait = old_const;
8987
}
9088

9189
fn with_in_trait(&mut self, is_const: bool, f: impl FnOnce(&mut Self)) {
92-
let old = mem::replace(&mut self.in_const_trait_defn, is_const);
90+
let old = mem::replace(&mut self.in_const_trait, is_const);
9391
f(self);
94-
self.in_const_trait_defn = old;
92+
self.in_const_trait = old;
9593
}
9694

9795
fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
@@ -1289,16 +1287,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12891287
}
12901288

12911289
let tilde_const_allowed =
1292-
if matches!(fk.header(), Some(FnHeader { constness: ast::Const::Yes(_), .. })) {
1293-
true
1294-
} else if let Some(FnCtxt::Assoc(ctxt)) = fk.ctxt() {
1295-
match ctxt {
1296-
AssocCtxt::Trait => self.in_const_trait_defn,
1297-
AssocCtxt::Impl => self.in_const_trait_impl,
1298-
}
1299-
} else {
1300-
false
1301-
};
1290+
matches!(fk.header(), Some(FnHeader { constness: ast::Const::Yes(_), .. }))
1291+
|| (matches!(fk.ctxt(), Some(FnCtxt::Assoc(_))) && self.in_const_trait);
13021292

13031293
let disallowed = (!tilde_const_allowed).then(|| DisallowTildeConstContext::Fn(fk));
13041294

@@ -1383,7 +1373,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13831373
walk_list!(self, visit_ty, ty);
13841374
}
13851375
AssocItemKind::Fn(box Fn { sig, generics, body, .. })
1386-
if self.in_const_trait_impl
1376+
if self.in_const_trait
13871377
|| ctxt == AssocCtxt::Trait
13881378
|| matches!(sig.header.constness, Const::Yes(_)) =>
13891379
{
@@ -1530,8 +1520,7 @@ pub fn check_crate(
15301520
features,
15311521
extern_mod: None,
15321522
in_trait_impl: false,
1533-
in_const_trait_impl: false,
1534-
in_const_trait_defn: false,
1523+
in_const_trait: false,
15351524
has_proc_macro_decls: false,
15361525
outer_impl_trait: None,
15371526
disallow_tilde_const: None,

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ trait MyTrait {
66
}
77

88
trait OtherTrait {
9-
fn do_something_else()
10-
where
11-
Self: ~const MyTrait;
9+
fn do_something_else() where Self: ~const MyTrait;
1210
//~^ ERROR `~const` is not allowed here
1311
}
1412

@@ -19,11 +17,8 @@ impl const MyTrait for u32 {
1917
}
2018

2119
impl<T> MyStruct<T> {
22-
pub fn foo(&self)
23-
where
24-
T: ~const MyTrait,
20+
pub fn foo(&self) where T: ~const MyTrait {
2521
//~^ ERROR `~const` is not allowed here
26-
{
2722
self.0.do_something();
2823
}
2924
}

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error: `~const` is not allowed here
2-
--> $DIR/const-bound-on-not-const-associated-fn.rs:11:15
2+
--> $DIR/const-bound-on-not-const-associated-fn.rs:9:40
33
|
4-
LL | Self: ~const MyTrait;
5-
| ^^^^^^^^^^^^^^
4+
LL | fn do_something_else() where Self: ~const MyTrait;
5+
| ^^^^^^^^^^^^^^
66
|
77
note: this function is not `const`, so it cannot have `~const` trait bounds
88
--> $DIR/const-bound-on-not-const-associated-fn.rs:9:8
99
|
10-
LL | fn do_something_else()
10+
LL | fn do_something_else() where Self: ~const MyTrait;
1111
| ^^^^^^^^^^^^^^^^^
1212

1313
error: `~const` is not allowed here
14-
--> $DIR/const-bound-on-not-const-associated-fn.rs:24:12
14+
--> $DIR/const-bound-on-not-const-associated-fn.rs:20:32
1515
|
16-
LL | T: ~const MyTrait,
17-
| ^^^^^^^^^^^^^^
16+
LL | pub fn foo(&self) where T: ~const MyTrait {
17+
| ^^^^^^^^^^^^^^
1818
|
1919
note: this function is not `const`, so it cannot have `~const` trait bounds
20-
--> $DIR/const-bound-on-not-const-associated-fn.rs:22:12
20+
--> $DIR/const-bound-on-not-const-associated-fn.rs:20:12
2121
|
22-
LL | pub fn foo(&self)
22+
LL | pub fn foo(&self) where T: ~const MyTrait {
2323
| ^^^
2424

2525
error: aborting due to 2 previous errors

tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ trait Bar {}
55

66
trait Foo {
77
fn a();
8-
fn b()
9-
where
10-
Self: ~const Bar;
8+
fn b() where Self: ~const Bar;
119
//~^ ERROR `~const` is not allowed here
1210
fn c<T: ~const Bar>();
1311
//~^ ERROR `~const` is not allowed here

tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.stderr

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
11
error: `~const` is not allowed here
2-
--> $DIR/trait-where-clause.rs:10:15
2+
--> $DIR/trait-where-clause.rs:8:24
33
|
4-
LL | Self: ~const Bar;
5-
| ^^^^^^^^^^
4+
LL | fn b() where Self: ~const Bar;
5+
| ^^^^^^^^^^
66
|
77
note: this function is not `const`, so it cannot have `~const` trait bounds
88
--> $DIR/trait-where-clause.rs:8:8
99
|
10-
LL | fn b()
10+
LL | fn b() where Self: ~const Bar;
1111
| ^
1212

1313
error: `~const` is not allowed here
14-
--> $DIR/trait-where-clause.rs:12:13
14+
--> $DIR/trait-where-clause.rs:10:13
1515
|
1616
LL | fn c<T: ~const Bar>();
1717
| ^^^^^^^^^^
1818
|
1919
note: this function is not `const`, so it cannot have `~const` trait bounds
20-
--> $DIR/trait-where-clause.rs:12:8
20+
--> $DIR/trait-where-clause.rs:10:8
2121
|
2222
LL | fn c<T: ~const Bar>();
2323
| ^
2424

2525
error[E0277]: the trait bound `T: Bar` is not satisfied
26-
--> $DIR/trait-where-clause.rs:18:5
26+
--> $DIR/trait-where-clause.rs:16:5
2727
|
2828
LL | T::b();
2929
| ^ the trait `Bar` is not implemented for `T`
3030
|
3131
note: required by a bound in `Foo::b`
32-
--> $DIR/trait-where-clause.rs:10:15
32+
--> $DIR/trait-where-clause.rs:8:24
3333
|
34-
LL | fn b()
35-
| - required by a bound in this associated function
36-
LL | where
37-
LL | Self: ~const Bar;
38-
| ^^^^^^^^^^ required by this bound in `Foo::b`
34+
LL | fn b() where Self: ~const Bar;
35+
| ^^^^^^^^^^ required by this bound in `Foo::b`
3936
help: consider further restricting this bound
4037
|
4138
LL | fn test1<T: Foo + Bar>() {
4239
| +++++
4340

4441
error[E0277]: the trait bound `T: Bar` is not satisfied
45-
--> $DIR/trait-where-clause.rs:20:12
42+
--> $DIR/trait-where-clause.rs:18:12
4643
|
4744
LL | T::c::<T>();
4845
| ^ the trait `Bar` is not implemented for `T`
4946
|
5047
note: required by a bound in `Foo::c`
51-
--> $DIR/trait-where-clause.rs:12:13
48+
--> $DIR/trait-where-clause.rs:10:13
5249
|
5350
LL | fn c<T: ~const Bar>();
5451
| ^^^^^^^^^^ required by this bound in `Foo::c`

0 commit comments

Comments
 (0)