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

Cannot match warnings with location: :keep show the wrong location #8605

Closed
gabrielpra1 opened this issue Jan 8, 2019 · 1 comment
Closed

Comments

@gabrielpra1
Copy link

Environment

  • Erlang/OTP 21 [erts-10.2], Elixir 1.7.2 (compiled with Erlang/OTP 21)
  • Operating system: Ubuntu 16.04

Current behavior

When using location: :keep, "cannot match" warnings are generated with the file name where the macro is defined but line number where the macro is called.

# bug_test.ex
defmodule BugTest do
  defmacro generate_function() do
    quote location: :keep do
      def unquote(:test)() do # Line 4
        :ok
      end
    end
  end
end
# test.exs
test "bug" do
  defmodule Test do
    require BugTest
    BugTest.generate_function # Line 113
    BugTest.generate_function # Line 114
  end
end

Generates the warning

warning: this clause cannot match because a previous clause at line 113 always matches
  lib/bug_test.ex:114

If the macro is defined without location: :keep, the warning is

warning: this clause cannot match because a previous clause at line 113 always matches
  test/test.exs:114

Expected behavior

I'm not sure if this is a bug or my misunderstanding, but the first warning looks weird, since the lines 113 and 114 don't even exist in bug_test.ex.

I understand that it's tricky to show a proper warning message here because the issue comes from calling the macro twice, so we need to reference this. However, I think a clearer warning message would be something like:

warning: this clause cannot match because a previous clause at line 113 in test/text.exs always matches
  lib/bug_test.ex:4

or simply

warning: this clause cannot match because a previous clause at line 113 always matches
  lib/bug_test.ex:4

That way, if we had multiple functions being generated by the generate_function macro we could see which function cannot be matched.

@josevalim
Copy link
Member

Unfortunately there is not much we can do in this case. This error happens from a pass in the compiler that is very far removed from the original code, so any extra information is lost. The location: :keep affects the runtime behaviour but the definition code uses the caller information, otherwise we would actually report:

warning: this clause cannot match because a previous clause at line 4 always matches
lib/bug_test.ex:4

Which is possibly more confusing.

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

No branches or pull requests

2 participants