Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3ab7a69

Browse files
authored
Merge pull request rust-lang#18436 from Veykril/lw-yvkqwpnwsouo
Do not render meta info when hovering usages
2 parents c21de28 + 8a51cbf commit 3ab7a69

File tree

5 files changed

+478
-191
lines changed

5 files changed

+478
-191
lines changed

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ pub mod term_search;
3434

3535
mod display;
3636

37-
use std::{mem::discriminant, ops::ControlFlow};
37+
use std::{
38+
mem::discriminant,
39+
ops::{ControlFlow, Not},
40+
};
3841

3942
use arrayvec::ArrayVec;
4043
use base_db::{CrateDisplayName, CrateId, CrateOrigin};
@@ -2697,6 +2700,18 @@ impl Trait {
26972700
hir_ty::dyn_compatibility::dyn_compatibility(db, self.id)
26982701
}
26992702

2703+
pub fn dyn_compatibility_all_violations(
2704+
&self,
2705+
db: &dyn HirDatabase,
2706+
) -> Option<Vec<DynCompatibilityViolation>> {
2707+
let mut violations = vec![];
2708+
hir_ty::dyn_compatibility::dyn_compatibility_with_callback(db, self.id, &mut |violation| {
2709+
violations.push(violation);
2710+
ControlFlow::Continue(())
2711+
});
2712+
violations.is_empty().not().then_some(violations)
2713+
}
2714+
27002715
fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> {
27012716
db.trait_data(self.id)
27022717
.macro_calls

src/tools/rust-analyzer/crates/ide/src/hover.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn hover_offset(
158158
if let Some(doc_comment) = token_as_doc_comment(&original_token) {
159159
cov_mark::hit!(no_highlight_on_comment_hover);
160160
return doc_comment.get_definition_with_descend_at(sema, offset, |def, node, range| {
161-
let res = hover_for_definition(sema, file_id, def, &node, None, config, edition);
161+
let res = hover_for_definition(sema, file_id, def, &node, None, false, config, edition);
162162
Some(RangeInfo::new(range, res))
163163
});
164164
}
@@ -172,6 +172,7 @@ fn hover_offset(
172172
Definition::from(resolution?),
173173
&original_token.parent()?,
174174
None,
175+
false,
175176
config,
176177
edition,
177178
);
@@ -218,6 +219,7 @@ fn hover_offset(
218219
break 'a vec![(
219220
Definition::Macro(macro_),
220221
sema.resolve_macro_call_arm(&macro_call),
222+
false,
221223
node,
222224
)];
223225
}
@@ -234,19 +236,34 @@ fn hover_offset(
234236
decl,
235237
..
236238
}) => {
237-
vec![(Definition::ExternCrateDecl(decl), None, node)]
239+
vec![(Definition::ExternCrateDecl(decl), None, false, node)]
238240
}
239241

