Skip to content

Commit 9d2af57

Browse files
authored
Merge pull request #75239 from hamishknight/patte-diag
[CS] A couple of minor diagnostic improvements
2 parents 57e38dc + d6d8d08 commit 9d2af57

File tree

9 files changed

+55
-6
lines changed

9 files changed

+55
-6
lines changed

include/swift/AST/DiagnosticsSema.def

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ ERROR(cannot_match_unresolved_expr_pattern_with_value,none,
208208
ERROR(cannot_match_value_with_pattern,none,
209209
"pattern of type %1 cannot match %0",
210210
(Type, Type))
211+
ERROR(pattern_does_not_conform_to_match,none,
212+
"pattern of type %0 does not conform to expected match type %1",
213+
(Type, Type))
211214

212215
ERROR(cannot_reference_compare_types,none,
213216
"cannot check reference equality of functions; operands here have types "

lib/Sema/CSDiagnostics.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -6505,6 +6505,13 @@ bool MissingContextualConformanceFailure::diagnoseAsError() {
65056505
break;
65066506
}
65076507

6508+
case ConstraintLocator::EnumPatternImplicitCastMatch: {
6509+
emitDiagnostic(diag::pattern_does_not_conform_to_match, getFromType(),
6510+
getToType())
6511+
.highlight(getSourceRange());
6512+
return true;
6513+
}
6514+
65086515
default:
65096516
break;
65106517
}
@@ -9124,7 +9131,7 @@ bool InvalidWeakAttributeUse::diagnoseAsError() {
91249131
ReferenceOwnership::Weak, varType);
91259132

91269133
auto typeRange = var->getTypeSourceRangeForDiagnostics();
9127-
if (varType->hasSimpleTypeRepr()) {
9134+
if (varType->lookThroughSingleOptionalType()->hasSimpleTypeRepr()) {
91289135
diagnostic.fixItInsertAfter(typeRange.End, "?");
91299136
} else {
91309137
diagnostic.fixItInsert(typeRange.Start, "(")

lib/Sema/CSSimplify.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -6667,6 +6667,11 @@ bool ConstraintSystem::repairFailures(
66676667
if (lhs->isPlaceholder() || rhs->isPlaceholder())
66686668
return true;
66696669

6670+
// If we're converting to an existential, we'll diagnose failures in
6671+
// the conformance constraint.
6672+
if (hasConversionOrRestriction(ConversionRestrictionKind::Existential))
6673+
return false;
6674+
66706675
conversionsOrFixes.push_back(ContextualMismatch::create(
66716676
*this, lhs, rhs, getConstraintLocator(locator)));
66726677
break;

lib/Sema/TypeCheckDeclOverride.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,11 @@ bool swift::checkOverrides(ValueDecl *decl) {
14211421
return false;
14221422
}
14231423

1424+
// Don't bother checking any further for invalid decls since they won't match
1425+
// anything.
1426+
if (decl->isInvalid())
1427+
return true;
1428+
14241429
// Set up matching, but bail out if there's nothing to match.
14251430
OverrideMatcher matcher(decl);
14261431
if (!matcher) return false;

test/Constraints/patterns.swift

+14
Original file line numberDiff line numberDiff line change
@@ -791,3 +791,17 @@ do {
791791
}
792792
}
793793
}
794+
795+
func testMatchingNonErrorConformingTypeInClosure(_ x: any Error) {
796+
enum E {
797+
case e
798+
}
799+
_ = {
800+
switch x {
801+
case E.e: // expected-error {{pattern of type 'E' does not conform to expected match type 'Error'}}
802+
break
803+
default:
804+
break
805+
}
806+
}
807+
}

test/attr/attributes.swift

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ weak var weak16 : Class!
191191

192192
@weak var weak17 : Class? // expected-error {{'weak' is a declaration modifier, not an attribute}} {{1-2=}}
193193

194+
class SomeClass {}
195+
protocol SomeProtocol {}
196+
_ = {
197+
// Make sure the fix-it here includes the parens
198+
weak var x: SomeClass & SomeProtocol // expected-error {{'weak' variable should have optional type '(any SomeClass & SomeProtocol)?'}} {{15-15=(}} {{39-39=)?}}
199+
}
194200

195201
@_exported var exportVar: Int // expected-error {{@_exported may only be used on 'import' declarations}}{{1-12=}}
196202
@_exported func exportFunc() {} // expected-error {{@_exported may only be used on 'import' declarations}}{{1-12=}}

test/decl/circularity.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ class C3: G1<A>, P {
8686
// expected-error@-2 {{cannot find type 'A' in scope}}
8787
// expected-note@-3 2{{through reference here}}
8888
override func run(a: A) {}
89-
// expected-error@-1 {{method does not override any method from its superclass}}
90-
// expected-error@-2 {{circular reference}}
91-
// expected-note@-3 2 {{through reference here}}
92-
// expected-note@-4 {{while resolving type 'A'}}
89+
// expected-error@-1 {{circular reference}}
90+
// expected-note@-2 2 {{through reference here}}
91+
// expected-note@-3 {{while resolving type 'A'}}
9392
}
9493

9594
// Another case that triggers circular override checking.

test/decl/class/override.swift

+10
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,13 @@ open class OpenDerivedFinal : OpenBase {
414414
open class OpenDerivedStatic : OpenBase {
415415
override public static func classMethod() {}
416416
}
417+
418+
// When override matching an invalid decl, avoid emitting
419+
// another error saying it doesn't override anything.
420+
class OverrideTypoBaseClass {
421+
func foo(_ x: Int) {}
422+
}
423+
class OverrideTypoSubclass: OverrideTypoBaseClass {
424+
override func foo(_ x: Itn) {} // expected-error {{cannot find type 'Itn' in scope}}
425+
}
426+

test/stmt/errors.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func twelve() {
156156
twelve_helper { (a, b) in // expected-error {{invalid conversion from throwing function of type '(Int, Int) throws -> ()' to non-throwing function type '(Int, Int) -> ()'}}
157157
do {
158158
try thrower()
159-
} catch Twelve.Payload(a...b) {
159+
} catch Twelve.Payload(a...b) { // expected-error {{pattern of type 'Twelve' does not conform to expected match type 'Error'}}
160160
}
161161
}
162162
}

0 commit comments

Comments
 (0)