Skip to content

Commit

Permalink
Reorder check_item_type diagnostics so they occur next to the corre…
Browse files Browse the repository at this point in the history
…sponding `check_well_formed` diagnostics
  • Loading branch information
oli-obk committed Oct 26, 2023
1 parent b4e16c3 commit 3cd5671
Show file tree
Hide file tree
Showing 33 changed files with 331 additions and 245 deletions.
14 changes: 2 additions & 12 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_attr as attr;
use rustc_errors::{ErrorGuaranteed, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::Node;
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
Expand Down Expand Up @@ -443,7 +443,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
}
}

fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let _indenter = indenter();
match tcx.def_kind(def_id) {
DefKind::Static(..) => {
Expand Down Expand Up @@ -1309,16 +1309,6 @@ pub(super) fn check_type_params_are_used<'tcx>(
}
}

pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
let module = tcx.hir_module_items(module_def_id);
for id in module.items() {
check_item_type(tcx, id.owner_id.def_id);
}
if module_def_id == LocalModDefId::CRATE_DEF_ID {
super::entry::check_for_entry_fn(tcx);
}
}

fn async_opaque_type_cycle_error(tcx: TyCtxt<'_>, span: Span) -> ErrorGuaranteed {
struct_span_err!(tcx.sess, span, E0733, "recursion in an `async fn` requires boxing")
.span_label(span, "recursive `async fn`")
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::ops::Not;
use super::check_function_signature;
use crate::errors;

pub(crate) fn check_for_entry_fn(tcx: TyCtxt<'_>) {
pub(crate) fn check_for_entry_fn(tcx: TyCtxt<'_>, (): ()) {
match tcx.entry_fn(()) {
Some((def_id, EntryFnType::Main { .. })) => check_main_fn_ty(tcx, def_id),
Some((def_id, EntryFnType::Start)) => check_start_fn_ty(tcx, def_id),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub use check::check_abi;

use std::num::NonZeroU32;

use check::check_mod_item_types;
use entry::check_for_entry_fn;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{pluralize, struct_span_err, Diagnostic, DiagnosticBuilder};
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn provide(providers: &mut Providers) {
wfcheck::provide(providers);
*providers = Providers {
adt_destructor,
check_mod_item_types,
check_for_entry_fn,
region_scope_tree,
collect_return_position_impl_trait_in_trait_tys,
compare_impl_const: compare_impl_item::compare_impl_const_raw,
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
item.name = ? tcx.def_path_str(def_id)
);

match item.kind {
let res = match item.kind {
// Right now we check that every default trait implementation
// has an implementation of itself. Basically, a case like:
//
Expand Down Expand Up @@ -268,7 +268,11 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
}
}
_ => Ok(()),
}
};

crate::check::check::check_item_type(tcx, def_id);

res
}

fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) -> Result<(), ErrorGuaranteed> {
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
tcx.hir().try_par_for_each_module(|module| tcx.ensure().check_mod_type_wf(module))
});

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

// HACK: `check_mod_type_wf` may spuriously emit errors due to `delay_span_bug`, even if those errors
// only actually get emitted in `check_mod_item_types`.
// HACK: `check_for_entry_fn` wants to report its errors even if `check_mod_type_wf` has errored.
errs?;

if tcx.features().rustc_attrs {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ rustc_queries! {
desc { |tcx| "checking naked functions in {}", describe_as_module(key, tcx) }
}

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

query check_mod_privacy(key: LocalModDefId) -> () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
error[E0277]: the trait bound `(T, U): Get` is not satisfied
--> $DIR/associated-types-no-suitable-supertrait.rs:22:5
|
LL | fn uhoh<U:Get>(&self, foo: U, bar: <(T, U) as Get>::Value) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `(T, U)`
|
help: this trait has no implementations, consider adding one
--> $DIR/associated-types-no-suitable-supertrait.rs:12:1
|
LL | trait Get {
| ^^^^^^^^^

error[E0277]: the trait bound `(T, U): Get` is not satisfied
--> $DIR/associated-types-no-suitable-supertrait.rs:22:40
|
Expand All @@ -21,18 +33,6 @@ help: consider further restricting `Self`
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Get {}
| +++++++++++++++

error[E0277]: the trait bound `(T, U): Get` is not satisfied
--> $DIR/associated-types-no-suitable-supertrait.rs:22:5
|
LL | fn uhoh<U:Get>(&self, foo: U, bar: <(T, U) as Get>::Value) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `(T, U)`
|
help: this trait has no implementations, consider adding one
--> $DIR/associated-types-no-suitable-supertrait.rs:12:1
|
LL | trait Get {
| ^^^^^^^^^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-cyclic-fail-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ impl Tr for u32 {
// ...but not in an impl that redefines one of the types.
impl Tr for bool {
type A = Box<Self::B>;
//~^ ERROR overflow evaluating the requirement `<bool as Tr>::B == _`
//~^ ERROR overflow evaluating the requirement `<bool as Tr>::A == _`
}
// (the error is shown twice for some reason)

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

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-cyclic-fail-1.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B == _`
error[E0275]: overflow evaluating the requirement `<bool as Tr>::A == _`
--> $DIR/defaults-cyclic-fail-1.rs:26:14
|
LL | type A = Box<Self::B>;
| ^^^^^^^^^^^^

error[E0275]: overflow evaluating the requirement `<usize as Tr>::A == _`
error[E0275]: overflow evaluating the requirement `<usize as Tr>::B == _`
--> $DIR/defaults-cyclic-fail-1.rs:32:14
|
LL | type B = &'static Self::A;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-cyclic-fail-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ impl Tr for u32 {

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

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

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-cyclic-fail-2.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B == _`
error[E0275]: overflow evaluating the requirement `<bool as Tr>::A == _`
--> $DIR/defaults-cyclic-fail-2.rs:27:14
|
LL | type A = Box<Self::B>;
| ^^^^^^^^^^^^

error[E0275]: overflow evaluating the requirement `<usize as Tr>::A == _`
error[E0275]: overflow evaluating the requirement `<usize as Tr>::B == _`
--> $DIR/defaults-cyclic-fail-2.rs:33:14
|
LL | type B = &'static Self::A;
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/async-await/issue-66312.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error[E0308]: mismatched types
--> $DIR/issue-66312.rs:9:8
|
LL | if x.is_some() {
| ^^^^^^^^^^^ expected `bool`, found `()`

error[E0307]: invalid `self` parameter type: T
--> $DIR/issue-66312.rs:4:22
|
Expand All @@ -7,12 +13,6 @@ LL | fn is_some(self: T);
= note: type of `self` must be `Self` or a type that dereferences to it
= 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`)

error[E0308]: mismatched types
--> $DIR/issue-66312.rs:9:8
|
LL | if x.is_some() {
| ^^^^^^^^^^^ expected `bool`, found `()`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0307, E0308.
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/const-generics/issues/issue-83765.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
trait TensorDimension {
const DIM: usize;
//~^ ERROR cycle detected when resolving instance
//~| ERROR cycle detected when resolving instance
// FIXME Given the current state of the compiler its expected that we cycle here,
// but the cycle is still wrong.
const ISSCALAR: bool = Self::DIM == 0;
Expand Down Expand Up @@ -79,6 +80,7 @@ impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> TensorSi
for BMap<'a, R, T, F, DIM>
{
fn size(&self) -> [usize; DIM] {
//~^ ERROR: method not compatible with trait
self.reference.size()
}
}
Expand All @@ -88,6 +90,7 @@ impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> Broadcas
{
type Element = R;
fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
//~^ ERROR: method not compatible with trait
self.reference.bget(index).map(&self.closure)
}
}
Expand Down
42 changes: 40 additions & 2 deletions tests/ui/const-generics/issues/issue-83765.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@ LL | trait TensorDimension {
| ^^^^^^^^^^^^^^^^^^^^^
= 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

error: aborting due to previous error
error[E0391]: cycle detected when resolving instance `<LazyUpdim<'_, T, <T as TensorDimension>::DIM, DIM> as TensorDimension>::DIM`
--> $DIR/issue-83765.rs:5:5
|
LL | const DIM: usize;
| ^^^^^^^^^^^^^^^^
|
note: ...which requires computing candidate for `<LazyUpdim<'_, T, <T as TensorDimension>::DIM, DIM> as TensorDimension>`...
--> $DIR/issue-83765.rs:4:1
|
LL | trait TensorDimension {
| ^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires resolving instance `<LazyUpdim<'_, T, <T as TensorDimension>::DIM, DIM> as TensorDimension>::DIM`, completing the cycle
note: cycle used when checking that `<impl at $DIR/issue-83765.rs:56:1: 56:97>` is well-formed
--> $DIR/issue-83765.rs:56:1
|
LL | impl<'a, T: Broadcastable, const DIM: usize> Broadcastable for LazyUpdim<'a, T, { T::DIM }, DIM> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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

error[E0308]: method not compatible with trait
--> $DIR/issue-83765.rs:82:5
|
LL | fn size(&self) -> [usize; DIM] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
|
= note: expected constant `Self::DIM`
found constant `DIM`

error[E0308]: method not compatible with trait
--> $DIR/issue-83765.rs:92:5
|
LL | fn bget(&self, index: [usize; DIM]) -> Option<Self::Element> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
|
= note: expected constant `Self::DIM`
found constant `DIM`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0391`.
Some errors have detailed explanations: E0308, E0391.
For more information about an error, try `rustc --explain E0308`.
46 changes: 23 additions & 23 deletions tests/ui/consts/const-unsized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,23 @@ LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
|
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/const-unsized.rs:7:18
|
LL | const CONST_FOO: str = *"foo";
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`

error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:11:18
--> $DIR/const-unsized.rs:3:35
|
LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
= note: constant expressions must have a statically known size

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

error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:3:35
|
LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
= note: constant expressions must have a statically known size

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/const-unsized.rs:7:24
|
Expand All @@ -48,6 +32,14 @@ LL | const CONST_FOO: str = *"foo";
= help: the trait `Sized` is not implemented for `str`
= note: constant expressions must have a statically known size

error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:11:18
|
LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`

error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
--> $DIR/const-unsized.rs:11:37
|
Expand All @@ -57,6 +49,14 @@ LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
= note: constant expressions must have a statically known size

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

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/const-unsized.rs:15:26
|
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/consts/issue-39974.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
error[E0308]: mismatched types
--> $DIR/issue-39974.rs:5:19
|
LL | f: [[f64; 2]; LENGTH],
| ^^^^^^ expected `usize`, found `f64`

error[E0308]: mismatched types
--> $DIR/issue-39974.rs:1:21
|
Expand All @@ -13,6 +7,12 @@ LL | const LENGTH: f64 = 2;
| expected `f64`, found integer
| help: use a float literal: `2.0`

error[E0308]: mismatched types
--> $DIR/issue-39974.rs:5:19
|
LL | f: [[f64; 2]; LENGTH],
| ^^^^^^ expected `usize`, found `f64`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Loading

0 comments on commit 3cd5671

Please sign in to comment.