diff mbox

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

Message ID 55D6EA2C.2090400@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Aug. 21, 2015, 9:06 a.m. UTC
... I'm testing the below. So far appears to work well for me.

Paolo.

///////////////////

Comments

Markus Trippelsdorf Aug. 21, 2015, 9:23 a.m. UTC | #1
On 2015.08.21 at 11:06 +0200, Paolo Carlini wrote:
> ... I'm testing the below. So far appears to work well for me.

Yes for me, too. Firefox now builds without any issues.
So emitting an error (like clang) is fine after all.

Thanks.
Paolo Carlini Aug. 21, 2015, 12:50 p.m. UTC | #2
Hi,

On 08/21/2015 11:23 AM, Markus Trippelsdorf wrote:
> On 2015.08.21 at 11:06 +0200, Paolo Carlini wrote:
>> ... I'm testing the below. So far appears to work well for me.
> Yes for me, too. Firefox now builds without any issues.
> So emitting an error (like clang) is fine after all.
Excellent. Testing completed successfully. Thus, unless Jason disagrees, 
I mean to commit it as an obvious follow up later today.

Paolo.
Jason Merrill Aug. 21, 2015, 3:37 p.m. UTC | #3
On 08/21/2015 05:06 AM, Paolo Carlini wrote:
>     if (DECL_NAME (decl)
>         && MAIN_NAME_P (DECL_NAME (decl))
> -      && CP_DECL_CONTEXT (decl) == global_namespace)
> +      && CP_DECL_CONTEXT (decl) == global_namespace
> +      && !at_function_scope_p ())

How about looking at the "scope" local variable instead of CP_DECL_CONTEXT?

Jason
Paolo Carlini Aug. 21, 2015, 5:11 p.m. UTC | #4
Hi,

On 08/21/2015 05:37 PM, Jason Merrill wrote:
> On 08/21/2015 05:06 AM, Paolo Carlini wrote:
>>     if (DECL_NAME (decl)
>>         && MAIN_NAME_P (DECL_NAME (decl))
>> -      && CP_DECL_CONTEXT (decl) == global_namespace)
>> +      && CP_DECL_CONTEXT (decl) == global_namespace
>> +      && !at_function_scope_p ())
>
> How about looking at the "scope" local variable instead of 
> CP_DECL_CONTEXT?
Ah nice. Simply checking:

     scope == global_namespace

appears to work great. Shall I go with that if testing is fine?

Thanks!
Paolo.
Jason Merrill Aug. 21, 2015, 5:31 p.m. UTC | #5
On 08/21/2015 01:11 PM, Paolo Carlini wrote:
> Ah nice. Simply checking:
>
>      scope == global_namespace
>
> appears to work great. Shall I go with that if testing is fine?

Please.

Jason
diff mbox

Patch

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 227054)
+++ cp/decl.c	(working copy)
@@ -8357,7 +8357,8 @@  grokvardecl (tree type,
 
   if (DECL_NAME (decl)
       && MAIN_NAME_P (DECL_NAME (decl))
-      && CP_DECL_CONTEXT (decl) == global_namespace)
+      && CP_DECL_CONTEXT (decl) == global_namespace
+      && !at_function_scope_p ())
     error ("cannot declare %<::main%> to be a global variable");
 
   /* Check that the variable can be safely declared as a concept.
Index: testsuite/g++.dg/other/pr67065.C
===================================================================
--- testsuite/g++.dg/other/pr67065.C	(revision 227054)
+++ testsuite/g++.dg/other/pr67065.C	(working copy)
@@ -1,3 +1,5 @@ 
 // PR c++/67065
 
 int main;  // { dg-error "cannot declare" }
+
+void foo() { int main; }