Skip to content

Commit 0b5ca9a

Browse files
committed
[NFC] Audit some DeclName➡️DeclNameRef conversions
Adds comments explaining why these conversions aren’t lossy.
1 parent 7e81332 commit 0b5ca9a

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
@@ -2995,11 +2995,10 @@ class ValueDecl : public Decl {
29952995
return Name.getBaseIdentifier();
29962996
}
29972997

2998-
/// Generates a DeclNameRef referring to this declaration with as much
2999-
/// specificity as possible.
3000-
DeclNameRef createNameRef() const {
3001-
return DeclNameRef(Name);
3002-
}
2998+
/// Generates a DeclNameRef referring to this declaration.
2999+
///
3000+
/// \param moduleSelector If true, the name ref includes the module name.
3001+
DeclNameRef createNameRef(bool moduleSelector = false) const;
30033002

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

lib/AST/Decl.cpp

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

2871+
DeclNameRef ValueDecl::createNameRef(bool moduleSelector) const {
2872+
if (moduleSelector)
2873+
return DeclNameRef(getASTContext(), getModuleContext()->getName(),
2874+
getName());
2875+
2876+
return DeclNameRef(getName());
2877+
}
2878+
28712879
static bool isPolymorphic(const AbstractStorageDecl *storage) {
28722880
if (storage->shouldUseObjCDispatch())
28732881
return true;

lib/Sema/CSApply.cpp

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

73997399
case ConversionRestrictionKind::CGFloatToDouble:
74007400
case ConversionRestrictionKind::DoubleToCGFloat: {
7401-
DeclName name(ctx, DeclBaseName::createConstructor(), Identifier());
7401+
// OK: Implicit conversion, no module selector to drop here.
7402+
DeclNameRef initRef(ctx, /*module selector=*/Identifier(),
7403+
DeclBaseName::createConstructor(), { Identifier() });
74027404

74037405
ConstructorDecl *decl = nullptr;
74047406
SmallVector<ValueDecl *, 2> candidates;
7405-
dc->lookupQualified(toType->getAnyNominal(),
7406-
DeclNameRef(name), SourceLoc(),
7407+
dc->lookupQualified(toType->getAnyNominal(), initRef, SourceLoc(),
74077408
NL_QualifiedDefault, candidates);
74087409
for (auto *candidate : candidates) {
74097410
auto *ctor = cast<ConstructorDecl>(candidate);

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,10 +3824,9 @@ namespace {
38243824
componentTypeVars.push_back(memberTy);
38253825
auto lookupName =
38263826
kind == KeyPathExpr::Component::Kind::UnresolvedMember
3827-
? DeclNameRef(
3828-
component.getUnresolvedDeclName()) // FIXME: type change
3829-
// needed
3830-
: component.getDeclRef().getDecl()->createNameRef();
3827+
? component.getUnresolvedDeclName()
3828+
: component.getDeclRef().getDecl()->createNameRef(
3829+
/*modSel=*/true);
38313830

38323831
auto refKind = component.getFunctionRefInfo();
38333832
CS.addValueMemberConstraint(base, lookupName, memberTy, CurDC,
@@ -5242,10 +5241,14 @@ ResolvedMemberResult swift::resolveValueMember(DeclContext &DC, Type BaseTy,
52425241
ResolvedMemberResult Result;
52435242
ConstraintSystem CS(&DC, std::nullopt);
52445243

5244+
// OK: By contract, `Name` should be derived from an existing Decl, not a
5245+
// name written by the user in source code. So when used as intended, we won't
5246+
// be dropping a module selector here.
5247+
DeclNameRef NameRef(Name);
52455248
// Look up all members of BaseTy with the given Name.
52465249
MemberLookupResult LookupResult =
5247-
CS.performMemberLookup(ConstraintKind::ValueMember, DeclNameRef(Name),
5248-
BaseTy, FunctionRefInfo::singleBaseNameApply(),
5250+
CS.performMemberLookup(ConstraintKind::ValueMember, NameRef, BaseTy,
5251+
FunctionRefInfo::singleBaseNameApply(),
52495252
CS.getConstraintLocator({}), false);
52505253

52515254
// 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
@@ -3164,10 +3164,11 @@ SynthesizeMainFunctionRequest::evaluate(Evaluator &evaluator,
31643164
}
31653165

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

31733174
FuncDecl *mainFunction = nullptr;

0 commit comments

Comments
 (0)