Skip to content

Commit 646b3dc

Browse files
resolve prelude early on
1 parent 8bfc790 commit 646b3dc

File tree

4 files changed

+27
-46
lines changed

4 files changed

+27
-46
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use tracing::debug;
3333

3434
use crate::Namespace::{MacroNS, TypeNS, ValueNS};
3535
use crate::def_collector::collect_definitions;
36+
use crate::errors::CannotGlobImportAllCrates;
3637
use crate::imports::{ImportData, ImportKind};
3738
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
3839
use crate::{
@@ -493,24 +494,6 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
493494
});
494495
}
495496
}
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, .. } => {
499-
// We do resolve the prelude path so it's set before import resolution.
500-
let path_res = self.r.cm().maybe_resolve_path(
501-
&import.module_path,
502-
None,
503-
&import.parent_scope,
504-
Some(import),
505-
);
506-
if let PathResult::Module(
507-
module_or_uniform_root @ ModuleOrUniformRoot::Module(module),
508-
) = path_res
509-
{
510-
import.imported_module.set(Some(module_or_uniform_root));
511-
self.r.prelude = Some(module);
512-
}
513-
}
514497
ImportKind::Glob { .. } => current_module.globs.borrow_mut().push(import),
515498
_ => unreachable!(),
516499
}
@@ -673,13 +656,20 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
673656
self.add_import(module_path, kind, use_tree.span, item, root_span, item.id, vis);
674657
}
675658
ast::UseTreeKind::Glob => {
676-
let kind = ImportKind::Glob {
677-
is_prelude: ast::attr::contains_name(&item.attrs, sym::prelude_import),
678-
max_vis: Cell::new(None),
679-
id,
680-
};
681-
682-
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
659+
if !ast::attr::contains_name(&item.attrs, sym::prelude_import) {
660+
let kind = ImportKind::Glob { max_vis: Cell::new(None), id };
661+
self.add_import(prefix, kind, use_tree.span, item, root_span, item.id, vis);
662+
return;
663+
}
664+
// Resolve the prelude import early.
665+
let path_res =
666+
self.r.cm().maybe_resolve_path(&prefix, None, &self.parent_scope, None);
667+
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = path_res {
668+
self.r.prelude = Some(module);
669+
} else {
670+
// Same as in `imports::resolve_glob_import`
671+
self.r.dcx().emit_err(CannotGlobImportAllCrates { span: use_tree.span });
672+
}
683673
}
684674
ast::UseTreeKind::Nested { ref items, .. } => {
685675
// 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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@
210210
#[allow(unused_extern_crates)]
211211
extern crate self as core;
212212

213+
/* The core prelude, not as all-encompassing as the std prelude */
214+
pub mod prelude;
215+
213216
#[prelude_import]
214217
#[allow(unused)]
215218
use prelude::rust_2024::*;
@@ -295,10 +298,6 @@ pub mod f64;
295298
#[macro_use]
296299
pub mod num;
297300

298-
/* The core prelude, not as all-encompassing as the std prelude */
299-
300-
pub mod prelude;
301-
302301
/* Core modules for ownership management */
303302

304303
pub mod hint;

library/std/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@
421421
//
422422
#![default_lib_allocator]
423423

424+
// The Rust prelude
425+
pub mod prelude;
426+
424427
// Explicitly import the prelude. The compiler uses this same unstable attribute
425428
// to import the prelude implicitly when building crates that depend on std.
426429
#[prelude_import]
@@ -476,9 +479,6 @@ mod macros;
476479
#[macro_use]
477480
pub mod rt;
478481

479-
// The Rust prelude
480-
pub mod prelude;
481-
482482
#[stable(feature = "rust1", since = "1.0.0")]
483483
pub use core::any;
484484
#[stable(feature = "core_array", since = "1.35.0")]

0 commit comments

Comments
 (0)