Skip to content

Commit f558e51

Browse files
committed
completion relevance consider if types can be unified
1 parent b4ed3e1 commit f558e51

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

crates/hir/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use hir_def::{
5151
};
5252
use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind};
5353
use hir_ty::{
54-
autoderef,
54+
autoderef, could_unify,
5555
method_resolution::{self, TyFingerprint},
5656
primitive::UintTy,
5757
to_assoc_type_id,
@@ -2100,6 +2100,10 @@ impl Type {
21002100

21012101
walk_type(db, self, &mut cb);
21022102
}
2103+
2104+
pub fn could_unify_with(&self, other: &Type) -> bool {
2105+
could_unify(&self.ty.value, &other.ty.value)
2106+
}
21032107
}
21042108

21052109
// FIXME: closures

crates/hir_ty/src/infer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::{
4545
to_assoc_type_id, AliasTy, Interner, TyKind,
4646
};
4747

48+
pub use unify::could_unify;
4849
pub(crate) use unify::unify;
4950

5051
mod unify;

crates/hir_ty/src/infer/unify.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ impl<T> Canonicalized<T> {
147147
}
148148
}
149149

150+
pub fn could_unify(t1: &Ty, t2: &Ty) -> bool {
151+
InferenceTable::new().unify(t1, t2)
152+
}
153+
150154
pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substs> {
151155
let mut table = InferenceTable::new();
152156
let vars = Substs(

crates/hir_ty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::{
4040
};
4141

4242
pub use autoderef::autoderef;
43-
pub use infer::{InferenceResult, InferenceVar};
43+
pub use infer::{could_unify, InferenceResult, InferenceVar};
4444
pub use lower::{
4545
associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
4646
TyDefId, TyLoweringContext, ValueTyDefId,

crates/ide_completion/src/render.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ fn compute_exact_type_match(ctx: &CompletionContext, completion_ty: &hir::Type)
311311
Some(expected_type) => {
312312
// We don't ever consider unit type to be an exact type match, since
313313
// nearly always this is not meaningful to the user.
314-
completion_ty == expected_type && !expected_type.is_unit()
314+
(completion_ty == expected_type || expected_type.could_unify_with(completion_ty))
315+
&& !expected_type.is_unit()
315316
}
316317
None => false,
317318
}
@@ -1313,4 +1314,32 @@ fn main() {
13131314
"#]],
13141315
)
13151316
}
1317+
1318+
#[test]
1319+
fn generic_enum() {
1320+
check_relevance(
1321+
r#"
1322+
enum Foo<T> { A(T), B }
1323+
// bar() should not be an exact type match
1324+
// because the generic parameters are different
1325+
fn bar() -> Foo<u8> { Foo::B }
1326+
// FIXME baz() should be an exact type match
1327+
// because the types could unify, but it currently
1328+
// is not. This is due to the T here being
1329+
// TyKind::Placeholder rather than TyKind::Missing.
1330+
fn baz<T>() -> Foo<T> { Foo::B }
1331+
fn foo() {
1332+
let _: Foo<u32> = $0;
1333+
}
1334+
"#,
1335+
expect![[r#"
1336+
ev Foo::A(…) [type]
1337+
ev Foo::B [type]
1338+
en Foo []
1339+
fn baz() []
1340+
fn bar() []
1341+
fn foo() []
1342+
"#]],
1343+
);
1344+
}
13161345
}

0 commit comments

Comments
 (0)