Skip to content

Commit 31e279f

Browse files
Rollup merge of #150125 - Bryntet:parse_rustc_lint_opt_deny_field_access, r=JonathanBrouwer
Port `#[rustc_lint_opt_deny_field_access]` to attribute parser r? `@JonathanBrouwer`
2 parents d8b64f4 + cd4d899 commit 31e279f

File tree

8 files changed

+37
-39
lines changed

8 files changed

+37
-39
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,35 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLegacyConstGenericsParser {
117117
}
118118
}
119119

120+
pub(crate) struct RustcLintOptDenyFieldAccessParser;
121+
122+
impl<S: Stage> SingleAttributeParser<S> for RustcLintOptDenyFieldAccessParser {
123+
const PATH: &[Symbol] = &[sym::rustc_lint_opt_deny_field_access];
124+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
125+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
126+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Field)]);
127+
const TEMPLATE: AttributeTemplate = template!(Word);
128+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
129+
let Some(arg) = args.list().and_then(MetaItemListParser::single) else {
130+
cx.expected_single_argument(cx.attr_span);
131+
return None;
132+
};
133+
134+
let MetaItemOrLitParser::Lit(MetaItemLit { kind: LitKind::Str(lint_message, _), .. }) = arg
135+
else {
136+
cx.expected_string_literal(arg.span(), arg.lit());
137+
return None;
138+
};
139+
140+
Some(AttributeKind::RustcLintOptDenyFieldAccess { lint_message: *lint_message })
141+
}
142+
}
143+
120144
pub(crate) struct RustcLintOptTyParser;
121145

122146
impl<S: Stage> NoArgsAttributeParser<S> for RustcLintOptTyParser {
123147
const PATH: &[Symbol] = &[sym::rustc_lint_opt_ty];
124-
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
148+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
125149
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
126150
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintOptTy;
127151
}
@@ -130,7 +154,7 @@ pub(crate) struct RustcLintQueryInstabilityParser;
130154

131155
impl<S: Stage> NoArgsAttributeParser<S> for RustcLintQueryInstabilityParser {
132156
const PATH: &[Symbol] = &[sym::rustc_lint_query_instability];
133-
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
157+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
134158
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
135159
Allow(Target::Fn),
136160
Allow(Target::Method(MethodKind::Inherent)),

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ 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-
RustcMainParser, RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
66-
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
64+
RustcLegacyConstGenericsParser, RustcLintOptDenyFieldAccessParser, RustcLintOptTyParser,
65+
RustcLintQueryInstabilityParser, RustcMainParser, RustcNeverReturnsNullPointerParser,
66+
RustcNoImplicitAutorefsParser, RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
6767
RustcSimdMonomorphizeLaneLimitParser,
6868
};
6969
use crate::attributes::semantics::MayDangleParser;
@@ -213,6 +213,7 @@ attribute_parsers!(
213213
Single<RustcLayoutScalarValidRangeEndParser>,
214214
Single<RustcLayoutScalarValidRangeStartParser>,
215215
Single<RustcLegacyConstGenericsParser>,
216+
Single<RustcLintOptDenyFieldAccessParser>,
216217
Single<RustcObjectLifetimeDefaultParser>,
217218
Single<RustcScalableVectorParser>,
218219
Single<RustcSimdMonomorphizeLaneLimitParser>,

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_opt_deny_field_access]`
935+
RustcLintOptDenyFieldAccess { lint_message: Symbol },
936+
934937
/// Represents `#[rustc_lint_opt_ty]`
935938
RustcLintOptTy,
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+
RustcLintOptDenyFieldAccess { .. } => Yes,
9798
RustcLintOptTy => Yes,
9899
RustcLintQueryInstability => Yes,
99100
RustcMain => No,

compiler/rustc_lint/src/internal.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -668,17 +668,12 @@ impl LateLintPass<'_> for BadOptAccess {
668668

669669
for field in adt_def.all_fields() {
670670
if field.name == target.name
671-
&& let Some(attr) =
672-
cx.tcx.get_attr(field.did, sym::rustc_lint_opt_deny_field_access)
673-
&& let Some(items) = attr.meta_item_list()
674-
&& let Some(item) = items.first()
675-
&& let Some(lit) = item.lit()
676-
&& let ast::LitKind::Str(val, _) = lit.kind
671+
&& let Some(lint_message) = find_attr!(cx.tcx.get_all_attrs(field.did), AttributeKind::RustcLintOptDenyFieldAccess { lint_message, } => lint_message)
677672
{
678673
cx.emit_span_lint(
679674
BAD_OPT_ACCESS,
680675
expr.span,
681-
BadOptAccessDiag { msg: val.as_str() },
676+
BadOptAccessDiag { msg: lint_message.as_str() },
682677
);
683678
}
684679
}

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,6 @@ passes_rustc_legacy_const_generics_only =
473473
#[rustc_legacy_const_generics] functions must only have const generics
474474
.label = non-const generic parameter
475475
476-
passes_rustc_lint_opt_deny_field_access =
477-
`#[rustc_lint_opt_deny_field_access]` should be applied to a field
478-
.label = not a field
479476
480477
passes_rustc_pub_transparent =
481478
attribute should be applied to `#[repr(transparent)]` types

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 15 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::RustcLintOptDenyFieldAccess { .. }
261262
| AttributeKind::RustcLintOptTy
262263
| AttributeKind::RustcLintQueryInstability
263264
| AttributeKind::RustcNeverReturnsNullPointer
@@ -314,9 +315,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
314315
[sym::rustc_lint_diagnostics, ..] => {
315316
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
316317
}
317-
[sym::rustc_lint_opt_deny_field_access, ..] => {
318-
self.check_rustc_lint_opt_deny_field_access(attr, span, target)
319-
}
320318
[sym::rustc_clean, ..]
321319
| [sym::rustc_dirty, ..]
322320
| [sym::rustc_if_this_changed, ..]
@@ -1251,18 +1249,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12511249
}
12521250
}
12531251

1254-
/// Checks that the `#[rustc_lint_opt_deny_field_access]` attribute is only applied to a field.
1255-
fn check_rustc_lint_opt_deny_field_access(&self, attr: &Attribute, span: Span, target: Target) {
1256-
match target {
1257-
Target::Field => {}
1258-
_ => {
1259-
self.tcx
1260-
.dcx()
1261-
.emit_err(errors::RustcLintOptDenyFieldAccess { attr_span: attr.span(), span });
1262-
}
1263-
}
1264-
}
1265-
12661252
/// Checks that the dep-graph debugging attributes are only present when the query-dep-graph
12671253
/// option is passed to the compiler.
12681254
fn check_rustc_dirty_clean(&self, attr: &Attribute) {

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,6 @@ pub(crate) struct UnusedMultiple {
412412
pub name: Symbol,
413413
}
414414

415-
#[derive(Diagnostic)]
416-
#[diag(passes_rustc_lint_opt_deny_field_access)]
417-
pub(crate) struct RustcLintOptDenyFieldAccess {
418-
#[primary_span]
419-
pub attr_span: Span,
420-
#[label]
421-
pub span: Span,
422-
}
423-
424415
#[derive(Diagnostic)]
425416
#[diag(passes_collapse_debuginfo)]
426417
pub(crate) struct CollapseDebuginfo {

0 commit comments

Comments
 (0)