Skip to content

Commit 8206036

Browse files
authored
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 9f5cbfb + 01d19d7 commit 8206036

31 files changed

+178
-179
lines changed

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

+19-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::traits::SignatureMismatchData;
1717
use rustc_middle::traits::select::OverflowError;
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
FmtPrinter, Print, PrintTraitPredicateExt as _, PrintTraitRefExt as _,
2323
with_forced_trimmed_paths,
@@ -1788,22 +1788,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17881788
return false;
17891789
}
17901790

1791-
let cand = self.resolve_vars_if_possible(impl_trait_ref).fold_with(
1792-
&mut BottomUpFolder {
1793-
tcx: self.tcx,
1794-
ty_op: |ty| ty,
1795-
lt_op: |lt| lt,
1796-
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
1797-
},
1798-
);
1799-
if cand.references_error() {
1791+
let impl_trait_ref = self.resolve_vars_if_possible(impl_trait_ref);
1792+
if impl_trait_ref.references_error() {
18001793
return false;
18011794
}
18021795
err.highlighted_help(vec![
1803-
StringPart::normal(format!("the trait `{}` ", cand.print_trait_sugared())),
1796+
StringPart::normal(format!(
1797+
"the trait `{}` ",
1798+
impl_trait_ref.print_trait_sugared()
1799+
)),
18041800
StringPart::highlighted("is"),
18051801
StringPart::normal(" implemented for `"),
1806-
StringPart::highlighted(cand.self_ty().to_string()),
1802+
StringPart::highlighted(impl_trait_ref.self_ty().to_string()),
18071803
StringPart::normal("`"),
18081804
]);
18091805

@@ -1915,15 +1911,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19151911
let mut impl_candidates: Vec<_> = impl_candidates
19161912
.iter()
19171913
.cloned()
1914+
.filter(|cand| !cand.trait_ref.references_error())
19181915
.map(|mut cand| {
1919-
// Fold the consts so that they shows up as, e.g., `10`
1920-
// instead of `core::::array::{impl#30}::{constant#0}`.
1921-
cand.trait_ref = cand.trait_ref.fold_with(&mut BottomUpFolder {
1922-
tcx: self.tcx,
1923-
ty_op: |ty| ty,
1924-
lt_op: |lt| lt,
1925-
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
1926-
});
1916+
// Normalize the trait ref in its *own* param-env so
1917+
// that consts are folded and any trivial projections
1918+
// are normalized.
1919+
cand.trait_ref = self
1920+
.tcx
1921+
.try_normalize_erasing_regions(
1922+
self.tcx.param_env(cand.impl_def_id),
1923+
cand.trait_ref,
1924+
)
1925+
.unwrap_or(cand.trait_ref);
19271926
cand
19281927
})
19291928
.collect();

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

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

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

+2-2
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

+2-2
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

+24-24
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

+1-1
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

+2-2
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

+2-2
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

+2-2
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

+16-16
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

tests/ui/issues/issue-24352.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | 1.0f64 - 1
66
|
77
= help: the trait `Sub<{integer}>` is not implemented for `f64`
88
= help: the following other types implement trait `Sub<Rhs>`:
9-
`&'a f64` implements `Sub<f64>`
10-
`&f64` implements `Sub<&f64>`
9+
`&f64` implements `Sub<f64>`
10+
`&f64` implements `Sub`
1111
`f64` implements `Sub<&f64>`
1212
`f64` implements `Sub`
1313
help: consider using a floating-point literal by writing it with `.0`

tests/ui/issues/issue-50582.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ LL | Vec::<[(); 1 + for x in 0..1 {}]>::new();
1616
|
1717
= help: the trait `Add<()>` is not implemented for `{integer}`
1818
= help: the following other types implement trait `Add<Rhs>`:
19-
`&'a f128` implements `Add<f128>`
20-
`&'a f16` implements `Add<f16>`
21-
`&'a f32` implements `Add<f32>`
22-
`&'a f64` implements `Add<f64>`
23-
`&'a i128` implements `Add<i128>`
24-
`&'a i16` implements `Add<i16>`
25-
`&'a i32` implements `Add<i32>`
26-
`&'a i64` implements `Add<i64>`
19+
`&f128` implements `Add<f128>`
20+
`&f128` implements `Add`
21+
`&f16` implements `Add<f16>`
22+
`&f16` implements `Add`
23+
`&f32` implements `Add<f32>`
24+
`&f32` implements `Add`
25+
`&f64` implements `Add<f64>`
26+
`&f64` implements `Add`
2727
and 56 others
2828

2929
error: aborting due to 2 previous errors

tests/ui/iterators/invalid-iterator-chain-fixable.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ LL | println!("{}", scores.sum::<i32>());
3333
|
3434
= help: the trait `Sum<()>` is not implemented for `i32`
3535
= help: the following other types implement trait `Sum<A>`:
36-
`i32` implements `Sum<&'a i32>`
36+
`i32` implements `Sum<&i32>`
3737
`i32` implements `Sum`
3838
note: the method call chain might not have had the expected associated types
3939
--> $DIR/invalid-iterator-chain-fixable.rs:14:10
@@ -66,7 +66,7 @@ LL | .sum::<i32>(),
6666
|
6767
= help: the trait `Sum<()>` is not implemented for `i32`
6868
= help: the following other types implement trait `Sum<A>`:
69-
`i32` implements `Sum<&'a i32>`
69+
`i32` implements `Sum<&i32>`
7070
`i32` implements `Sum`
7171
note: the method call chain might not have had the expected associated types
7272
--> $DIR/invalid-iterator-chain-fixable.rs:23:14
@@ -99,7 +99,7 @@ LL | println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>());
9999
|
100100
= help: the trait `Sum<()>` is not implemented for `i32`
101101
= help: the following other types implement trait `Sum<A>`:
102-
`i32` implements `Sum<&'a i32>`
102+
`i32` implements `Sum<&i32>`
103103
`i32` implements `Sum`
104104
note: the method call chain might not have had the expected associated types
105105
--> $DIR/invalid-iterator-chain-fixable.rs:27:38

