Skip to content

region errors: suggest new signature #44506

Open
@nikomatsakis

Description

@nikomatsakis

In some cases, we can suggest a new signature where the user introduces a new named lifetime. This is a bit tricky to figure out how to format the suggestion. It would get mildly easier with rust-lang/rfcs#2115.

struct Ref<'a> { x: &'a u32 }

fn foo<T>(mut x: Vec<Ref<T>>, data: u32, y: Ref<T>)  {
    x.push(y);
}

The old code used to produce a modified form of the HIR to format the suggestion. I'd prefer not to do that. I was thinking we could probably do something simpler where we just emit the "new parts" of the signature, maybe like this?

hint: consider changing the signature as shown:
  | fn foo<'a, ...>(mut x: Vec<Ref<'a, T>>, ..., y: Ref<'a, T>)

Idea would be to do something like this:

  • Find name for a fresh lifetime that is not in scope (e.g., '``a)
  • Emit fn
  • Emit name of function (foo)
  • If the function has generic parameters already:
    • Emit <, fresh lifetime, , >
  • Else:
    • Emit <, fresh lifetime, >
  • Emit (
  • Emit if needed
  • Emit parameter name 1 and type (with fresh lifetime substituted)
  • Emit if needed
  • Emit parameter name 2 and type (with fresh lifetime substituted)
  • Emit if needed
  • Emit )

Not sure if this is a good plan. Might be best to wait until the rust-lang/rfcs#2115 is settled, since that would permit us to make a suggestion where we just add a fresh named lifetime, and we don't have to add <'a> or anything.

Metadata

Metadata

Assignees

No one assigned

    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-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions