Bug 55352 - [4.7/4.8 Regression] Erroneous gfortran warning of unused module variable when variable is only used in namelist
Summary: [4.7/4.8 Regression] Erroneous gfortran warning of unused module variable whe...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.7.2
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-11-16 13:42 UTC by AstroFloyd
Modified: 2012-11-23 19:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-11-16 00:00:00


Attachments
Minimal example source code (212 bytes, text/x-fortran)
2012-11-16 13:42 UTC, AstroFloyd
Details
Verbose output from compilation of example source file (939 bytes, text/plain)
2012-11-16 13:43 UTC, AstroFloyd
Details
My adaptation of the patch in #3 (785 bytes, patch)
2012-11-18 17:53 UTC, AstroFloyd
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description AstroFloyd 2012-11-16 13:42:22 UTC
Created attachment 28709 [details]
Minimal example source code

gfortran-4.7.2 -Wall gives a warning about unused module variables when those variables are only used in a namelist.

Test: compile the attached minimal example source file test.f90 with:
gfortran -v -Wall -Werror test.f90 -o test

The following warning is printed:
test.f90:14.6:

  use data, only: a
      1
Warning: Unused module variable 'a' which has been explicitly imported at (1)

The warning goes away when print*,a is uncommented in line 20.  Verbose output is in the attached file compile_output.txt


Get:     warning about unused module variable
Expect:  no warning
Reproduceable:  always
gfortran version: gcc version 4.7.2 (Gentoo 4.7.2 p1.3, pie-0.5.5)
Comment 1 AstroFloyd 2012-11-16 13:43:25 UTC
Created attachment 28710 [details]
Verbose output from compilation of example source file
Comment 2 janus 2012-11-16 16:31:27 UTC
Confirmed.

I guess we should either set attr.referenced in gfc_match_namelist (match.c), or check for attr.in_namelist in generate_local_decl (trans-decl.c).
Comment 3 janus 2012-11-16 21:43:34 UTC
(In reply to comment #2)
> I guess we should either set attr.referenced in gfc_match_namelist (match.c),
> or check for attr.in_namelist in generate_local_decl (trans-decl.c).

The latter is what we do for related cases. Proposed patch:

Index: gcc/fortran/trans-decl.c
===================================================================
--- gcc/fortran/trans-decl.c	(revision 193567)
+++ gcc/fortran/trans-decl.c	(working copy)
@@ -4589,23 +4589,26 @@ generate_local_decl (gfc_symbol * sym)
 	}
 
       /* Warn for unused variables, but not if they're inside a common
-	 block, a namelist, or are use-associated.  */
+	 block or a namelist.  */
       else if (warn_unused_variable
-	       && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark
-		    || sym->attr.in_namelist))
+	       && !(sym->attr.in_common || sym->mark || sym->attr.in_namelist))
 	{
-	  gfc_warning ("Unused variable '%s' declared at %L", sym->name,
-		       &sym->declared_at);
-	  if (sym->backend_decl != NULL_TREE)
-	    TREE_NO_WARNING(sym->backend_decl) = 1;
+	  if (sym->attr.use_only)
+	    {
+	      gfc_warning ("Unused module variable '%s' which has been "
+			   "explicitly imported at %L", sym->name,
+			   &sym->declared_at);
+	      if (sym->backend_decl != NULL_TREE)
+		TREE_NO_WARNING(sym->backend_decl) = 1;
+	    }
+	  else if (!sym->attr.use_assoc)
+	    {
+	      gfc_warning ("Unused variable '%s' declared at %L",
+			   sym->name, &sym->declared_at);
+	      if (sym->backend_decl != NULL_TREE)
+		TREE_NO_WARNING(sym->backend_decl) = 1;
+	    }
 	}
-      else if (warn_unused_variable && sym->attr.use_only)
-	{
-	  gfc_warning ("Unused module variable '%s' which has been explicitly "
-		       "imported at %L", sym->name, &sym->declared_at);
-	  if (sym->backend_decl != NULL_TREE)
-	    TREE_NO_WARNING(sym->backend_decl) = 1;
-	}
 
       /* For variable length CHARACTER parameters, the PARM_DECL already
 	 references the length variable, so force gfc_get_symbol_decl
Comment 4 janus 2012-11-17 10:55:13 UTC
(In reply to comment #3)
> Proposed patch:

... regtests cleanly.
Comment 5 AstroFloyd 2012-11-18 17:53:36 UTC
Created attachment 28726 [details]
My adaptation of the patch in #3

This solves the problem for me, thank you very much - I'm impressed by your quick and competent work :-)

Somehow, your patch didn't work in my Gentoo version; I applied your changes manually and created the attached patch, which can be added to the Gentoo ebuild for sys-devel/gcc-4.7.2.
Comment 6 janus 2012-11-18 19:16:27 UTC
(In reply to comment #5)
> This solves the problem for me, thank you very much

You're welcome ...


> I'm impressed by your quick and competent work :-)

Thanks! In terms of gfortran bugs, this is certainly one of the easier ones to fix.


> Somehow, your patch didn't work in my Gentoo version;

My patch was against trunk, so it might not apply cleanly to the 4.7 branch.


The fix will presumably make it into the 4.8 release, but will probably not be backported to 4.7 (unless you can show that the bug is a regression, i.e. did not occur in a previous release of gfortran - I haven't checked this).
Comment 7 janus 2012-11-19 09:24:53 UTC
(In reply to comment #6)
> The fix will presumably make it into the 4.8 release, but will probably not be
> backported to 4.7 (unless you can show that the bug is a regression, i.e. did
> not occur in a previous release of gfortran - I haven't checked this).

Just checked: The bogus warning with -Wall reported here seems to be a 4.7 regression indeed. I verified that it does not occur with 4.3.4, 4.5.3 and 4.6.0.
Comment 8 janus 2012-11-21 22:20:05 UTC
Author: janus
Date: Wed Nov 21 22:19:51 2012
New Revision: 193711

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=193711
Log:
2012-11-21  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/55352
	* trans-decl.c (generate_local_decl): Don't warn for explicitly imported
	but unused module variables which are in a namelist or common block.

2012-11-21  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/55352
	* gfortran.dg/namelist_76.f90: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/namelist_76.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 janus 2012-11-23 19:05:20 UTC
Author: janus
Date: Fri Nov 23 19:05:14 2012
New Revision: 193766

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=193766
Log:
2012-11-23  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/55352
	* trans-decl.c (generate_local_decl): Don't warn for explicitly imported
	but unused module variables which are in a namelist or common block.

2012-11-23  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/55352
	* gfortran.dg/namelist_76.f90: New.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/gfortran.dg/namelist_76.f90
Modified:
    branches/gcc-4_7-branch/gcc/fortran/ChangeLog
    branches/gcc-4_7-branch/gcc/fortran/trans-decl.c
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
Comment 10 janus 2012-11-23 19:10:07 UTC
The patch in comment 3 has been applied to trunk and the 4.7 branch, which means the bug will be fixed in the 4.7.3 and 4.8.0 releases. Closing as fixed.

Thanks for the bugreport!