@@ -466,6 +466,13 @@ TypeChecker::typeCheckExpression(SyntacticElementTarget &target,
466466std::optional<SyntacticElementTarget>
467467TypeChecker::typeCheckTarget (SyntacticElementTarget &target,
468468 TypeCheckExprOptions options) {
469+ auto errorResult = [&]() -> std::optional<SyntacticElementTarget> {
470+ // Fill in ErrorTypes for the target if we can.
471+ if (!options.contains (TypeCheckExprFlags::AvoidInvalidatingAST))
472+ target.markInvalid ();
473+
474+ return std::nullopt ;
475+ };
469476 DeclContext *dc = target.getDeclContext ();
470477 auto &Context = dc->getASTContext ();
471478 PrettyStackTraceLocation stackTrace (Context, " type-checking-target" ,
@@ -475,11 +482,12 @@ TypeChecker::typeCheckTarget(SyntacticElementTarget &target,
475482 // expression and folding sequence expressions.
476483 if (ConstraintSystem::preCheckTarget (
477484 target, /* replaceInvalidRefsWithErrors=*/ true )) {
478- return std:: nullopt ;
485+ return errorResult () ;
479486 }
480487
481488 // Check whether given target has a code completion token which requires
482- // special handling.
489+ // special handling. Returns true if handled, in which case we've already
490+ // type-checked it for completion, and don't need the solution applied.
483491 if (Context.CompletionCallback &&
484492 typeCheckForCodeCompletion (target, /* needsPrecheck*/ false ,
485493 [&](const constraints::Solution &S) {
@@ -517,7 +525,7 @@ TypeChecker::typeCheckTarget(SyntacticElementTarget &target,
517525 // Attempt to solve the constraint system.
518526 auto viable = cs.solve (target, allowFreeTypeVariables);
519527 if (!viable)
520- return std:: nullopt ;
528+ return errorResult () ;
521529
522530 // Apply this solution to the constraint system.
523531 // FIXME: This shouldn't be necessary.
@@ -528,7 +536,7 @@ TypeChecker::typeCheckTarget(SyntacticElementTarget &target,
528536 auto resultTarget = cs.applySolution (solution, target);
529537 if (!resultTarget) {
530538 // Failure already diagnosed, above, as part of applying the solution.
531- return std:: nullopt ;
539+ return errorResult () ;
532540 }
533541
534542 // Unless the client has disabled them, perform syntactic checks on the
@@ -571,6 +579,13 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
571579 DiagnosticTransaction diagnostics (ctx.Diags );
572580
573581 TypeCheckExprOptions options;
582+
583+ // Avoid invalidating the AST since we'll fall through and try to type-check
584+ // with an archetype contextual type opened. Note we also don't need to call
585+ // `markInvalid` for the target below since we'll replace the expression
586+ // with an ErrorExpr on failure anyway.
587+ options |= TypeCheckExprFlags::AvoidInvalidatingAST;
588+
574589 // Expand macro expansion expression at caller side only
575590 if (!atCallerSide && isa<MacroExpansionExpr>(defaultValue)) {
576591 options |= TypeCheckExprFlags::DisableMacroExpansions;
0 commit comments