diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index a44953dd078d5..92b597e08dd24 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -52,10 +52,8 @@ struct AstValidator<'a> { /// Are we inside a trait impl? in_trait_impl: bool, - in_const_trait_impl: bool, - - /// Are we inside a const trait defn? - in_const_trait_defn: bool, + /// Are we inside a const trait defn or impl? + in_const_trait: bool, has_proc_macro_decls: bool, @@ -82,16 +80,16 @@ impl<'a> AstValidator<'a> { ) { let old = mem::replace(&mut self.in_trait_impl, is_in); let old_const = - mem::replace(&mut self.in_const_trait_impl, matches!(constness, Some(Const::Yes(_)))); + mem::replace(&mut self.in_const_trait, matches!(constness, Some(Const::Yes(_)))); f(self); self.in_trait_impl = old; - self.in_const_trait_impl = old_const; + self.in_const_trait = old_const; } fn with_in_trait(&mut self, is_const: bool, f: impl FnOnce(&mut Self)) { - let old = mem::replace(&mut self.in_const_trait_defn, is_const); + let old = mem::replace(&mut self.in_const_trait, is_const); f(self); - self.in_const_trait_defn = old; + self.in_const_trait = old; } fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) { @@ -1289,16 +1287,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } let tilde_const_allowed = - if matches!(fk.header(), Some(FnHeader { constness: ast::Const::Yes(_), .. })) { - true - } else if let Some(FnCtxt::Assoc(ctxt)) = fk.ctxt() { - match ctxt { - AssocCtxt::Trait => self.in_const_trait_defn, - AssocCtxt::Impl => self.in_const_trait_impl, - } - } else { - false - }; + matches!(fk.header(), Some(FnHeader { constness: ast::Const::Yes(_), .. })) + || (matches!(fk.ctxt(), Some(FnCtxt::Assoc(_))) && self.in_const_trait); let disallowed = (!tilde_const_allowed).then(|| DisallowTildeConstContext::Fn(fk)); @@ -1383,7 +1373,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { walk_list!(self, visit_ty, ty); } AssocItemKind::Fn(box Fn { sig, generics, body, .. }) - if self.in_const_trait_impl + if self.in_const_trait || ctxt == AssocCtxt::Trait || matches!(sig.header.constness, Const::Yes(_)) => { @@ -1530,8 +1520,7 @@ pub fn check_crate( features, extern_mod: None, in_trait_impl: false, - in_const_trait_impl: false, - in_const_trait_defn: false, + in_const_trait: false, has_proc_macro_decls: false, outer_impl_trait: None, disallow_tilde_const: None, diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.rs index 911ba3f2d1d22..1e22ddcea8d3e 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.rs +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.rs @@ -6,9 +6,7 @@ trait MyTrait { } trait OtherTrait { - fn do_something_else() - where - Self: ~const MyTrait; + fn do_something_else() where Self: ~const MyTrait; //~^ ERROR `~const` is not allowed here } @@ -19,11 +17,8 @@ impl const MyTrait for u32 { } impl MyStruct { - pub fn foo(&self) - where - T: ~const MyTrait, + pub fn foo(&self) where T: ~const MyTrait { //~^ ERROR `~const` is not allowed here - { self.0.do_something(); } } diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.stderr index dd57d62b932a0..9210f6427064f 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/const-bound-on-not-const-associated-fn.stderr @@ -1,25 +1,25 @@ error: `~const` is not allowed here - --> $DIR/const-bound-on-not-const-associated-fn.rs:11:15 + --> $DIR/const-bound-on-not-const-associated-fn.rs:9:40 | -LL | Self: ~const MyTrait; - | ^^^^^^^^^^^^^^ +LL | fn do_something_else() where Self: ~const MyTrait; + | ^^^^^^^^^^^^^^ | note: this function is not `const`, so it cannot have `~const` trait bounds --> $DIR/const-bound-on-not-const-associated-fn.rs:9:8 | -LL | fn do_something_else() +LL | fn do_something_else() where Self: ~const MyTrait; | ^^^^^^^^^^^^^^^^^ error: `~const` is not allowed here - --> $DIR/const-bound-on-not-const-associated-fn.rs:24:12 + --> $DIR/const-bound-on-not-const-associated-fn.rs:20:32 | -LL | T: ~const MyTrait, - | ^^^^^^^^^^^^^^ +LL | pub fn foo(&self) where T: ~const MyTrait { + | ^^^^^^^^^^^^^^ | note: this function is not `const`, so it cannot have `~const` trait bounds - --> $DIR/const-bound-on-not-const-associated-fn.rs:22:12 + --> $DIR/const-bound-on-not-const-associated-fn.rs:20:12 | -LL | pub fn foo(&self) +LL | pub fn foo(&self) where T: ~const MyTrait { | ^^^ error: aborting due to 2 previous errors diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.rs index d9d46d260fb06..11f353f3f8ad3 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.rs +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.rs @@ -5,9 +5,7 @@ trait Bar {} trait Foo { fn a(); - fn b() - where - Self: ~const Bar; + fn b() where Self: ~const Bar; //~^ ERROR `~const` is not allowed here fn c(); //~^ ERROR `~const` is not allowed here diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.stderr index fa6c46ceaa5ab..3d6fedbabbf60 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.stderr @@ -1,54 +1,51 @@ error: `~const` is not allowed here - --> $DIR/trait-where-clause.rs:10:15 + --> $DIR/trait-where-clause.rs:8:24 | -LL | Self: ~const Bar; - | ^^^^^^^^^^ +LL | fn b() where Self: ~const Bar; + | ^^^^^^^^^^ | note: this function is not `const`, so it cannot have `~const` trait bounds --> $DIR/trait-where-clause.rs:8:8 | -LL | fn b() +LL | fn b() where Self: ~const Bar; | ^ error: `~const` is not allowed here - --> $DIR/trait-where-clause.rs:12:13 + --> $DIR/trait-where-clause.rs:10:13 | LL | fn c(); | ^^^^^^^^^^ | note: this function is not `const`, so it cannot have `~const` trait bounds - --> $DIR/trait-where-clause.rs:12:8 + --> $DIR/trait-where-clause.rs:10:8 | LL | fn c(); | ^ error[E0277]: the trait bound `T: Bar` is not satisfied - --> $DIR/trait-where-clause.rs:18:5 + --> $DIR/trait-where-clause.rs:16:5 | LL | T::b(); | ^ the trait `Bar` is not implemented for `T` | note: required by a bound in `Foo::b` - --> $DIR/trait-where-clause.rs:10:15 + --> $DIR/trait-where-clause.rs:8:24 | -LL | fn b() - | - required by a bound in this associated function -LL | where -LL | Self: ~const Bar; - | ^^^^^^^^^^ required by this bound in `Foo::b` +LL | fn b() where Self: ~const Bar; + | ^^^^^^^^^^ required by this bound in `Foo::b` help: consider further restricting this bound | LL | fn test1() { | +++++ error[E0277]: the trait bound `T: Bar` is not satisfied - --> $DIR/trait-where-clause.rs:20:12 + --> $DIR/trait-where-clause.rs:18:12 | LL | T::c::(); | ^ the trait `Bar` is not implemented for `T` | note: required by a bound in `Foo::c` - --> $DIR/trait-where-clause.rs:12:13 + --> $DIR/trait-where-clause.rs:10:13 | LL | fn c(); | ^^^^^^^^^^ required by this bound in `Foo::c`