From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 553823858D35; Thu, 7 Mar 2024 11:57:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 553823858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709812671; bh=BYoyJOXs6zWQhrjy8VZbVVL/8DOy4fEOBy++KD0eEUM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=LXpYx2OKjY1elNTAhN4hURSBgs6bXPHbGLEWtFKiqpAkCSJnMOaw3V6B7J4wgimvk mZVenvsHbUXo+xcXJs3r9OIpvV6bHoJVq6QTmGYx/vlmdj1lhbQSIr5q5E4C43nMi1 TIxuz2roCKyUD9/jORsYn1R2V0aq2nLxcNuqt5f8= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/114265] Unhelpful message when var name is also a struct name Date: Thu, 07 Mar 2024 11:57:50 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cf_reconfirmed_on bug_status everconfirmed Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114265 Jonathan Wakely changed: What |Removed |Added ---------------------------------------------------------------------------- Last reconfirmed| |2024-03-07 Status|UNCONFIRMED |NEW Ever confirmed|0 |1 --- Comment #1 from Jonathan Wakely --- But it's valid to define Struct as a variable after also defining it as a t= ype, the problem is only when you later refer to it as a type. That's why the er= ror talks about the 'fn' being redeclared. When it sees `int fn(Struct*)` the second time, the language grammar says you are declaring an int variable, a= nd initializing it with an arithmetic expression involving the Struct variable multiplied by a missing operand. The code would be valid if the use of Struct after its redefinition used an elaborated-type-specifier to refer to it (i.e. included the 'struct' keywor= d): int fn(struct Struct *) {=20 return 0; } Maybe it would be possible for the "redeclared as a different kind of entit= y" to try to parse `int fn(Struct*)` again, excluding non-types from name look= up (except in unevaluated operands and default arguments?) and then if that succeeds, adda fix-it hint for the elaborated-type-specifier (with either 'struct', 'class', 'union', or 'enum' as appropriate): note: use 'struct Struct' to refer to the type int fn(struct Struct *) ^~~~~~ For comparison, other compilers give very similar results: Clang gives: s.cc:7:5: error: redefinition of 'fn' as different kind of symbol int fn(Struct *) ^ s.cc:3:5: note: previous definition is here int fn(Struct *); ^ s.cc:7:16: error: expected expression int fn(Struct *) ^ s.cc:7:17: error: expected ';' after top level declarator int fn(Struct *) ^ ; 3 errors generated. EDG gives: "s.cc", line 7: error: declaration is incompatible with "int fn(Struct *)" (declared at line 3) int fn(Struct *) ^ "s.cc", line 7: error: expected an expression int fn(Struct *) ^ "s.cc", line 8: error: expected a ";" {=20 ^ 3 errors detected in the compilation of "s.cc". MSVC seems the least helpful: (7): error C2059: syntax error: ')' (8): error C2143: syntax error: missing ';' before '{' (8): error C2447: '{': missing function header (old-style formal li= st?)=