mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Introduce TypeInfo
This commit is contained in:
parent
29506b5a26
commit
25ff7171c4
32 changed files with 127 additions and 124 deletions
|
@ -86,7 +86,7 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
|
|||
let name_ref = call_node.name_ref()?;
|
||||
let func_target = match call_node {
|
||||
FnCallNode::CallExpr(expr) => {
|
||||
let callable = sema.type_of_expr(&expr.expr()?)?.as_callable(db)?;
|
||||
let callable = sema.type_of_expr(&expr.expr()?)?.ty.as_callable(db)?;
|
||||
match callable.kind() {
|
||||
hir::CallableKind::Function(it) => it.try_to_nav(db),
|
||||
_ => None,
|
||||
|
|
|
@ -32,8 +32,8 @@ pub(crate) fn goto_type_definition(
|
|||
let (ty, node) = sema.token_ancestors_with_macros(token).find_map(|node| {
|
||||
let ty = match_ast! {
|
||||
match node {
|
||||
ast::Expr(it) => sema.type_of_expr(&it)?,
|
||||
ast::Pat(it) => sema.type_of_pat(&it)?,
|
||||
ast::Expr(it) => sema.type_of_expr(&it)?.ty,
|
||||
ast::Pat(it) => sema.type_of_pat(&it)?.ty,
|
||||
ast::SelfParam(it) => sema.type_of_self(&it)?,
|
||||
ast::Type(it) => sema.resolve_type(&it)?,
|
||||
ast::RecordField(it) => sema.to_def(&it).map(|d| d.ty(db.upcast()))?,
|
||||
|
|
|
@ -123,7 +123,7 @@ fn highlight_exit_points(
|
|||
}
|
||||
}
|
||||
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
|
||||
if sema.type_of_expr(&expr).map_or(false, |ty| ty.is_never()) {
|
||||
if sema.type_of_expr(&expr).map_or(false, |ty| ty.ty.is_never()) {
|
||||
highlights
|
||||
.push(HighlightedRange { access: None, range: expr.syntax().text_range() });
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use either::Either;
|
||||
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics};
|
||||
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
||||
use ide_db::{
|
||||
base_db::{FileRange, SourceDatabase},
|
||||
defs::{Definition, NameClass, NameRefClass},
|
||||
|
@ -225,19 +225,15 @@ fn hover_type_info(
|
|||
config: &HoverConfig,
|
||||
expr_or_pat: &Either<ast::Expr, ast::Pat>,
|
||||
) -> Option<HoverResult> {
|
||||
let (ty, coerced) = match expr_or_pat {
|
||||
Either::Left(expr) => sema.type_of_expr_with_coercion(expr)?,
|
||||
Either::Right(pat) => sema.type_of_pat_with_coercion(pat)?,
|
||||
let TypeInfo { ty, coerced } = match expr_or_pat {
|
||||
Either::Left(expr) => sema.type_of_expr(expr)?,
|
||||
Either::Right(pat) => sema.type_of_pat(pat)?,
|
||||
};
|
||||
|
||||
let mut res = HoverResult::default();
|
||||
res.markup = if coerced {
|
||||
let uncoerced_ty = match expr_or_pat {
|
||||
Either::Left(expr) => sema.type_of_expr(expr)?,
|
||||
Either::Right(pat) => sema.type_of_pat(pat)?,
|
||||
};
|
||||
let uncoerced = uncoerced_ty.display(sema.db).to_string();
|
||||
let coerced = ty.display(sema.db).to_string();
|
||||
res.markup = if let Some(coerced_ty) = coerced {
|
||||
let uncoerced = ty.display(sema.db).to_string();
|
||||
let coerced = coerced_ty.display(sema.db).to_string();
|
||||
format!(
|
||||
"```text\nType: {:>upad$}\nCoerced to: {:>cpad$}\n```\n",
|
||||
uncoerced = uncoerced,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use either::Either;
|
||||
use hir::{known, Callable, HasVisibility, HirDisplay, Semantics};
|
||||
use hir::{known, Callable, HasVisibility, HirDisplay, Semantics, TypeInfo};
|
||||
use ide_db::helpers::FamousDefs;
|
||||
use ide_db::RootDatabase;
|
||||
use stdx::to_lower_snake_case;
|
||||
|
@ -117,7 +117,7 @@ fn get_chaining_hints(
|
|||
next_next = tokens.next()?.kind();
|
||||
}
|
||||
if next_next == T![.] {
|
||||
let ty = sema.type_of_expr(&expr)?;
|
||||
let ty = sema.type_of_expr(&expr)?.ty;
|
||||
if ty.is_unknown() {
|
||||
return None;
|
||||
}
|
||||
|
@ -189,7 +189,7 @@ fn get_bind_pat_hints(
|
|||
let krate = sema.scope(pat.syntax()).module().map(|it| it.krate());
|
||||
let famous_defs = FamousDefs(sema, krate);
|
||||
|
||||
let ty = sema.type_of_pat(&pat.clone().into())?;
|
||||
let ty = sema.type_of_pat(&pat.clone().into())?.ty;
|
||||
|
||||
if should_not_display_type_hint(sema, &pat, &ty) {
|
||||
return None;
|
||||
|
@ -308,6 +308,7 @@ fn should_not_display_type_hint(
|
|||
return it.in_token().is_none() ||
|
||||
it.iterable()
|
||||
.and_then(|iterable_expr| sema.type_of_expr(&iterable_expr))
|
||||
.map(TypeInfo::ty)
|
||||
.map_or(true, |iterable_ty| iterable_ty.is_unknown() || iterable_ty.is_unit())
|
||||
},
|
||||
_ => (),
|
||||
|
@ -393,7 +394,7 @@ fn is_enum_name_similar_to_param_name(
|
|||
argument: &ast::Expr,
|
||||
param_name: &str,
|
||||
) -> bool {
|
||||
match sema.type_of_expr(argument).and_then(|t| t.as_adt()) {
|
||||
match sema.type_of_expr(argument).and_then(|t| t.ty.as_adt()) {
|
||||
Some(hir::Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
|
||||
_ => false,
|
||||
}
|
||||
|
@ -430,7 +431,7 @@ fn get_callable(
|
|||
) -> Option<(hir::Callable, ast::ArgList)> {
|
||||
match expr {
|
||||
ast::Expr::CallExpr(expr) => {
|
||||
sema.type_of_expr(&expr.expr()?)?.as_callable(sema.db).zip(expr.arg_list())
|
||||
sema.type_of_expr(&expr.expr()?)?.ty.as_callable(sema.db).zip(expr.arg_list())
|
||||
}
|
||||
ast::Expr::MethodCallExpr(expr) => {
|
||||
sema.resolve_method_call_as_callable(expr).zip(expr.arg_list())
|
||||
|
|
|
@ -123,7 +123,7 @@ pub(super) fn element(
|
|||
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
|
||||
|
||||
let expr = prefix_expr.expr()?;
|
||||
let ty = sema.type_of_expr(&expr)?;
|
||||
let ty = sema.type_of_expr(&expr)?.ty;
|
||||
if ty.is_raw_ptr() {
|
||||
HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
|
||||
} else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
|
||||
|
@ -555,7 +555,7 @@ fn highlight_method_call(
|
|||
if let Some(receiver_ty) =
|
||||
method_call.receiver().and_then(|it| sema.type_of_expr(&it))
|
||||
{
|
||||
if !receiver_ty.is_copy(sema.db) {
|
||||
if !receiver_ty.ty.is_copy(sema.db) {
|
||||
h |= HlMod::Consuming
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue