Zilliqa / scilla

Scilla - A Smart Contract Intermediate Level Language
https://scilla-lang.org
GNU General Public License v3.0
240 stars 79 forks source link

Report unreachable pattern matching arms for unused ADT constructors #1109

Closed jubnzv closed 2 years ago

jubnzv commented 2 years ago

DeadCodeDetector should report unreachable pattern matching arms that check unused ADT constructors.

Here is an example:

scilla_version 0

library Dead6

(* OtherError is dead. *)
type Error =
  | InternalError
  | OtherError

let mk_error_event =
  fun (err: Error) =>
    let err_code =
      match err with
        | InternalError => Uint32 1
        | OtherError => Uint32 2 (* <-- Add a warning here *)
    end in
    { _eventname: "Error"
    ; err_code: err_code }

contract Dead6()

(* Procedure receives an `Error` type, but this is a constant defined in
the transition. So, OtherError is dead. *)
procedure Event(err : Error)
  err_e = mk_error_event err;
  event err_e
end

transition dummy ()
  error = InternalError;
  Event error
end

We should report an arm in OtherError. Otherwise, the warning for unused ADT constructor seems confusing, because the user sees its use in mk_error_event. But they don't see that this code is dead, so, most likely they'll think that the warning is a false positive.