Skip to content

Commit 546f8bc

Browse files
committed
completion relevance consider if types can be unified
1 parent 4b997b8 commit 546f8bc

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

crates/hir/src/lib.rs

Lines changed: 5 additions & 1 deletion
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,
@@ -2121,6 +2121,10 @@ impl Type {
21212121

21222122
walk_type(db, self, &mut cb);
21232123
}
2124+
2125+
pub fn could_unify_with(&self, other: &Type) -> bool {
2126+
could_unify(&self.ty, &other.ty)
2127+
}
21242128
}
21252129

21262130
// FIXME: closures

crates/hir_ty/src/infer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ use crate::{
4545
to_assoc_type_id, to_chalk_trait_id, AliasEq, AliasTy, Interner, TyKind,
4646
};
4747

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

5055
mod unify;

crates/hir_ty/src/infer/unify.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ impl<T> Canonicalized<T> {
157157
}
158158
}
159159

160+
pub fn could_unify(t1: &Ty, t2: &Ty) -> bool {
161+
InferenceTable::new().unify(t1, t2)
162+
}
163+
160164
pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> {
161165
let mut table = InferenceTable::new();
162166
let vars = Substitution(

crates/hir_ty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
};
4242

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

crates/ide_completion/src/render.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ fn compute_exact_type_match(ctx: &CompletionContext, completion_ty: &hir::Type)
312312
Some(expected_type) => {
313313
// We don't ever consider unit type to be an exact type match, since
314314
// nearly always this is not meaningful to the user.
315-
completion_ty == expected_type && !expected_type.is_unit()
315+
(completion_ty == expected_type || expected_type.could_unify_with(completion_ty))
316+
&& !expected_type.is_unit()
316317
}
317318
None => false,
318319
}
@@ -1313,4 +1314,34 @@ 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: Foo<u32> = Foo::B;
1333+
let _: Foo<u32> = f$0;
1334+
}
1335+
"#,
1336+
expect![[r#"
1337+
ev Foo::A(…) [type]
1338+
ev Foo::B [type]
1339+
lc foo [type+local]
1340+
en Foo []
1341+
fn baz() []
1342+
fn bar() []
1343+
fn foo() []
1344+
"#]],
1345+
);
1346+
}
13161347
}

0 commit comments

Comments
 (0)