Qqwy / elixir-type_check

TypeCheck: Fast and flexible runtime type-checking for your Elixir projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regex generator produces illegal regexes

skwerlman opened this issue · comments

When running a spectest against a spec which includes Regex.t(), the test fails because it was given an illegal regex which cannot be produced normally, such as ~r//t, where t is an illegal option

iex(7)> ~r//t
** (Regex.CompileError) invalid_option at position t
    (elixir 1.13.4) lib/regex.ex:220: Regex.compile!/2
    (elixir 1.13.4) expanding macro: Kernel.sigil_r/2
    iex:8: (file)

You can produce these regexes by doing struct manipulation, though i would argue that normal functions should not be expected to handle them

iex(8)> %{__struct__: Regex, opts: "t", re_pattern: nil, re_version: nil, source: ""}
~r//t

according to the docs, the only legal options for regexes in 1.13 are u, i, s, m, x, f, and U, so changing the generator for opts: to only generate combinations of these should fix this

Ha! Thanks for your bug report.

This was missed by me when implementing defaults for all of the types in Elixir's standard library.

We can definitely support a better generator for the Regex struct.
Besides making sure that we only use the supported options, we should also make sure that we do not try to build malformed regexes like e.g. ~r{(} which would raise a Regex.CompileError.

If someone wants to pick this up; you can look at TypeCheck.DefaultOverrides.String for inspiration on how to wrap one of the types in the standard library with a custom generator.