Skip to content

Commit c327627

Browse files
Bail if printing item named _ in try_print_visible_def_path
1 parent 1f2cf1e commit c327627

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ pub trait PrettyPrinter<'tcx>:
319319
///
320320
/// `callers` is a chain of visible_parent's leading to `def_id`,
321321
/// to support cycle detection during recursion.
322+
///
323+
/// This method returns false if we can't print the visible path, so
324+
/// `print_def_path` can fall back on the item's real definition path.
322325
fn try_print_visible_def_path_recur(
323326
mut self,
324327
def_id: DefId,
@@ -405,19 +408,7 @@ pub trait PrettyPrinter<'tcx>:
405408
Some(parent) => parent,
406409
None => return Ok((self, false)),
407410
};
408-
if callers.contains(&visible_parent) {
409-
return Ok((self, false));
410-
}
411-
callers.push(visible_parent);
412-
// HACK(eddyb) this bypasses `path_append`'s prefix printing to avoid
413-
// knowing ahead of time whether the entire path will succeed or not.
414-
// To support printers that do not implement `PrettyPrinter`, a `Vec` or
415-
// linked list on the stack would need to be built, before any printing.
416-
match self.try_print_visible_def_path_recur(visible_parent, callers)? {
417-
(cx, false) => return Ok((cx, false)),
418-
(cx, true) => self = cx,
419-
}
420-
callers.pop();
411+
421412
let actual_parent = self.tcx().parent(def_id);
422413
debug!(
423414
"try_print_visible_def_path: visible_parent={:?} actual_parent={:?}",
@@ -463,14 +454,21 @@ pub trait PrettyPrinter<'tcx>:
463454
// `visible_parent_map`), looking for the specific child we currently have and then
464455
// have access to the re-exported name.
465456
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
457+
// Item might be re-exported several times, but filter for the one
458+
// that's public and whose identifier isn't `_`.
466459
let reexport = self
467460
.tcx()
468461
.item_children(visible_parent)
469462
.iter()
470-
.find(|child| child.res.opt_def_id() == Some(def_id))
463+
.filter(|child| child.res.opt_def_id() == Some(def_id))
464+
.find(|child| child.vis.is_public() && child.ident.name != kw::Underscore)
471465
.map(|child| child.ident.name);
472-
if let Some(reexport) = reexport {
473-
*name = reexport;
466+
467+
if let Some(new_name) = reexport {
468+
*name = new_name;
469+
} else {
470+
// There is no name that is public and isn't `_`, so bail.
471+
return Ok((self, false));
474472
}
475473
}
476474
// Re-exported `extern crate` (#43189).
@@ -481,6 +479,20 @@ pub trait PrettyPrinter<'tcx>:
481479
}
482480
debug!("try_print_visible_def_path: data={:?}", data);
483481

482+
if callers.contains(&visible_parent) {
483+
return Ok((self, false));
484+
}
485+
callers.push(visible_parent);
486+
// HACK(eddyb) this bypasses `path_append`'s prefix printing to avoid
487+
// knowing ahead of time whether the entire path will succeed or not.
488+
// To support printers that do not implement `PrettyPrinter`, a `Vec` or
489+
// linked list on the stack would need to be built, before any printing.
490+
match self.try_print_visible_def_path_recur(visible_parent, callers)? {
491+
(cx, false) => return Ok((cx, false)),
492+
(cx, true) => self = cx,
493+
}
494+
callers.pop();
495+
484496
Ok((self.path_append(Ok, &DisambiguatedDefPathData { data, disambiguator: 0 })?, true))
485497
}
486498

0 commit comments

Comments
 (0)