Skip to content

Commit 1af5c04

Browse files
committed
[NFC] Hoist getTypeSourceRangeForDiagnostics()
Allows code to get this for any AbstractStorageDecl.
1 parent cd61fd4 commit 1af5c04

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

5922+
/// Retrieve the source range of the variable type, or an invalid range if the
5923+
/// variable's type is not explicitly written in the source.
5924+
///
5925+
/// Only for use in diagnostics. It is not always possible to always
5926+
/// precisely point to the variable type because of type aliases.
5927+
SourceRange getTypeSourceRangeForDiagnostics() const;
5928+
59225929
/// Determine how this storage is implemented.
59235930
StorageImplInfo getImplInfo() const;
59245931

@@ -6353,13 +6360,6 @@ class VarDecl : public AbstractStorageDecl {
63536360
/// and not just getInterfaceType().
63546361
Type getTypeInContext() const;
63556362

6356-
/// Retrieve the source range of the variable type, or an invalid range if the
6357-
/// variable's type is not explicitly written in the source.
6358-
///
6359-
/// Only for use in diagnostics. It is not always possible to always
6360-
/// precisely point to the variable type because of type aliases.
6361-
SourceRange getTypeSourceRangeForDiagnostics() const;
6362-
63636363
/// Determine the mutability of this variable declaration when
63646364
/// accessed from a given declaration context.
63656365
StorageMutability mutability(

lib/AST/Decl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7874,14 +7874,19 @@ SourceRange VarDecl::getSourceRange() const {
78747874
return getNameLoc();
78757875
}
78767876

7877-
SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
7877+
SourceRange AbstractStorageDecl::getTypeSourceRangeForDiagnostics() const {
7878+
// Subscripts always have an explicitly-written type.
7879+
if (auto *SD = dyn_cast<SubscriptDecl>(this))
7880+
return SD->getElementTypeSourceRange();
7881+
78787882
// For a parameter, map back to its parameter to get the TypeLoc.
78797883
if (auto *PD = dyn_cast<ParamDecl>(this)) {
78807884
if (auto typeRepr = PD->getTypeRepr())
78817885
return typeRepr->getSourceRange();
78827886
}
7883-
7884-
Pattern *Pat = getParentPattern();
7887+
7888+
auto *VD = cast<VarDecl>(this);
7889+
Pattern *Pat = VD->getParentPattern();
78857890
if (!Pat || Pat->isImplicit())
78867891
return SourceRange();
78877892

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)