Improve lexer and parser diagnostic locations (#7990)

Lexer diagnostics used a single `const char*` as their source location,
which only allowed pointing at a single character rather than a run of
characters. This limited the underlining of unrecognized tokens, and
also made escape sequence diagnostics particularly strange by stopping
at whatever point the lexer discovered the error and pointing only at
that location. For example: `"\w"` was reported at the `w`, and `"\x"`
at the closing `"`.

This change tries to always keep the primary location of a diagnostic
the point at which typing (inserting or deleting) might fix the error.
So for `"\x"`, `\x` is underlined, but the position is at the `"` as
that's where you would type a hex code.

The lexer's emitter now takes a special lexing source location that
represents either a single position _between_ two characters, a single
character, or a range of the source buffer. The lexer diagnostics are
updated to construct these for the character range as appropriate.

Parser diagnostics for a missing token were located at the _next_ token,
which is particularly confusing for terminators like missing `;`s. Now
they are located at a position after the previous token, with or without
a space depending on the token, but typically on the same line.
Diagnostics still sort by how far the parser has read.

The parser diagnostics for unclosed groups also now attaches a label to
the opening token when it is on a separate line. And the diagnostics for
mismatched indent in a multi-line string attaches a label to the closing
`'''` that establishes that indent.

There are likely more location improvements we should do here, but this
seemed like a reasonable incremental improvement and is what Claude
found when doing an initial audit. One example where I think we should
make further improvements is around suggesting raw identifiers when we
find an unexpected `Cpp` or `Core` or `base`. We do this consistently
now, but raw identifiers should be rare. A TODO is added for these
cases.

Assisted-by: Claude Code

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Chandler CarruthandRichard Smith authored and GitHub committed 2026-11-01 06:22:00 +00:00
1 parent 611e6a210e
commit 68569d234d
173 files changed
+1782 -689

No files matched your search

+49 -3
View File
@@ -543,21 +543,30 @@ auto TokenizedBuffer::SourcePointerToDiagnosticLoc(const char* loc) const
auto line_range = line_infos_.values();
// Find the first line starting after the given location.
const auto next_line_it = llvm::partition_point(
auto next_line_it = llvm::partition_point(
line_range,
[offset](const LineInfo& line) { return line.start <= offset; });
// Step back one line to find the line containing the given position.
CARBON_CHECK(next_line_it != line_range.begin(),
"location precedes the start of the first line");
const auto line_it = std::prev(next_line_it);
auto line_it = std::prev(next_line_it);
// A file that doesn't end in a newline has an empty line after its last one,
// starting at the end of the file. The end of the file is the end of the
// last line instead.
llvm::StringRef text = source_->text();
if (line_it->start == offset && line_it != line_range.begin() &&
offset == static_cast<int32_t>(text.size()) && !text.ends_with("\n")) {
next_line_it = line_it;
--line_it;
}
int line_number = line_it - line_range.begin();
int column_number = offset - line_it->start;
// Grab the line from the buffer by slicing from this line to the next
// minus the newline. When on the last line, instead use the start to the end
// of the buffer.
llvm::StringRef text = source_->text();
llvm::StringRef line = next_line_it != line_range.end()
? text.slice(line_it->start, next_line_it->start)
: text.substr(line_it->start);
@@ -589,6 +598,43 @@ auto TokenizedBuffer::TokenToDiagnosticLoc(TokenIndex token) const
return converted;
}
auto TokenizedBuffer::TokenRangeToDiagnosticLoc(
InclusiveTokenRange tokens) const -> Diagnostics::ConvertedLoc {
auto begin_loc = TokenToDiagnosticLoc(tokens.begin);
if (tokens.begin == tokens.end) {
return begin_loc;
}
auto end_loc = TokenToDiagnosticLoc(tokens.end);
begin_loc.last_byte_offset = end_loc.last_byte_offset;
// TODO: A range across lines covers only the rest of its first line, because
// a `Diagnostics::Loc` is on one line. Cover the whole range once a location
// can span lines.
if (begin_loc.loc.line_number != end_loc.loc.line_number) {
begin_loc.loc.length =
begin_loc.loc.line.size() - begin_loc.loc.column_number + 1;
} else {
if (begin_loc.loc.column_number != end_loc.loc.column_number) {
begin_loc.loc.length = end_loc.loc.column_number + end_loc.loc.length -
begin_loc.loc.column_number;
}
}
return begin_loc;
}
auto TokenizedBuffer::PointAfterTokenToDiagnosticLoc(TokenIndex token) const
-> Diagnostics::ConvertedLoc {
// A recovery token isn't in the source. It was inserted just past the token
// before it, which puts the point just past it there too.
while (IsRecoveryToken(token)) {
token = TokenIndex(token.index - 1);
}
auto converted = SourcePointerToDiagnosticLoc(
source_->text().begin() + token_infos_.Get(token).byte_offset() +
GetTokenText(token).size());
converted.loc.length = 0;
return converted;
}
auto TokenizedBuffer::OverlapsWithDumpSemIRRange(
Lex::InclusiveTokenRange range) const -> bool {
CARBON_CHECK(!dump_sem_ir_ranges_.empty());