Skip to content

Commit d6573f9

Browse files
committed
Rename function and remove flag argument
Use less confusing name for function, duplicate logic rather than taking flag as argument.
1 parent 5392d56 commit d6573f9

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/consteval.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,31 @@ pub fn usize_const(db: &dyn HirDatabase, value: Option<u128>, krate: CrateId) ->
169169
}
170170

171171
pub fn try_const_usize(db: &dyn HirDatabase, c: &Const) -> Option<u128> {
172-
try_const_usize_sign_extend(db, c, false)
172+
match &c.data(Interner).value {
173+
chalk_ir::ConstValue::BoundVar(_) => None,
174+
chalk_ir::ConstValue::InferenceVar(_) => None,
175+
chalk_ir::ConstValue::Placeholder(_) => None,
176+
chalk_ir::ConstValue::Concrete(c) => match &c.interned {
177+
ConstScalar::Bytes(it, _) => Some(u128::from_le_bytes(pad16(it, false))),
178+
ConstScalar::UnevaluatedConst(c, subst) => {
179+
let ec = db.const_eval(*c, subst.clone(), None).ok()?;
180+
try_const_usize(db, &ec)
181+
}
182+
_ => None,
183+
},
184+
}
173185
}
174186

175-
pub fn try_const_usize_sign_extend(
176-
db: &dyn HirDatabase,
177-
c: &Const,
178-
is_signed: bool,
179-
) -> Option<u128> {
187+
pub fn try_const_isize(db: &dyn HirDatabase, c: &Const) -> Option<i128> {
180188
match &c.data(Interner).value {
181189
chalk_ir::ConstValue::BoundVar(_) => None,
182190
chalk_ir::ConstValue::InferenceVar(_) => None,
183191
chalk_ir::ConstValue::Placeholder(_) => None,
184192
chalk_ir::ConstValue::Concrete(c) => match &c.interned {
185-
ConstScalar::Bytes(it, _) => Some(u128::from_le_bytes(pad16(it, is_signed))),
193+
ConstScalar::Bytes(it, _) => Some(i128::from_le_bytes(pad16(it, true))),
186194
ConstScalar::UnevaluatedConst(c, subst) => {
187195
let ec = db.const_eval(*c, subst.clone(), None).ok()?;
188-
try_const_usize_sign_extend(db, &ec, is_signed)
196+
try_const_isize(db, &ec)
189197
}
190198
_ => None,
191199
},
@@ -287,7 +295,11 @@ pub(crate) fn const_eval_discriminant_variant(
287295
db.trait_environment_for_body(def),
288296
)?;
289297
let c = interpret_mir(db, mir_body, false, None).0?;
290-
let c = try_const_usize_sign_extend(db, &c, is_signed).unwrap() as i128;
298+
let c = if is_signed {
299+
try_const_isize(db, &c).unwrap()
300+
} else {
301+
try_const_usize(db, &c).unwrap() as i128
302+
};
291303
Ok(c)
292304
}
293305

0 commit comments

Comments
 (0)