Skip to content

Commit

Permalink
Rollup merge of #107304 - Nilstrieb:ᐸTy as PartialEqᐳ::eq because wha…
Browse files Browse the repository at this point in the history
…t else are we gonna use in rustc_middle, r=compiler-errors

Use `can_eq` to compare types for default assoc type error

This correctly handles inference variables like `{integer}`. I had to move all of this `note_and_explain` code to `rustc_infer`, it made no sense for it to be in `rustc_middle` anyways.

The commits are reviewed separately.

Fixes #106968
  • Loading branch information
matthiaskrgr authored Jan 26, 2023
2 parents f2f1234 + 943000f commit 3aeafca
Show file tree
Hide file tree
Showing 9 changed files with 703 additions and 648 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use std::path::PathBuf;
use std::{cmp, fmt, iter};

mod note;
mod note_and_explain;
mod suggest;

pub(crate) mod need_type_info;
Expand Down Expand Up @@ -1846,7 +1847,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}

self.check_and_note_conflicting_crates(diag, terr);
self.tcx.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());

self.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());

if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values
&& let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind()
Expand Down
654 changes: 654 additions & 0 deletions compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl std::fmt::Display for AssocKind {
/// done only on items with the same name.
#[derive(Debug, Clone, PartialEq, HashStable)]
pub struct AssocItems<'tcx> {
pub(super) items: SortedIndexMultiMap<u32, Symbol, &'tcx ty::AssocItem>,
items: SortedIndexMultiMap<u32, Symbol, &'tcx ty::AssocItem>,
}

impl<'tcx> AssocItems<'tcx> {
Expand Down
637 changes: 5 additions & 632 deletions compiler/rustc_middle/src/ty/error.rs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/ty/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ impl<'tcx> TyCtxt<'tcx> {
self.opt_def_kind(def_id)
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
}

pub fn bound_type_of(self, def_id: impl IntoQueryParam<DefId>) -> ty::EarlyBinder<Ty<'tcx>> {
ty::EarlyBinder(self.type_of(def_id))
}
}

impl<'tcx> TyCtxtAt<'tcx> {
Expand All @@ -449,4 +453,8 @@ impl<'tcx> TyCtxtAt<'tcx> {
self.opt_def_kind(def_id)
.unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
}

pub fn bound_type_of(self, def_id: impl IntoQueryParam<DefId>) -> ty::EarlyBinder<Ty<'tcx>> {
ty::EarlyBinder(self.type_of(def_id))
}
}
11 changes: 0 additions & 11 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use crate::mir;
use crate::ty::layout::IntegerExt;
use crate::ty::query::TyCtxtAt;
use crate::ty::{
self, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitable,
Expand Down Expand Up @@ -637,10 +636,6 @@ impl<'tcx> TyCtxt<'tcx> {
if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
}

pub fn bound_type_of(self, def_id: DefId) -> ty::EarlyBinder<Ty<'tcx>> {
ty::EarlyBinder(self.type_of(def_id))
}

pub fn bound_return_position_impl_trait_in_trait_tys(
self,
def_id: DefId,
Expand Down Expand Up @@ -738,12 +733,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

impl<'tcx> TyCtxtAt<'tcx> {
pub fn bound_type_of(self, def_id: DefId) -> ty::EarlyBinder<Ty<'tcx>> {
ty::EarlyBinder(self.type_of(def_id))
}
}

struct OpaqueTypeExpander<'tcx> {
// Contains the DefIds of the opaque types that are currently being
// expanded. When we expand an opaque type we insert the DefId of
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/associated-types/defaults-in-other-trait-items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ impl AssocConst for () {
const C: Self::Ty = 0u8;
}

pub trait Trait {
type Res = isize; //~ NOTE associated type defaults can't be assumed inside the trait defining them

fn infer_me_correctly() -> Self::Res {
//~^ NOTE expected `<Self as Trait>::Res` because of return type

// {integer} == isize
2
//~^ ERROR mismatched types
//~| NOTE expected associated type, found integer
//~| NOTE expected associated type `<Self as Trait>::Res`
}
}

fn main() {}
17 changes: 16 additions & 1 deletion tests/ui/associated-types/defaults-in-other-trait-items.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ LL | const C: Self::Ty = 0u8;
= note: expected associated type `<Self as AssocConst>::Ty`
found type `u8`

error: aborting due to 2 previous errors
error[E0308]: mismatched types
--> $DIR/defaults-in-other-trait-items.rs:54:9
|
LL | type Res = isize;
| ----------------- associated type defaults can't be assumed inside the trait defining them
LL |
LL | fn infer_me_correctly() -> Self::Res {
| --------- expected `<Self as Trait>::Res` because of return type
...
LL | 2
| ^ expected associated type, found integer
|
= note: expected associated type `<Self as Trait>::Res`
found type `{integer}`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.
4 changes: 2 additions & 2 deletions tests/ui/associated-types/issue-26681.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0308]: mismatched types
--> $DIR/issue-26681.rs:17:39
|
LL | type Fv: Foo = u8;
| ------------------ associated type defaults can't be assumed inside the trait defining them
LL | const C: <Self::Fv as Foo>::Bar = 6665;
| ^^^^ expected associated type, found integer
|
= note: expected associated type `<<Self as Baz>::Fv as Foo>::Bar`
found type `{integer}`
= help: consider constraining the associated type `<<Self as Baz>::Fv as Foo>::Bar` to `{integer}`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

error: aborting due to previous error

Expand Down

0 comments on commit 3aeafca

Please sign in to comment.