Move mixed export_name/no_mangle check to check_attr.rs and improve t… · rust-lang/rust@3d1cee5 (original) (raw)
`@@ -9,7 +9,7 @@ use rustc_attr_data_structures::{
`
9
9
`use rustc_hir::def::DefKind;
`
10
10
`use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
`
11
11
`use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
`
12
``
`-
use rustc_hir::{self as hir, HirId, LangItem, lang_items};
`
``
12
`+
use rustc_hir::{self as hir, LangItem, lang_items};
`
13
13
`use rustc_middle::middle::codegen_fn_attrs::{
`
14
14
`CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry,
`
15
15
`};
`
`@@ -87,7 +87,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
`
87
87
``
88
88
`let mut link_ordinal_span = None;
`
89
89
`let mut no_sanitize_span = None;
`
90
``
`-
let mut mixed_export_name_no_mangle_lint_state = MixedExportNameAndNoMangleState::default();
`
91
90
``
92
91
`for attr in attrs.iter() {
`
93
92
`` // In some cases, attribute are only valid on functions, but it's the check_attr
``
`@@ -119,20 +118,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
`
119
118
`.max();
`
120
119
`}
`
121
120
`AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
`
122
``
`-
AttributeKind::ExportName { name, span: attr_span } => {
`
``
121
`+
AttributeKind::ExportName { name, .. } => {
`
123
122
` codegen_fn_attrs.export_name = Some(*name);
`
124
``
`-
mixed_export_name_no_mangle_lint_state.track_export_name(*attr_span);
`
125
123
`}
`
126
124
`AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
`
127
125
`AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
`
128
126
`AttributeKind::NoMangle(attr_span) => {
`
129
127
`if tcx.opt_item_name(did.to_def_id()).is_some() {
`
130
128
` codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
`
131
``
`-
mixed_export_name_no_mangle_lint_state.track_no_mangle(
`
132
``
`-
*attr_span,
`
133
``
`-
tcx.local_def_id_to_hir_id(did),
`
134
``
`-
attr,
`
135
``
`-
);
`
136
129
`} else {
`
137
130
` tcx.dcx().emit_err(NoMangleNameless {
`
138
131
`span: *attr_span,
`
`@@ -437,8 +430,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
`
437
430
`}
`
438
431
`}
`
439
432
``
440
``
`-
mixed_export_name_no_mangle_lint_state.lint_if_mixed(tcx);
`
441
``
-
442
433
`// Apply the minimum function alignment here, so that individual backends don't have to.
`
443
434
` codegen_fn_attrs.alignment =
`
444
435
`Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
`
`@@ -665,49 +656,6 @@ fn check_link_name_xor_ordinal(
`
665
656
`}
`
666
657
`}
`
667
658
``
668
``
`-
#[derive(Default)]
`
669
``
`-
struct MixedExportNameAndNoMangleState<'a> {
`
670
``
`-
export_name: Option,
`
671
``
`-
hir_id: Option,
`
672
``
`-
no_mangle: Option,
`
673
``
`-
no_mangle_attr: Option<&'a hir::Attribute>,
`
674
``
`-
}
`
675
``
-
676
``
`-
impl<'a> MixedExportNameAndNoMangleState<'a> {
`
677
``
`-
fn track_export_name(&mut self, span: Span) {
`
678
``
`-
self.export_name = Some(span);
`
679
``
`-
}
`
680
``
-
681
``
`-
fn track_no_mangle(&mut self, span: Span, hir_id: HirId, attr_name: &'a hir::Attribute) {
`
682
``
`-
self.no_mangle = Some(span);
`
683
``
`-
self.hir_id = Some(hir_id);
`
684
``
`-
self.no_mangle_attr = Some(attr_name);
`
685
``
`-
}
`
686
``
-
687
``
`-
/// Emit diagnostics if the lint condition is met.
`
688
``
`-
fn lint_if_mixed(self, tcx: TyCtxt<'_>) {
`
689
``
`-
if let Self {
`
690
``
`-
export_name: Some(export_name),
`
691
``
`-
no_mangle: Some(no_mangle),
`
692
``
`-
hir_id: Some(hir_id),
`
693
``
`-
no_mangle_attr: Some(_),
`
694
``
`-
} = self
`
695
``
`-
{
`
696
``
`-
tcx.emit_node_span_lint(
`
697
``
`-
lint::builtin::UNUSED_ATTRIBUTES,
`
698
``
`-
hir_id,
`
699
``
`-
no_mangle,
`
700
``
`-
errors::MixedExportNameAndNoMangle {
`
701
``
`-
no_mangle,
`
702
``
`-
no_mangle_attr: "#[unsafe(no_mangle)]".to_string(),
`
703
``
`-
export_name,
`
704
``
`-
removal_span: no_mangle,
`
705
``
`-
},
`
706
``
`-
);
`
707
``
`-
}
`
708
``
`-
}
`
709
``
`-
}
`
710
``
-
711
659
`/// We now check the #[rustc_autodiff] attributes which we generated from the #[autodiff(...)]
`
712
660
`/// macros. There are two forms. The pure one without args to mark primal functions (the functions
`
713
661
`/// being differentiated). The other form is #[rustc_autodiff(Mode, ActivityList)] on top of the
`