Index: flang/include/flang/Evaluate/tools.h =================================================================== --- flang/include/flang/Evaluate/tools.h +++ flang/include/flang/Evaluate/tools.h @@ -583,8 +583,10 @@ template common::IfNoLvalue>, VALUE> ConvertToKind( int kind, VALUE &&x) { - return *common::SearchTypes( - ConvertToKindHelper{kind, std::move(x)}); + auto result{common::SearchTypes( + ConvertToKindHelper{kind, std::move(x)})}; + CHECK(result.has_value()); + return *result; } // Given a type category CAT, SameKindExprs is a variant that Index: flang/lib/Lower/Bridge.cpp =================================================================== --- flang/lib/Lower/Bridge.cpp +++ flang/lib/Lower/Bridge.cpp @@ -2769,11 +2769,13 @@ } else { llvm_unreachable("unknown category"); } - if (lhsIsWholeAllocatable) + if (lhsIsWholeAllocatable) { + assert(lhsRealloc.has_value()); fir::factory::finalizeRealloc(*builder, loc, *lhsMutableBox, /*lbounds=*/std::nullopt, /*takeLboundsIfRealloc=*/false, *lhsRealloc); + } }, // [2] User defined assignment. If the context is a scalar Index: flang/lib/Lower/ConvertCall.cpp =================================================================== --- flang/lib/Lower/ConvertCall.cpp +++ flang/lib/Lower/ConvertCall.cpp @@ -366,10 +366,12 @@ callNumResults = call.getNumResults(); } - if (caller.mustSaveResult()) + if (caller.mustSaveResult()) { + assert(allocatedResult.has_value()); builder.create(loc, callResult, fir::getBase(*allocatedResult), arrayResultShape, resultLengths); + } if (allocatedResult) { allocatedResult->match( Index: flang/lib/Lower/ConvertExpr.cpp =================================================================== --- flang/lib/Lower/ConvertExpr.cpp +++ flang/lib/Lower/ConvertExpr.cpp @@ -3669,8 +3669,10 @@ mlir::Value oldInnerArg = modifyOp.getSequence(); std::size_t offset = explicitSpace->argPosition(oldInnerArg); explicitSpace->setInnerArg(offset, fir::getBase(lexv)); - fir::ExtendedValue exv = arrayModifyToExv( - builder, loc, *explicitSpace->getLhsLoad(0), modifyOp.getResult(0)); + auto lhsLoad = explicitSpace->getLhsLoad(0); + assert(lhsLoad->has_value()); + fir::ExtendedValue exv = + arrayModifyToExv(builder, loc, *lhsLoad, modifyOp.getResult(0)); genScalarUserDefinedAssignmentCall(builder, loc, userAssignment, exv, elementalExv); } else { @@ -3845,6 +3847,7 @@ } mlir::Value val = builder.createConvert(loc, eleTy, origVal); if (isBoundsSpec()) { + assert(lbounds.has_value()); auto lbs = *lbounds; if (lbs.size() > 0) { // Rebox the value with user-specified shift. @@ -3854,9 +3857,11 @@ mlir::Value{}); } } else if (isBoundsRemap()) { + assert(lbounds.has_value()); auto lbs = *lbounds; if (lbs.size() > 0) { // Rebox the value with user-specified shift and shape. + assert(ubounds.has_value()); auto shapeShiftArgs = flatZip(lbs, *ubounds); auto shapeTy = fir::ShapeShiftType::get(eleTy.getContext(), lbs.size()); mlir::Value shapeShift = @@ -6244,6 +6249,7 @@ charLen = builder.createTemporary(loc, builder.getI64Type()); mlir::Value castLen = builder.createConvert(loc, builder.getI64Type(), fir::getLen(exv)); + assert(charLen.has_value()); builder.create(loc, castLen, *charLen); } } @@ -6258,6 +6264,7 @@ // Convert to extended value. if (fir::isa_char(seqTy.getEleTy())) { + assert(charLen.has_value()); auto len = builder.create(loc, *charLen); return {fir::CharArrayBoxValue{mem, len, extents}, /*needCopy=*/false}; } Index: flang/lib/Lower/ConvertExprToHLFIR.cpp =================================================================== --- flang/lib/Lower/ConvertExprToHLFIR.cpp +++ flang/lib/Lower/ConvertExprToHLFIR.cpp @@ -720,8 +720,10 @@ gen(const Fortran::evaluate::FunctionRef &expr) { mlir::Type resType = Fortran::lower::TypeBuilder::genType(getConverter(), expr); - return *Fortran::lower::convertCallToHLFIR( + auto result = Fortran::lower::convertCallToHLFIR( getLoc(), getConverter(), expr, resType, getSymMap(), getStmtCtx()); + assert(result.has_value()); + return *result; } template Index: flang/lib/Lower/CustomIntrinsicCall.cpp =================================================================== --- flang/lib/Lower/CustomIntrinsicCall.cpp +++ flang/lib/Lower/CustomIntrinsicCall.cpp @@ -179,8 +179,10 @@ llvm::SmallVector args; args.push_back(getOperand(0)); args.push_back(getOperand(1)); + auto iPC = isPresentCheck(2); + assert(iPC.has_value()); args.push_back(builder - .genIfOp(loc, {resultType}, *isPresentCheck(2), + .genIfOp(loc, {resultType}, *iPC, /*withElseRegion=*/true) .genThen([&]() { fir::ExtendedValue sizeExv = getOperand(2);