]> git.lizzy.rs Git - rust.git/commitdiff
Added more consteval tests and fixed consteval result
authorOleStrohm <strohm99@gmail.com>
Sun, 7 Aug 2022 16:42:59 +0000 (18:42 +0200)
committerOleStrohm <strohm99@gmail.com>
Mon, 12 Sep 2022 19:20:45 +0000 (20:20 +0100)
crates/hir-ty/src/consteval.rs
crates/hir-ty/src/consteval/tests.rs
crates/ide/src/hover/render.rs
crates/ide/src/hover/tests.rs

index 141652db73a7e8d8a6aa15477236fba560cd883e..11d93d19c3cfcf537bd5e21ab2fb95be50d8ec94 100644 (file)
@@ -121,15 +121,6 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     }
 }
 
-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")),
     }
index 357d43d225317cd108c74fb64e07fcadbf33cc95..b76506f6ebc2c867f3b19dcdfcaf31886f60527e 100644 (file)
@@ -100,6 +100,20 @@ enum E {
     "#,
         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, }
index 486739628f2aca6fc913916d36626838143e5ef9..f7cdc9e5b2fdaf48b9d05c1abf1cb59a53f54097 100644 (file)
@@ -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 {
index 362b9fa815d37965863806d0ff2e49467f1adffe..eb997e6fef83022f726dec8a2ff0e5cbcd6fdc86 100644 (file)
@@ -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#"