Introduce TypeInfo

This commit is contained in:
Lukas Wirth 2021-08-02 20:42:25 +02:00
parent 29506b5a26
commit 25ff7171c4
32 changed files with 127 additions and 124 deletions

View file

@ -87,6 +87,28 @@ impl PathResolution {
}
}
#[derive(Debug)]
pub struct TypeInfo {
/// The original type of the expression or pattern.
pub ty: Type,
/// The coerced type, if a coercion happened.
pub coerced: Option<Type>,
}
impl TypeInfo {
pub fn ty(self) -> Type {
self.ty
}
pub fn coerced(self) -> Option<Type> {
self.coerced
}
pub fn coerced_or_original(self) -> Type {
self.coerced.unwrap_or(self.ty)
}
}
/// Primary API to get semantic information, like types, from syntax trees.
pub struct Semantics<'db, DB> {
pub db: &'db DB,
@ -212,23 +234,14 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.resolve_type(ty)
}
pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {
self.imp.type_of_expr(expr)
}
/// Returns true in case a coercion happened.
pub fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, bool)> {
self.imp.type_of_expr_with_coercion(expr)
}
pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<TypeInfo> {
self.imp.type_of_pat(pat)
}
pub fn type_of_pat_with_coercion(&self, expr: &ast::Pat) -> Option<(Type, bool)> {
self.imp.type_of_pat_with_coercion(expr)
}
pub fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
self.imp.type_of_self(param)
}
@ -565,20 +578,16 @@ impl<'db> SemanticsImpl<'db> {
Type::new_with_resolver(self.db, &scope.resolver, ty)
}
fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
self.analyze(expr.syntax()).type_of_expr(self.db, expr)
fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {
self.analyze(expr.syntax())
.type_of_expr(self.db, expr)
.map(|(ty, coerced)| TypeInfo { ty, coerced })
}
fn type_of_expr_with_coercion(&self, expr: &ast::Expr) -> Option<(Type, bool)> {
self.analyze(expr.syntax()).type_of_expr_with_coercion(self.db, expr)
}
fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
self.analyze(pat.syntax()).type_of_pat(self.db, pat)
}
fn type_of_pat_with_coercion(&self, pat: &ast::Pat) -> Option<(Type, bool)> {
self.analyze(pat.syntax()).type_of_pat_with_coercion(self.db, pat)
fn type_of_pat(&self, pat: &ast::Pat) -> Option<TypeInfo> {
self.analyze(pat.syntax())
.type_of_pat(self.db, pat)
.map(|(ty, coerced)| TypeInfo { ty, coerced })
}
fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
@ -757,7 +766,7 @@ impl<'db> SemanticsImpl<'db> {
ast::Expr::FieldExpr(field_expr) => field_expr,
_ => return None,
};
let ty = self.type_of_expr(&field_expr.expr()?)?;
let ty = self.type_of_expr(&field_expr.expr()?)?.ty;
if !ty.is_packed(self.db) {
return None;
}
@ -784,7 +793,7 @@ impl<'db> SemanticsImpl<'db> {
self.type_of_expr(&expr)
})
// Binding a reference to a packed type is possibly unsafe.
.map(|ty| ty.is_packed(self.db))
.map(|ty| ty.ty.is_packed(self.db))
.unwrap_or(false)
// FIXME This needs layout computation to be correct. It will highlight
@ -830,7 +839,7 @@ impl<'db> SemanticsImpl<'db> {
}
})
// Binding a reference to a packed type is possibly unsafe.
.map(|ty| ty.is_packed(self.db))
.map(|ty| ty.ty.is_packed(self.db))
.unwrap_or(false)
}
}