From 75e00e8cf40faafbe8a19d2f35df66c0ca681bcb Mon Sep 17 00:00:00 2001 From: Tomasz Miąsko Date: Tue, 24 Nov 2020 00:00:00 +0000 Subject: Validate that `#[naked]` is applied to a function definition --- compiler/rustc_passes/src/check_attr.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'compiler') diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c94de9aec30..9c0234953bb 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -89,6 +89,8 @@ impl CheckAttrVisitor<'tcx> { self.check_allow_internal_unstable(&attr, span, target, &attrs) } else if self.tcx.sess.check_name(attr, sym::rustc_allow_const_fn_unstable) { self.check_rustc_allow_const_fn_unstable(hir_id, &attr, span, target) + } else if self.tcx.sess.check_name(attr, sym::naked) { + self.check_naked(attr, span, target) } else { // lint-only checks if self.tcx.sess.check_name(attr, sym::cold) { @@ -162,6 +164,25 @@ impl CheckAttrVisitor<'tcx> { } } + /// Checks if `#[naked]` is applied to a function definition. + fn check_naked(&self, attr: &Attribute, span: &Span, target: Target) -> bool { + match target { + Target::Fn + | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true, + _ => { + self.tcx + .sess + .struct_span_err( + attr.span, + "attribute should be applied to a function definition", + ) + .span_label(*span, "not a function definition") + .emit(); + false + } + } + } + /// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid. fn check_track_caller( &self, -- cgit 1.4.1-3-g733a5