Skip to content

Commit 3e9e253

Browse files
committed
[ConstraintSystem] Add a new locator element - argument attribute
`ArgumentAttribute` points to a particular attribute associated with one of the arguments e.g. `inout` or its type e.g. `@escaping`. This is very useful when dealing with argument-to-parameter failures because it allows to express in the locator kind of a problem.
1 parent e85b658 commit 3e9e253

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

lib/Sema/ConstraintLocator.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, ASTNode anchor,
5757
id.AddPointer(elt.castTo<LocatorPathElt::PatternMatch>().getPattern());
5858
break;
5959

60+
case ArgumentAttribute:
6061
case GenericArgument:
6162
case NamedTupleElement:
6263
case TupleElement:
@@ -125,6 +126,7 @@ unsigned LocatorPathElt::getNewSummaryFlags() const {
125126
case ConstraintLocator::ImplicitCallAsFunction:
126127
case ConstraintLocator::TernaryBranch:
127128
case ConstraintLocator::PatternMatch:
129+
case ConstraintLocator::ArgumentAttribute:
128130
return 0;
129131

130132
case ConstraintLocator::FunctionArgument:
@@ -500,6 +502,25 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) const {
500502
case PatternMatch:
501503
out << "pattern match";
502504
break;
505+
506+
case ArgumentAttribute: {
507+
using AttrLoc = LocatorPathElt::ArgumentAttribute;
508+
509+
auto attrElt = elt.castTo<AttrLoc>();
510+
out << "argument attribute: ";
511+
512+
switch (attrElt.getAttr()) {
513+
case AttrLoc::Attribute::InOut:
514+
out << "inout";
515+
break;
516+
517+
case AttrLoc::Attribute::Escaping:
518+
out << "@escaping";
519+
break;
520+
}
521+
522+
break;
523+
}
503524
}
504525
}
505526
out << ']';

lib/Sema/ConstraintLocator.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
8686
case SynthesizedArgument:
8787
case KeyPathDynamicMember:
8888
case TernaryBranch:
89+
case ArgumentAttribute:
8990
return 1;
9091

9192
case TypeParameterRequirement:
@@ -847,6 +848,31 @@ class LocatorPathElt::PatternMatch final : public LocatorPathElt {
847848
}
848849
};
849850

851+
class LocatorPathElt::ArgumentAttribute final : public LocatorPathElt {
852+
public:
853+
enum Attribute : uint8_t { InOut, Escaping };
854+
855+
private:
856+
ArgumentAttribute(Attribute attr)
857+
: LocatorPathElt(ConstraintLocator::ArgumentAttribute,
858+
static_cast<uint8_t>(attr)) {}
859+
860+
public:
861+
Attribute getAttr() const { return static_cast<Attribute>(getValue(0)); }
862+
863+
static ArgumentAttribute forInOut() {
864+
return ArgumentAttribute(Attribute::InOut);
865+
}
866+
867+
static ArgumentAttribute forEscaping() {
868+
return ArgumentAttribute(Attribute::Escaping);
869+
}
870+
871+
static bool classof(const LocatorPathElt *elt) {
872+
return elt->getKind() == ConstraintLocator::ArgumentAttribute;
873+
}
874+
};
875+
850876
/// A simple stack-only builder object that constructs a
851877
/// constraint locator without allocating memory.
852878
///

lib/Sema/ConstraintLocatorPathElts.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ CUSTOM_LOCATOR_PATH_ELT(TernaryBranch)
181181
/// Performing a pattern patch.
182182
CUSTOM_LOCATOR_PATH_ELT(PatternMatch)
183183

184+
/// Points to a particular attribute associated with one of
185+
/// the arguments e.g. `inout` or its type e.g. `@escaping`.
186+
///
187+
/// This is very useful when dealing with argument-to-parameter
188+
/// failures because it allows to express in the locator kind
189+
/// of a problem.
190+
CUSTOM_LOCATOR_PATH_ELT(ArgumentAttribute)
191+
184192
#undef LOCATOR_PATH_ELT
185193
#undef CUSTOM_LOCATOR_PATH_ELT
186194
#undef SIMPLE_LOCATOR_PATH_ELT

0 commit comments

Comments
 (0)