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

Suggest fn if fun, func, function or def is used instead #99751

Closed
Rageking8 opened this issue Jul 26, 2022 · 12 comments · Fixed by #100547 or #100750
Closed

Suggest fn if fun, func, function or def is used instead #99751

Rageking8 opened this issue Jul 26, 2022 · 12 comments · Fixed by #100547 or #100750
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Rageking8
Copy link
Contributor

Rageking8 commented Jul 26, 2022

Given the following code:

func foo() -> i32 {
    123
}

function foo() -> i32 {
    123
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: expected one of `!` or `::`, found `foo`
 --> src/main.rs:1:6
  |
1 | func foo() -> i32 {
  |      ^^^ expected one of `!` or `::`

error: could not compile `playground` due to previous error

Ideally the error message should suggest replacing func or function with fn.

@Rageking8 Rageking8 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 Jul 26, 2022
@EagleTw
Copy link

EagleTw commented Jul 26, 2022

@rustbot claim

@EagleTw
Copy link

EagleTw commented Jul 28, 2022

New to rustc community and wants to contribute code.

  1. Are there any options to debug rustc which line of which file the error abort happens?
  2. How can I dump the hierarchy tree for debugging use?
  3. In C++ we usually use env for compiler debugging. How do rustc often use?

Very newbie questions, thank you in advance.

@gimbling-away
Copy link
Contributor

gimbling-away commented Jul 29, 2022

@rustbot label +E-easy +E-mentor

@ypprog Welcome to Rust! ✨

This might be some help --
This issue is very similar to #99903, so take a peek at the diff there. IMO the following tokens are enough to check against

  • function (Julia/JavaScript/etc)
  • func (Go)
  • fun (Kotlin)
  • def (Python)

Have fun! <3

@rustbot rustbot added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Jul 29, 2022
@gimbling-away
Copy link
Contributor

gimbling-away commented Jul 29, 2022

Instructions --

  • Add a check if prev_token is either function/func/fun/etc here
  • Emit a message, something along the lines of "use fn instead of <prev_token> to make a function" using err.span_suggestion_short, there are a few usages of this in the same file so those might be good examples.
  • Add tests for this in src/test/ui/parser/ -- Ideally each suggestion would have a test.

Should be helpful to take #99903 as a reference

@Rageking8
Copy link
Contributor Author

We can include def if necessary.

@Rageking8 Rageking8 changed the title Suggest fn if func or function is used instead Suggest fn if fun, func, function or def is used instead Jul 29, 2022
@adam-tokarski
Copy link

Hi guys, another rust newbie looking for something easy to get started here. @ypprog are you still on this or could go with my attempt here?

@gimbling-away
Copy link
Contributor

@adam-tokarski ypprog does seem to be active and it hasn't been that long, if you'd like you can pick another issue. =)

Try going through the E-Easy and/or E-Mentor category for easy issues. However, to clarify, if an issue which is claimed doesn't seem to have any work done for a long period of time (A month or so is a good amount of time) -- Feel free to ping the assignee to confirm and handle it accordingly.

@bindsdev
Copy link
Contributor

bindsdev commented Aug 4, 2022

With these changes, I also think it is important to implement improved error messages for when a user puts the return type at the start of the function instead of the end.

Given this code:

u32 foo() {
    100
}

The error output is:

error: expected one of `!` or `::`, found `foo`
 --> src/main.rs:1:5
  |
1 | u32 foo() {
  |     ^^^ expected one of `!` or `::`

error: could not compile `playground` due to previous error

@gimbling-away
Copy link
Contributor

I feel like adding auto might also be helpful for C++ developers :)

@bindsdev
Copy link
Contributor

Is this still being worked on? @ypprog

@EagleTw
Copy link

EagleTw commented Aug 13, 2022

@rustbot release-assignment

@bindsdev
Copy link
Contributor

@rustbot claim

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 E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants