Skip to content
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

HIR and TyCtxt disagree about the parents of decl macros #77828

Closed
jyn514 opened this issue Oct 11, 2020 · 8 comments · Fixed by #80415
Closed

HIR and TyCtxt disagree about the parents of decl macros #77828

jyn514 opened this issue Oct 11, 2020 · 8 comments · Fixed by #80415
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. F-decl_macro `#![feature(decl_macro)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Oct 11, 2020

I tried this code:

#[unstable(feature = "raw_ref_macros", issue = "73394")]
#[rustc_macro_transparency = "semitransparent"]
#[allow_internal_unstable(raw_ref_op)]
pub macro raw_const($e:expr) {
&raw const $e
}

I expected to see this happen: tcx.hir().parent_module(raw_const_hir_id) and tcx.parent(raw_const_def_id).unwrap() are both the module core::ptr.

Instead, this happened: tcx.hir().parent_module(raw_const_hir_id) is core and tcx.parent(raw_const_def_id).unwrap() is core::ptr. I think core::ptr is correct.

Meta

rustc --version: HEAD (8cc82ee)

@jyn514 jyn514 added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. F-decl_macro `#![feature(decl_macro)]` labels Oct 11, 2020
@jyn514
Copy link
Member Author

jyn514 commented Oct 11, 2020

This is one of the problems causing #74355.

@RalfJung
Copy link
Member

Curious, and good catch!

Who would be a HIR expert we could ask about this?

@jyn514
Copy link
Member Author

jyn514 commented Oct 20, 2020

I don't know. Maybe @ecstatic-morse?

@danielhenrymantilla
Copy link
Contributor

danielhenrymantilla commented Jan 6, 2021

Hmm, this is still the case when the macro is defined inside a value-namespace-inhabiting scope:

const C: () = {
    pub macro m() {} // <- `def.hir_id()`
};

now yields:

[src/librustdoc/visit_ast.rs:86] self.cx.tcx.parent_module(def.hir_id) = DefId(0:0 ~ core[8787])
[src/librustdoc/visit_ast.rs:87] {
    use rustc_middle::ty::DefIdTree;
    self.cx.tcx.parent(self.cx.tcx.hir().local_def_id(def.hir_id).to_def_id()).unwrap()
} = DefId(0:16466 ~ core[8787]::C)

Should a new issue be opened, or this one be re-opened?

@jyn514
Copy link
Member Author

jyn514 commented Jan 7, 2021

@danielhenrymantilla which case did #80415 fix? Is the bug fixed for things in modules, but not in other contexts?

@jyn514 jyn514 reopened this Jan 7, 2021
@danielhenrymantilla
Copy link
Contributor

It looks like it: it definitely fixes it for macros inside modules, and the whole re-export ordeal works. Basically, in my PR, the only tests to fail were those involving a macro defined within a function or within a constant.

@cjgillot
Copy link
Contributor

cjgillot commented Jan 7, 2021

Hmm, this is still the case when the macro is defined inside a value-namespace-inhabiting scope:

const C: () = {
    pub macro m() {} // <- `def.hir_id()`
};

now yields:

[src/librustdoc/visit_ast.rs:86] self.cx.tcx.parent_module(def.hir_id) = DefId(0:0 ~ core[8787])
[src/librustdoc/visit_ast.rs:87] {
    use rustc_middle::ty::DefIdTree;
    self.cx.tcx.parent(self.cx.tcx.hir().local_def_id(def.hir_id).to_def_id()).unwrap()
} = DefId(0:16466 ~ core[8787]::C)

Should a new issue be opened, or this one be re-opened?

I don't think this is an issue any more.
The second output core[8787]::C is not a module, so parent_module will not return it.
#80415 allows to compute the correct parent module (ie. not crate root) when the macro is inside a module.

@danielhenrymantilla
Copy link
Contributor

Interesting, so it's intended that .parent() and .parent_module() not always agree. I think we can close this again (and maybe submit a PR clarifying that in the documentation), and I'll adapt my PR accordingly 🙂

@jyn514 jyn514 closed this as completed Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. F-decl_macro `#![feature(decl_macro)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants