From 349ba6bb51f53d93286600a0a078fefafff9e0d3 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 21 Oct 2022 08:27:39 +0000 Subject: [PATCH 1/2] Remove needless special cases and dead code --- .../rustc_ast_passes/src/ast_validation.rs | 52 ++----------------- 1 file changed, 3 insertions(+), 49 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index f42aca685f60e..54e711c2d50f0 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -251,20 +251,6 @@ impl<'a> AstValidator<'a> { } } - fn visit_struct_field_def(&mut self, field: &'a FieldDef) { - if let Some(ident) = field.ident { - if ident.name == kw::Underscore { - self.visit_vis(&field.vis); - self.visit_ident(ident); - self.visit_ty_common(&field.ty); - self.walk_ty(&field.ty); - walk_list!(self, visit_attribute, &field.attrs); - return; - } - } - self.visit_field_def(field); - } - fn err_handler(&self) -> &rustc_errors::Handler { &self.session.diagnostic() } @@ -1005,8 +991,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { visit::walk_lifetime(self, lifetime); } - fn visit_field_def(&mut self, s: &'a FieldDef) { - visit::walk_field_def(self, s) + fn visit_field_def(&mut self, field: &'a FieldDef) { + visit::walk_field_def(self, field) } fn visit_item(&mut self, item: &'a Item) { @@ -1194,42 +1180,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_mod_file_item_asciionly(item.ident); } } - ItemKind::Struct(ref vdata, ref generics) => match vdata { - // Duplicating the `Visitor` logic allows catching all cases - // of `Anonymous(Struct, Union)` outside of a field struct or union. - // - // Inside `visit_ty` the validator catches every `Anonymous(Struct, Union)` it - // encounters, and only on `ItemKind::Struct` and `ItemKind::Union` - // it uses `visit_ty_common`, which doesn't contain that specific check. - VariantData::Struct(ref fields, ..) => { - self.visit_vis(&item.vis); - self.visit_ident(item.ident); - self.visit_generics(generics); - self.with_banned_assoc_ty_bound(|this| { - walk_list!(this, visit_struct_field_def, fields); - }); - walk_list!(self, visit_attribute, &item.attrs); - return; - } - _ => {} - }, - ItemKind::Union(ref vdata, ref generics) => { + ItemKind::Union(ref vdata, ..) => { if vdata.fields().is_empty() { self.err_handler().span_err(item.span, "unions cannot have zero fields"); } - match vdata { - VariantData::Struct(ref fields, ..) => { - self.visit_vis(&item.vis); - self.visit_ident(item.ident); - self.visit_generics(generics); - self.with_banned_assoc_ty_bound(|this| { - walk_list!(this, visit_struct_field_def, fields); - }); - walk_list!(self, visit_attribute, &item.attrs); - return; - } - _ => {} - } } ItemKind::Const(def, .., None) => { self.check_defaultness(item.span, def); From d6cf8934db2af8b56f37b235c5aa66593d1eb22c Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 21 Oct 2022 13:49:11 +0000 Subject: [PATCH 2/2] Require Drop impls to have the same constness on its bounds as the bounds on the struct have --- .../rustc_hir_analysis/src/check/dropck.rs | 8 +-- .../const-drop-fail.precise.stderr | 65 ++++++++++++------- .../const-drop-fail.rs | 12 +++- .../const-drop-fail.stock.stderr | 65 ++++++++++++------- .../rfc-2632-const-trait-impl/const-drop.rs | 2 +- .../tilde_const_on_impl_bound.rs | 17 +++++ 6 files changed, 116 insertions(+), 53 deletions(-) create mode 100644 src/test/ui/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs diff --git a/compiler/rustc_hir_analysis/src/check/dropck.rs b/compiler/rustc_hir_analysis/src/check/dropck.rs index e5b212eb757b6..3d55ffc595f53 100644 --- a/compiler/rustc_hir_analysis/src/check/dropck.rs +++ b/compiler/rustc_hir_analysis/src/check/dropck.rs @@ -184,13 +184,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( let p = p.kind(); match (predicate.skip_binder(), p.skip_binder()) { (ty::PredicateKind::Trait(a), ty::PredicateKind::Trait(b)) => { - // Since struct predicates cannot have ~const, project the impl predicate - // onto one that ignores the constness. This is equivalent to saying that - // we match a `Trait` bound on the struct with a `Trait` or `~const Trait` - // in the impl. - let non_const_a = - ty::TraitPredicate { constness: ty::BoundConstness::NotConst, ..a }; - relator.relate(predicate.rebind(non_const_a), p.rebind(b)).is_ok() + relator.relate(predicate.rebind(a), p.rebind(b)).is_ok() } (ty::PredicateKind::Projection(a), ty::PredicateKind::Projection(b)) => { relator.relate(predicate.rebind(a), p.rebind(b)).is_ok() diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr index ddf0e2d91c097..796c0d388eaf0 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr @@ -45,34 +45,55 @@ note: required by a bound in `check` LL | const fn check(_: T) {} | ^^^^^^^^^^^^^^^ required by this bound in `check` -error[E0277]: the trait bound `ConstDropImplWithBounds: ~const Destruct` is not satisfied +error[E0277]: the trait bound `NonTrivialDrop: ~const A` is not satisfied + --> $DIR/const-drop-fail.rs:48:47 + | +LL | ConstDropImplWithBounds::(PhantomData), + | ----------------------------------------- ^^^^^^^^^^^ the trait `~const A` is not implemented for `NonTrivialDrop` + | | + | required by a bound introduced by this call + | +note: the trait `A` is implemented for `NonTrivialDrop`, but that implementation is not `const` + --> $DIR/const-drop-fail.rs:48:47 + | +LL | ConstDropImplWithBounds::(PhantomData), + | ^^^^^^^^^^^ +note: required by a bound in `ConstDropImplWithBounds` + --> $DIR/const-drop-fail.rs:27:35 + | +LL | struct ConstDropImplWithBounds(PhantomData); + | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` + +error[E0277]: the trait bound `NonTrivialDrop: ~const A` is not satisfied + --> $DIR/const-drop-fail.rs:48:5 + | +LL | ConstDropImplWithBounds::(PhantomData), + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const A` is not implemented for `NonTrivialDrop` + | +note: the trait `A` is implemented for `NonTrivialDrop`, but that implementation is not `const` --> $DIR/const-drop-fail.rs:48:5 | -LL | const _: () = check($exp); - | ----- required by a bound introduced by this call -... LL | ConstDropImplWithBounds::(PhantomData), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `ConstDropImplWithBounds` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `ConstDropImplWithBounds` + --> $DIR/const-drop-fail.rs:27:35 | -note: required for `ConstDropImplWithBounds` to implement `~const Destruct` - --> $DIR/const-drop-fail.rs:29:25 +LL | struct ConstDropImplWithBounds(PhantomData); + | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` + +error[E0367]: `Drop` impl requires `T: ~const A` but the struct it is implemented for does not + --> $DIR/const-drop-fail.rs:55:9 | -LL | impl const Drop for ConstDropImplWithBounds { - | ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: 1 redundant requirement hidden - = note: required for `ConstDropImplWithBounds` to implement `~const Destruct` -note: required by a bound in `check` - --> $DIR/const-drop-fail.rs:35:19 +LL | impl const Drop for ConstDropImplWithNonConstBounds { + | ^^^^^^^^ | -LL | const fn check(_: T) {} - | ^^^^^^^^^^^^^^^ required by this bound in `check` -help: consider borrowing here +note: the implementor must specify the same requirement + --> $DIR/const-drop-fail.rs:53:1 | -LL | &ConstDropImplWithBounds::(PhantomData), - | + -LL | &mut ConstDropImplWithBounds::(PhantomData), - | ++++ +LL | struct ConstDropImplWithNonConstBounds(PhantomData); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 3 previous errors +error: aborting due to 5 previous errors -For more information about this error, try `rustc --explain E0277`. +Some errors have detailed explanations: E0277, E0367. +For more information about an error, try `rustc --explain E0277`. diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs index 565e2c77ac5ca..d36c7f81ced8c 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.rs @@ -24,7 +24,7 @@ trait A { fn a() { } } impl A for NonTrivialDrop {} -struct ConstDropImplWithBounds(PhantomData); +struct ConstDropImplWithBounds(PhantomData); impl const Drop for ConstDropImplWithBounds { fn drop(&mut self) { @@ -47,6 +47,16 @@ check_all! { //~^ ERROR can't drop ConstDropImplWithBounds::(PhantomData), //~^ ERROR the trait bound + //~| ERROR the trait bound +} + +struct ConstDropImplWithNonConstBounds(PhantomData); + +impl const Drop for ConstDropImplWithNonConstBounds { +//~^ ERROR `Drop` impl requires `T: ~const A` but the struct it is implemented for does not + fn drop(&mut self) { + T::a(); + } } fn main() {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr index ddf0e2d91c097..796c0d388eaf0 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr @@ -45,34 +45,55 @@ note: required by a bound in `check` LL | const fn check(_: T) {} | ^^^^^^^^^^^^^^^ required by this bound in `check` -error[E0277]: the trait bound `ConstDropImplWithBounds: ~const Destruct` is not satisfied +error[E0277]: the trait bound `NonTrivialDrop: ~const A` is not satisfied + --> $DIR/const-drop-fail.rs:48:47 + | +LL | ConstDropImplWithBounds::(PhantomData), + | ----------------------------------------- ^^^^^^^^^^^ the trait `~const A` is not implemented for `NonTrivialDrop` + | | + | required by a bound introduced by this call + | +note: the trait `A` is implemented for `NonTrivialDrop`, but that implementation is not `const` + --> $DIR/const-drop-fail.rs:48:47 + | +LL | ConstDropImplWithBounds::(PhantomData), + | ^^^^^^^^^^^ +note: required by a bound in `ConstDropImplWithBounds` + --> $DIR/const-drop-fail.rs:27:35 + | +LL | struct ConstDropImplWithBounds(PhantomData); + | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` + +error[E0277]: the trait bound `NonTrivialDrop: ~const A` is not satisfied + --> $DIR/const-drop-fail.rs:48:5 + | +LL | ConstDropImplWithBounds::(PhantomData), + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const A` is not implemented for `NonTrivialDrop` + | +note: the trait `A` is implemented for `NonTrivialDrop`, but that implementation is not `const` --> $DIR/const-drop-fail.rs:48:5 | -LL | const _: () = check($exp); - | ----- required by a bound introduced by this call -... LL | ConstDropImplWithBounds::(PhantomData), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `ConstDropImplWithBounds` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `ConstDropImplWithBounds` + --> $DIR/const-drop-fail.rs:27:35 | -note: required for `ConstDropImplWithBounds` to implement `~const Destruct` - --> $DIR/const-drop-fail.rs:29:25 +LL | struct ConstDropImplWithBounds(PhantomData); + | ^^^^^^^^ required by this bound in `ConstDropImplWithBounds` + +error[E0367]: `Drop` impl requires `T: ~const A` but the struct it is implemented for does not + --> $DIR/const-drop-fail.rs:55:9 | -LL | impl const Drop for ConstDropImplWithBounds { - | ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: 1 redundant requirement hidden - = note: required for `ConstDropImplWithBounds` to implement `~const Destruct` -note: required by a bound in `check` - --> $DIR/const-drop-fail.rs:35:19 +LL | impl const Drop for ConstDropImplWithNonConstBounds { + | ^^^^^^^^ | -LL | const fn check(_: T) {} - | ^^^^^^^^^^^^^^^ required by this bound in `check` -help: consider borrowing here +note: the implementor must specify the same requirement + --> $DIR/const-drop-fail.rs:53:1 | -LL | &ConstDropImplWithBounds::(PhantomData), - | + -LL | &mut ConstDropImplWithBounds::(PhantomData), - | ++++ +LL | struct ConstDropImplWithNonConstBounds(PhantomData); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 3 previous errors +error: aborting due to 5 previous errors -For more information about this error, try `rustc --explain E0277`. +Some errors have detailed explanations: E0277, E0367. +For more information about an error, try `rustc --explain E0277`. diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs b/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs index 8b4c406581565..b0fc3adf984a4 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/const-drop.rs @@ -60,7 +60,7 @@ mod t { fn foo() {} } - pub struct ConstDropWithBound(pub core::marker::PhantomData); + pub struct ConstDropWithBound(pub core::marker::PhantomData); impl const Drop for ConstDropWithBound { fn drop(&mut self) { diff --git a/src/test/ui/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs b/src/test/ui/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs new file mode 100644 index 0000000000000..285cef571f332 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs @@ -0,0 +1,17 @@ +// check-pass +#![feature(const_trait_impl)] + +#[const_trait] +trait Foo { + fn foo(&self) {} +} + +struct Bar(T); + +impl Bar { + const fn foo(&self) { + self.0.foo() + } +} + +fn main() {}