Skip to content

Commit

Permalink
Apply changes as per @fee1-dead's comments in rust-lang#116210.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raekye committed Sep 28, 2023
1 parent 38698c7 commit 79ae722
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 55 deletions.
31 changes: 10 additions & 21 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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)) {
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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(_)) =>
{
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -19,11 +17,8 @@ impl const MyTrait for u32 {
}

impl<T> MyStruct<T> {
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();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: ~const Bar>();
//~^ ERROR `~const` is not allowed here
Expand Down
27 changes: 12 additions & 15 deletions tests/ui/rfcs/rfc-2632-const-trait-impl/trait-where-clause.stderr
Original file line number Diff line number Diff line change
@@ -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<T: ~const Bar>();
| ^^^^^^^^^^
|
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<T: ~const Bar>();
| ^

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<T: Foo + Bar>() {
| +++++

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::<T>();
| ^ 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<T: ~const Bar>();
| ^^^^^^^^^^ required by this bound in `Foo::c`
Expand Down

0 comments on commit 79ae722

Please sign in to comment.