Skip to content

Commit 3ed2cd7

Browse files
authoredJun 22, 2024
Rollup merge of #126686 - fmease:dump-preds-n-item-bounds, r=compiler-errors
Add `#[rustc_dump_{predicates,item_bounds}]` Conflicts with #126668. As discussed r? compiler-errors CC ``@fee1-dead``
2 parents 07e8b3a + 38bd7a0 commit 3ed2cd7

File tree

17 files changed

+187
-104
lines changed

17 files changed

+187
-104
lines changed
 

‎compiler/rustc_feature/src/builtin_attrs.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,14 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10881088
ErrorFollowing, EncodeCrossCrate::No,
10891089
"the `#[custom_mir]` attribute is just used for the Rust test suite",
10901090
),
1091+
rustc_attr!(
1092+
TEST, rustc_dump_item_bounds, Normal, template!(Word),
1093+
WarnFollowing, EncodeCrossCrate::No
1094+
),
1095+
rustc_attr!(
1096+
TEST, rustc_dump_predicates, Normal, template!(Word),
1097+
WarnFollowing, EncodeCrossCrate::No
1098+
),
10911099
rustc_attr!(
10921100
TEST, rustc_object_lifetime_default, Normal, template!(Word),
10931101
WarnFollowing, EncodeCrossCrate::No

‎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,43 @@
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+
}
19+
20+
pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
21+
for id in tcx.hir_crate_items(()).owners() {
22+
if tcx.has_attr(id, sym::rustc_dump_predicates) {
23+
let preds = tcx.predicates_of(id).instantiate_identity(tcx).predicates;
24+
let span = tcx.def_span(id);
25+
26+
let mut diag = tcx.dcx().struct_span_err(span, sym::rustc_dump_predicates.as_str());
27+
for pred in preds {
28+
diag.note(format!("{pred:?}"));
29+
}
30+
diag.emit();
31+
}
32+
if tcx.has_attr(id, sym::rustc_dump_item_bounds) {
33+
let bounds = tcx.item_bounds(id).instantiate_identity();
34+
let span = tcx.def_span(id);
35+
36+
let mut diag = tcx.dcx().struct_span_err(span, sym::rustc_dump_item_bounds.as_str());
37+
for bound in bounds {
38+
diag.note(format!("{bound:?}"));
39+
}
40+
diag.emit();
41+
}
42+
}
43+
}

‎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

+4-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,10 @@ 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);
171+
collect::dump::predicates_and_item_bounds(tcx);
177172
}
178173

179174
// 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.

‎compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,8 @@ symbols! {
15931593
rustc_do_not_const_check,
15941594
rustc_doc_primitive,
15951595
rustc_dummy,
1596+
rustc_dump_item_bounds,
1597+
rustc_dump_predicates,
15961598
rustc_dump_user_args,
15971599
rustc_dump_vtable,
15981600
rustc_effective_visibility,

‎tests/ui/attributes/dump-preds.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ normalize-stderr-test "DefId\(.+?\)" -> "DefId(..)"
2+
3+
#![feature(rustc_attrs)]
4+
5+
#[rustc_dump_predicates]
6+
trait Trait<T>: Iterator<Item: Copy>
7+
//~^ ERROR rustc_dump_predicates
8+
where
9+
String: From<T>
10+
{
11+
#[rustc_dump_predicates]
12+
#[rustc_dump_item_bounds]
13+
type Assoc<P: Eq>: std::ops::Deref<Target = ()>
14+
//~^ ERROR rustc_dump_predicates
15+
//~| ERROR rustc_dump_item_bounds
16+
where
17+
Self::Assoc<()>: Copy;
18+
}
19+
20+
fn main() {}

‎tests/ui/attributes/dump-preds.stderr

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: rustc_dump_predicates
2+
--> $DIR/dump-preds.rs:6:1
3+
|
4+
LL | trait Trait<T>: Iterator<Item: Copy>
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: Binder { value: TraitPredicate(<Self as std::iter::Iterator>, polarity:Positive), bound_vars: [] }
8+
= note: Binder { value: TraitPredicate(<<Self as std::iter::Iterator>::Item as std::marker::Copy>, polarity:Positive), bound_vars: [] }
9+
= note: Binder { value: TraitPredicate(<T as std::marker::Sized>, polarity:Positive), bound_vars: [] }
10+
= note: Binder { value: TraitPredicate(<std::string::String as std::convert::From<T>>, polarity:Positive), bound_vars: [] }
11+
= note: Binder { value: TraitPredicate(<Self as Trait<T>>, polarity:Positive), bound_vars: [] }
12+
13+
error: rustc_dump_predicates
14+
--> $DIR/dump-preds.rs:13:5
15+
|
16+
LL | type Assoc<P: Eq>: std::ops::Deref<Target = ()>
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
= note: Binder { value: TraitPredicate(<Self as std::iter::Iterator>, polarity:Positive), bound_vars: [] }
20+
= note: Binder { value: TraitPredicate(<<Self as std::iter::Iterator>::Item as std::marker::Copy>, polarity:Positive), bound_vars: [] }
21+
= note: Binder { value: TraitPredicate(<T as std::marker::Sized>, polarity:Positive), bound_vars: [] }
22+
= note: Binder { value: TraitPredicate(<std::string::String as std::convert::From<T>>, polarity:Positive), bound_vars: [] }
23+
= note: Binder { value: TraitPredicate(<Self as Trait<T>>, polarity:Positive), bound_vars: [] }
24+
= note: Binder { value: TraitPredicate(<P as std::marker::Sized>, polarity:Positive), bound_vars: [] }
25+
= note: Binder { value: TraitPredicate(<P as std::cmp::Eq>, polarity:Positive), bound_vars: [] }
26+
= note: Binder { value: TraitPredicate(<<Self as Trait<T>>::Assoc<()> as std::marker::Copy>, polarity:Positive), bound_vars: [] }
27+
28+
error: rustc_dump_item_bounds
29+
--> $DIR/dump-preds.rs:13:5
30+
|
31+
LL | type Assoc<P: Eq>: std::ops::Deref<Target = ()>
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
|
34+
= note: Binder { value: ProjectionPredicate(AliasTerm { args: [Alias(Projection, AliasTy { args: [Self/#0, T/#1, P/#2], def_id: DefId(..) })], def_id: DefId(..) }, Term::Ty(())), bound_vars: [] }
35+
= note: Binder { value: TraitPredicate(<<Self as Trait<T>>::Assoc<P> as std::ops::Deref>, polarity:Positive), bound_vars: [] }
36+
= note: Binder { value: TraitPredicate(<<Self as Trait<T>>::Assoc<P> as std::marker::Sized>, polarity:Positive), bound_vars: [] }
37+
38+
error: aborting due to 3 previous errors
39+

0 commit comments

Comments
 (0)