diff mbox

[C++] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")

Message ID 55D4DD2D.3070500@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Aug. 19, 2015, 7:46 p.m. UTC
Hi,

submitter noticed that, in violation of [basic.start.main], we don't 
reject as ill-formed the declaration of a 'main' variable in the global 
namespace. Not a big deal IMHO, but the below simple check works well 
for me on x86_64-linux.

Thanks,
Paolo.

//////////////////////////////
/cp
2015-08-19  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/67065
	* decl.c (grokvardecl): Reject 'main' as global variable.

/testsuite
2015-08-19  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/67065
	* g++.dg/other/pr67065.C: New.

Comments

Jason Merrill Aug. 20, 2015, 2:04 a.m. UTC | #1
OK.

Jason
Markus Trippelsdorf Aug. 21, 2015, 6:18 a.m. UTC | #2
On 2015.08.19 at 21:46 +0200, Paolo Carlini wrote:
> Hi,
> 
> submitter noticed that, in violation of [basic.start.main], we don't 
> reject as ill-formed the declaration of a 'main' variable in the global 
> namespace. Not a big deal IMHO, but the below simple check works well 
> for me on x86_64-linux.

Well, it breaks building Firefox for example (several violations).
I'm not sure this is really worth the hassle. Maybe a warning instead of
an error would be acceptable?
Markus Trippelsdorf Aug. 21, 2015, 6:30 a.m. UTC | #3
On 2015.08.21 at 08:18 +0200, Markus Trippelsdorf wrote:
> On 2015.08.19 at 21:46 +0200, Paolo Carlini wrote:
> > Hi,
> > 
> > submitter noticed that, in violation of [basic.start.main], we don't 
> > reject as ill-formed the declaration of a 'main' variable in the global 
> > namespace. Not a big deal IMHO, but the below simple check works well 
> > for me on x86_64-linux.
> 
> Well, it breaks building Firefox for example (several violations).
> I'm not sure this is really worth the hassle. Maybe a warning instead of
> an error would be acceptable?

For example:

 % echo "void foo() { int main; }" | g++ -c -x c++ -
<stdin>: In function ‘void foo()’:
<stdin>:1:18: error: cannot declare ‘::main’ to be a global variable
Paolo Carlini Aug. 21, 2015, 8:51 a.m. UTC | #4
Hi,

On 08/21/2015 08:30 AM, Markus Trippelsdorf wrote:
> On 2015.08.21 at 08:18 +0200, Markus Trippelsdorf wrote:
>> On 2015.08.19 at 21:46 +0200, Paolo Carlini wrote:
>>> Hi,
>>>
>>> submitter noticed that, in violation of [basic.start.main], we don't
>>> reject as ill-formed the declaration of a 'main' variable in the global
>>> namespace. Not a big deal IMHO, but the below simple check works well
>>> for me on x86_64-linux.
>> Well, it breaks building Firefox for example (several violations).
>> I'm not sure this is really worth the hassle. Maybe a warning instead of
>> an error would be acceptable?
> For example:
>
>   % echo "void foo() { int main; }" | g++ -c -x c++ -
> <stdin>: In function ‘void foo()’:
> <stdin>:1:18: error: cannot declare ‘::main’ to be a global variable
This is simply a bug in my patch, isn't intended. I'll see if I can fix 
it today or I will simply revert the change for now. Sorry about that. 
Another issue, is whether we want to implement that sentence, and we 
want an error. For example, current clang does, and emits an error. I 
don't have a strong opinion about that.

Paolo.
diff mbox

Patch

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 227003)
+++ cp/decl.c	(working copy)
@@ -8355,6 +8355,11 @@  grokvardecl (tree type,
   else
     DECL_INTERFACE_KNOWN (decl) = 1;
 
+  if (DECL_NAME (decl)
+      && MAIN_NAME_P (DECL_NAME (decl))
+      && CP_DECL_CONTEXT (decl) == global_namespace)
+    error ("cannot declare %<::main%> to be a global variable");
+
   /* Check that the variable can be safely declared as a concept.
      Note that this also forbids explicit specializations.  */
   if (conceptp)
Index: testsuite/g++.dg/other/pr67065.C
===================================================================
--- testsuite/g++.dg/other/pr67065.C	(revision 0)
+++ testsuite/g++.dg/other/pr67065.C	(working copy)
@@ -0,0 +1,3 @@ 
+// PR c++/67065
+
+int main;  // { dg-error "cannot declare" }