Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Implementation of trait bound hints.
//!
//! Currently this renders the implied `Sized` bound.
use either::Either;
use ide_db::{famous_defs::FamousDefs, text_edit::TextEdit};

use syntax::ast::{self, AstNode};

use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};

pub(super) fn hints(
    acc: &mut Vec<InlayHint>,
    FamousDefs(sema, _): &FamousDefs<'_, '_>,
    config: &InlayHintsConfig<'_>,
    path: Either<ast::PathType, ast::DynTraitType>,
) -> Option<()> {
    if !config.implied_dyn_trait_hints {
        return None;
    }

    let parent = path.syntax().parent()?;
    let range = match path {
        Either::Left(path) => {
            let paren = parent
                .ancestors()
                .take_while(|it| {
                    ast::ParenType::can_cast(it.kind()) || ast::ForType::can_cast(it.kind())
                })
                .last();
            let parent = paren.as_ref().and_then(|it| it.parent()).unwrap_or(parent);
            if ast::TypeBound::can_cast(parent.kind())
                || ast::TypeAnchor::can_cast(parent.kind())
                || ast::Impl::cast(parent).is_some_and(|it| {
                    it.trait_().map_or(
                        // only show it for impl type if the impl is not incomplete, otherwise we
                        // are likely typing a trait impl
                        it.assoc_item_list().is_none_or(|it| it.l_curly_token().is_none()),
                        |trait_| trait_.syntax() == path.syntax(),
                    )
                })
            {
                return None;
            }
            sema.resolve_trait(&path.path()?)?;
            path.syntax().text_range()
        }
        Either::Right(dyn_) => {
            if dyn_.dyn_token().is_some() {
                return None;
            }

            dyn_.syntax().text_range()
        }
    };

    acc.push(InlayHint {
        range,
        kind: InlayKind::Dyn,
        label: InlayHintLabel::simple("dyn", None, None),
        text_edit: Some(
            config.lazy_text_edit(|| TextEdit::insert(range.start(), "dyn ".to_owned())),
        ),
        position: InlayHintPosition::Before,
        pad_left: false,
        pad_right: true,
        resolve_parent: Some(range),
    });

    Some(())
}

#[cfg(test)]
mod tests {

    use expect_test::expect;

    use crate::inlay_hints::InlayHintsConfig;

    use crate::inlay_hints::tests::{DISABLED_CONFIG, check_edit, check_with_config};

    #[track_caller]
    fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
        check_with_config(
            InlayHintsConfig {
                sized_bound: true,
                implied_dyn_trait_hints: true,
                ..DISABLED_CONFIG
            },
            ra_fixture,
        );
    }

    #[test]
    fn path_works() {
        check(
            r#"
struct S {}
trait T {}
fn foo(_: T,  _: dyn T, _: S) {}
       // ^ dyn
fn foo(_: &T,  _: for<'a> T) {}
        // ^ dyn
                       // ^ dyn
impl T {}
  // ^ dyn
impl T for (T) {}
         // ^ dyn
impl T for {}
impl T
"#,
        );
    }

    #[test]
    fn missing_dyn_bounds() {
        check(
            r#"
trait T {}
fn foo(
    _: T + T,
    // ^^^^^ dyn
    _: T + 'a,
    // ^^^^^^ dyn
    _: 'a + T,
    // ^^^^^^ dyn
    _: &(T + T)
    //   ^^^^^ dyn
    _: &mut (T + T)
    //       ^^^^^ dyn
    _: *mut (T),
    //       ^ dyn
) {}
"#,
        );
    }

    #[test]
    fn edit() {
        check_edit(
            InlayHintsConfig { implied_dyn_trait_hints: true, ..DISABLED_CONFIG },
            r#"
trait T {}
fn foo(
    _: &mut T
) {}
"#,
            expect![[r#"
                trait T {}
                fn foo(
                    _: &mut dyn T
                ) {}
            "#]],
        );
    }

    #[test]
    fn hrtb_bound_does_not_add_dyn() {
        check(
            r#"
//- minicore: fn
fn test<F>(f: F) where F: for<'a> FnOnce(&'a i32) {}
     // ^: Sized
        "#,
        );
    }

    #[test]
    fn with_parentheses() {
        check(
            r#"
trait T {}
fn foo(v: &(T)) {}
         // ^ dyn
        "#,
        );
    }
}