Skip to content

Commit 044514e

Browse files
committed
Auto merge of #142689 - Urgau:rollup-4ho6835, r=Urgau
Rollup of 6 pull requests Successful merges: - #135656 (Add `-Z hint-mostly-unused` to tell rustc that most of a crate will go unused) - #138237 (Get rid of `EscapeDebugInner`.) - #141614 (lint direct use of rustc_type_ir ) - #142123 (Implement initial support for timing sections (`--json=timings`)) - #142377 (Try unremapping compiler sources) - #142674 (remove duplicate crash test) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c683403 + 663939d commit 044514e

File tree

35 files changed

+772
-181
lines changed

35 files changed

+772
-181
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::snippet::{
3434
Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
3535
};
3636
use crate::styled_buffer::StyledBuffer;
37+
use crate::timings::TimingRecord;
3738
use crate::translation::{Translate, to_fluent_args};
3839
use crate::{
3940
CodeSuggestion, DiagInner, DiagMessage, ErrCode, FluentBundle, LazyFallbackBundle, Level,
@@ -164,11 +165,16 @@ impl Margin {
164165
}
165166
}
166167

168+
pub enum TimingEvent {
169+
Start,
170+
End,
171+
}
172+
167173
const ANONYMIZED_LINE_NUM: &str = "LL";
168174

169175
pub type DynEmitter = dyn Emitter + DynSend;
170176

