Skip to content

Commit ea2daa3

Browse files
committed
Split ObligationCauseCode::BinOp for unops to UnOp
1 parent 18a36bc commit ea2daa3

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

compiler/rustc_hir_typeck/src/op.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -962,13 +962,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
962962
let (opt_rhs_expr, opt_rhs_ty) = opt_rhs.unzip();
963963
let cause = self.cause(
964964
span,
965-
ObligationCauseCode::BinOp {
966-
lhs_hir_id: lhs_expr.hir_id,
967-
rhs_hir_id: opt_rhs_expr.map(|expr| expr.hir_id),
968-
rhs_span: opt_rhs_expr.map(|expr| expr.span),
969-
rhs_is_lit: opt_rhs_expr
970-
.is_some_and(|expr| matches!(expr.kind, hir::ExprKind::Lit(_))),
971-
output_ty: expected.only_has_type(self),
965+
match opt_rhs_expr {
966+
Some(rhs) => ObligationCauseCode::BinOp {
967+
lhs_hir_id: lhs_expr.hir_id,
968+
rhs_hir_id: rhs.hir_id,
969+
rhs_span: rhs.span,
970+
rhs_is_lit: matches!(rhs.kind, hir::ExprKind::Lit(_)),
971+
output_ty: expected.only_has_type(self),
972+
},
973+
None => ObligationCauseCode::UnOp { hir_id: lhs_expr.hir_id },
972974
},
973975
);
974976

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,14 @@ pub enum ObligationCauseCode<'tcx> {
389389
/// against.
390390
MatchImpl(ObligationCause<'tcx>, DefId),
391391

392+
UnOp {
393+
hir_id: HirId,
394+
},
395+
392396
BinOp {
393397
lhs_hir_id: HirId,
394-
rhs_hir_id: Option<HirId>,
395-
rhs_span: Option<Span>,
398+
rhs_hir_id: HirId,
399+
rhs_span: Span,
396400
rhs_is_lit: bool,
397401
output_ty: Option<Ty<'tcx>>,
398402
},

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
963963
trait_pred: ty::PolyTraitPredicate<'tcx>,
964964
err: &mut Diag<'_>,
965965
) -> bool {
966-
if let ObligationCauseCode::BinOp { lhs_hir_id, .. } = obligation.cause.code()
967-
&& let hir::Node::Expr(expr) = self.tcx.hir_node(*lhs_hir_id)
966+
if let ObligationCauseCode::UnOp { hir_id, .. } = obligation.cause.code()
967+
&& let hir::Node::Expr(expr) = self.tcx.hir_node(*hir_id)
968968
&& let hir::ExprKind::Unary(hir::UnOp::Neg, inner) = expr.kind
969969
&& let hir::ExprKind::Lit(lit) = inner.kind
970970
&& let LitKind::Int(_, LitIntType::Unsuffixed) = lit.node
@@ -2769,9 +2769,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27692769
suggested: bool,
27702770
) {
27712771
let body_def_id = obligation.cause.body_id;
2772-
let span = if let ObligationCauseCode::BinOp { rhs_span: Some(rhs_span), .. } =
2773-
obligation.cause.code()
2774-
{
2772+
let span = if let ObligationCauseCode::BinOp { rhs_span, .. } = obligation.cause.code() {
27752773
*rhs_span
27762774
} else {
27772775
span

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
554554
return true;
555555
}
556556
} else if let (
557-
ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id: Some(rhs_hir_id), .. },
557+
ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, .. },
558558
predicate,
559559
) = code.peel_derives_with_predicate()
560560
&& let Some(typeck_results) = &self.typeck_results
@@ -2801,6 +2801,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
28012801
| ObligationCauseCode::QuestionMark
28022802
| ObligationCauseCode::CheckAssociatedTypeBounds { .. }
28032803
| ObligationCauseCode::LetElse
2804+
| ObligationCauseCode::UnOp { .. }
28042805
| ObligationCauseCode::BinOp { .. }
28052806
| ObligationCauseCode::AscribeUserTypeProvePredicate(..)
28062807
| ObligationCauseCode::AlwaysApplicableImpl
@@ -3839,9 +3840,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
38393840
trait_pred: ty::PolyTraitPredicate<'tcx>,
38403841
) {
38413842
let rhs_span = match obligation.cause.code() {
3842-
ObligationCauseCode::BinOp { rhs_span: Some(span), rhs_is_lit, .. } if *rhs_is_lit => {
3843-
span
3844-
}
3843+
ObligationCauseCode::BinOp { rhs_span, rhs_is_lit, .. } if *rhs_is_lit => rhs_span,
38453844
_ => return,
38463845
};
38473846
if let ty::Float(_) = trait_pred.skip_binder().self_ty().kind()
@@ -5108,16 +5107,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
51085107
let tcx = self.tcx;
51095108
let predicate = predicate.upcast(tcx);
51105109
match *cause_code {
5111-
ObligationCauseCode::BinOp {
5112-
lhs_hir_id,
5113-
rhs_hir_id: Some(rhs_hir_id),
5114-
rhs_span: Some(rhs_span),
5115-
..
5116-
} if let Some(typeck_results) = &self.typeck_results
5117-
&& let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
5118-
&& let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
5119-
&& let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
5120-
&& let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
5110+
ObligationCauseCode::BinOp { lhs_hir_id, rhs_hir_id, rhs_span, .. }
5111+
if let Some(typeck_results) = &self.typeck_results
5112+
&& let hir::Node::Expr(lhs) = tcx.hir_node(lhs_hir_id)
5113+
&& let hir::Node::Expr(rhs) = tcx.hir_node(rhs_hir_id)
5114+
&& let Some(lhs_ty) = typeck_results.expr_ty_opt(lhs)
5115+
&& let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs) =>
51215116
{
51225117
if let Some(pred) = predicate.as_trait_clause()
51235118
&& tcx.is_lang_item(pred.def_id(), LangItem::PartialEq)

0 commit comments

Comments
 (0)