240242
class => {
241-
multizip((class.definitions(), iter::repeat(None), iter::repeat(node)))
242-
.collect::<Vec<_>>()
243+
let is_def = matches!(class, IdentClass::NameClass(_));
244+
multizip((
245+
class.definitions(),
246+
iter::repeat(None),
247+
iter::repeat(is_def),
248+
iter::repeat(node),
249+
))
250+
.collect::<Vec<_>>()
243251
}
244252
}
245253
}
246254
.into_iter()
247-
.unique_by(|&(def, _, _)| def)
248-
.map(|(def, macro_arm, node)| {
249-
hover_for_definition(sema, file_id, def, &node, macro_arm, config, edition)
255+
.unique_by(|&(def, _, _, _)| def)
256+
.map(|(def, macro_arm, hovered_definition, node)| {
257+
hover_for_definition(
258+
sema,
259+
file_id,
260+
def,
261+
&node,
262+
macro_arm,
263+
hovered_definition,
264+
config,
265+
edition,
266+
)
250267
})
251268
.collect::<Vec<_>>(),
252269
)
@@ -366,6 +383,7 @@ pub(crate) fn hover_for_definition(
366383
def: Definition,
367384
scope_node: &SyntaxNode,
368385
macro_arm: Option<u32>,
386+
hovered_definition: bool,
369387
config: &HoverConfig,
370388
edition: Edition,
371389
) -> HoverResult {
@@ -397,6 +415,7 @@ pub(crate) fn hover_for_definition(
397415
famous_defs.as_ref(),
398416
&notable_traits,
399417
macro_arm,
418+
hovered_definition,
400419
config,
401420
edition,
402421
);

src/tools/rust-analyzer/crates/ide/src/hover/render.rs

Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub(super) fn keyword(
273273
let markup = process_markup(
274274
sema.db,
275275
Definition::Module(doc_owner),
276-
&markup(Some(docs.into()), description, None),
276+
&markup(Some(docs.into()), description, None, None),
277277
config,
278278
);
279279
Some(HoverResult { markup, actions })
@@ -419,6 +419,7 @@ pub(super) fn definition(
419419
famous_defs: Option<&FamousDefs<'_, '_>>,
420420
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
421421
macro_arm: Option<u32>,
422+
hovered_definition: bool,
422423
config: &HoverConfig,
423424
edition: Edition,
424425
) -> Markup {
@@ -456,7 +457,7 @@ pub(super) fn definition(
456457
_ => def.label(db, edition),
457458
};
458459
let docs = def.docs(db, famous_defs, edition);
459-
let value = (|| match def {
460+
let value = || match def {
460461
Definition::Variant(it) => {
461462
if !it.parent_enum(db).is_data_carrying(db) {
462463
match it.eval(db) {
@@ -494,9 +495,9 @@ pub(super) fn definition(
494495
Some(body.to_string())
495496
}
496497
_ => None,
497-
})();
498+
};
498499

499-
let layout_info = match def {
500+
let layout_info = || match def {
500501
Definition::Field(it) => render_memory_layout(
501502
config.memory_layout,
502503
|| it.layout(db),
@@ -529,34 +530,38 @@ pub(super) fn definition(
529530
_ => None,
530531
};
531532

532-
let dyn_compatibility_info = if let Definition::Trait(it) = def {
533-
let mut dyn_compatibility_info = String::new();
534-
render_dyn_compatibility(db, &mut dyn_compatibility_info, it.dyn_compatibility(db));
535-
Some(dyn_compatibility_info)
536-
} else {
537-
None
533+
let dyn_compatibility_info = || match def {
534+
Definition::Trait(it) => {
535+
let mut dyn_compatibility_info = String::new();
536+
render_dyn_compatibility(db, &mut dyn_compatibility_info, it.dyn_compatibility(db));
537+
Some(dyn_compatibility_info)
538+
}
539+
_ => None,
538540
};
539541

540-
let mut desc = String::new();
541-
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits, edition) {
542-
desc.push_str(&notable_traits);
543-
desc.push('\n');
544-
}
545-
if let Some(layout_info) = layout_info {
546-
desc.push_str(&layout_info);
547-
desc.push('\n');
548-
}
549-
if let Some(dyn_compatibility_info) = dyn_compatibility_info {
550-
desc.push_str(&dyn_compatibility_info);
551-
desc.push('\n');
542+
let mut extra = String::new();
543+
if hovered_definition {
544+
if let Some(notable_traits) = render_notable_trait(db, notable_traits, edition) {
545+
extra.push_str("\n___\n");
546+
extra.push_str(&notable_traits);
547+
}
548+
if let Some(layout_info) = layout_info() {
549+
extra.push_str("\n___\n");
550+
extra.push_str(&layout_info);
551+
}
552+
if let Some(dyn_compatibility_info) = dyn_compatibility_info() {
553+
extra.push_str("\n___\n");
554+
extra.push_str(&dyn_compatibility_info);
555+
}
552556
}
557+
let mut desc = String::new();
553558
desc.push_str(&label);
554-
if let Some(value) = value {
559+
if let Some(value) = value() {
555560
desc.push_str(" = ");
556561
desc.push_str(&value);
557562
}
558563

559-
markup(docs.map(Into::into), desc, mod_path)
564+
markup(docs.map(Into::into), desc, extra.is_empty().not().then_some(extra), mod_path)
560565
}
561566

562567
pub(super) fn literal(
@@ -626,7 +631,7 @@ pub(super) fn literal(
626631
Some(s.into())
627632
}
628633

629-
fn render_notable_trait_comment(
634+
fn render_notable_trait(
630635
db: &RootDatabase,
631636
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
632637
edition: Edition,
@@ -635,7 +640,7 @@ fn render_notable_trait_comment(
635640
let mut needs_impl_header = true;
636641
for (trait_, assoc_types) in notable_traits {
637642
desc.push_str(if mem::take(&mut needs_impl_header) {
638-
"// Implements notable traits: "
643+
"Implements notable traits: "
639644
} else {
640645
", "
641646
});
@@ -728,13 +733,12 @@ fn type_info(
728733
)
729734
.into()
730735
} else {
731-
let mut desc =
732-
match render_notable_trait_comment(db, &notable_traits(db, &original), edition) {
733-
Some(desc) => desc + "\n",
734-
None => String::new(),
735-
};
736-
format_to!(desc, "{}", original.display(db, edition));
737-
Markup::fenced_block(&desc)
736+
let mut desc = format!("```rust\n{}\n```", original.display(db, edition));
737+
if let Some(extra) = render_notable_trait(db, &notable_traits(db, &original), edition) {
738+
desc.push_str("\n___\n");
739+
desc.push_str(&extra);
740+
};
741+
desc.into()
738742
};
739743
if let Some(actions) = HoverAction::goto_type_from_targets(db, targets, edition) {
740744
res.actions.push(actions);
@@ -786,20 +790,16 @@ fn closure_ty(
786790
};
787791
let mut markup = format!("```rust\n{}", c.display_with_id(sema.db, edition));
788792

793+
if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) {
794+
push_new_def(hir::Trait::from(trait_).into())
795+
}
796+
format_to!(markup, "\n{}\n```", c.display_with_impl(sema.db, edition),);
789797
if let Some(layout) =
790798
render_memory_layout(config.memory_layout, || original.layout(sema.db), |_| None, |_| None)
791799
{
792-
format_to!(markup, " {layout}");
800+
format_to!(markup, "\n___\n{layout}");
793801
}
794-
if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) {
795-
push_new_def(hir::Trait::from(trait_).into())
796-
}
797-
format_to!(
798-
markup,
799-
"\n{}\n```{adjusted}\n\n## Captures\n{}",
800-
c.display_with_impl(sema.db, edition),
801-
captures_rendered,
802-
);
802+
format_to!(markup, "{adjusted}\n\n## Captures\n{}", captures_rendered,);
803803

804804
let mut res = HoverResult::default();
805805
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
@@ -824,15 +824,24 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) ->
824824
.map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
825825
}
826826

827-
fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Markup {
827+
fn markup(
828+
docs: Option<String>,
829+
rust: String,
830+
extra: Option<String>,
831+
mod_path: Option<String>,
832+
) -> Markup {
828833
let mut buf = String::new();
829834

830835
if let Some(mod_path) = mod_path {
831836
if !mod_path.is_empty() {
832837
format_to!(buf, "```rust\n{}\n```\n\n", mod_path);
833838
}
834839
}
835-
format_to!(buf, "```rust\n{}\n```", desc);
840+
format_to!(buf, "```rust\n{}\n```", rust);
841+
842+
if let Some(extra) = extra {
843+
buf.push_str(&extra);
844+
}
836845

837846
if let Some(doc) = docs {
838847
format_to!(buf, "\n___\n\n{}", doc);
@@ -862,7 +871,7 @@ fn render_memory_layout(
862871
let config = config?;
863872
let layout = layout().ok()?;
864873

865-
let mut label = String::from("// ");
874+
let mut label = String::new();
866875

867876
if let Some(render) = config.size {
868877
let size = match tag(&layout) {
@@ -994,55 +1003,53 @@ fn render_dyn_compatibility(
9941003
safety: Option<DynCompatibilityViolation>,
9951004
) {
9961005
let Some(osv) = safety else {
997-
buf.push_str("// Dyn Compatible: Yes");
1006+
buf.push_str("Is Dyn compatible");
9981007
return;
9991008
};
1000-
buf.push_str("// Dyn Compatible: No\n// - Reason: ");
1009+
buf.push_str("Is not Dyn compatible due to ");
10011010
match osv {
10021011
DynCompatibilityViolation::SizedSelf => {
1003-
buf.push_str("has a `Self: Sized` bound");
1012+
buf.push_str("having a `Self: Sized` bound");
10041013
}
10051014
DynCompatibilityViolation::SelfReferential => {
1006-
buf.push_str("has a bound that references `Self`");
1015+
buf.push_str("having a bound that references `Self`");
10071016
}
10081017
DynCompatibilityViolation::Method(func, mvc) => {
10091018
let name = hir::Function::from(func).name(db);
1010-
format_to!(
1011-
buf,
1012-
"has a method `{}` that is non dispatchable because of:\n// - ",
1013-
name.as_str()
1014-
);
1019+
format_to!(buf, "having a method `{}` that is not dispatchable due to ", name.as_str());
10151020
let desc = match mvc {
10161021
MethodViolationCode::StaticMethod => "missing a receiver",
1017-
MethodViolationCode::ReferencesSelfInput => "a parameter references `Self`",
1018-
MethodViolationCode::ReferencesSelfOutput => "the return type references `Self`",
1022+
MethodViolationCode::ReferencesSelfInput => "having a parameter referencing `Self`",
1023+
MethodViolationCode::ReferencesSelfOutput => "the return type referencing `Self`",
10191024
MethodViolationCode::ReferencesImplTraitInTrait => {
1020-
"the return type contains `impl Trait`"
1025+
"the return type containing `impl Trait`"
10211026
}
10221027
MethodViolationCode::AsyncFn => "being async",
10231028
MethodViolationCode::WhereClauseReferencesSelf => {
1024-
"a where clause references `Self`"
1029+
"a where clause referencing `Self`"
1030+
}
1031+
MethodViolationCode::Generic => "having a const or type generic parameter",
1032+
MethodViolationCode::UndispatchableReceiver => {
1033+
"having a non-dispatchable receiver type"
10251034
}
1026-
MethodViolationCode::Generic => "a non-lifetime generic parameter",
1027-
MethodViolationCode::UndispatchableReceiver => "a non-dispatchable receiver type",
10281035
};
10291036
buf.push_str(desc);
10301037
}
10311038
DynCompatibilityViolation::AssocConst(const_) => {
10321039
let name = hir::Const::from(const_).name(db);
10331040
if let Some(name) = name {
1034-
format_to!(buf, "has an associated constant `{}`", name.as_str());
1041+
format_to!(buf, "having an associated constant `{}`", name.as_str());
10351042
} else {
1036-
buf.push_str("has an associated constant");
1043+
buf.push_str("having an associated constant");
10371044
}
10381045
}
10391046
DynCompatibilityViolation::GAT(alias) => {
10401047
let name = hir::TypeAlias::from(alias).name(db);
1041-
format_to!(buf, "has a generic associated type `{}`", name.as_str());
1048+
format_to!(buf, "having a generic associated type `{}`", name.as_str());
10421049
}
10431050
DynCompatibilityViolation::HasNonCompatibleSuperTrait(super_trait) => {
10441051
let name = hir::Trait::from(super_trait).name(db);
1045-
format_to!(buf, "has a dyn incompatible supertrait `{}`", name.as_str());
1052+
format_to!(buf, "having a dyn incompatible supertrait `{}`", name.as_str());
10461053
}
10471054
}
10481055
}

0 commit comments

Comments
 (0)