Skip to content

Commit eb286dd

Browse files
Make can_eq and can_sub return booleans
1 parent 0b439b1 commit eb286dd

File tree

13 files changed

+33
-43
lines changed

13 files changed

+33
-43
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11441144
LateBoundRegionConversionTime::FnCall,
11451145
tcx.fn_sig(method_did).subst(tcx, method_substs).input(0),
11461146
)
1147-
&& infcx.can_eq(self.param_env, ty, self_ty).is_ok()
1147+
&& infcx.can_eq(self.param_env, ty, self_ty)
11481148
{
11491149
err.span_suggestion_verbose(
11501150
fn_call_span.shrink_to_lo(),

compiler/rustc_hir_analysis/src/astconv/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22322232
tcx.erase_regions(impl_.self_ty()),
22332233
tcx.erase_regions(qself_ty),
22342234
)
2235-
.is_ok()
22362235
})
22372236
&& tcx.impl_polarity(impl_def_id) != ty::ImplPolarity::Negative
22382237
})

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ fn compare_self_type<'tcx>(
11161116

11171117
let infcx = tcx.infer_ctxt().build();
11181118
let self_arg_ty = tcx.liberate_late_bound_regions(method.def_id, self_arg_ty);
1119-
let can_eq_self = |ty| infcx.can_eq(param_env, untransformed_self_ty, ty).is_ok();
1119+
let can_eq_self = |ty| infcx.can_eq(param_env, untransformed_self_ty, ty);
11201120
match ExplicitSelf::determine(self_arg_ty, can_eq_self) {
11211121
ExplicitSelf::ByValue => "self".to_owned(),
11221122
ExplicitSelf::ByReference(_, hir::Mutability::Not) => "&self".to_owned(),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ fn receiver_is_valid<'tcx>(
17161716
let cause =
17171717
ObligationCause::new(span, wfcx.body_def_id, traits::ObligationCauseCode::MethodReceiver);
17181718

1719-
let can_eq_self = |ty| infcx.can_eq(wfcx.param_env, self_ty, ty).is_ok();
1719+
let can_eq_self = |ty| infcx.can_eq(wfcx.param_env, self_ty, ty);
17201720

17211721
// `self: Self` is always valid.
17221722
if can_eq_self(receiver_ty) {

compiler/rustc_hir_typeck/src/demand.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
321321
let mut param_args = FxHashMap::default();
322322
let mut param_expected = FxHashMap::default();
323323
let mut param_found = FxHashMap::default();
324-
if self.can_eq(self.param_env, ty, found).is_ok() {
324+
if self.can_eq(self.param_env, ty, found) {
325325
// We only point at the first place where the found type was inferred.
326326
for (i, param_ty) in sig.inputs().skip_binder().iter().skip(1).enumerate() {
327327
if def_self_ty.contains(*param_ty) && let ty::Param(_) = param_ty.kind() {
@@ -369,7 +369,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
369369
for (param, (arg, arg_ty)) in param_args.iter() {
370370
let Some(expected) = param_expected.get(param) else { continue; };
371371
let Some(found) = param_found.get(param) else { continue; };
372-
if self.can_eq(self.param_env, *arg_ty, *found).is_err() { continue; }
372+
if !self.can_eq(self.param_env, *arg_ty, *found) { continue; }
373373
self.emit_coerce_suggestions(err, arg, *found, *expected, None, None);
374374
}
375375

@@ -379,7 +379,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
379379
}
380380
if ty != prev
381381
&& param_args.is_empty()
382-
&& self.can_eq(self.param_env, ty, found).is_ok()
382+
&& self.can_eq(self.param_env, ty, found)
383383
{
384384
// We only point at the first place where the found type was inferred.
385385
if !segment.ident.span.overlaps(mismatch_span) {
@@ -401,7 +401,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
401401
}
402402
if ty != prev
403403
&& let Some(span) = prev_span
404-
&& self.can_eq(self.param_env, ty, found).is_ok()
404+
&& self.can_eq(self.param_env, ty, found)
405405
{
406406
// We only point at the first place where the found type was inferred.
407407
// We use the *previous* span because if the type is known *here* it means
@@ -764,7 +764,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
764764
if let ty::Adt(expected_adt, substs) = expected.kind() {
765765
if let hir::ExprKind::Field(base, ident) = expr.kind {
766766
let base_ty = self.typeck_results.borrow().expr_ty(base);
767-
if self.can_eq(self.param_env, base_ty, expected).is_ok()
767+
if self.can_eq(self.param_env, base_ty, expected)
768768
&& let Some(base_span) = base.span.find_ancestor_inside(expr.span)
769769
{
770770
err.span_suggestion_verbose(
@@ -1357,7 +1357,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13571357
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, ref expr),
13581358
_,
13591359
&ty::Ref(_, checked, _),
1360-
) if self.can_sub(self.param_env, checked, expected).is_ok() => {
1360+
) if self.can_sub(self.param_env, checked, expected) => {
13611361
// We have `&T`, check if what was expected was `T`. If so,
13621362
// we may want to suggest removing a `&`.
13631363
if sm.is_imported(expr.span) {
@@ -2003,7 +2003,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20032003
};
20042004
let hir::StmtKind::Semi(tail_expr) = stmt.kind else { return; };
20052005
let Some(ty) = self.node_ty_opt(tail_expr.hir_id) else { return; };
2006-
if self.can_eq(self.param_env, expected_ty, ty).is_ok() {
2006+
if self.can_eq(self.param_env, expected_ty, ty) {
20072007
err.span_suggestion_short(
20082008
stmt.span.with_lo(tail_expr.span.hi()),
20092009
"remove this semicolon",

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10151015
let expr_inner_ty = substs.type_at(0);
10161016
let expected_inner_ty = expected_substs.type_at(0);
10171017
if let ty::Ref(_, ty, hir::Mutability::Not) = expr_inner_ty.kind()
1018-
&& self.can_eq(self.param_env, *ty, expected_inner_ty).is_ok()
1018+
&& self.can_eq(self.param_env, *ty, expected_inner_ty)
10191019
{
10201020
let def_path = self.tcx.def_path_str(adt_def.did());
10211021
if self.type_is_copy_modulo_regions(self.param_env, *ty, expr.span) {
@@ -1054,7 +1054,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10541054
if let Some(result_did) = self.tcx.get_diagnostic_item(sym::Result)
10551055
&& adt_def.did() == result_did
10561056
// Check that the error types are equal
1057-
&& self.can_eq(self.param_env, substs.type_at(1), expected_substs.type_at(1)).is_ok()
1057+
&& self.can_eq(self.param_env, substs.type_at(1), expected_substs.type_at(1))
10581058
{
10591059
return suggest_copied_or_cloned();
10601060
} else if let Some(option_did) = self.tcx.get_diagnostic_item(sym::Option)

compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
936936
return false;
937937
}
938938
}
939-
self.can_sub(self.param_env, fty.output(), expected).is_ok()
939+
self.can_sub(self.param_env, fty.output(), expected)
940940
}),
941941
_ => false,
942942
}

compiler/rustc_hir_typeck/src/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
9090
self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)?;
9191
if let Some(expr) = ti.origin_expr {
9292
self.suggest_fn_call(&mut diag, expr, expected, |output| {
93-
self.can_eq(self.param_env, output, actual).is_ok()
93+
self.can_eq(self.param_env, output, actual)
9494
});
9595
}
9696
Some(diag)
@@ -675,7 +675,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
675675
) {
676676
match (expected.kind(), actual.kind(), ba) {
677677
(ty::Ref(_, inner_ty, _), _, hir::BindingAnnotation::NONE)
678-
if self.can_eq(self.param_env, *inner_ty, actual).is_ok() =>
678+
if self.can_eq(self.param_env, *inner_ty, actual) =>
679679
{
680680
err.span_suggestion_verbose(
681681
span.shrink_to_lo(),
@@ -685,7 +685,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
685685
);
686686
}
687687
(_, ty::Ref(_, inner_ty, _), hir::BindingAnnotation::REF)
688-
if self.can_eq(self.param_env, expected, *inner_ty).is_ok() =>
688+
if self.can_eq(self.param_env, expected, *inner_ty) =>
689689
{
690690
err.span_suggestion_verbose(
691691
span.with_hi(span.lo() + BytePos(4)),

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ fn foo(&self) -> Self::T { String::new() }
576576
tcx.impl_defaultness(item.id.owner_id)
577577
{
578578
let assoc_ty = tcx.bound_type_of(item.id.owner_id).subst_identity();
579-
if self.infcx.can_eq(param_env, assoc_ty, found).is_ok() {
579+
if self.infcx.can_eq(param_env, assoc_ty, found) {
580580
diag.span_label(
581581
item.span,
582582
"associated type defaults can't be assumed inside the \
@@ -598,7 +598,7 @@ fn foo(&self) -> Self::T { String::new() }
598598
if let hir::AssocItemKind::Type = item.kind {
599599
let assoc_ty = tcx.bound_type_of(item.id.owner_id).subst_identity();
600600

601-
if self.infcx.can_eq(param_env, assoc_ty, found).is_ok() {
601+
if self.infcx.can_eq(param_env, assoc_ty, found) {
602602
diag.span_label(item.span, "expected this associated type");
603603
return true;
604604
}

compiler/rustc_infer/src/infer/mod.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -880,30 +880,20 @@ impl<'tcx> InferCtxt<'tcx> {
880880
self.inner.borrow_mut().unwrap_region_constraints().add_given(sub, sup);
881881
}
882882

883-
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
883+
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
884884
where
885885
T: at::ToTrace<'tcx>,
886886
{
887887
let origin = &ObligationCause::dummy();
888-
self.probe(|_| {
889-
self.at(origin, param_env).sub(a, b).map(|InferOk { obligations: _, .. }| {
890-
// Ignore obligations, since we are unrolling
891-
// everything anyway.
892-
})
893-
})
888+
self.probe(|_| self.at(origin, param_env).sub(a, b).is_ok())
894889
}
895890

896-
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
891+
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
897892
where
898893
T: at::ToTrace<'tcx>,
899894
{
900895
let origin = &ObligationCause::dummy();
901-
self.probe(|_| {
902-
self.at(origin, param_env).eq(a, b).map(|InferOk { obligations: _, .. }| {
903-
// Ignore obligations, since we are unrolling
904-
// everything anyway.
905-
})
906-
})
896+
self.probe(|_| self.at(origin, param_env).eq(a, b).is_ok())
907897
}
908898

909899
#[instrument(skip(self), level = "debug")]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16291629
// Eventually I'll need to implement param-env-aware
16301630
// `Γ₁ ⊦ φ₁ => Γ₂ ⊦ φ₂` logic.
16311631
let param_env = ty::ParamEnv::empty();
1632-
if self.can_sub(param_env, error, implication).is_ok() {
1632+
if self.can_sub(param_env, error, implication) {
16331633
debug!("error_implies: {:?} -> {:?} -> {:?}", cond, error, implication);
16341634
return true;
16351635
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
7272

7373
let impl_self_ty = impl_trait_ref.self_ty();
7474

75-
if let Ok(..) = self.can_eq(param_env, trait_self_ty, impl_self_ty) {
75+
if self.can_eq(param_env, trait_self_ty, impl_self_ty) {
7676
self_match_impls.push((def_id, impl_substs));
7777

7878
if iter::zip(

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
748748
let real_ty = real_trait_pred.self_ty();
749749
// We `erase_late_bound_regions` here because `make_subregion` does not handle
750750
// `ReLateBound`, and we don't particularly care about the regions.
751-
if self
752-
.can_eq(obligation.param_env, self.tcx.erase_late_bound_regions(real_ty), arg_ty)
753-
.is_err()
754-
{
751+
if !self.can_eq(
752+
obligation.param_env,
753+
self.tcx.erase_late_bound_regions(real_ty),
754+
arg_ty,
755+
) {
755756
continue;
756757
}
757758

@@ -3690,7 +3691,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
36903691
let Some((span, (assoc, ty))) = entry else { continue; };
36913692
if primary_spans.is_empty() || type_diffs.iter().any(|diff| {
36923693
let Sorts(expected_found) = diff else { return false; };
3693-
self.can_eq(param_env, expected_found.found, ty).is_ok()
3694+
self.can_eq(param_env, expected_found.found, ty)
36943695
}) {
36953696
// FIXME: this doesn't quite work for `Iterator::collect`
36963697
// because we have `Vec<i32>` and `()`, but we'd want `i32`
@@ -3717,10 +3718,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
37173718
let ty_str = with_forced_trimmed_paths!(self.ty_to_string(ty));
37183719

37193720
let assoc = with_forced_trimmed_paths!(self.tcx.def_path_str(assoc));
3720-
if self.can_eq(param_env, ty, *prev_ty).is_err() {
3721+
if !self.can_eq(param_env, ty, *prev_ty) {
37213722
if type_diffs.iter().any(|diff| {
37223723
let Sorts(expected_found) = diff else { return false; };
3723-
self.can_eq(param_env, expected_found.found, ty).is_ok()
3724+
self.can_eq(param_env, expected_found.found, ty)
37243725
}) {
37253726
primary_spans.push(span);
37263727
}
@@ -3868,7 +3869,7 @@ fn hint_missing_borrow<'tcx>(
38683869
let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
38693870
let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);
38703871

3871-
if infcx.can_eq(param_env, found_ty, expected_ty).is_ok() {
3872+
if infcx.can_eq(param_env, found_ty, expected_ty) {
38723873
// FIXME: This could handle more exotic cases like mutability mismatches too!
38733874
if found_refs.len() < expected_refs.len()
38743875
&& found_refs[..] == expected_refs[expected_refs.len() - found_refs.len()..]

0 commit comments

Comments
 (0)