From 8526aa53613bf82bc145bb02161933fb336643e3 Mon Sep 17 00:00:00 2001 From: Oscar Bray Date: Thu, 12 Feb 2026 17:43:04 +0000 Subject: [PATCH 1/2] Port #[prelude_import] to the attribute parser --- .../rustc_attr_parsing/src/attributes/rustc_internal.rs | 9 +++++++++ compiler/rustc_attr_parsing/src/context.rs | 1 + compiler/rustc_hir/src/attrs/data_structures.rs | 3 +++ compiler/rustc_hir/src/attrs/encode_cross_crate.rs | 1 + compiler/rustc_passes/src/check_attr.rs | 2 +- 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs index 0d19dc25d402c..f1b31365013ec 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs @@ -1211,3 +1211,12 @@ impl SingleAttributeParser for RustcReservationImplParser { Some(AttributeKind::RustcReservationImpl(cx.attr_span, value_str)) } } + +pub(crate) struct PreludeImportParser; + +impl NoArgsAttributeParser for PreludeImportParser { + const PATH: &[Symbol] = &[sym::prelude_import]; + const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn; + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Use)]); + const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::PreludeImport; +} diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 87aa4150becd3..c6f0914bfbdaf 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -258,6 +258,7 @@ attribute_parsers!( Single>, Single>, Single>, + Single>, Single>, Single>, Single>, diff --git a/compiler/rustc_hir/src/attrs/data_structures.rs b/compiler/rustc_hir/src/attrs/data_structures.rs index 8e68a3d002334..e28ecd06b89bc 100644 --- a/compiler/rustc_hir/src/attrs/data_structures.rs +++ b/compiler/rustc_hir/src/attrs/data_structures.rs @@ -1053,6 +1053,9 @@ pub enum AttributeKind { /// Represents `#[pointee]` Pointee(Span), + /// Represents `#[prelude_import]` + PreludeImport, + /// Represents `#[proc_macro]` ProcMacro(Span), diff --git a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs index e68ab1c42bafd..b4cf244bfb8aa 100644 --- a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs +++ b/compiler/rustc_hir/src/attrs/encode_cross_crate.rs @@ -82,6 +82,7 @@ impl AttributeKind { PatternComplexityLimit { .. } => No, PinV2(..) => Yes, Pointee(..) => No, + PreludeImport => No, ProcMacro(..) => No, ProcMacroAttribute(..) => No, ProcMacroDerive { .. } => No, diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index b6f0e9fedd6d8..08f2597e874d8 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -282,6 +282,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | AttributeKind::PatternComplexityLimit { .. } | AttributeKind::PinV2(..) | AttributeKind::Pointee(..) + | AttributeKind::PreludeImport | AttributeKind::ProfilerRuntime | AttributeKind::RecursionLimit { .. } | AttributeKind::ReexportTestHarnessMain(..) @@ -394,7 +395,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { // need to be fixed | sym::deprecated_safe // FIXME(deprecated_safe) // internal - | sym::prelude_import | sym::panic_handler | sym::lang | sym::default_lib_allocator From dd8a98a9bc83076db950c2f10c46a4ce4499a011 Mon Sep 17 00:00:00 2001 From: Oscar Bray Date: Thu, 12 Feb 2026 17:43:58 +0000 Subject: [PATCH 2/2] Fix pretty print tests with #[prelude_import] --- tests/pretty/delegation-inherit-attributes.pp | 2 +- tests/pretty/delegation-inline-attribute.pp | 2 +- tests/pretty/hir-delegation.pp | 2 +- tests/pretty/hir-fn-params.pp | 2 +- tests/pretty/hir-fn-variadic.pp | 2 +- tests/pretty/hir-if-else.pp | 2 +- tests/pretty/hir-lifetimes.pp | 2 +- tests/pretty/hir-pretty-attr.pp | 2 +- tests/pretty/hir-pretty-loop.pp | 2 +- tests/pretty/hir-struct-expr.pp | 2 +- tests/pretty/issue-4264.pp | 2 +- tests/pretty/issue-85089.pp | 2 +- tests/pretty/pin-ergonomics-hir.pp | 2 +- tests/ui/consts/const-block-items/hir.stdout | 2 +- tests/ui/macros/genercs-in-path-with-prettry-hir.stdout | 2 +- tests/ui/match/issue-82392.stdout | 2 +- tests/ui/type-alias-impl-trait/issue-60662.stdout | 2 +- tests/ui/unpretty/bad-literal.stdout | 2 +- tests/ui/unpretty/debug-fmt-hir.stdout | 2 +- tests/ui/unpretty/deprecated-attr.stdout | 2 +- tests/ui/unpretty/diagnostic-attr.stdout | 2 +- tests/ui/unpretty/exhaustive-asm.hir.stdout | 2 +- tests/ui/unpretty/exhaustive.hir.stdout | 4 ++-- tests/ui/unpretty/flattened-format-args.stdout | 2 +- tests/ui/unpretty/let-else-hir.stdout | 2 +- tests/ui/unpretty/self-hir.stdout | 2 +- .../unpretty/struct-exprs-tuple-call-pretty-printing.stdout | 2 +- tests/ui/unpretty/unpretty-expr-fn-arg.stdout | 2 +- 28 files changed, 29 insertions(+), 29 deletions(-) diff --git a/tests/pretty/delegation-inherit-attributes.pp b/tests/pretty/delegation-inherit-attributes.pp index 9d51a80da7b98..26ca5e99b885d 100644 --- a/tests/pretty/delegation-inherit-attributes.pp +++ b/tests/pretty/delegation-inherit-attributes.pp @@ -7,7 +7,7 @@ #![allow(incomplete_features)] #![feature(fn_delegation)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use std::prelude::rust_2021::*; extern crate to_reuse_functions; diff --git a/tests/pretty/delegation-inline-attribute.pp b/tests/pretty/delegation-inline-attribute.pp index f83ae47b81f6c..9f362fa863f82 100644 --- a/tests/pretty/delegation-inline-attribute.pp +++ b/tests/pretty/delegation-inline-attribute.pp @@ -5,7 +5,7 @@ #![allow(incomplete_features)] #![feature(fn_delegation)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; mod to_reuse { diff --git a/tests/pretty/hir-delegation.pp b/tests/pretty/hir-delegation.pp index 44a1deb750dc0..59491b6ebd7c1 100644 --- a/tests/pretty/hir-delegation.pp +++ b/tests/pretty/hir-delegation.pp @@ -5,7 +5,7 @@ #![allow(incomplete_features)] #![feature(fn_delegation)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; fn b(e: C) { } diff --git a/tests/pretty/hir-fn-params.pp b/tests/pretty/hir-fn-params.pp index 52310d5024cd6..15373bba24d3f 100644 --- a/tests/pretty/hir-fn-params.pp +++ b/tests/pretty/hir-fn-params.pp @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ pretty-compare-only //@ pretty-mode:hir diff --git a/tests/pretty/hir-fn-variadic.pp b/tests/pretty/hir-fn-variadic.pp index 6356eec80e0e8..3837b260cc5d6 100644 --- a/tests/pretty/hir-fn-variadic.pp +++ b/tests/pretty/hir-fn-variadic.pp @@ -4,7 +4,7 @@ #![feature(c_variadic)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; extern "C" { diff --git a/tests/pretty/hir-if-else.pp b/tests/pretty/hir-if-else.pp index d3721e1758157..c2050e1f6e472 100644 --- a/tests/pretty/hir-if-else.pp +++ b/tests/pretty/hir-if-else.pp @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ pretty-compare-only //@ pretty-mode:hir diff --git a/tests/pretty/hir-lifetimes.pp b/tests/pretty/hir-lifetimes.pp index ceb0f6e3b7c24..c35a40eed0c50 100644 --- a/tests/pretty/hir-lifetimes.pp +++ b/tests/pretty/hir-lifetimes.pp @@ -6,7 +6,7 @@ #![allow(unused)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; struct Foo<'a> { diff --git a/tests/pretty/hir-pretty-attr.pp b/tests/pretty/hir-pretty-attr.pp index a9d8b5e7e5770..be23294e8f7eb 100644 --- a/tests/pretty/hir-pretty-attr.pp +++ b/tests/pretty/hir-pretty-attr.pp @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ pretty-compare-only //@ pretty-mode:hir diff --git a/tests/pretty/hir-pretty-loop.pp b/tests/pretty/hir-pretty-loop.pp index e6614ce318cce..f9bc7416e4bcf 100644 --- a/tests/pretty/hir-pretty-loop.pp +++ b/tests/pretty/hir-pretty-loop.pp @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ pretty-compare-only //@ pretty-mode:hir diff --git a/tests/pretty/hir-struct-expr.pp b/tests/pretty/hir-struct-expr.pp index 198d7ad6a9b6b..2557aa42378af 100644 --- a/tests/pretty/hir-struct-expr.pp +++ b/tests/pretty/hir-struct-expr.pp @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ pretty-compare-only //@ pretty-mode:hir diff --git a/tests/pretty/issue-4264.pp b/tests/pretty/issue-4264.pp index 568269644bb87..d73ad35d62228 100644 --- a/tests/pretty/issue-4264.pp +++ b/tests/pretty/issue-4264.pp @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ pretty-compare-only //@ pretty-mode:hir,typed diff --git a/tests/pretty/issue-85089.pp b/tests/pretty/issue-85089.pp index 919573220fddd..ab386666da172 100644 --- a/tests/pretty/issue-85089.pp +++ b/tests/pretty/issue-85089.pp @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; // Test to print lifetimes on HIR pretty-printing. diff --git a/tests/pretty/pin-ergonomics-hir.pp b/tests/pretty/pin-ergonomics-hir.pp index cf9b6707ed2ff..e422edf54e0c5 100644 --- a/tests/pretty/pin-ergonomics-hir.pp +++ b/tests/pretty/pin-ergonomics-hir.pp @@ -5,7 +5,7 @@ #![feature(pin_ergonomics)] #![allow(dead_code, incomplete_features)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; use std::pin::Pin; diff --git a/tests/ui/consts/const-block-items/hir.stdout b/tests/ui/consts/const-block-items/hir.stdout index e2df04e98d4ea..2b7f0818556ff 100644 --- a/tests/ui/consts/const-block-items/hir.stdout +++ b/tests/ui/consts/const-block-items/hir.stdout @@ -3,7 +3,7 @@ #![feature(const_block_items)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; const _: () = diff --git a/tests/ui/macros/genercs-in-path-with-prettry-hir.stdout b/tests/ui/macros/genercs-in-path-with-prettry-hir.stdout index 6e41432ad7df1..f01807ec06175 100644 --- a/tests/ui/macros/genercs-in-path-with-prettry-hir.stdout +++ b/tests/ui/macros/genercs-in-path-with-prettry-hir.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir //@ edition: 2015 diff --git a/tests/ui/match/issue-82392.stdout b/tests/ui/match/issue-82392.stdout index d44ffbe216716..297a6f4827909 100644 --- a/tests/ui/match/issue-82392.stdout +++ b/tests/ui/match/issue-82392.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; // https://github.com/rust-lang/rust/issues/82329 //@ compile-flags: -Zunpretty=hir,typed diff --git a/tests/ui/type-alias-impl-trait/issue-60662.stdout b/tests/ui/type-alias-impl-trait/issue-60662.stdout index d1f337819f8b0..dff748b43119d 100644 --- a/tests/ui/type-alias-impl-trait/issue-60662.stdout +++ b/tests/ui/type-alias-impl-trait/issue-60662.stdout @@ -4,7 +4,7 @@ #![feature(type_alias_impl_trait)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; trait Animal { } diff --git a/tests/ui/unpretty/bad-literal.stdout b/tests/ui/unpretty/bad-literal.stdout index 267d59a868e41..711f3a9bdf875 100644 --- a/tests/ui/unpretty/bad-literal.stdout +++ b/tests/ui/unpretty/bad-literal.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir //@ check-fail diff --git a/tests/ui/unpretty/debug-fmt-hir.stdout b/tests/ui/unpretty/debug-fmt-hir.stdout index 342dc144909ce..1f0a6e2e334fd 100644 --- a/tests/ui/unpretty/debug-fmt-hir.stdout +++ b/tests/ui/unpretty/debug-fmt-hir.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir //@ check-pass diff --git a/tests/ui/unpretty/deprecated-attr.stdout b/tests/ui/unpretty/deprecated-attr.stdout index 32aac13586d5a..32d5cf06a3d67 100644 --- a/tests/ui/unpretty/deprecated-attr.stdout +++ b/tests/ui/unpretty/deprecated-attr.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir //@ check-pass diff --git a/tests/ui/unpretty/diagnostic-attr.stdout b/tests/ui/unpretty/diagnostic-attr.stdout index 25349681b02a3..0b4b5f9193435 100644 --- a/tests/ui/unpretty/diagnostic-attr.stdout +++ b/tests/ui/unpretty/diagnostic-attr.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir //@ check-pass diff --git a/tests/ui/unpretty/exhaustive-asm.hir.stdout b/tests/ui/unpretty/exhaustive-asm.hir.stdout index ed98191e1dd56..c44db08653967 100644 --- a/tests/ui/unpretty/exhaustive-asm.hir.stdout +++ b/tests/ui/unpretty/exhaustive-asm.hir.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use std::prelude::rust_2024::*; //@ revisions: expanded hir //@[expanded]compile-flags: -Zunpretty=expanded diff --git a/tests/ui/unpretty/exhaustive.hir.stdout b/tests/ui/unpretty/exhaustive.hir.stdout index f309aa0b5fb67..7ee848491d6e5 100644 --- a/tests/ui/unpretty/exhaustive.hir.stdout +++ b/tests/ui/unpretty/exhaustive.hir.stdout @@ -31,7 +31,7 @@ #![feature(yeet_expr)] #![allow(incomplete_features)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use std::prelude::rust_2024::*; mod prelude { @@ -46,7 +46,7 @@ mod prelude { } } -#[prelude_import] +#[attr = PreludeImport] use self::prelude::*; /// inner single-line doc comment diff --git a/tests/ui/unpretty/flattened-format-args.stdout b/tests/ui/unpretty/flattened-format-args.stdout index 156dcd68a674e..008d69c9b9ef7 100644 --- a/tests/ui/unpretty/flattened-format-args.stdout +++ b/tests/ui/unpretty/flattened-format-args.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir -Zflatten-format-args=yes //@ check-pass diff --git a/tests/ui/unpretty/let-else-hir.stdout b/tests/ui/unpretty/let-else-hir.stdout index cc19f392c3a43..73d627ef997a1 100644 --- a/tests/ui/unpretty/let-else-hir.stdout +++ b/tests/ui/unpretty/let-else-hir.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir //@ check-pass diff --git a/tests/ui/unpretty/self-hir.stdout b/tests/ui/unpretty/self-hir.stdout index c973e143275cd..b14c583f4f387 100644 --- a/tests/ui/unpretty/self-hir.stdout +++ b/tests/ui/unpretty/self-hir.stdout @@ -1,5 +1,5 @@ extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; //@ compile-flags: -Zunpretty=hir //@ check-pass diff --git a/tests/ui/unpretty/struct-exprs-tuple-call-pretty-printing.stdout b/tests/ui/unpretty/struct-exprs-tuple-call-pretty-printing.stdout index 8b6ca4f672dc4..c990837d2138a 100644 --- a/tests/ui/unpretty/struct-exprs-tuple-call-pretty-printing.stdout +++ b/tests/ui/unpretty/struct-exprs-tuple-call-pretty-printing.stdout @@ -5,7 +5,7 @@ #![expect(incomplete_features)] #![allow(dead_code)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; use std::marker::ConstParamTy; diff --git a/tests/ui/unpretty/unpretty-expr-fn-arg.stdout b/tests/ui/unpretty/unpretty-expr-fn-arg.stdout index 41d62d11aaa61..19bfe92e3b27f 100644 --- a/tests/ui/unpretty/unpretty-expr-fn-arg.stdout +++ b/tests/ui/unpretty/unpretty-expr-fn-arg.stdout @@ -9,7 +9,7 @@ //@ edition: 2015 #![allow(dead_code)] extern crate std; -#[prelude_import] +#[attr = PreludeImport] use ::std::prelude::rust_2015::*; fn main() ({ } as ())