TypeName => TypeCtor

This commit is contained in:
Florian Diebold 2019-03-21 22:20:03 +01:00
parent 8a5fbf4713
commit f10f5a81b3
7 changed files with 98 additions and 98 deletions

View file

@ -53,7 +53,7 @@ pub use self::{
name::Name, name::Name,
ids::{HirFileId, MacroCallId, MacroCallLoc, HirInterner}, ids::{HirFileId, MacroCallId, MacroCallLoc, HirInterner},
nameres::{PerNs, Namespace}, nameres::{PerNs, Namespace},
ty::{Ty, ApplicationTy, TypeName, Substs, display::HirDisplay}, ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay},
impl_block::{ImplBlock, ImplItem}, impl_block::{ImplBlock, ImplItem},
docs::{Docs, Documentation}, docs::{Docs, Documentation},
adt::AdtDef, adt::AdtDef,

View file

@ -21,7 +21,7 @@ pub(crate) use infer::{infer, InferenceResult, InferTy};
use display::{HirDisplay, HirFormatter}; use display::{HirDisplay, HirFormatter};
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum TypeName { pub enum TypeCtor {
/// The primitive boolean type. Written as `bool`. /// The primitive boolean type. Written as `bool`.
Bool, Bool,
@ -87,7 +87,7 @@ pub enum TypeName {
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct ApplicationTy { pub struct ApplicationTy {
pub name: TypeName, pub name: TypeCtor,
pub parameters: Substs, pub parameters: Substs,
} }
@ -191,17 +191,17 @@ impl FnSig {
} }
impl Ty { impl Ty {
pub fn simple(name: TypeName) -> Ty { pub fn simple(name: TypeCtor) -> Ty {
Ty::Apply(ApplicationTy { name, parameters: Substs::empty() }) Ty::Apply(ApplicationTy { name, parameters: Substs::empty() })
} }
pub fn apply_one(name: TypeName, param: Ty) -> Ty { pub fn apply_one(name: TypeCtor, param: Ty) -> Ty {
Ty::Apply(ApplicationTy { name, parameters: Substs::single(param) }) Ty::Apply(ApplicationTy { name, parameters: Substs::single(param) })
} }
pub fn apply(name: TypeName, parameters: Substs) -> Ty { pub fn apply(name: TypeCtor, parameters: Substs) -> Ty {
Ty::Apply(ApplicationTy { name, parameters }) Ty::Apply(ApplicationTy { name, parameters })
} }
pub fn unit() -> Self { pub fn unit() -> Self {
Ty::apply(TypeName::Tuple, Substs::empty()) Ty::apply(TypeCtor::Tuple, Substs::empty())
} }
pub fn walk(&self, f: &mut impl FnMut(&Ty)) { pub fn walk(&self, f: &mut impl FnMut(&Ty)) {
@ -236,7 +236,7 @@ impl Ty {
pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
match self { match self {
Ty::Apply(ApplicationTy { name: TypeName::Ref(mutability), parameters }) => { Ty::Apply(ApplicationTy { name: TypeCtor::Ref(mutability), parameters }) => {
Some((parameters.as_single(), *mutability)) Some((parameters.as_single(), *mutability))
} }
_ => None, _ => None,
@ -245,7 +245,7 @@ impl Ty {
pub fn as_adt(&self) -> Option<(AdtDef, &Substs)> { pub fn as_adt(&self) -> Option<(AdtDef, &Substs)> {
match self { match self {
Ty::Apply(ApplicationTy { name: TypeName::Adt(adt_def), parameters }) => { Ty::Apply(ApplicationTy { name: TypeCtor::Adt(adt_def), parameters }) => {
Some((*adt_def, parameters)) Some((*adt_def, parameters))
} }
_ => None, _ => None,
@ -254,7 +254,7 @@ impl Ty {
pub fn as_tuple(&self) -> Option<&Substs> { pub fn as_tuple(&self) -> Option<&Substs> {
match self { match self {
Ty::Apply(ApplicationTy { name: TypeName::Tuple, parameters }) => Some(parameters), Ty::Apply(ApplicationTy { name: TypeCtor::Tuple, parameters }) => Some(parameters),
_ => None, _ => None,
} }
} }
@ -262,8 +262,8 @@ impl Ty {
fn builtin_deref(&self) -> Option<Ty> { fn builtin_deref(&self) -> Option<Ty> {
match self { match self {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::Ref(..) => Some(Ty::clone(a_ty.parameters.as_single())), TypeCtor::Ref(..) => Some(Ty::clone(a_ty.parameters.as_single())),
TypeName::RawPtr(..) => Some(Ty::clone(a_ty.parameters.as_single())), TypeCtor::RawPtr(..) => Some(Ty::clone(a_ty.parameters.as_single())),
_ => None, _ => None,
}, },
_ => None, _ => None,
@ -318,25 +318,25 @@ impl HirDisplay for &Ty {
impl HirDisplay for ApplicationTy { impl HirDisplay for ApplicationTy {
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
match self.name { match self.name {
TypeName::Bool => write!(f, "bool")?, TypeCtor::Bool => write!(f, "bool")?,
TypeName::Char => write!(f, "char")?, TypeCtor::Char => write!(f, "char")?,
TypeName::Int(t) => write!(f, "{}", t)?, TypeCtor::Int(t) => write!(f, "{}", t)?,
TypeName::Float(t) => write!(f, "{}", t)?, TypeCtor::Float(t) => write!(f, "{}", t)?,
TypeName::Str => write!(f, "str")?, TypeCtor::Str => write!(f, "str")?,
TypeName::Slice | TypeName::Array => { TypeCtor::Slice | TypeCtor::Array => {
let t = self.parameters.as_single(); let t = self.parameters.as_single();
write!(f, "[{}]", t.display(f.db))?; write!(f, "[{}]", t.display(f.db))?;
} }
TypeName::RawPtr(m) => { TypeCtor::RawPtr(m) => {
let t = self.parameters.as_single(); let t = self.parameters.as_single();
write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?; write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?;
} }
TypeName::Ref(m) => { TypeCtor::Ref(m) => {
let t = self.parameters.as_single(); let t = self.parameters.as_single();
write!(f, "&{}{}", m.as_keyword_for_ref(), t.display(f.db))?; write!(f, "&{}{}", m.as_keyword_for_ref(), t.display(f.db))?;
} }
TypeName::Never => write!(f, "!")?, TypeCtor::Never => write!(f, "!")?,
TypeName::Tuple => { TypeCtor::Tuple => {
let ts = &self.parameters; let ts = &self.parameters;
if ts.0.len() == 1 { if ts.0.len() == 1 {
write!(f, "({},)", ts.0[0].display(f.db))?; write!(f, "({},)", ts.0[0].display(f.db))?;
@ -346,13 +346,13 @@ impl HirDisplay for ApplicationTy {
write!(f, ")")?; write!(f, ")")?;
} }
} }
TypeName::FnPtr => { TypeCtor::FnPtr => {
let sig = FnSig::from_fn_ptr_substs(&self.parameters); let sig = FnSig::from_fn_ptr_substs(&self.parameters);
write!(f, "fn(")?; write!(f, "fn(")?;
f.write_joined(sig.params(), ", ")?; f.write_joined(sig.params(), ", ")?;
write!(f, ") -> {}", sig.ret().display(f.db))?; write!(f, ") -> {}", sig.ret().display(f.db))?;
} }
TypeName::FnDef(def) => { TypeCtor::FnDef(def) => {
let sig = f.db.callable_item_signature(def); let sig = f.db.callable_item_signature(def);
let name = match def { let name = match def {
CallableDef::Function(ff) => ff.name(f.db), CallableDef::Function(ff) => ff.name(f.db),
@ -372,7 +372,7 @@ impl HirDisplay for ApplicationTy {
f.write_joined(sig.params(), ", ")?; f.write_joined(sig.params(), ", ")?;
write!(f, ") -> {}", sig.ret().display(f.db))?; write!(f, ") -> {}", sig.ret().display(f.db))?;
} }
TypeName::Adt(def_id) => { TypeCtor::Adt(def_id) => {
let name = match def_id { let name = match def_id {
AdtDef::Struct(s) => s.name(f.db), AdtDef::Struct(s) => s.name(f.db),
AdtDef::Enum(e) => e.name(f.db), AdtDef::Enum(e) => e.name(f.db),

View file

@ -38,7 +38,7 @@ use crate::{
resolve::{Resolver, Resolution}, resolve::{Resolver, Resolution},
nameres::Namespace nameres::Namespace
}; };
use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeName}; use super::{Ty, TypableDef, Substs, primitive, op, FnSig, ApplicationTy, TypeCtor};
/// The entry point of type inference. /// The entry point of type inference.
pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> { pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> {
@ -278,11 +278,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
match ty { match ty {
Ty::Unknown => self.new_type_var(), Ty::Unknown => self.new_type_var(),
Ty::Apply(ApplicationTy { Ty::Apply(ApplicationTy {
name: TypeName::Int(primitive::UncertainIntTy::Unknown), name: TypeCtor::Int(primitive::UncertainIntTy::Unknown),
.. ..
}) => self.new_integer_var(), }) => self.new_integer_var(),
Ty::Apply(ApplicationTy { Ty::Apply(ApplicationTy {
name: TypeName::Float(primitive::UncertainFloatTy::Unknown), name: TypeCtor::Float(primitive::UncertainFloatTy::Unknown),
.. ..
}) => self.new_float_var(), }) => self.new_float_var(),
_ => ty, _ => ty,
@ -629,7 +629,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.into(); .into();
Ty::apply(TypeName::Tuple, Substs(inner_tys)) Ty::apply(TypeCtor::Tuple, Substs(inner_tys))
} }
Pat::Ref { pat, mutability } => { Pat::Ref { pat, mutability } => {
let expectation = match expected.as_reference() { let expectation = match expected.as_reference() {
@ -642,7 +642,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
_ => &Ty::Unknown, _ => &Ty::Unknown,
}; };
let subty = self.infer_pat(*pat, expectation, default_bm); let subty = self.infer_pat(*pat, expectation, default_bm);
Ty::apply_one(TypeName::Ref(*mutability), subty.into()) Ty::apply_one(TypeCtor::Ref(*mutability), subty.into())
} }
Pat::TupleStruct { path: ref p, args: ref subpats } => { Pat::TupleStruct { path: ref p, args: ref subpats } => {
self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm) self.infer_tuple_struct_pat(p.as_ref(), subpats, expected, default_bm)
@ -670,7 +670,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let bound_ty = match mode { let bound_ty = match mode {
BindingMode::Ref(mutability) => { BindingMode::Ref(mutability) => {
Ty::apply_one(TypeName::Ref(mutability), inner_ty.clone().into()) Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone().into())
} }
BindingMode::Move => inner_ty.clone(), BindingMode::Move => inner_ty.clone(),
}; };
@ -725,7 +725,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
Expr::Missing => Ty::Unknown, Expr::Missing => Ty::Unknown,
Expr::If { condition, then_branch, else_branch } => { Expr::If { condition, then_branch, else_branch } => {
// if let is desugared to match, so this is always simple if // if let is desugared to match, so this is always simple if
self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeName::Bool))); self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
let then_ty = self.infer_expr(*then_branch, expected); let then_ty = self.infer_expr(*then_branch, expected);
match else_branch { match else_branch {
Some(else_branch) => { Some(else_branch) => {
@ -742,11 +742,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
Expr::Loop { body } => { Expr::Loop { body } => {
self.infer_expr(*body, &Expectation::has_type(Ty::unit())); self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
// TODO handle break with value // TODO handle break with value
Ty::simple(TypeName::Never) Ty::simple(TypeCtor::Never)
} }
Expr::While { condition, body } => { Expr::While { condition, body } => {
// while let is desugared to a match loop, so this is always simple while // while let is desugared to a match loop, so this is always simple while
self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeName::Bool))); self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
self.infer_expr(*body, &Expectation::has_type(Ty::unit())); self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
Ty::unit() Ty::unit()
} }
@ -777,11 +777,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let callee_ty = self.infer_expr(*callee, &Expectation::none()); let callee_ty = self.infer_expr(*callee, &Expectation::none());
let (param_tys, ret_ty) = match &callee_ty { let (param_tys, ret_ty) = match &callee_ty {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::FnPtr => { TypeCtor::FnPtr => {
let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters); let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters);
(sig.params().to_vec(), sig.ret().clone()) (sig.params().to_vec(), sig.ret().clone())
} }
TypeName::FnDef(def) => { TypeCtor::FnDef(def) => {
let sig = self.db.callable_item_signature(def); let sig = self.db.callable_item_signature(def);
let ret_ty = sig.ret().clone().subst(&a_ty.parameters); let ret_ty = sig.ret().clone().subst(&a_ty.parameters);
let param_tys = sig let param_tys = sig
@ -824,7 +824,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let method_ty = self.insert_type_vars(method_ty); let method_ty = self.insert_type_vars(method_ty);
let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty { let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::FnPtr => { TypeCtor::FnPtr => {
let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters); let sig = FnSig::from_fn_ptr_substs(&a_ty.parameters);
if !sig.params().is_empty() { if !sig.params().is_empty() {
( (
@ -836,7 +836,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
(Ty::Unknown, Vec::new(), sig.ret().clone()) (Ty::Unknown, Vec::new(), sig.ret().clone())
} }
} }
TypeName::FnDef(def) => { TypeCtor::FnDef(def) => {
let sig = self.db.callable_item_signature(def); let sig = self.db.callable_item_signature(def);
let ret_ty = sig.ret().clone().subst(&a_ty.parameters); let ret_ty = sig.ret().clone().subst(&a_ty.parameters);
@ -858,7 +858,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
// Apply autoref so the below unification works correctly // Apply autoref so the below unification works correctly
let actual_receiver_ty = match expected_receiver_ty.as_reference() { let actual_receiver_ty = match expected_receiver_ty.as_reference() {
Some((_, mutability)) => { Some((_, mutability)) => {
Ty::apply_one(TypeName::Ref(mutability), derefed_receiver_ty) Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty)
} }
_ => derefed_receiver_ty, _ => derefed_receiver_ty,
}; };
@ -885,7 +885,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
if let Some(guard_expr) = arm.guard { if let Some(guard_expr) = arm.guard {
self.infer_expr( self.infer_expr(
guard_expr, guard_expr,
&Expectation::has_type(Ty::simple(TypeName::Bool)), &Expectation::has_type(Ty::simple(TypeCtor::Bool)),
); );
} }
self.infer_expr(arm.expr, &expected); self.infer_expr(arm.expr, &expected);
@ -898,19 +898,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr); let resolver = expr::resolver_for_expr(self.body.clone(), self.db, tgt_expr);
self.infer_path_expr(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) self.infer_path_expr(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
} }
Expr::Continue => Ty::simple(TypeName::Never), Expr::Continue => Ty::simple(TypeCtor::Never),
Expr::Break { expr } => { Expr::Break { expr } => {
if let Some(expr) = expr { if let Some(expr) = expr {
// TODO handle break with value // TODO handle break with value
self.infer_expr(*expr, &Expectation::none()); self.infer_expr(*expr, &Expectation::none());
} }
Ty::simple(TypeName::Never) Ty::simple(TypeCtor::Never)
} }
Expr::Return { expr } => { Expr::Return { expr } => {
if let Some(expr) = expr { if let Some(expr) = expr {
self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone())); self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone()));
} }
Ty::simple(TypeName::Never) Ty::simple(TypeCtor::Never)
} }
Expr::StructLit { path, fields, spread } => { Expr::StructLit { path, fields, spread } => {
let (ty, def_id) = self.resolve_variant(path.as_ref()); let (ty, def_id) = self.resolve_variant(path.as_ref());
@ -933,11 +933,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
.autoderef(self.db) .autoderef(self.db)
.find_map(|derefed_ty| match derefed_ty { .find_map(|derefed_ty| match derefed_ty {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::Tuple => { TypeCtor::Tuple => {
let i = name.to_string().parse::<usize>().ok(); let i = name.to_string().parse::<usize>().ok();
i.and_then(|i| a_ty.parameters.0.get(i).cloned()) i.and_then(|i| a_ty.parameters.0.get(i).cloned())
} }
TypeName::Adt(AdtDef::Struct(s)) => { TypeCtor::Adt(AdtDef::Struct(s)) => {
s.field(self.db, name).map(|field| { s.field(self.db, name).map(|field| {
self.write_field_resolution(tgt_expr, field); self.write_field_resolution(tgt_expr, field);
field.ty(self.db).subst(&a_ty.parameters) field.ty(self.db).subst(&a_ty.parameters)
@ -973,7 +973,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}; };
// TODO reference coercions etc. // TODO reference coercions etc.
let inner_ty = self.infer_expr(*expr, &expectation); let inner_ty = self.infer_expr(*expr, &expectation);
Ty::apply_one(TypeName::Ref(*mutability), inner_ty) Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty)
} }
Expr::UnaryOp { expr, op } => { Expr::UnaryOp { expr, op } => {
let inner_ty = self.infer_expr(*expr, &Expectation::none()); let inner_ty = self.infer_expr(*expr, &Expectation::none());
@ -989,9 +989,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
UnaryOp::Neg => { UnaryOp::Neg => {
match &inner_ty { match &inner_ty {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::Int(primitive::UncertainIntTy::Unknown) TypeCtor::Int(primitive::UncertainIntTy::Unknown)
| TypeName::Int(primitive::UncertainIntTy::Signed(..)) | TypeCtor::Int(primitive::UncertainIntTy::Signed(..))
| TypeName::Float(..) => inner_ty, | TypeCtor::Float(..) => inner_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => { Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => {
@ -1004,7 +1004,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
UnaryOp::Not => { UnaryOp::Not => {
match &inner_ty { match &inner_ty {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::Bool | TypeName::Int(_) => inner_ty, TypeCtor::Bool | TypeCtor::Int(_) => inner_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
Ty::Infer(InferTy::IntVar(..)) => inner_ty, Ty::Infer(InferTy::IntVar(..)) => inner_ty,
@ -1018,7 +1018,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
Some(op) => { Some(op) => {
let lhs_expectation = match op { let lhs_expectation = match op {
BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { BinaryOp::BooleanAnd | BinaryOp::BooleanOr => {
Expectation::has_type(Ty::simple(TypeName::Bool)) Expectation::has_type(Ty::simple(TypeCtor::Bool))
} }
_ => Expectation::none(), _ => Expectation::none(),
}; };
@ -1039,12 +1039,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
ty_vec.push(self.infer_expr(*arg, &Expectation::none())); ty_vec.push(self.infer_expr(*arg, &Expectation::none()));
} }
Ty::apply(TypeName::Tuple, Substs(ty_vec.into())) Ty::apply(TypeCtor::Tuple, Substs(ty_vec.into()))
} }
Expr::Array { exprs } => { Expr::Array { exprs } => {
let elem_ty = match &expected.ty { let elem_ty = match &expected.ty {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::Slice | TypeName::Array => { TypeCtor::Slice | TypeCtor::Array => {
Ty::clone(&a_ty.parameters.as_single()) Ty::clone(&a_ty.parameters.as_single())
} }
_ => self.new_type_var(), _ => self.new_type_var(),
@ -1056,23 +1056,23 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone())); self.infer_expr(*expr, &Expectation::has_type(elem_ty.clone()));
} }
Ty::apply_one(TypeName::Array, elem_ty) Ty::apply_one(TypeCtor::Array, elem_ty)
} }
Expr::Literal(lit) => match lit { Expr::Literal(lit) => match lit {
Literal::Bool(..) => Ty::simple(TypeName::Bool), Literal::Bool(..) => Ty::simple(TypeCtor::Bool),
Literal::String(..) => { Literal::String(..) => {
Ty::apply_one(TypeName::Ref(Mutability::Shared), Ty::simple(TypeName::Str)) Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
} }
Literal::ByteString(..) => { Literal::ByteString(..) => {
let byte_type = Ty::simple(TypeName::Int(primitive::UncertainIntTy::Unsigned( let byte_type = Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Unsigned(
primitive::UintTy::U8, primitive::UintTy::U8,
))); )));
let slice_type = Ty::apply_one(TypeName::Slice, byte_type); let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type);
Ty::apply_one(TypeName::Ref(Mutability::Shared), slice_type) Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type)
} }
Literal::Char(..) => Ty::simple(TypeName::Char), Literal::Char(..) => Ty::simple(TypeCtor::Char),
Literal::Int(_v, ty) => Ty::simple(TypeName::Int(*ty)), Literal::Int(_v, ty) => Ty::simple(TypeCtor::Int(*ty)),
Literal::Float(_v, ty) => Ty::simple(TypeName::Float(*ty)), Literal::Float(_v, ty) => Ty::simple(TypeCtor::Float(*ty)),
}, },
}; };
// use a new type variable if we got Ty::Unknown here // use a new type variable if we got Ty::Unknown here
@ -1208,9 +1208,9 @@ impl InferTy {
match self { match self {
InferTy::TypeVar(..) => Ty::Unknown, InferTy::TypeVar(..) => Ty::Unknown,
InferTy::IntVar(..) => { InferTy::IntVar(..) => {
Ty::simple(TypeName::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32))) Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32)))
} }
InferTy::FloatVar(..) => Ty::simple(TypeName::Float( InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(
primitive::UncertainFloatTy::Known(primitive::FloatTy::F64), primitive::UncertainFloatTy::Known(primitive::FloatTy::F64),
)), )),
} }

View file

@ -19,40 +19,40 @@ use crate::{
generics::GenericParams, generics::GenericParams,
adt::VariantDef, adt::VariantDef,
}; };
use super::{Ty, primitive, FnSig, Substs, TypeName}; use super::{Ty, primitive, FnSig, Substs, TypeCtor};
impl Ty { impl Ty {
pub(crate) fn from_hir(db: &impl HirDatabase, resolver: &Resolver, type_ref: &TypeRef) -> Self { pub(crate) fn from_hir(db: &impl HirDatabase, resolver: &Resolver, type_ref: &TypeRef) -> Self {
match type_ref { match type_ref {
TypeRef::Never => Ty::simple(TypeName::Never), TypeRef::Never => Ty::simple(TypeCtor::Never),
TypeRef::Tuple(inner) => { TypeRef::Tuple(inner) => {
let inner_tys = let inner_tys =
inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>();
Ty::apply(TypeName::Tuple, Substs(inner_tys.into())) Ty::apply(TypeCtor::Tuple, Substs(inner_tys.into()))
} }
TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path),
TypeRef::RawPtr(inner, mutability) => { TypeRef::RawPtr(inner, mutability) => {
let inner_ty = Ty::from_hir(db, resolver, inner); let inner_ty = Ty::from_hir(db, resolver, inner);
Ty::apply_one(TypeName::RawPtr(*mutability), inner_ty) Ty::apply_one(TypeCtor::RawPtr(*mutability), inner_ty)
} }
TypeRef::Array(inner) => { TypeRef::Array(inner) => {
let inner_ty = Ty::from_hir(db, resolver, inner); let inner_ty = Ty::from_hir(db, resolver, inner);
Ty::apply_one(TypeName::Array, inner_ty) Ty::apply_one(TypeCtor::Array, inner_ty)
} }
TypeRef::Slice(inner) => { TypeRef::Slice(inner) => {
let inner_ty = Ty::from_hir(db, resolver, inner); let inner_ty = Ty::from_hir(db, resolver, inner);
Ty::apply_one(TypeName::Slice, inner_ty) Ty::apply_one(TypeCtor::Slice, inner_ty)
} }
TypeRef::Reference(inner, mutability) => { TypeRef::Reference(inner, mutability) => {
let inner_ty = Ty::from_hir(db, resolver, inner); let inner_ty = Ty::from_hir(db, resolver, inner);
Ty::apply_one(TypeName::Ref(*mutability), inner_ty) Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty)
} }
TypeRef::Placeholder => Ty::Unknown, TypeRef::Placeholder => Ty::Unknown,
TypeRef::Fn(params) => { TypeRef::Fn(params) => {
let inner_tys = let inner_tys =
params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>();
let sig = Substs(inner_tys.into()); let sig = Substs(inner_tys.into());
Ty::apply(TypeName::FnPtr, sig) Ty::apply(TypeCtor::FnPtr, sig)
} }
TypeRef::Error => Ty::Unknown, TypeRef::Error => Ty::Unknown,
} }
@ -62,14 +62,14 @@ impl Ty {
if let Some(name) = path.as_ident() { if let Some(name) = path.as_ident() {
// TODO handle primitive type names in resolver as well? // TODO handle primitive type names in resolver as well?
if let Some(int_ty) = primitive::UncertainIntTy::from_type_name(name) { if let Some(int_ty) = primitive::UncertainIntTy::from_type_name(name) {
return Ty::simple(TypeName::Int(int_ty)); return Ty::simple(TypeCtor::Int(int_ty));
} else if let Some(float_ty) = primitive::UncertainFloatTy::from_type_name(name) { } else if let Some(float_ty) = primitive::UncertainFloatTy::from_type_name(name) {
return Ty::simple(TypeName::Float(float_ty)); return Ty::simple(TypeCtor::Float(float_ty));
} else if let Some(known) = name.as_known_name() { } else if let Some(known) = name.as_known_name() {
match known { match known {
KnownName::Bool => return Ty::simple(TypeName::Bool), KnownName::Bool => return Ty::simple(TypeCtor::Bool),
KnownName::Char => return Ty::simple(TypeName::Char), KnownName::Char => return Ty::simple(TypeCtor::Char),
KnownName::Str => return Ty::simple(TypeName::Str), KnownName::Str => return Ty::simple(TypeCtor::Str),
_ => {} _ => {}
} }
} }
@ -245,7 +245,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty {
let generics = def.generic_params(db); let generics = def.generic_params(db);
let substs = make_substs(&generics); let substs = make_substs(&generics);
Ty::apply(TypeName::FnDef(def.into()), substs) Ty::apply(TypeCtor::FnDef(def.into()), substs)
} }
/// Build the declared type of a const. /// Build the declared type of a const.
@ -287,7 +287,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty {
} }
let generics = def.generic_params(db); let generics = def.generic_params(db);
let substs = make_substs(&generics); let substs = make_substs(&generics);
Ty::apply(TypeName::FnDef(def.into()), substs) Ty::apply(TypeCtor::FnDef(def.into()), substs)
} }
fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig {
@ -315,7 +315,7 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) ->
} }
let generics = def.parent_enum(db).generic_params(db); let generics = def.parent_enum(db).generic_params(db);
let substs = make_substs(&generics); let substs = make_substs(&generics);
Ty::apply(TypeName::FnDef(def.into()), substs) Ty::apply(TypeCtor::FnDef(def.into()), substs)
} }
fn make_substs(generics: &GenericParams) -> Substs { fn make_substs(generics: &GenericParams) -> Substs {
@ -331,12 +331,12 @@ fn make_substs(generics: &GenericParams) -> Substs {
fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty { fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty {
let generics = s.generic_params(db); let generics = s.generic_params(db);
Ty::apply(TypeName::Adt(s.into()), make_substs(&generics)) Ty::apply(TypeCtor::Adt(s.into()), make_substs(&generics))
} }
fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty { fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty {
let generics = s.generic_params(db); let generics = s.generic_params(db);
Ty::apply(TypeName::Adt(s.into()), make_substs(&generics)) Ty::apply(TypeCtor::Adt(s.into()), make_substs(&generics))
} }
fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty {

View file

@ -10,7 +10,7 @@ use crate::{
HirDatabase, Module, Crate, Name, Function, Trait, HirDatabase, Module, Crate, Name, Function, Trait,
ids::TraitId, ids::TraitId,
impl_block::{ImplId, ImplBlock, ImplItem}, impl_block::{ImplId, ImplBlock, ImplItem},
ty::{Ty, TypeName}, ty::{Ty, TypeCtor},
nameres::CrateModuleId, nameres::CrateModuleId,
}; };
@ -18,7 +18,7 @@ use crate::{
/// This is used as a key for indexing impls. /// This is used as a key for indexing impls.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum TyFingerprint { pub enum TyFingerprint {
Apply(TypeName), Apply(TypeCtor),
} }
impl TyFingerprint { impl TyFingerprint {
@ -112,7 +112,7 @@ impl CrateImplBlocks {
fn def_crate(db: &impl HirDatabase, ty: &Ty) -> Option<Crate> { fn def_crate(db: &impl HirDatabase, ty: &Ty) -> Option<Crate> {
match ty { match ty {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::Adt(def_id) => def_id.krate(db), TypeCtor::Adt(def_id) => def_id.krate(db),
_ => None, _ => None,
}, },
_ => None, _ => None,

View file

@ -1,5 +1,5 @@
use crate::{ ty::ApplicationTy, expr::BinaryOp}; use crate::{ ty::ApplicationTy, expr::BinaryOp};
use super::{Ty, TypeName, InferTy}; use super::{Ty, TypeCtor, InferTy};
pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
match op { match op {
@ -10,7 +10,7 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
| BinaryOp::LesserEqualTest | BinaryOp::LesserEqualTest
| BinaryOp::GreaterEqualTest | BinaryOp::GreaterEqualTest
| BinaryOp::LesserTest | BinaryOp::LesserTest
| BinaryOp::GreaterTest => Ty::simple(TypeName::Bool), | BinaryOp::GreaterTest => Ty::simple(TypeCtor::Bool),
BinaryOp::Assignment BinaryOp::Assignment
| BinaryOp::AddAssign | BinaryOp::AddAssign
| BinaryOp::SubAssign | BinaryOp::SubAssign
@ -33,7 +33,7 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
| BinaryOp::BitwiseOr | BinaryOp::BitwiseOr
| BinaryOp::BitwiseXor => match rhs_ty { | BinaryOp::BitwiseXor => match rhs_ty {
Ty::Apply(ApplicationTy { name, .. }) => match name { Ty::Apply(ApplicationTy { name, .. }) => match name {
TypeName::Int(..) | TypeName::Float(..) => rhs_ty, TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
@ -45,14 +45,14 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
match op { match op {
BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::simple(TypeName::Bool), BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::simple(TypeCtor::Bool),
BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty { BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty {
Ty::Apply(ApplicationTy { name, .. }) => match name { Ty::Apply(ApplicationTy { name, .. }) => match name {
TypeName::Int(..) TypeCtor::Int(..)
| TypeName::Float(..) | TypeCtor::Float(..)
| TypeName::Str | TypeCtor::Str
| TypeName::Char | TypeCtor::Char
| TypeName::Bool => lhs_ty, | TypeCtor::Bool => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
@ -83,7 +83,7 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
| BinaryOp::BitwiseOr | BinaryOp::BitwiseOr
| BinaryOp::BitwiseXor => match lhs_ty { | BinaryOp::BitwiseXor => match lhs_ty {
Ty::Apply(ApplicationTy { name, .. }) => match name { Ty::Apply(ApplicationTy { name, .. }) => match name {
TypeName::Int(..) | TypeName::Float(..) => lhs_ty, TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,

View file

@ -1,4 +1,4 @@
use hir::{Ty, AdtDef, TypeName}; use hir::{Ty, AdtDef, TypeCtor};
use crate::completion::{CompletionContext, Completions}; use crate::completion::{CompletionContext, Completions};
@ -25,13 +25,13 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
for receiver in receiver.autoderef(ctx.db) { for receiver in receiver.autoderef(ctx.db) {
match receiver { match receiver {
Ty::Apply(a_ty) => match a_ty.name { Ty::Apply(a_ty) => match a_ty.name {
TypeName::Adt(AdtDef::Struct(s)) => { TypeCtor::Adt(AdtDef::Struct(s)) => {
for field in s.fields(ctx.db) { for field in s.fields(ctx.db) {
acc.add_field(ctx, field, &a_ty.parameters); acc.add_field(ctx, field, &a_ty.parameters);
} }
} }
// TODO unions // TODO unions
TypeName::Tuple => { TypeCtor::Tuple => {
for (i, ty) in a_ty.parameters.iter().enumerate() { for (i, ty) in a_ty.parameters.iter().enumerate() {
acc.add_pos_field(ctx, i, ty); acc.add_pos_field(ctx, i, ty);
} }