Skip to content

Commit 455fcdc

Browse files
committed
[Sema] add redundant ~Copyable requirement warning
Originally I tried to implement a fancy redundant-inverse error diagnostic that identifies the "previous" requirement already seen. I ended up removing this error diagnostic because it was tricky to implement well and we weren't diagnosing other redundant requirements. Turns out we do have a mode of the compiler to diagnose redundant requirements, but as warnings, using `-warn-redundant-requirements`. This warning is much simpler in that it just points to one requirement that is redundant.
1 parent 031f112 commit 455fcdc

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3170,7 +3170,7 @@ ERROR(recursive_superclass_constraint,none,
31703170
ERROR(requires_same_concrete_type,none,
31713171
"generic signature requires types %0 and %1 to be the same", (Type, Type))
31723172
WARNING(redundant_conformance_constraint,none,
3173-
"redundant conformance constraint %0 : %1", (Type, ProtocolDecl *))
3173+
"redundant conformance constraint %0 : %1", (Type, Type))
31743174

31753175
WARNING(missing_protocol_refinement, none,
31763176
"protocol %0 should be declared to refine %1 due to a same-type constraint on 'Self'",

lib/AST/RequirementMachine/Diagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ bool swift::rewriting::diagnoseRequirementErrors(
227227
case RequirementKind::Conformance:
228228
ctx.Diags.diagnose(loc, diag::redundant_conformance_constraint,
229229
requirement.getFirstType(),
230-
requirement.getProtocolDecl());
230+
requirement.getSecondType());
231231
break;
232232
case RequirementKind::Superclass:
233233
ctx.Diags.diagnose(loc, diag::redundant_superclass_constraint,

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,19 @@ void swift::rewriting::expandDefaultRequirements(ASTContext &ctx,
823823
return true;
824824
}
825825

826+
auto currentDefaults = defaults[subject];
827+
auto inverseKind = inverse->getInverseKind();
828+
829+
// Check if this inverse is redundant.
830+
if (!currentDefaults.contains(inverseKind)) {
831+
errors.push_back(
832+
RequirementError::forRedundantRequirement(req, structReq.loc));
833+
return true;
834+
}
835+
826836
// Apply the inverse to the subject's defaults.
827-
defaults[subject].remove(inverse->getInverseKind());
837+
currentDefaults.remove(inverseKind);
838+
defaults[subject] = currentDefaults;
828839

829840
// Remove this inverse conformance requirement.
830841
return true;

test/Parse/inverses.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NoncopyableGenerics
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NoncopyableGenerics -warn-redundant-requirements
22

33
// REQUIRES: asserts
44

@@ -25,16 +25,21 @@ func blah<T>(_ t: borrowing T) where T: ~Copyable,
2525
func foo<T: ~Copyable>(x: borrowing T) {}
2626

2727
struct Buurap<T: ~Copyable> where T: ~Copyable {}
28+
// expected-warning@-1 {{redundant conformance constraint 'T' : '~Copyable'}}
2829

2930
struct ExtraNoncopyStruct: ~Copyable, ~Copyable {}
3031
struct ExtraNoncopyEnum: ~Copyable, ~Copyable {}
32+
3133
protocol ExtraNoncopyProto: ~Copyable, ~Copyable {}
34+
// expected-warning@-1 {{redundant conformance constraint 'Self' : '~Copyable'}}
3235

3336
protocol Foo: ~Copyable
3437
where Self: ~Copyable {
38+
// expected-warning@-1 {{redundant conformance constraint 'Self' : '~Copyable'}}
3539

3640
associatedtype Touch : ~Copyable,
3741
~Copyable
42+
// expected-warning@-1 {{redundant conformance constraint 'Self.Touch' : '~Copyable'}}
3843

3944
func test<T>(_ t: T) where T: ~Self // expected-error {{type 'Self' is not invertible}}
4045
}

0 commit comments

Comments
 (0)