Skip to content

Refactor resolve_ident_in_module to use scopes #142547

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

b-naber
Copy link
Contributor

@b-naber b-naber commented Jun 15, 2025

Refactoring that should help with introducing name resolution for namespaced crates.

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 Jun 15, 2025
@eholk
Copy link
Contributor

eholk commented Jun 17, 2025

I gave this a quick read and it seems cleaner than before!

@rust-cloud-vms rust-cloud-vms bot force-pushed the refactor-resolve-ident-module branch from f6807eb to f9a6e87 Compare June 18, 2025 11:07
// However, it causes resolution/expansion to stuck too often (#53144), so, to make
// progress, we have to ignore those potential unresolved invocations from other modules
// and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
// shadowing is enabled, see `macro_expanded_macro_export_errors`).
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 make sure none of the comments is lost, like in this case?

}
}

// Forbid expanded shadowing to avoid time travel.
Copy link
Contributor

@petrochenkov petrochenkov Jun 19, 2025

Choose a reason for hiding this comment

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

This also indicates that the conversion to scopes wasn't done properly.
Ideally MoreExpandedVsOuter should be reported here in a similar way to how it's done in early_resolve_ident_in_lexical_scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate on what you expect this logic to look like with the change to NameResolution?

Copy link
Contributor

Choose a reason for hiding this comment

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

I really need to experiment on this myself to give a good answer.
Basically, this logic should be subsumed by the innermost_result checking logic in early_resolve_ident_in_lexical_scope.

And the AmbiguityKind::GlobVsExpanded logic in try_define should be subsumed by it as well.
cc #gsoc > Project: Parallel Macro Expansion @ 💬

@@ -130,6 +130,13 @@ enum Scope<'ra> {
BuiltinTypes,
}

