Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fluent diagnostics #129477

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions compiler/rustc_fluent_macro/src/fluent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,8 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
// with a lowercase as rustc errors do.
err.replace_range(0..1, &err.chars().next().unwrap().to_lowercase().to_string());

let line_starts: Vec<usize> = std::iter::once(0)
.chain(
this.source()
.char_indices()
.filter_map(|(i, c)| Some(i + 1).filter(|_| c == '\n')),
)
.collect();
let line_start = line_starts
.iter()
.enumerate()
.map(|(line, idx)| (line + 1, idx))
.filter(|(_, idx)| **idx <= pos.start)
.last()
.unwrap()
.0;

let message = annotate_snippets::Level::Error.title(&err).snippet(
Snippet::source(this.source())
.line_start(line_start)
.origin(&relative_ftl_path)
.fold(true)
.annotation(annotate_snippets::Level::Error.span(pos.start..pos.end - 1)),
Expand Down
11 changes: 11 additions & 0 deletions tests/ui-fulldeps/fluent-messages/many-lines.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
no_crate_foo = foo

# This file tests error reporting for
# fluent files with many lines.
# The error message should point to the correct line number
# and include no more context than necessary.

no_crate_bar =

no_crate_baz =
baz
5 changes: 5 additions & 0 deletions tests/ui-fulldeps/fluent-messages/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ mod bad_escape {
//~| ERROR invalid escape `\"`
//~| ERROR invalid escape `\'`
}

mod many_lines {
rustc_fluent_macro::fluent_messages! { "./many-lines.ftl" }
//~^ ERROR could not parse Fluent resource
}
17 changes: 16 additions & 1 deletion tests/ui-fulldeps/fluent-messages/test.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,20 @@ LL | rustc_fluent_macro::fluent_messages! { "./invalid-escape.ftl" }
|
= note: Fluent does not interpret these escape sequences (<https://projectfluent.org/fluent/guide/special.html>)

error: aborting due to 13 previous errors
error: could not parse Fluent resource
--> $DIR/test.rs:85:44
|
LL | rustc_fluent_macro::fluent_messages! { "./many-lines.ftl" }
| ^^^^^^^^^^^^^^^^^^
|
= help: see additional errors emitted

error: expected a message field for "no_crate_bar"
--> ./many-lines.ftl:8:1
|
8 | no_crate_bar =
| ^^^^^^^^^^^^^^
|

error: aborting due to 14 previous errors

Loading