about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeón Orell Valerian Liehr <me@fmease.dev>2023-12-18 01:10:16 +0100
committerLeón Orell Valerian Liehr <me@fmease.dev>2023-12-18 01:48:49 +0100
commit4a5dd169f73d77429c592f96977ae5484f2cfad8 (patch)
tree226af2af8b7bada7644d77197056a7f959cc282d
parent3ad8e2d129a67b0dc450d6ef1100ae41d2cd4b8a (diff)
downloadrust-4a5dd169f73d77429c592f96977ae5484f2cfad8.tar.gz
rust-4a5dd169f73d77429c592f96977ae5484f2cfad8.zip
Deny ~const trait bounds in inherent impl headers
-rw-r--r--compiler/rustc_ast_passes/messages.ftl3
-rw-r--r--compiler/rustc_ast_passes/src/ast_validation.rs12
-rw-r--r--compiler/rustc_ast_passes/src/errors.rs5
-rw-r--r--tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs3
-rw-r--r--tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr14
-rw-r--r--tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs7
-rw-r--r--tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.stderr12
7 files changed, 36 insertions, 20 deletions
diff --git a/compiler/rustc_ast_passes/messages.ftl b/compiler/rustc_ast_passes/messages.ftl
index 28bd6c2111b..790b583134c 100644
--- a/compiler/rustc_ast_passes/messages.ftl
+++ b/compiler/rustc_ast_passes/messages.ftl
@@ -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
 
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs
index 59cf18c2459..326ac459c37 100644
--- a/compiler/rustc_ast_passes/src/ast_validation.rs
+++ b/compiler/rustc_ast_passes/src/ast_validation.rs
@@ -41,6 +41,7 @@ enum DisallowTildeConstContext<'a> {
     TraitObject,
     Fn(FnKind<'a>),
     Trait(Span),
+    TraitImpl(Span),
     Impl(Span),
     Item,
 }
@@ -837,7 +838,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
                     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 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
 
                 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 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
                         &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 => {
diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs
index 928bf19759a..4283fc7c07d 100644
--- a/compiler/rustc_ast_passes/src/errors.rs
+++ b/compiler/rustc_ast_passes/src/errors.rs
@@ -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]
diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs
index bbe1194f7a3..5ecb75094f0 100644
--- a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs
+++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs
@@ -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() {}
diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr
index c6e18924fd8..497ec5bcf84 100644
--- a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr
+++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr
@@ -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`.
diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs
index fbdc3a4f370..bfd9fe42e67 100644
--- a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs
+++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs
@@ -1,5 +1,4 @@
-// known-bug: #110395
-// FIXME check-pass
+// check-pass
 #![feature(const_trait_impl, effects)]
 
 #[const_trait]
@@ -9,8 +8,8 @@ trait Foo {
 
 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()
     }
 }
diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.stderr
deleted file mode 100644
index 0925bfa7e57..00000000000
--- a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.stderr
+++ /dev/null
@@ -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`.