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

Using @spec! allows calling private functions

baldwindavid opened this issue · comments

I was surprised to find that adding @spec! to a private function exposes that function publicly.

defmodule Typetest do
  use TypeCheck

  @spec! hello :: any()
  defp hello, do: "world"
end

# This works...
Typetest.hello()

I noticed it when changing a public function to private and seeing that my test of the function still somehow worked.

Interesting.
This is definitely not intended behaviour.

I will ask about this upstream, because it feels to me like this (using @spec! on a private function; more specifically this internally uses defoverridable on a private function) should result in a compiler error by Elixir itself.

It turns out that Elixir itself does allow this.
So we'll need to add a check ourselves, to make sure that we keep a defp-function defp.

I saw your documentation change in core. That is good information to know!

A question about this: Do you want to add specs to private functions?
Or would you rather expect a compile-time error to be thrown when someone tries to do so?

I don't think it's too common to add them to private functions, but I wouldn't expect a compile-time error when doing so. I haven't done it, but I've at least heard some of our devs lament that it might be easier to follow some of our complex flows if there were specs even on some private functions.

Perhaps some validation that specs on private functions should be allowed in TypeCheck?... elixir-lang/elixir#3144 (comment)