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

Error message for variable with same name as function could be more explicit #35126

Closed
antoine-levitt opened this issue Mar 16, 2020 · 4 comments · Fixed by #51979
Closed

Error message for variable with same name as function could be more explicit #35126

antoine-levitt opened this issue Mar 16, 2020 · 4 comments · Fixed by #51979
Labels
error messages Better, more actionable error messages

Comments

@antoine-levitt
Copy link
Contributor

julia> f(x) = 2
f (generic function with 1 method)

julia> let
       f = f(2)
       end
ERROR: UndefVarError: f not defined
Stacktrace:
 [1] top-level scope at REPL[7]:2
@StefanKarpinski
Copy link
Member

StefanKarpinski commented Mar 16, 2020

The outer function definition is a red herring here though—it's irrelevant. The issue is that inside of the let block it's an undefined local variable and you're accessing it before it's defined.

@antoine-levitt
Copy link
Contributor Author

Sure, the problem is that the variable shadows the function name. If I write g = f(2) I'm fine, and if I write f = g(2) it's OK too, it's just this particular conflicting case that's a bit tricky to find out. This is pretty frequent in numerical code, when you use quantity(x) as a function that computes quantity, and so you want to do quantity = quantity(x); quantity + quantity^2 or something.

@antoine-levitt
Copy link
Contributor Author

To clarify: I know this is invalid code and I understand why julia is giving this message, I just wish it could be more helpful

@vtjnash
Copy link
Member

vtjnash commented Mar 16, 2020

Duplicate of #17062

@vtjnash vtjnash marked this as a duplicate of #17062 Mar 16, 2020
@brenhinkeller brenhinkeller added the error messages Better, more actionable error messages label Nov 20, 2022
vtjnash added a commit that referenced this issue Oct 31, 2023
Record the 'scope' of the variable that was undefined (the Module, or a
descriptive word such as :local or :static_parameter). Add that scope to
the error message, and expand the hint suggestions added by the REPL to
include more specific advice on common mistakes:

  - forgetting to set an initial value
  - forgetting to import a global
  - creating a local of the same name as a global
  - not matching a static parameter in a signature subtype

Fixes #17062 (although more could probably be done to search for typos using REPL.string_distance and getting the method from stacktrace)
Fixes #18877
Fixes #25263
Fixes #35126
Fixes #39280
Fixes #41728
Fixes #48731
Fixes #49917
Fixes #50369
vtjnash added a commit that referenced this issue Nov 8, 2023
Record the 'scope' of the variable that was undefined (the Module, or a
descriptive word such as :local or :static_parameter). Add that scope to
the error message, and expand the hint suggestions added by the REPL to
include more specific advice on common mistakes:

  - forgetting to set an initial value
  - forgetting to import a global
  - creating a local of the same name as a global
  - not matching a static parameter in a signature subtype

Fixes #17062 (although more could probably be done to search for typos using REPL.string_distance and getting the method from stacktrace)
Fixes #18877
Fixes #25263
Fixes #35126
Fixes #39280
Fixes #41728
Fixes #48731
Fixes #49917
Fixes #50369
vtjnash added a commit that referenced this issue Nov 8, 2023
Record the 'scope' of the variable that was undefined (the Module, or a
descriptive word such as :local or :static_parameter). Add that scope to
the error message, and expand the hint suggestions added by the REPL to
include more specific advice on common mistakes:

  - forgetting to set an initial value
  - forgetting to import a global
  - creating a local of the same name as a global
  - not matching a static parameter in a signature subtype

Fixes #17062 (although more could probably be done to search for typos
using REPL.string_distance and getting the method from stacktrace)
Fixes #18877
Fixes #25263
Fixes #35126
Fixes #39280
Fixes #41728
Fixes #48731
Fixes #49917
Fixes #50369
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error messages Better, more actionable error messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants