Skip to content

Commit cd4c352

Browse files
committed
Reorder check_item_type diagnostics so they occur next to the corresponding check_well_formed diagnostics
1 parent dd2dee1 commit cd4c352

36 files changed

+339
-262
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_attr as attr;
88
use rustc_errors::{ErrorGuaranteed, MultiSpan};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{CtorKind, DefKind};
11-
use rustc_hir::def_id::LocalModDefId;
11+
use rustc_hir::def_id::{DefId, LocalDefId};
1212
use rustc_hir::Node;
1313
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
1414
use rustc_infer::traits::{Obligation, TraitEngineExt as _};
@@ -440,7 +440,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
440440
}
441441
}
442442

443-
fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
443+
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
444444
let _indenter = indenter();
445445
match tcx.def_kind(def_id) {
446446
DefKind::Static(..) => {
@@ -458,11 +458,7 @@ fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
458458
DefKind::Fn => {} // entirely within check_item_body
459459
DefKind::Impl { of_trait } => {
460460
if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(def_id) {
461-
check_impl_items_against_trait(
462-
tcx,
463-
def_id,
464-
impl_trait_ref.instantiate_identity(),
465-
);
461+
check_impl_items_against_trait(tcx, def_id, impl_trait_ref.instantiate_identity());
466462
check_on_unimplemented(tcx, def_id);
467463
}
468464
}
@@ -1304,16 +1300,6 @@ pub(super) fn check_type_params_are_used<'tcx>(
13041300
}
13051301
}
13061302

1307-
pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
1308-
let module = tcx.hir_module_items(module_def_id);
1309-
for id in module.items() {
1310-
check_item_type(tcx, id.owner_id.def_id);
1311-
}
1312-
if module_def_id == LocalModDefId::CRATE_DEF_ID {
1313-
super::entry::check_for_entry_fn(tcx);
1314-
}
1315-
}
1316-
13171303
fn async_opaque_type_cycle_error(tcx: TyCtxt<'_>, span: Span) -> ErrorGuaranteed {
13181304
struct_span_err!(tcx.dcx(), span, E0733, "recursion in an `async fn` requires boxing")
13191305
.span_label(span, "recursive `async fn`")

compiler/rustc_hir_analysis/src/check/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::ops::Not;
1414
use super::check_function_signature;
1515
use crate::errors;
1616

17-
pub(crate) fn check_for_entry_fn(tcx: TyCtxt<'_>) {
17+
pub(crate) fn check_for_entry_fn(tcx: TyCtxt<'_>, (): ()) {
1818
match tcx.entry_fn(()) {
1919
Some((def_id, EntryFnType::Main { .. })) => check_main_fn_ty(tcx, def_id),
2020
Some((def_id, EntryFnType::Start)) => check_start_fn_ty(tcx, def_id),

compiler/rustc_hir_analysis/src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub use check::check_abi;
7575

7676
use std::num::NonZeroU32;
7777

78-
use check::check_mod_item_types;
78+
use entry::check_for_entry_fn;
7979
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8080
use rustc_errors::ErrorGuaranteed;
8181
use rustc_errors::{pluralize, struct_span_err, Diagnostic, DiagnosticBuilder};
@@ -110,7 +110,7 @@ pub fn provide(providers: &mut Providers) {
110110
wfcheck::provide(providers);
111111
*providers = Providers {
112112
adt_destructor,
113-
check_mod_item_types,
113+
check_for_entry_fn,
114114
region_scope_tree,
115115
collect_return_position_impl_trait_in_trait_tys,
116116
compare_impl_const: compare_impl_item::compare_impl_const_raw,

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
172172
item.name = ? tcx.def_path_str(def_id)
173173
);
174174

175-
match item.kind {
175+
let res = match item.kind {
176176
// Right now we check that every default trait implementation
177177
// has an implementation of itself. Basically, a case like:
178178
//
@@ -271,7 +271,11 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
271271
}
272272
}
273273
_ => Ok(()),
274-
}
274+
};
275+
276+
crate::check::check::check_item_type(tcx, def_id);
277+
278+
res
275279
}
276280

277281
fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) -> Result<(), ErrorGuaranteed> {

compiler/rustc_hir_analysis/src/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
204204
tcx.hir().try_par_for_each_module(|module| tcx.ensure().check_mod_type_wf(module))
205205
});
206206

207-
// NOTE: This is copy/pasted in librustdoc/core.rs and should be kept in sync.
208-
tcx.sess.time("item_types_checking", || {
209-
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_item_types(module))
210-
});
207+
tcx.sess.time("entry_fn_checks", || tcx.ensure().check_for_entry_fn(()));
211208

212-
// HACK: `check_mod_type_wf` may spuriously emit errors due to `span_delayed_bug`, even if
213-
// those errors only actually get emitted in `check_mod_item_types`.
209+
// HACK: `check_for_entry_fn` wants to report its errors even if `check_mod_type_wf` has errored.
214210
errs?;
215211

216212
if tcx.features().rustc_attrs {

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,8 @@ rustc_queries! {
938938
desc { |tcx| "checking naked functions in {}", describe_as_module(key, tcx) }
939939
}
940940

941-
query check_mod_item_types(key: LocalModDefId) -> () {
942-
desc { |tcx| "checking item types in {}", describe_as_module(key, tcx) }
941+
query check_for_entry_fn(key: ()) -> () {
942+
desc { |_tcx| "checking entry functions" }
943943
}
944944

945945
query check_mod_privacy(key: LocalModDefId) -> () {

src/librustdoc/core.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ pub(crate) fn run_global_ctxt(
323323
tcx.hir().try_par_for_each_module(|module| tcx.ensure().check_mod_type_wf(module))
324324
});
325325
tcx.sess.time("item_types_checking", || {
326-
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_item_types(module))
326+
tcx.hir().for_each_module(|module| {
327+
let _ = tcx.ensure().check_mod_type_wf(module);
328+
});
327329
});
328330

329331
tcx.dcx().abort_if_errors();

tests/ui/associated-types/associated-types-no-suitable-supertrait.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
error[E0277]: the trait bound `(T, U): Get` is not satisfied
2+
--> $DIR/associated-types-no-suitable-supertrait.rs:22:5
3+
|
4+
LL | fn uhoh<U:Get>(&self, foo: U, bar: <(T, U) as Get>::Value) {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `(T, U)`
6+
|
7+
help: this trait has no implementations, consider adding one
8+
--> $DIR/associated-types-no-suitable-supertrait.rs:12:1
9+
|
10+
LL | trait Get {
11+
| ^^^^^^^^^
12+
113
error[E0277]: the trait bound `(T, U): Get` is not satisfied
214
--> $DIR/associated-types-no-suitable-supertrait.rs:22:40
315
|
@@ -21,18 +33,6 @@ help: consider further restricting `Self`
2133
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Get {}
2234
| +++++++++++++++
2335

24-
error[E0277]: the trait bound `(T, U): Get` is not satisfied
25-
--> $DIR/associated-types-no-suitable-supertrait.rs:22:5
26-
|
27-
LL | fn uhoh<U:Get>(&self, foo: U, bar: <(T, U) as Get>::Value) {}
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `(T, U)`
29-
|
30-
help: this trait has no implementations, consider adding one
31-
--> $DIR/associated-types-no-suitable-supertrait.rs:12:1
32-
|
33-
LL | trait Get {
34-
| ^^^^^^^^^
35-
3636
error: aborting due to 3 previous errors
3737

3838
For more information about this error, try `rustc --explain E0277`.

tests/ui/associated-types/defaults-cyclic-fail-1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ impl Tr for u32 {
2424
// ...but not in an impl that redefines one of the types.
2525
impl Tr for bool {
2626
type A = Box<Self::B>;
27-
//~^ ERROR overflow evaluating the requirement `<bool as Tr>::B == _`
27+
//~^ ERROR overflow evaluating the requirement `<bool as Tr>::A == _`
2828
}
2929
// (the error is shown twice for some reason)
3030

3131
impl Tr for usize {
3232
type B = &'static Self::A;
33-
//~^ ERROR overflow evaluating the requirement `<usize as Tr>::A == _`
33+
//~^ ERROR overflow evaluating the requirement `<usize as Tr>::B == _`
3434
}
3535

3636
fn main() {

tests/ui/associated-types/defaults-cyclic-fail-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B == _`
1+
error[E0275]: overflow evaluating the requirement `<bool as Tr>::A == _`
22
--> $DIR/defaults-cyclic-fail-1.rs:26:14
33
|
44
LL | type A = Box<Self::B>;
55
| ^^^^^^^^^^^^
66

7-
error[E0275]: overflow evaluating the requirement `<usize as Tr>::A == _`
7+
error[E0275]: overflow evaluating the requirement `<usize as Tr>::B == _`
88
--> $DIR/defaults-cyclic-fail-1.rs:32:14
99
|
1010
LL | type B = &'static Self::A;

tests/ui/associated-types/defaults-cyclic-fail-2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ impl Tr for u32 {
2525

2626
impl Tr for bool {
2727
type A = Box<Self::B>;
28-
//~^ ERROR overflow evaluating the requirement `<bool as Tr>::B == _`
28+
//~^ ERROR overflow evaluating the requirement `<bool as Tr>::A == _`
2929
}
3030
// (the error is shown twice for some reason)
3131

3232
impl Tr for usize {
3333
type B = &'static Self::A;
34-
//~^ ERROR overflow evaluating the requirement `<usize as Tr>::A == _`
34+
//~^ ERROR overflow evaluating the requirement `<usize as Tr>::B == _`
3535
}
3636

3737
fn main() {

tests/ui/associated-types/defaults-cyclic-fail-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B == _`
1+
error[E0275]: overflow evaluating the requirement `<bool as Tr>::A == _`
22
--> $DIR/defaults-cyclic-fail-2.rs:27:14
33
|
44
LL | type A = Box<Self::B>;
55
| ^^^^^^^^^^^^
66

7-
error[E0275]: overflow evaluating the requirement `<usize as Tr>::A == _`
7+
error[E0275]: overflow evaluating the requirement `<usize as Tr>::B == _`
88
--> $DIR/defaults-cyclic-fail-2.rs:33:14
99
|
1010
LL | type B = &'static Self::A;

tests/ui/async-await/issue-66312.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-66312.rs:9:8
3+
|
4+
LL | if x.is_some() {
5+
| ^^^^^^^^^^^ expected `bool`, found `()`
6+
17
error[E0307]: invalid `self` parameter type: T
28
--> $DIR/issue-66312.rs:4:22
39
|
@@ -7,12 +13,6 @@ LL | fn is_some(self: T);
713
= note: type of `self` must be `Self` or a type that dereferences to it
814
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
915

10-
error[E0308]: mismatched types
11-
--> $DIR/issue-66312.rs:9:8
12-
|
13-
LL | if x.is_some() {
14-
| ^^^^^^^^^^^ expected `bool`, found `()`
15-
1616
error: aborting due to 2 previous errors
1717

1818
Some errors have detailed explanations: E0307, E0308.

tests/ui/const-generics/issues/issue-83765.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
trait TensorDimension {
55
const DIM: usize;
66
//~^ ERROR cycle detected when resolving instance
7+
//~| ERROR cycle detected when resolving instance
78
// FIXME Given the current state of the compiler its expected that we cycle here,
89
// but the cycle is still wrong.
910
const ISSCALAR: bool = Self::DIM == 0;
@@ -79,6 +80,7 @@ impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> TensorSi
7980
for BMap<'a, R, T, F, DIM>
8081
{
8182
fn size(&self) -> [usize; DIM] {
83+
//~^ ERROR: method not compatible with trait
8284
self.reference.size()
8385
}
8486
}
@@ -88,6 +90,7 @@ impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> Broadcas
8890
{
8991
type Element = R;
9092
fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
93+
//~^ ERROR: method not compatible with trait
9194
self.reference.bget(index).map(&self.closure)
9295
}
9396
}

tests/ui/const-generics/issues/issue-83765.stderr

+40-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,44 @@ LL | trait TensorDimension {
1717
| ^^^^^^^^^^^^^^^^^^^^^
1818
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
1919

20-
error: aborting due to 1 previous error
20+
error[E0391]: cycle detected when resolving instance `<LazyUpdim<'_, T, <T as TensorDimension>::DIM, DIM> as TensorDimension>::DIM`
21+
--> $DIR/issue-83765.rs:5:5
22+
|
23+
LL | const DIM: usize;
24+
| ^^^^^^^^^^^^^^^^
25+
|
26+
note: ...which requires computing candidate for `<LazyUpdim<'_, T, <T as TensorDimension>::DIM, DIM> as TensorDimension>`...
27+
--> $DIR/issue-83765.rs:4:1
28+
|
29+
LL | trait TensorDimension {
30+
| ^^^^^^^^^^^^^^^^^^^^^
31+
= note: ...which again requires resolving instance `<LazyUpdim<'_, T, <T as TensorDimension>::DIM, DIM> as TensorDimension>::DIM`, completing the cycle
32+
note: cycle used when checking that `<impl at $DIR/issue-83765.rs:56:1: 56:97>` is well-formed
33+
--> $DIR/issue-83765.rs:56:1
34+
|
35+
LL | impl<'a, T: Broadcastable, const DIM: usize> Broadcastable for LazyUpdim<'a, T, { T::DIM }, DIM> {
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
38+
39+
error[E0308]: method not compatible with trait
40+
--> $DIR/issue-83765.rs:82:5
41+
|
42+
LL | fn size(&self) -> [usize; DIM] {
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
44+
|
45+
= note: expected constant `Self::DIM`
46+
found constant `DIM`
47+
48+
error[E0308]: method not compatible with trait
49+
--> $DIR/issue-83765.rs:92:5
50+
|
51+
LL | fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
52+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
53+
|
54+
= note: expected constant `Self::DIM`
55+
found constant `DIM`
56+
57+
error: aborting due to 4 previous errors
2158

22-
For more information about this error, try `rustc --explain E0391`.
59+
Some errors have detailed explanations: E0308, E0391.
60+
For more information about an error, try `rustc --explain E0308`.

tests/ui/consts/const-unsized.stderr

+23-23
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,23 @@ LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
88

9-
error[E0277]: the size for values of type `str` cannot be known at compilation time
10-
--> $DIR/const-unsized.rs:7:18
11-
|
12-
LL | const CONST_FOO: str = *"foo";
13-
| ^^^ doesn't have a size known at compile-time
14-
|
15-
= help: the trait `Sized` is not implemented for `str`
16-
179
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
18-
--> $DIR/const-unsized.rs:11:18
10+
--> $DIR/const-unsized.rs:3:35
1911
|
20-
LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
21-
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
12+
LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
2214
|
2315
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
16+
= note: constant expressions must have a statically known size
2417

2518
error[E0277]: the size for values of type `str` cannot be known at compilation time
26-
--> $DIR/const-unsized.rs:15:20
19+
--> $DIR/const-unsized.rs:7:18
2720
|
28-
LL | static STATIC_BAR: str = *"bar";
29-
| ^^^ doesn't have a size known at compile-time
21+
LL | const CONST_FOO: str = *"foo";
22+
| ^^^ doesn't have a size known at compile-time
3023
|
3124
= help: the trait `Sized` is not implemented for `str`
3225

33-
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
34-
--> $DIR/const-unsized.rs:3:35
35-
|
36-
LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
38-
|
39-
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
40-
= note: constant expressions must have a statically known size
41-
4226
error[E0277]: the size for values of type `str` cannot be known at compilation time
4327
--> $DIR/const-unsized.rs:7:24
4428
|
@@ -48,6 +32,14 @@ LL | const CONST_FOO: str = *"foo";
4832
= help: the trait `Sized` is not implemented for `str`
4933
= note: constant expressions must have a statically known size
5034

35+
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
36+
--> $DIR/const-unsized.rs:11:18
37+
|
38+
LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
39+
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
40+
|
41+
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
42+
5143
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
5244
--> $DIR/const-unsized.rs:11:37
5345
|
@@ -57,6 +49,14 @@ LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
5749
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
5850
= note: constant expressions must have a statically known size
5951

52+
error[E0277]: the size for values of type `str` cannot be known at compilation time
53+
--> $DIR/const-unsized.rs:15:20
54+
|
55+
LL | static STATIC_BAR: str = *"bar";
56+
| ^^^ doesn't have a size known at compile-time
57+
|
58+
= help: the trait `Sized` is not implemented for `str`
59+
6060
error[E0277]: the size for values of type `str` cannot be known at compilation time
6161
--> $DIR/const-unsized.rs:15:26
6262
|

0 commit comments

Comments
 (0)