mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Added more consteval tests and fixed consteval result
This commit is contained in:
parent
ad0a6bf1a3
commit
301b8894ea
4 changed files with 18 additions and 45 deletions
|
@ -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 {
|
fn scalar_max(scalar: &Scalar) -> i128 {
|
||||||
match scalar {
|
match scalar {
|
||||||
Scalar::Bool => 1,
|
Scalar::Bool => 1,
|
||||||
|
@ -200,11 +191,7 @@ pub fn eval_const(
|
||||||
}
|
}
|
||||||
_ => 0,
|
_ => 0,
|
||||||
};
|
};
|
||||||
Ok(ComputedExpr::Enum(
|
Ok(ComputedExpr::Literal(Literal::Int(value, Some(BuiltinInt::I128))))
|
||||||
get_name(variant, ctx),
|
|
||||||
variant,
|
|
||||||
Literal::Int(value, Some(BuiltinInt::I128)),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
_ => Err(ConstEvalError::IncompleteExpr),
|
_ => Err(ConstEvalError::IncompleteExpr),
|
||||||
},
|
},
|
||||||
|
@ -403,12 +390,9 @@ pub fn eval_const(
|
||||||
_ => Err(ConstEvalError::NotSupported("path that are not const or local")),
|
_ => 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)),
|
ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)),
|
||||||
expr => Err(ConstEvalError::NotSupported(Box::leak(Box::new(format!(
|
_ => Err(ConstEvalError::NotSupported("Can't cast these types")),
|
||||||
"Can't cast type: {:?}",
|
|
||||||
expr
|
|
||||||
))))),
|
|
||||||
},
|
},
|
||||||
_ => Err(ConstEvalError::NotSupported("This kind of expression")),
|
_ => Err(ConstEvalError::NotSupported("This kind of expression")),
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,6 +100,20 @@ fn enums() {
|
||||||
"#,
|
"#,
|
||||||
6,
|
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(
|
let r = eval_goal(
|
||||||
r#"
|
r#"
|
||||||
enum E { A = 1, }
|
enum E { A = 1, }
|
||||||
|
|
|
@ -351,7 +351,7 @@ pub(super) fn definition(
|
||||||
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
|
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
|
||||||
if it.parent.is_data_carrying(db) {
|
if it.parent.is_data_carrying(db) {
|
||||||
match it.eval(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)),
|
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3530,31 +3530,6 @@ impl<const LEN: usize> Foo<LEN$0> {}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hover_const_eval_variant() {
|
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
|
// show hex for <10
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue