Skip to content

Commit 76dfb1c

Browse files
committed
Port #[may_dangle] to the new attribute system
1 parent 45acf54 commit 76dfb1c

File tree

6 files changed

+48
-22
lines changed

6 files changed

+48
-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
@@ -224,6 +224,9 @@ pub enum AttributeKind {
224224
/// Represents `#[rustc_macro_transparency]`.
225225
MacroTransparency(Transparency),
226226

227+
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
228+
MayDangle(Span),
229+
227230
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
228231
Repr(ThinVec<(ReprAttr, Span)>),
229232

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(crate) mod confusables;
3131
pub(crate) mod deprecation;
3232
pub(crate) mod lint_helpers;
3333
pub(crate) mod repr;
34+
pub(crate) mod semantics;
3435
pub(crate) mod stability;
3536
pub(crate) mod transparency;
3637
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
@@ -20,6 +20,7 @@ use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
2121
use crate::attributes::lint_helpers::AsPtrParser;
2222
use crate::attributes::repr::ReprParser;
23+
use crate::attributes::semantics::MayDangleParser;
2324
use crate::attributes::stability::{
2425
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
2526
};
@@ -106,6 +107,7 @@ attribute_parsers!(
106107
Single<AsPtrParser>,
107108
Single<ConstStabilityIndirectParser>,
108109
Single<DeprecationParser>,
110+
Single<MayDangleParser>,
109111
Single<TransparencyParser>,
110112
// tidy-alphabetical-end
111113
];

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
@@ -150,6 +150,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
150150
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => {
151151
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
152152
}
153+
&Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
154+
// Check if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
155+
if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
156+
&& matches!(
157+
param.kind,
158+
hir::GenericParamKind::Lifetime { .. }
159+
| hir::GenericParamKind::Type { .. }
160+
)
161+
&& matches!(param.source, hir::GenericParamSource::Generics)
162+
&& let parent_hir_id = self.tcx.parent_hir_id(hir_id)
163+
&& let hir::Node::Item(item) = self.tcx.hir_node(parent_hir_id)
164+
&& let hir::ItemKind::Impl(impl_) = item.kind
165+
&& let Some(trait_) = impl_.of_trait
166+
&& let Some(def_id) = trait_.trait_def_id()
167+
&& self.tcx.is_lang_item(def_id, hir::LangItem::Drop)
168+
{
169+
// OK
170+
} else {
171+
self.dcx().emit_err(errors::InvalidMayDangle { attr_span });
172+
}
173+
}
153174
Attribute::Unparsed(_) => {
154175
match attr.path().as_slice() {
155176
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -225,7 +246,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
225246
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
226247
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
227248
[sym::must_use, ..] => self.check_must_use(hir_id, attr, target),
228-
[sym::may_dangle, ..] => self.check_may_dangle(hir_id, attr),
229249
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
230250
[sym::rustc_allow_incoherent_impl, ..] => {
231251
self.check_allow_incoherent_impl(attr, span, target)
@@ -1589,27 +1609,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15891609
}
15901610
}
15911611

1592-
/// Checks if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
1593-
fn check_may_dangle(&self, hir_id: HirId, attr: &Attribute) {
1594-
if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
1595-
&& matches!(
1596-
param.kind,
1597-
hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. }
1598-
)
1599-
&& matches!(param.source, hir::GenericParamSource::Generics)
1600-
&& let parent_hir_id = self.tcx.parent_hir_id(hir_id)
1601-
&& let hir::Node::Item(item) = self.tcx.hir_node(parent_hir_id)
1602-
&& let hir::ItemKind::Impl(impl_) = item.kind
1603-
&& let Some(trait_) = impl_.of_trait
1604-
&& let Some(def_id) = trait_.trait_def_id()
1605-
&& self.tcx.is_lang_item(def_id, hir::LangItem::Drop)
1606-
{
1607-
return;
1608-
}
1609-
1610-
self.dcx().emit_err(errors::InvalidMayDangle { attr_span: attr.span() });
1611-
}
1612-
16131612
/// Checks if `#[cold]` is applied to a non-function.
16141613
fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
16151614
match target {

0 commit comments

Comments
 (0)