Skip to content

[CS] A couple of minor diagnostic improvements #75239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ ERROR(cannot_match_unresolved_expr_pattern_with_value,none,
ERROR(cannot_match_value_with_pattern,none,
"pattern of type %1 cannot match %0",
(Type, Type))
ERROR(pattern_does_not_conform_to_match,none,
"pattern of type %0 does not conform to expected match type %1",
(Type, Type))

ERROR(cannot_reference_compare_types,none,
"cannot check reference equality of functions; operands here have types "
Expand Down
9 changes: 8 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6505,6 +6505,13 @@ bool MissingContextualConformanceFailure::diagnoseAsError() {
break;
}

case ConstraintLocator::EnumPatternImplicitCastMatch: {
emitDiagnostic(diag::pattern_does_not_conform_to_match, getFromType(),
getToType())
.highlight(getSourceRange());
return true;
}

default:
break;
}
Expand Down Expand Up @@ -9124,7 +9131,7 @@ bool InvalidWeakAttributeUse::diagnoseAsError() {
ReferenceOwnership::Weak, varType);

auto typeRange = var->getTypeSourceRangeForDiagnostics();
if (varType->hasSimpleTypeRepr()) {
if (varType->lookThroughSingleOptionalType()->hasSimpleTypeRepr()) {
diagnostic.fixItInsertAfter(typeRange.End, "?");
} else {
diagnostic.fixItInsert(typeRange.Start, "(")
Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6667,6 +6667,11 @@ bool ConstraintSystem::repairFailures(
if (lhs->isPlaceholder() || rhs->isPlaceholder())
return true;

// If we're converting to an existential, we'll diagnose failures in
// the conformance constraint.
if (hasConversionOrRestriction(ConversionRestrictionKind::Existential))
return false;

conversionsOrFixes.push_back(ContextualMismatch::create(
*this, lhs, rhs, getConstraintLocator(locator)));
break;
Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,11 @@ bool swift::checkOverrides(ValueDecl *decl) {
return false;
}

// Don't bother checking any further for invalid decls since they won't match
// anything.
if (decl->isInvalid())
return true;

// Set up matching, but bail out if there's nothing to match.
OverrideMatcher matcher(decl);
if (!matcher) return false;
Expand Down
14 changes: 14 additions & 0 deletions test/Constraints/patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,17 @@ do {
}
}
}

func testMatchingNonErrorConformingTypeInClosure(_ x: any Error) {
enum E {
case e
}
_ = {
switch x {
case E.e: // expected-error {{pattern of type 'E' does not conform to expected match type 'Error'}}
break
default:
break
}
}
}
6 changes: 6 additions & 0 deletions test/attr/attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ weak var weak16 : Class!

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

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

@_exported var exportVar: Int // expected-error {{@_exported may only be used on 'import' declarations}}{{1-12=}}
@_exported func exportFunc() {} // expected-error {{@_exported may only be used on 'import' declarations}}{{1-12=}}
Expand Down
7 changes: 3 additions & 4 deletions test/decl/circularity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ class C3: G1<A>, P {
// expected-error@-2 {{cannot find type 'A' in scope}}
// expected-note@-3 2{{through reference here}}
override func run(a: A) {}
// expected-error@-1 {{method does not override any method from its superclass}}
// expected-error@-2 {{circular reference}}
// expected-note@-3 2 {{through reference here}}
// expected-note@-4 {{while resolving type 'A'}}
// expected-error@-1 {{circular reference}}
// expected-note@-2 2 {{through reference here}}
// expected-note@-3 {{while resolving type 'A'}}
}

// Another case that triggers circular override checking.
Expand Down
10 changes: 10 additions & 0 deletions test/decl/class/override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,13 @@ open class OpenDerivedFinal : OpenBase {
open class OpenDerivedStatic : OpenBase {
override public static func classMethod() {}
}

// When override matching an invalid decl, avoid emitting
// another error saying it doesn't override anything.
class OverrideTypoBaseClass {
func foo(_ x: Int) {}
}
class OverrideTypoSubclass: OverrideTypoBaseClass {
override func foo(_ x: Itn) {} // expected-error {{cannot find type 'Itn' in scope}}
}

2 changes: 1 addition & 1 deletion test/stmt/errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func twelve() {
twelve_helper { (a, b) in // expected-error {{invalid conversion from throwing function of type '(Int, Int) throws -> ()' to non-throwing function type '(Int, Int) -> ()'}}
do {
try thrower()
} catch Twelve.Payload(a...b) {
} catch Twelve.Payload(a...b) { // expected-error {{pattern of type 'Twelve' does not conform to expected match type 'Error'}}
}
}
}
Expand Down