aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-04-03 19:22:59 +0100
committerFlorian Diebold <[email protected]>2021-04-04 12:16:38 +0100
commitb0fe3d929f6f8764f371970b9f9ca9e7c415dafd (patch)
tree384bd17e3428b591f1c82d1b061ceecee529c044 /crates/hir_ty
parentb15152c430237d6850ec709ac75aab269c4b7dee (diff)
Add TyBuilder::unit() and TyExt::is_unit()
Diffstat (limited to 'crates/hir_ty')
-rw-r--r--crates/hir_ty/src/chalk_ext.rs13
-rw-r--r--crates/hir_ty/src/diagnostics/expr.rs4
-rw-r--r--crates/hir_ty/src/display.rs7
-rw-r--r--crates/hir_ty/src/infer/expr.rs22
-rw-r--r--crates/hir_ty/src/lib.rs10
-rw-r--r--crates/hir_ty/src/op.rs4
-rw-r--r--crates/hir_ty/src/traits/chalk.rs4
7 files changed, 39 insertions, 25 deletions
diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs
new file mode 100644
index 000000000..b7463366b
--- /dev/null
+++ b/crates/hir_ty/src/chalk_ext.rs
@@ -0,0 +1,13 @@
1//! Various extensions traits for Chalk types.
2
3use crate::{Interner, Ty, TyKind};
4
5pub trait TyExt {
6 fn is_unit(&self) -> bool;
7}
8
9impl TyExt for Ty {
10 fn is_unit(&self) -> bool {
11 matches!(self.kind(&Interner), TyKind::Tuple(0, _))
12 }
13}
diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs
index ad1259b34..8169b759f 100644
--- a/crates/hir_ty/src/diagnostics/expr.rs
+++ b/crates/hir_ty/src/diagnostics/expr.rs
@@ -15,7 +15,7 @@ use crate::{
15 MissingPatFields, RemoveThisSemicolon, 15 MissingPatFields, RemoveThisSemicolon,
16 }, 16 },
17 utils::variant_data, 17 utils::variant_data,
18 AdtId, InferenceResult, Interner, Ty, TyKind, 18 AdtId, InferenceResult, Interner, TyExt, TyKind,
19}; 19};
20 20
21pub(crate) use hir_def::{ 21pub(crate) use hir_def::{
@@ -423,7 +423,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
423 None => return, 423 None => return,
424 }; 424 };
425 425
426 if mismatch.actual != Ty::unit() || mismatch.expected != *possible_tail_ty { 426 if !mismatch.actual.is_unit() || mismatch.expected != *possible_tail_ty {
427 return; 427 return;
428 } 428 }
429 429
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index 97f1092c6..eb82e1a11 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -19,7 +19,8 @@ use crate::{
19 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, 19 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
20 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, 20 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
21 CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy, 21 CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy,
22 ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause, 22 ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyExt, TyKind,
23 WhereClause,
23}; 24};
24 25
25pub struct HirFormatter<'a> { 26pub struct HirFormatter<'a> {
@@ -423,7 +424,7 @@ impl HirDisplay for Ty {
423 f.write_joined(sig.params(), ", ")?; 424 f.write_joined(sig.params(), ", ")?;
424 write!(f, ")")?; 425 write!(f, ")")?;
425 let ret = sig.ret(); 426 let ret = sig.ret();
426 if *ret != Ty::unit() { 427 if !ret.is_unit() {
427 let ret_display = ret.into_displayable( 428 let ret_display = ret.into_displayable(
428 f.db, 429 f.db,
429 f.max_size, 430 f.max_size,
@@ -663,7 +664,7 @@ impl HirDisplay for CallableSig {
663 } 664 }
664 write!(f, ")")?; 665 write!(f, ")")?;
665 let ret = self.ret(); 666 let ret = self.ret();
666 if *ret != Ty::unit() { 667 if !ret.is_unit() {
667 let ret_display = 668 let ret_display =
668 ret.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); 669 ret.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
669 write!(f, " -> {}", ret_display)?; 670 write!(f, " -> {}", ret_display)?;
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index dd3914ec3..db8aeead2 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -23,7 +23,7 @@ use crate::{
23 traits::{chalk::from_chalk, FnTrait, InEnvironment}, 23 traits::{chalk::from_chalk, FnTrait, InEnvironment},
24 utils::{generics, variant_data, Generics}, 24 utils::{generics, variant_data, Generics},
25 AdtId, Binders, CallableDefId, DomainGoal, FnPointer, FnSig, Interner, Rawness, Scalar, 25 AdtId, Binders, CallableDefId, DomainGoal, FnPointer, FnSig, Interner, Rawness, Scalar,
26 Substitution, TraitRef, Ty, TyKind, 26 Substitution, TraitRef, Ty, TyBuilder, TyKind,
27}; 27};
28 28
29use super::{ 29use super::{
@@ -138,7 +138,7 @@ impl<'a> InferenceContext<'a> {
138 both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe); 138 both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe);
139 let else_ty = match else_branch { 139 let else_ty = match else_branch {
140 Some(else_branch) => self.infer_expr_inner(*else_branch, &expected), 140 Some(else_branch) => self.infer_expr_inner(*else_branch, &expected),
141 None => Ty::unit(), 141 None => TyBuilder::unit(),
142 }; 142 };
143 both_arms_diverge &= self.diverges; 143 both_arms_diverge &= self.diverges;
144 144
@@ -193,7 +193,7 @@ impl<'a> InferenceContext<'a> {
193 break_ty: self.table.new_type_var(), 193 break_ty: self.table.new_type_var(),
194 label: label.map(|label| self.body[label].name.clone()), 194 label: label.map(|label| self.body[label].name.clone()),
195 }); 195 });
196 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 196 self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit()));
197 197
198 let ctxt = self.breakables.pop().expect("breakable stack broken"); 198 let ctxt = self.breakables.pop().expect("breakable stack broken");
199 if ctxt.may_break { 199 if ctxt.may_break {
@@ -217,11 +217,11 @@ impl<'a> InferenceContext<'a> {
217 *condition, 217 *condition,
218 &Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(&Interner)), 218 &Expectation::has_type(TyKind::Scalar(Scalar::Bool).intern(&Interner)),
219 ); 219 );
220 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 220 self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit()));
221 let _ctxt = self.breakables.pop().expect("breakable stack broken"); 221 let _ctxt = self.breakables.pop().expect("breakable stack broken");
222 // the body may not run, so it diverging doesn't mean we diverge 222 // the body may not run, so it diverging doesn't mean we diverge
223 self.diverges = Diverges::Maybe; 223 self.diverges = Diverges::Maybe;
224 Ty::unit() 224 TyBuilder::unit()
225 } 225 }
226 Expr::For { iterable, body, pat, label } => { 226 Expr::For { iterable, body, pat, label } => {
227 let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); 227 let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
@@ -236,11 +236,11 @@ impl<'a> InferenceContext<'a> {
236 236
237 self.infer_pat(*pat, &pat_ty, BindingMode::default()); 237 self.infer_pat(*pat, &pat_ty, BindingMode::default());
238 238
239 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 239 self.infer_expr(*body, &Expectation::has_type(TyBuilder::unit()));
240 let _ctxt = self.breakables.pop().expect("breakable stack broken"); 240 let _ctxt = self.breakables.pop().expect("breakable stack broken");
241 // the body may not run, so it diverging doesn't mean we diverge 241 // the body may not run, so it diverging doesn't mean we diverge
242 self.diverges = Diverges::Maybe; 242 self.diverges = Diverges::Maybe;
243 Ty::unit() 243 TyBuilder::unit()
244 } 244 }
245 Expr::Lambda { body, args, ret_type, arg_types } => { 245 Expr::Lambda { body, args, ret_type, arg_types } => {
246 assert_eq!(args.len(), arg_types.len()); 246 assert_eq!(args.len(), arg_types.len());
@@ -360,7 +360,7 @@ impl<'a> InferenceContext<'a> {
360 let val_ty = if let Some(expr) = expr { 360 let val_ty = if let Some(expr) = expr {
361 self.infer_expr(*expr, &Expectation::none()) 361 self.infer_expr(*expr, &Expectation::none())
362 } else { 362 } else {
363 Ty::unit() 363 TyBuilder::unit()
364 }; 364 };
365 365
366 let last_ty = 366 let last_ty =
@@ -386,7 +386,7 @@ impl<'a> InferenceContext<'a> {
386 if let Some(expr) = expr { 386 if let Some(expr) = expr {
387 self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone())); 387 self.infer_expr_coerce(*expr, &Expectation::has_type(self.return_ty.clone()));
388 } else { 388 } else {
389 let unit = Ty::unit(); 389 let unit = TyBuilder::unit();
390 self.coerce(&unit, &self.return_ty.clone()); 390 self.coerce(&unit, &self.return_ty.clone());
391 } 391 }
392 TyKind::Never.intern(&Interner) 392 TyKind::Never.intern(&Interner)
@@ -828,8 +828,8 @@ impl<'a> InferenceContext<'a> {
828 // we don't even make an attempt at coercion 828 // we don't even make an attempt at coercion
829 self.table.new_maybe_never_var() 829 self.table.new_maybe_never_var()
830 } else { 830 } else {
831 self.coerce(&Ty::unit(), &expected.coercion_target()); 831 self.coerce(&TyBuilder::unit(), &expected.coercion_target());
832 Ty::unit() 832 TyBuilder::unit()
833 } 833 }
834 }; 834 };
835 ty 835 ty
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index a8ddb43f5..b6173d87c 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -23,6 +23,7 @@ pub mod diagnostics;
23mod tests; 23mod tests;
24#[cfg(test)] 24#[cfg(test)]
25mod test_db; 25mod test_db;
26mod chalk_ext;
26 27
27use std::{iter, mem, sync::Arc}; 28use std::{iter, mem, sync::Arc};
28 29
@@ -42,6 +43,7 @@ use crate::{
42}; 43};
43 44
44pub use autoderef::autoderef; 45pub use autoderef::autoderef;
46pub use chalk_ext::TyExt;
45pub use infer::{could_unify, InferenceResult, InferenceVar}; 47pub use infer::{could_unify, InferenceResult, InferenceVar};
46pub use lower::{ 48pub use lower::{
47 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, 49 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
@@ -813,14 +815,12 @@ impl TypeWalk for CallableSig {
813struct TyBuilder {} 815struct TyBuilder {}
814 816
815impl TyBuilder { 817impl TyBuilder {
816 818 pub fn unit() -> Ty {
817}
818
819impl Ty {
820 pub fn unit() -> Self {
821 TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) 819 TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner)
822 } 820 }
821}
823 822
823impl Ty {
824 pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty { 824 pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty {
825 TyKind::Adt(AdtId(adt), substs).intern(&Interner) 825 TyKind::Adt(AdtId(adt), substs).intern(&Interner)
826 } 826 }
diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs
index 8533e1ed8..90dd31a35 100644
--- a/crates/hir_ty/src/op.rs
+++ b/crates/hir_ty/src/op.rs
@@ -2,12 +2,12 @@
2use chalk_ir::TyVariableKind; 2use chalk_ir::TyVariableKind;
3use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; 3use hir_def::expr::{ArithOp, BinaryOp, CmpOp};
4 4
5use crate::{Interner, Scalar, Ty, TyKind}; 5use crate::{Interner, Scalar, Ty, TyBuilder, TyKind};
6 6
7pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { 7pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
8 match op { 8 match op {
9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner), 9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
10 BinaryOp::Assignment { .. } => Ty::unit(), 10 BinaryOp::Assignment { .. } => TyBuilder::unit(),
11 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => { 11 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => {
12 match lhs_ty.kind(&Interner) { 12 match lhs_ty.kind(&Interner) {
13 TyKind::Scalar(Scalar::Int(_)) 13 TyKind::Scalar(Scalar::Int(_))
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index fbac51b05..36a397990 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -22,7 +22,7 @@ use crate::{
22 to_assoc_type_id, to_chalk_trait_id, 22 to_assoc_type_id, to_chalk_trait_id,
23 utils::generics, 23 utils::generics,
24 AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, 24 AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution,
25 TraitRef, Ty, TyKind, WhereClause, 25 TraitRef, Ty, TyBuilder, TyKind, WhereClause,
26}; 26};
27use mapping::{ 27use mapping::{
28 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, 28 convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue,
@@ -300,7 +300,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
300 _closure_id: chalk_ir::ClosureId<Interner>, 300 _closure_id: chalk_ir::ClosureId<Interner>,
301 _substs: &chalk_ir::Substitution<Interner>, 301 _substs: &chalk_ir::Substitution<Interner>,
302 ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> { 302 ) -> chalk_ir::Binders<chalk_ir::Ty<Interner>> {
303 let ty = Ty::unit().to_chalk(self.db); 303 let ty = TyBuilder::unit().to_chalk(self.db);
304 make_binders(ty, 0) 304 make_binders(ty, 0)
305 } 305 }
306 fn closure_fn_substitution( 306 fn closure_fn_substitution(