public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108238] New: returns_nonnull attribute with auto return type fails to compile
@ 2022-12-27 23:12 pobrn at protonmail dot com
  2022-12-28 18:59 ` [Bug c++/108238] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: pobrn at protonmail dot com @ 2022-12-27 23:12 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108238
           Summary: returns_nonnull attribute with auto return type fails
                    to compile
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pobrn at protonmail dot com
  Target Milestone: ---

See the following example code (https://gcc.godbolt.org/z/76vPhPe6z):

    [[gnu::returns_nonnull]]
    auto f() {
        return new int(42);
    }

It produces the following error:

    <source>:2:8: error: 'returns_nonnull' attribute on a function not
returning a pointer
    2 | auto f() {
      |        ^

I think this should compile.

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

* [Bug c++/108238] returns_nonnull attribute with auto return type fails to compile
  2022-12-27 23:12 [Bug c++/108238] New: returns_nonnull attribute with auto return type fails to compile pobrn at protonmail dot com
@ 2022-12-28 18:59 ` pinskia at gcc dot gnu.org
  2023-01-19 17:13 ` pobrn at protonmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-28 18:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |rejects-valid
   Last reconfirmed|                            |2022-12-28
      Known to fail|                            |4.9.0

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

Some debugging:

#1  0x00000000011e6010 in handle_returns_nonnull_attribute
(node=0x7fffffffd4b0, name=0x7ffff72bd9c0, no_add_attrs=0x7fffffffd4cf) at
/home/apinski/src/upstream-gcc/gcc/gcc/c-family/c-attribs.cc:5761
5761          error ("%qE attribute on a function not returning a pointer",
name);


5758      // Even without a prototype we still have a return type we can check.
5759      if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)

(gdb) p debug_tree(*node)
 <function_type 0x7ffff74399d8
    type <template_type_parm 0x7ffff7439930 auto VOID
        align:8 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type
0x7ffff74361f8
       index 0 level 1 orig_level 1
        chain <type_decl 0x7ffff72bfc78 auto>>
    QI
    size <integer_cst 0x7ffff72b30c0 type <integer_type 0x7ffff72b10a8
bitsizetype> constant 8>
    unit-size <integer_cst 0x7ffff72b30d8 type <integer_type 0x7ffff72b1000
sizetype> constant 1>
    align:8 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type
0x7ffff74362a0
    arg-types <tree_list 0x7ffff72a7d98
        value <void_type 0x7ffff72b1f18 void VOID
            align:8 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type
0x7ffff72b1f18
            pointer_to_this <pointer_type 0x7ffff72b9000>>>>

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

* [Bug c++/108238] returns_nonnull attribute with auto return type fails to compile
  2022-12-27 23:12 [Bug c++/108238] New: returns_nonnull attribute with auto return type fails to compile pobrn at protonmail dot com
  2022-12-28 18:59 ` [Bug c++/108238] " pinskia at gcc dot gnu.org
