Skip to content

Commit 2d1aa57

Browse files
committed
Auto merge of rust-lang#101860 - oli-obk:information_throwing, r=compiler-errors
Don't throw away information just to recompute it again also allows making some functions private.
2 parents 22f6aec + 8aed75b commit 2d1aa57

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

compiler/rustc_infer/src/infer/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10221022
cause: &ObligationCause<'tcx>,
10231023
param_env: ty::ParamEnv<'tcx>,
10241024
predicate: ty::PolyCoercePredicate<'tcx>,
1025-
) -> Option<InferResult<'tcx, ()>> {
1025+
) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
10261026
let subtype_predicate = predicate.map_bound(|p| ty::SubtypePredicate {
10271027
a_is_expected: false, // when coercing from `a` to `b`, `b` is expected
10281028
a: p.a,
@@ -1036,7 +1036,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10361036
cause: &ObligationCause<'tcx>,
10371037
param_env: ty::ParamEnv<'tcx>,
10381038
predicate: ty::PolySubtypePredicate<'tcx>,
1039-
) -> Option<InferResult<'tcx, ()>> {
1039+
) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
10401040
// Check for two unresolved inference variables, in which case we can
10411041
// make no progress. This is partly a micro-optimization, but it's
10421042
// also an opportunity to "sub-unify" the variables. This isn't
@@ -1055,12 +1055,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10551055
match (r_a.kind(), r_b.kind()) {
10561056
(&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
10571057
self.inner.borrow_mut().type_variables().sub(a_vid, b_vid);
1058-
return None;
1058+
return Err((a_vid, b_vid));
10591059
}
10601060
_ => {}
10611061
}
10621062

1063-
Some(self.commit_if_ok(|_snapshot| {
1063+
Ok(self.commit_if_ok(|_snapshot| {
10641064
let ty::SubtypePredicate { a_is_expected, a, b } =
10651065
self.replace_bound_vars_with_placeholders(predicate);
10661066

@@ -1848,7 +1848,7 @@ impl<'tcx> TyOrConstInferVar<'tcx> {
18481848

18491849
/// Tries to extract an inference variable from a type, returns `None`
18501850
/// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
1851-
pub fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
1851+
fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
18521852
match *ty.kind() {
18531853
ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
18541854
ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
@@ -1859,7 +1859,7 @@ impl<'tcx> TyOrConstInferVar<'tcx> {
18591859

18601860
/// Tries to extract an inference variable from a constant, returns `None`
18611861
/// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1862-
pub fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
1862+
fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
18631863
match ct.kind() {
18641864
ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
18651865
_ => None,

compiler/rustc_trait_selection/src/traits/fulfill.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,14 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
427427
obligation.param_env,
428428
Binder::dummy(subtype),
429429
) {
430-
None => {
430+
Err((a, b)) => {
431431
// None means that both are unresolved.
432-
pending_obligation.stalled_on = vec![
433-
TyOrConstInferVar::maybe_from_ty(subtype.a).unwrap(),
434-
TyOrConstInferVar::maybe_from_ty(subtype.b).unwrap(),
435-
];
432+
pending_obligation.stalled_on =
433+
vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
436434
ProcessResult::Unchanged
437435
}
438-
Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
439-
Some(Err(err)) => {
436+
Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
437+
Ok(Err(err)) => {
440438
let expected_found =
441439
ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
442440
ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
@@ -453,16 +451,14 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
453451
obligation.param_env,
454452
Binder::dummy(coerce),
455453
) {
456-
None => {
454+
Err((a, b)) => {
457455
// None means that both are unresolved.
458-
pending_obligation.stalled_on = vec![
459-
TyOrConstInferVar::maybe_from_ty(coerce.a).unwrap(),
460-
TyOrConstInferVar::maybe_from_ty(coerce.b).unwrap(),
461-
];
456+
pending_obligation.stalled_on =
457+
vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)];
462458
ProcessResult::Unchanged
463459
}
464-
Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
465-
Some(Err(err)) => {
460+
Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
461+
Ok(Err(err)) => {
466462
let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
467463
ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
468464
expected_found,

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -462,31 +462,31 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
462462
let p = bound_predicate.rebind(p);
463463
// Does this code ever run?
464464
match self.infcx.subtype_predicate(&obligation.cause, obligation.param_env, p) {
465-
Some(Ok(InferOk { mut obligations, .. })) => {
465+
Ok(Ok(InferOk { mut obligations, .. })) => {
466466
self.add_depth(obligations.iter_mut(), obligation.recursion_depth);
467467
self.evaluate_predicates_recursively(
468468
previous_stack,
469469
obligations.into_iter(),
470470
)
471471
}
472-
Some(Err(_)) => Ok(EvaluatedToErr),
473-
None => Ok(EvaluatedToAmbig),
472+
Ok(Err(_)) => Ok(EvaluatedToErr),
473+
Err(..) => Ok(EvaluatedToAmbig),
474474
}
475475
}
476476

477477
ty::PredicateKind::Coerce(p) => {
478478
let p = bound_predicate.rebind(p);
479479
// Does this code ever run?
480480
match self.infcx.coerce_predicate(&obligation.cause, obligation.param_env, p) {
481-
Some(Ok(InferOk { mut obligations, .. })) => {
481+
Ok(Ok(InferOk { mut obligations, .. })) => {
482482
self.add_depth(obligations.iter_mut(), obligation.recursion_depth);
483483
self.evaluate_predicates_recursively(
484484
previous_stack,
485485
obligations.into_iter(),
486486
)
487487
}
488-
Some(Err(_)) => Ok(EvaluatedToErr),
489-
None => Ok(EvaluatedToAmbig),
488+
Ok(Err(_)) => Ok(EvaluatedToErr),
489+
Err(..) => Ok(EvaluatedToAmbig),
490490
}
491491
}
492492

0 commit comments

Comments
 (0)