Skip to content

Commit 4511262

Browse files
committed
Module selector fix-its in @_dynamicReplacement
1 parent 27ac539 commit 4511262

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,6 +3684,28 @@ static void lookupReplacedDecl(DeclNameRef replacedDeclName,
36843684
results);
36853685
}
36863686

3687+
static void
3688+
diagnoseCandidatesEliminatedByModuleSelector(DeclNameRefWithLoc replacedDeclName,
3689+
DeclAttribute *attr,
3690+
const ValueDecl *replacement,
3691+
DiagnosticEngine &Diags) {
3692+
if (replacedDeclName.Name.getModuleSelector().empty())
3693+
return;
3694+
3695+
// Look up without the module selector
3696+
SmallVector<ValueDecl *, 4> results;
3697+
lookupReplacedDecl(DeclNameRef(replacedDeclName.Name.getFullName()),
3698+
attr, replacement, results);
3699+
3700+
auto selectorLoc = replacedDeclName.Loc.getModuleSelectorLoc();
3701+
3702+
for (auto candidate : results)
3703+
Diags.diagnose(selectorLoc, diag::note_change_module_selector,
3704+
candidate->getModuleContext()->getBaseIdentifier())
3705+
.fixItReplace(selectorLoc,
3706+
candidate->getModuleContext()->getBaseIdentifier().str());
3707+
}
3708+
36873709
/// Remove any argument labels from the interface type of the given value that
36883710
/// are extraneous from the type system's point of view, producing the
36893711
/// type to compare against for the purposes of dynamic replacement.
@@ -3703,14 +3725,14 @@ static Type getDynamicComparisonType(ValueDecl *value) {
37033725
return interfaceType->removeArgumentLabels(numArgumentLabels);
37043726
}
37053727

