Skip to content

Commit 9d131fd

Browse files
authored
Merge pull request #18999 from hamishknight/location-by-association
[GSB] Avoid emitting associated type diagnostics on the protocol decl
2 parents 3178d6d + 42dc4ca commit 9d131fd

File tree

8 files changed

+57
-56
lines changed

8 files changed

+57
-56
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,14 +1151,6 @@ class RequirementRepr {
11511151
return SecondLayout;
11521152
}
11531153

1154-
/// \brief Retrieve the location of the ':' in an explicitly-written
1155-
/// conformance requirement.
1156-
SourceLoc getColonLoc() const {
1157-
assert(getKind() == RequirementReprKind::TypeConstraint ||
1158-
getKind() == RequirementReprKind::LayoutConstraint);
1159-
return SeparatorLoc;
1160-
}
1161-
11621154
/// \brief Retrieve the first type of a same-type requirement.
11631155
Type getFirstType() const {
11641156
assert(getKind() == RequirementReprKind::SameType);
@@ -1201,10 +1193,9 @@ class RequirementRepr {
12011193
return SecondType;
12021194
}
12031195

1204-
/// \brief Retrieve the location of the '==' in an explicitly-written
1205-
/// same-type requirement.
1206-
SourceLoc getEqualLoc() const {
1207-
assert(getKind() == RequirementReprKind::SameType);
1196+
/// \brief Retrieve the location of the ':' or '==' in an explicitly-written
1197+
/// conformance or same-type requirement respectively.
1198+
SourceLoc getSeparatorLoc() const {
12081199
return SeparatorLoc;
12091200
}
12101201

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,11 +1911,11 @@ ERROR(protocol_composition_one_class,none,
19111911

19121912
ERROR(requires_conformance_nonprotocol,none,
19131913
"type %0 constrained to non-protocol, non-class type %1",
1914-
(TypeLoc, TypeLoc))
1914+
(Type, Type))
19151915
ERROR(requires_not_suitable_archetype,none,
19161916
"type %0 in conformance requirement does not refer to a "
19171917
"generic parameter or associated type",
1918-
(TypeLoc))
1918+
(Type))
19191919
WARNING(requires_no_same_type_archetype,none,
19201920
"neither type in same-type constraint (%0 or %1) refers to a "
19211921
"generic parameter or associated type",

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ GenericParamList::clone(DeclContext *dc) const {
621621
auto second = reqt.getConstraintLoc();
622622
reqt = RequirementRepr::getTypeConstraint(
623623
first.clone(ctx),
624-
reqt.getColonLoc(),
624+
reqt.getSeparatorLoc(),
625625
second.clone(ctx));
626626
break;
627627
}
@@ -630,7 +630,7 @@ GenericParamList::clone(DeclContext *dc) const {
630630
auto second = reqt.getSecondTypeLoc();
631631
reqt = RequirementRepr::getSameType(
632632
first.clone(ctx),
633-
reqt.getEqualLoc(),
633+
reqt.getSeparatorLoc(),
634634
second.clone(ctx));
635635
break;
636636
}
@@ -639,7 +639,7 @@ GenericParamList::clone(DeclContext *dc) const {
639639
auto layout = reqt.getLayoutConstraintLoc();
640640
reqt = RequirementRepr::getLayoutConstraint(
641641
first.clone(ctx),
642-
reqt.getColonLoc(),
642+
reqt.getSeparatorLoc(),
643643
layout);
644644
break;
645645
}

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,16 +1454,9 @@ SourceLoc RequirementSource::getLoc() const {
14541454
if (auto typeRepr = getTypeRepr())
14551455
return typeRepr->getStartLoc();
14561456

1457-
if (auto requirementRepr = getRequirementRepr()) {
1458-
switch (requirementRepr->getKind()) {
1459-
case RequirementReprKind::LayoutConstraint:
1460-
case RequirementReprKind::TypeConstraint:
1461-
return requirementRepr->getColonLoc();
1457+
if (auto requirementRepr = getRequirementRepr())
1458+
return requirementRepr->getSeparatorLoc();
14621459

1463-
case RequirementReprKind::SameType:
1464-
return requirementRepr->getEqualLoc();
1465-
}
1466-
}
14671460
if (parent)
14681461
return parent->getLoc();
14691462

@@ -1681,22 +1674,24 @@ const RequirementSource *FloatingRequirementSource::getSource(
16811674
}
16821675

16831676
SourceLoc FloatingRequirementSource::getLoc() const {
1677+
// For an explicit abstract protocol source, we can get a more accurate source
1678+
// location from the written protocol requirement.
1679+
if (kind == Kind::AbstractProtocol && isExplicit()) {
1680+
auto written = protocolReq.written;
1681+
if (auto typeRepr = written.dyn_cast<const TypeRepr *>())
1682+
return typeRepr->getLoc();
1683+
if (auto requirementRepr = written.dyn_cast<const RequirementRepr *>())
1684+
return requirementRepr->getSeparatorLoc();
1685+
}
1686+
16841687
if (auto source = storage.dyn_cast<const RequirementSource *>())
16851688
return source->getLoc();
16861689

16871690
if (auto typeRepr = storage.dyn_cast<const TypeRepr *>())
16881691
return typeRepr->getLoc();
16891692

1690-
if (auto requirementRepr = storage.dyn_cast<const RequirementRepr *>()) {
1691-
switch (requirementRepr->getKind()) {
1692-
case RequirementReprKind::LayoutConstraint:
1693-
case RequirementReprKind::TypeConstraint:
1694-
return requirementRepr->getColonLoc();
1695-
1696-
case RequirementReprKind::SameType:
1697-
return requirementRepr->getEqualLoc();
1698-
}
1699-
}
1693+
if (auto requirementRepr = storage.dyn_cast<const RequirementRepr *>())
1694+
return requirementRepr->getSeparatorLoc();
17001695

17011696
return SourceLoc();
17021697
}
@@ -4586,7 +4581,7 @@ ConstraintResult GenericSignatureBuilder::addLayoutRequirement(
45864581
Impl->HadAnyError = true;
45874582

45884583
Diags.diagnose(source.getLoc(), diag::requires_not_suitable_archetype,
4589-
TypeLoc::withoutLoc(concreteType));
4584+
concreteType);
45904585
return ConstraintResult::Concrete;
45914586
}
45924587

@@ -4719,8 +4714,7 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
47194714

47204715
Impl->HadAnyError = true;
47214716
Diags.diagnose(source.getLoc(), diag::requires_conformance_nonprotocol,
4722-
TypeLoc::withoutLoc(subjectType),
4723-
TypeLoc::withoutLoc(constraintType));
4717+
subjectType, constraintType);
47244718
}
47254719

