Skip to content

completion relevance consider if types can be unified #8056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use hir_def::{
};
use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind};
use hir_ty::{
autoderef,
autoderef, could_unify,
method_resolution::{self, TyFingerprint},
primitive::UintTy,
to_assoc_type_id,
Expand Down Expand Up @@ -2154,6 +2154,10 @@ impl Type {

walk_type(db, self, &mut cb);
}

pub fn could_unify_with(&self, other: &Type) -> bool {
could_unify(&self.ty, &other.ty)
}
}

// FIXME: closures
Expand Down
5 changes: 5 additions & 0 deletions crates/hir_ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ use crate::{
to_assoc_type_id, to_chalk_trait_id, AliasEq, AliasTy, Interner, TyKind,
};

// This lint has a false positive here. See the link below for details.
//
// https://github.com/rust-lang/rust/issues/57411
#[allow(unreachable_pub)]
pub use unify::could_unify;
pub(crate) use unify::unify;

mod unify;
Expand Down
4 changes: 4 additions & 0 deletions crates/hir_ty/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ impl<T> Canonicalized<T> {
}
}

pub fn could_unify(t1: &Ty, t2: &Ty) -> bool {
InferenceTable::new().unify(t1, t2)
}

pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> {
let mut table = InferenceTable::new();
let vars = Substitution(
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
};

pub use autoderef::autoderef;
pub use infer::{InferenceResult, InferenceVar};
pub use infer::{could_unify, InferenceResult, InferenceVar};
pub use lower::{
associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
TyDefId, TyLoweringContext, ValueTyDefId,
Expand Down
59 changes: 39 additions & 20 deletions crates/ide_completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl fmt::Debug for CompletionItem {
}
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Default)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These weren't used, because CompletionRelevance should only be compared by score. I could re-implement them in terms of score, but that would require updating the Eq impl which we use in a different way, so it would broaden the scope of this PR a bit.

The reason I removed these at all in this PR is because it didn't make sense to me to derive them on the new enum CompletionRelevanceTypeMatch.

#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct CompletionRelevance {
/// This is set in cases like these:
///
Expand All @@ -134,31 +134,41 @@ pub struct CompletionRelevance {
/// }
/// ```
pub exact_name_match: bool,
/// See CompletionRelevanceTypeMatch doc comments for cases where this is set.
pub type_match: Option<CompletionRelevanceTypeMatch>,
/// This is set in cases like these:
///
/// ```
/// fn f(spam: String) {}
/// fn main {
/// let foo = String::new();
/// f($0) // type of local matches the type of param
/// fn foo(a: u32) {
/// let b = 0;
/// $0 // `a` and `b` are local
/// }
/// ```
pub exact_type_match: bool,
pub is_local: bool,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CompletionRelevanceTypeMatch {
/// This is set in cases like these:
///
/// ```
/// fn foo(bar: u32) {
/// $0 // `bar` is local
/// enum Option<T> { Some(T), None }
/// fn f(a: Option<u32>) {}
/// fn main {
/// f(Option::N$0) // type `Option<T>` could unify with `Option<u32>`
/// }
/// ```
CouldUnify,
/// This is set in cases like these:
///
/// ```
/// fn foo() {
/// let bar = 0;
/// $0 // `bar` is local
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to this PR, but combining these two examples has been discussed as something we'd like to do.

/// fn f(spam: String) {}
/// fn main {
/// let foo = String::new();
/// f($0) // type of local matches the type of param
/// }
/// ```
pub is_local: bool,
Exact,
}

impl CompletionRelevance {
Expand All @@ -177,9 +187,11 @@ impl CompletionRelevance {
if self.exact_name_match {
score += 1;
}
if self.exact_type_match {
score += 3;
}
score += match self.type_match {
Some(CompletionRelevanceTypeMatch::Exact) => 4,
Some(CompletionRelevanceTypeMatch::CouldUnify) => 3,
None => 0,
};
if self.is_local {
score += 1;
}
Expand Down Expand Up @@ -342,7 +354,7 @@ impl CompletionItem {
// match, but with exact type match set because self.ref_match
// is only set if there is an exact type match.
let mut relevance = self.relevance;
relevance.exact_type_match = true;
relevance.type_match = Some(CompletionRelevanceTypeMatch::Exact);

self.ref_match.map(|mutability| (mutability, relevance))
}
Expand Down Expand Up @@ -523,7 +535,7 @@ mod tests {
use itertools::Itertools;
use test_utils::assert_eq_text;

use super::CompletionRelevance;
use super::{CompletionRelevance, CompletionRelevanceTypeMatch};

/// Check that these are CompletionRelevance are sorted in ascending order
/// by their relevance score.
Expand Down Expand Up @@ -576,15 +588,22 @@ mod tests {
is_local: true,
..CompletionRelevance::default()
}],
vec![CompletionRelevance { exact_type_match: true, ..CompletionRelevance::default() }],
vec![CompletionRelevance {
type_match: Some(CompletionRelevanceTypeMatch::CouldUnify),
..CompletionRelevance::default()
}],
vec![CompletionRelevance {
type_match: Some(CompletionRelevanceTypeMatch::Exact),
..CompletionRelevance::default()
}],
vec![CompletionRelevance {
exact_name_match: true,
exact_type_match: true,
type_match: Some(CompletionRelevanceTypeMatch::Exact),
..CompletionRelevance::default()
}],
vec![CompletionRelevance {
exact_name_match: true,
exact_type_match: true,
type_match: Some(CompletionRelevanceTypeMatch::Exact),
is_local: true,
}],
];
Expand Down
81 changes: 65 additions & 16 deletions crates/ide_completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use ide_db::{
use syntax::TextRange;

use crate::{
item::ImportEdit, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
CompletionRelevance,
item::{CompletionRelevanceTypeMatch, ImportEdit},
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance,
};

use crate::render::{enum_variant::render_variant, function::render_fn, macro_::render_macro};
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<'a> Render<'a> {
.set_deprecated(is_deprecated);

item.set_relevance(CompletionRelevance {
exact_type_match: compute_exact_type_match(self.ctx.completion, ty),
type_match: compute_type_match(self.ctx.completion, ty),
exact_name_match: compute_exact_name_match(self.ctx.completion, name.to_string()),
..CompletionRelevance::default()
});
Expand Down Expand Up @@ -245,7 +245,7 @@ impl<'a> Render<'a> {
}

item.set_relevance(CompletionRelevance {
exact_type_match: compute_exact_type_match(self.ctx.completion, &ty),
type_match: compute_type_match(self.ctx.completion, &ty),
exact_name_match: compute_exact_name_match(self.ctx.completion, &local_name),
is_local: true,
..CompletionRelevance::default()
Expand Down Expand Up @@ -309,14 +309,24 @@ impl<'a> Render<'a> {
}
}

fn compute_exact_type_match(ctx: &CompletionContext, completion_ty: &hir::Type) -> bool {
match ctx.expected_type.as_ref() {
Some(expected_type) => {
// We don't ever consider unit type to be an exact type match, since
// nearly always this is not meaningful to the user.
completion_ty == expected_type && !expected_type.is_unit()
}
None => false,
fn compute_type_match(
ctx: &CompletionContext,
completion_ty: &hir::Type,
) -> Option<CompletionRelevanceTypeMatch> {
let expected_type = ctx.expected_type.as_ref()?;

// We don't ever consider unit type to be an exact type match, since
// nearly always this is not meaningful to the user.
if expected_type.is_unit() {
return None;
}

if completion_ty == expected_type {
Some(CompletionRelevanceTypeMatch::Exact)
} else if expected_type.could_unify_with(completion_ty) {
Some(CompletionRelevanceTypeMatch::CouldUnify)
} else {
None
}
}

Expand Down Expand Up @@ -348,6 +358,7 @@ mod tests {
use itertools::Itertools;

use crate::{
item::CompletionRelevanceTypeMatch,
test_utils::{check_edit, do_completion, get_all_items, TEST_CONFIG},
CompletionKind, CompletionRelevance,
};
Expand All @@ -360,7 +371,11 @@ mod tests {
fn check_relevance(ra_fixture: &str, expect: Expect) {
fn display_relevance(relevance: CompletionRelevance) -> String {
let relevance_factors = vec![
(relevance.exact_type_match, "type"),
(relevance.type_match == Some(CompletionRelevanceTypeMatch::Exact), "type"),
(
relevance.type_match == Some(CompletionRelevanceTypeMatch::CouldUnify),
"type_could_unify",
),
(relevance.exact_name_match, "name"),
(relevance.is_local, "local"),
]
Expand Down Expand Up @@ -533,7 +548,9 @@ fn main() { let _: m::Spam = S$0 }
detail: "(i32)",
relevance: CompletionRelevance {
exact_name_match: false,
exact_type_match: true,
type_match: Some(
Exact,
),
is_local: false,
},
trigger_call_info: true,
Expand All @@ -559,7 +576,9 @@ fn main() { let _: m::Spam = S$0 }
detail: "()",
relevance: CompletionRelevance {
exact_name_match: false,
exact_type_match: true,
type_match: Some(
Exact,
),
is_local: false,
},
},
Expand Down Expand Up @@ -1108,7 +1127,7 @@ fn main() {
detail: "S",
relevance: CompletionRelevance {
exact_name_match: true,
exact_type_match: false,
type_match: None,
is_local: true,
},
ref_match: "&mut ",
Expand Down Expand Up @@ -1353,4 +1372,34 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
"#]],
);
}

#[test]
fn generic_enum() {
check_relevance(
r#"
enum Foo<T> { A(T), B }
// bar() should not be an exact type match
// because the generic parameters are different
fn bar() -> Foo<u8> { Foo::B }
// FIXME baz() should be an exact type match
// because the types could unify, but it currently
// is not. This is due to the T here being
// TyKind::Placeholder rather than TyKind::Missing.
fn baz<T>() -> Foo<T> { Foo::B }
fn foo() {
let foo: Foo<u32> = Foo::B;
let _: Foo<u32> = f$0;
}
"#,
expect![[r#"
ev Foo::A(…) [type_could_unify]
ev Foo::B [type_could_unify]
lc foo [type+local]
en Foo []
fn baz() []
fn bar() []
fn foo() []
"#]],
);
}
}
4 changes: 2 additions & 2 deletions crates/ide_completion/src/render/enum_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;

use crate::{
item::{CompletionItem, CompletionKind, ImportEdit},
render::{builder_ext::Params, compute_exact_type_match, compute_ref_match, RenderContext},
render::{builder_ext::Params, compute_ref_match, compute_type_match, RenderContext},
CompletionRelevance,
};

Expand Down Expand Up @@ -77,7 +77,7 @@ impl<'a> EnumRender<'a> {

let ty = self.variant.parent_enum(self.ctx.completion.db).ty(self.ctx.completion.db);
item.set_relevance(CompletionRelevance {
exact_type_match: compute_exact_type_match(self.ctx.completion, &ty),
type_match: compute_type_match(self.ctx.completion, &ty),
..CompletionRelevance::default()
});

Expand Down
4 changes: 2 additions & 2 deletions crates/ide_completion/src/render/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syntax::ast::Fn;
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, ImportEdit},
render::{
builder_ext::Params, compute_exact_name_match, compute_exact_type_match, compute_ref_match,
builder_ext::Params, compute_exact_name_match, compute_ref_match, compute_type_match,
RenderContext,
},
};
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<'a> FunctionRender<'a> {

let ret_type = self.func.ret_type(self.ctx.db());
item.set_relevance(CompletionRelevance {
exact_type_match: compute_exact_type_match(self.ctx.completion, &ret_type),
type_match: compute_type_match(self.ctx.completion, &ret_type),
exact_name_match: compute_exact_name_match(self.ctx.completion, self.name.clone()),
..CompletionRelevance::default()
});
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ mod tests {
(
"&arg",
Some(
"fffffffa",
"fffffff9",
),
),
(
Expand Down