Skip to content

Commit 50123f2

Browse files
committed
libutil: Fix Pos::getSourcePath
Previous implementation didn't actually check if std::get_if returned a nullptr: std::optional<SourcePath> getSourcePath() const { return *std::get_if<SourcePath>(&origin); }
1 parent bf12aed commit 50123f2

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/libutil/position.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ std::optional<std::string> Pos::getSource() const
6666
}, origin);
6767
}
6868

69+
std::optional<SourcePath> Pos::getSourcePath() const
70+
{
71+
if (auto * path = std::get_if<SourcePath>(&origin))
72+
return *path;
73+
return std::nullopt;
74+
}
75+
6976
void Pos::print(std::ostream & out, bool showOrigin) const
7077
{
7178
if (showOrigin) {

src/libutil/position.hh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ struct Pos
7070
/**
7171
* Get the SourcePath, if the source was loaded from a file.
7272
*/
73-
std::optional<SourcePath> getSourcePath() const {
74-
return *std::get_if<SourcePath>(&origin);
75-
}
73+
std::optional<SourcePath> getSourcePath() const;
7674

7775
struct LinesIterator {
7876
using difference_type = size_t;

0 commit comments

Comments
 (0)