Skip to content

Commit 14d38e7

Browse files
committed
[NFC] Sema: Refactor for recursive MemberTypeRepr representation
1 parent bbf03c0 commit 14d38e7

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ class DeclRefTypeRepr : public TypeRepr {
383383
/// arguments.
384384
bool hasGenericArgList() const;
385385

386+
unsigned getNumGenericArgs() const;
387+
386388
ArrayRef<TypeRepr *> getGenericArgs() const;
387389

388390
SourceRange getAngleBrackets() const;

lib/AST/TypeRepr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ void DeclRefTypeRepr::setValue(TypeDecl *TD, DeclContext *DC) {
240240
getLastComponent()->setValue(TD, DC);
241241
}
242242

243+
unsigned DeclRefTypeRepr::getNumGenericArgs() const {
244+
auto *lastComp = const_cast<DeclRefTypeRepr *>(this)->getLastComponent();
245+
if (auto *genericTR = dyn_cast<GenericIdentTypeRepr>(lastComp)) {
246+
return genericTR->getNumGenericArgs();
247+
}
248+
249+
return 0;
250+
}
251+
243252
bool DeclRefTypeRepr::hasGenericArgList() const {
244253
return isa<GenericIdentTypeRepr>(
245254
const_cast<DeclRefTypeRepr *>(this)->getLastComponent());

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6757,10 +6757,11 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote(
67576757
auto diagnostic = emitDiagnosticAt(
67586758
baseType->getLoc(), diag::unbound_generic_parameter_explicit_fix);
67596759

6760-
if (auto *genericTy = dyn_cast<GenericIdentTypeRepr>(baseType)) {
6761-
// If some of the eneric arguments have been specified, we need to
6760+
auto *declRefTR = dyn_cast<DeclRefTypeRepr>(baseType);
6761+
if (declRefTR && declRefTR->getAngleBrackets().isValid()) {
6762+
// If some of the generic arguments have been specified, we need to
67626763
// replace existing signature with a new one.
6763-
diagnostic.fixItReplace(genericTy->getAngleBrackets(), paramsAsString);
6764+
diagnostic.fixItReplace(declRefTR->getAngleBrackets(), paramsAsString);
67646765
} else {
67656766
// Otherwise we can simply insert new generic signature.
67666767
diagnostic.fixItInsertAfter(baseType->getEndLoc(), paramsAsString);
@@ -6770,7 +6771,7 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote(
67706771

67716772
bool MissingGenericArgumentsFailure::findArgumentLocations(
67726773
llvm::function_ref<void(TypeRepr *, GenericTypeParamType *)> callback) {
6773-
using Callback = llvm::function_ref<void(TypeRepr *, GenericTypeParamType *)>;
6774+
using Callback = decltype(callback);
67746775

67756776
auto *const typeRepr = [this]() -> TypeRepr * {
67766777
const auto anchor = getRawAnchor();
@@ -6799,14 +6800,14 @@ bool MissingGenericArgumentsFailure::findArgumentLocations(
67996800
}
68006801

68016802
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
6802-
if (Params.empty())
6803-
return Action::SkipNode();
6803+
if (allParamsAssigned())
6804+
return Action::Stop();
68046805

6805-
auto *ident = dyn_cast<IdentTypeRepr>(T);
6806-
if (!ident)
6806+
auto *declRefTR = dyn_cast<DeclRefTypeRepr>(T);
6807+
if (!declRefTR)
68076808
return Action::Continue();
68086809

6809-
auto *decl = dyn_cast_or_null<GenericTypeDecl>(ident->getBoundDecl());
6810+
auto *decl = dyn_cast_or_null<GenericTypeDecl>(declRefTR->getBoundDecl());
68106811
if (!decl)
68116812
return Action::Continue();
68126813

@@ -6817,9 +6818,8 @@ bool MissingGenericArgumentsFailure::findArgumentLocations(
68176818
// There could a situation like `S<S>()`, so we need to be
68186819
// careful not to point at first `S` because it has all of
68196820
// its generic parameters specified.
6820-
if (auto *generic = dyn_cast<GenericIdentTypeRepr>(ident)) {
6821-
if (paramList->size() == generic->getNumGenericArgs())
6822-
return Action::Continue();
6821+
if (paramList->size() == declRefTR->getNumGenericArgs()) {
6822+
return Action::Continue();
68236823
}
68246824

68256825
for (auto *candidate : paramList->getParams()) {
@@ -6829,7 +6829,7 @@ bool MissingGenericArgumentsFailure::findArgumentLocations(
68296829
});
68306830

68316831
if (result != Params.end()) {
6832-
Fn(ident, *result);
6832+
Fn(declRefTR, *result);
68336833
Params.erase(result);
68346834
}
68356835
}

lib/Sema/TypeCheckAccess.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ static void highlightOffendingType(InFlightDiagnostic &diag,
361361
diag.highlight(complainRepr->getSourceRange());
362362
diag.flush();
363363

364-
if (auto ITR = dyn_cast<IdentTypeRepr>(complainRepr)) {
365-
const ValueDecl *VD = ITR->getBoundDecl();
364+
if (auto *declRefTR = dyn_cast<DeclRefTypeRepr>(complainRepr)) {
365+
const ValueDecl *VD = declRefTR->getBoundDecl();
366366
VD->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
367367
}
368368
}
@@ -378,8 +378,8 @@ static void noteLimitingImport(ASTContext &ctx,
378378
assert(limitImport->accessLevel != AccessLevel::Public &&
379379
"a public import shouldn't limit the access level of a decl");
380380

381-
if (auto ITR = dyn_cast_or_null<IdentTypeRepr>(complainRepr)) {
382-
ValueDecl *VD = ITR->getBoundDecl();
381+
if (auto *declRefTR = dyn_cast_or_null<DeclRefTypeRepr>(complainRepr)) {
382+
ValueDecl *VD = declRefTR->getBoundDecl();
383383
ctx.Diags.diagnose(limitImport->importLoc,
384384
diag::decl_import_via_here,
385385
VD,

0 commit comments

Comments
 (0)