Skip to content

Commit c16ba50

Browse files
authored
Rollup merge of rust-lang#145322 - LorrensP-2158466:early-prelude-processing, r=petrochenkov
Resolve the prelude import in `build_reduced_graph` This pr tries to resolve the prelude import at the `build_reduced_graph` stage. Part of batched import resolution in rust-lang#145108 (cherry picked commit) and maybe needed for rust-lang#139493. r? petrochenkov
2 parents b22b6ba + ff560d3 commit c16ba50

File tree

12 files changed

+53
-49
lines changed

12 files changed

+53
-49
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,6 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
493493
});
494494
}
495495
}
496-
// We don't add prelude imports to the globs since they only affect lexical scopes,
497-
// which are not relevant to import resolution.
498-
ImportKind::Glob { is_prelude: true, .. } => {}
499496
ImportKind::Glob { .. } => current_module.globs.borrow_mut().push(import),
500497
_ => unreachable!(),
501498
}
@@ -658,13 +655,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
658655
self.add_import(module_path, kind, use_tree.span, item, root_span, item.id, vis);
659656
}
660657
ast::UseTreeKind::Glob => {
661-
let kind = ImportKind::Glob {
662-
is_prelude: ast::attr::contains_name(&item.attrs, sym::prelude_import),
663-
max_vis: Cell::new(None),
664-
id,
665-
};
666-
667-
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
658+
if !ast::attr::contains_name(&item.attrs, sym::prelude_import) {
659+
let kind = ImportKind::Glob { max_vis: Cell::new(None), id };
660+
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
661+
} else {
662+
// Resolve the prelude import early.
663+
let path_res =
664+
self.r.cm().maybe_resolve_path(&prefix, None, &self.parent_scope, None);
665+
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = path_res {
666+
self.r.prelude = Some(module);
667+
} else {
668+
self.r.dcx().span_err(use_tree.span, "cannot resolve a prelude import");
669+
}
670+
}
668671
}
669672
ast::UseTreeKind::Nested { ref items, .. } => {
670673
// Ensure there is at most one `self` in the list

compiler/rustc_resolve/src/imports.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub(crate) enum ImportKind<'ra> {
8787
id: NodeId,
8888
},
8989
Glob {
90-
is_prelude: bool,
9190
// The visibility of the greatest re-export.
9291
// n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
9392
max_vis: Cell<Option<Visibility>>,
@@ -125,12 +124,9 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
125124
.field("nested", nested)
126125
.field("id", id)
127126
.finish(),
128-
Glob { is_prelude, max_vis, id } => f
129-
.debug_struct("Glob")
130-
.field("is_prelude", is_prelude)
131-
.field("max_vis", max_vis)
132-
.field("id", id)
133-
.finish(),
127+
Glob { max_vis, id } => {
128+
f.debug_struct("Glob").field("max_vis", max_vis).field("id", id).finish()
129+
}
134130
ExternCrate { source, target, id } => f
135131
.debug_struct("ExternCrate")
136132
.field("source", source)
@@ -1073,7 +1069,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10731069
ImportKind::Single { source, target, ref bindings, type_ns_only, id, .. } => {
10741070
(source, target, bindings, type_ns_only, id)
10751071
}
1076-
ImportKind::Glob { is_prelude, ref max_vis, id } => {
1072+
ImportKind::Glob { ref max_vis, id } => {
10771073
if import.module_path.len() <= 1 {
10781074
// HACK(eddyb) `lint_if_path_starts_with_module` needs at least
10791075
// 2 segments, so the `resolve_path` above won't trigger it.
@@ -1096,8 +1092,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10961092
module: None,
10971093
});
10981094
}
1099-
if !is_prelude
1100-
&& let Some(max_vis) = max_vis.get()
1095+
if let Some(max_vis) = max_vis.get()
11011096
&& !max_vis.is_at_least(import.vis, self.tcx)
11021097
{
11031098
let def_id = self.local_def_id(id);
@@ -1485,7 +1480,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14851480

14861481
fn resolve_glob_import(&mut self, import: Import<'ra>) {
14871482
// This function is only called for glob imports.
1488-
let ImportKind::Glob { id, is_prelude, .. } = import.kind else { unreachable!() };
1483+
let ImportKind::Glob { id, .. } = import.kind else { unreachable!() };
14891484

14901485
let ModuleOrUniformRoot::Module(module) = import.imported_module.get().unwrap() else {
14911486
self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });
@@ -1504,9 +1499,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15041499

15051500
if module == import.parent_scope.module {
15061501
return;
1507-
} else if is_prelude {
1508-
self.prelude = Some(module);
1509-
return;
15101502
}
15111503

15121504
// Add to module's glob_importers

library/core/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@
208208
#[allow(unused_extern_crates)]
209209
extern crate self as core;
210210

211+
/* The core prelude, not as all-encompassing as the std prelude */
212+
// The compiler expects the prelude definition to be defined before it's use statement.
213+
pub mod prelude;
214+
211215
#[prelude_import]
212216
#[allow(unused)]
213217
use prelude::rust_2024::*;
@@ -293,10 +297,6 @@ pub mod f64;
293297
#[macro_use]
294298
pub mod num;
295299

296-
/* The core prelude, not as all-encompassing as the std prelude */
297-
298-
pub mod prelude;
299-
300300
/* Core modules for ownership management */
301301

302302
pub mod hint;

library/std/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@
428428
//
429429
#![default_lib_allocator]
430430

431+
// The Rust prelude
432+
// The compiler expects the prelude definition to be defined before it's use statement.
433+
pub mod prelude;
434+
431435
// Explicitly import the prelude. The compiler uses this same unstable attribute
432436
// to import the prelude implicitly when building crates that depend on std.
433437
#[prelude_import]
@@ -483,9 +487,6 @@ mod macros;
483487
#[macro_use]
484488
pub mod rt;
485489

486-
// The Rust prelude
487-
pub mod prelude;
488-
489490
#[stable(feature = "rust1", since = "1.0.0")]
490491
pub use core::any;
491492
#[stable(feature = "core_array", since = "1.35.0")]

tests/ui/extern-flag/empty-extern-arg.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//~ ERROR extern location for std does not exist
2+
//~^ ERROR cannot resolve a prelude import
23
//@ compile-flags: --extern std=
34
//@ needs-unwind since it affects the error output
45
//@ ignore-emscripten missing eh_catch_typeinfo lang item
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
error: extern location for std does not exist:
22

3+
error: cannot resolve a prelude import
4+
35
error: `#[panic_handler]` function required, but not found
46

57
error: unwinding panics are not supported without std
68
|
79
= help: using nightly cargo, use -Zbuild-std with panic="abort" to avoid unwinding
810
= note: since the core library is usually precompiled with panic="unwind", rebuilding your crate with panic="abort" may not be enough to fix the problem
911

10-
error: aborting due to 3 previous errors
12+
error: aborting due to 4 previous errors
1113

tests/ui/proc-macro/derive-helper-legacy-spurious.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
extern crate test_macros;
77

88
#[derive(Empty)]
9-
#[empty_helper] //~ ERROR cannot find attribute `empty_helper` in this scope
9+
#[empty_helper]
1010
struct Foo {}
1111

1212
fn main() {}

tests/ui/proc-macro/derive-helper-legacy-spurious.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: cannot find attribute `dummy` in this scope
44
LL | #![dummy]
55
| ^^^^^
66

7-
error: cannot find attribute `empty_helper` in this scope
8-
--> $DIR/derive-helper-legacy-spurious.rs:9:3
9-
|
10-
LL | #[empty_helper]
11-
| ^^^^^^^^^^^^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ check-pass
2+
//! This test checks that macro names resolved from the libstd prelude
3+
//! still work even if there's a crate-level custom inner attribute.
4+
5+
#![feature(custom_inner_attributes)]
6+
7+
#![rustfmt::skip]
8+
9+
fn main() {
10+
let _ = todo!();
11+
}

tests/ui/unpretty/exhaustive.expanded.stdout

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ extern crate std;
3434
#[prelude_import]
3535
use std::prelude::rust_2024::*;
3636

37-
#[prelude_import]
38-
use self::prelude::*;
39-
4037
mod prelude {
4138
pub use std::prelude::rust_2024::*;
4239

@@ -47,6 +44,9 @@ mod prelude {
4744
}
4845
}
4946

47+
#[prelude_import]
48+
use self::prelude::*;
49+
5050
mod attributes {
5151
//! inner single-line doc comment
5252
/*!

0 commit comments

Comments
 (0)