Skip to content
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
56 changes: 47 additions & 9 deletions crates/hir_ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ pub trait HirDisplay {
};
Ok(result)
}

/// Returns a String representation of `self` for test purposes
fn display_test<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
where
Self: Sized,
{
HirDisplayWrapper {
db,
t: self,
max_size: None,
omit_verbose_types: false,
display_target: DisplayTarget::Test,
}
}
}

impl<'a> HirFormatter<'a> {
Expand Down Expand Up @@ -134,12 +148,17 @@ enum DisplayTarget {
/// Display types for inserting them in source files.
/// The generated code should compile, so paths need to be qualified.
SourceCode { module_id: ModuleId },
/// Only for test purpose to keep real types
Test,
}

impl DisplayTarget {
fn is_source_code(&self) -> bool {
matches!(self, Self::SourceCode {..})
}
fn is_test(&self) -> bool {
matches!(self, Self::Test)
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -313,14 +332,18 @@ impl HirDisplay for ApplicationTy {
let ret_display = if f.omit_verbose_types() {
ret.display_truncated(f.db, f.max_size)
} else {
ret.display(f.db)
if f.display_target.is_test() {
Copy link
Member

Choose a reason for hiding this comment

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

hmm, these seem like they were already bugs. We should be reusing the whole HirFormatter. Anyway, we can look into that separately.

ret.display_test(f.db)
} else {
ret.display(f.db)
}
};
write!(f, " -> {}", ret_display)?;
}
}
TypeCtor::Adt(def_id) => {
match f.display_target {
DisplayTarget::Diagnostics => {
DisplayTarget::Diagnostics | DisplayTarget::Test => {
let name = match def_id {
AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
Expand Down Expand Up @@ -389,12 +412,23 @@ impl HirDisplay for ApplicationTy {
_ => panic!("not an associated type"),
};
let trait_ = f.db.trait_data(trait_);
let type_alias = f.db.type_alias_data(type_alias);
write!(f, "{}::{}", trait_.name, type_alias.name)?;
if self.parameters.len() > 0 {
write!(f, "<")?;
f.write_joined(&*self.parameters.0, ", ")?;
write!(f, ">")?;
let type_alias_data = f.db.type_alias_data(type_alias);

// Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
if f.display_target.is_test() {
write!(f, "{}::{}", trait_.name, type_alias_data.name)?;
if self.parameters.len() > 0 {
write!(f, "<")?;
f.write_joined(&*self.parameters.0, ", ")?;
write!(f, ">")?;
}
} else {
let projection_ty = ProjectionTy {
associated_ty: type_alias,
parameters: self.parameters.clone(),
};

projection_ty.hir_fmt(f)?;
}
}
TypeCtor::ForeignType(type_alias) => {
Expand Down Expand Up @@ -442,7 +476,11 @@ impl HirDisplay for ApplicationTy {
let ret_display = if f.omit_verbose_types() {
sig.ret().display_truncated(f.db, f.max_size)
} else {
sig.ret().display(f.db)
if f.display_target.is_test() {
sig.ret().display_test(f.db)
} else {
sig.ret().display(f.db)
}
};
write!(f, " -> {}", ret_display)?;
} else {
Expand Down
8 changes: 4 additions & 4 deletions crates/hir_ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn check_types_impl(ra_fixture: &str, display_source: bool) {
let module = db.module_for_file(file_id);
ty.display_source_code(&db, module).unwrap()
} else {
ty.display(&db).to_string()
ty.display_test(&db).to_string()
};
assert_eq!(expected, actual);
checked_one = true;
Expand Down Expand Up @@ -163,7 +163,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
macro_prefix,
range,
ellipsize(text, 15),
ty.display(&db)
ty.display_test(&db)
);
}
if include_mismatches {
Expand All @@ -179,8 +179,8 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
"{}{:?}: expected {}, got {}\n",
macro_prefix,
range,
mismatch.expected.display(&db),
mismatch.actual.display(&db),
mismatch.expected.display_test(&db),
mismatch.actual.display_test(&db),
);
}
}
Expand Down
21 changes: 21 additions & 0 deletions crates/ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,4 +1235,25 @@ fn main() {
"#,
);
}

#[test]
fn infer_call_method_return_associated_types_with_generic() {
check(
r#"
pub trait Default {
fn default() -> Self;
}
pub trait Foo {
type Bar: Default;
}

pub fn quux<T: Foo>() -> T::Bar {
let y = Default::default();
//^ <T as Foo>::Bar

y
}
"#,
);
}
}