Skip to content

Commit 86039cc

Browse files
committed
Port #[rustc_as_ptr] to the new attribute system
1 parent 8da6239 commit 86039cc

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ pub enum AttributeKind {
158158
/// Represents `#[allow_internal_unstable]`.
159159
AllowInternalUnstable(ThinVec<(Symbol, Span)>),
160160

161+
/// Represents `#[rustc_as_ptr]` (used by the `dangling_pointers_from_temporaries` lint).
162+
AsPtr(Span),
163+
161164
/// Represents `#[rustc_default_body_unstable]`.
162165
BodyStability {
163166
stability: DefaultBodyStability,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 AsPtrParser;
9+
10+
impl<S: Stage> SingleAttributeParser<S> for AsPtrParser {
11+
const PATH: &[Symbol] = &[sym::rustc_as_ptr];
12+
13+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
14+
15+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
16+
17+
fn convert(_cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
18+
// FIXME: check that there's no args
19+
Some(AttributeKind::AsPtr(_cx.attr_span))
20+
}
21+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) mod allow_unstable;
2929
pub(crate) mod cfg;
3030
pub(crate) mod confusables;
3131
pub(crate) mod deprecation;
32+
pub(crate) mod lint_helpers;
3233
pub(crate) mod repr;
3334
pub(crate) mod stability;
3435
pub(crate) mod transparency;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1818
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
1919
use crate::attributes::confusables::ConfusablesParser;
2020
use crate::attributes::deprecation::DeprecationParser;
21+
use crate::attributes::lint_helpers::AsPtrParser;
2122
use crate::attributes::repr::ReprParser;
2223
use crate::attributes::stability::{
2324
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -102,6 +103,7 @@ attribute_parsers!(
102103
// tidy-alphabetical-end
103104

104105
// tidy-alphabetical-start
106+
Single<AsPtrParser>,
105107
Single<ConstStabilityIndirectParser>,
106108
Single<DeprecationParser>,
107109
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::AsPtr(span)) => *span,
13051306
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13061307
}
13071308
}

compiler/rustc_lint/src/dangling.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_ast::visit::{visit_opt, walk_list};
2+
use rustc_attr_data_structures::{AttributeKind, find_attr};
23
use rustc_hir::def_id::LocalDefId;
34
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr};
45
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, LangItem};
@@ -133,7 +134,7 @@ fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
133134
&& let ty = cx.typeck_results().expr_ty(receiver)
134135
&& owns_allocation(cx.tcx, ty)
135136
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
136-
&& cx.tcx.has_attr(fn_id, sym::rustc_as_ptr)
137+
&& find_attr!(cx.tcx.get_all_attrs(fn_id), AttributeKind::AsPtr(_))
137138
{
138139
// FIXME: use `emit_node_lint` when `#[primary_span]` is added.
139140
cx.tcx.emit_node_span_lint(

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 3 deletions
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::AsPtr(_)) => {
151+
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
152+
}
150153
Attribute::Unparsed(_) => {
151154
match attr.path().as_slice() {
152155
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -188,9 +191,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
188191
self.check_rustc_std_internal_symbol(attr, span, target)
189192
}
190193
[sym::naked, ..] => self.check_naked(hir_id, attr, span, target, attrs),
191-
[sym::rustc_as_ptr, ..] => {
192-
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
193-
}
194194
[sym::rustc_no_implicit_autorefs, ..] => {
195195
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
196196
}

0 commit comments

Comments
 (0)