Skip to content

"snippet" for "did you mean x" is not so helpful #36164

Closed

Description

@nikomatsakis

On current beta, at least, I see:

error: attempted access of field `test_results` on type `TestResult`, but no field with that name was
 found
   --> src/main.rs:196:25
    |
196 |         tests_passed += normal_test.test_results.iter().filter(|t| t.status == "ok").count();
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: did you mean `results`?
   --> src/main.rs:196:37
    |
196 |         tests_passed += normal_test.test_results.iter().filter(|t| t.status == "ok").count();
    |                                     ^^^^^^^^^^^^

But I feel like I'd rather see:

error: attempted access of field `test_results` on type `TestResult`, but no field with that name was
 found
   --> src/main.rs:196:25
    |
196 |         tests_passed += normal_test.test_results.iter().filter(|t| t.status == "ok").count();
    |                                     ^^^^^^^^^^^^
    =  help: did you mean `results`?

Come to think of it, we could shorten the main message and move the suggestion into the label, as I think you've done elsewhere:

error: no field `test_results` on type `TestResult`
   --> src/main.rs:196:25
    |
196 |         tests_passed += normal_test.test_results.iter().filter(|t| t.status == "ok").count();
    |                                     ^^^^^^^^^^^^ did you mean `results`?
    |

Standalone example:

https://is.gd/8STXMd

cc @jonathandturner

Activity

  1. added
    A-diagnostics
    C-enhancement
    T-compiler
    on Aug 31, 2016
  2. durka commented on Aug 31, 2016

    @durka
    Contributor

    Another possibility is to actually replace it

    error: attempted access of field `test_results` on type `TestResult`, but no field with that name was
     found
       --> src/main.rs:196:25
        |
    196 |         tests_passed += normal_test.test_results.iter().filter(|t| t.status == "ok").count();
        |                         ^^^^^^^^^^^^^^^^^^^^^^^^
        |
    help: did you mean `results`?
       --> src/main.rs:196:37
        |
    196 |         tests_passed += normal_test.results.iter().filter(|t| t.status == "ok").count();
        |                                     ^^^^^^^
    

    But if it's only a guess, that might not be a good idea.

  3. nikomatsakis commented on Aug 31, 2016

    @nikomatsakis
    ContributorAuthor

    @durka my inclination is that the extra 5 lines of output aren't valuable enough in this case, it's not likely it's tricky to figure out how to apply the change

  4. sophiajt commented on Aug 31, 2016

    @sophiajt
    Contributor

    Or maybe even...

    error: attempted access of field `test_results` on type `TestResult`, but no field with that name was
     found
       --> src/main.rs:196:25
        |
    196 |         tests_passed += normal_test.test_results.iter().filter(|t| t.status == "ok").count();
        |                                     ^^^^^^^^^^^^ did you mean `results`?
    
  5. nikomatsakis commented on Sep 1, 2016

    @nikomatsakis
    ContributorAuthor

    @jonathandturner how is that different from my 3rd example? (Except for using a longer "error' message)

  6. sophiajt commented on Sep 1, 2016

    @sophiajt
    Contributor

    @nikomatsakis - lol are you sure you didn't add it after I commented?

    Just kidding, I just didn't see it the first time through.

  7. sophiajt commented on Sep 1, 2016

    @sophiajt
    Contributor

    My vote is your # 3 ;)

  8. added
    E-mentor
    on Sep 2, 2016
  9. nikomatsakis commented on Sep 2, 2016

    @nikomatsakis
    ContributorAuthor

    I'm happy to mentor the change. It seems like it would be fairly self-contained. The error is reported by this code in src/librustc_typeck/check/mod.rs -- just search for "attempted access of field" and you ought to find it.

    The idea would be to

    • change the main message there to something shorter, like "no field {} in type {}"
    • change the expr.span in self.type_error_struct(expr.span to field.span, so that we highlight just the field name itself
    • change the suggest_field_names function so that instead of calling err.span_help it calls err.span_label

    I think that will basically do it!

  10. nikomatsakis commented on Sep 2, 2016

    @nikomatsakis
    ContributorAuthor

    One wrinkle I am not sure about: the changes that I suggested above will, iirc, cause a ^^^ with no suggestion to appear in the case that we don't have a suggested field name to offer. I feel like we wanted to move away from those, which might suggest that we ought to offer a generic label like "this field here" or something? <-- @jonathandturner ?

  11. sophiajt commented on Sep 2, 2016

    @sophiajt
    Contributor

    Yes, a default label would be great when we don't know the name. Maybe something like "^^^^ unknown field" when we don't know of a good suggestion

  12. aravind-pg commented on Sep 2, 2016

    @aravind-pg
    Contributor

    I'd love to try my hand at this. The instructions posted seem sufficient for now, but I'll come back if I have any questions.

  13. KiChjang commented on Sep 17, 2016

    @KiChjang
    Contributor

    @aravind-pg Are you still working on this?

  14. aravind-pg commented on Sep 19, 2016

    @aravind-pg
    Contributor

    Sorry, I just haven't found time for this unfortunately. You can go ahead and un-assign me.

  15. added a commit that references this issue on Sep 29, 2016
    422d553
  16. added a commit that references this issue on Oct 4, 2016
    5a4ecb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsC-enhancementE-mentorT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions