Description
openedon Jul 11, 2020
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:
rust/src/librustc_middle/ty/print/pretty.rs
Lines 391 to 401 in daecab3
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