Skip to content

Commit bc9b313

Browse files
authored
Rollup merge of #143402 - GrigorenkoPV:attributes/link_attrs, r=jdonszelmann
Port several linking (linkage?) related attributes the new attribute system This ports: - `#[export_stable]` - `#[ffi_const]` - `#[ffi_pure]` - `#[rustc_std_internal_symbol]` Part of #131229 r? ``@oli-obk``
2 parents f838cbc + 4f0b0b0 commit bc9b313

File tree

9 files changed

+113
-40
lines changed

9 files changed

+113
-40
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ pub enum AttributeKind {
248248
span: Span,
249249
},
250250

251+
/// Represents `#[export_stable]`.
252+
ExportStable,
253+
254+
/// Represents `#[ffi_const]`.
255+
FfiConst(Span),
256+
257+
/// Represents `#[ffi_pure]`.
258+
FfiPure(Span),
259+
251260
/// Represents `#[ignore]`
252261
Ignore {
253262
span: Span,
@@ -326,6 +335,9 @@ pub enum AttributeKind {
326335
span: Span,
327336
},
328337

338+
/// Represents `#[rustc_std_internal_symbol]`.
339+
StdInternalSymbol(Span),
340+
329341
/// Represents `#[target_feature(enable = "...")]`
330342
TargetFeature(ThinVec<(Symbol, Span)>, Span),
331343

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ impl AttributeKind {
2626
Deprecation { .. } => Yes,
2727
DocComment { .. } => Yes,
2828
ExportName { .. } => Yes,
29+
ExportStable => No,
30+
FfiConst(..) => No,
31+
FfiPure(..) => No,
2932
Ignore { .. } => No,
3033
Inline(..) => No,
3134
LinkName { .. } => Yes,
@@ -48,6 +51,7 @@ impl AttributeKind {
4851
RustcObjectLifetimeDefault => No,
4952
SkipDuringMethodDispatch { .. } => No,
5053
Stability { .. } => Yes,
54+
StdInternalSymbol(..) => No,
5155
TargetFeature(..) => No,
5256
TrackCaller(..) => Yes,
5357
Used { .. } => No,

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use rustc_attr_data_structures::AttributeKind;
22
use rustc_attr_data_structures::AttributeKind::{LinkName, LinkSection};
33
use rustc_feature::{AttributeTemplate, template};
4-
use rustc_span::{Symbol, sym};
4+
use rustc_span::{Span, Symbol, sym};
55

6-
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6+
use crate::attributes::{
7+
AttributeOrder, NoArgsAttributeParser, OnDuplicate, SingleAttributeParser,
8+
};
79
use crate::context::{AcceptContext, Stage};
810
use crate::parser::ArgParser;
911
use crate::session_diagnostics::NullOnLinkSection;
@@ -57,3 +59,31 @@ impl<S: Stage> SingleAttributeParser<S> for LinkSectionParser {
5759
Some(LinkSection { name, span: cx.attr_span })
5860
}
5961
}
62+
63+
pub(crate) struct ExportStableParser;
64+
impl<S: Stage> NoArgsAttributeParser<S> for ExportStableParser {
65+
const PATH: &[Symbol] = &[sym::export_stable];
66+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
67+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ExportStable;
68+
}
69+
70+
pub(crate) struct FfiConstParser;
71+
impl<S: Stage> NoArgsAttributeParser<S> for FfiConstParser {
72+
const PATH: &[Symbol] = &[sym::ffi_const];
73+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
74+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::FfiConst;
75+
}
76+
77+
pub(crate) struct FfiPureParser;
78+
impl<S: Stage> NoArgsAttributeParser<S> for FfiPureParser {
79+
const PATH: &[Symbol] = &[sym::ffi_pure];
80+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
81+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::FfiPure;
82+
}
83+
84+
pub(crate) struct StdInternalSymbolParser;
85+
impl<S: Stage> NoArgsAttributeParser<S> for StdInternalSymbolParser {
86+
const PATH: &[Symbol] = &[sym::rustc_std_internal_symbol];
87+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
88+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::StdInternalSymbol;
89+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use crate::attributes::codegen_attrs::{
2222
use crate::attributes::confusables::ConfusablesParser;
2323
use crate::attributes::deprecation::DeprecationParser;
2424
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
25-
use crate::attributes::link_attrs::{LinkNameParser, LinkSectionParser};
25+
use crate::attributes::link_attrs::{
26+
ExportStableParser, FfiConstParser, FfiPureParser, LinkNameParser, LinkSectionParser,
27+
StdInternalSymbolParser,
28+
};
2629
use crate::attributes::lint_helpers::{AsPtrParser, PassByValueParser, PubTransparentParser};
2730
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2831
use crate::attributes::must_use::MustUseParser;
@@ -145,13 +148,17 @@ attribute_parsers!(
145148
Single<WithoutArgs<ColdParser>>,
146149
Single<WithoutArgs<ConstContinueParser>>,
147150
Single<WithoutArgs<ConstStabilityIndirectParser>>,
151+
Single<WithoutArgs<ExportStableParser>>,
152+
Single<WithoutArgs<FfiConstParser>>,
153+
Single<WithoutArgs<FfiPureParser>>,
148154
Single<WithoutArgs<LoopMatchParser>>,
149155
Single<WithoutArgs<MayDangleParser>>,
150156
Single<WithoutArgs<NoImplicitPreludeParser>>,
151157
Single<WithoutArgs<NoMangleParser>>,
152158
Single<WithoutArgs<NonExhaustiveParser>>,
153159
Single<WithoutArgs<PassByValueParser>>,
154160
Single<WithoutArgs<PubTransparentParser>>,
161+
Single<WithoutArgs<StdInternalSymbolParser>>,
155162
Single<WithoutArgs<TrackCallerParser>>,
156163
// tidy-alphabetical-end
157164
];

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
203203
UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
204204
UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
205205
},
206+
AttributeKind::FfiConst(_) => {
207+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST
208+
}
209+
AttributeKind::FfiPure(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
210+
AttributeKind::StdInternalSymbol(_) => {
211+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
212+
}
206213
_ => {}
207214
}
208215
}
@@ -213,17 +220,12 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
213220

214221
match name {
215222
sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
216-
sym::ffi_pure => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
217-
sym::ffi_const => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST,
218223
sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
219224
sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
220225
sym::rustc_deallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR,
221226
sym::rustc_allocator_zeroed => {
222227
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
223228
}
224-
sym::rustc_std_internal_symbol => {
225-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
226-
}
227229
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
228230
sym::linkage => {
229231
if let Some(val) = attr.value_str() {

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ pub fn check_builtin_meta_item(
271271
if matches!(
272272
name,
273273
sym::inline
274+
| sym::export_stable
275+
| sym::ffi_const
276+
| sym::ffi_pure
277+
| sym::rustc_std_internal_symbol
274278
| sym::may_dangle
275279
| sym::rustc_as_ptr
276280
| sym::rustc_pub_transparent

compiler/rustc_passes/src/check_attr.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
204204
AttributeKind::RustcLayoutScalarValidRangeStart(_num, attr_span)
205205
| AttributeKind::RustcLayoutScalarValidRangeEnd(_num, attr_span),
206206
) => self.check_rustc_layout_scalar_valid_range(*attr_span, span, target),
207+
Attribute::Parsed(AttributeKind::ExportStable) => {
208+
// handled in `check_export`
209+
}
210+
&Attribute::Parsed(AttributeKind::FfiConst(attr_span)) => {
211+
self.check_ffi_const(attr_span, target)
212+
}
213+
&Attribute::Parsed(AttributeKind::FfiPure(attr_span)) => {
214+
self.check_ffi_pure(attr_span, attrs, target)
215+
}
207216
Attribute::Parsed(
208217
AttributeKind::BodyStability { .. }
209218
| AttributeKind::ConstStabilityIndirect
@@ -233,6 +242,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
233242
&Attribute::Parsed(AttributeKind::PassByValue(attr_span)) => {
234243
self.check_pass_by_value(attr_span, span, target)
235244
}
245+
&Attribute::Parsed(AttributeKind::StdInternalSymbol(attr_span)) => {
246+
self.check_rustc_std_internal_symbol(attr_span, span, target)
247+
}
236248
Attribute::Unparsed(attr_item) => {
237249
style = Some(attr_item.style);
238250
match attr.path().as_slice() {
@@ -258,9 +270,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
258270
),
259271
[sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
260272
[sym::debugger_visualizer, ..] => self.check_debugger_visualizer(attr, target),
261-
[sym::rustc_std_internal_symbol, ..] => {
262-
self.check_rustc_std_internal_symbol(attr, span, target)
263-
}
264273
[sym::rustc_no_implicit_autorefs, ..] => {
265274
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
266275
}
@@ -300,8 +309,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
300309
[sym::rustc_has_incoherent_inherent_impls, ..] => {
301310
self.check_has_incoherent_inherent_impls(attr, span, target)
302311
}
303-
[sym::ffi_pure, ..] => self.check_ffi_pure(attr.span(), attrs, target),
304-
[sym::ffi_const, ..] => self.check_ffi_const(attr.span(), target),
305312
[sym::link_ordinal, ..] => self.check_link_ordinal(attr, span, target),
306313
[sym::link, ..] => self.check_link(hir_id, attr, span, target),
307314
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
@@ -346,7 +353,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
346353
| sym::cfg_attr
347354
| sym::cfg_trace
348355
| sym::cfg_attr_trace
349-
| sym::export_stable // handled in `check_export`
350356
// need to be fixed
351357
| sym::cfi_encoding // FIXME(cfi_encoding)
352358
| sym::pointee // FIXME(derive_coerce_pointee)
@@ -1507,7 +1513,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15071513
self.dcx().emit_err(errors::FfiPureInvalidTarget { attr_span });
15081514
return;
15091515
}
1510-
if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
1516+
if find_attr!(attrs, AttributeKind::FfiConst(_)) {
15111517
// `#[ffi_const]` functions cannot be `#[ffi_pure]`
15121518
self.dcx().emit_err(errors::BothFfiConstAndPure { attr_span });
15131519
}
@@ -2214,13 +2220,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22142220
}
22152221
}
22162222

2217-
fn check_rustc_std_internal_symbol(&self, attr: &Attribute, span: Span, target: Target) {
2223+
fn check_rustc_std_internal_symbol(&self, attr_span: Span, span: Span, target: Target) {
22182224
match target {
22192225
Target::Fn | Target::Static | Target::ForeignFn | Target::ForeignStatic => {}
22202226
_ => {
2221-
self.tcx
2222-
.dcx()
2223-
.emit_err(errors::RustcStdInternalSymbol { attr_span: attr.span(), span });
2227+
self.tcx.dcx().emit_err(errors::RustcStdInternalSymbol { attr_span, span });
22242228
}
22252229
}
22262230
}

compiler/rustc_passes/src/check_export.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::iter;
22
use std::ops::ControlFlow;
33

44
use rustc_abi::ExternAbi;
5+
use rustc_attr_data_structures::{AttributeKind, find_attr};
56
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
67
use rustc_hir as hir;
78
use rustc_hir::def::DefKind;
@@ -14,7 +15,7 @@ use rustc_middle::ty::{
1415
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, Visibility,
1516
};
1617
use rustc_session::config::CrateType;
17-
use rustc_span::{Span, sym};
18+
use rustc_span::Span;
1819

1920
use crate::errors::UnexportableItem;
2021

@@ -44,7 +45,7 @@ impl<'tcx> ExportableItemCollector<'tcx> {
4445
}
4546

4647
fn item_is_exportable(&self, def_id: LocalDefId) -> bool {
47-
let has_attr = self.tcx.has_attr(def_id, sym::export_stable);
48+
let has_attr = find_attr!(self.tcx.get_all_attrs(def_id), AttributeKind::ExportStable);
4849
if !self.in_exportable_mod && !has_attr {
4950
return false;
5051
}
@@ -80,7 +81,7 @@ impl<'tcx> ExportableItemCollector<'tcx> {
8081
fn walk_item_with_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
8182
let def_id = item.hir_id().owner.def_id;
8283
let old_exportable_mod = self.in_exportable_mod;
83-
if self.tcx.get_attr(def_id, sym::export_stable).is_some() {
84+
if find_attr!(self.tcx.get_all_attrs(def_id), AttributeKind::ExportStable) {
8485
self.in_exportable_mod = true;
8586
}
8687
let old_seen_exportable_in_mod = std::mem::replace(&mut self.seen_exportable_in_mod, false);

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ error: malformed `crate_name` attribute input
4040
LL | #[crate_name]
4141
| ^^^^^^^^^^^^^ help: must be of the form: `#[crate_name = "name"]`
4242

43-
error: malformed `export_stable` attribute input
44-
--> $DIR/malformed-attrs.rs:81:1
45-
|
46-
LL | #[export_stable = 1]
47-
| ^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[export_stable]`
48-
4943
error: malformed `coverage` attribute input
5044
--> $DIR/malformed-attrs.rs:90:1
5145
|
@@ -140,24 +134,12 @@ error: malformed `fundamental` attribute input
140134
LL | #[fundamental()]
141135
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#[fundamental]`
142136

143-
error: malformed `ffi_pure` attribute input
144-
--> $DIR/malformed-attrs.rs:165:5
145-
|
146-
LL | #[unsafe(ffi_pure = 1)]
147-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_pure]`
148-
149137
error: malformed `link_ordinal` attribute input
150138
--> $DIR/malformed-attrs.rs:167:5
151139
|
152140
LL | #[link_ordinal]
153141
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_ordinal(ordinal)]`
154142

155-
error: malformed `ffi_const` attribute input
156-
--> $DIR/malformed-attrs.rs:171:5
157-
|
158-
LL | #[unsafe(ffi_const = 1)]
159-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[ffi_const]`
160-
161143
error: malformed `linkage` attribute input
162144
--> $DIR/malformed-attrs.rs:173:5
163145
|
@@ -481,6 +463,15 @@ LL | #[target_feature]
481463
| expected this to be a list
482464
| help: must be of the form: `#[target_feature(enable = "feat1, feat2")]`
483465

466+
error[E0565]: malformed `export_stable` attribute input
467+
--> $DIR/malformed-attrs.rs:81:1
468+
|
469+
LL | #[export_stable = 1]
470+
| ^^^^^^^^^^^^^^^^---^
471+
| | |
472+
| | didn't expect any arguments here
473+
| help: must be of the form: `#[export_stable]`
474+
484475
error[E0539]: malformed `link_name` attribute input
485476
--> $DIR/malformed-attrs.rs:86:1
486477
|
@@ -537,6 +528,24 @@ LL | #[rustc_layout_scalar_valid_range_end]
537528
| expected this to be a list
538529
| help: must be of the form: `#[rustc_layout_scalar_valid_range_end(end)]`
539530

531+
error[E0565]: malformed `ffi_pure` attribute input
532+
--> $DIR/malformed-attrs.rs:165:5
533+
|
534+
LL | #[unsafe(ffi_pure = 1)]
535+
| ^^^^^^^^^^^^^^^^^^---^^
536+
| | |
537+
| | didn't expect any arguments here
538+
| help: must be of the form: `#[ffi_pure]`
539+
540+
error[E0565]: malformed `ffi_const` attribute input
541+
--> $DIR/malformed-attrs.rs:171:5
542+
|
543+
LL | #[unsafe(ffi_const = 1)]
544+
| ^^^^^^^^^^^^^^^^^^^---^^
545+
| | |
546+
| | didn't expect any arguments here
547+
| help: must be of the form: `#[ffi_const]`
548+
540549
error[E0565]: malformed `non_exhaustive` attribute input
541550
--> $DIR/malformed-attrs.rs:197:1
542551
|

0 commit comments

Comments
 (0)