Skip to content

Commit e27211a

Browse files
Port #[no_implicit_prelude] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 25face9 commit e27211a

File tree

9 files changed

+77
-26
lines changed

9 files changed

+77
-26
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 rustc_internal;
4041
pub(crate) mod semantics;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_span::{Span, sym};
3+
4+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
5+
use crate::context::Stage;
6+
7+
pub(crate) struct NoImplicitPreludeParser;
8+
9+
impl<S: Stage> NoArgsAttributeParser<S> for NoImplicitPreludeParser {
10+
const PATH: &[rustc_span::Symbol] = &[sym::no_implicit_prelude];
11+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
12+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoImplicitPrelude;
13+
}

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::rustc_internal::{
3132
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
@@ -140,6 +141,7 @@ attribute_parsers!(
140141
Single<WithoutArgs<ConstStabilityIndirectParser>>,
141142
Single<WithoutArgs<LoopMatchParser>>,
142143
Single<WithoutArgs<MayDangleParser>>,
144+
Single<WithoutArgs<NoImplicitPreludeParser>>,
143145
Single<WithoutArgs<NoMangleParser>>,
144146
Single<WithoutArgs<PubTransparentParser>>,
145147
Single<WithoutArgs<TrackCallerParser>>,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ fn emit_malformed_attribute(
308308
| sym::link_section
309309
| sym::rustc_layout_scalar_valid_range_start
310310
| sym::rustc_layout_scalar_valid_range_end
311+
| sym::no_implicit_prelude
311312
) {
312313
return;
313314
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
185185
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
186186
self.check_naked(hir_id, *attr_span, span, target)
187187
}
188+
Attribute::Parsed(AttributeKind::NoImplicitPrelude(attr_span)) => self
189+
.check_generic_attr(
190+
hir_id,
191+
sym::no_implicit_prelude,
192+
*attr_span,
193+
target,
194+
Target::Mod,
195+
),
188196
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
189197
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
190198
}
@@ -294,16 +302,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
294302
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
295303
self.check_macro_use(hir_id, attr, target)
296304
}
297-
[sym::path, ..] => self.check_generic_attr(hir_id, attr, target, Target::Mod),
305+
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
298306
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
299307
[sym::ignore, ..] | [sym::should_panic, ..] => {
300-
self.check_generic_attr(hir_id, attr, target, Target::Fn)
308+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
301309
}
302310
[sym::automatically_derived, ..] => {
303-
self.check_generic_attr(hir_id, attr, target, Target::Impl)
304-
}
305-
[sym::no_implicit_prelude, ..] => {
306-
self.check_generic_attr(hir_id, attr, target, Target::Mod)
311+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
307312
}
308313
[sym::proc_macro, ..] => {
309314
self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
@@ -312,7 +317,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
312317
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
313318
}
314319
[sym::proc_macro_derive, ..] => {
315-
self.check_generic_attr(hir_id, attr, target, Target::Fn);
320+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn);
316321
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
317322
}
318323
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
@@ -621,7 +626,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
621626
}
622627
}
623628

624-
fn check_generic_attr(
629+
/// FIXME: Remove when all attributes are ported to the new parser
630+
fn check_generic_attr_unparsed(
625631
&self,
626632
hir_id: HirId,
627633
attr: &Attribute,
@@ -644,6 +650,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
644650
}
645651
}
646652

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

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ error: malformed `no_sanitize` attribute input
7171
LL | #[no_sanitize]
7272
| ^^^^^^^^^^^^^^ help: must be of the form: `#[no_sanitize(address, kcfi, memory, thread)]`
7373

74-
error: malformed `no_implicit_prelude` attribute input
75-
--> $DIR/malformed-attrs.rs:95:1
76-
|
77-
LL | #[no_implicit_prelude = 23]
78-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[no_implicit_prelude]`
79-
8074
error: malformed `proc_macro` attribute input
8175
--> $DIR/malformed-attrs.rs:97:1
8276
|
@@ -511,6 +505,15 @@ error[E0539]: malformed `link_section` attribute input
511505
LL | #[link_section]
512506
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_section = "name"]`
513507

508+
error[E0565]: malformed `no_implicit_prelude` attribute input
509+
--> $DIR/malformed-attrs.rs:95:1
510+
|
511+
LL | #[no_implicit_prelude = 23]
512+
| ^^^^^^^^^^^^^^^^^^^^^^----^
513+
| | |
514+
| | didn't expect any arguments here
515+
| help: must be of the form: `#[no_implicit_prelude]`
516+
514517
error[E0539]: malformed `must_use` attribute input
515518
--> $DIR/malformed-attrs.rs:117:1
516519
|

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)