Skip to content

Resolve the prelude import in build_reduced_graph #145322

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

LorrensP-2158466
Copy link
Contributor

@LorrensP-2158466 LorrensP-2158466 commented Aug 12, 2025

This pr tries to resolve the prelude import at the build_reduced_graph stage.
Part of batched import resolution in #145108 (cherry picked commit) and maybe needed for #139493.

r? petrochenkov

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 12, 2025
@@ -495,7 +495,22 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
}
// We don't add prelude imports to the globs since they only affect lexical scopes,
// which are not relevant to import resolution.
ImportKind::Glob { is_prelude: true, .. } => {}
ImportKind::Glob { is_prelude: true, .. } => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you avoid calling add_import for prelude imports entirely?
And remove the is_prelude field from ImportKind::Glob.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think its avoidable, maybe_resolve_path takes an ignore_import, which is the glob_import itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore_import can be None here, it doesn't make sense during the prelude import resolution.

None,
&import.parent_scope,
Some(import),
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you immediately report an error if maybe_resolve_path returns anything other than PathResult::Module(ModuleOrUniformRoot::Module(..))?
Just some error, having a good error here is not important since it's an implementation detail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not possible I think, in library/core/src/lib.rs there is:

#[prelude_import]
#[allow(unused)]
use prelude::rust_2024::*;

Which failed and thus throws the error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import needs to be put after mod prelude;.
It's an internal detail so we can do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was it, thanks!

) = path_res
{
import.imported_module.set(Some(module_or_uniform_root));
self.r.prelude = Some(module);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you avoid calling maybe_resolve_path and setting the prelude if it is already set?
Can also report some error in that case.

@petrochenkov
Copy link
Contributor

Could you also add this case to tests?

//@ check-pass

#![feature(custom_inner_attributes)]

#![rustfmt::skip]

fn main() {
    let _ = todo!();
}

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2025
@LorrensP-2158466
Copy link
Contributor Author

LorrensP-2158466 commented Aug 12, 2025

I am currently unable to implement any of your suggestions; it is the prelude import that can not be resolved during build_reduced_graph. Which makes me think that this change doesn't actually do anything useful? At least the prelude import in stdlib keeps failing with every change.

@petrochenkov
Copy link
Contributor

@Voultapher
This change should fix your issues with #139493 and custom inner attributes.
Could you check it when it's ready?

@LorrensP-2158466
Copy link
Contributor Author

Suggestions worked, however, we can't emit an error when the prelude is already set, I just used a panic for ease of use, and it was triggered when compiling stdlib:

prelude already set: [Segment { ident: prelude#0, id: Some(NodeId(479)), has_generic_args: false, has_lifetime_args: false, args_span: no-location (#0) }, Segment { ident: rust_2021#0, id: Some(NodeId(480)), has_generic_args: false, has_lifetime_args: false, args_span: no-location (#0) }]

I also can't avoid to only resolve the prelude path when the prelude is not set. Errors like these get emitted:

error[E0433]: failed to resolve: use of undeclared type `String`
   --> library/std/src/panicking.rs:642:29
    |
642 |                 let mut s = String::new();
    |                             ^^^^^^ use of undeclared type `String`
    |
help: consider importing one of these structs
    |
 12 + use crate::prelude::rust_future::String;
    |
 12 + use alloc::string::String;
    |

error[E0433]: failed to resolve: use of undeclared type `Box`
   --> library/std/src/panicking.rs:656:13
    |
656 |             Box::into_raw(Box::new(contents))
    |             ^^^ use of undeclared type `Box`
    |
help: consider importing one of these structs
    |
 12 + use crate::prelude::rust_future::Box;
    |
 12 + use alloc::boxed::Box;

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants