Added more consteval tests and fixed consteval result

This commit is contained in:
OleStrohm 2022-08-07 18:42:59 +02:00
parent ad0a6bf1a3
commit 301b8894ea
4 changed files with 18 additions and 45 deletions

View 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")),
}

View 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, }

View 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 {

View 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#"