From f24983222d207096851344f7551f0f4ec5cc2a61 Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 31 Mar 2023 03:04:09 +0200 Subject: [PATCH 1/2] Use std::fs::read_to_file in fluent_messages macro --- compiler/rustc_macros/src/diagnostics/fluent.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_macros/src/diagnostics/fluent.rs b/compiler/rustc_macros/src/diagnostics/fluent.rs index 3b2f5cfdc7316..42a1fc147f7b4 100644 --- a/compiler/rustc_macros/src/diagnostics/fluent.rs +++ b/compiler/rustc_macros/src/diagnostics/fluent.rs @@ -15,8 +15,7 @@ use proc_macro2::TokenStream; use quote::quote; use std::{ collections::{HashMap, HashSet}, - fs::File, - io::Read, + fs::read_to_string, path::{Path, PathBuf}, }; use syn::{parse_macro_input, Ident, LitStr}; @@ -95,8 +94,8 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok // As this macro also outputs an `include_str!` for this file, the macro will always be // re-executed when the file changes. - let mut resource_file = match File::open(absolute_ftl_path) { - Ok(resource_file) => resource_file, + let resource_contents = match read_to_string(absolute_ftl_path) { + Ok(resource_contents) => resource_contents, Err(e) => { Diagnostic::spanned(resource_span, Level::Error, "could not open Fluent resource") .note(e.to_string()) @@ -104,13 +103,6 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok return failed(&crate_name); } }; - let mut resource_contents = String::new(); - if let Err(e) = resource_file.read_to_string(&mut resource_contents) { - Diagnostic::spanned(resource_span, Level::Error, "could not read Fluent resource") - .note(e.to_string()) - .emit(); - return failed(&crate_name); - } let mut bad = false; for esc in ["\\n", "\\\"", "\\'"] { for _ in resource_contents.matches(esc) { From 97fb15d9508da0712aa6a5b194251e90315105fb Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 31 Mar 2023 03:48:35 +0200 Subject: [PATCH 2/2] Don't emit the OS error in a note This makes it possible to make the normalization of the error message more precise, allowing us to not normalize all notes away. --- compiler/rustc_macros/src/diagnostics/fluent.rs | 9 ++++++--- tests/ui-fulldeps/fluent-messages/test.rs | 2 +- tests/ui-fulldeps/fluent-messages/test.stderr | 14 +++++--------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_macros/src/diagnostics/fluent.rs b/compiler/rustc_macros/src/diagnostics/fluent.rs index 42a1fc147f7b4..9f96a04148792 100644 --- a/compiler/rustc_macros/src/diagnostics/fluent.rs +++ b/compiler/rustc_macros/src/diagnostics/fluent.rs @@ -97,9 +97,12 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok let resource_contents = match read_to_string(absolute_ftl_path) { Ok(resource_contents) => resource_contents, Err(e) => { - Diagnostic::spanned(resource_span, Level::Error, "could not open Fluent resource") - .note(e.to_string()) - .emit(); + Diagnostic::spanned( + resource_span, + Level::Error, + format!("could not open Fluent resource: {}", e.to_string()), + ) + .emit(); return failed(&crate_name); } }; diff --git a/tests/ui-fulldeps/fluent-messages/test.rs b/tests/ui-fulldeps/fluent-messages/test.rs index 1ee7227a8e992..6ba13387b0460 100644 --- a/tests/ui-fulldeps/fluent-messages/test.rs +++ b/tests/ui-fulldeps/fluent-messages/test.rs @@ -1,4 +1,4 @@ -// normalize-stderr-test "note.*" -> "note: os-specific message" +// normalize-stderr-test "could not open Fluent resource:.*" -> "could not open Fluent resource: os-specific message" #![feature(rustc_private)] #![crate_type = "lib"] diff --git a/tests/ui-fulldeps/fluent-messages/test.stderr b/tests/ui-fulldeps/fluent-messages/test.stderr index 8a6a4a91cc201..2affe621c113a 100644 --- a/tests/ui-fulldeps/fluent-messages/test.stderr +++ b/tests/ui-fulldeps/fluent-messages/test.stderr @@ -1,18 +1,14 @@ -error: could not open Fluent resource +error: could not open Fluent resource: os-specific message --> $DIR/test.rs:24:24 | LL | fluent_messages! { "/definitely_does_not_exist.ftl" } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: os-specific message -error: could not open Fluent resource +error: could not open Fluent resource: os-specific message --> $DIR/test.rs:31:24 | LL | fluent_messages! { "../definitely_does_not_exist.ftl" } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: os-specific message error: could not parse Fluent resource --> $DIR/test.rs:38:24 @@ -89,7 +85,7 @@ error: invalid escape `\n` in Fluent resource LL | fluent_messages! { "./invalid-escape.ftl" } | ^^^^^^^^^^^^^^^^^^^^^^ | - = note: os-specific message + = note: Fluent does not interpret these escape sequences () error: invalid escape `\"` in Fluent resource --> $DIR/test.rs:99:24 @@ -97,7 +93,7 @@ error: invalid escape `\"` in Fluent resource LL | fluent_messages! { "./invalid-escape.ftl" } | ^^^^^^^^^^^^^^^^^^^^^^ | - = note: os-specific message + = note: Fluent does not interpret these escape sequences () error: invalid escape `\'` in Fluent resource --> $DIR/test.rs:99:24 @@ -105,7 +101,7 @@ error: invalid escape `\'` in Fluent resource LL | fluent_messages! { "./invalid-escape.ftl" } | ^^^^^^^^^^^^^^^^^^^^^^ | - = note: os-specific message + = note: Fluent does not interpret these escape sequences () error: aborting due to 13 previous errors