Skip to content

Commit 44cfed7

Browse files
committed
Port #[rustc_lint_diagnostics] to attribute parser
1 parent 5277241 commit 44cfed7

File tree

10 files changed

+42
-57
lines changed

10 files changed

+42
-57
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLegacyConstGenericsParser {
117117
}
118118
}
119119

120+
pub(crate) struct RustcLintDiagnosticsParser;
121+
122+
impl<S: Stage> NoArgsAttributeParser<S> for RustcLintDiagnosticsParser {
123+
const PATH: &[Symbol] = &[sym::rustc_lint_diagnostics];
124+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
125+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
126+
Allow(Target::Fn),
127+
Allow(Target::Method(MethodKind::Inherent)),
128+
Allow(Target::Method(MethodKind::Trait { body: false })),
129+
Allow(Target::Method(MethodKind::Trait { body: true })),
130+
Allow(Target::Method(MethodKind::TraitImpl)),
131+
]);
132+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintDiagnostics;
133+
}
134+
120135
pub(crate) struct RustcLintOptDenyFieldAccessParser;
121136

122137
impl<S: Stage> SingleAttributeParser<S> for RustcLintOptDenyFieldAccessParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ use crate::attributes::prototype::CustomMirParser;
6161
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6262
use crate::attributes::rustc_internal::{
6363
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
64-
RustcLegacyConstGenericsParser, RustcLintOptDenyFieldAccessParser, RustcLintOptTyParser,
65-
RustcLintQueryInstabilityParser, RustcLintUntrackedQueryInformationParser, RustcMainParser,
66-
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
67-
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
64+
RustcLegacyConstGenericsParser, RustcLintDiagnosticsParser, RustcLintOptDenyFieldAccessParser,
65+
RustcLintOptTyParser, RustcLintQueryInstabilityParser,
66+
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcNeverReturnsNullPointerParser,
67+
RustcNoImplicitAutorefsParser, RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
6868
RustcSimdMonomorphizeLaneLimitParser,
6969
};
7070
use crate::attributes::semantics::MayDangleParser;
@@ -258,6 +258,7 @@ attribute_parsers!(
258258
Single<WithoutArgs<ProcMacroParser>>,
259259
Single<WithoutArgs<PubTransparentParser>>,
260260
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
261+
Single<WithoutArgs<RustcLintDiagnosticsParser>>,
261262
Single<WithoutArgs<RustcLintOptTyParser>>,
262263
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
263264
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,9 @@ pub enum AttributeKind {
931931
/// Represents `#[rustc_legacy_const_generics]`
932932
RustcLegacyConstGenerics { fn_indexes: ThinVec<(usize, Span)>, attr_span: Span },
933933

934+
/// Represents `#[rustc_lint_diagnostics]`
935+
RustcLintDiagnostics,
936+
934937
/// Represents `#[rustc_lint_opt_deny_field_access]`
935938
RustcLintOptDenyFieldAccess { lint_message: Symbol },
936939

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl AttributeKind {
9494
RustcLayoutScalarValidRangeEnd(..) => Yes,
9595
RustcLayoutScalarValidRangeStart(..) => Yes,
9696
RustcLegacyConstGenerics { .. } => Yes,
97+
RustcLintDiagnostics => Yes,
9798
RustcLintOptDenyFieldAccess { .. } => Yes,
9899
RustcLintOptTy => Yes,
99100
RustcLintQueryInstability => Yes,

compiler/rustc_lint/src/internal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,14 @@ impl Diagnostics {
609609
else {
610610
return;
611611
};
612-
let has_attr = cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics);
613-
if !has_attr {
612+
613+
if !find_attr!(cx.tcx.get_all_attrs(inst.def_id()), AttributeKind::RustcLintDiagnostics) {
614614
return;
615615
};
616616

617617
for (hir_id, _parent) in cx.tcx.hir_parent_iter(current_id) {
618618
if let Some(owner_did) = hir_id.as_owner()
619-
&& cx.tcx.has_attr(owner_did, sym::rustc_lint_diagnostics)
619+
&& find_attr!(cx.tcx.get_all_attrs(owner_did), AttributeKind::RustcLintDiagnostics)
620620
{
621621
// The parent method is marked with `#[rustc_lint_diagnostics]`
622622
return;

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,6 @@ passes_sanitize_attribute_not_allowed =
484484
.no_body = function has no body
485485
.help = sanitize attribute can be applied to a function (with body), impl block, or module
486486
487-
passes_should_be_applied_to_fn =
488-
attribute should be applied to a function definition
489-
.label = {$on_crate ->
490-
[true] cannot be applied to crates
491-
*[false] not a function definition
492-
}
493-
494487
passes_should_be_applied_to_static =
495488
attribute should be applied to a static
496489
.label = not a static

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
258258
| AttributeKind::RustcNoImplicitAutorefs
259259
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
260260
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
261+
| AttributeKind::RustcLintDiagnostics
261262
| AttributeKind::RustcLintOptDenyFieldAccess { .. }
262263
| AttributeKind::RustcLintOptTy
263264
| AttributeKind::RustcLintQueryInstability
@@ -310,9 +311,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
310311
self.check_diagnostic_on_const(attr.span(), hir_id, target, item)
311312
}
312313
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
313-
[sym::rustc_lint_diagnostics, ..] => {
314-
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
315-
}
316314
[sym::rustc_clean, ..]
317315
| [sym::rustc_dirty, ..]
318316
| [sym::rustc_if_this_changed, ..]
@@ -1228,25 +1226,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12281226
}
12291227
}
12301228

1231-
/// Helper function for checking that the provided attribute is only applied to a function or
1232-
/// method.
1233-
fn check_applied_to_fn_or_method(
1234-
&self,
1235-
hir_id: HirId,
1236-
attr_span: Span,
1237-
defn_span: Span,
1238-
target: Target,
1239-
) {
1240-
let is_function = matches!(target, Target::Fn | Target::Method(..));
1241-
if !is_function {
1242-
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
1243-
attr_span,
1244-
defn_span,
1245-
on_crate: hir_id == CRATE_HIR_ID,
1246-
});
1247-
}
1248-
}
1249-
12501229
/// Checks that the dep-graph debugging attributes are only present when the query-dep-graph
12511230
/// option is passed to the compiler.
12521231
fn check_rustc_dirty_clean(&self, attr: &Attribute) {

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@ pub(crate) struct OuterCrateLevelAttrSuggestion {
8080
#[diag(passes_inner_crate_level_attr)]
8181
pub(crate) struct InnerCrateLevelAttr;
8282

83-
#[derive(Diagnostic)]
84-
#[diag(passes_should_be_applied_to_fn)]
85-
pub(crate) struct AttrShouldBeAppliedToFn {
86-
#[primary_span]
87-
pub attr_span: Span,
88-
#[label]
89-
pub defn_span: Span,
90-
pub on_crate: bool,
91-
}
92-
9383
#[derive(Diagnostic)]
9484
#[diag(passes_non_exhaustive_with_default_field_values)]
9585
pub(crate) struct NonExhaustiveWithDefaultFieldValues {

tests/ui/internal-lints/diagnostics_incorrect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![feature(rustc_attrs)]
44

55
#[rustc_lint_diagnostics]
6-
//~^ ERROR attribute should be applied to a function
6+
//~^ ERROR `#[rustc_lint_diagnostics]` attribute cannot be used on structs
77
struct Foo;
88

99
impl Foo {
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
error: malformed `rustc_lint_diagnostics` attribute input
2-
--> $DIR/diagnostics_incorrect.rs:10:5
3-
|
4-
LL | #[rustc_lint_diagnostics(a)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_lint_diagnostics]`
6-
7-
error: attribute should be applied to a function definition
1+
error: `#[rustc_lint_diagnostics]` attribute cannot be used on structs
82
--> $DIR/diagnostics_incorrect.rs:5:1
93
|
104
LL | #[rustc_lint_diagnostics]
115
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12-
LL |
13-
LL | struct Foo;
14-
| ----------- not a function definition
6+
|
7+
= help: `#[rustc_lint_diagnostics]` can only be applied to functions
8+
9+
error[E0565]: malformed `rustc_lint_diagnostics` attribute input
10+
--> $DIR/diagnostics_incorrect.rs:10:5
11+
|
12+
LL | #[rustc_lint_diagnostics(a)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^---^
14+
| | |
15+
| | didn't expect any arguments here
16+
| help: must be of the form: `#[rustc_lint_diagnostics]`
1517

1618
error: aborting due to 2 previous errors
1719

20+
For more information about this error, try `rustc --explain E0565`.

0 commit comments

Comments
 (0)