# Additional suggestions when field name doesn't exist

**URL:** <https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723>\
**Category:** language design\
**Created:** [June 13, 2018, 5:39am UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723 "2018-06-13T05:39:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [June 13, 2018, 5:39am UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723/1 "2018-06-13T05:39:04Z")

</div>

Link to RFC: [rfc-2472](https://github.com/rust-lang/rfcs/issues/2472)

Basically the gist of this improvement is adding to the list of suggestions for when a programmer types a field that does not exist in a struct.

Currently, if the field name that is typed does not exist in the struct then all fields in the struct are suggested as things that might have wanted to be typed instead. This is a wonderful feature.

I, therefore, propose that this might be even more helpful if there were some more suggestions that are presented. These being a path of field names within the struct where the fields are public to the location that the error occured and the last inner field name is equal to that of the field name typed.

How does this sound for people?

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [June 13, 2018, 6:59am UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723/2 "2018-06-13T06:59:37Z")

</div>

I like this idea quite a bit.

As a nit, I think I’d suggest a smaller limit on the depth of such searches, something like 2 rather than 5. And you should clarify that this kind of search only does an _exact_ match on the field name within a substruct, rather than an edit-distance-based search.

Also, to forestall a potential addition: I think this should remain limited to field names, not method names. In particular, because the right answer for a method name might be “you forgot to impl the appropriate trait”, and it’s more likely that you’d have identically-named methods on multiple objects for which a suggestion would be inappropriate.

---

<div class="post-metadata">

**Author:** ![ekuber](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ekuber/32/1646_2.png) [@ekuber](https://internals.rust-lang.org/u/ekuber)\
**Post date:** [June 13, 2018, 6:51pm UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723/3 "2018-06-13T18:51:04Z")

</div>

I like this feature request. At the very least `E0609` should have a span pointing at `DFA`'s definition (which can be added trivially now). With this feature request, it should also point at `State` and `State::tokens`, providing a structured suggestion to write `i.state.tokens[0].val = 5;`. That being said, making the suggestion 100% correct all the time might be tricky, without going through a full borrowck and typeck of the resulting code.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [June 13, 2018, 9:48pm UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723/4 "2018-06-13T21:48:17Z")

</div>

Yes I agree that this shouldn’t be for functions.

However, two things. Why such a small default depth? Second, would a levenstighn distance of 2-4 really be that bad?

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [June 13, 2018, 9:55pm UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723/5 "2018-06-13T21:55:35Z")

</div>

I don’t think we have to check about the borrow checker because I don’t think that it currently does. This is really just helping the programer choose a possibility correct value not a completely correct item

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [June 14, 2018, 5:00am UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723/6 "2018-06-14T05:00:15Z")

</div>

Seems too likely to have false positives otherwise.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/additional-suggestions-when-field-name-doesnt-exist/7723/7 "2019-03-25T08:30:22Z")

</div>

Closed on March 25, 2019, 8:30am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
