From 6e04e678dca99db95c28a3aa7091d4fa800dfb61 Mon Sep 17 00:00:00 2001 From: inquisitivecrystal <22333129+inquisitivecrystal@users.noreply.github.com> Date: Tue, 24 Jan 2023 02:54:00 -0800 Subject: [PATCH] Make sure FFI attrs aren't used on foreign statics Previously, we verified that FFI attrs were used on foreign items, but this allowed them on both foreign functions and foreign statics. This change only allows them on foreign functions. --- compiler/rustc_passes/src/check_attr.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index f38b9c5834de..663dfbb4d13a 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -150,9 +150,9 @@ impl CheckAttrVisitor<'_> { sym::rustc_has_incoherent_inherent_impls => { self.check_has_incoherent_inherent_impls(&attr, span, target) } - sym::ffi_pure => self.check_ffi_pure(hir_id, attr.span, attrs), - sym::ffi_const => self.check_ffi_const(hir_id, attr.span), - sym::ffi_returns_twice => self.check_ffi_returns_twice(hir_id, attr.span), + sym::ffi_pure => self.check_ffi_pure(attr.span, attrs, target), + sym::ffi_const => self.check_ffi_const(attr.span, target), + sym::ffi_returns_twice => self.check_ffi_returns_twice(attr.span, target), sym::rustc_const_unstable | sym::rustc_const_stable | sym::unstable @@ -1174,8 +1174,8 @@ impl CheckAttrVisitor<'_> { } } - fn check_ffi_pure(&self, hir_id: HirId, attr_span: Span, attrs: &[Attribute]) -> bool { - if !self.tcx.is_foreign_item(self.tcx.hir().local_def_id(hir_id)) { + fn check_ffi_pure(&self, attr_span: Span, attrs: &[Attribute], target: Target) -> bool { + if target != Target::ForeignFn { self.tcx.sess.emit_err(errors::FfiPureInvalidTarget { attr_span }); return false; } @@ -1188,8 +1188,8 @@ impl CheckAttrVisitor<'_> { } } - fn check_ffi_const(&self, hir_id: HirId, attr_span: Span) -> bool { - if self.tcx.is_foreign_item(self.tcx.hir().local_def_id(hir_id)) { + fn check_ffi_const(&self, attr_span: Span, target: Target) -> bool { + if target == Target::ForeignFn { true } else { self.tcx.sess.emit_err(errors::FfiConstInvalidTarget { attr_span }); @@ -1197,8 +1197,8 @@ impl CheckAttrVisitor<'_> { } } - fn check_ffi_returns_twice(&self, hir_id: HirId, attr_span: Span) -> bool { - if self.tcx.is_foreign_item(self.tcx.hir().local_def_id(hir_id)) { + fn check_ffi_returns_twice(&self, attr_span: Span, target: Target) -> bool { + if target == Target::ForeignFn { true } else { self.tcx.sess.emit_err(errors::FfiReturnsTwiceInvalidTarget { attr_span });