/// Scopes used for resolving an `Ident` in a `Module`.
#[derive(Debug, Copy, Clone, PartialEq)]
enum ModuleScope {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of this, I think enum Scope above should also get two variants ModuleNonGlobs and ModuleGlobs instead of one Module.

Then finding any resolution inside a module would be done using a new ScopeSet variant including just two scopes, but other ScopeSet would just iterate over ModuleNonGlobs and ModuleGlobs as a part of the common loop.

This would also be a much larger refactoring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm while I do think it would be more consistent to have those two scopes in the Scope enum, it would also add more complexity to it. I kind of like that the resolution of an Ident in a module is isolated with the current resolve_ident_in_module, and using a separate ModuleScope for that method simplifies things imo.

Copy link
Contributor

Choose a reason for hiding this comment

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

The logic in early_resolve_ident_in_lexical_scope should see the explicit resolution and the glob resolution in a module as two separate steps.
So either Scope::Module should be split into two scopes, or resolve_ident_in_module_unadjusted should somehow produce the both resolutions so that early_resolve_ident_in_lexical_scope could process them as if they were from two separate scopes.

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'm open to try to implement this, but I still don't understand why this is necessary. Can you explain in more detail, please?

Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty much the same answer as in #142547 (comment).
Sorry, I'll try to produce some more details later.

@petrochenkov
Copy link
Contributor

I think this is a small step in the right direction, but it probably doesn't move us much closer to implementing the open namespace proposal, splitting modules into scopes properly is a large refactoring that only partially intersects with open namespaces.

Also not sure if landing just this part of the refactoring is beneficial.
Extracting finalize_module_binding and single_import_can_define_name are certainly good though.

@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 Jun 19, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the refactor-resolve-ident-module branch from 81fff03 to 7148d37 Compare June 23, 2025 13:10
@rust-log-analyzer

This comment has been minimized.

@b-naber
Copy link
Contributor Author

b-naber commented Jun 23, 2025

Don't understand the CI failure. It seems to checkout old code, the line that fails isn't on this branch.

edit: Should have rebased and run a check.

@b-naber
Copy link
Contributor Author

b-naber commented Jun 23, 2025

I changed NameResolution to contain non_glob_binding and glob_binding. This causes some changes to diagnostics when an existing non-glob resolution fails. I think this is expected though.

@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the refactor-resolve-ident-module branch from 3beb2f8 to c0f41d4 Compare June 23, 2025 17:50
@petrochenkov petrochenkov 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 Jun 24, 2025
@petrochenkov
Copy link
Contributor

Could you move the (non_)glob_binding change, and the finalize_module_binding/single_import_can_define_name extraction to separate PRs?
To avoid mixing changes that can be merged soon and other changes that need unclear amount of work.

@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 Jun 24, 2025
@b-naber
Copy link
Contributor Author

b-naber commented Jun 25, 2025

I'd prefer if you would review the changes to NameResolution here. There's really no urgency to merge those two method extraction changes imo, and I can always pull them out later if the NameResolution change isn't feasible.

The introduction of ModuleScope also doesn't change any semantics, but if you believe that negatively affects the review of the NameResolution change, then I'd split those changes out. I would assume you wouldn't want to merge the change to ModuelScope when splitting the PRs though, right?

@b-naber b-naber 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 Jul 7, 2025
@petrochenkov
Copy link
Contributor

I don't have anything new to add now besides repeating #142547 (comment) and #142547 (comment).
@rustbot author

@rustbot rustbot 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 Jul 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 8, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@b-naber
Copy link
Contributor Author

b-naber commented Jul 8, 2025

Since that causes me extra work, I would like to at least get a justification for why that is necessary. The (non_)glob-part of this re-factoring is simplified by introducing ModuleScope. If I were to create a separate PR for the (non_)glob_binding change, then that PR would also contain ModuleScope (and the methods that would be extracted to another PR). I also don't see the urgency for extracting those two methods to a separate PR.

As I previously said, I would be open to remove ModuleScope and include it in Scope if that were necessary. I would like to get an argument for that before making that change, however. I understand that you would need to play around with the code in order to give definitive answers for that, but that's also possible with the PR in its current form, and as mentioned, a separate PR for the (non_)glob_binding chang wouldn't look any different from this PR.

@petrochenkov
Copy link
Contributor

@LorrensP-2158466 will do #142547 (comment).
We'll need to somehow partition fn resolve_ident_in_module_unadjusted into mutable/immutable speculative/non-speculative parts anyway as a part of import/macro resolution parallelization project.

@bors
Copy link
Collaborator

bors commented Jul 10, 2025

☔ The latest upstream changes (presumably #143721) made this pull request unmergeable. Please resolve the merge conflicts.

tgross35 added a commit to tgross35/rust that referenced this pull request Jul 10, 2025
…extraction, r=petrochenkov

Resolve refactor: extraction of `finalize_module_binding` and `single_import_can_define_name`

This pr the work Vadim asked for in rust-lang#142547 (comment). This part:
> finalize_module_binding/single_import_can_define_name extraction

Cherry-picked commits of b-naber. Extraction of 2 processes in `resolve_ident_in_module_unadjusted`:
- `finalize_module_binding`
- `single_import_can_define_name`

r? `@petrochenkov`
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 10, 2025
…extraction, r=petrochenkov

Resolve refactor: extraction of `finalize_module_binding` and `single_import_can_define_name`

This pr the work Vadim asked for in rust-lang#142547 (comment). This part:
> finalize_module_binding/single_import_can_define_name extraction

Cherry-picked commits of b-naber. Extraction of 2 processes in `resolve_ident_in_module_unadjusted`:
- `finalize_module_binding`
- `single_import_can_define_name`

r? ``@petrochenkov``
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 11, 2025
…extraction, r=petrochenkov

Resolve refactor: extraction of `finalize_module_binding` and `single_import_can_define_name`

This pr the work Vadim asked for in rust-lang#142547 (comment). This part:
> finalize_module_binding/single_import_can_define_name extraction

Cherry-picked commits of b-naber. Extraction of 2 processes in `resolve_ident_in_module_unadjusted`:
- `finalize_module_binding`
- `single_import_can_define_name`

r? ```@petrochenkov```
rust-timer added a commit that referenced this pull request Jul 11, 2025
Rollup merge of #143728 - LorrensP-2158466:refactor-resolve-extraction, r=petrochenkov

Resolve refactor: extraction of `finalize_module_binding` and `single_import_can_define_name`

This pr the work Vadim asked for in #142547 (comment). This part:
> finalize_module_binding/single_import_can_define_name extraction

Cherry-picked commits of b-naber. Extraction of 2 processes in `resolve_ident_in_module_unadjusted`:
- `finalize_module_binding`
- `single_import_can_define_name`

r? ```@petrochenkov```
fmease added a commit to fmease/rust that referenced this pull request Jul 13, 2025
…resolution-bindings, r=petrochenkov

Refactor resolve resolution bindings

This pr does the work asked in rust-lang#142547 (comment). This part:

> move the `(non)_glob_binding` change

r? `@petrochenkov`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

6 participants