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

Use structured suggestion for closure needed instead of inner function #65504

Open
estebank opened this issue Oct 17, 2019 · 4 comments
Open
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.

Comments

@estebank
Copy link
Contributor

We currently emit the following error

error[E0434]: can't capture dynamic environment in a fn item
  --> $DIR/bad-env-capture3.rs:4:31
   |
LL |         fn bar() { log(debug, x); }
   |                               ^
   |
   = help: use the `|| { ... }` closure form instead

It'd be nice if we provided a structured suggestion instead.

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. 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. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Oct 17, 2019
@XiangQingW
Copy link
Contributor

@estebank Excuse me, do you mean that we should use the span_suggestion method of DiagnosticBuilder to make a suggestion?

@estebank
Copy link
Contributor Author

estebank commented Oct 28, 2019

Correct. The tricky part will be finding the appropriate spans to suggest with. It seems to me we could use a multipart suggestion and replace the leading fn bar( with let bar = | and the ) with |.

This would get trickier if there are type parameters because you'd have to probably just remove the type of the affected argument, but we can fallback to the current note in cases like that.

@XiangQingW
Copy link
Contributor

XiangQingW commented Nov 5, 2019

I'm trying to carry out the idea and the span of fn bar can be got from the inputs of visit_fn.

fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, declaration: &'tcx FnDecl, sp: Span, _: NodeId)

However, I cannot find a way to get the span of ( and ). Could you please give me some advice if you have free time.

@estebank
Copy link
Contributor Author

estebank commented Feb 3, 2023

Triage: no change

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.
Projects
None yet
Development

No branches or pull requests

2 participants