Bug 80868 - "Duplicate const" warning emitted in `const typeof(foo) bar;`
Summary: "Duplicate const" warning emitted in `const typeof(foo) bar;`
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-05-23 18:35 UTC by George Burgess IV
Modified: 2024-01-25 23:18 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description George Burgess IV 2017-05-23 18:35:46 UTC
Minimal test-case (repros with 7.1: https://godbolt.org/g/A2dTCP):

$ cat tc.c
const int i;
const typeof(i) j;

$ gcc -std=gnu89 -pedantic tc.c
2:17: warning: duplicate 'const' [-Wpedantic]
 const typeof(i) j;

Expected behavior: no duplicate const warning when there's only one `const` on the decl for `j`.

While the current behavior is understandable, I think it would be good to relax the warning in this case. typeof is often used in macros, so silencing this warning isn't always trivial. Since typeof is also a language extension, I don't believe that C89 6.5.3 (which I assume is the only reason GCC is emitting these complaints in the first place) needs to apply here.

Thank you for your time :)
Comment 2 Marek Polacek 2017-05-24 10:10:47 UTC
We're supposed to complain for

	const const int x;
and
	typedef const int t;
	const t x;

and I think we should thus also warn for this (-std=gnu89 -pedantic only).

	const int a;
	const __typeof(a) x;

because __typeof() doesn't strip outermost type qualifications.  There were discussion about adding __nonqual_typeof() but that hasn't been added yet.
Comment 3 George Burgess IV 2017-05-24 22:19:13 UTC
Thanks for the response!

From the standpoint of consistency, I agree.

My point is more that GCC isn't bound by the standard to be as strict with `typeof`, and making an exception for `typeof` here would make it easier to use in macros. I believe the gain in usability here outweighs the cost of having this inconsistency.

(I also feel that this warning in general isn't useful when the only "duplicate" const has been inferred from an expression, but it seems that __auto_type has the same "duplicate const" behavior as typeof, so...)
Comment 4 Nick Desaulniers 2018-09-24 16:32:44 UTC
We can close this bug.  LLVM will match GCC here: https://reviews.llvm.org/D52248
Comment 5 Nick Desaulniers 2018-09-24 20:59:58 UTC
Oh, note in the typedef case:

typedef const int t;
const t x;

It seems that for -std=c89 (non pedantic, non GNU), GCC does not warn.  That seems to violate C90 6.5.3 constraints: "The same type qualifier shall not appear more than once in the same specifier-list or qualifier-list, either directly or via one or more typedefs."
Comment 6 Jonathan Wakely 2018-09-25 09:01:23 UTC
The observation in comment 5 probably warrants its own bug report.

George, do you agree with closing this?
Comment 7 Nick Desaulniers 2018-09-25 17:22:26 UTC
Forked: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87435
Comment 8 George Burgess IV 2018-09-25 18:15:06 UTC
> George, do you agree with closing this?

Sounds good to me.

Thanks everyone!