@ 2023-01-19 17:13 ` pobrn at protonmail dot com
  2023-10-19 22:05 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pobrn at protonmail dot com @ 2023-01-19 17:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Barnabás Pőcze <pobrn at protonmail dot com> ---
Here is a change that I believe might address this. It seems to work but I have
never done anything in gcc, so probably has shortcomings.

The error points to the return expression, like this:

  asd2.cpp: In function ‘auto f2()’:
  asd2.cpp:9:20: error: ‘returns_nonnull’ attribute on a function not returning
a pointer
      9 | auto f2() { return 42; }
        |                    ^~

---

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 4667f6de311..50f5ad6b8f1 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -5761,8 +5761,9 @@ static tree
 handle_returns_nonnull_attribute (tree *node, tree name, tree, int,
                                  bool *no_add_attrs)
 {
+  auto type_code = TREE_CODE (TREE_TYPE (*node));
   // Even without a prototype we still have a return type we can check.
-  if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
+  if (type_code != POINTER_TYPE && type_code != TEMPLATE_TYPE_PARM)
     {
       error ("%qE attribute on a function not returning a pointer", name);
       *no_add_attrs = true;
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 1656d02d6d1..b3ae608b350 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12305,6 +12305,10 @@ apply_deduced_return_type (tree fco, tree return_type)
   if (return_type == error_mark_node)
     return;

+  tree returns_nonnull = lookup_attribute("returns_nonnull", TYPE_ATTRIBUTES
(TREE_TYPE (fco)));
+  if (returns_nonnull && TREE_CODE (return_type) != POINTER_TYPE)
+    error ("%<returns_nonnull%> attribute on a function not returning a
pointer");
+
   if (DECL_CONV_FN_P (fco))
     DECL_NAME (fco) = make_conv_op_name (return_type);

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

* [Bug c++/108238] returns_nonnull attribute with auto return type fails to compile
  2022-12-27 23:12 [Bug c++/108238] New: returns_nonnull attribute with auto return type fails to compile pobrn at protonmail dot com
  2022-12-28 18:59 ` [Bug c++/108238] " pinskia at gcc dot gnu.org
  2023-01-19 17:13 ` pobrn at protonmail dot com
@ 2023-10-19 22:05 ` pinskia at gcc dot gnu.org
  2023-10-19 22:22 ` [Bug c++/108238] auto return type and some attributes don't get along pinskia at gcc dot gnu.org
  2023-10-20  9:20 ` pobrn at protonmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-19 22:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
alloc_align, alloc_size, assume_aligned, malloc attributes have a similar
issue.
const and pure does too.

```
[[gnu::const]]
auto f(){}

[[gnu::const]]
void f1(){}
```

warn_unused_result has a similar issue too:
```
[[gnu::warn_unused_result]]
auto f(){}

[[gnu::warn_unused_result]]
void f1(){}
```

There might be others too.

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

* [Bug c++/108238] auto return type and some attributes don't get along
  2022-12-27 23:12 [Bug c++/108238] New: returns_nonnull attribute with auto return type fails to compile pobrn at protonmail dot com
                   ` (2 preceding siblings ...)
  2023-10-19 22:05 ` pinskia at gcc dot gnu.org
@ 2023-10-19 22:22 ` pinskia at gcc dot gnu.org
  2023-10-20  9:20 ` pobrn at protonmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-19 22:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Here is one which is a little more complex for templated function too:
```
template<typename T>
[[gnu::returns_nonnull]]
auto f() {
  return new T(42);
}

auto g(void)
{
   return f<int>();
}
```

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

* [Bug c++/108238] auto return type and some attributes don't get along
  2022-12-27 23:12 [Bug c++/108238] New: returns_nonnull attribute with auto return type fails to compile pobrn at protonmail dot com
                   ` (3 preceding siblings ...)
  2023-10-19 22:22 ` [Bug c++/108238] auto return type and some attributes don't get along pinskia at gcc dot gnu.org
@ 2023-10-20  9:20 ` pobrn at protonmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: pobrn at protonmail dot com @ 2023-10-20  9:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Barnabás Pőcze <pobrn at protonmail dot com> ---
Based on a suggestion from Jakub Jelínek, I have tried to modify
`is_late_template_attribute()` instead, but it appears that function does not
run in this case.

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

end of thread, other threads:[~2023-10-20  9:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-27 23:12 [Bug c++/108238] New: returns_nonnull attribute with auto return type fails to compile pobrn at protonmail dot com
2022-12-28 18:59 ` [Bug c++/108238] " pinskia at gcc dot gnu.org
2023-01-19 17:13 ` pobrn at protonmail dot com
2023-10-19 22:05 ` pinskia at gcc dot gnu.org
2023-10-19 22:22 ` [Bug c++/108238] auto return type and some attributes don't get along pinskia at gcc dot gnu.org
2023-10-20  9:20 ` pobrn at protonmail dot com

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).