Skip to content

Commit f1840ed

Browse files
committed
Auto merge of #151056 - mejrs:do_not_recommend, r=<try>
Port `diagnostic::do_not_recommend` to new attr parsing
2 parents 4931e09 + ed6e630 commit f1840ed

21 files changed

Lines changed: 93 additions & 76 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use rustc_feature::{AttributeTemplate, template};
2+
use rustc_hir::attrs::AttributeKind;
3+
use rustc_hir::lints::AttributeLintKind;
4+
use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_ATTRIBUTES;
5+
use rustc_span::{Symbol, sym};
6+
7+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
8+
use crate::context::{AcceptContext, Stage};
9+
use crate::parser::ArgParser;
10+
use crate::target_checking::{ALL_TARGETS, AllowedTargets};
11+
12+
pub(crate) struct DoNotRecommendParser;
13+
impl<S: Stage> SingleAttributeParser<S> for DoNotRecommendParser {
14+
const PATH: &[Symbol] = &[sym::diagnostic, sym::do_not_recommend];
15+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
16+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
17+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Checked in check_attr.
18+
const TEMPLATE: AttributeTemplate = template!(Word /*doesn't matter */);
19+
20+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
21+
let attr_span = cx.attr_span;
22+
if !matches!(args, ArgParser::NoArgs) {
23+
cx.emit_lint(
24+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
25+
AttributeLintKind::DoNotRecommendDoesNotExpectArgs,
26+
attr_span,
27+
);
28+
}
29+
Some(AttributeKind::DoNotRecommend { attr_span })
30+
}
31+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub(crate) mod confusables;
3939
pub(crate) mod crate_level;
4040
pub(crate) mod debugger;
4141
pub(crate) mod deprecation;
42+
pub(crate) mod do_not_recommend;
4243
pub(crate) mod doc;
4344
pub(crate) mod dummy;
4445
pub(crate) mod inline;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::attributes::crate_level::{
3434
};
3535
use crate::attributes::debugger::DebuggerViualizerParser;
3636
use crate::attributes::deprecation::DeprecationParser;
37+
use crate::attributes::do_not_recommend::DoNotRecommendParser;
3738
use crate::attributes::doc::DocParser;
3839
use crate::attributes::dummy::DummyParser;
3940
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -201,6 +202,7 @@ attribute_parsers!(
201202
Single<CrateNameParser>,
202203
Single<CustomMirParser>,
203204
Single<DeprecationParser>,
205+
Single<DoNotRecommendParser>,
204206
Single<DummyParser>,
205207
Single<ExportNameParser>,
206208
Single<IgnoreParser>,

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ impl ArgParser {
113113
Some(match value {
114114
AttrArgs::Empty => Self::NoArgs,
115115
AttrArgs::Delimited(args) => {
116-
// The arguments of rustc_dummy are not validated if the arguments are delimited
117-
if parts == &[sym::rustc_dummy] {
116+
// The arguments of rustc_dummy and diagnostic attributes are not validated
117+
// if the arguments are delimited
118+
if parts == &[sym::rustc_dummy] || parts[0] == sym::diagnostic {
118119
return Some(ArgParser::List(MetaItemListParser {
119120
sub_parsers: ThinVec::new(),
120121
span: args.dspan.entire(),

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@ pub enum AttributeKind {
731731
/// Represents `#[rustc_do_not_implement_via_object]`.
732732
DoNotImplementViaObject(Span),
733733

734+
/// Represents `#[diagnostic::do_not_recommend]`.
735+
DoNotRecommend { attr_span: Span },
736+
734737
/// Represents [`#[doc]`](https://doc.rust-lang.org/stable/rustdoc/write-documentation/the-doc-attribute.html).
735738
/// Represents all other uses of the [`#[doc]`](https://doc.rust-lang.org/stable/rustdoc/write-documentation/the-doc-attribute.html)
736739
/// attribute.

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl AttributeKind {
4444
DenyExplicitImpl(..) => No,
4545
Deprecation { .. } => Yes,
4646
DoNotImplementViaObject(..) => No,
47+
DoNotRecommend { .. } => Yes,
4748
Doc(_) => Yes,
4849
DocComment { .. } => Yes,
4950
Dummy => No,

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ lint_improper_ctypes_unsafe_binder = unsafe binders are incompatible with foreig
480480
lint_improper_gpu_kernel_arg = passing type `{$ty}` to a function with "gpu-kernel" ABI may have unexpected behavior
481481
.help = use primitive types and raw pointers to get reliable behavior
482482
483+
lint_incorrect_do_not_recommend_args =
484+
`#[diagnostic::do_not_recommend]` does not expect any arguments
485+
483486
lint_int_to_ptr_transmutes = transmuting an integer to a pointer creates a pointer without provenance
484487
.note = this is dangerous because dereferencing the resulting pointer is undefined behavior
485488
.note_exposed_provenance = exposed provenance semantics can be used to create a pointer based on some previously exposed provenance

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,5 +419,9 @@ pub fn decorate_attribute_lint(
419419
&AttributeLintKind::DocTestLiteral => lints::DocTestLiteral.decorate_lint(diag),
420420

421421
&AttributeLintKind::AttrCrateLevelOnly => lints::AttrCrateLevelOnly.decorate_lint(diag),
422+
423+
&AttributeLintKind::DoNotRecommendDoesNotExpectArgs => {
424+
lints::DoNotRecommendDoesNotExpectArgs.decorate_lint(diag)
425+
}
422426
}
423427
}

compiler/rustc_lint/src/lints.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,3 +3317,7 @@ pub(crate) struct DocTestLiteral;
33173317
#[diag(lint_attr_crate_level)]
33183318
#[note]
33193319
pub(crate) struct AttrCrateLevelOnly;
3320+
3321+
#[derive(LintDiagnostic)]
3322+
#[diag(lint_incorrect_do_not_recommend_args)]
3323+
pub(crate) struct DoNotRecommendDoesNotExpectArgs;

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ pub enum AttributeLintKind {
821821
},
822822
DocTestLiteral,
823823
AttrCrateLevelOnly,
824+
DoNotRecommendDoesNotExpectArgs,
824825
}
825826

826827
pub type RegisteredTools = FxIndexSet<Ident>;

0 commit comments

Comments
 (0)