From ab0938d0df6f6801e0ad2d1f6d11f05106d00646 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 7 Jun 2022 00:55:12 -0700 Subject: [PATCH 1/9] Filter out intrinsics if we have other import candidates to suggest --- .../rustc_resolve/src/late/diagnostics.rs | 14 ++++++++++- src/test/ui/resolve/filter-intrinsics.rs | 10 ++++++++ src/test/ui/resolve/filter-intrinsics.stderr | 25 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/resolve/filter-intrinsics.rs create mode 100644 src/test/ui/resolve/filter-intrinsics.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index cb39eb5416ba2..6f1b1996474e4 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -384,7 +384,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { // Try to lookup name in more relaxed fashion for better error reporting. let ident = path.last().unwrap().ident; - let candidates = self + let mut candidates = self .r .lookup_import_candidates(ident, ns, &self.parent_scope, is_expected) .into_iter() @@ -396,6 +396,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { }) .collect::>(); let crate_def_id = CRATE_DEF_ID.to_def_id(); + // Try to filter out intrinsics candidates, as long as we have + // some other candidates to suggest. + let intrinsic_candidates: Vec<_> = candidates + .drain_filter(|sugg| { + let path = path_names_to_string(&sugg.path); + path.starts_with("core::intrinsics::") || path.starts_with("std::intrinsics::") + }) + .collect(); + if candidates.is_empty() { + // Put them back if we have no more candidates to suggest... + candidates.extend(intrinsic_candidates); + } if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) { let mut enum_candidates: Vec<_> = self .r diff --git a/src/test/ui/resolve/filter-intrinsics.rs b/src/test/ui/resolve/filter-intrinsics.rs new file mode 100644 index 0000000000000..c0956ef85aff6 --- /dev/null +++ b/src/test/ui/resolve/filter-intrinsics.rs @@ -0,0 +1,10 @@ +fn main() { + // Should suggest only `std::mem::size_of` + let _ = size_of::(); + //~^ ERROR cannot find + + // Should suggest `std::intrinsics::fabsf64`, + // since there is no non-intrinsic to suggest. + let _ = fabsf64(1.0); + //~^ ERROR cannot find +} diff --git a/src/test/ui/resolve/filter-intrinsics.stderr b/src/test/ui/resolve/filter-intrinsics.stderr new file mode 100644 index 0000000000000..955070891fbbe --- /dev/null +++ b/src/test/ui/resolve/filter-intrinsics.stderr @@ -0,0 +1,25 @@ +error[E0425]: cannot find function `size_of` in this scope + --> $DIR/filter-intrinsics.rs:3:13 + | +LL | let _ = size_of::(); + | ^^^^^^^ not found in this scope + | +help: consider importing this function + | +LL | use std::mem::size_of; + | + +error[E0425]: cannot find function `fabsf64` in this scope + --> $DIR/filter-intrinsics.rs:8:13 + | +LL | let _ = fabsf64(1.0); + | ^^^^^^^ not found in this scope + | +help: consider importing this function + | +LL | use std::intrinsics::fabsf64; + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0425`. From 4ea4e2e76db49ce8ee5dbb9656f1a07a24a5c674 Mon Sep 17 00:00:00 2001 From: klensy Date: Mon, 13 Jun 2022 21:50:44 +0300 Subject: [PATCH 2/9] remove currently unused deps --- Cargo.lock | 14 -------------- compiler/rustc_codegen_llvm/Cargo.toml | 1 - compiler/rustc_driver/Cargo.toml | 6 +++--- compiler/rustc_hir/Cargo.toml | 1 - compiler/rustc_infer/Cargo.toml | 1 - compiler/rustc_interface/Cargo.toml | 4 +++- compiler/rustc_mir_dataflow/Cargo.toml | 1 - compiler/rustc_mir_transform/Cargo.toml | 1 - compiler/rustc_monomorphize/Cargo.toml | 1 - compiler/rustc_passes/Cargo.toml | 1 - compiler/rustc_plugin_impl/Cargo.toml | 2 -- compiler/rustc_query_impl/Cargo.toml | 1 - compiler/rustc_query_system/Cargo.toml | 1 - compiler/rustc_symbol_mangling/Cargo.toml | 1 - 14 files changed, 6 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51b0da65b3145..a6d7eb4514d48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3663,7 +3663,6 @@ dependencies = [ "libloading", "measureme 10.0.0", "rustc-demangle", - "rustc_arena", "rustc_ast", "rustc_attr", "rustc_codegen_ssa", @@ -3785,7 +3784,6 @@ dependencies = [ "rustc_ast", "rustc_ast_pretty", "rustc_codegen_ssa", - "rustc_const_eval", "rustc_data_structures", "rustc_error_codes", "rustc_errors", @@ -3800,7 +3798,6 @@ dependencies = [ "rustc_parse", "rustc_plugin_impl", "rustc_save_analysis", - "rustc_serialize", "rustc_session", "rustc_span", "rustc_target", @@ -3896,7 +3893,6 @@ dependencies = [ "rustc_ast", "rustc_data_structures", "rustc_error_messages", - "rustc_feature", "rustc_index", "rustc_macros", "rustc_serialize", @@ -3957,7 +3953,6 @@ dependencies = [ "rustc_macros", "rustc_middle", "rustc_serialize", - "rustc_session", "rustc_span", "rustc_target", "smallvec", @@ -4191,7 +4186,6 @@ dependencies = [ "rustc_index", "rustc_middle", "rustc_serialize", - "rustc_session", "rustc_span", "rustc_target", "smallvec", @@ -4213,7 +4207,6 @@ dependencies = [ "rustc_index", "rustc_middle", "rustc_mir_dataflow", - "rustc_query_system", "rustc_serialize", "rustc_session", "rustc_span", @@ -4228,7 +4221,6 @@ name = "rustc_monomorphize" version = "0.0.0" dependencies = [ "rustc_data_structures", - "rustc_errors", "rustc_hir", "rustc_index", "rustc_middle", @@ -4280,7 +4272,6 @@ dependencies = [ "rustc_index", "rustc_lexer", "rustc_middle", - "rustc_parse", "rustc_serialize", "rustc_session", "rustc_span", @@ -4295,10 +4286,8 @@ dependencies = [ "libloading", "rustc_ast", "rustc_errors", - "rustc_hir", "rustc_lint", "rustc_metadata", - "rustc_middle", "rustc_session", "rustc_span", ] @@ -4337,7 +4326,6 @@ dependencies = [ "rustc_serialize", "rustc_session", "rustc_span", - "rustc_type_ir", "tracing", ] @@ -4359,7 +4347,6 @@ dependencies = [ "rustc_session", "rustc_span", "rustc_target", - "rustc_type_ir", "smallvec", "tracing", ] @@ -4464,7 +4451,6 @@ dependencies = [ "rustc_data_structures", "rustc_hir", "rustc_middle", - "rustc_query_system", "rustc_session", "rustc_span", "rustc_target", diff --git a/compiler/rustc_codegen_llvm/Cargo.toml b/compiler/rustc_codegen_llvm/Cargo.toml index 67183ff5887e9..383bd8ab7bad7 100644 --- a/compiler/rustc_codegen_llvm/Cargo.toml +++ b/compiler/rustc_codegen_llvm/Cargo.toml @@ -16,7 +16,6 @@ measureme = "10.0.0" tracing = "0.1" rustc_middle = { path = "../rustc_middle" } rustc-demangle = "0.1.21" -rustc_arena = { path = "../rustc_arena" } rustc_attr = { path = "../rustc_attr" } rustc_codegen_ssa = { path = "../rustc_codegen_ssa" } rustc_data_structures = { path = "../rustc_data_structures" } diff --git a/compiler/rustc_driver/Cargo.toml b/compiler/rustc_driver/Cargo.toml index a2cbd9abc9047..08d5d4f343c77 100644 --- a/compiler/rustc_driver/Cargo.toml +++ b/compiler/rustc_driver/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" crate-type = ["dylib"] [dependencies] -libc = "0.2" tracing = { version = "0.1.28" } serde_json = "1.0.59" rustc_log = { path = "../rustc_log" } @@ -21,7 +20,6 @@ rustc_feature = { path = "../rustc_feature" } rustc_hir = { path = "../rustc_hir" } rustc_hir_pretty = { path = "../rustc_hir_pretty" } rustc_metadata = { path = "../rustc_metadata" } -rustc_const_eval = { path = "../rustc_const_eval" } rustc_parse = { path = "../rustc_parse" } rustc_plugin_impl = { path = "../rustc_plugin_impl" } rustc_save_analysis = { path = "../rustc_save_analysis" } @@ -29,11 +27,13 @@ rustc_codegen_ssa = { path = "../rustc_codegen_ssa" } rustc_session = { path = "../rustc_session" } rustc_error_codes = { path = "../rustc_error_codes" } rustc_interface = { path = "../rustc_interface" } -rustc_serialize = { path = "../rustc_serialize" } rustc_ast = { path = "../rustc_ast" } rustc_span = { path = "../rustc_span" } rustc_typeck = { path = "../rustc_typeck" } +[target.'cfg(unix)'.dependencies] +libc = "0.2" + [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["consoleapi", "debugapi", "processenv"] } diff --git a/compiler/rustc_hir/Cargo.toml b/compiler/rustc_hir/Cargo.toml index 34d366f401332..47ace7ca3a750 100644 --- a/compiler/rustc_hir/Cargo.toml +++ b/compiler/rustc_hir/Cargo.toml @@ -8,7 +8,6 @@ doctest = false [dependencies] rustc_target = { path = "../rustc_target" } -rustc_feature = { path = "../rustc_feature" } rustc_macros = { path = "../rustc_macros" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_error_messages = { path = "../rustc_error_messages" } diff --git a/compiler/rustc_infer/Cargo.toml b/compiler/rustc_infer/Cargo.toml index f87ea43b1a72c..bbc2b76773471 100644 --- a/compiler/rustc_infer/Cargo.toml +++ b/compiler/rustc_infer/Cargo.toml @@ -14,7 +14,6 @@ rustc_errors = { path = "../rustc_errors" } rustc_hir = { path = "../rustc_hir" } rustc_index = { path = "../rustc_index" } rustc_macros = { path = "../rustc_macros" } -rustc_session = { path = "../rustc_session" } rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } rustc_target = { path = "../rustc_target" } diff --git a/compiler/rustc_interface/Cargo.toml b/compiler/rustc_interface/Cargo.toml index 47dc5f2ef8670..26b1b9948a047 100644 --- a/compiler/rustc_interface/Cargo.toml +++ b/compiler/rustc_interface/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" doctest = false [dependencies] -libc = "0.2" libloading = "0.7.1" tracing = "0.1" rustc-rayon-core = { version = "0.4.0", optional = true } @@ -49,6 +48,9 @@ rustc_trait_selection = { path = "../rustc_trait_selection" } rustc_ty_utils = { path = "../rustc_ty_utils" } tempfile = "3.2" +[target.'cfg(unix)'.dependencies] +libc = "0.2" + [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["libloaderapi"] } diff --git a/compiler/rustc_mir_dataflow/Cargo.toml b/compiler/rustc_mir_dataflow/Cargo.toml index ffd7e3cd06bd2..a0c70a3fd81f1 100644 --- a/compiler/rustc_mir_dataflow/Cargo.toml +++ b/compiler/rustc_mir_dataflow/Cargo.toml @@ -18,6 +18,5 @@ rustc_hir = { path = "../rustc_hir" } rustc_index = { path = "../rustc_index" } rustc_middle = { path = "../rustc_middle" } rustc_serialize = { path = "../rustc_serialize" } -rustc_session = { path = "../rustc_session" } rustc_target = { path = "../rustc_target" } rustc_span = { path = "../rustc_span" } diff --git a/compiler/rustc_mir_transform/Cargo.toml b/compiler/rustc_mir_transform/Cargo.toml index 4c1a7eaf6f03b..8a8098e9ab996 100644 --- a/compiler/rustc_mir_transform/Cargo.toml +++ b/compiler/rustc_mir_transform/Cargo.toml @@ -19,7 +19,6 @@ rustc_index = { path = "../rustc_index" } rustc_middle = { path = "../rustc_middle" } rustc_const_eval = { path = "../rustc_const_eval" } rustc_mir_dataflow = { path = "../rustc_mir_dataflow" } -rustc_query_system = { path = "../rustc_query_system" } rustc_serialize = { path = "../rustc_serialize" } rustc_session = { path = "../rustc_session" } rustc_target = { path = "../rustc_target" } diff --git a/compiler/rustc_monomorphize/Cargo.toml b/compiler/rustc_monomorphize/Cargo.toml index f812afe6b62ba..e4ac47f4982cb 100644 --- a/compiler/rustc_monomorphize/Cargo.toml +++ b/compiler/rustc_monomorphize/Cargo.toml @@ -10,7 +10,6 @@ doctest = false smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } tracing = "0.1" rustc_data_structures = { path = "../rustc_data_structures" } -rustc_errors = { path = "../rustc_errors" } rustc_hir = { path = "../rustc_hir" } rustc_index = { path = "../rustc_index" } rustc_middle = { path = "../rustc_middle" } diff --git a/compiler/rustc_passes/Cargo.toml b/compiler/rustc_passes/Cargo.toml index a3ef1981e84f9..787d37277c6c0 100644 --- a/compiler/rustc_passes/Cargo.toml +++ b/compiler/rustc_passes/Cargo.toml @@ -12,7 +12,6 @@ rustc_errors = { path = "../rustc_errors" } rustc_expand = { path = "../rustc_expand" } rustc_hir = { path = "../rustc_hir" } rustc_index = { path = "../rustc_index" } -rustc_parse = { path = "../rustc_parse" } rustc_session = { path = "../rustc_session" } rustc_target = { path = "../rustc_target" } rustc_ast = { path = "../rustc_ast" } diff --git a/compiler/rustc_plugin_impl/Cargo.toml b/compiler/rustc_plugin_impl/Cargo.toml index f5071eb6e8f96..b6ea533c80bdd 100644 --- a/compiler/rustc_plugin_impl/Cargo.toml +++ b/compiler/rustc_plugin_impl/Cargo.toml @@ -9,9 +9,7 @@ doctest = false [dependencies] libloading = "0.7.1" -rustc_middle = { path = "../rustc_middle" } rustc_errors = { path = "../rustc_errors" } -rustc_hir = { path = "../rustc_hir" } rustc_lint = { path = "../rustc_lint" } rustc_metadata = { path = "../rustc_metadata" } rustc_ast = { path = "../rustc_ast" } diff --git a/compiler/rustc_query_impl/Cargo.toml b/compiler/rustc_query_impl/Cargo.toml index 856f09b6fcc32..5673bb83b15f4 100644 --- a/compiler/rustc_query_impl/Cargo.toml +++ b/compiler/rustc_query_impl/Cargo.toml @@ -20,7 +20,6 @@ rustc_query_system = { path = "../rustc_query_system" } rustc_serialize = { path = "../rustc_serialize" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } -rustc_type_ir = { path = "../rustc_type_ir" } tracing = "0.1" [features] diff --git a/compiler/rustc_query_system/Cargo.toml b/compiler/rustc_query_system/Cargo.toml index 85fbb08eeeb53..b5a37cf324b00 100644 --- a/compiler/rustc_query_system/Cargo.toml +++ b/compiler/rustc_query_system/Cargo.toml @@ -21,7 +21,6 @@ rustc_serialize = { path = "../rustc_serialize" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } rustc_target = { path = "../rustc_target" } -rustc_type_ir = { path = "../rustc_type_ir" } parking_lot = "0.11" smallvec = { version = "1.6.1", features = ["union", "may_dangle"] } diff --git a/compiler/rustc_symbol_mangling/Cargo.toml b/compiler/rustc_symbol_mangling/Cargo.toml index b9751f0adb517..d5befa10e2363 100644 --- a/compiler/rustc_symbol_mangling/Cargo.toml +++ b/compiler/rustc_symbol_mangling/Cargo.toml @@ -16,5 +16,4 @@ rustc_middle = { path = "../rustc_middle" } rustc_hir = { path = "../rustc_hir" } rustc_target = { path = "../rustc_target" } rustc_data_structures = { path = "../rustc_data_structures" } -rustc_query_system = { path = "../rustc_query_system" } rustc_session = { path = "../rustc_session" } From 767d0e714233488d9cec713e7def4d940454a43b Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 13 Jun 2022 17:16:01 -0300 Subject: [PATCH 3/9] Move tests --- src/test/ui/{issues => consts}/issue-39974.rs | 0 src/test/ui/{issues => consts}/issue-39974.stderr | 0 src/test/ui/{issues => lifetimes}/issue-17728.rs | 0 src/test/ui/{issues => lifetimes}/issue-17728.stderr | 0 src/test/ui/{issues => lifetimes}/issue-55796.rs | 0 src/test/ui/{issues => lifetimes}/issue-55796.stderr | 0 src/test/ui/{issues => lto}/issue-11154.rs | 0 src/test/ui/{issues => lto}/issue-11154.stderr | 0 .../feature-gate-generic_assert.rs | 0 src/test/ui/{issues => nll}/issue-46023.rs | 0 src/test/ui/{issues => nll}/issue-46023.stderr | 0 src/test/ui/{issues => traits}/issue-18400.rs | 0 src/test/ui/{issues => traits}/issue-18400.stderr | 0 src/test/ui/{issues => traits}/issue-18412.rs | 0 src/test/ui/{issues => traits}/issue-35869.rs | 0 src/test/ui/{issues => traits}/issue-35869.stderr | 0 src/test/ui/{issues => typeck}/issue-29124.rs | 0 src/test/ui/{issues => typeck}/issue-29124.stderr | 0 src/tools/tidy/src/ui_tests.rs | 4 ++-- 19 files changed, 2 insertions(+), 2 deletions(-) rename src/test/ui/{issues => consts}/issue-39974.rs (100%) rename src/test/ui/{issues => consts}/issue-39974.stderr (100%) rename src/test/ui/{issues => lifetimes}/issue-17728.rs (100%) rename src/test/ui/{issues => lifetimes}/issue-17728.stderr (100%) rename src/test/ui/{issues => lifetimes}/issue-55796.rs (100%) rename src/test/ui/{issues => lifetimes}/issue-55796.stderr (100%) rename src/test/ui/{issues => lto}/issue-11154.rs (100%) rename src/test/ui/{issues => lto}/issue-11154.stderr (100%) rename src/test/ui/{ => macros}/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs (100%) rename src/test/ui/{issues => nll}/issue-46023.rs (100%) rename src/test/ui/{issues => nll}/issue-46023.stderr (100%) rename src/test/ui/{issues => traits}/issue-18400.rs (100%) rename src/test/ui/{issues => traits}/issue-18400.stderr (100%) rename src/test/ui/{issues => traits}/issue-18412.rs (100%) rename src/test/ui/{issues => traits}/issue-35869.rs (100%) rename src/test/ui/{issues => traits}/issue-35869.stderr (100%) rename src/test/ui/{issues => typeck}/issue-29124.rs (100%) rename src/test/ui/{issues => typeck}/issue-29124.stderr (100%) diff --git a/src/test/ui/issues/issue-39974.rs b/src/test/ui/consts/issue-39974.rs similarity index 100% rename from src/test/ui/issues/issue-39974.rs rename to src/test/ui/consts/issue-39974.rs diff --git a/src/test/ui/issues/issue-39974.stderr b/src/test/ui/consts/issue-39974.stderr similarity index 100% rename from src/test/ui/issues/issue-39974.stderr rename to src/test/ui/consts/issue-39974.stderr diff --git a/src/test/ui/issues/issue-17728.rs b/src/test/ui/lifetimes/issue-17728.rs similarity index 100% rename from src/test/ui/issues/issue-17728.rs rename to src/test/ui/lifetimes/issue-17728.rs diff --git a/src/test/ui/issues/issue-17728.stderr b/src/test/ui/lifetimes/issue-17728.stderr similarity index 100% rename from src/test/ui/issues/issue-17728.stderr rename to src/test/ui/lifetimes/issue-17728.stderr diff --git a/src/test/ui/issues/issue-55796.rs b/src/test/ui/lifetimes/issue-55796.rs similarity index 100% rename from src/test/ui/issues/issue-55796.rs rename to src/test/ui/lifetimes/issue-55796.rs diff --git a/src/test/ui/issues/issue-55796.stderr b/src/test/ui/lifetimes/issue-55796.stderr similarity index 100% rename from src/test/ui/issues/issue-55796.stderr rename to src/test/ui/lifetimes/issue-55796.stderr diff --git a/src/test/ui/issues/issue-11154.rs b/src/test/ui/lto/issue-11154.rs similarity index 100% rename from src/test/ui/issues/issue-11154.rs rename to src/test/ui/lto/issue-11154.rs diff --git a/src/test/ui/issues/issue-11154.stderr b/src/test/ui/lto/issue-11154.stderr similarity index 100% rename from src/test/ui/issues/issue-11154.stderr rename to src/test/ui/lto/issue-11154.stderr diff --git a/src/test/ui/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs b/src/test/ui/macros/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs similarity index 100% rename from src/test/ui/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs rename to src/test/ui/macros/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs diff --git a/src/test/ui/issues/issue-46023.rs b/src/test/ui/nll/issue-46023.rs similarity index 100% rename from src/test/ui/issues/issue-46023.rs rename to src/test/ui/nll/issue-46023.rs diff --git a/src/test/ui/issues/issue-46023.stderr b/src/test/ui/nll/issue-46023.stderr similarity index 100% rename from src/test/ui/issues/issue-46023.stderr rename to src/test/ui/nll/issue-46023.stderr diff --git a/src/test/ui/issues/issue-18400.rs b/src/test/ui/traits/issue-18400.rs similarity index 100% rename from src/test/ui/issues/issue-18400.rs rename to src/test/ui/traits/issue-18400.rs diff --git a/src/test/ui/issues/issue-18400.stderr b/src/test/ui/traits/issue-18400.stderr similarity index 100% rename from src/test/ui/issues/issue-18400.stderr rename to src/test/ui/traits/issue-18400.stderr diff --git a/src/test/ui/issues/issue-18412.rs b/src/test/ui/traits/issue-18412.rs similarity index 100% rename from src/test/ui/issues/issue-18412.rs rename to src/test/ui/traits/issue-18412.rs diff --git a/src/test/ui/issues/issue-35869.rs b/src/test/ui/traits/issue-35869.rs similarity index 100% rename from src/test/ui/issues/issue-35869.rs rename to src/test/ui/traits/issue-35869.rs diff --git a/src/test/ui/issues/issue-35869.stderr b/src/test/ui/traits/issue-35869.stderr similarity index 100% rename from src/test/ui/issues/issue-35869.stderr rename to src/test/ui/traits/issue-35869.stderr diff --git a/src/test/ui/issues/issue-29124.rs b/src/test/ui/typeck/issue-29124.rs similarity index 100% rename from src/test/ui/issues/issue-29124.rs rename to src/test/ui/typeck/issue-29124.rs diff --git a/src/test/ui/issues/issue-29124.stderr b/src/test/ui/typeck/issue-29124.stderr similarity index 100% rename from src/test/ui/issues/issue-29124.stderr rename to src/test/ui/typeck/issue-29124.stderr diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index 135a12b55713a..f59121181d29b 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -7,8 +7,8 @@ use std::path::Path; const ENTRY_LIMIT: usize = 1000; // FIXME: The following limits should be reduced eventually. -const ROOT_ENTRY_LIMIT: usize = 969; -const ISSUES_ENTRY_LIMIT: usize = 2211; +const ROOT_ENTRY_LIMIT: usize = 968; +const ISSUES_ENTRY_LIMIT: usize = 2179; fn check_entries(path: &Path, bad: &mut bool) { let dirs = walkdir::WalkDir::new(&path.join("test/ui")) From abe45a9ffa6a0af65a58427b475203b301b07be8 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 14 Jun 2022 14:52:01 +1000 Subject: [PATCH 4/9] Rename rustc_serialize::opaque::Encoder as MemEncoder. This avoids the name clash with `rustc_serialize::Encoder` (a trait), and allows lots qualifiers to be removed and imports to be simplified (e.g. fewer `as` imports). (This was previously merged as commit 5 in #94732 and then was reverted in #97905 because of a perf regression caused by commit 4 in #94732.) --- compiler/rustc_ast/src/ast.rs | 6 +- compiler/rustc_codegen_ssa/src/lib.rs | 7 +- .../rustc_data_structures/src/fingerprint.rs | 10 +-- .../rustc_incremental/src/persist/load.rs | 6 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 9 +-- compiler/rustc_metadata/src/rmeta/encoder.rs | 11 +-- compiler/rustc_metadata/src/rmeta/mod.rs | 4 +- compiler/rustc_metadata/src/rmeta/table.rs | 6 +- .../src/mir/graph_cyclic_cache.rs | 10 +-- compiler/rustc_middle/src/mir/predecessors.rs | 6 +- .../rustc_middle/src/mir/switch_sources.rs | 6 +- compiler/rustc_middle/src/mir/traversal.rs | 6 +- .../rustc_query_impl/src/on_disk_cache.rs | 12 ++-- .../src/dep_graph/serialized.rs | 6 +- compiler/rustc_serialize/src/opaque.rs | 52 +++++++------- compiler/rustc_serialize/tests/opaque.rs | 10 +-- compiler/rustc_span/src/lib.rs | 4 +- compiler/rustc_type_ir/src/sty.rs | 72 +++++++------------ src/librustdoc/scrape_examples.rs | 4 +- .../deriving-encodable-decodable-box.rs | 6 +- ...riving-encodable-decodable-cell-refcell.rs | 6 +- src/test/ui-fulldeps/issue-14021.rs | 6 +- 22 files changed, 124 insertions(+), 141 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 4e2495ab14c9a..e5b61d7000a32 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -31,7 +31,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::sync::Lrc; use rustc_data_structures::thin_vec::ThinVec; use rustc_macros::HashStable_Generic; -use rustc_serialize::{self, Decoder, Encoder}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -2488,11 +2488,11 @@ rustc_index::newtype_index! { } } -impl rustc_serialize::Encodable for AttrId { +impl Encodable for AttrId { fn encode(&self, _s: &mut S) {} } -impl rustc_serialize::Decodable for AttrId { +impl Decodable for AttrId { fn decode(_: &mut D) -> AttrId { crate::attr::mk_attr_id() } diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 771157dcad954..750432b0b2653 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -29,7 +29,8 @@ use rustc_middle::dep_graph::WorkProduct; use rustc_middle::middle::dependency_format::Dependencies; use rustc_middle::middle::exported_symbols::SymbolExportKind; use rustc_middle::ty::query::{ExternProviders, Providers}; -use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT}; use rustc_session::cstore::{self, CrateSource}; use rustc_session::utils::NativeLibKind; @@ -203,7 +204,7 @@ const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION"); impl CodegenResults { pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec { - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); encoder.emit_raw_bytes(RLINK_MAGIC); // `emit_raw_bytes` is used to make sure that the version representation does not depend on // Encoder's inner representation of `u32`. @@ -230,7 +231,7 @@ impl CodegenResults { return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string()); } - let mut decoder = opaque::Decoder::new(&data[4..], 0); + let mut decoder = MemDecoder::new(&data[4..], 0); let rustc_version = decoder.read_str(); let current_version = RUSTC_VERSION.unwrap(); if rustc_version != current_version { diff --git a/compiler/rustc_data_structures/src/fingerprint.rs b/compiler/rustc_data_structures/src/fingerprint.rs index a032b039f34ef..5ff2d18dd2be3 100644 --- a/compiler/rustc_data_structures/src/fingerprint.rs +++ b/compiler/rustc_data_structures/src/fingerprint.rs @@ -1,5 +1,5 @@ use crate::stable_hasher; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::hash::{Hash, Hasher}; @@ -142,14 +142,14 @@ impl stable_hasher::StableHasherResult for Fingerprint { impl_stable_hash_via_hash!(Fingerprint); -impl Encodable for Fingerprint { +impl Encodable for Fingerprint { #[inline] fn encode(&self, s: &mut E) { s.emit_raw_bytes(&self.to_le_bytes()); } } -impl Decodable for Fingerprint { +impl Decodable for Fingerprint { #[inline] fn decode(d: &mut D) -> Self { Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()) @@ -184,7 +184,7 @@ impl std::fmt::Display for PackedFingerprint { } } -impl Encodable for PackedFingerprint { +impl Encodable for PackedFingerprint { #[inline] fn encode(&self, s: &mut E) { // Copy to avoid taking reference to packed field. @@ -193,7 +193,7 @@ impl Encodable for PackedFingerprint { } } -impl Decodable for PackedFingerprint { +impl Decodable for PackedFingerprint { #[inline] fn decode(d: &mut D) -> Self { Self(Fingerprint::decode(d)) diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs index 9de14950aa8d3..9c325faae8058 100644 --- a/compiler/rustc_incremental/src/persist/load.rs +++ b/compiler/rustc_incremental/src/persist/load.rs @@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::memmap::Mmap; use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId}; use rustc_middle::ty::OnDiskCache; -use rustc_serialize::opaque::Decoder; +use rustc_serialize::opaque::MemDecoder; use rustc_serialize::Decodable; use rustc_session::config::IncrementalStateAssertion; use rustc_session::Session; @@ -156,7 +156,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture { if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result { // Decode the list of work_products - let mut work_product_decoder = Decoder::new(&work_products_data[..], start_pos); + let mut work_product_decoder = MemDecoder::new(&work_products_data[..], start_pos); let work_products: Vec = Decodable::decode(&mut work_product_decoder); @@ -193,7 +193,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture { LoadResult::DataOutOfDate => LoadResult::DataOutOfDate, LoadResult::Error { message } => LoadResult::Error { message }, LoadResult::Ok { data: (bytes, start_pos) } => { - let mut decoder = Decoder::new(&bytes, start_pos); + let mut decoder = MemDecoder::new(&bytes, start_pos); let prev_commandline_args_hash = u64::decode(&mut decoder); if prev_commandline_args_hash != expected_hash { diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 186031d4586aa..c1ded99a25af5 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -26,7 +26,8 @@ use rustc_middle::ty::codec::TyDecoder; use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::GeneratorDiagnosticData; use rustc_middle::ty::{self, ParameterizedOverTcx, Ty, TyCtxt, Visibility}; -use rustc_serialize::{opaque, Decodable, Decoder}; +use rustc_serialize::opaque::MemDecoder; +use rustc_serialize::{Decodable, Decoder}; use rustc_session::cstore::{ CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib, }; @@ -154,7 +155,7 @@ struct ImportedSourceFile { } pub(super) struct DecodeContext<'a, 'tcx> { - opaque: opaque::Decoder<'a>, + opaque: MemDecoder<'a>, cdata: Option>, blob: &'a MetadataBlob, sess: Option<&'tcx Session>, @@ -186,7 +187,7 @@ pub(super) trait Metadata<'a, 'tcx>: Copy { fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> { let tcx = self.tcx(); DecodeContext { - opaque: opaque::Decoder::new(self.blob(), pos), + opaque: MemDecoder::new(self.blob(), pos), cdata: self.cdata(), blob: self.blob(), sess: self.sess().or(tcx.map(|tcx| tcx.sess)), @@ -418,7 +419,7 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> { where F: FnOnce(&mut Self) -> R, { - let new_opaque = opaque::Decoder::new(self.opaque.data, pos); + let new_opaque = MemDecoder::new(self.opaque.data, pos); let old_opaque = mem::replace(&mut self.opaque, new_opaque); let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode); let r = f(self); diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 50c09aaf8d445..26fb21020008a 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,8 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; -use rustc_serialize::{opaque, Encodable, Encoder}; +use rustc_serialize::opaque::MemEncoder; +use rustc_serialize::{Encodable, Encoder}; use rustc_session::config::CrateType; use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib}; use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind}; @@ -43,7 +44,7 @@ use std::num::NonZeroUsize; use tracing::{debug, trace}; pub(super) struct EncodeContext<'a, 'tcx> { - opaque: opaque::Encoder, + opaque: MemEncoder, tcx: TyCtxt<'tcx>, feat: &'tcx rustc_feature::Features, @@ -93,8 +94,8 @@ macro_rules! encoder_methods { } impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> { - type Ok = ::Ok; - type Err = ::Err; + type Ok = ::Ok; + type Err = ::Err; encoder_methods! { emit_usize(usize); @@ -2180,7 +2181,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata { } fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata { - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); encoder.emit_raw_bytes(METADATA_HEADER); // Will be filled with the root position after encoding everything. diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index fb2ffe1d73d96..04f0847f5cccc 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -22,7 +22,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, ReprOptions, Ty}; use rustc_middle::ty::{GeneratorDiagnosticData, ParameterizedOverTcx, TyCtxt}; -use rustc_serialize::opaque::Encoder; +use rustc_serialize::opaque::MemEncoder; use rustc_session::config::SymbolManglingVersion; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_span::edition::Edition; @@ -323,7 +323,7 @@ macro_rules! define_tables { } impl TableBuilders { - fn encode(&self, buf: &mut Encoder) -> LazyTables { + fn encode(&self, buf: &mut MemEncoder) -> LazyTables { LazyTables { $($name: self.$name.encode(buf)),+ } diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 8baa67a8f9fcf..5ab4269ae99ad 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -4,8 +4,8 @@ use rustc_data_structures::fingerprint::Fingerprint; use rustc_hir::def::{CtorKind, CtorOf}; use rustc_index::vec::Idx; use rustc_middle::ty::ParameterizedOverTcx; -use rustc_serialize::opaque::Encoder; -use rustc_serialize::Encoder as _; +use rustc_serialize::opaque::MemEncoder; +use rustc_serialize::Encoder; use rustc_span::hygiene::MacroKind; use std::convert::TryInto; use std::marker::PhantomData; @@ -281,7 +281,7 @@ where Some(value).write_to_bytes(&mut self.blocks[i]); } - pub(crate) fn encode(&self, buf: &mut Encoder) -> LazyTable + pub(crate) fn encode(&self, buf: &mut MemEncoder) -> LazyTable where Option: FixedSizeEncoding, { diff --git a/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs b/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs index 096bf8cbc158a..1279f5aee3691 100644 --- a/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs +++ b/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs @@ -3,7 +3,7 @@ use rustc_data_structures::graph::{ }; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::OnceCell; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; /// Helper type to cache the result of `graph::is_cyclic`. #[derive(Clone, Debug)] @@ -36,17 +36,17 @@ impl GraphIsCyclicCache { } } -impl serialize::Encodable for GraphIsCyclicCache { +impl Encodable for GraphIsCyclicCache { #[inline] fn encode(&self, s: &mut S) { - serialize::Encodable::encode(&(), s); + Encodable::encode(&(), s); } } -impl serialize::Decodable for GraphIsCyclicCache { +impl Decodable for GraphIsCyclicCache { #[inline] fn decode(d: &mut D) -> Self { - let () = serialize::Decodable::decode(d); + let () = Decodable::decode(d); Self::new() } } diff --git a/compiler/rustc_middle/src/mir/predecessors.rs b/compiler/rustc_middle/src/mir/predecessors.rs index 9bc0cb1138ff1..620cf7e336ba4 100644 --- a/compiler/rustc_middle/src/mir/predecessors.rs +++ b/compiler/rustc_middle/src/mir/predecessors.rs @@ -3,7 +3,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::OnceCell; use rustc_index::vec::IndexVec; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use smallvec::SmallVec; use crate::mir::{BasicBlock, BasicBlockData}; @@ -54,12 +54,12 @@ impl PredecessorCache { } } -impl serialize::Encodable for PredecessorCache { +impl Encodable for PredecessorCache { #[inline] fn encode(&self, _s: &mut S) {} } -impl serialize::Decodable for PredecessorCache { +impl Decodable for PredecessorCache { #[inline] fn decode(_: &mut D) -> Self { Self::new() diff --git a/compiler/rustc_middle/src/mir/switch_sources.rs b/compiler/rustc_middle/src/mir/switch_sources.rs index 4872a7835e3fa..99d13fcfef43e 100644 --- a/compiler/rustc_middle/src/mir/switch_sources.rs +++ b/compiler/rustc_middle/src/mir/switch_sources.rs @@ -5,7 +5,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_map::FxHashMap; use rustc_data_structures::sync::OnceCell; use rustc_index::vec::IndexVec; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use smallvec::SmallVec; use crate::mir::{BasicBlock, BasicBlockData, Terminator, TerminatorKind}; @@ -54,12 +54,12 @@ impl SwitchSourceCache { } } -impl serialize::Encodable for SwitchSourceCache { +impl Encodable for SwitchSourceCache { #[inline] fn encode(&self, _s: &mut S) {} } -impl serialize::Decodable for SwitchSourceCache { +impl Decodable for SwitchSourceCache { #[inline] fn decode(_: &mut D) -> Self { Self::new() diff --git a/compiler/rustc_middle/src/mir/traversal.rs b/compiler/rustc_middle/src/mir/traversal.rs index f745e55307ae2..7228e3f33b126 100644 --- a/compiler/rustc_middle/src/mir/traversal.rs +++ b/compiler/rustc_middle/src/mir/traversal.rs @@ -1,7 +1,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::OnceCell; use rustc_index::bit_set::BitSet; -use rustc_serialize as serialize; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use super::*; @@ -365,12 +365,12 @@ impl PostorderCache { } } -impl serialize::Encodable for PostorderCache { +impl Encodable for PostorderCache { #[inline] fn encode(&self, _s: &mut S) {} } -impl serialize::Decodable for PostorderCache { +impl Decodable for PostorderCache { #[inline] fn decode(_: &mut D) -> Self { Self::new() diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index c2c876f7f1a12..d7327ca4bc602 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -15,7 +15,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_query_system::dep_graph::DepContext; use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects}; use rustc_serialize::{ - opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize}, + opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder}, Decodable, Decoder, Encodable, Encoder, }; use rustc_session::Session; @@ -158,7 +158,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { // Wrap in a scope so we can borrow `data`. let footer: Footer = { - let mut decoder = opaque::Decoder::new(&data, start_pos); + let mut decoder = MemDecoder::new(&data, start_pos); // Decode the *position* of the footer, which can be found in the // last 8 bytes of the file. @@ -437,7 +437,7 @@ impl<'sess> OnDiskCache<'sess> { let serialized_data = self.serialized_data.read(); let mut decoder = CacheDecoder { tcx, - opaque: opaque::Decoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()), + opaque: MemDecoder::new(serialized_data.as_deref().unwrap_or(&[]), pos.to_usize()), source_map: self.source_map, file_index_to_file: &self.file_index_to_file, file_index_to_stable_id: &self.file_index_to_stable_id, @@ -458,7 +458,7 @@ impl<'sess> OnDiskCache<'sess> { /// will also handle things that contain `Ty` instances. pub struct CacheDecoder<'a, 'tcx> { tcx: TyCtxt<'tcx>, - opaque: opaque::Decoder<'a>, + opaque: MemDecoder<'a>, source_map: &'a SourceMap, file_index_to_file: &'a Lock>>, file_index_to_stable_id: &'a FxHashMap, @@ -510,7 +510,7 @@ trait DecoderWithPosition: Decoder { fn position(&self) -> usize; } -impl<'a> DecoderWithPosition for opaque::Decoder<'a> { +impl<'a> DecoderWithPosition for MemDecoder<'a> { fn position(&self) -> usize { self.position() } @@ -586,7 +586,7 @@ impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> { { debug_assert!(pos < self.opaque.data.len()); - let new_opaque = opaque::Decoder::new(self.opaque.data, pos); + let new_opaque = MemDecoder::new(self.opaque.data, pos); let old_opaque = mem::replace(&mut self.opaque, new_opaque); let r = f(self); self.opaque = old_opaque; diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index 7fde9c0119b85..d583b45698ac4 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -19,7 +19,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sync::Lock; use rustc_index::vec::{Idx, IndexVec}; -use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize}; +use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixedSize, MemDecoder}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use smallvec::SmallVec; use std::convert::TryInto; @@ -96,11 +96,11 @@ impl SerializedDepGraph { } } -impl<'a, K: DepKind + Decodable>> Decodable> +impl<'a, K: DepKind + Decodable>> Decodable> for SerializedDepGraph { #[instrument(level = "debug", skip(d))] - fn decode(d: &mut opaque::Decoder<'a>) -> SerializedDepGraph { + fn decode(d: &mut MemDecoder<'a>) -> SerializedDepGraph { let start_position = d.position(); // The last 16 bytes are the node count and edge count. diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index b2dbf937eb75a..f828c0b71063d 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,5 +1,5 @@ use crate::leb128::{self, max_leb128_len}; -use crate::serialize::{self, Decoder as _, Encoder as _}; +use crate::serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::fs::File; use std::io::{self, Write}; @@ -11,13 +11,13 @@ use std::ptr; // Encoder // ----------------------------------------------------------------------------- -pub struct Encoder { +pub struct MemEncoder { pub data: Vec, } -impl Encoder { - pub fn new() -> Encoder { - Encoder { data: vec![] } +impl MemEncoder { + pub fn new() -> MemEncoder { + MemEncoder { data: vec![] } } #[inline] @@ -53,7 +53,7 @@ macro_rules! write_leb128 { /// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout const STR_SENTINEL: u8 = 0xC1; -impl serialize::Encoder for Encoder { +impl Encoder for MemEncoder { type Ok = Vec; type Err = !; @@ -161,7 +161,7 @@ pub type FileEncodeResult = Result; // `FileEncoder` encodes data to file via fixed-size buffer. // // When encoding large amounts of data to a file, using `FileEncoder` may be -// preferred over using `Encoder` to encode to a `Vec`, and then writing the +// preferred over using `MemEncoder` to encode to a `Vec`, and then writing the // `Vec` to file, as the latter uses as much memory as there is encoded data, // while the former uses the fixed amount of memory allocated to the buffer. // `FileEncoder` also has the advantage of not needing to reallocate as data @@ -425,7 +425,7 @@ macro_rules! file_encoder_write_leb128 { }}; } -impl serialize::Encoder for FileEncoder { +impl Encoder for FileEncoder { type Ok = usize; type Err = io::Error; @@ -535,15 +535,15 @@ impl serialize::Encoder for FileEncoder { // Decoder // ----------------------------------------------------------------------------- -pub struct Decoder<'a> { +pub struct MemDecoder<'a> { pub data: &'a [u8], position: usize, } -impl<'a> Decoder<'a> { +impl<'a> MemDecoder<'a> { #[inline] - pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> { - Decoder { data, position } + pub fn new(data: &'a [u8], position: usize) -> MemDecoder<'a> { + MemDecoder { data, position } } #[inline] @@ -566,7 +566,7 @@ macro_rules! read_leb128 { ($dec:expr, $fun:ident) => {{ leb128::$fun($dec.data, &mut $dec.position) }}; } -impl<'a> serialize::Decoder for Decoder<'a> { +impl<'a> Decoder for MemDecoder<'a> { #[inline] fn read_u128(&mut self) -> u128 { read_leb128!(self, read_u128_leb128) @@ -688,25 +688,25 @@ impl<'a> serialize::Decoder for Decoder<'a> { // Specialize encoding byte slices. This specialization also applies to encoding `Vec`s, etc., // since the default implementations call `encode` on their slices internally. -impl serialize::Encodable for [u8] { - fn encode(&self, e: &mut Encoder) { - serialize::Encoder::emit_usize(e, self.len()); +impl Encodable for [u8] { + fn encode(&self, e: &mut MemEncoder) { + Encoder::emit_usize(e, self.len()); e.emit_raw_bytes(self); } } -impl serialize::Encodable for [u8] { +impl Encodable for [u8] { fn encode(&self, e: &mut FileEncoder) { - serialize::Encoder::emit_usize(e, self.len()); + Encoder::emit_usize(e, self.len()); e.emit_raw_bytes(self); } } // Specialize decoding `Vec`. This specialization also applies to decoding `Box<[u8]>`s, etc., // since the default implementations call `decode` to produce a `Vec` internally. -impl<'a> serialize::Decodable> for Vec { - fn decode(d: &mut Decoder<'a>) -> Self { - let len = serialize::Decoder::read_usize(d); +impl<'a> Decodable> for Vec { + fn decode(d: &mut MemDecoder<'a>) -> Self { + let len = Decoder::read_usize(d); d.read_raw_bytes(len).to_owned() } } @@ -718,9 +718,9 @@ impl IntEncodedWithFixedSize { pub const ENCODED_SIZE: usize = 8; } -impl serialize::Encodable for IntEncodedWithFixedSize { +impl Encodable for IntEncodedWithFixedSize { #[inline] - fn encode(&self, e: &mut Encoder) { + fn encode(&self, e: &mut MemEncoder) { let _start_pos = e.position(); e.emit_raw_bytes(&self.0.to_le_bytes()); let _end_pos = e.position(); @@ -728,7 +728,7 @@ impl serialize::Encodable for IntEncodedWithFixedSize { } } -impl serialize::Encodable for IntEncodedWithFixedSize { +impl Encodable for IntEncodedWithFixedSize { #[inline] fn encode(&self, e: &mut FileEncoder) { let _start_pos = e.position(); @@ -738,9 +738,9 @@ impl serialize::Encodable for IntEncodedWithFixedSize { } } -impl<'a> serialize::Decodable> for IntEncodedWithFixedSize { +impl<'a> Decodable> for IntEncodedWithFixedSize { #[inline] - fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize { + fn decode(decoder: &mut MemDecoder<'a>) -> IntEncodedWithFixedSize { let _start_pos = decoder.position(); let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); let value = u64::from_le_bytes(bytes.try_into().unwrap()); diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 703b7f5e7a5f4..4eafb6fabde8e 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -1,7 +1,7 @@ #![allow(rustc::internal)] use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque::{Decoder, Encoder}; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder as EncoderTrait}; use std::fmt::Debug; @@ -28,16 +28,18 @@ struct Struct { q: Option, } -fn check_round_trip + for<'a> Decodable> + PartialEq + Debug>( +fn check_round_trip< + T: Encodable + for<'a> Decodable> + PartialEq + Debug, +>( values: Vec, ) { - let mut encoder = Encoder::new(); + let mut encoder = MemEncoder::new(); for value in &values { Encodable::encode(value, &mut encoder); } let data = encoder.finish().unwrap(); - let mut decoder = Decoder::new(&data[..], 0); + let mut decoder = MemDecoder::new(&data[..], 0); for value in values { let decoded = Decodable::decode(&mut decoder); diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index d7e2f621065f0..5d3d56b1e6699 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1911,13 +1911,13 @@ impl_pos! { pub struct CharPos(pub usize); } -impl Encodable for BytePos { +impl Encodable for BytePos { fn encode(&self, s: &mut S) { s.emit_u32(self.0); } } -impl Decodable for BytePos { +impl Decodable for BytePos { fn decode(d: &mut D) -> BytePos { BytePos(d.read_u32()) } diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs index a6a0d02c8ba99..9407218439993 100644 --- a/compiler/rustc_type_ir/src/sty.rs +++ b/compiler/rustc_type_ir/src/sty.rs @@ -14,7 +14,7 @@ use crate::UintTy; use self::TyKind::*; use rustc_data_structures::stable_hasher::HashStable; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Decoder, Encodable}; /// Defines the kinds of types used by the type system. /// @@ -833,56 +833,34 @@ where I::AllocId: Decodable, { fn decode(d: &mut D) -> Self { - match rustc_serialize::Decoder::read_usize(d) { + match Decoder::read_usize(d) { 0 => Bool, 1 => Char, - 2 => Int(rustc_serialize::Decodable::decode(d)), - 3 => Uint(rustc_serialize::Decodable::decode(d)), - 4 => Float(rustc_serialize::Decodable::decode(d)), - 5 => Adt(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)), - 6 => Foreign(rustc_serialize::Decodable::decode(d)), + 2 => Int(Decodable::decode(d)), + 3 => Uint(Decodable::decode(d)), + 4 => Float(Decodable::decode(d)), + 5 => Adt(Decodable::decode(d), Decodable::decode(d)), + 6 => Foreign(Decodable::decode(d)), 7 => Str, - 8 => { - Array(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 9 => Slice(rustc_serialize::Decodable::decode(d)), - 10 => RawPtr(rustc_serialize::Decodable::decode(d)), - 11 => Ref( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 12 => { - FnDef(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 13 => FnPtr(rustc_serialize::Decodable::decode(d)), - 14 => Dynamic( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 15 => Closure( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 16 => Generator( - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - rustc_serialize::Decodable::decode(d), - ), - 17 => GeneratorWitness(rustc_serialize::Decodable::decode(d)), + 8 => Array(Decodable::decode(d), Decodable::decode(d)), + 9 => Slice(Decodable::decode(d)), + 10 => RawPtr(Decodable::decode(d)), + 11 => Ref(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)), + 12 => FnDef(Decodable::decode(d), Decodable::decode(d)), + 13 => FnPtr(Decodable::decode(d)), + 14 => Dynamic(Decodable::decode(d), Decodable::decode(d)), + 15 => Closure(Decodable::decode(d), Decodable::decode(d)), + 16 => Generator(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)), + 17 => GeneratorWitness(Decodable::decode(d)), 18 => Never, - 19 => Tuple(rustc_serialize::Decodable::decode(d)), - 20 => Projection(rustc_serialize::Decodable::decode(d)), - 21 => { - Opaque(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 22 => Param(rustc_serialize::Decodable::decode(d)), - 23 => { - Bound(rustc_serialize::Decodable::decode(d), rustc_serialize::Decodable::decode(d)) - } - 24 => Placeholder(rustc_serialize::Decodable::decode(d)), - 25 => Infer(rustc_serialize::Decodable::decode(d)), - 26 => Error(rustc_serialize::Decodable::decode(d)), + 19 => Tuple(Decodable::decode(d)), + 20 => Projection(Decodable::decode(d)), + 21 => Opaque(Decodable::decode(d), Decodable::decode(d)), + 22 => Param(Decodable::decode(d)), + 23 => Bound(Decodable::decode(d), Decodable::decode(d)), + 24 => Placeholder(Decodable::decode(d)), + 25 => Infer(Decodable::decode(d)), + 26 => Error(Decodable::decode(d)), _ => panic!( "{}", format!( diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 242f926967c93..da09ae9dd0662 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -17,7 +17,7 @@ use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, TyCtxt}; use rustc_serialize::{ - opaque::{Decoder, FileEncoder}, + opaque::{FileEncoder, MemDecoder}, Decodable, Encodable, Encoder, }; use rustc_session::getopts; @@ -336,7 +336,7 @@ pub(crate) fn load_call_locations( let mut all_calls: AllCallLocations = FxHashMap::default(); for path in with_examples { let bytes = fs::read(&path).map_err(|e| format!("{} (for path {})", e, path))?; - let mut decoder = Decoder::new(&bytes, 0); + let mut decoder = MemDecoder::new(&bytes, 0); let calls = AllCallLocations::decode(&mut decoder); for (function, fn_calls) in calls.into_iter() { diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs index a09deeec4f182..4ad4ef60a5294 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-box.rs @@ -7,7 +7,7 @@ extern crate rustc_macros; extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder}; #[derive(Encodable, Decodable)] @@ -18,11 +18,11 @@ struct A { fn main() { let obj = A { foo: Box::new([true, false]) }; - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); obj.encode(&mut encoder); let data = encoder.finish().unwrap(); - let mut decoder = opaque::Decoder::new(&data, 0); + let mut decoder = MemDecoder::new(&data, 0); let obj2 = A::decode(&mut decoder); assert_eq!(obj.foo, obj2.foo); diff --git a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs index 9b6fb0e580621..3ac3abae692af 100644 --- a/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs +++ b/src/test/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs @@ -9,7 +9,7 @@ extern crate rustc_macros; extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder}; use std::cell::{Cell, RefCell}; @@ -27,11 +27,11 @@ struct B { fn main() { let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) }; - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); obj.encode(&mut encoder); let data = encoder.finish().unwrap(); - let mut decoder = opaque::Decoder::new(&data, 0); + let mut decoder = MemDecoder::new(&data, 0); let obj2 = B::decode(&mut decoder); assert_eq!(obj.foo.get(), obj2.foo.get()); diff --git a/src/test/ui-fulldeps/issue-14021.rs b/src/test/ui-fulldeps/issue-14021.rs index 4241456367e46..b7b6e1b860d35 100644 --- a/src/test/ui-fulldeps/issue-14021.rs +++ b/src/test/ui-fulldeps/issue-14021.rs @@ -8,7 +8,7 @@ extern crate rustc_macros; extern crate rustc_serialize; use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder}; #[derive(Encodable, Decodable, PartialEq, Debug)] @@ -17,11 +17,11 @@ struct UnitLikeStruct; pub fn main() { let obj = UnitLikeStruct; - let mut encoder = opaque::Encoder::new(); + let mut encoder = MemEncoder::new(); obj.encode(&mut encoder); let data = encoder.finish().unwrap(); - let mut decoder = opaque::Decoder::new(&data, 0); + let mut decoder = MemDecoder::new(&data, 0); let obj2 = UnitLikeStruct::decode(&mut decoder); assert_eq!(obj, obj2); From 801725a77b17a0641fcf40a310f491d71b059d80 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 14 Jun 2022 14:58:46 +0900 Subject: [PATCH 5/9] suggest adding a `#[macro_export]` to a private macro --- compiler/rustc_resolve/src/imports.rs | 36 +++++++++++++++---- src/test/ui/privacy/macro-private-reexport.rs | 11 ++++++ .../ui/privacy/macro-private-reexport.stderr | 17 +++++++++ .../uniform-paths/macro-rules.stderr | 8 ++--- 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 src/test/ui/privacy/macro-private-reexport.rs create mode 100644 src/test/ui/privacy/macro-private-reexport.stderr diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index de83a3a5932c2..a457169c74b7f 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -12,7 +12,7 @@ use rustc_ast::NodeId; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::intern::Interned; use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan}; -use rustc_hir::def::{self, PartialRes}; +use rustc_hir::def::{self, DefKind, PartialRes}; use rustc_middle::metadata::ModChild; use rustc_middle::span_bug; use rustc_middle::ty; @@ -922,11 +922,35 @@ impl<'a, 'b> ImportResolver<'a, 'b> { .note(&format!("consider declaring type or module `{}` with `pub`", ident)) .emit(); } else { - let note_msg = - format!("consider marking `{}` as `pub` in the imported module", ident); - struct_span_err!(self.r.session, import.span, E0364, "{}", error_msg) - .span_note(import.span, ¬e_msg) - .emit(); + let mut err = + struct_span_err!(self.r.session, import.span, E0364, "{error_msg}"); + match binding.kind { + NameBindingKind::Res(Res::Def(DefKind::Macro(_), _def_id), _) + // exclude decl_macro + if !self.r.session.features_untracked().decl_macro + || !self + .r + .session + .source_map() + .span_to_snippet(binding.span) + .map(|snippet| snippet.starts_with("macro ")) + .unwrap_or(true) => + { + err.span_help( + binding.span, + "consider adding a `#[macro_export]` to the macro in the imported module", + ); + } + _ => { + err.span_note( + import.span, + &format!( + "consider marking `{ident}` as `pub` in the imported module" + ), + ); + } + } + err.emit(); } } } diff --git a/src/test/ui/privacy/macro-private-reexport.rs b/src/test/ui/privacy/macro-private-reexport.rs new file mode 100644 index 0000000000000..5b53a861d25b5 --- /dev/null +++ b/src/test/ui/privacy/macro-private-reexport.rs @@ -0,0 +1,11 @@ +// edition:2018 + +mod foo { + macro_rules! bar { + () => {}; + } + + pub use bar as _; //~ ERROR `bar` is only public within the crate, and cannot be re-exported outside +} + +fn main() {} diff --git a/src/test/ui/privacy/macro-private-reexport.stderr b/src/test/ui/privacy/macro-private-reexport.stderr new file mode 100644 index 0000000000000..af85cbcf3f2e3 --- /dev/null +++ b/src/test/ui/privacy/macro-private-reexport.stderr @@ -0,0 +1,17 @@ +error[E0364]: `bar` is only public within the crate, and cannot be re-exported outside + --> $DIR/macro-private-reexport.rs:8:13 + | +LL | pub use bar as _; + | ^^^^^^^^ + | +help: consider adding a `#[macro_export]` to the macro in the imported module + --> $DIR/macro-private-reexport.rs:4:5 + | +LL | / macro_rules! bar { +LL | | () => {}; +LL | | } + | |_____^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0364`. diff --git a/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr b/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr index 9e48e26b1dfd3..9f8c928c32c04 100644 --- a/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr +++ b/src/test/ui/rust-2018/uniform-paths/macro-rules.stderr @@ -4,11 +4,11 @@ error[E0364]: `legacy_macro` is only public within the crate, and cannot be re-e LL | pub use legacy_macro as _; | ^^^^^^^^^^^^^^^^^ | -note: consider marking `legacy_macro` as `pub` in the imported module - --> $DIR/macro-rules.rs:11:13 +help: consider adding a `#[macro_export]` to the macro in the imported module + --> $DIR/macro-rules.rs:7:5 | -LL | pub use legacy_macro as _; - | ^^^^^^^^^^^^^^^^^ +LL | macro_rules! legacy_macro { () => () } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0659]: `legacy_macro` is ambiguous --> $DIR/macro-rules.rs:31:13 From 60a50d02abfa3552c825b8d7690cf73343195499 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 14 Jun 2022 15:46:11 +0900 Subject: [PATCH 6/9] change edition to 2021 --- src/test/ui/privacy/macro-private-reexport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/privacy/macro-private-reexport.rs b/src/test/ui/privacy/macro-private-reexport.rs index 5b53a861d25b5..bc3e6fb5c59f4 100644 --- a/src/test/ui/privacy/macro-private-reexport.rs +++ b/src/test/ui/privacy/macro-private-reexport.rs @@ -1,4 +1,4 @@ -// edition:2018 +// edition:2021 mod foo { macro_rules! bar { From 0d24405211404f5e6b5a746af1ecd08d3d2b4438 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 15 Jun 2022 00:31:21 +0900 Subject: [PATCH 7/9] implement `MacroData` --- .../rustc_resolve/src/build_reduced_graph.rs | 35 +++++++++++-------- compiler/rustc_resolve/src/ident.rs | 2 +- compiler/rustc_resolve/src/imports.rs | 11 ++---- compiler/rustc_resolve/src/lib.rs | 10 ++++-- compiler/rustc_resolve/src/macros.rs | 4 +-- 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index f8fa7a0941d00..b0e7679af972b 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -10,7 +10,9 @@ use crate::imports::{Import, ImportKind}; use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef}; use crate::Namespace::{self, MacroNS, TypeNS, ValueNS}; use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, ModuleOrUniformRoot}; -use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError}; +use crate::{ + MacroData, NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError, +}; use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError}; use rustc_ast::visit::{self, AssocCtxt, Visitor}; @@ -20,7 +22,6 @@ use rustc_ast_lowering::ResolverAstLowering; use rustc_attr as attr; use rustc_data_structures::sync::Lrc; use rustc_errors::{struct_span_err, Applicability}; -use rustc_expand::base::SyntaxExtension; use rustc_expand::expand::AstFragment; use rustc_hir::def::{self, *}; use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; @@ -180,26 +181,32 @@ impl<'a> Resolver<'a> { } } - pub(crate) fn get_macro(&mut self, res: Res) -> Option> { + pub(crate) fn get_macro(&mut self, res: Res) -> Option { match res { Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)), - Res::NonMacroAttr(_) => Some(self.non_macro_attr.clone()), + Res::NonMacroAttr(_) => { + Some(MacroData { ext: self.non_macro_attr.clone(), macro_rules: false }) + } _ => None, } } - pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> Lrc { - if let Some(ext) = self.macro_map.get(&def_id) { - return ext.clone(); + pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> MacroData { + if let Some(macro_data) = self.macro_map.get(&def_id) { + return macro_data.clone(); } - let ext = Lrc::new(match self.cstore().load_macro_untracked(def_id, &self.session) { - LoadedMacro::MacroDef(item, edition) => self.compile_macro(&item, edition).0, - LoadedMacro::ProcMacro(ext) => ext, - }); + let (ext, macro_rules) = match self.cstore().load_macro_untracked(def_id, &self.session) { + LoadedMacro::MacroDef(item, edition) => ( + Lrc::new(self.compile_macro(&item, edition).0), + matches!(item.kind, ItemKind::MacroDef(def) if def.macro_rules), + ), + LoadedMacro::ProcMacro(extz) => (Lrc::new(extz), false), + }; - self.macro_map.insert(def_id, ext.clone()); - ext + let macro_data = MacroData { ext, macro_rules }; + self.macro_map.insert(def_id, macro_data.clone()); + macro_data } pub(crate) fn build_reduced_graph( @@ -1251,7 +1258,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { }; let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id.to_def_id()); - self.r.macro_map.insert(def_id.to_def_id(), ext); + self.r.macro_map.insert(def_id.to_def_id(), MacroData { ext, macro_rules }); self.r.local_macro_def_scopes.insert(def_id, parent_scope.module); if macro_rules { diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index b25393c3ed8e4..e934e189f05f3 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -241,7 +241,7 @@ impl<'a> Resolver<'a> { { // The macro is a proc macro derive if let Some(def_id) = module.expansion.expn_data().macro_def_id { - let ext = self.get_macro_by_def_id(def_id); + let ext = self.get_macro_by_def_id(def_id).ext; if ext.builtin_name.is_none() && ext.macro_kind() == MacroKind::Derive && parent.expansion.outer_expn_is_descendant_of(*ctxt) diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index a457169c74b7f..c6aa57f039d4f 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -925,16 +925,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> { let mut err = struct_span_err!(self.r.session, import.span, E0364, "{error_msg}"); match binding.kind { - NameBindingKind::Res(Res::Def(DefKind::Macro(_), _def_id), _) + NameBindingKind::Res(Res::Def(DefKind::Macro(_), def_id), _) // exclude decl_macro - if !self.r.session.features_untracked().decl_macro - || !self - .r - .session - .source_map() - .span_to_snippet(binding.span) - .map(|snippet| snippet.starts_with("macro ")) - .unwrap_or(true) => + if self.r.get_macro_by_def_id(def_id).macro_rules => { err.span_help( binding.span, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 92a65fe249f4c..ac4e23cc04db5 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -866,6 +866,12 @@ struct DeriveData { has_derive_copy: bool, } +#[derive(Clone)] +struct MacroData { + ext: Lrc, + macro_rules: bool, +} + /// The main resolver class. /// /// This is the visitor that walks the whole crate. @@ -965,7 +971,7 @@ pub struct Resolver<'a> { registered_attrs: FxHashSet, registered_tools: RegisteredTools, macro_use_prelude: FxHashMap>, - macro_map: FxHashMap>, + macro_map: FxHashMap, dummy_ext_bang: Lrc, dummy_ext_derive: Lrc, non_macro_attr: Lrc, @@ -1522,7 +1528,7 @@ impl<'a> Resolver<'a> { } fn is_builtin_macro(&mut self, res: Res) -> bool { - self.get_macro(res).map_or(false, |ext| ext.builtin_name.is_some()) + self.get_macro(res).map_or(false, |macro_data| macro_data.ext.builtin_name.is_some()) } fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId { diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 3fb34cdcd9bd9..c86c2280d1986 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -658,7 +658,7 @@ impl<'a> Resolver<'a> { res }; - res.map(|res| (self.get_macro(res), res)) + res.map(|res| (self.get_macro(res).map(|macro_data| macro_data.ext), res)) } pub(crate) fn finalize_macro_resolutions(&mut self) { @@ -853,7 +853,7 @@ impl<'a> Resolver<'a> { // Reserve some names that are not quite covered by the general check // performed on `Resolver::builtin_attrs`. if ident.name == sym::cfg || ident.name == sym::cfg_attr { - let macro_kind = self.get_macro(res).map(|ext| ext.macro_kind()); + let macro_kind = self.get_macro(res).map(|macro_data| macro_data.ext.macro_kind()); if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) { self.session.span_err( ident.span, From d29915af79f4a372647b67e7d83f1fc4aabbb92a Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 15 Jun 2022 00:42:10 +0900 Subject: [PATCH 8/9] add a test case for `decl_macro` --- src/test/ui/privacy/macro-private-reexport.rs | 6 ++++++ .../ui/privacy/macro-private-reexport.stderr | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/test/ui/privacy/macro-private-reexport.rs b/src/test/ui/privacy/macro-private-reexport.rs index bc3e6fb5c59f4..d0aab528ed480 100644 --- a/src/test/ui/privacy/macro-private-reexport.rs +++ b/src/test/ui/privacy/macro-private-reexport.rs @@ -1,11 +1,17 @@ // edition:2021 +#![feature(decl_macro)] + mod foo { macro_rules! bar { () => {}; } pub use bar as _; //~ ERROR `bar` is only public within the crate, and cannot be re-exported outside + + macro baz() {} + + pub use baz as _; //~ ERROR `baz` is private, and cannot be re-exported } fn main() {} diff --git a/src/test/ui/privacy/macro-private-reexport.stderr b/src/test/ui/privacy/macro-private-reexport.stderr index af85cbcf3f2e3..b8768f3612e61 100644 --- a/src/test/ui/privacy/macro-private-reexport.stderr +++ b/src/test/ui/privacy/macro-private-reexport.stderr @@ -1,17 +1,29 @@ error[E0364]: `bar` is only public within the crate, and cannot be re-exported outside - --> $DIR/macro-private-reexport.rs:8:13 + --> $DIR/macro-private-reexport.rs:10:13 | LL | pub use bar as _; | ^^^^^^^^ | help: consider adding a `#[macro_export]` to the macro in the imported module - --> $DIR/macro-private-reexport.rs:4:5 + --> $DIR/macro-private-reexport.rs:6:5 | LL | / macro_rules! bar { LL | | () => {}; LL | | } | |_____^ -error: aborting due to previous error +error[E0364]: `baz` is private, and cannot be re-exported + --> $DIR/macro-private-reexport.rs:14:13 + | +LL | pub use baz as _; + | ^^^^^^^^ + | +note: consider marking `baz` as `pub` in the imported module + --> $DIR/macro-private-reexport.rs:14:13 + | +LL | pub use baz as _; + | ^^^^^^^^ + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0364`. From 50f6a9ed87e47c7a8ff6aefcde01a33821e80e20 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Mon, 13 Jun 2022 21:49:59 -0400 Subject: [PATCH 9/9] use unchecked mul to compute slice sizes ...since slice sizes can't signed wrap see https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html > The total size len * mem::size_of::() of the slice must be no larger than isize::MAX. --- compiler/rustc_codegen_ssa/src/glue.rs | 7 ++++- .../codegen/issue-96497-slice-size-nowrap.rs | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/test/codegen/issue-96497-slice-size-nowrap.rs diff --git a/compiler/rustc_codegen_ssa/src/glue.rs b/compiler/rustc_codegen_ssa/src/glue.rs index 694f5434e9afd..e6f402ef19d87 100644 --- a/compiler/rustc_codegen_ssa/src/glue.rs +++ b/compiler/rustc_codegen_ssa/src/glue.rs @@ -39,7 +39,12 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( // The info in this case is the length of the str, so the size is that // times the unit size. ( - bx.mul(info.unwrap(), bx.const_usize(unit.size.bytes())), + // All slice sizes must fit into `isize`, so this multiplication cannot (signed) wrap. + // NOTE: ideally, we want the effects of both `unchecked_smul` and `unchecked_umul` + // (resulting in `mul nsw nuw` in LLVM IR), since we know that the multiplication + // cannot signed wrap, and that both operands are non-negative. But at the time of writing, + // `BuilderMethods` can't do this, and it doesn't seem to enable any further optimizations. + bx.unchecked_smul(info.unwrap(), bx.const_usize(unit.size.bytes())), bx.const_usize(unit.align.abi.bytes()), ) } diff --git a/src/test/codegen/issue-96497-slice-size-nowrap.rs b/src/test/codegen/issue-96497-slice-size-nowrap.rs new file mode 100644 index 0000000000000..a5dbef9346027 --- /dev/null +++ b/src/test/codegen/issue-96497-slice-size-nowrap.rs @@ -0,0 +1,29 @@ +// This test case checks that LLVM is aware that computing the size of a slice cannot wrap. +// The possibility of wrapping results in an additional branch when dropping boxed slices +// in some situations, see https://github.com/rust-lang/rust/issues/96497#issuecomment-1112865218 + +// compile-flags: -O +// min-llvm-version: 14.0 + +#![crate_type="lib"] + +// CHECK-LABEL: @simple_size_of_nowrap +#[no_mangle] +pub fn simple_size_of_nowrap(x: &[u32]) -> usize { + // Make sure the shift used to compute the size has a nowrap flag. + + // CHECK: [[A:%.*]] = shl nsw {{.*}}, 2 + // CHECK-NEXT: ret {{.*}} [[A]] + core::mem::size_of_val(x) +} + +// CHECK-LABEL: @drop_write +#[no_mangle] +pub fn drop_write(mut x: Box<[u32]>) { + // Check that this write is optimized out. + // This depends on the size calculation not wrapping, + // since otherwise LLVM can't tell that the memory is always deallocated if the slice len > 0. + + // CHECK-NOT: store i32 42 + x[1] = 42; +}