Skip to content

Commit 1980db4

Browse files
authored
Unrolled build for #142539
Rollup merge of #142539 - GrigorenkoPV:attributes/may_dangle, r=jdonszelmann Port `#[may_dangle]` to the new attribute system Very similar to #142498. This is a part of #131229, so r? `@jdonszelmann`
2 parents 6d0c9e2 + 045faa8 commit 1980db4

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
@@ -233,8 +233,13 @@ pub enum AttributeKind {
233233

234234
/// Represents `#[rustc_macro_transparency]`.
235235
MacroTransparency(Transparency),
236+
237+
/// Represents [`#[may_dangle]`](https://std-dev-guide.rust-lang.org/tricky/may-dangle.html).
238+
MayDangle(Span),
239+
236240
/// Represents `#[optimize(size|speed)]`
237241
Optimize(OptimizeAttr, Span),
242+
238243
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
239244
Repr(ThinVec<(ReprAttr, Span)>),
240245

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
};
@@ -110,6 +111,7 @@ attribute_parsers!(
110111
Single<ConstStabilityIndirectParser>,
111112
Single<DeprecationParser>,
112113
Single<InlineParser>,
114+
Single<MayDangleParser>,
113115
Single<OptimizeParser>,
114116
Single<RustcForceInlineParser>,
115117
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
@@ -163,6 +163,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
163163
Attribute::Parsed(AttributeKind::AsPtr(attr_span)) => {
164164
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
165165
}
166+
&Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
167+
self.check_may_dangle(hir_id, attr_span)
168+
}
166169
Attribute::Unparsed(_) => {
167170
match attr.path().as_slice() {
168171
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -236,7 +239,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
236239
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
237240
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
238241
[sym::must_use, ..] => self.check_must_use(hir_id, attr, target),
239-
[sym::may_dangle, ..] => self.check_may_dangle(hir_id, attr),
240242
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
241243
[sym::rustc_allow_incoherent_impl, ..] => {
242244
self.check_allow_incoherent_impl(attr, span, target)
@@ -1619,7 +1621,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16191621
}
16201622

16211623
/// Checks if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
1622-
fn check_may_dangle(&self, hir_id: HirId, attr: &Attribute) {
1624+
fn check_may_dangle(&self, hir_id: HirId, attr_span: Span) {
16231625
if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
16241626
&& matches!(
16251627
param.kind,
@@ -1636,7 +1638,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
16361638
return;
16371639
}
16381640

1639-
self.dcx().emit_err(errors::InvalidMayDangle { attr_span: attr.span() });
1641+
self.dcx().emit_err(errors::InvalidMayDangle { attr_span });
16401642
}
16411643

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

0 commit comments

Comments
 (0)