2020-03-27 12:56:18 -05:00
|
|
|
//! Implementation of Chalk debug helper functions using TLS.
|
2021-05-15 15:26:55 -05:00
|
|
|
use std::fmt::{self, Debug};
|
2020-03-27 12:56:18 -05:00
|
|
|
|
2021-05-15 15:26:55 -05:00
|
|
|
use chalk_ir::AliasTy;
|
2020-04-13 07:39:44 -05:00
|
|
|
use itertools::Itertools;
|
2020-03-27 12:56:18 -05:00
|
|
|
|
2021-04-09 07:15:26 -05:00
|
|
|
use crate::{
|
2021-04-09 07:28:04 -05:00
|
|
|
chalk_db, db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, mapping::from_chalk,
|
|
|
|
CallableDefId, Interner,
|
2021-04-09 07:15:26 -05:00
|
|
|
};
|
2020-10-30 12:57:16 -05:00
|
|
|
use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId};
|
2020-03-27 12:56:18 -05:00
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) use unsafe_tls::{set_current_program, with_current_program};
|
2020-03-27 12:56:18 -05:00
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) struct DebugContext<'a>(&'a dyn HirDatabase);
|
2020-03-27 12:56:18 -05:00
|
|
|
|
|
|
|
impl DebugContext<'_> {
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn debug_struct_id(
|
2020-03-27 12:56:18 -05:00
|
|
|
&self,
|
2021-04-09 07:11:37 -05:00
|
|
|
id: chalk_db::AdtId,
|
2020-03-27 12:56:18 -05:00
|
|
|
f: &mut fmt::Formatter<'_>,
|
|
|
|
) -> Result<(), fmt::Error> {
|
2020-10-30 12:57:16 -05:00
|
|
|
let name = match id.0 {
|
|
|
|
AdtId::StructId(it) => self.0.struct_data(it).name.clone(),
|
|
|
|
AdtId::UnionId(it) => self.0.union_data(it).name.clone(),
|
|
|
|
AdtId::EnumId(it) => self.0.enum_data(it).name.clone(),
|
|
|
|
};
|
|
|
|
write!(f, "{}", name)
|
2020-03-27 12:56:18 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn debug_trait_id(
|
2020-03-27 12:56:18 -05:00
|
|
|
&self,
|
2021-04-09 07:11:37 -05:00
|
|
|
id: chalk_db::TraitId,
|
2020-03-27 12:56:18 -05:00
|
|
|
fmt: &mut fmt::Formatter<'_>,
|
|
|
|
) -> Result<(), fmt::Error> {
|
2021-04-09 07:28:04 -05:00
|
|
|
let trait_: hir_def::TraitId = from_chalk_trait_id(id);
|
2020-03-27 12:56:18 -05:00
|
|
|
let trait_data = self.0.trait_data(trait_);
|
|
|
|
write!(fmt, "{}", trait_data.name)
|
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn debug_assoc_type_id(
|
2020-03-27 12:56:18 -05:00
|
|
|
&self,
|
2021-04-09 07:11:37 -05:00
|
|
|
id: chalk_db::AssocTypeId,
|
2020-03-27 12:56:18 -05:00
|
|
|
fmt: &mut fmt::Formatter<'_>,
|
|
|
|
) -> Result<(), fmt::Error> {
|
2021-03-13 10:36:07 -06:00
|
|
|
let type_alias: TypeAliasId = from_assoc_type_id(id);
|
2020-03-27 12:56:18 -05:00
|
|
|
let type_alias_data = self.0.type_alias_data(type_alias);
|
|
|
|
let trait_ = match type_alias.lookup(self.0.upcast()).container {
|
|
|
|
AssocContainerId::TraitId(t) => t,
|
|
|
|
_ => panic!("associated type not in trait"),
|
|
|
|
};
|
|
|
|
let trait_data = self.0.trait_data(trait_);
|
|
|
|
write!(fmt, "{}::{}", trait_data.name, type_alias_data.name)
|
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn debug_alias(
|
2020-03-27 12:56:18 -05:00
|
|
|
&self,
|
2020-04-18 06:36:35 -05:00
|
|
|
alias_ty: &AliasTy<Interner>,
|
|
|
|
fmt: &mut fmt::Formatter<'_>,
|
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
match alias_ty {
|
|
|
|
AliasTy::Projection(projection_ty) => self.debug_projection_ty(projection_ty, fmt),
|
2021-05-15 15:26:55 -05:00
|
|
|
AliasTy::Opaque(opaque_ty) => opaque_ty.fmt(fmt),
|
2020-04-18 06:36:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn debug_projection_ty(
|
2020-04-18 06:36:35 -05:00
|
|
|
&self,
|
|
|
|
projection_ty: &chalk_ir::ProjectionTy<Interner>,
|
2020-03-27 12:56:18 -05:00
|
|
|
fmt: &mut fmt::Formatter<'_>,
|
|
|
|
) -> Result<(), fmt::Error> {
|
2021-03-13 10:36:07 -06:00
|
|
|
let type_alias = from_assoc_type_id(projection_ty.associated_ty_id);
|
2020-03-27 12:56:18 -05:00
|
|
|
let type_alias_data = self.0.type_alias_data(type_alias);
|
|
|
|
let trait_ = match type_alias.lookup(self.0.upcast()).container {
|
|
|
|
AssocContainerId::TraitId(t) => t,
|
|
|
|
_ => panic!("associated type not in trait"),
|
|
|
|
};
|
|
|
|
let trait_data = self.0.trait_data(trait_);
|
2020-07-11 08:22:46 -05:00
|
|
|
let params = projection_ty.substitution.as_slice(&Interner);
|
2020-04-13 07:39:44 -05:00
|
|
|
write!(fmt, "<{:?} as {}", ¶ms[0], trait_data.name,)?;
|
|
|
|
if params.len() > 1 {
|
|
|
|
write!(
|
|
|
|
fmt,
|
|
|
|
"<{}>",
|
|
|
|
¶ms[1..].iter().format_with(", ", |x, f| f(&format_args!("{:?}", x))),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
write!(fmt, ">::{}", type_alias_data.name)
|
2020-03-27 12:56:18 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn debug_fn_def_id(
|
2020-05-22 11:05:48 -05:00
|
|
|
&self,
|
2020-05-22 11:15:53 -05:00
|
|
|
fn_def_id: chalk_ir::FnDefId<Interner>,
|
2020-05-22 11:05:48 -05:00
|
|
|
fmt: &mut fmt::Formatter<'_>,
|
|
|
|
) -> Result<(), fmt::Error> {
|
2020-07-16 06:15:00 -05:00
|
|
|
let def: CallableDefId = from_chalk(self.0, fn_def_id);
|
2020-05-22 11:15:53 -05:00
|
|
|
let name = match def {
|
2020-07-16 06:15:00 -05:00
|
|
|
CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(),
|
|
|
|
CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(),
|
|
|
|
CallableDefId::EnumVariantId(e) => {
|
2020-05-22 11:15:53 -05:00
|
|
|
let enum_data = self.0.enum_data(e.parent);
|
|
|
|
enum_data.variants[e.local_id].name.clone()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match def {
|
2020-07-16 06:15:00 -05:00
|
|
|
CallableDefId::FunctionId(_) => write!(fmt, "{{fn {}}}", name),
|
|
|
|
CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {
|
2020-05-22 11:15:53 -05:00
|
|
|
write!(fmt, "{{ctor {}}}", name)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 11:05:48 -05:00
|
|
|
}
|
2020-03-27 12:56:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mod unsafe_tls {
|
|
|
|
use super::DebugContext;
|
|
|
|
use crate::db::HirDatabase;
|
|
|
|
use scoped_tls::scoped_thread_local;
|
|
|
|
|
|
|
|
scoped_thread_local!(static PROGRAM: DebugContext);
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn with_current_program<R>(
|
2020-03-27 12:56:18 -05:00
|
|
|
op: impl for<'a> FnOnce(Option<&'a DebugContext<'a>>) -> R,
|
|
|
|
) -> R {
|
|
|
|
if PROGRAM.is_set() {
|
|
|
|
PROGRAM.with(|prog| op(Some(prog)))
|
|
|
|
} else {
|
|
|
|
op(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 06:13:32 -06:00
|
|
|
pub(crate) fn set_current_program<OP, R>(p: &dyn HirDatabase, op: OP) -> R
|
2020-03-27 12:56:18 -05:00
|
|
|
where
|
|
|
|
OP: FnOnce() -> R,
|
|
|
|
{
|
|
|
|
let ctx = DebugContext(p);
|
|
|
|
// we're transmuting the lifetime in the DebugContext to static. This is
|
|
|
|
// fine because we only keep the reference for the lifetime of this
|
|
|
|
// function, *and* the only way to access the context is through
|
|
|
|
// `with_current_program`, which hides the lifetime through the `for`
|
|
|
|
// type.
|
|
|
|
let static_p: &DebugContext<'static> =
|
|
|
|
unsafe { std::mem::transmute::<&DebugContext, &DebugContext<'static>>(&ctx) };
|
|
|
|
PROGRAM.set(static_p, || op())
|
|
|
|
}
|
|
|
|
}
|