Skip to content
21 changes: 16 additions & 5 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use tracing::debug;

use crate::dedup_hash::DedupHashEncoder;
use crate::{
CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
MultiSpan, StashKey, Style, Sublevel, Substitution, SubstitutionPart, SuggestionStyle,
Suggestions,
CodeSuggestion, DiagCtxtHandle, DiagMessage, EmissionOverride, ErrCode, ErrorGuaranteed,
ExplicitBug, Level, MultiSpan, StashKey, Style, Sublevel, Substitution, SubstitutionPart,
SuggestionStyle, Suggestions,
};

/// Trait implemented by error types. This is rarely implemented manually. Instead, use
Expand Down Expand Up @@ -203,6 +203,13 @@ pub struct DiagInner {
pub emitted_at: DiagLocation,
}

/// Marker stored in `long_ty_path` for diagnostics that are never shown. Real paths are never empty.
pub const SUPPRESSED_LONG_TY_PATH: PathBuf = PathBuf::new();

pub fn is_long_ty_path_suppressed(path: &Option<PathBuf>) -> bool {
path.as_ref().is_some_and(|p| p.as_os_str().is_empty())
}

impl DiagInner {
#[track_caller]
pub fn new<M: Into<DiagMessage>>(level: Level, message: M) -> Self {
Expand All @@ -220,7 +227,9 @@ impl DiagInner {
suggestions: Suggestions::Enabled(vec![]),
args: Default::default(),
is_lint: None,
long_ty_path: None,
// Expected lints are never shown, so they must not write long type files.
long_ty_path: matches!(level, Level::Warning(Some(EmissionOverride::Expected { .. })))
Comment thread
LusterSourav marked this conversation as resolved.
Resolved

@JonathanBrouwer JonathanBrouwer Nov 22, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually could we make this a custom enum rather than an option? I think that's clearer
Like

enum LongTyPath {
    None,
    Surpressed,
    Some(PathBuf)
}

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, I agree a enum with none, suppresed and some(PathBuf) states the intent directl & its removes the empty path trick and makes it hard to forget the suppresed case here cost is that every place that uses the field need updting hmhmhm

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

but the q is i have to changes it one many file like 40+

The trait IntoDiagArg has a method that takes &mut option PathBuf ,it is declared in rustc_error_messages.
and every typ that implments this trait has to repeat that exact sigature. About 30 types across about 20 crates implemnt it, in rustc_abi, rustc_hir, rustc_target and others..... and the change option PathBuf to LongTyPath, evry one of those signatues stops compiling...so more and more line edts

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this enum get me more edits... ???????

@LusterSourav LusterSourav Nov 22, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

could u please suggest me what to doooo plsss

.then_some(SUPPRESSED_LONG_TY_PATH),
emitted_at: DiagLocation::caller(),
}
}
Expand Down Expand Up @@ -1139,7 +1148,9 @@ impl<'a> Diag<'a> {
/// `cancel`, etc. Afterwards, `drop` is the only code that will be run on
/// `self`.
fn take_diag(&mut self) -> DiagInner {
if let Some(path) = &self.long_ty_path {
if let Some(path) = &self.long_ty_path
&& !is_long_ty_path_suppressed(&self.long_ty_path)
{
self.note(format!(
"the full name for the type has been written to '{}'",
path.display()
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub use anstyle::{
pub use codes::*;
pub use decorate_diag::{BufferedEarlyLint, DecorateDiagCompat, LintBuffer};
pub use diagnostic::{
Diag, DiagDecorator, DiagInner, DiagLocation, DiagStyledString, Diagnostic, StringPart,
Subdiag, Subdiagnostic,
Diag, DiagDecorator, DiagInner, DiagLocation, DiagStyledString, Diagnostic,
SUPPRESSED_LONG_TY_PATH, StringPart, Subdiag, Subdiagnostic, is_long_ty_path_suppressed,
};
pub use diagnostic_impls::{
DiagSymbolList, ElidedLifetimeInPathSubdiag, ExpectedLifetimeParameter,
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::{DefaultHasher, Hash, Hasher};
use std::io::{Read, Write};
use std::path::PathBuf;

use rustc_errors::pluralize;
use rustc_errors::{is_long_ty_path_suppressed, pluralize};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind};
use rustc_macros::extension;
Expand Down Expand Up @@ -302,6 +302,9 @@ impl<'tcx> TyCtxt<'tcx> {
if regular == short {
return regular;
}
if is_long_ty_path_suppressed(path) {
return short;
}
// Ensure we create an unique file for the type passed in when we create a file.
let mut s = DefaultHasher::new();
t.hash(&mut s);
Expand Down
5 changes: 5 additions & 0 deletions tests/run-make/expect-lint-no-long-type-file/allowed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![allow(improper_ctypes_definitions)]

extern "C" fn foo(_: Option<Option<Option<Option<Option<Option<Option<Option<i32>>>>>>>>) {}

fn main() {}
5 changes: 5 additions & 0 deletions tests/run-make/expect-lint-no-long-type-file/expected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![expect(improper_ctypes_definitions, dead_code)]

extern "C" fn foo(_: Option<Option<Option<Option<Option<Option<Option<Option<i32>>>>>>>>) {}

fn main() {}
43 changes: 43 additions & 0 deletions tests/run-make/expect-lint-no-long-type-file/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Regression test for https://github.com/rust-lang/rust/issues/166293.
// Expected and allowed lints are never shown, so they must not write a long type file to disk.
// A lint that is shown still must write one, which guards against a vacuous pass.

use run_make_support::{rfs, rustc};

/// Compiles `input` and returns the long type files it created, removing them afterwards so
/// that every case is counted independently.
fn compile_and_count_long_type_files(input: &str) -> usize {
rustc()
.input(input)
.edition("2024")
.arg("-Zwrite-long-types-to-disk=yes")
.arg("--diagnostic-width=60")
.run();

let files: Vec<_> = rfs::read_dir(".")
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_name().to_string_lossy().contains("long-type"))
.collect();
for file in &files {
rfs::remove_file(file.path());
}
files.len()
}

fn main() {
assert_eq!(
compile_and_count_long_type_files("expected.rs"),
0,
"expected lint created a long type file"
);
assert_eq!(
compile_and_count_long_type_files("allowed.rs"),
0,
"allowed lint created a long type file"
);
assert_eq!(
compile_and_count_long_type_files("shown.rs"),
1,
"shown lint should create a long type file"
);
}
5 changes: 5 additions & 0 deletions tests/run-make/expect-lint-no-long-type-file/shown.rs
Comment thread
LusterSourav marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![warn(improper_ctypes_definitions)]

extern "C" fn foo(_: Option<Option<Option<Option<Option<Option<Option<Option<i32>>>>>>>>) {}

fn main() {}
Loading