Skip to content

Commit a64b544

Browse files
authored
Rollup merge of #74698 - ayrtonm:handle-traitref-mismatch, r=estebank
fixed error reporting for mismatched traits mismatched traits were previously referred to as types closes #72217
2 parents 7d545ed + b75ed4f commit a64b544

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -1402,8 +1402,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14021402
}
14031403

14041404
debug!("note_type_err(diag={:?})", diag);
1405+
enum Mismatch<'a> {
1406+
Variable(ty::error::ExpectedFound<Ty<'a>>),
1407+
Fixed(&'static str),
1408+
}
14051409
let (expected_found, exp_found, is_simple_error) = match values {
1406-
None => (None, None, false),
1410+
None => (None, Mismatch::Fixed("type"), false),
14071411
Some(values) => {
14081412
let (is_simple_error, exp_found) = match values {
14091413
ValuePairs::Types(exp_found) => {
@@ -1417,9 +1421,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14171421
)
14181422
.report(diag);
14191423

1420-
(is_simple_err, Some(exp_found))
1424+
(is_simple_err, Mismatch::Variable(exp_found))
14211425
}
1422-
_ => (false, None),
1426+
ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
1427+
_ => (false, Mismatch::Fixed("type")),
14231428
};
14241429
let vals = match self.values_str(&values) {
14251430
Some((expected, found)) => Some((expected, found)),
@@ -1445,8 +1450,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14451450
}
14461451
};
14471452
if let Some((expected, found)) = expected_found {
1448-
let expected_label = exp_found.map_or("type".into(), |ef| ef.expected.prefix_string());
1449-
let found_label = exp_found.map_or("type".into(), |ef| ef.found.prefix_string());
1453+
let expected_label = match exp_found {
1454+
Mismatch::Variable(ef) => ef.expected.prefix_string(),
1455+
Mismatch::Fixed(s) => s.into(),
1456+
};
1457+
let found_label = match exp_found {
1458+
Mismatch::Variable(ef) => ef.found.prefix_string(),
1459+
Mismatch::Fixed(s) => s.into(),
1460+
};
1461+
let exp_found = match exp_found {
1462+
Mismatch::Variable(exp_found) => Some(exp_found),
1463+
Mismatch::Fixed(_) => None,
1464+
};
14501465
match (&terr, expected == found) {
14511466
(TypeError::Sorts(values), extra) => {
14521467
let sort_string = |ty: Ty<'tcx>| match (extra, &ty.kind) {
@@ -1499,6 +1514,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14991514
}
15001515
}
15011516
}
1517+
let exp_found = match exp_found {
1518+
Mismatch::Variable(exp_found) => Some(exp_found),
1519+
Mismatch::Fixed(_) => None,
1520+
};
15021521
if let Some(exp_found) = exp_found {
15031522
self.suggest_as_ref_where_appropriate(span, &exp_found, diag);
15041523
}

src/test/ui/error-codes/E0308-2.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait DynEq {}
2+
3+
impl<'a> PartialEq for &'a (dyn DynEq + 'static) {
4+
fn eq(&self, _other: &Self) -> bool {
5+
true
6+
}
7+
}
8+
9+
impl Eq for &dyn DynEq {} //~ ERROR E0308
10+
11+
fn main() {
12+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/E0308-2.rs:9:6
3+
|
4+
LL | impl Eq for &dyn DynEq {}
5+
| ^^ lifetime mismatch
6+
|
7+
= note: expected trait `std::cmp::PartialEq`
8+
found trait `std::cmp::PartialEq`
9+
note: the lifetime `'_` as defined on the impl at 9:13...
10+
--> $DIR/E0308-2.rs:9:13
11+
|
12+
LL | impl Eq for &dyn DynEq {}
13+
| ^
14+
= note: ...does not necessarily outlive the static lifetime
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)