Skip to content

Commit e4432f5

Browse files
committed
Deny ~const trait bounds in inherent impl headers
1 parent 3ad8e2d commit e4432f5

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

compiler/rustc_ast_passes/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ ast_passes_tilde_const_disallowed = `~const` is not allowed here
225225
.closure = closures cannot have `~const` trait bounds
226226
.function = this function is not `const`, so it cannot have `~const` trait bounds
227227
.trait = this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
228-
.impl = this impl is not `const`, so it cannot have `~const` trait bounds
228+
.trait_impl = this impl is not `const`, so it cannot have `~const` trait bounds
229+
.impl = inherent impls cannot have `~const` trait bounds
229230
.object = trait objects cannot have `~const` trait bounds
230231
.item = this item cannot have `~const` trait bounds
231232

compiler/rustc_ast_passes/src/ast_validation.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ enum DisallowTildeConstContext<'a> {
4141
TraitObject,
4242
Fn(FnKind<'a>),
4343
Trait(Span),
44+
TraitImpl(Span),
4445
Impl(Span),
4546
Item,
4647
}
@@ -837,7 +838,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
837838
this.visit_vis(&item.vis);
838839
this.visit_ident(item.ident);
839840
let disallowed = matches!(constness, Const::No)
840-
.then(|| DisallowTildeConstContext::Impl(item.span));
841+
.then(|| DisallowTildeConstContext::TraitImpl(item.span));
841842
this.with_tilde_const(disallowed, |this| this.visit_generics(generics));
842843
this.visit_trait_ref(t);
843844
this.visit_ty(self_ty);
@@ -890,7 +891,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
890891

891892
self.visit_vis(&item.vis);
892893
self.visit_ident(item.ident);
893-
self.with_tilde_const(None, |this| this.visit_generics(generics));
894+
self.with_tilde_const(Some(DisallowTildeConstContext::Impl(item.span)), |this| {
895+
this.visit_generics(generics)
896+
});
894897
self.visit_ty(self_ty);
895898
walk_list!(self, visit_assoc_item, items, AssocCtxt::Impl);
896899
walk_list!(self, visit_attribute, &item.attrs);
@@ -1216,7 +1219,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12161219
&DisallowTildeConstContext::Trait(span) => {
12171220
errors::TildeConstReason::Trait { span }
12181221
}
1222+
&DisallowTildeConstContext::TraitImpl(span) => {
1223+
errors::TildeConstReason::TraitImpl { span }
1224+
}
12191225
&DisallowTildeConstContext::Impl(span) => {
1226+
// FIXME(effects): Consider providing a help message or even a structured
1227+
// suggestion for moving such bounds to the assoc const fns if available.
12201228
errors::TildeConstReason::Impl { span }
12211229
}
12221230
DisallowTildeConstContext::TraitObject => {

compiler/rustc_ast_passes/src/errors.rs

+5
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ pub enum TildeConstReason {
563563
#[primary_span]
564564
span: Span,
565565
},
566+
#[note(ast_passes_trait_impl)]
567+
TraitImpl {
568+
#[primary_span]
569+
span: Span,
570+
},
566571
#[note(ast_passes_impl)]
567572
Impl {
568573
#[primary_span]

tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs

+3
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ trait Child1 where Self: ~const Trait {} //~ ERROR `~const` is not allowed
5252
// non-const impl
5353
impl<T: ~const Trait> Trait for T {} //~ ERROR `~const` is not allowed
5454

55+
// inherent impl (regression test for issue #117004)
56+
impl<T: ~const Trait> Struct<T> {} //~ ERROR `~const` is not allowed
57+
5558
fn main() {}

tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ note: this impl is not `const`, so it cannot have `~const` trait bounds
194194
LL | impl<T: ~const Trait> Trait for T {}
195195
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
196196

197+
error: `~const` is not allowed here
198+
--> $DIR/tilde-const-invalid-places.rs:56:9
199+
|
200+
LL | impl<T: ~const Trait> Struct<T> {}
201+
| ^^^^^^
202+
|
203+
note: inherent impls cannot have `~const` trait bounds
204+
--> $DIR/tilde-const-invalid-places.rs:56:1
205+
|
206+
LL | impl<T: ~const Trait> Struct<T> {}
207+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
208+
197209
error[E0658]: generic const items are experimental
198210
--> $DIR/tilde-const-invalid-places.rs:19:15
199211
|
@@ -239,6 +251,6 @@ LL | type Type<T: ~const Trait> = ();
239251
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
240252
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
241253

242-
error: aborting due to 26 previous errors
254+
error: aborting due to 27 previous errors
243255

244256
For more information about this error, try `rustc --explain E0658`.

tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// known-bug: #110395
2-
// FIXME check-pass
1+
// check-pass
32
#![feature(const_trait_impl, effects)]
43

54
#[const_trait]
@@ -9,8 +8,8 @@ trait Foo {
98

109
struct Bar<T>(T);
1110

12-
impl<T: ~const Foo> Bar<T> {
13-
const fn foo(&self) {
11+
impl<T> Bar<T> {
12+
const fn foo(&self) where T: ~const Foo {
1413
self.0.foo()
1514
}
1615
}

0 commit comments

Comments
 (0)