Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

region errors: suggest new signature #44506

Open
nikomatsakis opened this issue Sep 11, 2017 · 1 comment
Open

region errors: suggest new signature #44506

nikomatsakis opened this issue Sep 11, 2017 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@nikomatsakis
Copy link
Contributor

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.

@nikomatsakis nikomatsakis added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 11, 2017
@TimNN TimNN added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Sep 17, 2017
@estebank estebank added the WG-diagnostics Working group: Diagnostics label Nov 30, 2017
@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Feb 14, 2020
@estebank estebank added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Apr 10, 2020
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

Current output:

error[[E0107]](https://doc.rust-lang.org/stable/error_codes/E0107.html): struct takes 0 generic arguments but 1 generic argument was supplied
 --> src/lib.rs:3:22
  |
3 | fn foo<T>(mut x: Vec<Ref<T>>, data: u32, y: Ref<T>)  {
  |                      ^^^ - help: remove this generic argument
  |                      |
  |                      expected 0 generic arguments
  |
note: struct defined here, with 0 generic parameters
 --> src/lib.rs:1:8
  |
1 | struct Ref<'a> { x: &'a u32 }
  |        ^^^

error[[E0107]](https://doc.rust-lang.org/stable/error_codes/E0107.html): struct takes 0 generic arguments but 1 generic argument was supplied
 --> src/lib.rs:3:45
  |
3 | fn foo<T>(mut x: Vec<Ref<T>>, data: u32, y: Ref<T>)  {
  |                                             ^^^ - help: remove this generic argument
  |                                             |
  |                                             expected 0 generic arguments
  |
note: struct defined here, with 0 generic parameters
 --> src/lib.rs:1:8
  |
1 | struct Ref<'a> { x: &'a u32 }
  |        ^^^
error: lifetime may not live long enough
 --> src/lib.rs:4:5
  |
3 | fn foo<T>(mut x: Vec<Ref>, data: u32, y: Ref)  {
  |           -----                       - has type `Ref<'1>`
  |           |
  |           has type `Vec<Ref<'2>>`
4 |     x.push(y);
  |     ^^^^^^^^^ argument requires that `'1` must outlive `'2`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

3 participants