Skip to content

Commit 1b61d43

Browse files
committed
Auto merge of #143237 - JonathanBrouwer:no_implicit_prelude_parser, r=jdonszelmann,oli-obk
Port `#[no_implicit_prelude]` to the new attribute parsing infrastructure Ports no_implicit_prelude to the new attribute parsing infrastructure for #131229 (comment) r? `@oli-obk` cc `@jdonszelmann`
2 parents c96a690 + fee5e3c commit 1b61d43

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,
@@ -141,6 +142,7 @@ attribute_parsers!(
141142
Single<WithoutArgs<ConstStabilityIndirectParser>>,
142143
Single<WithoutArgs<LoopMatchParser>>,
143144
Single<WithoutArgs<MayDangleParser>>,
145+
Single<WithoutArgs<NoImplicitPreludeParser>>,
144146
Single<WithoutArgs<NoMangleParser>>,
145147
Single<WithoutArgs<PubTransparentParser>>,
146148
Single<WithoutArgs<TrackCallerParser>>,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ fn emit_malformed_attribute(
310310
| sym::link_section
311311
| sym::rustc_layout_scalar_valid_range_start
312312
| sym::rustc_layout_scalar_valid_range_end
313+
| sym::no_implicit_prelude
313314
) {
314315
return;
315316
}

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
}
@@ -289,16 +297,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
289297
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
290298
self.check_macro_use(hir_id, attr, target)
291299
}
292-
[sym::path, ..] => self.check_generic_attr(hir_id, attr, target, Target::Mod),
300+
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
293301
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
294302
[sym::ignore, ..] | [sym::should_panic, ..] => {
295-
self.check_generic_attr(hir_id, attr, target, Target::Fn)
303+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
296304
}
297305
[sym::automatically_derived, ..] => {
298-
self.check_generic_attr(hir_id, attr, target, Target::Impl)
299-
}
300-
[sym::no_implicit_prelude, ..] => {
301-
self.check_generic_attr(hir_id, attr, target, Target::Mod)
306+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
302307
}
303308
[sym::proc_macro, ..] => {
304309
self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
@@ -307,7 +312,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
307312
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
308313
}
309314
[sym::proc_macro_derive, ..] => {
310-
self.check_generic_attr(hir_id, attr, target, Target::Fn);
315+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn);
311316
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
312317
}
313318
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
@@ -616,7 +621,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
616621
}
617622
}
618623

619-
fn check_generic_attr(
624+
/// FIXME: Remove when all attributes are ported to the new parser
625+
fn check_generic_attr_unparsed(
620626
&self,
621627
hir_id: HirId,
622628
attr: &Attribute,
@@ -639,6 +645,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
639645
}
640646
}
641647

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

tests/ui/attributes/malformed-attrs.stderr

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

68-
error: malformed `no_implicit_prelude` attribute input
69-
--> $DIR/malformed-attrs.rs:96:1
70-
|
71-
LL | #[no_implicit_prelude = 23]
72-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[no_implicit_prelude]`
73-
7468
error: malformed `proc_macro` attribute input
7569
--> $DIR/malformed-attrs.rs:98:1
7670
|
@@ -514,6 +508,15 @@ error[E0539]: malformed `link_section` attribute input
514508
LL | #[link_section]
515509
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_section = "name"]`
516510

511+
error[E0565]: malformed `no_implicit_prelude` attribute input
512+
--> $DIR/malformed-attrs.rs:96:1
513+
|
514+
LL | #[no_implicit_prelude = 23]
515+
| ^^^^^^^^^^^^^^^^^^^^^^----^
516+
| | |
517+
| | didn't expect any arguments here
518+
| help: must be of the form: `#[no_implicit_prelude]`
519+
517520
error[E0539]: malformed `must_use` attribute input
518521
--> $DIR/malformed-attrs.rs:118:1
519522
|

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)