public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/51405] New: Passing method result to constructor treated as function declaration
@ 2011-12-04  1:37 muhineg at gmail dot com
  2011-12-04  1:54 ` [Bug c++/51405] " muhineg at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: muhineg at gmail dot com @ 2011-12-04  1:37 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51405

             Bug #: 51405
           Summary: Passing method result to constructor treated as
                    function declaration
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: muhineg@gmail.com


Created attachment 25984
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25984
Testcase

Passing to constructor result of method with name which was typedefed before
results in treating object creation as function declaration.

The following code should not compile, but it compiles on gcc 4.2.4 and newer
Tested on 4.7.0 ubuntu snapshot, 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3), 4.4.6
(Ubuntu/Linaro 4.4.6-11ubuntu2) and 4.2.4 built from source.

#include <cstdio>

typedef int Something;

class A
{
  public:
    A ()
    {
      printf ("A::A\n");
    }
};

class B
{
};

int main ()
{
  printf ("start\n");

  A a (B ()->Something ());  //Should produce compile error

  printf ("finish\n");

  return 0;
}

This bug leads to very hard to find bugs in user code - if your class has
operator -> and returned type has method with name which was typedefed. For
example we have DisplayLock class which receives x11 Display* in constructor
and singleton which has "Display* Display ()" method, so code
DisplayLock lock (DisplaySingleton::Instance ()->Display ());
is ignored because Display typedefed as struct _XDisplay Display, and we see no
errors, because there are no access to lock object after creation;

For example:

#include <cstdio>

typedef int Something;

class A
{
  public:
    A (const char* arg)
    {
      printf ("A::A '%s'\n", arg);
    }
};

class B
{
  public:
    class Instance
    {
      public:
        Instance ()
        {
          printf ("B::Instance::Instance\n");
        }

        B* operator -> () const
        {
          static B instance;

          return &instance;
        }
    };

    const char* Something ()
    {
      return "Something";
    }
};

int main ()
{
  printf ("start '%s'\n", B::Instance ()->Something ());

  A a (B::Instance ()->Something ());

  printf ("finish\n");

  return 0;
}

Expected to print (works on gcc 4.2.4):
B::Instance::Instance
start 'Something'
B::Instance::Instance
A::A 'Something'
finish

But prints (4.4.6, 4.6.1 and 4.7.0):
B::Instance::Instance
start 'Something'
finish


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

* [Bug c++/51405] Passing method result to constructor treated as function declaration
  2011-12-04  1:37 [Bug c++/51405] New: Passing method result to constructor treated as function declaration muhineg at gmail dot com
@ 2011-12-04  1:54 ` muhineg at gmail dot com
  2011-12-04  1:59 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: muhineg at gmail dot com @ 2011-12-04  1:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51405

--- Comment #1 from Ievgen Mukhin <muhineg at gmail dot com> 2011-12-04 01:54:05 UTC ---
Sorry, i was wrong when I wrote that test case compiles on 4.2.4.
4.2.4 throws compilation error as it should: error: base operand of ‘->’ has
non-pointer type ‘B’
But newer versions don't produce this error.


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

