Skip to content

Calling .into() on argument that expects Into<Type> gives confusing error #71252

Closed

Description

@estebank

When calling (x: Type)::into() in a fn that expects Into<Type> we currently emit:

error[E0283]: type annotations needed
    --> src/librustc_resolve/late/diagnostics.rs:1143:21
     |
1143 |                 err.span_note(spans.into(), "these named lifetimes are available to use");
     |                     ^^^^^^^^^ ------------ this method call resolves to `T`
     |                     |
     |                     cannot infer type for type parameter `S` declared on the method `span_note`
     |
     = note: cannot resolve `_: std::convert::Into<rustc_span::MultiSpan>`

We should check whether T == MultiSpan and suggest the removal of the into() call.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    P-lowLow priority
    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-confusingDiagnostics: Confusing error or lint that should be reworked.
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    on Apr 17, 2020
  2. Duddino commented on Apr 18, 2020

    @Duddino

    @rustbot claim

  3. self-assigned this
    on Apr 18, 2020
  4. estebank commented on Apr 18, 2020

    @estebank
    ContributorAuthor

    @Duddino feel free to reach out either on this ticket or on a PR, but be warned that this has the potential to be quite involved. It is a great introduction to how some parts of rustc fit together though.

  5. Duddino commented on Apr 21, 2020

    @Duddino

    I've been looking into this for a while now, and my idea was to: Check if there a .into() call, check if the type of self was T and finally check if the argument of the function was Into. However I cannot figure how to get the type of Self. As far as my understanding goes, rustc hasn't yet inferred the type.

  6. estebank commented on Apr 21, 2020

    @estebank
    ContributorAuthor

    You can query the in-progress type table with the HirId for the expression. You're gonna have an Expr with a kind: ExprKind::MethodCall(method_name, [args including self]). From the first argument in that variant you can take the first element and get the expression for foo in foo.into(). Then you can use the HirId for that expression to do

    let tables = tcx.typeck_tables_of(tcx.hir().local_def_id(hir_id));
    let opt_ty = tables.node_type_opt(expr.hir_id));

    I do not know if we will have populated expr's type by then, but if we have, then that will give you the type of Self. Then, if you're in an FnCtxt you can use can_coerce to see if the inferred type for expr can be coerced to MultiSpan in the original example. I know this is a bit vague, but for this is inherently exploratory work :)

  7. Alexendoo commented on Aug 5, 2020

    @Alexendoo
    Member

    Hi, are you still working on this issue @Duddino?

  8. Duddino commented on Aug 5, 2020

    @Duddino
  9. Alexendoo commented on Aug 5, 2020

    @Alexendoo
    Member

    @rustbot release-assignment

  10. removed their assignment
    on Aug 5, 2020
  11. self-assigned this
    on Feb 14, 2024
  12. added 4 commits that reference this issue on Feb 14, 2024
    9a1d514
    a71a218
    5a1c454
    9b3fcf9
  13. added a commit that references this issue on Feb 17, 2024
    7fedf99
  14. added a commit that references this issue on Feb 17, 2024
    439fabc
  15. added a commit that references this issue on Feb 18, 2024
    6499eb5
  16. added a commit that references this issue on Feb 18, 2024
    72997ad
  17. added a commit that references this issue on Feb 18, 2024
    e92758c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-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