@@ -2718,20 +2718,28 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
2718
2718
auto element = Body->getLastElement ();
2719
2719
// Let's see if the last statement would make for a valid return value.
2720
2720
if (auto expr = element.dyn_cast <Expr *>()) {
2721
- bool conforms = llvm::all_of (OpaqueDecl->getOpaqueInterfaceGenericSignature ().getRequirements (),
2722
- [&expr, this ](auto requirement) {
2723
- if (requirement.getKind () == RequirementKind::Conformance) {
2724
- auto conformance =
2725
- TypeChecker::conformsToProtocol (expr->getType ()->getRValueType (),
2726
- requirement.getProtocolDecl (),
2727
- Implementation->getModuleContext (),
2728
- /* allowMissing=*/ false );
2729
- return !conformance.isInvalid ();
2730
- }
2731
- // If we encounter any requirements other than `Conformance`, we do
2732
- // not attempt to type check the expression.
2733
- return false ;
2734
- });
2721
+ auto exprType = expr->getType ();
2722
+ // Function body might not be valid and we cannot reply on
2723
+ // \c typeCheckStmt here to propagate HadError because its
2724
+ // unreliable.
2725
+ if (!exprType || exprType->hasError ())
2726
+ return ;
2727
+
2728
+ bool conforms = llvm::all_of (
2729
+ OpaqueDecl->getOpaqueInterfaceGenericSignature ()
2730
+ .getRequirements (),
2731
+ [&exprType, this ](auto requirement) {
2732
+ if (requirement.getKind () == RequirementKind::Conformance) {
2733
+ auto conformance = TypeChecker::conformsToProtocol (
2734
+ exprType->getRValueType (), requirement.getProtocolDecl (),
2735
+ Implementation->getModuleContext (),
2736
+ /* allowMissing=*/ false );
2737
+ return !conformance.isInvalid ();
2738
+ }
2739
+ // If we encounter any requirements other than `Conformance`, we
2740
+ // do not attempt to type check the expression.
2741
+ return false ;
2742
+ });
2735
2743
2736
2744
// If all requirements are fulfilled, we offer to insert `return` to
2737
2745
// fix the issue.
0 commit comments