Skip to content

Commit 28f3986

Browse files
use Const::eval instead of QueryNormalize in error reporting
1 parent 1d67eba commit 28f3986

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCod
1111
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1212
use crate::infer::{self, InferCtxt};
1313
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
14-
use crate::traits::query::normalize::QueryNormalizeExt as _;
1514
use crate::traits::specialize::to_pretty_impl_header;
1615
use crate::traits::NormalizeExt;
1716
use on_unimplemented::{AppendConstMessage, OnUnimplementedNote, TypeErrCtxtExt as _};
@@ -31,7 +30,7 @@ use rustc_middle::traits::select::OverflowError;
3130
use rustc_middle::traits::SelectionOutputTypeParameterMismatch;
3231
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
3332
use rustc_middle::ty::error::{ExpectedFound, TypeError};
34-
use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable};
33+
use rustc_middle::ty::fold::{BottomUpFolder, TypeFolder, TypeSuperFoldable};
3534
use rustc_middle::ty::print::{with_forced_trimmed_paths, FmtPrinter, Print};
3635
use rustc_middle::ty::{
3736
self, SubtypePredicate, ToPolyTraitRef, ToPredicate, TraitRef, Ty, TyCtxt, TypeFoldable,
@@ -60,7 +59,7 @@ pub enum CandidateSimilarity {
6059
Fuzzy { ignoring_lifetimes: bool },
6160
}
6261

63-
#[derive(Debug, Clone, Copy)]
62+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6463
pub struct ImplCandidate<'tcx> {
6564
pub trait_ref: ty::TraitRef<'tcx>,
6665
pub similarity: CandidateSimilarity,
@@ -1992,7 +1991,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
19921991
// Mentioning implementers of `Copy`, `Debug` and friends is not useful.
19931992
return false;
19941993
}
1995-
let normalized_impl_candidates: Vec<_> = self
1994+
let impl_candidates: Vec<_> = self
19961995
.tcx
19971996
.all_impls(def_id)
19981997
// Ignore automatically derived impls and `!Trait` impls.
@@ -2019,7 +2018,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20192018
}
20202019
})
20212020
.collect();
2022-
return report(normalized_impl_candidates, err);
2021+
return report(impl_candidates, err);
20232022
}
20242023

20252024
// Sort impl candidates so that ordering is consistent for UI tests.
@@ -2028,27 +2027,26 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20282027
//
20292028
// Prefer more similar candidates first, then sort lexicographically
20302029
// by their normalized string representation.
2031-
let mut normalized_impl_candidates_and_similarities = impl_candidates
2032-
.iter()
2033-
.copied()
2034-
.map(|ImplCandidate { trait_ref, similarity }| {
2035-
// FIXME(compiler-errors): This should be using `NormalizeExt::normalize`
2036-
let normalized = self
2037-
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
2038-
.query_normalize(trait_ref)
2039-
.map_or(trait_ref, |normalized| normalized.value);
2040-
(similarity, normalized)
2041-
})
2042-
.collect::<Vec<_>>();
2043-
normalized_impl_candidates_and_similarities.sort();
2044-
normalized_impl_candidates_and_similarities.dedup();
2030+
let mut impl_candidates = impl_candidates.to_vec();
2031+
impl_candidates.sort_by_key(|cand| (cand.similarity, cand.trait_ref));
2032+
impl_candidates.dedup();
20452033

2046-
let normalized_impl_candidates = normalized_impl_candidates_and_similarities
2047-
.into_iter()
2048-
.map(|(_, normalized)| normalized)
2049-
.collect::<Vec<_>>();
2050-
2051-
report(normalized_impl_candidates, err)
2034+
report(
2035+
impl_candidates
2036+
.into_iter()
2037+
.map(|cand| {
2038+
// Fold the const so that it shows up as, e.g., `10`
2039+
// instead of `core::::array::{impl#30}::{constant#0}`.
2040+
cand.trait_ref.fold_with(&mut BottomUpFolder {
2041+
tcx: self.tcx,
2042+
ty_op: |ty| ty,
2043+
lt_op: |lt| lt,
2044+
ct_op: |ct| ct.eval(self.tcx, ty::ParamEnv::empty()),
2045+
})
2046+
})
2047+
.collect(),
2048+
err,
2049+
)
20522050
}
20532051

20542052
fn report_similar_impl_candidates_for_root_obligation(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct X;
2+
3+
// Make sure that we show the impl trait refs in the help message with
4+
// their evaluated constants, rather than `core::::array::{impl#30}::{constant#0}`
5+
6+
fn main() {
7+
<[X; 35] as Default>::default();
8+
//~^ ERROR the trait bound `[X; 35]: Default` is not satisfied
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0277]: the trait bound `[X; 35]: Default` is not satisfied
2+
--> $DIR/missing-larger-array-impl.rs:7:5
3+
|
4+
LL | <[X; 35] as Default>::default();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `[X; 35]`
6+
|
7+
= help: the following other types implement trait `Default`:
8+
&[T]
9+
&mut [T]
10+
[T; 0]
11+
[T; 10]
12+
[T; 11]
13+
[T; 12]
14+
[T; 13]
15+
[T; 14]
16+
and 27 others
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)