Skip to content

Commit bc12972

Browse files
committed
Slightly refactor the dumping of HIR analysis data
1 parent cb8a7ea commit bc12972

File tree

13 files changed

+92
-104
lines changed

13 files changed

+92
-104
lines changed

compiler/rustc_hir_analysis/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ hir_analysis_ty_param_some = type parameter `{$param}` must be used as the type
510510
.note = implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
511511
.only_note = only traits defined in the current crate can be implemented for a type parameter
512512
513-
hir_analysis_type_of = {$type_of}
513+
hir_analysis_type_of = {$ty}
514514
515515
hir_analysis_typeof_reserved_keyword_used =
516516
`typeof` is a reserved keyword but unimplemented
@@ -566,7 +566,7 @@ hir_analysis_value_of_associated_struct_already_specified =
566566
hir_analysis_variadic_function_compatible_convention = C-variadic function must have a compatible calling convention, like {$conventions}
567567
.label = C-variadic function must have a compatible calling convention
568568
569-
hir_analysis_variances_of = {$variances_of}
569+
hir_analysis_variances_of = {$variances}
570570
571571
hir_analysis_where_clause_on_main = `main` function is not allowed to have a `where` clause
572572
.label = `main` cannot have a `where` clause

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use std::ops::Bound;
4545
use crate::check::intrinsic::intrinsic_operation_unsafety;
4646
use crate::errors;
4747
use crate::hir_ty_lowering::{HirTyLowerer, RegionInferReason};
48-
pub use type_of::test_opaque_hidden_types;
4948

49+
pub(crate) mod dump;
5050
mod generics_of;
5151
mod item_bounds;
5252
mod predicates_of;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use rustc_hir::def::DefKind;
2+
use rustc_hir::def_id::CRATE_DEF_ID;
3+
use rustc_middle::ty::TyCtxt;
4+
use rustc_span::sym;
5+
6+
pub(crate) fn opaque_hidden_types(tcx: TyCtxt<'_>) {
7+
if !tcx.has_attr(CRATE_DEF_ID, sym::rustc_hidden_type_of_opaques) {
8+
return;
9+
}
10+
11+
for id in tcx.hir().items() {
12+
let DefKind::OpaqueTy = tcx.def_kind(id.owner_id) else { continue };
13+
14+
let ty = tcx.type_of(id.owner_id).instantiate_identity();
15+
16+
tcx.dcx().emit_err(crate::errors::TypeOf { span: tcx.def_span(id.owner_id), ty });
17+
}
18+
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::errors::TypeofReservedKeywordUsed;
1515

1616
use super::bad_placeholder;
1717
use super::ItemCtxt;
18-
pub use opaque::test_opaque_hidden_types;
1918

2019
mod opaque;
2120

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
11
use rustc_errors::StashKey;
22
use rustc_hir::def::DefKind;
3-
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
3+
use rustc_hir::def_id::LocalDefId;
44
use rustc_hir::intravisit::{self, Visitor};
55
use rustc_hir::{self as hir, def, Expr, ImplItem, Item, Node, TraitItem};
66
use rustc_middle::bug;
77
use rustc_middle::hir::nested_filter;
88
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
9-
use rustc_span::{sym, ErrorGuaranteed, DUMMY_SP};
9+
use rustc_span::DUMMY_SP;
1010

11-
use crate::errors::{TaitForwardCompat, TaitForwardCompat2, TypeOf, UnconstrainedOpaqueType};
12-
13-
pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
14-
let mut res = Ok(());
15-
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_hidden_type_of_opaques) {
16-
for id in tcx.hir().items() {
17-
if matches!(tcx.def_kind(id.owner_id), DefKind::OpaqueTy) {
18-
let type_of = tcx.type_of(id.owner_id).instantiate_identity();
19-
20-
res = Err(tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of }));
21-
}
22-
}
23-
}
24-
res
25-
}
11+
use crate::errors::{TaitForwardCompat, TaitForwardCompat2, UnconstrainedOpaqueType};
2612

2713
/// Checks "defining uses" of opaque `impl Trait` in associated types.
2814
/// These can only be defined by associated items of the same trait.

compiler/rustc_hir_analysis/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -682,15 +682,15 @@ pub(crate) enum CannotCaptureLateBound {
682682
pub(crate) struct VariancesOf {
683683
#[primary_span]
684684
pub span: Span,
685-
pub variances_of: String,
685+
pub variances: String,
686686
}
687687

688688
#[derive(Diagnostic)]
689689
#[diag(hir_analysis_type_of)]
690690
pub(crate) struct TypeOf<'tcx> {
691691
#[primary_span]
692692
pub span: Span,
693-
pub type_of: Ty<'tcx>,
693+
pub ty: Ty<'tcx>,
694694
}
695695

