@@ -125,11 +125,26 @@ defmodule Module do
125125 The Mix compiler automatically looks for calls to deprecated modules
126126 and emit warnings during compilation, computed via `mix xref warnings`.
127127
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.
133148
134149 We recommend using this feature with care, especially library authors.
135150 Deprecating code always pushes the burden towards library users. We
@@ -181,7 +196,7 @@ defmodule Module do
181196
182197 Note that since the compiler also defines some additional metadata,
183198 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 `.
185200
186201 ### `@dialyzer`
187202
@@ -1336,8 +1351,7 @@ defmodule Module do
13361351 defp doc_key ( :defmacro ) , do: :macro
13371352
13381353 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 )
13411355 doc_meta = get_doc_meta ( doc_meta , set )
13421356 add_defaults_count ( doc_meta , defaults )
13431357 end
@@ -1349,13 +1363,6 @@ defmodule Module do
13491363 end
13501364 end
13511365
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-
13591366 defp compile_deprecated ( doc_meta , set , bag , name , arity , defaults ) do
13601367 case :ets . take ( set , :deprecated ) do
13611368 [ { :deprecated , reason , _ } ] when is_binary ( reason ) ->
@@ -1764,7 +1771,7 @@ defmodule Module do
17641771 # We do not insert into the :attributes key in the bag table
17651772 # because those attributes are deleted on every definition.
17661773 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
17681775 try do
17691776 :ets . lookup_element ( set , key , 3 )
17701777 catch
@@ -1812,7 +1819,7 @@ defmodule Module do
18121819 { line , doc } when is_integer ( line ) ->
18131820 raise ArgumentError ,
18141821 "@#{ 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 ) } "
18161823
18171824 _other ->
18181825 raise ArgumentError ,
@@ -1878,13 +1885,6 @@ defmodule Module do
18781885 "dependencies. It should be a string the path to a file, got: #{ inspect ( value ) } "
18791886 end
18801887
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-
18881888 defp preprocess_attribute ( :deprecated , value ) when not is_binary ( value ) do
18891889 raise ArgumentError ,
18901890 "@deprecated is a built-in module attribute that annotates a definition as deprecated. " <>
@@ -1902,7 +1902,7 @@ defmodule Module do
19021902 _ ->
19031903 raise ArgumentError ,
19041904 "@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, " <>
19061906 "got: #{ inspect ( value ) } "
19071907 end
19081908 end
@@ -1914,8 +1914,8 @@ defmodule Module do
19141914 defp preprocess_doc_meta ( [ ] , _module , _line , map ) , do: map
19151915
19161916 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 ) } "
19191919 IO . warn ( message , attribute_stack ( module , line ) )
19201920 preprocess_doc_meta ( tail , module , line , map )
19211921 end
@@ -1927,9 +1927,14 @@ defmodule Module do
19271927
19281928 defp validate_doc_meta ( :since , value ) when not is_binary ( value ) do
19291929 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 ) } "
19331938 end
19341939
19351940 defp validate_doc_meta ( _ , _ ) , do: :ok
0 commit comments