Closed
Description
Silent corruption, from this user post
The following code compiles and runs, but produces bogus result. PartialEq and thus Expr are not object safe, and because of that Box<Expr>
should have been rejected.
use std::fmt::Debug;
trait Expr: Debug + PartialEq {
fn print_element_count(&self);
}
//#[derive(PartialEq)]
#[derive(Debug)]
struct SExpr<'x> {
elements: Vec<Box<Expr+ 'x>>,
}
impl<'x> PartialEq for SExpr<'x> {
fn eq(&self, other:&SExpr<'x>) -> bool {
println!("L1: {} L2: {}", self.elements.len(), other.elements.len());
let result = self.elements.len() == other.elements.len();
println!("Got compare {}", result);
return result;
}
}
impl <'x> SExpr<'x> {
fn new() -> SExpr<'x> { return SExpr{elements: Vec::new(),}; }
}
impl <'x> Expr for SExpr<'x> {
fn print_element_count(&self) {
println!("element count: {}", self.elements.len());
}
}
fn main() {
let a: Box<Expr> = Box::new(SExpr::new());
let b: Box<Expr> = Box::new(SExpr::new());
a.print_element_count();
b.print_element_count();
assert_eq!(a , b);
}
Just this is the ICE case in #18956
fn main() {
let x: Box<PartialEq> = Box::new(1);
}