Skip to content

Commit caa328a

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

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
@@ -3038,11 +3038,10 @@ class ValueDecl : public Decl {
30383038
return Name.getBaseIdentifier();
30393039
}
30403040

3041-
/// Generates a DeclNameRef referring to this declaration with as much
3042-
/// specificity as possible.
3043-
DeclNameRef createNameRef() const {
3044-
return DeclNameRef(Name);
3045-
}
3041+
/// Generates a DeclNameRef referring to this declaration.
3042+
///
3043+
/// \param moduleSelector If true, the name ref includes the module name.
3044+
DeclNameRef createNameRef(bool moduleSelector = false) const;
30463045

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

lib/AST/Decl.cpp

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

2932+
DeclNameRef ValueDecl::createNameRef(bool moduleSelector) const {
2933+
if (moduleSelector)
2934+
return DeclNameRef(getASTContext(), getModuleContext()->getName(),
2935+
getName());
2936+
2937+
return DeclNameRef(getName());
2938+
}
2939+
29322940
static bool isPolymorphic(const AbstractStorageDecl *storage) {
29332941
if (storage->shouldUseObjCDispatch())
29342942
return true;

lib/Sema/CSApply.cpp

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

74617461
case ConversionRestrictionKind::CGFloatToDouble:
74627462
case ConversionRestrictionKind::DoubleToCGFloat: {
7463-
DeclName name(ctx, DeclBaseName::createConstructor(), Identifier());
7463+
// OK: Implicit conversion, no module selector to drop here.
7464+
DeclNameRef initRef(ctx, /*module selector=*/Identifier(),
7465+
DeclBaseName::createConstructor(), { Identifier() });
74647466

74657467
ConstructorDecl *decl = nullptr;
74667468
SmallVector<ValueDecl *, 2> candidates;
7467-
dc->lookupQualified(toType->getAnyNominal(),
7468-
DeclNameRef(name), SourceLoc(),
7469+
dc->lookupQualified(toType->getAnyNominal(), initRef, SourceLoc(),
74697470
NL_QualifiedDefault, candidates);
74707471
for (auto *candidate : candidates) {
74717472
auto *ctor = cast<ConstructorDecl>(candidate);

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3853,10 +3853,9 @@ namespace {
38533853
componentTypeVars.push_back(memberTy);
38543854
auto lookupName =
38553855
kind == KeyPathExpr::Component::Kind::UnresolvedMember
3856-
? DeclNameRef(
3857-
component.getUnresolvedDeclName()) // FIXME: type change
3858-
// needed
3859-
: component.getDeclRef().getDecl()->createNameRef();
3856+
? component.getUnresolvedDeclName()
3857+
: component.getDeclRef().getDecl()->createNameRef(
3858+
/*modSel=*/true);
38603859

38613860
auto refKind = component.getFunctionRefInfo();
38623861
CS.addValueMemberConstraint(base, lookupName, memberTy, CurDC,
@@ -5290,10 +5289,14 @@ ResolvedMemberResult swift::resolveValueMember(DeclContext &DC, Type BaseTy,
52905289
ResolvedMemberResult Result;
52915290
ConstraintSystem CS(&DC, std::nullopt);
52925291

5292+
// OK: By contract, `Name` should be derived from an existing Decl, not a
5293+
// name written by the user in source code. So when used as intended, we won't
5294+
// be dropping a module selector here.
5295+
DeclNameRef NameRef(Name);
52935296
// Look up all members of BaseTy with the given Name.
52945297
MemberLookupResult LookupResult =
5295-
CS.performMemberLookup(ConstraintKind::ValueMember, DeclNameRef(Name),
5296-
BaseTy, FunctionRefInfo::singleBaseNameApply(),
5298+
CS.performMemberLookup(ConstraintKind::ValueMember, NameRef, BaseTy,
5299+
FunctionRefInfo::singleBaseNameApply(),
52975300
CS.getConstraintLocator({}), false);
52985301

52995302
// 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
@@ -8567,6 +8567,9 @@ ConstraintSystem::simplifyConstructionConstraint(
85678567
// variable T. T2 is the result type provided via the construction
85688568
// constraint itself.
85698569
addValueMemberConstraint(MetatypeType::get(valueType, getASTContext()),
8570+
// OK: simplifyConstructionConstraint() is only used
8571+
// for `T(...)` init calls, not `T.init(...)` calls,
8572+
// so there's no module selector on `init`.
85708573
DeclNameRef::createConstructor(),
85718574
memberType,
85728575
useDC, functionRefInfo,

lib/Sema/TypeCheckAttr.cpp

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

32423242
CS.addDisjunctionConstraint(typeEqualityConstraints, locator);
3243-
CS.addValueMemberConstraint(
3244-
nominal->getInterfaceType(), DeclNameRef(context.Id_main),
3245-
Type(mainType), declContext, FunctionRefInfo::singleBaseNameApply(), {},
3246-
locator);
3243+
CS.addValueMemberConstraint(nominal->getInterfaceType(),
3244+
DeclNameRef(context.Id_main), // OK: Generated
3245+
Type(mainType), declContext,
3246+
FunctionRefInfo::singleBaseNameApply(), {},
3247+
locator);
32473248
}
32483249

32493250
FuncDecl *mainFunction = nullptr;

0 commit comments

Comments
 (0)