<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/143325>143325</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            relocation diagnostics asserts in response to code that is broken for unrelated reasons #awesome
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            good first issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            cor3ntin
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ojhunt
      </td>
    </tr>
</table>

<pre>
    I made an error in one of my test cases and got a crash due to a null value. After much confusion I realized the problem is actually that I had redefined the same struct multiple times, and that's what caused the unexpected state.

The test case:

```cpp
struct Foo  {
  Foo(const Foo&);
  ~Foo();
};

struct Foo {
  Foo();
  int;
};

struct Wrapper {
  union {
    Foo p;
  } u;
};
static_assert(__builtin_is_cpp_trivially_relocatable(Wrapper));
```

The assertion is triggered under `DiagnoseNonTriviallyRelocatableReason` in this code:
```cpp
  if (!D->hasSimpleMoveConstructor() && !D->hasSimpleCopyConstructor()) {
    const auto *Decl = cast<CXXConstructorDecl>(
        LookupSpecialMemberFromXValue(SemaRef, D, /*Assign=*/false));
    if (Decl && Decl->isUserProvided())
 SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason)
          << diag::TraitNotSatisfiedReason::UserProvidedCtr
          << Decl->isMoveConstructor() << Decl->getSourceRange();
  }
```

The issue is the `cast< CXXConstructorDecl >` as `LookupSpecialMemberFromXValue` can return null. The `cast<>` is followed by a null check so it's presumably expected that `LookupSpecialMemberFromXValue` returns null, in which case the fix is likely just to replace `cast` with `cast_or_null`. If it's expected that the cast itself may fail then I assume there's `dyn_cast_or_null` that should be used.


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVU9v27gT_TT0ZVBDpmzFPvig2jUQoC1-SPLb7U2gyJHFhiIFDpnUe9jPvqAk23HTLlYIYlGcefPnDR8FkT5axC1bfWScS-dzG7RlnLPVfiZi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