Skip to content

Commit 152718c

Browse files
committed
[NFC] Additional DeclNameRef auditing
1 parent 610d99b commit 152718c

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7404,12 +7404,13 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
74047404

74057405
case ConversionRestrictionKind::CGFloatToDouble:
74067406
case ConversionRestrictionKind::DoubleToCGFloat: {
7407-
DeclName name(ctx, DeclBaseName::createConstructor(), Identifier());
7407+
// OK: Implicit conversion, no module selector to drop here.
7408+
DeclNameRef initRef(ctx, /*module selector=*/Identifier(),
7409+
DeclBaseName::createConstructor(), { Identifier() });
74087410

74097411
ConstructorDecl *decl = nullptr;
74107412
SmallVector<ValueDecl *, 2> candidates;
7411-
dc->lookupQualified(toType->getAnyNominal(),
7412-
DeclNameRef(name), SourceLoc(),
7413+
dc->lookupQualified(toType->getAnyNominal(), initRef, SourceLoc(),
74137414
NL_QualifiedDefault, candidates);
74147415
for (auto *candidate : candidates) {
74157416
auto *ctor = cast<ConstructorDecl>(candidate);

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,10 +3809,9 @@ namespace {
38093809
componentTypeVars.push_back(memberTy);
38103810
auto lookupName =
38113811
kind == KeyPathExpr::Component::Kind::UnresolvedMember
3812-
? DeclNameRef(
3813-
component.getUnresolvedDeclName()) // FIXME: type change
3814-
// needed
3815-
: component.getDeclRef().getDecl()->createNameRef();
3812+
? component.getUnresolvedDeclName()
3813+
: component.getDeclRef().getDecl()->createNameRef(
3814+
/*modSel=*/true);
38163815

38173816
auto refKind = component.getFunctionRefInfo();
38183817
CS.addValueMemberConstraint(base, lookupName, memberTy, CurDC,
@@ -5227,10 +5226,14 @@ ResolvedMemberResult swift::resolveValueMember(DeclContext &DC, Type BaseTy,
52275226
ResolvedMemberResult Result;
52285227
ConstraintSystem CS(&DC, std::nullopt);
52295228

5229+
// OK: By contract, `Name` should be derived from an existing Decl, not a
5230+
// name written by the user in source code. So when used as intended, we won't
5231+
// be dropping a module selector here.
5232+
DeclNameRef NameRef(Name);
52305233
// Look up all members of BaseTy with the given Name.
52315234
MemberLookupResult LookupResult =
5232-
CS.performMemberLookup(ConstraintKind::ValueMember, DeclNameRef(Name),
5233-
BaseTy, FunctionRefInfo::singleBaseNameApply(),
5235+
CS.performMemberLookup(ConstraintKind::ValueMember, NameRef, BaseTy,
5236+
FunctionRefInfo::singleBaseNameApply(),
52345237
CS.getConstraintLocator({}), false);
52355238

52365239
// Keep track of all the unviable members.

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8488,6 +8488,9 @@ ConstraintSystem::simplifyConstructionConstraint(
84888488
// variable T. T2 is the result type provided via the construction
84898489
// constraint itself.
84908490
addValueMemberConstraint(MetatypeType::get(valueType, getASTContext()),
8491+
// OK: simplifyConstructionConstraint() is only used
8492+
// for `T(...)` init calls, not `T.init(...)` calls,
8493+
// so there's no module selector on `init`.
84918494
DeclNameRef::createConstructor(),
84928495
memberType,
84938496
useDC, functionRefInfo,

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,10 +3163,11 @@ SynthesizeMainFunctionRequest::evaluate(Evaluator &evaluator,
31633163
}
31643164

31653165
CS.addDisjunctionConstraint(typeEqualityConstraints, locator);
3166-
CS.addValueMemberConstraint(
3167-
nominal->getInterfaceType(), DeclNameRef(context.Id_main),
3168-
Type(mainType), declContext, FunctionRefInfo::singleBaseNameApply(), {},
3169-
locator);
3166+
CS.addValueMemberConstraint(nominal->getInterfaceType(),
3167+
DeclNameRef(context.Id_main), // OK: Generated
3168+
Type(mainType), declContext,
3169+
FunctionRefInfo::singleBaseNameApply(), {},
3170+
locator);
31703171
}
31713172

31723173
FuncDecl *mainFunction = nullptr;

test/NameLookup/module_selector.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// * Whether we handle access paths correctly
1212
// * Interaction with ClangImporter
1313
// * Cross-import overlays
14+
// * Key paths
15+
// * Key path dynamic member lookup
1416
//
1517
// It also might not cover all combinations of name lookup paths and inputs.
1618

0 commit comments

Comments
 (0)