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

Add opportunistic warnings for shadowed map patterns #8600

Merged

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Jun 20, 2024

The compiler can emit so-called opportunistic warnings, which are warnings emitted when the compiler notices something suspicious during optimization or code generation. The trouble with opportunistic warnings is that there is no guarantee that the compiler will notice all kind of suspicious code. For example, the compiler in Erlang/OTP 27 and earlier would not emit any warnings for the following functions:

mm_1(#{}) ->
    a;
mm_1(#{first := First}) ->
    {b,First}.

mm_2(#{first := First}) ->
    {b,First};
mm_2(#{first := First, second := Second}) ->
    {c,First,Second}.

Since it is easy to arrange clauses in the wrong order and put the matching for any map before other map patterns, it seems worthwhile to catch at least some of the map patterns that will shadow subsequent patterns.

With this pull request, the compiler will generate the following warnings:

t.erl:6:1: Warning: this clause cannot match because a previous clause at line 4 matches the same pattern as this clause
%    6| mm_1(#{first := First}) ->
%     | ^

t.erl:11:1: Warning: this clause cannot match because a previous clause at line 9 matches the same pattern as this clause
%   11| mm_2(#{first := First, second := Second}) ->
%     | ^

Note that these warnings are still opportunistic and there is no guarantee that the compiler will notice all kind of shadowed map patterns.

Resolves #8558

The compiler can emit so-called *opportunistic warnings*, which are
warnings emitted when the compiler notices something suspicious during
optimization or code generation. The trouble with opportunistic
warnings is that there is no guarantee that the compiler will notice
all kind of suspicious code. For example, the compiler in Erlang/OTP
27 and earlier would not emit any warnings for the following
functions:

    mm_1(#{}) ->
        a;
    mm_1(#{first := First}) ->
        {b,First}.

    mm_2(#{first := First}) ->
        {b,First};
    mm_2(#{first := First, second := Second}) ->
        {c,First,Second}.

Since it is easy to arrange clauses in the wrong order and put the
matching for any map before other map patterns, it seems worthwhile
to catch at least some of the map patterns that will shadow subsequent
patterns.

With this commit, the compiler will generate the following warnings:

    t.erl:6:1: Warning: this clause cannot match because a previous clause at line 4 matches the same pattern as this clause
    %    6| mm_1(#{first := First}) ->
    %     | ^

    t.erl:11:1: Warning: this clause cannot match because a previous clause at line 9 matches the same pattern as this clause
    %   11| mm_2(#{first := First, second := Second}) ->
    %     | ^

Note that these warnings are still opportunistic and there is no
guarantee that the compiler will notice all kind of shadowed map
patterns.

Resolves erlang#8558
@bjorng bjorng added team:VM Assigned to OTP team VM enhancement testing currently being tested, tag is used by OTP internal CI labels Jun 20, 2024
@bjorng bjorng requested a review from jhogberg June 20, 2024 11:42
@bjorng bjorng self-assigned this Jun 20, 2024
Copy link
Contributor

github-actions bot commented Jun 20, 2024

CT Test Results

    2 files    324 suites   10m 17s ⏱️
  816 tests   814 ✅ 2 💤 0 ❌
5 419 runs  5 417 ✅ 2 💤 0 ❌

Results for commit 8a8ea04.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@PragTob
Copy link
Contributor

PragTob commented Jun 21, 2024

Just one more time here, thanks a lot for taking the time to implement this. It's a great improvement! 💚

IMG_20220901_174517

@bjorng bjorng merged commit 2a64588 into erlang:master Jun 26, 2024
17 checks passed
@bjorng bjorng deleted the bjorn/compiler/nomatch-warnings/GH-8558/OTP-19141 branch June 26, 2024 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Idea: More warnings related to clauses that can never match (map/keys
2 participants