Extend Infer ty for binary operators#107567
Extend Infer ty for binary operators#107567chenyukang wants to merge 1 commit intorust-lang:masterfrom
Conversation
|
(rustbot has picked a reviewer for you, use r? to override) |
|
This may a little bit aggressive, my intution is when the operand ty is a Infer var, With this change, |
| 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.
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;
}There was a problem hiding this comment.
In this example x is constrained to be A because of the inference, but that would imply that y is type B, not A.
There was a problem hiding this comment.
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?
| } | ||
| } | ||
| } | ||
| Rvalue::UnaryOp(..) => {} |
There was a problem hiding this comment.
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.
It's too late last night, this should be a Draft.
I just found more document for this validations, 6343691
|
@rustbot author |
|
☔ The latest upstream changes (presumably #107267) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #106138