Skip to content

Commit 985b48b

Browse files
committed
Added more consteval tests and fixed consteval result
1 parent e01f82d commit 985b48b

File tree

4 files changed

+18
-45
lines changed

4 files changed

+18
-45
lines changed

crates/hir-ty/src/consteval.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@ impl Display for ComputedExpr {
122122
}
123123
}
124124

125-
impl ComputedExpr {
126-
pub fn enum_value(&self) -> Option<ComputedExpr> {
127-
match self {
128-
ComputedExpr::Enum(_, _, lit) => Some(ComputedExpr::Literal(lit.clone())),
129-
_ => None,
130-
}
131-
}
132-
}
133-
134125
fn scalar_max(scalar: &Scalar) -> i128 {
135126
match scalar {
136127
Scalar::Bool => 1,
@@ -201,11 +192,7 @@ pub fn eval_const(
201192
}
202193
_ => 0,
203194
};
204-
Ok(ComputedExpr::Enum(
205-
get_name(variant, ctx),
206-
variant,
207-
Literal::Int(value, Some(BuiltinInt::I128)),
208-
))
195+
Ok(ComputedExpr::Literal(Literal::Int(value, Some(BuiltinInt::I128))))
209196
}
210197
_ => Err(ConstEvalError::IncompleteExpr),
211198
},
@@ -404,12 +391,9 @@ pub fn eval_const(
404391
_ => Err(ConstEvalError::NotSupported("path that are not const or local")),
405392
}
406393
}
407-
Expr::Cast { expr, .. } => match eval_const(*expr, ctx, None)? {
394+
&Expr::Cast { expr, .. } => match eval_const(expr, ctx, None)? {
408395
ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)),
409-
expr => Err(ConstEvalError::NotSupported(Box::leak(Box::new(format!(
410-
"Can't cast type: {:?}",
411-
expr
412-
))))),
396+
_ => Err(ConstEvalError::NotSupported("Can't cast these types")),
413397
},
414398
_ => Err(ConstEvalError::NotSupported("This kind of expression")),
415399
}

crates/hir-ty/src/consteval/tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ fn enums() {
100100
"#,
101101
6,
102102
);
103+
check_number(
104+
r#"
105+
enum E { F1 = 1, F2, }
106+
const GOAL: i32 = E::F2 as u8;
107+
"#,
108+
2,
109+
);
110+
check_number(
111+
r#"
112+
enum E { F1, }
113+
const GOAL: i32 = E::F1 as u8;
114+
"#,
115+
0,
116+
);
103117
let r = eval_goal(
104118
r#"
105119
enum E { A = 1, }

crates/ide/src/hover/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ pub(super) fn definition(
348348
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
349349
if it.parent.is_data_carrying(db) {
350350
match it.eval(db) {
351-
Ok(x) => Some(format!("{}", x.enum_value().unwrap_or(x))),
351+
Ok(x) => Some(format!("{}", x)),
352352
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
353353
}
354354
} else {

crates/ide/src/hover/tests.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3502,31 +3502,6 @@ impl<const LEN: usize> Foo<LEN$0> {}
35023502

35033503
#[test]
35043504
fn hover_const_eval_variant() {
3505-
check(
3506-
r#"
3507-
#[repr(u8)]
3508-
enum E {
3509-
A = 4,
3510-
/// This is a doc
3511-
B$0 = E::A as u8 + 1,
3512-
}
3513-
"#,
3514-
expect![[r#"
3515-
*B*
3516-
3517-
```rust
3518-
test::E
3519-
```
3520-
3521-
```rust
3522-
B = 5
3523-
```
3524-
3525-
---
3526-
3527-
This is a doc
3528-
"#]],
3529-
);
35303505
// show hex for <10
35313506
check(
35323507
r#"

0 commit comments

Comments
 (0)