47264720
return ConstraintResult::Conflicting;
@@ -4768,7 +4762,7 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
47684762
if (source.getLoc().isValid()) {
47694763
Impl->HadAnyError = true;
47704764
Diags.diagnose(source.getLoc(), diag::requires_not_suitable_archetype,
4771-
TypeLoc::withoutLoc(subjectType));
4765+
subjectType);
47724766
}
47734767

47744768
return ConstraintResult::Concrete;
@@ -5356,7 +5350,7 @@ GenericSignatureBuilder::addRequirement(const Requirement &req,
53565350
!req.getSecondType()->hasTypeParameter() &&
53575351
!req.getFirstType()->hasError() &&
53585352
!req.getSecondType()->hasError()) {
5359-
Diags.diagnose(reqRepr->getEqualLoc(),
5353+
Diags.diagnose(reqRepr->getSeparatorLoc(),
53605354
diag::requires_no_same_type_archetype,
53615355
req.getFirstType(), req.getSecondType())
53625356
.highlight(reqRepr->getFirstTypeLoc().getSourceRange())

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,11 +2719,11 @@ void ConformanceChecker::checkNonFinalClassWitness(ValueDecl *requirement,
27192719
emitDeclaredHereIfNeeded(diags, diagLoc, witness);
27202720

27212721
if (auto requirementRepr = *constraint) {
2722-
diags.diagnose(requirementRepr->getEqualLoc(),
2722+
diags.diagnose(requirementRepr->getSeparatorLoc(),
27232723
diag::witness_self_weaken_same_type,
27242724
requirementRepr->getFirstType(),
27252725
requirementRepr->getSecondType())
2726-
.fixItReplace(requirementRepr->getEqualLoc(), ":");
2726+
.fixItReplace(requirementRepr->getSeparatorLoc(), ":");
27272727
}
27282728
}
27292729
}

test/NameBinding/scope_map_lookup.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ protocol P1 {
2323
}
2424

2525
// Protocols involving associated types.
26-
protocol AProtocol { // expected-error{{type 'Self.e' constrained to non-protocol, non-class type 'Self.e'}}
26+
protocol AProtocol {
2727
associatedtype e : e
2828
// expected-error@-1 {{inheritance from non-protocol, non-class type 'Self.e'}}
29+
// expected-error@-2 {{type 'Self.e' constrained to non-protocol, non-class type 'Self.e'}}
2930
}
3031

3132
// Extensions.

test/Sema/circular_decl_checking.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func TopLevelGenericFunc2<T : TopLevelGenericFunc2>(x: T) -> T { return x} // ex
4343
var TopLevelVar: TopLevelVar? { return nil } // expected-error {{use of undeclared type 'TopLevelVar'}}
4444

4545

46-
// FIXME: The first error is redundant, isn't correct in what it states, and
47-
// also should be emitted on the inheritance clause.
48-
protocol AProtocol { // expected-error {{type 'Self.e' constrained to non-protocol, non-class type 'Self.e'}}
49-
associatedtype e : e // expected-error {{inheritance from non-protocol, non-class type 'Self.e'}}
46+
// FIXME: The first error is redundant and isn't correct in what it states.
47+
protocol AProtocol {
48+
associatedtype e : e
49+
// expected-error@-1 {{type 'Self.e' constrained to non-protocol, non-class type 'Self.e'}}
50+
// expected-error@-2 {{inheritance from non-protocol, non-class type 'Self.e'}}
5051
}
5152

5253

test/decl/protocol/req/unsatisfiable.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,30 @@ protocol Base {
3737
associatedtype Assoc
3838
}
3939

40-
// FIXME: The first error is redundant, isn't correct in what it states, and
41-
// also should be emitted on the inheritance clause.
40+
// FIXME: The first error is redundant and isn't correct in what it states.
4241
// FIXME: This used to /not/ error in Swift 3. It didn't impose any statically-
4342
// enforced requirements, but the compiler crashed if you used anything but the
4443
// same type.
45-
protocol Sub1: Base { // expected-error {{type 'Self.SubAssoc' constrained to non-protocol, non-class type 'Self.Assoc'}}
46-
associatedtype SubAssoc: Assoc // expected-error {{inheritance from non-protocol, non-class type 'Self.Assoc'}}
44+
protocol Sub1: Base {
45+
associatedtype SubAssoc: Assoc
46+
// expected-error@-1 {{type 'Self.SubAssoc' constrained to non-protocol, non-class type 'Self.Assoc'}}
47+
// expected-error@-2 {{inheritance from non-protocol, non-class type 'Self.Assoc'}}
4748
}
48-
// FIXME: This error is incorrect in what it states and should be emitted on
49-
// the where-clause.
50-
protocol Sub2: Base { // expected-error {{type 'Self.SubAssoc' constrained to non-protocol, non-class type 'Self.Assoc'}}
51-
associatedtype SubAssoc where SubAssoc: Assoc
49+
50+
// FIXME: This error is incorrect in what it states.
51+
protocol Sub2: Base {
52+
associatedtype SubAssoc where SubAssoc: Assoc // expected-error {{type 'Self.SubAssoc' constrained to non-protocol, non-class type 'Self.Assoc'}}
53+
}
54+
55+
struct S {}
56+
57+
// FIX-ME: One of these errors is redundant.
58+
protocol P4 {
59+
associatedtype X : S
60+
// expected-error@-1 {{type 'Self.X' constrained to non-protocol, non-class type 'S'}}
61+
// expected-error@-2 {{inheritance from non-protocol, non-class type 'S'}}
62+
}
63+
64+
protocol P5 {
65+
associatedtype Y where Y : S // expected-error {{type 'Self.Y' constrained to non-protocol, non-class type 'S'}}
5266
}

0 commit comments

Comments
 (0)