Skip to content

Commit 1a6138a

Browse files
committed
[NFC] Hoist getTypeSourceRangeForDiagnostics()
Allows code to get this for any AbstractFunctionDecl.
1 parent d47fc42 commit 1a6138a

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5946,6 +5946,13 @@ class AbstractStorageDecl : public ValueDecl {
59465946
/// Return the interface type of the stored value.
59475947
Type getValueInterfaceType() const;
59485948

5949+
/// Retrieve the source range of the variable type, or an invalid range if the
5950+
/// variable's type is not explicitly written in the source.
5951+
///
5952+
/// Only for use in diagnostics. It is not always possible to always
5953+
/// precisely point to the variable type because of type aliases.
5954+
SourceRange getTypeSourceRangeForDiagnostics() const;
5955+
59495956
/// Determine how this storage is implemented.
59505957
StorageImplInfo getImplInfo() const;
59515958

@@ -6380,13 +6387,6 @@ class VarDecl : public AbstractStorageDecl {
63806387
/// and not just getInterfaceType().
63816388
Type getTypeInContext() const;
63826389

6383-
/// Retrieve the source range of the variable type, or an invalid range if the
6384-
/// variable's type is not explicitly written in the source.
6385-
///
6386-
/// Only for use in diagnostics. It is not always possible to always
6387-
/// precisely point to the variable type because of type aliases.
6388-
SourceRange getTypeSourceRangeForDiagnostics() const;
6389-
63906390
/// Determine the mutability of this variable declaration when
63916391
/// accessed from a given declaration context.
63926392
StorageMutability mutability(

lib/AST/Decl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7746,14 +7746,19 @@ SourceRange VarDecl::getSourceRange() const {
77467746
return getNameLoc();
77477747
}
77487748

7749-
SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
7749+
SourceRange AbstractStorageDecl::getTypeSourceRangeForDiagnostics() const {
7750+
// Subscripts always have an explicitly-written type.
7751+
if (auto *SD = dyn_cast<SubscriptDecl>(this))
7752+
return SD->getElementTypeSourceRange();
7753+
77507754
// For a parameter, map back to its parameter to get the TypeLoc.
77517755
if (auto *PD = dyn_cast<ParamDecl>(this)) {
77527756
if (auto typeRepr = PD->getTypeRepr())
77537757
return typeRepr->getSourceRange();
77547758
}
7755-
7756-
Pattern *Pat = getParentPattern();
7759+
7760+
auto *VD = cast<VarDecl>(this);
7761+
Pattern *Pat = VD->getParentPattern();
77577762
if (!Pat || Pat->isImplicit())
77587763
return SourceRange();
77597764

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,15 +3705,12 @@ bool AssociatedTypeInference::diagnoseNoSolutions(
37053705
failed.Result.getKind() != CheckTypeWitnessResult::Superclass) {
37063706
Type resultType;
37073707
SourceRange typeRange;
3708-
if (auto *var = dyn_cast<VarDecl>(failed.Witness)) {
3709-
resultType = var->getValueInterfaceType();
3710-
typeRange = var->getTypeSourceRangeForDiagnostics();
3708+
if (auto *storage = dyn_cast<AbstractStorageDecl>(failed.Witness)) {
3709+
resultType = storage->getValueInterfaceType();
3710+
typeRange = storage->getTypeSourceRangeForDiagnostics();
37113711
} else if (auto *func = dyn_cast<FuncDecl>(failed.Witness)) {
37123712
resultType = func->getResultInterfaceType();
37133713
typeRange = func->getResultTypeSourceRange();
3714-
} else if (auto *subscript = dyn_cast<SubscriptDecl>(failed.Witness)) {
3715-
resultType = subscript->getElementInterfaceType();
3716-
typeRange = subscript->getElementTypeSourceRange();
37173714
}
37183715

37193716
// If the type witness was inferred from an existential

0 commit comments

Comments
 (0)