Bug 94492 - no way to silence -Wdeprecated-copy for aggregates
Summary: no way to silence -Wdeprecated-copy for aggregates
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: 12.0
Assignee: Jason Merrill
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2020-04-05 20:13 UTC by Nikita Kniazev
Modified: 2022-07-06 20:42 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-04-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nikita Kniazev 2020-04-05 20:13:24 UTC
There is no way to silence the warning without making a type non-aggregate.
A simplified case from Boost.Proto:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-copy"
struct expr
{
    int a, b;
    expr& operator=(const expr&) { return *this; }
};
#pragma GCC diagnostic pop

#include <type_traits>
static_assert(std::is_aggregate_v<expr>);

expr foo(expr e)
{
    return e;
}

<source>: In function 'expr foo(expr)':
<source>:15:12: error: implicitly-declared 'constexpr expr::expr(const expr&)' is deprecated [-Werror=deprecated-copy]
   15 |     return e;
      |            ^
<source>:6:11: note: because 'expr' has user-provided 'expr& expr::operator=(const expr&)'
    6 |     expr& operator=(const expr&) { return *this; }
      |           ^~~~~~~~
Comment 1 GCC Commits 2021-06-01 15:38:22 UTC
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:620cd7861e1266991c9c2a82e1e2d5f4d723ec88

commit r12-1144-g620cd7861e1266991c9c2a82e1e2d5f4d723ec88
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Apr 27 17:13:39 2021 -0400

    c++: -Wdeprecated-copy and #pragma diagnostic [PR94492]
    
    -Wdeprecated-copy was depending only on the state of the warning at the
    point where we call the function, making it hard to use #pragma diagnostic
    to suppress the warning for a particular implicitly declared function.
    
    But checking whether the warning is enabled at the location of the implicit
    declaration turned out to be a bit complicated; option_enabled only tests
    whether it was enabled at the start of compilation, the actual test only
    existed in the middle of diagnostic_report_diagnostic.  So this patch
    factors it out and adds a new warning_enabled function to diagnostic.h.
    
    gcc/ChangeLog:
    
            PR c++/94492
            * diagnostic.h (warning_enabled_at): Declare.
            * diagnostic.c (diagnostic_enabled): Factor out from...
            (diagnostic_report_diagnostic): ...here.
            (warning_enabled_at): New.
    
    gcc/cp/ChangeLog:
    
            PR c++/94492
            * decl2.c (cp_warn_deprecated_use): Check warning_enabled_at.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/94492
            * g++.dg/cpp0x/depr-copy4.C: New test.
Comment 2 Nikita Kniazev 2021-06-03 01:52:14 UTC
Could this be backported? The issue affects every release with -Wdeprecated-copy, which are GCC 9+.
Comment 3 Andrew Pinski 2021-08-11 20:41:25 UTC
Fixed.