Skip to content

Commit d5f3863

Browse files
committed
Consider lifetimes when comparing types for equality in MIR validator
1 parent b768f24 commit d5f3863

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

compiler/rustc_const_eval/src/transform/validate.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
315315
| ty::FnPtr(..)
316316
)
317317
}
318-
// None of the possible types have lifetimes, so we can just compare
319-
// directly
320-
if a != b {
318+
// The function pointer types can have lifetimes
319+
if !self.mir_assign_valid_types(a, b) {
321320
self.fail(
322321
location,
323322
format!("Cannot compare unequal types {:?} and {:?}", a, b),
@@ -464,7 +463,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
464463
};
465464
// since CopyNonOverlapping is parametrized by 1 type,
466465
// we only need to check that they are equal and not keep an extra parameter.
467-
if op_src_ty != op_dst_ty {
466+
if !self.mir_assign_valid_types(op_src_ty, op_dst_ty) {
468467
self.fail(location, format!("bad arg ({:?} != {:?})", op_src_ty, op_dst_ty));
469468
}
470469

compiler/rustc_middle/src/mir/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,8 @@ pub enum Rvalue<'tcx> {
25182518
/// * `Offset` has the same semantics as [`offset`](pointer::offset), except that the second
25192519
/// parameter may be a `usize` as well.
25202520
/// * The comparison operations accept `bool`s, `char`s, signed or unsigned integers, floats,
2521-
/// raw pointers, or function pointers of matching types and return a `bool`.
2521+
/// raw pointers, or function pointers and return a `bool`. The types of the operands must be
2522+
/// matching, up to the usual caveat of the lifetimes in function pointers.
25222523
/// * Left and right shift operations accept signed or unsigned integers not necessarily of the
25232524
/// same type and return a value of the same type as their LHS. Like in Rust, the RHS is
25242525
/// truncated as needed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// check-pass
2+
// compile-flags: -Zvalidate-mir
3+
4+
fn foo(_a: &str) {}
5+
6+
fn main() {
7+
let x = foo as fn(&'static str);
8+
9+
let _ = x == foo;
10+
}

0 commit comments

Comments
 (0)