Skip to content

Commit 045faa8

Browse files
committed
Port #[may_dangle] to the new attribute system
1 parent 9c4ff56 commit 045faa8

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,13 @@ pub enum AttributeKind {
230230

231231
/// Represents `#[rustc_macro_transparency]`.
232232
MacroTransparency(Transparency),
233+
234+
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
235+
MayDangle(Span),
236+
233237
/// Represents `#[optimize(size|speed)]`
234238
Optimize(OptimizeAttr, Span),
239+
235240
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
236241
Repr(ThinVec<(ReprAttr, Span)>),
237242

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub(crate) mod deprecation;
3434
pub(crate) mod inline;
3535
pub(crate) mod lint_helpers;
3636
pub(crate) mod repr;
37+
pub(crate) mod semantics;
3738
pub(crate) mod stability;
3839
pub(crate) mod transparency;
3940
pub(crate) mod util;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_feature::{AttributeTemplate, template};
3+
use rustc_span::{Symbol, sym};
4+
5+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6+
use crate::context::{AcceptContext, Stage};
7+
use crate::parser::ArgParser;
8+
9+
pub(crate) struct MayDangleParser;
10+
impl<S: Stage> SingleAttributeParser<S> for MayDangleParser {
11+
const PATH: &[Symbol] = &[sym::may_dangle];
12+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
13+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
14+
const TEMPLATE: AttributeTemplate = template!(Word);
15+
16+
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
17+
Some(AttributeKind::MayDangle(cx.attr_span))
18+
}
19+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::attributes::deprecation::DeprecationParser;
2121
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
2222
use crate::attributes::lint_helpers::AsPtrParser;
2323
use crate::attributes::repr::{AlignParser, ReprParser};
24+
use crate::attributes::semantics::MayDangleParser;
2425
use crate::attributes::stability::{
2526
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
2627
};
@@ -109,6 +110,7 @@ attribute_parsers!(
109110
Single<ConstStabilityIndirectParser>,
110111
Single<DeprecationParser>,
111112
Single<InlineParser>,
113+
Single<MayDangleParser>,
112114
Single<OptimizeParser>,
113115
Single<RustcForceInlineParser>,
114116
Single<TransparencyParser>,

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
161161
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => {
162162
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
163163
}
164+
&Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
165+
self.check_may_dangle(hir_id, attr_span)
166+
}
164167
Attribute::Unparsed(_) => {
165168
match attr.path().as_slice() {
166169
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -234,7 +237,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
234237
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
235238
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
236239
[sym::must_use, ..] => self.check_must_use(hir_id, attr, target),
237-
[sym::may_dangle, ..] => self.check_may_dangle(hir_id, attr),
238240
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
239241
[sym::rustc_allow_incoherent_impl, ..] => {
240242
self.check_allow_incoherent_impl(attr, span, target)
@@ -1616,7 +1618,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16161618
}
16171619

16181620
/// Checks if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
1619-
fn check_may_dangle(&self, hir_id: HirId, attr: &Attribute) {
1621+
fn check_may_dangle(&self, hir_id: HirId, attr_span: Span) {
16201622
if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
16211623
&& matches!(
16221624
param.kind,
@@ -1633,7 +1635,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16331635
return;
16341636
}
16351637

1636-
self.dcx().emit_err(errors::InvalidMayDangle { attr_span: attr.span() });
1638+
self.dcx().emit_err(errors::InvalidMayDangle { attr_span });
16371639
}
16381640

16391641
/// Checks if `#[cold]` is applied to a non-function.

0 commit comments

Comments
 (0)