Skip to content

Port #[no_implicit_prelude] to the new attribute parsing infrastructure #143237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_attr_data_structures/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ pub enum AttributeKind {
/// Represents `#[naked]`
Naked(Span),

/// Represents `#[no_implicit_prelude]`
NoImplicitPrelude(Span),

/// Represents `#[no_mangle]`
NoMangle(Span),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl AttributeKind {
MayDangle(..) => No,
MustUse { .. } => Yes,
Naked(..) => No,
NoImplicitPrelude(..) => No,
NoMangle(..) => No,
Optimize(..) => No,
PubTransparent(..) => Yes,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) mod link_attrs;
pub(crate) mod lint_helpers;
pub(crate) mod loop_match;
pub(crate) mod must_use;
pub(crate) mod no_implicit_prelude;
pub(crate) mod repr;
pub(crate) mod rustc_internal;
pub(crate) mod semantics;
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/no_implicit_prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use rustc_attr_data_structures::AttributeKind;
use rustc_span::{Span, sym};

use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
use crate::context::Stage;

pub(crate) struct NoImplicitPreludeParser;

impl<S: Stage> NoArgsAttributeParser<S> for NoImplicitPreludeParser {
const PATH: &[rustc_span::Symbol] = &[sym::no_implicit_prelude];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoImplicitPrelude;
}
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::attributes::link_attrs::{LinkNameParser, LinkSectionParser};
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
use crate::attributes::must_use::MustUseParser;
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
use crate::attributes::repr::{AlignParser, ReprParser};
use crate::attributes::rustc_internal::{
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
Expand Down Expand Up @@ -141,6 +142,7 @@ attribute_parsers!(
Single<WithoutArgs<ConstStabilityIndirectParser>>,
Single<WithoutArgs<LoopMatchParser>>,
Single<WithoutArgs<MayDangleParser>>,
Single<WithoutArgs<NoImplicitPreludeParser>>,
Single<WithoutArgs<NoMangleParser>>,
Single<WithoutArgs<PubTransparentParser>>,
Single<WithoutArgs<TrackCallerParser>>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/validate_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ fn emit_malformed_attribute(
| sym::link_section
| sym::rustc_layout_scalar_valid_range_start
| sym::rustc_layout_scalar_valid_range_end
| sym::no_implicit_prelude
) {
return;
}
Expand Down
43 changes: 35 additions & 8 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
Attribute::Parsed(AttributeKind::Naked(attr_span)) => {
self.check_naked(hir_id, *attr_span, span, target)
}
Attribute::Parsed(AttributeKind::NoImplicitPrelude(attr_span)) => self
.check_generic_attr(
hir_id,
sym::no_implicit_prelude,
*attr_span,
target,
Target::Mod,
),
Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
self.check_track_caller(hir_id, *attr_span, attrs, span, target)
}
Expand Down Expand Up @@ -289,16 +297,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::macro_use, ..] | [sym::macro_escape, ..] => {
self.check_macro_use(hir_id, attr, target)
}
[sym::path, ..] => self.check_generic_attr(hir_id, attr, target, Target::Mod),
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
[sym::ignore, ..] | [sym::should_panic, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Fn)
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
}
[sym::automatically_derived, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Impl)
}
[sym::no_implicit_prelude, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Mod)
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
}
[sym::proc_macro, ..] => {
self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
Expand All @@ -307,7 +312,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
}
[sym::proc_macro_derive, ..] => {
self.check_generic_attr(hir_id, attr, target, Target::Fn);
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn);
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
}
[sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
Expand Down Expand Up @@ -616,7 +621,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}

fn check_generic_attr(
/// FIXME: Remove when all attributes are ported to the new parser
fn check_generic_attr_unparsed(
&self,
hir_id: HirId,
attr: &Attribute,
Expand All @@ -639,6 +645,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}

fn check_generic_attr(
&self,
hir_id: HirId,
attr_name: Symbol,
attr_span: Span,
target: Target,
allowed_target: Target,
) {
if target != allowed_target {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr_span,
errors::OnlyHasEffectOn {
attr_name: attr_name.to_string(),
target_name: allowed_target.name().replace(' ', "_"),
},
);
}
}

/// Checks if `#[naked]` is applied to a function definition.
fn check_naked(&self, hir_id: HirId, attr_span: Span, span: Span, target: Target) {
match target {
Expand Down
15 changes: 9 additions & 6 deletions tests/ui/attributes/malformed-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ error: malformed `no_sanitize` attribute input
LL | #[no_sanitize]
| ^^^^^^^^^^^^^^ help: must be of the form: `#[no_sanitize(address, kcfi, memory, thread)]`

error: malformed `no_implicit_prelude` attribute input
--> $DIR/malformed-attrs.rs:96:1
|
LL | #[no_implicit_prelude = 23]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[no_implicit_prelude]`

error: malformed `proc_macro` attribute input
--> $DIR/malformed-attrs.rs:98:1
|
Expand Down Expand Up @@ -514,6 +508,15 @@ error[E0539]: malformed `link_section` attribute input
LL | #[link_section]
| ^^^^^^^^^^^^^^^ help: must be of the form: `#[link_section = "name"]`

error[E0565]: malformed `no_implicit_prelude` attribute input
--> $DIR/malformed-attrs.rs:96:1
|
LL | #[no_implicit_prelude = 23]
| ^^^^^^^^^^^^^^^^^^^^^^----^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[no_implicit_prelude]`

error[E0539]: malformed `must_use` attribute input
--> $DIR/malformed-attrs.rs:118:1
|
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/lint/unused/unused-attr-duplicate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,6 @@ note: attribute also specified here
LL | #![no_std]
| ^^^^^^^^^^

error: unused attribute
--> $DIR/unused-attr-duplicate.rs:25:1
|
LL | #![no_implicit_prelude]
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
note: attribute also specified here
--> $DIR/unused-attr-duplicate.rs:24:1
|
LL | #![no_implicit_prelude]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: unused attribute
--> $DIR/unused-attr-duplicate.rs:27:1
|
Expand Down Expand Up @@ -302,5 +290,17 @@ LL | #[link_section = ".bss"]
| ^^^^^^^^^^^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

error: unused attribute
--> $DIR/unused-attr-duplicate.rs:25:1
|
LL | #![no_implicit_prelude]
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
note: attribute also specified here
--> $DIR/unused-attr-duplicate.rs:24:1
|
LL | #![no_implicit_prelude]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 24 previous errors

Loading