File 3191-dialyzer-Add-option-to-be-used-with-attribute-dialyz.patch of Package erlang

From 672299bb104f34b5a47b7d3334d798f121070fa8 Mon Sep 17 00:00:00 2001
From: Hans Bolinder <hasse@erlang.org>
Date: Mon, 15 Feb 2021 12:11:12 +0100
Subject: [PATCH] dialyzer: Add option to be used with attribute -dialyzer

Add option 'no_underspecs'. See also GH-4033.
---
 lib/dialyzer/doc/src/dialyzer.xml             | 30 ++++++++-
 lib/dialyzer/src/dialyzer.hrl                 |  1 +
 lib/dialyzer/src/dialyzer_cl_parse.erl        |  6 ++
 lib/dialyzer/src/dialyzer_options.erl         |  2 +
 .../test/options1_SUITE_data/results/remote   |  9 +++
 .../src/remote/remotes1.erl                   | 67 +++++++++++++++++++
 .../src/remote/some_known_remote.erl          |  5 ++
 .../src/remote/remotes1.erl                   |  4 ++
 lib/stdlib/src/erl_lint.erl                   |  4 +-
 9 files changed, 125 insertions(+), 3 deletions(-)
 create mode 100644 lib/dialyzer/test/options1_SUITE_data/results/remote
 create mode 100644 lib/dialyzer/test/options1_SUITE_data/src/remote/remotes1.erl
 create mode 100644 lib/dialyzer/test/options1_SUITE_data/src/remote/some_known_remote.erl

diff --git a/lib/dialyzer/doc/src/dialyzer.xml b/lib/dialyzer/doc/src/dialyzer.xml
index 030e838cd6..cc797193ad 100644
--- a/lib/dialyzer/doc/src/dialyzer.xml
+++ b/lib/dialyzer/doc/src/dialyzer.xml
@@ -413,6 +413,22 @@ dialyzer --plts plt_1 ... plt_n -- files_to_analyze</code>
       <p>*** denotes options that turn on warnings rather than
         turning them off.</p>
     </note>
+
+    <p>The following option is not strictly needed as it specifies
+      the default. It is primarily intended to be used with the
+      <c>-dialyzer</c> attribute. For an example see section <seealso
+      marker="#suppression">Requesting or Suppressing Warnings in
+      Source Files</seealso>.</p>
+
+    <taglist>
+      <tag><c>-Wno_underspecs</c></tag>
+      <item>
+        <p>Suppress warnings about underspecified functions (the
+          specification is strictly more allowing than the success typing).
+          </p>
+      </item>
+    </taglist>
+
   </section>
 
   <section>
@@ -475,6 +491,16 @@ dialyzer --plts plt_1 ... plt_n -- files_to_analyze</code>
     <code type="none">
 -dialyzer({[no_return, no_match], [g/0, h/0]}).</code>
 
+    <p>The warning option for underspecified functions, <c>-Wunderspecs</c>,
+      can result in useful warnings, but often functions with
+      specifications that are strictly more allowing than the success
+      typing cannot easily be modified to be less allowing. To turn
+      off the warning for underspecified function <c>f/0</c>,
+      include the following line:</p>
+
+    <code type="none">
+-dialyzer({no_underspecs, f/0}).</code>
+
     <p>For help on the warning options, use <c>dialyzer -Whelp</c>. The
       options are also enumerated, see function <seealso marker="#gui/1">
       <c>gui/1</c></seealso> below (<c>WarnOpts</c>).</p>
diff --git a/lib/dialyzer/src/dialyzer_cl_parse.erl b/lib/dialyzer/src/dialyzer_cl_parse.erl
index cefe466f01..ff7930f21a 100644
--- a/lib/dialyzer/src/dialyzer_cl_parse.erl
+++ b/lib/dialyzer/src/dialyzer_cl_parse.erl
@@ -566,4 +566,10 @@ The following options are also available but their use is not recommended:
      Warn when the -spec is different than the success typing.
 
 *** Identifies options that turn on warnings rather than turning them off.
