Skip to content

Commit fbaf07c

Browse files
committed
Port #[rustc_lint_diagnostics] to attribute parser
1 parent 8383cca commit fbaf07c

File tree

9 files changed

+39
-56
lines changed

9 files changed

+39
-56
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 RustcLintOptTyParser;
121136

122137
impl<S: Stage> NoArgsAttributeParser<S> for RustcLintOptTyParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +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, RustcLintOptTyParser, RustcLintQueryInstabilityParser,
65-
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcNeverReturnsNullPointerParser,
66-
RustcNoImplicitAutorefsParser, RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
64+
RustcLegacyConstGenericsParser, RustcLintDiagnosticsParser, RustcLintOptTyParser,
65+
RustcLintQueryInstabilityParser, RustcLintUntrackedQueryInformationParser, RustcMainParser,
66+
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
67+
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
6768
RustcSimdMonomorphizeLaneLimitParser,
6869
};
6970
use crate::attributes::semantics::MayDangleParser;
@@ -256,6 +257,7 @@ attribute_parsers!(
256257
Single<WithoutArgs<ProcMacroParser>>,
257258
Single<WithoutArgs<PubTransparentParser>>,
258259
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
260+
Single<WithoutArgs<RustcLintDiagnosticsParser>>,
259261
Single<WithoutArgs<RustcLintOptTyParser>>,
260262
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
261263
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,

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
RustcLintOptTy => Yes,
9899
RustcLintQueryInstability => Yes,
99100
RustcLintUntrackedQueryInformation => 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
@@ -487,13 +487,6 @@ passes_sanitize_attribute_not_allowed =
487487
.no_body = function has no body
488488
.help = sanitize attribute can be applied to a function (with body), impl block, or module
489489
490-
passes_should_be_applied_to_fn =
491-
attribute should be applied to a function definition
492-
.label = {$on_crate ->
493-
[true] cannot be applied to crates
494-
*[false] not a function definition
495-
}
496-
497490
passes_should_be_applied_to_static =
498491
attribute should be applied to a static
499492
.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::RustcLintOptTy
262263
| AttributeKind::RustcLintQueryInstability
263264
| AttributeKind::RustcLintUntrackedQueryInformation
@@ -309,9 +310,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
309310
self.check_diagnostic_on_const(attr.span(), hir_id, target, item)
310311
}
311312
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
312-
[sym::rustc_lint_diagnostics, ..] => {
313-
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
314-
}
315313
[sym::rustc_lint_opt_deny_field_access, ..] => {
316314
self.check_rustc_lint_opt_deny_field_access(attr, span, target)
317315
}
@@ -1230,25 +1228,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12301228
}
12311229
}
12321230

1233-
/// Helper function for checking that the provided attribute is only applied to a function or
1234-
/// method.
1235-
fn check_applied_to_fn_or_method(
1236-
&self,
1237-
hir_id: HirId,
1238-
attr_span: Span,
1239-
defn_span: Span,
1240-
target: Target,
1241-
) {
1242-
let is_function = matches!(target, Target::Fn | Target::Method(..));
1243-
if !is_function {
1244-
self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
1245-
attr_span,
1246-
defn_span,
1247-
on_crate: hir_id == CRATE_HIR_ID,
1248-
});
1249-
}
1250-
}
1251-
12521231
/// Checks that the `#[rustc_lint_opt_deny_field_access]` attribute is only applied to a field.
12531232
fn check_rustc_lint_opt_deny_field_access(&self, attr: &Attribute, span: Span, target: Target) {
12541233
match target {

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)