@@ -3,7 +3,7 @@ use std::str::FromStr;
33use rustc_abi:: { Align , ExternAbi } ;
44use rustc_ast:: expand:: autodiff_attrs:: { AutoDiffAttrs , DiffActivity , DiffMode } ;
55use rustc_ast:: { LitKind , MetaItem , MetaItemInner , attr} ;
6- use rustc_hir:: attrs:: { AttributeKind , InlineAttr , InstructionSetAttr , UsedBy } ;
6+ use rustc_hir:: attrs:: { AttributeKind , InlineAttr , InstructionSetAttr , LinkageArg , UsedBy } ;
77use rustc_hir:: def:: DefKind ;
88use rustc_hir:: def_id:: { DefId , LOCAL_CRATE , LocalDefId } ;
99use rustc_hir:: weak_lang_items:: WEAK_LANG_ITEMS ;
@@ -26,31 +26,6 @@ use crate::target_features::{
2626 check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
2727} ;
2828
29- fn linkage_by_name ( tcx : TyCtxt < ' _ > , def_id : LocalDefId , name : & str ) -> Linkage {
30- use rustc_middle:: mir:: mono:: Linkage :: * ;
31-
32- // Use the names from src/llvm/docs/LangRef.rst here. Most types are only
33- // applicable to variable declarations and may not really make sense for
34- // Rust code in the first place but allow them anyway and trust that the
35- // user knows what they're doing. Who knows, unanticipated use cases may pop
36- // up in the future.
37- //
38- // ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
39- // and don't have to be, LLVM treats them as no-ops.
40- match name {
41- "available_externally" => AvailableExternally ,
42- "common" => Common ,
43- "extern_weak" => ExternalWeak ,
44- "external" => External ,
45- "internal" => Internal ,
46- "linkonce" => LinkOnceAny ,
47- "linkonce_odr" => LinkOnceODR ,
48- "weak" => WeakAny ,
49- "weak_odr" => WeakODR ,
50- _ => tcx. dcx ( ) . span_fatal ( tcx. def_span ( def_id) , "invalid linkage specified" ) ,
51- }
52- }
53-
5429/// In some cases, attributes are only valid on functions, but it's the `check_attr`
5530/// pass that checks that they aren't used anywhere else, rather than this module.
5631/// In these cases, we bail from performing further checks that are only meaningful for
@@ -103,13 +78,6 @@ fn parse_instruction_set_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> Option<Instr
10378 }
10479}
10580
106- // FIXME(jdonszelmann): remove when linkage becomes a parsed attr
107- fn parse_linkage_attr ( tcx : TyCtxt < ' _ > , did : LocalDefId , attr : & Attribute ) -> Option < Linkage > {
108- let val = attr. value_str ( ) ?;
109- let linkage = linkage_by_name ( tcx, did, val. as_str ( ) ) ;
110- Some ( linkage)
111- }
112-
11381// FIXME(jdonszelmann): remove when no_sanitize becomes a parsed attr
11482fn parse_no_sanitize_attr ( tcx : TyCtxt < ' _ > , attr : & Attribute ) -> Option < SanitizerSet > {
11583 let list = attr. meta_item_list ( ) ?;
@@ -332,6 +300,40 @@ fn process_builtin_attrs(
332300 AttributeKind :: StdInternalSymbol ( _) => {
333301 codegen_fn_attrs. flags |= CodegenFnAttrFlags :: RUSTC_STD_INTERNAL_SYMBOL
334302 }
303+ AttributeKind :: Linkage ( linkage, _) => {
304+ let linkage = match linkage {
305+ LinkageArg :: AvailableExternally => Linkage :: AvailableExternally ,
306+ LinkageArg :: Common => Linkage :: Common ,
307+ LinkageArg :: ExternWeak => Linkage :: ExternalWeak ,
308+ LinkageArg :: External => Linkage :: External ,
309+ LinkageArg :: Internal => Linkage :: Internal ,
310+ LinkageArg :: Linkonce => Linkage :: LinkOnceAny ,
311+ LinkageArg :: LinkOnceOdr => Linkage :: LinkOnceODR ,
312+ LinkageArg :: Weak => Linkage :: WeakAny ,
313+ LinkageArg :: WeakOdr => Linkage :: WeakODR ,
314+ } ;
315+
316+ let linkage = Some ( linkage) ;
317+
318+ if tcx. is_foreign_item ( did) {
319+ codegen_fn_attrs. import_linkage = linkage;
320+
321+ if tcx. is_mutable_static ( did. into ( ) ) {
322+ let mut diag = tcx. dcx ( ) . struct_span_err (
323+ attr. span ( ) ,
324+ "extern mutable statics are not allowed with `#[linkage]`" ,
325+ ) ;
326+ diag. note (
327+ "marking the extern static mutable would allow changing which \
328+ symbol the static references rather than make the target of the \
329+ symbol mutable",
330+ ) ;
331+ diag. emit ( ) ;
332+ }
333+ } else {
334+ codegen_fn_attrs. linkage = linkage;
335+ }
336+ }
335337 _ => { }
336338 }
337339 }
@@ -349,28 +351,6 @@ fn process_builtin_attrs(
349351 codegen_fn_attrs. flags |= CodegenFnAttrFlags :: ALLOCATOR_ZEROED
350352 }
351353 sym:: thread_local => codegen_fn_attrs. flags |= CodegenFnAttrFlags :: THREAD_LOCAL ,
352- sym:: linkage => {
353- let linkage = parse_linkage_attr ( tcx, did, attr) ;
354-
355- if tcx. is_foreign_item ( did) {
356- codegen_fn_attrs. import_linkage = linkage;
357-
358- if tcx. is_mutable_static ( did. into ( ) ) {
359- let mut diag = tcx. dcx ( ) . struct_span_err (
360- attr. span ( ) ,
361- "extern mutable statics are not allowed with `#[linkage]`" ,
362- ) ;
363- diag. note (
364- "marking the extern static mutable would allow changing which \
365- symbol the static references rather than make the target of the \
366- symbol mutable",
367- ) ;
368- diag. emit ( ) ;
369- }
370- } else {
371- codegen_fn_attrs. linkage = linkage;
372- }
373- }
374354 sym:: no_sanitize => {
375355 interesting_spans. no_sanitize = Some ( attr. span ( ) ) ;
376356 codegen_fn_attrs. no_sanitize |=
0 commit comments