+
+The following options are not strictly needed as they specify the default.
+They are primarily intended to be used with the -dialyzer attribute:
+  -Wno_underspecs
+     Suppress warnings about underspecified functions (those whose -spec
+     is strictly more allowing than the success typing).
 ".
diff --git a/lib/dialyzer/src/dialyzer_options.erl b/lib/dialyzer/src/dialyzer_options.erl
index 44914bf4a1..44c75d60c1 100644
--- a/lib/dialyzer/src/dialyzer_options.erl
+++ b/lib/dialyzer/src/dialyzer_options.erl
@@ -346,6 +346,8 @@ build_warnings([Opt|Opts], Warnings) ->
 	ordsets:add_element(?WARN_CONTRACT_SUBTYPE, Warnings);
       underspecs ->
 	ordsets:add_element(?WARN_CONTRACT_SUPERTYPE, Warnings);
+      no_underspecs ->
+	ordsets:del_element(?WARN_CONTRACT_SUPERTYPE, Warnings);
       unknown ->
 	ordsets:add_element(?WARN_UNKNOWN, Warnings);
       OtherAtom ->
diff --git a/lib/dialyzer/test/options1_SUITE_data/results/remote b/lib/dialyzer/test/options1_SUITE_data/results/remote
new file mode 100644
index 0000000000..63983919aa
--- /dev/null
+++ b/lib/dialyzer/test/options1_SUITE_data/results/remote
@@ -0,0 +1,9 @@
+
+remotes1.erl:19:2: The specification for remotes1:foo5/1 states that the function might also return 'ko' but the inferred return is 'ok'
+remotes1.erl:22:2: Type specification remotes1:foo6('ok' | 'ko') -> 'ok' is a supertype of the success typing: remotes1:foo6('ok') -> 'ok'
+remotes1.erl:27:2: The specification for remotes1:foo7/1 states that the function might also return 'ko' but the inferred return is 'ok'
+remotes1.erl:30:2: Type specification remotes1:foo8(local_type_42()) -> 'ok' is a supertype of the success typing: remotes1:foo8('ok') -> 'ok'
+remotes1.erl:35:2: The specification for remotes1:foo9/1 states that the function might also return 'ko' but the inferred return is 'ok'
+remotes1.erl:38:2: Type specification remotes1:foo10(local_and_known_remote_type_42()) -> 'ok' is a supertype of the success typing: remotes1:foo10('ok') -> 'ok'
+remotes1.erl:51:2: Type specification remotes1:foo13('ok') -> local_and_unknown_remote_type_42() is a supertype of the success typing: remotes1:foo13('ok') -> 'ok'
+remotes1.erl:54:2: Type specification remotes1:foo14(local_and_unknown_remote_type_42()) -> 'ok' is a supertype of the success typing: remotes1:foo14('ok') -> 'ok'
diff --git a/lib/dialyzer/test/options1_SUITE_data/src/remote/remotes1.erl b/lib/dialyzer/test/options1_SUITE_data/src/remote/remotes1.erl
new file mode 100644
index 0000000000..d471e244c8
--- /dev/null
+++ b/lib/dialyzer/test/options1_SUITE_data/src/remote/remotes1.erl
@@ -0,0 +1,67 @@
+-module(remotes1).
+
+-compile(export_all).
+
+-dialyzer(underspecs).
+
+-spec foo1(some_unknown_remote:type42()) -> ok.
+foo1(ok) -> ok.
+
+-spec foo2(ok) -> some_unknown_remote:type42().
+foo2(ok) -> ok.
+
+-spec foo3(some_known_remote:type42()) -> ok.
+foo3(ok) -> ok.
+
+-spec foo4(ok) -> some_known_remote:type42().
+foo4(ok) -> ok.
+
+-spec foo5(ok|ko) -> ok|ko.
+foo5(ok) -> ok.
+
+-spec foo6(ok|ko) -> ok.
+foo6(ok) -> ok.
+
+-type local_type_42() :: ok | ko.
+
+-spec foo7(ok) -> local_type_42().
+foo7(ok) -> ok.
+
+-spec foo8(local_type_42()) -> ok.
+foo8(ok) -> ok.
+
+-type local_and_known_remote_type_42() :: some_known_remote:type42() | ok | ko.
+
+-spec foo9(ok) -> local_and_known_remote_type_42().
+foo9(ok) -> ok.
+
+-spec foo10(local_and_known_remote_type_42()) -> ok.
+foo10(ok) -> ok.
+
+-type local_and_ok_known_remote_type_42() :: some_known_remote:type42() | ok.
+
+-spec foo11(ok) -> local_and_ok_known_remote_type_42().
+foo11(ok) -> ok.
+
+-spec foo12(local_and_ok_known_remote_type_42()) -> ok.
+foo12(ok) -> ok.
+
+-type local_and_unknown_remote_type_42() :: some_unknown_remote:type42() | ok | ko.
+
+-spec foo13(ok) -> local_and_unknown_remote_type_42().
+foo13(ok) -> ok.
+
+-spec foo14(local_and_unknown_remote_type_42()) -> ok.
+foo14(ok) -> ok.
+
+-type local_and_ok_unknown_remote_type_42() :: some_unknown_remote:type42() | ok.
+
+-spec foo15(ok) -> local_and_ok_unknown_remote_type_42().
+foo15(ok) -> ok.
+
+-spec foo16(local_and_ok_unknown_remote_type_42()) -> ok.
+foo16(ok) -> ok.
+
+-dialyzer({no_underspecs,foo17/1}).
+-spec foo17(ok|ko) -> ok.
+foo17(ok) -> ok.
diff --git a/lib/dialyzer/test/options1_SUITE_data/src/remote/some_known_remote.erl b/lib/dialyzer/test/options1_SUITE_data/src/remote/some_known_remote.erl
new file mode 100644
index 0000000000..437f1e7826
--- /dev/null
+++ b/lib/dialyzer/test/options1_SUITE_data/src/remote/some_known_remote.erl
@@ -0,0 +1,5 @@
+-module(some_known_remote).
+
+-export_type([type42/0]).
+
+-type type42() :: ok | ko.
diff --git a/lib/dialyzer/test/underspecs_SUITE_data/src/remote/remotes1.erl b/lib/dialyzer/test/underspecs_SUITE_data/src/remote/remotes1.erl
index b722495095..960ff004f6 100644
--- a/lib/dialyzer/test/underspecs_SUITE_data/src/remote/remotes1.erl
+++ b/lib/dialyzer/test/underspecs_SUITE_data/src/remote/remotes1.erl
@@ -59,3 +59,7 @@ foo15(ok) -> ok.
 
 -spec foo16(local_and_ok_unknown_remote_type_42()) -> ok.
 foo16(ok) -> ok.
+
+-dialyzer({no_underspecs,foo17/1}).
+-spec foo17(ok|ko) -> ok.
+foo17(ok) -> ok.
diff --git a/lib/stdlib/src/erl_lint.erl b/lib/stdlib/src/erl_lint.erl
index 9d4806911b..7732c585f0 100644
--- a/lib/stdlib/src/erl_lint.erl
+++ b/lib/stdlib/src/erl_lint.erl
@@ -3345,7 +3345,9 @@ is_module_dialyzer_option(Option) ->
                   no_match,no_opaque,no_fail_call,no_contracts,
                   no_behaviours,no_undefined_callbacks,unmatched_returns,
                   error_handling,race_conditions,no_missing_calls,
-                  specdiffs,overspecs,underspecs,unknown]).
+                  specdiffs,overspecs,underspecs,unknown,
+                  no_underspecs
+                 ]).
 
 %% try_catch_clauses(Scs, Ccs, In, ImportVarTable, State) ->
 %%      {UpdVt,State}.
-- 
2.26.2

openSUSE Build Service is sponsored by