Skip to content

Commit 627c4bf

Browse files
committed
Auto merge of #147444 - jyn514:fully-qualified-paths, r=nnethercote
Allow printing a fully-qualified path in `def_path_str` Previously, the local crate would always be printed as a leading `crate::`. Allow resolving it to the crate name instead. This allows printing a fully qualified path with: ```rust let qualified_name = with_no_visible_paths!(with_resolve_crate_name!( with_no_trimmed_paths!(tcx.def_path_str(def_id)) )); ``` I found this useful for an out-of-tree rustc-driver. I do not currently have a usecase in mind upstream; I'm ok if you don't want this PR for that reason. See [#t-compiler/help > print a fully qualified path name? @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/print.20a.20fully.20qualified.20path.20name.3F/near/541560961) for additional context. This does not currently have tests. I am not aware of an easy way to test def-id printing, since it requires having access to a TyCtxt.
2 parents 9725c4b + fa9162d commit 627c4bf

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::ty::{
3131

3232
thread_local! {
3333
static FORCE_IMPL_FILENAME_LINE: Cell<bool> = const { Cell::new(false) };
34+
static SHOULD_PREFIX_WITH_CRATE_NAME: Cell<bool> = const { Cell::new(false) };
3435
static SHOULD_PREFIX_WITH_CRATE: Cell<bool> = const { Cell::new(false) };
3536
static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
3637
static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
@@ -98,7 +99,18 @@ define_helper!(
9899
/// cycle errors, this can result in extra or suboptimal error output,
99100
/// so this variable disables that check.
100101
fn with_forced_impl_filename_line(ForcedImplGuard, FORCE_IMPL_FILENAME_LINE);
102+
/// Adds the crate name prefix to paths where appropriate.
103+
/// Unlike `with_crate_prefix`, this unconditionally uses `tcx.crate_name` instead of sometimes
104+
/// using `crate::` for local items.
105+
///
106+
/// Overrides `with_crate_prefix`.
107+
108+
// This function is not currently used in-tree, but it's used by a downstream rustc-driver in
109+
// Ferrocene. Please check with them before removing it.
110+
fn with_resolve_crate_name(CrateNamePrefixGuard, SHOULD_PREFIX_WITH_CRATE_NAME);
101111
/// Adds the `crate::` prefix to paths where appropriate.
112+
///
113+
/// Ignored if `with_resolve_crate_name` is active.
102114
fn with_crate_prefix(CratePrefixGuard, SHOULD_PREFIX_WITH_CRATE);
103115
/// Prevent path trimming if it is turned on. Path trimming affects `Display` impl
104116
/// of various rustc types, for example `std::vec::Vec` would be trimmed to `Vec`,
@@ -2313,7 +2325,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
23132325

23142326
fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
23152327
self.empty_path = true;
2316-
if cnum == LOCAL_CRATE {
2328+
if cnum == LOCAL_CRATE && !with_resolve_crate_name() {
23172329
if self.tcx.sess.at_least_rust_2018() {
23182330
// We add the `crate::` keyword on Rust 2018, only when desired.
23192331
if with_crate_prefix() {

0 commit comments

Comments
 (0)