Skip to content

Commit 2c54a7b

Browse files
authored
Merge pull request swiftlang#5663 from DougGregor/remove-type-member
2 parents 15f4ec9 + e20464a commit 2c54a7b

File tree

7 files changed

+10
-145
lines changed

7 files changed

+10
-145
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,25 +2109,6 @@ static bool isConversionConstraint(const Constraint *C) {
21092109
return C->getClassification() == ConstraintClassification::Relational;
21102110
}
21112111

2112-
/// Return true if this member constraint is a low priority for diagnostics, so
2113-
/// low that we would only like to issue an error message about it if there is
2114-
/// nothing else interesting we can scrape out of the constraint system.
2115-
static bool isLowPriorityConstraint(Constraint *C) {
2116-
// If the member constraint is a ".Iterator" lookup to find the iterator
2117-
// type in a foreach loop, or a ".Element" lookup to find its element type,
2118-
// then it is very low priority: We will get a better and more useful
2119-
// diagnostic from the failed conversion to Sequence that will fail as well.
2120-
if (C->getKind() == ConstraintKind::TypeMember) {
2121-
if (auto *loc = C->getLocator())
2122-
for (auto Elt : loc->getPath())
2123-
if (Elt.getKind() == ConstraintLocator::GeneratorElementType ||
2124-
Elt.getKind() == ConstraintLocator::SequenceIteratorProtocol)
2125-
return true;
2126-
}
2127-
2128-
return false;
2129-
}
2130-
21312112
/// Attempt to diagnose a failure without taking into account the specific
21322113
/// kind of expression that could not be type checked.
21332114
bool FailureDiagnosis::diagnoseConstraintFailure() {
@@ -2148,9 +2129,6 @@ bool FailureDiagnosis::diagnoseConstraintFailure() {
21482129
// This is a predicate that classifies constraints according to our
21492130
// priorities.
21502131
std::function<void (Constraint*)> classifyConstraint = [&](Constraint *C) {
2151-
if (isLowPriorityConstraint(C))
2152-
return rankedConstraints.push_back({C, CR_OtherConstraint});
2153-
21542132
if (isMemberConstraint(C))
21552133
return rankedConstraints.push_back({C, CR_MemberConstraint});
21562134

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@ matchCallArguments(ConstraintSystem &cs, ConstraintKind kind,
686686
case ConstraintKind::LiteralConformsTo:
687687
case ConstraintKind::OptionalObject:
688688
case ConstraintKind::SelfObjectOfProtocol:
689-
case ConstraintKind::TypeMember:
690689
case ConstraintKind::UnresolvedValueMember:
691690
case ConstraintKind::ValueMember:
692691
llvm_unreachable("Not a call argument constraint");
@@ -813,7 +812,6 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
813812
case ConstraintKind::LiteralConformsTo:
814813
case ConstraintKind::OptionalObject:
815814
case ConstraintKind::SelfObjectOfProtocol:
816-
case ConstraintKind::TypeMember:
817815
case ConstraintKind::UnresolvedValueMember:
818816
case ConstraintKind::ValueMember:
819817
llvm_unreachable("Not a conversion");
@@ -940,7 +938,6 @@ static bool matchFunctionRepresentations(FunctionTypeRepresentation rep1,
940938
case ConstraintKind::LiteralConformsTo:
941939
case ConstraintKind::OptionalObject:
942940
case ConstraintKind::SelfObjectOfProtocol:
943-
case ConstraintKind::TypeMember:
944941
case ConstraintKind::UnresolvedValueMember:
945942
case ConstraintKind::ValueMember:
946943
return false;
@@ -1006,7 +1003,6 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
10061003
case ConstraintKind::LiteralConformsTo:
10071004
case ConstraintKind::OptionalObject:
10081005
case ConstraintKind::SelfObjectOfProtocol:
1009-
case ConstraintKind::TypeMember:
10101006
case ConstraintKind::UnresolvedValueMember:
10111007
case ConstraintKind::ValueMember:
10121008
llvm_unreachable("Not a relational constraint");
@@ -1431,7 +1427,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
14311427
case ConstraintKind::LiteralConformsTo:
14321428
case ConstraintKind::OptionalObject:
14331429
case ConstraintKind::SelfObjectOfProtocol:
1434-
case ConstraintKind::TypeMember:
14351430
case ConstraintKind::UnresolvedValueMember:
14361431
case ConstraintKind::ValueMember:
14371432
llvm_unreachable("Not a relational constraint");
@@ -2969,40 +2964,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
29692964
return result;
29702965
}
29712966

2972-
// If we want member types only, use member type lookup.
2973-
if (constraintKind == ConstraintKind::TypeMember) {
2974-
// Types don't have compound names.
2975-
// FIXME: Customize diagnostic to mention types and compound names.
2976-
if (!memberName.isSimpleName())
2977-
return result; // No result.
2978-
2979-
NameLookupOptions lookupOptions = defaultMemberTypeLookupOptions;
2980-
if (isa<AbstractFunctionDecl>(DC))
2981-
lookupOptions |= NameLookupFlags::KnownPrivate;
2982-
2983-
// If we're doing a lookup for diagnostics, include inaccessible members,
2984-
// the diagnostics machinery will sort it out.
2985-
if (includeInaccessibleMembers)
2986-
lookupOptions |= NameLookupFlags::IgnoreAccessibility;
2987-
2988-
auto lookup = TC.lookupMemberType(DC, baseObjTy, memberName.getBaseName(),
2989-
lookupOptions);
2990-
// Form the overload set.
2991-
for (auto candidate : lookup) {
2992-
// If the result is invalid, don't cascade errors.
2993-
TC.validateDecl(candidate.first, true);
2994-
if (candidate.first->isInvalid())
2995-
return result.markErrorAlreadyDiagnosed();
2996-
2997-
result.addViable(OverloadChoice(baseTy, candidate.first,
2998-
/*isSpecialized=*/false,
2999-
functionRefKind));
3000-
}
3001-
3002-
return result;
3003-
}
3004-
3005-
30062967
// Look for members within the base.
30072968
LookupResult &lookup = lookupMember(baseObjTy, memberName);
30082969

@@ -3303,32 +3264,6 @@ ConstraintSystem::simplifyMemberConstraint(ConstraintKind kind,
33033264
// If the lookup found no hits at all (either viable or unviable), diagnose it
33043265
// as such and try to recover in various ways.
33053266

3306-
if (kind == ConstraintKind::TypeMember) {
3307-
// If the base type was an optional, try to look through it.
3308-
if (shouldAttemptFixes() && baseObjTy->getOptionalObjectType()) {
3309-
// Determine whether or not we want to provide an optional chaining fixit or
3310-
// a force unwrap fixit.
3311-
bool optionalChain;
3312-
if (!getContextualType())
3313-
optionalChain = !(Options & ConstraintSystemFlags::PreferForceUnwrapToOptional);
3314-
else
3315-
optionalChain = !getContextualType()->getOptionalObjectType().isNull();
3316-
auto fixKind = optionalChain ? FixKind::OptionalChaining : FixKind::ForceOptional;
3317-
3318-
// Note the fix.
3319-
if (recordFix(fixKind, locator))
3320-
return SolutionKind::Error;
3321-
3322-
// Look through one level of optional.
3323-
addTypeMemberConstraint(baseObjTy->getOptionalObjectType(),
3324-
member, memberTy, locator);
3325-
return SolutionKind::Solved;
3326-
}
3327-
3328-
return SolutionKind::Error;
3329-
}
3330-
3331-
33323267
auto instanceTy = baseObjTy;
33333268
if (auto MTT = instanceTy->getAs<MetatypeType>())
33343269
instanceTy = MTT->getInstanceType();
@@ -4207,7 +4142,6 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
42074142

42084143
case ConstraintKind::ValueMember:
42094144
case ConstraintKind::UnresolvedValueMember:
4210-
case ConstraintKind::TypeMember:
42114145
case ConstraintKind::BindOverload:
42124146
case ConstraintKind::Disjunction:
42134147
llvm_unreachable("Use the correct addConstraint()");
@@ -4328,7 +4262,6 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
43284262

43294263
case ConstraintKind::ValueMember:
43304264
case ConstraintKind::UnresolvedValueMember:
4331-
case ConstraintKind::TypeMember:
43324265
return simplifyMemberConstraint(constraint.getKind(),
43334266
constraint.getFirstType(),
43344267
constraint.getMember(),

lib/Sema/CSSolver.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,6 @@ static bool shouldBindToValueType(Constraint *constraint)
720720
case ConstraintKind::DynamicTypeOf:
721721
case ConstraintKind::ValueMember:
722722
case ConstraintKind::UnresolvedValueMember:
723-
case ConstraintKind::TypeMember:
724723
case ConstraintKind::Defaultable:
725724
case ConstraintKind::Disjunction:
726725
llvm_unreachable("shouldBindToValueType() may only be called on "
@@ -950,7 +949,6 @@ static PotentialBindings getPotentialBindings(ConstraintSystem &cs,
950949

951950
case ConstraintKind::ValueMember:
952951
case ConstraintKind::UnresolvedValueMember:
953-
case ConstraintKind::TypeMember:
954952
// If our type variable shows up in the base type, there's
955953
// nothing to do.
956954
// FIXME: Can we avoid simplification here?

lib/Sema/Constraint.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
7777
assert(!Member && "Relational constraint cannot have a member");
7878
break;
7979

80-
case ConstraintKind::TypeMember:
8180
case ConstraintKind::ValueMember:
8281
case ConstraintKind::UnresolvedValueMember:
8382
assert(Member && "Member constraint has no member");
@@ -175,7 +174,6 @@ Constraint *Constraint::clone(ConstraintSystem &cs) const {
175174

176175
case ConstraintKind::ValueMember:
177176
case ConstraintKind::UnresolvedValueMember:
178-
case ConstraintKind::TypeMember:
179177
return create(cs, getKind(), getFirstType(), getSecondType(), getMember(),
180178
getFunctionRefKind(), getLocator());
181179

@@ -291,9 +289,6 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
291289
case ConstraintKind::UnresolvedValueMember:
292290
Out << "[(implicit) ." << Types.Member << ": value] == ";
293291
break;
294-
case ConstraintKind::TypeMember:
295-
Out << "[." << Types.Member << ": type] == ";
296-
break;
297292
case ConstraintKind::Defaultable:
298293
Out << " can default to ";
299294
break;
@@ -462,7 +457,6 @@ gatherReferencedTypeVars(Constraint *constraint,
462457
case ConstraintKind::CheckedCast:
463458
case ConstraintKind::Equal:
464459
case ConstraintKind::Subtype:
465-
case ConstraintKind::TypeMember:
466460
case ConstraintKind::UnresolvedValueMember:
467461
case ConstraintKind::ValueMember:
468462
case ConstraintKind::DynamicTypeOf:

lib/Sema/Constraint.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ enum class ConstraintKind : char {
114114
/// name, and the type of that member, when referenced as a value, is the
115115
/// second type.
116116
UnresolvedValueMember,
117-
/// \brief The first type has a type member with the given name, and the
118-
/// type of that member, when referenced as a type, is the second type.
119-
TypeMember,
120117
/// \brief The first type can be defaulted to the second (which currently
121118
/// cannot be dependent). This is more like a type property than a
122119
/// relational constraint.
@@ -485,7 +482,6 @@ class Constraint final : public llvm::ilist_node<Constraint>,
485482

486483
case ConstraintKind::ValueMember:
487484
case ConstraintKind::UnresolvedValueMember:
488-
case ConstraintKind::TypeMember:
489485
return ConstraintClassification::Member;
490486

491487
case ConstraintKind::DynamicTypeOf:
@@ -519,16 +515,14 @@ class Constraint final : public llvm::ilist_node<Constraint>,
519515
/// \brief Retrieve the name of the member for a member constraint.
520516
DeclName getMember() const {
521517
assert(Kind == ConstraintKind::ValueMember ||
522-
Kind == ConstraintKind::UnresolvedValueMember ||
523-
Kind == ConstraintKind::TypeMember);
518+
Kind == ConstraintKind::UnresolvedValueMember);
524519
return Types.Member;
525520
}
526521

527522
/// \brief Determine whether this constraint kind has a second type.
528523
static bool hasMember(ConstraintKind kind) {
529524
return kind == ConstraintKind::ValueMember
530-
|| kind == ConstraintKind::UnresolvedValueMember
531-
|| kind == ConstraintKind::TypeMember;
525+
|| kind == ConstraintKind::UnresolvedValueMember;
532526
}
533527

534528
/// Determine the kind of function reference we have for a member reference.

lib/Sema/ConstraintSystem.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,32 +1336,6 @@ class ConstraintSystem {
13361336
resolveOverload(locator, boundTy, choice);
13371337
}
13381338

1339-
/// \brief Add a value member constraint to the constraint system.
1340-
void addTypeMemberConstraint(Type baseTy, DeclName name, Type memberTy,
1341-
ConstraintLocatorBuilder locator) {
1342-
assert(baseTy);
1343-
assert(memberTy);
1344-
assert(name);
1345-
switch (simplifyMemberConstraint(ConstraintKind::TypeMember, baseTy, name,
1346-
memberTy, FunctionRefKind::Compound,
1347-
TMF_GenerateConstraints, locator)) {
1348-
case SolutionKind::Unsolved:
1349-
llvm_unreachable("Unsolved result when generating constraints!");
1350-
1351-
case SolutionKind::Solved:
1352-
break;
1353-
1354-
case SolutionKind::Error:
1355-
if (shouldAddNewFailingConstraint()) {
1356-
addNewFailingConstraint(
1357-
Constraint::create(*this, ConstraintKind::TypeMember, baseTy,
1358-
memberTy, name, FunctionRefKind::Compound,
1359-
getConstraintLocator(locator)));
1360-
}
1361-
break;
1362-
}
1363-
}
1364-
13651339
/// \brief Add a value member constraint to the constraint system.
13661340
void addValueMemberConstraint(Type baseTy, DeclName name, Type memberTy,
13671341
FunctionRefKind functionRefKind,

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,11 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
20172017

20182018
auto sequenceType = expr->getType()->getRValueType();
20192019

2020+
// Look through one level of optional; this improves recovery but doesn't
2021+
// change the result.
2022+
if (auto sequenceObjectType = sequenceType->getAnyOptionalObjectType())
2023+
sequenceType = sequenceObjectType;
2024+
20202025
// If the sequence type is an existential, we should not attempt to
20212026
// look up the member type at all, since we cannot represent associated
20222027
// types of existentials.
@@ -2053,27 +2058,16 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
20532058
}
20542059
}
20552060

2056-
// If the type lookup failed, just add some constraints we can
2057-
// try to solve later.
20582061
if (elementType.isNull()) {
2059-
// Determine the iterator type of the sequence.
2060-
iteratorType = cs.createTypeVariable(Locator, /*options=*/0);
2061-
cs.addTypeMemberConstraint(SequenceType, tc.Context.Id_Iterator,
2062-
iteratorType, iteratorLocator);
2063-
2064-
// Determine the element type of the iterator.
2065-
// FIXME: Should look up the type witness.
2066-
elementType = cs.createTypeVariable(Locator, /*options=*/0);
2067-
cs.addTypeMemberConstraint(iteratorType, tc.Context.Id_Element,
2068-
elementType, elementLocator);
2062+
elementType = cs.createTypeVariable(elementLocator,
2063+
TVO_MustBeMaterializable);
20692064
}
2070-
20712065

20722066
// Add a conversion constraint between the element type of the sequence
20732067
// and the type of the element pattern.
20742068
cs.addConstraint(ConstraintKind::Conversion, elementType, InitType,
20752069
elementLocator);
2076-
2070+
20772071
Stmt->setSequence(expr);
20782072
return false;
20792073
}

0 commit comments

Comments
 (0)