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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

347 lines
11 KiB
Rust
Raw Normal View History

//! Constant evaluation details
2023-12-07 09:57:51 +00:00
use base_db::{salsa::Cycle, CrateId};
use chalk_ir::{cast::Cast, BoundVar, DebruijnIndex};
use hir_def::{
body::Body,
hir::{Expr, ExprId},
2023-03-08 17:28:52 +00:00
path::Path,
2023-02-03 11:16:25 +00:00
resolver::{Resolver, ValueNs},
2023-06-05 11:27:19 +00:00
type_ref::LiteralConstRef,
ConstBlockLoc, EnumVariantId, GeneralConstId, StaticId,
};
use hir_expand::Lookup;
use stdx::{never, IsNoneOr};
2023-05-12 14:47:15 +00:00
use triomphe::Arc;
2022-03-09 18:50:24 +00:00
use crate::{
2024-06-21 08:49:19 +00:00
db::HirDatabase, generics::Generics, infer::InferenceContext, lower::ParamLoweringMode,
mir::monomorphize_mir_body_bad, to_placeholder_idx, Const, ConstData, ConstScalar, ConstValue,
GenericArg, Interner, MemoryMap, Substitution, TraitEnvironment, Ty, TyBuilder,
2022-03-09 18:50:24 +00:00
};
2023-02-03 11:16:25 +00:00
use super::mir::{interpret_mir, lower_to_mir, pad16, MirEvalError, MirLowerError};
/// Extension trait for [`Const`]
pub trait ConstExt {
/// Is a [`Const`] unknown?
fn is_unknown(&self) -> bool;
}
impl ConstExt for Const {
fn is_unknown(&self) -> bool {
2021-12-19 16:58:39 +00:00
match self.data(Interner).value {
// interned Unknown
chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst {
interned: ConstScalar::Unknown,
}) => true,
// interned concrete anything else
chalk_ir::ConstValue::Concrete(..) => false,
_ => {
2021-08-15 12:46:13 +00:00
tracing::error!(
"is_unknown was called on a non-concrete constant value! {:?}",
self
);
true
}
}
}
}
2022-03-20 13:45:28 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
2021-12-04 22:21:36 +00:00
pub enum ConstEvalError {
2023-02-03 11:16:25 +00:00
MirLowerError(MirLowerError),
MirEvalError(MirEvalError),
2021-12-04 22:21:36 +00:00
}
2023-02-03 11:16:25 +00:00
impl From<MirLowerError> for ConstEvalError {
fn from(value: MirLowerError) -> Self {
match value {
2023-04-28 17:14:30 +00:00
MirLowerError::ConstEvalError(_, e) => *e,
2023-02-03 11:16:25 +00:00
_ => ConstEvalError::MirLowerError(value),
2021-12-04 22:21:36 +00:00
}
}
}
2023-02-03 11:16:25 +00:00
impl From<MirEvalError> for ConstEvalError {
fn from(value: MirEvalError) -> Self {
ConstEvalError::MirEvalError(value)
2021-12-04 22:21:36 +00:00
}
}
2024-06-21 16:38:37 +00:00
pub(crate) fn path_to_const<'g>(
2022-03-09 18:50:24 +00:00
db: &dyn HirDatabase,
resolver: &Resolver,
2023-03-08 17:28:52 +00:00
path: &Path,
2022-03-09 18:50:24 +00:00
mode: ParamLoweringMode,
2024-06-21 16:38:37 +00:00
args: impl FnOnce() -> Option<&'g Generics>,
2022-03-09 18:50:24 +00:00
debruijn: DebruijnIndex,
expected_ty: Ty,
2022-03-09 18:50:24 +00:00
) -> Option<Const> {
2022-12-30 08:05:03 +00:00
match resolver.resolve_path_in_value_ns_fully(db.upcast(), path) {
2022-03-09 18:50:24 +00:00
Some(ValueNs::GenericParam(p)) => {
let ty = db.const_param_ty(p);
let value = match mode {
ParamLoweringMode::Placeholder => {
ConstValue::Placeholder(to_placeholder_idx(db, p.into()))
}
2024-04-06 11:12:07 +00:00
ParamLoweringMode::Variable => {
let args = args();
2024-06-21 16:38:37 +00:00
match args.and_then(|args| args.type_or_const_param_idx(p.into())) {
2024-04-06 11:12:07 +00:00
Some(it) => ConstValue::BoundVar(BoundVar::new(debruijn, it)),
None => {
never!(
"Generic list doesn't contain this param: {:?}, {:?}, {:?}",
args,
path,
p
);
return None;
}
2022-03-09 18:50:24 +00:00
}
2024-04-06 11:12:07 +00:00
}
2022-03-09 18:50:24 +00:00
};
Some(ConstData { ty, value }.intern(Interner))
}
Some(ValueNs::ConstId(c)) => Some(intern_const_scalar(
ConstScalar::UnevaluatedConst(c.into(), Substitution::empty(Interner)),
expected_ty,
)),
2022-03-09 18:50:24 +00:00
_ => None,
}
}
pub fn unknown_const(ty: Ty) -> Const {
ConstData {
ty,
value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: ConstScalar::Unknown }),
}
.intern(Interner)
}
pub fn unknown_const_as_generic(ty: Ty) -> GenericArg {
unknown_const(ty).cast(Interner)
2022-03-09 18:50:24 +00:00
}
2022-04-07 01:00:33 +00:00
/// Interns a constant scalar with the given type
2022-07-17 15:22:11 +00:00
pub fn intern_const_scalar(value: ConstScalar, ty: Ty) -> Const {
2022-04-07 01:00:33 +00:00
ConstData { ty, value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: value }) }
.intern(Interner)
}
2023-02-03 11:16:25 +00:00
/// Interns a constant scalar with the given type
2023-06-05 11:27:19 +00:00
pub fn intern_const_ref(
db: &dyn HirDatabase,
value: &LiteralConstRef,
ty: Ty,
krate: CrateId,
) -> Const {
let layout = db.layout_of_ty(ty.clone(), TraitEnvironment::empty(krate));
2023-02-03 11:16:25 +00:00
let bytes = match value {
2023-06-05 11:27:19 +00:00
LiteralConstRef::Int(i) => {
2023-02-03 11:16:25 +00:00
// FIXME: We should handle failure of layout better.
2023-07-06 14:03:17 +00:00
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
2024-01-06 16:48:07 +00:00
ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
2023-02-03 11:16:25 +00:00
}
2023-06-05 11:27:19 +00:00
LiteralConstRef::UInt(i) => {
2023-07-06 14:03:17 +00:00
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
2024-01-06 16:48:07 +00:00
ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
2023-02-03 11:16:25 +00:00
}
2024-01-06 16:48:07 +00:00
LiteralConstRef::Bool(b) => ConstScalar::Bytes(Box::new([*b as u8]), MemoryMap::default()),
2023-06-05 11:27:19 +00:00
LiteralConstRef::Char(c) => {
2024-01-06 16:48:07 +00:00
ConstScalar::Bytes((*c as u32).to_le_bytes().into(), MemoryMap::default())
2023-02-03 11:16:25 +00:00
}
2023-06-05 11:27:19 +00:00
LiteralConstRef::Unknown => ConstScalar::Unknown,
2023-02-03 11:16:25 +00:00
};
intern_const_scalar(bytes, ty)
}
/// Interns a possibly-unknown target usize
2023-02-03 11:16:25 +00:00
pub fn usize_const(db: &dyn HirDatabase, value: Option<u128>, krate: CrateId) -> Const {
intern_const_ref(
db,
2023-06-05 11:27:19 +00:00
&value.map_or(LiteralConstRef::Unknown, LiteralConstRef::UInt),
2023-02-03 11:16:25 +00:00
TyBuilder::usize(),
krate,
)
}
2023-05-12 14:47:15 +00:00
pub fn try_const_usize(db: &dyn HirDatabase, c: &Const) -> Option<u128> {
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_isize(db: &dyn HirDatabase, c: &Const) -> Option<i128> {
2023-02-03 11:16:25 +00:00
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(i128::from_le_bytes(pad16(it, true))),
2023-05-12 14:47:15 +00:00
ConstScalar::UnevaluatedConst(c, subst) => {
let ec = db.const_eval(*c, subst.clone(), None).ok()?;
try_const_isize(db, &ec)
2023-05-12 14:47:15 +00:00
}
2023-02-03 11:16:25 +00:00
_ => None,
},
}
}
2022-03-20 13:45:28 +00:00
pub(crate) fn const_eval_recover(
_: &dyn HirDatabase,
2023-12-07 09:57:51 +00:00
_: &Cycle,
2023-05-12 14:47:15 +00:00
_: &GeneralConstId,
_: &Substitution,
_: &Option<Arc<TraitEnvironment>>,
2023-02-03 11:16:25 +00:00
) -> Result<Const, ConstEvalError> {
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
2022-03-20 13:45:28 +00:00
}
2023-05-12 14:47:15 +00:00
pub(crate) fn const_eval_static_recover(
_: &dyn HirDatabase,
2023-12-07 09:57:51 +00:00
_: &Cycle,
2023-05-12 14:47:15 +00:00
_: &StaticId,
) -> Result<Const, ConstEvalError> {
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
}
2023-02-03 11:16:25 +00:00
pub(crate) fn const_eval_discriminant_recover(
2022-08-06 16:50:21 +00:00
_: &dyn HirDatabase,
2023-12-07 09:57:51 +00:00
_: &Cycle,
2022-08-06 16:50:21 +00:00
_: &EnumVariantId,
2023-02-03 11:16:25 +00:00
) -> Result<i128, ConstEvalError> {
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
2022-08-06 16:50:21 +00:00
}
2023-02-03 11:16:25 +00:00
pub(crate) fn const_eval_query(
2022-03-20 13:45:28 +00:00
db: &dyn HirDatabase,
2023-05-12 14:47:15 +00:00
def: GeneralConstId,
subst: Substitution,
trait_env: Option<Arc<TraitEnvironment>>,
2023-02-03 11:16:25 +00:00
) -> Result<Const, ConstEvalError> {
2023-05-12 14:47:15 +00:00
let body = match def {
2023-05-25 21:15:37 +00:00
GeneralConstId::ConstId(c) => {
db.monomorphized_mir_body(c.into(), subst, db.trait_environment(c.into()))?
}
2023-06-11 21:07:11 +00:00
GeneralConstId::ConstBlockId(c) => {
let ConstBlockLoc { parent, root } = db.lookup_intern_anonymous_const(c);
let body = db.body(parent);
let infer = db.infer(parent);
2023-05-25 21:15:37 +00:00
Arc::new(monomorphize_mir_body_bad(
db,
lower_to_mir(db, parent, &body, &infer, root)?,
2023-05-25 21:15:37 +00:00
subst,
db.trait_environment_for_body(parent),
2023-05-25 21:15:37 +00:00
)?)
2023-05-12 14:47:15 +00:00
}
2023-06-05 11:27:19 +00:00
GeneralConstId::InTypeConstId(c) => db.mir_body(c.into())?,
2023-05-12 14:47:15 +00:00
};
let c = interpret_mir(db, body, false, trait_env).0?;
2023-05-12 14:47:15 +00:00
Ok(c)
}
pub(crate) fn const_eval_static_query(
db: &dyn HirDatabase,
def: StaticId,
) -> Result<Const, ConstEvalError> {
2023-05-25 21:15:37 +00:00
let body = db.monomorphized_mir_body(
def.into(),
Substitution::empty(Interner),
db.trait_environment_for_body(def.into()),
)?;
let c = interpret_mir(db, body, false, None).0?;
2023-02-03 11:16:25 +00:00
Ok(c)
2022-03-20 13:45:28 +00:00
}
2023-02-03 11:16:25 +00:00
pub(crate) fn const_eval_discriminant_variant(
2022-08-06 16:50:21 +00:00
db: &dyn HirDatabase,
variant_id: EnumVariantId,
2023-02-03 11:16:25 +00:00
) -> Result<i128, ConstEvalError> {
2022-08-06 16:50:21 +00:00
let def = variant_id.into();
let body = db.body(def);
let loc = variant_id.lookup(db.upcast());
2023-02-03 11:16:25 +00:00
if body.exprs[body.body_expr] == Expr::Missing {
2024-01-15 11:03:31 +00:00
let prev_idx = loc.index.checked_sub(1);
2023-02-03 11:16:25 +00:00
let value = match prev_idx {
Some(prev_idx) => {
1 + db.const_eval_discriminant(
db.enum_data(loc.parent).variants[prev_idx as usize].0,
)?
2023-02-03 11:16:25 +00:00
}
_ => 0,
};
return Ok(value);
}
let repr = db.enum_data(loc.parent).repr;
2024-09-02 17:04:35 +00:00
let is_signed = IsNoneOr::is_none_or(repr.and_then(|repr| repr.int), |int| int.is_signed());
2023-05-25 21:15:37 +00:00
let mir_body = db.monomorphized_mir_body(
def,
Substitution::empty(Interner),
db.trait_environment_for_body(def),
)?;
let c = interpret_mir(db, mir_body, false, None).0?;
let c = if is_signed {
try_const_isize(db, &c).unwrap()
} else {
try_const_usize(db, &c).unwrap() as i128
};
2023-02-03 11:16:25 +00:00
Ok(c)
2022-08-06 16:50:21 +00:00
}
2023-02-03 11:16:25 +00:00
// FIXME: Ideally constants in const eval should have separate body (issue #7434), and this function should
// get an `InferenceResult` instead of an `InferenceContext`. And we should remove `ctx.clone().resolve_all()` here
// and make this function private. See the fixme comment on `InferenceContext::resolve_all`.
pub(crate) fn eval_to_const(
expr: ExprId,
2022-03-09 18:50:24 +00:00
mode: ParamLoweringMode,
ctx: &mut InferenceContext<'_>,
2022-03-09 18:50:24 +00:00
debruijn: DebruijnIndex,
) -> Const {
2023-02-03 11:16:25 +00:00
let db = ctx.db;
let infer = ctx.clone().resolve_all();
fn has_closure(body: &Body, expr: ExprId) -> bool {
if matches!(body[expr], Expr::Closure { .. }) {
return true;
}
let mut r = false;
body[expr].walk_child_exprs(|idx| r |= has_closure(body, idx));
r
}
2024-01-18 12:59:49 +00:00
if has_closure(ctx.body, expr) {
// Type checking clousres need an isolated body (See the above FIXME). Bail out early to prevent panic.
return unknown_const(infer[expr].clone());
}
2022-03-09 18:50:24 +00:00
if let Expr::Path(p) = &ctx.body.exprs[expr] {
let resolver = &ctx.resolver;
2024-04-06 11:12:07 +00:00
if let Some(c) =
path_to_const(db, resolver, p, mode, || ctx.generics(), debruijn, infer[expr].clone())
{
2022-03-09 18:50:24 +00:00
return c;
}
}
2024-01-18 12:59:49 +00:00
if let Ok(mir_body) = lower_to_mir(ctx.db, ctx.owner, ctx.body, &infer, expr) {
if let Ok(result) = interpret_mir(db, Arc::new(mir_body), true, None).0 {
2023-02-03 11:16:25 +00:00
return result;
}
}
unknown_const(infer[expr].clone())
2022-03-09 18:50:24 +00:00
}
2022-03-24 08:36:27 +00:00
#[cfg(test)]
mod tests;