tests/ui/iterators/invalid-iterator-chain-with-int-infer.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | let x = Some(()).iter().map(|()| 1).sum::<f32>();
88
|
99
= help: the trait `Sum<{integer}>` is not implemented for `f32`
1010
= help: the following other types implement trait `Sum<A>`:
11-
`f32` implements `Sum<&'a f32>`
11+
`f32` implements `Sum<&f32>`
1212
`f32` implements `Sum`
1313
note: the method call chain might not have had the expected associated types
1414
--> $DIR/invalid-iterator-chain-with-int-infer.rs:2:29

tests/ui/iterators/invalid-iterator-chain.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ LL | println!("{}", scores.sum::<i32>());
3333
|
3434
= help: the trait `Sum<()>` is not implemented for `i32`
3535
= help: the following other types implement trait `Sum<A>`:
36-
`i32` implements `Sum<&'a i32>`
36+
`i32` implements `Sum<&i32>`
3737
`i32` implements `Sum`
3838
note: the method call chain might not have had the expected associated types
3939
--> $DIR/invalid-iterator-chain.rs:12:10
@@ -65,7 +65,7 @@ LL | .sum::<i32>(),
6565
|
6666
= help: the trait `Sum<()>` is not implemented for `i32`
6767
= help: the following other types implement trait `Sum<A>`:
68-
`i32` implements `Sum<&'a i32>`
68+
`i32` implements `Sum<&i32>`
6969
`i32` implements `Sum`
7070
note: the method call chain might not have had the expected associated types
7171
--> $DIR/invalid-iterator-chain.rs:25:14
@@ -104,7 +104,7 @@ LL | .sum::<i32>(),
104104
|
105105
= help: the trait `Sum<f64>` is not implemented for `i32`
106106
= help: the following other types implement trait `Sum<A>`:
107-
`i32` implements `Sum<&'a i32>`
107+
`i32` implements `Sum<&i32>`
108108
`i32` implements `Sum`
109109
note: the method call chain might not have had the expected associated types
110110
--> $DIR/invalid-iterator-chain.rs:33:14
@@ -134,7 +134,7 @@ LL | println!("{}", vec![0, 1].iter().map(|x| { x; }).sum::<i32>());
134134
|
135135
= help: the trait `Sum<()>` is not implemented for `i32`
136136
= help: the following other types implement trait `Sum<A>`:
137-
`i32` implements `Sum<&'a i32>`
137+
`i32` implements `Sum<&i32>`
138138
`i32` implements `Sum`
139139
note: the method call chain might not have had the expected associated types
140140
--> $DIR/invalid-iterator-chain.rs:38:38
@@ -162,7 +162,7 @@ LL | println!("{}", vec![(), ()].iter().sum::<i32>());
162162
|
163163
= help: the trait `Sum<&()>` is not implemented for `i32`
164164
= help: the following other types implement trait `Sum<A>`:
165-
`i32` implements `Sum<&'a i32>`
165+
`i32` implements `Sum<&i32>`
166166
`i32` implements `Sum`
167167
note: the method call chain might not have had the expected associated types
168168
--> $DIR/invalid-iterator-chain.rs:39:33

tests/ui/lazy-type-alias/trailing-where-clause.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | let _: Alias<()>;
99
`String` implements `From<&mut str>`
1010
`String` implements `From<&str>`
1111
`String` implements `From<Box<str>>`
12-
`String` implements `From<Cow<'a, str>>`
12+
`String` implements `From<Cow<'_, str>>`
1313
`String` implements `From<char>`
1414
note: required by a bound in `Alias`
1515
--> $DIR/trailing-where-clause.rs:8:13

0 commit comments

Comments
 (0)