Skip to content

Commit 37bc3f6

Browse files
committed
Port #[may_dangle] to the new attribute system
1 parent 2fcf177 commit 37bc3f6

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

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

233+
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
234+
MayDangle(Span),
235+
233236
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
234237
Repr(ThinVec<(ReprAttr, Span)>),
235238

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub(crate) mod deprecation;
3333
pub(crate) mod inline;
3434
pub(crate) mod lint_helpers;
3535
pub(crate) mod repr;
36+
pub(crate) mod semantics;
3637
pub(crate) mod stability;
3738
pub(crate) mod transparency;
3839
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
@@ -20,6 +20,7 @@ use crate::attributes::deprecation::DeprecationParser;
2020
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
2121
use crate::attributes::lint_helpers::AsPtrParser;
2222
use crate::attributes::repr::{AlignParser, ReprParser};
23+
use crate::attributes::semantics::MayDangleParser;
2324
use crate::attributes::stability::{
2425
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
2526
};
@@ -108,6 +109,7 @@ attribute_parsers!(
108109
Single<ConstStabilityIndirectParser>,
109110
Single<DeprecationParser>,
110111
Single<InlineParser>,
112+
Single<MayDangleParser>,
111113
Single<RustcForceInlineParser>,
112114
Single<TransparencyParser>,
113115
// tidy-alphabetical-end

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: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
158158
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => {
159159
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
160160
}
161+
&Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
162+
// Check if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
163+
if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
164+
&& matches!(
165+
param.kind,
166+
hir::GenericParamKind::Lifetime { .. }
167+
| hir::GenericParamKind::Type { .. }
168+
)
169+
&& matches!(param.source, hir::GenericParamSource::Generics)
170+
&& let parent_hir_id = self.tcx.parent_hir_id(hir_id)
171+
&& let hir::Node::Item(item) = self.tcx.hir_node(parent_hir_id)
172+
&& let hir::ItemKind::Impl(impl_) = item.kind
173+
&& let Some(trait_) = impl_.of_trait
174+
&& let Some(def_id) = trait_.trait_def_id()
175+
&& self.tcx.is_lang_item(def_id, hir::LangItem::Drop)
176+
{
177+
// OK
178+
} else {
179+
self.dcx().emit_err(errors::InvalidMayDangle { attr_span });
180+
}
181+
}
161182
Attribute::Unparsed(_) => {
162183
match attr.path().as_slice() {
163184
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -232,7 +253,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
232253
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
233254
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
234255
[sym::must_use, ..] => self.check_must_use(hir_id, attr, target),
235-
[sym::may_dangle, ..] => self.check_may_dangle(hir_id, attr),
236256
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
237257
[sym::rustc_allow_incoherent_impl, ..] => {
238258
self.check_allow_incoherent_impl(attr, span, target)
@@ -1613,27 +1633,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16131633
}
16141634
}
16151635

1616-
/// Checks if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
1617-
fn check_may_dangle(&self, hir_id: HirId, attr: &Attribute) {
1618-
if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
1619-
&& matches!(
1620-
param.kind,
1621-
hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. }
1622-
)
1623-
&& matches!(param.source, hir::GenericParamSource::Generics)
1624-
&& let parent_hir_id = self.tcx.parent_hir_id(hir_id)
1625-
&& let hir::Node::Item(item) = self.tcx.hir_node(parent_hir_id)
1626-
&& let hir::ItemKind::Impl(impl_) = item.kind
1627-
&& let Some(trait_) = impl_.of_trait
1628-
&& let Some(def_id) = trait_.trait_def_id()
1629-
&& self.tcx.is_lang_item(def_id, hir::LangItem::Drop)
1630-
{
1631-
return;
1632-
}
1633-
1634-
self.dcx().emit_err(errors::InvalidMayDangle { attr_span: attr.span() });
1635-
}
1636-
16371636
/// Checks if `#[cold]` is applied to a non-function.
16381637
fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
16391638
match target {

0 commit comments

Comments
 (0)