This commit is contained in:
Andrzej Głuszak 2021-10-20 21:35:35 +02:00
parent 98676efdc5
commit a2242dcf1b
3 changed files with 17 additions and 29 deletions

View file

@ -1,6 +1,7 @@
use rustc_hash::{FxHashMap, FxHashSet};
use hir::{HasSource, HirDisplay, Module, ModuleDef, Semantics, TypeInfo};
use ide_db::helpers::FamousDefs;
use ide_db::{
base_db::FileId,
defs::{Definition, NameRefClass},
@ -512,13 +513,10 @@ fn fn_arg_type(ctx: &AssistContext, target_module: hir::Module, fn_arg: &ast::Ex
}
if ty.is_reference() || ty.is_mutable_reference() {
convert_reference_type(
ty.strip_references(),
ctx,
ctx.sema.scope(fn_arg.syntax()).krate(),
)
.map(|conversion| conversion.convert_type(ctx.db()))
.or_else(|| ty.display_source_code(ctx.db(), target_module.into()).ok())
let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(fn_arg.syntax()).krate());
convert_reference_type(ty.strip_references(), ctx.db(), famous_defs)
.map(|conversion| conversion.convert_type(ctx.db()))
.or_else(|| ty.display_source_code(ctx.db(), target_module.into()).ok())
} else {
ty.display_source_code(ctx.db(), target_module.into()).ok()
}

View file

@ -1,3 +1,4 @@
use ide_db::helpers::FamousDefs;
use stdx::{format_to, to_lower_snake_case};
use syntax::ast::{self, AstNode, HasName, HasVisibility};
@ -112,16 +113,12 @@ pub(crate) fn generate_getter_impl(
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
let (ty, body) = if mutable {
(
format!("&mut {}", field_ty.to_string()),
format!("&mut self.{}", field_name.to_string()),
)
(format!("&mut {}", field_ty), format!("&mut self.{}", field_name))
} else {
let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(field_ty.syntax()).krate());
ctx.sema
.resolve_type(&field_ty)
.and_then(|ty| {
convert_reference_type(ty, ctx, ctx.sema.scope(field_ty.syntax()).krate())
})
.and_then(|ty| convert_reference_type(ty, ctx.db(), famous_defs))
.map(|conversion| {
cov_mark::hit!(convert_reference_type);
(

View file

@ -5,10 +5,10 @@ use std::ops;
use itertools::Itertools;
pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
use hir::db::HirDatabase;
use hir::{Crate, HasSource, HirDisplay};
use ide_db::helpers::FamousDefs;
use ide_db::{helpers::SnippetCap, path_transform::PathTransform, RootDatabase};
use hir::{db::HirDatabase, HasSource, HirDisplay};
use ide_db::{
helpers::FamousDefs, helpers::SnippetCap, path_transform::PathTransform, RootDatabase,
};
use stdx::format_to;
use syntax::{
ast::{
@ -577,19 +577,13 @@ impl ReferenceConversion {
// FIXME: It should return a new hir::Type, but currently constructing new types is too cumbersome
// and all users of this function operate on string type names, so they can do the conversion
// itself themselves.
// Another problem is that it probably shouldn't take AssistContext as a parameter, as
// it should be usable not only in assists.
pub(crate) fn convert_reference_type(
ty: hir::Type,
ctx: &AssistContext,
krate: Option<Crate>,
db: &RootDatabase,
famous_defs: &FamousDefs,
) -> Option<ReferenceConversion> {
let sema = &ctx.sema;
let db = sema.db;
let famous_defs = &FamousDefs(sema, krate);
handle_copy(&ty, db)
.or_else(|| handle_as_ref_str(&ty, db, famous_defs, ctx))
.or_else(|| handle_as_ref_str(&ty, db, famous_defs))
.or_else(|| handle_as_ref_slice(&ty, db, famous_defs))
.or_else(|| handle_dereferenced(&ty, db, famous_defs))
.or_else(|| handle_option_as_ref(&ty, db, famous_defs))
@ -605,9 +599,8 @@ fn handle_as_ref_str(
ty: &hir::Type,
db: &dyn HirDatabase,
famous_defs: &FamousDefs,
ctx: &AssistContext,
) -> Option<ReferenceConversionType> {
let module = ctx.sema.to_module_def(ctx.file_id())?;
let module = famous_defs.1?.root_module(db);
let str_type = hir::BuiltinType::str().ty(db, module);
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type])