1
Fork 0

Modify primary span label for E0308

The previous output was unintuitive to users.
This commit is contained in:
Esteban Küber 2023-01-02 18:00:33 -08:00
parent 006ca9b14d
commit 62ba3e70a1
383 changed files with 889 additions and 926 deletions

View file

@ -67,6 +67,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::Node;
use rustc_middle::dep_graph::DepContext;
use rustc_middle::ty::print::with_forced_trimmed_paths;
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
use rustc_middle::ty::{
self, error::TypeError, List, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
@ -1612,16 +1613,31 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
{
format!("expected this to be `{}`", expected)
} else {
terr.to_string()
terr.to_string(self.tcx)
};
label_or_note(sp, &terr);
label_or_note(span, &msg);
} else {
label_or_note(span, &terr.to_string());
label_or_note(span, &terr.to_string(self.tcx));
label_or_note(sp, &msg);
}
} else {
label_or_note(span, &terr.to_string());
if let Some(values) = values
&& let Some((e, f)) = values.ty()
&& let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) = terr
{
let e = self.tcx.erase_regions(e);
let f = self.tcx.erase_regions(f);
let expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
let found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
if expected == found {
label_or_note(span, &terr.to_string(self.tcx));
} else {
label_or_note(span, &format!("expected {expected}, found {found}"));
}
} else {
label_or_note(span, &terr.to_string(self.tcx));
}
}
if let Some((expected, found, exp_p, found_p)) = expected_found {

View file

@ -137,25 +137,25 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
diag.help(
"given a type parameter `T` and a method `foo`:
```
trait Trait<T> { fn foo(&tcx) -> T; }
trait Trait<T> { fn foo(&self) -> T; }
```
the only ways to implement method `foo` are:
- constrain `T` with an explicit type:
```
impl Trait<String> for X {
fn foo(&tcx) -> String { String::new() }
fn foo(&self) -> String { String::new() }
}
```
- add a trait bound to `T` and call a method on that trait that returns `Self`:
```
impl<T: std::default::Default> Trait<T> for X {
fn foo(&tcx) -> T { <T as std::default::Default>::default() }
fn foo(&self) -> T { <T as std::default::Default>::default() }
}
```
- change `foo` to return an argument of type `T`:
```
impl<T> Trait<T> for X {
fn foo(&tcx, x: T) -> T { x }
fn foo(&self, x: T) -> T { x }
}
```",
);
@ -389,14 +389,14 @@ impl<T> Trait<T> for X {
```
trait Trait {
type T;
fn foo(&tcx) -> Self::T;
fn foo(&self) -> Self::T;
}
```
the only way of implementing method `foo` is to constrain `T` with an explicit associated type:
```
impl Trait for X {
type T = String;
fn foo(&tcx) -> Self::T { String::new() }
fn foo(&self) -> Self::T { String::new() }
}
```",
);