[PATCH] [Sema] Don't permit dependent alignments on non-dependent typedef-names

Aaron Ballman aaron at aaronballman.com
Tue Apr 7 05:07:40 PDT 2015


On Mon, Apr 6, 2015 at 9:08 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> On Mon, Apr 6, 2015 at 5:39 PM, Aaron Ballman <aaron at aaronballman.com>
> wrote:
>>
>> On Mon, Apr 6, 2015 at 8:36 PM, Richard Smith <richard at metafoo.co.uk>
>> wrote:
>> > On Mon, Apr 6, 2015 at 4:35 PM, Aaron Ballman <aaron at aaronballman.com>
>> > wrote:
>> >>
>> >> On Mon, Apr 6, 2015 at 7:30 PM, Richard Smith <richard at metafoo.co.uk>
>> >> wrote:
>> >> > ================
>> >> > Comment at: lib/Sema/SemaDeclAttr.cpp:2866
>> >> > @@ -2865,1 +2865,3 @@
>> >> >
>> >> > +  if (E->isTypeDependent() || E->isValueDependent()) {
>> >> > +    if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
>> >> > ----------------
>> >> > The 'type dependent' check here is redundant.
>> >> >
>> >> > ================
>> >> > Comment at: lib/Sema/SemaDeclAttr.cpp:2869
>> >> > @@ +2868,3 @@
>> >> > +      QualType T = TND->getUnderlyingType();
>> >> > +      if (!T->isDependentType() &&
>> >> > !T->isInstantiationDependentType())
>> >> > {
>> >> > +        S.Diag(Attr.getLoc(),
>> >> > diag::err_alignment_dependent_typedef_name)
>> >> > ----------------
>> >> > I don't think you should check instantiation-dependence here. I think
>> >> > you should reject this, for instance:
>> >> >
>> >> >   template<int N> struct X {
>> >> >     typedef __attribute__((aligned(N))) int X[sizeof(N)];
>> >> >   };
>> >> >
>> >>
>> >> Out of curiosity, why do you think that should be rejected?
>> >
>> >
>> > For the same reason we're rejecting anything here: we'd otherwise create
>> > a
>> > non-dependent type that has a dependent alignment.
>>
>> Huh, I guess I thought this type was dependent because of the
>> sizeof(N) in the type.
>
>
> For posterity, these are approximately-correct definitions:
>  * value-dependent means "we don't know until instantiation whether this is
> a constant expression or what it evaluates to if it is"
>  * type-dependent means "we don't know until instantiation what the
> (canonical) type of this expression is"
>  * instantiation-dependent means "this refers to a template parameter in any
> way (and thus instantiation might possibly fail)"
>
> The relevant rule:
>
> "A type is dependent if it is [...]  an array type constructed from any
> dependent type or whose size is specified by a constant expression that is
> value-dependent,"
>
> sizeof(N) is instantiation-dependent but not value-dependent -- its value is
> always sizeof(int) -- so the resulting type is an instantiation-dependent
> type but not a dependent type.

Thank you for the helpful explanation!

~Aaron



More information about the cfe-commits mailing list