Skip to content

Commit 50d3145

Browse files
committed
fluent_macro: fix diagnostics for fluent parse failures
1 parent 29eb701 commit 50d3145

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

compiler/rustc_fluent_macro/src/fluent.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
132132
Diagnostic::spanned(resource_span, Level::Error, "could not parse Fluent resource")
133133
.help("see additional errors emitted")
134134
.emit();
135-
for ParserError { pos, slice: _, kind } in errs {
135+
for ParserError { pos, slice, kind } in errs {
136136
let mut err = kind.to_string();
137137
// Entirely unnecessary string modification so that the error message starts
138138
// with a lowercase as rustc errors do.
@@ -142,17 +142,16 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
142142
.chain(
143143
this.source()
144144
.char_indices()
145-
.filter_map(|(i, c)| Some(i + 1).filter(|_| c == '\n')),
145+
.filter_map(|(i, c)| (c == '\n').then_some(i + 1)),
146146
)
147147
.collect();
148-
let line_start = line_starts
149-
.iter()
150-
.enumerate()
151-
.map(|(line, idx)| (line + 1, idx))
152-
.filter(|(_, idx)| **idx <= pos.start)
153-
.last()
154-
.unwrap()
155-
.0;
148+
let get_line = |pos| match line_starts.binary_search(&pos) {
149+
Ok(n) => n,
150+
Err(n) => n.saturating_sub(1),
151+
};
152+
153+
let start_line = get_line(slice.as_ref().unwrap_or(&pos).start);
154+
let start_line_pos = line_starts[start_line];
156155

157156
let snippet = Snippet {
158157
title: Some(Annotation {
@@ -162,14 +161,15 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
162161
}),
163162
footer: vec![],
164163
slices: vec![Slice {
165-
source: this.source(),
166-
line_start,
164+
source: &this.source()[start_line_pos..],
165+
// lines are one-indexed
166+
line_start: start_line + 1,
167167
origin: Some(&relative_ftl_path),
168168
fold: true,
169169
annotations: vec![SourceAnnotation {
170170
label: "",
171171
annotation_type: AnnotationType::Error,
172-
range: (pos.start, pos.end - 1),
172+
range: (pos.start - start_line_pos, pos.end - start_line_pos),
173173
}],
174174
}],
175175
};

0 commit comments

Comments
 (0)