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

Commit fae9049

Browse files
committed
Fall back to parameter definitions on error types in signature help
1 parent 5582402 commit fae9049

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

crates/ide/src/signature_help.rs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ fn signature_help_for_call(
110110
SignatureHelp { doc: None, signature: String::new(), parameters: vec![], active_parameter };
111111

112112
let db = sema.db;
113+
let mut fn_params = None;
113114
match callable.kind() {
114115
hir::CallableKind::Function(func) => {
115116
res.doc = func.docs(db).map(|it| it.into());
116117
format_to!(res.signature, "fn {}", func.name(db));
118+
fn_params = Some(func.assoc_fn_params(db));
117119
}
118120
hir::CallableKind::TupleStruct(strukt) => {
119121
res.doc = strukt.docs(db).map(|it| it.into());
@@ -137,15 +139,23 @@ fn signature_help_for_call(
137139
format_to!(res.signature, "{}", self_param)
138140
}
139141
let mut buf = String::new();
140-
for (pat, ty) in callable.params(db) {
142+
for (idx, (pat, ty)) in callable.params(db).into_iter().enumerate() {
141143
buf.clear();
142144
if let Some(pat) = pat {
143145
match pat {
144146
Either::Left(_self) => format_to!(buf, "self: "),
145147
Either::Right(pat) => format_to!(buf, "{}: ", pat),
146148
}
147149
}
148-
format_to!(buf, "{}", ty.display(db));
150+
// APITs (argument position `impl Trait`s) are inferred as {unknown} as the user is
151+
// in the middle of entering call arguments.
152+
// In that case, fall back to render definition of the argument.
153+
// This is overly conservative: we do not substitute known type vars
154+
// (see FIXME in tests::impl_trait).
155+
match (ty.contains_unknown(), fn_params.as_deref()) {
156+
(true, Some(fn_params)) => format_to!(buf, "{}", fn_params[idx].ty().display(db)),
157+
_ => format_to!(buf, "{}", ty.display(db)),
158+
}
149159
res.push_call_param(&buf);
150160
}
151161
}
@@ -420,8 +430,8 @@ fn foo<T, U: Copy + Display>(x: T, y: U) -> u32
420430
fn bar() { foo($03, ); }
421431
"#,
422432
expect![[r#"
423-
fn foo(x: i32, y: {unknown}) -> u32
424-
^^^^^^ ------------
433+
fn foo(x: i32, y: U) -> u32
434+
^^^^^^ ----
425435
"#]],
426436
);
427437
}
@@ -633,26 +643,21 @@ pub fn do_it() {
633643
fn test_fn_signature_with_docs_from_actix() {
634644
check(
635645
r#"
636-
struct WriteHandler<E>;
637-
638-
impl<E> WriteHandler<E> {
639-
/// Method is called when writer emits error.
640-
///
641-
/// If this method returns `ErrorAction::Continue` writer processing
642-
/// continues otherwise stream processing stops.
643-
fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running {
644-
Running::Stop
645-
}
646-
646+
trait Actor {
647+
/// Actor execution context type
648+
type Context;
649+
}
650+
trait WriteHandler<E>
651+
where
652+
Self: Actor
653+
{
647654
/// Method is called when writer finishes.
648655
///
649656
/// By default this method stops actor's `Context`.
650-
fn finished(&mut self, ctx: &mut Self::Context) {
651-
ctx.stop()
652-
}
657+
fn finished(&mut self, ctx: &mut Self::Context) {}
653658
}
654659
655-
pub fn foo(mut r: WriteHandler<()>) {
660+
fn foo(mut r: impl WriteHandler<()>) {
656661
r.finished($0);
657662
}
658663
"#,
@@ -661,8 +666,8 @@ pub fn foo(mut r: WriteHandler<()>) {
661666
662667
By default this method stops actor's `Context`.
663668
------
664-
fn finished(&mut self, ctx: &mut {unknown})
665-
^^^^^^^^^^^^^^^^^^^
669+
fn finished(&mut self, ctx: &mut <impl WriteHandler<()> as Actor>::Context)
670+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
666671
"#]],
667672
);
668673
}
@@ -1055,4 +1060,23 @@ fn f() {
10551060
"#]],
10561061
);
10571062
}
1063+
1064+
#[test]
1065+
fn impl_trait() {
1066+
// FIXME: Substitute type vars in impl trait (`U` -> `i8`)
1067+
check(
1068+
r#"
1069+
trait Trait<T> {}
1070+
struct Wrap<T>(T);
1071+
fn foo<U>(x: Wrap<impl Trait<U>>) {}
1072+
fn f() {
1073+
foo::<i8>($0)
1074+
}
1075+
"#,
1076+
expect![[r#"
1077+
fn foo(x: Wrap<impl Trait<U>>)
1078+
^^^^^^^^^^^^^^^^^^^^^^
1079+
"#]],
1080+
);
1081+
}
10581082
}

0 commit comments

Comments
 (0)