Skip to content

Commit f2ffea0

Browse files
committed
Port #[may_dangle] to the new attribute system
1 parent 75e7cf5 commit f2ffea0

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ pub enum AttributeKind {
191191
/// Represents `#[rustc_macro_transparency]`.
192192
MacroTransparency(Transparency),
193193

194+
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
195+
MayDangle(Span),
196+
194197
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
195198
Repr(ThinVec<(ReprAttr, Span)>),
196199

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) mod cfg;
3030
pub(crate) mod confusables;
3131
pub(crate) mod deprecation;
3232
pub(crate) mod repr;
33+
pub(crate) mod semantics;
3334
pub(crate) mod stability;
3435
pub(crate) mod transparency;
3536
pub(crate) mod util;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_span::{Symbol, sym};
3+
4+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
5+
use crate::context::{AcceptContext, Stage};
6+
use crate::parser::ArgParser;
7+
8+
pub(crate) struct MayDangleParser;
9+
impl<S: Stage> SingleAttributeParser<S> for MayDangleParser {
10+
const PATH: &[Symbol] = &[sym::may_dangle];
11+
12+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
13+
14+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
15+
16+
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
17+
// FIXME: check that there's no args (this is currently checked elsewhere)
18+
Some(AttributeKind::MayDangle(cx.attr_span))
19+
}
20+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInterna
1919
use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
2121
use crate::attributes::repr::ReprParser;
22+
use crate::attributes::semantics::MayDangleParser;
2223
use crate::attributes::stability::{
2324
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
2425
};
@@ -104,6 +105,7 @@ attribute_parsers!(
104105
// tidy-alphabetical-start
105106
Single<ConstStabilityIndirectParser>,
106107
Single<DeprecationParser>,
108+
Single<MayDangleParser>,
107109
Single<TransparencyParser>,
108110
// tidy-alphabetical-end
109111
];

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ impl AttributeExt for Attribute {
13021302
// FIXME: should not be needed anymore when all attrs are parsed
13031303
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
13041304
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
1305+
Attribute::Parsed(AttributeKind::MayDangle(span)) => *span,
13051306
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13061307
}
13071308
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
147147
| AttributeKind::ConstStabilityIndirect
148148
| AttributeKind::MacroTransparency(_),
149149
) => { /* do nothing */ }
150+
Attribute::Parsed(AttributeKind::MayDangle(_)) => {
151+
self.check_may_dangle(hir_id, attr)
152+
}
150153
Attribute::Unparsed(_) => {
151154
match attr.path().as_slice() {
152155
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -225,7 +228,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
225228
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
226229
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
227230
[sym::must_use, ..] => self.check_must_use(hir_id, attr, target),
228-
[sym::may_dangle, ..] => self.check_may_dangle(hir_id, attr),
229231
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
230232
[sym::rustc_allow_incoherent_impl, ..] => {
231233
self.check_allow_incoherent_impl(attr, span, target)

0 commit comments

Comments
 (0)