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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

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

@Nilstrieb Nilstrieb 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

@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
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.

@rustbot
Copy link
Collaborator

rustbot commented Nov 19, 2023

rustc_error_messages was changed

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

@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang changed the title [WIP] New attribute macros format for diagnostic structs without fluent slug New attribute macros format for diagnostic structs without fluent slug Dec 1, 2023
@rustbot
Copy link
Collaborator

rustbot commented Dec 1, 2023

Could not assign reviewer from: davidtwco.
User(s) davidtwco are either the PR author, already assigned, or on vacation, and there are no other candidates.
Use r? to specify someone else to assign.

@chenyukang chenyukang force-pushed the errors-refactor-no-fluent branch 2 times, most recently from 3f2e2a1 to a7e9e3e Compare December 8, 2023 11:11
@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.

@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.

@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.

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.

None yet

9 participants