Commit 76945821 authored by Elizabeth Andrews's avatar Elizabeth Andrews
Browse files

Fix crash on switch conditions of non-integer types in templates

Clang currently crashes for switch statements inside a template when
the condition is a non-integer field. The crash is due to incorrect
type-dependency of field. Type-dependency of member expressions is
currently set based on the containing class. This patch changes this for
'members of the current instantiation' to set the type dependency based
on the member's type instead.

A few lit tests started to fail once I applied this patch because errors
are now diagnosed earlier (does not wait till instantiation). I've modified
these tests in this patch as well.

Patch fixes PR#40982

Differential Revision: https://reviews.llvm.org/D61027

llvm-svn: 368706
parent 0a04a062
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -1669,6 +1669,15 @@ MemberExpr *MemberExpr::Create(
  MemberExpr *E = new (Mem) MemberExpr(Base, IsArrow, OperatorLoc, MemberDecl,
                                       NameInfo, T, VK, OK, NOUR);

  if (FieldDecl *Field = dyn_cast<FieldDecl>(MemberDecl)) {
    DeclContext *DC = MemberDecl->getDeclContext();
    // dyn_cast_or_null is used to handle objC variables which do not
    // have a declaration context.
    CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
    if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC))
      E->setTypeDependent(T->isDependentType());
  }

  if (HasQualOrFound) {
    // FIXME: Wrong. We should be looking at the member declaration we found.
    if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
+2 −0
Original line number Diff line number Diff line
@@ -14288,6 +14288,8 @@ void Sema::RefersToMemberWithReducedAlignment(
  bool AnyIsPacked = false;
  do {
    QualType BaseType = ME->getBase()->getType();
    if (BaseType->isDependentType())
      return;
    if (ME->isArrow())
      BaseType = BaseType->getPointeeType();
    RecordDecl *RD = BaseType->getAs<RecordType>()->getDecl();
+0 −3
Original line number Diff line number Diff line
@@ -273,9 +273,6 @@ namespace PR10187 {
      }
      int e[10];
    };
    void g() {
      S<int>().f(); // expected-note {{here}}
    }
  }

  namespace A2 {
+1 −2
Original line number Diff line number Diff line
// RUN: %clang_cc1 -fsyntax-only -verify %s
// expected-no-diagnostics

enum Enum { val = 1 };
template <Enum v> struct C {
@@ -31,7 +30,7 @@ namespace rdar8020920 {
    unsigned long long bitfield : e0;

    void f(int j) {
      bitfield + j;
      bitfield + j; // expected-warning {{expression result unused}}
    }
  };
}
+1 −1
Original line number Diff line number Diff line
@@ -156,7 +156,7 @@ namespace test6 {
    void get(B **ptr) {
      // It's okay if at some point we figure out how to diagnose this
      // at instantiation time.
      *ptr = field;
      *ptr = field; // expected-error {{assigning to 'test6::B *' from incompatible type 'test6::A *}}
    }
  };
}
Loading