Skip to content

Compute proper module parent during resolution - #77984

Merged
bors merged 1 commit into
rust-lang:masterfrom
Aaron1011:fix/macro-mod-weird-parent
Oct 25, 2020
Merged

Compute proper module parent during resolution#77984
bors merged 1 commit into
rust-lang:masterfrom
Aaron1011:fix/macro-mod-weird-parent

Conversation

@Aaron1011

Copy link
Copy Markdown
Contributor

Fixes#75982

The direct parent of a module may not be a module
(e.g. const _: () = { #[path = "foo.rs"] mod foo; };).

To find the parent of a module for purposes of resolution, we need to
walk up the tree until we hit a module or a crate root.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @davidtwco

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 15, 2020
@jyn514

Copy link
Copy Markdown
Member

This same code is copied in rustdoc:

// The immediate parent might not always be a module.
// Find the first parent which is.
loop{
ifletSome(parent) = self.cx.tcx.parent(current){
ifself.cx.tcx.def_kind(parent) == DefKind::Mod{
breakSome(parent);
}
current = parent;
}else{
debug!(
"{:?} has no parent (kind={:?}, original was {:?})",
current,
self.cx.tcx.def_kind(current),
item.def_id
);
breakNone;
}
}

Maybe it's worth making this a query or method on tcx?

@jyn514jyn514 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 15, 2020
@Aaron1011

Copy link
Copy Markdown
ContributorAuthor

@jyn514: It's not quite the same code - we don't have a tcx available in the resolver, so we need to go through the cstore methods directly. I think it's better to keep this separate, since going through a tcx is better when a tcx is available.

@petrochenkovpetrochenkov self-assigned this Oct 15, 2020
@davidtwcodavidtwco removed their assignment Oct 16, 2020
Comment threadsrc/test/ui/macros/auxiliary/issue-75982.rs Outdated
Comment threadcompiler/rustc_resolve/src/build_reduced_graph.rs Outdated
@petrochenkov

Copy link
Copy Markdown
Contributor

So, the sad part here is that the Module in question is not actually a mod item, but any "module in resolve sense" aka "a container for named items", that includes blocks.

If

const _:() = {#[macro_export]macro_rules! my_macro {() => {};}};

was written in the current crate, then get_module would indeed returned the constant's block (it would have ModuleKind::Block).

We don't track blocks for other crates, so we simply cannot produce a correct answer here.
This situation is specific to #[macro_export] macros which have unique ability to be visible to other crates while being written in very internal locations.

So, returning the closest mod item is not really correct, but at least it doesn't ICEs.
Thankfully, returning an incorrect parent module in this case shouldn't have any observable effect in theory (but it would be a different story if my_macro could use def-site hygiene).

@petrochenkovpetrochenkov 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 Oct 19, 2020
@Aaron1011
Aaron1011force-pushed the fix/macro-mod-weird-parent branch 2 times, most recently from 0567261 to c07f8f8CompareOctober 24, 2020 16:30
@Aaron1011Aaron1011 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 Oct 24, 2020
@petrochenkov

Copy link
Copy Markdown
Contributor

r=me with the test changes #77984 (comment)

@petrochenkovpetrochenkov 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 Oct 24, 2020
Fixesrust-lang#75982
The direct parent of a module may not be a module
(e.g. `const _: () = { #[path = "foo.rs"] mod foo; };`).
To find the parent of a module for purposes of resolution, we need to
walk up the tree until we hit a module or a crate root.
@Aaron1011
Aaron1011force-pushed the fix/macro-mod-weird-parent branch from c07f8f8 to 283053aCompareOctober 24, 2020 18:28
@Aaron1011

Copy link
Copy Markdown
ContributorAuthor

@petrochenkov: Your suggested test caught a bug in my implementation. We also need to perform the parent-walking in macro_def_scope (not just in get_module), since a macro_rules! might occur directly inside a const item.

@petrochenkov

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Oct 24, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit 283053a has been approved by petrochenkov

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 24, 2020
@JohnTitorJohnTitor mentioned this pull request Oct 25, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 25, 2020
Rollup of 8 pull requests
Successful merges:
- rust-lang#77984 (Compute proper module parent during resolution)
- rust-lang#78085 (MIR validation should check `SwitchInt` values are valid for the type)
- rust-lang#78208 (replace `#[allow_internal_unstable]` with `#[rustc_allow_const_fn_unstable]` for `const fn`s)
- rust-lang#78209 (Update `compiler_builtins` to 0.1.36)
- rust-lang#78276 (Bump backtrace-rs to enable Mach-O support on iOS.)
- rust-lang#78320 (Link to cargo's `build-std` feature instead of `xargo` in custom target docs)
- rust-lang#78322 (BTreeMap: stop mistaking node::MIN_LEN for a node level constraint)
- rust-lang#78326 (Split out statement attributes changes from rust-lang#78306)
Failed merges:
r? `@ghost`
@bors
bors merged commit 569d29d into rust-lang:masterOct 25, 2020
@rustbotrustbot added this to the 1.49.0 milestone Oct 25, 2020
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant 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.

ICE: Expected module, found DefId

7 participants

@Aaron1011@rust-highfive@jyn514@petrochenkov@bors@davidtwco@rustbot