Add TyBuilder::unit() and TyExt::is_unit()
This commit is contained in:
parent
b15152c430
commit
b0fe3d929f
13
crates/hir_ty/src/chalk_ext.rs
Normal file
13
crates/hir_ty/src/chalk_ext.rs
Normal file
@ -0,0 +1,13 @@
|
||||
//! Various extensions traits for Chalk types.
|
||||
|
||||
use crate::{Interner, Ty, TyKind};
|
||||
|
||||
pub trait TyExt {
|
||||
fn is_unit(&self) -> bool;
|
||||
}
|
||||
|
||||
impl TyExt for Ty {
|
||||
fn is_unit(&self) -> bool {
|
||||
matches!(self.kind(&Interner), TyKind::Tuple(0, _))
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ use crate::{
|
||||
MissingPatFields, RemoveThisSemicolon,
|
||||
},
|
||||
utils::variant_data,
|
||||
AdtId, InferenceResult, Interner, Ty, TyKind,
|
||||
AdtId, InferenceResult, Interner, TyExt, TyKind,
|
||||
};
|
||||
|
||||
pub(crate) use hir_def::{
|
||||
@ -423,7 +423,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||
None => return,
|
||||
};
|
||||
|
||||
if mismatch.actual != Ty::unit() || mismatch.expected != *possible_tail_ty {
|
||||
if !mismatch.actual.is_unit() || mismatch.expected != *possible_tail_ty {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,8 @@ use crate::{
|
||||
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
|
||||
to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
|
||||
CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy,
|
||||
ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause,
|
||||
ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyExt, TyKind,
|
||||
WhereClause,
|
||||
};
|
||||
|
||||
pub struct HirFormatter<'a> {
|
||||
@ -423,7 +424,7 @@ impl HirDisplay for Ty {
|
||||
f.write_joined(sig.params(), ", ")?;
|
||||
write!(f, ")")?;
|
||||
let ret = sig.ret();
|
||||
if *ret != Ty::unit() {
|
||||
if !ret.is_unit() {
|
||||
let ret_display = ret.into_displayable(
|
||||
f.db,
|
||||
f.max_size,
|
||||
@ -663,7 +664,7 @@ impl HirDisplay for CallableSig {
|
||||
}
|
||||
write!(f, ")")?;
|
||||
let ret = self.ret();
|
||||
if *ret != Ty::unit() {
|
||||
if !ret.is_unit() {
|
||||
let ret_display =
|
||||
ret.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
|
||||
write!(f, " -> {}", ret_display)?;
|
||||
|
@ -23,7 +23,7 @@ use crate::{
|
||||
traits::{chalk::from_chalk, FnTrait, InEnvironment},
|
||||
utils::{generics, variant_data, Generics},
|
||||
AdtId, Binders, CallableDefId, DomainGoal, FnPointer, FnSig, Interner, Rawness, Scalar,
|
||||
Substitution, TraitRef, Ty, TyKind,
|
||||
Substitution, TraitRef, Ty, TyBuilder, TyKind,
|
||||
};
|
||||
|
||||
use super::{
|
||||
@ -138,7 +138,7 @@ impl<'a> InferenceContext<'a> {
|
||||
both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe);
|
||||
let else_ty = match else_branch {
|
||||
Some(else_branch) => self.infer_expr_inner(*else_branch, &expected),
|
||||
None => Ty::unit(),
|
||||
None => TyBuilder::unit(),
|
||||
};
|
||||
both_arms_diverge &= self.diverges;
|
||||
|
||||
@ -193,7 +193,7 @@ impl<'a> InferenceContext<'a> {
|
||||
break_ty: self.table.new_type_var(),
|
||||
label: label.map(|label| self.body[label].name.clone()),
|
||||
});
|
||||
self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
|
||||
self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit()));
|
||||
|
||||
let ctxt = self.breakables.pop().expect("breakable stack broken");
|
||||
if ctxt.may_break {
|
||||
@ -217,11 +217,11 @@ impl<'a> InferenceContext<'a> {
|
||||
*condition,
|
||||
&Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(&Interner)),
|
||||
);
|
||||
self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
|
||||
self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit()));
|
||||
let _ctxt = self.breakables.pop().expect("breakable stack broken");
|
||||
// the body may not run, so it diverging doesn't mean we diverge
|
||||
self.diverges = Diverges::Maybe;
|
||||
Ty::unit()
|
||||
TyBuilder::unit()
|
||||
}
|
||||
Expr::For { iterable, body, pat, label } => {
|
||||
let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
|
||||
@ -236,11 +236,11 @@ impl<'a> InferenceContext<'a> {
|
||||
|
||||
self.infer_pat(*pat, &pat_ty, BindingMode::default());
|
||||
|
||||
self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
|
||||
self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit()));
|
||||
let _ctxt = self.breakables.pop().expect("breakable stack broken");
|
||||
// the body may not run, so it diverging doesn't mean we diverge
|
||||
self.diverges = Diverges::Maybe;
|
||||
Ty::unit()
|
||||
TyBuilder::unit()
|
||||
}
|
||||
Expr::Lambda { body, args, ret_type, arg_types } => {
|
||||
assert_eq!(args.len(), arg_types.len());
|
||||
@ -360,7 +360,7 @@ impl<'a> InferenceContext<'a> {
|
||||
let val_ty = if let Some(expr) = expr {
|
||||
self.infer_expr(*expr, &Expectation::none())
|
||||
} else {
|
||||
Ty::unit()
|
||||
TyBuilder::unit()
|
||||
};
|
||||
|
||||
let last_ty =
|
||||
@ -386,7 +386,7 @@ impl<'a> InferenceContext<'a> {
|
||||
if let Some(expr) = expr {
|
||||
self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone()));
|
||||
} else {
|
||||
let unit = Ty::unit();
|
||||
let unit = TyBuilder::unit();
|
||||
self.coerce(&unit, &self.return_ty.clone());
|
||||
}
|
||||
TyKind::Never.intern(&Interner)
|
||||
@ -828,8 +828,8 @@ impl<'a> InferenceContext<'a> {
|
||||
// we don't even make an attempt at coercion
|
||||
self.table.new_maybe_never_var()
|
||||
} else {
|
||||
self.coerce(&Ty::unit(), &expected.coercion_target());
|
||||
Ty::unit()
|
||||
self.coerce(&TyBuilder::unit(), &expected.coercion_target());
|
||||
TyBuilder::unit()
|
||||
}
|
||||
};
|
||||
ty
|
||||
|
@ -23,6 +23,7 @@ pub mod diagnostics;
|
||||
mod tests;
|
||||
#[cfg(test)]
|
||||
mod test_db;
|
||||
mod chalk_ext;
|
||||
|
||||
use std::{iter, mem, sync::Arc};
|
||||
|
||||
@ -42,6 +43,7 @@ use crate::{
|
||||
};
|
||||
|
||||
pub use autoderef::autoderef;
|
||||
pub use chalk_ext::TyExt;
|
||||
pub use infer::{could_unify, InferenceResult, InferenceVar};
|
||||
pub use lower::{
|
||||
associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
|
||||
@ -813,14 +815,12 @@ impl TypeWalk for CallableSig {
|
||||
struct TyBuilder {}
|
||||
|
||||
impl TyBuilder {
|
||||
|
||||
pub fn unit() -> Ty {
|
||||
TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ty {
|
||||
pub fn unit() -> Self {
|
||||
TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner)
|
||||
}
|
||||
|
||||
pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty {
|
||||
TyKind::Adt(AdtId(adt), substs).intern(&Interner)
|
||||
}
|
||||
|
@ -2,12 +2,12 @@
|
||||
use chalk_ir::TyVariableKind;
|
||||
use hir_def::expr::{ArithOp, BinaryOp, CmpOp};
|
||||
|
||||
use crate::{Interner, Scalar, Ty, TyKind};
|
||||
use crate::{Interner, Scalar, Ty, TyBuilder, TyKind};
|
||||
|
||||
pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
|
||||
match op {
|
||||
BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
|
||||
BinaryOp::Assignment { .. } => Ty::unit(),
|
||||
BinaryOp::Assignment { .. } => TyBuilder::unit(),
|
||||
BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => {
|
||||
match lhs_ty.kind(&Interner) {
|
||||
TyKind::Scalar(Scalar::Int(_))
|
||||
|
@ -22,7 +22,7 @@ use crate::{
|
||||
to_assoc_type_id, to_chalk_trait_id,
|
||||
utils::generics,
|
||||
AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution,
|
||||
TraitRef, Ty, TyKind, WhereClause,
|
||||
TraitRef, Ty, TyBuilder, TyKind, WhereClause,
|
||||
};
|
||||
use mapping::{
|
||||
convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue,
|
||||
@ -300,7 +300,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
|
||||
_closure_id: chalk_ir::ClosureId<Interner>,
|
||||
_substs: &chalk_ir::Substitution<Interner>,
|
||||
) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> {
|
||||
let ty = Ty::unit().to_chalk(self.db);
|
||||
let ty = TyBuilder::unit().to_chalk(self.db);
|
||||
make_binders(ty, 0)
|
||||
}
|
||||
fn closure_fn_substitution(
|
||||
|
Loading…
x
Reference in New Issue
Block a user