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

New attribute macros format for diagnostic structs without fluent slug #117867

Closed

Conversation

chenyukang
Copy link
Member

The background for this:
Split out Fluent from Diagnostics Structs - HackMD

  • The change on compiler/rustc_macros/src/diagnostics are making Diagnostic and Subdiagnostic compatible with the old format and new format, after we migrating all crates, we may need to clean up old code.

  • I made a program to migrate from old format to the new format for each crate automatically(but still need some trivial manual touch), I'd like to get some feedback for parser as a starting point.

r? @davidtwco

Ping @estebank @oli-obk who may also interested on this part.

@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 13, 2023
@rustbot
Copy link
Collaborator

rustbot commented Nov 13, 2023

The list of allowed third-party dependencies may have been modified! You must ensure that any new dependencies have compatible licenses before merging.

cc @davidtwco, @wesleywiser

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

rustc_errors::translation was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

rustc_macros::diagnostics was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

return Ok(());
match &attr.meta {
// support syntax `#[diag("message ...", code = "E0045", note = "node message")]`
Meta::List(MetaList { path, tokens: token_stream, .. }) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to support:

#[diag("message....")] // or #[diag("message", code = "...", note = "...")]

If it's:

#[diag(label = "message....")] 

it will be handled by attr.parse_nested_meta below.

I'd suggest we only support the late one, which is more consistent with suggestion, multipart_suggestion, or we only support #[diag("message....")](without extra nested elements), if there are multiple nested elements label = is required?

@@ -58,9 +56,10 @@ impl<'a> Parser<'a> {
let span = self.token.span;
let mut err = self.sess.span_diagnostic.struct_span_err_with_code(
span,
fluent::parse_inner_doc_comment_not_permitted,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we handle this?
maybe we need a new macro to mark the content need to be translated.

@chenyukang chenyukang changed the title [WIP] New macro proc format for diagnostic structs without fluent slug [WIP] New attribute macros format for diagnostic structs without fluent slug Nov 13, 2023
@fmease
Copy link
Member

fmease commented Nov 13, 2023

I think this needs an MCP (major change proposal over at https://github.com/rust-lang/compiler-team)?
@rustbot label needs-mcp

@rustbot rustbot added the needs-mcp This change is large enough that it needs a major change proposal before starting work. label Nov 13, 2023
Copy link
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 from me, but I think it would be nicer to have a smaller crate with fewer diagnostics as an example here instead of rustc_parse, which is quite big and will lead to some merge conflicts

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch from 176bb46 to bf3b14d Compare November 14, 2023 01:15
@rustbot
Copy link
Collaborator

rustbot commented Nov 14, 2023

rustc_macros::diagnostics was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

The list of allowed third-party dependencies may have been modified! You must ensure that any new dependencies have compatible licenses before merging.

cc @davidtwco, @wesleywiser

rustc_errors::translation was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

@rust-log-analyzer

This comment has been minimized.


parse_inner_attr_explanation = inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
parse_inner_attr_not_permitted = an inner attribute is not permitted in this context
.label_does_not_annotate_this = {parse_label_inner_attr_does_not_annotate_this}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we support this scenario in the new format?
I think we may support it like this:

#[derive(Diagnostic)]
#[diag(label = "the label...", does_not_annotate_this = "the content can be refer...")]
pub(crate) struct ErrorStruct {
  ...

  pub sugg: Option<SubErrorStruct>,
}

#[derive(Subdiagnostic)]
pub(crate) struct SubErrorStruct {

    #[suggestion("{does_not_annotate_this}")
    pub span: Span
}

@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch from 728a864 to 9d8105d Compare November 14, 2023 02:14
@chenyukang
Copy link
Member Author

+1 from me, but I think it would be nicer to have a smaller crate with fewer diagnostics as an example here instead of rustc_parse, which is quite big and will lead to some merge conflicts

yeah, I fixed the conflict.
I started with parser because its errors are complex enough to contain different kinds of scenarios😆

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch 3 times, most recently from b1820bd to 1a3dc67 Compare November 16, 2023 08:22
@bors

This comment was marked as resolved.

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch from d5ece88 to 1df013b Compare November 19, 2023 17:46
@rustbot
Copy link
Collaborator

rustbot commented Nov 19, 2023

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @TaKO8Ki

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch from 1df013b to db6553b Compare November 19, 2023 18:02
@rustbot
Copy link
Collaborator

rustbot commented Dec 8, 2023

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@bors
Copy link
Contributor

bors commented Dec 8, 2023

☔ The latest upstream changes (presumably #118527) made this pull request unmergeable. Please resolve the merge conflicts.

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch from 14ed478 to 03c8665 Compare December 9, 2023 02:58
@bors
Copy link
Contributor

bors commented Dec 10, 2023

☔ The latest upstream changes (presumably #118692) made this pull request unmergeable. Please resolve the merge conflicts.

@apiraino
Copy link
Contributor

Switching to waiting on author to take action. Seems that an MCP should be appropriate for these changes and also the rust-analyzer comment) suggest to be resolved.

Thanks!

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 28, 2023
@chenyukang
Copy link
Member Author

Switching to waiting on author to take action. Seems that an MCP should be appropriate for these changes and also the rust-analyzer comment) suggest to be resolved.

Thanks!

@rustbot author

The rust-analyzer related change was committed by accident, I already rollback it.
Seems there are some conflicts right now, I will fix it and add a MCP.

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch from 03c8665 to b25a2bb Compare December 28, 2023 15:24
@bors
Copy link
Contributor

bors commented Jan 4, 2024

☔ The latest upstream changes (presumably #119578) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies it took me so long to get to this. I've left some feedback. As it's been a while, this may be a big rebase, particularly given the many changes to the diagnostic internals that have been happening recently.

}
}
(Some(_slug), Some(_raw_label)) => {
unreachable!("BUG: slug and raw label specified");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an error from the proc macro rather than a unreachable!.

DiagnosticMessage::FluentRaw(msg) => {
// FIXME(yukang): calculate the `slug` from the raw fluent content,
// The fluent resources are generated by a simple standalone visitor:
// https://github.com/chenyukang/fluent-utils/blob/main/src/visitor.rs#L13-L97
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I still prefer some solution where the compiler is able to emit the reference ftl rather than needing another tool to extract it. I know that we tried to do this by writing static variables into a section, but we might also be able to do it from the proc macro by writing to the filesystem (which isn't ideal) based on an environment variable.

@@ -44,9 +46,15 @@ pub(crate) struct DiagnosticDeriveVariantBuilder {
/// has the actual diagnostic message.
pub slug: SpannedOption<Path>,

/// Label is a the text embedded in the struct attribute and corresponds to the diagnostic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe message instead of label because label is the term we use for a type of subdiagnostic.

@@ -182,59 +193,116 @@ impl DiagnosticDeriveVariantBuilder {
let name = attr.path().segments.last().unwrap().ident.to_string();
let name = name.as_str();

let mut first = true;
let mut set_label = false;
let keys = vec!["note", "help", "warning", "suggestion"];

if name == "diag" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be simpler to introduce a mutually exclusive diag_new and re-use some logic, what would parse as slug becomes message (or label as you have it now)?

@@ -601,6 +612,9 @@ pub(super) struct SubdiagnosticVariant {
pub(super) kind: SubdiagnosticKind,
pub(super) slug: Option<Path>,
pub(super) no_span: bool,
/// A subdiagnostic can have a raw_label field, e.g. `#[help("some text")]`.
/// if `slug` is None, this field need to be set.
pub(super) raw_label: Option<LitStr>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an enum for slug that has a message or a slug, to capture the mutual exclusive-ness of these?

@@ -29,6 +29,17 @@ pub(crate) fn new_code_ident() -> syn::Ident {
})
}

pub(crate) fn convert_to_litstr(lit: &proc_macro2::Literal) -> LitStr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

@estebank
Copy link
Contributor

I think it might be a good idea for @chenyukang, @davidtwco, @oli-obk and myself to talk synchronously sometime soon. We need to come to an agreement to what the long term plan is here and I know I have thoughts about options we have available which would affect whether this PR can be merged as is or would require changes. I'm really excited about this project and want to ensure its success.

@chenyukang
Copy link
Member Author

@estebank good idea, I'm ok for online meeting.

@Dylan-DPC
Copy link
Member

@chenyukang any updates on this? thanks

@oli-obk
Copy link
Contributor

oli-obk commented Aug 1, 2024

cc @Manishearth as you also have opinions on translation infrastructure, there's a summary of what this PR is about in https://hackmd.io/@e0xmMzbUT7SeCAVAVjBv2Q/S1XOUOdQa (the hackmd linked from the main post)

@Manishearth
Copy link
Member

I've expressed opinions on this before: https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Localization.20infra.20interferes.20with.20grepping.20for.20error

TLDR: translation files are source code, they have intentionalilty to them, they ought to be commentable, they should be managed by the translation team like source code.

It's theoretically possible to have an autogenerate-the-ftl-file workflow that retains this, but you'd need a bunch more design work. At the very very least: we'd need an attribute that allows one to provide context to translators, which turns into a comment in the FTL file. Another common need is organizing the file, it's less important and something people can get without, but definitely something we lose here.

This proposal seems to be designed entirely with the needs of compiler devs in mind, without consideration for the needs of translators.

instead of referring to slugs, we'd generate those from the type name and a hash of the message contents, i.e. signature-redeclaration-8af429de or something. Because of the message being present here, there are no unused Fluent messages or else there would be unused types; and we can check the fields match the interpolations at compile-time.

I don't think this is a good idea: there is a distinction between "this is a wording change that's just sprucing stuff up in English" and "this is a wording change that changes the meaning of this slug and needs retranslation".

I do recognize that the current system is prone to people making changes that do not notify the translation teams (was hoping to have some automation set up eventually that does this).

Furthermore, slugs are meant to be human readable.

This separation also makes the next steps more difficult, we want to use Pontoon to do the translations, and it doesn't handle versioning of messages. We have to manually add -1, -2 prefixes to the slugs and that's dumb.

This is intentional choice (and common practice in translation environments): people should be carefully considering whether or not the prefix needs to be added when this crops up.


So I'm mostly against such a change. If someone who understands the needs of translation teams were to design such a change it could work, but at the moment that's not the case, the most basic ability to comment and organize the translation file is lost.

I'd be grudgingly okay with such a change with the following modifications:

  • There is a way (probably a new attribute) to provide context to translators, that turns into an FTL comment
  • The hash autogeneration is removed and replaced with an explicit versioning attribute. I think there's tons of room for improvement on workflows there1, but a hash is basically a complete non starter here.

Footnotes

  1. Would very much be in favor of automation that catches whenever an English string changes without a version bump, and requires the translation team to approve it

@alex-semenyuk alex-semenyuk added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 15, 2024
@chenyukang chenyukang closed this Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic needs-mcp This change is large enough that it needs a major change proposal before starting work. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.