@@ -125,11 +125,26 @@ defmodule Module do
125
125
The Mix compiler automatically looks for calls to deprecated modules
126
126
and emit warnings during compilation, computed via `mix xref warnings`.
127
127
128
- The `@deprecated` attribute may also be used to annotate callbacks or
129
- types. In these cases the annotation is only informational and doesn't
130
- come with compile time checks. In any case, the annotation will also
131
- become part of the documentation metadata to be used by tools like
132
- ExDoc or IEx.
128
+ Using the `@deprecated` attribute will also be reflected in the
129
+ documentation of the given function and macro. You can choose between
130
+ the `@deprecated` attribute and the documentation metadata to provide
131
+ hard-deprecations (with warnings) and soft-deprecations (with warnings):
132
+
133
+ This is a soft-deprecation as it simply annotates the documentation
134
+ as deprecated:
135
+
136
+ @doc deprecated: "Use Kernel.length/1 instead"
137
+ def size(keyword)
138
+
139
+ This is a hard-deprecation as it emits warnings and annotates the
140
+ documentation as deprecated:
141
+
142
+ @deprecated "Use Kernel.length/1 instead"
143
+ def size(keyword)
144
+
145
+ Currently `@deprecated` only supports functions and macros. However
146
+ you can use the `:deprecated` key in the annotation metadata to
147
+ annotate the docs of modules, types and callbacks too.
133
148
134
149
We recommend using this feature with care, especially library authors.
135
150
Deprecating code always pushes the burden towards library users. We
@@ -181,7 +196,7 @@ defmodule Module do
181
196
182
197
Note that since the compiler also defines some additional metadata,
183
198
there are a few reserved keys that will be ignored and warned if used.
184
- Currently these are: `:opaque`, `:defaults`, and `:deprecated `.
199
+ Currently these are: `:opaque` and `:defaults `.
185
200
186
201
### `@dialyzer`
187
202
@@ -1336,8 +1351,7 @@ defmodule Module do
1336
1351
defp doc_key ( :defmacro ) , do: :macro
1337
1352
1338
1353
defp compile_doc_meta ( set , bag , name , arity , defaults ) do
1339
- doc_meta = compile_since ( % { } , set )
1340
- doc_meta = compile_deprecated ( doc_meta , set , bag , name , arity , defaults )
1354
+ doc_meta = compile_deprecated ( % { } , set , bag , name , arity , defaults )
1341
1355
doc_meta = get_doc_meta ( doc_meta , set )
1342
1356
add_defaults_count ( doc_meta , defaults )
1343
1357
end
@@ -1349,13 +1363,6 @@ defmodule Module do
1349
1363
end
1350
1364
end
1351
1365
1352
- defp compile_since ( doc_meta , table ) do
1353
- case :ets . take ( table , :since ) do
1354
- [ { :since , since , _ } ] when is_binary ( since ) -> Map . put ( doc_meta , :since , since )
1355
- _ -> doc_meta
1356
- end
1357
- end
1358
-
1359
1366
defp compile_deprecated ( doc_meta , set , bag , name , arity , defaults ) do
1360
1367
case :ets . take ( set , :deprecated ) do
1361
1368
[ { :deprecated , reason , _ } ] when is_binary ( reason ) ->
@@ -1764,7 +1771,7 @@ defmodule Module do
1764
1771
# We do not insert into the :attributes key in the bag table
1765
1772
# because those attributes are deleted on every definition.
1766
1773
defp put_attribute ( module , key , value , line , set , _bag )
1767
- when key in [ :doc , :typedoc , :moduledoc , :impl , :since , : deprecated] do
1774
+ when key in [ :doc , :typedoc , :moduledoc , :impl , :deprecated ] do
1768
1775
try do
1769
1776
:ets . lookup_element ( set , key , 3 )
1770
1777
catch
@@ -1812,7 +1819,7 @@ defmodule Module do
1812
1819
{ line , doc } when is_integer ( line ) ->
1813
1820
raise ArgumentError ,
1814
1821
"@#{ key } is a built-in module attribute for documentation. It should be " <>
1815
- "a binary , boolean, keyword list, or nil, got: #{ inspect ( doc ) } "
1822
+ "a string , boolean, keyword list, or nil, got: #{ inspect ( doc ) } "
1816
1823
1817
1824
_other ->
1818
1825
raise ArgumentError ,
@@ -1878,13 +1885,6 @@ defmodule Module do
1878
1885
"dependencies. It should be a string the path to a file, got: #{ inspect ( value ) } "
1879
1886
end
1880
1887
1881
- defp preprocess_attribute ( :since , value ) when not is_binary ( value ) do
1882
- raise ArgumentError ,
1883
- "@since is a built-in module attribute used for documentation purposes. " <>
1884
- "It should be a string representing the version a function, macro, type or " <>
1885
- "callback was added, got: #{ inspect ( value ) } "
1886
- end
1887
-
1888
1888
defp preprocess_attribute ( :deprecated , value ) when not is_binary ( value ) do
1889
1889
raise ArgumentError ,
1890
1890
"@deprecated is a built-in module attribute that annotates a definition as deprecated. " <>
@@ -1902,7 +1902,7 @@ defmodule Module do
1902
1902
_ ->
1903
1903
raise ArgumentError ,
1904
1904
"@file is a built-in module attribute that annotates the file and line the next " <>
1905
- "definition comes from. It should be a string or a {string, line} tuple as value, " <>
1905
+ "definition comes from. It should be a string or {string, line} tuple as value, " <>
1906
1906
"got: #{ inspect ( value ) } "
1907
1907
end
1908
1908
end
@@ -1914,8 +1914,8 @@ defmodule Module do
1914
1914
defp preprocess_doc_meta ( [ ] , _module , _line , map ) , do: map
1915
1915
1916
1916
defp preprocess_doc_meta ( [ { key , _ } | tail ] , module , line , map )
1917
- when key in [ :opaque , :defaults , :deprecated ] do
1918
- message = "ignoring reserved documentation metadata key: : #{ key } "
1917
+ when key in [ :opaque , :defaults ] do
1918
+ message = "ignoring reserved documentation metadata key: #{ inspect ( key ) } "
1919
1919
IO . warn ( message , attribute_stack ( module , line ) )
1920
1920
preprocess_doc_meta ( tail , module , line , map )
1921
1921
end
@@ -1927,9 +1927,14 @@ defmodule Module do
1927
1927
1928
1928
defp validate_doc_meta ( :since , value ) when not is_binary ( value ) do
1929
1929
raise ArgumentError ,
1930
- ":since is a built-in documentation metadata key. " <>
1931
- "It should be a string representing the version a function, macro, type or " <>
1932
- "callback was added, got: #{ inspect ( value ) } "
1930
+ ":since is a built-in documentation metadata key. It should be a string representing " <>
1931
+ "the version in which the documented entity was added, got: #{ inspect ( value ) } "
1932
+ end
1933
+
1934
+ defp validate_doc_meta ( :deprecated , value ) when not is_binary ( value ) do
1935
+ raise ArgumentError ,
1936
+ ":deprecated is a built-in documentation metadata key. It should be a string " <>
1937
+ "representing the replacement for the deprecated entity, got: #{ inspect ( value ) } "
1933
1938
end
1934
1939
1935
1940
defp validate_doc_meta ( _ , _ ) , do: :ok
0 commit comments