[PATCH] D107275: [Sema] a[x] has type T when a has type T* or T[], even when T is dependent

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 30 15:57:54 PST 2021


sammccall added a comment.

In D107275#3214272 <https://reviews.llvm.org/D107275#3214272>, @sammccall wrote:

> In the motivating case, the subscript is a known integer type and the LHS is an array or pointer. In this case we don't have the above concern, and we also don't have my #1 above. So I'll restrict the code to cover that case.

Actually I like the restriction a lot, but this *doesn't* take care of #1.
Even with the restriction, we can still end up with a non-dependent type for our type-dependent ArraySubscriptExpr:

Case 1: base and index do not have dependent types. One example of this is that base can be a reference to a static member of the current instantiation that is an array of unknown bound.

  template <int>
  struct foo {
    static int arr[];
    static constexpr int first = arr[0]; // arr is type-dependent but has non-dependent type int[].
  };

Case 2: base is an array with dependent size.

  template <int N>
  struct foo {
    static int arr[N];
    static constexpr int first = arr[0];
  };

Case 3: index is a dependent type that is nevertheless known to be a good index.

  static int arr[];
  
  template <int>
  struct foo {
    enum E { Zero = 0; }
    static constexpr int first = arr[Zero];
  };

So I see two options:

- arbitrarily force the type to be dependent - if we end up with a non-dependent type, use DependentTy instead. This "forgetting" the type is consistent with other situations, like `this->member` inside a template, which the standard says is type-dependent and clang assigns DependentTy.
- accept that we have type-dependent expressions of non-dependent types in some cases. This is consistent with the idea that such exceptions exist today (the DeclRefExpr to unknown-bound-array static members mentioned above).

I feel a little out of my depth, so I'm going to go with the "safe option" of bailing out to DependentTy.
@rsmith or other experts, It would be great to get guidance on whether it's safe to create type-dependent expressions without dependent types.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D107275/new/

https://reviews.llvm.org/D107275



More information about the cfe-commits mailing list