Skip to content

Commit

Permalink
Make impl HirDisplay for TraitRef provide just the trait type
Browse files Browse the repository at this point in the history
  • Loading branch information
mgsloan committed Dec 30, 2024
1 parent 517d52d commit b7cdd64
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 68 deletions.
63 changes: 17 additions & 46 deletions src/tools/rust-analyzer/crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
">::{}",
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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());
Expand Down
13 changes: 2 additions & 11 deletions src/tools/rust-analyzer/crates/hir/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/tools/rust-analyzer/crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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},
Expand Down
13 changes: 4 additions & 9 deletions src/tools/rust-analyzer/crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b7cdd64

Please sign in to comment.