mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-28 10:39:45 +00:00
Merge pull request #18986 from Veykril/push-zlwvwlowpzqm
Goto `Display::fmt` when invoked on `to_string`
This commit is contained in:
commit
2c040c03cf
4 changed files with 49 additions and 0 deletions
|
|
@ -1483,6 +1483,8 @@ impl<'db> SemanticsImpl<'db> {
|
||||||
self.analyze(try_expr.syntax())?.resolve_try_expr(self.db, try_expr)
|
self.analyze(try_expr.syntax())?.resolve_try_expr(self.db, try_expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This does not resolve the method call to the correct trait impl!
|
||||||
|
// We should probably fix that.
|
||||||
pub fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
|
pub fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
|
||||||
self.analyze(call.syntax())?.resolve_method_call_as_callable(self.db, call)
|
self.analyze(call.syntax())?.resolve_method_call_as_callable(self.db, call)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -142,6 +142,13 @@ impl FamousDefs<'_, '_> {
|
||||||
self.find_macro("core:unimplemented")
|
self.find_macro("core:unimplemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn core_fmt_Display(&self) -> Option<Trait> {
|
||||||
|
self.find_trait("core:fmt:Display")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn alloc_string_ToString(&self) -> Option<Trait> {
|
||||||
|
self.find_trait("alloc:string:ToString")
|
||||||
|
}
|
||||||
pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
|
pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
|
||||||
IntoIterator::into_iter([
|
IntoIterator::into_iter([
|
||||||
self.std(),
|
self.std(),
|
||||||
|
|
|
||||||
|
|
@ -185,6 +185,15 @@ fn find_definition_for_known_blanket_dual_impls(
|
||||||
// Extract the `T` from `Result<T, ..>`
|
// Extract the `T` from `Result<T, ..>`
|
||||||
[return_type.type_arguments().next()?, callable.receiver_param(sema.db)?.1],
|
[return_type.type_arguments().next()?, callable.receiver_param(sema.db)?.1],
|
||||||
)?
|
)?
|
||||||
|
} else if fn_name == sym::to_string && fd.alloc_string_ToString() == Some(t) {
|
||||||
|
let dual = fd.core_fmt_Display()?;
|
||||||
|
let dual_f = dual.function(sema.db, &sym::fmt)?;
|
||||||
|
sema.resolve_trait_impl_method(
|
||||||
|
return_type.clone(),
|
||||||
|
dual,
|
||||||
|
dual_f,
|
||||||
|
[callable.receiver_param(sema.db)?.1.strip_reference()],
|
||||||
|
)?
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
@ -3231,6 +3240,36 @@ impl FromStr for A {
|
||||||
}
|
}
|
||||||
fn f() {
|
fn f() {
|
||||||
let a: Result<A, _> = "aaaaaa".parse$0();
|
let a: Result<A, _> = "aaaaaa".parse$0();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_string_call_to_display_definition() {
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
//- minicore:fmt
|
||||||
|
//- /alloc.rs crate:alloc
|
||||||
|
pub mod string {
|
||||||
|
pub struct String;
|
||||||
|
pub trait ToString {
|
||||||
|
fn to_string(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: core::fmt::Display> ToString for T {
|
||||||
|
fn to_string(&self) -> String { String }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//- /lib.rs crate:lib deps:alloc
|
||||||
|
use alloc::string::ToString;
|
||||||
|
struct A;
|
||||||
|
impl core::fmt::Display for A {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {}
|
||||||
|
// ^^^
|
||||||
|
}
|
||||||
|
fn f() {
|
||||||
|
A.to_string$0();
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -461,6 +461,7 @@ define_symbols! {
|
||||||
test,
|
test,
|
||||||
then,
|
then,
|
||||||
thiscall,
|
thiscall,
|
||||||
|
to_string,
|
||||||
trace_macros,
|
trace_macros,
|
||||||
transmute_opts,
|
transmute_opts,
|
||||||
transmute_trait,
|
transmute_trait,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue