Skip to content

Commit 6ad844a

Browse files
committed
[NFC] Hoist getTypeSourceRangeForDiagnostics()
Allows code to get this for any AbstractStorageDecl.
1 parent 4f1c3df commit 6ad844a

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

5895+
/// Retrieve the source range of the variable type, or an invalid range if the
5896+
/// variable's type is not explicitly written in the source.
5897+
///
5898+
/// Only for use in diagnostics. It is not always possible to always
5899+
/// precisely point to the variable type because of type aliases.
5900+
SourceRange getTypeSourceRangeForDiagnostics() const;
5901+
58955902
/// Determine how this storage is implemented.
58965903
StorageImplInfo getImplInfo() const;
58975904

@@ -6326,13 +6333,6 @@ class VarDecl : public AbstractStorageDecl {
63266333
/// and not just getInterfaceType().
63276334
Type getTypeInContext() const;
63286335

6329-
/// Retrieve the source range of the variable type, or an invalid range if the
6330-
/// variable's type is not explicitly written in the source.
6331-
///
6332-
/// Only for use in diagnostics. It is not always possible to always
6333-
/// precisely point to the variable type because of type aliases.
6334-
SourceRange getTypeSourceRangeForDiagnostics() const;
6335-
63366336
/// Determine the mutability of this variable declaration when
63376337
/// accessed from a given declaration context.
63386338
StorageMutability mutability(

lib/AST/Decl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7758,14 +7758,19 @@ SourceRange VarDecl::getSourceRange() const {
77587758
return getNameLoc();
77597759
}
77607760

7761-
SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
7761+
SourceRange AbstractStorageDecl::getTypeSourceRangeForDiagnostics() const {
7762+
// Subscripts always have an explicitly-written type.
7763+
if (auto *SD = dyn_cast<SubscriptDecl>(this))
7764+
return SD->getElementTypeSourceRange();
7765+
77627766
// For a parameter, map back to its parameter to get the TypeLoc.
77637767
if (auto *PD = dyn_cast<ParamDecl>(this)) {
77647768
if (auto typeRepr = PD->getTypeRepr())
77657769
return typeRepr->getSourceRange();
77667770
}
7767-
7768-
Pattern *Pat = getParentPattern();
7771+
7772+
auto *VD = cast<VarDecl>(this);
7773+
Pattern *Pat = VD->getParentPattern();
77697774
if (!Pat || Pat->isImplicit())
77707775
return SourceRange();
77717776

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)