about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-02-01 00:50:28 +0000
committerbors <bors@rust-lang.org>2022-02-01 00:50:28 +0000
commita6cd4aa9a784e3d2e54bca2a1b41082fda67310d (patch)
treec423461f7720efc62cfe443a9adc0e7ac5d487cd
parent745e9264873ab001a189f739446c86c509e6dc3d (diff)
parent4ca56d28884678d89c753c03de9f0c7544d018e6 (diff)
downloadrust-a6cd4aa9a784e3d2e54bca2a1b41082fda67310d.tar.gz
rust-a6cd4aa9a784e3d2e54bca2a1b41082fda67310d.zip
Auto merge of #93386 - WaffleLapkin:rustc_must_implement_one_of_check_target, r=nagisa
Check that `#[rustc_must_implement_one_of]` is applied to a trait

`#[rustc_must_implement_one_of]` only makes sense when applied to a trait, so it's sensible to emit an error otherwise.
-rw-r--r--compiler/rustc_passes/src/check_attr.rs23
-rw-r--r--src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs8
-rw-r--r--src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr20
3 files changed, 50 insertions, 1 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index c3d5bae32e6..ec4cefa3537 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -76,6 +76,9 @@ impl CheckAttrVisitor<'_> {
                 sym::inline => self.check_inline(hir_id, attr, span, target),
                 sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
                 sym::marker => self.check_marker(hir_id, attr, span, target),
+                sym::rustc_must_implement_one_of => {
+                    self.check_rustc_must_implement_one_of(attr, span, target)
+                }
                 sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
                 sym::track_caller => {
                     self.check_track_caller(hir_id, &attr.span, attrs, span, target)
@@ -477,6 +480,26 @@ impl CheckAttrVisitor<'_> {
         }
     }
 
+    /// Checks if the `#[rustc_must_implement_one_of]` attribute on a `target` is valid. Returns `true` if valid.
+    fn check_rustc_must_implement_one_of(
+        &self,
+        attr: &Attribute,
+        span: &Span,
+        target: Target,
+    ) -> bool {
+        match target {
+            Target::Trait => true,
+            _ => {
+                self.tcx
+                    .sess
+                    .struct_span_err(attr.span, "attribute can only be applied to a trait")
+                    .span_label(*span, "not a trait")
+                    .emit();
+                false
+            }
+        }
+    }
+
     /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
     fn check_target_feature(
         &self,
diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs
index 1089e5f9c4a..d9de6d5edb9 100644
--- a/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs
+++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs
@@ -35,4 +35,12 @@ trait Tr5 {
     fn b(); //~ This function doesn't have a default implementation
 }
 
+#[rustc_must_implement_one_of(abc, xyz)]
+//~^ attribute can only be applied to a trait
+fn function() {}
+
+#[rustc_must_implement_one_of(abc, xyz)]
+//~^ attribute can only be applied to a trait
+struct Struct {}
+
 fn main() {}
diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr
index 74a6dc8fec9..bc28dc2c4f4 100644
--- a/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr
+++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr
@@ -4,6 +4,24 @@ error: malformed `rustc_must_implement_one_of` attribute input
 LL | #[rustc_must_implement_one_of]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_must_implement_one_of(function1, function2, ...)]`
 
+error: attribute can only be applied to a trait
+  --> $DIR/rustc_must_implement_one_of_misuse.rs:38:1
+   |
+LL | #[rustc_must_implement_one_of(abc, xyz)]
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | fn function() {}
+   | ---------------- not a trait
+
+error: attribute can only be applied to a trait
+  --> $DIR/rustc_must_implement_one_of_misuse.rs:42:1
+   |
+LL | #[rustc_must_implement_one_of(abc, xyz)]
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | struct Struct {}
+   | ---------------- not a trait
+
 error: Function not found in this trait
   --> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
    |
@@ -78,5 +96,5 @@ note: required by this annotation
 LL | #[rustc_must_implement_one_of(a, b)]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: aborting due to 9 previous errors
+error: aborting due to 11 previous errors