Skip to content

Commit e662993

Browse files
committed
[NFC] Audit some DeclName➡️DeclNameRef conversions
Adds comments explaining why these conversions aren’t lossy.
1 parent 64d8b70 commit e662993

File tree

6 files changed

+33
-18
lines changed

6 files changed

+33
-18
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3072,11 +3072,10 @@ class ValueDecl : public Decl {
30723072
return Name.getBaseIdentifier();
30733073
}
30743074

3075-
/// Generates a DeclNameRef referring to this declaration with as much
3076-
/// specificity as possible.
3077-
DeclNameRef createNameRef() const {
3078-
return DeclNameRef(Name);
3079-
}
3075+
/// Generates a DeclNameRef referring to this declaration.
3076+
///
3077+
/// \param moduleSelector If true, the name ref includes the module name.
3078+
DeclNameRef createNameRef(bool moduleSelector = false) const;
30803079

30813080
/// Retrieve the C declaration name that names this function, or empty
30823081
/// string if it has none.

lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,6 +2935,14 @@ SourceRange TopLevelCodeDecl::getSourceRange() const {
29352935
return Body? Body->getSourceRange() : SourceRange();
29362936
}
29372937

2938+
DeclNameRef ValueDecl::createNameRef(bool moduleSelector) const {
2939+
if (moduleSelector)
2940+
return DeclNameRef(getASTContext(), getModuleContext()->getName(),
2941+
getName());
2942+
2943+
return DeclNameRef(getName());
2944+
}
2945+
29382946
static bool isPolymorphic(const AbstractStorageDecl *storage) {
29392947
if (storage->shouldUseObjCDispatch())
29402948
return true;

lib/Sema/CSApply.cpp

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

73707370
case ConversionRestrictionKind::CGFloatToDouble:
73717371
case ConversionRestrictionKind::DoubleToCGFloat: {
7372-
DeclName name(ctx, DeclBaseName::createConstructor(), Identifier());
7372+
// OK: Implicit conversion, no module selector to drop here.
7373+
DeclNameRef initRef(ctx, /*module selector=*/Identifier(),
7374+
DeclBaseName::createConstructor(), { Identifier() });
73737375

73747376
ConstructorDecl *decl = nullptr;
73757377
SmallVector<ValueDecl *, 2> candidates;
7376-
dc->lookupQualified(toType->getAnyNominal(),
7377-
DeclNameRef(name), SourceLoc(),
7378+
dc->lookupQualified(toType->getAnyNominal(), initRef, SourceLoc(),
73787379
NL_QualifiedDefault, candidates);
73797380
for (auto *candidate : candidates) {
73807381
auto *ctor = cast<ConstructorDecl>(candidate);

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,10 +3870,9 @@ namespace {
38703870
componentTypeVars.push_back(memberTy);
38713871
auto lookupName =
38723872
kind == KeyPathExpr::Component::Kind::UnresolvedMember
3873-
? DeclNameRef(
3874-
component.getUnresolvedDeclName()) // FIXME: type change
3875-
// needed
3876-
: component.getDeclRef().getDecl()->createNameRef();
3873+
? component.getUnresolvedDeclName()
3874+
: component.getDeclRef().getDecl()->createNameRef(
3875+
/*modSel=*/true);
38773876

38783877
auto refKind = component.getFunctionRefInfo();
38793878
CS.addValueMemberConstraint(base, lookupName, memberTy, CurDC,
@@ -5306,10 +5305,14 @@ ResolvedMemberResult swift::resolveValueMember(DeclContext &DC, Type BaseTy,
53065305
ResolvedMemberResult Result;
53075306
ConstraintSystem CS(&DC, std::nullopt);
53085307

5308+
// OK: By contract, `Name` should be derived from an existing Decl, not a
5309+
// name written by the user in source code. So when used as intended, we won't
5310+
// be dropping a module selector here.
5311+
DeclNameRef NameRef(Name);
53095312
// Look up all members of BaseTy with the given Name.
53105313
MemberLookupResult LookupResult =
5311-
CS.performMemberLookup(ConstraintKind::ValueMember, DeclNameRef(Name),
5312-
BaseTy, FunctionRefInfo::singleBaseNameApply(),
5314+
CS.performMemberLookup(ConstraintKind::ValueMember, NameRef, BaseTy,
5315+
FunctionRefInfo::singleBaseNameApply(),
53135316
CS.getConstraintLocator({}), false);
53145317

53155318
// 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
@@ -8627,6 +8627,9 @@ ConstraintSystem::simplifyConstructionConstraint(
86278627
// variable T. T2 is the result type provided via the construction
86288628
// constraint itself.
86298629
addValueMemberConstraint(MetatypeType::get(valueType, getASTContext()),
8630+
// OK: simplifyConstructionConstraint() is only used
8631+
// for `T(...)` init calls, not `T.init(...)` calls,
8632+
// so there's no module selector on `init`.
86308633
DeclNameRef::createConstructor(),
86318634
memberType,
86328635
useDC, functionRefInfo,

lib/Sema/TypeCheckAttr.cpp

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

32723272
CS.addDisjunctionConstraint(typeEqualityConstraints, locator);
3273-
CS.addValueMemberConstraint(
3274-
nominal->getInterfaceType(), DeclNameRef(context.Id_main),
3275-
Type(mainType), declContext, FunctionRefInfo::singleBaseNameApply(), {},
3276-
locator);
3273+
CS.addValueMemberConstraint(nominal->getInterfaceType(),
3274+
DeclNameRef(context.Id_main), // OK: Generated
3275+
Type(mainType), declContext,
3276+
FunctionRefInfo::singleBaseNameApply(), {},
3277+
locator);
32773278
}
32783279

32793280
FuncDecl *mainFunction = nullptr;

0 commit comments

Comments
 (0)