Skip to content

ICE when printing paths in modules with non-DefId reexports (e.g. NonMacroAttr(Builtin)). #74236

Closed

Description

dep.rs (compiled with --edition=2018):

mod private { pub struct Pub; }
// Reexport built-in attribute without a DefId (requires Rust 2018).
pub use cfg_attr as attr;
// This export needs to be after the built-in attribute to trigger the bug.
pub use private::Pub as Renamed;

main.rs:

fn main() {
    // Trigger an error that will print the path of dep::private::Pub (as "dep::Renamed").
    let () = dep::Renamed;
}

Results in attempted .def_id() on invalid res: NonMacroAttr(Builtin) due to the .def_id() call in:

DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
let reexport = self
.tcx()
.item_children(visible_parent)
.iter()
.find(|child| child.res.def_id() == def_id)
.map(|child| child.ident.name);
if let Some(reexport) = reexport {
*name = reexport;
}
}

The snippet above is searching dep's children for the reexport of dep::private::Pub in order to grab its reexported name (Renamed here) when it runs into the pub use cfg_attr as attr; reexport lacking a DefId.

Fix is straight-forward, we can just ignore siblings lacking a DefId:

-         .find(|child| child.res.def_id() == def_id)
+         .find(|child| child.res.opt_def_id() == Some(def_id))

But I'm mostly making this issue to point out the fact that it is possible to create strange reexports that could break various parts of the compiler, even if it might be very rare (#74081 appears to be an instance of this).

(as I was writing this I found that @da-x had already tried out the fix - see #74081 (comment))

cc @rust-lang/compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions