Uploaded image for project: 'Erlang/OTP'
  1. Erlang/OTP
  2. ERL-1440

Compiler emits the wrong code as if variable mutated

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 23.1, OTP 24
    • Fix Version/s: OTP-23.2
    • Component/s: compiler
    • Labels:
      None

      Description

      Giventhis code:

      -module(dummy).
      -compile([no_auto_import]).
      -export([execute/1]).
      
      execute(_x@1) ->
          _a@1 = get_a(_x@1),
          _b@1 = get_b(_x@1),
          erlang:display(_b@1),
          case _a@1 /= _b@1 of
              false ->
                  nil;
              true ->
                  erlang:display(_b@1),
                  ok
          end.
      get_a(_x@1) ->
          case _x@1 == <<"A">> of
              false ->
                  2;
              true ->
                  3
          end.
      get_b(_x@1) ->
          case _x@1 == <<"A">> of
              false ->
                  1;
              true ->
                  2
          end.
      

      Compiling it and then:

      $ erl
      Erlang/OTP 24 [DEVELOPMENT] [erts-11.1.1] [source-4a42e03c79] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
      
      Eshell V11.1.1  (abort with ^G)
      1> dummy:execute(<<"A">>).
      2
      1
      ok
      

      As you see, it prints 2 and 1, but it is the same var in the same scope, so it must always be 2 and 2. After a cursory glance, it seems to be a compiler bug.

      I was able to reproduce it on master (from Nov 27th) and others mentioned it was also on 23.1.5.

        Attachments

          Activity

            People

            Assignee:
            john John Högberg
            Reporter:
            josevalim José Valim
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: