Deny ~const trait bounds in inherent impl headers

This commit is contained in:
León Orell Valerian Liehr 2023-12-18 01:10:16 +01:00
parent 3ad8e2d129
commit 4a5dd169f7
No known key found for this signature in database
GPG Key ID: D17A07215F68E713
7 changed files with 36 additions and 20 deletions

View File

@ -225,7 +225,8 @@ ast_passes_tilde_const_disallowed = `~const` is not allowed here
.closure = closures cannot have `~const` trait bounds .closure = closures cannot have `~const` trait bounds
.function = this function is not `const`, so it cannot have `~const` trait bounds .function = this function is not `const`, so it cannot have `~const` trait bounds
.trait = this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds .trait = this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
.impl = this impl is not `const`, so it cannot have `~const` trait bounds .trait_impl = this impl is not `const`, so it cannot have `~const` trait bounds
.impl = inherent impls cannot have `~const` trait bounds
.object = trait objects cannot have `~const` trait bounds .object = trait objects cannot have `~const` trait bounds
.item = this item cannot have `~const` trait bounds .item = this item cannot have `~const` trait bounds

View File

@ -41,6 +41,7 @@ enum DisallowTildeConstContext<'a> {
TraitObject, TraitObject,
Fn(FnKind<'a>), Fn(FnKind<'a>),
Trait(Span), Trait(Span),
TraitImpl(Span),
Impl(Span), Impl(Span),
Item, Item,
} }
@ -837,7 +838,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
this.visit_vis(&item.vis); this.visit_vis(&item.vis);
this.visit_ident(item.ident); this.visit_ident(item.ident);
let disallowed = matches!(constness, Const::No) let disallowed = matches!(constness, Const::No)
.then(|| DisallowTildeConstContext::Impl(item.span)); .then(|| DisallowTildeConstContext::TraitImpl(item.span));
this.with_tilde_const(disallowed, |this| this.visit_generics(generics)); this.with_tilde_const(disallowed, |this| this.visit_generics(generics));
this.visit_trait_ref(t); this.visit_trait_ref(t);
this.visit_ty(self_ty); this.visit_ty(self_ty);
@ -890,7 +891,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.visit_vis(&item.vis); self.visit_vis(&item.vis);
self.visit_ident(item.ident); self.visit_ident(item.ident);
self.with_tilde_const(None, |this| this.visit_generics(generics)); self.with_tilde_const(Some(DisallowTildeConstContext::Impl(item.span)), |this| {
this.visit_generics(generics)
});
self.visit_ty(self_ty); self.visit_ty(self_ty);
walk_list!(self, visit_assoc_item, items, AssocCtxt::Impl); walk_list!(self, visit_assoc_item, items, AssocCtxt::Impl);
walk_list!(self, visit_attribute, &item.attrs); walk_list!(self, visit_attribute, &item.attrs);
@ -1216,7 +1219,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
&DisallowTildeConstContext::Trait(span) => { &DisallowTildeConstContext::Trait(span) => {
errors::TildeConstReason::Trait { span } errors::TildeConstReason::Trait { span }
} }
&DisallowTildeConstContext::TraitImpl(span) => {
errors::TildeConstReason::TraitImpl { span }
}
&DisallowTildeConstContext::Impl(span) => { &DisallowTildeConstContext::Impl(span) => {
// FIXME(effects): Consider providing a help message or even a structured
// suggestion for moving such bounds to the assoc const fns if available.
errors::TildeConstReason::Impl { span } errors::TildeConstReason::Impl { span }
} }
DisallowTildeConstContext::TraitObject => { DisallowTildeConstContext::TraitObject => {

View File

@ -563,6 +563,11 @@ pub enum TildeConstReason {
#[primary_span] #[primary_span]
span: Span, span: Span,
}, },
#[note(ast_passes_trait_impl)]
TraitImpl {
#[primary_span]
span: Span,
},
#[note(ast_passes_impl)] #[note(ast_passes_impl)]
Impl { Impl {
#[primary_span] #[primary_span]

View File

@ -52,4 +52,7 @@ trait Child1 where Self: ~const Trait {} //~ ERROR `~const` is not allowed
// non-const impl // non-const impl
impl<T: ~const Trait> Trait for T {} //~ ERROR `~const` is not allowed impl<T: ~const Trait> Trait for T {} //~ ERROR `~const` is not allowed
// inherent impl (regression test for issue #117004)
impl<T: ~const Trait> Struct<T> {} //~ ERROR `~const` is not allowed
fn main() {} fn main() {}

View File

@ -194,6 +194,18 @@ note: this impl is not `const`, so it cannot have `~const` trait bounds
LL | impl<T: ~const Trait> Trait for T {} LL | impl<T: ~const Trait> Trait for T {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `~const` is not allowed here
--> $DIR/tilde-const-invalid-places.rs:56:9
|
LL | impl<T: ~const Trait> Struct<T> {}
| ^^^^^^
|
note: inherent impls cannot have `~const` trait bounds
--> $DIR/tilde-const-invalid-places.rs:56:1
|
LL | impl<T: ~const Trait> Struct<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0658]: generic const items are experimental error[E0658]: generic const items are experimental
--> $DIR/tilde-const-invalid-places.rs:19:15 --> $DIR/tilde-const-invalid-places.rs:19:15
| |
@ -239,6 +251,6 @@ LL | type Type<T: ~const Trait> = ();
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information = note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable = help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
error: aborting due to 26 previous errors error: aborting due to 27 previous errors
For more information about this error, try `rustc --explain E0658`. For more information about this error, try `rustc --explain E0658`.

View File

@ -1,5 +1,4 @@
// known-bug: #110395 // check-pass
// FIXME check-pass
#![feature(const_trait_impl, effects)] #![feature(const_trait_impl, effects)]
#[const_trait] #[const_trait]
@ -9,8 +8,8 @@ trait Foo {
struct Bar<T>(T); struct Bar<T>(T);
impl<T: ~const Foo> Bar<T> { impl<T> Bar<T> {
const fn foo(&self) { const fn foo(&self) where T: ~const Foo {
self.0.foo() self.0.foo()
} }
} }

View File

@ -1,12 +0,0 @@
error[E0308]: mismatched types
--> $DIR/tilde_const_on_impl_bound.rs:14:9
|
LL | self.0.foo()
| ^^^^^^^^^^^^ expected `host`, found `true`
|
= note: expected constant `host`
found constant `true`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.