From b38d5394bb53ffc9233322e5523c1f8a789dbd34 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 19 Mar 2024 10:40:36 +0100 Subject: [PATCH] internal: Move grammar codegen into xtask --- .github/workflows/publish-libs.yaml | 1 + Cargo.lock | 8 +- crates/hir-ty/Cargo.toml | 2 +- .../src/handlers/mutability_errors.rs | 22 +- crates/ide-diagnostics/src/lib.rs | 5 +- crates/syntax/Cargo.toml | 4 - crates/syntax/src/ast/generated/nodes.rs | 504 +++++++++--------- crates/syntax/src/ast/generated/tokens.rs | 184 +++---- crates/syntax/src/tests.rs | 25 +- xtask/Cargo.toml | 4 + xtask/src/codegen.rs | 2 + .../src/codegen/grammar.rs | 90 ++-- .../src/codegen/grammar}/ast_src.rs | 0 xtask/src/flags.rs | 2 + 14 files changed, 448 insertions(+), 405 deletions(-) rename crates/syntax/src/tests/sourcegen_ast.rs => xtask/src/codegen/grammar.rs (92%) rename {crates/syntax/src/tests => xtask/src/codegen/grammar}/ast_src.rs (100%) diff --git a/.github/workflows/publish-libs.yaml b/.github/workflows/publish-libs.yaml index 862373ec1cc..34ca53e2e53 100644 --- a/.github/workflows/publish-libs.yaml +++ b/.github/workflows/publish-libs.yaml @@ -32,4 +32,5 @@ jobs: git config --global user.name "GitHub Action" # Remove r-a crates from the workspaces so we don't auto-publish them as well sed -i 's/ "crates\/\*"//' ./Cargo.toml + sed -i 's/ "xtask\/"//' ./Cargo.toml cargo workspaces publish --yes --exact --from-git --no-git-commit --allow-dirty diff --git a/Cargo.lock b/Cargo.lock index a8e3dc338f9..3e1c56c3563 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1869,20 +1869,16 @@ dependencies = [ "itertools", "once_cell", "parser", - "proc-macro2", - "quote", "ra-ap-rustc_lexer", "rayon", "rowan", "rustc-hash", "smol_str", - "sourcegen", "stdx", "test-utils", "text-edit", "tracing", "triomphe", - "ungrammar", ] [[package]] @@ -2438,8 +2434,12 @@ version = "0.1.0" dependencies = [ "anyhow", "flate2", + "itertools", + "proc-macro2", + "quote", "stdx", "time", + "ungrammar", "write-json", "xflags", "xshell", diff --git a/crates/hir-ty/Cargo.toml b/crates/hir-ty/Cargo.toml index 3cfedcdcb4d..a2711f8cbe3 100644 --- a/crates/hir-ty/Cargo.toml +++ b/crates/hir-ty/Cargo.toml @@ -53,7 +53,7 @@ expect-test = "1.4.0" tracing.workspace = true tracing-subscriber.workspace = true tracing-tree.workspace = true -project-model = { path = "../project-model" } +project-model.workspace = true # local deps test-utils.workspace = true diff --git a/crates/ide-diagnostics/src/handlers/mutability_errors.rs b/crates/ide-diagnostics/src/handlers/mutability_errors.rs index 6602aca2fa1..00352266ddb 100644 --- a/crates/ide-diagnostics/src/handlers/mutability_errors.rs +++ b/crates/ide-diagnostics/src/handlers/mutability_errors.rs @@ -51,8 +51,12 @@ pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Option // Diagnostic: unused-mut // // This diagnostic is triggered when a mutable variable isn't actually mutated. -pub(crate) fn unused_mut(ctx: &DiagnosticsContext<'_>, d: &hir::UnusedMut) -> Diagnostic { +pub(crate) fn unused_mut(ctx: &DiagnosticsContext<'_>, d: &hir::UnusedMut) -> Option { let ast = d.local.primary_source(ctx.sema.db).syntax_ptr(); + if ast.file_id.macro_file().is_some() { + // FIXME: Our infra can't handle allow from within macro expansions rn + return None; + } let fixes = (|| { let file_id = ast.file_id.file_id()?; let mut edit_builder = TextEdit::builder(); @@ -76,14 +80,16 @@ pub(crate) fn unused_mut(ctx: &DiagnosticsContext<'_>, d: &hir::UnusedMut) -> Di )]) })(); let ast = d.local.primary_source(ctx.sema.db).syntax_ptr(); - Diagnostic::new_with_syntax_node_ptr( - ctx, - DiagnosticCode::RustcLint("unused_mut"), - "variable does not need to be mutable", - ast, + Some( + Diagnostic::new_with_syntax_node_ptr( + ctx, + DiagnosticCode::RustcLint("unused_mut"), + "variable does not need to be mutable", + ast, + ) + .experimental() // Not supporting `#[allow(unused_mut)]` in proc macros leads to false positive. + .with_fixes(fixes), ) - .experimental() // Not supporting `#[allow(unused_mut)]` in proc macros leads to false positive. - .with_fixes(fixes) } pub(super) fn token(parent: &SyntaxNode, kind: SyntaxKind) -> Option { diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs index dea802204ab..270cf844c65 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -387,7 +387,10 @@ pub fn diagnostics( AnyDiagnostic::UnresolvedMethodCall(d) => handlers::unresolved_method::unresolved_method(&ctx, &d), AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d), AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d, config.proc_macros_enabled, config.proc_attr_macros_enabled), - AnyDiagnostic::UnusedMut(d) => handlers::mutability_errors::unused_mut(&ctx, &d), + AnyDiagnostic::UnusedMut(d) => match handlers::mutability_errors::unused_mut(&ctx, &d) { + Some(it) => it, + None => continue, + }, AnyDiagnostic::UnusedVariable(d) => match handlers::unused_variables::unused_variables(&ctx, &d) { Some(it) => it, None => continue, diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index 9a8d73cf7ff..1809ca7dea5 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -33,12 +33,8 @@ text-edit.workspace = true [dev-dependencies] rayon.workspace = true expect-test = "1.4.0" -proc-macro2 = "1.0.47" -quote = "1.0.20" -ungrammar = "1.16.1" test-utils.workspace = true -sourcegen.workspace = true [features] in-rust-tree = [] diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs index e8801653660..c82bc4151ac 100644 --- a/crates/syntax/src/ast/generated/nodes.rs +++ b/crates/syntax/src/ast/generated/nodes.rs @@ -44,10 +44,10 @@ pub struct ArrayType { } impl ArrayType { pub fn const_arg(&self) -> Option { support::child(&self.syntax) } + pub fn ty(&self) -> Option { support::child(&self.syntax) } pub fn l_brack_token(&self) -> Option { support::token(&self.syntax, T!['[']) } pub fn r_brack_token(&self) -> Option { support::token(&self.syntax, T![']']) } pub fn semicolon_token(&self) -> Option { support::token(&self.syntax, T![;]) } - pub fn ty(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -56,12 +56,12 @@ pub struct AsmExpr { } impl ast::HasAttrs for AsmExpr {} impl AsmExpr { + pub fn expr(&self) -> Option { support::child(&self.syntax) } + pub fn pound_token(&self) -> Option { support::token(&self.syntax, T![#]) } + pub fn l_paren_token(&self) -> Option { support::token(&self.syntax, T!['(']) } + pub fn r_paren_token(&self) -> Option { support::token(&self.syntax, T![')']) } pub fn asm_token(&self) -> Option { support::token(&self.syntax, T![asm]) } pub fn builtin_token(&self) -> Option { support::token(&self.syntax, T![builtin]) } - pub fn expr(&self) -> Option { support::child(&self.syntax) } - pub fn l_paren_token(&self) -> Option { support::token(&self.syntax, T!['(']) } - pub fn pound_token(&self) -> Option { support::token(&self.syntax, T![#]) } - pub fn r_paren_token(&self) -> Option { support::token(&self.syntax, T![')']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -82,12 +82,12 @@ pub struct AssocTypeArg { impl ast::HasTypeBounds for AssocTypeArg {} impl AssocTypeArg { pub fn const_arg(&self) -> Option { support::child(&self.syntax) } - pub fn eq_token(&self) -> Option { support::token(&self.syntax, T![=]) } pub fn generic_arg_list(&self) -> Option { support::child(&self.syntax) } pub fn name_ref(&self) -> Option { support::child(&self.syntax) } pub fn param_list(&self) -> Option { support::child(&self.syntax) } pub fn ret_type(&self) -> Option { support::child(&self.syntax) } pub fn ty(&self) -> Option { support::child(&self.syntax) } + pub fn eq_token(&self) -> Option { support::token(&self.syntax, T![=]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -95,10 +95,10 @@ pub struct Attr { pub(crate) syntax: SyntaxNode, } impl Attr { - pub fn excl_token(&self) -> Option { support::token(&self.syntax, T![!]) } - pub fn l_brack_token(&self) -> Option { support::token(&self.syntax, T!['[']) } pub fn meta(&self) -> Option { support::child(&self.syntax) } + pub fn excl_token(&self) -> Option { support::token(&self.syntax, T![!]) } pub fn pound_token(&self) -> Option { support::token(&self.syntax, T![#]) } + pub fn l_brack_token(&self) -> Option { support::token(&self.syntax, T!['[']) } pub fn r_brack_token(&self) -> Option { support::token(&self.syntax, T![']']) } } @@ -108,9 +108,9 @@ pub struct AwaitExpr { } impl ast::HasAttrs for AwaitExpr {} impl AwaitExpr { - pub fn await_token(&self) -> Option { support::token(&self.syntax, T![await]) } - pub fn dot_token(&self) -> Option { support::token(&self.syntax, T![.]) } pub fn expr(&self) -> Option { support::child(&self.syntax) } + pub fn dot_token(&self) -> Option { support::token(&self.syntax, T![.]) } + pub fn await_token(&self) -> Option { support::token(&self.syntax, T![await]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -119,8 +119,8 @@ pub struct BecomeExpr { } impl ast::HasAttrs for BecomeExpr {} impl BecomeExpr { - pub fn become_token(&self) -> Option { support::token(&self.syntax, T![become]) } pub fn expr(&self) -> Option { support::child(&self.syntax) } + pub fn become_token(&self) -> Option { support::token(&self.syntax, T![become]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -136,10 +136,10 @@ pub struct BlockExpr { } impl ast::HasAttrs for BlockExpr {} impl BlockExpr { - pub fn async_token(&self) -> Option { support::token(&self.syntax, T![async]) } - pub fn const_token(&self) -> Option { support::token(&self.syntax, T![const]) } pub fn label(&self) -> Option