171-
/// Emitter trait for emitting errors.
177+
/// Emitter trait for emitting errors and other structured information.
172178
pub trait Emitter: Translate {
173179
/// Emit a structured diagnostic.
174180
fn emit_diagnostic(&mut self, diag: DiagInner, registry: &Registry);
@@ -177,6 +183,10 @@ pub trait Emitter: Translate {
177183
/// Currently only supported for the JSON format.
178184
fn emit_artifact_notification(&mut self, _path: &Path, _artifact_type: &str) {}
179185

186+
/// Emit a timestamp with start/end of a timing section.
187+
/// Currently only supported for the JSON format.
188+
fn emit_timing_section(&mut self, _record: TimingRecord, _event: TimingEvent) {}
189+
180190
/// Emit a report about future breakage.
181191
/// Currently only supported for the JSON format.
182192
fn emit_future_breakage_report(&mut self, _diags: Vec<DiagInner>, _registry: &Registry) {}

compiler/rustc_errors/src/json.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use termcolor::{ColorSpec, WriteColor};
2828
use crate::diagnostic::IsLint;
2929
use crate::emitter::{
3030
ColorConfig, Destination, Emitter, HumanEmitter, HumanReadableErrorType, OutputTheme,
31-
should_show_source_code,
31+
TimingEvent, should_show_source_code,
3232
};
3333
use crate::registry::Registry;
34+
use crate::timings::{TimingRecord, TimingSection};
3435
use crate::translation::{Translate, to_fluent_args};
3536
use crate::{
3637
CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, Subdiag, Suggestions,
@@ -104,6 +105,7 @@ impl JsonEmitter {
104105
enum EmitTyped<'a> {
105106
Diagnostic(Diagnostic),
106107
Artifact(ArtifactNotification<'a>),
108+
SectionTiming(SectionTimestamp<'a>),
107109
FutureIncompat(FutureIncompatReport<'a>),
108110
UnusedExtern(UnusedExterns<'a>),
109111
}
@@ -135,6 +137,21 @@ impl Emitter for JsonEmitter {
135137
}
136138
}
137139

140+
fn emit_timing_section(&mut self, record: TimingRecord, event: TimingEvent) {
141+
let event = match event {
142+
TimingEvent::Start => "start",
143+
TimingEvent::End => "end",
144+
};
145+
let name = match record.section {
146+
TimingSection::Linking => "link",
147+
};
148+
let data = SectionTimestamp { name, event, timestamp: record.timestamp };
149+
let result = self.emit(EmitTyped::SectionTiming(data));
150+
if let Err(e) = result {
151+
panic!("failed to print timing section: {e:?}");
152+
}
153+
}
154+
138155
fn emit_future_breakage_report(&mut self, diags: Vec<crate::DiagInner>, registry: &Registry) {
139156
let data: Vec<FutureBreakageItem<'_>> = diags
140157
.into_iter()
@@ -263,6 +280,16 @@ struct ArtifactNotification<'a> {
263280
emit: &'a str,
264281
}
265282

283+
#[derive(Serialize)]
284+
struct SectionTimestamp<'a> {
285+
/// Name of the section
286+
name: &'a str,
287+
/// Start/end of the section
288+
event: &'a str,
289+
/// Opaque timestamp.
290+
timestamp: u128,
291+
}
292+
266293
#[derive(Serialize)]
267294
struct FutureBreakageItem<'a> {
268295
// Always EmitTyped::Diagnostic, but we want to make sure it gets serialized

compiler/rustc_errors/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![allow(internal_features)]
88
#![allow(rustc::diagnostic_outside_of_impl)]
99
#![allow(rustc::untranslatable_diagnostic)]
10+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
1011
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1112
#![doc(rust_logo)]
1213
#![feature(array_windows)]
@@ -74,7 +75,9 @@ pub use snippet::Style;
7475
pub use termcolor::{Color, ColorSpec, WriteColor};
7576
use tracing::debug;
7677

78+
use crate::emitter::TimingEvent;
7779
use crate::registry::Registry;
80+
use crate::timings::TimingRecord;
7881

7982
pub mod annotate_snippet_emitter_writer;
8083
pub mod codes;
@@ -90,6 +93,7 @@ mod snippet;
9093
mod styled_buffer;
9194
#[cfg(test)]
9295
mod tests;
96+
pub mod timings;
9397
pub mod translation;
9498

9599
pub type PResult<'a, T> = Result<T, Diag<'a>>;
@@ -1156,6 +1160,14 @@ impl<'a> DiagCtxtHandle<'a> {
11561160
self.inner.borrow_mut().emitter.emit_artifact_notification(path, artifact_type);
11571161
}
11581162

1163+
pub fn emit_timing_section_start(&self, record: TimingRecord) {
1164+
self.inner.borrow_mut().emitter.emit_timing_section(record, TimingEvent::Start);
1165+
}
1166+
1167+
pub fn emit_timing_section_end(&self, record: TimingRecord) {
1168+
self.inner.borrow_mut().emitter.emit_timing_section(record, TimingEvent::End);
1169+
}
1170+
11591171
pub fn emit_future_breakage_report(&self) {
11601172
let inner = &mut *self.inner.borrow_mut();
11611173
let diags = std::mem::take(&mut inner.future_breakage_diagnostics);

compiler/rustc_errors/src/timings.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::time::Instant;
2+
3+
use crate::DiagCtxtHandle;
4+
5+
/// A high-level section of the compilation process.
6+
#[derive(Copy, Clone, Debug)]
7+
pub enum TimingSection {
8+
/// Time spent linking.
9+
Linking,
10+
}
11+
12+
/// Section with attached timestamp
13+
#[derive(Copy, Clone, Debug)]
14+
pub struct TimingRecord {
15+
pub section: TimingSection,
16+
/// Microseconds elapsed since some predetermined point in time (~start of the rustc process).
17+
pub timestamp: u128,
18+
}
19+
20+
impl TimingRecord {
21+
fn from_origin(origin: Instant, section: TimingSection) -> Self {
22+
Self { section, timestamp: Instant::now().duration_since(origin).as_micros() }
23+
}
24+
25+
pub fn section(&self) -> TimingSection {
26+
self.section
27+
}
28+
29+
pub fn timestamp(&self) -> u128 {
30+
self.timestamp
31+
}
32+
}
33+
34+
/// Manages emission of start/end section timings, enabled through `--json=timings`.
35+
pub struct TimingSectionHandler {
36+
/// Time when the compilation session started.
37+
/// If `None`, timing is disabled.
38+
origin: Option<Instant>,
39+
}
40+
41+
impl TimingSectionHandler {
42+
pub fn new(enabled: bool) -> Self {
43+
let origin = if enabled { Some(Instant::now()) } else { None };
44+
Self { origin }
45+
}
46+
47+
/// Returns a RAII guard that will immediately emit a start the provided section, and then emit
48+
/// its end when it is dropped.
49+
pub fn start_section<'a>(
50+
&self,
51+
diag_ctxt: DiagCtxtHandle<'a>,
52+
section: TimingSection,
53+
) -> TimingSectionGuard<'a> {
54+
TimingSectionGuard::create(diag_ctxt, section, self.origin)
55+
}
56+
}
57+
58+
/// RAII wrapper for starting and ending section timings.
59+
pub struct TimingSectionGuard<'a> {
60+
dcx: DiagCtxtHandle<'a>,
61+
section: TimingSection,
62+
origin: Option<Instant>,
63+
}
64+
65+
impl<'a> TimingSectionGuard<'a> {
66+
fn create(dcx: DiagCtxtHandle<'a>, section: TimingSection, origin: Option<Instant>) -> Self {
67+
if let Some(origin) = origin {
68+
dcx.emit_timing_section_start(TimingRecord::from_origin(origin, section));
69+
}
70+
Self { dcx, section, origin }
71+
}
72+
}
73+
74+
impl<'a> Drop for TimingSectionGuard<'a> {
75+
fn drop(&mut self) {
76+
if let Some(origin) = self.origin {
77+
self.dcx.emit_timing_section_end(TimingRecord::from_origin(origin, self.section));
78+
}
79+
}
80+
}

compiler/rustc_infer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#![allow(internal_features)]
1717
#![allow(rustc::diagnostic_outside_of_impl)]
1818
#![allow(rustc::untranslatable_diagnostic)]
19+
#![cfg_attr(not(bootstrap), allow(rustc::direct_use_of_rustc_type_ir))]
1920
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
2021
#![doc(rust_logo)]
2122
#![feature(assert_matches)]

compiler/rustc_interface/src/queries.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44
use rustc_codegen_ssa::CodegenResults;
55
use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::svh::Svh;
7+
use rustc_errors::timings::TimingSection;
78
use rustc_hir::def_id::LOCAL_CRATE;
89
use rustc_metadata::EncodedMetadata;
910
use rustc_middle::dep_graph::DepGraph;
@@ -88,6 +89,7 @@ impl Linker {
8889
}
8990

9091
let _timer = sess.prof.verbose_generic_activity("link_crate");
92+
let _timing = sess.timings.start_section(sess.dcx(), TimingSection::Linking);
9193
codegen_backend.link(sess, codegen_results, self.metadata, &self.output_filenames)
9294
}
9395
}

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ fn test_unstable_options_tracking_hash() {
802802
tracked!(force_unstable_if_unmarked, true);
803803
tracked!(function_return, FunctionReturn::ThunkExtern);
804804
tracked!(function_sections, Some(false));
805+
tracked!(hint_mostly_unused, true);
805806
tracked!(human_readable_cgu_names, true);
806807
tracked!(incremental_ignore_spans, true);
807808
tracked!(inline_mir, Some(true));

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ lint_tykind = usage of `ty::TyKind`
812812
lint_tykind_kind = usage of `ty::TyKind::<kind>`
813813
.suggestion = try using `ty::<kind>` directly
814814
815+
lint_type_ir_direct_use = do not use `rustc_type_ir` unless you are implementing type system internals
816+
.note = use `rustc_middle::ty` instead
817+
815818
lint_type_ir_inherent_usage = do not use `rustc_type_ir::inherent` unless you're inside of the trait solver
816819
.note = the method or struct you're looking for is likely defined somewhere else downstream in the compiler
817820

compiler/rustc_lint/src/internal.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use {rustc_ast as ast, rustc_hir as hir};
1414
use crate::lints::{
1515
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
1616
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag,
17-
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrInherentUsage,
18-
TypeIrTraitUsage, UntranslatableDiag,
17+
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrDirectUse,
18+
TypeIrInherentUsage, TypeIrTraitUsage, UntranslatableDiag,
1919
};
2020
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2121

@@ -301,8 +301,18 @@ declare_tool_lint! {
301301
"usage `rustc_type_ir`-specific abstraction traits outside of trait system",
302302
report_in_external_macro: true
303303
}
304+
declare_tool_lint! {
305+
/// The `direct_use_of_rustc_type_ir` lint detects usage of `rustc_type_ir`.
306+
///
307+
/// This module should only be used within the trait solver and some desirable
308+
/// crates like rustc_middle.
309+
pub rustc::DIRECT_USE_OF_RUSTC_TYPE_IR,
310+
Allow,
311+
"usage `rustc_type_ir` abstraction outside of trait system",
312+
report_in_external_macro: true
313+
}
304314

305-
declare_lint_pass!(TypeIr => [NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]);
315+
declare_lint_pass!(TypeIr => [DIRECT_USE_OF_RUSTC_TYPE_IR, NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_INHERENT, USAGE_OF_TYPE_IR_TRAITS]);
306316

307317
impl<'tcx> LateLintPass<'tcx> for TypeIr {
308318
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
@@ -372,6 +382,21 @@ impl<'tcx> LateLintPass<'tcx> for TypeIr {
372382
NonGlobImportTypeIrInherent { suggestion: lo.eq_ctxt(hi).then(|| lo.to(hi)), snippet },
373383
);
374384
}
385+
386+
fn check_path(
387+
&mut self,
388+
cx: &LateContext<'tcx>,
389+
path: &rustc_hir::Path<'tcx>,
390+
_: rustc_hir::HirId,
391+
) {
392+
if let Some(seg) = path.segments.iter().find(|seg| {
393+
seg.res
394+
.opt_def_id()
395+
.is_some_and(|def_id| cx.tcx.is_diagnostic_item(sym::type_ir, def_id))
396+
}) {
397+
cx.emit_span_lint(DIRECT_USE_OF_RUSTC_TYPE_IR, seg.ident.span, TypeIrDirectUse);
398+
}
399+
}
375400
}
376401

377402
declare_tool_lint! {

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ fn register_internals(store: &mut LintStore) {
668668
LintId::of(USAGE_OF_TYPE_IR_TRAITS),
669669
LintId::of(BAD_OPT_ACCESS),
670670
LintId::of(SPAN_USE_EQ_CTXT),
671+
LintId::of(DIRECT_USE_OF_RUSTC_TYPE_IR),
671672
],
672673
);
673674
}

compiler/rustc_lint/src/lints.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,11 @@ pub(crate) struct TypeIrInherentUsage;
969969
#[note]
970970
pub(crate) struct TypeIrTraitUsage;
971971

972+
#[derive(LintDiagnostic)]
973+
#[diag(lint_type_ir_direct_use)]
974+
#[note]
975+
pub(crate) struct TypeIrDirectUse;
976+
972977
#[derive(LintDiagnostic)]
973978
#[diag(lint_non_glob_import_type_ir_inherent)]
974979
pub(crate) struct NonGlobImportTypeIrInherent {

0 commit comments

Comments
 (0)