Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Infer ty for binary operators #107567

Closed
Closed
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
18 changes: 2 additions & 16 deletions compiler/rustc_const_eval/src/transform/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_middle::mir::{
traversal, AggregateKind, BasicBlock, BinOp, Body, BorrowKind, CastKind, CopyNonOverlapping,
Local, Location, MirPass, MirPhase, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef,
ProjectionElem, RetagKind, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind,
Terminator, TerminatorKind, UnOp, START_BLOCK,
Terminator, TerminatorKind, START_BLOCK,
};
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitable};
use rustc_mir_dataflow::impls::MaybeStorageLive;
Expand Down Expand Up @@ -573,21 +573,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
_ => self.fail(location, format!("There is no checked version of {:?}", op)),
}
}
Rvalue::UnaryOp(op, operand) => {
let a = operand.ty(&self.body.local_decls, self.tcx);
match op {
UnOp::Neg => {
check_kinds!(a, "Cannot negate type {:?}", ty::Int(..) | ty::Float(..))
}
UnOp::Not => {
check_kinds!(
a,
"Cannot binary not type {:?}",
ty::Int(..) | ty::Uint(..) | ty::Bool
);
}
}
}
Rvalue::UnaryOp(..) => {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validation should not be removed without at least a valid explanation for why it's not necessary anymore 😓

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @JakobDegen who added this validation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too late last night, this should be a Draft.
I just found more document for this validations, 6343691

Rvalue::ShallowInitBox(operand, _) => {
let a = operand.ty(&self.body.local_decls, self.tcx);
check_kinds!(a, "Cannot shallow init type {:?}", ty::RawPtr(..));
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut oprnd_t = self.check_expr_with_expectation(&oprnd, expected_inner);

if !oprnd_t.references_error() {
match (unop, oprnd_t.kind()) {
Copy link
Member

@compiler-errors compiler-errors Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inference strategy is incorrect. There's no guarantee that the input and output type of Neg or Not are related, and this causes an ICE in the compiler:

#[derive(Copy, Clone, Default)]
struct A;

struct B;

impl std::ops::Not for A {
    type Output = B;

    fn not(self) -> B { B }
}

fn main() {
    let x = Default::default();
    let y: A = !x;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this example x is constrained to be A because of the inference, but that would imply that y is type B, not A.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense 👍
this is a trivial difference here, binary op is more eager, so unary will infer type error.

pub fn bools(x: &Vec<bool>) {
    let binary = |i, a: &Vec<bool>| {
        a[i] && a[i+1] // ok
    };

    let unary = |i, a: &Vec<bool>| {
        !a[i] // cannot infer type
    };

    binary(0, x);
    unary(0, x);
}

Seems we can not find proper fix for this scenario?

(hir::UnOp::Not | hir::UnOp::Neg, ty::Infer(ty::TyVar(_))) => {
return oprnd_t;
}
_ => {}
}

oprnd_t = self.structurally_resolved_type(expr.span, oprnd_t);
match unop {
hir::UnOp::Deref => {
Expand Down
60 changes: 60 additions & 0 deletions tests/ui/inference/issue-106138.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// check-pass

fn myfunction(x: &Vec<bool>, y: &Vec<i32> ) {
let one = |i, a: &Vec<bool>| {
a[i] // ok
};

let two = |i, a: &Vec<bool>| {
!a[i] // cannot infer type
};

let three = |i, b: &Vec<i32>| {
-b[i] // ok
};

let r = one(0, x);
assert_eq!(r, x[0]);
let r = two(0, x);
assert_eq!(r, !x[0]);
let r = three(0, y);
assert_eq!(r, -y[0]);
}

fn bools(x: &Vec<bool>) {
let binary = |i, a: &Vec<bool>| {
a[i] && a[i+1] // ok
};

let unary = |i, a: &Vec<bool>| {
!a[i] // cannot infer type
};

let r = binary(0, x);
assert_eq!(r, x[0] && x[1]);

let r = unary(0, x);
assert_eq!(r, !x[0]);
}

fn ints(x: &Vec<i32>) {
let binary = |i, a: &Vec<i32>| {
a[i] + a[i+1] // ok
};
let unary = |i, a: &Vec<i32>| {
-a[i] // cannot infer type
};

let r = binary(0, x);
assert_eq!(r, x[0] + x[1]);
let r = unary(0, x);
assert_eq!(r, -x[0]);
}

fn main() {
let x = vec![true, false];
let y = vec![1, 2, 3];
myfunction(&x, &y);
bools(&x);
ints(&y);
}