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 https://github.com/rust-lang/rust/issues/131229#issuecomment-2971353197

r? `@oli-obk`
cc `@jdonszelmann`
This commit is contained in:
bors
2025-07-04 07:21:46 +00:00
9 changed files with 77 additions and 26 deletions
@@ -278,6 +278,9 @@ pub enum AttributeKind {
/// Represents `#[naked]`
Naked(Span),
/// Represents `#[no_implicit_prelude]`
NoImplicitPrelude(Span),
/// Represents `#[no_mangle]`
NoMangle(Span),
@@ -35,6 +35,7 @@ pub fn encode_cross_crate(&self) -> EncodeCrossCrate {
MayDangle(..) => No,
MustUse { .. } => Yes,
Naked(..) => No,
NoImplicitPrelude(..) => No,
NoMangle(..) => No,
Optimize(..) => No,
PubTransparent(..) => Yes,
@@ -35,6 +35,7 @@
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;
@@ -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;
}
@@ -26,6 +26,7 @@
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,
@@ -141,6 +142,7 @@ mod late {
Single<WithoutArgs<ConstStabilityIndirectParser>>,
Single<WithoutArgs<LoopMatchParser>>,
Single<WithoutArgs<MayDangleParser>>,
Single<WithoutArgs<NoImplicitPreludeParser>>,
Single<WithoutArgs<NoMangleParser>>,
Single<WithoutArgs<PubTransparentParser>>,
Single<WithoutArgs<TrackCallerParser>>,
@@ -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;
}
+35 -8
View File
@@ -183,6 +183,14 @@ fn check_attributes(
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)
}
@@ -289,16 +297,13 @@ fn check_attributes(
[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)
@@ -307,7 +312,7 @@ fn check_attributes(
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, ..] => {
@@ -616,7 +621,8 @@ fn check_no_sanitize(&self, attr: &Attribute, span: Span, target: Target) {
}
}
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,
@@ -639,6 +645,27 @@ fn check_generic_attr(
}
}
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 {
+9 -6
View File
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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