diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp --- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp +++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp @@ -93,7 +93,18 @@ Environment::ValueModel &Model) { // Join distinct boolean values preserving information about the constraints // in the respective path conditions. - if (auto *Expr1 = dyn_cast(&Val1)) { + if (Type->isBooleanType()) { + // FIXME: This is sort of workaround. Since now we just ignore all (implicit) + // integral casts, treating the resulting value as the same as the underlying + // value, it could cause inconsistency between values after `Join` if in + // some paths the type doesn't strictly match: + // + // std::optional o; + // int x; + // if (o.has_value()) { + // x = o.value(); + // } + auto *Expr1 = cast(&Val1); auto *Expr2 = cast(&Val2); auto &MergedVal = MergedEnv.makeAtomicBoolValue(); MergedEnv.addToFlowCondition(MergedEnv.makeOr( diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp --- a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp @@ -2970,6 +2970,23 @@ cxxConstructorDecl(ofClass(hasName("Target")))); } +// This is regression test, it shouldn't crash. +TEST_P(UncheckedOptionalAccessTest, Bitfield) { + using namespace ast_matchers; + ExpectDiagnosticsFor( + R"( + #include "unchecked_optional_access_test.h" + struct Dst { + unsigned int n : 1; + }; + void target() { + $ns::$optional v; + Dst d; + if (v.has_value()) + d.n = v.value(); + } + )"); +} // FIXME: Add support for: // - constructors (copy, move) // - assignment operators (default, copy, move)