-
Notifications
You must be signed in to change notification settings - Fork 13k
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
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, |
@@ -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 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;
}
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.
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.
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?
} | ||
} | ||
} | ||
Rvalue::UnaryOp(..) => {} |
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
@rustbot author |
☔ The latest upstream changes (presumably #107267) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #106138