Merge pull request #19330 from ChayimFriedman2/normalize-projection

fix: Normalize projections in evaluated const display and layout calculation
This commit is contained in:
Lukas Wirth 2025-03-10 09:15:35 +00:00 committed by GitHub
commit 27a5b1ba0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
64 changed files with 887 additions and 521 deletions

View file

@ -16,7 +16,7 @@ fn render(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem>
let name = const_.name(db)?;
let (name, escaped_name) =
(name.as_str().to_smolstr(), name.display(db, ctx.completion.edition).to_smolstr());
let detail = const_.display(db, ctx.completion.edition).to_string();
let detail = const_.display(db, ctx.completion.display_target).to_string();
let mut item =
CompletionItem::new(SymbolKind::Const, ctx.source_range(), name, ctx.completion.edition);

View file

@ -4,7 +4,7 @@ use hir::{db::HirDatabase, AsAssocItem, HirDisplay};
use ide_db::{SnippetCap, SymbolKind};
use itertools::Itertools;
use stdx::{format_to, to_lower_snake_case};
use syntax::{format_smolstr, AstNode, Edition, SmolStr, ToSmolStr};
use syntax::{format_smolstr, AstNode, SmolStr, ToSmolStr};
use crate::{
context::{
@ -142,9 +142,9 @@ fn render(
}
let detail = if ctx.completion.config.full_function_signatures {
detail_full(db, func, ctx.completion.edition)
detail_full(ctx.completion, func)
} else {
detail(ctx.completion, func, ctx.completion.edition)
detail(ctx.completion, func)
};
item.set_documentation(ctx.docs(func))
.set_deprecated(ctx.is_deprecated(func) || ctx.is_deprecated_assoc_item(func))
@ -251,7 +251,7 @@ pub(super) fn add_call_parens<'b>(
format!(
"{}(${{1:{}}}{}{})$0",
escaped_name,
self_param.display(ctx.db, ctx.edition),
self_param.display(ctx.db, ctx.display_target),
if params.is_empty() { "" } else { ", " },
function_params_snippet
)
@ -307,7 +307,7 @@ fn ref_of_param(ctx: &CompletionContext<'_>, arg: &str, ty: &hir::Type) -> &'sta
""
}
fn detail(ctx: &CompletionContext<'_>, func: hir::Function, edition: Edition) -> String {
fn detail(ctx: &CompletionContext<'_>, func: hir::Function) -> String {
let mut ret_ty = func.ret_type(ctx.db);
let mut detail = String::new();
@ -324,15 +324,15 @@ fn detail(ctx: &CompletionContext<'_>, func: hir::Function, edition: Edition) ->
format_to!(detail, "unsafe ");
}
format_to!(detail, "fn({})", params_display(ctx.db, func, edition));
format_to!(detail, "fn({})", params_display(ctx, func));
if !ret_ty.is_unit() {
format_to!(detail, " -> {}", ret_ty.display(ctx.db, edition));
format_to!(detail, " -> {}", ret_ty.display(ctx.db, ctx.display_target));
}
detail
}
fn detail_full(db: &dyn HirDatabase, func: hir::Function, edition: Edition) -> String {
let signature = format!("{}", func.display(db, edition));
fn detail_full(ctx: &CompletionContext<'_>, func: hir::Function) -> String {
let signature = format!("{}", func.display(ctx.db, ctx.display_target));
let mut detail = String::with_capacity(signature.len());
for segment in signature.split_whitespace() {
@ -346,24 +346,24 @@ fn detail_full(db: &dyn HirDatabase, func: hir::Function, edition: Edition) -> S
detail
}
fn params_display(db: &dyn HirDatabase, func: hir::Function, edition: Edition) -> String {
if let Some(self_param) = func.self_param(db) {
let assoc_fn_params = func.assoc_fn_params(db);
fn params_display(ctx: &CompletionContext<'_>, func: hir::Function) -> String {
if let Some(self_param) = func.self_param(ctx.db) {
let assoc_fn_params = func.assoc_fn_params(ctx.db);
let params = assoc_fn_params
.iter()
.skip(1) // skip the self param because we are manually handling that
.map(|p| p.ty().display(db, edition));
.map(|p| p.ty().display(ctx.db, ctx.display_target));
format!(
"{}{}",
self_param.display(db, edition),
self_param.display(ctx.db, ctx.display_target),
params.format_with("", |display, f| {
f(&", ")?;
f(&display)
})
)
} else {
let assoc_fn_params = func.assoc_fn_params(db);
assoc_fn_params.iter().map(|p| p.ty().display(db, edition)).join(", ")
let assoc_fn_params = func.assoc_fn_params(ctx.db);
assoc_fn_params.iter().map(|p| p.ty().display(ctx.db, ctx.display_target)).join(", ")
}
}

View file

@ -82,10 +82,10 @@ fn render(
let mut rendered = match kind {
StructKind::Tuple if should_add_parens => {
render_tuple_lit(db, snippet_cap, &fields, &escaped_qualified_name, completion.edition)
render_tuple_lit(completion, snippet_cap, &fields, &escaped_qualified_name)
}
StructKind::Record if should_add_parens => {
render_record_lit(db, snippet_cap, &fields, &escaped_qualified_name, completion.edition)
render_record_lit(completion, snippet_cap, &fields, &escaped_qualified_name)
}
_ => RenderedLiteral {
literal: escaped_qualified_name.clone(),

View file

@ -62,7 +62,7 @@ fn render(
completion.edition,
);
item.set_deprecated(ctx.is_deprecated(macro_))
.detail(macro_.display(completion.db, completion.edition).to_string())
.detail(macro_.display(completion.db, completion.display_target).to_string())
.set_documentation(docs)
.set_relevance(ctx.completion_relevance());

View file

@ -38,7 +38,7 @@ fn render(
} else {
(name.as_str().to_smolstr(), name.display_no_db(ctx.completion.edition).to_smolstr())
};
let detail = type_alias.display(db, ctx.completion.edition).to_string();
let detail = type_alias.display(db, ctx.completion.display_target).to_string();
let mut item = CompletionItem::new(
SymbolKind::TypeAlias,

View file

@ -88,7 +88,7 @@ pub(crate) fn render_union_literal(
f(&format_args!(
"{}: {}",
field.name(ctx.db()).display(ctx.db(), ctx.completion.edition),
field.ty(ctx.db()).display(ctx.db(), ctx.completion.edition)
field.ty(ctx.db()).display(ctx.db(), ctx.completion.display_target)
))
}),
if fields_omitted { ", .." } else { "" }

View file

@ -1,10 +1,10 @@
//! Code common to structs, unions, and enum variants.
use crate::context::CompletionContext;
use hir::{db::HirDatabase, sym, HasAttrs, HasCrate, HasVisibility, HirDisplay, StructKind};
use hir::{sym, HasAttrs, HasCrate, HasVisibility, HirDisplay, StructKind};
use ide_db::SnippetCap;
use itertools::Itertools;
use syntax::{Edition, SmolStr};
use syntax::SmolStr;
/// A rendered struct, union, or enum variant, split into fields for actual
/// auto-completion (`literal`, using `field: ()`) and display in the
@ -17,11 +17,10 @@ pub(crate) struct RenderedLiteral {
/// Render a record type (or sub-type) to a `RenderedCompound`. Use `None` for
/// the `name` argument for an anonymous type.
pub(crate) fn render_record_lit(
db: &dyn HirDatabase,
ctx: &CompletionContext<'_>,
snippet_cap: Option<SnippetCap>,
fields: &[hir::Field],
path: &str,
edition: Edition,
) -> RenderedLiteral {
if snippet_cap.is_none() {
return RenderedLiteral { literal: path.to_owned(), detail: path.to_owned() };
@ -30,19 +29,19 @@ pub(crate) fn render_record_lit(
if snippet_cap.is_some() {
f(&format_args!(
"{}: ${{{}:()}}",
field.name(db).display(db.upcast(), edition),
field.name(ctx.db).display(ctx.db, ctx.edition),
idx + 1
))
} else {
f(&format_args!("{}: ()", field.name(db).display(db.upcast(), edition)))
f(&format_args!("{}: ()", field.name(ctx.db).display(ctx.db, ctx.edition)))
}
});
let types = fields.iter().format_with(", ", |field, f| {
f(&format_args!(
"{}: {}",
field.name(db).display(db.upcast(), edition),
field.ty(db).display(db, edition)
field.name(ctx.db).display(ctx.db, ctx.edition),
field.ty(ctx.db).display(ctx.db, ctx.display_target)
))
});
@ -55,11 +54,10 @@ pub(crate) fn render_record_lit(
/// Render a tuple type (or sub-type) to a `RenderedCompound`. Use `None` for
/// the `name` argument for an anonymous type.
pub(crate) fn render_tuple_lit(
db: &dyn HirDatabase,
ctx: &CompletionContext<'_>,
snippet_cap: Option<SnippetCap>,
fields: &[hir::Field],
path: &str,
edition: Edition,
) -> RenderedLiteral {
if snippet_cap.is_none() {
return RenderedLiteral { literal: path.to_owned(), detail: path.to_owned() };
@ -72,7 +70,9 @@ pub(crate) fn render_tuple_lit(
}
});
let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db, edition)));
let types = fields
.iter()
.format_with(", ", |field, f| f(&field.ty(ctx.db).display(ctx.db, ctx.display_target)));
RenderedLiteral {
literal: format!("{path}({completions})"),