Skip to content

Commit b8f78fb

Browse files
authored
Rollup merge of #114169 - lcnr:unsize, r=compiler-errors
refactor builtin unsize handling, extend comments r? `@compiler-errors`
2 parents b321edd + 17f87c5 commit b8f78fb

File tree

9 files changed

+272
-219
lines changed

9 files changed

+272
-219
lines changed

compiler/rustc_middle/src/traits/solve.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub enum Certainty {
5757

5858
impl Certainty {
5959
pub const AMBIGUOUS: Certainty = Certainty::Maybe(MaybeCause::Ambiguity);
60+
pub const OVERFLOW: Certainty = Certainty::Maybe(MaybeCause::Overflow);
6061

6162
/// Use this function to merge the certainty of multiple nested subgoals.
6263
///
@@ -66,7 +67,7 @@ impl Certainty {
6667
/// success, we merge these two responses. This results in ambiguity.
6768
///
6869
/// If we unify ambiguity with overflow, we return overflow. This doesn't matter
69-
/// inside of the solver as we distinguish ambiguity from overflow. It does
70+
/// inside of the solver as we do not distinguish ambiguity from overflow. It does
7071
/// however matter for diagnostics. If `T: Foo` resulted in overflow and `T: Bar`
7172
/// in ambiguity without changing the inference state, we still want to tell the
7273
/// user that `T: Baz` results in overflow.

compiler/rustc_middle/src/ty/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1359,12 +1359,24 @@ impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyTraitPredicate<'tcx> {
13591359
}
13601360
}
13611361

1362+
impl<'tcx> ToPredicate<'tcx> for OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>> {
1363+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1364+
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::RegionOutlives(self))).to_predicate(tcx)
1365+
}
1366+
}
1367+
13621368
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
13631369
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
13641370
self.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).to_predicate(tcx)
13651371
}
13661372
}
13671373

1374+
impl<'tcx> ToPredicate<'tcx> for OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> {
1375+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1376+
ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(self))).to_predicate(tcx)
1377+
}
1378+
}
1379+
13681380
impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
13691381
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
13701382
self.map_bound(|p| PredicateKind::Clause(ClauseKind::TypeOutlives(p))).to_predicate(tcx)

