Skip to content

Commit bb8b741

Browse files
committed
Port #[export_stable] to the new attribute system
1 parent c720f49 commit bb8b741

File tree

8 files changed

+34
-13
lines changed

8 files changed

+34
-13
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

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

251+
/// Represents `#[export_stable]`.
252+
ExportStable,
253+
251254
/// Represents `#[ignore]`
252255
Ignore {
253256
span: Span,

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl AttributeKind {
2626
Deprecation { .. } => Yes,
2727
DocComment { .. } => Yes,
2828
ExportName { .. } => Yes,
29+
ExportStable => No,
2930
Ignore { .. } => No,
3031
Inline(..) => No,
3132
LinkName { .. } => Yes,

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 11 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,10 @@ 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+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ 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::{ExportStableParser, LinkNameParser, LinkSectionParser};
2626
use crate::attributes::lint_helpers::{AsPtrParser, PassByValueParser, PubTransparentParser};
2727
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
@@ -145,6 +145,7 @@ attribute_parsers!(
145145
Single<WithoutArgs<ColdParser>>,
146146
Single<WithoutArgs<ConstContinueParser>>,
147147
Single<WithoutArgs<ConstStabilityIndirectParser>>,
148+
Single<WithoutArgs<ExportStableParser>>,
148149
Single<WithoutArgs<LoopMatchParser>>,
149150
Single<WithoutArgs<MayDangleParser>>,
150151
Single<WithoutArgs<NoImplicitPreludeParser>>,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ pub fn check_builtin_meta_item(
271271
if matches!(
272272
name,
273273
sym::inline
274+
| sym::export_stable
274275
| sym::may_dangle
275276
| sym::rustc_as_ptr
276277
| sym::rustc_pub_transparent

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ 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+
}
207210
Attribute::Parsed(
208211
AttributeKind::BodyStability { .. }
209212
| AttributeKind::ConstStabilityIndirect
@@ -346,7 +349,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
346349
| sym::cfg_attr
347350
| sym::cfg_trace
348351
| sym::cfg_attr_trace
349-
| sym::export_stable // handled in `check_export`
350352
// need to be fixed
351353
| sym::cfi_encoding // FIXME(cfi_encoding)
352354
| sym::pointee // FIXME(derive_coerce_pointee)

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: 9 additions & 6 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
|
@@ -481,6 +475,15 @@ LL | #[target_feature]
481475
| expected this to be a list
482476
| help: must be of the form: `#[target_feature(enable = "feat1, feat2")]`
483477

478+
error[E0565]: malformed `export_stable` attribute input
479+
--> $DIR/malformed-attrs.rs:81:1
480+
|
481+
LL | #[export_stable = 1]
482+
| ^^^^^^^^^^^^^^^^---^
483+
| | |
484+
| | didn't expect any arguments here
485+
| help: must be of the form: `#[export_stable]`
486+
484487
error[E0539]: malformed `link_name` attribute input
485488
--> $DIR/malformed-attrs.rs:86:1
486489
|

0 commit comments

Comments
 (0)