Bug 81676 - Wrong warning with unused-but-set-parameter within 'if constexpr'
Summary: Wrong warning with unused-but-set-parameter within 'if constexpr'
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.1.1
: P3 normal
Target Milestone: 10.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, patch
: 100956 (view as bug list)
Depends on:
Blocks: Wunused
  Show dependency treegraph
 
Reported: 2017-08-02 16:20 UTC by Benjamin Buch
Modified: 2021-12-17 04:01 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-08-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benjamin Buch 2017-08-02 16:20:47 UTC
template < typename T >
int f(T v){
    if constexpr(sizeof(T) == sizeof(int)){
        return v;
    }else{
        return 0;
    }
}

int main(){
    f(0);
    f('a');
}


This is a valid program which should not produce a warning. (Normal 'if' doesn't produce a warning either.)


$ g++ -std=c++1z -Wunused-but-set-parameter gcc-warning.cpp
gcc-warning.cpp: In instantiation of 'int f(T) [with T = char]':
gcc-warning.cpp:12:7:   required from here
gcc-warning.cpp:2:9: warning: parameter 'v' set but not used [-Wunused-but-set-parameter]
 int f(T v){
         ^

$ g++ --version
g++ (GCC) 7.1.1 20170724
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Comment 1 Marek Polacek 2017-08-03 11:26:54 UTC
Confirmed.
Comment 2 Benjamin Buch 2017-12-01 13:00:01 UTC
Does still exist in:

$ g++ --version
g++ (GCC) 8.0.0 20171201 (experimental)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Comment 3 Vittorio Romeo 2019-08-16 13:32:16 UTC
Found this today, still present in gcc trunk (9.2+). Example:

int main()
{
    auto f = [](auto a, auto b) {
        if constexpr (sizeof(b) == 1) {
            return a;
        } else {
            return b;
        }
    };

    return f(1, 1) + f(1, 'a');
}

https://gcc.godbolt.org/z/SOvLEV
Comment 4 Marek Polacek 2019-08-16 14:02:27 UTC
The problem seems to be that we're losing DECL_READ_P on the parm_decl v.
Comment 5 Marek Polacek 2019-08-16 21:31:06 UTC
Well, patch posted: https://gcc.gnu.org/ml/gcc-patches/2019-08/msg01196.html

I'm not very optimistic about it being accepted, but so far no one has submitted anything better.
Comment 6 Marek Polacek 2019-08-28 02:23:01 UTC
Author: mpolacek
Date: Wed Aug 28 02:22:29 2019
New Revision: 274982

URL: https://gcc.gnu.org/viewcvs?rev=274982&root=gcc&view=rev
Log:
	PR c++/81676 - bogus -Wunused warnings in constexpr if.
	* semantics.c (maybe_mark_exp_read_r): New function.
	(finish_if_stmt): Call it on THEN_CLAUSE and ELSE_CLAUSE.

	* g++.dg/cpp1z/constexpr-if31.C: New test.
	* g++.dg/cpp1z/constexpr-if32.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C
    trunk/gcc/testsuite/g++.dg/cpp1z/constexpr-if32.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Marek Polacek 2019-08-28 02:27:18 UTC
Fixed.
Comment 8 Jonathan Wakely 2021-06-08 12:35:32 UTC
*** Bug 100956 has been marked as a duplicate of this bug. ***