Unnamed repository; edit this file 'description' to name the repository.
Rename function and remove flag argument
Use less confusing name for function, duplicate logic rather than taking flag as argument.
riverbl 2024-08-31
parent fbc9d8a · commit db1ccb8
-rw-r--r--crates/hir-ty/src/consteval.rs30
1 files changed, 21 insertions, 9 deletions
diff --git a/crates/hir-ty/src/consteval.rs b/crates/hir-ty/src/consteval.rs
index 3f60834e02..e5c493ac56 100644
--- a/crates/hir-ty/src/consteval.rs
+++ b/crates/hir-ty/src/consteval.rs
@@ -169,23 +169,31 @@ pub fn usize_const(db: &dyn HirDatabase, value: Option<u128>, krate: CrateId) ->
}
pub fn try_const_usize(db: &dyn HirDatabase, c: &Const) -> Option<u128> {
- try_const_usize_sign_extend(db, c, false)
+ match &c.data(Interner).value {
+ chalk_ir::ConstValue::BoundVar(_) => None,
+ chalk_ir::ConstValue::InferenceVar(_) => None,
+ chalk_ir::ConstValue::Placeholder(_) => None,
+ chalk_ir::ConstValue::Concrete(c) => match &c.interned {
+ ConstScalar::Bytes(it, _) => Some(u128::from_le_bytes(pad16(it, false))),
+ ConstScalar::UnevaluatedConst(c, subst) => {
+ let ec = db.const_eval(*c, subst.clone(), None).ok()?;
+ try_const_usize(db, &ec)
+ }
+ _ => None,
+ },
+ }
}
-pub fn try_const_usize_sign_extend(
- db: &dyn HirDatabase,
- c: &Const,
- is_signed: bool,
-) -> Option<u128> {
+pub fn try_const_isize(db: &dyn HirDatabase, c: &Const) -> Option<i128> {
match &c.data(Interner).value {
chalk_ir::ConstValue::BoundVar(_) => None,
chalk_ir::ConstValue::InferenceVar(_) => None,
chalk_ir::ConstValue::Placeholder(_) => None,
chalk_ir::ConstValue::Concrete(c) => match &c.interned {
- ConstScalar::Bytes(it, _) => Some(u128::from_le_bytes(pad16(it, is_signed))),
+ ConstScalar::Bytes(it, _) => Some(i128::from_le_bytes(pad16(it, true))),
ConstScalar::UnevaluatedConst(c, subst) => {
let ec = db.const_eval(*c, subst.clone(), None).ok()?;
- try_const_usize_sign_extend(db, &ec, is_signed)
+ try_const_isize(db, &ec)
}
_ => None,
},
@@ -287,7 +295,11 @@ pub(crate) fn const_eval_discriminant_variant(
db.trait_environment_for_body(def),
)?;
let c = interpret_mir(db, mir_body, false, None).0?;
- let c = try_const_usize_sign_extend(db, &c, is_signed).unwrap() as i128;
+ let c = if is_signed {
+ try_const_isize(db, &c).unwrap()
+ } else {
+ try_const_usize(db, &c).unwrap() as i128
+ };
Ok(c)
}