Bug 90847 - misplaced alignment-specifier not diagnosed
Summary: misplaced alignment-specifier not diagnosed
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: accepts-invalid
: 96039 (view as bug list)
Depends on:
Blocks:
 
Reported: 2019-06-11 23:45 UTC by zhonghao
Modified: 2022-03-10 22:24 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-06-12 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description zhonghao 2019-06-11 23:45:03 UTC
My gcc is 10.0.0, and the code is:

template <int N> 
void test() {
 constexpr int N2 = N;
 typedef int T alignas(N2);
 // error: requested alignment is not an integer constant
}

int main() {
 test<4>();
 return 0;
}

gcc accepts the code. clang rejects it:
source>:4:16: error: 'alignas' attribute only applies to variables, data members and tag types

 typedef int T alignas(N2);

               ^

1 error generated.

Compiler returned: 1

icc gives a warning:
<source>(4): warning #3463: alignas does not apply here

   typedef int T alignas(N2);

                 ^

Compiler returned: 0
Comment 1 Jonathan Wakely 2019-06-12 07:44:56 UTC
(In reply to zhonghao from comment #0)
> My gcc is 10.0.0, and the code is:
> 
> template <int N> 
> void test() {
>  constexpr int N2 = N;
>  typedef int T alignas(N2);
>  // error: requested alignment is not an integer constant

What is this comment here for? G++ does not give this error, and it should not give this error. Neither Clang nor icc gives this error either.

It's not helpful to provide code with misleading comments about bogus errors. It makes it look like the comment is somehow relevant to your bug report, and it's not.

Anyway, confirmed, G++ should issue a diagnostic here:

 An alignment-specifier may be applied to a variable or to a class data member,
 but it shall not be applied to a bit-field, a function parameter, or an
 exception-declaration (14.3). An alignment-specifier may also be applied to
 the declaration of a class (in an elaborated-type-specifier (9.1.7.3) or
 class-head (Clause 11), respectively).
Comment 2 Marek Polacek 2019-06-13 01:05:45 UTC
Extended testcase.  I would expect 6 errors but we only issue 2:

struct S {
  alignas(4) int a : 4; // error
  int b : 4 alignas(4); // error
  typedef int T alignas(4); // error
};

void
fn (alignas(4) int, // error
    alignas(4) int n) // error
{
  try 
    {
    }
  catch (alignas(4) int &e) // error
    {
    }
}
Comment 3 Marek Polacek 2019-06-13 01:06:37 UTC
Fixing the title.
Comment 4 Marek Polacek 2020-07-02 20:29:19 UTC
*** Bug 96039 has been marked as a duplicate of this bug. ***