]> git.lizzy.rs Git - rust.git/commitdiff
Merge the `~const` and `impl const` checks and add some explanatory notes
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 4 Oct 2022 08:59:20 +0000 (08:59 +0000)
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 4 Oct 2022 08:59:20 +0000 (08:59 +0000)
compiler/rustc_passes/src/check_const.rs
compiler/rustc_trait_selection/src/traits/wf.rs
src/test/ui/rfc-2632-const-trait-impl/const-impl-requires-const-trait.rs
src/test/ui/rfc-2632-const-trait-impl/const-impl-requires-const-trait.stderr

index 116aaf4834981fafd967d0e80d578c1d01fbb37b..e502b9b54e3021ab926d96706f2d7b1d6fc81acf 100644 (file)
@@ -191,32 +191,6 @@ fn nested_visit_map(&mut self) -> Self::Map {
         self.tcx.hir()
     }
 
-    fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
-        let tcx = self.tcx;
-        if let hir::ItemKind::Impl(hir::Impl {
-            constness: hir::Constness::Const,
-            of_trait: Some(trait_ref),
-            ..
-        }) = item.kind
-            && let Some(def_id) = trait_ref.trait_def_id()
-        {
-            let source_map = tcx.sess.source_map();
-            if !tcx.has_attr(def_id, sym::const_trait) {
-                tcx.sess
-                    .struct_span_err(
-                        source_map.guess_head_span(item.span),
-                        "const `impl`s must be for traits marked with `#[const_trait]`",
-                    )
-                    .span_note(
-                        source_map.guess_head_span(tcx.def_span(def_id)),
-                        "this trait must be annotated with `#[const_trait]`",
-                    )
-                    .emit();
-            }
-        }
-        intravisit::walk_item(self, item);
-    }
-
     fn visit_anon_const(&mut self, anon: &'tcx hir::AnonConst) {
         let kind = Some(hir::ConstContext::Const);
         self.recurse_into(kind, None, |this| intravisit::walk_anon_const(this, anon));
index ef7b27e408cde22d92a35056467e9aba8c219335..5f901d6995e4bd929fa766d5b2e73d43faf87494 100644 (file)
@@ -309,8 +309,30 @@ fn compute_trait_pred(&mut self, trait_pred: &ty::TraitPredicate<'tcx>, elaborat
             self.nominal_obligations_without_const(trait_ref.def_id, trait_ref.substs)
         } else {
             if !tcx.has_attr(trait_ref.def_id, rustc_span::sym::const_trait) {
-                tcx.sess
-                    .span_err(self.span, "~const can only be applied to `#[const_trait]` traits");
+                if let Some(item) = self.item &&
+                   let hir::ItemKind::Impl(impl_) = item.kind &&
+                   let Some(trait_) = &impl_.of_trait &&
+                   let Some(def_id) = trait_.trait_def_id() &&
+                   def_id == trait_ref.def_id
+                {
+                    let trait_name = tcx.item_name(def_id);
+                    let mut err = tcx.sess.struct_span_err(
+                        self.span,
+                        &format!("const `impl` for trait `{trait_name}` which is not marked with `#[const_trait]`"),
+                    );
+                    if def_id.is_local() {
+                        let sp = tcx.def_span(def_id).shrink_to_lo();
+                        err.span_suggestion(sp, &format!("mark `{trait_name}` as const"), "#[const_trait]", rustc_errors::Applicability::MachineApplicable);
+                    }
+                    err.note("marking a trait with `#[const_trait]` ensures all default method bodies are `const`");
+                    err.note("adding a non-const method body in the future would be a breaking change");
+                    err.emit();
+                } else {
+                    tcx.sess.span_err(
+                        self.span,
+                        "~const can only be applied to `#[const_trait]` traits",
+                    );
+                }
             }
             self.nominal_obligations(trait_ref.def_id, trait_ref.substs)
         };
index 8757d74082e1a76245be1217e8cf25a3207c244d..2b4963991dbefacd4a08c3e1f9baba36831cca35 100644 (file)
@@ -1,10 +1,9 @@
 #![feature(const_trait_impl)]
 
 pub trait A {}
-//~^ NOTE: this trait must be annotated with `#[const_trait]`
+//~^ HELP: mark `A` as const
 
 impl const A for () {}
-//~^ ERROR: const `impl`s must be for traits marked with `#[const_trait]`
-//~| ERROR: ~const can only be applied to `#[const_trait]` traits
+//~^ ERROR: const `impl` for trait `A` which is not marked with `#[const_trait]`
 
 fn main() {}
index f9c69d6c6bf84a3b98a109fdcb4bc8aade8ee4c3..478adcf3e9e898d28cfd48addc6ad15f79c612f3 100644 (file)
@@ -1,20 +1,14 @@
-error: const `impl`s must be for traits marked with `#[const_trait]`
-  --> $DIR/const-impl-requires-const-trait.rs:6:1
-   |
-LL | impl const A for () {}
-   | ^^^^^^^^^^^^^^^^^^^
-   |
-note: this trait must be annotated with `#[const_trait]`
-  --> $DIR/const-impl-requires-const-trait.rs:3:1
-   |
-LL | pub trait A {}
-   | ^^^^^^^^^^^
-
-error: ~const can only be applied to `#[const_trait]` traits
+error: const `impl` for trait `A` which is not marked with `#[const_trait]`
   --> $DIR/const-impl-requires-const-trait.rs:6:12
    |
+LL | pub trait A {}
+   | - help: mark `A` as const: `#[const_trait]`
+...
 LL | impl const A for () {}
    |            ^
+   |
+   = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
+   = note: adding a non-const method body in the future would be a breaking change
 
-error: aborting due to 2 previous errors
+error: aborting due to previous error