Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::ty::{

thread_local! {
static FORCE_IMPL_FILENAME_LINE: Cell<bool> = const { Cell::new(false) };
static SHOULD_PREFIX_WITH_CRATE_NAME: Cell<bool> = const { Cell::new(false) };
static SHOULD_PREFIX_WITH_CRATE: Cell<bool> = const { Cell::new(false) };
static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
Expand Down Expand Up @@ -98,7 +99,18 @@ define_helper!(
/// cycle errors, this can result in extra or suboptimal error output,
/// so this variable disables that check.
fn with_forced_impl_filename_line(ForcedImplGuard, FORCE_IMPL_FILENAME_LINE);
/// Adds the crate name prefix to paths where appropriate.
/// Unlike `with_crate_prefix`, this unconditionally uses `tcx.crate_name` instead of sometimes
/// using `crate::` for local items.
///
/// Overrides `with_crate_prefix`.

// This function is not currently used in-tree, but it's used by a downstream rustc-driver in
// Ferrocene. Please check with them before removing it.
fn with_resolve_crate_name(CrateNamePrefixGuard, SHOULD_PREFIX_WITH_CRATE_NAME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interaction between SHOULD_PREFIX_WITH_CRATE and SHOULD_PREFIX_WITH_CRATE_NAME is unclear. This extends an existing problem with these flags, which is how they all interact is confusing. E.g. with_no_trimmed_paths vs with_forced_trimmed_paths. Perhaps some of them need to be tri-state enums instead of booleans?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tri-state enums seem reasonable, but the API doesn't expose anything like that currently. we'd need to switch away from with!( ... ) macros to something like with!(CratePrinting::Expanded, ...).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it would be a follow-up for sure.

/// Adds the `crate::` prefix to paths where appropriate.
///
/// Ignored if `with_resolve_crate_name` is active.
fn with_crate_prefix(CratePrefixGuard, SHOULD_PREFIX_WITH_CRATE);
/// Prevent path trimming if it is turned on. Path trimming affects `Display` impl
/// of various rustc types, for example `std::vec::Vec` would be trimmed to `Vec`,
Expand Down Expand Up @@ -2313,7 +2325,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {

fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
self.empty_path = true;
if cnum == LOCAL_CRATE {
if cnum == LOCAL_CRATE && !with_resolve_crate_name() {
if self.tcx.sess.at_least_rust_2018() {
// We add the `crate::` keyword on Rust 2018, only when desired.
if with_crate_prefix() {
Expand Down
Loading