public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108395] New: [C2x] Bogus -Wunused-but-set-variable when returning constexpr variable
@ 2023-01-13 14:58 pexu@gcc-bugzilla.mail.kapsi.fi
  2023-01-13 20:31 ` [Bug c/108395] " pinskia at gcc dot gnu.org
  2023-01-13 22:26 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: pexu@gcc-bugzilla.mail.kapsi.fi @ 2023-01-13 14:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108395

            Bug ID: 108395
           Summary: [C2x] Bogus -Wunused-but-set-variable when returning
                    constexpr variable
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pexu@gcc-bugzilla.mail.kapsi.fi
  Target Milestone: ---

Hi.

It appears if constexpr variable, declared at block scope, is used verbatim as
a return value GCC emits a bogus -Wunused-but-set-variable.

Using version `13.0.0 20230113 (experimental)'.

# echo 'int f() { constexpr int v = 0; return v; }' | gcc -c -xc -std=c2x -Wall
-Wextra -
<stdin>: In function 'f':
<stdin>:1:25: warning: variable 'v' set but not used
[-Wunused-but-set-variable]

As a workaround any operation on v (cast, effectively-no-op arithmetics etc.)
or maybe_unused attribute (or file scope) can be used.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/108395] [C2x] Bogus -Wunused-but-set-variable when returning constexpr variable
  2023-01-13 14:58 [Bug c/108395] New: [C2x] Bogus -Wunused-but-set-variable when returning constexpr variable pexu@gcc-bugzilla.mail.kapsi.fi
@ 2023-01-13 20:31 ` pinskia at gcc dot gnu.org
  2023-01-13 22:26 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-13 20:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108395

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |89180
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-01-13
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89180
[Bug 89180] [meta-bug] bogus/missing -Wunused warnings

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/108395] [C2x] Bogus -Wunused-but-set-variable when returning constexpr variable
  2023-01-13 14:58 [Bug c/108395] New: [C2x] Bogus -Wunused-but-set-variable when returning constexpr variable pexu@gcc-bugzilla.mail.kapsi.fi
  2023-01-13 20:31 ` [Bug c/108395] " pinskia at gcc dot gnu.org
@ 2023-01-13 22:26 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-13 22:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108395

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is will most likely fix it, but I am not 100% sure it fixes all of the
constexpr cases though:
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index e06f052eb46..3a169eceece 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -2154,7 +2154,10 @@ maybe_get_constexpr_init (tree expr)
       && C_DECL_DECLARED_CONSTEXPR (decl)
       && DECL_INITIAL (decl) != NULL_TREE
       && !error_operand_p (DECL_INITIAL (decl)))
-    return DECL_INITIAL (decl);
+    {
+      mark_exp_read (decl);
+      return DECL_INITIAL (decl);
+    }
   if (TREE_CODE (expr) != COMPONENT_REF)
     return NULL_TREE;
   tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-01-13 22:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-13 14:58 [Bug c/108395] New: [C2x] Bogus -Wunused-but-set-variable when returning constexpr variable pexu@gcc-bugzilla.mail.kapsi.fi
2023-01-13 20:31 ` [Bug c/108395] " pinskia at gcc dot gnu.org
2023-01-13 22:26 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).