Skip to content

Commit 8762b79

Browse files
committed
do not use associated types placeholder for inlay hint
Signed-off-by: Benjamin Coenen <[email protected]>
1 parent 73161cc commit 8762b79

File tree

2 files changed

+102
-31
lines changed

2 files changed

+102
-31
lines changed

crates/hir_ty/src/display.rs

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ pub trait HirDisplay {
7474
curr_size: 0,
7575
max_size: None,
7676
omit_verbose_types: false,
77+
#[cfg(not(test))]
7778
display_target: DisplayTarget::SourceCode { module_id },
79+
#[cfg(test)]
80+
display_target: DisplayTarget::Test { module_id },
7881
}) {
7982
Ok(()) => {}
8083
Err(HirDisplayError::FmtError) => panic!("Writing to String can't fail!"),
@@ -134,12 +137,25 @@ enum DisplayTarget {
134137
/// Display types for inserting them in source files.
135138
/// The generated code should compile, so paths need to be qualified.
136139
SourceCode { module_id: ModuleId },
140+
/// Only for test purpose to keep real types
141+
#[cfg(test)]
142+
Test { module_id: ModuleId },
137143
}
138144

139145
impl DisplayTarget {
140146
fn is_source_code(&self) -> bool {
141147
matches!(self, Self::SourceCode {..})
142148
}
149+
fn is_test(&self) -> bool {
150+
#[cfg(test)]
151+
{
152+
matches!(self, Self::Test {..})
153+
}
154+
#[cfg(not(test))]
155+
{
156+
false
157+
}
158+
}
143159
}
144160

145161
#[derive(Debug)]
@@ -341,41 +357,57 @@ impl HirDisplay for ApplicationTy {
341357
));
342358
}
343359
}
360+
#[cfg(test)]
361+
DisplayTarget::Test { module_id } => {
362+
if let Some(path) = find_path::find_path(
363+
f.db.upcast(),
364+
ItemInNs::Types(def_id.into()),
365+
module_id,
366+
) {
367+
write!(f, "{}", path)?;
368+
} else {
369+
return Err(HirDisplayError::DisplaySourceCodeError(
370+
DisplaySourceCodeError::PathNotFound,
371+
));
372+
}
373+
}
344374
}
345375

346376
if self.parameters.len() > 0 {
347-
let parameters_to_write =
348-
if f.display_target.is_source_code() || f.omit_verbose_types() {
349-
match self
350-
.ctor
351-
.as_generic_def()
352-
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
353-
.filter(|defaults| !defaults.is_empty())
354-
{
355-
None => self.parameters.0.as_ref(),
356-
Some(default_parameters) => {
357-
let mut default_from = 0;
358-
for (i, parameter) in self.parameters.iter().enumerate() {
359-
match (parameter, default_parameters.get(i)) {
360-
(&Ty::Unknown, _) | (_, None) => {
377+
let parameters_to_write = if f.display_target.is_source_code()
378+
|| f.display_target.is_test()
379+
|| f.omit_verbose_types()
380+
{
381+
match self
382+
.ctor
383+
.as_generic_def()
384+
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
385+
.filter(|defaults| !defaults.is_empty())
386+
{
387+
None => self.parameters.0.as_ref(),
388+
Some(default_parameters) => {
389+
let mut default_from = 0;
390+
for (i, parameter) in self.parameters.iter().enumerate() {
391+
match (parameter, default_parameters.get(i)) {
392+
(&Ty::Unknown, _) | (_, None) => {
393+
default_from = i + 1;
394+
}
395+
(_, Some(default_parameter)) => {
396+
let actual_default = default_parameter
397+
.clone()
398+
.subst(&self.parameters.prefix(i));
399+
if parameter != &actual_default {
361400
default_from = i + 1;
362401
}
363-
(_, Some(default_parameter)) => {
364-
let actual_default = default_parameter
365-
.clone()
366-
.subst(&self.parameters.prefix(i));
367-
if parameter != &actual_default {
368-
default_from = i + 1;
369-
}
370-
}
371402
}
372403
}
373-
&self.parameters.0[0..default_from]
374404
}
405+
&self.parameters.0[0..default_from]
375406
}
376-
} else {
377-
self.parameters.0.as_ref()
378-
};
407+
}
408+
} else {
409+
self.parameters.0.as_ref()
410+
};
379411
if !parameters_to_write.is_empty() {
380412
write!(f, "<")?;
381413
f.write_joined(parameters_to_write, ", ")?;
@@ -391,8 +423,8 @@ impl HirDisplay for ApplicationTy {
391423
let trait_ = f.db.trait_data(trait_);
392424
let type_alias = f.db.type_alias_data(type_alias);
393425

394-
// Use placeholder associated types when the target is source code (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
395-
if f.display_target.is_source_code() || self.parameters.len() > 1 {
426+
// Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
427+
if f.display_target.is_test() || self.parameters.len() > 1 {
396428
write!(f, "{}::{}", trait_.name, type_alias.name)?;
397429
if self.parameters.len() > 0 {
398430
write!(f, "<")?;

crates/hir_ty/src/tests/traits.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use expect_test::expect;
22
use test_utils::mark;
33

4-
use super::{check_infer, check_infer_with_mismatches, check_types};
4+
use super::{check_infer, check_infer_with_mismatches, check_types, check_types_source_code};
55

66
#[test]
77
fn infer_await() {
@@ -907,7 +907,7 @@ fn test<T: Trait>(t: T) { (*t); }
907907
}
908908

909909
#[test]
910-
fn associated_type_placeholder() {
910+
fn associated_type_inlay_hints() {
911911
check_types(
912912
r#"
913913
pub trait ApplyL {
@@ -929,7 +929,7 @@ fn test<T: ApplyL>() {
929929
}
930930

931931
#[test]
932-
fn associated_type_placeholder_2() {
932+
fn associated_type_inlay_hints_2() {
933933
check_types(
934934
r#"
935935
pub trait ApplyL {
@@ -945,6 +945,45 @@ fn test<T: ApplyL>(t: T) {
945945
);
946946
}
947947

948+
#[test]
949+
fn associated_type_placeholder() {
950+
check_types_source_code(
951+
r#"
952+
pub trait ApplyL {
953+
type Out;
954+
}
955+
956+
pub struct RefMutL<T>;
957+
958+
impl<T> ApplyL for RefMutL<T> {
959+
type Out = <T as ApplyL>::Out;
960+
}
961+
962+
fn test<T: ApplyL>() {
963+
let y: <RefMutL<T> as ApplyL>::Out = no_matter;
964+
y;
965+
} //^ ApplyL::Out<T>
966+
"#,
967+
);
968+
}
969+
970+
#[test]
971+
fn associated_type_placeholder_2() {
972+
check_types_source_code(
973+
r#"
974+
pub trait ApplyL {
975+
type Out;
976+
}
977+
fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
978+
979+
fn test<T: ApplyL>(t: T) {
980+
let y = foo(t);
981+
y;
982+
} //^ ApplyL::Out<T>
983+
"#,
984+
);
985+
}
986+
948987
#[test]
949988
fn argument_impl_trait() {
950989
check_infer_with_mismatches(

0 commit comments

Comments
 (0)