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
.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
.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
.item = this item cannot have `~const` trait bounds

View file

@ -41,6 +41,7 @@ enum DisallowTildeConstContext<'a> {
TraitObject,
Fn(FnKind<'a>),
Trait(Span),
TraitImpl(Span),
Impl(Span),
Item,
}
@ -837,7 +838,7 @@ fn visit_item(&mut self, item: &'a Item) {
this.visit_vis(&item.vis);
this.visit_ident(item.ident);
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.visit_trait_ref(t);
this.visit_ty(self_ty);
@ -890,7 +891,9 @@ fn visit_item(&mut self, item: &'a Item) {
self.visit_vis(&item.vis);
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);
walk_list!(self, visit_assoc_item, items, AssocCtxt::Impl);
walk_list!(self, visit_attribute, &item.attrs);
@ -1216,7 +1219,12 @@ fn visit_param_bound(&mut self, bound: &'a GenericBound, ctxt: BoundKind) {
&DisallowTildeConstContext::Trait(span) => {
errors::TildeConstReason::Trait { span }
}
&DisallowTildeConstContext::TraitImpl(span) => {
errors::TildeConstReason::TraitImpl { 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 }
}
DisallowTildeConstContext::TraitObject => {

View file

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

View file

@ -52,4 +52,7 @@ trait Child1 where Self: ~const Trait {} //~ ERROR `~const` is not allowed
// non-const impl
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() {}

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 {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
--> $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
= 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`.

View file

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