Skip to content

Commit d4c308d

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

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
@@ -275,6 +275,9 @@ pub enum AttributeKind {
275275
/// Represents `#[naked]`
276276
Naked(Span),
277277

278+
/// Represents `#[no_implicit_prelude]`
279+
NoImplicitPrelude(Span),
280+
278281
/// Represents `#[no_mangle]`
279282
NoMangle(Span),
280283

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl AttributeKind {
3434
MayDangle(..) => No,
3535
MustUse { .. } => Yes,
3636
Naked(..) => No,
37+
NoImplicitPrelude(..) => No,
3738
NoMangle(..) => No,
3839
Optimize(..) => No,
3940
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;
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::{
@@ -126,6 +127,7 @@ attribute_parsers!(
126127
Single<LoopMatchParser>,
127128
Single<MayDangleParser>,
128129
Single<MustUseParser>,
130+
Single<NoImplicitPreludeParser>,
129131
Single<NoMangleParser>,
130132
Single<OptimizeParser>,
131133
Single<PubTransparentParser>,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
180180
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
181181
self.check_naked(hir_id, *attr_span, span, target)
182182
}
183+
Attribute::Parsed(AttributeKind::NoImplicitPrelude(attr_span)) => self
184+
.check_generic_attr(
185+
hir_id,
186+
sym::no_implicit_prelude,
187+
*attr_span,
188+
target,
189+
Target::Mod,
190+
),
183191
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
184192
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
185193
}
@@ -290,16 +298,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
290298
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
291299
self.check_macro_use(hir_id, attr, target)
292300
}
293-
[sym::path, ..] => self.check_generic_attr(hir_id, attr, target, Target::Mod),
301+
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
294302
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
295303
[sym::ignore, ..] | [sym::should_panic, ..] => {
296-
self.check_generic_attr(hir_id, attr, target, Target::Fn)
304+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
297305
}
298306
[sym::automatically_derived, ..] => {
299-
self.check_generic_attr(hir_id, attr, target, Target::Impl)
300-
}
301-
[sym::no_implicit_prelude, ..] => {
302-
self.check_generic_attr(hir_id, attr, target, Target::Mod)
307+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
303308
}
304309
[sym::rustc_object_lifetime_default, ..] => self.check_object_lifetime_default(hir_id),
305310
[sym::proc_macro, ..] => {
@@ -309,7 +314,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
309314
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
310315
}
311316
[sym::proc_macro_derive, ..] => {
312-
self.check_generic_attr(hir_id, attr, target, Target::Fn);
317+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn);
313318
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
314319
}
315320
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
@@ -618,7 +623,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
618623
}
619624
}
620625

621-
fn check_generic_attr(
626+
/// FIXME: Remove when all attributes are ported to the new parser
627+
fn check_generic_attr_unparsed(
622628
&self,
623629
hir_id: HirId,
624630
attr: &Attribute,
@@ -641,6 +647,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
641647
}
642648
}
643649

650+
fn check_generic_attr(
651+
&self,
652+
hir_id: HirId,
653+
attr_name: Symbol,
654+
attr_span: Span,
655+
target: Target,
656+
allowed_target: Target,
657+
) {
658+
if target != allowed_target {
659+
self.tcx.emit_node_span_lint(
660+
UNUSED_ATTRIBUTES,
661+
hir_id,
662+
attr_span,
663+
errors::OnlyHasEffectOn {
664+
attr_name: attr_name.to_string(),
665+
target_name: allowed_target.name().replace(' ', "_"),
666+
},
667+
);
668+
}
669+
}
670+
644671
/// Checks if `#[naked]` is applied to a function definition.
645672
fn check_naked(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
646673
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
|
@@ -289,5 +277,17 @@ note: attribute also specified here
289277
LL | #[used]
290278
| ^^^^^^^
291279

280+
error: unused attribute
281+
--> $DIR/unused-attr-duplicate.rs:25:1
282+
|
283+
LL | #![no_implicit_prelude]
284+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
285+
|
286+
note: attribute also specified here
287+
--> $DIR/unused-attr-duplicate.rs:24:1
288+
|
289+
LL | #![no_implicit_prelude]
290+
| ^^^^^^^^^^^^^^^^^^^^^^^
291+
292292
error: aborting due to 23 previous errors
293293

0 commit comments

Comments
 (0)