compiler/rustc_trait_selection/src/solve/assembly/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def_id::DefId;
77
use rustc_infer::traits::query::NoSolution;
88
use rustc_infer::traits::Reveal;
99
use rustc_middle::traits::solve::inspect::CandidateKind;
10-
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, MaybeCause, QueryResult};
10+
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, QueryResult};
1111
use rustc_middle::traits::BuiltinImplSource;
1212
use rustc_middle::ty::fast_reject::{SimplifiedType, TreatParams};
1313
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -299,7 +299,7 @@ pub(super) trait GoalKind<'tcx>:
299299
/// for unsize coercion in hir typeck and because it is difficult to
300300
/// otherwise recompute this for codegen. This is a bit of a mess but the
301301
/// easiest way to maintain the existing behavior for now.
302-
fn consider_builtin_unsize_and_upcast_candidates(
302+
fn consider_builtin_unsize_candidates(
303303
ecx: &mut EvalCtxt<'_, 'tcx>,
304304
goal: Goal<'tcx, Self>,
305305
) -> Vec<(CanonicalResponse<'tcx>, BuiltinImplSource)>;
@@ -402,7 +402,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
402402
ecx.with_incremented_depth(
403403
|ecx| {
404404
let result = ecx.evaluate_added_goals_and_make_canonical_response(
405-
Certainty::Maybe(MaybeCause::Overflow),
405+
Certainty::OVERFLOW,
406406
)?;
407407
Ok(vec![Candidate {
408408
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
@@ -624,7 +624,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
624624
// There may be multiple unsize candidates for a trait with several supertraits:
625625
// `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
626626
if lang_items.unsize_trait() == Some(trait_def_id) {
627-
for (result, source) in G::consider_builtin_unsize_and_upcast_candidates(self, goal) {
627+
for (result, source) in G::consider_builtin_unsize_candidates(self, goal) {
628628
candidates.push(Candidate { source: CandidateSource::BuiltinImpl(source), result });
629629
}
630630
}

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_infer::traits::ObligationCause;
1111
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
1212
use rustc_middle::traits::solve::inspect;
1313
use rustc_middle::traits::solve::{
14-
CanonicalInput, CanonicalResponse, Certainty, IsNormalizesToHack, MaybeCause,
15-
PredefinedOpaques, PredefinedOpaquesData, QueryResult,
14+
CanonicalInput, CanonicalResponse, Certainty, IsNormalizesToHack, PredefinedOpaques,
15+
PredefinedOpaquesData, QueryResult,
1616
};
1717
use rustc_middle::traits::DefiningAnchor;
1818
use rustc_middle::ty::{
@@ -475,7 +475,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
475475
let mut new_goals = NestedGoals::new();
476476

477477
let response = self.repeat_while_none(
478-
|_| Ok(Certainty::Maybe(MaybeCause::Overflow)),
478+
|_| Ok(Certainty::OVERFLOW),
479479
|this| {
480480
this.inspect.evaluate_added_goals_loop_start();
481481

compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ fn rematch_unsize<'tcx>(
338338
.into_obligations(),
339339
);
340340

341-
// Similar to ADTs, require that the rest of the fields are equal.
341+
// Similar to ADTs, require that we can unsize the tail.
342342
nested.push(Obligation::new(
343343
tcx,
344344
ObligationCause::dummy(),

compiler/rustc_trait_selection/src/solve/project_goals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
503503
)
504504
}
505505

506-
fn consider_builtin_unsize_and_upcast_candidates(
506+
fn consider_builtin_unsize_candidates(
507507
_ecx: &mut EvalCtxt<'_, 'tcx>,
508508
goal: Goal<'tcx, Self>,
509509
) -> Vec<(CanonicalResponse<'tcx>, BuiltinImplSource)> {

compiler/rustc_trait_selection/src/solve/search_graph/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use cache::ProvisionalCache;
99
use overflow::OverflowData;
1010
use rustc_index::IndexVec;
1111
use rustc_middle::dep_graph::DepKind;
12-
use rustc_middle::traits::solve::{
13-
CanonicalInput, Certainty, EvaluationCache, MaybeCause, QueryResult,
14-
};
12+
use rustc_middle::traits::solve::{CanonicalInput, Certainty, EvaluationCache, QueryResult};
1513
use rustc_middle::ty::TyCtxt;
1614
use std::{collections::hash_map::Entry, mem};
1715

@@ -146,11 +144,7 @@ impl<'tcx> SearchGraph<'tcx> {
146144
{
147145
Err(cache.provisional_result(entry_index))
148146
} else {
149-
Err(super::response_no_constraints(
150-
tcx,
151-
input,
152-
Certainty::Maybe(MaybeCause::Overflow),
153-
))
147+
Err(super::response_no_constraints(tcx, input, Certainty::OVERFLOW))
154148
}
155149
}
156150
}

compiler/rustc_trait_selection/src/solve/search_graph/overflow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_infer::infer::canonical::Canonical;
22
use rustc_infer::traits::query::NoSolution;
3-
use rustc_middle::traits::solve::{Certainty, MaybeCause, QueryResult};
3+
use rustc_middle::traits::solve::{Certainty, QueryResult};
44
use rustc_middle::ty::TyCtxt;
55
use rustc_session::Limit;
66

@@ -115,6 +115,6 @@ impl<'tcx> SearchGraph<'tcx> {
115115
goal: Canonical<'tcx, impl Sized>,
116116
) -> QueryResult<'tcx> {
117117
self.overflow_data.deal_with_overflow();
118-
response_no_constraints(tcx, goal, Certainty::Maybe(MaybeCause::Overflow))
118+
response_no_constraints(tcx, goal, Certainty::OVERFLOW)
119119
}
120120
}

0 commit comments

Comments
 (0)