Skip to content

Commit 301b889

Browse files
committed
Added more consteval tests and fixed consteval result
1 parent ad0a6bf commit 301b889

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
@@ -121,15 +121,6 @@ impl Display for ComputedExpr {
121121
}
122122
}
123123

124-
impl ComputedExpr {
125-
pub fn enum_value(&self) -> Option<ComputedExpr> {
126-
match self {
127-
ComputedExpr::Enum(_, _, lit) => Some(ComputedExpr::Literal(lit.clone())),
128-
_ => None,
129-
}
130-
}
131-
}
132-
133124
fn scalar_max(scalar: &Scalar) -> i128 {
134125
match scalar {
135126
Scalar::Bool => 1,
@@ -200,11 +191,7 @@ pub fn eval_const(
200191
}
201192
_ => 0,
202193
};
203-
Ok(ComputedExpr::Enum(
204-
get_name(variant, ctx),
205-
variant,
206-
Literal::Int(value, Some(BuiltinInt::I128)),
207-
))
194+
Ok(ComputedExpr::Literal(Literal::Int(value, Some(BuiltinInt::I128))))
208195
}
209196
_ => Err(ConstEvalError::IncompleteExpr),
210197
},
@@ -403,12 +390,9 @@ pub fn eval_const(
403390
_ => Err(ConstEvalError::NotSupported("path that are not const or local")),
404391
}
405392
}
406-
Expr::Cast { expr, .. } => match eval_const(*expr, ctx, None)? {
393+
&Expr::Cast { expr, .. } => match eval_const(expr, ctx, None)? {
407394
ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)),
408-
expr => Err(ConstEvalError::NotSupported(Box::leak(Box::new(format!(
409-
"Can't cast type: {:?}",
410-
expr
411-
))))),
395+
_ => Err(ConstEvalError::NotSupported("Can't cast these types")),
412396
},
413397
_ => Err(ConstEvalError::NotSupported("This kind of expression")),
414398
}

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
@@ -351,7 +351,7 @@ pub(super) fn definition(
351351
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
352352
if it.parent.is_data_carrying(db) {
353353
match it.eval(db) {
354-
Ok(x) => Some(format!("{}", x.enum_value().unwrap_or(x))),
354+
Ok(x) => Some(format!("{}", x)),
355355
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
356356
}
357357
} else {

crates/ide/src/hover/tests.rs

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

35313531
#[test]
35323532
fn hover_const_eval_variant() {
3533-
check(
3534-
r#"
3535-
#[repr(u8)]
3536-
enum E {
3537-
A = 4,
3538-
/// This is a doc
3539-
B$0 = E::A as u8 + 1,
3540-
}
3541-
"#,
3542-
expect![[r#"
3543-
*B*
3544-
3545-
```rust
3546-
test::E
3547-
```
3548-
3549-
```rust
3550-
B = 5
3551-
```
3552-
3553-
---
3554-
3555-
This is a doc
3556-
"#]],
3557-
);
35583533
// show hex for <10
35593534
check(
35603535
r#"

0 commit comments

Comments
 (0)