From e5eadb339039e21718d382c0b3d02a4bf053b3f4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Nov 2019 14:02:57 +0300 Subject: Introduce hir::Type It should provide a convenient API over more low-level Ty --- crates/ra_ide_api/src/call_info.rs | 4 +-- crates/ra_ide_api/src/completion/complete_dot.rs | 36 ++++++++------------ .../ra_ide_api/src/completion/complete_postfix.rs | 16 +++------ .../src/completion/complete_record_literal.rs | 7 ++-- .../src/completion/complete_record_pattern.rs | 7 ++-- crates/ra_ide_api/src/completion/presentation.rs | 39 +++++++++------------- crates/ra_ide_api/src/goto_type_definition.rs | 4 +-- crates/ra_ide_api/src/inlay_hints.rs | 23 ++++--------- crates/ra_ide_api/src/syntax_highlighting.rs | 9 +++-- 9 files changed, 52 insertions(+), 93 deletions(-) (limited to 'crates/ra_ide_api/src') diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 9beceb29c..d0283e410 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -26,8 +26,8 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { - //FIXME: don't poke into Ty - let (callable_def, _subst) = analyzer.type_of(db, &expr.expr()?)?.as_callable()?; + //FIXME: Type::as_callable is broken + let callable_def = analyzer.type_of(db, &expr.expr()?)?.as_callable()?; match callable_def { hir::CallableDef::FunctionId(it) => { let fn_def = it.into(); diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 5a3f9b5f6..b6fe48627 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{Adt, Ty, TypeCtor}; +use hir::Type; use crate::completion::completion_item::CompletionKind; use crate::{ @@ -22,12 +22,12 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { }; if !ctx.is_call { - complete_fields(acc, ctx, receiver_ty.clone()); + complete_fields(acc, ctx, &receiver_ty); } - complete_methods(acc, ctx, receiver_ty.clone()); + complete_methods(acc, ctx, &receiver_ty); // Suggest .await syntax for types that implement Future trait - if ctx.analyzer.impls_future(ctx.db, receiver_ty) { + if ctx.analyzer.impls_future(ctx.db, receiver_ty.into_ty()) { CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await") .detail("expr.await") .insert_text("await") @@ -35,28 +35,18 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { } } -fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { - for receiver in ctx.analyzer.autoderef(ctx.db, receiver) { - if let Ty::Apply(a_ty) = receiver { - match a_ty.ctor { - TypeCtor::Adt(Adt::Struct(s)) => { - for field in s.fields(ctx.db) { - acc.add_field(ctx, field, &a_ty.parameters); - } - } - // FIXME unions - TypeCtor::Tuple { .. } => { - for (i, ty) in a_ty.parameters.iter().enumerate() { - acc.add_tuple_field(ctx, i, ty); - } - } - _ => {} - } - }; +fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { + for receiver in receiver.autoderef(ctx.db) { + for (field, ty) in receiver.fields(ctx.db) { + acc.add_field(ctx, field, &ty); + } + for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { + acc.add_tuple_field(ctx, i, &ty); + } } } -fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { +fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { let mut seen_methods = FxHashSet::default(); ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, func| { if func.has_self_param(ctx.db) && seen_methods.insert(func.name(ctx.db)) { diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index 17b75cf7e..646a30c76 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs @@ -1,6 +1,5 @@ //! FIXME: write short doc here -use hir::{Ty, TypeCtor}; use ra_syntax::{ast::AstNode, TextRange, TextUnit}; use ra_text_edit::TextEdit; @@ -30,9 +29,12 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { dot_receiver.syntax().text().to_string() }; - let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver); + let receiver_ty = match ctx.analyzer.type_of(ctx.db, &dot_receiver) { + Some(it) => it, + None => return, + }; - if is_bool_or_unknown(receiver_ty) { + if receiver_ty.is_bool() || receiver_ty.is_unknown() { postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text)) .add_to(acc); postfix_snippet( @@ -75,14 +77,6 @@ fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: .snippet_edit(edit) } -fn is_bool_or_unknown(ty: Option) -> bool { - match &ty { - Some(Ty::Apply(app)) if app.ctor == TypeCtor::Bool => true, - Some(Ty::Unknown) | None => true, - Some(_) => false, - } -} - #[cfg(test)] mod tests { use insta::assert_debug_snapshot; diff --git a/crates/ra_ide_api/src/completion/complete_record_literal.rs b/crates/ra_ide_api/src/completion/complete_record_literal.rs index 45a4a9738..577c394d2 100644 --- a/crates/ra_ide_api/src/completion/complete_record_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_record_literal.rs @@ -1,7 +1,5 @@ //! FIXME: write short doc here -use hir::Substs; - use crate::completion::{CompletionContext, Completions}; /// Complete fields in fields literals. @@ -15,10 +13,9 @@ pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionCon Some(it) => it, _ => return, }; - let substs = &ty.substs().unwrap_or_else(Substs::empty); - for field in variant.fields(ctx.db) { - acc.add_field(ctx, field, substs); + for (field, field_ty) in ty.variant_fields(ctx.db, variant) { + acc.add_field(ctx, field, &field_ty); } } diff --git a/crates/ra_ide_api/src/completion/complete_record_pattern.rs b/crates/ra_ide_api/src/completion/complete_record_pattern.rs index aa0fd6d24..a56c7e3a1 100644 --- a/crates/ra_ide_api/src/completion/complete_record_pattern.rs +++ b/crates/ra_ide_api/src/completion/complete_record_pattern.rs @@ -1,7 +1,5 @@ //! FIXME: write short doc here -use hir::Substs; - use crate::completion::{CompletionContext, Completions}; pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionContext) { @@ -14,10 +12,9 @@ pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionCon Some(it) => it, _ => return, }; - let substs = &ty.substs().unwrap_or_else(Substs::empty); - for field in variant.fields(ctx.db) { - acc.add_field(ctx, field, substs); + for (field, field_ty) in ty.variant_fields(ctx.db, variant) { + acc.add_field(ctx, field, &field_ty); } } diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 85b053a6e..5f056730a 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -1,12 +1,12 @@ //! This modules takes care of rendering various definitions as completion items. -use hir::{db::HirDatabase, Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; +use hir::{db::HirDatabase, Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, Type}; use join_to_string::join; use ra_syntax::ast::NameOwner; use test_utils::tested_by; use crate::completion::{ - db, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, + CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, }; use crate::display::{const_label, function_label, macro_label, type_label}; @@ -16,7 +16,7 @@ impl Completions { &mut self, ctx: &CompletionContext, field: hir::StructField, - substs: &hir::Substs, + ty: &Type, ) { let is_deprecated = is_deprecated(field, ctx.db); CompletionItem::new( @@ -25,13 +25,13 @@ impl Completions { field.name(ctx.db).to_string(), ) .kind(CompletionItemKind::Field) - .detail(field.ty(ctx.db).subst(substs).display(ctx.db).to_string()) + .detail(ty.display(ctx.db).to_string()) .set_documentation(field.docs(ctx.db)) .set_deprecated(is_deprecated) .add_to(self); } - pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &hir::Ty) { + pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) { CompletionItem::new(CompletionKind::Reference, ctx.source_range(), field.to_string()) .kind(CompletionItemKind::Field) .detail(ty.display(ctx.db).to_string()) @@ -98,7 +98,7 @@ impl Completions { CompletionItem::new(completion_kind, ctx.source_range(), local_name.clone()); if let ScopeDef::Local(local) = resolution { let ty = local.ty(ctx.db); - if ty != Ty::Unknown { + if !ty.is_unknown() { completion_item = completion_item.detail(ty.display(ctx.db).to_string()); } }; @@ -108,19 +108,17 @@ impl Completions { && !ctx.has_type_args && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") { - let generic_def: Option = match resolution { - ScopeDef::ModuleDef(Adt(it)) => Some((*it).into()), - ScopeDef::ModuleDef(TypeAlias(it)) => Some((*it).into()), - _ => None, + let has_non_default_type_params = match resolution { + ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(ctx.db), + ScopeDef::ModuleDef(TypeAlias(it)) => it.has_non_default_type_params(ctx.db), + _ => false, }; - if let Some(def) = generic_def { - if has_non_default_type_params(def, ctx.db) { - tested_by!(inserts_angle_brackets_for_generics); - completion_item = completion_item - .lookup_by(local_name.clone()) - .label(format!("{}<…>", local_name)) - .insert_snippet(format!("{}<$0>", local_name)); - } + if has_non_default_type_params { + tested_by!(inserts_angle_brackets_for_generics); + completion_item = completion_item + .lookup_by(local_name.clone()) + .label(format!("{}<…>", local_name)) + .insert_snippet(format!("{}<$0>", local_name)); } } @@ -291,11 +289,6 @@ fn is_deprecated(node: impl HasAttrs, db: &impl HirDatabase) -> bool { node.attrs(db).by_key("deprecated").exists() } -fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool { - let subst = db.generic_defaults(def.into()); - subst.iter().any(|ty| ty == &Ty::Unknown) -} - #[cfg(test)] mod tests { use insta::assert_debug_snapshot; diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs index 28a83a3e2..992a08809 100644 --- a/crates/ra_ide_api/src/goto_type_definition.rs +++ b/crates/ra_ide_api/src/goto_type_definition.rs @@ -24,7 +24,7 @@ pub(crate) fn goto_type_definition( let analyzer = hir::SourceAnalyzer::new(db, token.with_value(&node), None); - let ty: hir::Ty = if let Some(ty) = + let ty: hir::Type = if let Some(ty) = ast::Expr::cast(node.clone()).and_then(|e| analyzer.type_of(db, &e)) { ty @@ -35,7 +35,7 @@ pub(crate) fn goto_type_definition( return None; }; - let adt_def = analyzer.autoderef(db, ty).find_map(|ty| ty.as_adt().map(|adt| adt.0))?; + let adt_def = ty.autoderef(db).find_map(|ty| ty.as_adt())?; let nav = adt_def.to_nav(db); Some(RangeInfo::new(node.text_range(), vec![nav])) diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 24a7ca5e7..45149bf0c 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use crate::{db::RootDatabase, FileId}; -use hir::{HirDisplay, SourceAnalyzer, Ty}; +use hir::{HirDisplay, SourceAnalyzer}; use ra_syntax::{ ast::{self, AstNode, TypeAscriptionOwner}, match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange, @@ -100,8 +100,11 @@ fn get_pat_type_hints( .into_iter() .filter(|pat| !skip_root_pat_hint || pat != original_pat) .filter_map(|pat| { - get_node_displayable_type(db, &analyzer, &pat) - .map(|pat_type| (pat.syntax().text_range(), pat_type)) + let ty = analyzer.type_of_pat(db, &pat)?; + if ty.is_unknown() { + return None; + } + Some((pat.syntax().text_range(), ty)) }) .map(|(range, pat_type)| InlayHint { range, @@ -158,20 +161,6 @@ fn get_leaf_pats(root_pat: ast::Pat) -> Vec { leaf_pats } -fn get_node_displayable_type( - db: &RootDatabase, - analyzer: &SourceAnalyzer, - node_pat: &ast::Pat, -) -> Option { - analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| { - if let Ty::Apply(_) = resolved_type { - Some(resolved_type) - } else { - None - } - }) -} - #[cfg(test)] mod tests { use crate::mock_analysis::single_file; diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 2b653fe8f..10165a9bb 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -2,7 +2,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; -use hir::{Mutability, Name, Source}; +use hir::{Name, Source}; use ra_db::SourceDatabase; use ra_prof::profile; use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; @@ -230,11 +230,10 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { Local(local) => { if local.is_mut(db) { "variable.mut" + } else if local.ty(db).is_mutable_reference() { + "variable.mut" } else { - match local.ty(db).as_reference() { - Some((_, Mutability::Mut)) => "variable.mut", - _ => "variable", - } + "variable" } } } -- cgit v1.2.3