diff options
| author | OleStrohm <strohm99@gmail.com> | 2022-08-07 18:42:59 +0200 |
|---|---|---|
| committer | OleStrohm <strohm99@gmail.com> | 2022-09-12 20:20:45 +0100 |
| commit | 301b8894ead191cb33e8aa72ce74f51332fee274 (patch) | |
| tree | 56d5da5212b25c8e7afd1fe16e05a3d56a6b948e | |
| parent | ad0a6bf1a3eda25698e9ce5cea0743c2cdf9f051 (diff) | |
| download | rust-301b8894ead191cb33e8aa72ce74f51332fee274.tar.gz rust-301b8894ead191cb33e8aa72ce74f51332fee274.zip | |
Added more consteval tests and fixed consteval result
| -rw-r--r-- | crates/hir-ty/src/consteval.rs | 22 | ||||
| -rw-r--r-- | crates/hir-ty/src/consteval/tests.rs | 14 | ||||
| -rw-r--r-- | crates/ide/src/hover/render.rs | 2 | ||||
| -rw-r--r-- | crates/ide/src/hover/tests.rs | 25 |
4 files changed, 18 insertions, 45 deletions
diff --git a/crates/hir-ty/src/consteval.rs b/crates/hir-ty/src/consteval.rs index 141652db73a..11d93d19c3c 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<ComputedExpr> { - 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 357d43d2253..b76506f6ebc 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 486739628f2..f7cdc9e5b2f 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 362b9fa815d..eb997e6fef8 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -3530,31 +3530,6 @@ impl<const LEN: usize> Foo<LEN$0> {} #[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#" |
