about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDeadbeef <ent3rm4n@gmail.com>2021-07-10 11:13:52 +0800
committerDeadbeef <ent3rm4n@gmail.com>2021-07-10 20:54:48 +0800
commit032cbe4ccec5bb6565a5c7656971aa83f56774a9 (patch)
treede46c24f01efe6d1d734c79e67ac36404ff3351b
parent56d79adf3be666a4583ffeddd276de5c8b6ab32e (diff)
downloadrust-032cbe4ccec5bb6565a5c7656971aa83f56774a9.tar.gz
rust-032cbe4ccec5bb6565a5c7656971aa83f56774a9.zip
Check if the attribute is applied correctly
-rw-r--r--compiler/rustc_passes/src/check_attr.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index d0795841c53..71231830e99 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -98,6 +98,9 @@ impl CheckAttrVisitor<'tcx> {
                 | sym::rustc_if_this_changed
                 | sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(&attr),
                 sym::cmse_nonsecure_entry => self.check_cmse_nonsecure_entry(attr, span, target),
+                sym::default_method_body_is_const => {
+                    self.check_default_method_body_is_const(attr, span, target)
+                }
                 _ => true,
             };
             // lint-only checks
@@ -1465,6 +1468,29 @@ impl CheckAttrVisitor<'tcx> {
             }
         }
     }
+
+    /// default_method_body_is_const should only be applied to trait methods with default bodies.
+    fn check_default_method_body_is_const(
+        &self,
+        attr: &Attribute,
+        span: &Span,
+        target: Target,
+    ) -> bool {
+        match target {
+            Target::Method(MethodKind::Trait { body: true }) => true,
+            _ => {
+                self.tcx
+                    .sess
+                    .struct_span_err(
+                        attr.span,
+                        "attribute should be applied to a trait method with body",
+                    )
+                    .span_label(*span, "not a trait method or missing a body")
+                    .emit();
+                false
+            }
+        }
+    }
 }
 
 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {