Skip to content

Commit 3cb0ee5

Browse files
Rollup merge of rust-lang#130712 - compiler-errors:const-eval-error-reporting, r=BoxyUwU
Don't call `ty::Const::normalize` in error reporting We do this to ensure that trait refs with unevaluated consts have those consts simplified to their evaluated forms. Instead, use `try_normalize_erasing_regions`. **NOTE:** This has the side-effect of erasing regions from all of our trait refs. If this is too much to review or you think it's too opinionated of a diagnostics change, then I could split out the effective change (i.e. erasing regions from this impl suggestion) into another PR and have someone else review it.
2 parents 01a4d75 + 01d19d7 commit 3cb0ee5

31 files changed

+178
-179
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::traits::select::OverflowError;
1717
use rustc_middle::traits::SignatureMismatchData;
1818
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1919
use rustc_middle::ty::error::{ExpectedFound, TypeError};
20-
use rustc_middle::ty::fold::{BottomUpFolder, TypeFolder, TypeSuperFoldable};
20+
use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable};
2121
use rustc_middle::ty::print::{
2222
with_forced_trimmed_paths, FmtPrinter, Print, PrintTraitPredicateExt as _,
2323
PrintTraitRefExt as _,
@@ -1794,22 +1794,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17941794
return false;
17951795
}
17961796

1797-
let cand = self.resolve_vars_if_possible(impl_trait_ref).fold_with(
1798-
&mut BottomUpFolder {
1799-
tcx: self.tcx,
1800-
ty_op: |ty| ty,
1801-
lt_op: |lt| lt,
1802-
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
1803-
},
1804-
);
1805-
if cand.references_error() {
1797+
let impl_trait_ref = self.resolve_vars_if_possible(impl_trait_ref);
1798+
if impl_trait_ref.references_error() {
18061799
return false;
18071800
}
18081801
err.highlighted_help(vec![
1809-
StringPart::normal(format!("the trait `{}` ", cand.print_trait_sugared())),
1802+
StringPart::normal(format!(
1803+
"the trait `{}` ",
1804+
impl_trait_ref.print_trait_sugared()
1805+
)),
18101806
StringPart::highlighted("is"),
18111807
StringPart::normal(" implemented for `"),
1812-
StringPart::highlighted(cand.self_ty().to_string()),
1808+
StringPart::highlighted(impl_trait_ref.self_ty().to_string()),
18131809
StringPart::normal("`"),
18141810
]);
18151811

@@ -1921,15 +1917,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19211917
let mut impl_candidates: Vec<_> = impl_candidates
19221918
.iter()
19231919
.cloned()
1920+
.filter(|cand| !cand.trait_ref.references_error())
19241921
.map(|mut cand| {
1925-
// Fold the consts so that they shows up as, e.g., `10`
1926-
// instead of `core::::array::{impl#30}::{constant#0}`.
1927-
cand.trait_ref = cand.trait_ref.fold_with(&mut BottomUpFolder {
1928-
tcx: self.tcx,
1929-
ty_op: |ty| ty,
1930-
lt_op: |lt| lt,
1931-
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
1932-
});
1922+
// Normalize the trait ref in its *own* param-env so
1923+
// that consts are folded and any trivial projections
1924+
// are normalized.
1925+
cand.trait_ref = self
1926+
.tcx
1927+
.try_normalize_erasing_regions(
1928+
self.tcx.param_env(cand.impl_def_id),
1929+
cand.trait_ref,
1930+
)
1931+
.unwrap_or(cand.trait_ref);
19331932
cand
19341933
})
19351934
.collect();

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4621,7 +4621,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
46214621
format!("&{}{ty}", mutability.prefix_str())
46224622
}
46234623
}
4624-
ty::Array(ty, len) if let Some(len) = len.try_eval_target_usize(tcx, param_env) => {
4624+
ty::Array(ty, len) if let Some(len) = len.try_to_target_usize(tcx) => {
46254625
if len == 0 {
46264626
"[]".to_string()
46274627
} else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {

tests/ui/binop/binary-op-suggest-deref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ LL | let _ = FOO & (*"Sized".to_string().into_boxed_str());
303303
|
304304
= help: the trait `BitAnd<str>` is not implemented for `i32`
305305
= help: the following other types implement trait `BitAnd<Rhs>`:
306-
`&'a i32` implements `BitAnd<i32>`
307-
`&i32` implements `BitAnd<&i32>`
306+
`&i32` implements `BitAnd<i32>`
307+
`&i32` implements `BitAnd`
308308
`i32` implements `BitAnd<&i32>`
309309
`i32` implements `BitAnd`
310310

tests/ui/binop/binop-mul-i32-f32.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | x * y
66
|
77
= help: the trait `Mul<f32>` is not implemented for `i32`
88
= help: the following other types implement trait `Mul<Rhs>`:
9-
`&'a i32` implements `Mul<i32>`
10-
`&i32` implements `Mul<&i32>`
9+
`&i32` implements `Mul<i32>`
10+
`&i32` implements `Mul`
1111
`i32` implements `Mul<&i32>`
1212
`i32` implements `Mul`
1313

tests/ui/binop/shift-various-bad-types.stderr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ LL | 22 >> p.char;
66
|
77
= help: the trait `Shr<char>` is not implemented for `{integer}`
88
= help: the following other types implement trait `Shr<Rhs>`:
9-
`&'a i128` implements `Shr<i128>`
10-
`&'a i128` implements `Shr<i16>`
11-
`&'a i128` implements `Shr<i32>`
12-
`&'a i128` implements `Shr<i64>`
13-
`&'a i128` implements `Shr<i8>`
14-
`&'a i128` implements `Shr<isize>`
15-
`&'a i128` implements `Shr<u128>`
16-
`&'a i128` implements `Shr<u16>`
9+
`&i128` implements `Shr<&i16>`
10+
`&i128` implements `Shr<&i32>`
11+
`&i128` implements `Shr<&i64>`
12+
`&i128` implements `Shr<&i8>`
13+
`&i128` implements `Shr<&isize>`
14+
`&i128` implements `Shr<&u128>`
15+
`&i128` implements `Shr<&u16>`
16+
`&i128` implements `Shr<&u32>`
1717
and 568 others
1818

1919
error[E0277]: no implementation for `{integer} >> &str`
@@ -24,14 +24,14 @@ LL | 22 >> p.str;
2424
|
2525
= help: the trait `Shr<&str>` is not implemented for `{integer}`
2626
= help: the following other types implement trait `Shr<Rhs>`:
27-
`&'a i128` implements `Shr<i128>`
28-
`&'a i128` implements `Shr<i16>`
29-
`&'a i128` implements `Shr<i32>`
30-
`&'a i128` implements `Shr<i64>`
31-
`&'a i128` implements `Shr<i8>`
32-
`&'a i128` implements `Shr<isize>`
33-
`&'a i128` implements `Shr<u128>`
34-
`&'a i128` implements `Shr<u16>`
27+
`&i128` implements `Shr<&i16>`
28+
`&i128` implements `Shr<&i32>`
29+
`&i128` implements `Shr<&i64>`
30+
`&i128` implements `Shr<&i8>`
31+
`&i128` implements `Shr<&isize>`
32+
`&i128` implements `Shr<&u128>`
33+
`&i128` implements `Shr<&u16>`
34+
`&i128` implements `Shr<&u32>`
3535
and 568 others
3636

3737
error[E0277]: no implementation for `{integer} >> &Panolpy`
@@ -42,14 +42,14 @@ LL | 22 >> p;
4242
|
4343
= help: the trait `Shr<&Panolpy>` is not implemented for `{integer}`
4444
= help: the following other types implement trait `Shr<Rhs>`:
45-
`&'a i128` implements `Shr<i128>`
46-
`&'a i128` implements `Shr<i16>`
47-
`&'a i128` implements `Shr<i32>`
48-
`&'a i128` implements `Shr<i64>`
49-
`&'a i128` implements `Shr<i8>`
50-
`&'a i128` implements `Shr<isize>`
51-
`&'a i128` implements `Shr<u128>`
52-
`&'a i128` implements `Shr<u16>`
45+
`&i128` implements `Shr<&i16>`
46+
`&i128` implements `Shr<&i32>`
47+
`&i128` implements `Shr<&i64>`
48+
`&i128` implements `Shr<&i8>`
49+
`&i128` implements `Shr<&isize>`
50+
`&i128` implements `Shr<&u128>`
51+
`&i128` implements `Shr<&u16>`
52+
`&i128` implements `Shr<&u32>`
5353
and 568 others
5454

5555
error[E0308]: mismatched types

tests/ui/const-generics/occurs-check/unused-substs-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the trait bound `A<_>: Bar<_>` is not satisfied
44
LL | let _ = A;
55
| ^ the trait `Bar<_>` is not implemented for `A<_>`
66
|
7-
= help: the trait `Bar<_>` is implemented for `A<7>`
7+
= help: the trait `Bar<_>` is implemented for `A<{ 6 + 1 }>`
88
note: required by a bound in `A`
99
--> $DIR/unused-substs-1.rs:9:11
1010
|

tests/ui/consts/const-eval/const-eval-overflow-3b.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ LL | = [0; (i8::MAX + 1u8) as usize];
1212
|
1313
= help: the trait `Add<u8>` is not implemented for `i8`
1414
= help: the following other types implement trait `Add<Rhs>`:
15-
`&'a i8` implements `Add<i8>`
16-
`&i8` implements `Add<&i8>`
15+
`&i8` implements `Add<i8>`
16+
`&i8` implements `Add`
1717
`i8` implements `Add<&i8>`
1818
`i8` implements `Add`
1919

tests/ui/consts/const-eval/const-eval-overflow-4b.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ LL | : [u32; (i8::MAX as i8 + 1u8) as usize]
1212
|
1313
= help: the trait `Add<u8>` is not implemented for `i8`
1414
= help: the following other types implement trait `Add<Rhs>`:
15-
`&'a i8` implements `Add<i8>`
16-
`&i8` implements `Add<&i8>`
15+
`&i8` implements `Add<i8>`
16+
`&i8` implements `Add`
1717
`i8` implements `Add<&i8>`
1818
`i8` implements `Add`
1919

tests/ui/impl-trait/equality.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ LL | n + sum_to(n - 1)
3030
|
3131
= help: the trait `Add<impl Foo>` is not implemented for `u32`
3232
= help: the following other types implement trait `Add<Rhs>`:
33-
`&'a u32` implements `Add<u32>`
34-
`&u32` implements `Add<&u32>`
33+
`&u32` implements `Add<u32>`
34+
`&u32` implements `Add`
3535
`u32` implements `Add<&u32>`
3636
`u32` implements `Add`
3737

tests/ui/issues/issue-11771.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ LL | 1 +
66
|
77
= help: the trait `Add<()>` is not implemented for `{integer}`
88
= help: the following other types implement trait `Add<Rhs>`:
9-
`&'a f128` implements `Add<f128>`
10-
`&'a f16` implements `Add<f16>`
11-
`&'a f32` implements `Add<f32>`
12-
`&'a f64` implements `Add<f64>`
13-
`&'a i128` implements `Add<i128>`
14-
`&'a i16` implements `Add<i16>`
15-
`&'a i32` implements `Add<i32>`
16-
`&'a i64` implements `Add<i64>`
9+
`&f128` implements `Add<f128>`
10+
`&f128` implements `Add`
11+
`&f16` implements `Add<f16>`
12+
`&f16` implements `Add`
13+
`&f32` implements `Add<f32>`
14+
`&f32` implements `Add`
15+
`&f64` implements `Add<f64>`
16+
`&f64` implements `Add`
1717
and 56 others
1818

1919
error[E0277]: cannot add `()` to `{integer}`
@@ -24,14 +24,14 @@ LL | 1 +
2424
|
2525
= help: the trait `Add<()>` is not implemented for `{integer}`
2626
= help: the following other types implement trait `Add<Rhs>`:
27-
`&'a f128` implements `Add<f128>`
28-
`&'a f16` implements `Add<f16>`
29-
`&'a f32` implements `Add<f32>`
30-
`&'a f64` implements `Add<f64>`
31-
`&'a i128` implements `Add<i128>`
32-
`&'a i16` implements `Add<i16>`
33-
`&'a i32` implements `Add<i32>`
34-
`&'a i64` implements `Add<i64>`
27+
`&f128` implements `Add<f128>`
28+
`&f128` implements `Add`
29+
`&f16` implements `Add<f16>`
30+
`&f16` implements `Add`
31+
`&f32` implements `Add<f32>`
32+
`&f32` implements `Add`
33+
`&f64` implements `Add<f64>`
34+
`&f64` implements `Add`
3535
and 56 others
3636

3737
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy