Skip to content

Commit 62ba3e7

Browse files
committed
Modify primary span label for E0308
The previous output was unintuitive to users.
1 parent 006ca9b commit 62ba3e7

File tree

383 files changed

+889
-926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

383 files changed

+889
-926
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ fn check_opaque_meets_bounds<'tcx>(
444444
Err(ty_err) => {
445445
tcx.sess.delay_span_bug(
446446
span,
447-
&format!("could not unify `{hidden_ty}` with revealed type:\n{ty_err}"),
447+
&format!(
448+
"could not unify `{hidden_ty}` with revealed type:\n{}",
449+
ty_err.to_string(tcx)
450+
),
448451
);
449452
}
450453
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
6767
use rustc_hir::lang_items::LangItem;
6868
use rustc_hir::Node;
6969
use rustc_middle::dep_graph::DepContext;
70+
use rustc_middle::ty::print::with_forced_trimmed_paths;
7071
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7172
use rustc_middle::ty::{
7273
self, error::TypeError, List, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
@@ -1612,16 +1613,31 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
16121613
{
16131614
format!("expected this to be `{}`", expected)
16141615
} else {
1615-
terr.to_string()
1616+
terr.to_string(self.tcx)
16161617
};
16171618
label_or_note(sp, &terr);
16181619
label_or_note(span, &msg);
16191620
} else {
1620-
label_or_note(span, &terr.to_string());
1621+
label_or_note(span, &terr.to_string(self.tcx));
16211622
label_or_note(sp, &msg);
16221623
}
16231624
} else {
1624-
label_or_note(span, &terr.to_string());
1625+
if let Some(values) = values
1626+
&& let Some((e, f)) = values.ty()
1627+
&& let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) = terr
1628+
{
1629+
let e = self.tcx.erase_regions(e);
1630+
let f = self.tcx.erase_regions(f);
1631+
let expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1632+
let found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1633+
if expected == found {
1634+
label_or_note(span, &terr.to_string(self.tcx));
1635+
} else {
1636+
label_or_note(span, &format!("expected {expected}, found {found}"));
1637+
}
1638+
} else {
1639+
label_or_note(span, &terr.to_string(self.tcx));
1640+
}
16251641
}
16261642

16271643
if let Some((expected, found, exp_p, found_p)) = expected_found {

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,25 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
137137
diag.help(
138138
"given a type parameter `T` and a method `foo`:
139139
```
140-
trait Trait<T> { fn foo(&tcx) -> T; }
140+
trait Trait<T> { fn foo(&self) -> T; }
141141
```
142142
the only ways to implement method `foo` are:
143143
- constrain `T` with an explicit type:
144144
```
145145
impl Trait<String> for X {
146-
fn foo(&tcx) -> String { String::new() }
146+
fn foo(&self) -> String { String::new() }
147147
}
148148
```
149149
- add a trait bound to `T` and call a method on that trait that returns `Self`:
150150
```
151151
impl<T: std::default::Default> Trait<T> for X {
152-
fn foo(&tcx) -> T { <T as std::default::Default>::default() }
152+
fn foo(&self) -> T { <T as std::default::Default>::default() }
153153
}
154154
```
155155
- change `foo` to return an argument of type `T`:
156156
```
157157
impl<T> Trait<T> for X {
158-
fn foo(&tcx, x: T) -> T { x }
158+
fn foo(&self, x: T) -> T { x }
159159
}
160160
```",
161161
);
@@ -389,14 +389,14 @@ impl<T> Trait<T> for X {
389389
```
390390
trait Trait {
391391
type T;
392-
fn foo(&tcx) -> Self::T;
392+
fn foo(&self) -> Self::T;
393393
}
394394
```
395395
the only way of implementing method `foo` is to constrain `T` with an explicit associated type:
396396
```
397397
impl Trait for X {
398398
type T = String;
399-
fn foo(&tcx) -> Self::T { String::new() }
399+
fn foo(&self) -> Self::T { String::new() }
400400
}
401401
```",
402402
);

0 commit comments

Comments
 (0)