-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 #[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;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make sense 👍 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 => { | ||
|
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); | ||
} |
There was a problem hiding this comment.
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 😓
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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