Skip to content

Commit 5172406

Browse files
Port #[no_implicit_prelude] to the new attribute parsing infrastructure
1 parent c65dcca commit 5172406

File tree

7 files changed

+79
-20
lines changed

7 files changed

+79
-20
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ pub enum AttributeKind {
278278
/// Represents `#[naked]`
279279
Naked(Span),
280280

281+
/// Represents `#[no_implicit_prelude]`
282+
NoImplicitPrelude(Span),
283+
281284
/// Represents `#[no_mangle]`
282285
NoMangle(Span),
283286

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl AttributeKind {
3535
MayDangle(..) => No,
3636
MustUse { .. } => Yes,
3737
Naked(..) => No,
38+
NoImplicitPrelude(..) => No,
3839
NoMangle(..) => No,
3940
Optimize(..) => No,
4041
PubTransparent(..) => Yes,

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) mod link_attrs;
3535
pub(crate) mod lint_helpers;
3636
pub(crate) mod loop_match;
3737
pub(crate) mod must_use;
38+
pub(crate) mod no_implicit_prelude;
3839
pub(crate) mod repr;
3940
pub(crate) mod semantics;
4041
pub(crate) mod stability;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_feature::{AttributeTemplate, template};
3+
use rustc_span::sym;
4+
5+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6+
use crate::context::{AcceptContext, Stage};
7+
use crate::parser::ArgParser;
8+
9+
pub(crate) struct NoImplicitPreludeParser;
10+
11+
impl<S: Stage> SingleAttributeParser<S> for NoImplicitPreludeParser {
12+
const PATH: &[rustc_span::Symbol] = &[sym::no_implicit_prelude];
13+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
14+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
15+
const TEMPLATE: AttributeTemplate = template!(Word);
16+
17+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
18+
if let Err(span) = args.no_args() {
19+
cx.expected_no_args(span);
20+
return None;
21+
}
22+
23+
Some(AttributeKind::NoImplicitPrelude(cx.attr_span))
24+
}
25+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::attributes::link_attrs::{LinkNameParser, LinkSectionParser};
2626
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2727
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
29+
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
2930
use crate::attributes::repr::{AlignParser, ReprParser};
3031
use crate::attributes::semantics::MayDangleParser;
3132
use crate::attributes::stability::{
@@ -127,6 +128,7 @@ attribute_parsers!(
127128
Single<LoopMatchParser>,
128129
Single<MayDangleParser>,
129130
Single<MustUseParser>,
131+
Single<NoImplicitPreludeParser>,
130132
Single<NoMangleParser>,
131133
Single<OptimizeParser>,
132134
Single<PubTransparentParser>,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
183183
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
184184
self.check_naked(hir_id, *attr_span, span, target)
185185
}
186+
Attribute::Parsed(AttributeKind::NoImplicitPrelude(attr_span)) => self
187+
.check_generic_attr(
188+
hir_id,
189+
sym::no_implicit_prelude,
190+
*attr_span,
191+
target,
192+
Target::Mod,
193+
),
186194
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
187195
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
188196
}
@@ -292,16 +300,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
292300
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
293301
self.check_macro_use(hir_id, attr, target)
294302
}
295-
[sym::path, ..] => self.check_generic_attr(hir_id, attr, target, Target::Mod),
303+
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
296304
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
297305
[sym::ignore, ..] | [sym::should_panic, ..] => {
298-
self.check_generic_attr(hir_id, attr, target, Target::Fn)
306+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
299307
}
300308
[sym::automatically_derived, ..] => {
301-
self.check_generic_attr(hir_id, attr, target, Target::Impl)
302-
}
303-
[sym::no_implicit_prelude, ..] => {
304-
self.check_generic_attr(hir_id, attr, target, Target::Mod)
309+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
305310
}
306311
[sym::rustc_object_lifetime_default, ..] => self.check_object_lifetime_default(hir_id),
307312
[sym::proc_macro, ..] => {
@@ -311,7 +316,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
311316
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
312317
}
313318
[sym::proc_macro_derive, ..] => {
314-
self.check_generic_attr(hir_id, attr, target, Target::Fn);
319+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn);
315320
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
316321
}
317322
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
@@ -620,7 +625,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
620625
}
621626
}
622627

623-
fn check_generic_attr(
628+
/// FIXME: Remove when all attributes are ported to the new parser
629+
fn check_generic_attr_unparsed(
624630
&self,
625631
hir_id: HirId,
626632
attr: &Attribute,
@@ -643,6 +649,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
643649
}
644650
}
645651

652+
fn check_generic_attr(
653+
&self,
654+
hir_id: HirId,
655+
attr_name: Symbol,
656+
attr_span: Span,
657+
target: Target,
658+
allowed_target: Target,
659+
) {
660+
if target != allowed_target {
661+
self.tcx.emit_node_span_lint(
662+
UNUSED_ATTRIBUTES,
663+
hir_id,
664+
attr_span,
665+
errors::OnlyHasEffectOn {
666+
attr_name: attr_name.to_string(),
667+
target_name: allowed_target.name().replace(' ', "_"),
668+
},
669+
);
670+
}
671+
}
672+
646673
/// Checks if `#[naked]` is applied to a function definition.
647674
fn check_naked(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
648675
match target {

tests/ui/lint/unused/unused-attr-duplicate.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,6 @@ note: attribute also specified here
140140
LL | #![no_std]
141141
| ^^^^^^^^^^
142142

143-
error: unused attribute
144-
--> $DIR/unused-attr-duplicate.rs:25:1
145-
|
146-
LL | #![no_implicit_prelude]
147-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
148-
|
149-
note: attribute also specified here
150-
--> $DIR/unused-attr-duplicate.rs:24:1
151-
|
152-
LL | #![no_implicit_prelude]
153-
| ^^^^^^^^^^^^^^^^^^^^^^^
154-
155143
error: unused attribute
156144
--> $DIR/unused-attr-duplicate.rs:27:1
157145
|
@@ -302,5 +290,17 @@ LL | #[link_section = ".bss"]
302290
| ^^^^^^^^^^^^^^^^^^^^^^^^
303291
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
304292

293+
error: unused attribute
294+
--> $DIR/unused-attr-duplicate.rs:25:1
295+
|
296+
LL | #![no_implicit_prelude]
297+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
298+
|
299+
note: attribute also specified here
300+
--> $DIR/unused-attr-duplicate.rs:24:1
301+
|
302+
LL | #![no_implicit_prelude]
303+
| ^^^^^^^^^^^^^^^^^^^^^^^
304+
305305
error: aborting due to 24 previous errors
306306

0 commit comments

Comments
 (0)