Skip to content

Diagnostic suggests adding type [type error] #79040

Closed

Description

@camelid

I think the suggestion is trying to suggest a type, but gets a TyKind::Error due to the type error in the body of the const. Ideally we would just not suggest a type, but there are probably a lot of other places that this issue occurs.

const FOO = "hello" + 1;

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0369]: cannot add `{integer}` to `&str`
 --> src/lib.rs:1:21
  |
1 | const FOO = "hello" + 1;
  |             ------- ^ - {integer}
  |             |
  |             &str

error: missing type for `const` item
 --> src/lib.rs:1:7
  |
1 | const FOO = "hello" + 1;
  |       ^^^ help: provide a type for the item: `FOO: [type error]`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0369`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Nov 14, 2020
  2. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Nov 14, 2020
  3. oli-obk commented on Nov 14, 2020

    @oli-obk
    Contributor

    Fix instructions:

    1. find the place where the provide a type for the item suggestion is emitted
    2. prevent the suggestion from being created if the suggested type is
  4. added
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Nov 14, 2020
  5. oli-obk commented on Nov 14, 2020

    @oli-obk
    Contributor

    Cool! You can mark yourself as workin on this issue by writing @rustbot claim in it. Feel free to ask away with anything that is unclear. Or actually, just anything ^^ If you prefer chat over github messages, you can also reach out in zulip by opening a new thread.

  6. oli-obk commented on Nov 26, 2020

    @oli-obk
    Contributor

    I like the delay_as_bug() variant more.

    Though I think we should add an ignore method that takes a DelaySpanBugEmitted, which we get from the ty::Error. What do you think @rust-lang/wg-diagnostics

  7. petar-dambovaliev commented on Nov 30, 2020

    @petar-dambovaliev
    Contributor

    cannot add {integer} to &str

    Shouldn't it also be cannot add `&str` to `{integer}, same order as in the code?

  8. 1 remaining item

  9. oli-obk commented on Nov 30, 2020

    @oli-obk
    Contributor

    cannot add {integer} to &str

    Shouldn't it also be cannot add `&str` to `{integer}, same order as in the code?

    The order is indeed odd, but at least grammatically correct. If you have 5 + 6, you are adding 6 to 5. This along with another order swap around <SomeType as Add<OtherType>> being for an impl Add<OtherType> for SomeType and meaning an operation value_of_some_type + value_of_other_type. I think we could have an entire design meeting just around how to get all of that into a diagnostic without causing more confusion than the status quo. If you want you can open an issue about just the order confusion, I'll add the appropriate tags.

  10. estebank commented on Nov 30, 2020

    @estebank
    Contributor

    I like the delay_as_bug() variant more.

    I'm concerned about the possibility of there being a case where [type error] is generated without any other error (but that shouldn't happen). The _ case is weird and I think we should emit the error in that case (although maybe not a suggestion, but not the end of the world). I like the possibility of using emit_unless:

            Some(mut err) => {
                err.suggestions.clear();
                err.span_suggestion(
                    span,
                    "provide a type for the item",
                    format!("{}: {}", item_ident, ty),
                    Applicability::MachineApplicable,
                )
                .emit_unless(matches!(ty.kind(), ty::Error(_)));
            }
  11. petar-dambovaliev commented on Dec 1, 2020

    @petar-dambovaliev
    Contributor

    cannot add {integer} to &str

    Shouldn't it also be cannot add `&str` to `{integer}, same order as in the code?

    The order is indeed odd, but at least grammatically correct. If you have 5 + 6, you are adding 6 to 5. This along with another order swap around <SomeType as Add<OtherType>> being for an impl Add<OtherType> for SomeType and meaning an operation value_of_some_type + value_of_other_type. I think we could have an entire design meeting just around how to get all of that into a diagnostic without causing more confusion than the status quo. If you want you can open an issue about just the order confusion, I'll add the appropriate tags.

    I don't know enough about the language design space to be opening up meaningful issues. I was just making a comment. I don't know whether it's an issue or not and I certainly don't want to create issues that will be closed immediately.

  12. estebank commented on Dec 1, 2020

    @estebank
    Contributor

    I don't know enough about the language design space to be opening up meaningful issues.

    @petar-dambovaliev That shouldn't stop you. Some times there are things that the team hasn't yet noticed and it is a good idea to rise the point. Other times tickets are duplicated but that is also a useful signal that can be a proxy for urgency.

    I don't know whether it's an issue or not and I certainly don't want to create issues that will be closed immediately.

    We treat these kind of issues seriously. We rarely immediately close tickets around here unless they are a duplicate (where we close with a link) or completely off-topic (where we point of more reasonable venues). If you still feel this comment isn't worth a new ticket but still want to have a conversation around it, feel free to visit https://internals.rust-lang.org/ and start a new thread.

    IIRC, I am the one that wrote that message and I went back and forth between a sentence that made grammatical sense and one that looked like the provided code, and landed in the current message and a main label in the order of the code. Since the output has changed slightly and the later has been "lost". As the "culprit" of the current state, I welcome any conversation around this. That's how we improve :)

  13. added a commit that references this issue on Mar 3, 2021
    d9d6921
  14. henryboisdequin commented on Mar 3, 2021

    @henryboisdequin
    Contributor

    @rustbot claim

    #82720 is open :)

  15. assigned and unassigned on Mar 3, 2021
  16. added 2 commits that reference this issue on Mar 6, 2021
    090489c
    866d5e4
  17. added a commit that references this issue on Mar 6, 2021
    ec5a93a
  18. added a commit that references this issue on Mar 6, 2021
    44cb131
  19. added a commit that references this issue on Mar 7, 2021
    a5a825e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions