Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions crates/hir-ty/src/consteval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,21 @@ fn overloaded_binop() {
"#,
1,
);
check_number(
r#"
//- minicore: eq
struct AlwaysEqual(u32);

impl PartialEq for AlwaysEqual {
fn eq(&self, _other: &Self) -> bool {
true
}
}

const GOAL: bool = AlwaysEqual(1) == AlwaysEqual(2);
"#,
1,
);
check_number(
r#"
//- minicore: add
Expand Down
41 changes: 25 additions & 16 deletions crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,11 @@ impl<'a, 'db> MirLowerCtx<'a, 'db> {
}
Expr::BinaryOp { lhs, rhs, op } => {
let op: BinaryOp = op.ok_or(MirLowerError::IncompleteExpr)?;
// Without adjust here is a hack. We assume that we know every possible adjustment
// for binary operator, and use without adjust to simplify our conditions.
let lhs_ty = self.expr_ty_without_adjust(*lhs);
let rhs_ty = self.expr_ty_without_adjust(*rhs);
let is_builtin = 'b: {
// Without adjust here is a hack. We assume that we know every possible adjustment
// for binary operator, and use without adjust to simplify our conditions.
let lhs_ty = self.expr_ty_without_adjust(*lhs);
let rhs_ty = self.expr_ty_without_adjust(*rhs);
if matches!(op, BinaryOp::CmpOp(syntax::ast::CmpOp::Eq { .. }))
&& matches!(lhs_ty.kind(), TyKind::RawPtr(..))
&& matches!(rhs_ty.kind(), TyKind::RawPtr(..))
Expand Down Expand Up @@ -1069,18 +1069,27 @@ impl<'a, 'db> MirLowerCtx<'a, 'db> {
| TyKind::Float(_)
) && (lhs_ty == rhs_ty || builtin_inequal_impls)
};
if !is_builtin
&& let Some((func_id, generic_args)) = self.infer.method_resolution(expr_id)
{
let func = Operand::from_fn(self.db, func_id, generic_args);
return self.lower_call_and_args(
func,
[*lhs, *rhs].into_iter(),
place,
current,
self.is_uninhabited(expr_id),
expr_id.into(),
);
if !is_builtin {
if let Some((func_id, generic_args)) = self.infer.method_resolution(expr_id) {
let func = Operand::from_fn(self.db, func_id, generic_args);
return self.lower_call_and_args(
func,
[*lhs, *rhs].into_iter(),
place,
current,
self.is_uninhabited(expr_id),
expr_id.into(),
);
}
// When PartialEq is unavailable, inference cannot record a method resolution.
// Keep the existing direct equality lowering for same-typed values in that case.
if !matches!(op, BinaryOp::CmpOp(syntax::ast::CmpOp::Eq { .. }))
|| lhs_ty != rhs_ty
{
return Err(MirLowerError::TypeError(
"binary operation was neither builtin nor overloaded",
));
}
}
if let hir_def::hir::BinaryOp::Assignment { op: Some(op) } = op {
// last adjustment is `&mut` which we don't want it.
Expand Down