Skip to content

Commit ff620f0

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

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
@@ -5947,6 +5947,13 @@ class AbstractStorageDecl : public ValueDecl {
59475947
/// Return the interface type of the stored value.
59485948
Type getValueInterfaceType() const;
59495949

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

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

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

lib/AST/Decl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7763,14 +7763,19 @@ SourceRange VarDecl::getSourceRange() const {
77637763
return getNameLoc();
77647764
}
77657765

7766-
SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
7766+
SourceRange AbstractStorageDecl::getTypeSourceRangeForDiagnostics() const {
7767+
// Subscripts always have an explicitly-written type.
7768+
if (auto *SD = dyn_cast<SubscriptDecl>(this))
7769+
return SD->getElementTypeSourceRange();
7770+
77677771
// For a parameter, map back to its parameter to get the TypeLoc.
77687772
if (auto *PD = dyn_cast<ParamDecl>(this)) {
77697773
if (auto typeRepr = PD->getTypeRepr())
77707774
return typeRepr->getSourceRange();
77717775
}
7772-
7773-
Pattern *Pat = getParentPattern();
7776+
7777+
auto *VD = cast<VarDecl>(this);
7778+
Pattern *Pat = VD->getParentPattern();
77747779
if (!Pat || Pat->isImplicit())
77757780
return SourceRange();
77767781

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,15 +3724,12 @@ bool AssociatedTypeInference::diagnoseNoSolutions(
37243724
failed.Result.getKind() != CheckTypeWitnessResult::Superclass) {
37253725
Type resultType;
37263726
SourceRange typeRange;
3727-
if (auto *var = dyn_cast<VarDecl>(failed.Witness)) {
3728-
resultType = var->getValueInterfaceType();
3729-
typeRange = var->getTypeSourceRangeForDiagnostics();
3727+
if (auto *storage = dyn_cast<AbstractStorageDecl>(failed.Witness)) {
3728+
resultType = storage->getValueInterfaceType();
3729+
typeRange = storage->getTypeSourceRangeForDiagnostics();
37303730
} else if (auto *func = dyn_cast<FuncDecl>(failed.Witness)) {
37313731
resultType = func->getResultInterfaceType();
37323732
typeRange = func->getResultTypeSourceRange();
3733-
} else if (auto *subscript = dyn_cast<SubscriptDecl>(failed.Witness)) {
3734-
resultType = subscript->getElementInterfaceType();
3735-
typeRange = subscript->getElementTypeSourceRange();
37363733
}
37373734

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

0 commit comments

Comments
 (0)