<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/120129>120129</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Optimization in clang frontend breaks `dynamic_cast` on Apple platforms.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ahatanak
</td>
</tr>
</table>
<pre>
This is caused by 9d525bf94b255df89587db955b5fa2d3c03c2c3e.
`dynamic_cast` in the following code fails unless the optimization is disabled by passing `-fno-assume-unique-vtables`.
% cat a.h
```
struct Foo {
virtual ~Foo();
};
struct Bar final : public Foo {
};
Foo* makeBar();
```
% cat a.cc
```
#include "a.h"
Foo::~Foo() {}
Foo* makeBar() {
return new Bar();
}
```
% cat b.cc
```
#include "a.h"
int main() {
Foo* f = makeBar();
Bar* b = dynamic_cast<Bar*>(f); // b will incorrectly be nullptr
return !!b;
}
```
% clang++ -std=c++11 -c a.cc -O1
% clang++ -std=c++11 -c b.cc -O1
% clang++ -shared -o liba.so a.o
% clang++ --std=c++11 -o a.out b.o -Wl,-rpath,$PWD liba.so
% ./a.out
% echo $?
0
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUlEuPq7gTxT9NZVMCmTIkYcGCdP7Z3v9ipLsc-UXwtLEZ23SrZzGffQRJ335Mz0tCSlDVOf4d25RIyV69MR00J2jOO7HkMcROjCILLx53MuiX7qfRJrQJlViS0ShfsNUNNXJoa0lNo4dj2xwPWrZNI5tBkOaKcUWKmxJYvz57pl-8mKz6WYmUYc_QesyjwSE4F56tv6IK2uAgrEu4eGdS2uphznayv4lsg18RtE1CuhvEvML7K8KeFYMPhUhpmUyxePvrYoqnvDYm2LNXCGpQiYyiHG9I94f1KcdFZbyEgHA4AesRn2zMi3D4-yUEoCNQC3ytwOF8__NDdhIRB-uFQ-A9zot0Vr3zeq_YzHqcxKM5ifjB9x3OCvAGq9TnMnHrlVu0QSBa0xC92fMeeP9GvUEczn-9_I_E0eQlevTmGT-z3fQfCV8B5X8BtD7jJKz_tPSda0Dg5682B29EPcqt48NV4g-3GvD_AR2HmwaBLkAXlPhsnUPrVYjRqOxeUBr0i3Nzju9TA1VAlfynvE74K9AJ6IRFyhr4Wd1eqwoLtR0WFt-qf9ku_659FNFoLAI6K0WZAooy4Fedf3JeRaIMy3o2AYvvDuihiLPII9ADUP3_7-dX07tfCXTZFPd3o8aAQDXwC7Ce7XTHdctbsTNddeA1Pxyoqndjx5tKmlpoJlgrlRmYVMQ0tceDbhvNmp3tiFFdUbWvWto3dbnnhgwnva-UFvzAoGZmEtaVzj1NZYjXnU1pMV1FrKJ254Q0Lm2zieiemtYxFbtVUMjlmqBmzqac3iyyzc503z5MDn_bNBxi8Nl4jTIa8Zjwi8kUPPbz7AzOTuQhxCmVuyW6bsx5Tuv3td2tq83jIksVJqDLuvT9p5hj-MWoDHTZoiSgyz3NU0d_BAAA___QzI9H">