Skip to content

Commit da8d6bb

Browse files
committed
Port #[rustc_as_ptr] to the new attribute system
1 parent 68ac5ab commit da8d6bb

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
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 (this is currently checked elsewhere)
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_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: 12 additions & 12 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(attr_span)) => {
151+
self.check_applied_to_fn_or_method(hir_id, *attr_span, span, target)
152+
}
150153
Attribute::Unparsed(_) => {
151154
match attr.path().as_slice() {
152155
[sym::diagnostic, sym::do_not_recommend, ..] => {
@@ -188,26 +191,23 @@ 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, ..] => {
195-
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
195+
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
196196
}
197197
[sym::rustc_never_returns_null_ptr, ..] => {
198-
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
198+
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
199199
}
200200
[sym::rustc_legacy_const_generics, ..] => {
201201
self.check_rustc_legacy_const_generics(hir_id, attr, span, target, item)
202202
}
203203
[sym::rustc_lint_query_instability, ..] => {
204-
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
204+
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
205205
}
206206
[sym::rustc_lint_untracked_query_information, ..] => {
207-
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
207+
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
208208
}
209209
[sym::rustc_lint_diagnostics, ..] => {
210-
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
210+
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
211211
}
212212
[sym::rustc_lint_opt_ty, ..] => self.check_rustc_lint_opt_ty(attr, span, target),
213213
[sym::rustc_lint_opt_deny_field_access, ..] => {
@@ -1825,15 +1825,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18251825
fn check_applied_to_fn_or_method(
18261826
&self,
18271827
hir_id: HirId,
1828-
attr: &Attribute,
1829-
span: Span,
1828+
attr_span: Span,
1829+
defn_span: Span,
18301830
target: Target,
18311831
) {
18321832
let is_function = matches!(target, Target::Fn | Target::Method(..));
18331833
if !is_function {
18341834
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
1835-
attr_span: attr.span(),
1836-
defn_span: span,
1835+
attr_span,
1836+
defn_span,
18371837
on_crate: hir_id == CRATE_HIR_ID,
18381838
});
18391839
}

0 commit comments

Comments
 (0)