diff --git a/crates/hir-ty/src/consteval.rs b/crates/hir-ty/src/consteval.rs index 141652db73..11d93d19c3 100644 --- a/crates/hir-ty/src/consteval.rs +++ b/crates/hir-ty/src/consteval.rs @@ -121,15 +121,6 @@ impl Display for ComputedExpr { } } -impl ComputedExpr { - pub fn enum_value(&self) -> Option { - match self { - ComputedExpr::Enum(_, _, lit) => Some(ComputedExpr::Literal(lit.clone())), - _ => None, - } - } -} - fn scalar_max(scalar: &Scalar) -> i128 { match scalar { Scalar::Bool => 1, @@ -200,11 +191,7 @@ pub fn eval_const( } _ => 0, }; - Ok(ComputedExpr::Enum( - get_name(variant, ctx), - variant, - Literal::Int(value, Some(BuiltinInt::I128)), - )) + Ok(ComputedExpr::Literal(Literal::Int(value, Some(BuiltinInt::I128)))) } _ => Err(ConstEvalError::IncompleteExpr), }, @@ -403,12 +390,9 @@ pub fn eval_const( _ => Err(ConstEvalError::NotSupported("path that are not const or local")), } } - Expr::Cast { expr, .. } => match eval_const(*expr, ctx, None)? { + &Expr::Cast { expr, .. } => match eval_const(expr, ctx, None)? { ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)), - expr => Err(ConstEvalError::NotSupported(Box::leak(Box::new(format!( - "Can't cast type: {:?}", - expr - ))))), + _ => Err(ConstEvalError::NotSupported("Can't cast these types")), }, _ => Err(ConstEvalError::NotSupported("This kind of expression")), } diff --git a/crates/hir-ty/src/consteval/tests.rs b/crates/hir-ty/src/consteval/tests.rs index 357d43d225..b76506f6eb 100644 --- a/crates/hir-ty/src/consteval/tests.rs +++ b/crates/hir-ty/src/consteval/tests.rs @@ -100,6 +100,20 @@ fn enums() { "#, 6, ); + check_number( + r#" + enum E { F1 = 1, F2, } + const GOAL: i32 = E::F2 as u8; + "#, + 2, + ); + check_number( + r#" + enum E { F1, } + const GOAL: i32 = E::F1 as u8; + "#, + 0, + ); let r = eval_goal( r#" enum E { A = 1, } diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 486739628f..f7cdc9e5b2 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -351,7 +351,7 @@ pub(super) fn definition( Definition::Variant(it) => label_value_and_docs(db, it, |&it| { if it.parent.is_data_carrying(db) { match it.eval(db) { - Ok(x) => Some(format!("{}", x.enum_value().unwrap_or(x))), + Ok(x) => Some(format!("{}", x)), Err(_) => it.value(db).map(|x| format!("{:?}", x)), } } else { diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 362b9fa815..eb997e6fef 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -3530,31 +3530,6 @@ impl Foo {} #[test] fn hover_const_eval_variant() { - check( - r#" -#[repr(u8)] -enum E { - A = 4, - /// This is a doc - B$0 = E::A as u8 + 1, -} -"#, - expect![[r#" - *B* - - ```rust - test::E - ``` - - ```rust - B = 5 - ``` - - --- - - This is a doc - "#]], - ); // show hex for <10 check( r#"