* [Bug c++/51405] Passing method result to constructor treated as function declaration
  2011-12-04  1:37 [Bug c++/51405] New: Passing method result to constructor treated as function declaration muhineg at gmail dot com
  2011-12-04  1:54 ` [Bug c++/51405] " muhineg at gmail dot com
@ 2011-12-04  1:59 ` redi at gcc dot gnu.org
  2021-07-23 19:42 ` [Bug c++/51405] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2011-12-04  1:59 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51405

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011-12-04
     Ever Confirmed|0                           |1
           Severity|critical                    |normal

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-12-04 01:59:04 UTC ---
how strange - confirmed


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

* [Bug c++/51405] [9/10/11/12 Regression] Passing method result to constructor treated as function declaration
  2011-12-04  1:37 [Bug c++/51405] New: Passing method result to constructor treated as function declaration muhineg at gmail dot com
  2011-12-04  1:54 ` [Bug c++/51405] " muhineg at gmail dot com
  2011-12-04  1:59 ` redi at gcc dot gnu.org
@ 2021-07-23 19:42 ` pinskia at gcc dot gnu.org
  2022-01-17 15:25 ` rguenth at gcc dot gnu.org
  2022-04-13 18:31 ` [Bug c++/51405] " jason at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-23 19:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
               Host|ubuntu 11.10                |
      Known to fail|                            |9.1.0
           Keywords|                            |rejects-valid
   Last reconfirmed|2011-12-04 00:00:00         |2021-7-23
            Summary|Passing method result to    |[9/10/11/12 Regression]
                   |constructor treated as      |Passing method result to
                   |function declaration        |constructor treated as
                   |                            |function declaration
   Target Milestone|---                         |9.5
             Target|i386                        |

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
We started to reject the code in GCC 9 with:
<source>: In function 'int main()':
<source>:43:8: error: trailing return type only available with '-std=c++11' or
'-std=gnu++11'
   43 |   A a (B::Instance ()->Something ());
      |        ^

And if I use -std=c++11 we get:
<source>: In function 'int main()':
<source>:43:8: error: 'parameter' function with trailing return type not
declared with 'auto' type specifier
   43 |   A a (B::Instance ()->Something ());
      |        ^

In GCC 8.0 and before we do get the incorrect code gen.

I suspect this regressed when the start of the C++11 support (-std=c++0x) was
added to the parser.

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

* [Bug c++/51405] [9/10/11/12 Regression] Passing method result to constructor treated as function declaration
  2011-12-04  1:37 [Bug c++/51405] New: Passing method result to constructor treated as function declaration muhineg at gmail dot com
                   ` (2 preceding siblings ...)
  2021-07-23 19:42 ` [Bug c++/51405] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
@ 2022-01-17 15:25 ` rguenth at gcc dot gnu.org
  2022-04-13 18:31 ` [Bug c++/51405] " jason at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-17 15:25 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

* [Bug c++/51405] Passing method result to constructor treated as function declaration
  2011-12-04  1:37 [Bug c++/51405] New: Passing method result to constructor treated as function declaration muhineg at gmail dot com
                   ` (3 preceding siblings ...)
  2022-01-17 15:25 ` rguenth at gcc dot gnu.org
@ 2022-04-13 18:31 ` jason at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jason at gcc dot gnu.org @ 2022-04-13 18:31 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org
           Keywords|rejects-valid               |
             Status|NEW                         |RESOLVED
            Summary|[9/10/11/12 Regression]     |Passing method result to
                   |Passing method result to    |constructor treated as
                   |constructor treated as      |function declaration
                   |function declaration        |
         Resolution|---                         |FIXED

--- Comment #4 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> We started to reject the code in GCC 9 with:
> <source>: In function 'int main()':
> <source>:43:8: error: trailing return type only available with '-std=c++11'
> or '-std=gnu++11'

So the bug was fixed in GCC 9.  The remaining behavior is just the "most vexing
parse": in

  A a (B::Instance ()->Something ());

We parse a function declaration for a function named a that returns A, with an
unnamed parameter which has type function returning B::Instance and a trailing
return type of function returning Something, aka int.

Now, clearly this is ill-formed, and so we correctly diagnose that the trailing
return type requires that the previous declared return type be 'auto'.

But this is a semantic rule, not part of the grammar.

We could make this work in GCC by catching some of these problems in the
parser, but that's not what the standard says.

The EDG compiler gives the same error.

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

end of thread, other threads:[~2022-04-13 18:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-04  1:37 [Bug c++/51405] New: Passing method result to constructor treated as function declaration muhineg at gmail dot com
2011-12-04  1:54 ` [Bug c++/51405] " muhineg at gmail dot com
2011-12-04  1:59 ` redi at gcc dot gnu.org
2021-07-23 19:42 ` [Bug c++/51405] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
2022-01-17 15:25 ` rguenth at gcc dot gnu.org
2022-04-13 18:31 ` [Bug c++/51405] " jason 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).