Skip to content

Commit 2f5cfad

Browse files
authored
Merge pull request #83974 from hamishknight/reprap
[Sema] Avoid marking TypeRepr invalid with `SilenceErrors`
2 parents 788e3c0 + 5b8dfc7 commit 2f5cfad

File tree

10 files changed

+47
-12
lines changed

10 files changed

+47
-12
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5736,7 +5736,7 @@ class ConstraintSystem {
57365736

57375737
/// If we aren't certain that we've emitted a diagnostic, emit a fallback
57385738
/// diagnostic.
5739-
void maybeProduceFallbackDiagnostic(SyntacticElementTarget target) const;
5739+
void maybeProduceFallbackDiagnostic(SourceLoc loc) const;
57405740

57415741
/// Check whether given AST node represents an argument of an application
57425742
/// of some sort (call, operator invocation, subscript etc.)

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10023,7 +10023,7 @@ ConstraintSystem::applySolution(Solution &solution,
1002310023
{
1002410024
const auto &score = solution.getFixedScore();
1002510025
if (score.Data[SK_Fix] > numResolvableFixes || score.Data[SK_Hole] > 0) {
10026-
maybeProduceFallbackDiagnostic(target);
10026+
maybeProduceFallbackDiagnostic(target.getLoc());
1002710027
return std::nullopt;
1002810028
}
1002910029
}

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ bool FailureDiagnostic::conformsToKnownProtocol(
236236
return TypeChecker::conformsToKnownProtocol(type, protocol);
237237
}
238238

239+
bool FallbackDiagnostic::diagnoseAsError() {
240+
getConstraintSystem().maybeProduceFallbackDiagnostic(getLoc());
241+
return true;
242+
}
243+
239244
Type RequirementFailure::getOwnerType() const {
240245
auto anchor = getAnchor();
241246
// If diagnostic is anchored at assignment expression

lib/Sema/CSDiagnostics.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ class FailureDiagnostic {
210210
llvm::SmallVectorImpl<char> &scratch) const;
211211
};
212212

213+
/// Emits a fallback diagnostic message if no other error has been emitted.
214+
class FallbackDiagnostic final : public FailureDiagnostic {
215+
public:
216+
FallbackDiagnostic(const Solution &solution, ConstraintLocator *locator)
217+
: FailureDiagnostic(solution, locator) {}
218+
219+
bool diagnoseAsError() override;
220+
};
221+
213222
/// Base class for all of the diagnostics related to generic requirement
214223
/// failures, provides common information like failed requirement,
215224
/// declaration where such requirement comes from, etc.

lib/Sema/CSFix.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,10 @@ AllowKeyPathWithoutComponents::create(ConstraintSystem &cs,
21872187

21882188
bool IgnoreInvalidResultBuilderBody::diagnose(const Solution &solution,
21892189
bool asNote) const {
2190-
return true; // Already diagnosed by `matchResultBuilder`.
2190+
// This should already be diagnosed by `matchResultBuilder`, emit a fallback
2191+
// diagnostic if not.
2192+
FallbackDiagnostic diag(solution, getLocator());
2193+
return diag.diagnose(asNote);
21912194
}
21922195

21932196
IgnoreInvalidResultBuilderBody *
@@ -2198,7 +2201,10 @@ IgnoreInvalidResultBuilderBody::create(ConstraintSystem &cs,
21982201

21992202
bool IgnoreInvalidASTNode::diagnose(const Solution &solution,
22002203
bool asNote) const {
2201-
return true; // Already diagnosed by the producer of ErrorExpr or ErrorType.
2204+
// This should already be diagnosed by the producer of ErrorExpr or ErrorType,
2205+
// emit a fallback diagnostic if not.
2206+
FallbackDiagnostic diag(solution, getLocator());
2207+
return diag.diagnose(asNote);
22022208
}
22032209

22042210
IgnoreInvalidASTNode *IgnoreInvalidASTNode::create(ConstraintSystem &cs,

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ ConstraintSystem::solve(SyntacticElementTarget &target,
14571457
}
14581458

14591459
case SolutionResult::Error:
1460-
maybeProduceFallbackDiagnostic(target);
1460+
maybeProduceFallbackDiagnostic(target.getLoc());
14611461
return std::nullopt;
14621462

14631463
case SolutionResult::TooComplex: {

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,8 +4875,7 @@ bool ConstraintSystem::isConformanceUnavailable(ProtocolConformanceRef conforman
48754875

48764876
/// If we aren't certain that we've emitted a diagnostic, emit a fallback
48774877
/// diagnostic.
4878-
void ConstraintSystem::maybeProduceFallbackDiagnostic(
4879-
SyntacticElementTarget target) const {
4878+
void ConstraintSystem::maybeProduceFallbackDiagnostic(SourceLoc loc) const {
48804879
if (Options.contains(ConstraintSystemFlags::SuppressDiagnostics))
48814880
return;
48824881

@@ -4888,7 +4887,7 @@ void ConstraintSystem::maybeProduceFallbackDiagnostic(
48884887
(diagnosticTransaction && diagnosticTransaction->hasErrors()))
48894888
return;
48904889

4891-
ctx.Diags.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
4890+
ctx.Diags.diagnose(loc, diag::failed_to_produce_diagnostic);
48924891
}
48934892

48944893
SourceLoc constraints::getLoc(ASTNode anchor) {

lib/Sema/TypeCheckType.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,10 +2637,12 @@ static Type evaluateTypeResolution(const TypeResolution *resolution,
26372637
TypeResolver(*resolution, silContext)
26382638
.resolveType(TyR, resolution->getOptions());
26392639

2640-
// If we resolved down to an error, make sure to mark the typeRepr as invalid
2641-
// so we don't produce a redundant diagnostic.
2640+
// If we resolved down to an error, and we haven't silenced diagnostics, make
2641+
// sure to mark the typeRepr as invalid so we don't produce a redundant
2642+
// diagnostic.
26422643
if (result->hasError()) {
2643-
TyR->setInvalid();
2644+
if (!options.contains(TypeResolutionFlags::SilenceErrors))
2645+
TyR->setInvalid();
26442646
return result;
26452647
}
26462648

test/Constraints/patterns.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,3 +820,17 @@ do {
820820
}
821821
}
822822
}
823+
824+
// Make sure we diagnose 'Undefined' here.
825+
func testUndefinedTypeInPattern(_ x: Int) {
826+
switch x {
827+
case Optional<Undefined>.alsoUndefined: // expected-error {{cannot find type 'Undefined' in scope}}
828+
break
829+
}
830+
_ = {
831+
switch x {
832+
case Optional<Undefined>.alsoUndefined: // expected-error {{cannot find type 'Undefined' in scope}}
833+
break
834+
}
835+
}
836+
}

validation-test/compiler_crashers_2/75d8fd688b445bb.swift renamed to validation-test/compiler_crashers_2_fixed/75d8fd688b445bb.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {"kind":"typecheck","original":"b7a10f72","signature":"(anonymous namespace)::Verifier::walkToExprPre(swift::Expr*)","signatureAssert":"Assertion failed: ((HadError || !isa<SourceFile *>(M) || cast<SourceFile *>(M)->ASTStage < SourceFile::TypeChecked) && \"OverloadedDeclRef\" \"in wrong phase\"), function walkToExprPre"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
func a(b : Int) {
44
_ = {
55
switch b {

0 commit comments

Comments
 (0)