Make impl HirDisplay for TraitRef provide just the trait type

This commit is contained in:
Michael Sloan 2024-12-30 13:33:12 -07:00
parent 34dc94bb2d
commit 03cb63cc75
4 changed files with 24 additions and 68 deletions

View file

@ -474,7 +474,9 @@ impl HirDisplay for ProjectionTy {
let trait_ref = self.trait_ref(f.db);
write!(f, "<")?;
fmt_trait_ref(f, &trait_ref, TraitRefFormat::SelfAsTrait)?;
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, " as ")?;
trait_ref.hir_fmt(f)?;
write!(
f,
">::{}",
@ -1775,50 +1777,14 @@ fn write_bounds_like_dyn_trait(
Ok(())
}
#[derive(Clone, Copy)]
pub enum TraitRefFormat {
SelfAsTrait,
SelfImplementsTrait,
OnlyTrait,
}
fn fmt_trait_ref(
f: &mut HirFormatter<'_>,
tr: &TraitRef,
format: TraitRefFormat,
) -> Result<(), HirDisplayError> {
if f.should_truncate() {
return write!(f, "{TYPE_HINT_TRUNCATION}");
}
match format {
TraitRefFormat::SelfAsTrait => {
tr.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, " as ")?;
}
TraitRefFormat::SelfImplementsTrait => {
tr.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, ": ")?;
}
TraitRefFormat::OnlyTrait => {}
}
let trait_ = tr.hir_trait_id();
f.start_location_link(trait_.into());
write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast(), f.edition()))?;
f.end_location_link();
let substs = tr.substitution.as_slice(Interner);
hir_fmt_generics(f, &substs[1..], None, substs[0].ty(Interner))
}
pub struct TraitRefDisplayWrapper {
pub trait_ref: TraitRef,
pub format: TraitRefFormat,
}
impl HirDisplay for TraitRefDisplayWrapper {
impl HirDisplay for TraitRef {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
fmt_trait_ref(f, &self.trait_ref, self.format)
let trait_ = self.hir_trait_id();
f.start_location_link(trait_.into());
write!(f, "{}", f.db.trait_data(trait_).name.display(f.db.upcast(), f.edition()))?;
f.end_location_link();
let substs = self.substitution.as_slice(Interner);
hir_fmt_generics(f, &substs[1..], None, substs[0].ty(Interner))
}
}
@ -1830,11 +1796,16 @@ impl HirDisplay for WhereClause {
match self {
WhereClause::Implemented(trait_ref) => {
fmt_trait_ref(f, trait_ref, TraitRefFormat::SelfImplementsTrait)?;
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, ": ")?;
trait_ref.hir_fmt(f)?;
}
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
write!(f, "<")?;
fmt_trait_ref(f, &projection_ty.trait_ref(f.db), TraitRefFormat::SelfAsTrait)?;
let trait_ref = &projection_ty.trait_ref(f.db);
trait_ref.self_type_parameter(Interner).hir_fmt(f)?;
write!(f, " as ")?;
trait_ref.hir_fmt(f)?;
write!(f, ">::",)?;
let type_alias = from_assoc_type_id(projection_ty.associated_ty_id);
f.start_location_link(type_alias.into());

View file

@ -743,18 +743,9 @@ impl HirDisplay for Static {
}
}
pub struct TraitRefDisplayWrapper {
pub trait_ref: TraitRef,
pub format: hir_ty::display::TraitRefFormat,
}
impl HirDisplay for TraitRefDisplayWrapper {
impl HirDisplay for TraitRef {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
hir_ty::display::TraitRefDisplayWrapper {
format: self.format,
trait_ref: self.trait_ref.trait_ref.clone(),
}
.hir_fmt(f)
self.trait_ref.hir_fmt(f)
}
}

View file

@ -96,7 +96,6 @@ use crate::db::{DefDatabase, HirDatabase};
pub use crate::{
attrs::{resolve_doc_path_on, HasAttrs},
diagnostics::*,
display::TraitRefDisplayWrapper,
has_source::HasSource,
semantics::{
PathResolution, Semantics, SemanticsImpl, SemanticsScope, TypeInfo, VisibleTraits,
@ -149,7 +148,7 @@ pub use {
hir_ty::{
consteval::ConstEvalError,
diagnostics::UnsafetyReason,
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite, TraitRefFormat},
display::{ClosureStyle, HirDisplay, HirDisplayError, HirWrite},
dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode},
layout::LayoutError,
mir::{MirEvalError, MirLowerError},

View file

@ -3,10 +3,7 @@
use core::fmt;
use hir::{
Adt, AsAssocItem, Crate, HirDisplay, MacroKind, Semantics, TraitRefDisplayWrapper,
TraitRefFormat,
};
use hir::{Adt, AsAssocItem, Crate, HirDisplay, MacroKind, Semantics};
use ide_db::{
base_db::{CrateOrigin, LangCrateOrigin},
defs::{Definition, IdentClass},
@ -312,15 +309,13 @@ fn def_to_non_local_moniker(
match def {
Definition::SelfType(impl_) => {
if let Some(trait_ref) = impl_.trait_ref(db) {
// Trait impls use `trait_type` constraint syntax for the 2nd parameter.
let trait_ref_for_display =
TraitRefDisplayWrapper { trait_ref, format: TraitRefFormat::OnlyTrait };
// Trait impls use the trait type for the 2nd parameter.
reverse_description.push(MonikerDescriptor {
name: display(db, edition, module, trait_ref_for_display),
name: display(db, edition, module, trait_ref),
desc: MonikerDescriptorKind::TypeParameter,
});
}
// Both inherent and trait impls use `self_type` as the first parameter.
// Both inherent and trait impls use the self type for the first parameter.
reverse_description.push(MonikerDescriptor {
name: display(db, edition, module, impl_.self_ty(db)),
desc: MonikerDescriptorKind::TypeParameter,