696696
#[derive(Diagnostic)]

compiler/rustc_hir_analysis/src/lib.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ pub fn provide(providers: &mut Providers) {
151151
pub fn check_crate(tcx: TyCtxt<'_>) {
152152
let _prof_timer = tcx.sess.timer("type_check_crate");
153153

154-
if tcx.features().rustc_attrs {
155-
let _ = tcx.sess.time("outlives_testing", || outlives::test::test_inferred_outlives(tcx));
156-
}
157-
158154
tcx.sess.time("coherence_checking", || {
159155
tcx.hir().par_for_each_module(|module| {
160156
let _ = tcx.ensure().check_mod_type_wf(module);
@@ -169,11 +165,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
169165
});
170166

171167
if tcx.features().rustc_attrs {
172-
let _ = tcx.sess.time("variance_testing", || variance::test::test_variance(tcx));
173-
}
174-
175-
if tcx.features().rustc_attrs {
176-
let _ = collect::test_opaque_hidden_types(tcx);
168+
tcx.sess.time("outlives_dumping", || outlives::dump::inferred_outlives(tcx));
169+
tcx.sess.time("variance_dumping", || variance::dump::variances(tcx));
170+
collect::dump::opaque_hidden_types(tcx);
177171
}
178172

179173
// Make sure we evaluate all static and (non-associated) const items, even if unused.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_middle::bug;
2+
use rustc_middle::ty::{self, TyCtxt};
3+
use rustc_span::sym;
4+
5+
pub(crate) fn inferred_outlives(tcx: TyCtxt<'_>) {
6+
for id in tcx.hir().items() {
7+
if !tcx.has_attr(id.owner_id, sym::rustc_outlives) {
8+
continue;
9+
}
10+
11+
let preds = tcx.inferred_outlives_of(id.owner_id);
12+
let mut preds: Vec<_> = preds
13+
.iter()
14+
.map(|(pred, _)| match pred.kind().skip_binder() {
15+
ty::ClauseKind::RegionOutlives(p) => p.to_string(),
16+
ty::ClauseKind::TypeOutlives(p) => p.to_string(),
17+
err => bug!("unexpected clause {:?}", err),
18+
})
19+
.collect();
20+
preds.sort();
21+
22+
let span = tcx.def_span(id.owner_id);
23+
let mut err = tcx.dcx().struct_span_err(span, sym::rustc_outlives.as_str());
24+
for pred in preds {
25+
err.note(pred);
26+
}
27+
err.emit();
28+
}
29+
}

compiler/rustc_hir_analysis/src/outlives/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use rustc_middle::ty::GenericArgKind;
55
use rustc_middle::ty::{self, CratePredicatesMap, TyCtxt, Upcast};
66
use rustc_span::Span;
77

8+
pub(crate) mod dump;
89
mod explicit;
910
mod implicit_infer;
10-
/// Code to write unit test for outlives.
11-
pub mod test;
1211
mod utils;
1312

1413
pub fn provide(providers: &mut Providers) {

compiler/rustc_hir_analysis/src/outlives/test.rs

-31
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use rustc_hir::def::DefKind;
2+
use rustc_hir::def_id::CRATE_DEF_ID;
3+
use rustc_middle::ty::TyCtxt;
4+
use rustc_span::symbol::sym;
5+
6+
pub(crate) fn variances(tcx: TyCtxt<'_>) {
7+
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_variance_of_opaques) {
8+
for id in tcx.hir().items() {
9+
let DefKind::OpaqueTy = tcx.def_kind(id.owner_id) else { continue };
10+
11+
let variances = tcx.variances_of(id.owner_id);
12+
13+
tcx.dcx().emit_err(crate::errors::VariancesOf {
14+
span: tcx.def_span(id.owner_id),
15+
variances: format!("{variances:?}"),
16+
});
17+
}
18+
}
19+
20+
for id in tcx.hir().items() {
21+
if !tcx.has_attr(id.owner_id, sym::rustc_variance) {
22+
continue;
23+
}
24+
25+
let variances = tcx.variances_of(id.owner_id);
26+
27+
tcx.dcx().emit_err(crate::errors::VariancesOf {
28+
span: tcx.def_span(id.owner_id),
29+
variances: format!("{variances:?}"),
30+
});
31+
}
32+
}

compiler/rustc_hir_analysis/src/variance/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ mod constraints;
2222
/// Code to solve constraints and write out the results.
2323
mod solve;
2424

25-
/// Code to write unit tests of variance.
26-
pub mod test;
25+
pub(crate) mod dump;
2726

2827
/// Code for transforming variances.
2928
mod xform;

compiler/rustc_hir_analysis/src/variance/test.rs

-37
This file was deleted.

0 commit comments

Comments
 (0)