3706-
static FuncDecl *findSimilarAccessor(DeclNameRef replacedVarName,
3728+
static FuncDecl *findSimilarAccessor(DeclNameRefWithLoc replacedVarName,
37073729
const AccessorDecl *replacement,
37083730
DeclAttribute *attr, ASTContext &ctx,
37093731
bool forDynamicReplacement) {
37103732

37113733
// Retrieve the replaced abstract storage decl.
37123734
SmallVector<ValueDecl *, 4> results;
3713-
lookupReplacedDecl(replacedVarName, attr, replacement, results);
3735+
lookupReplacedDecl(replacedVarName.Name, attr, replacement, results);
37143736

37153737
// Filter out any accessors that won't work.
37163738
if (!results.empty()) {
@@ -3743,15 +3765,19 @@ static FuncDecl *findSimilarAccessor(DeclNameRef replacedVarName,
37433765
if (results.empty()) {
37443766
Diags.diagnose(attr->getLocation(),
37453767
diag::dynamic_replacement_accessor_not_found,
3746-
replacedVarName);
3768+
replacedVarName.Name);
37473769
attr->setInvalid();
3770+
3771+
diagnoseCandidatesEliminatedByModuleSelector(replacedVarName, attr,
3772+
replacement, Diags);
3773+
37483774
return nullptr;
37493775
}
37503776

37513777
if (results.size() > 1) {
37523778
Diags.diagnose(attr->getLocation(),
37533779
diag::dynamic_replacement_accessor_ambiguous,
3754-
replacedVarName);
3780+
replacedVarName.Name);
37553781
for (auto result : results) {
37563782
Diags.diagnose(result,
37573783
diag::dynamic_replacement_accessor_ambiguous_candidate,
@@ -3793,15 +3819,15 @@ static FuncDecl *findSimilarAccessor(DeclNameRef replacedVarName,
37933819
return origAccessor;
37943820
}
37953821

3796-
static FuncDecl *findReplacedAccessor(DeclNameRef replacedVarName,
3822+
static FuncDecl *findReplacedAccessor(DeclNameRefWithLoc replacedVarName,
37973823
const AccessorDecl *replacement,
37983824
DeclAttribute *attr,
37993825
ASTContext &ctx) {
38003826
return findSimilarAccessor(replacedVarName, replacement, attr, ctx,
38013827
/*forDynamicReplacement*/ true);
38023828
}
38033829

3804-
static FuncDecl *findTargetAccessor(DeclNameRef replacedVarName,
3830+
static FuncDecl *findTargetAccessor(DeclNameRefWithLoc replacedVarName,
38053831
const AccessorDecl *replacement,
38063832
DeclAttribute *attr,
38073833
ASTContext &ctx) {
@@ -3810,15 +3836,15 @@ static FuncDecl *findTargetAccessor(DeclNameRef replacedVarName,
38103836
}
38113837

38123838
static AbstractFunctionDecl *
3813-
findSimilarFunction(DeclNameRef replacedFunctionName,
3839+
findSimilarFunction(DeclNameRefWithLoc replacedFunctionName,
38143840
const AbstractFunctionDecl *base, DeclAttribute *attr,
38153841
DiagnosticEngine *Diags, bool forDynamicReplacement) {
38163842

38173843
// Note: we might pass a constant attribute when typechecker is nullptr.
38183844
// Any modification to attr must be guarded by a null check on TC.
38193845
//
38203846
SmallVector<ValueDecl *, 4> lookupResults;
3821-
lookupReplacedDecl(replacedFunctionName, attr, base, lookupResults);
3847+
lookupReplacedDecl(replacedFunctionName.Name, attr, base, lookupResults);
38223848

38233849
SmallVector<AbstractFunctionDecl *, 4> candidates;
38243850
for (auto *result : lookupResults) {
@@ -3839,7 +3865,9 @@ findSimilarFunction(DeclNameRef replacedFunctionName,
38393865
forDynamicReplacement
38403866
? diag::dynamic_replacement_function_not_found
38413867
: diag::specialize_target_function_not_found,
3842-
replacedFunctionName);
3868+
replacedFunctionName.Name);
3869+
diagnoseCandidatesEliminatedByModuleSelector(replacedFunctionName, attr,
3870+
base, *Diags);
38433871
}
38443872

38453873
attr->setInvalid();
@@ -3905,7 +3933,7 @@ findSimilarFunction(DeclNameRef replacedFunctionName,
39053933
forDynamicReplacement
39063934
? diag::dynamic_replacement_function_of_type_not_found
39073935
: diag::specialize_target_function_of_type_not_found,
3908-
replacedFunctionName,
3936+
replacedFunctionName.Name,
39093937
base->getInterfaceType()->getCanonicalType());
39103938

39113939
for (auto *result : matches) {
@@ -3921,15 +3949,15 @@ findSimilarFunction(DeclNameRef replacedFunctionName,
39213949
}
39223950

39233951
static AbstractFunctionDecl *
3924-
findReplacedFunction(DeclNameRef replacedFunctionName,
3952+
findReplacedFunction(DeclNameRefWithLoc replacedFunctionName,
39253953
const AbstractFunctionDecl *replacement,
39263954
DynamicReplacementAttr *attr, DiagnosticEngine *Diags) {
39273955
return findSimilarFunction(replacedFunctionName, replacement, attr, Diags,
39283956
true /*forDynamicReplacement*/);
39293957
}
39303958

39313959
static AbstractFunctionDecl *
3932-
findTargetFunction(DeclNameRef targetFunctionName,
3960+
findTargetFunction(DeclNameRefWithLoc targetFunctionName,
39333961
const AbstractFunctionDecl *base,
39343962
SpecializeAttr * attr, DiagnosticEngine *diags) {
39353963
return findSimilarFunction(targetFunctionName, base, attr, diags,
@@ -5587,13 +5615,15 @@ DynamicallyReplacedDeclRequest::evaluate(Evaluator &evaluator,
55875615
}
55885616

55895617
auto &Ctx = VD->getASTContext();
5618+
DeclNameRefWithLoc nameWithLoc{attr->getReplacedFunctionName(),
5619+
attr->getReplacedFunctionNameLoc(),
5620+
std::nullopt};
55905621
if (auto *AD = dyn_cast<AccessorDecl>(VD)) {
5591-
return findReplacedAccessor(attr->getReplacedFunctionName(), AD, attr, Ctx);
5622+
return findReplacedAccessor(nameWithLoc, AD, attr, Ctx);
55925623
}
55935624

55945625
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
5595-
return findReplacedFunction(attr->getReplacedFunctionName(), AFD,
5596-
attr, &Ctx.Diags);
5626+
return findReplacedFunction(nameWithLoc, AFD, attr, &Ctx.Diags);
55975627
}
55985628

55995629
if (auto *SD = dyn_cast<AbstractStorageDecl>(VD)) {
@@ -5616,8 +5646,10 @@ SpecializeAttrTargetDeclRequest::evaluate(Evaluator &evaluator,
56165646

56175647
auto &ctx = vd->getASTContext();
56185648

5619-
auto targetFunctionName = attr->getTargetFunctionName();
5620-
if (!targetFunctionName)
5649+
DeclNameRefWithLoc targetFunctionName{attr->getTargetFunctionName(),
5650+
attr->getTargetFunctionNameLoc(),
5651+
std::nullopt};
5652+
if (!targetFunctionName.Name)
56215653
return nullptr;
56225654

56235655
if (auto *ad = dyn_cast<AccessorDecl>(vd)) {

test/NameLookup/module_selector.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ extension B: main::Equatable {
8383
// @_derivative(of:)
8484

8585
@_dynamicReplacement(for: main::negate())
86-
// FIXME improve: expected-error@-1 {{replaced function 'main::negate()' could not be found}}
86+
// FIXME shouldn't this succeed?: expected-error@-1 {{replaced function 'main::negate()' could not be found}}
87+
// FIXME: expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{29-33=ModuleSelectorTestingKit}}
8788
mutating func myNegate() {
8889
let fn: (main::Int, main::Int) -> main::Int =
8990
// expected-error@-1 3{{type 'Int' is not imported through module 'main'}}
@@ -206,7 +207,8 @@ extension D: @retroactive Swift::Equatable {
206207
// @_derivative(of:)
207208

208209
@_dynamicReplacement(for: Swift::negate())
209-
// FIXME improve: expected-error@-1 {{replaced function 'Swift::negate()' could not be found}}
210+
// expected-error@-1 {{replaced function 'Swift::negate()' could not be found}}
211+
// expected-note@-2 {{did you mean module 'ModuleSelectorTestingKit'?}} {{29-34=ModuleSelectorTestingKit}}
210212
mutating func myNegate() {
211213
// FIXME improve: expected-note@-1 {{did you mean 'myNegate'?}}
212214

0 commit comments

Comments
 (0)