From b0fe3d929f6f8764f371970b9f9ca9e7c415dafd Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 20:22:59 +0200 Subject: Add TyBuilder::unit() and TyExt::is_unit() --- crates/hir_ty/src/chalk_ext.rs | 13 +++++++++++++ crates/hir_ty/src/diagnostics/expr.rs | 4 ++-- crates/hir_ty/src/display.rs | 7 ++++--- crates/hir_ty/src/infer/expr.rs | 22 +++++++++++----------- crates/hir_ty/src/lib.rs | 10 +++++----- crates/hir_ty/src/op.rs | 4 ++-- crates/hir_ty/src/traits/chalk.rs | 4 ++-- 7 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 crates/hir_ty/src/chalk_ext.rs (limited to 'crates/hir_ty') 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 @@ +//! 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, _)) + } +} 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::{ 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; } 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::{ 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)?; 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::{ 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 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; 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 { - -} - -impl Ty { - pub fn unit() -> Self { + pub fn unit() -> Ty { TyKind::Tuple(0, Substitution::empty(&Interner)).intern(&Interner) } +} +impl Ty { pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty { TyKind::Adt(AdtId(adt), substs).intern(&Interner) } 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 @@ 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(_)) 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::{ 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 for ChalkContext<'a> { _closure_id: chalk_ir::ClosureId, _substs: &chalk_ir::Substitution, ) -> chalk_ir::Binders> { - let ty = Ty::unit().to_chalk(self.db); + let ty = TyBuilder::unit().to_chalk(self.db); make_binders(ty, 0) } fn closure_fn_substitution( -- cgit v1.2.3