Skip to content

Commit c276ab2

Browse files
committed
Port #[export_stable] to the new attribute system
1 parent 0c4fa26 commit c276ab2

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
@@ -250,6 +250,9 @@ pub enum AttributeKind {
250250
span: Span,
251251
},
252252

253+
/// Represents `#[export_stable]`.
254+
ExportStable,
255+
253256
/// Represents `#[inline]` and `#[rustc_force_inline]`.
254257
Inline(InlineAttr, Span),
255258

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
Inline(..) => No,
3031
LinkName { .. } => Yes,
3132
LinkSection { .. } => No,

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;
@@ -140,6 +140,7 @@ attribute_parsers!(
140140
Single<WithoutArgs<ColdParser>>,
141141
Single<WithoutArgs<ConstContinueParser>>,
142142
Single<WithoutArgs<ConstStabilityIndirectParser>>,
143+
Single<WithoutArgs<ExportStableParser>>,
143144
Single<WithoutArgs<LoopMatchParser>>,
144145
Single<WithoutArgs<MayDangleParser>>,
145146
Single<WithoutArgs<NoImplicitPreludeParser>>,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ fn emit_malformed_attribute(
286286
if matches!(
287287
name,
288288
sym::inline
289+
| sym::export_stable
289290
| sym::may_dangle
290291
| sym::rustc_as_ptr
291292
| 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
@@ -198,6 +198,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
198198
AttributeKind::RustcLayoutScalarValidRangeStart(_num, attr_span)
199199
| AttributeKind::RustcLayoutScalarValidRangeEnd(_num, attr_span),
200200
) => self.check_rustc_layout_scalar_valid_range(*attr_span, span, target),
201+
Attribute::Parsed(AttributeKind::ExportStable) => {
202+
// handled in `check_export`
203+
}
201204
Attribute::Parsed(
202205
AttributeKind::BodyStability { .. }
203206
| AttributeKind::ConstStabilityIndirect
@@ -338,7 +341,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
338341
| sym::cfg_attr
339342
| sym::cfg_trace
340343
| sym::cfg_attr_trace
341-
| sym::export_stable // handled in `check_export`
342344
// need to be fixed
343345
| sym::cfi_encoding // FIXME(cfi_encoding)
344346
| 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:80: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:89:1
5145
|
@@ -496,6 +490,15 @@ LL | #[target_feature]
496490
| expected this to be a list
497491
| help: must be of the form: `#[target_feature(enable = "feat1, feat2")]`
498492

493+
error[E0565]: malformed `export_stable` attribute input
494+
--> $DIR/malformed-attrs.rs:80:1
495+
|
496+
LL | #[export_stable = 1]
497+
| ^^^^^^^^^^^^^^^^---^
498+
| | |
499+
| | didn't expect any arguments here
500+
| help: must be of the form: `#[export_stable]`
501+
499502
error[E0539]: malformed `link_name` attribute input
500503
--> $DIR/malformed-attrs.rs:85:1
501504
|

0 commit comments

Comments
 (0)