[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Jakub Jelinek changed: What|Removed |Added Status|NEW |RESOLVED Resolution|--- |INVALID --- Comment #17 from Jakub Jelinek --- Avoid decltype in that case, or use decltype on some other function that has the same arguments as malloc and same return value, just not such attributes. void *my_malloc (size_t); ... decltype (my_malloc) ...
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #16 from Milian Wolff --- So how can I silence the warning then for the case I pasted in the first comment: ~~~+ #include template struct foo {}; fooemit_unexpected_warning; int main() { return 0; } ~~~+ Most notably when you take into account that this is only sometimes required based on the optimization level. Should I simply not use decltype and instead paste the definition of malloc inline? Or is there a "silent" decltype alternative I could use?
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #15 from Martin Sebor --- As Jason explained it works as designed. But the warning is certainly confusing. It doesn't help that not all attributes enjoy this special treatment or that the manual doesn't explain it. At a minimum, I think the manual needs to be updated. It would help if the warning mentioned the names of the attributes that are being ignored and, perhaps in a note, gave a hint as to why.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #14 from Jakub Jelinek --- > I imagine it was to support using them on function pointers. Yes, that is the reason why they apply to function types rather than decls.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Jakub Jelinek changed: What|Removed |Added CC||jakub at gcc dot gnu.org --- Comment #13 from Jakub Jelinek --- So isn't this actually NOTABUG?
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Jakub Jelinek changed: What|Removed |Added Target Milestone|6.3 |6.4 --- Comment #12 from Jakub Jelinek --- GCC 6.3 is being released, adjusting target milestone.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #11 from Richard Biener --- GCC 6.2 is being released, adjusting target milestone.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Richard Biener changed: What|Removed |Added Target Milestone|6.2 |6.3 --- Comment #10 from Richard Biener --- GCC 6.2 is being released, adjusting target milestone.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #9 from Jason Merrill --- (In reply to Milian Wolff from comment #8) > As an interested bystander, may I ask: If the attribute is part of the type, > shouldn't it then be transferred via decltype() and then also used in the > template to trigger the warning there? The attribute isn't properly part of the C++ type, so in the testcase below A needs to be the same type as A, and so they share a member function. So we have to choose which type to use as the canonical type, and we choose the type without the attribute. And that's what the "ignoring attributes" warning is telling you. typedef int fn(); typedef int fna() __attribute ((warn_unused_result)); template struct A { T *p; void g() { p(); } // no warning }; int main() { A().g(); A().g(); }
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #8 from Milian Wolff --- As an interested bystander, may I ask: If the attribute is part of the type, shouldn't it then be transferred via decltype() and then also used in the template to trigger the warning there? To me, the example you gave shows a defect in GCC (no warning inside the template), and not a nice feature. What am I missing?
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #7 from Jason Merrill --- (In reply to Martin Sebor from comment #6) > I didn't know that GCC considers attribute warn_unused_result part of the > function type. When you say that most of these attributes apply to the > function type, which others are you referring to? Other attributes that apply to a function type: nonnull, returns_nonnull, sentinel, alloc_size, alloc_align, assume_aligned, format, format_arg. I don't know why they were defined this way; I imagine it was to support using them on function pointers.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 --- Comment #6 from Martin Sebor --- I didn't know that GCC considers attribute warn_unused_result part of the function type. When you say that most of these attributes apply to the function type, which others are you referring to? FWIW, I find this surprising not only because the attribute is documented as a function attribute and not one that applies to types, but also because it's different from other common function attributes that affect warnings such as attribute unused or used. Those are not considered to be part of the function type as the test case below shows. $ cat x.C && /home/msebor/build/gcc-trunk-svn/gcc/xgcc -B /home/msebor/build/gcc-trunk-svn/gcc -O2 -S -Wall -Wextra -Wpedantic -xc++ x.C static void __attribute ((unused)) f_unused (void); static void f_unused (void) { } // no warning as expected static void __attribute ((used)) f_used (void); static void f_used (void) { } // no warning as expected static __typeof__ (f_unused) g_unused; static void g_unused (void) { } // emits -Wnunused-function static __typeof__ (f_used) g_used; static void g_used (void) { } // emits -Wnunused-function x.C:11:13: warning: ‘void g_used()’ defined but not used [-Wunused-function] static void g_used (void) { } // emits -Wnunused-function ^~ x.C:8:13: warning: ‘void g_unused()’ defined but not used [-Wunused-function] static void g_unused (void) { } // emits -Wnunused-function ^~~~ I also tried a few other others, including attribute weak and attribute nothrow, but couldn't find one where the attribute is treated as part of the function type. In any case, if warn_unused_result is meant to be part of the function type then the right resolution is to update the documentation to make it clear. I'm willing to put together a documentation patch if you're convinced that's the right fix though it seems to me that changing GCC to have the attribute behave like the others above would be preferable.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Jason Merrill changed: What|Removed |Added CC||jason at gcc dot gnu.org --- Comment #5 from Jason Merrill --- (In reply to Martin Sebor from comment #4) > Second, it's unclear to me what purpose the warning is meant to serve in > this case. Since a function attribute always applies to the instance of the > function it decorates and never affects its type the warning doesn't > indicate anything unusual or unexpected, and only serves to confuse users. Actually, most of these attributes, including warn_unused_result, apply to the function type, not to the function declaration. That's why you get the warning. As an example of this: __attribute ((warn_unused_result)) int f(); decltype(f)* p; int main() { p(); // warns about unused result, by design. } The warning is letting you know that the attribute is being discarded, so you don't get the warning within a template: __attribute ((warn_unused_result)) int f(); template struct A { T *p; void g() { p(); } // no warning }; int main() { A().g(); } Note that the C++17 [[nodiscard]] attribute applies to the function, so it works differently. > (In cases where the function is declared in a system header it's also > unclear how the should be avoided.) If you don't want this warning, -Wno-ignored-attributes will silence it.
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Richard Biener changed: What|Removed |Added Priority|P3 |P2
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Andrew Pinski changed: What|Removed |Added Target Milestone|--- |6.2
[Bug c++/71463] [6/7 regression] unexpected warning: ignoring function return attributes on template argument
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71463 Martin Sebor changed: What|Removed |Added Status|UNCONFIRMED |NEW Known to work||5.4.0 Keywords||diagnostic Last reconfirmed||2016-06-13 CC||msebor at gcc dot gnu.org Ever confirmed|0 |1 Summary|"ignoring attributes on |[6/7 regression] unexpected |template argument" in -O1 |warning: ignoring function |and above |return attributes on ||template argument Known to fail||6.1.0, 7.0 --- Comment #4 from Martin Sebor --- I looked into this a bit and I'm not sure the warning works quite as intended in this case. First, the warning doesn't say which of the attributes is ignored, and so when multiple attributes are specified, it suggests that all of them are ignored. But tests with single attributes show that only some trigger the warning, raising the question of whether the warning works correctly. Second, it's unclear to me what purpose the warning is meant to serve in this case. Since a function attribute always applies to the instance of the function it decorates and never affects its type the warning doesn't indicate anything unusual or unexpected, and only serves to confuse users. (In cases where the function is declared in a system header it's also unclear how the should be avoided.) Looking at the history of the warning for the test case, it started with r222530 committed to fix bug 50800 which has to do with type attributes, not those of functions (or variables), and there is no test that verifies that it should be issued for the case of functions (or variables). I'm inclined to agree that this is a bug. Confirming as a 6/7 regression with the test case below: $ cat t.C && /home/msebor/build/gcc-6-branch/gcc/xgcc -B /home/msebor/build/gcc-6-branch/gcc -S -Wall -Wextra -Wpedantic t.C void* __attribute__ ((assume_aligned (32))) f0 (); void* __attribute__ ((returns_nonnull)) f1 (); void* __attribute__ ((const)) f2 (); void* __attribute__ ((const, warn_unused_result)) f3 (); template struct S { }; S s0; S s1; S s2; // no warning S s3; // which of the two attributes are ignored? t.C:9:17: warning: ignoring attributes on template argument ‘void* (*)()’ [-Wignored-attributes] S s0; ^ t.C:10:17: warning: ignoring attributes on template argument ‘void* (*)()’ [-Wignored-attributes] S s1; ^ t.C:12:17: warning: ignoring attributes on template argument ‘void* (*)()’ [-Wignored-attributes] S s3; // which of the two attributes are ignored? ^