From 6001c50cac411d7a889c4dfd176d6dda8b4b181f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 7 Dec 2023 09:53:08 +1100 Subject: [PATCH 01/24] Detect `NulInCStr` error earlier. By making it an `EscapeError` instead of a `LitError`. This makes it like the other errors produced when checking string literals contents, e.g. for invalid escape sequences or bare CR chars. NOTE: this means these errors are issued earlier, before expansion, which changes behaviour. It will be possible to move the check back to the later point if desired. If that happens, it's likely that all the string literal contents checks will be delayed together. One nice thing about this: the old approach had some code in `report_lit_error` to calculate the span of the nul char from a range. This code used a hardwired `+2` to account for the `c"` at the start of a C string literal, but this should have changed to a `+3` for raw C string literals to account for the `cr"`, which meant that the caret in `cr"` nul error messages was one short of where it should have been. The new approach doesn't need any of this and avoids the off-by-one error. --- crates/parser/src/lexed_str.rs | 1 + crates/syntax/src/validation.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/crates/parser/src/lexed_str.rs b/crates/parser/src/lexed_str.rs index b9e7566fdf9bc..3753a1beb7a8e 100644 --- a/crates/parser/src/lexed_str.rs +++ b/crates/parser/src/lexed_str.rs @@ -371,6 +371,7 @@ fn error_to_diagnostic_message(error: EscapeError, mode: Mode) -> &'static str { "non-ASCII character in byte string literal" } EscapeError::NonAsciiCharInByte => "non-ASCII character in raw byte string literal", + EscapeError::NulInCStr => "null character in C string literal", EscapeError::UnskippedWhitespaceWarning => "", EscapeError::MultipleSkippedLinesWarning => "", } diff --git a/crates/syntax/src/validation.rs b/crates/syntax/src/validation.rs index eabbda2c3983c..0504f67c9dc7e 100644 --- a/crates/syntax/src/validation.rs +++ b/crates/syntax/src/validation.rs @@ -106,6 +106,9 @@ fn rustc_unescape_error_to_string(err: unescape::EscapeError) -> (&'static str, EE::NonAsciiCharInByte => { "Byte literals must not contain non-ASCII characters" } + EE::NulInCStr => { + "C strings literals must not contain null characters" + } EE::UnskippedWhitespaceWarning => "Whitespace after this escape is not skipped", EE::MultipleSkippedLinesWarning => "Multiple lines are skipped by this escape", From 6231ca5f5eddc7691a79373804e9530e19df9a5d Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 18 Jan 2024 06:50:06 +0000 Subject: [PATCH 02/24] fix(rust-analyzer): use new pkgid spec to compare Starting from cargo#13311, Cargo's compiler artifact message uses Package ID specification as package's identifier format. --- crates/proc-macro-srv/proc-macro-test/build.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/proc-macro-srv/proc-macro-test/build.rs b/crates/proc-macro-srv/proc-macro-test/build.rs index 6cf2b5643e579..e6903fb8d4aed 100644 --- a/crates/proc-macro-srv/proc-macro-test/build.rs +++ b/crates/proc-macro-srv/proc-macro-test/build.rs @@ -92,12 +92,24 @@ fn main() { panic!("proc-macro-test-impl failed to build"); } + // Old Package ID Spec + let repr = format!("{name} {version}"); + // New Package Id Spec since rust-lang/cargo#13311 + let pkgid = String::from_utf8( + Command::new(toolchain::cargo()) + .current_dir(&staging_dir) + .args(["pkgid", name]) + .output() + .unwrap().stdout, + ) + .unwrap(); + let pkgid = pkgid.trim(); + let mut artifact_path = None; for message in Message::parse_stream(output.stdout.as_slice()) { if let Message::CompilerArtifact(artifact) = message.unwrap() { if artifact.target.kind.contains(&"proc-macro".to_string()) { - let repr = format!("{name} {version}"); - if artifact.package_id.repr.starts_with(&repr) { + if artifact.package_id.repr.starts_with(&repr) || artifact.package_id.repr == pkgid { artifact_path = Some(PathBuf::from(&artifact.filenames[0])); } } From 1a9ef233b736e063d8b51437022b366c77d5ac75 Mon Sep 17 00:00:00 2001 From: r0cky Date: Sun, 21 Jan 2024 12:56:23 +0800 Subject: [PATCH 03/24] Remove unused codes --- crates/hir-ty/src/diagnostics/match_check.rs | 95 ------------------- crates/ide-assists/src/handlers/sort_items.rs | 2 - 2 files changed, 97 deletions(-) diff --git a/crates/hir-ty/src/diagnostics/match_check.rs b/crates/hir-ty/src/diagnostics/match_check.rs index e4d4536fc9321..9e84cd0184cd4 100644 --- a/crates/hir-ty/src/diagnostics/match_check.rs +++ b/crates/hir-ty/src/diagnostics/match_check.rs @@ -413,98 +413,3 @@ where (self.0)(f) } } - -pub(crate) trait PatternFoldable: Sized { - fn fold_with(&self, folder: &mut F) -> Self { - self.super_fold_with(folder) - } - - fn super_fold_with(&self, folder: &mut F) -> Self; -} - -pub(crate) trait PatternFolder: Sized { - fn fold_pattern(&mut self, pattern: &Pat) -> Pat { - pattern.super_fold_with(self) - } - - fn fold_pattern_kind(&mut self, kind: &PatKind) -> PatKind { - kind.super_fold_with(self) - } -} - -impl PatternFoldable for Box { - fn super_fold_with(&self, folder: &mut F) -> Self { - let content: T = (**self).fold_with(folder); - Box::new(content) - } -} - -impl PatternFoldable for Vec { - fn super_fold_with(&self, folder: &mut F) -> Self { - self.iter().map(|t| t.fold_with(folder)).collect() - } -} - -impl PatternFoldable for Option { - fn super_fold_with(&self, folder: &mut F) -> Self { - self.as_ref().map(|t| t.fold_with(folder)) - } -} - -macro_rules! clone_impls { - ($($ty:ty),+) => { - $( - impl PatternFoldable for $ty { - fn super_fold_with(&self, _: &mut F) -> Self { - Clone::clone(self) - } - } - )+ - } -} - -clone_impls! { LocalFieldId, Ty, Substitution, EnumVariantId } - -impl PatternFoldable for FieldPat { - fn super_fold_with(&self, folder: &mut F) -> Self { - FieldPat { field: self.field.fold_with(folder), pattern: self.pattern.fold_with(folder) } - } -} - -impl PatternFoldable for Pat { - fn fold_with(&self, folder: &mut F) -> Self { - folder.fold_pattern(self) - } - - fn super_fold_with(&self, folder: &mut F) -> Self { - Pat { ty: self.ty.fold_with(folder), kind: self.kind.fold_with(folder) } - } -} - -impl PatternFoldable for PatKind { - fn fold_with(&self, folder: &mut F) -> Self { - folder.fold_pattern_kind(self) - } - - fn super_fold_with(&self, folder: &mut F) -> Self { - match self { - PatKind::Wild => PatKind::Wild, - PatKind::Binding { name, subpattern } => { - PatKind::Binding { name: name.clone(), subpattern: subpattern.fold_with(folder) } - } - PatKind::Variant { substs, enum_variant, subpatterns } => PatKind::Variant { - substs: substs.fold_with(folder), - enum_variant: enum_variant.fold_with(folder), - subpatterns: subpatterns.fold_with(folder), - }, - PatKind::Leaf { subpatterns } => { - PatKind::Leaf { subpatterns: subpatterns.fold_with(folder) } - } - PatKind::Deref { subpattern } => { - PatKind::Deref { subpattern: subpattern.fold_with(folder) } - } - &PatKind::LiteralBool { value } => PatKind::LiteralBool { value }, - PatKind::Or { pats } => PatKind::Or { pats: pats.fold_with(folder) }, - } - } -} diff --git a/crates/ide-assists/src/handlers/sort_items.rs b/crates/ide-assists/src/handlers/sort_items.rs index 3a0121f55fa02..64e30b1834522 100644 --- a/crates/ide-assists/src/handlers/sort_items.rs +++ b/crates/ide-assists/src/handlers/sort_items.rs @@ -116,11 +116,9 @@ trait AddRewrite { new: Vec, target: TextRange, ) -> Option<()>; - fn yeet() {} } impl AddRewrite for Assists { - fn yeet() {} fn add_rewrite( &mut self, label: &str, From e4866b6ddb1126646b8d78fc10514ac1ac4c4bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sun, 21 Jan 2024 16:53:06 +0200 Subject: [PATCH 04/24] Merge commit 'a9116523604c998e7781f60d3b5a6f586e0414a9' into sync-from-ra --- .editorconfig | 1 + Cargo.lock | 25 +- Cargo.toml | 73 +- crates/base-db/src/input.rs | 51 +- crates/hir-def/src/attr.rs | 95 +- crates/hir-def/src/body.rs | 19 +- crates/hir-def/src/body/lower.rs | 65 +- crates/hir-def/src/body/pretty.rs | 46 +- crates/hir-def/src/child_by_source.rs | 25 +- crates/hir-def/src/data.rs | 17 +- crates/hir-def/src/data/adt.rs | 205 +- crates/hir-def/src/db.rs | 80 +- crates/hir-def/src/dyn_map/keys.rs | 2 +- crates/hir-def/src/find_path.rs | 14 +- crates/hir-def/src/generics.rs | 10 +- crates/hir-def/src/hir.rs | 2 +- crates/hir-def/src/hir/type_ref.rs | 28 +- crates/hir-def/src/import_map.rs | 12 +- crates/hir-def/src/item_tree.rs | 67 +- crates/hir-def/src/item_tree/lower.rs | 28 +- crates/hir-def/src/item_tree/pretty.rs | 96 +- crates/hir-def/src/item_tree/tests.rs | 69 +- crates/hir-def/src/lang_item.rs | 55 +- crates/hir-def/src/lib.rs | 86 +- .../hir-def/src/macro_expansion_tests/mbe.rs | 22 +- .../hir-def/src/macro_expansion_tests/mod.rs | 11 +- .../src/macro_expansion_tests/proc_macros.rs | 6 +- crates/hir-def/src/nameres.rs | 8 +- crates/hir-def/src/nameres/collector.rs | 171 +- crates/hir-def/src/nameres/diagnostics.rs | 17 + crates/hir-def/src/nameres/path_resolution.rs | 51 +- crates/hir-def/src/nameres/tests/macros.rs | 4 +- crates/hir-def/src/path.rs | 6 +- crates/hir-def/src/path/lower.rs | 2 +- crates/hir-def/src/pretty.rs | 15 +- crates/hir-def/src/resolver.rs | 13 +- crates/hir-def/src/src.rs | 21 +- crates/hir-def/src/trace.rs | 2 + crates/hir-def/src/visibility.rs | 8 +- crates/hir-expand/src/ast_id_map.rs | 10 +- crates/hir-expand/src/attrs.rs | 4 +- crates/hir-expand/src/builtin_derive_macro.rs | 2 +- crates/hir-expand/src/builtin_fn_macro.rs | 9 +- crates/hir-expand/src/db.rs | 17 +- crates/hir-expand/src/fixup.rs | 2 +- crates/hir-expand/src/lib.rs | 16 +- crates/hir-expand/src/mod_path.rs | 2 +- crates/hir-expand/src/name.rs | 22 +- crates/hir-expand/src/quote.rs | 14 +- crates/hir-ty/Cargo.toml | 8 +- crates/hir-ty/src/builder.rs | 14 +- crates/hir-ty/src/chalk_db.rs | 48 +- crates/hir-ty/src/chalk_ext.rs | 10 +- crates/hir-ty/src/consteval.rs | 19 +- crates/hir-ty/src/db.rs | 12 +- crates/hir-ty/src/diagnostics/decl_check.rs | 10 +- crates/hir-ty/src/diagnostics/expr.rs | 46 +- crates/hir-ty/src/diagnostics/match_check.rs | 3 +- .../match_check/deconstruct_pat.rs | 2 +- .../src/diagnostics/match_check/usefulness.rs | 2 +- crates/hir-ty/src/display.rs | 102 +- crates/hir-ty/src/infer.rs | 91 +- crates/hir-ty/src/infer/cast.rs | 1 - crates/hir-ty/src/infer/closure.rs | 46 +- crates/hir-ty/src/infer/coerce.rs | 4 +- crates/hir-ty/src/infer/expr.rs | 43 +- crates/hir-ty/src/infer/pat.rs | 10 +- crates/hir-ty/src/infer/path.rs | 11 +- crates/hir-ty/src/infer/unify.rs | 9 +- crates/hir-ty/src/inhabitedness.rs | 19 +- crates/hir-ty/src/interner.rs | 8 +- crates/hir-ty/src/layout.rs | 14 +- crates/hir-ty/src/layout/adt.rs | 19 +- crates/hir-ty/src/layout/target.rs | 2 +- crates/hir-ty/src/layout/tests.rs | 8 +- crates/hir-ty/src/layout/tests/closure.rs | 15 +- crates/hir-ty/src/lib.rs | 163 +- crates/hir-ty/src/lower.rs | 116 +- crates/hir-ty/src/mapping.rs | 10 +- crates/hir-ty/src/method_resolution.rs | 13 +- crates/hir-ty/src/mir.rs | 46 +- crates/hir-ty/src/mir/borrowck.rs | 34 +- crates/hir-ty/src/mir/eval.rs | 229 +- crates/hir-ty/src/mir/eval/shim.rs | 51 +- crates/hir-ty/src/mir/eval/shim/simd.rs | 4 +- crates/hir-ty/src/mir/eval/tests.rs | 6 +- crates/hir-ty/src/mir/lower.rs | 141 +- crates/hir-ty/src/mir/lower/as_place.rs | 94 +- .../hir-ty/src/mir/lower/pattern_matching.rs | 86 +- crates/hir-ty/src/mir/monomorphization.rs | 4 +- crates/hir-ty/src/mir/pretty.rs | 17 +- crates/hir-ty/src/test_db.rs | 4 +- crates/hir-ty/src/tests.rs | 22 +- crates/hir-ty/src/tests/coercion.rs | 3 +- crates/hir-ty/src/tests/macros.rs | 4 +- crates/hir-ty/src/tests/patterns.rs | 12 +- crates/hir-ty/src/tests/regression.rs | 58 +- crates/hir-ty/src/tests/simple.rs | 80 +- crates/hir-ty/src/tests/traits.rs | 26 +- crates/hir-ty/src/tls.rs | 5 +- crates/hir-ty/src/utils.rs | 26 +- crates/hir/src/attrs.rs | 2 +- crates/hir/src/db.rs | 22 +- crates/hir/src/from_id.rs | 4 +- crates/hir/src/has_source.rs | 2 +- crates/hir/src/lib.rs | 138 +- crates/hir/src/semantics.rs | 14 +- crates/hir/src/semantics/source_to_def.rs | 15 +- crates/hir/src/source_analyzer.rs | 12 +- crates/hir/src/symbols.rs | 4 +- .../src/handlers/add_missing_match_arms.rs | 4 +- .../src/handlers/add_turbo_fish.rs | 4 +- .../ide-assists/src/handlers/auto_import.rs | 4 +- .../ide-assists/src/handlers/bool_to_enum.rs | 8 +- .../src/handlers/change_visibility.rs | 2 +- .../src/handlers/convert_integer_literal.rs | 2 +- .../src/handlers/convert_match_to_let_else.rs | 4 +- .../src/handlers/convert_to_guarded_return.rs | 34 +- .../src/handlers/destructure_tuple_binding.rs | 8 +- .../src/handlers/extract_module.rs | 23 +- .../extract_struct_from_enum_variant.rs | 46 + .../src/handlers/extract_type_alias.rs | 2 +- .../src/handlers/extract_variable.rs | 2 +- .../src/handlers/flip_trait_bound.rs | 4 +- .../src/handlers/generate_constant.rs | 7 +- .../src/handlers/generate_delegate_methods.rs | 2 +- .../src/handlers/generate_delegate_trait.rs | 70 +- .../generate_documentation_template.rs | 2 +- .../src/handlers/generate_enum_is_method.rs | 10 +- .../src/handlers/generate_enum_variant.rs | 4 +- .../handlers/generate_from_impl_for_enum.rs | 2 +- .../src/handlers/generate_getter_or_setter.rs | 18 +- .../ide-assists/src/handlers/generate_impl.rs | 4 +- .../handlers/generate_is_empty_from_len.rs | 2 +- .../src/handlers/generate_trait_from_impl.rs | 26 +- .../ide-assists/src/handlers/inline_call.rs | 147 +- .../src/handlers/inline_const_as_literal.rs | 20 +- .../ide-assists/src/handlers/inline_macro.rs | 2 +- .../src/handlers/introduce_named_generic.rs | 2 +- .../ide-assists/src/handlers/merge_imports.rs | 214 +- .../src/handlers/remove_unused_imports.rs | 10 +- .../src/handlers/replace_if_let_with_match.rs | 2 +- .../replace_is_method_with_if_let_method.rs | 2 +- .../src/handlers/replace_method_eager_lazy.rs | 2 +- .../replace_qualified_name_with_use.rs | 2 +- .../replace_turbofish_with_explicit_type.rs | 4 +- .../src/handlers/unnecessary_async.rs | 6 +- .../ide-assists/src/handlers/unwrap_block.rs | 2 +- crates/ide-assists/src/tests.rs | 44 +- crates/ide-assists/src/tests/sourcegen.rs | 2 +- crates/ide-assists/src/utils.rs | 4 +- .../src/utils/gen_trait_fn_body.rs | 4 +- crates/ide-assists/src/utils/suggest_name.rs | 8 +- crates/ide-completion/src/completions.rs | 8 +- .../src/completions/attribute.rs | 4 +- crates/ide-completion/src/completions/dot.rs | 8 +- .../src/completions/env_vars.rs | 2 +- .../src/completions/extern_abi.rs | 5 +- .../ide-completion/src/completions/field.rs | 28 +- .../src/completions/flyimport.rs | 17 +- .../src/completions/fn_param.rs | 2 +- .../src/completions/item_list.rs | 2 +- .../src/completions/item_list/trait_impl.rs | 12 +- .../ide-completion/src/completions/postfix.rs | 4 +- .../ide-completion/src/completions/record.rs | 18 +- .../ide-completion/src/completions/snippet.rs | 4 +- crates/ide-completion/src/completions/use_.rs | 4 +- crates/ide-completion/src/context.rs | 9 +- crates/ide-completion/src/context/analysis.rs | 31 +- crates/ide-completion/src/item.rs | 16 +- crates/ide-completion/src/render.rs | 17 +- crates/ide-completion/src/render/function.rs | 13 +- crates/ide-completion/src/render/literal.rs | 10 +- crates/ide-completion/src/render/macro_.rs | 4 +- crates/ide-completion/src/tests/use_tree.rs | 27 + crates/ide-db/src/active_parameter.rs | 24 +- crates/ide-db/src/apply_change.rs | 10 +- crates/ide-db/src/defs.rs | 24 +- crates/ide-db/src/generated/lints.rs | 1683 ++++++++------- crates/ide-db/src/imports/import_assets.rs | 2 +- crates/ide-db/src/imports/insert_use.rs | 112 +- crates/ide-db/src/imports/insert_use/tests.rs | 248 ++- crates/ide-db/src/imports/merge_imports.rs | 340 ++- crates/ide-db/src/lib.rs | 16 +- crates/ide-db/src/path_transform.rs | 2 +- crates/ide-db/src/rename.rs | 9 +- crates/ide-db/src/search.rs | 6 +- crates/ide-db/src/symbol_index.rs | 14 +- .../src/handlers/break_outside_of_loop.rs | 2 +- .../src/handlers/expected_function.rs | 2 +- .../src/handlers/inactive_code.rs | 2 +- .../src/handlers/incoherent_impl.rs | 4 +- .../src/handlers/incorrect_case.rs | 4 +- .../src/handlers/invalid_derive_target.rs | 2 +- .../src/handlers/macro_error.rs | 2 +- .../src/handlers/malformed_derive.rs | 2 +- .../src/handlers/mismatched_arg_count.rs | 4 +- .../src/handlers/missing_fields.rs | 9 +- .../src/handlers/missing_match_arms.rs | 2 +- .../src/handlers/missing_unsafe.rs | 2 +- .../src/handlers/moved_out_of_ref.rs | 2 +- .../src/handlers/mutability_errors.rs | 2 +- .../src/handlers/no_such_field.rs | 2 +- .../src/handlers/private_assoc_item.rs | 2 +- .../src/handlers/private_field.rs | 2 +- .../replace_filter_map_next_with_find_map.rs | 2 +- .../src/handlers/trait_impl_orphan.rs | 5 +- .../trait_impl_redundant_assoc_item.rs | 11 +- .../src/handlers/type_mismatch.rs | 8 +- .../src/handlers/typed_hole.rs | 2 +- .../src/handlers/undeclared_label.rs | 2 +- .../handlers/unimplemented_builtin_macro.rs | 2 +- .../src/handlers/unreachable_label.rs | 2 +- .../src/handlers/unresolved_assoc_item.rs | 2 +- .../src/handlers/unresolved_extern_crate.rs | 2 +- .../src/handlers/unresolved_import.rs | 2 +- .../src/handlers/unresolved_module.rs | 2 +- crates/ide-diagnostics/src/lib.rs | 8 +- crates/ide-ssr/src/fragments.rs | 4 +- crates/ide-ssr/src/matching.rs | 10 +- crates/ide-ssr/src/search.rs | 2 +- crates/ide/src/annotations/fn_references.rs | 2 +- crates/ide/src/doc_links.rs | 28 +- crates/ide/src/doc_links/tests.rs | 50 +- crates/ide/src/fetch_crates.rs | 2 +- crates/ide/src/folding_ranges.rs | 12 +- crates/ide/src/highlight_related.rs | 12 +- crates/ide/src/hover.rs | 152 +- crates/ide/src/hover/render.rs | 151 +- crates/ide/src/hover/tests.rs | 1883 +++++++++++------ crates/ide/src/inlay_hints.rs | 2 +- crates/ide/src/inlay_hints/discriminant.rs | 2 +- crates/ide/src/inlay_hints/fn_lifetime_fn.rs | 24 +- crates/ide/src/inlay_hints/implicit_drop.rs | 5 +- crates/ide/src/inlay_hints/param_name.rs | 2 +- crates/ide/src/interpret_function.rs | 2 +- crates/ide/src/lib.rs | 11 +- crates/ide/src/markup.rs | 3 + crates/ide/src/moniker.rs | 6 +- crates/ide/src/navigation_target.rs | 6 +- crates/ide/src/references.rs | 19 +- crates/ide/src/rename.rs | 70 +- crates/ide/src/runnables.rs | 4 +- crates/ide/src/signature_help.rs | 12 +- crates/ide/src/syntax_highlighting.rs | 7 +- crates/ide/src/syntax_tree.rs | 2 - crates/ide/src/typing.rs | 21 +- crates/ide/src/view_crate_graph.rs | 2 +- crates/ide/src/view_memory_layout.rs | 4 +- crates/load-cargo/src/lib.rs | 6 +- crates/mbe/src/expander/transcriber.rs | 20 +- crates/mbe/src/lib.rs | 2 +- crates/mbe/src/syntax_bridge.rs | 4 +- crates/parser/src/grammar/items.rs | 43 +- crates/parser/src/grammar/items/use_item.rs | 28 +- .../src/tests/sourcegen_inline_tests.rs | 2 +- .../parser/err/0036_partial_use.rast | 45 +- .../err/0026_macro_rules_as_macro_name.rast | 39 + .../err/0026_macro_rules_as_macro_name.rs | 3 + .../err/0026_use_tree_list_err_recovery.rast | 46 + .../err/0026_use_tree_list_err_recovery.rs | 4 + .../ok/0208_macro_rules_as_macro_name.rast | 72 + .../ok/0208_macro_rules_as_macro_name.rs | 6 + crates/project-model/src/build_scripts.rs | 19 +- crates/project-model/src/cargo_workspace.rs | 6 +- crates/project-model/src/manifest_path.rs | 2 +- crates/project-model/src/sysroot.rs | 272 ++- crates/project-model/src/tests.rs | 65 +- crates/project-model/src/workspace.rs | 340 +-- ...rust_project_hello_world_project_model.txt | 2 +- crates/rust-analyzer/Cargo.toml | 1 + crates/rust-analyzer/src/bin/main.rs | 2 +- .../rust-analyzer/src/cli/analysis_stats.rs | 3 +- crates/rust-analyzer/src/cli/diagnostics.rs | 2 +- crates/rust-analyzer/src/cli/flags.rs | 4 + crates/rust-analyzer/src/cli/lsif.rs | 5 +- .../rust-analyzer/src/cli/progress_report.rs | 2 +- crates/rust-analyzer/src/cli/rustc_tests.rs | 61 +- crates/rust-analyzer/src/cli/scip.rs | 4 +- crates/rust-analyzer/src/config.rs | 38 +- crates/rust-analyzer/src/diagnostics.rs | 2 +- crates/rust-analyzer/src/global_state.rs | 4 +- .../src/handlers/notification.rs | 2 +- crates/rust-analyzer/src/handlers/request.rs | 43 +- crates/rust-analyzer/src/line_index.rs | 5 +- crates/rust-analyzer/src/lsp/from_proto.rs | 2 +- .../rust-analyzer/src/lsp/semantic_tokens.rs | 2 +- crates/rust-analyzer/src/lsp/to_proto.rs | 36 +- crates/rust-analyzer/src/main_loop.rs | 41 +- crates/rust-analyzer/src/reload.rs | 82 +- crates/rust-analyzer/tests/slow-tests/main.rs | 47 +- .../rust-analyzer/tests/slow-tests/support.rs | 16 +- .../rust-analyzer/tests/slow-tests/testdir.rs | 37 +- crates/rust-analyzer/tests/slow-tests/tidy.rs | 2 +- crates/span/src/lib.rs | 19 +- crates/span/src/map.rs | 2 +- crates/stdx/src/lib.rs | 4 + crates/syntax/src/algo.rs | 4 +- crates/syntax/src/ast/edit_in_place.rs | 21 +- crates/syntax/src/ast/make.rs | 3 + crates/syntax/src/ast/node_ext.rs | 28 +- crates/syntax/src/ast/token_ext.rs | 90 +- crates/syntax/src/lib.rs | 2 +- crates/syntax/src/ptr.rs | 4 +- crates/test-fixture/src/lib.rs | 2 +- crates/test-utils/src/minicore.rs | 24 +- crates/vfs-notify/src/lib.rs | 24 +- crates/vfs/src/loader.rs | 14 +- docs/user/generated_config.adoc | 15 + docs/user/manual.adoc | 3 +- editors/code/package.json | 16 +- lib/la-arena/src/map.rs | 2 + lib/line-index/src/lib.rs | 113 +- lib/lsp-server/src/lib.rs | 18 +- xtask/src/metrics.rs | 8 +- 315 files changed, 7171 insertions(+), 4498 deletions(-) create mode 100644 crates/parser/test_data/parser/inline/err/0026_macro_rules_as_macro_name.rast create mode 100644 crates/parser/test_data/parser/inline/err/0026_macro_rules_as_macro_name.rs create mode 100644 crates/parser/test_data/parser/inline/err/0026_use_tree_list_err_recovery.rast create mode 100644 crates/parser/test_data/parser/inline/err/0026_use_tree_list_err_recovery.rs create mode 100644 crates/parser/test_data/parser/inline/ok/0208_macro_rules_as_macro_name.rast create mode 100644 crates/parser/test_data/parser/inline/ok/0208_macro_rules_as_macro_name.rs diff --git a/.editorconfig b/.editorconfig index f00ade5fd8266..e337066f7eaca 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,6 +8,7 @@ end_of_line = lf insert_final_newline = true indent_style = space indent_size = 4 +max_line_length = 100 [*.md] indent_size = 2 diff --git a/Cargo.lock b/Cargo.lock index 15d06222eb42d..a743d1c870a5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,9 +160,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chalk-derive" -version = "0.95.0" +version = "0.96.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "329427f28cd2bddaacd47c4dcd3d7082d315c61fb164394c690fe98c1b6ee9d3" +checksum = "5676cea088c32290fe65c82895be9d06dd21e0fa49bb97ca840529e9417ab71a" dependencies = [ "proc-macro2", "quote", @@ -172,9 +172,9 @@ dependencies = [ [[package]] name = "chalk-ir" -version = "0.95.0" +version = "0.96.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1e1659238bd598d0f7dbc5034cf1ff46010a3d6827704c9ed443c8359cb484" +checksum = "ff550c2cdd63ff74394214dce03d06386928a641c0f08837535f04af573a966d" dependencies = [ "bitflags 2.4.1", "chalk-derive", @@ -183,9 +183,9 @@ dependencies = [ [[package]] name = "chalk-recursive" -version = "0.95.0" +version = "0.96.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3e0bff0ba1bed11407384fcec0353aeb6888901e63cb47d04505ec47adad847" +checksum = "4c4559e5c9b200240453b07d893f9c3c74413b53b0d33cbe272c68b0b77aa1c3" dependencies = [ "chalk-derive", "chalk-ir", @@ -196,9 +196,9 @@ dependencies = [ [[package]] name = "chalk-solve" -version = "0.95.0" +version = "0.96.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb9c46d501cf83732a91056c0c846ae7a16d6b3c67a6a6bb5e9cc0a2e91563b6" +checksum = "0882e68ce9eb5a0a2413806538494d19df6ee520ab17d1faf489e952f32e98b8" dependencies = [ "chalk-derive", "chalk-ir", @@ -1001,9 +1001,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.6.4" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "memmap2" @@ -1532,6 +1532,7 @@ dependencies = [ "lsp-server 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", "lsp-types", "mbe", + "memchr", "mimalloc", "nohash-hasher", "num_cpus", @@ -1712,9 +1713,9 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "smol_str" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74212e6bbe9a4352329b2f68ba3130c15a3f26fe88ff22dbdc6cdd58fa85e99c" +checksum = "e6845563ada680337a52d43bb0b29f396f2d911616f6573012645b9e3d048a49" dependencies = [ "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 35bef151196f0..2547f1ccb9915 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -105,7 +105,7 @@ dissimilar = "1.0.7" either = "1.9.0" expect-test = "1.4.0" hashbrown = { version = "0.14", features = [ - "inline-more", + "inline-more", ], default-features = false } indexmap = "2.1.0" itertools = "0.12.0" @@ -118,11 +118,11 @@ semver = "1.0.14" serde = { version = "1.0.192", features = ["derive"] } serde_json = "1.0.108" smallvec = { version = "1.10.0", features = [ - "const_new", - "union", - "const_generics", + "const_new", + "union", + "const_generics", ] } -smol_str = "0.2.0" +smol_str = "0.2.1" text-size = "1.1.1" tracing = "0.1.40" tracing-tree = "0.3.0" @@ -138,8 +138,63 @@ xshell = "0.2.5" # We need to freeze the version of the crate, as the raw-api feature is considered unstable dashmap = { version = "=5.5.3", features = ["raw-api"] } +[workspace.lints.rust] +rust_2018_idioms = "warn" +unused_lifetimes = "warn" +semicolon_in_expressions_from_macros = "warn" + [workspace.lints.clippy] -collapsible_if = "allow" -needless_pass_by_value = "allow" -nonminimal_bool = "allow" -redundant_pattern_matching = "allow" +# FIXME Remove the tidy test once the lint table is stable + +## lint groups +complexity = { level = "warn", priority = -1 } +correctness = { level = "deny", priority = -1 } +perf = { level = "deny", priority = -1 } +restriction = { level = "allow", priority = -1 } +style = { level = "warn", priority = -1 } +suspicious = { level = "warn", priority = -1 } + +## allow following lints +# () makes a fine error in most cases +result_unit_err = "allow" +# We don't expose public APIs that matter like this +len_without_is_empty = "allow" +# We have macros that rely on this currently +enum_variant_names = "allow" +# Builder pattern disagrees +new_ret_no_self = "allow" + +## Following lints should be tackled at some point +borrowed_box = "allow" +borrow_deref_ref = "allow" +derivable_impls = "allow" +derived_hash_with_manual_eq = "allow" +field_reassign_with_default = "allow" +forget_non_drop = "allow" +format_collect = "allow" +large_enum_variant = "allow" +needless_doctest_main = "allow" +new_without_default = "allow" +non_canonical_clone_impl = "allow" +non_canonical_partial_ord_impl = "allow" +self_named_constructors = "allow" +skip_while_next = "allow" +too_many_arguments = "allow" +toplevel_ref_arg = "allow" +type_complexity = "allow" +unnecessary_cast = "allow" +unnecessary_filter_map = "allow" +unnecessary_lazy_evaluations = "allow" +unnecessary_mut_passed = "allow" +useless_conversion = "allow" +useless_format = "allow" +wildcard_in_or_patterns = "allow" +wrong_self_convention = "allow" + +## warn at following lints +dbg_macro = "warn" +todo = "warn" +unimplemented = "allow" +rc_buffer = "warn" +# FIXME enable this, we use this pattern a lot so its annoying work ... +# str_to_string = "warn" diff --git a/crates/base-db/src/input.rs b/crates/base-db/src/input.rs index e45a81238ac9b..852f36ea712a6 100644 --- a/crates/base-db/src/input.rs +++ b/crates/base-db/src/input.rs @@ -9,7 +9,7 @@ use std::{fmt, mem, ops, str::FromStr}; use cfg::CfgOptions; -use la_arena::{Arena, Idx}; +use la_arena::{Arena, Idx, RawIdx}; use rustc_hash::{FxHashMap, FxHashSet}; use semver::Version; use syntax::SmolStr; @@ -157,6 +157,10 @@ impl CrateOrigin { pub fn is_lib(&self) -> bool { matches!(self, CrateOrigin::Library { .. }) } + + pub fn is_lang(&self) -> bool { + matches!(self, CrateOrigin::Lang { .. }) + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -174,7 +178,7 @@ impl From<&str> for LangCrateOrigin { match s { "alloc" => LangCrateOrigin::Alloc, "core" => LangCrateOrigin::Core, - "proc-macro" => LangCrateOrigin::ProcMacro, + "proc-macro" | "proc_macro" => LangCrateOrigin::ProcMacro, "std" => LangCrateOrigin::Std, "test" => LangCrateOrigin::Test, _ => LangCrateOrigin::Other, @@ -257,6 +261,7 @@ impl ReleaseChannel { } } + #[allow(clippy::should_implement_trait)] pub fn from_str(str: &str) -> Option { Some(match str { "" | "stable" => ReleaseChannel::Stable, @@ -326,7 +331,7 @@ impl CrateData { return false; } - if let Some(_) = opts.next() { + if opts.next().is_some() { return false; } } @@ -522,7 +527,7 @@ impl CrateGraph { self.arena.iter().map(|(idx, _)| idx) } - // FIXME: used for `handle_hack_cargo_workspace`, should be removed later + // FIXME: used for fixing up the toolchain sysroot, should be removed and done differently #[doc(hidden)] pub fn iter_mut(&mut self) -> impl Iterator + '_ { self.arena.iter_mut() @@ -619,7 +624,12 @@ impl CrateGraph { /// This will deduplicate the crates of the graph where possible. /// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id. /// If the crate dependencies were sorted, the resulting graph from this `extend` call will also have the crate dependencies sorted. - pub fn extend(&mut self, mut other: CrateGraph, proc_macros: &mut ProcMacroPaths) { + pub fn extend( + &mut self, + mut other: CrateGraph, + proc_macros: &mut ProcMacroPaths, + on_finished: impl FnOnce(&FxHashMap), + ) { let topo = other.crates_in_topological_order(); let mut id_map: FxHashMap = FxHashMap::default(); for topo in topo { @@ -630,7 +640,7 @@ impl CrateGraph { let res = self.arena.iter().find_map(|(id, data)| { match (&data.origin, &crate_data.origin) { (a, b) if a == b => { - if data.eq_ignoring_origin_and_deps(&crate_data, false) { + if data.eq_ignoring_origin_and_deps(crate_data, false) { return Some((id, false)); } } @@ -642,8 +652,8 @@ impl CrateGraph { // version and discard the library one as the local version may have // dev-dependencies that we want to keep resolving. See #15656 for more // information. - if data.eq_ignoring_origin_and_deps(&crate_data, true) { - return Some((id, if a.is_local() { false } else { true })); + if data.eq_ignoring_origin_and_deps(crate_data, true) { + return Some((id, !a.is_local())); } } (_, _) => return None, @@ -670,6 +680,8 @@ impl CrateGraph { *proc_macros = mem::take(proc_macros).into_iter().map(|(id, macros)| (id_map[&id], macros)).collect(); + + on_finished(&id_map); } fn find_path( @@ -721,6 +733,29 @@ impl CrateGraph { fn hacky_find_crate<'a>(&'a self, display_name: &'a str) -> impl Iterator + 'a { self.iter().filter(move |it| self[*it].display_name.as_deref() == Some(display_name)) } + + /// Removes all crates from this crate graph except for the ones in `to_keep` and fixes up the dependencies. + /// Returns a mapping from old crate ids to new crate ids. + pub fn remove_crates_except(&mut self, to_keep: &[CrateId]) -> Vec> { + let mut id_map = vec![None; self.arena.len()]; + self.arena = std::mem::take(&mut self.arena) + .into_iter() + .filter_map(|(id, data)| if to_keep.contains(&id) { Some((id, data)) } else { None }) + .enumerate() + .map(|(new_id, (id, data))| { + id_map[id.into_raw().into_u32() as usize] = + Some(CrateId::from_raw(RawIdx::from_u32(new_id as u32))); + data + }) + .collect(); + for (_, data) in self.arena.iter_mut() { + data.dependencies.iter_mut().for_each(|dep| { + dep.crate_id = + id_map[dep.crate_id.into_raw().into_u32() as usize].expect("crate was filtered") + }); + } + id_map + } } impl ops::Index for CrateGraph { diff --git a/crates/hir-def/src/attr.rs b/crates/hir-def/src/attr.rs index 30452e34aac36..8fbfcc81d2800 100644 --- a/crates/hir-def/src/attr.rs +++ b/crates/hir-def/src/attr.rs @@ -24,12 +24,12 @@ use triomphe::Arc; use crate::{ db::DefDatabase, - item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeNode}, + item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeModItemNode}, lang_item::LangItem, nameres::{ModuleOrigin, ModuleSource}, src::{HasChildSource, HasSource}, - AdtId, AssocItemLoc, AttrDefId, EnumId, GenericParamId, ItemLoc, LocalEnumVariantId, - LocalFieldId, Lookup, MacroId, VariantId, + AdtId, AssocItemLoc, AttrDefId, GenericParamId, ItemLoc, LocalFieldId, Lookup, MacroId, + VariantId, }; #[derive(Default, Debug, Clone, PartialEq, Eq)] @@ -70,33 +70,6 @@ impl ops::Deref for AttrsWithOwner { impl Attrs { pub const EMPTY: Self = Self(RawAttrs::EMPTY); - pub(crate) fn variants_attrs_query( - db: &dyn DefDatabase, - e: EnumId, - ) -> Arc> { - let _p = profile::span("variants_attrs_query"); - // FIXME: There should be some proper form of mapping between item tree enum variant ids and hir enum variant ids - let mut res = ArenaMap::default(); - - let loc = e.lookup(db); - let krate = loc.container.krate; - let item_tree = loc.id.item_tree(db); - let enum_ = &item_tree[loc.id.value]; - let crate_graph = db.crate_graph(); - let cfg_options = &crate_graph[krate].cfg_options; - - let mut idx = 0; - for variant in enum_.variants.clone() { - let attrs = item_tree.attrs(db, krate, variant.into()); - if attrs.is_cfg_enabled(cfg_options) { - res.insert(Idx::from_raw(RawIdx::from(idx)), attrs); - idx += 1; - } - } - - Arc::new(res) - } - pub(crate) fn fields_attrs_query( db: &dyn DefDatabase, v: VariantId, @@ -108,29 +81,11 @@ impl Attrs { let crate_graph = db.crate_graph(); let (fields, item_tree, krate) = match v { VariantId::EnumVariantId(it) => { - let e = it.parent; - let loc = e.lookup(db); - let krate = loc.container.krate; + let loc = it.lookup(db); + let krate = loc.parent.lookup(db).container.krate; let item_tree = loc.id.item_tree(db); - let enum_ = &item_tree[loc.id.value]; - - let cfg_options = &crate_graph[krate].cfg_options; - - let Some(variant) = enum_ - .variants - .clone() - .filter(|variant| { - let attrs = item_tree.attrs(db, krate, (*variant).into()); - attrs.is_cfg_enabled(cfg_options) - }) - .zip(0u32..) - .find(|(_variant, idx)| it.local_id == Idx::from_raw(RawIdx::from(*idx))) - .map(|(variant, _idx)| variant) - else { - return Arc::new(res); - }; - - (item_tree[variant].fields.clone(), item_tree, krate) + let variant = &item_tree[loc.id.value]; + (variant.fields.clone(), item_tree, krate) } VariantId::StructId(it) => { let loc = it.lookup(db); @@ -401,10 +356,12 @@ impl AttrsWithOwner { AttrDefId::FieldId(it) => { return db.fields_attrs(it.parent)[it.local_id].clone(); } + // FIXME: DRY this up AttrDefId::EnumVariantId(it) => { - return db.variants_attrs(it.parent)[it.local_id].clone(); + let id = it.lookup(db).id; + let tree = id.item_tree(db); + tree.raw_attrs(id.value.into()).clone() } - // FIXME: DRY this up AttrDefId::AdtId(it) => match it { AdtId::StructId(it) => attrs_from_item_tree_loc(db, it), AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it), @@ -503,12 +460,7 @@ impl AttrsWithOwner { AdtId::EnumId(id) => any_has_attrs(db, id), }, AttrDefId::FunctionId(id) => any_has_attrs(db, id), - AttrDefId::EnumVariantId(id) => { - let map = db.variants_attrs_source_map(id.parent); - let file_id = id.parent.lookup(db).id.file_id(); - let root = db.parse_or_expand(file_id); - InFile::new(file_id, ast::AnyHasAttrs::new(map[id.local_id].to_node(&root))) - } + AttrDefId::EnumVariantId(id) => any_has_attrs(db, id), AttrDefId::StaticId(id) => any_has_attrs(db, id), AttrDefId::ConstId(id) => any_has_attrs(db, id), AttrDefId::TraitId(id) => any_has_attrs(db, id), @@ -654,13 +606,16 @@ fn any_has_attrs<'db>( id.lookup(db).source(db).map(ast::AnyHasAttrs::new) } -fn attrs_from_item_tree(db: &dyn DefDatabase, id: ItemTreeId) -> RawAttrs { +fn attrs_from_item_tree( + db: &dyn DefDatabase, + id: ItemTreeId, +) -> RawAttrs { let tree = id.item_tree(db); let mod_item = N::id_to_mod_item(id.value); tree.raw_attrs(mod_item.into()).clone() } -fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>( +fn attrs_from_item_tree_loc<'db, N: ItemTreeModItemNode>( db: &(dyn DefDatabase + 'db), lookup: impl Lookup = dyn DefDatabase + 'db, Data = ItemLoc>, ) -> RawAttrs { @@ -668,7 +623,7 @@ fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>( attrs_from_item_tree(db, id) } -fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>( +fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>( db: &(dyn DefDatabase + 'db), lookup: impl Lookup = dyn DefDatabase + 'db, Data = AssocItemLoc>, ) -> RawAttrs { @@ -676,20 +631,6 @@ fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>( attrs_from_item_tree(db, id) } -pub(crate) fn variants_attrs_source_map( - db: &dyn DefDatabase, - def: EnumId, -) -> Arc>> { - let mut res = ArenaMap::default(); - let child_source = def.child_source(db); - - for (idx, variant) in child_source.value.iter() { - res.insert(idx, AstPtr::new(variant)); - } - - Arc::new(res) -} - pub(crate) fn fields_attrs_source_map( db: &dyn DefDatabase, def: VariantId, diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/body.rs index db28c6731ece1..81132d738539f 100644 --- a/crates/hir-def/src/body.rs +++ b/crates/hir-def/src/body.rs @@ -26,7 +26,7 @@ use crate::{ }, nameres::DefMap, path::{ModPath, Path}, - src::{HasChildSource, HasSource}, + src::HasSource, BlockId, DefWithBodyId, HasModule, Lookup, }; @@ -37,7 +37,7 @@ pub struct Body { pub pats: Arena, pub bindings: Arena, pub labels: Arena