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

Emit warning when variable is being expanded to function call #3517

Merged
merged 1 commit into from
Jun 11, 2016

Conversation

lexmag
Copy link
Member

@lexmag lexmag commented Jul 21, 2015

Closes #3268.

@josevalim
Copy link
Member

I think the goal is to warn only if inside a function, i.e. E#elixir_env.function is not nil.

@@ -313,6 +313,8 @@ expand({Name, Meta, Kind} = Var, #{vars := Vars} = E) when is_atom(Name), is_ato
compile_error(Meta, ?m(E, file), "expected var \"~ts\"~ts to expand to an existing variable "
"or be part of a match", [Name, elixir_scope:context_info(Kind)]);
true ->
Message = io_lib:format("variable ~ts is used as function call", [Name]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add to the error message: ", please use parentheses to remove the ambiguity and solve this warning"

@lexmag lexmag force-pushed the var-fun-warn branch 2 times, most recently from 52697ad to 1ef9c99 Compare July 21, 2015 12:50
@josevalim
Copy link
Member

@lexmag just a heads up: let's move this to 1.3. There are already quite some changes in 1.1 and 1.2 is meant to be completely compatible. So 1.3 is when we can start pushing this.

@lexmag lexmag added this to the v1.3.0 milestone Dec 29, 2015
@lexmag lexmag modified the milestones: v1.4.0, v1.3.0 Mar 17, 2016
@lexmag lexmag force-pushed the var-fun-warn branch 6 times, most recently from 5b2317f to f201ee8 Compare June 11, 2016 15:49
@lexmag lexmag changed the title [WIP] Warn if variable is used as function call Emit warning when variable is being expanded to function call Jun 11, 2016
@lexmag
Copy link
Member Author

lexmag commented Jun 11, 2016

I'm still wondering if it'll be considered inconsistent if we emit this warning only inside functions.

@josevalim
Copy link
Member

josevalim commented Jun 11, 2016

What do you mean by only inside functions? We should always emit it,
regardless of __ENV__.function is nil or not, no?

@lexmag
Copy link
Member Author

lexmag commented Jun 11, 2016

Yes, but previously (Jul 21, 2015) we thought "to warn only if inside a function".

The only downside of not checking if "function isn't nil" is the following:
screen shot 2016-06-11 at 18 32 44

@josevalim
Copy link
Member

:shipit:

@lexmag lexmag merged commit 96da837 into elixir-lang:master Jun 11, 2016
@lexmag lexmag deleted the var-fun-warn branch June 11, 2016 20:31
@danhper
Copy link

danhper commented Jun 22, 2016

When compiling a project using the master branch, I found out this warning, which I think is good in almost all cases.
However, I am a little concerned about DSLs.
For example, to define an Ecto schema, this forces to write:

  schema "my_schema" do
    field :my_field, :string

    timestamps()
  end

which I think does not look very DSL like.
So basically I think this change would remove the syntax-sugar for all booleans attributes in DSLs.

I did not look at the implementation details yet, but do you think it would be possible and worth to handle such a case?

@whatyouhide
Copy link
Member

@tuvistavie timestamps in the DLS you showed presents the same problem that this warning is trying to solve: if you have timestamps = :foo a few lines up, it will override your timestamp function (macro) call. Personally I don't think it's worth to handle such a case (provided it is possible).

@danhper
Copy link

danhper commented Jun 22, 2016

timestamps in the DLS you showed presents the same problem

I understand you point, and the point of this warning,
but often a DSL is used as something declarative,
where overriding something by mistake is very unlikely to happen.
For example, let's say we have a DSL to write HTML.

html do
  body do
    h1 "foobar"
    br()
  end
end

Having to make an explicit function call here kind of looses the beauty of the DSL in my opinion.

@josevalim
Copy link
Member

@tuvistavie it depends on the DSL. Some DSLs will bypass this warning by simply annotating those variables as generated or by translating the AST. For example, foo |> bar |> baz won't warn because bar is never really called as is.

@danhper
Copy link

danhper commented Jun 22, 2016

@josevalim Thank you for the explanation.
So I suppose the best thing to do is to handle this at the DSL level
by changing these kind of calls into macros and making sure we don't trigger the warning, right?

@josevalim
Copy link
Member

Yes, that's a possibility, definitely. We will continue collecting feedback on this in any case. :)

@voltidev
Copy link

I use Elixir 1.3.2 and I don't see the warning when I try this code:

defmodule WarningTest do
  defp do_stuff, do: 'stuff'

  def my_func do
    do_stuff
    a = do_stuff
  end
end

@lexmag
Copy link
Member Author

lexmag commented Aug 14, 2016

@ivanvotti this warning will be part of Elixir v1.4.0.

@hellerve
Copy link

hellerve commented Feb 1, 2017

Late to the party, but I cannot seem to find this in the v1.4.0 changelog. Is there any reason this was omitted?

@josevalim
Copy link
Member

[Kernel] Warn if variable is used as a function call

https://github.com/elixir-lang/elixir/blob/v1.4/CHANGELOG.md

@hellerve
Copy link

hellerve commented Feb 1, 2017

Okay, searched for bare words and arity. Sorry for the noise, then.

@josevalim
Copy link
Member

No problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants