-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
translation: eager translation #102623
Merged
+539
−235
Merged
translation: eager translation #102623
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
febbf71
macros: tidy up lint changes
davidtwco 508d7e6
errors: use `HashMap` to store diagnostic args
davidtwco b4ac262
errors: `AddToDiagnostic::add_to_diagnostic_with`
davidtwco 540b203
errors: `DiagnosticMessage::Eager`
davidtwco 291a473
macros: `#[subdiagnostic(eager)]`
davidtwco 113e943
query_system: finish migration
davidtwco 7e20929
macros: separate suggestion fmt'ing and emission
davidtwco fbac1f2
macros: simplify field ordering in diag derive
davidtwco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,11 @@ pub struct SuggestionsDisabled; | |
/// Simplified version of `FluentArg` that can implement `Encodable` and `Decodable`. Collection of | ||
/// `DiagnosticArg` are converted to `FluentArgs` (consuming the collection) at the start of | ||
/// diagnostic emission. | ||
pub type DiagnosticArg<'source> = (Cow<'source, str>, DiagnosticArgValue<'source>); | ||
pub type DiagnosticArg<'iter, 'source> = | ||
(&'iter DiagnosticArgName<'source>, &'iter DiagnosticArgValue<'source>); | ||
|
||
/// Name of a diagnostic argument. | ||
pub type DiagnosticArgName<'source> = Cow<'source, str>; | ||
|
||
/// Simplified version of `FluentValue` that can implement `Encodable` and `Decodable`. Converted | ||
/// to a `FluentValue` by the emitter to be used in diagnostic translation. | ||
|
@@ -199,9 +203,20 @@ impl IntoDiagnosticArg for ast::token::TokenKind { | |
/// `#[derive(Subdiagnostic)]` -- see [rustc_macros::Subdiagnostic]. | ||
#[cfg_attr(bootstrap, rustc_diagnostic_item = "AddSubdiagnostic")] | ||
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "AddToDiagnostic")] | ||
pub trait AddToDiagnostic { | ||
pub trait AddToDiagnostic | ||
where | ||
Self: Sized, | ||
{ | ||
/// Add a subdiagnostic to an existing diagnostic. | ||
fn add_to_diagnostic(self, diag: &mut Diagnostic); | ||
fn add_to_diagnostic(self, diag: &mut Diagnostic) { | ||
self.add_to_diagnostic_with(diag, |_, m| m); | ||
} | ||
|
||
/// Add a subdiagnostic to an existing diagnostic where `f` is invoked on every message used | ||
/// (to optionally perform eager translation). | ||
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F) | ||
where | ||
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage; | ||
} | ||
|
||
/// Trait implemented by lint types. This should not be implemented manually. Instead, use | ||
|
@@ -229,7 +244,7 @@ pub struct Diagnostic { | |
pub span: MultiSpan, | ||
pub children: Vec<SubDiagnostic>, | ||
pub suggestions: Result<Vec<CodeSuggestion>, SuggestionsDisabled>, | ||
args: Vec<DiagnosticArg<'static>>, | ||
args: FxHashMap<DiagnosticArgName<'static>, DiagnosticArgValue<'static>>, | ||
|
||
/// This is not used for highlighting or rendering any error message. Rather, it can be used | ||
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of | ||
|
@@ -321,7 +336,7 @@ impl Diagnostic { | |
span: MultiSpan::new(), | ||
children: vec![], | ||
suggestions: Ok(vec![]), | ||
args: vec![], | ||
args: Default::default(), | ||
sort_span: DUMMY_SP, | ||
is_lint: false, | ||
} | ||
|
@@ -917,13 +932,30 @@ impl Diagnostic { | |
self | ||
} | ||
|
||
/// Add a subdiagnostic from a type that implements `Subdiagnostic` - see | ||
/// [rustc_macros::Subdiagnostic]. | ||
/// Add a subdiagnostic from a type that implements `Subdiagnostic` (see | ||
/// [rustc_macros::Subdiagnostic]). | ||
pub fn subdiagnostic(&mut self, subdiagnostic: impl AddToDiagnostic) -> &mut Self { | ||
subdiagnostic.add_to_diagnostic(self); | ||
self | ||
} | ||
|
||
/// Add a subdiagnostic from a type that implements `Subdiagnostic` (see | ||
/// [rustc_macros::Subdiagnostic]). Performs eager translation of any translatable messages | ||
/// used in the subdiagnostic, so suitable for use with repeated messages (i.e. re-use of | ||
/// interpolated variables). | ||
pub fn eager_subdiagnostic( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I expect a use case of this function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See 113e943 |
||
&mut self, | ||
handler: &crate::Handler, | ||
subdiagnostic: impl AddToDiagnostic, | ||
) -> &mut Self { | ||
subdiagnostic.add_to_diagnostic_with(self, |diag, msg| { | ||
let args = diag.args(); | ||
let msg = diag.subdiagnostic_message_to_diagnostic_message(msg); | ||
handler.eagerly_translate(msg, args) | ||
}); | ||
self | ||
} | ||
|
||
pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self { | ||
self.span = sp.into(); | ||
if let Some(span) = self.span.primary_span() { | ||
|
@@ -956,16 +988,19 @@ impl Diagnostic { | |
self | ||
} | ||
|
||
pub fn args(&self) -> &[DiagnosticArg<'static>] { | ||
&self.args | ||
// Exact iteration order of diagnostic arguments shouldn't make a difference to output because | ||
// they're only used in interpolation. | ||
#[allow(rustc::potential_query_instability)] | ||
pub fn args<'a>(&'a self) -> impl Iterator<Item = DiagnosticArg<'a, 'static>> { | ||
self.args.iter() | ||
} | ||
|
||
pub fn set_arg( | ||
&mut self, | ||
name: impl Into<Cow<'static, str>>, | ||
arg: impl IntoDiagnosticArg, | ||
) -> &mut Self { | ||
self.args.push((name.into(), arg.into_diagnostic_arg())); | ||
self.args.insert(name.into(), arg.into_diagnostic_arg()); | ||
self | ||
} | ||
|
||
|
@@ -976,7 +1011,7 @@ impl Diagnostic { | |
/// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by | ||
/// combining it with the primary message of the diagnostic (if translatable, otherwise it just | ||
/// passes the user's string along). | ||
fn subdiagnostic_message_to_diagnostic_message( | ||
pub(crate) fn subdiagnostic_message_to_diagnostic_message( | ||
&self, | ||
attr: impl Into<SubdiagnosticMessage>, | ||
) -> DiagnosticMessage { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidtwco How about those English words inside ($desc}?
- note: ...which requires computing the super traits of B...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point - these will need fixed, they're currently provided by the query system's macro for defining queries, so that will require some future work to address.