From f5e0b760d0ef062447f4f5b35adef5c75c8c69ff Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Mon, 26 Sep 2022 16:04:58 +0100 Subject: [PATCH 001/321] Stabilize default_alloc_error_handler Closes #66741 --- .../locales/en-US/metadata.ftl | 6 ----- compiler/rustc_feature/src/accepted.rs | 2 ++ compiler/rustc_feature/src/active.rs | 2 -- compiler/rustc_metadata/src/creader.rs | 11 +++------ compiler/rustc_metadata/src/errors.rs | 8 ------- .../no_std-alloc-error-handler-default.rs | 2 -- .../ui/missing/missing-alloc_error_handler.rs | 23 ------------------- .../missing-alloc_error_handler.stderr | 6 ----- 8 files changed, 5 insertions(+), 55 deletions(-) delete mode 100644 src/test/ui/missing/missing-alloc_error_handler.rs delete mode 100644 src/test/ui/missing/missing-alloc_error_handler.stderr diff --git a/compiler/rustc_error_messages/locales/en-US/metadata.ftl b/compiler/rustc_error_messages/locales/en-US/metadata.ftl index c292ae9b32ab..eb5549d86720 100644 --- a/compiler/rustc_error_messages/locales/en-US/metadata.ftl +++ b/compiler/rustc_error_messages/locales/en-US/metadata.ftl @@ -166,12 +166,6 @@ metadata_conflicting_alloc_error_handler = metadata_global_alloc_required = no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait -metadata_alloc_func_required = - `#[alloc_error_handler]` function required, but not found - -metadata_missing_alloc_error_handler = - use `#![feature(default_alloc_error_handler)]` for a default error handler - metadata_no_transitive_needs_dep = the crate `{$crate_name}` cannot depend on a crate that needs {$needs_crate_name}, but it depends on `{$deps_crate_name}` diff --git a/compiler/rustc_feature/src/accepted.rs b/compiler/rustc_feature/src/accepted.rs index db289a64046a..b81c30fd880a 100644 --- a/compiler/rustc_feature/src/accepted.rs +++ b/compiler/rustc_feature/src/accepted.rs @@ -126,6 +126,8 @@ declare_features! ( (accepted, copy_closures, "1.26.0", Some(44490), None), /// Allows `crate` in paths. (accepted, crate_in_paths, "1.30.0", Some(45477), None), + /// Allows rustc to inject a default alloc_error_handler + (accepted, default_alloc_error_handler, "CURRENT_RUSTC_VERSION", Some(66741), None), /// Allows using assigning a default type to type parameters in algebraic data type definitions. (accepted, default_type_params, "1.0.0", None, None), /// Allows `#[deprecated]` attribute. diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 7900f1500489..9c6da830ce9a 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -366,8 +366,6 @@ declare_features! ( (active, debugger_visualizer, "1.62.0", Some(95939), None), /// Allows declarative macros 2.0 (`macro`). (active, decl_macro, "1.17.0", Some(39412), None), - /// Allows rustc to inject a default alloc_error_handler - (active, default_alloc_error_handler, "1.48.0", Some(66741), None), /// Allows default type parameters to influence type inference. (active, default_type_parameter_fallback, "1.3.0", Some(27336), None), /// Allows using `#[deprecated_safe]` to deprecate the safeness of a function or trait diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index d4c457975a84..d5fb98b998fa 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -1,10 +1,9 @@ //! Validates all used crates and extern libraries and loads their metadata use crate::errors::{ - AllocFuncRequired, ConflictingAllocErrorHandler, ConflictingGlobalAlloc, CrateNotPanicRuntime, - GlobalAllocRequired, MissingAllocErrorHandler, NoMultipleAllocErrorHandler, - NoMultipleGlobalAlloc, NoPanicStrategy, NoTransitiveNeedsDep, NotProfilerRuntime, - ProfilerBuiltinsNeedsCore, + ConflictingAllocErrorHandler, ConflictingGlobalAlloc, CrateNotPanicRuntime, + GlobalAllocRequired, NoMultipleAllocErrorHandler, NoMultipleGlobalAlloc, NoPanicStrategy, + NoTransitiveNeedsDep, NotProfilerRuntime, ProfilerBuiltinsNeedsCore, }; use crate::locator::{CrateError, CrateLocator, CratePaths}; use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob}; @@ -892,10 +891,6 @@ impl<'a> CrateLoader<'a> { } else { // The alloc crate provides a default allocation error handler if // one isn't specified. - if !self.sess.features_untracked().default_alloc_error_handler { - self.sess.emit_err(AllocFuncRequired); - self.sess.emit_note(MissingAllocErrorHandler); - } self.cstore.alloc_error_handler_kind = Some(AllocatorKind::Default); } } diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index e5b91d566e52..3f0869c19ad4 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -371,14 +371,6 @@ pub struct ConflictingAllocErrorHandler { #[diag(metadata_global_alloc_required)] pub struct GlobalAllocRequired; -#[derive(Diagnostic)] -#[diag(metadata_alloc_func_required)] -pub struct AllocFuncRequired; - -#[derive(Diagnostic)] -#[diag(metadata_missing_alloc_error_handler)] -pub struct MissingAllocErrorHandler; - #[derive(Diagnostic)] #[diag(metadata_no_transitive_needs_dep)] pub struct NoTransitiveNeedsDep<'a> { diff --git a/src/test/ui/allocator/no_std-alloc-error-handler-default.rs b/src/test/ui/allocator/no_std-alloc-error-handler-default.rs index 30ce0f162c7a..601fda87b910 100644 --- a/src/test/ui/allocator/no_std-alloc-error-handler-default.rs +++ b/src/test/ui/allocator/no_std-alloc-error-handler-default.rs @@ -6,10 +6,8 @@ // only-linux // compile-flags:-C panic=abort // aux-build:helper.rs -// gate-test-default_alloc_error_handler #![feature(start, rustc_private, new_uninit, panic_info_message, lang_items)] -#![feature(default_alloc_error_handler)] #![no_std] extern crate alloc; diff --git a/src/test/ui/missing/missing-alloc_error_handler.rs b/src/test/ui/missing/missing-alloc_error_handler.rs deleted file mode 100644 index 4d378f010ed4..000000000000 --- a/src/test/ui/missing/missing-alloc_error_handler.rs +++ /dev/null @@ -1,23 +0,0 @@ -// compile-flags: -C panic=abort -// no-prefer-dynamic - -#![no_std] -#![crate_type = "staticlib"] -#![feature(alloc_error_handler)] - -#[panic_handler] -fn panic(_: &core::panic::PanicInfo) -> ! { - loop {} -} - -extern crate alloc; - -#[global_allocator] -static A: MyAlloc = MyAlloc; - -struct MyAlloc; - -unsafe impl core::alloc::GlobalAlloc for MyAlloc { - unsafe fn alloc(&self, _: core::alloc::Layout) -> *mut u8 { 0 as _ } - unsafe fn dealloc(&self, _: *mut u8, _: core::alloc::Layout) {} -} diff --git a/src/test/ui/missing/missing-alloc_error_handler.stderr b/src/test/ui/missing/missing-alloc_error_handler.stderr deleted file mode 100644 index 995fa7cf85e9..000000000000 --- a/src/test/ui/missing/missing-alloc_error_handler.stderr +++ /dev/null @@ -1,6 +0,0 @@ -error: `#[alloc_error_handler]` function required, but not found - -note: use `#![feature(default_alloc_error_handler)]` for a default error handler - -error: aborting due to previous error - From aa66212e291cf6d726d58d139b7ab19631431bd5 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Mon, 21 Nov 2022 22:55:14 +0100 Subject: [PATCH 002/321] Cleanup `rustc_tool_util` and add a convenient macro for `build.rs` --- Cargo.toml | 4 ++-- build.rs | 14 +------------- rustc_tools_util/README.md | 35 ++++++++++++++--------------------- rustc_tools_util/src/lib.rs | 36 ++++++++++++++++++++++++++++-------- src/driver.rs | 1 - src/main.rs | 1 - tests/versioncheck.rs | 1 - 7 files changed, 45 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6bdac84ada00..698ff035a861 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ path = "src/driver.rs" [dependencies] clippy_lints = { path = "clippy_lints" } semver = "1.0" -rustc_tools_util = "0.2.1" +rustc_tools_util = {version = "0.2.1", path = "./rustc_tools_util"} tempfile = { version = "3.2", optional = true } termize = "0.1" @@ -56,7 +56,7 @@ tokio = { version = "1", features = ["io-util"] } rustc-semver = "1.1" [build-dependencies] -rustc_tools_util = "0.2.1" +rustc_tools_util = {version = "0.2.1", path = "./rustc_tools_util"} [features] deny-warnings = ["clippy_lints/deny-warnings"] diff --git a/build.rs b/build.rs index b5484bec3c8b..b79d09b0dd2d 100644 --- a/build.rs +++ b/build.rs @@ -3,17 +3,5 @@ fn main() { println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap()); // Don't rebuild even if nothing changed println!("cargo:rerun-if-changed=build.rs"); - // forward git repo hashes we build at - println!( - "cargo:rustc-env=GIT_HASH={}", - rustc_tools_util::get_commit_hash().unwrap_or_default() - ); - println!( - "cargo:rustc-env=COMMIT_DATE={}", - rustc_tools_util::get_commit_date().unwrap_or_default() - ); - println!( - "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", - rustc_tools_util::get_channel() - ); + rustc_tools_util::setup_version_info!(); } diff --git a/rustc_tools_util/README.md b/rustc_tools_util/README.md index e947f9c7e66e..6204ca174f35 100644 --- a/rustc_tools_util/README.md +++ b/rustc_tools_util/README.md @@ -20,36 +20,29 @@ rustc_tools_util = "0.2.1" ```` In `build.rs`, generate the data in your `main()` -````rust -fn main() { - println!( - "cargo:rustc-env=GIT_HASH={}", - rustc_tools_util::get_commit_hash().unwrap_or_default() - ); - println!( - "cargo:rustc-env=COMMIT_DATE={}", - rustc_tools_util::get_commit_date().unwrap_or_default() - ); - println!( - "cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", - rustc_tools_util::get_channel().unwrap_or_default() - ); -} -```` +```rust +fn main() { + rustc_tools_util::setup_version_info!(); +} +``` Use the version information in your main.rs -````rust -use rustc_tools_util::*; +```rust fn show_version() { let version_info = rustc_tools_util::get_version_info!(); println!("{}", version_info); } -```` -This gives the following output in clippy: -`clippy 0.0.212 (a416c5e 2018-12-14)` +``` +This gives the following output in clippy: +`clippy 0.1.66 (a28f3c8 2022-11-20)` + +## Repository + +This project is part of the rust-lang/rust-clippy repository. The source code +can be found under `./rustc_tools_util/`. ## License diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs index 01d25c53126f..5e856319c886 100644 --- a/rustc_tools_util/src/lib.rs +++ b/rustc_tools_util/src/lib.rs @@ -2,19 +2,21 @@ use std::env; +/// This macro creates the version string during compilation from the +/// current environment #[macro_export] macro_rules! get_version_info { () => {{ - let major = env!("CARGO_PKG_VERSION_MAJOR").parse::().unwrap(); - let minor = env!("CARGO_PKG_VERSION_MINOR").parse::().unwrap(); - let patch = env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap(); - let crate_name = String::from(env!("CARGO_PKG_NAME")); + let major = std::env!("CARGO_PKG_VERSION_MAJOR").parse::().unwrap(); + let minor = std::env!("CARGO_PKG_VERSION_MINOR").parse::().unwrap(); + let patch = std::env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap(); + let crate_name = String::from(std::env!("CARGO_PKG_NAME")); - let host_compiler = option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string); - let commit_hash = option_env!("GIT_HASH").map(str::to_string); - let commit_date = option_env!("COMMIT_DATE").map(str::to_string); + let host_compiler = std::option_env!("RUSTC_RELEASE_CHANNEL").map(str::to_string); + let commit_hash = std::option_env!("GIT_HASH").map(str::to_string); + let commit_date = std::option_env!("COMMIT_DATE").map(str::to_string); - VersionInfo { + $crate::VersionInfo { major, minor, patch, @@ -26,6 +28,24 @@ macro_rules! get_version_info { }}; } +/// This macro can be used in `build.rs` to automatically set the needed +/// environment values, namely `GIT_HASH`, `COMMIT_DATE` and +/// `RUSTC_RELEASE_CHANNEL` +#[macro_export] +macro_rules! setup_version_info { + () => {{ + println!( + "cargo:rustc-env=GIT_HASH={}", + $crate::get_commit_hash().unwrap_or_default() + ); + println!( + "cargo:rustc-env=COMMIT_DATE={}", + $crate::get_commit_date().unwrap_or_default() + ); + println!("cargo:rustc-env=RUSTC_RELEASE_CHANNEL={}", $crate::get_channel()); + }}; +} + // some code taken and adapted from RLS and cargo pub struct VersionInfo { pub major: u8, diff --git a/src/driver.rs b/src/driver.rs index ee2a3ad20d3e..0aa7d437b4da 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -18,7 +18,6 @@ extern crate rustc_span; use rustc_interface::interface; use rustc_session::parse::ParseSess; use rustc_span::symbol::Symbol; -use rustc_tools_util::VersionInfo; use std::borrow::Cow; use std::env; diff --git a/src/main.rs b/src/main.rs index d418d2daa313..7a78b32620d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ // warn on lints, that are included in `rust-lang/rust`s bootstrap #![warn(rust_2018_idioms, unused_lifetimes)] -use rustc_tools_util::VersionInfo; use std::env; use std::path::PathBuf; use std::process::{self, Command}; diff --git a/tests/versioncheck.rs b/tests/versioncheck.rs index 7a85386a3df4..c721e9969c9a 100644 --- a/tests/versioncheck.rs +++ b/tests/versioncheck.rs @@ -2,7 +2,6 @@ #![warn(rust_2018_idioms, unused_lifetimes)] #![allow(clippy::single_match_else)] -use rustc_tools_util::VersionInfo; use std::fs; #[test] From ac31d52324f3a0c41b5daabd842e36448fd95aae Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 2 Nov 2022 13:37:37 +0100 Subject: [PATCH 003/321] implement PlaceBuilder::try_ty --- Cargo.lock | 52 ++++++----------- .../src/build/expr/as_place.rs | 58 +++++++++++++++++++ 2 files changed, 75 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c987bf44ec00..12e075a08c25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -285,7 +285,7 @@ dependencies = [ [[package]] name = "cargo" -version = "0.68.0" +version = "0.67.0" dependencies = [ "anyhow", "atty", @@ -307,7 +307,6 @@ dependencies = [ "glob", "hex 0.4.2", "home", - "http-auth", "humantime 2.0.1", "ignore", "im-rc", @@ -350,11 +349,11 @@ dependencies = [ [[package]] name = "cargo-credential" -version = "0.2.0" +version = "0.1.0" [[package]] name = "cargo-credential-1password" -version = "0.2.0" +version = "0.1.0" dependencies = [ "cargo-credential", "serde", @@ -363,7 +362,7 @@ dependencies = [ [[package]] name = "cargo-credential-macos-keychain" -version = "0.2.0" +version = "0.1.0" dependencies = [ "cargo-credential", "security-framework", @@ -371,7 +370,7 @@ dependencies = [ [[package]] name = "cargo-credential-wincred" -version = "0.2.0" +version = "0.1.0" dependencies = [ "cargo-credential", "winapi", @@ -425,6 +424,7 @@ dependencies = [ "glob", "itertools", "lazy_static", + "remove_dir_all", "serde_json", "snapbox", "tar", @@ -436,7 +436,7 @@ dependencies = [ [[package]] name = "cargo-util" -version = "0.2.3" +version = "0.2.2" dependencies = [ "anyhow", "core-foundation", @@ -446,7 +446,7 @@ dependencies = [ "jobserver", "libc", "log", - "miow 0.5.0", + "miow", "same-file", "shell-escape", "tempfile", @@ -808,7 +808,7 @@ dependencies = [ "lazy_static", "lazycell", "libc", - "miow 0.3.7", + "miow", "miropt-test-tools", "regex", "rustfix", @@ -833,7 +833,7 @@ dependencies = [ "lazy_static", "libc", "log", - "miow 0.3.7", + "miow", "regex", "rustfix", "serde", @@ -846,9 +846,9 @@ dependencies = [ [[package]] name = "concolor" -version = "0.0.9" +version = "0.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b90f9dcd9490a97db91a85ccd79e38a87e14323f0bb824659ee3274e9143ba37" +checksum = "015267563b1df20adccdd00cb05257b1dfbea70a04928e9cf88ffb850c1a40af" dependencies = [ "atty", "bitflags", @@ -857,9 +857,9 @@ dependencies = [ [[package]] name = "concolor-query" -version = "0.1.0" +version = "0.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82a90734b3d5dcf656e7624cca6bce9c3a90ee11f900e80141a7427ccfb3d317" +checksum = "d6417fe6fc03a8b533fd2177742eeb39a90c7233eedec7bac96d4d6b69a09449" [[package]] name = "content_inspector" @@ -909,7 +909,7 @@ dependencies = [ [[package]] name = "crates-io" -version = "0.35.0" +version = "0.34.0" dependencies = [ "anyhow", "curl", @@ -1698,15 +1698,6 @@ dependencies = [ "syn", ] -[[package]] -name = "http-auth" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0b40b39d66c28829a0cf4d09f7e139ff8201f7500a5083732848ed3b4b4d850" -dependencies = [ - "memchr", -] - [[package]] name = "humantime" version = "1.3.0" @@ -2309,15 +2300,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "miow" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ffbca2f655e33c08be35d87278e5b18b89550a37dbd598c20db92f6a471123" -dependencies = [ - "windows-sys", -] - [[package]] name = "miri" version = "0.1.0" @@ -4711,9 +4693,9 @@ checksum = "da73c8f77aebc0e40c300b93f0a5f1bece7a248a36eee287d4e095f35c7b7d6e" [[package]] name = "snapbox" -version = "0.4.1" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "827c00e91b15e2674d8a5270bae91f898693cbf9561cbb58d8eaa31974597293" +checksum = "44d199ccf8f606592df2d145db26f2aa45344e23c64b074cc5a4047f1d99b0f7" dependencies = [ "concolor", "content_inspector", diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index edd527286264..b6f2e3de6682 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -323,6 +323,64 @@ impl<'tcx> PlaceBuilder<'tcx> { projection: Vec::from_iter(self.projection.iter().copied().chain([elem])), } } + + pub fn try_ty(&self, local_decls: &D, cx: &Builder<'_, 'tcx>) -> Option> + where + D: HasLocalDecls<'tcx>, + { + let tcx = cx.tcx; + + let project_ty = |ty: Ty<'tcx>, elem: &PlaceElem<'tcx>| -> Ty<'tcx> { + match elem { + ProjectionElem::Deref => { + ty.builtin_deref(true) + .unwrap_or_else(|| { + bug!("deref projection of non-dereferenceable ty {:?}", ty) + }) + .ty + } + ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => { + ty.builtin_index().unwrap() + } + ProjectionElem::Subslice { from, to, from_end } => match ty.kind() { + ty::Slice(..) => ty, + ty::Array(inner, _) if !from_end => tcx.mk_array(*inner, (to - from) as u64), + ty::Array(inner, size) if *from_end => { + let size = size.eval_usize(tcx, ty::ParamEnv::empty()); + let len = size - (*from as u64) - (*to as u64); + tcx.mk_array(*inner, len) + } + _ => bug!("cannot subslice non-array type: `{:?}`", ty), + }, + ProjectionElem::Downcast(..) => ty, + ProjectionElem::Field(_, ty) | ProjectionElem::OpaqueCast(ty) => *ty, + } + }; + + match self.base { + PlaceBase::Local(local) => { + let base_ty = local_decls.local_decls()[local].ty; + Some(self.projection.iter().fold(base_ty, |ty, &elem| project_ty(ty, &elem))) + } + PlaceBase::Upvar { .. } => { + match to_upvars_resolved_place_builder(self.clone(), cx) { + Ok(resolved_place_builder) => { + // `base` is guaranteed to be `PlaceBase::Local` now, so recursive call is ok + resolved_place_builder.try_ty(local_decls, cx) + } + Err(place_builder) => { + match &place_builder.projection[..] { + &[ProjectionElem::OpaqueCast(base_ty), ref projections @ ..] => Some( + projections.iter().fold(base_ty, |ty, &elem| project_ty(ty, &elem)), + ), + + _ => None, // would need a base `Ty` for these + } + } + } + } + } + } } impl<'tcx> From for PlaceBuilder<'tcx> { From 4d3a91b12b8c9e1b89a19c3663f430e58c16acd3 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 2 Nov 2022 13:39:47 +0100 Subject: [PATCH 004/321] use non-ascribed type as field type in mir --- .../src/build/matches/simplify.rs | 4 +- .../rustc_mir_build/src/build/matches/util.rs | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/build/matches/simplify.rs b/compiler/rustc_mir_build/src/build/matches/simplify.rs index f6b1955fdec4..fb9f19b7b906 100644 --- a/compiler/rustc_mir_build/src/build/matches/simplify.rs +++ b/compiler/rustc_mir_build/src/build/matches/simplify.rs @@ -294,7 +294,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { PatKind::Leaf { ref subpatterns } => { // tuple struct, match subpats (if any) - candidate.match_pairs.extend(self.field_match_pairs(match_pair.place, subpatterns)); + candidate + .match_pairs + .extend(self.field_match_pairs_tuple_struct(match_pair.place, subpatterns)); Ok(()) } diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index bd435f9ab009..6a07b07ee127 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -25,6 +25,51 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .collect() } + #[instrument(skip(self), level = "debug")] + pub(crate) fn field_match_pairs_tuple_struct<'pat>( + &mut self, + place_builder: PlaceBuilder<'tcx>, + subpatterns: &'pat [FieldPat<'tcx>], + ) -> Vec> { + let place_ty = place_builder + .try_ty(&self.local_decls, self) + .map(|ty| self.tcx.normalize_erasing_regions(self.param_env, ty)); + debug!(?place_ty); + + subpatterns + .iter() + .map(|fieldpat| { + // NOTE: With type ascriptions it can happen that we get errors + // during borrow-checking on higher-ranked types if we use the + // ascribed type as the field type, so we try to get the actual field + // type from the `Place`, if possible, see issue #96514 + let field_ty = if let Some(place_ty) = place_ty { + let field_idx = fieldpat.field.as_usize(); + let field_ty = match place_ty.kind() { + ty::Adt(adt_def, substs) => { + adt_def.all_fields().collect::>()[field_idx].ty(self.tcx, substs) + } + ty::Tuple(elems) => elems.to_vec()[field_idx], + _ => bug!( + "no field available, place_ty: {:#?}, kind: {:?}", + place_ty, + place_ty.kind() + ), + }; + + self.tcx.normalize_erasing_regions(self.param_env, field_ty) + } else { + fieldpat.pattern.ty + }; + + let place = place_builder.clone().field(fieldpat.field, field_ty); + debug!(?place, ?field_ty); + + MatchPair::new(place, &fieldpat.pattern, self) + }) + .collect() + } + pub(crate) fn prefix_slice_suffix<'pat>( &mut self, match_pairs: &mut SmallVec<[MatchPair<'pat, 'tcx>; 1]>, From 906c52743aa60002abadae2a9f19673bdad3cf80 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 2 Nov 2022 13:49:42 +0100 Subject: [PATCH 005/321] add tests --- src/test/ui/mir/field-ty-ascription.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/ui/mir/field-ty-ascription.rs diff --git a/src/test/ui/mir/field-ty-ascription.rs b/src/test/ui/mir/field-ty-ascription.rs new file mode 100644 index 000000000000..4147f05776a3 --- /dev/null +++ b/src/test/ui/mir/field-ty-ascription.rs @@ -0,0 +1,21 @@ +// build-pass + +struct Foo(T); // `T` is covariant. + +fn foo<'b>(x: Foo fn(&'a ())>) { + let Foo(y): Foo = x; +} + +fn foo_nested<'b>(x: Foo fn(&'a ())>>) { + let Foo(Foo(y)): Foo> = x; +} + +fn tuple<'b>(x: (u32, for<'a> fn(&'a ()))) { + let (_, y): (u32, fn(&'b ())) = x; +} + +fn tuple_nested<'b>(x: (u32, (u32, for<'a> fn(&'a ())))) { + let (_, (_, y)): (u32, (u32, fn(&'b ()))) = x; +} + +fn main() {} From f015842ed2094340ac1cce2cc4b559eca98e3371 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 2 Nov 2022 22:02:39 +0100 Subject: [PATCH 006/321] properly handle enum field projections --- .../src/build/expr/as_place.rs | 56 ++++++------------- .../src/build/matches/simplify.rs | 10 ++-- .../rustc_mir_build/src/build/matches/util.rs | 18 ++++-- 3 files changed, 35 insertions(+), 49 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index b6f2e3de6682..b682b0c3bd47 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -7,6 +7,7 @@ use rustc_hir::def_id::LocalDefId; use rustc_middle::hir::place::Projection as HirProjection; use rustc_middle::hir::place::ProjectionKind as HirProjectionKind; use rustc_middle::middle::region; +use rustc_middle::mir::tcx::PlaceTy; use rustc_middle::mir::AssertKind::BoundsCheck; use rustc_middle::mir::*; use rustc_middle::thir::*; @@ -324,56 +325,35 @@ impl<'tcx> PlaceBuilder<'tcx> { } } - pub fn try_ty(&self, local_decls: &D, cx: &Builder<'_, 'tcx>) -> Option> + pub fn try_compute_ty( + &self, + local_decls: &D, + cx: &Builder<'_, 'tcx>, + ) -> Option> where D: HasLocalDecls<'tcx>, { - let tcx = cx.tcx; - - let project_ty = |ty: Ty<'tcx>, elem: &PlaceElem<'tcx>| -> Ty<'tcx> { - match elem { - ProjectionElem::Deref => { - ty.builtin_deref(true) - .unwrap_or_else(|| { - bug!("deref projection of non-dereferenceable ty {:?}", ty) - }) - .ty - } - ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => { - ty.builtin_index().unwrap() - } - ProjectionElem::Subslice { from, to, from_end } => match ty.kind() { - ty::Slice(..) => ty, - ty::Array(inner, _) if !from_end => tcx.mk_array(*inner, (to - from) as u64), - ty::Array(inner, size) if *from_end => { - let size = size.eval_usize(tcx, ty::ParamEnv::empty()); - let len = size - (*from as u64) - (*to as u64); - tcx.mk_array(*inner, len) - } - _ => bug!("cannot subslice non-array type: `{:?}`", ty), - }, - ProjectionElem::Downcast(..) => ty, - ProjectionElem::Field(_, ty) | ProjectionElem::OpaqueCast(ty) => *ty, - } - }; - match self.base { - PlaceBase::Local(local) => { - let base_ty = local_decls.local_decls()[local].ty; - Some(self.projection.iter().fold(base_ty, |ty, &elem| project_ty(ty, &elem))) - } + PlaceBase::Local(_) => Some(self.clone().into_place(cx).ty(local_decls, cx.tcx)), PlaceBase::Upvar { .. } => { match to_upvars_resolved_place_builder(self.clone(), cx) { Ok(resolved_place_builder) => { // `base` is guaranteed to be `PlaceBase::Local` now, so recursive call is ok - resolved_place_builder.try_ty(local_decls, cx) + resolved_place_builder.try_compute_ty(local_decls, cx) } Err(place_builder) => { match &place_builder.projection[..] { - &[ProjectionElem::OpaqueCast(base_ty), ref projections @ ..] => Some( - projections.iter().fold(base_ty, |ty, &elem| project_ty(ty, &elem)), - ), + &[ProjectionElem::OpaqueCast(base_ty), ref projections @ ..] => { + let place_ty = projections + .iter() + .fold(PlaceTy::from_ty(base_ty), |place_ty, &elem| { + place_ty.projection_ty(cx.tcx, elem) + }); + debug!(?place_ty); + + Some(place_ty) + } _ => None, // would need a base `Ty` for these } } diff --git a/compiler/rustc_mir_build/src/build/matches/simplify.rs b/compiler/rustc_mir_build/src/build/matches/simplify.rs index fb9f19b7b906..36aa7693e827 100644 --- a/compiler/rustc_mir_build/src/build/matches/simplify.rs +++ b/compiler/rustc_mir_build/src/build/matches/simplify.rs @@ -272,9 +272,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { || !adt_def.is_variant_list_non_exhaustive()); if irrefutable { let place_builder = match_pair.place.downcast(adt_def, variant_index); - candidate - .match_pairs - .extend(self.field_match_pairs(place_builder, subpatterns)); + let field_match_pairs = + self.field_match_pairs(place_builder.clone(), subpatterns); + candidate.match_pairs.extend(field_match_pairs); Ok(()) } else { Err(match_pair) @@ -294,9 +294,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { PatKind::Leaf { ref subpatterns } => { // tuple struct, match subpats (if any) - candidate - .match_pairs - .extend(self.field_match_pairs_tuple_struct(match_pair.place, subpatterns)); + candidate.match_pairs.extend(self.field_match_pairs(match_pair.place, subpatterns)); Ok(()) } diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index 6a07b07ee127..7423b5e1ae3e 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -31,10 +31,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { place_builder: PlaceBuilder<'tcx>, subpatterns: &'pat [FieldPat<'tcx>], ) -> Vec> { - let place_ty = place_builder - .try_ty(&self.local_decls, self) - .map(|ty| self.tcx.normalize_erasing_regions(self.param_env, ty)); - debug!(?place_ty); + let place_ty_and_variant_idx = + place_builder.try_compute_ty(&self.local_decls, self).map(|place_ty| { + ( + self.tcx.normalize_erasing_regions(self.param_env, place_ty.ty), + place_ty.variant_index, + ) + }); + debug!(?place_ty_and_variant_idx); subpatterns .iter() @@ -43,9 +47,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // during borrow-checking on higher-ranked types if we use the // ascribed type as the field type, so we try to get the actual field // type from the `Place`, if possible, see issue #96514 - let field_ty = if let Some(place_ty) = place_ty { + let field_ty = if let Some((place_ty, opt_variant_idx)) = place_ty_and_variant_idx { let field_idx = fieldpat.field.as_usize(); let field_ty = match place_ty.kind() { + ty::Adt(adt_def, substs) if adt_def.is_enum() => { + let variant_idx = opt_variant_idx.unwrap(); + adt_def.variant(variant_idx).fields[field_idx].ty(self.tcx, substs) + } ty::Adt(adt_def, substs) => { adt_def.all_fields().collect::>()[field_idx].ty(self.tcx, substs) } From 2ef8308687f57335e117fdfa7d92002cf6f53eda Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 2 Nov 2022 22:05:19 +0100 Subject: [PATCH 007/321] add more tests --- src/test/ui/mir/field-ty-ascription-enums.rs | 15 +++++++++++++++ src/test/ui/mir/field-ty-ascription.rs | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/test/ui/mir/field-ty-ascription-enums.rs diff --git a/src/test/ui/mir/field-ty-ascription-enums.rs b/src/test/ui/mir/field-ty-ascription-enums.rs new file mode 100644 index 000000000000..179af6170906 --- /dev/null +++ b/src/test/ui/mir/field-ty-ascription-enums.rs @@ -0,0 +1,15 @@ +// build-pass + +enum Foo { + Var(T), +} // `T` is covariant. + +fn foo<'b>(x: Foo fn(&'a ())>) { + let Foo::Var(x): Foo = x; +} + +fn foo_nested<'b>(x: Foo fn(&'a ())>>) { + let Foo::Var(Foo::Var(x)): Foo> = x; +} + +fn main() {} diff --git a/src/test/ui/mir/field-ty-ascription.rs b/src/test/ui/mir/field-ty-ascription.rs index 4147f05776a3..178c7916bc59 100644 --- a/src/test/ui/mir/field-ty-ascription.rs +++ b/src/test/ui/mir/field-ty-ascription.rs @@ -2,6 +2,22 @@ struct Foo(T); // `T` is covariant. +struct Bar { + x: T, +} // `T` is covariant. + +fn bar<'b>(x: Bar fn(&'a ())>) { + let Bar { x }: Bar = x; +} + +fn bar_nested<'b>(x: Bar fn(&'a ())>>) { + let Bar { x: Bar { x } }: Bar> = x; +} + +fn bar_foo_nested<'b>(x: Bar fn(&'a ())>>) { + let Bar { x: Foo ( x ) }: Bar> = x; +} + fn foo<'b>(x: Foo fn(&'a ())>) { let Foo(y): Foo = x; } From fd6fed3027be425c27a5a8c825575bdd7a1e63dd Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 2 Nov 2022 22:20:17 +0100 Subject: [PATCH 008/321] address review --- .../rustc_mir_build/src/build/expr/as_place.rs | 7 +++++++ .../rustc_mir_build/src/build/matches/util.rs | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index b682b0c3bd47..36f08a7a48fd 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -325,6 +325,13 @@ impl<'tcx> PlaceBuilder<'tcx> { } } + /// Similar to `Place::ty` but needed during mir building. + /// + /// Applies the projections in the `PlaceBuilder` to the base + /// type. + /// + /// Fallible as the root of this place may be an upvar for + /// which no base type can be determined. pub fn try_compute_ty( &self, local_decls: &D, diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index 7423b5e1ae3e..fb2a5be28a2e 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -54,10 +54,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let variant_idx = opt_variant_idx.unwrap(); adt_def.variant(variant_idx).fields[field_idx].ty(self.tcx, substs) } - ty::Adt(adt_def, substs) => { - adt_def.all_fields().collect::>()[field_idx].ty(self.tcx, substs) - } - ty::Tuple(elems) => elems.to_vec()[field_idx], + ty::Adt(adt_def, substs) => adt_def + .all_fields() + .nth(field_idx) + .unwrap_or_else(|| { + bug!( + "expected to take field idx {:?} of fields of {:?}", + field_idx, + adt_def + ) + }) + .ty(self.tcx, substs), + ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { + bug!("expected to take field idx {:?} of {:?}", field_idx, elems) + }), _ => bug!( "no field available, place_ty: {:#?}, kind: {:?}", place_ty, From 4040734e44d72697dbeb35c4c1be1a70cb7b0b37 Mon Sep 17 00:00:00 2001 From: b-naber Date: Mon, 21 Nov 2022 20:56:27 +0100 Subject: [PATCH 009/321] get field ty during projecting --- Cargo.lock | 52 ++-- .../src/build/expr/as_place.rs | 260 ++++++++++++------ .../src/build/expr/as_rvalue.rs | 9 +- .../rustc_mir_build/src/build/expr/into.rs | 6 +- .../rustc_mir_build/src/build/matches/test.rs | 3 +- .../rustc_mir_build/src/build/matches/util.rs | 73 +---- 6 files changed, 220 insertions(+), 183 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12e075a08c25..c987bf44ec00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -285,7 +285,7 @@ dependencies = [ [[package]] name = "cargo" -version = "0.67.0" +version = "0.68.0" dependencies = [ "anyhow", "atty", @@ -307,6 +307,7 @@ dependencies = [ "glob", "hex 0.4.2", "home", + "http-auth", "humantime 2.0.1", "ignore", "im-rc", @@ -349,11 +350,11 @@ dependencies = [ [[package]] name = "cargo-credential" -version = "0.1.0" +version = "0.2.0" [[package]] name = "cargo-credential-1password" -version = "0.1.0" +version = "0.2.0" dependencies = [ "cargo-credential", "serde", @@ -362,7 +363,7 @@ dependencies = [ [[package]] name = "cargo-credential-macos-keychain" -version = "0.1.0" +version = "0.2.0" dependencies = [ "cargo-credential", "security-framework", @@ -370,7 +371,7 @@ dependencies = [ [[package]] name = "cargo-credential-wincred" -version = "0.1.0" +version = "0.2.0" dependencies = [ "cargo-credential", "winapi", @@ -424,7 +425,6 @@ dependencies = [ "glob", "itertools", "lazy_static", - "remove_dir_all", "serde_json", "snapbox", "tar", @@ -436,7 +436,7 @@ dependencies = [ [[package]] name = "cargo-util" -version = "0.2.2" +version = "0.2.3" dependencies = [ "anyhow", "core-foundation", @@ -446,7 +446,7 @@ dependencies = [ "jobserver", "libc", "log", - "miow", + "miow 0.5.0", "same-file", "shell-escape", "tempfile", @@ -808,7 +808,7 @@ dependencies = [ "lazy_static", "lazycell", "libc", - "miow", + "miow 0.3.7", "miropt-test-tools", "regex", "rustfix", @@ -833,7 +833,7 @@ dependencies = [ "lazy_static", "libc", "log", - "miow", + "miow 0.3.7", "regex", "rustfix", "serde", @@ -846,9 +846,9 @@ dependencies = [ [[package]] name = "concolor" -version = "0.0.8" +version = "0.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "015267563b1df20adccdd00cb05257b1dfbea70a04928e9cf88ffb850c1a40af" +checksum = "b90f9dcd9490a97db91a85ccd79e38a87e14323f0bb824659ee3274e9143ba37" dependencies = [ "atty", "bitflags", @@ -857,9 +857,9 @@ dependencies = [ [[package]] name = "concolor-query" -version = "0.0.5" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6417fe6fc03a8b533fd2177742eeb39a90c7233eedec7bac96d4d6b69a09449" +checksum = "82a90734b3d5dcf656e7624cca6bce9c3a90ee11f900e80141a7427ccfb3d317" [[package]] name = "content_inspector" @@ -909,7 +909,7 @@ dependencies = [ [[package]] name = "crates-io" -version = "0.34.0" +version = "0.35.0" dependencies = [ "anyhow", "curl", @@ -1698,6 +1698,15 @@ dependencies = [ "syn", ] +[[package]] +name = "http-auth" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0b40b39d66c28829a0cf4d09f7e139ff8201f7500a5083732848ed3b4b4d850" +dependencies = [ + "memchr", +] + [[package]] name = "humantime" version = "1.3.0" @@ -2300,6 +2309,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "miow" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ffbca2f655e33c08be35d87278e5b18b89550a37dbd598c20db92f6a471123" +dependencies = [ + "windows-sys", +] + [[package]] name = "miri" version = "0.1.0" @@ -4693,9 +4711,9 @@ checksum = "da73c8f77aebc0e40c300b93f0a5f1bece7a248a36eee287d4e095f35c7b7d6e" [[package]] name = "snapbox" -version = "0.3.3" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d199ccf8f606592df2d145db26f2aa45344e23c64b074cc5a4047f1d99b0f7" +checksum = "827c00e91b15e2674d8a5270bae91f898693cbf9561cbb58d8eaa31974597293" dependencies = [ "concolor", "content_inspector", diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 36f08a7a48fd..5c9459c97f40 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -21,21 +21,28 @@ use rustc_index::vec::Idx; use std::assert_matches::assert_matches; use std::iter; -/// The "outermost" place that holds this value. -#[derive(Copy, Clone, Debug, PartialEq)] -pub(crate) enum PlaceBase { +/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a +/// place by pushing more and more projections onto the end, and then convert the final set into a +/// place using the `into_place` method. +/// +/// This is used internally when building a place for an expression like `a.b.c`. The fields `b` +/// and `c` can be progressively pushed onto the place builder that is created when converting `a`. +#[derive(Clone, Debug, PartialEq)] +pub(in crate::build) enum PlaceBuilder<'tcx> { /// Denotes the start of a `Place`. - Local(Local), + /// + /// We use `PlaceElem` since this has all `Field` types available. + Local(Local, Vec>), /// When building place for an expression within a closure, the place might start off a /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture /// index (within the desugared closure) of the captured path until most of the projections - /// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the + /// are applied. We use `PlaceBuilder::Upvar` to keep track of the root variable off of which the /// captured path starts, the closure the capture belongs to and the trait the closure /// implements. /// - /// Once we have figured out the capture index, we can convert the place builder to start from - /// `PlaceBase::Local`. + /// Once we have figured out the capture index, we can convert the place builder to + /// `PlaceBuilder::Local`. /// /// Consider the following example /// ```rust @@ -56,24 +63,16 @@ pub(crate) enum PlaceBase { /// /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to /// figure out that it is captured until all the `Field` projections are applied. - Upvar { - /// HirId of the upvar - var_hir_id: LocalVarId, - /// DefId of the closure - closure_def_id: LocalDefId, - }, + /// + /// Note: in contrast to `PlaceBuilder::Local` we have not yet determined all `Field` types + /// and will only do so once converting to `PlaceBuilder::Local`. + UpVar(UpVar, Vec>), } -/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a -/// place by pushing more and more projections onto the end, and then convert the final set into a -/// place using the `to_place` method. -/// -/// This is used internally when building a place for an expression like `a.b.c`. The fields `b` -/// and `c` can be progressively pushed onto the place builder that is created when converting `a`. -#[derive(Clone, Debug, PartialEq)] -pub(in crate::build) struct PlaceBuilder<'tcx> { - base: PlaceBase, - projection: Vec>, +#[derive(Copy, Clone, Debug, PartialEq)] +pub(crate) struct UpVar { + var_hir_id: LocalVarId, + closure_def_id: LocalDefId, } /// Given a list of MIR projections, convert them to list of HIR ProjectionKind. @@ -175,7 +174,7 @@ fn to_upvars_resolved_place_builder<'tcx>( cx: &Builder<'_, 'tcx>, var_hir_id: LocalVarId, closure_def_id: LocalDefId, - projection: &[PlaceElem<'tcx>], + projection: &[UpvarProjectionElem<'tcx>], ) -> Option> { let Some((capture_index, capture)) = find_capture_matching_projections( @@ -197,23 +196,31 @@ fn to_upvars_resolved_place_builder<'tcx>( var_hir_id, projection, ); } + return None; }; // Access the capture by accessing the field within the Closure struct. let capture_info = &cx.upvars[capture_index]; - let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place); + let Place { local: upvar_resolved_local, projection: local_projection } = + capture_info.use_place; // We used some of the projections to build the capture itself, // now we apply the remaining to the upvar resolved place. - trace!(?capture.captured_place, ?projection); - let remaining_projections = strip_prefix( + let upvar_projection = strip_prefix( capture.captured_place.place.base_ty, projection, &capture.captured_place.place.projections, + ) + .collect::>(); + + let upvar_resolved_place_builder = PlaceBuilder::construct_local_place_builder( + cx, + upvar_resolved_local, + local_projection.to_vec(), + upvar_projection, ); - upvar_resolved_place_builder.projection.extend(remaining_projections); Some(upvar_resolved_place_builder) } @@ -235,6 +242,8 @@ fn strip_prefix<'a, 'tcx>( // Filter out opaque casts, they are unnecessary in the prefix. .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))); for projection in prefix_projections { + debug!(?projection, ?projection.ty); + match projection.kind { HirProjectionKind::Deref => { assert_matches!(iter.next(), Some(ProjectionElem::Deref)); @@ -249,6 +258,7 @@ fn strip_prefix<'a, 'tcx>( bug!("unexpected projection kind: {:?}", projection); } } + base_ty = projection.ty; } iter @@ -263,9 +273,9 @@ impl<'tcx> PlaceBuilder<'tcx> { pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option> { let resolved = self.resolve_upvar(cx); let builder = resolved.as_ref().unwrap_or(self); - let PlaceBase::Local(local) = builder.base else { return None }; - let projection = cx.tcx.intern_place_elems(&builder.projection); - Some(Place { local, projection }) + let PlaceBuilder::Local(local, projection) = builder else { return None }; + let projection = cx.tcx.intern_place_elems(&projection); + Some(Place { local: *local, projection }) } /// Attempts to resolve the `PlaceBuilder`. @@ -282,22 +292,39 @@ impl<'tcx> PlaceBuilder<'tcx> { &self, cx: &Builder<'_, 'tcx>, ) -> Option> { - let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else { + let PlaceBuilder::Upvar( Upvar {var_hir_id, closure_def_id }, projection) = self else { return None; }; - to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection) + + to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &projection) } - pub(crate) fn base(&self) -> PlaceBase { - self.base + pub(crate) fn get_local_projection(&self) -> &[PlaceElem<'tcx>] { + match self { + Self::Local(_, projection) => projection, + Self::UpVar(..) => { + bug!("get_local_projection_mut can only be called on PlaceBuilder::Local") + } + } } - pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] { - &self.projection - } + #[instrument(skip(cx), level = "debug")] + pub(crate) fn field( + self, + cx: &Builder<'_, 'tcx>, + f: Field, + default_field_ty: Ty<'tcx>, + ) -> Self { + let field_ty = match self { + PlaceBuilder::Local(..) => { + let base_place = self.clone(); + PlaceBuilder::try_compute_field_ty(cx, f, base_place) + .unwrap_or_else(|| default_field_ty) + } + PlaceBuilder::UpVar(..) => default_field_ty, + }; - pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self { - self.project(PlaceElem::Field(f, ty)) + self.project(ProjectionElem::Field(f, field_ty)) } pub(crate) fn deref(self) -> Self { @@ -312,16 +339,34 @@ impl<'tcx> PlaceBuilder<'tcx> { self.project(PlaceElem::Index(index)) } - pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self { - self.projection.push(elem); - self + #[instrument(level = "debug")] + pub(crate) fn project(self, elem: PlaceElem<'tcx>) -> Self { + let result = match self { + PlaceBuilder::Local(local, mut proj) => { + proj.push(elem); + PlaceBuilder::Local(local, proj) + } + PlaceBuilder::UpVar(upvar, mut proj) => { + proj.push(elem); + PlaceBuilder::UpVar(upvar, proj) + } + }; + + debug!(?result); + result } /// Same as `.clone().project(..)` but more efficient pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self { - Self { - base: self.base, - projection: Vec::from_iter(self.projection.iter().copied().chain([elem])), + match self { + PlaceBuilder::Local(local, proj) => PlaceBuilder::Local( + *local, + Vec::from_iter(proj.iter().copied().chain([elem.into()])), + ), + PlaceBuilder::Upvar(upvar, proj) => PlaceBuilder::UpVar( + upvar, + Vec::from_iter(proj.iter().copied().chain([elem.into()])), + ), } } @@ -332,59 +377,96 @@ impl<'tcx> PlaceBuilder<'tcx> { /// /// Fallible as the root of this place may be an upvar for /// which no base type can be determined. - pub fn try_compute_ty( - &self, - local_decls: &D, + #[instrument(skip(cx), level = "debug")] + fn try_compute_field_ty( cx: &Builder<'_, 'tcx>, - ) -> Option> - where - D: HasLocalDecls<'tcx>, - { - match self.base { - PlaceBase::Local(_) => Some(self.clone().into_place(cx).ty(local_decls, cx.tcx)), - PlaceBase::Upvar { .. } => { - match to_upvars_resolved_place_builder(self.clone(), cx) { - Ok(resolved_place_builder) => { - // `base` is guaranteed to be `PlaceBase::Local` now, so recursive call is ok - resolved_place_builder.try_compute_ty(local_decls, cx) - } - Err(place_builder) => { - match &place_builder.projection[..] { - &[ProjectionElem::OpaqueCast(base_ty), ref projections @ ..] => { - let place_ty = projections - .iter() - .fold(PlaceTy::from_ty(base_ty), |place_ty, &elem| { - place_ty.projection_ty(cx.tcx, elem) - }); + field: Field, + base_place: PlaceBuilder<'tcx>, + ) -> Option> { + let field_idx = field.as_usize(); + let PlaceTy { ty, variant_index } = base_place.to_place(cx).ty(&cx.local_decls, cx.tcx); + let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty); + debug!(?base_ty); - debug!(?place_ty); + let field_ty = match base_ty.kind() { + ty::Adt(adt_def, substs) if adt_def.is_enum() => { + let variant_idx = variant_index.unwrap(); + adt_def.variant(variant_idx).fields[field_idx].ty(cx.tcx, substs) + } + ty::Adt(adt_def, substs) => adt_def + .all_fields() + .nth(field_idx) + .unwrap_or_else(|| { + bug!("expected to take field idx {:?} of fields of {:?}", field_idx, adt_def) + }) + .ty(cx.tcx, substs), + ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { + bug!("expected to take field idx {:?} of {:?}", field_idx, elems) + }), + _ => return None, + }; - Some(place_ty) - } - _ => None, // would need a base `Ty` for these - } - } + Some(cx.tcx.normalize_erasing_regions(cx.param_env, field_ty)) + } + + /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::UpVar` whose upvars + /// are resolved. This function takes two kinds of projections: `local_projection` + /// contains the projections of the captured upvar and `upvar_projection` the + /// projections that are applied to the captured upvar. The main purpose of this + /// function is to figure out the `Ty`s of the field projections in `upvar_projection`. + #[instrument(skip(cx, local))] + fn construct_local_place_builder( + cx: &Builder<'_, 'tcx>, + local: Local, + mut local_projection: Vec>, + upvar_projection: Vec>, + ) -> Self { + // We iterate through `upvar_projection` and whenever we find a `ProjectionElem::Field` we use + // the ancestor projections, i.e. those projection elements that come before the field projection, + // to get the `Ty` for the field. + + for proj in upvar_projection.iter() { + debug!("proj: {:?}, local_projection: {:?}", proj, local_projection); + match *proj { + ProjectionElem::Field(field, default_field_ty) => { + let ancestor_proj = local_projection.to_vec(); + let base_place = PlaceBuilder::Local(local, ancestor_proj); + let field_ty = PlaceBuilder::try_compute_field_ty(cx, field, base_place) + .unwrap_or_else(|| default_field_ty); + debug!(?field_ty); + + local_projection.push(ProjectionElem::Field(field, field_ty)); + debug!(?local_projection); + } + ProjectionElem::Deref => local_projection.push(ProjectionElem::Deref), + ProjectionElem::Index(idx) => local_projection.push(ProjectionElem::Index(idx)), + ProjectionElem::ConstantIndex { offset, min_length, from_end } => local_projection + .push(ProjectionElem::ConstantIndex { offset, min_length, from_end }), + ProjectionElem::Subslice { from, to, from_end } => { + local_projection.push(ProjectionElem::Subslice { from, to, from_end }) + } + ProjectionElem::Downcast(sym, variant_idx) => { + local_projection.push(ProjectionElem::Downcast(sym, variant_idx)) + } + ProjectionElem::OpaqueCast(ty) => { + local_projection.push(ProjectionElem::OpaqueCast(ty)) } } } + + PlaceBuilder::Local(local, local_projection) } } impl<'tcx> From for PlaceBuilder<'tcx> { fn from(local: Local) -> Self { - Self { base: PlaceBase::Local(local), projection: Vec::new() } - } -} - -impl<'tcx> From for PlaceBuilder<'tcx> { - fn from(base: PlaceBase) -> Self { - Self { base, projection: Vec::new() } + Self::Local(local, Vec::new()) } } impl<'tcx> From> for PlaceBuilder<'tcx> { fn from(p: Place<'tcx>) -> Self { - Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() } + Self::Local(p.local, p.projection.to_vec()) } } @@ -448,6 +530,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { self.expr_as_place(block, expr, Mutability::Not, None) } + #[instrument(skip(self, fake_borrow_temps), level = "debug")] fn expr_as_place( &mut self, mut block: BasicBlock, @@ -455,8 +538,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { mutability: Mutability, fake_borrow_temps: Option<&mut Vec>, ) -> BlockAnd> { - debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability); - let this = self; let expr_span = expr.span; let source_info = this.source_info(expr_span); @@ -470,12 +551,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let lhs = &this.thir[lhs]; let mut place_builder = unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,)); + debug!(?place_builder); if let ty::Adt(adt_def, _) = lhs.ty.kind() { if adt_def.is_enum() { place_builder = place_builder.downcast(*adt_def, variant_index); } } - block.and(place_builder.field(name, expr.ty)) + block.and(place_builder.field(this, name, expr.ty)) } ExprKind::Deref { arg } => { let place_builder = unpack!( @@ -617,7 +699,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } /// Lower a captured upvar. Note we might not know the actual capture index, - /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved + /// so we create a place starting from `UpVar`, which will be resolved /// once all projections that allow us to identify a capture have been applied. fn lower_captured_upvar( &mut self, @@ -625,7 +707,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { closure_def_id: LocalDefId, var_hir_id: LocalVarId, ) -> BlockAnd> { - block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id })) + block.and(PlaceBuilder::UpVar(UpVar { var_hir_id, closure_def_id }, vec![])) } /// Lower an index expression @@ -716,8 +798,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { source_info: SourceInfo, ) { let tcx = self.tcx; - let place_ty = base_place.ty(&self.local_decls, tcx); + if let ty::Slice(_) = place_ty.ty.kind() { // We need to create fake borrows to ensure that the bounds // check that we just did stays valid. Since we can't assign to diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 0814793f2779..618aa9e31095 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -4,9 +4,8 @@ use rustc_index::vec::Idx; use rustc_middle::ty::util::IntTypeExt; use rustc_target::abi::{Abi, Primitive}; -use crate::build::expr::as_place::PlaceBase; use crate::build::expr::category::{Category, RvalueFunc}; -use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary}; +use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary, PlaceBuilder}; use rustc_hir::lang_items::LangItem; use rustc_middle::middle::region; use rustc_middle::mir::AssertKind; @@ -651,15 +650,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let arg_place_builder = unpack!(block = this.as_place_builder(block, arg)); - let mutability = match arg_place_builder.base() { + let mutability = match arg_place_builder { // We are capturing a path that starts off a local variable in the parent. // The mutability of the current capture is same as the mutability // of the local declaration in the parent. - PlaceBase::Local(local) => this.local_decls[local].mutability, + PlaceBuilder::Local(local, _) => this.local_decls[local].mutability, // Parent is a closure and we are capturing a path that is captured // by the parent itself. The mutability of the current capture // is same as that of the capture in the parent closure. - PlaceBase::Upvar { .. } => { + PlaceBuilder::UpVar(..) => { let enclosing_upvars_resolved = arg_place_builder.to_place(this); match enclosing_upvars_resolved.as_ref() { diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index 218a26e62797..12cd0e65a115 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -358,8 +358,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .map(|(n, ty)| match fields_map.get(&n) { Some(v) => v.clone(), None => { - let place = place_builder.clone_project(PlaceElem::Field(n, *ty)); - this.consume_by_copy_or_move(place.to_place(this)) + let place_builder = place_builder.clone(); + this.consume_by_copy_or_move( + place_builder.field(this, n, *ty).to_place(this), + ) } }) .collect() diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index 58513bde2aa2..7b932eda7443 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -760,8 +760,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let downcast_place = match_pair.place.downcast(adt_def, variant_index); // `(x as Variant)` let consequent_match_pairs = subpatterns.iter().map(|subpattern| { // e.g., `(x as Variant).0` - let place = downcast_place - .clone_project(PlaceElem::Field(subpattern.field, subpattern.pattern.ty)); + let place = downcast_place.clone().field(self, subpattern.field, subpattern.pattern.ty); // e.g., `(x as Variant).0 @ P1` MatchPair::new(place, &subpattern.pattern, self) }); diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index fb2a5be28a2e..30f4e49d8bae 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -1,4 +1,3 @@ -use crate::build::expr::as_place::PlaceBase; use crate::build::expr::as_place::PlaceBuilder; use crate::build::matches::MatchPair; use crate::build::Builder; @@ -18,70 +17,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { subpatterns .iter() .map(|fieldpat| { - let place = - place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty)); - MatchPair::new(place, &fieldpat.pattern, self) - }) - .collect() - } - - #[instrument(skip(self), level = "debug")] - pub(crate) fn field_match_pairs_tuple_struct<'pat>( - &mut self, - place_builder: PlaceBuilder<'tcx>, - subpatterns: &'pat [FieldPat<'tcx>], - ) -> Vec> { - let place_ty_and_variant_idx = - place_builder.try_compute_ty(&self.local_decls, self).map(|place_ty| { - ( - self.tcx.normalize_erasing_regions(self.param_env, place_ty.ty), - place_ty.variant_index, - ) - }); - debug!(?place_ty_and_variant_idx); - - subpatterns - .iter() - .map(|fieldpat| { - // NOTE: With type ascriptions it can happen that we get errors - // during borrow-checking on higher-ranked types if we use the - // ascribed type as the field type, so we try to get the actual field - // type from the `Place`, if possible, see issue #96514 - let field_ty = if let Some((place_ty, opt_variant_idx)) = place_ty_and_variant_idx { - let field_idx = fieldpat.field.as_usize(); - let field_ty = match place_ty.kind() { - ty::Adt(adt_def, substs) if adt_def.is_enum() => { - let variant_idx = opt_variant_idx.unwrap(); - adt_def.variant(variant_idx).fields[field_idx].ty(self.tcx, substs) - } - ty::Adt(adt_def, substs) => adt_def - .all_fields() - .nth(field_idx) - .unwrap_or_else(|| { - bug!( - "expected to take field idx {:?} of fields of {:?}", - field_idx, - adt_def - ) - }) - .ty(self.tcx, substs), - ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { - bug!("expected to take field idx {:?} of {:?}", field_idx, elems) - }), - _ => bug!( - "no field available, place_ty: {:#?}, kind: {:?}", - place_ty, - place_ty.kind() - ), - }; - - self.tcx.normalize_erasing_regions(self.param_env, field_ty) - } else { - fieldpat.pattern.ty - }; - - let place = place_builder.clone().field(fieldpat.field, field_ty); - debug!(?place, ?field_ty); + let place = place.clone().field(self, fieldpat.field, fieldpat.pattern.ty); MatchPair::new(place, &fieldpat.pattern, self) }) @@ -171,9 +107,10 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> { // Only add the OpaqueCast projection if the given place is an opaque type and the // expected type from the pattern is not. - let may_need_cast = match place.base() { - PlaceBase::Local(local) => { - let ty = Place::ty_from(local, place.projection(), &cx.local_decls, cx.tcx).ty; + let may_need_cast = match place { + PlaceBuilder::Local(local, _) => { + let ty = + Place::ty_from(local, place.get_local_projection(), &cx.local_decls, cx.tcx).ty; ty != pattern.ty && ty.has_opaque_types() } _ => true, From 4d00026edd315a05c4aff27c8d8cb233a6e95d92 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 22 Nov 2022 22:08:09 +0100 Subject: [PATCH 010/321] add invariance test --- src/test/ui/mir/field-projection-invariant.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/test/ui/mir/field-projection-invariant.rs diff --git a/src/test/ui/mir/field-projection-invariant.rs b/src/test/ui/mir/field-projection-invariant.rs new file mode 100644 index 000000000000..b5d6add043cb --- /dev/null +++ b/src/test/ui/mir/field-projection-invariant.rs @@ -0,0 +1,24 @@ +// build-pass +struct Inv<'a>(&'a mut &'a ()); +enum Foo { + Bar, + Var(T), +} +type Supertype = Foo fn(Inv<'a>, Inv<'a>)>; + +fn foo(x: Foo fn(Inv<'a>, Inv<'b>)>) { + match x { + Supertype::Bar => {} + Supertype::Var(x) => {} + } +} + +fn foo_nested(x: Foo fn(Inv<'a>, Inv<'b>)>>) { + match x { + Foo::Bar => {} + Foo::Var(Supertype::Bar) => {} + Foo::Var(Supertype::Var(x)) => {} + } +} + +fn main() {} From c39de61d2a8fd1f3226e623d67e1e5bac593dac6 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 23 Nov 2022 17:42:12 +0100 Subject: [PATCH 011/321] include closures and generators in try_compute_field_ty --- .../src/build/expr/as_place.rs | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 5c9459c97f40..bf417dae7851 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -397,13 +397,49 @@ impl<'tcx> PlaceBuilder<'tcx> { .all_fields() .nth(field_idx) .unwrap_or_else(|| { - bug!("expected to take field idx {:?} of fields of {:?}", field_idx, adt_def) + bug!( + "expected to take field with idx {:?} of fields of {:?}", + field_idx, + adt_def + ) }) .ty(cx.tcx, substs), ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { - bug!("expected to take field idx {:?} of {:?}", field_idx, elems) + bug!("expected to take field with idx {:?} of {:?}", field_idx, elems) }), - _ => return None, + ty::Closure(_, substs) => { + let substs = substs.as_closure(); + let Some(f_ty) = substs.upvar_tys().nth(field_idx) else { + bug!("expected to take field with idx {:?} of {:?}", field_idx, substs.upvar_tys().collect::>()); + }; + + f_ty + } + &ty::Generator(def_id, substs, _) => { + if let Some(var) = variant_index { + let gen_body = cx.tcx.optimized_mir(def_id); + let Some(layout) = gen_body.generator_layout() else { + bug!("No generator layout for {:?}", base_ty); + }; + + let Some(&local) = layout.variant_fields[var].get(field) else { + bug!("expected to take field {:?} of {:?}", field, layout.variant_fields[var]); + }; + + let Some(&f_ty) = layout.field_tys.get(local) else { + bug!("expected to get element for {:?} in {:?}", local, layout.field_tys); + }; + + f_ty + } else { + let Some(f_ty) = substs.as_generator().prefix_tys().nth(field.index()) else { + bug!("expected to take index {:?} in {:?}", field.index(), substs.as_generator().prefix_tys().collect::>()); + }; + + f_ty + } + } + _ => bug!("couldn't create field type, unexpected base type: {:?}", base_ty), }; Some(cx.tcx.normalize_erasing_regions(cx.param_env, field_ty)) From 9061ffba8ceb6a3a927aff15b570f9bd4247862a Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 23 Nov 2022 19:55:24 +0100 Subject: [PATCH 012/321] use no type in ProjectionElem::Field for PlaceBuilder::UpVar --- compiler/rustc_middle/src/mir/mod.rs | 4 +- compiler/rustc_middle/src/mir/syntax.rs | 32 ++++++- compiler/rustc_middle/src/mir/tcx.rs | 15 +-- .../src/build/expr/as_place.rs | 92 ++++++++++++------- .../rustc_mir_build/src/build/expr/into.rs | 4 +- .../rustc_mir_build/src/build/matches/test.rs | 2 +- .../rustc_mir_build/src/build/matches/util.rs | 2 +- .../src/move_paths/abs_domain.rs | 2 +- .../rustc_mir_dataflow/src/value_analysis.rs | 4 +- 9 files changed, 105 insertions(+), 52 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 364c1b375ae5..a646860ed45f 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -1517,7 +1517,7 @@ impl<'tcx> StatementKind<'tcx> { /////////////////////////////////////////////////////////////////////////// // Places -impl ProjectionElem { +impl ProjectionElem { /// Returns `true` if the target of this projection may refer to a different region of memory /// than the base. fn is_indirect(&self) -> bool { @@ -1546,7 +1546,7 @@ impl ProjectionElem { /// Alias for projections as they appear in `UserTypeProjection`, where we /// need neither the `V` parameter for `Index` nor the `T` for `Field`. -pub type ProjectionKind = ProjectionElem<(), ()>; +pub type ProjectionKind = ProjectionElem<(), (), ()>; rustc_index::newtype_index! { /// A [newtype'd][wrapper] index type in the MIR [control-flow graph][CFG] diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index fed943169dfb..004dd004c723 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -877,9 +877,9 @@ pub struct Place<'tcx> { #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)] -pub enum ProjectionElem { +pub enum ProjectionElem { Deref, - Field(Field, T), + Field(Field, T1), /// Index into a slice/array. /// /// Note that this does not also dereference, and so it does not exactly correspond to slice @@ -935,12 +935,36 @@ pub enum ProjectionElem { /// Like an explicit cast from an opaque type to a concrete type, but without /// requiring an intermediate variable. - OpaqueCast(T), + OpaqueCast(T2), } /// Alias for projections as they appear in places, where the base is a place /// and the index is a local. -pub type PlaceElem<'tcx> = ProjectionElem>; +pub type PlaceElem<'tcx> = ProjectionElem, Ty<'tcx>>; + +/// Alias for projections that appear in `PlaceBuilder::UpVar`, for which +/// we cannot provide any field types. +pub type UpvarProjectionElem<'tcx> = ProjectionElem>; + +impl<'tcx> From> for UpvarProjectionElem<'tcx> { + fn from(elem: PlaceElem<'tcx>) -> Self { + match elem { + ProjectionElem::Deref => ProjectionElem::Deref, + ProjectionElem::Field(field, _) => ProjectionElem::Field(field, ()), + ProjectionElem::Index(v) => ProjectionElem::Index(v), + ProjectionElem::ConstantIndex { offset, min_length, from_end } => { + ProjectionElem::ConstantIndex { offset, min_length, from_end } + } + ProjectionElem::Subslice { from, to, from_end } => { + ProjectionElem::Subslice { from, to, from_end } + } + ProjectionElem::Downcast(opt_sym, variant_idx) => { + ProjectionElem::Downcast(opt_sym, variant_idx) + } + ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), + } + } +} /////////////////////////////////////////////////////////////////////////// // Operands diff --git a/compiler/rustc_middle/src/mir/tcx.rs b/compiler/rustc_middle/src/mir/tcx.rs index fa3adafd4b85..e3ca1f41d7ea 100644 --- a/compiler/rustc_middle/src/mir/tcx.rs +++ b/compiler/rustc_middle/src/mir/tcx.rs @@ -28,8 +28,8 @@ impl<'tcx> PlaceTy<'tcx> { /// `place_ty.field_ty(tcx, f)` computes the type at a given field /// of a record or enum-variant. (Most clients of `PlaceTy` can /// instead just extract the relevant type directly from their - /// `PlaceElem`, but some instances of `ProjectionElem` do - /// not carry a `Ty` for `T`.) + /// `PlaceElem`, but some instances of `ProjectionElem` do + /// not carry a `Ty` for `T1` or `T2`.) /// /// Note that the resulting type has not been normalized. pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: Field) -> Ty<'tcx> { @@ -64,17 +64,18 @@ impl<'tcx> PlaceTy<'tcx> { /// `Ty` or downcast variant corresponding to that projection. /// The `handle_field` callback must map a `Field` to its `Ty`, /// (which should be trivial when `T` = `Ty`). - pub fn projection_ty_core( + pub fn projection_ty_core( self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, - elem: &ProjectionElem, - mut handle_field: impl FnMut(&Self, Field, T) -> Ty<'tcx>, - mut handle_opaque_cast: impl FnMut(&Self, T) -> Ty<'tcx>, + elem: &ProjectionElem, + mut handle_field: impl FnMut(&Self, Field, T1) -> Ty<'tcx>, + mut handle_opaque_cast: impl FnMut(&Self, T2) -> Ty<'tcx>, ) -> PlaceTy<'tcx> where V: ::std::fmt::Debug, - T: ::std::fmt::Debug + Copy, + T1: ::std::fmt::Debug + Copy, + T2: ::std::fmt::Debug + Copy, { if self.variant_index.is_some() && !matches!(elem, ProjectionElem::Field(..)) { bug!("cannot use non field projection on downcasted place") diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index bf417dae7851..bd27b6c2bebc 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -19,6 +19,7 @@ use rustc_target::abi::VariantIdx; use rustc_index::vec::Idx; use std::assert_matches::assert_matches; +use std::convert::From; use std::iter; /// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a @@ -66,7 +67,7 @@ pub(in crate::build) enum PlaceBuilder<'tcx> { /// /// Note: in contrast to `PlaceBuilder::Local` we have not yet determined all `Field` types /// and will only do so once converting to `PlaceBuilder::Local`. - UpVar(UpVar, Vec>), + UpVar(UpVar, Vec>), } #[derive(Copy, Clone, Debug, PartialEq)] @@ -82,7 +83,7 @@ pub(crate) struct UpVar { /// part of a path that is captured by a closure. We stop applying projections once we see the first /// projection that isn't captured by a closure. fn convert_to_hir_projections_and_truncate_for_capture<'tcx>( - mir_projections: &[PlaceElem<'tcx>], + mir_projections: &[UpvarProjectionElem<'tcx>], ) -> Vec { let mut hir_projections = Vec::new(); let mut variant = None; @@ -156,7 +157,7 @@ fn is_ancestor_or_same_capture( fn find_capture_matching_projections<'a, 'tcx>( upvars: &'a CaptureMap<'tcx>, var_hir_id: LocalVarId, - projections: &[PlaceElem<'tcx>], + projections: &[UpvarProjectionElem<'tcx>], ) -> Option<(usize, &'a Capture<'tcx>)> { let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections); @@ -212,8 +213,7 @@ fn to_upvars_resolved_place_builder<'tcx>( capture.captured_place.place.base_ty, projection, &capture.captured_place.place.projections, - ) - .collect::>(); + ); let upvar_resolved_place_builder = PlaceBuilder::construct_local_place_builder( cx, @@ -222,6 +222,37 @@ fn to_upvars_resolved_place_builder<'tcx>( upvar_projection, ); + debug_assert!({ + let builder = upvar_resolved_place_builder.clone(); + let mut valid_conversion = true; + match builder { + PlaceBuilder::Local(_, projections) => { + for proj in projections.iter() { + match proj { + ProjectionElem::Field(_, field_ty) => { + if matches!(field_ty.kind(), ty::Infer(..)) { + debug!( + "field ty should have been converted for projection {:?} in PlaceBuilder {:?}", + proj, + upvar_resolved_place_builder.clone() + ); + + valid_conversion = false; + break; + } + } + _ => {} + } + } + } + PlaceBuilder::UpVar(..) => { + unreachable!() + } + } + + valid_conversion + }); + Some(upvar_resolved_place_builder) } @@ -233,14 +264,14 @@ fn to_upvars_resolved_place_builder<'tcx>( /// projection kinds are unsupported. fn strip_prefix<'a, 'tcx>( mut base_ty: Ty<'tcx>, - projections: &'a [PlaceElem<'tcx>], + projections: &'a [UpvarProjectionElem<'tcx>], prefix_projections: &[HirProjection<'tcx>], -) -> impl Iterator> + 'a { +) -> Vec> { let mut iter = projections .iter() - .copied() // Filter out opaque casts, they are unnecessary in the prefix. - .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))); + .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))) + .map(|elem| *elem); for projection in prefix_projections { debug!(?projection, ?projection.ty); @@ -261,7 +292,8 @@ fn strip_prefix<'a, 'tcx>( base_ty = projection.ty; } - iter + + iter.collect::>() } impl<'tcx> PlaceBuilder<'tcx> { @@ -292,11 +324,11 @@ impl<'tcx> PlaceBuilder<'tcx> { &self, cx: &Builder<'_, 'tcx>, ) -> Option> { - let PlaceBuilder::Upvar( Upvar {var_hir_id, closure_def_id }, projection) = self else { + let PlaceBuilder::UpVar( UpVar {var_hir_id, closure_def_id }, projection) = self else { return None; }; - to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &projection) + to_upvars_resolved_place_builder(cx, *var_hir_id, *closure_def_id, &projection) } pub(crate) fn get_local_projection(&self) -> &[PlaceElem<'tcx>] { @@ -309,19 +341,16 @@ impl<'tcx> PlaceBuilder<'tcx> { } #[instrument(skip(cx), level = "debug")] - pub(crate) fn field( - self, - cx: &Builder<'_, 'tcx>, - f: Field, - default_field_ty: Ty<'tcx>, - ) -> Self { + pub(crate) fn field(self, cx: &Builder<'_, 'tcx>, f: Field) -> Self { let field_ty = match self { PlaceBuilder::Local(..) => { let base_place = self.clone(); - PlaceBuilder::try_compute_field_ty(cx, f, base_place) - .unwrap_or_else(|| default_field_ty) + PlaceBuilder::compute_field_ty(cx, f, base_place) + } + PlaceBuilder::UpVar(..) => { + let dummy_ty = cx.tcx.mk_ty_infer(ty::FreshTy(0)); + dummy_ty } - PlaceBuilder::UpVar(..) => default_field_ty, }; self.project(ProjectionElem::Field(f, field_ty)) @@ -347,7 +376,7 @@ impl<'tcx> PlaceBuilder<'tcx> { PlaceBuilder::Local(local, proj) } PlaceBuilder::UpVar(upvar, mut proj) => { - proj.push(elem); + proj.push(elem.into()); PlaceBuilder::UpVar(upvar, proj) } }; @@ -363,8 +392,8 @@ impl<'tcx> PlaceBuilder<'tcx> { *local, Vec::from_iter(proj.iter().copied().chain([elem.into()])), ), - PlaceBuilder::Upvar(upvar, proj) => PlaceBuilder::UpVar( - upvar, + PlaceBuilder::UpVar(upvar, proj) => PlaceBuilder::UpVar( + *upvar, Vec::from_iter(proj.iter().copied().chain([elem.into()])), ), } @@ -378,11 +407,11 @@ impl<'tcx> PlaceBuilder<'tcx> { /// Fallible as the root of this place may be an upvar for /// which no base type can be determined. #[instrument(skip(cx), level = "debug")] - fn try_compute_field_ty( + fn compute_field_ty( cx: &Builder<'_, 'tcx>, field: Field, base_place: PlaceBuilder<'tcx>, - ) -> Option> { + ) -> Ty<'tcx> { let field_idx = field.as_usize(); let PlaceTy { ty, variant_index } = base_place.to_place(cx).ty(&cx.local_decls, cx.tcx); let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty); @@ -442,7 +471,7 @@ impl<'tcx> PlaceBuilder<'tcx> { _ => bug!("couldn't create field type, unexpected base type: {:?}", base_ty), }; - Some(cx.tcx.normalize_erasing_regions(cx.param_env, field_ty)) + cx.tcx.normalize_erasing_regions(cx.param_env, field_ty) } /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::UpVar` whose upvars @@ -455,7 +484,7 @@ impl<'tcx> PlaceBuilder<'tcx> { cx: &Builder<'_, 'tcx>, local: Local, mut local_projection: Vec>, - upvar_projection: Vec>, + upvar_projection: Vec>, ) -> Self { // We iterate through `upvar_projection` and whenever we find a `ProjectionElem::Field` we use // the ancestor projections, i.e. those projection elements that come before the field projection, @@ -464,11 +493,10 @@ impl<'tcx> PlaceBuilder<'tcx> { for proj in upvar_projection.iter() { debug!("proj: {:?}, local_projection: {:?}", proj, local_projection); match *proj { - ProjectionElem::Field(field, default_field_ty) => { + ProjectionElem::Field(field, _) => { let ancestor_proj = local_projection.to_vec(); let base_place = PlaceBuilder::Local(local, ancestor_proj); - let field_ty = PlaceBuilder::try_compute_field_ty(cx, field, base_place) - .unwrap_or_else(|| default_field_ty); + let field_ty = PlaceBuilder::compute_field_ty(cx, field, base_place); debug!(?field_ty); local_projection.push(ProjectionElem::Field(field, field_ty)); @@ -593,7 +621,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { place_builder = place_builder.downcast(*adt_def, variant_index); } } - block.and(place_builder.field(this, name, expr.ty)) + block.and(place_builder.field(this, name)) } ExprKind::Deref { arg } => { let place_builder = unpack!( diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index 12cd0e65a115..e5c2f4f4cbeb 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -355,12 +355,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // base-supplied field, generate an operand that // reads it from the base. iter::zip(field_names, &**field_types) - .map(|(n, ty)| match fields_map.get(&n) { + .map(|(n, _ty)| match fields_map.get(&n) { Some(v) => v.clone(), None => { let place_builder = place_builder.clone(); this.consume_by_copy_or_move( - place_builder.field(this, n, *ty).to_place(this), + place_builder.field(this, n).to_place(this), ) } }) diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index 7b932eda7443..c669016e0ed7 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -760,7 +760,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let downcast_place = match_pair.place.downcast(adt_def, variant_index); // `(x as Variant)` let consequent_match_pairs = subpatterns.iter().map(|subpattern| { // e.g., `(x as Variant).0` - let place = downcast_place.clone().field(self, subpattern.field, subpattern.pattern.ty); + let place = downcast_place.clone().field(self, subpattern.field); // e.g., `(x as Variant).0 @ P1` MatchPair::new(place, &subpattern.pattern, self) }); diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index 30f4e49d8bae..27a7b03465f6 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -17,7 +17,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { subpatterns .iter() .map(|fieldpat| { - let place = place.clone().field(self, fieldpat.field, fieldpat.pattern.ty); + let place = place.clone().field(self, fieldpat.field); MatchPair::new(place, &fieldpat.pattern, self) }) diff --git a/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs b/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs index 7806e8f45d3a..5cfbbb1ac01e 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs @@ -18,7 +18,7 @@ use rustc_middle::ty::Ty; pub struct AbstractOperand; #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct AbstractType; -pub type AbstractElem = ProjectionElem; +pub type AbstractElem = ProjectionElem; pub trait Lift { type Abstract; diff --git a/compiler/rustc_mir_dataflow/src/value_analysis.rs b/compiler/rustc_mir_dataflow/src/value_analysis.rs index cc69a1bb02db..b09503a30930 100644 --- a/compiler/rustc_mir_dataflow/src/value_analysis.rs +++ b/compiler/rustc_mir_dataflow/src/value_analysis.rs @@ -777,10 +777,10 @@ pub enum TrackElem { Field(Field), } -impl TryFrom> for TrackElem { +impl TryFrom> for TrackElem { type Error = (); - fn try_from(value: ProjectionElem) -> Result { + fn try_from(value: ProjectionElem) -> Result { match value { ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)), _ => Err(()), From 93b5c893e6394104b5ae80cf42d7220ad3d18779 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 10 Nov 2022 13:19:15 +0100 Subject: [PATCH 013/321] Add semicolon-outside/inside-block lints --- CHANGELOG.md | 2 + clippy_lints/src/declared_lints.rs | 2 + clippy_lints/src/lib.rs | 2 + clippy_lints/src/semicolon_block.rs | 131 ++++++++++++++++++++++++ tests/ui/semicolon_inside_block.fixed | 83 +++++++++++++++ tests/ui/semicolon_inside_block.rs | 83 +++++++++++++++ tests/ui/semicolon_inside_block.stderr | 39 +++++++ tests/ui/semicolon_outside_block.fixed | 83 +++++++++++++++ tests/ui/semicolon_outside_block.rs | 83 +++++++++++++++ tests/ui/semicolon_outside_block.stderr | 39 +++++++ 10 files changed, 547 insertions(+) create mode 100644 clippy_lints/src/semicolon_block.rs create mode 100644 tests/ui/semicolon_inside_block.fixed create mode 100644 tests/ui/semicolon_inside_block.rs create mode 100644 tests/ui/semicolon_inside_block.stderr create mode 100644 tests/ui/semicolon_outside_block.fixed create mode 100644 tests/ui/semicolon_outside_block.rs create mode 100644 tests/ui/semicolon_outside_block.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b6b12c623af..dac4a54b11fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4353,6 +4353,8 @@ Released 2018-09-13 [`self_named_constructors`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_constructors [`self_named_module_files`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_module_files [`semicolon_if_nothing_returned`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned +[`semicolon_inside_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_inside_block +[`semicolon_outside_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_outside_block [`separated_literal_suffix`]: https://rust-lang.github.io/rust-clippy/master/index.html#separated_literal_suffix [`serde_api_misuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#serde_api_misuse [`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index eb3210946f11..76b4a2ccf5e1 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -525,6 +525,8 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::returns::NEEDLESS_RETURN_INFO, crate::same_name_method::SAME_NAME_METHOD_INFO, crate::self_named_constructors::SELF_NAMED_CONSTRUCTORS_INFO, + crate::semicolon_block::SEMICOLON_INSIDE_BLOCK_INFO, + crate::semicolon_block::SEMICOLON_OUTSIDE_BLOCK_INFO, crate::semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED_INFO, crate::serde_api::SERDE_API_MISUSE_INFO, crate::shadow::SHADOW_REUSE_INFO, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 17dbd983b6c0..cbca317a1463 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -257,6 +257,7 @@ mod return_self_not_must_use; mod returns; mod same_name_method; mod self_named_constructors; +mod semicolon_block; mod semicolon_if_nothing_returned; mod serde_api; mod shadow; @@ -884,6 +885,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr)); store.register_late_pass(|_| Box::new(suspicious_xor_used_as_pow::ConfusingXorAndPow)); store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(msrv()))); + store.register_late_pass(|_| Box::new(semicolon_block::SemicolonBlock)); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs new file mode 100644 index 000000000000..911d38f65d6c --- /dev/null +++ b/clippy_lints/src/semicolon_block.rs @@ -0,0 +1,131 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet_with_macro_callsite; +use clippy_utils::{get_parent_expr_for_hir, get_parent_node}; +use rustc_errors::Applicability; +use rustc_hir::{Block, Expr, ExprKind, Node, Stmt, StmtKind}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; + +declare_clippy_lint! { + /// ### What it does + /// + /// For () returning expressions, check that the semicolon is inside the block. + /// + /// ### Why is this bad? + /// + /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests inside the block. + /// Take a look at `semicolon_outside_block` for the other alternative. + /// + /// ### Example + /// + /// ```rust + /// # fn f(_: u32) {} + /// # let x = 0; + /// unsafe { f(x) }; + /// ``` + /// Use instead: + /// ```rust + /// # fn f(_: u32) {} + /// # let x = 0; + /// unsafe { f(x); } + /// ``` + #[clippy::version = "1.66.0"] + pub SEMICOLON_INSIDE_BLOCK, + restriction, + "add a semicolon inside the block" +} +declare_clippy_lint! { + /// ### What it does + /// + /// For () returning expressions, check that the semicolon is outside the block. + /// + /// ### Why is this bad? + /// + /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests outside the block. + /// Take a look at `semicolon_inside_block` for the other alternative. + /// + /// ### Example + /// + /// ```rust + /// # fn f(_: u32) {} + /// # let x = 0; + /// unsafe { f(x); } + /// ``` + /// Use instead: + /// ```rust + /// # fn f(_: u32) {} + /// # let x = 0; + /// unsafe { f(x) }; + /// ``` + #[clippy::version = "1.66.0"] + pub SEMICOLON_OUTSIDE_BLOCK, + restriction, + "add a semicolon outside the block" +} +declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]); + +impl LateLintPass<'_> for SemicolonBlock { + fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) { + semicolon_inside_block(cx, block); + semicolon_outside_block(cx, block); + } +} + +fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>) { + if !block.span.from_expansion() + && let Some(tail) = block.expr + && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx, block.hir_id) + && let Some(Node::Stmt(Stmt { kind: StmtKind::Semi(_), span, .. })) = get_parent_node(cx.tcx, block_expr.hir_id) + { + let expr_snip = snippet_with_macro_callsite(cx, tail.span, ".."); + + let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); + + if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() { + suggestion.insert(expr_offset + expr_snip.len(), ';'); + } else { + return; + } + + span_lint_and_sugg( + cx, + SEMICOLON_INSIDE_BLOCK, + *span, + "consider moving the `;` inside the block for consistent formatting", + "put the `;` here", + suggestion, + Applicability::MaybeIncorrect, + ); + } +} + +fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>) { + if !block.span.from_expansion() + && block.expr.is_none() + && let [.., Stmt { kind: StmtKind::Semi(expr), .. }] = block.stmts + && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx,block.hir_id) + && let Some(Node::Stmt(Stmt { kind: StmtKind::Expr(_), .. })) = get_parent_node(cx.tcx, block_expr.hir_id) { + let expr_snip = snippet_with_macro_callsite(cx, expr.span, ".."); + + let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); + + if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() + && let Some(semi_offset) = suggestion[expr_offset + expr_snip.len()..].find(';') { + suggestion.remove(expr_offset + expr_snip.len() + semi_offset); + } else { + return; + } + + suggestion.push(';'); + + span_lint_and_sugg( + cx, + SEMICOLON_OUTSIDE_BLOCK, + block.span, + "consider moving the `;` outside the block for consistent formatting", + "put the `;` outside the block", + suggestion, + Applicability::MaybeIncorrect, + ); + } +} diff --git a/tests/ui/semicolon_inside_block.fixed b/tests/ui/semicolon_inside_block.fixed new file mode 100644 index 000000000000..4cd112dd5e12 --- /dev/null +++ b/tests/ui/semicolon_inside_block.fixed @@ -0,0 +1,83 @@ +// run-rustfix +#![allow( + unused, + clippy::unused_unit, + clippy::unnecessary_operation, + clippy::no_effect, + clippy::single_element_loop +)] +#![warn(clippy::semicolon_inside_block)] + +macro_rules! m { + (()) => { + () + }; + (0) => {{ + 0 + };}; + (1) => {{ + 1; + }}; + (2) => {{ + 2; + }}; +} + +fn unit_fn_block() { + () +} + +#[rustfmt::skip] +fn main() { + { unit_fn_block() } + unsafe { unit_fn_block() } + + { + unit_fn_block() + } + + { unit_fn_block(); } + unsafe { unit_fn_block(); } + + { unit_fn_block(); } + unsafe { unit_fn_block(); } + + { unit_fn_block(); }; + unsafe { unit_fn_block(); }; + + { + unit_fn_block(); + unit_fn_block(); + } + { + unit_fn_block(); + unit_fn_block(); + } + { + unit_fn_block(); + unit_fn_block(); + }; + + { m!(()); } + { m!(()); } + { m!(()); }; + m!(0); + m!(1); + m!(2); + + for _ in [()] { + unit_fn_block(); + } + for _ in [()] { + unit_fn_block() + } + + let _d = || { + unit_fn_block(); + }; + let _d = || { + unit_fn_block() + }; + + unit_fn_block() +} diff --git a/tests/ui/semicolon_inside_block.rs b/tests/ui/semicolon_inside_block.rs new file mode 100644 index 000000000000..7512125c051d --- /dev/null +++ b/tests/ui/semicolon_inside_block.rs @@ -0,0 +1,83 @@ +// run-rustfix +#![allow( + unused, + clippy::unused_unit, + clippy::unnecessary_operation, + clippy::no_effect, + clippy::single_element_loop +)] +#![warn(clippy::semicolon_inside_block)] + +macro_rules! m { + (()) => { + () + }; + (0) => {{ + 0 + };}; + (1) => {{ + 1; + }}; + (2) => {{ + 2; + }}; +} + +fn unit_fn_block() { + () +} + +#[rustfmt::skip] +fn main() { + { unit_fn_block() } + unsafe { unit_fn_block() } + + { + unit_fn_block() + } + + { unit_fn_block() }; + unsafe { unit_fn_block() }; + + { unit_fn_block(); } + unsafe { unit_fn_block(); } + + { unit_fn_block(); }; + unsafe { unit_fn_block(); }; + + { + unit_fn_block(); + unit_fn_block() + }; + { + unit_fn_block(); + unit_fn_block(); + } + { + unit_fn_block(); + unit_fn_block(); + }; + + { m!(()) }; + { m!(()); } + { m!(()); }; + m!(0); + m!(1); + m!(2); + + for _ in [()] { + unit_fn_block(); + } + for _ in [()] { + unit_fn_block() + } + + let _d = || { + unit_fn_block(); + }; + let _d = || { + unit_fn_block() + }; + + unit_fn_block() +} diff --git a/tests/ui/semicolon_inside_block.stderr b/tests/ui/semicolon_inside_block.stderr new file mode 100644 index 000000000000..febe74b49bda --- /dev/null +++ b/tests/ui/semicolon_inside_block.stderr @@ -0,0 +1,39 @@ +error: consider moving the `;` inside the block for consistent formatting + --> $DIR/semicolon_inside_block.rs:39:5 + | +LL | { unit_fn_block() }; + | ^^^^^^^^^^^^^^^^^^^^ help: put the `;` here: `{ unit_fn_block(); }` + | + = note: `-D clippy::semicolon-inside-block` implied by `-D warnings` + +error: consider moving the `;` inside the block for consistent formatting + --> $DIR/semicolon_inside_block.rs:40:5 + | +LL | unsafe { unit_fn_block() }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: put the `;` here: `unsafe { unit_fn_block(); }` + +error: consider moving the `;` inside the block for consistent formatting + --> $DIR/semicolon_inside_block.rs:48:5 + | +LL | / { +LL | | unit_fn_block(); +LL | | unit_fn_block() +LL | | }; + | |______^ + | +help: put the `;` here + | +LL ~ { +LL + unit_fn_block(); +LL + unit_fn_block(); +LL + } + | + +error: consider moving the `;` inside the block for consistent formatting + --> $DIR/semicolon_inside_block.rs:61:5 + | +LL | { m!(()) }; + | ^^^^^^^^^^^ help: put the `;` here: `{ m!(()); }` + +error: aborting due to 4 previous errors + diff --git a/tests/ui/semicolon_outside_block.fixed b/tests/ui/semicolon_outside_block.fixed new file mode 100644 index 000000000000..5bc18faaad8e --- /dev/null +++ b/tests/ui/semicolon_outside_block.fixed @@ -0,0 +1,83 @@ +// run-rustfix +#![allow( + unused, + clippy::unused_unit, + clippy::unnecessary_operation, + clippy::no_effect, + clippy::single_element_loop +)] +#![warn(clippy::semicolon_outside_block)] + +macro_rules! m { + (()) => { + () + }; + (0) => {{ + 0 + };}; + (1) => {{ + 1; + }}; + (2) => {{ + 2; + }}; +} + +fn unit_fn_block() { + () +} + +#[rustfmt::skip] +fn main() { + { unit_fn_block() } + unsafe { unit_fn_block() } + + { + unit_fn_block() + } + + { unit_fn_block() }; + unsafe { unit_fn_block() }; + + { unit_fn_block() }; + unsafe { unit_fn_block() }; + + { unit_fn_block(); }; + unsafe { unit_fn_block(); }; + + { + unit_fn_block(); + unit_fn_block() + }; + { + unit_fn_block(); + unit_fn_block() + }; + { + unit_fn_block(); + unit_fn_block(); + }; + + { m!(()) }; + { m!(()) }; + { m!(()); }; + m!(0); + m!(1); + m!(2); + + for _ in [()] { + unit_fn_block(); + } + for _ in [()] { + unit_fn_block() + } + + let _d = || { + unit_fn_block(); + }; + let _d = || { + unit_fn_block() + }; + + unit_fn_block() +} diff --git a/tests/ui/semicolon_outside_block.rs b/tests/ui/semicolon_outside_block.rs new file mode 100644 index 000000000000..0a4293238763 --- /dev/null +++ b/tests/ui/semicolon_outside_block.rs @@ -0,0 +1,83 @@ +// run-rustfix +#![allow( + unused, + clippy::unused_unit, + clippy::unnecessary_operation, + clippy::no_effect, + clippy::single_element_loop +)] +#![warn(clippy::semicolon_outside_block)] + +macro_rules! m { + (()) => { + () + }; + (0) => {{ + 0 + };}; + (1) => {{ + 1; + }}; + (2) => {{ + 2; + }}; +} + +fn unit_fn_block() { + () +} + +#[rustfmt::skip] +fn main() { + { unit_fn_block() } + unsafe { unit_fn_block() } + + { + unit_fn_block() + } + + { unit_fn_block() }; + unsafe { unit_fn_block() }; + + { unit_fn_block(); } + unsafe { unit_fn_block(); } + + { unit_fn_block(); }; + unsafe { unit_fn_block(); }; + + { + unit_fn_block(); + unit_fn_block() + }; + { + unit_fn_block(); + unit_fn_block(); + } + { + unit_fn_block(); + unit_fn_block(); + }; + + { m!(()) }; + { m!(()); } + { m!(()); }; + m!(0); + m!(1); + m!(2); + + for _ in [()] { + unit_fn_block(); + } + for _ in [()] { + unit_fn_block() + } + + let _d = || { + unit_fn_block(); + }; + let _d = || { + unit_fn_block() + }; + + unit_fn_block() +} diff --git a/tests/ui/semicolon_outside_block.stderr b/tests/ui/semicolon_outside_block.stderr new file mode 100644 index 000000000000..fddf51208130 --- /dev/null +++ b/tests/ui/semicolon_outside_block.stderr @@ -0,0 +1,39 @@ +error: consider moving the `;` outside the block for consistent formatting + --> $DIR/semicolon_outside_block.rs:42:5 + | +LL | { unit_fn_block(); } + | ^^^^^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `{ unit_fn_block() };` + | + = note: `-D clippy::semicolon-outside-block` implied by `-D warnings` + +error: consider moving the `;` outside the block for consistent formatting + --> $DIR/semicolon_outside_block.rs:43:5 + | +LL | unsafe { unit_fn_block(); } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `unsafe { unit_fn_block() };` + +error: consider moving the `;` outside the block for consistent formatting + --> $DIR/semicolon_outside_block.rs:52:5 + | +LL | / { +LL | | unit_fn_block(); +LL | | unit_fn_block(); +LL | | } + | |_____^ + | +help: put the `;` outside the block + | +LL ~ { +LL + unit_fn_block(); +LL + unit_fn_block() +LL + }; + | + +error: consider moving the `;` outside the block for consistent formatting + --> $DIR/semicolon_outside_block.rs:62:5 + | +LL | { m!(()); } + | ^^^^^^^^^^^ help: put the `;` outside the block: `{ m!(()) };` + +error: aborting due to 4 previous errors + From ba951e3ca72283d39757bd39d20bfd75ce901147 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 15 Nov 2022 18:29:43 +0100 Subject: [PATCH 014/321] Fix formatting of let chains --- clippy_lints/src/semicolon_block.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index 911d38f65d6c..a41f89a6aa33 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -73,9 +73,9 @@ impl LateLintPass<'_> for SemicolonBlock { fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>) { if !block.span.from_expansion() - && let Some(tail) = block.expr - && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx, block.hir_id) - && let Some(Node::Stmt(Stmt { kind: StmtKind::Semi(_), span, .. })) = get_parent_node(cx.tcx, block_expr.hir_id) + && let Some(tail) = block.expr + && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx, block.hir_id) + && let Some(Node::Stmt(Stmt { kind: StmtKind::Semi(_), span, .. })) = get_parent_node(cx.tcx, block_expr.hir_id) { let expr_snip = snippet_with_macro_callsite(cx, tail.span, ".."); @@ -101,16 +101,18 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>) { fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>) { if !block.span.from_expansion() - && block.expr.is_none() - && let [.., Stmt { kind: StmtKind::Semi(expr), .. }] = block.stmts - && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx,block.hir_id) - && let Some(Node::Stmt(Stmt { kind: StmtKind::Expr(_), .. })) = get_parent_node(cx.tcx, block_expr.hir_id) { + && block.expr.is_none() + && let [.., Stmt { kind: StmtKind::Semi(expr), .. }] = block.stmts + && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx,block.hir_id) + && let Some(Node::Stmt(Stmt { kind: StmtKind::Expr(_), .. })) = get_parent_node(cx.tcx, block_expr.hir_id) + { let expr_snip = snippet_with_macro_callsite(cx, expr.span, ".."); let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() - && let Some(semi_offset) = suggestion[expr_offset + expr_snip.len()..].find(';') { + && let Some(semi_offset) = suggestion[expr_offset + expr_snip.len()..].find(';') + { suggestion.remove(expr_offset + expr_snip.len() + semi_offset); } else { return; From 23744cd4ba57d2fc5c57459002282fe2d896193b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 15 Nov 2022 19:22:00 +0100 Subject: [PATCH 015/321] Use multi-span suggestions --- clippy_lints/src/semicolon_block.rs | 131 ++++++++++++------------ tests/ui/semicolon_inside_block.fixed | 4 +- tests/ui/semicolon_inside_block.stderr | 32 ++++-- tests/ui/semicolon_outside_block.fixed | 14 ++- tests/ui/semicolon_outside_block.stderr | 31 ++++-- 5 files changed, 126 insertions(+), 86 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index a41f89a6aa33..34bf97448273 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -1,10 +1,9 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::source::snippet_with_macro_callsite; -use clippy_utils::{get_parent_expr_for_hir, get_parent_node}; +use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then}; use rustc_errors::Applicability; -use rustc_hir::{Block, Expr, ExprKind, Node, Stmt, StmtKind}; +use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_span::Span; declare_clippy_lint! { /// ### What it does @@ -65,69 +64,73 @@ declare_clippy_lint! { declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]); impl LateLintPass<'_> for SemicolonBlock { - fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) { - semicolon_inside_block(cx, block); - semicolon_outside_block(cx, block); - } -} - -fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>) { - if !block.span.from_expansion() - && let Some(tail) = block.expr - && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx, block.hir_id) - && let Some(Node::Stmt(Stmt { kind: StmtKind::Semi(_), span, .. })) = get_parent_node(cx.tcx, block_expr.hir_id) - { - let expr_snip = snippet_with_macro_callsite(cx, tail.span, ".."); - - let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); - - if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() { - suggestion.insert(expr_offset + expr_snip.len(), ';'); - } else { - return; + fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { + match stmt.kind { + StmtKind::Expr(Expr { + kind: + ExprKind::Block( + block @ Block { + expr: None, + stmts: + &[ + .., + Stmt { + kind: StmtKind::Semi(expr), + span, + .. + }, + ], + .. + }, + _, + ), + .. + }) if !block.span.from_expansion() => semicolon_outside_block(cx, block, expr, span), + StmtKind::Semi(Expr { + kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _), + .. + }) if !block.span.from_expansion() => semicolon_inside_block(cx, block, tail, stmt.span), + _ => (), } - - span_lint_and_sugg( - cx, - SEMICOLON_INSIDE_BLOCK, - *span, - "consider moving the `;` inside the block for consistent formatting", - "put the `;` here", - suggestion, - Applicability::MaybeIncorrect, - ); } } -fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>) { - if !block.span.from_expansion() - && block.expr.is_none() - && let [.., Stmt { kind: StmtKind::Semi(expr), .. }] = block.stmts - && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx,block.hir_id) - && let Some(Node::Stmt(Stmt { kind: StmtKind::Expr(_), .. })) = get_parent_node(cx.tcx, block_expr.hir_id) - { - let expr_snip = snippet_with_macro_callsite(cx, expr.span, ".."); +fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) { + let insert_span = tail.span.with_lo(tail.span.hi()); + let remove_span = semi_span.with_lo(block.span.hi()); - let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); - - if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() - && let Some(semi_offset) = suggestion[expr_offset + expr_snip.len()..].find(';') - { - suggestion.remove(expr_offset + expr_snip.len() + semi_offset); - } else { - return; - } - - suggestion.push(';'); - - span_lint_and_sugg( - cx, - SEMICOLON_OUTSIDE_BLOCK, - block.span, - "consider moving the `;` outside the block for consistent formatting", - "put the `;` outside the block", - suggestion, - Applicability::MaybeIncorrect, - ); - } + span_lint_and_then( + cx, + SEMICOLON_INSIDE_BLOCK, + semi_span, + "consider moving the `;` inside the block for consistent formatting", + |diag| { + multispan_sugg_with_applicability( + diag, + "put the `;` here", + Applicability::MachineApplicable, + [(remove_span, String::new()), (insert_span, ";".to_owned())], + ); + }, + ); +} + +fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_expr: &Expr<'_>, semi_span: Span) { + let insert_span = block.span.with_lo(block.span.hi()); + let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi()); + + span_lint_and_then( + cx, + SEMICOLON_OUTSIDE_BLOCK, + block.span, + "consider moving the `;` outside the block for consistent formatting", + |diag| { + multispan_sugg_with_applicability( + diag, + "put the `;` here", + Applicability::MachineApplicable, + [(remove_span, String::new()), (insert_span, ";".to_owned())], + ); + }, + ); } diff --git a/tests/ui/semicolon_inside_block.fixed b/tests/ui/semicolon_inside_block.fixed index 4cd112dd5e12..d2cd8b218829 100644 --- a/tests/ui/semicolon_inside_block.fixed +++ b/tests/ui/semicolon_inside_block.fixed @@ -10,7 +10,7 @@ macro_rules! m { (()) => { - () + (); }; (0) => {{ 0 @@ -58,7 +58,7 @@ fn main() { unit_fn_block(); }; - { m!(()); } + { m!(()) } { m!(()); } { m!(()); }; m!(0); diff --git a/tests/ui/semicolon_inside_block.stderr b/tests/ui/semicolon_inside_block.stderr index febe74b49bda..99f3b65be0f8 100644 --- a/tests/ui/semicolon_inside_block.stderr +++ b/tests/ui/semicolon_inside_block.stderr @@ -2,15 +2,26 @@ error: consider moving the `;` inside the block for consistent formatting --> $DIR/semicolon_inside_block.rs:39:5 | LL | { unit_fn_block() }; - | ^^^^^^^^^^^^^^^^^^^^ help: put the `;` here: `{ unit_fn_block(); }` + | ^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::semicolon-inside-block` implied by `-D warnings` +help: put the `;` here + | +LL - { unit_fn_block() }; +LL + { unit_fn_block(); } + | error: consider moving the `;` inside the block for consistent formatting --> $DIR/semicolon_inside_block.rs:40:5 | LL | unsafe { unit_fn_block() }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: put the `;` here: `unsafe { unit_fn_block(); }` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: put the `;` here + | +LL - unsafe { unit_fn_block() }; +LL + unsafe { unit_fn_block(); } + | error: consider moving the `;` inside the block for consistent formatting --> $DIR/semicolon_inside_block.rs:48:5 @@ -23,17 +34,24 @@ LL | | }; | help: put the `;` here | -LL ~ { -LL + unit_fn_block(); -LL + unit_fn_block(); -LL + } +LL ~ unit_fn_block(); +LL ~ } | error: consider moving the `;` inside the block for consistent formatting --> $DIR/semicolon_inside_block.rs:61:5 | LL | { m!(()) }; - | ^^^^^^^^^^^ help: put the `;` here: `{ m!(()); }` + | ^^^^^^^^^^^ + | +help: put the `;` here + | +LL ~ (); +LL | }; + ... +LL | +LL ~ { m!(()) } + | error: aborting due to 4 previous errors diff --git a/tests/ui/semicolon_outside_block.fixed b/tests/ui/semicolon_outside_block.fixed index 5bc18faaad8e..35eacfe6d3f1 100644 --- a/tests/ui/semicolon_outside_block.fixed +++ b/tests/ui/semicolon_outside_block.fixed @@ -21,6 +21,9 @@ macro_rules! m { (2) => {{ 2; }}; + (stmt) => { + stmt; + }; } fn unit_fn_block() { @@ -39,8 +42,8 @@ fn main() { { unit_fn_block() }; unsafe { unit_fn_block() }; - { unit_fn_block() }; - unsafe { unit_fn_block() }; + { unit_fn_block(); } + unsafe { unit_fn_block(); } { unit_fn_block(); }; unsafe { unit_fn_block(); }; @@ -51,19 +54,20 @@ fn main() { }; { unit_fn_block(); - unit_fn_block() - }; + unit_fn_block(); + } { unit_fn_block(); unit_fn_block(); }; { m!(()) }; - { m!(()) }; + { m!(()); } { m!(()); }; m!(0); m!(1); m!(2); + { m!(stmt) }; for _ in [()] { unit_fn_block(); diff --git a/tests/ui/semicolon_outside_block.stderr b/tests/ui/semicolon_outside_block.stderr index fddf51208130..c2151f7af637 100644 --- a/tests/ui/semicolon_outside_block.stderr +++ b/tests/ui/semicolon_outside_block.stderr @@ -2,15 +2,26 @@ error: consider moving the `;` outside the block for consistent formatting --> $DIR/semicolon_outside_block.rs:42:5 | LL | { unit_fn_block(); } - | ^^^^^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `{ unit_fn_block() };` + | ^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::semicolon-outside-block` implied by `-D warnings` +help: put the `;` here + | +LL - { unit_fn_block(); } +LL + { unit_fn_block() }; + | error: consider moving the `;` outside the block for consistent formatting --> $DIR/semicolon_outside_block.rs:43:5 | LL | unsafe { unit_fn_block(); } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: put the `;` outside the block: `unsafe { unit_fn_block() };` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: put the `;` here + | +LL - unsafe { unit_fn_block(); } +LL + unsafe { unit_fn_block() }; + | error: consider moving the `;` outside the block for consistent formatting --> $DIR/semicolon_outside_block.rs:52:5 @@ -21,19 +32,23 @@ LL | | unit_fn_block(); LL | | } | |_____^ | -help: put the `;` outside the block +help: put the `;` here | -LL ~ { -LL + unit_fn_block(); -LL + unit_fn_block() -LL + }; +LL ~ unit_fn_block() +LL ~ }; | error: consider moving the `;` outside the block for consistent formatting --> $DIR/semicolon_outside_block.rs:62:5 | LL | { m!(()); } - | ^^^^^^^^^^^ help: put the `;` outside the block: `{ m!(()) };` + | ^^^^^^^^^^^ + | +help: put the `;` here + | +LL - () +LL + (); }; + | error: aborting due to 4 previous errors From dd1163f4611d8597d03d2efd569d63a1d82bfd15 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 16 Nov 2022 14:30:53 +0100 Subject: [PATCH 016/321] Fix macro statement handling --- clippy_lints/src/semicolon_block.rs | 7 +++++-- tests/ui/semicolon_inside_block.fixed | 4 ++-- tests/ui/semicolon_inside_block.stderr | 7 ++----- tests/ui/semicolon_outside_block.fixed | 14 +++++--------- tests/ui/semicolon_outside_block.stderr | 4 ++-- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index 34bf97448273..57f835d45b12 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then}; use rustc_errors::Applicability; use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind}; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::Span; @@ -96,7 +96,8 @@ impl LateLintPass<'_> for SemicolonBlock { } fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) { - let insert_span = tail.span.with_lo(tail.span.hi()); + let tail_span_end = tail.span.source_callsite().hi(); + let insert_span = Span::with_root_ctxt(tail_span_end, tail_span_end); let remove_span = semi_span.with_lo(block.span.hi()); span_lint_and_then( @@ -117,6 +118,8 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<' fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_expr: &Expr<'_>, semi_span: Span) { let insert_span = block.span.with_lo(block.span.hi()); + // account for macro calls + let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span); let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi()); span_lint_and_then( diff --git a/tests/ui/semicolon_inside_block.fixed b/tests/ui/semicolon_inside_block.fixed index d2cd8b218829..4cd112dd5e12 100644 --- a/tests/ui/semicolon_inside_block.fixed +++ b/tests/ui/semicolon_inside_block.fixed @@ -10,7 +10,7 @@ macro_rules! m { (()) => { - (); + () }; (0) => {{ 0 @@ -58,7 +58,7 @@ fn main() { unit_fn_block(); }; - { m!(()) } + { m!(()); } { m!(()); } { m!(()); }; m!(0); diff --git a/tests/ui/semicolon_inside_block.stderr b/tests/ui/semicolon_inside_block.stderr index 99f3b65be0f8..48d3690e2bde 100644 --- a/tests/ui/semicolon_inside_block.stderr +++ b/tests/ui/semicolon_inside_block.stderr @@ -46,11 +46,8 @@ LL | { m!(()) }; | help: put the `;` here | -LL ~ (); -LL | }; - ... -LL | -LL ~ { m!(()) } +LL - { m!(()) }; +LL + { m!(()); } | error: aborting due to 4 previous errors diff --git a/tests/ui/semicolon_outside_block.fixed b/tests/ui/semicolon_outside_block.fixed index 35eacfe6d3f1..5bc18faaad8e 100644 --- a/tests/ui/semicolon_outside_block.fixed +++ b/tests/ui/semicolon_outside_block.fixed @@ -21,9 +21,6 @@ macro_rules! m { (2) => {{ 2; }}; - (stmt) => { - stmt; - }; } fn unit_fn_block() { @@ -42,8 +39,8 @@ fn main() { { unit_fn_block() }; unsafe { unit_fn_block() }; - { unit_fn_block(); } - unsafe { unit_fn_block(); } + { unit_fn_block() }; + unsafe { unit_fn_block() }; { unit_fn_block(); }; unsafe { unit_fn_block(); }; @@ -54,20 +51,19 @@ fn main() { }; { unit_fn_block(); - unit_fn_block(); - } + unit_fn_block() + }; { unit_fn_block(); unit_fn_block(); }; { m!(()) }; - { m!(()); } + { m!(()) }; { m!(()); }; m!(0); m!(1); m!(2); - { m!(stmt) }; for _ in [()] { unit_fn_block(); diff --git a/tests/ui/semicolon_outside_block.stderr b/tests/ui/semicolon_outside_block.stderr index c2151f7af637..dcc102e60994 100644 --- a/tests/ui/semicolon_outside_block.stderr +++ b/tests/ui/semicolon_outside_block.stderr @@ -46,8 +46,8 @@ LL | { m!(()); } | help: put the `;` here | -LL - () -LL + (); }; +LL - { m!(()); } +LL + { m!(()) }; | error: aborting due to 4 previous errors From ab4154697512ec076c4c5550d218d46638353eb9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 24 Nov 2022 09:54:09 +0100 Subject: [PATCH 017/321] Address reviews --- clippy_lints/src/semicolon_block.rs | 35 +++++++++++++---------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index 57f835d45b12..d06f21c89c4a 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -67,25 +67,21 @@ impl LateLintPass<'_> for SemicolonBlock { fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { match stmt.kind { StmtKind::Expr(Expr { - kind: - ExprKind::Block( - block @ Block { - expr: None, - stmts: - &[ - .., - Stmt { - kind: StmtKind::Semi(expr), - span, - .. - }, - ], - .. - }, - _, - ), + kind: ExprKind::Block(block, _), .. - }) if !block.span.from_expansion() => semicolon_outside_block(cx, block, expr, span), + }) if !block.span.from_expansion() => { + let Block { + expr: None, + stmts: [.., stmt], + .. + } = block else { return }; + let &Stmt { + kind: StmtKind::Semi(expr), + span, + .. + } = stmt else { return }; + semicolon_outside_block(cx, block, expr, span) + }, StmtKind::Semi(Expr { kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _), .. @@ -96,8 +92,7 @@ impl LateLintPass<'_> for SemicolonBlock { } fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) { - let tail_span_end = tail.span.source_callsite().hi(); - let insert_span = Span::with_root_ctxt(tail_span_end, tail_span_end); + let insert_span = tail.span.source_callsite().shrink_to_hi(); let remove_span = semi_span.with_lo(block.span.hi()); span_lint_and_then( From f585414cd2fc635fecfe0437f9f71b08aba51d16 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 24 Nov 2022 20:37:07 +0100 Subject: [PATCH 018/321] Adjust semicolon block lint descriptions --- clippy_lints/src/semicolon_block.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index d06f21c89c4a..6861b3709394 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -8,7 +8,7 @@ use rustc_span::Span; declare_clippy_lint! { /// ### What it does /// - /// For () returning expressions, check that the semicolon is inside the block. + /// Checks for semicolon terminated blocks containing only a single expression. /// /// ### Why is this bad? /// @@ -36,7 +36,7 @@ declare_clippy_lint! { declare_clippy_lint! { /// ### What it does /// - /// For () returning expressions, check that the semicolon is outside the block. + /// Checks for blocks containing only a single semicolon terminated statement. /// /// ### Why is this bad? /// From f62eab43123ac463d7ff95590c285cb30e86b1f7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 25 Nov 2022 17:10:05 +0100 Subject: [PATCH 019/321] Adjust description once more --- clippy_lints/src/semicolon_block.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index 6861b3709394..d3cab68137c4 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -8,7 +8,7 @@ use rustc_span::Span; declare_clippy_lint! { /// ### What it does /// - /// Checks for semicolon terminated blocks containing only a single expression. + /// Suggests moving the semicolon from a block inside of the block to its kast expression. /// /// ### Why is this bad? /// @@ -36,7 +36,7 @@ declare_clippy_lint! { declare_clippy_lint! { /// ### What it does /// - /// Checks for blocks containing only a single semicolon terminated statement. + /// Suggests moving the semicolon from a block's final expression outside of the block. /// /// ### Why is this bad? /// @@ -80,7 +80,7 @@ impl LateLintPass<'_> for SemicolonBlock { span, .. } = stmt else { return }; - semicolon_outside_block(cx, block, expr, span) + semicolon_outside_block(cx, block, expr, span); }, StmtKind::Semi(Expr { kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _), From 2c541786cf6f54d55b12dd649f322a4b7af5770c Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Mon, 28 Nov 2022 11:48:43 -0500 Subject: [PATCH 020/321] Implement DerefMut for PathBuf --- library/std/src/path.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index af88b9070c18..59d72e54736a 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -1724,6 +1724,14 @@ impl ops::Deref for PathBuf { } } +#[stable(feature = "path_buf_deref_mut", since = "CURRENT_RUSTC_VERSION")] +impl ops::DerefMut for PathBuf { + #[inline] + fn deref_mut(&mut self) -> &mut Path { + Path::from_inner_mut(&mut self.inner) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Borrow for PathBuf { #[inline] @@ -1976,6 +1984,12 @@ impl Path { unsafe { &*(s.as_ref() as *const OsStr as *const Path) } } + fn from_inner_mut(inner: &mut OsStr) -> &mut Path { + // SAFETY: Path is just a wrapper around OsStr, + // therefore converting &mut OsStr to &mut Path is safe. + unsafe { &mut *(inner as *mut OsStr as *mut Path) } + } + /// Yields the underlying [`OsStr`] slice. /// /// # Examples From 96f1385fdd75af35ea888eeb68dc3631d7637a2b Mon Sep 17 00:00:00 2001 From: naosense Date: Mon, 21 Nov 2022 14:36:56 +0800 Subject: [PATCH 021/321] add `suppress_lint_in_const` conf --- clippy_lints/src/indexing_slicing.rs | 19 ++++++++++++++++--- clippy_lints/src/lib.rs | 3 +++ clippy_lints/src/utils/conf.rs | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 4cd7dff4cfd7..23beeb4458c7 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -82,11 +82,24 @@ declare_clippy_lint! { "indexing/slicing usage" } +#[derive(Copy, Clone)] +pub struct IndexingSlicing { + suppress_lint_in_const: bool, +} + +impl IndexingSlicing { + pub fn new(suppress_lint_in_const: bool) -> Self { + Self { + suppress_lint_in_const, + } + } +} + declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if cx.tcx.hir().is_inside_const_context(expr.hir_id) { + if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) { return; } @@ -146,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind() { // Index is a const block. - if let ExprKind::ConstBlock(..) = index.kind { + if self.suppress_lint_in_const && let ExprKind::ConstBlock(..) = index.kind { return; } // Index is a constant uint. @@ -191,7 +204,7 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1 } else { Some(x) } - }, + } Some(_) => None, None => Some(array_size), }; diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3fe39488ab82..3bac394582e0 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -562,7 +562,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; let allow_expect_in_tests = conf.allow_expect_in_tests; let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; + let suppress_lint_in_const = conf.suppress_lint_in_const; store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv()))); + store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv))); store.register_late_pass(move |_| { Box::new(methods::Methods::new( avoid_breaking_exported_api, @@ -684,6 +686,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd)); store.register_late_pass(|_| Box::new(unwrap::Unwrap)); store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing)); + store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst)); store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast)); store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index b6dc8cd7ab11..1352632b3a65 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -406,6 +406,10 @@ define_Conf! { /// /// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)` (allow_mixed_uninlined_format_args: bool = true), + /// Lint: SUPPRESS_LINT + /// + /// Whether to suppress lint in const function + (suppress_lint_in_const: bool = true), } /// Search for the configuration file. From aed9497978ce0520147cf1cf100513bf049ddf50 Mon Sep 17 00:00:00 2001 From: naosense Date: Tue, 22 Nov 2022 17:12:50 +0800 Subject: [PATCH 022/321] add test and stderr --- clippy_lints/src/indexing_slicing.rs | 12 +++++------- clippy_lints/src/lib.rs | 3 +-- clippy_lints/src/utils/conf.rs | 2 +- tests/ui-toml/suppress_lint_in_const/clippy.toml | 1 + tests/ui-toml/suppress_lint_in_const/test.rs | 15 +++++++++++++++ tests/ui-toml/suppress_lint_in_const/test.stderr | 11 +++++++++++ .../toml_unknown_key/conf_unknown_key.stderr | 1 + 7 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 tests/ui-toml/suppress_lint_in_const/clippy.toml create mode 100644 tests/ui-toml/suppress_lint_in_const/test.rs create mode 100644 tests/ui-toml/suppress_lint_in_const/test.stderr diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 23beeb4458c7..92facbbf0341 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -7,7 +7,7 @@ use rustc_ast::ast::RangeLimits; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { /// ### What it does @@ -82,6 +82,8 @@ declare_clippy_lint! { "indexing/slicing usage" } +impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); + #[derive(Copy, Clone)] pub struct IndexingSlicing { suppress_lint_in_const: bool, @@ -89,14 +91,10 @@ pub struct IndexingSlicing { impl IndexingSlicing { pub fn new(suppress_lint_in_const: bool) -> Self { - Self { - suppress_lint_in_const, - } + Self { suppress_lint_in_const } } } -declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); - impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) { @@ -204,7 +202,7 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1 } else { Some(x) } - } + }, Some(_) => None, None => Some(array_size), }; diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3bac394582e0..3b5bac644ef5 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -685,8 +685,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(inherent_impl::MultipleInherentImpl)); store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd)); store.register_late_pass(|_| Box::new(unwrap::Unwrap)); - store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing)); - store.register_late_pass(|_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); + store.register_late_pass(move |_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst)); store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast)); store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 1352632b3a65..4a0e135db835 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -406,7 +406,7 @@ define_Conf! { /// /// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)` (allow_mixed_uninlined_format_args: bool = true), - /// Lint: SUPPRESS_LINT + /// Lint: INDEXING_SLICING /// /// Whether to suppress lint in const function (suppress_lint_in_const: bool = true), diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml new file mode 100644 index 000000000000..fd459ff5ac54 --- /dev/null +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -0,0 +1 @@ +suppress-lint-in-const = false \ No newline at end of file diff --git a/tests/ui-toml/suppress_lint_in_const/test.rs b/tests/ui-toml/suppress_lint_in_const/test.rs new file mode 100644 index 000000000000..e5f4ca7cc902 --- /dev/null +++ b/tests/ui-toml/suppress_lint_in_const/test.rs @@ -0,0 +1,15 @@ +#![warn(clippy::indexing_slicing)] + +/// An opaque integer representation +pub struct Integer<'a> { + /// The underlying data + value: &'a [u8], +} +impl<'a> Integer<'a> { + // Check whether `self` holds a negative number or not + pub const fn is_negative(&self) -> bool { + self.value[0] & 0b1000_0000 != 0 + } +} + +fn main() {} diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr new file mode 100644 index 000000000000..b4f6fe0c024b --- /dev/null +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -0,0 +1,11 @@ +error: indexing may panic + --> $DIR/test.rs:11:9 + | +LL | self.value[0] & 0b1000_0000 != 0 + | ^^^^^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + = note: `-D clippy::indexing-slicing` implied by `-D warnings` + +error: aborting due to previous error + diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 01a5e962c949..521af13fe03d 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -35,6 +35,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie pass-by-value-size-limit single-char-binding-names-threshold standard-macro-braces + suppress-lint-in-const third-party too-large-for-stack too-many-arguments-threshold From 4528aec7e9a4fdce65a5dfec3a06aad5ee1950dd Mon Sep 17 00:00:00 2001 From: naosense Date: Wed, 23 Nov 2022 16:26:25 +0800 Subject: [PATCH 023/321] update config and suggest --- clippy_lints/src/indexing_slicing.rs | 45 ++++++++++++------ clippy_lints/src/lib.rs | 8 +++- clippy_lints/src/utils/conf.rs | 8 +++- .../suppress_lint_in_const/clippy.toml | 2 +- .../suppress_lint_in_const/test.stderr | 3 +- .../toml_unknown_key/conf_unknown_key.stderr | 2 +- tests/ui/indexing_slicing_index.stderr | 23 +++------ tests/ui/indexing_slicing_slice.stderr | 47 +++++-------------- 8 files changed, 63 insertions(+), 75 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 92facbbf0341..dfea0bf18d10 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -1,9 +1,10 @@ //! lint on indexing and slicing operations use clippy_utils::consts::{constant, Constant}; -use clippy_utils::diagnostics::{span_lint, span_lint_and_help}; +use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; use clippy_utils::higher; use rustc_ast::ast::RangeLimits; +use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; @@ -86,18 +87,20 @@ impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]); #[derive(Copy, Clone)] pub struct IndexingSlicing { - suppress_lint_in_const: bool, + suppress_restriction_lint_in_const: bool, } impl IndexingSlicing { - pub fn new(suppress_lint_in_const: bool) -> Self { - Self { suppress_lint_in_const } + pub fn new(suppress_restriction_lint_in_const: bool) -> Self { + Self { + suppress_restriction_lint_in_const, + } } } impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if self.suppress_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) { + if self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) { return; } @@ -152,12 +155,19 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { (None, None) => return, // [..] is ok. }; - span_lint_and_help(cx, INDEXING_SLICING, expr.span, "slicing may panic", None, help_msg); + span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| { + let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) { + "the suggestion might not be applicable in constant blocks" + } else { + "" + }; + diag.span_suggestion(expr.span, help_msg, note, Applicability::MachineApplicable); + }); } else { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind() { // Index is a const block. - if self.suppress_lint_in_const && let ExprKind::ConstBlock(..) = index.kind { + if self.suppress_restriction_lint_in_const && let ExprKind::ConstBlock(..) = index.kind { return; } // Index is a constant uint. @@ -167,14 +177,19 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { } } - span_lint_and_help( - cx, - INDEXING_SLICING, - expr.span, - "indexing may panic", - None, - "consider using `.get(n)` or `.get_mut(n)` instead", - ); + span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| { + let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) { + "the suggestion might not be applicable in constant blocks" + } else { + "" + }; + diag.span_suggestion( + expr.span, + "consider using `.get(n)` or `.get_mut(n)` instead", + note, + Applicability::MachineApplicable, + ); + }); } } } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3b5bac644ef5..2dc3a0822774 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -562,7 +562,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; let allow_expect_in_tests = conf.allow_expect_in_tests; let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; - let suppress_lint_in_const = conf.suppress_lint_in_const; + let suppress_restriction_lint_in_const = conf.suppress_restriction_lint_in_const; store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv()))); store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv))); store.register_late_pass(move |_| { @@ -685,7 +685,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(inherent_impl::MultipleInherentImpl)); store.register_late_pass(|_| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd)); store.register_late_pass(|_| Box::new(unwrap::Unwrap)); - store.register_late_pass(move |_| Box::new(indexing_slicing::IndexingSlicing::new(suppress_lint_in_const))); + store.register_late_pass(move |_| { + Box::new(indexing_slicing::IndexingSlicing::new( + suppress_restriction_lint_in_const, + )) + }); store.register_late_pass(|_| Box::new(non_copy_const::NonCopyConst)); store.register_late_pass(|_| Box::new(ptr_offset_with_cast::PtrOffsetWithCast)); store.register_late_pass(|_| Box::new(redundant_clone::RedundantClone)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 4a0e135db835..0022044ea417 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -408,8 +408,12 @@ define_Conf! { (allow_mixed_uninlined_format_args: bool = true), /// Lint: INDEXING_SLICING /// - /// Whether to suppress lint in const function - (suppress_lint_in_const: bool = true), + /// Whether to suppress a restriction lint in constant code. In same + /// cases the restructured operation might not be unavoidable, as the + /// suggested counterparts are unavailable in constant code. This + /// configuration will cause restriction lints to trigger even + /// if no suggestion can be made. + (suppress_restriction_lint_in_const: bool = true), } /// Search for the configuration file. diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml index fd459ff5ac54..d458f53a73dd 100644 --- a/tests/ui-toml/suppress_lint_in_const/clippy.toml +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -1 +1 @@ -suppress-lint-in-const = false \ No newline at end of file +suppress-restriction-lint-in-const = false \ No newline at end of file diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr index b4f6fe0c024b..4e4583ab33cf 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.stderr +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -2,9 +2,8 @@ error: indexing may panic --> $DIR/test.rs:11:9 | LL | self.value[0] & 0b1000_0000 != 0 - | ^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead: `the suggestion might not be applicable in constant blocks` | - = help: consider using `.get(n)` or `.get_mut(n)` instead = note: `-D clippy::indexing-slicing` implied by `-D warnings` error: aborting due to previous error diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 521af13fe03d..6ef2abb15b28 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -35,7 +35,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie pass-by-value-size-limit single-char-binding-names-threshold standard-macro-braces - suppress-lint-in-const + suppress-restriction-lint-in-const third-party too-large-for-stack too-many-arguments-threshold diff --git a/tests/ui/indexing_slicing_index.stderr b/tests/ui/indexing_slicing_index.stderr index d8b6e3f1262b..30fb69907958 100644 --- a/tests/ui/indexing_slicing_index.stderr +++ b/tests/ui/indexing_slicing_index.stderr @@ -14,50 +14,39 @@ error: indexing may panic --> $DIR/indexing_slicing_index.rs:22:5 | LL | x[index]; - | ^^^^^^^^ + | ^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead | - = help: consider using `.get(n)` or `.get_mut(n)` instead = note: `-D clippy::indexing-slicing` implied by `-D warnings` error: indexing may panic --> $DIR/indexing_slicing_index.rs:38:5 | LL | v[0]; - | ^^^^ - | - = help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:39:5 | LL | v[10]; - | ^^^^^ - | - = help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:40:5 | LL | v[1 << 3]; - | ^^^^^^^^^ - | - = help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:46:5 | LL | v[N]; - | ^^^^ - | - = help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:47:5 | LL | v[M]; - | ^^^^ - | - = help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead error[E0080]: evaluation of constant value failed --> $DIR/indexing_slicing_index.rs:10:24 diff --git a/tests/ui/indexing_slicing_slice.stderr b/tests/ui/indexing_slicing_slice.stderr index dc54bd41365d..ac2cc009a22a 100644 --- a/tests/ui/indexing_slicing_slice.stderr +++ b/tests/ui/indexing_slicing_slice.stderr @@ -2,50 +2,39 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:12:6 | LL | &x[index..]; - | ^^^^^^^^^^ + | ^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead | - = help: consider using `.get(n..)` or .get_mut(n..)` instead = note: `-D clippy::indexing-slicing` implied by `-D warnings` error: slicing may panic --> $DIR/indexing_slicing_slice.rs:13:6 | LL | &x[..index]; - | ^^^^^^^^^^ - | - = help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:14:6 | LL | &x[index_from..index_to]; - | ^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead + | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:15:6 | LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to]. - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:15:6 | LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to]. - | ^^^^^^^^^^^^^^^ - | - = help: consider using `.get(n..)` or .get_mut(n..)` instead + | ^^^^^^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:16:6 | LL | &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10]. - | ^^^^^^^^^^^^ - | - = help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead error: range is out of bounds --> $DIR/indexing_slicing_slice.rs:16:8 @@ -59,17 +48,13 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:17:6 | LL | &x[0..][..3]; - | ^^^^^^^^^^^ - | - = help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:18:6 | LL | &x[1..][..5]; - | ^^^^^^^^^^^ - | - = help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead error: range is out of bounds --> $DIR/indexing_slicing_slice.rs:25:12 @@ -87,17 +72,13 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:31:6 | LL | &v[10..100]; - | ^^^^^^^^^^ - | - = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead + | ^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:32:6 | LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100]. - | ^^^^^^^^^^^^^^ - | - = help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead error: range is out of bounds --> $DIR/indexing_slicing_slice.rs:32:8 @@ -109,17 +90,13 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:33:6 | LL | &v[10..]; - | ^^^^^^^ - | - = help: consider using `.get(n..)` or .get_mut(n..)` instead + | ^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:34:6 | LL | &v[..100]; - | ^^^^^^^^ - | - = help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead error: aborting due to 16 previous errors From c9bf4b75cea2259ad927228c762d4cf323628ae5 Mon Sep 17 00:00:00 2001 From: naosense Date: Wed, 23 Nov 2022 16:43:42 +0800 Subject: [PATCH 024/321] resolve conflicts --- clippy_lints/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 2dc3a0822774..5ce70de88104 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -564,7 +564,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; let suppress_restriction_lint_in_const = conf.suppress_restriction_lint_in_const; store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv()))); - store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv))); store.register_late_pass(move |_| { Box::new(methods::Methods::new( avoid_breaking_exported_api, From 1fc98c51dfd771e5cd45c2feee0649ae299ab18b Mon Sep 17 00:00:00 2001 From: naosense Date: Wed, 23 Nov 2022 16:50:05 +0800 Subject: [PATCH 025/321] change default value --- clippy_lints/src/utils/conf.rs | 2 +- tests/ui-toml/suppress_lint_in_const/clippy.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 0022044ea417..f5f0e3ef48cf 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -413,7 +413,7 @@ define_Conf! { /// suggested counterparts are unavailable in constant code. This /// configuration will cause restriction lints to trigger even /// if no suggestion can be made. - (suppress_restriction_lint_in_const: bool = true), + (suppress_restriction_lint_in_const: bool = false), } /// Search for the configuration file. diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml index d458f53a73dd..d6b6fc7f2688 100644 --- a/tests/ui-toml/suppress_lint_in_const/clippy.toml +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -1 +1 @@ -suppress-restriction-lint-in-const = false \ No newline at end of file +suppress-restriction-lint-in-const = false From 67a94135cbce195078efcbf18ac2347a1d734e68 Mon Sep 17 00:00:00 2001 From: naosense Date: Thu, 24 Nov 2022 09:43:11 +0800 Subject: [PATCH 026/321] change note style --- clippy_lints/src/indexing_slicing.rs | 29 +++---- .../suppress_lint_in_const/test.stderr | 4 +- tests/ui/indexing_slicing_index.stderr | 79 +++++++++++++++++-- tests/ui/indexing_slicing_slice.stderr | 47 ++++++++--- 4 files changed, 120 insertions(+), 39 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index dfea0bf18d10..1a8ac43ac894 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -4,7 +4,6 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; use clippy_utils::higher; use rustc_ast::ast::RangeLimits; -use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; @@ -105,6 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { } if let ExprKind::Index(array, index) = &expr.kind { + let note = "the suggestion might not be applicable in constant blocks"; let ty = cx.typeck_results().expr_ty(array).peel_refs(); if let Some(range) = higher::Range::hir(index) { // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..] @@ -156,12 +156,11 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { }; span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| { - let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) { - "the suggestion might not be applicable in constant blocks" - } else { - "" - }; - diag.span_suggestion(expr.span, help_msg, note, Applicability::MachineApplicable); + diag.help(help_msg); + + if cx.tcx.hir().is_inside_const_context(expr.hir_id) { + diag.note(note); + } }); } else { // Catchall non-range index, i.e., [n] or [n << m] @@ -178,17 +177,11 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { } span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| { - let note = if cx.tcx.hir().is_inside_const_context(expr.hir_id) { - "the suggestion might not be applicable in constant blocks" - } else { - "" - }; - diag.span_suggestion( - expr.span, - "consider using `.get(n)` or `.get_mut(n)` instead", - note, - Applicability::MachineApplicable, - ); + diag.help("consider using `.get(n)` or `.get_mut(n)` instead"); + + if cx.tcx.hir().is_inside_const_context(expr.hir_id) { + diag.note(note); + } }); } } diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr index 4e4583ab33cf..c2acfed559d0 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.stderr +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -2,8 +2,10 @@ error: indexing may panic --> $DIR/test.rs:11:9 | LL | self.value[0] & 0b1000_0000 != 0 - | ^^^^^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead: `the suggestion might not be applicable in constant blocks` + | ^^^^^^^^^^^^^ | + = help: consider using `.get(n)` or `.get_mut(n)` instead + = note: the suggestion might not be applicable in constant blocks = note: `-D clippy::indexing-slicing` implied by `-D warnings` error: aborting due to previous error diff --git a/tests/ui/indexing_slicing_index.stderr b/tests/ui/indexing_slicing_index.stderr index 30fb69907958..84e1f65623c3 100644 --- a/tests/ui/indexing_slicing_index.stderr +++ b/tests/ui/indexing_slicing_index.stderr @@ -1,3 +1,22 @@ +error: indexing may panic + --> $DIR/indexing_slicing_index.rs:9:20 + | +LL | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr. + | ^^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + = note: the suggestion might not be applicable in constant blocks + = note: `-D clippy::indexing-slicing` implied by `-D warnings` + +error: indexing may panic + --> $DIR/indexing_slicing_index.rs:10:24 + | +LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. + | ^^^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + = note: the suggestion might not be applicable in constant blocks + error[E0080]: evaluation of `main::{constant#3}` failed --> $DIR/indexing_slicing_index.rs:31:14 | @@ -14,39 +33,83 @@ error: indexing may panic --> $DIR/indexing_slicing_index.rs:22:5 | LL | x[index]; - | ^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^^^^^ | - = note: `-D clippy::indexing-slicing` implied by `-D warnings` + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/indexing_slicing_index.rs:28:5 + | +LL | x[const { idx() }]; // Ok, should not produce stderr. + | ^^^^^^^^^^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/indexing_slicing_index.rs:29:5 + | +LL | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + | ^^^^^^^^^^^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/indexing_slicing_index.rs:30:14 + | +LL | const { &ARR[idx()] }; // Ok, should not produce stderr. + | ^^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + = note: the suggestion might not be applicable in constant blocks + +error: indexing may panic + --> $DIR/indexing_slicing_index.rs:31:14 + | +LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. + | ^^^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + = note: the suggestion might not be applicable in constant blocks error: indexing may panic --> $DIR/indexing_slicing_index.rs:38:5 | LL | v[0]; - | ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:39:5 | LL | v[10]; - | ^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:40:5 | LL | v[1 << 3]; - | ^^^^^^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:46:5 | LL | v[N]; - | ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead error: indexing may panic --> $DIR/indexing_slicing_index.rs:47:5 | LL | v[M]; - | ^^^^ help: consider using `.get(n)` or `.get_mut(n)` instead + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead error[E0080]: evaluation of constant value failed --> $DIR/indexing_slicing_index.rs:10:24 @@ -54,6 +117,6 @@ error[E0080]: evaluation of constant value failed LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. | ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 -error: aborting due to 8 previous errors +error: aborting due to 14 previous errors For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/indexing_slicing_slice.stderr b/tests/ui/indexing_slicing_slice.stderr index ac2cc009a22a..dc54bd41365d 100644 --- a/tests/ui/indexing_slicing_slice.stderr +++ b/tests/ui/indexing_slicing_slice.stderr @@ -2,39 +2,50 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:12:6 | LL | &x[index..]; - | ^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead + | ^^^^^^^^^^ | + = help: consider using `.get(n..)` or .get_mut(n..)` instead = note: `-D clippy::indexing-slicing` implied by `-D warnings` error: slicing may panic --> $DIR/indexing_slicing_slice.rs:13:6 | LL | &x[..index]; - | ^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^ + | + = help: consider using `.get(..n)`or `.get_mut(..n)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:14:6 | LL | &x[index_from..index_to]; - | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:15:6 | LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to]. - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider using `.get(..n)`or `.get_mut(..n)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:15:6 | LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to]. - | ^^^^^^^^^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead + | ^^^^^^^^^^^^^^^ + | + = help: consider using `.get(n..)` or .get_mut(n..)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:16:6 | LL | &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10]. - | ^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^^ + | + = help: consider using `.get(..n)`or `.get_mut(..n)` instead error: range is out of bounds --> $DIR/indexing_slicing_slice.rs:16:8 @@ -48,13 +59,17 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:17:6 | LL | &x[0..][..3]; - | ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^ + | + = help: consider using `.get(..n)`or `.get_mut(..n)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:18:6 | LL | &x[1..][..5]; - | ^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^ + | + = help: consider using `.get(..n)`or `.get_mut(..n)` instead error: range is out of bounds --> $DIR/indexing_slicing_slice.rs:25:12 @@ -72,13 +87,17 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:31:6 | LL | &v[10..100]; - | ^^^^^^^^^^ help: consider using `.get(n..m)` or `.get_mut(n..m)` instead + | ^^^^^^^^^^ + | + = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:32:6 | LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100]. - | ^^^^^^^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^^^^^^^ + | + = help: consider using `.get(..n)`or `.get_mut(..n)` instead error: range is out of bounds --> $DIR/indexing_slicing_slice.rs:32:8 @@ -90,13 +109,17 @@ error: slicing may panic --> $DIR/indexing_slicing_slice.rs:33:6 | LL | &v[10..]; - | ^^^^^^^ help: consider using `.get(n..)` or .get_mut(n..)` instead + | ^^^^^^^ + | + = help: consider using `.get(n..)` or .get_mut(n..)` instead error: slicing may panic --> $DIR/indexing_slicing_slice.rs:34:6 | LL | &v[..100]; - | ^^^^^^^^ help: consider using `.get(..n)`or `.get_mut(..n)` instead + | ^^^^^^^^ + | + = help: consider using `.get(..n)`or `.get_mut(..n)` instead error: aborting due to 16 previous errors From dc93a28e98e6bdcb73441ec699f43aa01d10e4e6 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 29 Nov 2022 13:42:52 +0100 Subject: [PATCH 027/321] reduce allocations --- .../src/build/expr/as_place.rs | 147 +++++++++++++----- 1 file changed, 110 insertions(+), 37 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index bd27b6c2bebc..3f52334b7247 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -218,7 +218,7 @@ fn to_upvars_resolved_place_builder<'tcx>( let upvar_resolved_place_builder = PlaceBuilder::construct_local_place_builder( cx, upvar_resolved_local, - local_projection.to_vec(), + local_projection.as_slice(), upvar_projection, ); @@ -266,7 +266,7 @@ fn strip_prefix<'a, 'tcx>( mut base_ty: Ty<'tcx>, projections: &'a [UpvarProjectionElem<'tcx>], prefix_projections: &[HirProjection<'tcx>], -) -> Vec> { +) -> impl Iterator> + 'a { let mut iter = projections .iter() // Filter out opaque casts, they are unnecessary in the prefix. @@ -293,7 +293,7 @@ fn strip_prefix<'a, 'tcx>( base_ty = projection.ty; } - iter.collect::>() + iter } impl<'tcx> PlaceBuilder<'tcx> { @@ -342,10 +342,14 @@ impl<'tcx> PlaceBuilder<'tcx> { #[instrument(skip(cx), level = "debug")] pub(crate) fn field(self, cx: &Builder<'_, 'tcx>, f: Field) -> Self { - let field_ty = match self { - PlaceBuilder::Local(..) => { - let base_place = self.clone(); - PlaceBuilder::compute_field_ty(cx, f, base_place) + let field_ty = match self.clone() { + PlaceBuilder::Local(local, projection) => { + let base_place = PlaceBuilder::Local(local, projection); + let PlaceTy { ty, variant_index } = + base_place.to_place(cx).ty(&cx.local_decls, cx.tcx); + let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty); + + PlaceBuilder::compute_field_ty(cx, f, base_ty, variant_index) } PlaceBuilder::UpVar(..) => { let dummy_ty = cx.tcx.mk_ty_infer(ty::FreshTy(0)); @@ -410,13 +414,10 @@ impl<'tcx> PlaceBuilder<'tcx> { fn compute_field_ty( cx: &Builder<'_, 'tcx>, field: Field, - base_place: PlaceBuilder<'tcx>, + base_ty: Ty<'tcx>, + variant_index: Option, ) -> Ty<'tcx> { let field_idx = field.as_usize(); - let PlaceTy { ty, variant_index } = base_place.to_place(cx).ty(&cx.local_decls, cx.tcx); - let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty); - debug!(?base_ty); - let field_ty = match base_ty.kind() { ty::Adt(adt_def, substs) if adt_def.is_enum() => { let variant_idx = variant_index.unwrap(); @@ -479,41 +480,38 @@ impl<'tcx> PlaceBuilder<'tcx> { /// contains the projections of the captured upvar and `upvar_projection` the /// projections that are applied to the captured upvar. The main purpose of this /// function is to figure out the `Ty`s of the field projections in `upvar_projection`. - #[instrument(skip(cx, local))] + #[instrument(skip(cx, local, upvar_projection))] fn construct_local_place_builder( cx: &Builder<'_, 'tcx>, local: Local, - mut local_projection: Vec>, - upvar_projection: Vec>, + local_projection: &[PlaceElem<'tcx>], + upvar_projection: impl Iterator>, ) -> Self { - // We iterate through `upvar_projection` and whenever we find a `ProjectionElem::Field` we use - // the ancestor projections, i.e. those projection elements that come before the field projection, - // to get the `Ty` for the field. + // We maintain a `Ty` to which we apply a projection in each iteration over `upvar_projection`. + // This `ancestor_ty` let's us infer the field type whenever we encounter a + // `ProjectionElem::Field`. + let (mut ancestor_ty, mut opt_variant_idx) = + local_projections_to_ty(cx, local, local_projection); - for proj in upvar_projection.iter() { - debug!("proj: {:?}, local_projection: {:?}", proj, local_projection); - match *proj { + // We add all projection elements we encounter to this `Vec`. + let mut local_projection = local_projection.to_vec(); + + for (i, proj) in upvar_projection.enumerate() { + debug!("i: {:?}, proj: {:?}, local_projection: {:?}", i, proj, local_projection); + match proj { ProjectionElem::Field(field, _) => { - let ancestor_proj = local_projection.to_vec(); - let base_place = PlaceBuilder::Local(local, ancestor_proj); - let field_ty = PlaceBuilder::compute_field_ty(cx, field, base_place); + let field_ty = + PlaceBuilder::compute_field_ty(cx, field, ancestor_ty, opt_variant_idx); debug!(?field_ty); local_projection.push(ProjectionElem::Field(field, field_ty)); - debug!(?local_projection); + ancestor_ty = field_ty; + opt_variant_idx = None; } - ProjectionElem::Deref => local_projection.push(ProjectionElem::Deref), - ProjectionElem::Index(idx) => local_projection.push(ProjectionElem::Index(idx)), - ProjectionElem::ConstantIndex { offset, min_length, from_end } => local_projection - .push(ProjectionElem::ConstantIndex { offset, min_length, from_end }), - ProjectionElem::Subslice { from, to, from_end } => { - local_projection.push(ProjectionElem::Subslice { from, to, from_end }) - } - ProjectionElem::Downcast(sym, variant_idx) => { - local_projection.push(ProjectionElem::Downcast(sym, variant_idx)) - } - ProjectionElem::OpaqueCast(ty) => { - local_projection.push(ProjectionElem::OpaqueCast(ty)) + _ => { + let proj = upvar_proj_to_place_elem_no_field_proj(proj); + (ancestor_ty, opt_variant_idx) = project_ty(cx.tcx, ancestor_ty, proj); + local_projection.push(proj); } } } @@ -534,6 +532,81 @@ impl<'tcx> From> for PlaceBuilder<'tcx> { } } +fn project_ty<'tcx>( + tcx: TyCtxt<'tcx>, + ty: Ty<'tcx>, + elem: PlaceElem<'tcx>, +) -> (Ty<'tcx>, Option) { + match elem { + ProjectionElem::Deref => { + let updated_ty = ty + .builtin_deref(true) + .unwrap_or_else(|| bug!("deref projection of non-dereferenceable ty {:?}", ty)) + .ty; + + (updated_ty, None) + } + ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => { + (ty.builtin_index().unwrap(), None) + } + ProjectionElem::Subslice { from, to, from_end } => { + let ty = match ty.kind() { + ty::Slice(..) => ty, + ty::Array(inner, _) if !from_end => tcx.mk_array(*inner, (to - from) as u64), + ty::Array(inner, size) if from_end => { + let size = size.eval_usize(tcx, ty::ParamEnv::empty()); + let len = size - (from as u64) - (to as u64); + tcx.mk_array(*inner, len) + } + _ => bug!("cannot subslice non-array type: `{:?}`", ty), + }; + + (ty, None) + } + ProjectionElem::Downcast(_, variant_idx) => (ty, Some(variant_idx)), + ProjectionElem::Field(_, ty) => { + if matches!(ty.kind(), ty::Infer(..)) { + bug!("Field ty should have been resolved"); + } + + (ty, None) + } + ProjectionElem::OpaqueCast(..) => bug!("didn't expect OpaqueCast"), + } +} + +fn local_projections_to_ty<'a, 'tcx>( + cx: &'a Builder<'a, 'tcx>, + local: Local, + projection: &'a [PlaceElem<'tcx>], +) -> (Ty<'tcx>, Option) { + let local_ty = cx.local_decls.local_decls()[local].ty; + projection.iter().fold((local_ty, None), |ty_variant_idx, elem| { + let ty = ty_variant_idx.0; + project_ty(cx.tcx, ty, *elem) + }) +} + +// Converts an `UpvarProjectionElem` to `PlaceElem`, ICE'ing when being passed a +// field projection. +fn upvar_proj_to_place_elem_no_field_proj<'tcx>( + upvar_proj: UpvarProjectionElem<'tcx>, +) -> PlaceElem<'tcx> { + match upvar_proj { + ProjectionElem::Deref => ProjectionElem::Deref, + ProjectionElem::Index(i) => ProjectionElem::Index(i), + ProjectionElem::ConstantIndex { offset, min_length, from_end } => { + ProjectionElem::ConstantIndex { offset, min_length, from_end } + } + ProjectionElem::Subslice { from, to, from_end } => { + ProjectionElem::Subslice { from, to, from_end } + } + ProjectionElem::Downcast(ty, variant_idx) => ProjectionElem::Downcast(ty, variant_idx), + ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), + ProjectionElem::Field(..) => bug!("should not be called with `ProjectionElem::Field`"), + } +} + impl<'a, 'tcx> Builder<'a, 'tcx> { /// Compile `expr`, yielding a place that we can move from etc. /// From 7a0f0c0e8b79d0dd95e0c97660e327e51fa09c0f Mon Sep 17 00:00:00 2001 From: mdgaziur Date: Sat, 26 Nov 2022 12:17:49 +0600 Subject: [PATCH 028/321] Fix #9958 --- clippy_lints/src/len_zero.rs | 20 +++++++--- tests/ui/len_zero.fixed | 40 +++++++++++++++++++ tests/ui/len_zero.rs | 40 +++++++++++++++++++ tests/ui/len_zero.stderr | 74 ++++++++++++++++++++++++++++-------- 4 files changed, 153 insertions(+), 21 deletions(-) diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index 4c133c06a157..3babc2a58959 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -1,13 +1,13 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{get_item_name, get_parent_as_impl, is_lint_allowed}; +use clippy_utils::{get_item_name, get_parent_as_impl, is_lint_allowed, peel_ref_operators}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; use rustc_hir::def_id::DefIdSet; use rustc_hir::{ def_id::DefId, AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, ImplItem, ImplItemKind, ImplicitSelfKind, Item, - ItemKind, Mutability, Node, TraitItemRef, TyKind, + ItemKind, Mutability, Node, TraitItemRef, TyKind, UnOp, }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, AssocKind, FnSig, Ty}; @@ -16,6 +16,7 @@ use rustc_span::{ source_map::{Span, Spanned, Symbol}, symbol::sym, }; +use std::borrow::Cow; declare_clippy_lint! { /// ### What it does @@ -428,16 +429,23 @@ fn check_len( fn check_empty_expr(cx: &LateContext<'_>, span: Span, lit1: &Expr<'_>, lit2: &Expr<'_>, op: &str) { if (is_empty_array(lit2) || is_empty_string(lit2)) && has_is_empty(cx, lit1) { let mut applicability = Applicability::MachineApplicable; + + let lit1 = peel_ref_operators(cx, lit1); + let mut lit_str = snippet_with_applicability(cx, lit1.span, "_", &mut applicability); + + // Wrap the expression in parentheses if it's a deref expression. Otherwise operator precedence will + // cause the code to dereference boolean(won't compile). + if let ExprKind::Unary(UnOp::Deref, _) = lit1.kind { + lit_str = Cow::from(format!("({lit_str})")); + } + span_lint_and_sugg( cx, COMPARISON_TO_EMPTY, span, "comparison to empty slice", &format!("using `{op}is_empty` is clearer and more explicit"), - format!( - "{op}{}.is_empty()", - snippet_with_applicability(cx, lit1.span, "_", &mut applicability) - ), + format!("{op}{lit_str}.is_empty()"), applicability, ); } diff --git a/tests/ui/len_zero.fixed b/tests/ui/len_zero.fixed index 1f3b8ac99b19..c1c0b5ae40f6 100644 --- a/tests/ui/len_zero.fixed +++ b/tests/ui/len_zero.fixed @@ -3,6 +3,9 @@ #![warn(clippy::len_zero)] #![allow(dead_code, unused, clippy::len_without_is_empty)] +extern crate core; +use core::ops::Deref; + pub struct One; struct Wither; @@ -56,6 +59,26 @@ impl WithIsEmpty for Wither { } } +struct DerefToDerefToString; + +impl Deref for DerefToDerefToString { + type Target = DerefToString; + + fn deref(&self) -> &Self::Target { + &DerefToString {} + } +} + +struct DerefToString; + +impl Deref for DerefToString { + type Target = str; + + fn deref(&self) -> &Self::Target { + "Hello, world!" + } +} + fn main() { let x = [1, 2]; if x.is_empty() { @@ -64,6 +87,23 @@ fn main() { if "".is_empty() {} + let s = "Hello, world!"; + let s1 = &s; + let s2 = &s1; + let s3 = &s2; + let s4 = &s3; + let s5 = &s4; + let s6 = &s5; + println!("{}", s1.is_empty()); + println!("{}", s2.is_empty()); + println!("{}", s3.is_empty()); + println!("{}", s4.is_empty()); + println!("{}", s5.is_empty()); + println!("{}", (s6).is_empty()); + + let d2s = DerefToDerefToString {}; + println!("{}", (**d2s).is_empty()); + let y = One; if y.len() == 0 { // No error; `One` does not have `.is_empty()`. diff --git a/tests/ui/len_zero.rs b/tests/ui/len_zero.rs index dc21de0001b6..cc2eb05b6bfd 100644 --- a/tests/ui/len_zero.rs +++ b/tests/ui/len_zero.rs @@ -3,6 +3,9 @@ #![warn(clippy::len_zero)] #![allow(dead_code, unused, clippy::len_without_is_empty)] +extern crate core; +use core::ops::Deref; + pub struct One; struct Wither; @@ -56,6 +59,26 @@ impl WithIsEmpty for Wither { } } +struct DerefToDerefToString; + +impl Deref for DerefToDerefToString { + type Target = DerefToString; + + fn deref(&self) -> &Self::Target { + &DerefToString {} + } +} + +struct DerefToString; + +impl Deref for DerefToString { + type Target = str; + + fn deref(&self) -> &Self::Target { + "Hello, world!" + } +} + fn main() { let x = [1, 2]; if x.len() == 0 { @@ -64,6 +87,23 @@ fn main() { if "".len() == 0 {} + let s = "Hello, world!"; + let s1 = &s; + let s2 = &s1; + let s3 = &s2; + let s4 = &s3; + let s5 = &s4; + let s6 = &s5; + println!("{}", *s1 == ""); + println!("{}", **s2 == ""); + println!("{}", ***s3 == ""); + println!("{}", ****s4 == ""); + println!("{}", *****s5 == ""); + println!("{}", ******(s6) == ""); + + let d2s = DerefToDerefToString {}; + println!("{}", &**d2s == ""); + let y = One; if y.len() == 0 { // No error; `One` does not have `.is_empty()`. diff --git a/tests/ui/len_zero.stderr b/tests/ui/len_zero.stderr index 6c71f1beeac6..b6f13780253c 100644 --- a/tests/ui/len_zero.stderr +++ b/tests/ui/len_zero.stderr @@ -1,5 +1,5 @@ error: length comparison to zero - --> $DIR/len_zero.rs:61:8 + --> $DIR/len_zero.rs:84:8 | LL | if x.len() == 0 { | ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `x.is_empty()` @@ -7,82 +7,126 @@ LL | if x.len() == 0 { = note: `-D clippy::len-zero` implied by `-D warnings` error: length comparison to zero - --> $DIR/len_zero.rs:65:8 + --> $DIR/len_zero.rs:88:8 | LL | if "".len() == 0 {} | ^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `"".is_empty()` +error: comparison to empty slice + --> $DIR/len_zero.rs:97:20 + | +LL | println!("{}", *s1 == ""); + | ^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s1.is_empty()` + | + = note: `-D clippy::comparison-to-empty` implied by `-D warnings` + +error: comparison to empty slice + --> $DIR/len_zero.rs:98:20 + | +LL | println!("{}", **s2 == ""); + | ^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s2.is_empty()` + +error: comparison to empty slice + --> $DIR/len_zero.rs:99:20 + | +LL | println!("{}", ***s3 == ""); + | ^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s3.is_empty()` + +error: comparison to empty slice + --> $DIR/len_zero.rs:100:20 + | +LL | println!("{}", ****s4 == ""); + | ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s4.is_empty()` + +error: comparison to empty slice + --> $DIR/len_zero.rs:101:20 + | +LL | println!("{}", *****s5 == ""); + | ^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s5.is_empty()` + +error: comparison to empty slice + --> $DIR/len_zero.rs:102:20 + | +LL | println!("{}", ******(s6) == ""); + | ^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `(s6).is_empty()` + +error: comparison to empty slice + --> $DIR/len_zero.rs:105:20 + | +LL | println!("{}", &**d2s == ""); + | ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `(**d2s).is_empty()` + error: length comparison to zero - --> $DIR/len_zero.rs:80:8 + --> $DIR/len_zero.rs:120:8 | LL | if has_is_empty.len() == 0 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:83:8 + --> $DIR/len_zero.rs:123:8 | LL | if has_is_empty.len() != 0 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:86:8 + --> $DIR/len_zero.rs:126:8 | LL | if has_is_empty.len() > 0 { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:89:8 + --> $DIR/len_zero.rs:129:8 | LL | if has_is_empty.len() < 1 { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:92:8 + --> $DIR/len_zero.rs:132:8 | LL | if has_is_empty.len() >= 1 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:103:8 + --> $DIR/len_zero.rs:143:8 | LL | if 0 == has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:106:8 + --> $DIR/len_zero.rs:146:8 | LL | if 0 != has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:109:8 + --> $DIR/len_zero.rs:149:8 | LL | if 0 < has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:112:8 + --> $DIR/len_zero.rs:152:8 | LL | if 1 <= has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!has_is_empty.is_empty()` error: length comparison to one - --> $DIR/len_zero.rs:115:8 + --> $DIR/len_zero.rs:155:8 | LL | if 1 > has_is_empty.len() { | ^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:129:8 + --> $DIR/len_zero.rs:169:8 | LL | if with_is_empty.len() == 0 { | ^^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `with_is_empty.is_empty()` error: length comparison to zero - --> $DIR/len_zero.rs:142:8 + --> $DIR/len_zero.rs:182:8 | LL | if b.len() != 0 {} | ^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!b.is_empty()` -error: aborting due to 14 previous errors +error: aborting due to 21 previous errors From e66220f747e4823ca4fc771cc1c46f3fd2f6bc81 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Wed, 30 Nov 2022 15:55:10 +0000 Subject: [PATCH 029/321] Reduce the number of unstable features in tests --- .../no_std-alloc-error-handler-custom.rs | 57 +++++++------------ .../no_std-alloc-error-handler-default.rs | 47 +++++---------- 2 files changed, 34 insertions(+), 70 deletions(-) diff --git a/src/test/ui/allocator/no_std-alloc-error-handler-custom.rs b/src/test/ui/allocator/no_std-alloc-error-handler-custom.rs index 851da231a734..289262433909 100644 --- a/src/test/ui/allocator/no_std-alloc-error-handler-custom.rs +++ b/src/test/ui/allocator/no_std-alloc-error-handler-custom.rs @@ -7,9 +7,10 @@ // compile-flags:-C panic=abort // aux-build:helper.rs -#![feature(start, rustc_private, new_uninit, panic_info_message, lang_items)] +#![feature(rustc_private, lang_items)] #![feature(alloc_error_handler)] #![no_std] +#![no_main] extern crate alloc; extern crate libc; @@ -21,35 +22,30 @@ pub fn __aeabi_unwind_cpp_pr0() {} #[no_mangle] pub fn __aeabi_unwind_cpp_pr1() {} -use core::ptr::null_mut; -use core::alloc::{GlobalAlloc, Layout}; use alloc::boxed::Box; +use alloc::string::ToString; +use core::alloc::{GlobalAlloc, Layout}; +use core::ptr::null_mut; extern crate helper; struct MyAllocator; #[alloc_error_handler] -fn my_oom(layout: Layout) -> ! -{ +fn my_oom(layout: Layout) -> ! { use alloc::fmt::write; unsafe { let size = layout.size(); let mut s = alloc::string::String::new(); write(&mut s, format_args!("My OOM: failed to allocate {} bytes!\n", size)).unwrap(); - let s = s.as_str(); - libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); + libc::write(libc::STDERR_FILENO, s.as_ptr() as *const _, s.len()); libc::exit(0) } } unsafe impl GlobalAlloc for MyAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - if layout.size() < 4096 { - libc::malloc(layout.size()) as _ - } else { - null_mut() - } + if layout.size() < 4096 { libc::malloc(layout.size()) as _ } else { null_mut() } } unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} } @@ -60,26 +56,12 @@ static A: MyAllocator = MyAllocator; #[panic_handler] fn panic(panic_info: &core::panic::PanicInfo) -> ! { unsafe { - if let Some(s) = panic_info.payload().downcast_ref::<&str>() { - const PSTR: &str = "panic occurred: "; - const CR: &str = "\n"; - libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); - libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); - libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); - } - if let Some(args) = panic_info.message() { - let mut s = alloc::string::String::new(); - alloc::fmt::write(&mut s, *args).unwrap(); - let s = s.as_str(); - const PSTR: &str = "panic occurred: "; - const CR: &str = "\n"; - libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); - libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); - libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); - } else { - const PSTR: &str = "panic occurred\n"; - libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); - } + let s = panic_info.to_string(); + const PSTR: &str = "panic occurred: "; + const CR: &str = "\n"; + libc::write(libc::STDERR_FILENO, PSTR.as_ptr() as *const _, PSTR.len()); + libc::write(libc::STDERR_FILENO, s.as_ptr() as *const _, s.len()); + libc::write(libc::STDERR_FILENO, CR.as_ptr() as *const _, CR.len()); libc::exit(1) } } @@ -89,15 +71,14 @@ fn panic(panic_info: &core::panic::PanicInfo) -> ! { // in these libraries will refer to `rust_eh_personality` if LLVM can not *prove* the contents won't // unwind. So, for this test case we will define the symbol. #[lang = "eh_personality"] -extern fn rust_eh_personality() {} +extern "C" fn rust_eh_personality() {} -#[derive(Debug)] +#[derive(Default, Debug)] struct Page(#[allow(unused_tuple_struct_fields)] [[u64; 32]; 16]); -#[start] -pub fn main(_argc: isize, _argv: *const *const u8) -> isize { - let zero = Box::::new_zeroed(); - let zero = unsafe { zero.assume_init() }; +#[no_mangle] +fn main(_argc: i32, _argv: *const *const u8) -> isize { + let zero = Box::::new(Default::default()); helper::work_with(&zero); 1 } diff --git a/src/test/ui/allocator/no_std-alloc-error-handler-default.rs b/src/test/ui/allocator/no_std-alloc-error-handler-default.rs index 601fda87b910..56409e713391 100644 --- a/src/test/ui/allocator/no_std-alloc-error-handler-default.rs +++ b/src/test/ui/allocator/no_std-alloc-error-handler-default.rs @@ -7,8 +7,9 @@ // compile-flags:-C panic=abort // aux-build:helper.rs -#![feature(start, rustc_private, new_uninit, panic_info_message, lang_items)] +#![feature(rustc_private, lang_items)] #![no_std] +#![no_main] extern crate alloc; extern crate libc; @@ -21,6 +22,7 @@ pub fn __aeabi_unwind_cpp_pr0() {} pub fn __aeabi_unwind_cpp_pr1() {} use alloc::boxed::Box; +use alloc::string::ToString; use core::alloc::{GlobalAlloc, Layout}; use core::ptr::null_mut; @@ -30,11 +32,7 @@ struct MyAllocator; unsafe impl GlobalAlloc for MyAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - if layout.size() < 4096 { - libc::malloc(layout.size()) as _ - } else { - null_mut() - } + if layout.size() < 4096 { libc::malloc(layout.size()) as _ } else { null_mut() } } unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {} } @@ -45,26 +43,12 @@ static A: MyAllocator = MyAllocator; #[panic_handler] fn panic(panic_info: &core::panic::PanicInfo) -> ! { unsafe { - if let Some(s) = panic_info.payload().downcast_ref::<&str>() { - const PSTR: &str = "panic occurred: "; - const CR: &str = "\n"; - libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); - libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); - libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); - } - if let Some(args) = panic_info.message() { - let mut s = alloc::string::String::new(); - alloc::fmt::write(&mut s, *args).unwrap(); - let s = s.as_str(); - const PSTR: &str = "panic occurred: "; - const CR: &str = "\n"; - libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); - libc::write(libc::STDERR_FILENO, s as *const _ as _, s.len()); - libc::write(libc::STDERR_FILENO, CR as *const _ as _, CR.len()); - } else { - const PSTR: &str = "panic occurred\n"; - libc::write(libc::STDERR_FILENO, PSTR as *const _ as _, PSTR.len()); - } + let s = panic_info.to_string(); + const PSTR: &str = "panic occurred: "; + const CR: &str = "\n"; + libc::write(libc::STDERR_FILENO, PSTR.as_ptr() as *const _, PSTR.len()); + libc::write(libc::STDERR_FILENO, s.as_ptr() as *const _, s.len()); + libc::write(libc::STDERR_FILENO, CR.as_ptr() as *const _, CR.len()); libc::exit(0) } } @@ -74,15 +58,14 @@ fn panic(panic_info: &core::panic::PanicInfo) -> ! { // in these libraries will refer to `rust_eh_personality` if LLVM can not *prove* the contents won't // unwind. So, for this test case we will define the symbol. #[lang = "eh_personality"] -extern fn rust_eh_personality() {} +extern "C" fn rust_eh_personality() {} -#[derive(Debug)] +#[derive(Default, Debug)] struct Page(#[allow(unused_tuple_struct_fields)] [[u64; 32]; 16]); -#[start] -pub fn main(_argc: isize, _argv: *const *const u8) -> isize { - let zero = Box::::new_zeroed(); - let zero = unsafe { zero.assume_init() }; +#[no_mangle] +fn main(_argc: i32, _argv: *const *const u8) -> isize { + let zero = Box::::new(Default::default()); helper::work_with(&zero); 1 } From 03ba0ea400b42a71455a9774b6cbbf7f28a8e9a7 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 30 Nov 2022 16:25:57 -0500 Subject: [PATCH 030/321] Don't suggest keeping borrows in `identity_op` --- clippy_lints/src/operators/identity_op.rs | 74 ++++++++++++++++++++--- tests/ui/identity_op.fixed | 6 +- tests/ui/identity_op.rs | 4 ++ tests/ui/identity_op.stderr | 12 +++- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/operators/identity_op.rs b/clippy_lints/src/operators/identity_op.rs index b48d6c4e2e2a..14a12da862ef 100644 --- a/clippy_lints/src/operators/identity_op.rs +++ b/clippy_lints/src/operators/identity_op.rs @@ -1,7 +1,7 @@ use clippy_utils::consts::{constant_full_int, constant_simple, Constant, FullInt}; use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{clip, unsext}; +use clippy_utils::{clip, peel_hir_expr_refs, unsext}; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, Node}; use rustc_lint::LateContext; @@ -20,20 +20,76 @@ pub(crate) fn check<'tcx>( if !is_allowed(cx, op, left, right) { match op { BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => { - check_op(cx, left, 0, expr.span, right.span, needs_parenthesis(cx, expr, right)); - check_op(cx, right, 0, expr.span, left.span, Parens::Unneeded); + check_op( + cx, + left, + 0, + expr.span, + peel_hir_expr_refs(right).0.span, + needs_parenthesis(cx, expr, right), + ); + check_op( + cx, + right, + 0, + expr.span, + peel_hir_expr_refs(left).0.span, + Parens::Unneeded, + ); }, BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => { - check_op(cx, right, 0, expr.span, left.span, Parens::Unneeded); + check_op( + cx, + right, + 0, + expr.span, + peel_hir_expr_refs(left).0.span, + Parens::Unneeded, + ); }, BinOpKind::Mul => { - check_op(cx, left, 1, expr.span, right.span, needs_parenthesis(cx, expr, right)); - check_op(cx, right, 1, expr.span, left.span, Parens::Unneeded); + check_op( + cx, + left, + 1, + expr.span, + peel_hir_expr_refs(right).0.span, + needs_parenthesis(cx, expr, right), + ); + check_op( + cx, + right, + 1, + expr.span, + peel_hir_expr_refs(left).0.span, + Parens::Unneeded, + ); }, - BinOpKind::Div => check_op(cx, right, 1, expr.span, left.span, Parens::Unneeded), + BinOpKind::Div => check_op( + cx, + right, + 1, + expr.span, + peel_hir_expr_refs(left).0.span, + Parens::Unneeded, + ), BinOpKind::BitAnd => { - check_op(cx, left, -1, expr.span, right.span, needs_parenthesis(cx, expr, right)); - check_op(cx, right, -1, expr.span, left.span, Parens::Unneeded); + check_op( + cx, + left, + -1, + expr.span, + peel_hir_expr_refs(right).0.span, + needs_parenthesis(cx, expr, right), + ); + check_op( + cx, + right, + -1, + expr.span, + peel_hir_expr_refs(left).0.span, + Parens::Unneeded, + ); }, BinOpKind::Rem => check_remainder(cx, left, right, expr.span, left.span), _ => (), diff --git a/tests/ui/identity_op.fixed b/tests/ui/identity_op.fixed index e7b9a78c5dbc..cac69ef42c41 100644 --- a/tests/ui/identity_op.fixed +++ b/tests/ui/identity_op.fixed @@ -65,7 +65,7 @@ fn main() { 42; 1; 42; - &x; + x; x; let mut a = A(String::new()); @@ -112,6 +112,10 @@ fn main() { 2 * { a }; (({ a } + 4)); 1; + + // Issue #9904 + let x = 0i32; + let _: i32 = x; } pub fn decide(a: bool, b: bool) -> u32 { diff --git a/tests/ui/identity_op.rs b/tests/ui/identity_op.rs index 9a435cdbb753..33201aad4f64 100644 --- a/tests/ui/identity_op.rs +++ b/tests/ui/identity_op.rs @@ -112,6 +112,10 @@ fn main() { 2 * (0 + { a }); 1 * ({ a } + 4); 1 * 1; + + // Issue #9904 + let x = 0i32; + let _: i32 = &x + 0; } pub fn decide(a: bool, b: bool) -> u32 { diff --git a/tests/ui/identity_op.stderr b/tests/ui/identity_op.stderr index 1a104a20b841..3ba557d18b24 100644 --- a/tests/ui/identity_op.stderr +++ b/tests/ui/identity_op.stderr @@ -70,7 +70,7 @@ error: this operation has no effect --> $DIR/identity_op.rs:68:5 | LL | &x >> 0; - | ^^^^^^^ help: consider reducing it to: `&x` + | ^^^^^^^ help: consider reducing it to: `x` error: this operation has no effect --> $DIR/identity_op.rs:69:5 @@ -229,10 +229,16 @@ LL | 1 * 1; | ^^^^^ help: consider reducing it to: `1` error: this operation has no effect - --> $DIR/identity_op.rs:118:5 + --> $DIR/identity_op.rs:118:18 + | +LL | let _: i32 = &x + 0; + | ^^^^^^ help: consider reducing it to: `x` + +error: this operation has no effect + --> $DIR/identity_op.rs:122:5 | LL | 0 + if a { 1 } else { 2 } + if b { 3 } else { 5 } | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider reducing it to: `(if a { 1 } else { 2 })` -error: aborting due to 39 previous errors +error: aborting due to 40 previous errors From 55096eab0f5b398294cba8b9182e06b1c1e3d423 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 30 Nov 2022 21:29:48 -0500 Subject: [PATCH 031/321] Don't suggest removing `mut` from references in `redundant_static_lifetimes` --- clippy_lints/src/redundant_static_lifetimes.rs | 2 +- tests/ui/redundant_static_lifetimes.fixed | 6 ++++++ tests/ui/redundant_static_lifetimes.rs | 6 ++++++ tests/ui/redundant_static_lifetimes.stderr | 10 ++++++++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/redundant_static_lifetimes.rs b/clippy_lints/src/redundant_static_lifetimes.rs index 3aa2490bc44e..41f991a967bf 100644 --- a/clippy_lints/src/redundant_static_lifetimes.rs +++ b/clippy_lints/src/redundant_static_lifetimes.rs @@ -66,7 +66,7 @@ impl RedundantStaticLifetimes { TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { if lifetime.ident.name == rustc_span::symbol::kw::StaticLifetime { let snip = snippet(cx, borrow_type.ty.span, ""); - let sugg = format!("&{snip}"); + let sugg = format!("&{}{snip}", borrow_type.mutbl.prefix_str()); span_lint_and_then( cx, REDUNDANT_STATIC_LIFETIMES, diff --git a/tests/ui/redundant_static_lifetimes.fixed b/tests/ui/redundant_static_lifetimes.fixed index 4c5846fe837e..bca777a890c3 100644 --- a/tests/ui/redundant_static_lifetimes.fixed +++ b/tests/ui/redundant_static_lifetimes.fixed @@ -39,8 +39,14 @@ static STATIC_VAR_TUPLE: &(u8, u8) = &(1, 2); // ERROR Consider removing 'static static STATIC_VAR_ARRAY: &[u8; 1] = b"T"; // ERROR Consider removing 'static. +static mut STATIC_MUT_SLICE: &mut [u32] = &mut [0]; + fn main() { let false_positive: &'static str = "test"; + + unsafe { + STATIC_MUT_SLICE[0] = 0; + } } trait Bar { diff --git a/tests/ui/redundant_static_lifetimes.rs b/tests/ui/redundant_static_lifetimes.rs index 64a66be1a83c..afe7644816d2 100644 --- a/tests/ui/redundant_static_lifetimes.rs +++ b/tests/ui/redundant_static_lifetimes.rs @@ -39,8 +39,14 @@ static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. +static mut STATIC_MUT_SLICE: &'static mut [u32] = &mut [0]; + fn main() { let false_positive: &'static str = "test"; + + unsafe { + STATIC_MUT_SLICE[0] = 0; + } } trait Bar { diff --git a/tests/ui/redundant_static_lifetimes.stderr b/tests/ui/redundant_static_lifetimes.stderr index 0938ebf783ff..b2cbd2d9d01b 100644 --- a/tests/ui/redundant_static_lifetimes.stderr +++ b/tests/ui/redundant_static_lifetimes.stderr @@ -97,10 +97,16 @@ LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removin | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:65:16 + --> $DIR/redundant_static_lifetimes.rs:42:31 + | +LL | static mut STATIC_MUT_SLICE: &'static mut [u32] = &mut [0]; + | -^^^^^^^---------- help: consider removing `'static`: `&mut [u32]` + +error: statics have by default a `'static` lifetime + --> $DIR/redundant_static_lifetimes.rs:71:16 | LL | static V: &'static u8 = &17; | -^^^^^^^--- help: consider removing `'static`: `&u8` -error: aborting due to 17 previous errors +error: aborting due to 18 previous errors From a1b15f13f7187b9c61e1d691a339b32550dfed43 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Wed, 30 Nov 2022 22:34:42 -0500 Subject: [PATCH 032/321] Treat custom enum discriminant values as constants --- clippy_utils/src/lib.rs | 2 +- tests/ui/cast_lossless_integer.fixed | 6 ++++++ tests/ui/cast_lossless_integer.rs | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 90192f46cbfa..09ed7255a2b2 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -196,7 +196,7 @@ pub fn in_constant(cx: &LateContext<'_>, id: HirId) -> bool { let parent_id = cx.tcx.hir().get_parent_item(id).def_id; match cx.tcx.hir().get_by_def_id(parent_id) { Node::Item(&Item { - kind: ItemKind::Const(..) | ItemKind::Static(..), + kind: ItemKind::Const(..) | ItemKind::Static(..) | ItemKind::Enum(..), .. }) | Node::TraitItem(&TraitItem { diff --git a/tests/ui/cast_lossless_integer.fixed b/tests/ui/cast_lossless_integer.fixed index 72a708b40737..925cbf25368f 100644 --- a/tests/ui/cast_lossless_integer.fixed +++ b/tests/ui/cast_lossless_integer.fixed @@ -45,3 +45,9 @@ mod cast_lossless_in_impl { } } } + +#[derive(PartialEq, Debug)] +#[repr(i64)] +enum Test { + A = u32::MAX as i64 + 1, +} diff --git a/tests/ui/cast_lossless_integer.rs b/tests/ui/cast_lossless_integer.rs index 34bb47181e69..c82bd9108d23 100644 --- a/tests/ui/cast_lossless_integer.rs +++ b/tests/ui/cast_lossless_integer.rs @@ -45,3 +45,9 @@ mod cast_lossless_in_impl { } } } + +#[derive(PartialEq, Debug)] +#[repr(i64)] +enum Test { + A = u32::MAX as i64 + 1, +} From 2fba07842b2553f5abaada91590817032df36f7d Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Thu, 1 Dec 2022 12:13:58 +0100 Subject: [PATCH 033/321] Make `VecDeque::new` const --- library/alloc/src/collections/vec_deque/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 86d77182bcce..30514cbba92a 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -531,12 +531,13 @@ impl VecDeque { /// /// let deque: VecDeque = VecDeque::new(); /// ``` - // FIXME: This should probably be const #[inline] #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_const_stable(feature = "const_vec_deque_new", since = "CURRENT_RUSTC_VERSION")] #[must_use] - pub fn new() -> VecDeque { - VecDeque::new_in(Global) + pub const fn new() -> VecDeque { + // FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable. + VecDeque { head: 0, len: 0, buf: RawVec::NEW } } /// Creates an empty deque with space for at least `capacity` elements. From 4f8c49e950d70aad48d2e1ec4642cef0a364c0ec Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 25 Nov 2022 11:26:36 +0300 Subject: [PATCH 034/321] rustc_hir: Relax lifetime requirements on `Visitor::visit_path` --- clippy_lints/src/from_over_into.rs | 2 +- clippy_lints/src/methods/option_map_unwrap_or.rs | 4 ++-- .../src/utils/internal_lints/lint_without_lint_pass.rs | 2 +- clippy_lints/src/utils/internal_lints/metadata_collector.rs | 2 +- clippy_utils/src/usage.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/from_over_into.rs b/clippy_lints/src/from_over_into.rs index 8b24a4962fb2..6d9ede5f73bb 100644 --- a/clippy_lints/src/from_over_into.rs +++ b/clippy_lints/src/from_over_into.rs @@ -126,7 +126,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SelfFinder<'a, 'tcx> { self.cx.tcx.hir() } - fn visit_path(&mut self, path: &'tcx Path<'tcx>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { for segment in path.segments { match segment.ident.name { kw::SelfLower => self.lower.push(segment.ident.span), diff --git a/clippy_lints/src/methods/option_map_unwrap_or.rs b/clippy_lints/src/methods/option_map_unwrap_or.rs index 30421a6dd5af..910ee14855e2 100644 --- a/clippy_lints/src/methods/option_map_unwrap_or.rs +++ b/clippy_lints/src/methods/option_map_unwrap_or.rs @@ -97,7 +97,7 @@ struct UnwrapVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> { type NestedFilter = nested_filter::All; - fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { self.identifiers.insert(ident(path)); walk_path(self, path); } @@ -116,7 +116,7 @@ struct MapExprVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for MapExprVisitor<'a, 'tcx> { type NestedFilter = nested_filter::All; - fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) { + fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) { if self.identifiers.contains(&ident(path)) { self.found_identifier = true; return; diff --git a/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs b/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs index 1aebb8b3104b..786d9608c851 100644 --- a/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs +++ b/clippy_lints/src/utils/internal_lints/lint_without_lint_pass.rs @@ -330,7 +330,7 @@ struct LintCollector<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for LintCollector<'a, 'tcx> { type NestedFilter = nested_filter::All; - fn visit_path(&mut self, path: &'tcx Path<'_>, _: HirId) { + fn visit_path(&mut self, path: &Path<'_>, _: HirId) { if path.segments.len() == 1 { self.output.insert(path.segments[0].ident.name); } diff --git a/clippy_lints/src/utils/internal_lints/metadata_collector.rs b/clippy_lints/src/utils/internal_lints/metadata_collector.rs index d06a616e4b30..857abe77e21f 100644 --- a/clippy_lints/src/utils/internal_lints/metadata_collector.rs +++ b/clippy_lints/src/utils/internal_lints/metadata_collector.rs @@ -1019,7 +1019,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for ApplicabilityResolver<'a, 'hir> { self.cx.tcx.hir() } - fn visit_path(&mut self, path: &'hir hir::Path<'hir>, _id: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'hir>, _id: hir::HirId) { for (index, enum_value) in paths::APPLICABILITY_VALUES.iter().enumerate() { if match_path(path, enum_value) { self.add_new_index(index); diff --git a/clippy_utils/src/usage.rs b/clippy_utils/src/usage.rs index 797722cfc1fc..ab3976a13bdb 100644 --- a/clippy_utils/src/usage.rs +++ b/clippy_utils/src/usage.rs @@ -128,7 +128,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> { } } - fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) { + fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) { if let hir::def::Res::Local(id) = path.res { if self.binding_ids.contains(&id) { self.usage_found = true; From 9314e5b942c1e2cbc9d4c7d520ca7102675d6d08 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 25 Nov 2022 17:39:38 +0300 Subject: [PATCH 035/321] rustc_hir: Change representation of import paths to support multiple resolutions --- clippy_lints/src/disallowed_types.rs | 4 +- clippy_lints/src/macro_use.rs | 5 +- .../src/missing_enforced_import_rename.rs | 59 ++++++++++--------- clippy_lints/src/redundant_pub_crate.rs | 2 +- clippy_lints/src/wildcard_imports.rs | 3 +- 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/clippy_lints/src/disallowed_types.rs b/clippy_lints/src/disallowed_types.rs index aee3d8c4f085..1f56d0118a40 100644 --- a/clippy_lints/src/disallowed_types.rs +++ b/clippy_lints/src/disallowed_types.rs @@ -106,7 +106,9 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedTypes { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { if let ItemKind::Use(path, UseKind::Single) = &item.kind { - self.check_res_emit(cx, &path.res, item.span); + for res in &path.res { + self.check_res_emit(cx, res, item.span); + } } } diff --git a/clippy_lints/src/macro_use.rs b/clippy_lints/src/macro_use.rs index 594f6af76b3d..e2e6a87a3015 100644 --- a/clippy_lints/src/macro_use.rs +++ b/clippy_lints/src/macro_use.rs @@ -94,7 +94,10 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports { let hir_id = item.hir_id(); let attrs = cx.tcx.hir().attrs(hir_id); if let Some(mac_attr) = attrs.iter().find(|attr| attr.has_name(sym::macro_use)); - if let Res::Def(DefKind::Mod, id) = path.res; + if let Some(id) = path.res.iter().find_map(|res| match res { + Res::Def(DefKind::Mod, id) => Some(id), + _ => None, + }); if !id.is_local(); then { for kid in cx.tcx.module_children(id).iter() { diff --git a/clippy_lints/src/missing_enforced_import_rename.rs b/clippy_lints/src/missing_enforced_import_rename.rs index 4712846939e6..773174679dbd 100644 --- a/clippy_lints/src/missing_enforced_import_rename.rs +++ b/clippy_lints/src/missing_enforced_import_rename.rs @@ -66,35 +66,38 @@ impl LateLintPass<'_> for ImportRename { } fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { - if_chain! { - if let ItemKind::Use(path, UseKind::Single) = &item.kind; - if let Res::Def(_, id) = path.res; - if let Some(name) = self.renames.get(&id); - // Remove semicolon since it is not present for nested imports - let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';'); - if let Some(snip) = snippet_opt(cx, span_without_semi); - if let Some(import) = match snip.split_once(" as ") { - None => Some(snip.as_str()), - Some((import, rename)) => { - if rename.trim() == name.as_str() { - None - } else { - Some(import.trim()) + if let ItemKind::Use(path, UseKind::Single) = &item.kind { + for &res in &path.res { + if_chain! { + if let Res::Def(_, id) = res; + if let Some(name) = self.renames.get(&id); + // Remove semicolon since it is not present for nested imports + let span_without_semi = cx.sess().source_map().span_until_char(item.span, ';'); + if let Some(snip) = snippet_opt(cx, span_without_semi); + if let Some(import) = match snip.split_once(" as ") { + None => Some(snip.as_str()), + Some((import, rename)) => { + if rename.trim() == name.as_str() { + None + } else { + Some(import.trim()) + } + }, + }; + then { + span_lint_and_sugg( + cx, + MISSING_ENFORCED_IMPORT_RENAMES, + span_without_semi, + "this import should be renamed", + "try", + format!( + "{import} as {name}", + ), + Applicability::MachineApplicable, + ); } - }, - }; - then { - span_lint_and_sugg( - cx, - MISSING_ENFORCED_IMPORT_RENAMES, - span_without_semi, - "this import should be renamed", - "try", - format!( - "{import} as {name}", - ), - Applicability::MachineApplicable, - ); + } } } } diff --git a/clippy_lints/src/redundant_pub_crate.rs b/clippy_lints/src/redundant_pub_crate.rs index 833dc4913b46..d612d249c2f0 100644 --- a/clippy_lints/src/redundant_pub_crate.rs +++ b/clippy_lints/src/redundant_pub_crate.rs @@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate { fn is_not_macro_export<'tcx>(item: &'tcx Item<'tcx>) -> bool { if let ItemKind::Use(path, _) = item.kind { - if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = path.res { + if path.res.iter().all(|res| matches!(res, Res::Def(DefKind::Macro(MacroKind::Bang), _))) { return false; } } else if let ItemKind::Macro(..) = item.kind { diff --git a/clippy_lints/src/wildcard_imports.rs b/clippy_lints/src/wildcard_imports.rs index be98344470b9..e4d1ee195c4d 100644 --- a/clippy_lints/src/wildcard_imports.rs +++ b/clippy_lints/src/wildcard_imports.rs @@ -176,7 +176,8 @@ impl LateLintPass<'_> for WildcardImports { format!("{import_source_snippet}::{imports_string}") }; - let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res { + // Glob imports always have a single resolution. + let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res[0] { (ENUM_GLOB_USE, "usage of wildcard import for enum variants") } else { (WILDCARD_IMPORTS, "usage of wildcard import") From b0d490e308561eaa3b30067fe6e35c47d47e8896 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 1 Dec 2022 18:51:20 +0300 Subject: [PATCH 036/321] rustc_ast_lowering: Stop lowering imports into multiple items Lower them into a single item with multiple resolutions instead. This also allows to remove additional `NodId`s and `DefId`s related to those additional items. --- clippy_lints/src/single_component_path_imports.rs | 4 ++-- clippy_lints/src/unnecessary_self_imports.rs | 2 +- clippy_lints/src/unsafe_removed_from_name.rs | 4 ++-- clippy_utils/src/ast_utils.rs | 2 +- tests/ui/macro_use_imports.stderr | 8 ++++---- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/single_component_path_imports.rs b/clippy_lints/src/single_component_path_imports.rs index 2036e85db7e8..d46f6a6352c6 100644 --- a/clippy_lints/src/single_component_path_imports.rs +++ b/clippy_lints/src/single_component_path_imports.rs @@ -149,7 +149,7 @@ impl SingleComponentPathImports { // keep track of `use some_module;` usages if segments.len() == 1 { - if let UseTreeKind::Simple(None, _, _) = use_tree.kind { + if let UseTreeKind::Simple(None) = use_tree.kind { let name = segments[0].ident.name; if !macros.contains(&name) { single_use_usages.push(SingleUse { @@ -169,7 +169,7 @@ impl SingleComponentPathImports { for tree in trees { let segments = &tree.0.prefix.segments; if segments.len() == 1 { - if let UseTreeKind::Simple(None, _, _) = tree.0.kind { + if let UseTreeKind::Simple(None) = tree.0.kind { let name = segments[0].ident.name; if !macros.contains(&name) { single_use_usages.push(SingleUse { diff --git a/clippy_lints/src/unnecessary_self_imports.rs b/clippy_lints/src/unnecessary_self_imports.rs index bc0dd263d88a..397633f533b2 100644 --- a/clippy_lints/src/unnecessary_self_imports.rs +++ b/clippy_lints/src/unnecessary_self_imports.rs @@ -57,7 +57,7 @@ impl EarlyLintPass for UnnecessarySelfImports { format!( "{}{};", last_segment.ident, - if let UseTreeKind::Simple(Some(alias), ..) = self_tree.kind { format!(" as {alias}") } else { String::new() }, + if let UseTreeKind::Simple(Some(alias)) = self_tree.kind { format!(" as {alias}") } else { String::new() }, ), Applicability::MaybeIncorrect, ); diff --git a/clippy_lints/src/unsafe_removed_from_name.rs b/clippy_lints/src/unsafe_removed_from_name.rs index 952586527689..7ee785804f0a 100644 --- a/clippy_lints/src/unsafe_removed_from_name.rs +++ b/clippy_lints/src/unsafe_removed_from_name.rs @@ -39,7 +39,7 @@ impl EarlyLintPass for UnsafeNameRemoval { fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) { match use_tree.kind { - UseTreeKind::Simple(Some(new_name), ..) => { + UseTreeKind::Simple(Some(new_name)) => { let old_name = use_tree .prefix .segments @@ -48,7 +48,7 @@ fn check_use_tree(use_tree: &UseTree, cx: &EarlyContext<'_>, span: Span) { .ident; unsafe_to_safe_check(old_name, new_name, cx, span); }, - UseTreeKind::Simple(None, ..) | UseTreeKind::Glob => {}, + UseTreeKind::Simple(None) | UseTreeKind::Glob => {}, UseTreeKind::Nested(ref nested_use_tree) => { for (use_tree, _) in nested_use_tree { check_use_tree(use_tree, cx, span); diff --git a/clippy_utils/src/ast_utils.rs b/clippy_utils/src/ast_utils.rs index 6bcf0bbd7eb7..49e5f283db08 100644 --- a/clippy_utils/src/ast_utils.rs +++ b/clippy_utils/src/ast_utils.rs @@ -566,7 +566,7 @@ pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool { use UseTreeKind::*; match (l, r) { (Glob, Glob) => true, - (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)), + (Simple(l), Simple(r)) => both(l, r, |l, r| eq_id(*l, *r)), (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)), _ => false, } diff --git a/tests/ui/macro_use_imports.stderr b/tests/ui/macro_use_imports.stderr index bf7b6edd0e31..61843124ccd9 100644 --- a/tests/ui/macro_use_imports.stderr +++ b/tests/ui/macro_use_imports.stderr @@ -1,8 +1,8 @@ error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:23:5 + --> $DIR/macro_use_imports.rs:25:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;` | = note: `-D clippy::macro-use-imports` implied by `-D warnings` @@ -13,10 +13,10 @@ LL | #[macro_use] | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;` error: `macro_use` attributes are no longer needed in the Rust 2018 edition - --> $DIR/macro_use_imports.rs:25:5 + --> $DIR/macro_use_imports.rs:23:5 | LL | #[macro_use] - | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;` + | ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::foofoo, inner::try_err};` error: `macro_use` attributes are no longer needed in the Rust 2018 edition --> $DIR/macro_use_imports.rs:19:5 From 6ecdff07e5c38f3cf53f299219ba575ebd4f9831 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Tue, 29 Nov 2022 00:20:53 -0500 Subject: [PATCH 037/321] Don't lint `from_over_into` for opaque types --- clippy_lints/src/from_over_into.rs | 3 ++- tests/ui/from_over_into.fixed | 7 +++++++ tests/ui/from_over_into.rs | 7 +++++++ tests/ui/from_over_into.stderr | 10 +++++----- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/from_over_into.rs b/clippy_lints/src/from_over_into.rs index 8621504c1b47..cc3c35f48635 100644 --- a/clippy_lints/src/from_over_into.rs +++ b/clippy_lints/src/from_over_into.rs @@ -10,7 +10,7 @@ use rustc_hir::{ TyKind, }; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::hir::nested_filter::OnlyBodies; +use rustc_middle::{hir::nested_filter::OnlyBodies, ty}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::{kw, sym}; use rustc_span::{Span, Symbol}; @@ -78,6 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for FromOverInto { && let Some(GenericArgs { args: [GenericArg::Type(target_ty)], .. }) = into_trait_seg.args && let Some(middle_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id) && cx.tcx.is_diagnostic_item(sym::Into, middle_trait_ref.def_id) + && !matches!(middle_trait_ref.substs.type_at(1).kind(), ty::Opaque(..)) { span_lint_and_then( cx, diff --git a/tests/ui/from_over_into.fixed b/tests/ui/from_over_into.fixed index 125c9a69cd3f..72d635c2ccd6 100644 --- a/tests/ui/from_over_into.fixed +++ b/tests/ui/from_over_into.fixed @@ -1,5 +1,6 @@ // run-rustfix +#![feature(type_alias_impl_trait)] #![warn(clippy::from_over_into)] #![allow(unused)] @@ -81,4 +82,10 @@ fn msrv_1_41() { } } +type Opaque = impl Sized; +struct IntoOpaque; +impl Into for IntoOpaque { + fn into(self) -> Opaque {} +} + fn main() {} diff --git a/tests/ui/from_over_into.rs b/tests/ui/from_over_into.rs index 5aa127bfabe4..965f4d5d7859 100644 --- a/tests/ui/from_over_into.rs +++ b/tests/ui/from_over_into.rs @@ -1,5 +1,6 @@ // run-rustfix +#![feature(type_alias_impl_trait)] #![warn(clippy::from_over_into)] #![allow(unused)] @@ -81,4 +82,10 @@ fn msrv_1_41() { } } +type Opaque = impl Sized; +struct IntoOpaque; +impl Into for IntoOpaque { + fn into(self) -> Opaque {} +} + fn main() {} diff --git a/tests/ui/from_over_into.stderr b/tests/ui/from_over_into.stderr index a1764a5ea12a..3c4d011d6fb4 100644 --- a/tests/ui/from_over_into.stderr +++ b/tests/ui/from_over_into.stderr @@ -1,5 +1,5 @@ error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:9:1 + --> $DIR/from_over_into.rs:10:1 | LL | impl Into for String { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -13,7 +13,7 @@ LL ~ StringWrapper(val) | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:17:1 + --> $DIR/from_over_into.rs:18:1 | LL | impl Into for String { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -26,7 +26,7 @@ LL ~ SelfType(String::new()) | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:32:1 + --> $DIR/from_over_into.rs:33:1 | LL | impl Into for X { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -41,7 +41,7 @@ LL ~ let _: X = val; | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:44:1 + --> $DIR/from_over_into.rs:45:1 | LL | impl core::convert::Into for crate::ExplicitPaths { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -59,7 +59,7 @@ LL ~ val.0 | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:77:5 + --> $DIR/from_over_into.rs:78:5 | LL | impl Into> for Vec { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ From d05e2865a05e86de9cfd283d4a6f88340346f48b Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Thu, 1 Dec 2022 18:29:38 +0100 Subject: [PATCH 038/321] Merge commit 'd822110d3b5625b9dc80ccc442e06fc3cc851d76' into clippyup --- CHANGELOG.md | 2 + README.md | 6 +- book/src/SUMMARY.md | 1 + book/src/configuration.md | 6 +- book/src/development/adding_lints.md | 18 +- .../proposals/syntax-tree-patterns.md | 986 ++++++++++++++++++ clippy_dev/src/new_lint.rs | 16 +- .../src/almost_complete_letter_range.rs | 11 +- clippy_lints/src/approx_const.rs | 8 +- clippy_lints/src/attrs.rs | 12 +- .../src/casts/cast_abs_to_unsigned.rs | 7 +- clippy_lints/src/casts/cast_lossless.rs | 16 +- .../src/casts/cast_possible_truncation.rs | 7 +- .../src/casts/cast_slice_different_sizes.rs | 8 +- .../src/casts/cast_slice_from_raw_parts.rs | 14 +- clippy_lints/src/casts/mod.rs | 22 +- clippy_lints/src/casts/ptr_as_ptr.rs | 7 +- clippy_lints/src/casts/unnecessary_cast.rs | 17 +- clippy_lints/src/checked_conversions.rs | 10 +- clippy_lints/src/collapsible_if.rs | 10 +- clippy_lints/src/declared_lints.rs | 2 + clippy_lints/src/dereference.rs | 41 +- clippy_lints/src/derive.rs | 4 +- clippy_lints/src/doc.rs | 2 +- clippy_lints/src/eta_reduction.rs | 22 +- clippy_lints/src/exit.rs | 23 +- clippy_lints/src/format_args.rs | 82 +- clippy_lints/src/from_over_into.rs | 10 +- .../src/functions/misnamed_getters.rs | 125 +++ clippy_lints/src/functions/mod.rs | 45 + clippy_lints/src/functions/result.rs | 8 +- clippy_lints/src/future_not_send.rs | 4 +- clippy_lints/src/if_then_some_else_none.rs | 14 +- clippy_lints/src/index_refutable_slice.rs | 13 +- clippy_lints/src/instant_subtraction.rs | 18 +- clippy_lints/src/lib.rs | 109 +- clippy_lints/src/lifetimes.rs | 13 +- clippy_lints/src/manual_bits.rs | 10 +- clippy_lints/src/manual_clamp.rs | 33 +- clippy_lints/src/manual_is_ascii_check.rs | 15 +- clippy_lints/src/manual_let_else.rs | 36 +- clippy_lints/src/manual_non_exhaustive.rs | 16 +- clippy_lints/src/manual_rem_euclid.rs | 12 +- clippy_lints/src/manual_retain.rs | 24 +- clippy_lints/src/manual_strip.rs | 10 +- clippy_lints/src/matches/mod.rs | 14 +- clippy_lints/src/matches/try_err.rs | 6 +- clippy_lints/src/mem_replace.rs | 10 +- .../src/methods/cloned_instead_of_copied.rs | 10 +- clippy_lints/src/methods/err_expect.rs | 8 +- clippy_lints/src/methods/filter_map_next.rs | 8 +- .../src/methods/is_digit_ascii_radix.rs | 9 +- clippy_lints/src/methods/map_clone.rs | 16 +- clippy_lints/src/methods/map_unwrap_or.rs | 7 +- clippy_lints/src/methods/mod.rs | 36 +- .../src/methods/option_as_ref_deref.rs | 8 +- clippy_lints/src/methods/str_splitn.rs | 8 +- .../src/methods/unnecessary_to_owned.rs | 61 +- clippy_lints/src/missing_const_for_fn.rs | 14 +- clippy_lints/src/needless_pass_by_value.rs | 8 +- clippy_lints/src/no_effect.rs | 8 +- clippy_lints/src/ptr.rs | 9 +- clippy_lints/src/ranges.rs | 10 +- clippy_lints/src/redundant_closure_call.rs | 4 +- clippy_lints/src/redundant_field_names.rs | 9 +- .../src/redundant_static_lifetimes.rs | 9 +- clippy_lints/src/returns.rs | 29 +- clippy_lints/src/transmute/mod.rs | 8 +- .../src/transmute/transmute_ptr_to_ref.rs | 10 +- .../src/undocumented_unsafe_blocks.rs | 429 ++++++-- clippy_lints/src/unnested_or_patterns.rs | 17 +- clippy_lints/src/unused_rounding.rs | 21 +- clippy_lints/src/use_self.rs | 14 +- clippy_lints/src/utils/conf.rs | 4 + .../utils/internal_lints/msrv_attr_impl.rs | 2 +- clippy_utils/src/attrs.rs | 24 +- clippy_utils/src/eager_or_lazy.rs | 26 +- clippy_utils/src/lib.rs | 33 +- clippy_utils/src/msrvs.rs | 101 ++ clippy_utils/src/paths.rs | 5 +- clippy_utils/src/qualify_min_const_fn.rs | 28 +- clippy_utils/src/source.rs | 38 +- clippy_utils/src/sugg.rs | 65 +- clippy_utils/src/ty.rs | 38 +- clippy_utils/src/visitors.rs | 12 +- lintcheck/src/main.rs | 15 +- rust-toolchain | 2 +- src/driver.rs | 5 + .../ui-internal/invalid_msrv_attr_impl.fixed | 4 +- tests/ui-internal/invalid_msrv_attr_impl.rs | 4 +- ...unnecessary_def_path_hardcoded_path.stderr | 18 +- .../clippy.toml | 1 + .../uninlined_format_args.fixed | 14 + .../uninlined_format_args.rs | 14 + .../uninlined_format_args.stderr | 76 ++ .../toml_unknown_key/conf_unknown_key.stderr | 1 + tests/ui/almost_complete_letter_range.fixed | 5 +- tests/ui/almost_complete_letter_range.rs | 5 +- tests/ui/almost_complete_letter_range.stderr | 26 +- tests/ui/cast_abs_to_unsigned.fixed | 7 +- tests/ui/cast_abs_to_unsigned.rs | 7 +- tests/ui/cast_abs_to_unsigned.stderr | 36 +- tests/ui/cast_lossless_bool.fixed | 7 +- tests/ui/cast_lossless_bool.rs | 7 +- tests/ui/cast_lossless_bool.stderr | 28 +- tests/ui/cfg_attr_rustfmt.fixed | 8 +- tests/ui/cfg_attr_rustfmt.rs | 8 +- tests/ui/cfg_attr_rustfmt.stderr | 2 +- tests/ui/checked_conversions.fixed | 7 +- tests/ui/checked_conversions.rs | 7 +- tests/ui/checked_conversions.stderr | 34 +- tests/ui/cloned_instead_of_copied.fixed | 10 +- tests/ui/cloned_instead_of_copied.rs | 10 +- tests/ui/cloned_instead_of_copied.stderr | 16 +- tests/ui/err_expect.fixed | 7 +- tests/ui/err_expect.rs | 7 +- tests/ui/err_expect.stderr | 4 +- tests/ui/eta.fixed | 22 + tests/ui/eta.rs | 22 + tests/ui/eta.stderr | 26 +- tests/ui/explicit_auto_deref.fixed | 4 + tests/ui/explicit_auto_deref.rs | 4 + tests/ui/filter_map_next_fixable.fixed | 7 +- tests/ui/filter_map_next_fixable.rs | 7 +- tests/ui/filter_map_next_fixable.stderr | 4 +- tests/ui/from_over_into.fixed | 7 +- tests/ui/from_over_into.rs | 7 +- tests/ui/from_over_into.stderr | 10 +- tests/ui/if_then_some_else_none.rs | 5 +- tests/ui/if_then_some_else_none.stderr | 10 +- tests/ui/manual_clamp.rs | 7 +- tests/ui/manual_clamp.stderr | 70 +- tests/ui/manual_is_ascii_check.fixed | 11 +- tests/ui/manual_is_ascii_check.rs | 11 +- tests/ui/manual_is_ascii_check.stderr | 22 +- tests/ui/manual_let_else.rs | 14 + tests/ui/manual_let_else.stderr | 11 +- tests/ui/manual_rem_euclid.fixed | 13 +- tests/ui/manual_rem_euclid.rs | 13 +- tests/ui/manual_rem_euclid.stderr | 20 +- tests/ui/manual_retain.fixed | 7 +- tests/ui/manual_retain.rs | 7 +- tests/ui/manual_retain.stderr | 38 +- tests/ui/manual_split_once.fixed | 5 +- tests/ui/manual_split_once.rs | 5 +- tests/ui/manual_split_once.stderr | 38 +- tests/ui/manual_str_repeat.fixed | 5 +- tests/ui/manual_str_repeat.rs | 5 +- tests/ui/manual_str_repeat.stderr | 20 +- tests/ui/manual_strip.rs | 7 +- tests/ui/manual_strip.stderr | 32 +- tests/ui/map_unwrap_or.rs | 7 +- tests/ui/map_unwrap_or.stderr | 24 +- tests/ui/match_expr_like_matches_macro.fixed | 7 +- tests/ui/match_expr_like_matches_macro.rs | 7 +- tests/ui/match_expr_like_matches_macro.stderr | 28 +- tests/ui/mem_replace.fixed | 7 +- tests/ui/mem_replace.rs | 7 +- tests/ui/mem_replace.stderr | 40 +- tests/ui/min_rust_version_attr.rs | 41 +- tests/ui/min_rust_version_attr.stderr | 26 +- tests/ui/min_rust_version_invalid_attr.stderr | 2 +- tests/ui/misnamed_getters.rs | 124 +++ tests/ui/misnamed_getters.stderr | 166 +++ .../ui/missing_const_for_fn/cant_be_const.rs | 4 +- .../ui/missing_const_for_fn/could_be_const.rs | 10 +- .../could_be_const.stderr | 24 +- tests/ui/needless_borrow.fixed | 33 +- tests/ui/needless_borrow.rs | 33 +- tests/ui/needless_borrow.stderr | 18 +- tests/ui/needless_question_mark.fixed | 1 - tests/ui/needless_question_mark.rs | 1 - tests/ui/needless_question_mark.stderr | 28 +- tests/ui/needless_return.fixed | 19 +- tests/ui/needless_return.rs | 13 + tests/ui/needless_return.stderr | 85 +- tests/ui/needless_splitn.fixed | 3 +- tests/ui/needless_splitn.rs | 3 +- tests/ui/needless_splitn.stderr | 26 +- tests/ui/option_as_ref_deref.fixed | 7 +- tests/ui/option_as_ref_deref.rs | 7 +- tests/ui/option_as_ref_deref.stderr | 36 +- tests/ui/ptr_as_ptr.fixed | 5 +- tests/ui/ptr_as_ptr.rs | 5 +- tests/ui/ptr_as_ptr.stderr | 16 +- tests/ui/range_contains.fixed | 7 +- tests/ui/range_contains.rs | 7 +- tests/ui/range_contains.stderr | 42 +- tests/ui/redundant_closure_call_fixable.fixed | 12 + tests/ui/redundant_closure_call_fixable.rs | 12 + .../ui/redundant_closure_call_fixable.stderr | 24 +- tests/ui/redundant_field_names.fixed | 7 +- tests/ui/redundant_field_names.rs | 7 +- tests/ui/redundant_field_names.stderr | 16 +- tests/ui/redundant_static_lifetimes.fixed | 7 +- tests/ui/redundant_static_lifetimes.rs | 7 +- tests/ui/redundant_static_lifetimes.stderr | 34 +- tests/ui/result_large_err.rs | 6 + tests/ui/seek_from_current.fixed | 5 +- tests/ui/seek_from_current.rs | 5 +- tests/ui/seek_from_current.stderr | 2 +- .../ui/seek_to_start_instead_of_rewind.fixed | 7 +- tests/ui/seek_to_start_instead_of_rewind.rs | 7 +- .../ui/seek_to_start_instead_of_rewind.stderr | 6 +- tests/ui/transmute_ptr_to_ref.fixed | 5 +- tests/ui/transmute_ptr_to_ref.rs | 5 +- tests/ui/transmute_ptr_to_ref.stderr | 44 +- tests/ui/undocumented_unsafe_blocks.rs | 2 +- tests/ui/undocumented_unsafe_blocks.stderr | 33 +- tests/ui/uninlined_format_args.fixed | 24 +- tests/ui/uninlined_format_args.rs | 5 +- tests/ui/uninlined_format_args.stderr | 213 ++-- tests/ui/unnecessary_cast.fixed | 14 + tests/ui/unnecessary_cast.rs | 14 + tests/ui/unnecessary_cast.stderr | 52 +- tests/ui/unnecessary_lazy_eval.fixed | 23 +- tests/ui/unnecessary_lazy_eval.rs | 23 +- tests/ui/unnecessary_lazy_eval.stderr | 84 +- tests/ui/unnecessary_operation.fixed | 9 + tests/ui/unnecessary_operation.rs | 9 + tests/ui/unnecessary_safety_comment.rs | 51 + tests/ui/unnecessary_safety_comment.stderr | 115 ++ tests/ui/unnecessary_to_owned.fixed | 34 +- tests/ui/unnecessary_to_owned.rs | 34 +- tests/ui/unnecessary_to_owned.stderr | 168 +-- ..._unsafe.rs => unnecessary_unsafety_doc.rs} | 1 + ...stderr => unnecessary_unsafety_doc.stderr} | 14 +- tests/ui/unnested_or_patterns.fixed | 8 +- tests/ui/unnested_or_patterns.rs | 8 +- tests/ui/unnested_or_patterns.stderr | 2 +- tests/ui/unused_rounding.fixed | 3 + tests/ui/unused_rounding.rs | 3 + tests/ui/unused_rounding.stderr | 8 +- tests/ui/use_self.fixed | 7 +- tests/ui/use_self.rs | 7 +- tests/ui/use_self.stderr | 84 +- triagebot.toml | 21 +- 237 files changed, 4245 insertions(+), 1914 deletions(-) create mode 100644 book/src/development/proposals/syntax-tree-patterns.md create mode 100644 clippy_lints/src/functions/misnamed_getters.rs create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs create mode 100644 tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr create mode 100644 tests/ui/misnamed_getters.rs create mode 100644 tests/ui/misnamed_getters.stderr create mode 100644 tests/ui/unnecessary_safety_comment.rs create mode 100644 tests/ui/unnecessary_safety_comment.stderr rename tests/ui/{doc_unnecessary_unsafe.rs => unnecessary_unsafety_doc.rs} (98%) rename tests/ui/{doc_unnecessary_unsafe.stderr => unnecessary_unsafety_doc.stderr} (81%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1f73c1fd2f..23912bb3ed6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4188,6 +4188,7 @@ Released 2018-09-13 [`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute [`mismatched_target_os`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatched_target_os [`mismatching_type_param_order`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatching_type_param_order +[`misnamed_getters`]: https://rust-lang.github.io/rust-clippy/master/index.html#misnamed_getters [`misrefactored_assign_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#misrefactored_assign_op [`missing_const_for_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn [`missing_docs_in_private_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items @@ -4450,6 +4451,7 @@ Released 2018-09-13 [`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed [`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation [`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings +[`unnecessary_safety_comment`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_comment [`unnecessary_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_doc [`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports [`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by diff --git a/README.md b/README.md index f74de7de42b8..81254ba8b8b8 100644 --- a/README.md +++ b/README.md @@ -197,8 +197,8 @@ disallowed-names = ["toto", "tata", "titi"] cognitive-complexity-threshold = 30 ``` -See the [list of lints](https://rust-lang.github.io/rust-clippy/master/index.html) for more information about which -lints can be configured and the meaning of the variables. +See the [list of configurable lints](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration), +the lint descriptions contain the names and meanings of these configuration variables. > **Note** > @@ -224,7 +224,7 @@ in the `Cargo.toml` can be used. rust-version = "1.30" ``` -The MSRV can also be specified as an inner attribute, like below. +The MSRV can also be specified as an attribute, like below. ```rust #![feature(custom_inner_attributes)] diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 0b945faf9b78..1f0b8db28a15 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -21,3 +21,4 @@ - [The Clippy Book](development/infrastructure/book.md) - [Proposals](development/proposals/README.md) - [Roadmap 2021](development/proposals/roadmap-2021.md) + - [Syntax Tree Patterns](development/proposals/syntax-tree-patterns.md) diff --git a/book/src/configuration.md b/book/src/configuration.md index 77f1d2e8797a..430ff8b739ae 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -11,8 +11,8 @@ disallowed-names = ["toto", "tata", "titi"] cognitive-complexity-threshold = 30 ``` -See the [list of lints](https://rust-lang.github.io/rust-clippy/master/index.html) for more information about which -lints can be configured and the meaning of the variables. +See the [list of configurable lints](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration), +the lint descriptions contain the names and meanings of these configuration variables. To deactivate the "for further information visit *lint-link*" message you can define the `CLIPPY_DISABLE_DOCS_LINKS` environment variable. @@ -72,7 +72,7 @@ minimum supported Rust version (MSRV) in the clippy configuration file. msrv = "1.30.0" ``` -The MSRV can also be specified as an inner attribute, like below. +The MSRV can also be specified as an attribute, like below. ```rust #![feature(custom_inner_attributes)] diff --git a/book/src/development/adding_lints.md b/book/src/development/adding_lints.md index 3c3f368a529b..8b4eee8c9d94 100644 --- a/book/src/development/adding_lints.md +++ b/book/src/development/adding_lints.md @@ -443,27 +443,27 @@ value is passed to the constructor in `clippy_lints/lib.rs`. ```rust pub struct ManualStrip { - msrv: Option, + msrv: Msrv, } impl ManualStrip { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } ``` The project's MSRV can then be matched against the feature MSRV in the LintPass -using the `meets_msrv` utility function. +using the `Msrv::meets` method. ``` rust -if !meets_msrv(self.msrv, msrvs::STR_STRIP_PREFIX) { +if !self.msrv.meets(msrvs::STR_STRIP_PREFIX) { return; } ``` -The project's MSRV can also be specified as an inner attribute, which overrides +The project's MSRV can also be specified as an attribute, which overrides the value from `clippy.toml`. This can be accounted for using the `extract_msrv_attr!(LintContext)` macro and passing `LateContext`/`EarlyContext`. @@ -483,19 +483,15 @@ have a case for the version below the MSRV and one with the same contents but for the MSRV version itself. ```rust -#![feature(custom_inner_attributes)] - ... +#[clippy::msrv = "1.44"] fn msrv_1_44() { - #![clippy::msrv = "1.44"] - /* something that would trigger the lint */ } +#[clippy::msrv = "1.45"] fn msrv_1_45() { - #![clippy::msrv = "1.45"] - /* something that would trigger the lint */ } ``` diff --git a/book/src/development/proposals/syntax-tree-patterns.md b/book/src/development/proposals/syntax-tree-patterns.md new file mode 100644 index 000000000000..c5587c4bf908 --- /dev/null +++ b/book/src/development/proposals/syntax-tree-patterns.md @@ -0,0 +1,986 @@ +- Feature Name: syntax-tree-patterns +- Start Date: 2019-03-12 +- RFC PR: [#3875](https://github.com/rust-lang/rust-clippy/pull/3875) + +# Summary + +Introduce a domain-specific language (similar to regular expressions) that +allows to describe lints using *syntax tree patterns*. + + +# Motivation + +Finding parts of a syntax tree (AST, HIR, ...) that have certain properties +(e.g. "*an if that has a block as its condition*") is a major task when writing +lints. For non-trivial lints, it often requires nested pattern matching of AST / +HIR nodes. For example, testing that an expression is a boolean literal requires +the following checks: + +```rust +if let ast::ExprKind::Lit(lit) = &expr.node { + if let ast::LitKind::Bool(_) = &lit.node { + ... + } +} +``` + +Writing this kind of matching code quickly becomes a complex task and the +resulting code is often hard to comprehend. The code below shows a simplified +version of the pattern matching required by the `collapsible_if` lint: + +```rust +// simplified version of the collapsible_if lint +if let ast::ExprKind::If(check, then, None) = &expr.node { + if then.stmts.len() == 1 { + if let ast::StmtKind::Expr(inner) | ast::StmtKind::Semi(inner) = &then.stmts[0].node { + if let ast::ExprKind::If(check_inner, content, None) = &inner.node { + ... + } + } + } +} +``` + +The `if_chain` macro can improve readability by flattening the nested if +statements, but the resulting code is still quite hard to read: + +```rust +// simplified version of the collapsible_if lint +if_chain! { + if let ast::ExprKind::If(check, then, None) = &expr.node; + if then.stmts.len() == 1; + if let ast::StmtKind::Expr(inner) | ast::StmtKind::Semi(inner) = &then.stmts[0].node; + if let ast::ExprKind::If(check_inner, content, None) = &inner.node; + then { + ... + } +} +``` + +The code above matches if expressions that contain only another if expression +(where both ifs don't have an else branch). While it's easy to explain what the +lint does, it's hard to see that from looking at the code samples above. + +Following the motivation above, the first goal this RFC is to **simplify writing +and reading lints**. + +The second part of the motivation is clippy's dependence on unstable +compiler-internal data structures. Clippy lints are currently written against +the compiler's AST / HIR which means that even small changes in these data +structures might break a lot of lints. The second goal of this RFC is to **make +lints independant of the compiler's AST / HIR data structures**. + +# Approach + +A lot of complexity in writing lints currently seems to come from having to +manually implement the matching logic (see code samples above). It's an +imparative style that describes *how* to match a syntax tree node instead of +specifying *what* should be matched against declaratively. In other areas, it's +common to use declarative patterns to describe desired information and let the +implementation do the actual matching. A well-known example of this approach are +[regular expressions](https://en.wikipedia.org/wiki/Regular_expression). Instead +of writing code that detects certain character sequences, one can describe a +search pattern using a domain-specific language and search for matches using +that pattern. The advantage of using a declarative domain-specific language is +that its limited domain (e.g. matching character sequences in the case of +regular expressions) allows to express entities in that domain in a very natural +and expressive way. + +While regular expressions are very useful when searching for patterns in flat +character sequences, they cannot easily be applied to hierarchical data +structures like syntax trees. This RFC therefore proposes a pattern matching +system that is inspired by regular expressions and designed for hierarchical +syntax trees. + +# Guide-level explanation + +This proposal adds a `pattern!` macro that can be used to specify a syntax tree +pattern to search for. A simple pattern is shown below: + +```rust +pattern!{ + my_pattern: Expr = + Lit(Bool(false)) +} +``` + +This macro call defines a pattern named `my_pattern` that can be matched against +an `Expr` syntax tree node. The actual pattern (`Lit(Bool(false))` in this case) +defines which syntax trees should match the pattern. This pattern matches +expressions that are boolean literals with value `false`. + +The pattern can then be used to implement lints in the following way: + +```rust +... + +impl EarlyLintPass for MyAwesomeLint { + fn check_expr(&mut self, cx: &EarlyContext, expr: &syntax::ast::Expr) { + + if my_pattern(expr).is_some() { + cx.span_lint( + MY_AWESOME_LINT, + expr.span, + "This is a match for a simple pattern. Well done!", + ); + } + + } +} +``` + +The `pattern!` macro call expands to a function `my_pattern` that expects a +syntax tree expression as its argument and returns an `Option` that indicates +whether the pattern matched. + +> Note: The result type is explained in more detail in [a later +> section](#the-result-type). For now, it's enough to know that the result is +> `Some` if the pattern matched and `None` otherwise. + +## Pattern syntax + +The following examples demonstate the pattern syntax: + + +#### Any (`_`) + +The simplest pattern is the any pattern. It matches anything and is therefore +similar to regex's `*`. + +```rust +pattern!{ + // matches any expression + my_pattern: Expr = + _ +} +``` + +#### Node (`()`) + +Nodes are used to match a specific variant of an AST node. A node has a name and +a number of arguments that depends on the node type. For example, the `Lit` node +has a single argument that describes the type of the literal. As another +example, the `If` node has three arguments describing the if's condition, then +block and else block. + +```rust +pattern!{ + // matches any expression that is a literal + my_pattern: Expr = + Lit(_) +} + +pattern!{ + // matches any expression that is a boolean literal + my_pattern: Expr = + Lit(Bool(_)) +} + +pattern!{ + // matches if expressions that have a boolean literal in their condition + // Note: The `_?` syntax here means that the else branch is optional and can be anything. + // This is discussed in more detail in the section `Repetition`. + my_pattern: Expr = + If( Lit(Bool(_)) , _, _?) +} +``` + + +#### Literal (``) + +A pattern can also contain Rust literals. These literals match themselves. + +```rust +pattern!{ + // matches the boolean literal false + my_pattern: Expr = + Lit(Bool(false)) +} + +pattern!{ + // matches the character literal 'x' + my_pattern: Expr = + Lit(Char('x')) +} +``` + +#### Alternations (`a | b`) + +```rust +pattern!{ + // matches if the literal is a boolean or integer literal + my_pattern: Lit = + Bool(_) | Int(_) +} + +pattern!{ + // matches if the expression is a char literal with value 'x' or 'y' + my_pattern: Expr = + Lit( Char('x' | 'y') ) +} +``` + +#### Empty (`()`) + +The empty pattern represents an empty sequence or the `None` variant of an +optional. + +```rust +pattern!{ + // matches if the expression is an empty array + my_pattern: Expr = + Array( () ) +} + +pattern!{ + // matches if expressions that don't have an else clause + my_pattern: Expr = + If(_, _, ()) +} +``` + +#### Sequence (` `) + +```rust +pattern!{ + // matches the array [true, false] + my_pattern: Expr = + Array( Lit(Bool(true)) Lit(Bool(false)) ) +} +``` + +#### Repetition (`*`, `+`, `?`, `{n}`, `{n,m}`, `{n,}`) + +Elements may be repeated. The syntax for specifying repetitions is identical to +[regex's syntax](https://docs.rs/regex/1.1.2/regex/#repetitions). + +```rust +pattern!{ + // matches arrays that contain 2 'x's as their last or second-last elements + // Examples: + // ['x', 'x'] match + // ['x', 'x', 'y'] match + // ['a', 'b', 'c', 'x', 'x', 'y'] match + // ['x', 'x', 'y', 'z'] no match + my_pattern: Expr = + Array( _* Lit(Char('x')){2} _? ) +} + +pattern!{ + // matches if expressions that **may or may not** have an else block + // Attn: `If(_, _, _)` matches only ifs that **have** an else block + // + // | if with else block | if witout else block + // If(_, _, _) | match | no match + // If(_, _, _?) | match | match + // If(_, _, ()) | no match | match + my_pattern: Expr = + If(_, _, _?) +} +``` + +#### Named submatch (`#`) + +```rust +pattern!{ + // matches character literals and gives the literal the name foo + my_pattern: Expr = + Lit(Char(_)#foo) +} + +pattern!{ + // matches character literals and gives the char the name bar + my_pattern: Expr = + Lit(Char(_#bar)) +} + +pattern!{ + // matches character literals and gives the expression the name baz + my_pattern: Expr = + Lit(Char(_))#baz +} +``` + +The reason for using named submatches is described in the section [The result +type](#the-result-type). + +### Summary + +The following table gives an summary of the pattern syntax: + +| Syntax | Concept | Examples | +|-------------------------|------------------|--------------------------------------------| +|`_` | Any | `_` | +|`()` | Node | `Lit(Bool(true))`, `If(_, _, _)` | +|`` | Literal | `'x'`, `false`, `101` | +|` \| ` | Alternation | `Char(_) \| Bool(_)` | +|`()` | Empty | `Array( () )` | +|` ` | Sequence | `Tuple( Lit(Bool(_)) Lit(Int(_)) Lit(_) )` | +|`*`
`
+`
`
?`
`
{n}`
`
{n,m}`
`
{n,}` | Repetition





| `Array( _* )`,
`Block( Semi(_)+ )`,
`If(_, _, Block(_)?)`,
`Array( Lit(_){10} )`,
`Lit(_){5,10}`,
`Lit(Bool(_)){10,}` | +|`
#` | Named submatch | `Lit(Int(_))#foo` `Lit(Int(_#bar))` | + + +## The result type + +A lot of lints require checks that go beyond what the pattern syntax described +above can express. For example, a lint might want to check whether a node was +created as part of a macro expansion or whether there's no comment above a node. +Another example would be a lint that wants to match two nodes that have the same +value (as needed by lints like `almost_swapped`). Instead of allowing users to +write these checks into the pattern directly (which might make patterns hard to +read), the proposed solution allows users to assign names to parts of a pattern +expression. When matching a pattern against a syntax tree node, the return value +will contain references to all nodes that were matched by these named +subpatterns. This is similar to capture groups in regular expressions. + +For example, given the following pattern + +```rust +pattern!{ + // matches character literals + my_pattern: Expr = + Lit(Char(_#val_inner)#val)#val_outer +} +``` + +one could get references to the nodes that matched the subpatterns in the +following way: + +```rust +... +fn check_expr(expr: &syntax::ast::Expr) { + if let Some(result) = my_pattern(expr) { + result.val_inner // type: &char + result.val // type: &syntax::ast::Lit + result.val_outer // type: &syntax::ast::Expr + } +} +``` + +The types in the `result` struct depend on the pattern. For example, the +following pattern + +```rust +pattern!{ + // matches arrays of character literals + my_pattern_seq: Expr = + Array( Lit(_)*#foo ) +} +``` + +matches arrays that consist of any number of literal expressions. Because those +expressions are named `foo`, the result struct contains a `foo` attribute which +is a vector of expressions: + +```rust +... +if let Some(result) = my_pattern_seq(expr) { + result.foo // type: Vec<&syntax::ast::Expr> +} +``` + +Another result type occurs when a name is only defined in one branch of an +alternation: + +```rust +pattern!{ + // matches if expression is a boolean or integer literal + my_pattern_alt: Expr = + Lit( Bool(_#bar) | Int(_) ) +} +``` + +In the pattern above, the `bar` name is only defined if the pattern matches a +boolean literal. If it matches an integer literal, the name isn't set. To +account for this, the result struct's `bar` attribute is an option type: + +```rust +... +if let Some(result) = my_pattern_alt(expr) { + result.bar // type: Option<&bool> +} +``` + +It's also possible to use a name in multiple alternation branches if they have +compatible types: + +```rust +pattern!{ + // matches if expression is a boolean or integer literal + my_pattern_mult: Expr = + Lit(_#baz) | Array( Lit(_#baz) ) +} +... +if let Some(result) = my_pattern_mult(expr) { + result.baz // type: &syntax::ast::Lit +} +``` + +Named submatches are a **flat** namespace and this is intended. In the example +above, two different sub-structures are assigned to a flat name. I expect that +for most lints, a flat namespace is sufficient and easier to work with than a +hierarchical one. + +#### Two stages + +Using named subpatterns, users can write lints in two stages. First, a coarse +selection of possible matches is produced by the pattern syntax. In the second +stage, the named subpattern references can be used to do additional tests like +asserting that a node hasn't been created as part of a macro expansion. + +## Implementing clippy lints using patterns + +As a "real-world" example, I re-implemented the `collapsible_if` lint using +patterns. The code can be found +[here](https://github.com/fkohlgrueber/rust-clippy-pattern/blob/039b07ecccaf96d6aa7504f5126720d2c9cceddd/clippy_lints/src/collapsible_if.rs#L88-L163). +The pattern-based version passes all test cases that were written for +`collapsible_if`. + + +# Reference-level explanation + +## Overview + +The following diagram shows the dependencies between the main parts of the +proposed solution: + +``` + Pattern syntax + | + | parsing / lowering + v + PatternTree + ^ + | + | + IsMatch trait + | + | + +---------------+-----------+---------+ + | | | | + v v v v + syntax::ast rustc::hir syn ... +``` + +The pattern syntax described in the previous section is parsed / lowered into +the so-called *PatternTree* data structure that represents a valid syntax tree +pattern. Matching a *PatternTree* against an actual syntax tree (e.g. rust ast / +hir or the syn ast, ...) is done using the *IsMatch* trait. + +The *PatternTree* and the *IsMatch* trait are introduced in more detail in the +following sections. + +## PatternTree + +The core data structure of this RFC is the **PatternTree**. + +It's a data structure similar to rust's AST / HIR, but with the following +differences: + +- The PatternTree doesn't contain parsing information like `Span`s +- The PatternTree can represent alternatives, sequences and optionals + +The code below shows a simplified version of the current PatternTree: + +> Note: The current implementation can be found +> [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/pattern_tree.rs#L50-L96). + + +```rust +pub enum Expr { + Lit(Alt), + Array(Seq), + Block_(Alt), + If(Alt, Alt, Opt), + IfLet( + Alt, + Opt, + ), +} + +pub enum Lit { + Char(Alt), + Bool(Alt), + Int(Alt), +} + +pub enum Stmt { + Expr(Alt), + Semi(Alt), +} + +pub enum BlockType { + Block(Seq), +} +``` + +The `Alt`, `Seq` and `Opt` structs look like these: + +> Note: The current implementation can be found +> [here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/matchers.rs#L35-L60). + +```rust +pub enum Alt { + Any, + Elmt(Box), + Alt(Box, Box), + Named(Box, ...) +} + +pub enum Opt { + Any, // anything, but not None + Elmt(Box), + None, + Alt(Box, Box), + Named(Box, ...) +} + +pub enum Seq { + Any, + Empty, + Elmt(Box), + Repeat(Box, RepeatRange), + Seq(Box, Box), + Alt(Box, Box), + Named(Box, ...) +} + +pub struct RepeatRange { + pub start: usize, + pub end: Option // exclusive +} +``` + +## Parsing / Lowering + +The input of a `pattern!` macro call is parsed into a `ParseTree` first and then +lowered to a `PatternTree`. + +Valid patterns depend on the *PatternTree* definitions. For example, the pattern +`Lit(Bool(_)*)` isn't valid because the parameter type of the `Lit` variant of +the `Expr` enum is `Any` and therefore doesn't support repetition (`*`). As +another example, `Array( Lit(_)* )` is a valid pattern because the parameter of +`Array` is of type `Seq` which allows sequences and repetitions. + +> Note: names in the pattern syntax correspond to *PatternTree* enum +> **variants**. For example, the `Lit` in the pattern above refers to the `Lit` +> variant of the `Expr` enum (`Expr::Lit`), not the `Lit` enum. + +## The IsMatch Trait + +The pattern syntax and the *PatternTree* are independant of specific syntax tree +implementations (rust ast / hir, syn, ...). When looking at the different +pattern examples in the previous sections, it can be seen that the patterns +don't contain any information specific to a certain syntax tree implementation. +In contrast, clippy lints currently match against ast / hir syntax tree nodes +and therefore directly depend on their implementation. + +The connection between the *PatternTree* and specific syntax tree +implementations is the `IsMatch` trait. It defines how to match *PatternTree* +nodes against specific syntax tree nodes. A simplified implementation of the +`IsMatch` trait is shown below: + +```rust +pub trait IsMatch { + fn is_match(&self, other: &'o O) -> bool; +} +``` + +This trait needs to be implemented on each enum of the *PatternTree* (for the +corresponding syntax tree types). For example, the `IsMatch` implementation for +matching `ast::LitKind` against the *PatternTree's* `Lit` enum might look like +this: + +```rust +impl IsMatch for Lit { + fn is_match(&self, other: &ast::LitKind) -> bool { + match (self, other) { + (Lit::Char(i), ast::LitKind::Char(j)) => i.is_match(j), + (Lit::Bool(i), ast::LitKind::Bool(j)) => i.is_match(j), + (Lit::Int(i), ast::LitKind::Int(j, _)) => i.is_match(j), + _ => false, + } + } +} +``` + +All `IsMatch` implementations for matching the current *PatternTree* against +`syntax::ast` can be found +[here](https://github.com/fkohlgrueber/pattern-matching/blob/dfb3bc9fbab69cec7c91e72564a63ebaa2ede638/pattern-match/src/ast_match.rs). + + +# Drawbacks + +#### Performance + +The pattern matching code is currently not optimized for performance, so it +might be slower than hand-written matching code. Additionally, the two-stage +approach (matching against the coarse pattern first and checking for additional +properties later) might be slower than the current practice of checking for +structure and additional properties in one pass. For example, the following lint + +```rust +pattern!{ + pat_if_without_else: Expr = + If( + _, + Block( + Expr( If(_, _, ())#inner ) + | Semi( If(_, _, ())#inner ) + )#then, + () + ) +} +... +fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { + if let Some(result) = pat_if_without_else(expr) { + if !block_starts_with_comment(cx, result.then) { + ... + } +} +``` + +first matches against the pattern and then checks that the `then` block doesn't +start with a comment. Using clippy's current approach, it's possible to check +for these conditions earlier: + +```rust +fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { + if_chain! { + if let ast::ExprKind::If(ref check, ref then, None) = expr.node; + if !block_starts_with_comment(cx, then); + if let Some(inner) = expr_block(then); + if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node; + then { + ... + } + } +} +``` + +Whether or not this causes performance regressions depends on actual patterns. +If it turns out to be a problem, the pattern matching algorithms could be +extended to allow "early filtering" (see the [Early Filtering](#early-filtering) +section in Future Possibilities). + +That being said, I don't see any conceptual limitations regarding pattern +matching performance. + +#### Applicability + +Even though I'd expect that a lot of lints can be written using the proposed +pattern syntax, it's unlikely that all lints can be expressed using patterns. I +suspect that there will still be lints that need to be implemented by writing +custom pattern matching code. This would lead to mix within clippy's codebase +where some lints are implemented using patterns and others aren't. This +inconsistency might be considered a drawback. + + +# Rationale and alternatives + +Specifying lints using syntax tree patterns has a couple of advantages compared +to the current approach of manually writing matching code. First, syntax tree +patterns allow users to describe patterns in a simple and expressive way. This +makes it easier to write new lints for both novices and experts and also makes +reading / modifying existing lints simpler. + +Another advantage is that lints are independent of specific syntax tree +implementations (e.g. AST / HIR, ...). When these syntax tree implementations +change, only the `IsMatch` trait implementations need to be adapted and existing +lints can remain unchanged. This also means that if the `IsMatch` trait +implementations were integrated into the compiler, updating the `IsMatch` +implementations would be required for the compiler to compile successfully. This +could reduce the number of times clippy breaks because of changes in the +compiler. Another advantage of the pattern's independence is that converting an +`EarlyLintPass` lint into a `LatePassLint` wouldn't require rewriting the whole +pattern matching code. In fact, the pattern might work just fine without any +adaptions. + + +## Alternatives + +### Rust-like pattern syntax + +The proposed pattern syntax requires users to know the structure of the +`PatternTree` (which is very similar to the AST's / HIR's structure) and also +the pattern syntax. An alternative would be to introduce a pattern syntax that +is similar to actual Rust syntax (probably like the `quote!` macro). For +example, a pattern that matches `if` expressions that have `false` in their +condition could look like this: + +```rust +if false { + #[*] +} +``` + +#### Problems + +Extending Rust syntax (which is quite complex by itself) with additional syntax +needed for specifying patterns (alternations, sequences, repetisions, named +submatches, ...) might become difficult to read and really hard to parse +properly. + +For example, a pattern that matches a binary operation that has `0` on both +sides might look like this: + +``` +0 #[*:BinOpKind] 0 +``` + +Now consider this slightly more complex example: + +``` +1 + 0 #[*:BinOpKind] 0 +``` + +The parser would need to know the precedence of `#[*:BinOpKind]` because it +affects the structure of the resulting AST. `1 + 0 + 0` is parsed as `(1 + 0) + +0` while `1 + 0 * 0` is parsed as `1 + (0 * 0)`. Since the pattern could be any +`BinOpKind`, the precedence cannot be known in advance. + +Another example of a problem would be named submatches. Take a look at this +pattern: + +```rust +fn test() { + 1 #foo +} +``` + +Which node is `#foo` referring to? `int`, `ast::Lit`, `ast::Expr`, `ast::Stmt`? +Naming subpatterns in a rust-like syntax is difficult because a lot of AST nodes +don't have a syntactic element that can be used to put the name tag on. In these +situations, the only sensible option would be to assign the name tag to the +outermost node (`ast::Stmt` in the example above), because the information of +all child nodes can be retrieved through the outermost node. The problem with +this then would be that accessing inner nodes (like `ast::Lit`) would again +require manual pattern matching. + +In general, Rust syntax contains a lot of code structure implicitly. This +structure is reconstructed during parsing (e.g. binary operations are +reconstructed using operator precedence and left-to-right) and is one of the +reasons why parsing is a complex task. The advantage of this approach is that +writing code is simpler for users. + +When writing *syntax tree patterns*, each element of the hierarchy might have +alternatives, repetitions, etc.. Respecting that while still allowing +human-friendly syntax that contains structure implicitly seems to be really +complex, if not impossible. + +Developing such a syntax would also require to maintain a custom parser that is +at least as complex as the Rust parser itself. Additionally, future changes in +the Rust syntax might be incompatible with such a syntax. + +In summary, I think that developing such a syntax would introduce a lot of +complexity to solve a relatively minor problem. + +The issue of users not knowing about the *PatternTree* structure could be solved +by a tool that, given a rust program, generates a pattern that matches only this +program (similar to the clippy author lint). + +For some simple cases (like the first example above), it might be possible to +successfully mix Rust and pattern syntax. This space could be further explored +in a future extension. + +# Prior art + +The pattern syntax is heavily inspired by regular expressions (repetitions, +alternatives, sequences, ...). + +From what I've seen until now, other linters also implement lints that directly +work on syntax tree data structures, just like clippy does currently. I would +therefore consider the pattern syntax to be *new*, but please correct me if I'm +wrong. + +# Unresolved questions + +#### How to handle multiple matches? + +When matching a syntax tree node against a pattern, there are possibly multiple +ways in which the pattern can be matched. A simple example of this would be the +following pattern: + +```rust +pattern!{ + my_pattern: Expr = + Array( _* Lit(_)+#literals) +} +``` + +This pattern matches arrays that end with at least one literal. Now given the +array `[x, 1, 2]`, should `1` be matched as part of the `_*` or the `Lit(_)+` +part of the pattern? The difference is important because the named submatch +`#literals` would contain 1 or 2 elements depending how the pattern is matched. +In regular expressions, this problem is solved by matching "greedy" by default +and "non-greedy" optionally. + +I haven't looked much into this yet because I don't know how relevant it is for +most lints. The current implementation simply returns the first match it finds. + +# Future possibilities + +#### Implement rest of Rust Syntax + +The current project only implements a small part of the Rust syntax. In the +future, this should incrementally be extended to more syntax to allow +implementing more lints. Implementing more of the Rust syntax requires extending +the `PatternTree` and `IsMatch` implementations, but should be relatively +straight-forward. + +#### Early filtering + +As described in the *Drawbacks/Performance* section, allowing additional checks +during the pattern matching might be beneficial. + +The pattern below shows how this could look like: + +```rust +pattern!{ + pat_if_without_else: Expr = + If( + _, + Block( + Expr( If(_, _, ())#inner ) + | Semi( If(_, _, ())#inner ) + )#then, + () + ) + where + !in_macro(#then.span); +} +``` + +The difference compared to the currently proposed two-stage filtering is that +using early filtering, the condition (`!in_macro(#then.span)` in this case) +would be evaluated as soon as the `Block(_)#then` was matched. + +Another idea in this area would be to introduce a syntax for backreferences. +They could be used to require that multiple parts of a pattern should match the +same value. For example, the `assign_op_pattern` lint that searches for `a = a +op b` and recommends changing it to `a op= b` requires that both occurrances of +`a` are the same. Using `=#...` as syntax for backreferences, the lint could be +implemented like this: + +```rust +pattern!{ + assign_op_pattern: Expr = + Assign(_#target, Binary(_, =#target, _) +} +``` + +#### Match descendant + +A lot of lints currently implement custom visitors that check whether any +subtree (which might not be a direct descendant) of the current node matches +some properties. This cannot be expressed with the proposed pattern syntax. +Extending the pattern syntax to allow patterns like "a function that contains at +least two return statements" could be a practical addition. + +#### Negation operator for alternatives + +For patterns like "a literal that is not a boolean literal" one currently needs +to list all alternatives except the boolean case. Introducing a negation +operator that allows to write `Lit(!Bool(_))` might be a good idea. This pattern +would be eqivalent to `Lit( Char(_) | Int(_) )` (given that currently only three +literal types are implemented). + +#### Functional composition + +Patterns currently don't have any concept of composition. This leads to +repetitions within patterns. For example, one of the collapsible-if patterns +currently has to be written like this: + +```rust +pattern!{ + pat_if_else: Expr = + If( + _, + _, + Block_( + Block( + Expr((If(_, _, _?) | IfLet(_, _?))#else_) | + Semi((If(_, _, _?) | IfLet(_, _?))#else_) + )#block_inner + )#block + ) | + IfLet( + _, + Block_( + Block( + Expr((If(_, _, _?) | IfLet(_, _?))#else_) | + Semi((If(_, _, _?) | IfLet(_, _?))#else_) + )#block_inner + )#block + ) +} +``` + +If patterns supported defining functions of subpatterns, the code could be +simplified as follows: + +```rust +pattern!{ + fn expr_or_semi(expr: Expr) -> Stmt { + Expr(expr) | Semi(expr) + } + fn if_or_if_let(then: Block, else: Opt) -> Expr { + If(_, then, else) | IfLet(then, else) + } + pat_if_else: Expr = + if_or_if_let( + _, + Block_( + Block( + expr_or_semi( if_or_if_let(_, _?)#else_ ) + )#block_inner + )#block + ) +} +``` + +Additionally, common patterns like `expr_or_semi` could be shared between +different lints. + +#### Clippy Pattern Author + +Another improvement could be to create a tool that, given some valid Rust +syntax, generates a pattern that matches this syntax exactly. This would make +starting to write a pattern easier. A user could take a look at the patterns +generated for a couple of Rust code examples and use that information to write a +pattern that matches all of them. + +This is similar to clippy's author lint. + +#### Supporting other syntaxes + +Most of the proposed system is language-agnostic. For example, the pattern +syntax could also be used to describe patterns for other programming languages. + +In order to support other languages' syntaxes, one would need to implement +another `PatternTree` that sufficiently describes the languages' AST and +implement `IsMatch` for this `PatternTree` and the languages' AST. + +One aspect of this is that it would even be possible to write lints that work on +the pattern syntax itself. For example, when writing the following pattern + + +```rust +pattern!{ + my_pattern: Expr = + Array( Lit(Bool(false)) Lit(Bool(false)) ) +} +``` + +a lint that works on the pattern syntax's AST could suggest using this pattern +instead: + +```rust +pattern!{ + my_pattern: Expr = + Array( Lit(Bool(false)){2} ) +} +``` + +In the future, clippy could use this system to also provide lints for custom +syntaxes like those found in macros. diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs index 9e15f1504fa9..ec7f1dd0d846 100644 --- a/clippy_dev/src/new_lint.rs +++ b/clippy_dev/src/new_lint.rs @@ -120,7 +120,7 @@ fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> { let new_lint = if enable_msrv { format!( - "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv)));\n ", + "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv())));\n ", lint_pass = lint.pass, ctor_arg = if lint.pass == "late" { "_" } else { "" }, module_name = lint.name, @@ -238,10 +238,9 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String { result.push_str(&if enable_msrv { formatdoc!( r#" - use clippy_utils::msrvs; + use clippy_utils::msrvs::{{self, Msrv}}; {pass_import} use rustc_lint::{{{context_import}, {pass_type}, LintContext}}; - use rustc_semver::RustcVersion; use rustc_session::{{declare_tool_lint, impl_lint_pass}}; "# @@ -263,12 +262,12 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String { formatdoc!( r#" pub struct {name_camel} {{ - msrv: Option, + msrv: Msrv, }} impl {name_camel} {{ #[must_use] - pub fn new(msrv: Option) -> Self {{ + pub fn new(msrv: Msrv) -> Self {{ Self {{ msrv }} }} }} @@ -357,15 +356,14 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R let _ = writedoc!( lint_file_contents, r#" - use clippy_utils::{{meets_msrv, msrvs}}; + use clippy_utils::msrvs::{{self, Msrv}}; use rustc_lint::{{{context_import}, LintContext}}; - use rustc_semver::RustcVersion; use super::{name_upper}; // TODO: Adjust the parameters as necessary - pub(super) fn check(cx: &{context_import}, msrv: Option) {{ - if !meets_msrv(msrv, todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{ + pub(super) fn check(cx: &{context_import}, msrv: &Msrv) {{ + if !msrv.meets(todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{ return; }} todo!(); diff --git a/clippy_lints/src/almost_complete_letter_range.rs b/clippy_lints/src/almost_complete_letter_range.rs index df92579a85df..52beaf504a4e 100644 --- a/clippy_lints/src/almost_complete_letter_range.rs +++ b/clippy_lints/src/almost_complete_letter_range.rs @@ -1,11 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{trim_span, walk_span_to_context}; -use clippy_utils::{meets_msrv, msrvs}; use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; @@ -33,10 +32,10 @@ declare_clippy_lint! { impl_lint_pass!(AlmostCompleteLetterRange => [ALMOST_COMPLETE_LETTER_RANGE]); pub struct AlmostCompleteLetterRange { - msrv: Option, + msrv: Msrv, } impl AlmostCompleteLetterRange { - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -46,7 +45,7 @@ impl EarlyLintPass for AlmostCompleteLetterRange { let ctxt = e.span.ctxt(); let sugg = if let Some(start) = walk_span_to_context(start.span, ctxt) && let Some(end) = walk_span_to_context(end.span, ctxt) - && meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE) + && self.msrv.meets(msrvs::RANGE_INCLUSIVE) { Some((trim_span(cx.sess().source_map(), start.between(end)), "..=")) } else { @@ -60,7 +59,7 @@ impl EarlyLintPass for AlmostCompleteLetterRange { if let PatKind::Range(Some(start), Some(end), kind) = &p.kind && matches!(kind.node, RangeEnd::Excluded) { - let sugg = if meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE) { + let sugg = if self.msrv.meets(msrvs::RANGE_INCLUSIVE) { "..=" } else { "..." diff --git a/clippy_lints/src/approx_const.rs b/clippy_lints/src/approx_const.rs index 724490fb4959..ccf82f132f4e 100644 --- a/clippy_lints/src/approx_const.rs +++ b/clippy_lints/src/approx_const.rs @@ -1,5 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::{meets_msrv, msrvs}; +use clippy_utils::msrvs::{self, Msrv}; use rustc_ast::ast::{FloatTy, LitFloatType, LitKind}; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -63,12 +63,12 @@ const KNOWN_CONSTS: [(f64, &str, usize, Option); 19] = [ ]; pub struct ApproxConstant { - msrv: Option, + msrv: Msrv, } impl ApproxConstant { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } @@ -87,7 +87,7 @@ impl ApproxConstant { let s = s.as_str(); if s.parse::().is_ok() { for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS { - if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| meets_msrv(self.msrv, msrv)) { + if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| self.msrv.meets(msrv)) { span_lint_and_help( cx, APPROX_CONSTANT, diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 018f10f25886..0710ac0bb0a7 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -2,9 +2,8 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then}; use clippy_utils::macros::{is_panic, macro_backtrace}; -use clippy_utils::msrvs; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments}; -use clippy_utils::{extract_msrv_attr, meets_msrv}; use if_chain::if_chain; use rustc_ast::{AttrKind, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem}; use rustc_errors::Applicability; @@ -14,7 +13,6 @@ use rustc_hir::{ use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; use rustc_span::symbol::Symbol; @@ -599,7 +597,7 @@ fn is_word(nmi: &NestedMetaItem, expected: Symbol) -> bool { } pub struct EarlyAttributes { - pub msrv: Option, + pub msrv: Msrv, } impl_lint_pass!(EarlyAttributes => [ @@ -614,7 +612,7 @@ impl EarlyLintPass for EarlyAttributes { } fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) { - check_deprecated_cfg_attr(cx, attr, self.msrv); + check_deprecated_cfg_attr(cx, attr, &self.msrv); check_mismatched_target_os(cx, attr); } @@ -654,9 +652,9 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It } } -fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: Option) { +fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) { if_chain! { - if meets_msrv(msrv, msrvs::TOOL_ATTRIBUTES); + if msrv.meets(msrvs::TOOL_ATTRIBUTES); // check cfg_attr if attr.has_name(sym::cfg_attr); if let Some(items) = attr.meta_item_list(); diff --git a/clippy_lints/src/casts/cast_abs_to_unsigned.rs b/clippy_lints/src/casts/cast_abs_to_unsigned.rs index 3f1edabe6c50..442262983337 100644 --- a/clippy_lints/src/casts/cast_abs_to_unsigned.rs +++ b/clippy_lints/src/casts/cast_abs_to_unsigned.rs @@ -1,11 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::sugg::Sugg; -use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty}; -use rustc_semver::RustcVersion; use super::CAST_ABS_TO_UNSIGNED; @@ -15,9 +14,9 @@ pub(super) fn check( cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, - msrv: Option, + msrv: &Msrv, ) { - if meets_msrv(msrv, msrvs::UNSIGNED_ABS) + if msrv.meets(msrvs::UNSIGNED_ABS) && let ty::Int(from) = cast_from.kind() && let ty::Uint(to) = cast_to.kind() && let ExprKind::MethodCall(method_path, receiver, ..) = cast_expr.kind diff --git a/clippy_lints/src/casts/cast_lossless.rs b/clippy_lints/src/casts/cast_lossless.rs index 13c403234dad..cf07e050ccce 100644 --- a/clippy_lints/src/casts/cast_lossless.rs +++ b/clippy_lints/src/casts/cast_lossless.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::in_constant; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; use clippy_utils::ty::is_isize_or_usize; -use clippy_utils::{in_constant, meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, FloatTy, Ty}; -use rustc_semver::RustcVersion; use super::{utils, CAST_LOSSLESS}; @@ -16,7 +16,7 @@ pub(super) fn check( cast_op: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, - msrv: Option, + msrv: &Msrv, ) { if !should_lint(cx, expr, cast_from, cast_to, msrv) { return; @@ -57,13 +57,7 @@ pub(super) fn check( ); } -fn should_lint( - cx: &LateContext<'_>, - expr: &Expr<'_>, - cast_from: Ty<'_>, - cast_to: Ty<'_>, - msrv: Option, -) -> bool { +fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool { // Do not suggest using From in consts/statics until it is valid to do so (see #2267). if in_constant(cx, expr.hir_id) { return false; @@ -89,7 +83,7 @@ fn should_lint( }; !is_isize_or_usize(cast_from) && from_nbits < to_nbits }, - (false, true) if matches!(cast_from.kind(), ty::Bool) && meets_msrv(msrv, msrvs::FROM_BOOL) => true, + (false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(msrvs::FROM_BOOL) => true, (_, _) => { matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64)) }, diff --git a/clippy_lints/src/casts/cast_possible_truncation.rs b/clippy_lints/src/casts/cast_possible_truncation.rs index adbcfd3189b7..a6376484914b 100644 --- a/clippy_lints/src/casts/cast_possible_truncation.rs +++ b/clippy_lints/src/casts/cast_possible_truncation.rs @@ -118,12 +118,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, }; let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx); - let cast_from_ptr_size = def.repr().int.map_or(true, |ty| { - matches!( - ty, - IntegerType::Pointer(_), - ) - }); + let cast_from_ptr_size = def.repr().int.map_or(true, |ty| matches!(ty, IntegerType::Pointer(_),)); let suffix = match (cast_from_ptr_size, is_isize_or_usize(cast_to)) { (false, false) if from_nbits > to_nbits => "", (true, false) if from_nbits > to_nbits => "", diff --git a/clippy_lints/src/casts/cast_slice_different_sizes.rs b/clippy_lints/src/casts/cast_slice_different_sizes.rs index d31d10d22b92..e862f13e69fc 100644 --- a/clippy_lints/src/casts/cast_slice_different_sizes.rs +++ b/clippy_lints/src/casts/cast_slice_different_sizes.rs @@ -1,16 +1,16 @@ -use clippy_utils::{diagnostics::span_lint_and_then, meets_msrv, msrvs, source}; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::{diagnostics::span_lint_and_then, source}; use if_chain::if_chain; use rustc_ast::Mutability; use rustc_hir::{Expr, ExprKind, Node}; use rustc_lint::LateContext; use rustc_middle::ty::{self, layout::LayoutOf, Ty, TypeAndMut}; -use rustc_semver::RustcVersion; use super::CAST_SLICE_DIFFERENT_SIZES; -pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Option) { +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Msrv) { // suggestion is invalid if `ptr::slice_from_raw_parts` does not exist - if !meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS) { + if !msrv.meets(msrvs::PTR_SLICE_RAW_PARTS) { return; } diff --git a/clippy_lints/src/casts/cast_slice_from_raw_parts.rs b/clippy_lints/src/casts/cast_slice_from_raw_parts.rs index 284ef165998a..627b795d6edd 100644 --- a/clippy_lints/src/casts/cast_slice_from_raw_parts.rs +++ b/clippy_lints/src/casts/cast_slice_from_raw_parts.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{match_def_path, meets_msrv, msrvs, paths}; +use clippy_utils::{match_def_path, paths}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{def_id::DefId, Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty}; -use rustc_semver::RustcVersion; use super::CAST_SLICE_FROM_RAW_PARTS; @@ -25,15 +25,9 @@ fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option { } } -pub(super) fn check( - cx: &LateContext<'_>, - expr: &Expr<'_>, - cast_expr: &Expr<'_>, - cast_to: Ty<'_>, - msrv: Option, -) { +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>, msrv: &Msrv) { if_chain! { - if meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS); + if msrv.meets(msrvs::PTR_SLICE_RAW_PARTS); if let ty::RawPtr(ptrty) = cast_to.kind(); if let ty::Slice(_) = ptrty.ty.kind(); if let ExprKind::Call(fun, [ptr_arg, len_arg]) = cast_expr.peel_blocks().kind; diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs index 7148b5e6ebf4..c6d505c4a181 100644 --- a/clippy_lints/src/casts/mod.rs +++ b/clippy_lints/src/casts/mod.rs @@ -21,11 +21,11 @@ mod ptr_as_ptr; mod unnecessary_cast; mod utils; -use clippy_utils::{is_hir_ty_cfg_dependant, meets_msrv, msrvs}; +use clippy_utils::is_hir_ty_cfg_dependant; +use clippy_utils::msrvs::{self, Msrv}; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -648,12 +648,12 @@ declare_clippy_lint! { } pub struct Casts { - msrv: Option, + msrv: Msrv, } impl Casts { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -686,7 +686,7 @@ impl_lint_pass!(Casts => [ impl<'tcx> LateLintPass<'tcx> for Casts { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if !in_external_macro(cx.sess(), expr.span) { - ptr_as_ptr::check(cx, expr, self.msrv); + ptr_as_ptr::check(cx, expr, &self.msrv); } if expr.span.from_expansion() { @@ -705,7 +705,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts { if unnecessary_cast::check(cx, expr, cast_expr, cast_from, cast_to) { return; } - cast_slice_from_raw_parts::check(cx, expr, cast_expr, cast_to, self.msrv); + cast_slice_from_raw_parts::check(cx, expr, cast_expr, cast_to, &self.msrv); as_ptr_cast_mut::check(cx, expr, cast_expr, cast_to); fn_to_numeric_cast_any::check(cx, expr, cast_expr, cast_from, cast_to); fn_to_numeric_cast::check(cx, expr, cast_expr, cast_from, cast_to); @@ -717,16 +717,16 @@ impl<'tcx> LateLintPass<'tcx> for Casts { cast_possible_wrap::check(cx, expr, cast_from, cast_to); cast_precision_loss::check(cx, expr, cast_from, cast_to); cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to); - cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv); + cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv); cast_nan_to_int::check(cx, expr, cast_expr, cast_from, cast_to); } - cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv); + cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv); cast_enum_constructor::check(cx, expr, cast_expr, cast_from); } as_underscore::check(cx, expr, cast_to_hir); - if meets_msrv(self.msrv, msrvs::BORROW_AS_PTR) { + if self.msrv.meets(msrvs::BORROW_AS_PTR) { borrow_as_ptr::check(cx, expr, cast_expr, cast_to_hir); } } @@ -734,8 +734,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts { cast_ref_to_mut::check(cx, expr); cast_ptr_alignment::check(cx, expr); char_lit_as_u8::check(cx, expr); - ptr_as_ptr::check(cx, expr, self.msrv); - cast_slice_different_sizes::check(cx, expr, self.msrv); + ptr_as_ptr::check(cx, expr, &self.msrv); + cast_slice_different_sizes::check(cx, expr, &self.msrv); } extract_msrv_attr!(LateContext); diff --git a/clippy_lints/src/casts/ptr_as_ptr.rs b/clippy_lints/src/casts/ptr_as_ptr.rs index b9509ca656f7..15ffb00da88b 100644 --- a/clippy_lints/src/casts/ptr_as_ptr.rs +++ b/clippy_lints/src/casts/ptr_as_ptr.rs @@ -1,19 +1,18 @@ use std::borrow::Cow; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::sugg::Sugg; -use clippy_utils::{meets_msrv, msrvs}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, Mutability, TyKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, TypeAndMut}; -use rustc_semver::RustcVersion; use super::PTR_AS_PTR; -pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Option) { - if !meets_msrv(msrv, msrvs::POINTER_CAST) { +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) { + if !msrv.meets(msrvs::POINTER_CAST) { return; } diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index c8596987e4d7..7e23318076cf 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::get_parent_expr; use clippy_utils::numeric_literal::NumericLiteral; use clippy_utils::source::snippet_opt; +use clippy_utils::{get_parent_expr, path_to_local}; use if_chain::if_chain; use rustc_ast::{LitFloatType, LitIntType, LitKind}; use rustc_errors::Applicability; @@ -75,13 +75,26 @@ pub(super) fn check<'tcx>( } if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { + if let Some(id) = path_to_local(cast_expr) + && let Some(span) = cx.tcx.hir().opt_span(id) + && span.ctxt() != cast_expr.span.ctxt() + { + // Binding context is different than the identifiers context. + // Weird macro wizardry could be involved here. + return false; + } + span_lint_and_sugg( cx, UNNECESSARY_CAST, expr.span, &format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"), "try", - cast_str, + if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::AddrOf(..))) { + format!("{{ {cast_str} }}") + } else { + cast_str + }, Applicability::MachineApplicable, ); return true; diff --git a/clippy_lints/src/checked_conversions.rs b/clippy_lints/src/checked_conversions.rs index 78e9921f036f..9102a89e3772 100644 --- a/clippy_lints/src/checked_conversions.rs +++ b/clippy_lints/src/checked_conversions.rs @@ -1,14 +1,14 @@ //! lint on manually implemented checked conversions that could be transformed into `try_from` use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{in_constant, is_integer_literal, meets_msrv, msrvs, SpanlessEq}; +use clippy_utils::{in_constant, is_integer_literal, SpanlessEq}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, QPath, TyKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -37,12 +37,12 @@ declare_clippy_lint! { } pub struct CheckedConversions { - msrv: Option, + msrv: Msrv, } impl CheckedConversions { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -51,7 +51,7 @@ impl_lint_pass!(CheckedConversions => [CHECKED_CONVERSIONS]); impl<'tcx> LateLintPass<'tcx> for CheckedConversions { fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::TRY_FROM) { + if !self.msrv.meets(msrvs::TRY_FROM) { return; } diff --git a/clippy_lints/src/collapsible_if.rs b/clippy_lints/src/collapsible_if.rs index 90430b71a0ef..b38e09dc09f4 100644 --- a/clippy_lints/src/collapsible_if.rs +++ b/clippy_lints/src/collapsible_if.rs @@ -160,11 +160,13 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: & if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.kind; // Prevent triggering on `if c { if let a = b { .. } }`. if !matches!(check_inner.kind, ast::ExprKind::Let(..)); - if expr.span.ctxt() == inner.span.ctxt(); + let ctxt = expr.span.ctxt(); + if inner.span.ctxt() == ctxt; then { span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this `if` statement can be collapsed", |diag| { - let lhs = Sugg::ast(cx, check, ".."); - let rhs = Sugg::ast(cx, check_inner, ".."); + let mut app = Applicability::MachineApplicable; + let lhs = Sugg::ast(cx, check, "..", ctxt, &mut app); + let rhs = Sugg::ast(cx, check_inner, "..", ctxt, &mut app); diag.span_suggestion( expr.span, "collapse nested if block", @@ -173,7 +175,7 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: & lhs.and(&rhs), snippet_block(cx, content.span, "..", Some(expr.span)), ), - Applicability::MachineApplicable, // snippet + app, // snippet ); }); } diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 0d3fc43a6443..e4d76f07d6b4 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -177,6 +177,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::from_raw_with_void_ptr::FROM_RAW_WITH_VOID_PTR_INFO, crate::from_str_radix_10::FROM_STR_RADIX_10_INFO, crate::functions::DOUBLE_MUST_USE_INFO, + crate::functions::MISNAMED_GETTERS_INFO, crate::functions::MUST_USE_CANDIDATE_INFO, crate::functions::MUST_USE_UNIT_INFO, crate::functions::NOT_UNSAFE_PTR_ARG_DEREF_INFO, @@ -583,6 +584,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::types::TYPE_COMPLEXITY_INFO, crate::types::VEC_BOX_INFO, crate::undocumented_unsafe_blocks::UNDOCUMENTED_UNSAFE_BLOCKS_INFO, + crate::undocumented_unsafe_blocks::UNNECESSARY_SAFETY_COMMENT_INFO, crate::unicode::INVISIBLE_CHARACTERS_INFO, crate::unicode::NON_ASCII_LITERAL_INFO, crate::unicode::UNICODE_NOT_NFC_INFO, diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 47ea98956be2..38329659e02b 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -1,12 +1,13 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then}; use clippy_utils::mir::{enclosing_mir, expr_local, local_assignments, used_exactly_once, PossibleBorrowerMap}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet_with_applicability, snippet_with_context}; use clippy_utils::sugg::has_enclosing_paren; use clippy_utils::ty::{expr_sig, is_copy, peel_mid_ty_refs, ty_sig, variant_of_res}; use clippy_utils::{ - fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, meets_msrv, msrvs, path_to_local, - walk_to_expr_usage, + fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, path_to_local, walk_to_expr_usage, }; + use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX}; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::graph::iterate::{CycleDetector, TriColorDepthFirstSearch}; @@ -28,7 +29,6 @@ use rustc_middle::ty::{ self, Binder, BoundVariableKind, Clause, EarlyBinder, FnSig, GenericArgKind, List, ParamTy, PredicateKind, ProjectionPredicate, Ty, TyCtxt, TypeVisitable, TypeckResults, }; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{symbol::sym, Span, Symbol}; use rustc_trait_selection::infer::InferCtxtExt as _; @@ -181,12 +181,12 @@ pub struct Dereferencing<'tcx> { possible_borrowers: Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>, // `IntoIterator` for arrays requires Rust 1.53. - msrv: Option, + msrv: Msrv, } impl<'tcx> Dereferencing<'tcx> { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, ..Dereferencing::default() @@ -286,26 +286,27 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { match (self.state.take(), kind) { (None, kind) => { let expr_ty = typeck.expr_ty(expr); - let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, self.msrv); + let (position, adjustments) = walk_parents(cx, &mut self.possible_borrowers, expr, &self.msrv); match kind { RefOp::Deref => { + let sub_ty = typeck.expr_ty(sub_expr); if let Position::FieldAccess { name, of_union: false, } = position - && !ty_contains_field(typeck.expr_ty(sub_expr), name) + && !ty_contains_field(sub_ty, name) { self.state = Some(( State::ExplicitDerefField { name }, StateData { span: expr.span, hir_id: expr.hir_id, position }, )); - } else if position.is_deref_stable() { + } else if position.is_deref_stable() && sub_ty.is_ref() { self.state = Some(( State::ExplicitDeref { mutability: None }, StateData { span: expr.span, hir_id: expr.hir_id, position }, )); } - } + }, RefOp::Method(target_mut) if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id) && position.lint_explicit_deref() => @@ -320,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { StateData { span: expr.span, hir_id: expr.hir_id, - position + position, }, )); }, @@ -394,7 +395,11 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { msg, snip_expr, }), - StateData { span: expr.span, hir_id: expr.hir_id, position }, + StateData { + span: expr.span, + hir_id: expr.hir_id, + position, + }, )); } else if position.is_deref_stable() // Auto-deref doesn't combine with other adjustments @@ -406,7 +411,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> { StateData { span: expr.span, hir_id: expr.hir_id, - position + position, }, )); } @@ -698,7 +703,7 @@ fn walk_parents<'tcx>( cx: &LateContext<'tcx>, possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>, e: &'tcx Expr<'_>, - msrv: Option, + msrv: &Msrv, ) -> (Position, &'tcx [Adjustment<'tcx>]) { let mut adjustments = [].as_slice(); let mut precedence = 0i8; @@ -862,7 +867,11 @@ fn walk_parents<'tcx>( } && impl_ty.is_ref() && let infcx = cx.tcx.infer_ctxt().build() && infcx - .type_implements_trait(trait_id, [impl_ty.into()].into_iter().chain(subs.iter().copied()), cx.param_env) + .type_implements_trait( + trait_id, + [impl_ty.into()].into_iter().chain(subs.iter().copied()), + cx.param_env, + ) .must_apply_modulo_regions() { return Some(Position::MethodReceiverRefImpl) @@ -1078,7 +1087,7 @@ fn needless_borrow_impl_arg_position<'tcx>( param_ty: ParamTy, mut expr: &Expr<'tcx>, precedence: i8, - msrv: Option, + msrv: &Msrv, ) -> Position { let destruct_trait_def_id = cx.tcx.lang_items().destruct_trait(); let sized_trait_def_id = cx.tcx.lang_items().sized_trait(); @@ -1178,7 +1187,7 @@ fn needless_borrow_impl_arg_position<'tcx>( && let ty::Param(param_ty) = trait_predicate.self_ty().kind() && let GenericArgKind::Type(ty) = substs_with_referent_ty[param_ty.index as usize].unpack() && ty.is_array() - && !meets_msrv(msrv, msrvs::ARRAY_INTO_ITERATOR) + && !msrv.meets(msrvs::ARRAY_INTO_ITERATOR) { return false; } diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index d870e0ceef47..9e596ca8157e 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -14,8 +14,8 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; use rustc_middle::traits::Reveal; use rustc_middle::ty::{ - self, Binder, BoundConstness, Clause, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, TraitRef, - Ty, TyCtxt, + self, Binder, BoundConstness, Clause, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, + TraitRef, Ty, TyCtxt, }; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index ae5f9424b232..cdc23a4d2273 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -253,7 +253,7 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.66.0"] pub UNNECESSARY_SAFETY_DOC, - style, + restriction, "`pub fn` or `pub trait` with `# Safety` docs" } diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index f34cbee03558..3543910c3b55 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -11,7 +11,7 @@ use rustc_hir::{Closure, Expr, ExprKind, Param, PatKind, Unsafety}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow}; use rustc_middle::ty::binding::BindingMode; -use rustc_middle::ty::{self, Ty, TypeVisitable}; +use rustc_middle::ty::{self, EarlyBinder, SubstsRef, Ty, TypeVisitable}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::symbol::sym; @@ -125,7 +125,12 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction { if let Some(mut snippet) = snippet_opt(cx, callee.span) { if let Some(fn_mut_id) = cx.tcx.lang_items().fn_mut_trait() && let args = cx.tcx.erase_late_bound_regions(substs.as_closure().sig()).inputs() - && implements_trait(cx, callee_ty.peel_refs(), fn_mut_id, &args.iter().copied().map(Into::into).collect::>()) + && implements_trait( + cx, + callee_ty.peel_refs(), + fn_mut_id, + &args.iter().copied().map(Into::into).collect::>(), + ) && path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr)) { // Mutable closure is used after current expr; we cannot consume it. @@ -152,7 +157,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction { if check_sig(cx, closure_ty, call_ty); then { span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure", |diag| { - let name = get_ufcs_type_name(cx, method_def_id); + let name = get_ufcs_type_name(cx, method_def_id, substs); diag.span_suggestion( expr.span, "replace the closure with the method itself", @@ -222,7 +227,7 @@ fn check_sig<'tcx>(cx: &LateContext<'tcx>, closure_ty: Ty<'tcx>, call_ty: Ty<'tc cx.tcx.erase_late_bound_regions(closure_sig) == cx.tcx.erase_late_bound_regions(call_sig) } -fn get_ufcs_type_name(cx: &LateContext<'_>, method_def_id: DefId) -> String { +fn get_ufcs_type_name<'tcx>(cx: &LateContext<'tcx>, method_def_id: DefId, substs: SubstsRef<'tcx>) -> String { let assoc_item = cx.tcx.associated_item(method_def_id); let def_id = assoc_item.container_id(cx.tcx); match assoc_item.container { @@ -231,6 +236,15 @@ fn get_ufcs_type_name(cx: &LateContext<'_>, method_def_id: DefId) -> String { let ty = cx.tcx.type_of(def_id); match ty.kind() { ty::Adt(adt, _) => cx.tcx.def_path_str(adt.did()), + ty::Array(..) + | ty::Dynamic(..) + | ty::Never + | ty::RawPtr(_) + | ty::Ref(..) + | ty::Slice(_) + | ty::Tuple(_) => { + format!("<{}>", EarlyBinder(ty).subst(cx.tcx, substs)) + }, _ => ty.to_string(), } }, diff --git a/clippy_lints/src/exit.rs b/clippy_lints/src/exit.rs index 407dd1b39575..9c8b0d076dfd 100644 --- a/clippy_lints/src/exit.rs +++ b/clippy_lints/src/exit.rs @@ -7,21 +7,34 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; declare_clippy_lint! { /// ### What it does - /// `exit()` terminates the program and doesn't provide a - /// stack trace. + /// Detects calls to the `exit()` function which terminates the program. /// /// ### Why is this bad? - /// Ideally a program is terminated by finishing + /// Exit terminates the program at the location it is called. For unrecoverable + /// errors `panics` should be used to provide a stacktrace and potentualy other + /// information. A normal termination or one with an error code should happen in /// the main function. /// /// ### Example - /// ```ignore + /// ``` /// std::process::exit(0) /// ``` + /// + /// Use instead: + /// + /// ```ignore + /// // To provide a stacktrace and additional information + /// panic!("message"); + /// + /// // or a main method with a return + /// fn main() -> Result<(), i32> { + /// Ok(()) + /// } + /// ``` #[clippy::version = "1.41.0"] pub EXIT, restriction, - "`std::process::exit` is called, terminating the program" + "detects `std::process::exit` calls" } declare_lint_pass!(Exit => [EXIT]); diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index f0fe845d3303..111b624f5299 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -1,19 +1,22 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; -use clippy_utils::macros::FormatParamKind::{Implicit, Named, Numbered, Starred}; +use clippy_utils::is_diag_trait_item; +use clippy_utils::macros::FormatParamKind::{Implicit, Named, NamedInline, Numbered, Starred}; use clippy_utils::macros::{ is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam, FormatParamUsage, }; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; use clippy_utils::ty::{implements_trait, is_type_diagnostic_item}; -use clippy_utils::{is_diag_trait_item, meets_msrv, msrvs}; use if_chain::if_chain; use itertools::Itertools; -use rustc_errors::Applicability; +use rustc_errors::{ + Applicability, + SuggestionStyle::{CompletelyHidden, ShowCode}, +}; use rustc_hir::{Expr, ExprKind, HirId, QPath}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty::adjustment::{Adjust, Adjustment}; use rustc_middle::ty::Ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::def_id::DefId; use rustc_span::edition::Edition::Edition2021; @@ -103,19 +106,25 @@ declare_clippy_lint! { /// format!("{var:.prec$}"); /// ``` /// - /// ### Known Problems - /// - /// There may be a false positive if the format string is expanded from certain proc macros: - /// - /// ```ignore - /// println!(indoc!("{}"), var); + /// If allow-mixed-uninlined-format-args is set to false in clippy.toml, + /// the following code will also trigger the lint: + /// ```rust + /// # let var = 42; + /// format!("{} {}", var, 1+2); /// ``` + /// Use instead: + /// ```rust + /// # let var = 42; + /// format!("{var} {}", 1+2); + /// ``` + /// + /// ### Known Problems /// /// If a format string contains a numbered argument that cannot be inlined /// nothing will be suggested, e.g. `println!("{0}={1}", var, 1+2)`. #[clippy::version = "1.65.0"] pub UNINLINED_FORMAT_ARGS, - pedantic, + style, "using non-inlined variables in `format!` calls" } @@ -158,13 +167,17 @@ impl_lint_pass!(FormatArgs => [ ]); pub struct FormatArgs { - msrv: Option, + msrv: Msrv, + ignore_mixed: bool, } impl FormatArgs { #[must_use] - pub fn new(msrv: Option) -> Self { - Self { msrv } + pub fn new(msrv: Msrv, allow_mixed_uninlined_format_args: bool) -> Self { + Self { + msrv, + ignore_mixed: allow_mixed_uninlined_format_args, + } } } @@ -188,8 +201,8 @@ impl<'tcx> LateLintPass<'tcx> for FormatArgs { check_format_in_format_args(cx, outermost_expn_data.call_site, name, arg.param.value); check_to_string_in_format_args(cx, name, arg.param.value); } - if meets_msrv(self.msrv, msrvs::FORMAT_ARGS_CAPTURE) { - check_uninlined_args(cx, &format_args, outermost_expn_data.call_site, macro_def_id); + if self.msrv.meets(msrvs::FORMAT_ARGS_CAPTURE) { + check_uninlined_args(cx, &format_args, outermost_expn_data.call_site, macro_def_id, self.ignore_mixed); } } } @@ -267,7 +280,13 @@ fn check_unused_format_specifier(cx: &LateContext<'_>, arg: &FormatArg<'_>) { } } -fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_site: Span, def_id: DefId) { +fn check_uninlined_args( + cx: &LateContext<'_>, + args: &FormatArgsExpn<'_>, + call_site: Span, + def_id: DefId, + ignore_mixed: bool, +) { if args.format_string.span.from_expansion() { return; } @@ -282,14 +301,13 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si // we cannot remove any other arguments in the format string, // because the index numbers might be wrong after inlining. // Example of an un-inlinable format: print!("{}{1}", foo, 2) - if !args.params().all(|p| check_one_arg(args, &p, &mut fixes)) || fixes.is_empty() { + if !args.params().all(|p| check_one_arg(args, &p, &mut fixes, ignore_mixed)) || fixes.is_empty() { return; } - // Temporarily ignore multiline spans: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308 - if fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span)) { - return; - } + // multiline span display suggestion is sometimes broken: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308 + // in those cases, make the code suggestion hidden + let multiline_fix = fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span)); span_lint_and_then( cx, @@ -297,12 +315,22 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si call_site, "variables can be used directly in the `format!` string", |diag| { - diag.multipart_suggestion("change this to", fixes, Applicability::MachineApplicable); + diag.multipart_suggestion_with_style( + "change this to", + fixes, + Applicability::MachineApplicable, + if multiline_fix { CompletelyHidden } else { ShowCode }, + ); }, ); } -fn check_one_arg(args: &FormatArgsExpn<'_>, param: &FormatParam<'_>, fixes: &mut Vec<(Span, String)>) -> bool { +fn check_one_arg( + args: &FormatArgsExpn<'_>, + param: &FormatParam<'_>, + fixes: &mut Vec<(Span, String)>, + ignore_mixed: bool, +) -> bool { if matches!(param.kind, Implicit | Starred | Named(_) | Numbered) && let ExprKind::Path(QPath::Resolved(None, path)) = param.value.kind && let [segment] = path.segments @@ -317,8 +345,10 @@ fn check_one_arg(args: &FormatArgsExpn<'_>, param: &FormatParam<'_>, fixes: &mut fixes.push((arg_span, String::new())); true // successful inlining, continue checking } else { - // if we can't inline a numbered argument, we can't continue - param.kind != Numbered + // Do not continue inlining (return false) in case + // * if we can't inline a numbered argument, e.g. `print!("{0} ...", foo.bar, ...)` + // * if allow_mixed_uninlined_format_args is false and this arg hasn't been inlined already + param.kind != Numbered && (!ignore_mixed || matches!(param.kind, NamedInline(_))) } } diff --git a/clippy_lints/src/from_over_into.rs b/clippy_lints/src/from_over_into.rs index 8b24a4962fb2..8621504c1b47 100644 --- a/clippy_lints/src/from_over_into.rs +++ b/clippy_lints/src/from_over_into.rs @@ -1,7 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::macros::span_is_local; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::path_def_id; use clippy_utils::source::snippet_opt; -use clippy_utils::{meets_msrv, msrvs, path_def_id}; use rustc_errors::Applicability; use rustc_hir::intravisit::{walk_path, Visitor}; use rustc_hir::{ @@ -10,7 +11,6 @@ use rustc_hir::{ }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter::OnlyBodies; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::{kw, sym}; use rustc_span::{Span, Symbol}; @@ -49,12 +49,12 @@ declare_clippy_lint! { } pub struct FromOverInto { - msrv: Option, + msrv: Msrv, } impl FromOverInto { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { FromOverInto { msrv } } } @@ -63,7 +63,7 @@ impl_lint_pass!(FromOverInto => [FROM_OVER_INTO]); impl<'tcx> LateLintPass<'tcx> for FromOverInto { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { - if !meets_msrv(self.msrv, msrvs::RE_REBALANCING_COHERENCE) || !span_is_local(item.span) { + if !self.msrv.meets(msrvs::RE_REBALANCING_COHERENCE) || !span_is_local(item.span) { return; } diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs new file mode 100644 index 000000000000..27acad45ccf7 --- /dev/null +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -0,0 +1,125 @@ +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::source::snippet; +use rustc_errors::Applicability; +use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind, Unsafety}; +use rustc_lint::LateContext; +use rustc_middle::ty; +use rustc_span::Span; + +use std::iter; + +use super::MISNAMED_GETTERS; + +pub fn check_fn( + cx: &LateContext<'_>, + kind: FnKind<'_>, + decl: &FnDecl<'_>, + body: &Body<'_>, + span: Span, + _hir_id: HirId, +) { + let FnKind::Method(ref ident, sig) = kind else { + return; + }; + + // Takes only &(mut) self + if decl.inputs.len() != 1 { + return; + } + + let name = ident.name.as_str(); + + let name = match decl.implicit_self { + ImplicitSelfKind::MutRef => { + let Some(name) = name.strip_suffix("_mut") else { + return; + }; + name + }, + ImplicitSelfKind::Imm | ImplicitSelfKind::Mut | ImplicitSelfKind::ImmRef => name, + ImplicitSelfKind::None => return, + }; + + let name = if sig.header.unsafety == Unsafety::Unsafe { + name.strip_suffix("_unchecked").unwrap_or(name) + } else { + name + }; + + // Body must be &(mut) .name + // self_data is not neccessarilly self, to also lint sub-getters, etc… + + let block_expr = if_chain! { + if let ExprKind::Block(block,_) = body.value.kind; + if block.stmts.is_empty(); + if let Some(block_expr) = block.expr; + then { + block_expr + } else { + return; + } + }; + let expr_span = block_expr.span; + + // Accept &, &mut and + let expr = if let ExprKind::AddrOf(_, _, tmp) = block_expr.kind { + tmp + } else { + block_expr + }; + let (self_data, used_ident) = if_chain! { + if let ExprKind::Field(self_data, ident) = expr.kind; + if ident.name.as_str() != name; + then { + (self_data, ident) + } else { + return; + } + }; + + let mut used_field = None; + let mut correct_field = None; + let typeck_results = cx.typeck_results(); + for adjusted_type in iter::once(typeck_results.expr_ty(self_data)) + .chain(typeck_results.expr_adjustments(self_data).iter().map(|adj| adj.target)) + { + let ty::Adt(def,_) = adjusted_type.kind() else { + continue; + }; + + for f in def.all_fields() { + if f.name.as_str() == name { + correct_field = Some(f); + } + if f.name == used_ident.name { + used_field = Some(f); + } + } + } + + let Some(used_field) = used_field else { + // Can happen if the field access is a tuple. We don't lint those because the getter name could not start with a number. + return; + }; + + let Some(correct_field) = correct_field else { + // There is no field corresponding to the getter name. + // FIXME: This can be a false positive if the correct field is reachable trought deeper autodereferences than used_field is + return; + }; + + if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) { + let left_span = block_expr.span.until(used_ident.span); + let snippet = snippet(cx, left_span, ".."); + let sugg = format!("{snippet}{name}"); + span_lint_and_then( + cx, + MISNAMED_GETTERS, + span, + "getter function appears to return the wrong field", + |diag| { + diag.span_suggestion(expr_span, "consider using", sugg, Applicability::MaybeIncorrect); + }, + ); + } +} diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index ae0e08334463..91e6ffe64479 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -1,3 +1,4 @@ +mod misnamed_getters; mod must_use; mod not_unsafe_ptr_arg_deref; mod result; @@ -260,6 +261,48 @@ declare_clippy_lint! { "function returning `Result` with large `Err` type" } +declare_clippy_lint! { + /// ### What it does + /// Checks for getter methods that return a field that doesn't correspond + /// to the name of the method, when there is a field's whose name matches that of the method. + /// + /// ### Why is this bad? + /// It is most likely that such a method is a bug caused by a typo or by copy-pasting. + /// + /// ### Example + + /// ```rust + /// struct A { + /// a: String, + /// b: String, + /// } + /// + /// impl A { + /// fn a(&self) -> &str{ + /// &self.b + /// } + /// } + + /// ``` + /// Use instead: + /// ```rust + /// struct A { + /// a: String, + /// b: String, + /// } + /// + /// impl A { + /// fn a(&self) -> &str{ + /// &self.a + /// } + /// } + /// ``` + #[clippy::version = "1.67.0"] + pub MISNAMED_GETTERS, + suspicious, + "getter method returning the wrong field" +} + #[derive(Copy, Clone)] pub struct Functions { too_many_arguments_threshold: u64, @@ -286,6 +329,7 @@ impl_lint_pass!(Functions => [ MUST_USE_CANDIDATE, RESULT_UNIT_ERR, RESULT_LARGE_ERR, + MISNAMED_GETTERS, ]); impl<'tcx> LateLintPass<'tcx> for Functions { @@ -301,6 +345,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions { too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold); too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold); not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id); + misnamed_getters::check_fn(cx, kind, decl, body, span, hir_id); } fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { diff --git a/clippy_lints/src/functions/result.rs b/clippy_lints/src/functions/result.rs index f7e30b051a69..23da145d0382 100644 --- a/clippy_lints/src/functions/result.rs +++ b/clippy_lints/src/functions/result.rs @@ -94,7 +94,9 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty if let hir::ItemKind::Enum(ref def, _) = item.kind; then { let variants_size = AdtVariantInfo::new(cx, *adt, subst); - if variants_size[0].size >= large_err_threshold { + if let Some((first_variant, variants)) = variants_size.split_first() + && first_variant.size >= large_err_threshold + { span_lint_and_then( cx, RESULT_LARGE_ERR, @@ -102,11 +104,11 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty "the `Err`-variant returned from this function is very large", |diag| { diag.span_label( - def.variants[variants_size[0].ind].span, + def.variants[first_variant.ind].span, format!("the largest variant contains at least {} bytes", variants_size[0].size), ); - for variant in &variants_size[1..] { + for variant in variants { if variant.size >= large_err_threshold { let variant_def = &def.variants[variant.ind]; diag.span_label( diff --git a/clippy_lints/src/future_not_send.rs b/clippy_lints/src/future_not_send.rs index a9425a40f885..61934a914263 100644 --- a/clippy_lints/src/future_not_send.rs +++ b/clippy_lints/src/future_not_send.rs @@ -91,7 +91,9 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { infcx .err_ctxt() .maybe_note_obligation_cause_for_async_await(db, &obligation); - if let PredicateKind::Clause(Clause::Trait(trait_pred)) = obligation.predicate.kind().skip_binder() { + if let PredicateKind::Clause(Clause::Trait(trait_pred)) = + obligation.predicate.kind().skip_binder() + { db.note(&format!( "`{}` doesn't implement `{}`", trait_pred.self_ty(), diff --git a/clippy_lints/src/if_then_some_else_none.rs b/clippy_lints/src/if_then_some_else_none.rs index 0d6718c168a5..9cadaaa493e4 100644 --- a/clippy_lints/src/if_then_some_else_none.rs +++ b/clippy_lints/src/if_then_some_else_none.rs @@ -1,14 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::eager_or_lazy::switch_to_eager_eval; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_macro_callsite; -use clippy_utils::{ - contains_return, higher, is_else_clause, is_res_lang_ctor, meets_msrv, msrvs, path_res, peel_blocks, -}; +use clippy_utils::{contains_return, higher, is_else_clause, is_res_lang_ctor, path_res, peel_blocks}; use rustc_hir::LangItem::{OptionNone, OptionSome}; use rustc_hir::{Expr, ExprKind, Stmt, StmtKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -47,12 +45,12 @@ declare_clippy_lint! { } pub struct IfThenSomeElseNone { - msrv: Option, + msrv: Msrv, } impl IfThenSomeElseNone { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -61,7 +59,7 @@ impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]); impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { - if !meets_msrv(self.msrv, msrvs::BOOL_THEN) { + if !self.msrv.meets(msrvs::BOOL_THEN) { return; } @@ -94,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone { } else { format!("{{ /* snippet */ {arg_snip} }}") }; - let method_name = if switch_to_eager_eval(cx, expr) && meets_msrv(self.msrv, msrvs::BOOL_THEN_SOME) { + let method_name = if switch_to_eager_eval(cx, expr) && self.msrv.meets(msrvs::BOOL_THEN_SOME) { "then_some" } else { method_body.insert_str(0, "|| "); diff --git a/clippy_lints/src/index_refutable_slice.rs b/clippy_lints/src/index_refutable_slice.rs index 0d5099bde6de..cf35b1f175c6 100644 --- a/clippy_lints/src/index_refutable_slice.rs +++ b/clippy_lints/src/index_refutable_slice.rs @@ -1,8 +1,9 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::IfLet; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::is_copy; -use clippy_utils::{is_expn_of, is_lint_allowed, meets_msrv, msrvs, path_to_local}; +use clippy_utils::{is_expn_of, is_lint_allowed, path_to_local}; use if_chain::if_chain; use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_errors::Applicability; @@ -11,7 +12,6 @@ use rustc_hir::intravisit::{self, Visitor}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{symbol::Ident, Span}; @@ -47,18 +47,17 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.59.0"] pub INDEX_REFUTABLE_SLICE, - nursery, + pedantic, "avoid indexing on slices which could be destructed" } -#[derive(Copy, Clone)] pub struct IndexRefutableSlice { max_suggested_slice: u64, - msrv: Option, + msrv: Msrv, } impl IndexRefutableSlice { - pub fn new(max_suggested_slice_pattern_length: u64, msrv: Option) -> Self { + pub fn new(max_suggested_slice_pattern_length: u64, msrv: Msrv) -> Self { Self { max_suggested_slice: max_suggested_slice_pattern_length, msrv, @@ -74,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexRefutableSlice { if !expr.span.from_expansion() || is_expn_of(expr.span, "if_chain").is_some(); if let Some(IfLet {let_pat, if_then, ..}) = IfLet::hir(cx, expr); if !is_lint_allowed(cx, INDEX_REFUTABLE_SLICE, expr.hir_id); - if meets_msrv(self.msrv, msrvs::SLICE_PATTERNS); + if self.msrv.meets(msrvs::SLICE_PATTERNS); let found_slices = find_slice_values(cx, let_pat); if !found_slices.is_empty(); diff --git a/clippy_lints/src/instant_subtraction.rs b/clippy_lints/src/instant_subtraction.rs index 60754b224fc8..dd1b23e7d9d2 100644 --- a/clippy_lints/src/instant_subtraction.rs +++ b/clippy_lints/src/instant_subtraction.rs @@ -1,13 +1,11 @@ -use clippy_utils::{ - diagnostics::{self, span_lint_and_sugg}, - meets_msrv, msrvs, source, - sugg::Sugg, - ty, -}; +use clippy_utils::diagnostics::{self, span_lint_and_sugg}; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::source; +use clippy_utils::sugg::Sugg; +use clippy_utils::ty; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{source_map::Spanned, sym}; @@ -68,12 +66,12 @@ declare_clippy_lint! { } pub struct InstantSubtraction { - msrv: Option, + msrv: Msrv, } impl InstantSubtraction { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -101,7 +99,7 @@ impl LateLintPass<'_> for InstantSubtraction { } else { if_chain! { if !expr.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TRY_FROM); + if self.msrv.meets(msrvs::TRY_FROM); if is_an_instant(cx, lhs); if is_a_duration(cx, rhs); diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 601990cd6a31..7b17d8a156d5 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -52,10 +52,9 @@ extern crate declare_clippy_lint; use std::io; use std::path::PathBuf; -use clippy_utils::parse_msrv; +use clippy_utils::msrvs::Msrv; use rustc_data_structures::fx::FxHashSet; use rustc_lint::{Lint, LintId}; -use rustc_semver::RustcVersion; use rustc_session::Session; #[cfg(feature = "internal")] @@ -322,48 +321,10 @@ pub use crate::utils::conf::{lookup_conf_file, Conf}; /// Used in `./src/driver.rs`. pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, sess: &Session, conf: &Conf) { // NOTE: Do not add any more pre-expansion passes. These should be removed eventually. + let msrv = Msrv::read(&conf.msrv, sess); + let msrv = move || msrv.clone(); - let msrv = conf.msrv.as_ref().and_then(|s| { - parse_msrv(s, None, None).or_else(|| { - sess.err(format!( - "error reading Clippy's configuration file. `{s}` is not a valid Rust version" - )); - None - }) - }); - - store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv })); -} - -fn read_msrv(conf: &Conf, sess: &Session) -> Option { - let cargo_msrv = std::env::var("CARGO_PKG_RUST_VERSION") - .ok() - .and_then(|v| parse_msrv(&v, None, None)); - let clippy_msrv = conf.msrv.as_ref().and_then(|s| { - parse_msrv(s, None, None).or_else(|| { - sess.err(format!( - "error reading Clippy's configuration file. `{s}` is not a valid Rust version" - )); - None - }) - }); - - if let Some(cargo_msrv) = cargo_msrv { - if let Some(clippy_msrv) = clippy_msrv { - // if both files have an msrv, let's compare them and emit a warning if they differ - if clippy_msrv != cargo_msrv { - sess.warn(format!( - "the MSRV in `clippy.toml` and `Cargo.toml` differ; using `{clippy_msrv}` from `clippy.toml`" - )); - } - - Some(clippy_msrv) - } else { - Some(cargo_msrv) - } - } else { - clippy_msrv - } + store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv: msrv() })); } #[doc(hidden)] @@ -595,43 +556,44 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions)); store.register_early_pass(|| Box::new(unnecessary_self_imports::UnnecessarySelfImports)); - let msrv = read_msrv(conf, sess); + let msrv = Msrv::read(&conf.msrv, sess); + let msrv = move || msrv.clone(); let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; let allow_expect_in_tests = conf.allow_expect_in_tests; let allow_unwrap_in_tests = conf.allow_unwrap_in_tests; - store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv))); + store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(msrv()))); store.register_late_pass(move |_| { Box::new(methods::Methods::new( avoid_breaking_exported_api, - msrv, + msrv(), allow_expect_in_tests, allow_unwrap_in_tests, )) }); - store.register_late_pass(move |_| Box::new(matches::Matches::new(msrv))); + store.register_late_pass(move |_| Box::new(matches::Matches::new(msrv()))); let matches_for_let_else = conf.matches_for_let_else; - store.register_late_pass(move |_| Box::new(manual_let_else::ManualLetElse::new(msrv, matches_for_let_else))); - store.register_early_pass(move || Box::new(manual_non_exhaustive::ManualNonExhaustiveStruct::new(msrv))); - store.register_late_pass(move |_| Box::new(manual_non_exhaustive::ManualNonExhaustiveEnum::new(msrv))); - store.register_late_pass(move |_| Box::new(manual_strip::ManualStrip::new(msrv))); - store.register_early_pass(move || Box::new(redundant_static_lifetimes::RedundantStaticLifetimes::new(msrv))); - store.register_early_pass(move || Box::new(redundant_field_names::RedundantFieldNames::new(msrv))); - store.register_late_pass(move |_| Box::new(checked_conversions::CheckedConversions::new(msrv))); - store.register_late_pass(move |_| Box::new(mem_replace::MemReplace::new(msrv))); - store.register_late_pass(move |_| Box::new(ranges::Ranges::new(msrv))); - store.register_late_pass(move |_| Box::new(from_over_into::FromOverInto::new(msrv))); - store.register_late_pass(move |_| Box::new(use_self::UseSelf::new(msrv))); - store.register_late_pass(move |_| Box::new(missing_const_for_fn::MissingConstForFn::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_let_else::ManualLetElse::new(msrv(), matches_for_let_else))); + store.register_early_pass(move || Box::new(manual_non_exhaustive::ManualNonExhaustiveStruct::new(msrv()))); + store.register_late_pass(move |_| Box::new(manual_non_exhaustive::ManualNonExhaustiveEnum::new(msrv()))); + store.register_late_pass(move |_| Box::new(manual_strip::ManualStrip::new(msrv()))); + store.register_early_pass(move || Box::new(redundant_static_lifetimes::RedundantStaticLifetimes::new(msrv()))); + store.register_early_pass(move || Box::new(redundant_field_names::RedundantFieldNames::new(msrv()))); + store.register_late_pass(move |_| Box::new(checked_conversions::CheckedConversions::new(msrv()))); + store.register_late_pass(move |_| Box::new(mem_replace::MemReplace::new(msrv()))); + store.register_late_pass(move |_| Box::new(ranges::Ranges::new(msrv()))); + store.register_late_pass(move |_| Box::new(from_over_into::FromOverInto::new(msrv()))); + store.register_late_pass(move |_| Box::new(use_self::UseSelf::new(msrv()))); + store.register_late_pass(move |_| Box::new(missing_const_for_fn::MissingConstForFn::new(msrv()))); store.register_late_pass(move |_| Box::new(needless_question_mark::NeedlessQuestionMark)); - store.register_late_pass(move |_| Box::new(casts::Casts::new(msrv))); - store.register_early_pass(move || Box::new(unnested_or_patterns::UnnestedOrPatterns::new(msrv))); + store.register_late_pass(move |_| Box::new(casts::Casts::new(msrv()))); + store.register_early_pass(move || Box::new(unnested_or_patterns::UnnestedOrPatterns::new(msrv()))); store.register_late_pass(|_| Box::new(size_of_in_element_count::SizeOfInElementCount)); store.register_late_pass(|_| Box::new(same_name_method::SameNameMethod)); let max_suggested_slice_pattern_length = conf.max_suggested_slice_pattern_length; store.register_late_pass(move |_| { Box::new(index_refutable_slice::IndexRefutableSlice::new( max_suggested_slice_pattern_length, - msrv, + msrv(), )) }); store.register_late_pass(|_| Box::::default()); @@ -648,7 +610,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(borrow_deref_ref::BorrowDerefRef)); store.register_late_pass(|_| Box::new(no_effect::NoEffect)); store.register_late_pass(|_| Box::new(temporary_assignment::TemporaryAssignment)); - store.register_late_pass(move |_| Box::new(transmute::Transmute::new(msrv))); + store.register_late_pass(move |_| Box::new(transmute::Transmute::new(msrv()))); let cognitive_complexity_threshold = conf.cognitive_complexity_threshold; store.register_late_pass(move |_| { Box::new(cognitive_complexity::CognitiveComplexity::new( @@ -806,7 +768,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(move |_| Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports))); store.register_late_pass(|_| Box::::default()); store.register_late_pass(|_| Box::new(unnamed_address::UnnamedAddress)); - store.register_late_pass(move |_| Box::new(dereference::Dereferencing::new(msrv))); + store.register_late_pass(move |_| Box::new(dereference::Dereferencing::new(msrv()))); store.register_late_pass(|_| Box::new(option_if_let_else::OptionIfLetElse)); store.register_late_pass(|_| Box::new(future_not_send::FutureNotSend)); store.register_late_pass(|_| Box::new(if_let_mutex::IfLetMutex)); @@ -840,7 +802,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::::default()); store.register_late_pass(|_| Box::new(redundant_slicing::RedundantSlicing)); store.register_late_pass(|_| Box::new(from_str_radix_10::FromStrRadix10)); - store.register_late_pass(move |_| Box::new(if_then_some_else_none::IfThenSomeElseNone::new(msrv))); + store.register_late_pass(move |_| Box::new(if_then_some_else_none::IfThenSomeElseNone::new(msrv()))); store.register_late_pass(|_| Box::new(bool_assert_comparison::BoolAssertComparison)); store.register_early_pass(move || Box::new(module_style::ModStyle)); store.register_late_pass(|_| Box::new(unused_async::UnusedAsync)); @@ -865,14 +827,15 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: )) }); store.register_late_pass(move |_| Box::new(undocumented_unsafe_blocks::UndocumentedUnsafeBlocks)); - store.register_late_pass(move |_| Box::new(format_args::FormatArgs::new(msrv))); + let allow_mixed_uninlined = conf.allow_mixed_uninlined_format_args; + store.register_late_pass(move |_| Box::new(format_args::FormatArgs::new(msrv(), allow_mixed_uninlined))); store.register_late_pass(|_| Box::new(trailing_empty_array::TrailingEmptyArray)); store.register_early_pass(|| Box::new(octal_escapes::OctalEscapes)); store.register_late_pass(|_| Box::new(needless_late_init::NeedlessLateInit)); store.register_late_pass(|_| Box::new(return_self_not_must_use::ReturnSelfNotMustUse)); store.register_late_pass(|_| Box::new(init_numbered_fields::NumberedFields)); store.register_early_pass(|| Box::new(single_char_lifetime_names::SingleCharLifetimeNames)); - store.register_late_pass(move |_| Box::new(manual_bits::ManualBits::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_bits::ManualBits::new(msrv()))); store.register_late_pass(|_| Box::new(default_union_representation::DefaultUnionRepresentation)); store.register_late_pass(|_| Box::::default()); let allow_dbg_in_tests = conf.allow_dbg_in_tests; @@ -896,20 +859,20 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(rc_clone_in_vec_init::RcCloneInVecInit)); store.register_early_pass(|| Box::::default()); store.register_early_pass(|| Box::new(unused_rounding::UnusedRounding)); - store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv))); + store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv()))); store.register_late_pass(|_| Box::new(swap_ptr_to_ref::SwapPtrToRef)); store.register_late_pass(|_| Box::new(mismatching_type_param_order::TypeParamMismatch)); store.register_late_pass(|_| Box::new(read_zero_byte_vec::ReadZeroByteVec)); store.register_late_pass(|_| Box::new(default_instead_of_iter_empty::DefaultIterEmpty)); - store.register_late_pass(move |_| Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv))); - store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv()))); + store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(msrv()))); let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold; store.register_late_pass(move |_| Box::new(operators::Operators::new(verbose_bit_mask_threshold))); store.register_late_pass(|_| Box::new(invalid_utf8_in_unchecked::InvalidUtf8InUnchecked)); store.register_late_pass(|_| Box::::default()); - store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(msrv))); + store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(msrv()))); store.register_late_pass(|_| Box::new(partialeq_to_none::PartialeqToNone)); - store.register_late_pass(move |_| Box::new(manual_clamp::ManualClamp::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_clamp::ManualClamp::new(msrv()))); store.register_late_pass(|_| Box::new(manual_string_new::ManualStringNew)); store.register_late_pass(|_| Box::new(unused_peekable::UnusedPeekable)); store.register_early_pass(|| Box::new(multi_assignments::MultiAssignments)); @@ -920,7 +883,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(missing_trait_methods::MissingTraitMethods)); store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr)); store.register_late_pass(|_| Box::new(suspicious_xor_used_as_pow::ConfusingXorAndPow)); - store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(msrv))); + store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(msrv()))); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index 220941dcd5db..7cf1a6b8084a 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -3,7 +3,7 @@ use clippy_utils::trait_ref_of_method; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter}; use rustc_hir::intravisit::{ - walk_fn_decl, walk_generic_param, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound, + walk_fn_decl, walk_generic_arg, walk_generic_param, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound, walk_poly_trait_ref, walk_trait_ref, walk_ty, Visitor, }; use rustc_hir::lang_items; @@ -481,7 +481,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> { sub_visitor.visit_fn_decl(decl); self.nested_elision_site_lts.append(&mut sub_visitor.all_lts()); }, - TyKind::TraitObject(bounds, ref lt, _) => { + TyKind::TraitObject(bounds, lt, _) => { if !lt.is_elided() { self.unelided_trait_object_lifetime = true; } @@ -497,14 +497,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> { if let GenericArg::Lifetime(l) = generic_arg && let LifetimeName::Param(def_id) = l.res { self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.ident.span); } - // Replace with `walk_generic_arg` if/when https://github.com/rust-lang/rust/pull/103692 lands. - // walk_generic_arg(self, generic_arg); - match generic_arg { - GenericArg::Lifetime(lt) => self.visit_lifetime(lt), - GenericArg::Type(ty) => self.visit_ty(ty), - GenericArg::Const(ct) => self.visit_anon_const(&ct.value), - GenericArg::Infer(inf) => self.visit_infer(inf), - } + walk_generic_arg(self, generic_arg); } } diff --git a/clippy_lints/src/manual_bits.rs b/clippy_lints/src/manual_bits.rs index 6655c92b1da8..462d73cf0b97 100644 --- a/clippy_lints/src/manual_bits.rs +++ b/clippy_lints/src/manual_bits.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::get_parent_expr; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{get_parent_expr, meets_msrv, msrvs}; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, Ty}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::sym; @@ -34,12 +34,12 @@ declare_clippy_lint! { #[derive(Clone)] pub struct ManualBits { - msrv: Option, + msrv: Msrv, } impl ManualBits { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -48,7 +48,7 @@ impl_lint_pass!(ManualBits => [MANUAL_BITS]); impl<'tcx> LateLintPass<'tcx> for ManualBits { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::MANUAL_BITS) { + if !self.msrv.meets(msrvs::MANUAL_BITS) { return; } diff --git a/clippy_lints/src/manual_clamp.rs b/clippy_lints/src/manual_clamp.rs index 02dc8755dd61..bb6d628af3b5 100644 --- a/clippy_lints/src/manual_clamp.rs +++ b/clippy_lints/src/manual_clamp.rs @@ -1,28 +1,25 @@ +use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then}; +use clippy_utils::higher::If; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::sugg::Sugg; +use clippy_utils::ty::implements_trait; +use clippy_utils::visitors::is_const_evaluatable; +use clippy_utils::MaybePath; +use clippy_utils::{ + eq_expr_value, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks, peel_blocks_with_stmt, +}; use itertools::Itertools; +use rustc_errors::Applicability; use rustc_errors::Diagnostic; use rustc_hir::{ def::Res, Arm, BinOpKind, Block, Expr, ExprKind, Guard, HirId, PatKind, PathSegment, PrimTy, QPath, StmtKind, }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::Ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{symbol::sym, Span}; use std::ops::Deref; -use clippy_utils::{ - diagnostics::{span_lint_and_then, span_lint_hir_and_then}, - eq_expr_value, - higher::If, - is_diag_trait_item, is_trait_method, meets_msrv, msrvs, path_res, path_to_local_id, peel_blocks, - peel_blocks_with_stmt, - sugg::Sugg, - ty::implements_trait, - visitors::is_const_evaluatable, - MaybePath, -}; -use rustc_errors::Applicability; - declare_clippy_lint! { /// ### What it does /// Identifies good opportunities for a clamp function from std or core, and suggests using it. @@ -87,11 +84,11 @@ declare_clippy_lint! { impl_lint_pass!(ManualClamp => [MANUAL_CLAMP]); pub struct ManualClamp { - msrv: Option, + msrv: Msrv, } impl ManualClamp { - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -114,7 +111,7 @@ struct InputMinMax<'tcx> { impl<'tcx> LateLintPass<'tcx> for ManualClamp { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { - if !meets_msrv(self.msrv, msrvs::CLAMP) { + if !self.msrv.meets(msrvs::CLAMP) { return; } if !expr.span.from_expansion() { @@ -130,7 +127,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp { } fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { - if !meets_msrv(self.msrv, msrvs::CLAMP) { + if !self.msrv.meets(msrvs::CLAMP) { return; } for suggestion in is_two_if_pattern(cx, block) { diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs index bb8c142f8e46..5ab049d8d133 100644 --- a/clippy_lints/src/manual_is_ascii_check.rs +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -1,15 +1,12 @@ +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::{diagnostics::span_lint_and_sugg, in_constant, macros::root_macro_call, source::snippet}; use rustc_ast::LitKind::{Byte, Char}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, PatKind, RangeEnd}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{def_id::DefId, sym}; -use clippy_utils::{ - diagnostics::span_lint_and_sugg, in_constant, macros::root_macro_call, meets_msrv, msrvs, source::snippet, -}; - declare_clippy_lint! { /// ### What it does /// Suggests to use dedicated built-in methods, @@ -45,12 +42,12 @@ declare_clippy_lint! { impl_lint_pass!(ManualIsAsciiCheck => [MANUAL_IS_ASCII_CHECK]); pub struct ManualIsAsciiCheck { - msrv: Option, + msrv: Msrv, } impl ManualIsAsciiCheck { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -70,11 +67,11 @@ enum CharRange { impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::IS_ASCII_DIGIT) { + if !self.msrv.meets(msrvs::IS_ASCII_DIGIT) { return; } - if in_constant(cx, expr.hir_id) && !meets_msrv(self.msrv, msrvs::IS_ASCII_DIGIT_CONST) { + if in_constant(cx, expr.hir_id) && !self.msrv.meets(msrvs::IS_ASCII_DIGIT_CONST) { return; } diff --git a/clippy_lints/src/manual_let_else.rs b/clippy_lints/src/manual_let_else.rs index 1846596fa4c8..874d36ca9f4e 100644 --- a/clippy_lints/src/manual_let_else.rs +++ b/clippy_lints/src/manual_let_else.rs @@ -1,16 +1,16 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::IfLetOrMatch; -use clippy_utils::source::snippet_opt; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::peel_blocks; +use clippy_utils::source::snippet_with_context; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::visitors::{for_each_expr, Descend}; -use clippy_utils::{meets_msrv, msrvs, peel_blocks}; use if_chain::if_chain; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, MatchSource, Pat, PatKind, QPath, Stmt, StmtKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -50,13 +50,13 @@ declare_clippy_lint! { } pub struct ManualLetElse { - msrv: Option, + msrv: Msrv, matches_behaviour: MatchLintBehaviour, } impl ManualLetElse { #[must_use] - pub fn new(msrv: Option, matches_behaviour: MatchLintBehaviour) -> Self { + pub fn new(msrv: Msrv, matches_behaviour: MatchLintBehaviour) -> Self { Self { msrv, matches_behaviour, @@ -69,7 +69,7 @@ impl_lint_pass!(ManualLetElse => [MANUAL_LET_ELSE]); impl<'tcx> LateLintPass<'tcx> for ManualLetElse { fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &'tcx Stmt<'tcx>) { let if_let_or_match = if_chain! { - if meets_msrv(self.msrv, msrvs::LET_ELSE); + if self.msrv.meets(msrvs::LET_ELSE); if !in_external_macro(cx.sess(), stmt.span); if let StmtKind::Local(local) = stmt.kind; if let Some(init) = local.init; @@ -141,20 +141,18 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat: // * unused binding collision detection with existing ones // * putting patterns with at the top level | inside () // for this to be machine applicable. - let app = Applicability::HasPlaceholders; + let mut app = Applicability::HasPlaceholders; + let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", &mut app); + let (sn_expr, _) = snippet_with_context(cx, expr.span, span.ctxt(), "", &mut app); + let (sn_else, _) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app); - if let Some(sn_pat) = snippet_opt(cx, pat.span) && - let Some(sn_expr) = snippet_opt(cx, expr.span) && - let Some(sn_else) = snippet_opt(cx, else_body.span) - { - let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) { - sn_else - } else { - format!("{{ {sn_else} }}") - }; - let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};"); - diag.span_suggestion(span, "consider writing", sugg, app); - } + let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) { + sn_else.into_owned() + } else { + format!("{{ {sn_else} }}") + }; + let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};"); + diag.span_suggestion(span, "consider writing", sugg, app); }, ); } diff --git a/clippy_lints/src/manual_non_exhaustive.rs b/clippy_lints/src/manual_non_exhaustive.rs index 4877cee0cc1e..bca193be9e71 100644 --- a/clippy_lints/src/manual_non_exhaustive.rs +++ b/clippy_lints/src/manual_non_exhaustive.rs @@ -1,6 +1,7 @@ use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then}; +use clippy_utils::is_doc_hidden; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; -use clippy_utils::{is_doc_hidden, meets_msrv, msrvs}; use rustc_ast::ast::{self, VisibilityKind}; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; @@ -8,7 +9,6 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; use rustc_hir::{self as hir, Expr, ExprKind, QPath}; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; use rustc_middle::ty::DefIdTree; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::{sym, Span}; @@ -63,12 +63,12 @@ declare_clippy_lint! { #[expect(clippy::module_name_repetitions)] pub struct ManualNonExhaustiveStruct { - msrv: Option, + msrv: Msrv, } impl ManualNonExhaustiveStruct { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -77,14 +77,14 @@ impl_lint_pass!(ManualNonExhaustiveStruct => [MANUAL_NON_EXHAUSTIVE]); #[expect(clippy::module_name_repetitions)] pub struct ManualNonExhaustiveEnum { - msrv: Option, + msrv: Msrv, constructed_enum_variants: FxHashSet<(DefId, DefId)>, potential_enums: Vec<(LocalDefId, LocalDefId, Span, Span)>, } impl ManualNonExhaustiveEnum { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, constructed_enum_variants: FxHashSet::default(), @@ -97,7 +97,7 @@ impl_lint_pass!(ManualNonExhaustiveEnum => [MANUAL_NON_EXHAUSTIVE]); impl EarlyLintPass for ManualNonExhaustiveStruct { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { - if !meets_msrv(self.msrv, msrvs::NON_EXHAUSTIVE) { + if !self.msrv.meets(msrvs::NON_EXHAUSTIVE) { return; } @@ -149,7 +149,7 @@ impl EarlyLintPass for ManualNonExhaustiveStruct { impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustiveEnum { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { - if !meets_msrv(self.msrv, msrvs::NON_EXHAUSTIVE) { + if !self.msrv.meets(msrvs::NON_EXHAUSTIVE) { return; } diff --git a/clippy_lints/src/manual_rem_euclid.rs b/clippy_lints/src/manual_rem_euclid.rs index 6f25a2ed8e43..8d447c37150b 100644 --- a/clippy_lints/src/manual_rem_euclid.rs +++ b/clippy_lints/src/manual_rem_euclid.rs @@ -1,12 +1,12 @@ use clippy_utils::consts::{constant_full_int, FullInt}; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{in_constant, meets_msrv, msrvs, path_to_local}; +use clippy_utils::{in_constant, path_to_local}; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -34,12 +34,12 @@ declare_clippy_lint! { } pub struct ManualRemEuclid { - msrv: Option, + msrv: Msrv, } impl ManualRemEuclid { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -48,11 +48,11 @@ impl_lint_pass!(ManualRemEuclid => [MANUAL_REM_EUCLID]); impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::REM_EUCLID) { + if !self.msrv.meets(msrvs::REM_EUCLID) { return; } - if in_constant(cx, expr.hir_id) && !meets_msrv(self.msrv, msrvs::REM_EUCLID_CONST) { + if in_constant(cx, expr.hir_id) && !self.msrv.meets(msrvs::REM_EUCLID_CONST) { return; } diff --git a/clippy_lints/src/manual_retain.rs b/clippy_lints/src/manual_retain.rs index d6438ca7fec2..c1e6c82487dc 100644 --- a/clippy_lints/src/manual_retain.rs +++ b/clippy_lints/src/manual_retain.rs @@ -1,8 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; use clippy_utils::{get_parent_expr, match_def_path, paths, SpanlessEq}; -use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_hir::def_id::DefId; @@ -50,12 +50,12 @@ declare_clippy_lint! { } pub struct ManualRetain { - msrv: Option, + msrv: Msrv, } impl ManualRetain { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -71,9 +71,9 @@ impl<'tcx> LateLintPass<'tcx> for ManualRetain { && let hir::ExprKind::MethodCall(_, target_expr, [], _) = &collect_expr.kind && let Some(collect_def_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id) && match_def_path(cx, collect_def_id, &paths::CORE_ITER_COLLECT) { - check_into_iter(cx, parent_expr, left_expr, target_expr, self.msrv); - check_iter(cx, parent_expr, left_expr, target_expr, self.msrv); - check_to_owned(cx, parent_expr, left_expr, target_expr, self.msrv); + check_into_iter(cx, parent_expr, left_expr, target_expr, &self.msrv); + check_iter(cx, parent_expr, left_expr, target_expr, &self.msrv); + check_to_owned(cx, parent_expr, left_expr, target_expr, &self.msrv); } } @@ -85,7 +85,7 @@ fn check_into_iter( parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, target_expr: &hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { if let hir::ExprKind::MethodCall(_, into_iter_expr, [_], _) = &target_expr.kind && let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id) @@ -104,7 +104,7 @@ fn check_iter( parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, target_expr: &hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { if let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind && let Some(copied_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id) @@ -127,9 +127,9 @@ fn check_to_owned( parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, target_expr: &hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { - if meets_msrv(msrv, msrvs::STRING_RETAIN) + if msrv.meets(msrvs::STRING_RETAIN) && let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind && let Some(to_owned_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id) && match_def_path(cx, to_owned_def_id, &paths::TO_OWNED_METHOD) @@ -215,10 +215,10 @@ fn match_acceptable_def_path(cx: &LateContext<'_>, collect_def_id: DefId) -> boo .any(|&method| match_def_path(cx, collect_def_id, method)) } -fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: Option) -> bool { +fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: &Msrv) -> bool { let expr_ty = cx.typeck_results().expr_ty(expr).peel_refs(); ACCEPTABLE_TYPES.iter().any(|(ty, acceptable_msrv)| { is_type_diagnostic_item(cx, expr_ty, *ty) - && acceptable_msrv.map_or(true, |acceptable_msrv| meets_msrv(msrv, acceptable_msrv)) + && acceptable_msrv.map_or(true, |acceptable_msrv| msrv.meets(acceptable_msrv)) }) } diff --git a/clippy_lints/src/manual_strip.rs b/clippy_lints/src/manual_strip.rs index 0976940afac3..de166b9765f4 100644 --- a/clippy_lints/src/manual_strip.rs +++ b/clippy_lints/src/manual_strip.rs @@ -1,8 +1,9 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::usage::mutated_variables; -use clippy_utils::{eq_expr_value, higher, match_def_path, meets_msrv, msrvs, paths}; +use clippy_utils::{eq_expr_value, higher, match_def_path, paths}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_hir::def::Res; @@ -11,7 +12,6 @@ use rustc_hir::BinOpKind; use rustc_hir::{BorrowKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Spanned; use rustc_span::Span; @@ -48,12 +48,12 @@ declare_clippy_lint! { } pub struct ManualStrip { - msrv: Option, + msrv: Msrv, } impl ManualStrip { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -68,7 +68,7 @@ enum StripKind { impl<'tcx> LateLintPass<'tcx> for ManualStrip { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if !meets_msrv(self.msrv, msrvs::STR_STRIP_PREFIX) { + if !self.msrv.meets(msrvs::STR_STRIP_PREFIX) { return; } diff --git a/clippy_lints/src/matches/mod.rs b/clippy_lints/src/matches/mod.rs index 7d8171ead89e..7b15a307fecf 100644 --- a/clippy_lints/src/matches/mod.rs +++ b/clippy_lints/src/matches/mod.rs @@ -23,13 +23,13 @@ mod single_match; mod try_err; mod wild_in_or_pats; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet_opt, walk_span_to_context}; -use clippy_utils::{higher, in_constant, is_span_match, meets_msrv, msrvs}; +use clippy_utils::{higher, in_constant, is_span_match}; use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat}; use rustc_lexer::{tokenize, TokenKind}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{Span, SpanData, SyntaxContext}; @@ -930,13 +930,13 @@ declare_clippy_lint! { #[derive(Default)] pub struct Matches { - msrv: Option, + msrv: Msrv, infallible_destructuring_match_linted: bool, } impl Matches { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, ..Matches::default() @@ -1000,9 +1000,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches { if !from_expansion && !contains_cfg_arm(cx, expr, ex, arms) { if source == MatchSource::Normal { - if !(meets_msrv(self.msrv, msrvs::MATCHES_MACRO) - && match_like_matches::check_match(cx, expr, ex, arms)) - { + if !(self.msrv.meets(msrvs::MATCHES_MACRO) && match_like_matches::check_match(cx, expr, ex, arms)) { match_same_arms::check(cx, arms); } @@ -1034,7 +1032,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches { collapsible_match::check_if_let(cx, if_let.let_pat, if_let.if_then, if_let.if_else); if !from_expansion { if let Some(else_expr) = if_let.if_else { - if meets_msrv(self.msrv, msrvs::MATCHES_MACRO) { + if self.msrv.meets(msrvs::MATCHES_MACRO) { match_like_matches::check_if_let( cx, expr, diff --git a/clippy_lints/src/matches/try_err.rs b/clippy_lints/src/matches/try_err.rs index c6cba81d8718..704c34c32bf7 100644 --- a/clippy_lints/src/matches/try_err.rs +++ b/clippy_lints/src/matches/try_err.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_applicability; use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{get_parent_expr, is_res_lang_ctor, match_def_path, path_res, paths}; +use clippy_utils::{get_parent_expr, is_res_lang_ctor, path_res}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::LangItem::ResultErr; @@ -107,7 +107,7 @@ fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option> { if_chain! { if let ty::Adt(def, subst) = ty.kind(); - if match_def_path(cx, def.did(), &paths::POLL); + if cx.tcx.lang_items().get(LangItem::Poll) == Some(def.did()); let ready_ty = subst.type_at(0); if let ty::Adt(ready_def, ready_subst) = ready_ty.kind(); @@ -124,7 +124,7 @@ fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option< fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option> { if_chain! { if let ty::Adt(def, subst) = ty.kind(); - if match_def_path(cx, def.did(), &paths::POLL); + if cx.tcx.lang_items().get(LangItem::Poll) == Some(def.did()); let ready_ty = subst.type_at(0); if let ty::Adt(ready_def, ready_subst) = ready_ty.kind(); diff --git a/clippy_lints/src/mem_replace.rs b/clippy_lints/src/mem_replace.rs index 0c4d9f100f7a..35024ec1224f 100644 --- a/clippy_lints/src/mem_replace.rs +++ b/clippy_lints/src/mem_replace.rs @@ -1,14 +1,14 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet, snippet_with_applicability}; use clippy_utils::ty::is_non_aggregate_primitive_type; -use clippy_utils::{is_default_equivalent, is_res_lang_ctor, meets_msrv, msrvs, path_res}; +use clippy_utils::{is_default_equivalent, is_res_lang_ctor, path_res}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::LangItem::OptionNone; use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; use rustc_span::symbol::sym; @@ -227,12 +227,12 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr< } pub struct MemReplace { - msrv: Option, + msrv: Msrv, } impl MemReplace { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -248,7 +248,7 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace { then { check_replace_option_with_none(cx, src, dest, expr.span); check_replace_with_uninit(cx, src, dest, expr.span); - if meets_msrv(self.msrv, msrvs::MEM_TAKE) { + if self.msrv.meets(msrvs::MEM_TAKE) { check_replace_with_default(cx, src, dest, expr.span); } } diff --git a/clippy_lints/src/methods/cloned_instead_of_copied.rs b/clippy_lints/src/methods/cloned_instead_of_copied.rs index e9aeab2d5b62..4e6ec61f6a83 100644 --- a/clippy_lints/src/methods/cloned_instead_of_copied.rs +++ b/clippy_lints/src/methods/cloned_instead_of_copied.rs @@ -1,25 +1,25 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::is_trait_method; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::{get_iterator_item_ty, is_copy}; -use clippy_utils::{is_trait_method, meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_span::{sym, Span}; use super::CLONED_INSTEAD_OF_COPIED; -pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, msrv: Option) { +pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, msrv: &Msrv) { let recv_ty = cx.typeck_results().expr_ty_adjusted(recv); let inner_ty = match recv_ty.kind() { // `Option` -> `T` ty::Adt(adt, subst) - if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && meets_msrv(msrv, msrvs::OPTION_COPIED) => + if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && msrv.meets(msrvs::OPTION_COPIED) => { subst.type_at(0) }, - _ if is_trait_method(cx, expr, sym::Iterator) && meets_msrv(msrv, msrvs::ITERATOR_COPIED) => { + _ if is_trait_method(cx, expr, sym::Iterator) && msrv.meets(msrvs::ITERATOR_COPIED) => { match get_iterator_item_ty(cx, recv_ty) { // ::Item Some(ty) => ty, diff --git a/clippy_lints/src/methods/err_expect.rs b/clippy_lints/src/methods/err_expect.rs index 720d9a68c85e..ae03da0d3f9c 100644 --- a/clippy_lints/src/methods/err_expect.rs +++ b/clippy_lints/src/methods/err_expect.rs @@ -1,27 +1,27 @@ use super::ERR_EXPECT; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::has_debug_impl; -use clippy_utils::{meets_msrv, msrvs, ty::is_type_diagnostic_item}; +use clippy_utils::ty::is_type_diagnostic_item; use rustc_errors::Applicability; use rustc_lint::LateContext; use rustc_middle::ty; use rustc_middle::ty::Ty; -use rustc_semver::RustcVersion; use rustc_span::{sym, Span}; pub(super) fn check( cx: &LateContext<'_>, _expr: &rustc_hir::Expr<'_>, recv: &rustc_hir::Expr<'_>, - msrv: Option, expect_span: Span, err_span: Span, + msrv: &Msrv, ) { if_chain! { if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result); // Test the version to make sure the lint can be showed (expect_err has been // introduced in rust 1.17.0 : https://github.com/rust-lang/rust/pull/38982) - if meets_msrv(msrv, msrvs::EXPECT_ERR); + if msrv.meets(msrvs::EXPECT_ERR); // Grabs the `Result` type let result_type = cx.typeck_results().expr_ty(recv); diff --git a/clippy_lints/src/methods/filter_map_next.rs b/clippy_lints/src/methods/filter_map_next.rs index ddf8a1f09b87..175e04f8ac06 100644 --- a/clippy_lints/src/methods/filter_map_next.rs +++ b/clippy_lints/src/methods/filter_map_next.rs @@ -1,10 +1,10 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; +use clippy_utils::is_trait_method; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; -use clippy_utils::{is_trait_method, meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; -use rustc_semver::RustcVersion; use rustc_span::sym; use super::FILTER_MAP_NEXT; @@ -14,10 +14,10 @@ pub(super) fn check<'tcx>( expr: &'tcx hir::Expr<'_>, recv: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { if is_trait_method(cx, expr, sym::Iterator) { - if !meets_msrv(msrv, msrvs::ITERATOR_FIND_MAP) { + if !msrv.meets(msrvs::ITERATOR_FIND_MAP) { return; } diff --git a/clippy_lints/src/methods/is_digit_ascii_radix.rs b/clippy_lints/src/methods/is_digit_ascii_radix.rs index 304024e80666..301aff5ae6ac 100644 --- a/clippy_lints/src/methods/is_digit_ascii_radix.rs +++ b/clippy_lints/src/methods/is_digit_ascii_radix.rs @@ -1,23 +1,22 @@ //! Lint for `c.is_digit(10)` use super::IS_DIGIT_ASCII_RADIX; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::{ - consts::constant_full_int, consts::FullInt, diagnostics::span_lint_and_sugg, meets_msrv, msrvs, - source::snippet_with_applicability, + consts::constant_full_int, consts::FullInt, diagnostics::span_lint_and_sugg, source::snippet_with_applicability, }; use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; -use rustc_semver::RustcVersion; pub(super) fn check<'tcx>( cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, self_arg: &'tcx Expr<'_>, radix: &'tcx Expr<'_>, - msrv: Option, + msrv: &Msrv, ) { - if !meets_msrv(msrv, msrvs::IS_ASCII_DIGIT) { + if !msrv.meets(msrvs::IS_ASCII_DIGIT) { return; } diff --git a/clippy_lints/src/methods/map_clone.rs b/clippy_lints/src/methods/map_clone.rs index 6bc783c6d505..52cc1e0464bf 100644 --- a/clippy_lints/src/methods/map_clone.rs +++ b/clippy_lints/src/methods/map_clone.rs @@ -1,7 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; use clippy_utils::ty::{is_copy, is_type_diagnostic_item}; -use clippy_utils::{is_diag_trait_item, meets_msrv, msrvs, peel_blocks}; +use clippy_utils::{is_diag_trait_item, peel_blocks}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir as hir; @@ -9,19 +10,12 @@ use rustc_lint::LateContext; use rustc_middle::mir::Mutability; use rustc_middle::ty; use rustc_middle::ty::adjustment::Adjust; -use rustc_semver::RustcVersion; use rustc_span::symbol::Ident; use rustc_span::{sym, Span}; use super::MAP_CLONE; -pub(super) fn check( - cx: &LateContext<'_>, - e: &hir::Expr<'_>, - recv: &hir::Expr<'_>, - arg: &hir::Expr<'_>, - msrv: Option, -) { +pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, msrv: &Msrv) { if_chain! { if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id); if cx.tcx.impl_of_method(method_id) @@ -97,10 +91,10 @@ fn lint_needless_cloning(cx: &LateContext<'_>, root: Span, receiver: Span) { ); } -fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: Option) { +fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: &Msrv) { let mut applicability = Applicability::MachineApplicable; - let (message, sugg_method) = if is_copy && meets_msrv(msrv, msrvs::ITERATOR_COPIED) { + let (message, sugg_method) = if is_copy && msrv.meets(msrvs::ITERATOR_COPIED) { ("you are using an explicit closure for copying elements", "copied") } else { ("you are using an explicit closure for cloning elements", "cloned") diff --git a/clippy_lints/src/methods/map_unwrap_or.rs b/clippy_lints/src/methods/map_unwrap_or.rs index 74fdead216b0..3122f72ee915 100644 --- a/clippy_lints/src/methods/map_unwrap_or.rs +++ b/clippy_lints/src/methods/map_unwrap_or.rs @@ -1,12 +1,11 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::usage::mutated_variables; -use clippy_utils::{meets_msrv, msrvs}; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; -use rustc_semver::RustcVersion; use rustc_span::symbol::sym; use super::MAP_UNWRAP_OR; @@ -19,13 +18,13 @@ pub(super) fn check<'tcx>( recv: &'tcx hir::Expr<'_>, map_arg: &'tcx hir::Expr<'_>, unwrap_arg: &'tcx hir::Expr<'_>, - msrv: Option, + msrv: &Msrv, ) -> bool { // lint if the caller of `map()` is an `Option` let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option); let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result); - if is_result && !meets_msrv(msrv, msrvs::RESULT_MAP_OR_ELSE) { + if is_result && !msrv.meets(msrvs::RESULT_MAP_OR_ELSE) { return false; } diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 38165ab4fb26..d2913680cbb7 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -104,8 +104,9 @@ mod zst_offset; use bind_instead_of_map::BindInsteadOfMap; use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint, span_lint_and_help}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::{contains_ty_adt_constructor_opaque, implements_trait, is_copy, is_type_diagnostic_item}; -use clippy_utils::{contains_return, is_bool, is_trait_method, iter_input_pats, meets_msrv, msrvs, return_ty}; +use clippy_utils::{contains_return, is_bool, is_trait_method, iter_input_pats, return_ty}; use if_chain::if_chain; use rustc_hir as hir; use rustc_hir::{Expr, ExprKind, TraitItem, TraitItemKind}; @@ -113,7 +114,6 @@ use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::{self, TraitRef, Ty}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{sym, Span}; @@ -3163,7 +3163,7 @@ declare_clippy_lint! { pub struct Methods { avoid_breaking_exported_api: bool, - msrv: Option, + msrv: Msrv, allow_expect_in_tests: bool, allow_unwrap_in_tests: bool, } @@ -3172,7 +3172,7 @@ impl Methods { #[must_use] pub fn new( avoid_breaking_exported_api: bool, - msrv: Option, + msrv: Msrv, allow_expect_in_tests: bool, allow_unwrap_in_tests: bool, ) -> Self { @@ -3325,7 +3325,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { single_char_add_str::check(cx, expr, receiver, args); into_iter_on_ref::check(cx, expr, method_span, method_call.ident.name, receiver); single_char_pattern::check(cx, expr, method_call.ident.name, receiver, args); - unnecessary_to_owned::check(cx, expr, method_call.ident.name, receiver, args, self.msrv); + unnecessary_to_owned::check(cx, expr, method_call.ident.name, receiver, args, &self.msrv); }, hir::ExprKind::Binary(op, lhs, rhs) if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne => { let mut info = BinaryExprInfo { @@ -3501,7 +3501,7 @@ impl Methods { ("as_mut", []) => useless_asref::check(cx, expr, "as_mut", recv), ("as_ref", []) => useless_asref::check(cx, expr, "as_ref", recv), ("assume_init", []) => uninit_assumed_init::check(cx, expr, recv), - ("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, self.msrv), + ("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, &self.msrv), ("collect", []) if is_trait_method(cx, expr, sym::Iterator) => { needless_collect::check(cx, span, expr, recv, call_span); match method_call(recv) { @@ -3512,7 +3512,7 @@ impl Methods { map_collect_result_unit::check(cx, expr, m_recv, m_arg); }, Some(("take", take_self_arg, [take_arg], _, _)) => { - if meets_msrv(self.msrv, msrvs::STR_REPEAT) { + if self.msrv.meets(msrvs::STR_REPEAT) { manual_str_repeat::check(cx, expr, recv, take_self_arg, take_arg); } }, @@ -3539,7 +3539,7 @@ impl Methods { }, ("expect", [_]) => match method_call(recv) { Some(("ok", recv, [], _, _)) => ok_expect::check(cx, expr, recv), - Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, self.msrv, span, err_span), + Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, span, err_span, &self.msrv), _ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests), }, ("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests), @@ -3578,7 +3578,7 @@ impl Methods { unit_hash::check(cx, expr, recv, arg); }, ("is_file", []) => filetype_is_file::check(cx, expr, recv), - ("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, self.msrv), + ("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, &self.msrv), ("is_none", []) => check_is_some_is_none(cx, expr, recv, false), ("is_some", []) => check_is_some_is_none(cx, expr, recv, true), ("iter" | "iter_mut" | "into_iter", []) => { @@ -3601,7 +3601,7 @@ impl Methods { }, (name @ ("map" | "map_err"), [m_arg]) => { if name == "map" { - map_clone::check(cx, expr, recv, m_arg, self.msrv); + map_clone::check(cx, expr, recv, m_arg, &self.msrv); if let Some((map_name @ ("iter" | "into_iter"), recv2, _, _, _)) = method_call(recv) { iter_kv_map::check(cx, map_name, expr, recv2, m_arg); } @@ -3610,8 +3610,8 @@ impl Methods { } if let Some((name, recv2, args, span2,_)) = method_call(recv) { match (name, args) { - ("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, self.msrv), - ("as_ref", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, false, self.msrv), + ("as_mut", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, true, &self.msrv), + ("as_ref", []) => option_as_ref_deref::check(cx, expr, recv2, m_arg, false, &self.msrv), ("filter", [f_arg]) => { filter_map::check(cx, expr, recv2, f_arg, span2, recv, m_arg, span, false); }, @@ -3632,7 +3632,7 @@ impl Methods { match (name2, args2) { ("cloned", []) => iter_overeager_cloned::check(cx, expr, recv, recv2, false, false), ("filter", [arg]) => filter_next::check(cx, expr, recv2, arg), - ("filter_map", [arg]) => filter_map_next::check(cx, expr, recv2, arg, self.msrv), + ("filter_map", [arg]) => filter_map_next::check(cx, expr, recv2, arg, &self.msrv), ("iter", []) => iter_next_slice::check(cx, expr, recv2), ("skip", [arg]) => iter_skip_next::check(cx, expr, recv2, arg), ("skip_while", [_]) => skip_while_next::check(cx, expr), @@ -3680,10 +3680,10 @@ impl Methods { vec_resize_to_zero::check(cx, expr, count_arg, default_arg, span); }, ("seek", [arg]) => { - if meets_msrv(self.msrv, msrvs::SEEK_FROM_CURRENT) { + if self.msrv.meets(msrvs::SEEK_FROM_CURRENT) { seek_from_current::check(cx, expr, recv, arg); } - if meets_msrv(self.msrv, msrvs::SEEK_REWIND) { + if self.msrv.meets(msrvs::SEEK_REWIND) { seek_to_start_instead_of_rewind::check(cx, expr, recv, arg, span); } }, @@ -3699,7 +3699,7 @@ impl Methods { ("splitn" | "rsplitn", [count_arg, pat_arg]) => { if let Some((Constant::Int(count), _)) = constant(cx, cx.typeck_results(), count_arg) { suspicious_splitn::check(cx, name, expr, recv, count); - str_splitn::check(cx, name, expr, recv, pat_arg, count, self.msrv); + str_splitn::check(cx, name, expr, recv, pat_arg, count, &self.msrv); } }, ("splitn_mut" | "rsplitn_mut", [count_arg, _]) => { @@ -3717,7 +3717,7 @@ impl Methods { }, ("take", []) => needless_option_take::check(cx, expr, recv), ("then", [arg]) => { - if !meets_msrv(self.msrv, msrvs::BOOL_THEN_SOME) { + if !self.msrv.meets(msrvs::BOOL_THEN_SOME) { return; } unnecessary_lazy_eval::check(cx, expr, recv, arg, "then_some"); @@ -3760,7 +3760,7 @@ impl Methods { }, ("unwrap_or_else", [u_arg]) => match method_call(recv) { Some(("map", recv, [map_arg], _, _)) - if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, self.msrv) => {}, + if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, &self.msrv) => {}, _ => { unwrap_or_else_default::check(cx, expr, recv, u_arg); unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or"); diff --git a/clippy_lints/src/methods/option_as_ref_deref.rs b/clippy_lints/src/methods/option_as_ref_deref.rs index e6eb64bcbde6..3e33f9193374 100644 --- a/clippy_lints/src/methods/option_as_ref_deref.rs +++ b/clippy_lints/src/methods/option_as_ref_deref.rs @@ -1,13 +1,13 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{match_def_path, meets_msrv, msrvs, path_to_local_id, paths, peel_blocks}; +use clippy_utils::{match_def_path, path_to_local_id, paths, peel_blocks}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_span::sym; use super::OPTION_AS_REF_DEREF; @@ -19,9 +19,9 @@ pub(super) fn check( as_ref_recv: &hir::Expr<'_>, map_arg: &hir::Expr<'_>, is_mut: bool, - msrv: Option, + msrv: &Msrv, ) { - if !meets_msrv(msrv, msrvs::OPTION_AS_DEREF) { + if !msrv.meets(msrvs::OPTION_AS_DEREF) { return; } diff --git a/clippy_lints/src/methods/str_splitn.rs b/clippy_lints/src/methods/str_splitn.rs index 1acac59144ce..3c01ce1fecd3 100644 --- a/clippy_lints/src/methods/str_splitn.rs +++ b/clippy_lints/src/methods/str_splitn.rs @@ -1,9 +1,10 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_context; use clippy_utils::usage::local_used_after_expr; use clippy_utils::visitors::{for_each_expr_with_closures, Descend}; -use clippy_utils::{is_diag_item_method, match_def_path, meets_msrv, msrvs, path_to_local_id, paths}; +use clippy_utils::{is_diag_item_method, match_def_path, path_to_local_id, paths}; use core::ops::ControlFlow; use if_chain::if_chain; use rustc_errors::Applicability; @@ -12,7 +13,6 @@ use rustc_hir::{ }; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_span::{sym, Span, Symbol, SyntaxContext}; use super::{MANUAL_SPLIT_ONCE, NEEDLESS_SPLITN}; @@ -24,7 +24,7 @@ pub(super) fn check( self_arg: &Expr<'_>, pat_arg: &Expr<'_>, count: u128, - msrv: Option, + msrv: &Msrv, ) { if count < 2 || !cx.typeck_results().expr_ty_adjusted(self_arg).peel_refs().is_str() { return; @@ -34,7 +34,7 @@ pub(super) fn check( IterUsageKind::Nth(n) => count > n + 1, IterUsageKind::NextTuple => count > 2, }; - let manual = count == 2 && meets_msrv(msrv, msrvs::STR_SPLIT_ONCE); + let manual = count == 2 && msrv.meets(msrvs::STR_SPLIT_ONCE); match parse_iter_usage(cx, expr.span.ctxt(), cx.tcx.hir().parent_iter(expr.hir_id)) { Some(usage) if needless(usage.kind) => lint_needless(cx, method_name, expr, self_arg, pat_arg), diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index 7ff13b95626b..17b0507682ae 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -1,13 +1,11 @@ use super::implicit_clone::is_clone_like; use super::unnecessary_iter_cloned::{self, is_into_iter}; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs}; use clippy_utils::visitors::find_all_ret_expressions; -use clippy_utils::{ - fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty, -}; -use clippy_utils::{meets_msrv, msrvs}; +use clippy_utils::{fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty}; use rustc_errors::Applicability; use rustc_hir::{def_id::DefId, BorrowKind, Expr, ExprKind, ItemKind, Node}; use rustc_hir_typeck::{FnCtxt, Inherited}; @@ -16,14 +14,9 @@ use rustc_lint::LateContext; use rustc_middle::mir::Mutability; use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref}; use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef}; -use rustc_middle::ty::EarlyBinder; -use rustc_middle::ty::{self, Clause, ParamTy, PredicateKind, ProjectionPredicate, TraitPredicate, Ty}; -use rustc_semver::RustcVersion; +use rustc_middle::ty::{self, Clause, EarlyBinder, ParamTy, PredicateKind, ProjectionPredicate, TraitPredicate, Ty}; use rustc_span::{sym, Symbol}; -use rustc_trait_selection::traits::{ - query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause, -}; -use std::cmp::max; +use rustc_trait_selection::traits::{query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause}; use super::UNNECESSARY_TO_OWNED; @@ -33,7 +26,7 @@ pub fn check<'tcx>( method_name: Symbol, receiver: &'tcx Expr<'_>, args: &'tcx [Expr<'_>], - msrv: Option, + msrv: &Msrv, ) { if_chain! { if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); @@ -204,7 +197,7 @@ fn check_into_iter_call_arg( expr: &Expr<'_>, method_name: Symbol, receiver: &Expr<'_>, - msrv: Option, + msrv: &Msrv, ) -> bool { if_chain! { if let Some(parent) = get_parent_expr(cx, expr); @@ -219,7 +212,7 @@ fn check_into_iter_call_arg( if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) { return true; } - let cloned_or_copied = if is_copy(cx, item_ty) && meets_msrv(msrv, msrvs::ITERATOR_COPIED) { + let cloned_or_copied = if is_copy(cx, item_ty) && msrv.meets(msrvs::ITERATOR_COPIED) { "copied" } else { "cloned" @@ -267,11 +260,22 @@ fn check_other_call_arg<'tcx>( if let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef); if trait_predicate.def_id() == deref_trait_id || trait_predicate.def_id() == as_ref_trait_id; let receiver_ty = cx.typeck_results().expr_ty(receiver); - if can_change_type(cx, maybe_arg, receiver_ty); // We can't add an `&` when the trait is `Deref` because `Target = &T` won't match // `Target = T`. - if n_refs > 0 || is_copy(cx, receiver_ty) || trait_predicate.def_id() != deref_trait_id; - let n_refs = max(n_refs, usize::from(!is_copy(cx, receiver_ty))); + if let Some((n_refs, receiver_ty)) = if n_refs > 0 || is_copy(cx, receiver_ty) { + Some((n_refs, receiver_ty)) + } else if trait_predicate.def_id() != deref_trait_id { + Some((1, cx.tcx.mk_ref( + cx.tcx.lifetimes.re_erased, + ty::TypeAndMut { + ty: receiver_ty, + mutbl: Mutability::Not, + }, + ))) + } else { + None + }; + if can_change_type(cx, maybe_arg, receiver_ty); if let Some(receiver_snippet) = snippet_opt(cx, receiver.span); then { span_lint_and_sugg( @@ -345,13 +349,13 @@ fn get_input_traits_and_projections<'tcx>( if trait_predicate.trait_ref.self_ty() == input { trait_predicates.push(trait_predicate); } - } + }, PredicateKind::Clause(Clause::Projection(projection_predicate)) => { if projection_predicate.projection_ty.self_ty() == input { projection_predicates.push(projection_predicate); } - } - _ => {} + }, + _ => {}, } } (trait_predicates, projection_predicates) @@ -403,10 +407,12 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty< let mut trait_predicates = cx.tcx.param_env(callee_def_id) .caller_bounds().iter().filter(|predicate| { - if let PredicateKind::Clause(Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() - && trait_predicate.trait_ref.self_ty() == *param_ty { - true - } else { + if let PredicateKind::Clause(Clause::Trait(trait_predicate)) + = predicate.kind().skip_binder() + && trait_predicate.trait_ref.self_ty() == *param_ty + { + true + } else { false } }); @@ -466,12 +472,7 @@ fn is_cloned_or_copied(cx: &LateContext<'_>, method_name: Symbol, method_def_id: /// Returns true if the named method can be used to convert the receiver to its "owned" /// representation. -fn is_to_owned_like<'a>( - cx: &LateContext<'a>, - call_expr: &Expr<'a>, - method_name: Symbol, - method_def_id: DefId, -) -> bool { +fn is_to_owned_like<'a>(cx: &LateContext<'a>, call_expr: &Expr<'a>, method_name: Symbol, method_def_id: DefId) -> bool { is_clone_like(cx, method_name.as_str(), method_def_id) || is_cow_into_owned(cx, method_name, method_def_id) || is_to_string_on_string_like(cx, call_expr, method_name, method_def_id) diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs index 71cc0d0a81cd..5bc04bc17fb4 100644 --- a/clippy_lints/src/missing_const_for_fn.rs +++ b/clippy_lints/src/missing_const_for_fn.rs @@ -1,9 +1,8 @@ use clippy_utils::diagnostics::span_lint; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::qualify_min_const_fn::is_min_const_fn; use clippy_utils::ty::has_drop; -use clippy_utils::{ - fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, meets_msrv, msrvs, trait_ref_of_method, -}; +use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, trait_ref_of_method}; use rustc_hir as hir; use rustc_hir::def_id::CRATE_DEF_ID; use rustc_hir::intravisit::FnKind; @@ -11,7 +10,6 @@ use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId}; use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; @@ -75,12 +73,12 @@ declare_clippy_lint! { impl_lint_pass!(MissingConstForFn => [MISSING_CONST_FOR_FN]); pub struct MissingConstForFn { - msrv: Option, + msrv: Msrv, } impl MissingConstForFn { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -95,7 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { span: Span, hir_id: HirId, ) { - if !meets_msrv(self.msrv, msrvs::CONST_IF_MATCH) { + if !self.msrv.meets(msrvs::CONST_IF_MATCH) { return; } @@ -152,7 +150,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { let mir = cx.tcx.optimized_mir(def_id); - if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv) { + if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, &self.msrv) { if cx.tcx.is_const_fn_raw(def_id.to_def_id()) { cx.tcx.sess.span_err(span, err.as_ref()); } diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 75e12715458f..2f0b7ce16e51 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -1,7 +1,9 @@ use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then}; use clippy_utils::ptr::get_spans; use clippy_utils::source::{snippet, snippet_opt}; -use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy, is_type_diagnostic_item, is_type_lang_item}; +use clippy_utils::ty::{ + implements_trait, implements_trait_with_env, is_copy, is_type_diagnostic_item, is_type_lang_item, +}; use clippy_utils::{get_trait_def_id, is_self, paths}; use if_chain::if_chain; use rustc_ast::ast::Attribute; @@ -124,7 +126,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { .filter_map(|obligation| { // Note that we do not want to deal with qualified predicates here. match obligation.predicate.kind().no_bound_vars() { - Some(ty::PredicateKind::Clause(ty::Clause::Trait(pred))) if pred.def_id() != sized_trait => Some(pred), + Some(ty::PredicateKind::Clause(ty::Clause::Trait(pred))) if pred.def_id() != sized_trait => { + Some(pred) + }, _ => None, } }) diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs index 819646bb6780..79c1ae4861e8 100644 --- a/clippy_lints/src/no_effect.rs +++ b/clippy_lints/src/no_effect.rs @@ -6,7 +6,8 @@ use clippy_utils::ty::has_drop; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, PatKind, Stmt, StmtKind, UnsafeSource}; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; use rustc_session::{declare_lint_pass, declare_tool_lint}; use std::ops::Deref; @@ -159,8 +160,11 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) { if_chain! { if let StmtKind::Semi(expr) = stmt.kind; + let ctxt = stmt.span.ctxt(); + if expr.span.ctxt() == ctxt; if let Some(reduced) = reduce_expression(cx, expr); - if !&reduced.iter().any(|e| e.span.from_expansion()); + if !in_external_macro(cx.sess(), stmt.span); + if reduced.iter().all(|e| e.span.ctxt() == ctxt); then { if let ExprKind::Index(..) = &expr.kind { let snippet = if let (Some(arr), Some(func)) = diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 92920bbad6e0..e395ff54cb15 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -490,7 +490,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>( ty_name: name.ident.name, method_renames, ref_prefix: RefPrefix { - lt: lt.clone(), + lt: *lt, mutability, }, deref_ty, @@ -693,9 +693,10 @@ fn matches_preds<'tcx>( cx.tcx, ObligationCause::dummy(), cx.param_env, - cx.tcx.mk_predicate(Binder::dummy( - PredicateKind::Clause(Clause::Projection(p.with_self_ty(cx.tcx, ty))), - )), + cx.tcx + .mk_predicate(Binder::dummy(PredicateKind::Clause(Clause::Projection( + p.with_self_ty(cx.tcx, ty), + )))), )), ExistentialPredicate::AutoTrait(p) => infcx .type_implements_trait(p, [ty], cx.param_env) diff --git a/clippy_lints/src/ranges.rs b/clippy_lints/src/ranges.rs index c6fbb5e805ab..0a1b9d173cf9 100644 --- a/clippy_lints/src/ranges.rs +++ b/clippy_lints/src/ranges.rs @@ -1,16 +1,16 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then}; use clippy_utils::higher; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::{snippet, snippet_opt, snippet_with_applicability}; use clippy_utils::sugg::Sugg; -use clippy_utils::{get_parent_expr, in_constant, is_integer_const, meets_msrv, msrvs, path_to_local}; +use clippy_utils::{get_parent_expr, in_constant, is_integer_const, path_to_local}; use if_chain::if_chain; use rustc_ast::ast::RangeLimits; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, HirId}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::{Span, Spanned}; use std::cmp::Ordering; @@ -161,12 +161,12 @@ declare_clippy_lint! { } pub struct Ranges { - msrv: Option, + msrv: Msrv, } impl Ranges { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -181,7 +181,7 @@ impl_lint_pass!(Ranges => [ impl<'tcx> LateLintPass<'tcx> for Ranges { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if let ExprKind::Binary(ref op, l, r) = expr.kind { - if meets_msrv(self.msrv, msrvs::RANGE_CONTAINS) { + if self.msrv.meets(msrvs::RANGE_CONTAINS) { check_possible_range_contains(cx, op.node, l, r, expr, expr.span); } } diff --git a/clippy_lints/src/redundant_closure_call.rs b/clippy_lints/src/redundant_closure_call.rs index 8e675d34a183..2a42e73488f1 100644 --- a/clippy_lints/src/redundant_closure_call.rs +++ b/clippy_lints/src/redundant_closure_call.rs @@ -81,8 +81,8 @@ impl EarlyLintPass for RedundantClosureCall { "try not to call a closure in the expression where it is declared", |diag| { if fn_decl.inputs.is_empty() { - let app = Applicability::MachineApplicable; - let mut hint = Sugg::ast(cx, body, ".."); + let mut app = Applicability::MachineApplicable; + let mut hint = Sugg::ast(cx, body, "..", closure.span.ctxt(), &mut app); if asyncness.is_async() { // `async x` is a syntax error, so it becomes `async { x }` diff --git a/clippy_lints/src/redundant_field_names.rs b/clippy_lints/src/redundant_field_names.rs index 40b03068f6c7..61bff4a0e38d 100644 --- a/clippy_lints/src/redundant_field_names.rs +++ b/clippy_lints/src/redundant_field_names.rs @@ -1,10 +1,9 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::{meets_msrv, msrvs}; +use clippy_utils::msrvs::{self, Msrv}; use rustc_ast::ast::{Expr, ExprKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_middle::lint::in_external_macro; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -37,12 +36,12 @@ declare_clippy_lint! { } pub struct RedundantFieldNames { - msrv: Option, + msrv: Msrv, } impl RedundantFieldNames { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -51,7 +50,7 @@ impl_lint_pass!(RedundantFieldNames => [REDUNDANT_FIELD_NAMES]); impl EarlyLintPass for RedundantFieldNames { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if !meets_msrv(self.msrv, msrvs::FIELD_INIT_SHORTHAND) { + if !self.msrv.meets(msrvs::FIELD_INIT_SHORTHAND) { return; } diff --git a/clippy_lints/src/redundant_static_lifetimes.rs b/clippy_lints/src/redundant_static_lifetimes.rs index 60ba62c4a433..3aa2490bc44e 100644 --- a/clippy_lints/src/redundant_static_lifetimes.rs +++ b/clippy_lints/src/redundant_static_lifetimes.rs @@ -1,10 +1,9 @@ use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet; -use clippy_utils::{meets_msrv, msrvs}; use rustc_ast::ast::{Item, ItemKind, Ty, TyKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; declare_clippy_lint! { @@ -34,12 +33,12 @@ declare_clippy_lint! { } pub struct RedundantStaticLifetimes { - msrv: Option, + msrv: Msrv, } impl RedundantStaticLifetimes { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -96,7 +95,7 @@ impl RedundantStaticLifetimes { impl EarlyLintPass for RedundantStaticLifetimes { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if !meets_msrv(self.msrv, msrvs::STATIC_IN_CONST) { + if !self.msrv.meets(msrvs::STATIC_IN_CONST) { return; } diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 2b2a41d16011..81143d7799ea 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -12,6 +12,7 @@ use rustc_middle::lint::in_external_macro; use rustc_middle::ty::subst::GenericArgKind; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; +use rustc_span::{BytePos, Pos}; declare_clippy_lint! { /// ### What it does @@ -209,13 +210,14 @@ fn check_final_expr<'tcx>( if cx.tcx.hir().attrs(expr.hir_id).is_empty() { let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner)); if !borrows { - emit_return_lint( - cx, - peeled_drop_expr.span, - semi_spans, - inner.as_ref().map(|i| i.span), - replacement, - ); + // check if expr return nothing + let ret_span = if inner.is_none() && replacement == RetReplacement::Empty { + extend_span_to_previous_non_ws(cx, peeled_drop_expr.span) + } else { + peeled_drop_expr.span + }; + + emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement); } } }, @@ -289,3 +291,16 @@ fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) }) .is_some() } + +// Go backwards while encountering whitespace and extend the given Span to that point. +fn extend_span_to_previous_non_ws(cx: &LateContext<'_>, sp: Span) -> Span { + if let Ok(prev_source) = cx.sess().source_map().span_to_prev_source(sp) { + let ws = [' ', '\t', '\n']; + if let Some(non_ws_pos) = prev_source.rfind(|c| !ws.contains(&c)) { + let len = prev_source.len() - non_ws_pos - 1; + return sp.with_lo(sp.lo() - BytePos::from_usize(len)); + } + } + + sp +} diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs index 424a6e9264e4..83e651aba8e8 100644 --- a/clippy_lints/src/transmute/mod.rs +++ b/clippy_lints/src/transmute/mod.rs @@ -16,10 +16,10 @@ mod utils; mod wrong_transmute; use clippy_utils::in_constant; +use clippy_utils::msrvs::Msrv; use if_chain::if_chain; use rustc_hir::{Expr, ExprKind, QPath}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::symbol::sym; @@ -410,7 +410,7 @@ declare_clippy_lint! { } pub struct Transmute { - msrv: Option, + msrv: Msrv, } impl_lint_pass!(Transmute => [ CROSSPOINTER_TRANSMUTE, @@ -431,7 +431,7 @@ impl_lint_pass!(Transmute => [ ]); impl Transmute { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -461,7 +461,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute { let linted = wrong_transmute::check(cx, e, from_ty, to_ty) | crosspointer_transmute::check(cx, e, from_ty, to_ty) | transmuting_null::check(cx, e, arg, to_ty) - | transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, path, self.msrv) + | transmute_ptr_to_ref::check(cx, e, from_ty, to_ty, arg, path, &self.msrv) | transmute_int_to_char::check(cx, e, from_ty, to_ty, arg, const_context) | transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context) | transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg) diff --git a/clippy_lints/src/transmute/transmute_ptr_to_ref.rs b/clippy_lints/src/transmute/transmute_ptr_to_ref.rs index 12d0b866e1c9..3dde4eee6717 100644 --- a/clippy_lints/src/transmute/transmute_ptr_to_ref.rs +++ b/clippy_lints/src/transmute/transmute_ptr_to_ref.rs @@ -1,12 +1,12 @@ use super::TRANSMUTE_PTR_TO_REF; use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{meets_msrv, msrvs, sugg}; +use clippy_utils::sugg; use rustc_errors::Applicability; use rustc_hir::{self as hir, Expr, GenericArg, Mutability, Path, TyKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty, TypeVisitable}; -use rustc_semver::RustcVersion; /// Checks for `transmute_ptr_to_ref` lint. /// Returns `true` if it's triggered, otherwise returns `false`. @@ -17,7 +17,7 @@ pub(super) fn check<'tcx>( to_ty: Ty<'tcx>, arg: &'tcx Expr<'_>, path: &'tcx Path<'_>, - msrv: Option, + msrv: &Msrv, ) -> bool { match (&from_ty.kind(), &to_ty.kind()) { (ty::RawPtr(from_ptr_ty), ty::Ref(_, to_ref_ty, mutbl)) => { @@ -37,7 +37,7 @@ pub(super) fn check<'tcx>( let sugg = if let Some(ty) = get_explicit_type(path) { let ty_snip = snippet_with_applicability(cx, ty.span, "..", &mut app); - if meets_msrv(msrv, msrvs::POINTER_CAST) { + if msrv.meets(msrvs::POINTER_CAST) { format!("{deref}{}.cast::<{ty_snip}>()", arg.maybe_par()) } else if from_ptr_ty.has_erased_regions() { sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {ty_snip}"))).to_string() @@ -46,7 +46,7 @@ pub(super) fn check<'tcx>( } } else if from_ptr_ty.ty == *to_ref_ty { if from_ptr_ty.has_erased_regions() { - if meets_msrv(msrv, msrvs::POINTER_CAST) { + if msrv.meets(msrvs::POINTER_CAST) { format!("{deref}{}.cast::<{to_ref_ty}>()", arg.maybe_par()) } else { sugg::make_unop(deref, arg.as_ty(format!("{cast} () as {cast} {to_ref_ty}"))) diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs index e8f15a444735..2e1b6d8d4ea7 100644 --- a/clippy_lints/src/undocumented_unsafe_blocks.rs +++ b/clippy_lints/src/undocumented_unsafe_blocks.rs @@ -1,6 +1,10 @@ +use std::ops::ControlFlow; + use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::source::walk_span_to_context; +use clippy_utils::visitors::{for_each_expr_with_closures, Descend}; use clippy_utils::{get_parent_node, is_lint_allowed}; +use hir::HirId; use rustc_data_structures::sync::Lrc; use rustc_hir as hir; use rustc_hir::{Block, BlockCheckMode, ItemKind, Node, UnsafeSource}; @@ -59,11 +63,39 @@ declare_clippy_lint! { restriction, "creating an unsafe block without explaining why it is safe" } +declare_clippy_lint! { + /// ### What it does + /// Checks for `// SAFETY: ` comments on safe code. + /// + /// ### Why is this bad? + /// Safe code has no safety requirements, so there is no need to + /// describe safety invariants. + /// + /// ### Example + /// ```rust + /// use std::ptr::NonNull; + /// let a = &mut 42; + /// + /// // SAFETY: references are guaranteed to be non-null. + /// let ptr = NonNull::new(a).unwrap(); + /// ``` + /// Use instead: + /// ```rust + /// use std::ptr::NonNull; + /// let a = &mut 42; + /// + /// let ptr = NonNull::new(a).unwrap(); + /// ``` + #[clippy::version = "1.67.0"] + pub UNNECESSARY_SAFETY_COMMENT, + restriction, + "annotating safe code with a safety comment" +} -declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS]); +declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS, UNNECESSARY_SAFETY_COMMENT]); -impl LateLintPass<'_> for UndocumentedUnsafeBlocks { - fn check_block(&mut self, cx: &LateContext<'_>, block: &'_ Block<'_>) { +impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks { + fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { if block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) && !in_external_macro(cx.tcx.sess, block.span) && !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, block.hir_id) @@ -87,35 +119,175 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks { "consider adding a safety comment on the preceding line", ); } + + if let Some(tail) = block.expr + && !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, tail.hir_id) + && !in_external_macro(cx.tcx.sess, tail.span) + && let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, tail.span, tail.hir_id) + && let Some(help_span) = expr_has_unnecessary_safety_comment(cx, tail, pos) + { + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + tail.span, + "expression has unnecessary safety comment", + Some(help_span), + "consider removing the safety comment", + ); + } + } + + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &hir::Stmt<'tcx>) { + let ( + hir::StmtKind::Local(&hir::Local { init: Some(expr), .. }) + | hir::StmtKind::Expr(expr) + | hir::StmtKind::Semi(expr) + ) = stmt.kind else { return }; + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, stmt.hir_id) + && !in_external_macro(cx.tcx.sess, stmt.span) + && let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, stmt.span, stmt.hir_id) + && let Some(help_span) = expr_has_unnecessary_safety_comment(cx, expr, pos) + { + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + stmt.span, + "statement has unnecessary safety comment", + Some(help_span), + "consider removing the safety comment", + ); + } } fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) { - if let hir::ItemKind::Impl(imple) = item.kind - && imple.unsafety == hir::Unsafety::Unsafe - && !in_external_macro(cx.tcx.sess, item.span) - && !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id()) - && !is_unsafe_from_proc_macro(cx, item.span) - && !item_has_safety_comment(cx, item) - { + if in_external_macro(cx.tcx.sess, item.span) { + return; + } + + let mk_spans = |pos: BytePos| { let source_map = cx.tcx.sess.source_map(); + let span = Span::new(pos, pos, SyntaxContext::root(), None); + let help_span = source_map.span_extend_to_next_char(span, '\n', true); let span = if source_map.is_multiline(item.span) { source_map.span_until_char(item.span, '\n') } else { item.span }; + (span, help_span) + }; - span_lint_and_help( - cx, - UNDOCUMENTED_UNSAFE_BLOCKS, - span, - "unsafe impl missing a safety comment", - None, - "consider adding a safety comment on the preceding line", - ); + let item_has_safety_comment = item_has_safety_comment(cx, item); + match (&item.kind, item_has_safety_comment) { + // lint unsafe impl without safety comment + (hir::ItemKind::Impl(impl_), HasSafetyComment::No) if impl_.unsafety == hir::Unsafety::Unsafe => { + if !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id()) + && !is_unsafe_from_proc_macro(cx, item.span) + { + let source_map = cx.tcx.sess.source_map(); + let span = if source_map.is_multiline(item.span) { + source_map.span_until_char(item.span, '\n') + } else { + item.span + }; + + span_lint_and_help( + cx, + UNDOCUMENTED_UNSAFE_BLOCKS, + span, + "unsafe impl missing a safety comment", + None, + "consider adding a safety comment on the preceding line", + ); + } + }, + // lint safe impl with unnecessary safety comment + (hir::ItemKind::Impl(impl_), HasSafetyComment::Yes(pos)) if impl_.unsafety == hir::Unsafety::Normal => { + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) { + let (span, help_span) = mk_spans(pos); + + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + span, + "impl has unnecessary safety comment", + Some(help_span), + "consider removing the safety comment", + ); + } + }, + (hir::ItemKind::Impl(_), _) => {}, + // const and static items only need a safety comment if their body is an unsafe block, lint otherwise + (&hir::ItemKind::Const(.., body) | &hir::ItemKind::Static(.., body), HasSafetyComment::Yes(pos)) => { + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, body.hir_id) { + let body = cx.tcx.hir().body(body); + if !matches!( + body.value.kind, hir::ExprKind::Block(block, _) + if block.rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) + ) { + let (span, help_span) = mk_spans(pos); + + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + span, + &format!("{} has unnecessary safety comment", item.kind.descr()), + Some(help_span), + "consider removing the safety comment", + ); + } + } + }, + // Aside from unsafe impls and consts/statics with an unsafe block, items in general + // do not have safety invariants that need to be documented, so lint those. + (_, HasSafetyComment::Yes(pos)) => { + if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) { + let (span, help_span) = mk_spans(pos); + + span_lint_and_help( + cx, + UNNECESSARY_SAFETY_COMMENT, + span, + &format!("{} has unnecessary safety comment", item.kind.descr()), + Some(help_span), + "consider removing the safety comment", + ); + } + }, + _ => (), } } } +fn expr_has_unnecessary_safety_comment<'tcx>( + cx: &LateContext<'tcx>, + expr: &'tcx hir::Expr<'tcx>, + comment_pos: BytePos, +) -> Option { + // this should roughly be the reverse of `block_parents_have_safety_comment` + if for_each_expr_with_closures(cx, expr, |expr| match expr.kind { + hir::ExprKind::Block( + Block { + rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), + .. + }, + _, + ) => ControlFlow::Break(()), + // statements will be handled by check_stmt itself again + hir::ExprKind::Block(..) => ControlFlow::Continue(Descend::No), + _ => ControlFlow::Continue(Descend::Yes), + }) + .is_some() + { + return None; + } + + let source_map = cx.tcx.sess.source_map(); + let span = Span::new(comment_pos, comment_pos, SyntaxContext::root(), None); + let help_span = source_map.span_extend_to_next_char(span, '\n', true); + + Some(help_span) +} + fn is_unsafe_from_proc_macro(cx: &LateContext<'_>, span: Span) -> bool { let source_map = cx.sess().source_map(); let file_pos = source_map.lookup_byte_offset(span.lo()); @@ -170,85 +342,134 @@ fn block_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { // won't work. This is to avoid dealing with where such a comment should be place relative to // attributes and doc comments. - span_from_macro_expansion_has_safety_comment(cx, span) || span_in_body_has_safety_comment(cx, span) + matches!( + span_from_macro_expansion_has_safety_comment(cx, span), + HasSafetyComment::Yes(_) + ) || span_in_body_has_safety_comment(cx, span) +} + +enum HasSafetyComment { + Yes(BytePos), + No, + Maybe, } /// Checks if the lines immediately preceding the item contain a safety comment. #[allow(clippy::collapsible_match)] -fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> bool { - if span_from_macro_expansion_has_safety_comment(cx, item.span) { - return true; +fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSafetyComment { + match span_from_macro_expansion_has_safety_comment(cx, item.span) { + HasSafetyComment::Maybe => (), + has_safety_comment => return has_safety_comment, } - if item.span.ctxt() == SyntaxContext::root() { - if let Some(parent_node) = get_parent_node(cx.tcx, item.hir_id()) { - let comment_start = match parent_node { - Node::Crate(parent_mod) => { - comment_start_before_impl_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item) - }, - Node::Item(parent_item) => { - if let ItemKind::Mod(parent_mod) = &parent_item.kind { - comment_start_before_impl_in_mod(cx, parent_mod, parent_item.span, item) - } else { - // Doesn't support impls in this position. Pretend a comment was found. - return true; - } - }, - Node::Stmt(stmt) => { - if let Some(stmt_parent) = get_parent_node(cx.tcx, stmt.hir_id) { - match stmt_parent { - Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), - _ => { - // Doesn't support impls in this position. Pretend a comment was found. - return true; - }, - } - } else { - // Problem getting the parent node. Pretend a comment was found. - return true; - } - }, - _ => { + if item.span.ctxt() != SyntaxContext::root() { + return HasSafetyComment::No; + } + if let Some(parent_node) = get_parent_node(cx.tcx, item.hir_id()) { + let comment_start = match parent_node { + Node::Crate(parent_mod) => { + comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item) + }, + Node::Item(parent_item) => { + if let ItemKind::Mod(parent_mod) = &parent_item.kind { + comment_start_before_item_in_mod(cx, parent_mod, parent_item.span, item) + } else { // Doesn't support impls in this position. Pretend a comment was found. - return true; - }, - }; + return HasSafetyComment::Maybe; + } + }, + Node::Stmt(stmt) => { + if let Some(Node::Block(block)) = get_parent_node(cx.tcx, stmt.hir_id) { + walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo) + } else { + // Problem getting the parent node. Pretend a comment was found. + return HasSafetyComment::Maybe; + } + }, + _ => { + // Doesn't support impls in this position. Pretend a comment was found. + return HasSafetyComment::Maybe; + }, + }; - let source_map = cx.sess().source_map(); - if let Some(comment_start) = comment_start - && let Ok(unsafe_line) = source_map.lookup_line(item.span.lo()) - && let Ok(comment_start_line) = source_map.lookup_line(comment_start) - && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) - && let Some(src) = unsafe_line.sf.src.as_deref() - { - unsafe_line.sf.lines(|lines| { - comment_start_line.line < unsafe_line.line && text_has_safety_comment( + let source_map = cx.sess().source_map(); + if let Some(comment_start) = comment_start + && let Ok(unsafe_line) = source_map.lookup_line(item.span.lo()) + && let Ok(comment_start_line) = source_map.lookup_line(comment_start) + && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) + && let Some(src) = unsafe_line.sf.src.as_deref() + { + return unsafe_line.sf.lines(|lines| { + if comment_start_line.line >= unsafe_line.line { + HasSafetyComment::No + } else { + match text_has_safety_comment( src, &lines[comment_start_line.line + 1..=unsafe_line.line], unsafe_line.sf.start_pos.to_usize(), - ) - }) - } else { - // Problem getting source text. Pretend a comment was found. - true - } - } else { - // No parent node. Pretend a comment was found. - true + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, + } + } + }); } - } else { - false } + HasSafetyComment::Maybe } -fn comment_start_before_impl_in_mod( +/// Checks if the lines immediately preceding the item contain a safety comment. +#[allow(clippy::collapsible_match)] +fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> HasSafetyComment { + match span_from_macro_expansion_has_safety_comment(cx, span) { + HasSafetyComment::Maybe => (), + has_safety_comment => return has_safety_comment, + } + + if span.ctxt() != SyntaxContext::root() { + return HasSafetyComment::No; + } + + if let Some(parent_node) = get_parent_node(cx.tcx, hir_id) { + let comment_start = match parent_node { + Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo), + _ => return HasSafetyComment::Maybe, + }; + + let source_map = cx.sess().source_map(); + if let Some(comment_start) = comment_start + && let Ok(unsafe_line) = source_map.lookup_line(span.lo()) + && let Ok(comment_start_line) = source_map.lookup_line(comment_start) + && Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf) + && let Some(src) = unsafe_line.sf.src.as_deref() + { + return unsafe_line.sf.lines(|lines| { + if comment_start_line.line >= unsafe_line.line { + HasSafetyComment::No + } else { + match text_has_safety_comment( + src, + &lines[comment_start_line.line + 1..=unsafe_line.line], + unsafe_line.sf.start_pos.to_usize(), + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, + } + } + }); + } + } + HasSafetyComment::Maybe +} + +fn comment_start_before_item_in_mod( cx: &LateContext<'_>, parent_mod: &hir::Mod<'_>, parent_mod_span: Span, - imple: &hir::Item<'_>, + item: &hir::Item<'_>, ) -> Option { parent_mod.item_ids.iter().enumerate().find_map(|(idx, item_id)| { - if *item_id == imple.item_id() { + if *item_id == item.item_id() { if idx == 0 { // mod A { /* comment */ unsafe impl T {} ... } // ^------------------------------------------^ returns the start of this span @@ -270,11 +491,11 @@ fn comment_start_before_impl_in_mod( }) } -fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { +fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span) -> HasSafetyComment { let source_map = cx.sess().source_map(); let ctxt = span.ctxt(); if ctxt == SyntaxContext::root() { - false + HasSafetyComment::Maybe } else { // From a macro expansion. Get the text from the start of the macro declaration to start of the // unsafe block. @@ -286,15 +507,22 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span && let Some(src) = unsafe_line.sf.src.as_deref() { unsafe_line.sf.lines(|lines| { - macro_line.line < unsafe_line.line && text_has_safety_comment( - src, - &lines[macro_line.line + 1..=unsafe_line.line], - unsafe_line.sf.start_pos.to_usize(), - ) + if macro_line.line < unsafe_line.line { + match text_has_safety_comment( + src, + &lines[macro_line.line + 1..=unsafe_line.line], + unsafe_line.sf.start_pos.to_usize(), + ) { + Some(b) => HasSafetyComment::Yes(b), + None => HasSafetyComment::No, + } + } else { + HasSafetyComment::No + } }) } else { // Problem getting source text. Pretend a comment was found. - true + HasSafetyComment::Maybe } } } @@ -333,7 +561,7 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { src, &lines[body_line.line + 1..=unsafe_line.line], unsafe_line.sf.start_pos.to_usize(), - ) + ).is_some() }) } else { // Problem getting source text. Pretend a comment was found. @@ -345,30 +573,34 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool { } /// Checks if the given text has a safety comment for the immediately proceeding line. -fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> bool { +fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> Option { let mut lines = line_starts .array_windows::<2>() .rev() .map_while(|[start, end]| { let start = start.to_usize() - offset; let end = end.to_usize() - offset; - src.get(start..end).map(|text| (start, text.trim_start())) + let text = src.get(start..end)?; + let trimmed = text.trim_start(); + Some((start + (text.len() - trimmed.len()), trimmed)) }) .filter(|(_, text)| !text.is_empty()); let Some((line_start, line)) = lines.next() else { - return false; + return None; }; // Check for a sequence of line comments. if line.starts_with("//") { - let mut line = line; + let (mut line, mut line_start) = (line, line_start); loop { if line.to_ascii_uppercase().contains("SAFETY:") { - return true; + return Some(BytePos( + u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(), + )); } match lines.next() { - Some((_, x)) if x.starts_with("//") => line = x, - _ => return false, + Some((s, x)) if x.starts_with("//") => (line, line_start) = (x, s), + _ => return None, } } } @@ -377,16 +609,19 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> let (mut line_start, mut line) = (line_start, line); loop { if line.starts_with("/*") { - let src = src[line_start..line_starts.last().unwrap().to_usize() - offset].trim_start(); + let src = &src[line_start..line_starts.last().unwrap().to_usize() - offset]; let mut tokens = tokenize(src); - return src[..tokens.next().unwrap().len as usize] + return (src[..tokens.next().unwrap().len as usize] .to_ascii_uppercase() .contains("SAFETY:") - && tokens.all(|t| t.kind == TokenKind::Whitespace); + && tokens.all(|t| t.kind == TokenKind::Whitespace)) + .then_some(BytePos( + u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(), + )); } match lines.next() { Some(x) => (line_start, line) = x, - None => return false, + None => return None, } } } diff --git a/clippy_lints/src/unnested_or_patterns.rs b/clippy_lints/src/unnested_or_patterns.rs index bb6fb38e9690..7355260ae4af 100644 --- a/clippy_lints/src/unnested_or_patterns.rs +++ b/clippy_lints/src/unnested_or_patterns.rs @@ -2,14 +2,14 @@ use clippy_utils::ast_utils::{eq_field_pat, eq_id, eq_maybe_qself, eq_pat, eq_path}; use clippy_utils::diagnostics::span_lint_and_then; -use clippy_utils::{meets_msrv, msrvs, over}; +use clippy_utils::msrvs::{self, Msrv}; +use clippy_utils::over; use rustc_ast::mut_visit::*; use rustc_ast::ptr::P; use rustc_ast::{self as ast, Mutability, Pat, PatKind, PatKind::*, DUMMY_NODE_ID}; use rustc_ast_pretty::pprust; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::DUMMY_SP; @@ -45,14 +45,13 @@ declare_clippy_lint! { "unnested or-patterns, e.g., `Foo(Bar) | Foo(Baz) instead of `Foo(Bar | Baz)`" } -#[derive(Clone, Copy)] pub struct UnnestedOrPatterns { - msrv: Option, + msrv: Msrv, } impl UnnestedOrPatterns { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv } } } @@ -61,13 +60,13 @@ impl_lint_pass!(UnnestedOrPatterns => [UNNESTED_OR_PATTERNS]); impl EarlyLintPass for UnnestedOrPatterns { fn check_arm(&mut self, cx: &EarlyContext<'_>, a: &ast::Arm) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { lint_unnested_or_patterns(cx, &a.pat); } } fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { if let ast::ExprKind::Let(pat, _, _) = &e.kind { lint_unnested_or_patterns(cx, pat); } @@ -75,13 +74,13 @@ impl EarlyLintPass for UnnestedOrPatterns { } fn check_param(&mut self, cx: &EarlyContext<'_>, p: &ast::Param) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { lint_unnested_or_patterns(cx, &p.pat); } } fn check_local(&mut self, cx: &EarlyContext<'_>, l: &ast::Local) { - if meets_msrv(self.msrv, msrvs::OR_PATTERNS) { + if self.msrv.meets(msrvs::OR_PATTERNS) { lint_unnested_or_patterns(cx, &l.pat); } } diff --git a/clippy_lints/src/unused_rounding.rs b/clippy_lints/src/unused_rounding.rs index aac6719a8dc0..097568cd1f70 100644 --- a/clippy_lints/src/unused_rounding.rs +++ b/clippy_lints/src/unused_rounding.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet; use rustc_ast::ast::{Expr, ExprKind, MethodCall}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; @@ -29,22 +30,16 @@ declare_clippy_lint! { } declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]); -fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { +fn is_useless_rounding<'a>(cx: &EarlyContext<'_>, expr: &'a Expr) -> Option<(&'a str, String)> { if let ExprKind::MethodCall(box MethodCall { seg:name_ident, receiver, .. }) = &expr.kind && let method_name = name_ident.ident.name.as_str() && (method_name == "ceil" || method_name == "round" || method_name == "floor") && let ExprKind::Lit(token_lit) = &receiver.kind - && token_lit.is_semantic_float() { - let mut f_str = token_lit.symbol.to_string(); - let f = f_str.trim_end_matches('_').parse::().unwrap(); - if let Some(suffix) = token_lit.suffix { - f_str.push_str(suffix.as_str()); - } - if f.fract() == 0.0 { - Some((method_name, f_str)) - } else { - None - } + && token_lit.is_semantic_float() + && let Ok(f) = token_lit.symbol.as_str().replace('_', "").parse::() { + (f.fract() == 0.0).then(|| + (method_name, snippet(cx, receiver.span, "..").to_string()) + ) } else { None } @@ -52,7 +47,7 @@ fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> { impl EarlyLintPass for UnusedRounding { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { - if let Some((method_name, float)) = is_useless_rounding(expr) { + if let Some((method_name, float)) = is_useless_rounding(cx, expr) { span_lint_and_sugg( cx, UNUSED_ROUNDING, diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index e2860db71a5a..4c755d812a0e 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -1,6 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::is_from_proc_macro; +use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::same_type_and_consts; -use clippy_utils::{is_from_proc_macro, meets_msrv, msrvs}; use if_chain::if_chain; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; @@ -14,7 +15,6 @@ use rustc_hir::{ }; use rustc_hir_analysis::hir_ty_to_ty; use rustc_lint::{LateContext, LateLintPass}; -use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; @@ -57,13 +57,13 @@ declare_clippy_lint! { #[derive(Default)] pub struct UseSelf { - msrv: Option, + msrv: Msrv, stack: Vec, } impl UseSelf { #[must_use] - pub fn new(msrv: Option) -> Self { + pub fn new(msrv: Msrv) -> Self { Self { msrv, ..Self::default() @@ -199,7 +199,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) { if_chain! { if !hir_ty.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS); + if self.msrv.meets(msrvs::TYPE_ALIAS_ENUM_VARIANTS); if let Some(&StackItem::Check { impl_id, in_body, @@ -228,7 +228,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { if_chain! { if !expr.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS); + if self.msrv.meets(msrvs::TYPE_ALIAS_ENUM_VARIANTS); if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last(); if cx.typeck_results().expr_ty(expr) == cx.tcx.type_of(impl_id); then {} else { return; } @@ -248,7 +248,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) { if_chain! { if !pat.span.from_expansion(); - if meets_msrv(self.msrv, msrvs::TYPE_ALIAS_ENUM_VARIANTS); + if self.msrv.meets(msrvs::TYPE_ALIAS_ENUM_VARIANTS); if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last(); // get the path from the pattern if let PatKind::Path(QPath::Resolved(_, path)) diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index b37d4239477e..b6dc8cd7ab11 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -402,6 +402,10 @@ define_Conf! { /// A list of paths to types that should be treated like `Arc`, i.e. ignored but /// for the generic parameters for determining interior mutability (ignore_interior_mutability: Vec = Vec::from(["bytes::Bytes".into()])), + /// Lint: UNINLINED_FORMAT_ARGS. + /// + /// Whether to allow mixed uninlined format args, e.g. `format!("{} {}", a, foo.bar)` + (allow_mixed_uninlined_format_args: bool = true), } /// Search for the configuration file. diff --git a/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs b/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs index 1e994e3f2b17..9876a8a765cc 100644 --- a/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs +++ b/clippy_lints/src/utils/internal_lints/msrv_attr_impl.rs @@ -41,7 +41,7 @@ impl LateLintPass<'_> for MsrvAttrImpl { .type_of(f.did) .walk() .filter(|t| matches!(t.unpack(), GenericArgKind::Type(_))) - .any(|t| match_type(cx, t.expect_ty(), &paths::RUSTC_VERSION)) + .any(|t| match_type(cx, t.expect_ty(), &paths::MSRV)) }); if !items.iter().any(|item| item.ident.name == sym!(enter_lint_attrs)); then { diff --git a/clippy_utils/src/attrs.rs b/clippy_utils/src/attrs.rs index cd8575c90e86..7987a233bdc1 100644 --- a/clippy_utils/src/attrs.rs +++ b/clippy_utils/src/attrs.rs @@ -125,19 +125,19 @@ fn parse_attrs(sess: &Session, attrs: &[ast::Attribute], name: &' } } -pub fn get_unique_inner_attr(sess: &Session, attrs: &[ast::Attribute], name: &'static str) -> Option { - let mut unique_attr = None; +pub fn get_unique_attr<'a>( + sess: &'a Session, + attrs: &'a [ast::Attribute], + name: &'static str, +) -> Option<&'a ast::Attribute> { + let mut unique_attr: Option<&ast::Attribute> = None; for attr in get_attr(sess, attrs, name) { - match attr.style { - ast::AttrStyle::Inner if unique_attr.is_none() => unique_attr = Some(attr.clone()), - ast::AttrStyle::Inner => { - sess.struct_span_err(attr.span, &format!("`{name}` is defined multiple times")) - .span_note(unique_attr.as_ref().unwrap().span, "first definition found here") - .emit(); - }, - ast::AttrStyle::Outer => { - sess.span_err(attr.span, format!("`{name}` cannot be an outer attribute")); - }, + if let Some(duplicate) = unique_attr { + sess.struct_span_err(attr.span, &format!("`{name}` is defined multiple times")) + .span_note(duplicate.span, "first definition found here") + .emit(); + } else { + unique_attr = Some(attr); } } unique_attr diff --git a/clippy_utils/src/eager_or_lazy.rs b/clippy_utils/src/eager_or_lazy.rs index f74f7dadfa90..96711936968b 100644 --- a/clippy_utils/src/eager_or_lazy.rs +++ b/clippy_utils/src/eager_or_lazy.rs @@ -91,6 +91,16 @@ fn fn_eagerness(cx: &LateContext<'_>, fn_id: DefId, name: Symbol, have_one_arg: } } +fn res_has_significant_drop(res: Res, cx: &LateContext<'_>, e: &Expr<'_>) -> bool { + if let Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_) = res { + cx.typeck_results() + .expr_ty(e) + .has_significant_drop(cx.tcx, cx.param_env) + } else { + false + } +} + #[expect(clippy::too_many_lines)] fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessSuggestion { struct V<'cx, 'tcx> { @@ -113,13 +123,8 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS }, args, ) => match self.cx.qpath_res(path, hir_id) { - Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_) => { - if self - .cx - .typeck_results() - .expr_ty(e) - .has_significant_drop(self.cx.tcx, self.cx.param_env) - { + res @ (Res::Def(DefKind::Ctor(..) | DefKind::Variant, _) | Res::SelfCtor(_)) => { + if res_has_significant_drop(res, self.cx, e) { self.eagerness = ForceNoChange; return; } @@ -147,6 +152,12 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS self.eagerness |= NoChange; return; }, + ExprKind::Path(ref path) => { + if res_has_significant_drop(self.cx.qpath_res(path, e.hir_id), self.cx, e) { + self.eagerness = ForceNoChange; + return; + } + }, ExprKind::MethodCall(name, ..) => { self.eagerness |= self .cx @@ -206,7 +217,6 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS | ExprKind::Match(..) | ExprKind::Closure { .. } | ExprKind::Field(..) - | ExprKind::Path(_) | ExprKind::AddrOf(..) | ExprKind::Struct(..) | ExprKind::Repeat(..) diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 9e2682925a22..90192f46cbfa 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -105,8 +105,6 @@ use rustc_middle::ty::{ layout::IntegerExt, BorrowKind, ClosureKind, DefIdTree, Ty, TyCtxt, TypeAndMut, TypeVisitable, UpvarCapture, }; use rustc_middle::ty::{FloatTy, IntTy, UintTy}; -use rustc_semver::RustcVersion; -use rustc_session::Session; use rustc_span::hygiene::{ExpnKind, MacroKind}; use rustc_span::source_map::SourceMap; use rustc_span::sym; @@ -118,36 +116,17 @@ use crate::consts::{constant, Constant}; use crate::ty::{can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type, ty_is_fn_once_param}; use crate::visitors::for_each_expr; -pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option) -> Option { - if let Ok(version) = RustcVersion::parse(msrv) { - return Some(version); - } else if let Some(sess) = sess { - if let Some(span) = span { - sess.span_err(span, format!("`{msrv}` is not a valid Rust version")); - } - } - None -} - -pub fn meets_msrv(msrv: Option, lint_msrv: RustcVersion) -> bool { - msrv.map_or(true, |msrv| msrv.meets(lint_msrv)) -} - #[macro_export] macro_rules! extract_msrv_attr { ($context:ident) => { fn enter_lint_attrs(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) { let sess = rustc_lint::LintContext::sess(cx); - match $crate::get_unique_inner_attr(sess, attrs, "msrv") { - Some(msrv_attr) => { - if let Some(msrv) = msrv_attr.value_str() { - self.msrv = $crate::parse_msrv(&msrv.to_string(), Some(sess), Some(msrv_attr.span)); - } else { - sess.span_err(msrv_attr.span, "bad clippy attribute"); - } - }, - _ => (), - } + self.msrv.enter_lint_attrs(sess, attrs); + } + + fn exit_lint_attrs(&mut self, cx: &rustc_lint::$context<'_>, attrs: &[rustc_ast::ast::Attribute]) { + let sess = rustc_lint::LintContext::sess(cx); + self.msrv.exit_lint_attrs(sess, attrs); } }; } diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index 79b19e6fb3eb..12a512f78a69 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -1,4 +1,11 @@ +use std::sync::OnceLock; + +use rustc_ast::Attribute; use rustc_semver::RustcVersion; +use rustc_session::Session; +use rustc_span::Span; + +use crate::attrs::get_unique_attr; macro_rules! msrv_aliases { ($($major:literal,$minor:literal,$patch:literal { @@ -40,3 +47,97 @@ msrv_aliases! { 1,16,0 { STR_REPEAT } 1,55,0 { SEEK_REWIND } } + +fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option) -> Option { + if let Ok(version) = RustcVersion::parse(msrv) { + return Some(version); + } else if let Some(sess) = sess { + if let Some(span) = span { + sess.span_err(span, format!("`{msrv}` is not a valid Rust version")); + } + } + None +} + +/// Tracks the current MSRV from `clippy.toml`, `Cargo.toml` or set via `#[clippy::msrv]` +#[derive(Debug, Clone, Default)] +pub struct Msrv { + stack: Vec, +} + +impl Msrv { + fn new(initial: Option) -> Self { + Self { + stack: Vec::from_iter(initial), + } + } + + fn read_inner(conf_msrv: &Option, sess: &Session) -> Self { + let cargo_msrv = std::env::var("CARGO_PKG_RUST_VERSION") + .ok() + .and_then(|v| parse_msrv(&v, None, None)); + let clippy_msrv = conf_msrv.as_ref().and_then(|s| { + parse_msrv(s, None, None).or_else(|| { + sess.err(format!( + "error reading Clippy's configuration file. `{s}` is not a valid Rust version" + )); + None + }) + }); + + // if both files have an msrv, let's compare them and emit a warning if they differ + if let Some(cargo_msrv) = cargo_msrv + && let Some(clippy_msrv) = clippy_msrv + && clippy_msrv != cargo_msrv + { + sess.warn(format!( + "the MSRV in `clippy.toml` and `Cargo.toml` differ; using `{clippy_msrv}` from `clippy.toml`" + )); + } + + Self::new(clippy_msrv.or(cargo_msrv)) + } + + /// Set the initial MSRV from the Clippy config file or from Cargo due to the `rust-version` + /// field in `Cargo.toml` + /// + /// Returns a `&'static Msrv` as `Copy` types are more easily passed to the + /// `register_{late,early}_pass` callbacks + pub fn read(conf_msrv: &Option, sess: &Session) -> &'static Self { + static PARSED: OnceLock = OnceLock::new(); + + PARSED.get_or_init(|| Self::read_inner(conf_msrv, sess)) + } + + pub fn current(&self) -> Option { + self.stack.last().copied() + } + + pub fn meets(&self, required: RustcVersion) -> bool { + self.current().map_or(true, |version| version.meets(required)) + } + + fn parse_attr(sess: &Session, attrs: &[Attribute]) -> Option { + if let Some(msrv_attr) = get_unique_attr(sess, attrs, "msrv") { + if let Some(msrv) = msrv_attr.value_str() { + return parse_msrv(&msrv.to_string(), Some(sess), Some(msrv_attr.span)); + } + + sess.span_err(msrv_attr.span, "bad clippy attribute"); + } + + None + } + + pub fn enter_lint_attrs(&mut self, sess: &Session, attrs: &[Attribute]) { + if let Some(version) = Self::parse_attr(sess, attrs) { + self.stack.push(version); + } + } + + pub fn exit_lint_attrs(&mut self, sess: &Session, attrs: &[Attribute]) { + if Self::parse_attr(sess, attrs).is_some() { + self.stack.pop(); + } + } +} diff --git a/clippy_utils/src/paths.rs b/clippy_utils/src/paths.rs index 6c09c146082a..6417f0f3c713 100644 --- a/clippy_utils/src/paths.rs +++ b/clippy_utils/src/paths.rs @@ -60,6 +60,8 @@ pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"]; #[cfg(feature = "internal")] pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"]; pub const MEM_SWAP: [&str; 3] = ["core", "mem", "swap"]; +#[cfg(feature = "internal")] +pub const MSRV: [&str; 3] = ["clippy_utils", "msrvs", "Msrv"]; pub const OPEN_OPTIONS: [&str; 3] = ["std", "fs", "OpenOptions"]; pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"]; pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"]; @@ -72,7 +74,6 @@ pub const PEEKABLE: [&str; 5] = ["core", "iter", "adapters", "peekable", "Peekab pub const PERMISSIONS: [&str; 3] = ["std", "fs", "Permissions"]; #[cfg_attr(not(unix), allow(clippy::invalid_paths))] pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "PermissionsExt", "from_mode"]; -pub const POLL: [&str; 4] = ["core", "task", "poll", "Poll"]; pub const PTR_COPY: [&str; 3] = ["core", "intrinsics", "copy"]; pub const PTR_COPY_NONOVERLAPPING: [&str; 3] = ["core", "intrinsics", "copy_nonoverlapping"]; pub const PTR_EQ: [&str; 3] = ["core", "ptr", "eq"]; @@ -101,8 +102,6 @@ pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "re_bytes", "Regex", "new"]; pub const REGEX_BYTES_SET_NEW: [&str; 5] = ["regex", "re_set", "bytes", "RegexSet", "new"]; pub const REGEX_NEW: [&str; 4] = ["regex", "re_unicode", "Regex", "new"]; pub const REGEX_SET_NEW: [&str; 5] = ["regex", "re_set", "unicode", "RegexSet", "new"]; -#[cfg(feature = "internal")] -pub const RUSTC_VERSION: [&str; 2] = ["rustc_semver", "RustcVersion"]; pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; pub const SLICE_FROM_RAW_PARTS: [&str; 4] = ["core", "slice", "raw", "from_raw_parts"]; diff --git a/clippy_utils/src/qualify_min_const_fn.rs b/clippy_utils/src/qualify_min_const_fn.rs index b8c2dd5ab9ea..480e8e55cf39 100644 --- a/clippy_utils/src/qualify_min_const_fn.rs +++ b/clippy_utils/src/qualify_min_const_fn.rs @@ -3,6 +3,7 @@ // of terminologies might not be relevant in the context of Clippy. Note that its behavior might // differ from the time of `rustc` even if the name stays the same. +use crate::msrvs::Msrv; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_middle::mir::{ @@ -18,20 +19,22 @@ use std::borrow::Cow; type McfResult = Result<(), (Span, Cow<'static, str>)>; -pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: Option) -> McfResult { +pub fn is_min_const_fn<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, msrv: &Msrv) -> McfResult { let def_id = body.source.def_id(); let mut current = def_id; loop { let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { match predicate.kind().skip_binder() { - ty::PredicateKind::Clause(ty::Clause::RegionOutlives(_)) - | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(_)) + ty::PredicateKind::Clause( + ty::Clause::RegionOutlives(_) + | ty::Clause::TypeOutlives(_) + | ty::Clause::Projection(_) + | ty::Clause::Trait(..), + ) | ty::PredicateKind::WellFormed(_) - | ty::PredicateKind::Clause(ty::Clause::Projection(_)) | ty::PredicateKind::ConstEvaluatable(..) | ty::PredicateKind::ConstEquate(..) - | ty::PredicateKind::Clause(ty::Clause::Trait(..)) | ty::PredicateKind::TypeWellFormedFromEnv(..) => continue, ty::PredicateKind::ObjectSafe(_) => panic!("object safe predicate on function: {predicate:#?}"), ty::PredicateKind::ClosureKind(..) => panic!("closure kind predicate on function: {predicate:#?}"), @@ -281,7 +284,7 @@ fn check_terminator<'tcx>( tcx: TyCtxt<'tcx>, body: &Body<'tcx>, terminator: &Terminator<'tcx>, - msrv: Option, + msrv: &Msrv, ) -> McfResult { let span = terminator.source_info.span; match &terminator.kind { @@ -365,7 +368,7 @@ fn check_terminator<'tcx>( } } -fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option) -> bool { +fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool { tcx.is_const_fn(def_id) && tcx.lookup_const_stability(def_id).map_or(true, |const_stab| { if let rustc_attr::StabilityLevel::Stable { since, .. } = const_stab.level { @@ -384,15 +387,12 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option) -> bo let since = rustc_span::Symbol::intern(short_version); - crate::meets_msrv( - msrv, - RustcVersion::parse(since.as_str()).unwrap_or_else(|err| { - panic!("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted: `{since}`, {err:?}") - }), - ) + msrv.meets(RustcVersion::parse(since.as_str()).unwrap_or_else(|err| { + panic!("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted: `{since}`, {err:?}") + })) } else { // Unstable const fn with the feature enabled. - msrv.is_none() + msrv.current().is_none() } }) } diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs index eacfa91ba556..cd5dcfdaca34 100644 --- a/clippy_utils/src/source.rs +++ b/clippy_utils/src/source.rs @@ -5,6 +5,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LintContext}; +use rustc_session::Session; use rustc_span::hygiene; use rustc_span::source_map::{original_sp, SourceMap}; use rustc_span::{BytePos, Pos, Span, SpanData, SyntaxContext, DUMMY_SP}; @@ -204,11 +205,20 @@ pub fn snippet_with_applicability<'a, T: LintContext>( span: Span, default: &'a str, applicability: &mut Applicability, +) -> Cow<'a, str> { + snippet_with_applicability_sess(cx.sess(), span, default, applicability) +} + +fn snippet_with_applicability_sess<'a>( + sess: &Session, + span: Span, + default: &'a str, + applicability: &mut Applicability, ) -> Cow<'a, str> { if *applicability != Applicability::Unspecified && span.from_expansion() { *applicability = Applicability::MaybeIncorrect; } - snippet_opt(cx, span).map_or_else( + snippet_opt_sess(sess, span).map_or_else( || { if *applicability == Applicability::MachineApplicable { *applicability = Applicability::HasPlaceholders; @@ -226,8 +236,12 @@ pub fn snippet_with_macro_callsite<'a, T: LintContext>(cx: &T, span: Span, defau } /// Converts a span to a code snippet. Returns `None` if not available. -pub fn snippet_opt(cx: &T, span: Span) -> Option { - cx.sess().source_map().span_to_snippet(span).ok() +pub fn snippet_opt(cx: &impl LintContext, span: Span) -> Option { + snippet_opt_sess(cx.sess(), span) +} + +fn snippet_opt_sess(sess: &Session, span: Span) -> Option { + sess.source_map().span_to_snippet(span).ok() } /// Converts a span (from a block) to a code snippet if available, otherwise use default. @@ -277,8 +291,8 @@ pub fn snippet_block<'a, T: LintContext>( /// Same as `snippet_block`, but adapts the applicability level by the rules of /// `snippet_with_applicability`. -pub fn snippet_block_with_applicability<'a, T: LintContext>( - cx: &T, +pub fn snippet_block_with_applicability<'a>( + cx: &impl LintContext, span: Span, default: &'a str, indent_relative_to: Option, @@ -299,7 +313,17 @@ pub fn snippet_block_with_applicability<'a, T: LintContext>( /// /// This will also return whether or not the snippet is a macro call. pub fn snippet_with_context<'a>( - cx: &LateContext<'_>, + cx: &impl LintContext, + span: Span, + outer: SyntaxContext, + default: &'a str, + applicability: &mut Applicability, +) -> (Cow<'a, str>, bool) { + snippet_with_context_sess(cx.sess(), span, outer, default, applicability) +} + +fn snippet_with_context_sess<'a>( + sess: &Session, span: Span, outer: SyntaxContext, default: &'a str, @@ -318,7 +342,7 @@ pub fn snippet_with_context<'a>( ); ( - snippet_with_applicability(cx, span, default, applicability), + snippet_with_applicability_sess(sess, span, default, applicability), is_macro_call, ) } diff --git a/clippy_utils/src/sugg.rs b/clippy_utils/src/sugg.rs index 3cacdb493772..b66604f33db1 100644 --- a/clippy_utils/src/sugg.rs +++ b/clippy_utils/src/sugg.rs @@ -176,25 +176,28 @@ impl<'a> Sugg<'a> { } /// Prepare a suggestion from an expression. - pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self { + pub fn ast( + cx: &EarlyContext<'_>, + expr: &ast::Expr, + default: &'a str, + ctxt: SyntaxContext, + app: &mut Applicability, + ) -> Self { use rustc_ast::ast::RangeLimits; - let snippet_without_expansion = |cx, span: Span, default| { - if span.from_expansion() { - snippet_with_macro_callsite(cx, span, default) - } else { - snippet(cx, span, default) - } - }; - + #[expect(clippy::match_wildcard_for_single_variants)] match expr.kind { + _ if expr.span.ctxt() != ctxt => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0), ast::ExprKind::AddrOf(..) | ast::ExprKind::Box(..) | ast::ExprKind::Closure { .. } | ast::ExprKind::If(..) | ast::ExprKind::Let(..) | ast::ExprKind::Unary(..) - | ast::ExprKind::Match(..) => Sugg::MaybeParen(snippet_without_expansion(cx, expr.span, default)), + | ast::ExprKind::Match(..) => match snippet_with_context(cx, expr.span, ctxt, default, app) { + (snip, false) => Sugg::MaybeParen(snip), + (snip, true) => Sugg::NonParen(snip), + }, ast::ExprKind::Async(..) | ast::ExprKind::Block(..) | ast::ExprKind::Break(..) @@ -224,45 +227,49 @@ impl<'a> Sugg<'a> { | ast::ExprKind::Array(..) | ast::ExprKind::While(..) | ast::ExprKind::Await(..) - | ast::ExprKind::Err => Sugg::NonParen(snippet_without_expansion(cx, expr.span, default)), + | ast::ExprKind::Err => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0), ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp( AssocOp::DotDot, - lhs.as_ref() - .map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)), - rhs.as_ref() - .map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)), + lhs.as_ref().map_or("".into(), |lhs| { + snippet_with_context(cx, lhs.span, ctxt, default, app).0 + }), + rhs.as_ref().map_or("".into(), |rhs| { + snippet_with_context(cx, rhs.span, ctxt, default, app).0 + }), ), ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp( AssocOp::DotDotEq, - lhs.as_ref() - .map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)), - rhs.as_ref() - .map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)), + lhs.as_ref().map_or("".into(), |lhs| { + snippet_with_context(cx, lhs.span, ctxt, default, app).0 + }), + rhs.as_ref().map_or("".into(), |rhs| { + snippet_with_context(cx, rhs.span, ctxt, default, app).0 + }), ), ast::ExprKind::Assign(ref lhs, ref rhs, _) => Sugg::BinOp( AssocOp::Assign, - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, rhs.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, rhs.span, ctxt, default, app).0, ), ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => Sugg::BinOp( astbinop2assignop(op), - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, rhs.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, rhs.span, ctxt, default, app).0, ), ast::ExprKind::Binary(op, ref lhs, ref rhs) => Sugg::BinOp( AssocOp::from_ast_binop(op.node), - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, rhs.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, rhs.span, ctxt, default, app).0, ), ast::ExprKind::Cast(ref lhs, ref ty) => Sugg::BinOp( AssocOp::As, - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, ty.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, ty.span, ctxt, default, app).0, ), ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp( AssocOp::Colon, - snippet_without_expansion(cx, lhs.span, default), - snippet_without_expansion(cx, ty.span, default), + snippet_with_context(cx, lhs.span, ctxt, default, app).0, + snippet_with_context(cx, ty.span, ctxt, default, app).0, ), } } diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 2ceda3511fe4..bfb2d472a393 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -9,7 +9,10 @@ use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::{Expr, FnDecl, LangItem, TyKind, Unsafety}; -use rustc_infer::infer::{TyCtxtInferExt, type_variable::{TypeVariableOrigin, TypeVariableOriginKind}}; +use rustc_infer::infer::{ + type_variable::{TypeVariableOrigin, TypeVariableOriginKind}, + TyCtxtInferExt, +}; use rustc_lint::LateContext; use rustc_middle::mir::interpret::{ConstValue, Scalar}; use rustc_middle::ty::{ @@ -189,7 +192,13 @@ pub fn implements_trait<'tcx>( trait_id: DefId, ty_params: &[GenericArg<'tcx>], ) -> bool { - implements_trait_with_env(cx.tcx, cx.param_env, ty, trait_id, ty_params.iter().map(|&arg| Some(arg))) + implements_trait_with_env( + cx.tcx, + cx.param_env, + ty, + trait_id, + ty_params.iter().map(|&arg| Some(arg)), + ) } /// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context. @@ -212,7 +221,11 @@ pub fn implements_trait_with_env<'tcx>( kind: TypeVariableOriginKind::MiscVariable, span: DUMMY_SP, }; - let ty_params = tcx.mk_substs(ty_params.into_iter().map(|arg| arg.unwrap_or_else(|| infcx.next_ty_var(orig).into()))); + let ty_params = tcx.mk_substs( + ty_params + .into_iter() + .map(|arg| arg.unwrap_or_else(|| infcx.next_ty_var(orig).into())), + ); infcx .type_implements_trait(trait_id, [ty.into()].into_iter().chain(ty_params), param_env) .must_apply_modulo_regions() @@ -712,7 +725,9 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> O } inputs = Some(i); }, - PredicateKind::Clause(ty::Clause::Projection(p)) if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output() => { + PredicateKind::Clause(ty::Clause::Projection(p)) + if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output() => + { if output.is_some() { // Multiple different fn trait impls. Is this even allowed? return None; @@ -992,14 +1007,12 @@ pub fn make_projection<'tcx>( debug_assert!( generic_count == substs.len(), - "wrong number of substs for `{:?}`: found `{}` expected `{}`.\n\ + "wrong number of substs for `{:?}`: found `{}` expected `{generic_count}`.\n\ note: the expected parameters are: {:#?}\n\ - the given arguments are: `{:#?}`", + the given arguments are: `{substs:#?}`", assoc_item.def_id, substs.len(), - generic_count, params.map(ty::GenericParamDefKind::descr).collect::>(), - substs, ); if let Some((idx, (param, arg))) = params @@ -1017,14 +1030,11 @@ pub fn make_projection<'tcx>( { debug_assert!( false, - "mismatched subst type at index {}: expected a {}, found `{:?}`\n\ + "mismatched subst type at index {idx}: expected a {}, found `{arg:?}`\n\ note: the expected parameters are {:#?}\n\ - the given arguments are {:#?}", - idx, + the given arguments are {substs:#?}", param.descr(), - arg, - params.map(ty::GenericParamDefKind::descr).collect::>(), - substs, + params.map(ty::GenericParamDefKind::descr).collect::>() ); } } diff --git a/clippy_utils/src/visitors.rs b/clippy_utils/src/visitors.rs index d4294f18fd50..863fb60fcfca 100644 --- a/clippy_utils/src/visitors.rs +++ b/clippy_utils/src/visitors.rs @@ -170,22 +170,22 @@ where cb: F, } - struct WithStmtGuarg<'a, F> { + struct WithStmtGuard<'a, F> { val: &'a mut RetFinder, prev_in_stmt: bool, } impl RetFinder { - fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> { + fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuard<'_, F> { let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt); - WithStmtGuarg { + WithStmtGuard { val: self, prev_in_stmt, } } } - impl std::ops::Deref for WithStmtGuarg<'_, F> { + impl std::ops::Deref for WithStmtGuard<'_, F> { type Target = RetFinder; fn deref(&self) -> &Self::Target { @@ -193,13 +193,13 @@ where } } - impl std::ops::DerefMut for WithStmtGuarg<'_, F> { + impl std::ops::DerefMut for WithStmtGuard<'_, F> { fn deref_mut(&mut self) -> &mut Self::Target { self.val } } - impl Drop for WithStmtGuarg<'_, F> { + impl Drop for WithStmtGuard<'_, F> { fn drop(&mut self) { self.val.in_stmt = self.prev_in_stmt; } diff --git a/lintcheck/src/main.rs b/lintcheck/src/main.rs index ee8ab7c1d7cb..bd49f0960726 100644 --- a/lintcheck/src/main.rs +++ b/lintcheck/src/main.rs @@ -120,8 +120,8 @@ impl ClippyWarning { format!("$CARGO_HOME/{}", stripped.display()) } else { format!( - "target/lintcheck/sources/{}-{}/{}", - crate_name, crate_version, span.file_name + "target/lintcheck/sources/{crate_name}-{crate_version}/{}", + span.file_name ) }; @@ -322,13 +322,13 @@ impl Crate { if config.max_jobs == 1 { println!( - "{}/{} {}% Linting {} {}", - index, total_crates_to_lint, perc, &self.name, &self.version + "{index}/{total_crates_to_lint} {perc}% Linting {} {}", + &self.name, &self.version ); } else { println!( - "{}/{} {}% Linting {} {} in target dir {:?}", - index, total_crates_to_lint, perc, &self.name, &self.version, thread_index + "{index}/{total_crates_to_lint} {perc}% Linting {} {} in target dir {thread_index:?}", + &self.name, &self.version ); } @@ -398,8 +398,7 @@ impl Crate { .output() .unwrap_or_else(|error| { panic!( - "Encountered error:\n{:?}\ncargo_clippy_path: {}\ncrate path:{}\n", - error, + "Encountered error:\n{error:?}\ncargo_clippy_path: {}\ncrate path:{}\n", &cargo_clippy_path.display(), &self.path.display() ); diff --git a/rust-toolchain b/rust-toolchain index a806c1564796..19fee38db46e 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-11-21" +channel = "nightly-2022-12-01" components = ["cargo", "llvm-tools", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"] diff --git a/src/driver.rs b/src/driver.rs index ad6132a49baa..9ec4df8e651b 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -1,6 +1,7 @@ #![feature(rustc_private)] #![feature(let_chains)] #![feature(once_cell)] +#![feature(lint_reasons)] #![cfg_attr(feature = "deny-warnings", deny(warnings))] // warn on lints, that are included in `rust-lang/rust`s bootstrap #![warn(rust_2018_idioms, unused_lifetimes)] @@ -90,6 +91,10 @@ fn track_files(parse_sess: &mut ParseSess, conf_path_string: Option) { // During development track the `clippy-driver` executable so that cargo will re-run clippy whenever // it is rebuilt + #[expect( + clippy::collapsible_if, + reason = "Due to a bug in let_chains this if statement can't be collapsed" + )] if cfg!(debug_assertions) { if let Ok(current_exe) = env::current_exe() && let Some(current_exe) = current_exe.to_str() diff --git a/tests/ui-internal/invalid_msrv_attr_impl.fixed b/tests/ui-internal/invalid_msrv_attr_impl.fixed index 900a8fffd408..08634063a575 100644 --- a/tests/ui-internal/invalid_msrv_attr_impl.fixed +++ b/tests/ui-internal/invalid_msrv_attr_impl.fixed @@ -11,9 +11,9 @@ extern crate rustc_middle; #[macro_use] extern crate rustc_session; use clippy_utils::extract_msrv_attr; +use clippy_utils::msrvs::Msrv; use rustc_hir::Expr; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass}; -use rustc_semver::RustcVersion; declare_lint! { pub TEST_LINT, @@ -22,7 +22,7 @@ declare_lint! { } struct Pass { - msrv: Option, + msrv: Msrv, } impl_lint_pass!(Pass => [TEST_LINT]); diff --git a/tests/ui-internal/invalid_msrv_attr_impl.rs b/tests/ui-internal/invalid_msrv_attr_impl.rs index 4bc8164db67b..f8af77e6d395 100644 --- a/tests/ui-internal/invalid_msrv_attr_impl.rs +++ b/tests/ui-internal/invalid_msrv_attr_impl.rs @@ -11,9 +11,9 @@ extern crate rustc_middle; #[macro_use] extern crate rustc_session; use clippy_utils::extract_msrv_attr; +use clippy_utils::msrvs::Msrv; use rustc_hir::Expr; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass}; -use rustc_semver::RustcVersion; declare_lint! { pub TEST_LINT, @@ -22,7 +22,7 @@ declare_lint! { } struct Pass { - msrv: Option, + msrv: Msrv, } impl_lint_pass!(Pass => [TEST_LINT]); diff --git a/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr b/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr index 8bfc060e9910..2a240cc249b0 100644 --- a/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr +++ b/tests/ui-internal/unnecessary_def_path_hardcoded_path.stderr @@ -1,12 +1,3 @@ -error: hardcoded path to a language item - --> $DIR/unnecessary_def_path_hardcoded_path.rs:11:40 - | -LL | const DEREF_MUT_TRAIT: [&str; 4] = ["core", "ops", "deref", "DerefMut"]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: convert all references to use `LangItem::DerefMut` - = note: `-D clippy::unnecessary-def-path` implied by `-D warnings` - error: hardcoded path to a diagnostic item --> $DIR/unnecessary_def_path_hardcoded_path.rs:10:36 | @@ -14,6 +5,7 @@ LL | const DEREF_TRAIT: [&str; 4] = ["core", "ops", "deref", "Deref"]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: convert all references to use `sym::Deref` + = note: `-D clippy::unnecessary-def-path` implied by `-D warnings` error: hardcoded path to a diagnostic item --> $DIR/unnecessary_def_path_hardcoded_path.rs:12:43 @@ -23,5 +15,13 @@ LL | const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", | = help: convert all references to use `sym::deref_method` +error: hardcoded path to a language item + --> $DIR/unnecessary_def_path_hardcoded_path.rs:11:40 + | +LL | const DEREF_MUT_TRAIT: [&str; 4] = ["core", "ops", "deref", "DerefMut"]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: convert all references to use `LangItem::DerefMut` + error: aborting due to 3 previous errors diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml b/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml new file mode 100644 index 000000000000..b95e806aae24 --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml @@ -0,0 +1 @@ +allow-mixed-uninlined-format-args = false diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed new file mode 100644 index 000000000000..aa8b45b5fe7d --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed @@ -0,0 +1,14 @@ +// run-rustfix +#![warn(clippy::uninlined_format_args)] + +fn main() { + let local_i32 = 1; + let local_f64 = 2.0; + let local_opt: Option = Some(3); + + println!("val='{local_i32}'"); + println!("Hello x is {local_f64:.local_i32$}"); + println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("{local_i32}, {}", local_opt.unwrap()); +} diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs new file mode 100644 index 000000000000..ad2e4863ee8e --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs @@ -0,0 +1,14 @@ +// run-rustfix +#![warn(clippy::uninlined_format_args)] + +fn main() { + let local_i32 = 1; + let local_f64 = 2.0; + let local_opt: Option = Some(3); + + println!("val='{}'", local_i32); + println!("Hello {} is {:.*}", "x", local_i32, local_f64); + println!("Hello {} is {:.*}", local_i32, 5, local_f64); + println!("Hello {} is {2:.*}", local_i32, 5, local_f64); + println!("{}, {}", local_i32, local_opt.unwrap()); +} diff --git a/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr new file mode 100644 index 000000000000..ee9417621961 --- /dev/null +++ b/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr @@ -0,0 +1,76 @@ +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:9:5 + | +LL | println!("val='{}'", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::uninlined-format-args` implied by `-D warnings` +help: change this to + | +LL - println!("val='{}'", local_i32); +LL + println!("val='{local_i32}'"); + | + +error: literal with an empty format string + --> $DIR/uninlined_format_args.rs:10:35 + | +LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); + | ^^^ + | + = note: `-D clippy::print-literal` implied by `-D warnings` +help: try this + | +LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); +LL + println!("Hello x is {:.*}", local_i32, local_f64); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:10:5 + | +LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); +LL + println!("Hello {} is {local_f64:.local_i32$}", "x"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:11:5 + | +LL | println!("Hello {} is {:.*}", local_i32, 5, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {:.*}", local_i32, 5, local_f64); +LL + println!("Hello {local_i32} is {local_f64:.*}", 5); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:12:5 + | +LL | println!("Hello {} is {2:.*}", local_i32, 5, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {2:.*}", local_i32, 5, local_f64); +LL + println!("Hello {local_i32} is {local_f64:.*}", 5); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:13:5 + | +LL | println!("{}, {}", local_i32, local_opt.unwrap()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}, {}", local_i32, local_opt.unwrap()); +LL + println!("{local_i32}, {}", local_opt.unwrap()); + | + +error: aborting due to 6 previous errors + diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index f91d285c2e0e..01a5e962c949 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -1,6 +1,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of allow-dbg-in-tests allow-expect-in-tests + allow-mixed-uninlined-format-args allow-print-in-tests allow-unwrap-in-tests allowed-scripts diff --git a/tests/ui/almost_complete_letter_range.fixed b/tests/ui/almost_complete_letter_range.fixed index 079b7c000dce..adcbd4d5134d 100644 --- a/tests/ui/almost_complete_letter_range.fixed +++ b/tests/ui/almost_complete_letter_range.fixed @@ -2,7 +2,6 @@ // edition:2018 // aux-build:macro_rules.rs -#![feature(custom_inner_attributes)] #![feature(exclusive_range_pattern)] #![feature(stmt_expr_attributes)] #![warn(clippy::almost_complete_letter_range)] @@ -62,16 +61,16 @@ fn main() { b!(); } +#[clippy::msrv = "1.25"] fn _under_msrv() { - #![clippy::msrv = "1.25"] let _ = match 'a' { 'a'...'z' => 1, _ => 2, }; } +#[clippy::msrv = "1.26"] fn _meets_msrv() { - #![clippy::msrv = "1.26"] let _ = 'a'..='z'; let _ = match 'a' { 'a'..='z' => 1, diff --git a/tests/ui/almost_complete_letter_range.rs b/tests/ui/almost_complete_letter_range.rs index a66900a976ef..9979316eca42 100644 --- a/tests/ui/almost_complete_letter_range.rs +++ b/tests/ui/almost_complete_letter_range.rs @@ -2,7 +2,6 @@ // edition:2018 // aux-build:macro_rules.rs -#![feature(custom_inner_attributes)] #![feature(exclusive_range_pattern)] #![feature(stmt_expr_attributes)] #![warn(clippy::almost_complete_letter_range)] @@ -62,16 +61,16 @@ fn main() { b!(); } +#[clippy::msrv = "1.25"] fn _under_msrv() { - #![clippy::msrv = "1.25"] let _ = match 'a' { 'a'..'z' => 1, _ => 2, }; } +#[clippy::msrv = "1.26"] fn _meets_msrv() { - #![clippy::msrv = "1.26"] let _ = 'a'..'z'; let _ = match 'a' { 'a'..'z' => 1, diff --git a/tests/ui/almost_complete_letter_range.stderr b/tests/ui/almost_complete_letter_range.stderr index 3de44c72c1b9..9abf6d6c5e7d 100644 --- a/tests/ui/almost_complete_letter_range.stderr +++ b/tests/ui/almost_complete_letter_range.stderr @@ -1,5 +1,5 @@ error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:30:17 + --> $DIR/almost_complete_letter_range.rs:29:17 | LL | let _ = ('a') ..'z'; | ^^^^^^--^^^ @@ -9,7 +9,7 @@ LL | let _ = ('a') ..'z'; = note: `-D clippy::almost-complete-letter-range` implied by `-D warnings` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:31:17 + --> $DIR/almost_complete_letter_range.rs:30:17 | LL | let _ = 'A' .. ('Z'); | ^^^^--^^^^^^ @@ -17,7 +17,7 @@ LL | let _ = 'A' .. ('Z'); | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:37:13 + --> $DIR/almost_complete_letter_range.rs:36:13 | LL | let _ = (b'a')..(b'z'); | ^^^^^^--^^^^^^ @@ -25,7 +25,7 @@ LL | let _ = (b'a')..(b'z'); | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:38:13 + --> $DIR/almost_complete_letter_range.rs:37:13 | LL | let _ = b'A'..b'Z'; | ^^^^--^^^^ @@ -33,7 +33,7 @@ LL | let _ = b'A'..b'Z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:43:13 + --> $DIR/almost_complete_letter_range.rs:42:13 | LL | let _ = a!()..'z'; | ^^^^--^^^ @@ -41,7 +41,7 @@ LL | let _ = a!()..'z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:46:9 + --> $DIR/almost_complete_letter_range.rs:45:9 | LL | b'a'..b'z' if true => 1, | ^^^^--^^^^ @@ -49,7 +49,7 @@ LL | b'a'..b'z' if true => 1, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:47:9 + --> $DIR/almost_complete_letter_range.rs:46:9 | LL | b'A'..b'Z' if true => 2, | ^^^^--^^^^ @@ -57,7 +57,7 @@ LL | b'A'..b'Z' if true => 2, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:54:9 + --> $DIR/almost_complete_letter_range.rs:53:9 | LL | 'a'..'z' if true => 1, | ^^^--^^^ @@ -65,7 +65,7 @@ LL | 'a'..'z' if true => 1, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:55:9 + --> $DIR/almost_complete_letter_range.rs:54:9 | LL | 'A'..'Z' if true => 2, | ^^^--^^^ @@ -73,7 +73,7 @@ LL | 'A'..'Z' if true => 2, | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:23:17 + --> $DIR/almost_complete_letter_range.rs:22:17 | LL | let _ = 'a'..'z'; | ^^^--^^^ @@ -86,7 +86,7 @@ LL | b!(); = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info) error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:68:9 + --> $DIR/almost_complete_letter_range.rs:67:9 | LL | 'a'..'z' => 1, | ^^^--^^^ @@ -94,7 +94,7 @@ LL | 'a'..'z' => 1, | help: use an inclusive range: `...` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:75:13 + --> $DIR/almost_complete_letter_range.rs:74:13 | LL | let _ = 'a'..'z'; | ^^^--^^^ @@ -102,7 +102,7 @@ LL | let _ = 'a'..'z'; | help: use an inclusive range: `..=` error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:77:9 + --> $DIR/almost_complete_letter_range.rs:76:9 | LL | 'a'..'z' => 1, | ^^^--^^^ diff --git a/tests/ui/cast_abs_to_unsigned.fixed b/tests/ui/cast_abs_to_unsigned.fixed index e6bf944c7a5e..8676b562b4f9 100644 --- a/tests/ui/cast_abs_to_unsigned.fixed +++ b/tests/ui/cast_abs_to_unsigned.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::cast_abs_to_unsigned)] #![allow(clippy::uninlined_format_args, unused)] @@ -33,16 +32,14 @@ fn main() { let _ = (x as i64 - y as i64).unsigned_abs() as u32; } +#[clippy::msrv = "1.50"] fn msrv_1_50() { - #![clippy::msrv = "1.50"] - let x: i32 = 10; assert_eq!(10u32, x.abs() as u32); } +#[clippy::msrv = "1.51"] fn msrv_1_51() { - #![clippy::msrv = "1.51"] - let x: i32 = 10; assert_eq!(10u32, x.unsigned_abs()); } diff --git a/tests/ui/cast_abs_to_unsigned.rs b/tests/ui/cast_abs_to_unsigned.rs index c87320b5209d..5775af874f8f 100644 --- a/tests/ui/cast_abs_to_unsigned.rs +++ b/tests/ui/cast_abs_to_unsigned.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::cast_abs_to_unsigned)] #![allow(clippy::uninlined_format_args, unused)] @@ -33,16 +32,14 @@ fn main() { let _ = (x as i64 - y as i64).abs() as u32; } +#[clippy::msrv = "1.50"] fn msrv_1_50() { - #![clippy::msrv = "1.50"] - let x: i32 = 10; assert_eq!(10u32, x.abs() as u32); } +#[clippy::msrv = "1.51"] fn msrv_1_51() { - #![clippy::msrv = "1.51"] - let x: i32 = 10; assert_eq!(10u32, x.abs() as u32); } diff --git a/tests/ui/cast_abs_to_unsigned.stderr b/tests/ui/cast_abs_to_unsigned.stderr index 1b39c554b038..4668554f4511 100644 --- a/tests/ui/cast_abs_to_unsigned.stderr +++ b/tests/ui/cast_abs_to_unsigned.stderr @@ -1,5 +1,5 @@ error: casting the result of `i32::abs()` to u32 - --> $DIR/cast_abs_to_unsigned.rs:9:18 + --> $DIR/cast_abs_to_unsigned.rs:8:18 | LL | let y: u32 = x.abs() as u32; | ^^^^^^^^^^^^^^ help: replace with: `x.unsigned_abs()` @@ -7,103 +7,103 @@ LL | let y: u32 = x.abs() as u32; = note: `-D clippy::cast-abs-to-unsigned` implied by `-D warnings` error: casting the result of `i32::abs()` to usize - --> $DIR/cast_abs_to_unsigned.rs:13:20 + --> $DIR/cast_abs_to_unsigned.rs:12:20 | LL | let _: usize = a.abs() as usize; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i32::abs()` to usize - --> $DIR/cast_abs_to_unsigned.rs:14:20 + --> $DIR/cast_abs_to_unsigned.rs:13:20 | LL | let _: usize = a.abs() as _; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i32::abs()` to usize - --> $DIR/cast_abs_to_unsigned.rs:15:13 + --> $DIR/cast_abs_to_unsigned.rs:14:13 | LL | let _ = a.abs() as usize; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i64::abs()` to usize - --> $DIR/cast_abs_to_unsigned.rs:18:13 + --> $DIR/cast_abs_to_unsigned.rs:17:13 | LL | let _ = a.abs() as usize; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i64::abs()` to u8 - --> $DIR/cast_abs_to_unsigned.rs:19:13 + --> $DIR/cast_abs_to_unsigned.rs:18:13 | LL | let _ = a.abs() as u8; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i64::abs()` to u16 - --> $DIR/cast_abs_to_unsigned.rs:20:13 + --> $DIR/cast_abs_to_unsigned.rs:19:13 | LL | let _ = a.abs() as u16; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i64::abs()` to u32 - --> $DIR/cast_abs_to_unsigned.rs:21:13 + --> $DIR/cast_abs_to_unsigned.rs:20:13 | LL | let _ = a.abs() as u32; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i64::abs()` to u64 - --> $DIR/cast_abs_to_unsigned.rs:22:13 + --> $DIR/cast_abs_to_unsigned.rs:21:13 | LL | let _ = a.abs() as u64; | ^^^^^^^^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i64::abs()` to u128 - --> $DIR/cast_abs_to_unsigned.rs:23:13 + --> $DIR/cast_abs_to_unsigned.rs:22:13 | LL | let _ = a.abs() as u128; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `isize::abs()` to usize - --> $DIR/cast_abs_to_unsigned.rs:26:13 + --> $DIR/cast_abs_to_unsigned.rs:25:13 | LL | let _ = a.abs() as usize; | ^^^^^^^^^^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `isize::abs()` to u8 - --> $DIR/cast_abs_to_unsigned.rs:27:13 + --> $DIR/cast_abs_to_unsigned.rs:26:13 | LL | let _ = a.abs() as u8; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `isize::abs()` to u16 - --> $DIR/cast_abs_to_unsigned.rs:28:13 + --> $DIR/cast_abs_to_unsigned.rs:27:13 | LL | let _ = a.abs() as u16; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `isize::abs()` to u32 - --> $DIR/cast_abs_to_unsigned.rs:29:13 + --> $DIR/cast_abs_to_unsigned.rs:28:13 | LL | let _ = a.abs() as u32; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `isize::abs()` to u64 - --> $DIR/cast_abs_to_unsigned.rs:30:13 + --> $DIR/cast_abs_to_unsigned.rs:29:13 | LL | let _ = a.abs() as u64; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `isize::abs()` to u128 - --> $DIR/cast_abs_to_unsigned.rs:31:13 + --> $DIR/cast_abs_to_unsigned.rs:30:13 | LL | let _ = a.abs() as u128; | ^^^^^^^ help: replace with: `a.unsigned_abs()` error: casting the result of `i64::abs()` to u32 - --> $DIR/cast_abs_to_unsigned.rs:33:13 + --> $DIR/cast_abs_to_unsigned.rs:32:13 | LL | let _ = (x as i64 - y as i64).abs() as u32; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `(x as i64 - y as i64).unsigned_abs()` error: casting the result of `i32::abs()` to u32 - --> $DIR/cast_abs_to_unsigned.rs:47:23 + --> $DIR/cast_abs_to_unsigned.rs:44:23 | LL | assert_eq!(10u32, x.abs() as u32); | ^^^^^^^^^^^^^^ help: replace with: `x.unsigned_abs()` diff --git a/tests/ui/cast_lossless_bool.fixed b/tests/ui/cast_lossless_bool.fixed index af13b755e310..13b3cf838c9f 100644 --- a/tests/ui/cast_lossless_bool.fixed +++ b/tests/ui/cast_lossless_bool.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(dead_code)] #![warn(clippy::cast_lossless)] @@ -42,14 +41,12 @@ mod cast_lossless_in_impl { } } +#[clippy::msrv = "1.27"] fn msrv_1_27() { - #![clippy::msrv = "1.27"] - let _ = true as u8; } +#[clippy::msrv = "1.28"] fn msrv_1_28() { - #![clippy::msrv = "1.28"] - let _ = u8::from(true); } diff --git a/tests/ui/cast_lossless_bool.rs b/tests/ui/cast_lossless_bool.rs index 3b06af899c60..3eed2135562c 100644 --- a/tests/ui/cast_lossless_bool.rs +++ b/tests/ui/cast_lossless_bool.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(dead_code)] #![warn(clippy::cast_lossless)] @@ -42,14 +41,12 @@ mod cast_lossless_in_impl { } } +#[clippy::msrv = "1.27"] fn msrv_1_27() { - #![clippy::msrv = "1.27"] - let _ = true as u8; } +#[clippy::msrv = "1.28"] fn msrv_1_28() { - #![clippy::msrv = "1.28"] - let _ = true as u8; } diff --git a/tests/ui/cast_lossless_bool.stderr b/tests/ui/cast_lossless_bool.stderr index 768b033d10a2..ce240b70f891 100644 --- a/tests/ui/cast_lossless_bool.stderr +++ b/tests/ui/cast_lossless_bool.stderr @@ -1,5 +1,5 @@ error: casting `bool` to `u8` is more cleanly stated with `u8::from(_)` - --> $DIR/cast_lossless_bool.rs:9:13 + --> $DIR/cast_lossless_bool.rs:8:13 | LL | let _ = true as u8; | ^^^^^^^^^^ help: try: `u8::from(true)` @@ -7,79 +7,79 @@ LL | let _ = true as u8; = note: `-D clippy::cast-lossless` implied by `-D warnings` error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)` - --> $DIR/cast_lossless_bool.rs:10:13 + --> $DIR/cast_lossless_bool.rs:9:13 | LL | let _ = true as u16; | ^^^^^^^^^^^ help: try: `u16::from(true)` error: casting `bool` to `u32` is more cleanly stated with `u32::from(_)` - --> $DIR/cast_lossless_bool.rs:11:13 + --> $DIR/cast_lossless_bool.rs:10:13 | LL | let _ = true as u32; | ^^^^^^^^^^^ help: try: `u32::from(true)` error: casting `bool` to `u64` is more cleanly stated with `u64::from(_)` - --> $DIR/cast_lossless_bool.rs:12:13 + --> $DIR/cast_lossless_bool.rs:11:13 | LL | let _ = true as u64; | ^^^^^^^^^^^ help: try: `u64::from(true)` error: casting `bool` to `u128` is more cleanly stated with `u128::from(_)` - --> $DIR/cast_lossless_bool.rs:13:13 + --> $DIR/cast_lossless_bool.rs:12:13 | LL | let _ = true as u128; | ^^^^^^^^^^^^ help: try: `u128::from(true)` error: casting `bool` to `usize` is more cleanly stated with `usize::from(_)` - --> $DIR/cast_lossless_bool.rs:14:13 + --> $DIR/cast_lossless_bool.rs:13:13 | LL | let _ = true as usize; | ^^^^^^^^^^^^^ help: try: `usize::from(true)` error: casting `bool` to `i8` is more cleanly stated with `i8::from(_)` - --> $DIR/cast_lossless_bool.rs:16:13 + --> $DIR/cast_lossless_bool.rs:15:13 | LL | let _ = true as i8; | ^^^^^^^^^^ help: try: `i8::from(true)` error: casting `bool` to `i16` is more cleanly stated with `i16::from(_)` - --> $DIR/cast_lossless_bool.rs:17:13 + --> $DIR/cast_lossless_bool.rs:16:13 | LL | let _ = true as i16; | ^^^^^^^^^^^ help: try: `i16::from(true)` error: casting `bool` to `i32` is more cleanly stated with `i32::from(_)` - --> $DIR/cast_lossless_bool.rs:18:13 + --> $DIR/cast_lossless_bool.rs:17:13 | LL | let _ = true as i32; | ^^^^^^^^^^^ help: try: `i32::from(true)` error: casting `bool` to `i64` is more cleanly stated with `i64::from(_)` - --> $DIR/cast_lossless_bool.rs:19:13 + --> $DIR/cast_lossless_bool.rs:18:13 | LL | let _ = true as i64; | ^^^^^^^^^^^ help: try: `i64::from(true)` error: casting `bool` to `i128` is more cleanly stated with `i128::from(_)` - --> $DIR/cast_lossless_bool.rs:20:13 + --> $DIR/cast_lossless_bool.rs:19:13 | LL | let _ = true as i128; | ^^^^^^^^^^^^ help: try: `i128::from(true)` error: casting `bool` to `isize` is more cleanly stated with `isize::from(_)` - --> $DIR/cast_lossless_bool.rs:21:13 + --> $DIR/cast_lossless_bool.rs:20:13 | LL | let _ = true as isize; | ^^^^^^^^^^^^^ help: try: `isize::from(true)` error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)` - --> $DIR/cast_lossless_bool.rs:24:13 + --> $DIR/cast_lossless_bool.rs:23:13 | LL | let _ = (true | false) as u16; | ^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::from(true | false)` error: casting `bool` to `u8` is more cleanly stated with `u8::from(_)` - --> $DIR/cast_lossless_bool.rs:54:13 + --> $DIR/cast_lossless_bool.rs:51:13 | LL | let _ = true as u8; | ^^^^^^^^^^ help: try: `u8::from(true)` diff --git a/tests/ui/cfg_attr_rustfmt.fixed b/tests/ui/cfg_attr_rustfmt.fixed index 8a5645b22ed1..b970b1209b65 100644 --- a/tests/ui/cfg_attr_rustfmt.fixed +++ b/tests/ui/cfg_attr_rustfmt.fixed @@ -1,5 +1,5 @@ // run-rustfix -#![feature(stmt_expr_attributes, custom_inner_attributes)] +#![feature(stmt_expr_attributes)] #![allow(unused, clippy::no_effect, clippy::unnecessary_operation)] #![warn(clippy::deprecated_cfg_attr)] @@ -30,16 +30,14 @@ mod foo { pub fn f() {} } +#[clippy::msrv = "1.29"] fn msrv_1_29() { - #![clippy::msrv = "1.29"] - #[cfg_attr(rustfmt, rustfmt::skip)] 1+29; } +#[clippy::msrv = "1.30"] fn msrv_1_30() { - #![clippy::msrv = "1.30"] - #[rustfmt::skip] 1+30; } diff --git a/tests/ui/cfg_attr_rustfmt.rs b/tests/ui/cfg_attr_rustfmt.rs index 2fb140efae76..0a8e6a89d8a0 100644 --- a/tests/ui/cfg_attr_rustfmt.rs +++ b/tests/ui/cfg_attr_rustfmt.rs @@ -1,5 +1,5 @@ // run-rustfix -#![feature(stmt_expr_attributes, custom_inner_attributes)] +#![feature(stmt_expr_attributes)] #![allow(unused, clippy::no_effect, clippy::unnecessary_operation)] #![warn(clippy::deprecated_cfg_attr)] @@ -30,16 +30,14 @@ mod foo { pub fn f() {} } +#[clippy::msrv = "1.29"] fn msrv_1_29() { - #![clippy::msrv = "1.29"] - #[cfg_attr(rustfmt, rustfmt::skip)] 1+29; } +#[clippy::msrv = "1.30"] fn msrv_1_30() { - #![clippy::msrv = "1.30"] - #[cfg_attr(rustfmt, rustfmt::skip)] 1+30; } diff --git a/tests/ui/cfg_attr_rustfmt.stderr b/tests/ui/cfg_attr_rustfmt.stderr index 08df7b2b39a0..524a2bf72484 100644 --- a/tests/ui/cfg_attr_rustfmt.stderr +++ b/tests/ui/cfg_attr_rustfmt.stderr @@ -13,7 +13,7 @@ LL | #[cfg_attr(rustfmt, rustfmt_skip)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]` error: `cfg_attr` is deprecated for rustfmt and got replaced by tool attributes - --> $DIR/cfg_attr_rustfmt.rs:43:5 + --> $DIR/cfg_attr_rustfmt.rs:41:5 | LL | #[cfg_attr(rustfmt, rustfmt::skip)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]` diff --git a/tests/ui/checked_conversions.fixed b/tests/ui/checked_conversions.fixed index f936957cb40c..e279ba3147bb 100644 --- a/tests/ui/checked_conversions.fixed +++ b/tests/ui/checked_conversions.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow( clippy::cast_lossless, unused, @@ -78,16 +77,14 @@ pub const fn issue_8898(i: u32) -> bool { i <= i32::MAX as u32 } +#[clippy::msrv = "1.33"] fn msrv_1_33() { - #![clippy::msrv = "1.33"] - let value: i64 = 33; let _ = value <= (u32::MAX as i64) && value >= 0; } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let value: i64 = 34; let _ = u32::try_from(value).is_ok(); } diff --git a/tests/ui/checked_conversions.rs b/tests/ui/checked_conversions.rs index 77aec713ff31..9d7a40995c37 100644 --- a/tests/ui/checked_conversions.rs +++ b/tests/ui/checked_conversions.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow( clippy::cast_lossless, unused, @@ -78,16 +77,14 @@ pub const fn issue_8898(i: u32) -> bool { i <= i32::MAX as u32 } +#[clippy::msrv = "1.33"] fn msrv_1_33() { - #![clippy::msrv = "1.33"] - let value: i64 = 33; let _ = value <= (u32::MAX as i64) && value >= 0; } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let value: i64 = 34; let _ = value <= (u32::MAX as i64) && value >= 0; } diff --git a/tests/ui/checked_conversions.stderr b/tests/ui/checked_conversions.stderr index b2bf7af8daf8..273ead73bda5 100644 --- a/tests/ui/checked_conversions.stderr +++ b/tests/ui/checked_conversions.stderr @@ -1,5 +1,5 @@ error: checked cast can be simplified - --> $DIR/checked_conversions.rs:17:13 + --> $DIR/checked_conversions.rs:16:13 | LL | let _ = value <= (u32::max_value() as i64) && value >= 0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()` @@ -7,97 +7,97 @@ LL | let _ = value <= (u32::max_value() as i64) && value >= 0; = note: `-D clippy::checked-conversions` implied by `-D warnings` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:18:13 + --> $DIR/checked_conversions.rs:17:13 | LL | let _ = value <= (u32::MAX as i64) && value >= 0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:22:13 + --> $DIR/checked_conversions.rs:21:13 | LL | let _ = value <= i64::from(u16::max_value()) && value >= 0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:23:13 + --> $DIR/checked_conversions.rs:22:13 | LL | let _ = value <= i64::from(u16::MAX) && value >= 0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:27:13 + --> $DIR/checked_conversions.rs:26:13 | LL | let _ = value <= (u8::max_value() as isize) && value >= 0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:28:13 + --> $DIR/checked_conversions.rs:27:13 | LL | let _ = value <= (u8::MAX as isize) && value >= 0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:34:13 + --> $DIR/checked_conversions.rs:33:13 | LL | let _ = value <= (i32::max_value() as i64) && value >= (i32::min_value() as i64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:35:13 + --> $DIR/checked_conversions.rs:34:13 | LL | let _ = value <= (i32::MAX as i64) && value >= (i32::MIN as i64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:39:13 + --> $DIR/checked_conversions.rs:38:13 | LL | let _ = value <= i64::from(i16::max_value()) && value >= i64::from(i16::min_value()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:40:13 + --> $DIR/checked_conversions.rs:39:13 | LL | let _ = value <= i64::from(i16::MAX) && value >= i64::from(i16::MIN); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:46:13 + --> $DIR/checked_conversions.rs:45:13 | LL | let _ = value <= i32::max_value() as u32; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:47:13 + --> $DIR/checked_conversions.rs:46:13 | LL | let _ = value <= i32::MAX as u32; | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:51:13 + --> $DIR/checked_conversions.rs:50:13 | LL | let _ = value <= isize::max_value() as usize && value as i32 == 5; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:52:13 + --> $DIR/checked_conversions.rs:51:13 | LL | let _ = value <= isize::MAX as usize && value as i32 == 5; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:56:13 + --> $DIR/checked_conversions.rs:55:13 | LL | let _ = value <= u16::max_value() as u32 && value as i32 == 5; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:57:13 + --> $DIR/checked_conversions.rs:56:13 | LL | let _ = value <= u16::MAX as u32 && value as i32 == 5; | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()` error: checked cast can be simplified - --> $DIR/checked_conversions.rs:92:13 + --> $DIR/checked_conversions.rs:89:13 | LL | let _ = value <= (u32::MAX as i64) && value >= 0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()` diff --git a/tests/ui/cloned_instead_of_copied.fixed b/tests/ui/cloned_instead_of_copied.fixed index 42ed232d1001..ecbfc1feedf6 100644 --- a/tests/ui/cloned_instead_of_copied.fixed +++ b/tests/ui/cloned_instead_of_copied.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::cloned_instead_of_copied)] #![allow(unused)] @@ -17,23 +16,20 @@ fn main() { let _ = Some(&String::new()).cloned(); } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let _ = [1].iter().cloned(); let _ = Some(&1).cloned(); } +#[clippy::msrv = "1.35"] fn msrv_1_35() { - #![clippy::msrv = "1.35"] - let _ = [1].iter().cloned(); let _ = Some(&1).copied(); // Option::copied needs 1.35 } +#[clippy::msrv = "1.36"] fn msrv_1_36() { - #![clippy::msrv = "1.36"] - let _ = [1].iter().copied(); // Iterator::copied needs 1.36 let _ = Some(&1).copied(); } diff --git a/tests/ui/cloned_instead_of_copied.rs b/tests/ui/cloned_instead_of_copied.rs index 471bd9654cc1..163dc3dddce6 100644 --- a/tests/ui/cloned_instead_of_copied.rs +++ b/tests/ui/cloned_instead_of_copied.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::cloned_instead_of_copied)] #![allow(unused)] @@ -17,23 +16,20 @@ fn main() { let _ = Some(&String::new()).cloned(); } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let _ = [1].iter().cloned(); let _ = Some(&1).cloned(); } +#[clippy::msrv = "1.35"] fn msrv_1_35() { - #![clippy::msrv = "1.35"] - let _ = [1].iter().cloned(); let _ = Some(&1).cloned(); // Option::copied needs 1.35 } +#[clippy::msrv = "1.36"] fn msrv_1_36() { - #![clippy::msrv = "1.36"] - let _ = [1].iter().cloned(); // Iterator::copied needs 1.36 let _ = Some(&1).cloned(); } diff --git a/tests/ui/cloned_instead_of_copied.stderr b/tests/ui/cloned_instead_of_copied.stderr index 914c9a91e830..e0361acd9560 100644 --- a/tests/ui/cloned_instead_of_copied.stderr +++ b/tests/ui/cloned_instead_of_copied.stderr @@ -1,5 +1,5 @@ error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:9:24 + --> $DIR/cloned_instead_of_copied.rs:8:24 | LL | let _ = [1].iter().cloned(); | ^^^^^^ help: try: `copied` @@ -7,43 +7,43 @@ LL | let _ = [1].iter().cloned(); = note: `-D clippy::cloned-instead-of-copied` implied by `-D warnings` error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:10:31 + --> $DIR/cloned_instead_of_copied.rs:9:31 | LL | let _ = vec!["hi"].iter().cloned(); | ^^^^^^ help: try: `copied` error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:11:22 + --> $DIR/cloned_instead_of_copied.rs:10:22 | LL | let _ = Some(&1).cloned(); | ^^^^^^ help: try: `copied` error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:12:34 + --> $DIR/cloned_instead_of_copied.rs:11:34 | LL | let _ = Box::new([1].iter()).cloned(); | ^^^^^^ help: try: `copied` error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:13:32 + --> $DIR/cloned_instead_of_copied.rs:12:32 | LL | let _ = Box::new(Some(&1)).cloned(); | ^^^^^^ help: try: `copied` error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:31:22 + --> $DIR/cloned_instead_of_copied.rs:28:22 | LL | let _ = Some(&1).cloned(); // Option::copied needs 1.35 | ^^^^^^ help: try: `copied` error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:37:24 + --> $DIR/cloned_instead_of_copied.rs:33:24 | LL | let _ = [1].iter().cloned(); // Iterator::copied needs 1.36 | ^^^^^^ help: try: `copied` error: used `cloned` where `copied` could be used instead - --> $DIR/cloned_instead_of_copied.rs:38:22 + --> $DIR/cloned_instead_of_copied.rs:34:22 | LL | let _ = Some(&1).cloned(); | ^^^^^^ help: try: `copied` diff --git a/tests/ui/err_expect.fixed b/tests/ui/err_expect.fixed index 3bac738acd65..b63cbd8a8e6b 100644 --- a/tests/ui/err_expect.fixed +++ b/tests/ui/err_expect.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] struct MyTypeNonDebug; @@ -16,16 +15,14 @@ fn main() { test_non_debug.err().expect("Testing non debug type"); } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - let x: Result = Ok(16); x.err().expect("16"); } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - let x: Result = Ok(17); x.expect_err("17"); } diff --git a/tests/ui/err_expect.rs b/tests/ui/err_expect.rs index 6e7c47d9ad3c..c081a745fb40 100644 --- a/tests/ui/err_expect.rs +++ b/tests/ui/err_expect.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] struct MyTypeNonDebug; @@ -16,16 +15,14 @@ fn main() { test_non_debug.err().expect("Testing non debug type"); } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - let x: Result = Ok(16); x.err().expect("16"); } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - let x: Result = Ok(17); x.err().expect("17"); } diff --git a/tests/ui/err_expect.stderr b/tests/ui/err_expect.stderr index 91a6cf8de65f..82c0754cfb00 100644 --- a/tests/ui/err_expect.stderr +++ b/tests/ui/err_expect.stderr @@ -1,5 +1,5 @@ error: called `.err().expect()` on a `Result` value - --> $DIR/err_expect.rs:13:16 + --> $DIR/err_expect.rs:12:16 | LL | test_debug.err().expect("Testing debug type"); | ^^^^^^^^^^^^ help: try: `expect_err` @@ -7,7 +7,7 @@ LL | test_debug.err().expect("Testing debug type"); = note: `-D clippy::err-expect` implied by `-D warnings` error: called `.err().expect()` on a `Result` value - --> $DIR/err_expect.rs:30:7 + --> $DIR/err_expect.rs:27:7 | LL | x.err().expect("17"); | ^^^^^^^^^^^^ help: try: `expect_err` diff --git a/tests/ui/eta.fixed b/tests/ui/eta.fixed index a9cc80aaaf62..dc129591eac4 100644 --- a/tests/ui/eta.fixed +++ b/tests/ui/eta.fixed @@ -316,3 +316,25 @@ pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) - move || takes_fn_mut(&mut f_used_once) } + +impl dyn TestTrait + '_ { + fn method_on_dyn(&self) -> bool { + false + } +} + +// https://github.com/rust-lang/rust-clippy/issues/7746 +fn angle_brackets_and_substs() { + let array_opt: Option<&[u8; 3]> = Some(&[4, 8, 7]); + array_opt.map(<[u8; 3]>::as_slice); + + let slice_opt: Option<&[u8]> = Some(b"slice"); + slice_opt.map(<[u8]>::len); + + let ptr_opt: Option<*const usize> = Some(&487); + ptr_opt.map(<*const usize>::is_null); + + let test_struct = TestStruct { some_ref: &487 }; + let dyn_opt: Option<&dyn TestTrait> = Some(&test_struct); + dyn_opt.map(::method_on_dyn); +} diff --git a/tests/ui/eta.rs b/tests/ui/eta.rs index cc99906ccd66..025fd6a0b7af 100644 --- a/tests/ui/eta.rs +++ b/tests/ui/eta.rs @@ -316,3 +316,25 @@ pub fn mutable_impl_fn_mut(mut f: impl FnMut(), mut f_used_once: impl FnMut()) - move || takes_fn_mut(|| f_used_once()) } + +impl dyn TestTrait + '_ { + fn method_on_dyn(&self) -> bool { + false + } +} + +// https://github.com/rust-lang/rust-clippy/issues/7746 +fn angle_brackets_and_substs() { + let array_opt: Option<&[u8; 3]> = Some(&[4, 8, 7]); + array_opt.map(|a| a.as_slice()); + + let slice_opt: Option<&[u8]> = Some(b"slice"); + slice_opt.map(|s| s.len()); + + let ptr_opt: Option<*const usize> = Some(&487); + ptr_opt.map(|p| p.is_null()); + + let test_struct = TestStruct { some_ref: &487 }; + let dyn_opt: Option<&dyn TestTrait> = Some(&test_struct); + dyn_opt.map(|d| d.method_on_dyn()); +} diff --git a/tests/ui/eta.stderr b/tests/ui/eta.stderr index 434706b7e258..a521fb868607 100644 --- a/tests/ui/eta.stderr +++ b/tests/ui/eta.stderr @@ -134,5 +134,29 @@ error: redundant closure LL | move || takes_fn_mut(|| f_used_once()) | ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut f_used_once` -error: aborting due to 22 previous errors +error: redundant closure + --> $DIR/eta.rs:329:19 + | +LL | array_opt.map(|a| a.as_slice()); + | ^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8; 3]>::as_slice` + +error: redundant closure + --> $DIR/eta.rs:332:19 + | +LL | slice_opt.map(|s| s.len()); + | ^^^^^^^^^^^ help: replace the closure with the method itself: `<[u8]>::len` + +error: redundant closure + --> $DIR/eta.rs:335:17 + | +LL | ptr_opt.map(|p| p.is_null()); + | ^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `<*const usize>::is_null` + +error: redundant closure + --> $DIR/eta.rs:339:17 + | +LL | dyn_opt.map(|d| d.method_on_dyn()); + | ^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `::method_on_dyn` + +error: aborting due to 26 previous errors diff --git a/tests/ui/explicit_auto_deref.fixed b/tests/ui/explicit_auto_deref.fixed index 59ff5e4040a3..475fae5e823b 100644 --- a/tests/ui/explicit_auto_deref.fixed +++ b/tests/ui/explicit_auto_deref.fixed @@ -277,4 +277,8 @@ fn main() { unimplemented!() } let _: String = takes_assoc(&*String::new()); + + // Issue #9901 + fn takes_ref(_: &i32) {} + takes_ref(*Box::new(&0i32)); } diff --git a/tests/ui/explicit_auto_deref.rs b/tests/ui/explicit_auto_deref.rs index bcfb60c32788..c1894258f4d8 100644 --- a/tests/ui/explicit_auto_deref.rs +++ b/tests/ui/explicit_auto_deref.rs @@ -277,4 +277,8 @@ fn main() { unimplemented!() } let _: String = takes_assoc(&*String::new()); + + // Issue #9901 + fn takes_ref(_: &i32) {} + takes_ref(*Box::new(&0i32)); } diff --git a/tests/ui/filter_map_next_fixable.fixed b/tests/ui/filter_map_next_fixable.fixed index 41828ddd7acd..462d46169fcb 100644 --- a/tests/ui/filter_map_next_fixable.fixed +++ b/tests/ui/filter_map_next_fixable.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::all, clippy::pedantic)] #![allow(unused)] @@ -11,16 +10,14 @@ fn main() { assert_eq!(element, Some(1)); } +#[clippy::msrv = "1.29"] fn msrv_1_29() { - #![clippy::msrv = "1.29"] - let a = ["1", "lol", "3", "NaN", "5"]; let _: Option = a.iter().filter_map(|s| s.parse().ok()).next(); } +#[clippy::msrv = "1.30"] fn msrv_1_30() { - #![clippy::msrv = "1.30"] - let a = ["1", "lol", "3", "NaN", "5"]; let _: Option = a.iter().find_map(|s| s.parse().ok()); } diff --git a/tests/ui/filter_map_next_fixable.rs b/tests/ui/filter_map_next_fixable.rs index be492a81b45e..2ea00cf73072 100644 --- a/tests/ui/filter_map_next_fixable.rs +++ b/tests/ui/filter_map_next_fixable.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::all, clippy::pedantic)] #![allow(unused)] @@ -11,16 +10,14 @@ fn main() { assert_eq!(element, Some(1)); } +#[clippy::msrv = "1.29"] fn msrv_1_29() { - #![clippy::msrv = "1.29"] - let a = ["1", "lol", "3", "NaN", "5"]; let _: Option = a.iter().filter_map(|s| s.parse().ok()).next(); } +#[clippy::msrv = "1.30"] fn msrv_1_30() { - #![clippy::msrv = "1.30"] - let a = ["1", "lol", "3", "NaN", "5"]; let _: Option = a.iter().filter_map(|s| s.parse().ok()).next(); } diff --git a/tests/ui/filter_map_next_fixable.stderr b/tests/ui/filter_map_next_fixable.stderr index e789efeabd55..a9fc6abe88fa 100644 --- a/tests/ui/filter_map_next_fixable.stderr +++ b/tests/ui/filter_map_next_fixable.stderr @@ -1,5 +1,5 @@ error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead - --> $DIR/filter_map_next_fixable.rs:10:32 + --> $DIR/filter_map_next_fixable.rs:9:32 | LL | let element: Option = a.iter().filter_map(|s| s.parse().ok()).next(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `a.iter().find_map(|s| s.parse().ok())` @@ -7,7 +7,7 @@ LL | let element: Option = a.iter().filter_map(|s| s.parse().ok()).next = note: `-D clippy::filter-map-next` implied by `-D warnings` error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead - --> $DIR/filter_map_next_fixable.rs:25:26 + --> $DIR/filter_map_next_fixable.rs:22:26 | LL | let _: Option = a.iter().filter_map(|s| s.parse().ok()).next(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `a.iter().find_map(|s| s.parse().ok())` diff --git a/tests/ui/from_over_into.fixed b/tests/ui/from_over_into.fixed index 1cf49ca45f49..125c9a69cd3f 100644 --- a/tests/ui/from_over_into.fixed +++ b/tests/ui/from_over_into.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::from_over_into)] #![allow(unused)] @@ -60,9 +59,8 @@ impl From for A { } } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - struct FromOverInto(Vec); impl Into> for Vec { @@ -72,9 +70,8 @@ fn msrv_1_40() { } } +#[clippy::msrv = "1.41"] fn msrv_1_41() { - #![clippy::msrv = "1.41"] - struct FromOverInto(Vec); impl From> for FromOverInto { diff --git a/tests/ui/from_over_into.rs b/tests/ui/from_over_into.rs index d30f3c3fc925..5aa127bfabe4 100644 --- a/tests/ui/from_over_into.rs +++ b/tests/ui/from_over_into.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::from_over_into)] #![allow(unused)] @@ -60,9 +59,8 @@ impl From for A { } } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - struct FromOverInto(Vec); impl Into> for Vec { @@ -72,9 +70,8 @@ fn msrv_1_40() { } } +#[clippy::msrv = "1.41"] fn msrv_1_41() { - #![clippy::msrv = "1.41"] - struct FromOverInto(Vec); impl Into> for Vec { diff --git a/tests/ui/from_over_into.stderr b/tests/ui/from_over_into.stderr index 9c2a7c04c364..a1764a5ea12a 100644 --- a/tests/ui/from_over_into.stderr +++ b/tests/ui/from_over_into.stderr @@ -1,5 +1,5 @@ error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:10:1 + --> $DIR/from_over_into.rs:9:1 | LL | impl Into for String { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -13,7 +13,7 @@ LL ~ StringWrapper(val) | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:18:1 + --> $DIR/from_over_into.rs:17:1 | LL | impl Into for String { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -26,7 +26,7 @@ LL ~ SelfType(String::new()) | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:33:1 + --> $DIR/from_over_into.rs:32:1 | LL | impl Into for X { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -41,7 +41,7 @@ LL ~ let _: X = val; | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:45:1 + --> $DIR/from_over_into.rs:44:1 | LL | impl core::convert::Into for crate::ExplicitPaths { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -59,7 +59,7 @@ LL ~ val.0 | error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true - --> $DIR/from_over_into.rs:80:5 + --> $DIR/from_over_into.rs:77:5 | LL | impl Into> for Vec { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/if_then_some_else_none.rs b/tests/ui/if_then_some_else_none.rs index 3bc3a0395244..0e89fdb0dfa2 100644 --- a/tests/ui/if_then_some_else_none.rs +++ b/tests/ui/if_then_some_else_none.rs @@ -1,5 +1,4 @@ #![warn(clippy::if_then_some_else_none)] -#![feature(custom_inner_attributes)] fn main() { // Should issue an error. @@ -66,8 +65,8 @@ fn main() { let _ = if foo() { into_some("foo") } else { None }; } +#[clippy::msrv = "1.49"] fn _msrv_1_49() { - #![clippy::msrv = "1.49"] // `bool::then` was stabilized in 1.50. Do not lint this let _ = if foo() { println!("true!"); @@ -77,8 +76,8 @@ fn _msrv_1_49() { }; } +#[clippy::msrv = "1.50"] fn _msrv_1_50() { - #![clippy::msrv = "1.50"] let _ = if foo() { println!("true!"); Some(150) diff --git a/tests/ui/if_then_some_else_none.stderr b/tests/ui/if_then_some_else_none.stderr index 24e0b5947f19..d728a3c31a3b 100644 --- a/tests/ui/if_then_some_else_none.stderr +++ b/tests/ui/if_then_some_else_none.stderr @@ -1,5 +1,5 @@ error: this could be simplified with `bool::then` - --> $DIR/if_then_some_else_none.rs:6:13 + --> $DIR/if_then_some_else_none.rs:5:13 | LL | let _ = if foo() { | _____________^ @@ -14,7 +14,7 @@ LL | | }; = note: `-D clippy::if-then-some-else-none` implied by `-D warnings` error: this could be simplified with `bool::then` - --> $DIR/if_then_some_else_none.rs:14:13 + --> $DIR/if_then_some_else_none.rs:13:13 | LL | let _ = if matches!(true, true) { | _____________^ @@ -28,7 +28,7 @@ LL | | }; = help: consider using `bool::then` like: `matches!(true, true).then(|| { /* snippet */ matches!(true, false) })` error: this could be simplified with `bool::then_some` - --> $DIR/if_then_some_else_none.rs:23:28 + --> $DIR/if_then_some_else_none.rs:22:28 | LL | let _ = x.and_then(|o| if o < 32 { Some(o) } else { None }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL | let _ = x.and_then(|o| if o < 32 { Some(o) } else { None }); = help: consider using `bool::then_some` like: `(o < 32).then_some(o)` error: this could be simplified with `bool::then_some` - --> $DIR/if_then_some_else_none.rs:27:13 + --> $DIR/if_then_some_else_none.rs:26:13 | LL | let _ = if !x { Some(0) } else { None }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -44,7 +44,7 @@ LL | let _ = if !x { Some(0) } else { None }; = help: consider using `bool::then_some` like: `(!x).then_some(0)` error: this could be simplified with `bool::then` - --> $DIR/if_then_some_else_none.rs:82:13 + --> $DIR/if_then_some_else_none.rs:81:13 | LL | let _ = if foo() { | _____________^ diff --git a/tests/ui/manual_clamp.rs b/tests/ui/manual_clamp.rs index 331fd29b74e8..f7902e6fd538 100644 --- a/tests/ui/manual_clamp.rs +++ b/tests/ui/manual_clamp.rs @@ -1,4 +1,3 @@ -#![feature(custom_inner_attributes)] #![warn(clippy::manual_clamp)] #![allow( unused, @@ -304,9 +303,8 @@ fn cmp_min_max(input: i32) -> i32 { input * 3 } +#[clippy::msrv = "1.49"] fn msrv_1_49() { - #![clippy::msrv = "1.49"] - let (input, min, max) = (0, -1, 2); let _ = if input < min { min @@ -317,9 +315,8 @@ fn msrv_1_49() { }; } +#[clippy::msrv = "1.50"] fn msrv_1_50() { - #![clippy::msrv = "1.50"] - let (input, min, max) = (0, -1, 2); let _ = if input < min { min diff --git a/tests/ui/manual_clamp.stderr b/tests/ui/manual_clamp.stderr index 70abe28091c9..988ad1527e83 100644 --- a/tests/ui/manual_clamp.stderr +++ b/tests/ui/manual_clamp.stderr @@ -1,5 +1,5 @@ error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:77:5 + --> $DIR/manual_clamp.rs:76:5 | LL | / if x9 < min { LL | | x9 = min; @@ -13,7 +13,7 @@ LL | | } = note: `-D clippy::manual-clamp` implied by `-D warnings` error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:92:5 + --> $DIR/manual_clamp.rs:91:5 | LL | / if x11 > max { LL | | x11 = max; @@ -26,7 +26,7 @@ LL | | } = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:100:5 + --> $DIR/manual_clamp.rs:99:5 | LL | / if min > x12 { LL | | x12 = min; @@ -39,7 +39,7 @@ LL | | } = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:108:5 + --> $DIR/manual_clamp.rs:107:5 | LL | / if max < x13 { LL | | x13 = max; @@ -52,7 +52,7 @@ LL | | } = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:162:5 + --> $DIR/manual_clamp.rs:161:5 | LL | / if max < x33 { LL | | x33 = max; @@ -65,7 +65,7 @@ LL | | } = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:22:14 + --> $DIR/manual_clamp.rs:21:14 | LL | let x0 = if max < input { | ______________^ @@ -80,7 +80,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:30:14 + --> $DIR/manual_clamp.rs:29:14 | LL | let x1 = if input > max { | ______________^ @@ -95,7 +95,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:38:14 + --> $DIR/manual_clamp.rs:37:14 | LL | let x2 = if input < min { | ______________^ @@ -110,7 +110,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:46:14 + --> $DIR/manual_clamp.rs:45:14 | LL | let x3 = if min > input { | ______________^ @@ -125,7 +125,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:54:14 + --> $DIR/manual_clamp.rs:53:14 | LL | let x4 = input.max(min).min(max); | ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(min, max)` @@ -133,7 +133,7 @@ LL | let x4 = input.max(min).min(max); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:56:14 + --> $DIR/manual_clamp.rs:55:14 | LL | let x5 = input.min(max).max(min); | ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(min, max)` @@ -141,7 +141,7 @@ LL | let x5 = input.min(max).max(min); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:58:14 + --> $DIR/manual_clamp.rs:57:14 | LL | let x6 = match input { | ______________^ @@ -154,7 +154,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:64:14 + --> $DIR/manual_clamp.rs:63:14 | LL | let x7 = match input { | ______________^ @@ -167,7 +167,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:70:14 + --> $DIR/manual_clamp.rs:69:14 | LL | let x8 = match input { | ______________^ @@ -180,7 +180,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:84:15 + --> $DIR/manual_clamp.rs:83:15 | LL | let x10 = match input { | _______________^ @@ -193,7 +193,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:115:15 + --> $DIR/manual_clamp.rs:114:15 | LL | let x14 = if input > CONST_MAX { | _______________^ @@ -208,7 +208,7 @@ LL | | }; = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:124:19 + --> $DIR/manual_clamp.rs:123:19 | LL | let x15 = if input > max { | ___________________^ @@ -224,7 +224,7 @@ LL | | }; = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:135:19 + --> $DIR/manual_clamp.rs:134:19 | LL | let x16 = cmp_max(cmp_min(input, CONST_MAX), CONST_MIN); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -232,7 +232,7 @@ LL | let x16 = cmp_max(cmp_min(input, CONST_MAX), CONST_MIN); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:136:19 + --> $DIR/manual_clamp.rs:135:19 | LL | let x17 = cmp_min(cmp_max(input, CONST_MIN), CONST_MAX); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -240,7 +240,7 @@ LL | let x17 = cmp_min(cmp_max(input, CONST_MIN), CONST_MAX); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:137:19 + --> $DIR/manual_clamp.rs:136:19 | LL | let x18 = cmp_max(CONST_MIN, cmp_min(input, CONST_MAX)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -248,7 +248,7 @@ LL | let x18 = cmp_max(CONST_MIN, cmp_min(input, CONST_MAX)); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:138:19 + --> $DIR/manual_clamp.rs:137:19 | LL | let x19 = cmp_min(CONST_MAX, cmp_max(input, CONST_MIN)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -256,7 +256,7 @@ LL | let x19 = cmp_min(CONST_MAX, cmp_max(input, CONST_MIN)); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:139:19 + --> $DIR/manual_clamp.rs:138:19 | LL | let x20 = cmp_max(cmp_min(CONST_MAX, input), CONST_MIN); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -264,7 +264,7 @@ LL | let x20 = cmp_max(cmp_min(CONST_MAX, input), CONST_MIN); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:140:19 + --> $DIR/manual_clamp.rs:139:19 | LL | let x21 = cmp_min(cmp_max(CONST_MIN, input), CONST_MAX); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -272,7 +272,7 @@ LL | let x21 = cmp_min(cmp_max(CONST_MIN, input), CONST_MAX); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:141:19 + --> $DIR/manual_clamp.rs:140:19 | LL | let x22 = cmp_max(CONST_MIN, cmp_min(CONST_MAX, input)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -280,7 +280,7 @@ LL | let x22 = cmp_max(CONST_MIN, cmp_min(CONST_MAX, input)); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:142:19 + --> $DIR/manual_clamp.rs:141:19 | LL | let x23 = cmp_min(CONST_MAX, cmp_max(CONST_MIN, input)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_MIN, CONST_MAX)` @@ -288,7 +288,7 @@ LL | let x23 = cmp_min(CONST_MAX, cmp_max(CONST_MIN, input)); = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:144:19 + --> $DIR/manual_clamp.rs:143:19 | LL | let x24 = f64::max(f64::min(input, CONST_F64_MAX), CONST_F64_MIN); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -297,7 +297,7 @@ LL | let x24 = f64::max(f64::min(input, CONST_F64_MAX), CONST_F64_MIN); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:145:19 + --> $DIR/manual_clamp.rs:144:19 | LL | let x25 = f64::min(f64::max(input, CONST_F64_MIN), CONST_F64_MAX); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -306,7 +306,7 @@ LL | let x25 = f64::min(f64::max(input, CONST_F64_MIN), CONST_F64_MAX); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:146:19 + --> $DIR/manual_clamp.rs:145:19 | LL | let x26 = f64::max(CONST_F64_MIN, f64::min(input, CONST_F64_MAX)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -315,7 +315,7 @@ LL | let x26 = f64::max(CONST_F64_MIN, f64::min(input, CONST_F64_MAX)); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:147:19 + --> $DIR/manual_clamp.rs:146:19 | LL | let x27 = f64::min(CONST_F64_MAX, f64::max(input, CONST_F64_MIN)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -324,7 +324,7 @@ LL | let x27 = f64::min(CONST_F64_MAX, f64::max(input, CONST_F64_MIN)); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:148:19 + --> $DIR/manual_clamp.rs:147:19 | LL | let x28 = f64::max(f64::min(CONST_F64_MAX, input), CONST_F64_MIN); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -333,7 +333,7 @@ LL | let x28 = f64::max(f64::min(CONST_F64_MAX, input), CONST_F64_MIN); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:149:19 + --> $DIR/manual_clamp.rs:148:19 | LL | let x29 = f64::min(f64::max(CONST_F64_MIN, input), CONST_F64_MAX); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -342,7 +342,7 @@ LL | let x29 = f64::min(f64::max(CONST_F64_MIN, input), CONST_F64_MAX); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:150:19 + --> $DIR/manual_clamp.rs:149:19 | LL | let x30 = f64::max(CONST_F64_MIN, f64::min(CONST_F64_MAX, input)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -351,7 +351,7 @@ LL | let x30 = f64::max(CONST_F64_MIN, f64::min(CONST_F64_MAX, input)); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:151:19 + --> $DIR/manual_clamp.rs:150:19 | LL | let x31 = f64::min(CONST_F64_MAX, f64::max(CONST_F64_MIN, input)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with clamp: `input.clamp(CONST_F64_MIN, CONST_F64_MAX)` @@ -360,7 +360,7 @@ LL | let x31 = f64::min(CONST_F64_MAX, f64::max(CONST_F64_MIN, input)); = note: clamp returns NaN if the input is NaN error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:154:5 + --> $DIR/manual_clamp.rs:153:5 | LL | / if x32 < min { LL | | x32 = min; @@ -372,7 +372,7 @@ LL | | } = note: clamp will panic if max < min error: clamp-like pattern without using clamp function - --> $DIR/manual_clamp.rs:324:13 + --> $DIR/manual_clamp.rs:321:13 | LL | let _ = if input < min { | _____________^ diff --git a/tests/ui/manual_is_ascii_check.fixed b/tests/ui/manual_is_ascii_check.fixed index 765bb785994e..231ba83b1426 100644 --- a/tests/ui/manual_is_ascii_check.fixed +++ b/tests/ui/manual_is_ascii_check.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused, dead_code)] #![warn(clippy::manual_is_ascii_check)] @@ -18,28 +17,26 @@ fn main() { assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); } +#[clippy::msrv = "1.23"] fn msrv_1_23() { - #![clippy::msrv = "1.23"] - assert!(matches!(b'1', b'0'..=b'9')); assert!(matches!('X', 'A'..='Z')); assert!(matches!('x', 'A'..='Z' | 'a'..='z')); } +#[clippy::msrv = "1.24"] fn msrv_1_24() { - #![clippy::msrv = "1.24"] - assert!(b'1'.is_ascii_digit()); assert!('X'.is_ascii_uppercase()); assert!('x'.is_ascii_alphabetic()); } +#[clippy::msrv = "1.46"] fn msrv_1_46() { - #![clippy::msrv = "1.46"] const FOO: bool = matches!('x', '0'..='9'); } +#[clippy::msrv = "1.47"] fn msrv_1_47() { - #![clippy::msrv = "1.47"] const FOO: bool = 'x'.is_ascii_digit(); } diff --git a/tests/ui/manual_is_ascii_check.rs b/tests/ui/manual_is_ascii_check.rs index be1331610412..39ee6151c56f 100644 --- a/tests/ui/manual_is_ascii_check.rs +++ b/tests/ui/manual_is_ascii_check.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused, dead_code)] #![warn(clippy::manual_is_ascii_check)] @@ -18,28 +17,26 @@ fn main() { assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); } +#[clippy::msrv = "1.23"] fn msrv_1_23() { - #![clippy::msrv = "1.23"] - assert!(matches!(b'1', b'0'..=b'9')); assert!(matches!('X', 'A'..='Z')); assert!(matches!('x', 'A'..='Z' | 'a'..='z')); } +#[clippy::msrv = "1.24"] fn msrv_1_24() { - #![clippy::msrv = "1.24"] - assert!(matches!(b'1', b'0'..=b'9')); assert!(matches!('X', 'A'..='Z')); assert!(matches!('x', 'A'..='Z' | 'a'..='z')); } +#[clippy::msrv = "1.46"] fn msrv_1_46() { - #![clippy::msrv = "1.46"] const FOO: bool = matches!('x', '0'..='9'); } +#[clippy::msrv = "1.47"] fn msrv_1_47() { - #![clippy::msrv = "1.47"] const FOO: bool = matches!('x', '0'..='9'); } diff --git a/tests/ui/manual_is_ascii_check.stderr b/tests/ui/manual_is_ascii_check.stderr index c0a9d4db1a15..397cbe05c822 100644 --- a/tests/ui/manual_is_ascii_check.stderr +++ b/tests/ui/manual_is_ascii_check.stderr @@ -1,5 +1,5 @@ error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:8:13 + --> $DIR/manual_is_ascii_check.rs:7:13 | LL | assert!(matches!('x', 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_lowercase()` @@ -7,61 +7,61 @@ LL | assert!(matches!('x', 'a'..='z')); = note: `-D clippy::manual-is-ascii-check` implied by `-D warnings` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:9:13 + --> $DIR/manual_is_ascii_check.rs:8:13 | LL | assert!(matches!('X', 'A'..='Z')); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'X'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:10:13 + --> $DIR/manual_is_ascii_check.rs:9:13 | LL | assert!(matches!(b'x', b'a'..=b'z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'x'.is_ascii_lowercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:11:13 + --> $DIR/manual_is_ascii_check.rs:10:13 | LL | assert!(matches!(b'X', b'A'..=b'Z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'X'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:14:13 + --> $DIR/manual_is_ascii_check.rs:13:13 | LL | assert!(matches!(num, '0'..='9')); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:15:13 + --> $DIR/manual_is_ascii_check.rs:14:13 | LL | assert!(matches!(b'1', b'0'..=b'9')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'1'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:16:13 + --> $DIR/manual_is_ascii_check.rs:15:13 | LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:32:13 + --> $DIR/manual_is_ascii_check.rs:29:13 | LL | assert!(matches!(b'1', b'0'..=b'9')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'1'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:33:13 + --> $DIR/manual_is_ascii_check.rs:30:13 | LL | assert!(matches!('X', 'A'..='Z')); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'X'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:34:13 + --> $DIR/manual_is_ascii_check.rs:31:13 | LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:44:23 + --> $DIR/manual_is_ascii_check.rs:41:23 | LL | const FOO: bool = matches!('x', '0'..='9'); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_digit()` diff --git a/tests/ui/manual_let_else.rs b/tests/ui/manual_let_else.rs index 2ef40e5911af..48a162c13602 100644 --- a/tests/ui/manual_let_else.rs +++ b/tests/ui/manual_let_else.rs @@ -234,4 +234,18 @@ fn not_fire() { // If a type annotation is present, don't lint as // expressing the type might be too hard let v: () = if let Some(v_some) = g() { v_some } else { panic!() }; + + // Issue 9940 + // Suggestion should not expand macros + macro_rules! macro_call { + () => { + return () + }; + } + + let ff = Some(1); + let _ = match ff { + Some(value) => value, + _ => macro_call!(), + }; } diff --git a/tests/ui/manual_let_else.stderr b/tests/ui/manual_let_else.stderr index 453b68b8bd00..52aac6bc673d 100644 --- a/tests/ui/manual_let_else.stderr +++ b/tests/ui/manual_let_else.stderr @@ -259,5 +259,14 @@ LL | create_binding_if_some!(w, g()); | = note: this error originates in the macro `create_binding_if_some` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 17 previous errors +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:247:5 + | +LL | / let _ = match ff { +LL | | Some(value) => value, +LL | | _ => macro_call!(), +LL | | }; + | |______^ help: consider writing: `let Some(value) = ff else { macro_call!() };` + +error: aborting due to 18 previous errors diff --git a/tests/ui/manual_rem_euclid.fixed b/tests/ui/manual_rem_euclid.fixed index b942fbfe9305..4cdc0546a744 100644 --- a/tests/ui/manual_rem_euclid.fixed +++ b/tests/ui/manual_rem_euclid.fixed @@ -1,7 +1,6 @@ // run-rustfix // aux-build:macro_rules.rs -#![feature(custom_inner_attributes)] #![warn(clippy::manual_rem_euclid)] #[macro_use] @@ -55,31 +54,27 @@ pub const fn const_rem_euclid_4(num: i32) -> i32 { num.rem_euclid(4) } +#[clippy::msrv = "1.37"] pub fn msrv_1_37() { - #![clippy::msrv = "1.37"] - let x: i32 = 10; let _: i32 = ((x % 4) + 4) % 4; } +#[clippy::msrv = "1.38"] pub fn msrv_1_38() { - #![clippy::msrv = "1.38"] - let x: i32 = 10; let _: i32 = x.rem_euclid(4); } // For const fns: +#[clippy::msrv = "1.51"] pub const fn msrv_1_51() { - #![clippy::msrv = "1.51"] - let x: i32 = 10; let _: i32 = ((x % 4) + 4) % 4; } +#[clippy::msrv = "1.52"] pub const fn msrv_1_52() { - #![clippy::msrv = "1.52"] - let x: i32 = 10; let _: i32 = x.rem_euclid(4); } diff --git a/tests/ui/manual_rem_euclid.rs b/tests/ui/manual_rem_euclid.rs index 7462d532169f..58a9e20f38b1 100644 --- a/tests/ui/manual_rem_euclid.rs +++ b/tests/ui/manual_rem_euclid.rs @@ -1,7 +1,6 @@ // run-rustfix // aux-build:macro_rules.rs -#![feature(custom_inner_attributes)] #![warn(clippy::manual_rem_euclid)] #[macro_use] @@ -55,31 +54,27 @@ pub const fn const_rem_euclid_4(num: i32) -> i32 { ((num % 4) + 4) % 4 } +#[clippy::msrv = "1.37"] pub fn msrv_1_37() { - #![clippy::msrv = "1.37"] - let x: i32 = 10; let _: i32 = ((x % 4) + 4) % 4; } +#[clippy::msrv = "1.38"] pub fn msrv_1_38() { - #![clippy::msrv = "1.38"] - let x: i32 = 10; let _: i32 = ((x % 4) + 4) % 4; } // For const fns: +#[clippy::msrv = "1.51"] pub const fn msrv_1_51() { - #![clippy::msrv = "1.51"] - let x: i32 = 10; let _: i32 = ((x % 4) + 4) % 4; } +#[clippy::msrv = "1.52"] pub const fn msrv_1_52() { - #![clippy::msrv = "1.52"] - let x: i32 = 10; let _: i32 = ((x % 4) + 4) % 4; } diff --git a/tests/ui/manual_rem_euclid.stderr b/tests/ui/manual_rem_euclid.stderr index d51bac03b565..e3122a588b64 100644 --- a/tests/ui/manual_rem_euclid.stderr +++ b/tests/ui/manual_rem_euclid.stderr @@ -1,5 +1,5 @@ error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:20:18 + --> $DIR/manual_rem_euclid.rs:19:18 | LL | let _: i32 = ((value % 4) + 4) % 4; | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)` @@ -7,31 +7,31 @@ LL | let _: i32 = ((value % 4) + 4) % 4; = note: `-D clippy::manual-rem-euclid` implied by `-D warnings` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:21:18 + --> $DIR/manual_rem_euclid.rs:20:18 | LL | let _: i32 = (4 + (value % 4)) % 4; | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:22:18 + --> $DIR/manual_rem_euclid.rs:21:18 | LL | let _: i32 = (value % 4 + 4) % 4; | ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:23:18 + --> $DIR/manual_rem_euclid.rs:22:18 | LL | let _: i32 = (4 + value % 4) % 4; | ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:24:22 + --> $DIR/manual_rem_euclid.rs:23:22 | LL | let _: i32 = 1 + (4 + value % 4) % 4; | ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:13:22 + --> $DIR/manual_rem_euclid.rs:12:22 | LL | let _: i32 = ((value % 4) + 4) % 4; | ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)` @@ -42,25 +42,25 @@ LL | internal_rem_euclid!(); = note: this error originates in the macro `internal_rem_euclid` (in Nightly builds, run with -Z macro-backtrace for more info) error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:50:5 + --> $DIR/manual_rem_euclid.rs:49:5 | LL | ((num % 4) + 4) % 4 | ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:55:5 + --> $DIR/manual_rem_euclid.rs:54:5 | LL | ((num % 4) + 4) % 4 | ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:69:18 + --> $DIR/manual_rem_euclid.rs:66:18 | LL | let _: i32 = ((x % 4) + 4) % 4; | ^^^^^^^^^^^^^^^^^ help: consider using: `x.rem_euclid(4)` error: manual `rem_euclid` implementation - --> $DIR/manual_rem_euclid.rs:84:18 + --> $DIR/manual_rem_euclid.rs:79:18 | LL | let _: i32 = ((x % 4) + 4) % 4; | ^^^^^^^^^^^^^^^^^ help: consider using: `x.rem_euclid(4)` diff --git a/tests/ui/manual_retain.fixed b/tests/ui/manual_retain.fixed index fba503a20667..e5ae3cf3e503 100644 --- a/tests/ui/manual_retain.fixed +++ b/tests/ui/manual_retain.fixed @@ -1,5 +1,4 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_retain)] #![allow(unused)] use std::collections::BTreeMap; @@ -216,8 +215,8 @@ fn vec_deque_retain() { bar = foobar.into_iter().filter(|x| x % 2 == 0).collect(); } +#[clippy::msrv = "1.52"] fn _msrv_153() { - #![clippy::msrv = "1.52"] let mut btree_map: BTreeMap = (0..8).map(|x| (x, x * 10)).collect(); btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect(); @@ -225,14 +224,14 @@ fn _msrv_153() { btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect(); } +#[clippy::msrv = "1.25"] fn _msrv_126() { - #![clippy::msrv = "1.25"] let mut s = String::from("foobar"); s = s.chars().filter(|&c| c != 'o').to_owned().collect(); } +#[clippy::msrv = "1.17"] fn _msrv_118() { - #![clippy::msrv = "1.17"] let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]); hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect(); let mut hash_map: HashMap = (0..8).map(|x| (x, x * 10)).collect(); diff --git a/tests/ui/manual_retain.rs b/tests/ui/manual_retain.rs index 81a849fe7684..1021f15edd1e 100644 --- a/tests/ui/manual_retain.rs +++ b/tests/ui/manual_retain.rs @@ -1,5 +1,4 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_retain)] #![allow(unused)] use std::collections::BTreeMap; @@ -222,8 +221,8 @@ fn vec_deque_retain() { bar = foobar.into_iter().filter(|x| x % 2 == 0).collect(); } +#[clippy::msrv = "1.52"] fn _msrv_153() { - #![clippy::msrv = "1.52"] let mut btree_map: BTreeMap = (0..8).map(|x| (x, x * 10)).collect(); btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect(); @@ -231,14 +230,14 @@ fn _msrv_153() { btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect(); } +#[clippy::msrv = "1.25"] fn _msrv_126() { - #![clippy::msrv = "1.25"] let mut s = String::from("foobar"); s = s.chars().filter(|&c| c != 'o').to_owned().collect(); } +#[clippy::msrv = "1.17"] fn _msrv_118() { - #![clippy::msrv = "1.17"] let mut hash_set = HashSet::from([1, 2, 3, 4, 5, 6]); hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect(); let mut hash_map: HashMap = (0..8).map(|x| (x, x * 10)).collect(); diff --git a/tests/ui/manual_retain.stderr b/tests/ui/manual_retain.stderr index ec635919b48f..89316ce1d998 100644 --- a/tests/ui/manual_retain.stderr +++ b/tests/ui/manual_retain.stderr @@ -1,5 +1,5 @@ error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:52:5 + --> $DIR/manual_retain.rs:51:5 | LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|k, _| k % 2 == 0)` @@ -7,13 +7,13 @@ LL | btree_map = btree_map.into_iter().filter(|(k, _)| k % 2 == 0).collect() = note: `-D clippy::manual-retain` implied by `-D warnings` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:53:5 + --> $DIR/manual_retain.rs:52:5 | LL | btree_map = btree_map.into_iter().filter(|(_, v)| v % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_map.retain(|_, &mut v| v % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:54:5 + --> $DIR/manual_retain.rs:53:5 | LL | / btree_map = btree_map LL | | .into_iter() @@ -22,37 +22,37 @@ LL | | .collect(); | |__________________^ help: consider calling `.retain()` instead: `btree_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:76:5 + --> $DIR/manual_retain.rs:75:5 | LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).copied().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:77:5 + --> $DIR/manual_retain.rs:76:5 | LL | btree_set = btree_set.iter().filter(|&x| x % 2 == 0).cloned().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:78:5 + --> $DIR/manual_retain.rs:77:5 | LL | btree_set = btree_set.into_iter().filter(|x| x % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `btree_set.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:108:5 + --> $DIR/manual_retain.rs:107:5 | LL | hash_map = hash_map.into_iter().filter(|(k, _)| k % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|k, _| k % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:109:5 + --> $DIR/manual_retain.rs:108:5 | LL | hash_map = hash_map.into_iter().filter(|(_, v)| v % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_map.retain(|_, &mut v| v % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:110:5 + --> $DIR/manual_retain.rs:109:5 | LL | / hash_map = hash_map LL | | .into_iter() @@ -61,61 +61,61 @@ LL | | .collect(); | |__________________^ help: consider calling `.retain()` instead: `hash_map.retain(|k, &mut v| (k % 2 == 0) && (v % 2 == 0))` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:131:5 + --> $DIR/manual_retain.rs:130:5 | LL | hash_set = hash_set.into_iter().filter(|x| x % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:132:5 + --> $DIR/manual_retain.rs:131:5 | LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).copied().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:133:5 + --> $DIR/manual_retain.rs:132:5 | LL | hash_set = hash_set.iter().filter(|&x| x % 2 == 0).cloned().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `hash_set.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:162:5 + --> $DIR/manual_retain.rs:161:5 | LL | s = s.chars().filter(|&c| c != 'o').to_owned().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `s.retain(|c| c != 'o')` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:174:5 + --> $DIR/manual_retain.rs:173:5 | LL | vec = vec.iter().filter(|&x| x % 2 == 0).copied().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:175:5 + --> $DIR/manual_retain.rs:174:5 | LL | vec = vec.iter().filter(|&x| x % 2 == 0).cloned().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:176:5 + --> $DIR/manual_retain.rs:175:5 | LL | vec = vec.into_iter().filter(|x| x % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:198:5 + --> $DIR/manual_retain.rs:197:5 | LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).copied().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:199:5 + --> $DIR/manual_retain.rs:198:5 | LL | vec_deque = vec_deque.iter().filter(|&x| x % 2 == 0).cloned().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)` error: this expression can be written more simply using `.retain()` - --> $DIR/manual_retain.rs:200:5 + --> $DIR/manual_retain.rs:199:5 | LL | vec_deque = vec_deque.into_iter().filter(|x| x % 2 == 0).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `.retain()` instead: `vec_deque.retain(|x| x % 2 == 0)` diff --git a/tests/ui/manual_split_once.fixed b/tests/ui/manual_split_once.fixed index c7ca770434a3..50b02019cc27 100644 --- a/tests/ui/manual_split_once.fixed +++ b/tests/ui/manual_split_once.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_split_once)] #![allow(unused, clippy::iter_skip_next, clippy::iter_nth_zero)] @@ -127,8 +126,8 @@ fn indirect() -> Option<()> { None } +#[clippy::msrv = "1.51"] fn _msrv_1_51() { - #![clippy::msrv = "1.51"] // `str::split_once` was stabilized in 1.52. Do not lint this let _ = "key=value".splitn(2, '=').nth(1).unwrap(); @@ -137,8 +136,8 @@ fn _msrv_1_51() { let b = iter.next().unwrap(); } +#[clippy::msrv = "1.52"] fn _msrv_1_52() { - #![clippy::msrv = "1.52"] let _ = "key=value".split_once('=').unwrap().1; let (a, b) = "a.b.c".split_once('.').unwrap(); diff --git a/tests/ui/manual_split_once.rs b/tests/ui/manual_split_once.rs index ee2848a251ee..e1e8b71a9def 100644 --- a/tests/ui/manual_split_once.rs +++ b/tests/ui/manual_split_once.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_split_once)] #![allow(unused, clippy::iter_skip_next, clippy::iter_nth_zero)] @@ -127,8 +126,8 @@ fn indirect() -> Option<()> { None } +#[clippy::msrv = "1.51"] fn _msrv_1_51() { - #![clippy::msrv = "1.51"] // `str::split_once` was stabilized in 1.52. Do not lint this let _ = "key=value".splitn(2, '=').nth(1).unwrap(); @@ -137,8 +136,8 @@ fn _msrv_1_51() { let b = iter.next().unwrap(); } +#[clippy::msrv = "1.52"] fn _msrv_1_52() { - #![clippy::msrv = "1.52"] let _ = "key=value".splitn(2, '=').nth(1).unwrap(); let mut iter = "a.b.c".splitn(2, '.'); diff --git a/tests/ui/manual_split_once.stderr b/tests/ui/manual_split_once.stderr index 2696694680ad..78da5a16cc52 100644 --- a/tests/ui/manual_split_once.stderr +++ b/tests/ui/manual_split_once.stderr @@ -1,5 +1,5 @@ error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:14:13 + --> $DIR/manual_split_once.rs:13:13 | LL | let _ = "key=value".splitn(2, '=').nth(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').unwrap().1` @@ -7,79 +7,79 @@ LL | let _ = "key=value".splitn(2, '=').nth(1).unwrap(); = note: `-D clippy::manual-split-once` implied by `-D warnings` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:15:13 + --> $DIR/manual_split_once.rs:14:13 | LL | let _ = "key=value".splitn(2, '=').skip(1).next().unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').unwrap().1` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:16:18 + --> $DIR/manual_split_once.rs:15:18 | LL | let (_, _) = "key=value".splitn(2, '=').next_tuple().unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=')` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:19:13 + --> $DIR/manual_split_once.rs:18:13 | LL | let _ = s.splitn(2, '=').nth(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=').unwrap().1` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:22:13 + --> $DIR/manual_split_once.rs:21:13 | LL | let _ = s.splitn(2, '=').nth(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=').unwrap().1` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:25:13 + --> $DIR/manual_split_once.rs:24:13 | LL | let _ = s.splitn(2, '=').skip(1).next().unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=').unwrap().1` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:28:17 + --> $DIR/manual_split_once.rs:27:17 | LL | let _ = s.splitn(2, '=').nth(1)?; | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=')?.1` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:29:17 + --> $DIR/manual_split_once.rs:28:17 | LL | let _ = s.splitn(2, '=').skip(1).next()?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=')?.1` error: manual implementation of `rsplit_once` - --> $DIR/manual_split_once.rs:30:17 + --> $DIR/manual_split_once.rs:29:17 | LL | let _ = s.rsplitn(2, '=').nth(1)?; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.rsplit_once('=')?.0` error: manual implementation of `rsplit_once` - --> $DIR/manual_split_once.rs:31:17 + --> $DIR/manual_split_once.rs:30:17 | LL | let _ = s.rsplitn(2, '=').skip(1).next()?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.rsplit_once('=')?.0` error: manual implementation of `rsplit_once` - --> $DIR/manual_split_once.rs:39:13 + --> $DIR/manual_split_once.rs:38:13 | LL | let _ = "key=value".rsplitn(2, '=').nth(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".rsplit_once('=').unwrap().0` error: manual implementation of `rsplit_once` - --> $DIR/manual_split_once.rs:40:18 + --> $DIR/manual_split_once.rs:39:18 | LL | let (_, _) = "key=value".rsplitn(2, '=').next_tuple().unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".rsplit_once('=').map(|(x, y)| (y, x))` error: manual implementation of `rsplit_once` - --> $DIR/manual_split_once.rs:41:13 + --> $DIR/manual_split_once.rs:40:13 | LL | let _ = s.rsplitn(2, '=').nth(1); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.rsplit_once('=').map(|x| x.0)` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:45:5 + --> $DIR/manual_split_once.rs:44:5 | LL | let mut iter = "a.b.c".splitn(2, '.'); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -104,7 +104,7 @@ LL + | error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:49:5 + --> $DIR/manual_split_once.rs:48:5 | LL | let mut iter = "a.b.c".splitn(2, '.'); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -129,7 +129,7 @@ LL + | error: manual implementation of `rsplit_once` - --> $DIR/manual_split_once.rs:53:5 + --> $DIR/manual_split_once.rs:52:5 | LL | let mut iter = "a.b.c".rsplitn(2, '.'); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -154,7 +154,7 @@ LL + | error: manual implementation of `rsplit_once` - --> $DIR/manual_split_once.rs:57:5 + --> $DIR/manual_split_once.rs:56:5 | LL | let mut iter = "a.b.c".rsplitn(2, '.'); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -179,13 +179,13 @@ LL + | error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:142:13 + --> $DIR/manual_split_once.rs:141:13 | LL | let _ = "key=value".splitn(2, '=').nth(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').unwrap().1` error: manual implementation of `split_once` - --> $DIR/manual_split_once.rs:144:5 + --> $DIR/manual_split_once.rs:143:5 | LL | let mut iter = "a.b.c".splitn(2, '.'); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/manual_str_repeat.fixed b/tests/ui/manual_str_repeat.fixed index 0704ba2f933e..3d56f2a0dedb 100644 --- a/tests/ui/manual_str_repeat.fixed +++ b/tests/ui/manual_str_repeat.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_str_repeat)] use std::borrow::Cow; @@ -54,13 +53,13 @@ fn main() { let _: String = repeat(x).take(count).collect(); } +#[clippy::msrv = "1.15"] fn _msrv_1_15() { - #![clippy::msrv = "1.15"] // `str::repeat` was stabilized in 1.16. Do not lint this let _: String = std::iter::repeat("test").take(10).collect(); } +#[clippy::msrv = "1.16"] fn _msrv_1_16() { - #![clippy::msrv = "1.16"] let _: String = "test".repeat(10); } diff --git a/tests/ui/manual_str_repeat.rs b/tests/ui/manual_str_repeat.rs index f522be439aa0..e8240a949dbc 100644 --- a/tests/ui/manual_str_repeat.rs +++ b/tests/ui/manual_str_repeat.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_str_repeat)] use std::borrow::Cow; @@ -54,13 +53,13 @@ fn main() { let _: String = repeat(x).take(count).collect(); } +#[clippy::msrv = "1.15"] fn _msrv_1_15() { - #![clippy::msrv = "1.15"] // `str::repeat` was stabilized in 1.16. Do not lint this let _: String = std::iter::repeat("test").take(10).collect(); } +#[clippy::msrv = "1.16"] fn _msrv_1_16() { - #![clippy::msrv = "1.16"] let _: String = std::iter::repeat("test").take(10).collect(); } diff --git a/tests/ui/manual_str_repeat.stderr b/tests/ui/manual_str_repeat.stderr index c65116897164..bdfee7cab261 100644 --- a/tests/ui/manual_str_repeat.stderr +++ b/tests/ui/manual_str_repeat.stderr @@ -1,5 +1,5 @@ error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:10:21 + --> $DIR/manual_str_repeat.rs:9:21 | LL | let _: String = std::iter::repeat("test").take(10).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"test".repeat(10)` @@ -7,55 +7,55 @@ LL | let _: String = std::iter::repeat("test").take(10).collect(); = note: `-D clippy::manual-str-repeat` implied by `-D warnings` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:11:21 + --> $DIR/manual_str_repeat.rs:10:21 | LL | let _: String = std::iter::repeat('x').take(10).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"x".repeat(10)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:12:21 + --> $DIR/manual_str_repeat.rs:11:21 | LL | let _: String = std::iter::repeat('/'').take(10).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"'".repeat(10)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:13:21 + --> $DIR/manual_str_repeat.rs:12:21 | LL | let _: String = std::iter::repeat('"').take(10).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"/"".repeat(10)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:17:13 + --> $DIR/manual_str_repeat.rs:16:13 | LL | let _ = repeat(x).take(count + 2).collect::(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `x.repeat(count + 2)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:26:21 + --> $DIR/manual_str_repeat.rs:25:21 | LL | let _: String = repeat(*x).take(count).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(*x).repeat(count)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:35:21 + --> $DIR/manual_str_repeat.rs:34:21 | LL | let _: String = repeat(x).take(count).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `x.repeat(count)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:47:21 + --> $DIR/manual_str_repeat.rs:46:21 | LL | let _: String = repeat(Cow::Borrowed("test")).take(count).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `Cow::Borrowed("test").repeat(count)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:50:21 + --> $DIR/manual_str_repeat.rs:49:21 | LL | let _: String = repeat(x).take(count).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `x.repeat(count)` error: manual implementation of `str::repeat` using iterators - --> $DIR/manual_str_repeat.rs:65:21 + --> $DIR/manual_str_repeat.rs:64:21 | LL | let _: String = std::iter::repeat("test").take(10).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"test".repeat(10)` diff --git a/tests/ui/manual_strip.rs b/tests/ui/manual_strip.rs index 85009d78558b..b0b1c262aeed 100644 --- a/tests/ui/manual_strip.rs +++ b/tests/ui/manual_strip.rs @@ -1,4 +1,3 @@ -#![feature(custom_inner_attributes)] #![warn(clippy::manual_strip)] fn main() { @@ -66,18 +65,16 @@ fn main() { } } +#[clippy::msrv = "1.44"] fn msrv_1_44() { - #![clippy::msrv = "1.44"] - let s = "abc"; if s.starts_with('a') { s[1..].to_string(); } } +#[clippy::msrv = "1.45"] fn msrv_1_45() { - #![clippy::msrv = "1.45"] - let s = "abc"; if s.starts_with('a') { s[1..].to_string(); diff --git a/tests/ui/manual_strip.stderr b/tests/ui/manual_strip.stderr index ad2a362f3e76..f592e898fc92 100644 --- a/tests/ui/manual_strip.stderr +++ b/tests/ui/manual_strip.stderr @@ -1,11 +1,11 @@ error: stripping a prefix manually - --> $DIR/manual_strip.rs:8:24 + --> $DIR/manual_strip.rs:7:24 | LL | str::to_string(&s["ab".len()..]); | ^^^^^^^^^^^^^^^^ | note: the prefix was tested here - --> $DIR/manual_strip.rs:7:5 + --> $DIR/manual_strip.rs:6:5 | LL | if s.starts_with("ab") { | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -21,13 +21,13 @@ LL ~ .to_string(); | error: stripping a suffix manually - --> $DIR/manual_strip.rs:16:24 + --> $DIR/manual_strip.rs:15:24 | LL | str::to_string(&s[..s.len() - "bc".len()]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the suffix was tested here - --> $DIR/manual_strip.rs:15:5 + --> $DIR/manual_strip.rs:14:5 | LL | if s.ends_with("bc") { | ^^^^^^^^^^^^^^^^^^^^^ @@ -42,13 +42,13 @@ LL ~ .to_string(); | error: stripping a prefix manually - --> $DIR/manual_strip.rs:25:24 + --> $DIR/manual_strip.rs:24:24 | LL | str::to_string(&s[1..]); | ^^^^^^^ | note: the prefix was tested here - --> $DIR/manual_strip.rs:24:5 + --> $DIR/manual_strip.rs:23:5 | LL | if s.starts_with('a') { | ^^^^^^^^^^^^^^^^^^^^^^ @@ -60,13 +60,13 @@ LL ~ .to_string(); | error: stripping a prefix manually - --> $DIR/manual_strip.rs:32:24 + --> $DIR/manual_strip.rs:31:24 | LL | str::to_string(&s[prefix.len()..]); | ^^^^^^^^^^^^^^^^^^ | note: the prefix was tested here - --> $DIR/manual_strip.rs:31:5 + --> $DIR/manual_strip.rs:30:5 | LL | if s.starts_with(prefix) { | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -77,13 +77,13 @@ LL ~ str::to_string(); | error: stripping a prefix manually - --> $DIR/manual_strip.rs:38:24 + --> $DIR/manual_strip.rs:37:24 | LL | str::to_string(&s[PREFIX.len()..]); | ^^^^^^^^^^^^^^^^^^ | note: the prefix was tested here - --> $DIR/manual_strip.rs:37:5 + --> $DIR/manual_strip.rs:36:5 | LL | if s.starts_with(PREFIX) { | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -95,13 +95,13 @@ LL ~ str::to_string(); | error: stripping a prefix manually - --> $DIR/manual_strip.rs:45:24 + --> $DIR/manual_strip.rs:44:24 | LL | str::to_string(&TARGET[prefix.len()..]); | ^^^^^^^^^^^^^^^^^^^^^^^ | note: the prefix was tested here - --> $DIR/manual_strip.rs:44:5 + --> $DIR/manual_strip.rs:43:5 | LL | if TARGET.starts_with(prefix) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -112,13 +112,13 @@ LL ~ str::to_string(); | error: stripping a prefix manually - --> $DIR/manual_strip.rs:51:9 + --> $DIR/manual_strip.rs:50:9 | LL | s1[2..].to_uppercase(); | ^^^^^^^ | note: the prefix was tested here - --> $DIR/manual_strip.rs:50:5 + --> $DIR/manual_strip.rs:49:5 | LL | if s1.starts_with("ab") { | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -129,13 +129,13 @@ LL ~ .to_uppercase(); | error: stripping a prefix manually - --> $DIR/manual_strip.rs:83:9 + --> $DIR/manual_strip.rs:80:9 | LL | s[1..].to_string(); | ^^^^^^ | note: the prefix was tested here - --> $DIR/manual_strip.rs:82:5 + --> $DIR/manual_strip.rs:79:5 | LL | if s.starts_with('a') { | ^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/map_unwrap_or.rs b/tests/ui/map_unwrap_or.rs index 396b22a9abb3..32631024ca5d 100644 --- a/tests/ui/map_unwrap_or.rs +++ b/tests/ui/map_unwrap_or.rs @@ -1,6 +1,5 @@ // aux-build:option_helpers.rs -#![feature(custom_inner_attributes)] #![warn(clippy::map_unwrap_or)] #![allow(clippy::uninlined_format_args, clippy::unnecessary_lazy_evaluations)] @@ -82,17 +81,15 @@ fn main() { result_methods(); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let res: Result = Ok(1); let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); } +#[clippy::msrv = "1.41"] fn msrv_1_41() { - #![clippy::msrv = "1.41"] - let res: Result = Ok(1); let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); diff --git a/tests/ui/map_unwrap_or.stderr b/tests/ui/map_unwrap_or.stderr index d17d24a403ea..41781b050fa2 100644 --- a/tests/ui/map_unwrap_or.stderr +++ b/tests/ui/map_unwrap_or.stderr @@ -1,5 +1,5 @@ error: called `map().unwrap_or()` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:18:13 + --> $DIR/map_unwrap_or.rs:17:13 | LL | let _ = opt.map(|x| x + 1) | _____________^ @@ -15,7 +15,7 @@ LL + let _ = opt.map_or(0, |x| x + 1); | error: called `map().unwrap_or()` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:22:13 + --> $DIR/map_unwrap_or.rs:21:13 | LL | let _ = opt.map(|x| { | _____________^ @@ -33,7 +33,7 @@ LL ~ ); | error: called `map().unwrap_or()` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:26:13 + --> $DIR/map_unwrap_or.rs:25:13 | LL | let _ = opt.map(|x| x + 1) | _____________^ @@ -50,7 +50,7 @@ LL ~ }, |x| x + 1); | error: called `map().unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then()` instead - --> $DIR/map_unwrap_or.rs:31:13 + --> $DIR/map_unwrap_or.rs:30:13 | LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -62,7 +62,7 @@ LL + let _ = opt.and_then(|x| Some(x + 1)); | error: called `map().unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then()` instead - --> $DIR/map_unwrap_or.rs:33:13 + --> $DIR/map_unwrap_or.rs:32:13 | LL | let _ = opt.map(|x| { | _____________^ @@ -80,7 +80,7 @@ LL ~ ); | error: called `map().unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then()` instead - --> $DIR/map_unwrap_or.rs:37:13 + --> $DIR/map_unwrap_or.rs:36:13 | LL | let _ = opt | _____________^ @@ -95,7 +95,7 @@ LL + .and_then(|x| Some(x + 1)); | error: called `map().unwrap_or()` on an `Option` value. This can be done more directly by calling `map_or(, )` instead - --> $DIR/map_unwrap_or.rs:48:13 + --> $DIR/map_unwrap_or.rs:47:13 | LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -107,7 +107,7 @@ LL + let _ = Some("prefix").map_or(id, |p| format!("{}.", p)); | error: called `map().unwrap_or_else()` on an `Option` value. This can be done more directly by calling `map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:52:13 + --> $DIR/map_unwrap_or.rs:51:13 | LL | let _ = opt.map(|x| { | _____________^ @@ -117,7 +117,7 @@ LL | | ).unwrap_or_else(|| 0); | |__________________________^ error: called `map().unwrap_or_else()` on an `Option` value. This can be done more directly by calling `map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:56:13 + --> $DIR/map_unwrap_or.rs:55:13 | LL | let _ = opt.map(|x| x + 1) | _____________^ @@ -127,7 +127,7 @@ LL | | ); | |_________^ error: called `map().unwrap_or_else()` on a `Result` value. This can be done more directly by calling `.map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:68:13 + --> $DIR/map_unwrap_or.rs:67:13 | LL | let _ = res.map(|x| { | _____________^ @@ -137,7 +137,7 @@ LL | | ).unwrap_or_else(|_e| 0); | |____________________________^ error: called `map().unwrap_or_else()` on a `Result` value. This can be done more directly by calling `.map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:72:13 + --> $DIR/map_unwrap_or.rs:71:13 | LL | let _ = res.map(|x| x + 1) | _____________^ @@ -147,7 +147,7 @@ LL | | }); | |__________^ error: called `map().unwrap_or_else()` on a `Result` value. This can be done more directly by calling `.map_or_else(, )` instead - --> $DIR/map_unwrap_or.rs:98:13 + --> $DIR/map_unwrap_or.rs:95:13 | LL | let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)` diff --git a/tests/ui/match_expr_like_matches_macro.fixed b/tests/ui/match_expr_like_matches_macro.fixed index 968f462f8a02..55cd15bd5c38 100644 --- a/tests/ui/match_expr_like_matches_macro.fixed +++ b/tests/ui/match_expr_like_matches_macro.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::match_like_matches_macro)] #![allow( unreachable_patterns, @@ -200,17 +199,15 @@ fn main() { }; } +#[clippy::msrv = "1.41"] fn msrv_1_41() { - #![clippy::msrv = "1.41"] - let _y = match Some(5) { Some(0) => true, _ => false, }; } +#[clippy::msrv = "1.42"] fn msrv_1_42() { - #![clippy::msrv = "1.42"] - let _y = matches!(Some(5), Some(0)); } diff --git a/tests/ui/match_expr_like_matches_macro.rs b/tests/ui/match_expr_like_matches_macro.rs index c6b479e27c5a..5d645e108e51 100644 --- a/tests/ui/match_expr_like_matches_macro.rs +++ b/tests/ui/match_expr_like_matches_macro.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::match_like_matches_macro)] #![allow( unreachable_patterns, @@ -241,18 +240,16 @@ fn main() { }; } +#[clippy::msrv = "1.41"] fn msrv_1_41() { - #![clippy::msrv = "1.41"] - let _y = match Some(5) { Some(0) => true, _ => false, }; } +#[clippy::msrv = "1.42"] fn msrv_1_42() { - #![clippy::msrv = "1.42"] - let _y = match Some(5) { Some(0) => true, _ => false, diff --git a/tests/ui/match_expr_like_matches_macro.stderr b/tests/ui/match_expr_like_matches_macro.stderr index a4df8008ac23..46f67ef4900f 100644 --- a/tests/ui/match_expr_like_matches_macro.stderr +++ b/tests/ui/match_expr_like_matches_macro.stderr @@ -1,5 +1,5 @@ error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:16:14 + --> $DIR/match_expr_like_matches_macro.rs:15:14 | LL | let _y = match x { | ______________^ @@ -11,7 +11,7 @@ LL | | }; = note: `-D clippy::match-like-matches-macro` implied by `-D warnings` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:22:14 + --> $DIR/match_expr_like_matches_macro.rs:21:14 | LL | let _w = match x { | ______________^ @@ -21,7 +21,7 @@ LL | | }; | |_____^ help: try this: `matches!(x, Some(_))` error: redundant pattern matching, consider using `is_none()` - --> $DIR/match_expr_like_matches_macro.rs:28:14 + --> $DIR/match_expr_like_matches_macro.rs:27:14 | LL | let _z = match x { | ______________^ @@ -33,7 +33,7 @@ LL | | }; = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:34:15 + --> $DIR/match_expr_like_matches_macro.rs:33:15 | LL | let _zz = match x { | _______________^ @@ -43,13 +43,13 @@ LL | | }; | |_____^ help: try this: `!matches!(x, Some(r) if r == 0)` error: if let .. else expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:40:16 + --> $DIR/match_expr_like_matches_macro.rs:39:16 | LL | let _zzz = if let Some(5) = x { true } else { false }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `matches!(x, Some(5))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:64:20 + --> $DIR/match_expr_like_matches_macro.rs:63:20 | LL | let _ans = match x { | ____________________^ @@ -60,7 +60,7 @@ LL | | }; | |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:74:20 + --> $DIR/match_expr_like_matches_macro.rs:73:20 | LL | let _ans = match x { | ____________________^ @@ -73,7 +73,7 @@ LL | | }; | |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:84:20 + --> $DIR/match_expr_like_matches_macro.rs:83:20 | LL | let _ans = match x { | ____________________^ @@ -84,7 +84,7 @@ LL | | }; | |_________^ help: try this: `!matches!(x, E::B(_) | E::C)` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:144:18 + --> $DIR/match_expr_like_matches_macro.rs:143:18 | LL | let _z = match &z { | __________________^ @@ -94,7 +94,7 @@ LL | | }; | |_________^ help: try this: `matches!(z, Some(3))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:153:18 + --> $DIR/match_expr_like_matches_macro.rs:152:18 | LL | let _z = match &z { | __________________^ @@ -104,7 +104,7 @@ LL | | }; | |_________^ help: try this: `matches!(&z, Some(3))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:170:21 + --> $DIR/match_expr_like_matches_macro.rs:169:21 | LL | let _ = match &z { | _____________________^ @@ -114,7 +114,7 @@ LL | | }; | |_____________^ help: try this: `matches!(&z, AnEnum::X)` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:184:20 + --> $DIR/match_expr_like_matches_macro.rs:183:20 | LL | let _res = match &val { | ____________________^ @@ -124,7 +124,7 @@ LL | | }; | |_________^ help: try this: `matches!(&val, &Some(ref _a))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:196:20 + --> $DIR/match_expr_like_matches_macro.rs:195:20 | LL | let _res = match &val { | ____________________^ @@ -134,7 +134,7 @@ LL | | }; | |_________^ help: try this: `matches!(&val, &Some(ref _a))` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:256:14 + --> $DIR/match_expr_like_matches_macro.rs:253:14 | LL | let _y = match Some(5) { | ______________^ diff --git a/tests/ui/mem_replace.fixed b/tests/ui/mem_replace.fixed index ae237395b95f..874d55843303 100644 --- a/tests/ui/mem_replace.fixed +++ b/tests/ui/mem_replace.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #![warn( clippy::all, @@ -80,16 +79,14 @@ fn main() { dont_lint_primitive(); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let mut s = String::from("foo"); let _ = std::mem::take(&mut s); } diff --git a/tests/ui/mem_replace.rs b/tests/ui/mem_replace.rs index 3202e99e0be9..f4f3bff51446 100644 --- a/tests/ui/mem_replace.rs +++ b/tests/ui/mem_replace.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #![warn( clippy::all, @@ -80,16 +79,14 @@ fn main() { dont_lint_primitive(); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); } diff --git a/tests/ui/mem_replace.stderr b/tests/ui/mem_replace.stderr index dd8a50dab900..caa127f76eef 100644 --- a/tests/ui/mem_replace.stderr +++ b/tests/ui/mem_replace.stderr @@ -1,5 +1,5 @@ error: replacing an `Option` with `None` - --> $DIR/mem_replace.rs:17:13 + --> $DIR/mem_replace.rs:16:13 | LL | let _ = mem::replace(&mut an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` @@ -7,13 +7,13 @@ LL | let _ = mem::replace(&mut an_option, None); = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` error: replacing an `Option` with `None` - --> $DIR/mem_replace.rs:19:13 + --> $DIR/mem_replace.rs:18:13 | LL | let _ = mem::replace(an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:24:13 + --> $DIR/mem_replace.rs:23:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)` @@ -21,103 +21,103 @@ LL | let _ = std::mem::replace(&mut s, String::default()); = note: `-D clippy::mem-replace-with-default` implied by `-D warnings` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:27:13 + --> $DIR/mem_replace.rs:26:13 | LL | let _ = std::mem::replace(s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:28:13 + --> $DIR/mem_replace.rs:27:13 | LL | let _ = std::mem::replace(s, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:31:13 + --> $DIR/mem_replace.rs:30:13 | LL | let _ = std::mem::replace(&mut v, Vec::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:32:13 + --> $DIR/mem_replace.rs:31:13 | LL | let _ = std::mem::replace(&mut v, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:33:13 + --> $DIR/mem_replace.rs:32:13 | LL | let _ = std::mem::replace(&mut v, Vec::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:34:13 + --> $DIR/mem_replace.rs:33:13 | LL | let _ = std::mem::replace(&mut v, vec![]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:37:13 + --> $DIR/mem_replace.rs:36:13 | LL | let _ = std::mem::replace(&mut hash_map, HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut hash_map)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:40:13 + --> $DIR/mem_replace.rs:39:13 | LL | let _ = std::mem::replace(&mut btree_map, BTreeMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut btree_map)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:43:13 + --> $DIR/mem_replace.rs:42:13 | LL | let _ = std::mem::replace(&mut vd, VecDeque::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut vd)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:46:13 + --> $DIR/mem_replace.rs:45:13 | LL | let _ = std::mem::replace(&mut hash_set, HashSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut hash_set)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:49:13 + --> $DIR/mem_replace.rs:48:13 | LL | let _ = std::mem::replace(&mut btree_set, BTreeSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut btree_set)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:52:13 + --> $DIR/mem_replace.rs:51:13 | LL | let _ = std::mem::replace(&mut list, LinkedList::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut list)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:55:13 + --> $DIR/mem_replace.rs:54:13 | LL | let _ = std::mem::replace(&mut binary_heap, BinaryHeap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut binary_heap)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:58:13 + --> $DIR/mem_replace.rs:57:13 | LL | let _ = std::mem::replace(&mut tuple, (vec![], BinaryHeap::new())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut tuple)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:61:13 + --> $DIR/mem_replace.rs:60:13 | LL | let _ = std::mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut refstr)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:64:13 + --> $DIR/mem_replace.rs:63:13 | LL | let _ = std::mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut slice)` error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` - --> $DIR/mem_replace.rs:94:13 + --> $DIR/mem_replace.rs:91:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)` diff --git a/tests/ui/min_rust_version_attr.rs b/tests/ui/min_rust_version_attr.rs index cd148063bf06..955e7eb72763 100644 --- a/tests/ui/min_rust_version_attr.rs +++ b/tests/ui/min_rust_version_attr.rs @@ -3,27 +3,60 @@ fn main() {} +#[clippy::msrv = "1.42.0"] fn just_under_msrv() { - #![clippy::msrv = "1.42.0"] let log2_10 = 3.321928094887362; } +#[clippy::msrv = "1.43.0"] fn meets_msrv() { - #![clippy::msrv = "1.43.0"] let log2_10 = 3.321928094887362; } +#[clippy::msrv = "1.44.0"] fn just_above_msrv() { - #![clippy::msrv = "1.44.0"] let log2_10 = 3.321928094887362; } +#[clippy::msrv = "1.42"] fn no_patch_under() { + let log2_10 = 3.321928094887362; +} + +#[clippy::msrv = "1.43"] +fn no_patch_meets() { + let log2_10 = 3.321928094887362; +} + +fn inner_attr_under() { #![clippy::msrv = "1.42"] let log2_10 = 3.321928094887362; } -fn no_patch_meets() { +fn inner_attr_meets() { #![clippy::msrv = "1.43"] let log2_10 = 3.321928094887362; } + +// https://github.com/rust-lang/rust-clippy/issues/6920 +fn scoping() { + mod m { + #![clippy::msrv = "1.42.0"] + } + + // Should warn + let log2_10 = 3.321928094887362; + + mod a { + #![clippy::msrv = "1.42.0"] + + fn should_warn() { + #![clippy::msrv = "1.43.0"] + let log2_10 = 3.321928094887362; + } + + fn should_not_warn() { + let log2_10 = 3.321928094887362; + } + } +} diff --git a/tests/ui/min_rust_version_attr.stderr b/tests/ui/min_rust_version_attr.stderr index 68aa58748190..7e2135584efd 100644 --- a/tests/ui/min_rust_version_attr.stderr +++ b/tests/ui/min_rust_version_attr.stderr @@ -23,5 +23,29 @@ LL | let log2_10 = 3.321928094887362; | = help: consider using the constant directly -error: aborting due to 3 previous errors +error: approximate value of `f{32, 64}::consts::LOG2_10` found + --> $DIR/min_rust_version_attr.rs:38:19 + | +LL | let log2_10 = 3.321928094887362; + | ^^^^^^^^^^^^^^^^^ + | + = help: consider using the constant directly + +error: approximate value of `f{32, 64}::consts::LOG2_10` found + --> $DIR/min_rust_version_attr.rs:48:19 + | +LL | let log2_10 = 3.321928094887362; + | ^^^^^^^^^^^^^^^^^ + | + = help: consider using the constant directly + +error: approximate value of `f{32, 64}::consts::LOG2_10` found + --> $DIR/min_rust_version_attr.rs:55:27 + | +LL | let log2_10 = 3.321928094887362; + | ^^^^^^^^^^^^^^^^^ + | + = help: consider using the constant directly + +error: aborting due to 6 previous errors diff --git a/tests/ui/min_rust_version_invalid_attr.stderr b/tests/ui/min_rust_version_invalid_attr.stderr index 93370a0fa9c9..675b78031525 100644 --- a/tests/ui/min_rust_version_invalid_attr.stderr +++ b/tests/ui/min_rust_version_invalid_attr.stderr @@ -4,7 +4,7 @@ error: `invalid.version` is not a valid Rust version LL | #![clippy::msrv = "invalid.version"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: `msrv` cannot be an outer attribute +error: `invalid.version` is not a valid Rust version --> $DIR/min_rust_version_invalid_attr.rs:6:1 | LL | #[clippy::msrv = "invalid.version"] diff --git a/tests/ui/misnamed_getters.rs b/tests/ui/misnamed_getters.rs new file mode 100644 index 000000000000..03e7dac7df94 --- /dev/null +++ b/tests/ui/misnamed_getters.rs @@ -0,0 +1,124 @@ +#![allow(unused)] +#![warn(clippy::misnamed_getters)] + +struct A { + a: u8, + b: u8, + c: u8, +} + +impl A { + fn a(&self) -> &u8 { + &self.b + } + fn a_mut(&mut self) -> &mut u8 { + &mut self.b + } + + fn b(self) -> u8 { + self.a + } + + fn b_mut(&mut self) -> &mut u8 { + &mut self.a + } + + fn c(&self) -> &u8 { + &self.b + } + + fn c_mut(&mut self) -> &mut u8 { + &mut self.a + } +} + +union B { + a: u8, + b: u8, +} + +impl B { + unsafe fn a(&self) -> &u8 { + &self.b + } + unsafe fn a_mut(&mut self) -> &mut u8 { + &mut self.b + } + + unsafe fn b(self) -> u8 { + self.a + } + + unsafe fn b_mut(&mut self) -> &mut u8 { + &mut self.a + } + + unsafe fn c(&self) -> &u8 { + &self.b + } + + unsafe fn c_mut(&mut self) -> &mut u8 { + &mut self.a + } + + unsafe fn a_unchecked(&self) -> &u8 { + &self.b + } + unsafe fn a_unchecked_mut(&mut self) -> &mut u8 { + &mut self.b + } + + unsafe fn b_unchecked(self) -> u8 { + self.a + } + + unsafe fn b_unchecked_mut(&mut self) -> &mut u8 { + &mut self.a + } + + unsafe fn c_unchecked(&self) -> &u8 { + &self.b + } + + unsafe fn c_unchecked_mut(&mut self) -> &mut u8 { + &mut self.a + } +} + +struct D { + d: u8, + inner: A, +} + +impl core::ops::Deref for D { + type Target = A; + fn deref(&self) -> &A { + &self.inner + } +} + +impl core::ops::DerefMut for D { + fn deref_mut(&mut self) -> &mut A { + &mut self.inner + } +} + +impl D { + fn a(&self) -> &u8 { + &self.b + } + fn a_mut(&mut self) -> &mut u8 { + &mut self.b + } + + fn d(&self) -> &u8 { + &self.b + } + fn d_mut(&mut self) -> &mut u8 { + &mut self.b + } +} + +fn main() { + // test code goes here +} diff --git a/tests/ui/misnamed_getters.stderr b/tests/ui/misnamed_getters.stderr new file mode 100644 index 000000000000..1e38a83d019a --- /dev/null +++ b/tests/ui/misnamed_getters.stderr @@ -0,0 +1,166 @@ +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:11:5 + | +LL | / fn a(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` +LL | | } + | |_____^ + | + = note: `-D clippy::misnamed-getters` implied by `-D warnings` + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:14:5 + | +LL | / fn a_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:18:5 + | +LL | / fn b(self) -> u8 { +LL | | self.a + | | ------ help: consider using: `self.b` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:22:5 + | +LL | / fn b_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.b` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:26:5 + | +LL | / fn c(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.c` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:30:5 + | +LL | / fn c_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.c` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:41:5 + | +LL | / unsafe fn a(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:44:5 + | +LL | / unsafe fn a_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:48:5 + | +LL | / unsafe fn b(self) -> u8 { +LL | | self.a + | | ------ help: consider using: `self.b` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:52:5 + | +LL | / unsafe fn b_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.b` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:64:5 + | +LL | / unsafe fn a_unchecked(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:67:5 + | +LL | / unsafe fn a_unchecked_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:71:5 + | +LL | / unsafe fn b_unchecked(self) -> u8 { +LL | | self.a + | | ------ help: consider using: `self.b` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:75:5 + | +LL | / unsafe fn b_unchecked_mut(&mut self) -> &mut u8 { +LL | | &mut self.a + | | ----------- help: consider using: `&mut self.b` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:107:5 + | +LL | / fn a(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:110:5 + | +LL | / fn a_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.a` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:114:5 + | +LL | / fn d(&self) -> &u8 { +LL | | &self.b + | | ------- help: consider using: `&self.d` +LL | | } + | |_____^ + +error: getter function appears to return the wrong field + --> $DIR/misnamed_getters.rs:117:5 + | +LL | / fn d_mut(&mut self) -> &mut u8 { +LL | | &mut self.b + | | ----------- help: consider using: `&mut self.d` +LL | | } + | |_____^ + +error: aborting due to 18 previous errors + diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs index b950248ef942..75cace181675 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -7,7 +7,6 @@ #![warn(clippy::missing_const_for_fn)] #![feature(start)] -#![feature(custom_inner_attributes)] extern crate helper; extern crate proc_macro_with_span; @@ -115,9 +114,8 @@ fn unstably_const_fn() { helper::unstably_const_fn() } +#[clippy::msrv = "1.46.0"] mod const_fn_stabilized_after_msrv { - #![clippy::msrv = "1.46.0"] - // Do not lint this because `u8::is_ascii_digit` is stabilized as a const function in 1.47.0. fn const_fn_stabilized_after_msrv(byte: u8) { byte.is_ascii_digit(); diff --git a/tests/ui/missing_const_for_fn/could_be_const.rs b/tests/ui/missing_const_for_fn/could_be_const.rs index b85e88784918..0246c8622ed3 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.rs +++ b/tests/ui/missing_const_for_fn/could_be_const.rs @@ -1,6 +1,5 @@ #![warn(clippy::missing_const_for_fn)] #![allow(incomplete_features, clippy::let_and_return)] -#![feature(custom_inner_attributes)] use std::mem::transmute; @@ -68,24 +67,21 @@ mod with_drop { } } +#[clippy::msrv = "1.47.0"] mod const_fn_stabilized_before_msrv { - #![clippy::msrv = "1.47.0"] - // This could be const because `u8::is_ascii_digit` is a stable const function in 1.47. fn const_fn_stabilized_before_msrv(byte: u8) { byte.is_ascii_digit(); } } +#[clippy::msrv = "1.45"] fn msrv_1_45() -> i32 { - #![clippy::msrv = "1.45"] - 45 } +#[clippy::msrv = "1.46"] fn msrv_1_46() -> i32 { - #![clippy::msrv = "1.46"] - 46 } diff --git a/tests/ui/missing_const_for_fn/could_be_const.stderr b/tests/ui/missing_const_for_fn/could_be_const.stderr index f8e221c82f1a..955e1ed26340 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.stderr +++ b/tests/ui/missing_const_for_fn/could_be_const.stderr @@ -1,5 +1,5 @@ error: this could be a `const fn` - --> $DIR/could_be_const.rs:13:5 + --> $DIR/could_be_const.rs:12:5 | LL | / pub fn new() -> Self { LL | | Self { guess: 42 } @@ -9,7 +9,7 @@ LL | | } = note: `-D clippy::missing-const-for-fn` implied by `-D warnings` error: this could be a `const fn` - --> $DIR/could_be_const.rs:17:5 + --> $DIR/could_be_const.rs:16:5 | LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] { LL | | b @@ -17,7 +17,7 @@ LL | | } | |_____^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:23:1 + --> $DIR/could_be_const.rs:22:1 | LL | / fn one() -> i32 { LL | | 1 @@ -25,7 +25,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:28:1 + --> $DIR/could_be_const.rs:27:1 | LL | / fn two() -> i32 { LL | | let abc = 2; @@ -34,7 +34,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:34:1 + --> $DIR/could_be_const.rs:33:1 | LL | / fn string() -> String { LL | | String::new() @@ -42,7 +42,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:39:1 + --> $DIR/could_be_const.rs:38:1 | LL | / unsafe fn four() -> i32 { LL | | 4 @@ -50,7 +50,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:44:1 + --> $DIR/could_be_const.rs:43:1 | LL | / fn generic(t: T) -> T { LL | | t @@ -58,7 +58,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:52:1 + --> $DIR/could_be_const.rs:51:1 | LL | / fn generic_arr(t: [T; 1]) -> T { LL | | t[0] @@ -66,7 +66,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:65:9 + --> $DIR/could_be_const.rs:64:9 | LL | / pub fn b(self, a: &A) -> B { LL | | B @@ -74,7 +74,7 @@ LL | | } | |_________^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:75:5 + --> $DIR/could_be_const.rs:73:5 | LL | / fn const_fn_stabilized_before_msrv(byte: u8) { LL | | byte.is_ascii_digit(); @@ -82,11 +82,9 @@ LL | | } | |_____^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:86:1 + --> $DIR/could_be_const.rs:84:1 | LL | / fn msrv_1_46() -> i32 { -LL | | #![clippy::msrv = "1.46"] -LL | | LL | | 46 LL | | } | |_^ diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed index 85b6b639d554..4cb7f6b687f1 100644 --- a/tests/ui/needless_borrow.fixed +++ b/tests/ui/needless_borrow.fixed @@ -1,13 +1,13 @@ // run-rustfix -#![feature(custom_inner_attributes, lint_reasons)] - -#[warn(clippy::all, clippy::needless_borrow)] -#[allow(unused_variables)] -#[allow( +#![feature(lint_reasons)] +#![allow( + unused, clippy::uninlined_format_args, clippy::unnecessary_mut_passed, clippy::unnecessary_to_owned )] +#![warn(clippy::needless_borrow)] + fn main() { let a = 5; let ref_a = &a; @@ -171,14 +171,12 @@ impl<'a> Trait for &'a str {} fn h(_: &dyn Trait) {} -#[allow(dead_code)] fn check_expect_suppression() { let a = 5; #[expect(clippy::needless_borrow)] let _ = x(&&a); } -#[allow(dead_code)] mod issue9160 { pub struct S { f: F, @@ -267,7 +265,6 @@ where } // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321 -#[allow(dead_code)] mod copyable_iterator { #[derive(Clone, Copy)] struct Iter; @@ -287,25 +284,20 @@ mod copyable_iterator { } } +#[clippy::msrv = "1.52.0"] mod under_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.52.0"] - fn foo() { let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); } } +#[clippy::msrv = "1.53.0"] mod meets_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.53.0"] - fn foo() { let _ = std::process::Command::new("ls").args(["-a", "-l"]).status().unwrap(); } } -#[allow(unused)] fn issue9383() { // Should not lint because unions need explicit deref when accessing field use std::mem::ManuallyDrop; @@ -334,7 +326,6 @@ fn issue9383() { } } -#[allow(dead_code)] fn closure_test() { let env = "env".to_owned(); let arg = "arg".to_owned(); @@ -348,7 +339,6 @@ fn closure_test() { f(arg); } -#[allow(dead_code)] mod significant_drop { #[derive(Debug)] struct X; @@ -368,7 +358,6 @@ mod significant_drop { fn debug(_: impl std::fmt::Debug) {} } -#[allow(dead_code)] mod used_exactly_once { fn foo(x: String) { use_x(x); @@ -376,7 +365,6 @@ mod used_exactly_once { fn use_x(_: impl AsRef) {} } -#[allow(dead_code)] mod used_more_than_once { fn foo(x: String) { use_x(&x); @@ -387,7 +375,6 @@ mod used_more_than_once { } // https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280 -#[allow(dead_code)] mod issue_9111 { struct A; @@ -409,7 +396,6 @@ mod issue_9111 { } } -#[allow(dead_code)] mod issue_9710 { fn main() { let string = String::new(); @@ -421,7 +407,6 @@ mod issue_9710 { fn f>(_: T) {} } -#[allow(dead_code)] mod issue_9739 { fn foo(_it: impl IntoIterator) {} @@ -434,7 +419,6 @@ mod issue_9739 { } } -#[allow(dead_code)] mod issue_9739_method_variant { struct S; @@ -451,7 +435,6 @@ mod issue_9739_method_variant { } } -#[allow(dead_code)] mod issue_9782 { fn foo>(t: T) { println!("{}", std::mem::size_of::()); @@ -475,7 +458,6 @@ mod issue_9782 { } } -#[allow(dead_code)] mod issue_9782_type_relative_variant { struct S; @@ -493,7 +475,6 @@ mod issue_9782_type_relative_variant { } } -#[allow(dead_code)] mod issue_9782_method_variant { struct S; diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs index 7b97bcf3817e..9a01190ed8db 100644 --- a/tests/ui/needless_borrow.rs +++ b/tests/ui/needless_borrow.rs @@ -1,13 +1,13 @@ // run-rustfix -#![feature(custom_inner_attributes, lint_reasons)] - -#[warn(clippy::all, clippy::needless_borrow)] -#[allow(unused_variables)] -#[allow( +#![feature(lint_reasons)] +#![allow( + unused, clippy::uninlined_format_args, clippy::unnecessary_mut_passed, clippy::unnecessary_to_owned )] +#![warn(clippy::needless_borrow)] + fn main() { let a = 5; let ref_a = &a; @@ -171,14 +171,12 @@ impl<'a> Trait for &'a str {} fn h(_: &dyn Trait) {} -#[allow(dead_code)] fn check_expect_suppression() { let a = 5; #[expect(clippy::needless_borrow)] let _ = x(&&a); } -#[allow(dead_code)] mod issue9160 { pub struct S { f: F, @@ -267,7 +265,6 @@ where } // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321 -#[allow(dead_code)] mod copyable_iterator { #[derive(Clone, Copy)] struct Iter; @@ -287,25 +284,20 @@ mod copyable_iterator { } } +#[clippy::msrv = "1.52.0"] mod under_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.52.0"] - fn foo() { let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); } } +#[clippy::msrv = "1.53.0"] mod meets_msrv { - #![allow(dead_code)] - #![clippy::msrv = "1.53.0"] - fn foo() { let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); } } -#[allow(unused)] fn issue9383() { // Should not lint because unions need explicit deref when accessing field use std::mem::ManuallyDrop; @@ -334,7 +326,6 @@ fn issue9383() { } } -#[allow(dead_code)] fn closure_test() { let env = "env".to_owned(); let arg = "arg".to_owned(); @@ -348,7 +339,6 @@ fn closure_test() { f(arg); } -#[allow(dead_code)] mod significant_drop { #[derive(Debug)] struct X; @@ -368,7 +358,6 @@ mod significant_drop { fn debug(_: impl std::fmt::Debug) {} } -#[allow(dead_code)] mod used_exactly_once { fn foo(x: String) { use_x(&x); @@ -376,7 +365,6 @@ mod used_exactly_once { fn use_x(_: impl AsRef) {} } -#[allow(dead_code)] mod used_more_than_once { fn foo(x: String) { use_x(&x); @@ -387,7 +375,6 @@ mod used_more_than_once { } // https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280 -#[allow(dead_code)] mod issue_9111 { struct A; @@ -409,7 +396,6 @@ mod issue_9111 { } } -#[allow(dead_code)] mod issue_9710 { fn main() { let string = String::new(); @@ -421,7 +407,6 @@ mod issue_9710 { fn f>(_: T) {} } -#[allow(dead_code)] mod issue_9739 { fn foo(_it: impl IntoIterator) {} @@ -434,7 +419,6 @@ mod issue_9739 { } } -#[allow(dead_code)] mod issue_9739_method_variant { struct S; @@ -451,7 +435,6 @@ mod issue_9739_method_variant { } } -#[allow(dead_code)] mod issue_9782 { fn foo>(t: T) { println!("{}", std::mem::size_of::()); @@ -475,7 +458,6 @@ mod issue_9782 { } } -#[allow(dead_code)] mod issue_9782_type_relative_variant { struct S; @@ -493,7 +475,6 @@ mod issue_9782_type_relative_variant { } } -#[allow(dead_code)] mod issue_9782_method_variant { struct S; diff --git a/tests/ui/needless_borrow.stderr b/tests/ui/needless_borrow.stderr index 485e6b84c868..d26c317124b8 100644 --- a/tests/ui/needless_borrow.stderr +++ b/tests/ui/needless_borrow.stderr @@ -163,55 +163,55 @@ LL | let _ = std::fs::write("x", &"".to_string()); | ^^^^^^^^^^^^^^^ help: change this to: `"".to_string()` error: this expression borrows a value the compiler would automatically borrow - --> $DIR/needless_borrow.rs:192:13 + --> $DIR/needless_borrow.rs:190:13 | LL | (&self.f)() | ^^^^^^^^^ help: change this to: `(self.f)` error: this expression borrows a value the compiler would automatically borrow - --> $DIR/needless_borrow.rs:201:13 + --> $DIR/needless_borrow.rs:199:13 | LL | (&mut self.f)() | ^^^^^^^^^^^^^ help: change this to: `(self.f)` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:286:20 + --> $DIR/needless_borrow.rs:283:20 | LL | takes_iter(&mut x) | ^^^^^^ help: change this to: `x` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:304:55 + --> $DIR/needless_borrow.rs:297:55 | LL | let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap(); | ^^^^^^^^^^^^^ help: change this to: `["-a", "-l"]` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:344:37 + --> $DIR/needless_borrow.rs:335:37 | LL | let _ = std::fs::write("x", &arg); | ^^^^ help: change this to: `arg` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:345:37 + --> $DIR/needless_borrow.rs:336:37 | LL | let _ = std::fs::write("x", &loc); | ^^^^ help: change this to: `loc` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:364:15 + --> $DIR/needless_borrow.rs:354:15 | LL | debug(&x); | ^^ help: change this to: `x` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:374:15 + --> $DIR/needless_borrow.rs:363:15 | LL | use_x(&x); | ^^ help: change this to: `x` error: the borrowed expression implements the required traits - --> $DIR/needless_borrow.rs:474:13 + --> $DIR/needless_borrow.rs:457:13 | LL | foo(&a); | ^^ help: change this to: `a` diff --git a/tests/ui/needless_question_mark.fixed b/tests/ui/needless_question_mark.fixed index ba9d15e59d0e..7eaca571992f 100644 --- a/tests/ui/needless_question_mark.fixed +++ b/tests/ui/needless_question_mark.fixed @@ -8,7 +8,6 @@ dead_code, unused_must_use )] -#![feature(custom_inner_attributes)] struct TO { magic: Option, diff --git a/tests/ui/needless_question_mark.rs b/tests/ui/needless_question_mark.rs index 3a6523e8fe87..960bc7b78983 100644 --- a/tests/ui/needless_question_mark.rs +++ b/tests/ui/needless_question_mark.rs @@ -8,7 +8,6 @@ dead_code, unused_must_use )] -#![feature(custom_inner_attributes)] struct TO { magic: Option, diff --git a/tests/ui/needless_question_mark.stderr b/tests/ui/needless_question_mark.stderr index f8308e24e771..d1f89e326c67 100644 --- a/tests/ui/needless_question_mark.stderr +++ b/tests/ui/needless_question_mark.stderr @@ -1,5 +1,5 @@ error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:23:12 + --> $DIR/needless_question_mark.rs:22:12 | LL | return Some(to.magic?); | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` @@ -7,67 +7,67 @@ LL | return Some(to.magic?); = note: `-D clippy::needless-question-mark` implied by `-D warnings` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:31:12 + --> $DIR/needless_question_mark.rs:30:12 | LL | return Some(to.magic?) | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:36:5 + --> $DIR/needless_question_mark.rs:35:5 | LL | Some(to.magic?) | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:41:21 + --> $DIR/needless_question_mark.rs:40:21 | LL | to.and_then(|t| Some(t.magic?)) | ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:50:9 + --> $DIR/needless_question_mark.rs:49:9 | LL | Some(t.magic?) | ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:55:12 + --> $DIR/needless_question_mark.rs:54:12 | LL | return Ok(tr.magic?); | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:62:12 + --> $DIR/needless_question_mark.rs:61:12 | LL | return Ok(tr.magic?) | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:66:5 + --> $DIR/needless_question_mark.rs:65:5 | LL | Ok(tr.magic?) | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:70:21 + --> $DIR/needless_question_mark.rs:69:21 | LL | tr.and_then(|t| Ok(t.magic?)) | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:78:9 + --> $DIR/needless_question_mark.rs:77:9 | LL | Ok(t.magic?) | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:85:16 + --> $DIR/needless_question_mark.rs:84:16 | LL | return Ok(t.magic?); | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:120:27 + --> $DIR/needless_question_mark.rs:119:27 | LL | || -> Option<_> { Some(Some($expr)?) }() | ^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `Some($expr)` @@ -78,13 +78,13 @@ LL | let _x = some_and_qmark_in_macro!(x?); = note: this error originates in the macro `some_and_qmark_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:131:5 + --> $DIR/needless_question_mark.rs:130:5 | LL | Some(to.magic?) | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic` error: question mark operator is useless here - --> $DIR/needless_question_mark.rs:139:5 + --> $DIR/needless_question_mark.rs:138:5 | LL | Ok(s.magic?) | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `s.magic` diff --git a/tests/ui/needless_return.fixed b/tests/ui/needless_return.fixed index d2163b14fcad..4386aaec49e2 100644 --- a/tests/ui/needless_return.fixed +++ b/tests/ui/needless_return.fixed @@ -59,14 +59,11 @@ fn test_macro_call() -> i32 { } fn test_void_fun() { - } fn test_void_if_fun(b: bool) { if b { - } else { - } } @@ -82,7 +79,6 @@ fn test_nested_match(x: u32) { 0 => (), 1 => { let _ = 42; - }, _ => (), } @@ -126,7 +122,6 @@ mod issue6501 { fn test_closure() { let _ = || { - }; let _ = || {}; } @@ -179,14 +174,11 @@ async fn async_test_macro_call() -> i32 { } async fn async_test_void_fun() { - } async fn async_test_void_if_fun(b: bool) { if b { - } else { - } } @@ -269,4 +261,15 @@ fn issue9503(x: usize) -> isize { } } +mod issue9416 { + pub fn with_newline() { + let _ = 42; + } + + #[rustfmt::skip] + pub fn oneline() { + let _ = 42; + } +} + fn main() {} diff --git a/tests/ui/needless_return.rs b/tests/ui/needless_return.rs index 114414b5fac7..666dc54b76b4 100644 --- a/tests/ui/needless_return.rs +++ b/tests/ui/needless_return.rs @@ -269,4 +269,17 @@ fn issue9503(x: usize) -> isize { }; } +mod issue9416 { + pub fn with_newline() { + let _ = 42; + + return; + } + + #[rustfmt::skip] + pub fn oneline() { + let _ = 42; return; + } +} + fn main() {} diff --git a/tests/ui/needless_return.stderr b/tests/ui/needless_return.stderr index 047fb6c2311a..a8b5d86cd558 100644 --- a/tests/ui/needless_return.stderr +++ b/tests/ui/needless_return.stderr @@ -72,26 +72,32 @@ LL | return the_answer!(); = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:62:5 + --> $DIR/needless_return.rs:61:21 | -LL | return; - | ^^^^^^ +LL | fn test_void_fun() { + | _____________________^ +LL | | return; + | |__________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:67:9 + --> $DIR/needless_return.rs:66:11 | -LL | return; - | ^^^^^^ +LL | if b { + | ___________^ +LL | | return; + | |______________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:69:9 + --> $DIR/needless_return.rs:68:13 | -LL | return; - | ^^^^^^ +LL | } else { + | _____________^ +LL | | return; + | |______________^ | = help: remove `return` @@ -104,10 +110,12 @@ LL | _ => return, = help: replace `return` with a unit value error: unneeded `return` statement - --> $DIR/needless_return.rs:85:13 + --> $DIR/needless_return.rs:84:24 | -LL | return; - | ^^^^^^ +LL | let _ = 42; + | ________________________^ +LL | | return; + | |__________________^ | = help: remove `return` @@ -144,10 +152,12 @@ LL | bar.unwrap_or_else(|_| return) = help: replace `return` with an empty block error: unneeded `return` statement - --> $DIR/needless_return.rs:129:13 + --> $DIR/needless_return.rs:128:21 | -LL | return; - | ^^^^^^ +LL | let _ = || { + | _____________________^ +LL | | return; + | |__________________^ | = help: remove `return` @@ -240,26 +250,32 @@ LL | return the_answer!(); = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:182:5 + --> $DIR/needless_return.rs:181:33 | -LL | return; - | ^^^^^^ +LL | async fn async_test_void_fun() { + | _________________________________^ +LL | | return; + | |__________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:187:9 + --> $DIR/needless_return.rs:186:11 | -LL | return; - | ^^^^^^ +LL | if b { + | ___________^ +LL | | return; + | |______________^ | = help: remove `return` error: unneeded `return` statement - --> $DIR/needless_return.rs:189:9 + --> $DIR/needless_return.rs:188:13 | -LL | return; - | ^^^^^^ +LL | } else { + | _____________^ +LL | | return; + | |______________^ | = help: remove `return` @@ -351,5 +367,24 @@ LL | return !*(x as *const isize); | = help: remove `return` -error: aborting due to 44 previous errors +error: unneeded `return` statement + --> $DIR/needless_return.rs:274:20 + | +LL | let _ = 42; + | ____________________^ +LL | | +LL | | return; + | |______________^ + | + = help: remove `return` + +error: unneeded `return` statement + --> $DIR/needless_return.rs:281:20 + | +LL | let _ = 42; return; + | ^^^^^^^ + | + = help: remove `return` + +error: aborting due to 46 previous errors diff --git a/tests/ui/needless_splitn.fixed b/tests/ui/needless_splitn.fixed index 61f5fc4e679e..5496031fefab 100644 --- a/tests/ui/needless_splitn.fixed +++ b/tests/ui/needless_splitn.fixed @@ -1,7 +1,6 @@ // run-rustfix // edition:2018 -#![feature(custom_inner_attributes)] #![warn(clippy::needless_splitn)] #![allow(clippy::iter_skip_next, clippy::iter_nth_zero, clippy::manual_split_once)] @@ -40,8 +39,8 @@ fn _question_mark(s: &str) -> Option<()> { Some(()) } +#[clippy::msrv = "1.51"] fn _test_msrv() { - #![clippy::msrv = "1.51"] // `manual_split_once` MSRV shouldn't apply to `needless_splitn` let _ = "key=value".split('=').nth(0).unwrap(); } diff --git a/tests/ui/needless_splitn.rs b/tests/ui/needless_splitn.rs index 71d9a7077faa..35c2465bae13 100644 --- a/tests/ui/needless_splitn.rs +++ b/tests/ui/needless_splitn.rs @@ -1,7 +1,6 @@ // run-rustfix // edition:2018 -#![feature(custom_inner_attributes)] #![warn(clippy::needless_splitn)] #![allow(clippy::iter_skip_next, clippy::iter_nth_zero, clippy::manual_split_once)] @@ -40,8 +39,8 @@ fn _question_mark(s: &str) -> Option<()> { Some(()) } +#[clippy::msrv = "1.51"] fn _test_msrv() { - #![clippy::msrv = "1.51"] // `manual_split_once` MSRV shouldn't apply to `needless_splitn` let _ = "key=value".splitn(2, '=').nth(0).unwrap(); } diff --git a/tests/ui/needless_splitn.stderr b/tests/ui/needless_splitn.stderr index f112b29e7f20..f607d8e1ab5f 100644 --- a/tests/ui/needless_splitn.stderr +++ b/tests/ui/needless_splitn.stderr @@ -1,5 +1,5 @@ error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:15:13 + --> $DIR/needless_splitn.rs:14:13 | LL | let _ = str.splitn(2, '=').next(); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` @@ -7,73 +7,73 @@ LL | let _ = str.splitn(2, '=').next(); = note: `-D clippy::needless-splitn` implied by `-D warnings` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:16:13 + --> $DIR/needless_splitn.rs:15:13 | LL | let _ = str.splitn(2, '=').nth(0); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:19:18 + --> $DIR/needless_splitn.rs:18:18 | LL | let (_, _) = str.splitn(3, '=').next_tuple().unwrap(); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:22:13 + --> $DIR/needless_splitn.rs:21:13 | LL | let _ = str.rsplitn(2, '=').next(); | ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:23:13 + --> $DIR/needless_splitn.rs:22:13 | LL | let _ = str.rsplitn(2, '=').nth(0); | ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:26:18 + --> $DIR/needless_splitn.rs:25:18 | LL | let (_, _) = str.rsplitn(3, '=').next_tuple().unwrap(); | ^^^^^^^^^^^^^^^^^^^ help: try this: `str.rsplit('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:28:13 + --> $DIR/needless_splitn.rs:27:13 | LL | let _ = str.splitn(5, '=').next(); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:29:13 + --> $DIR/needless_splitn.rs:28:13 | LL | let _ = str.splitn(5, '=').nth(3); | ^^^^^^^^^^^^^^^^^^ help: try this: `str.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:35:13 + --> $DIR/needless_splitn.rs:34:13 | LL | let _ = s.splitn(2, '=').next()?; | ^^^^^^^^^^^^^^^^ help: try this: `s.split('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:36:13 + --> $DIR/needless_splitn.rs:35:13 | LL | let _ = s.splitn(2, '=').nth(0)?; | ^^^^^^^^^^^^^^^^ help: try this: `s.split('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:37:13 + --> $DIR/needless_splitn.rs:36:13 | LL | let _ = s.rsplitn(2, '=').next()?; | ^^^^^^^^^^^^^^^^^ help: try this: `s.rsplit('=')` error: unnecessary use of `rsplitn` - --> $DIR/needless_splitn.rs:38:13 + --> $DIR/needless_splitn.rs:37:13 | LL | let _ = s.rsplitn(2, '=').nth(0)?; | ^^^^^^^^^^^^^^^^^ help: try this: `s.rsplit('=')` error: unnecessary use of `splitn` - --> $DIR/needless_splitn.rs:46:13 + --> $DIR/needless_splitn.rs:45:13 | LL | let _ = "key=value".splitn(2, '=').nth(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split('=')` diff --git a/tests/ui/option_as_ref_deref.fixed b/tests/ui/option_as_ref_deref.fixed index bc376d0d7fb3..d124d133faa2 100644 --- a/tests/ui/option_as_ref_deref.fixed +++ b/tests/ui/option_as_ref_deref.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused, clippy::redundant_clone)] #![warn(clippy::option_as_ref_deref)] @@ -44,16 +43,14 @@ fn main() { let _ = opt.as_deref(); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let opt = Some(String::from("123")); let _ = opt.as_ref().map(String::as_str); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let opt = Some(String::from("123")); let _ = opt.as_deref(); } diff --git a/tests/ui/option_as_ref_deref.rs b/tests/ui/option_as_ref_deref.rs index ba3a2eedc225..86e354c6716b 100644 --- a/tests/ui/option_as_ref_deref.rs +++ b/tests/ui/option_as_ref_deref.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused, clippy::redundant_clone)] #![warn(clippy::option_as_ref_deref)] @@ -47,16 +46,14 @@ fn main() { let _ = opt.as_ref().map(std::ops::Deref::deref); } +#[clippy::msrv = "1.39"] fn msrv_1_39() { - #![clippy::msrv = "1.39"] - let opt = Some(String::from("123")); let _ = opt.as_ref().map(String::as_str); } +#[clippy::msrv = "1.40"] fn msrv_1_40() { - #![clippy::msrv = "1.40"] - let opt = Some(String::from("123")); let _ = opt.as_ref().map(String::as_str); } diff --git a/tests/ui/option_as_ref_deref.stderr b/tests/ui/option_as_ref_deref.stderr index 7de8b3b6ba43..e471b56eea81 100644 --- a/tests/ui/option_as_ref_deref.stderr +++ b/tests/ui/option_as_ref_deref.stderr @@ -1,5 +1,5 @@ error: called `.as_ref().map(Deref::deref)` on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead - --> $DIR/option_as_ref_deref.rs:14:13 + --> $DIR/option_as_ref_deref.rs:13:13 | LL | let _ = opt.clone().as_ref().map(Deref::deref).map(str::len); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.clone().as_deref()` @@ -7,7 +7,7 @@ LL | let _ = opt.clone().as_ref().map(Deref::deref).map(str::len); = note: `-D clippy::option-as-ref-deref` implied by `-D warnings` error: called `.as_ref().map(Deref::deref)` on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead - --> $DIR/option_as_ref_deref.rs:17:13 + --> $DIR/option_as_ref_deref.rs:16:13 | LL | let _ = opt.clone() | _____________^ @@ -17,97 +17,97 @@ LL | | ) | |_________^ help: try using as_deref instead: `opt.clone().as_deref()` error: called `.as_mut().map(DerefMut::deref_mut)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:23:13 + --> $DIR/option_as_ref_deref.rs:22:13 | LL | let _ = opt.as_mut().map(DerefMut::deref_mut); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_ref().map(String::as_str)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:25:13 + --> $DIR/option_as_ref_deref.rs:24:13 | LL | let _ = opt.as_ref().map(String::as_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_ref().map(|x| x.as_str())` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:26:13 + --> $DIR/option_as_ref_deref.rs:25:13 | LL | let _ = opt.as_ref().map(|x| x.as_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_mut().map(String::as_mut_str)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:27:13 + --> $DIR/option_as_ref_deref.rs:26:13 | LL | let _ = opt.as_mut().map(String::as_mut_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_mut().map(|x| x.as_mut_str())` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:28:13 + --> $DIR/option_as_ref_deref.rs:27:13 | LL | let _ = opt.as_mut().map(|x| x.as_mut_str()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_ref().map(CString::as_c_str)` on an Option value. This can be done more directly by calling `Some(CString::new(vec![]).unwrap()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:29:13 + --> $DIR/option_as_ref_deref.rs:28:13 | LL | let _ = Some(CString::new(vec![]).unwrap()).as_ref().map(CString::as_c_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(CString::new(vec![]).unwrap()).as_deref()` error: called `.as_ref().map(OsString::as_os_str)` on an Option value. This can be done more directly by calling `Some(OsString::new()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:30:13 + --> $DIR/option_as_ref_deref.rs:29:13 | LL | let _ = Some(OsString::new()).as_ref().map(OsString::as_os_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(OsString::new()).as_deref()` error: called `.as_ref().map(PathBuf::as_path)` on an Option value. This can be done more directly by calling `Some(PathBuf::new()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:31:13 + --> $DIR/option_as_ref_deref.rs:30:13 | LL | let _ = Some(PathBuf::new()).as_ref().map(PathBuf::as_path); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(PathBuf::new()).as_deref()` error: called `.as_ref().map(Vec::as_slice)` on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref()` instead - --> $DIR/option_as_ref_deref.rs:32:13 + --> $DIR/option_as_ref_deref.rs:31:13 | LL | let _ = Some(Vec::<()>::new()).as_ref().map(Vec::as_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(Vec::<()>::new()).as_deref()` error: called `.as_mut().map(Vec::as_mut_slice)` on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:33:13 + --> $DIR/option_as_ref_deref.rs:32:13 | LL | let _ = Some(Vec::<()>::new()).as_mut().map(Vec::as_mut_slice); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `Some(Vec::<()>::new()).as_deref_mut()` error: called `.as_ref().map(|x| x.deref())` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:35:13 + --> $DIR/option_as_ref_deref.rs:34:13 | LL | let _ = opt.as_ref().map(|x| x.deref()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_mut().map(|x| x.deref_mut())` on an Option value. This can be done more directly by calling `opt.clone().as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:36:13 + --> $DIR/option_as_ref_deref.rs:35:13 | LL | let _ = opt.clone().as_mut().map(|x| x.deref_mut()).map(|x| x.len()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.clone().as_deref_mut()` error: called `.as_ref().map(|x| &**x)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:43:13 + --> $DIR/option_as_ref_deref.rs:42:13 | LL | let _ = opt.as_ref().map(|x| &**x); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_mut().map(|x| &mut **x)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead - --> $DIR/option_as_ref_deref.rs:44:13 + --> $DIR/option_as_ref_deref.rs:43:13 | LL | let _ = opt.as_mut().map(|x| &mut **x); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()` error: called `.as_ref().map(std::ops::Deref::deref)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:47:13 + --> $DIR/option_as_ref_deref.rs:46:13 | LL | let _ = opt.as_ref().map(std::ops::Deref::deref); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` error: called `.as_ref().map(String::as_str)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead - --> $DIR/option_as_ref_deref.rs:61:13 + --> $DIR/option_as_ref_deref.rs:58:13 | LL | let _ = opt.as_ref().map(String::as_str); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()` diff --git a/tests/ui/ptr_as_ptr.fixed b/tests/ui/ptr_as_ptr.fixed index bea6be66a8e0..df36a9b842bf 100644 --- a/tests/ui/ptr_as_ptr.fixed +++ b/tests/ui/ptr_as_ptr.fixed @@ -2,7 +2,6 @@ // aux-build:macro_rules.rs #![warn(clippy::ptr_as_ptr)] -#![feature(custom_inner_attributes)] extern crate macro_rules; @@ -45,8 +44,8 @@ fn main() { let _ = macro_rules::ptr_as_ptr_cast!(ptr); } +#[clippy::msrv = "1.37"] fn _msrv_1_37() { - #![clippy::msrv = "1.37"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; @@ -55,8 +54,8 @@ fn _msrv_1_37() { let _ = mut_ptr as *mut i32; } +#[clippy::msrv = "1.38"] fn _msrv_1_38() { - #![clippy::msrv = "1.38"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; diff --git a/tests/ui/ptr_as_ptr.rs b/tests/ui/ptr_as_ptr.rs index ca2616b0069a..302c66462d9b 100644 --- a/tests/ui/ptr_as_ptr.rs +++ b/tests/ui/ptr_as_ptr.rs @@ -2,7 +2,6 @@ // aux-build:macro_rules.rs #![warn(clippy::ptr_as_ptr)] -#![feature(custom_inner_attributes)] extern crate macro_rules; @@ -45,8 +44,8 @@ fn main() { let _ = macro_rules::ptr_as_ptr_cast!(ptr); } +#[clippy::msrv = "1.37"] fn _msrv_1_37() { - #![clippy::msrv = "1.37"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; @@ -55,8 +54,8 @@ fn _msrv_1_37() { let _ = mut_ptr as *mut i32; } +#[clippy::msrv = "1.38"] fn _msrv_1_38() { - #![clippy::msrv = "1.38"] let ptr: *const u32 = &42_u32; let mut_ptr: *mut u32 = &mut 42_u32; diff --git a/tests/ui/ptr_as_ptr.stderr b/tests/ui/ptr_as_ptr.stderr index c58c55cfd83a..a68e1cab6d35 100644 --- a/tests/ui/ptr_as_ptr.stderr +++ b/tests/ui/ptr_as_ptr.stderr @@ -1,5 +1,5 @@ error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:19:13 + --> $DIR/ptr_as_ptr.rs:18:13 | LL | let _ = ptr as *const i32; | ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::()` @@ -7,31 +7,31 @@ LL | let _ = ptr as *const i32; = note: `-D clippy::ptr-as-ptr` implied by `-D warnings` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:20:13 + --> $DIR/ptr_as_ptr.rs:19:13 | LL | let _ = mut_ptr as *mut i32; | ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:25:17 + --> $DIR/ptr_as_ptr.rs:24:17 | LL | let _ = *ptr_ptr as *const i32; | ^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `(*ptr_ptr).cast::()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:38:25 + --> $DIR/ptr_as_ptr.rs:37:25 | LL | let _: *const i32 = ptr as *const _; | ^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:39:23 + --> $DIR/ptr_as_ptr.rs:38:23 | LL | let _: *mut i32 = mut_ptr as _; | ^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:11:9 + --> $DIR/ptr_as_ptr.rs:10:9 | LL | $ptr as *const i32 | ^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `$ptr.cast::()` @@ -42,13 +42,13 @@ LL | let _ = cast_it!(ptr); = note: this error originates in the macro `cast_it` (in Nightly builds, run with -Z macro-backtrace for more info) error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:63:13 + --> $DIR/ptr_as_ptr.rs:62:13 | LL | let _ = ptr as *const i32; | ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::()` error: `as` casting between raw pointers without changing its mutability - --> $DIR/ptr_as_ptr.rs:64:13 + --> $DIR/ptr_as_ptr.rs:63:13 | LL | let _ = mut_ptr as *mut i32; | ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::()` diff --git a/tests/ui/range_contains.fixed b/tests/ui/range_contains.fixed index 824f00cb99e8..4923731fe45e 100644 --- a/tests/ui/range_contains.fixed +++ b/tests/ui/range_contains.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_range_contains)] #![allow(unused)] #![allow(clippy::no_effect)] @@ -65,16 +64,14 @@ pub const fn in_range(a: i32) -> bool { 3 <= a && a <= 20 } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let x = 5; x >= 8 && x < 34; } +#[clippy::msrv = "1.35"] fn msrv_1_35() { - #![clippy::msrv = "1.35"] - let x = 5; (8..35).contains(&x); } diff --git a/tests/ui/range_contains.rs b/tests/ui/range_contains.rs index df925eeadfe5..d623ccb5da63 100644 --- a/tests/ui/range_contains.rs +++ b/tests/ui/range_contains.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::manual_range_contains)] #![allow(unused)] #![allow(clippy::no_effect)] @@ -65,16 +64,14 @@ pub const fn in_range(a: i32) -> bool { 3 <= a && a <= 20 } +#[clippy::msrv = "1.34"] fn msrv_1_34() { - #![clippy::msrv = "1.34"] - let x = 5; x >= 8 && x < 34; } +#[clippy::msrv = "1.35"] fn msrv_1_35() { - #![clippy::msrv = "1.35"] - let x = 5; x >= 8 && x < 35; } diff --git a/tests/ui/range_contains.stderr b/tests/ui/range_contains.stderr index 9689e665b05c..ea34023a4664 100644 --- a/tests/ui/range_contains.stderr +++ b/tests/ui/range_contains.stderr @@ -1,5 +1,5 @@ error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:14:5 + --> $DIR/range_contains.rs:13:5 | LL | x >= 8 && x < 12; | ^^^^^^^^^^^^^^^^ help: use: `(8..12).contains(&x)` @@ -7,121 +7,121 @@ LL | x >= 8 && x < 12; = note: `-D clippy::manual-range-contains` implied by `-D warnings` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:15:5 + --> $DIR/range_contains.rs:14:5 | LL | x < 42 && x >= 21; | ^^^^^^^^^^^^^^^^^ help: use: `(21..42).contains(&x)` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:16:5 + --> $DIR/range_contains.rs:15:5 | LL | 100 > x && 1 <= x; | ^^^^^^^^^^^^^^^^^ help: use: `(1..100).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:19:5 + --> $DIR/range_contains.rs:18:5 | LL | x >= 9 && x <= 99; | ^^^^^^^^^^^^^^^^^ help: use: `(9..=99).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:20:5 + --> $DIR/range_contains.rs:19:5 | LL | x <= 33 && x >= 1; | ^^^^^^^^^^^^^^^^^ help: use: `(1..=33).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:21:5 + --> $DIR/range_contains.rs:20:5 | LL | 999 >= x && 1 <= x; | ^^^^^^^^^^^^^^^^^^ help: use: `(1..=999).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:24:5 + --> $DIR/range_contains.rs:23:5 | LL | x < 8 || x >= 12; | ^^^^^^^^^^^^^^^^ help: use: `!(8..12).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:25:5 + --> $DIR/range_contains.rs:24:5 | LL | x >= 42 || x < 21; | ^^^^^^^^^^^^^^^^^ help: use: `!(21..42).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:26:5 + --> $DIR/range_contains.rs:25:5 | LL | 100 <= x || 1 > x; | ^^^^^^^^^^^^^^^^^ help: use: `!(1..100).contains(&x)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:29:5 + --> $DIR/range_contains.rs:28:5 | LL | x < 9 || x > 99; | ^^^^^^^^^^^^^^^ help: use: `!(9..=99).contains(&x)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:30:5 + --> $DIR/range_contains.rs:29:5 | LL | x > 33 || x < 1; | ^^^^^^^^^^^^^^^ help: use: `!(1..=33).contains(&x)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:31:5 + --> $DIR/range_contains.rs:30:5 | LL | 999 < x || 1 > x; | ^^^^^^^^^^^^^^^^ help: use: `!(1..=999).contains(&x)` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:46:5 + --> $DIR/range_contains.rs:45:5 | LL | y >= 0. && y < 1.; | ^^^^^^^^^^^^^^^^^ help: use: `(0. ..1.).contains(&y)` error: manual `!RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:47:5 + --> $DIR/range_contains.rs:46:5 | LL | y < 0. || y > 1.; | ^^^^^^^^^^^^^^^^ help: use: `!(0. ..=1.).contains(&y)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:50:5 + --> $DIR/range_contains.rs:49:5 | LL | x >= -10 && x <= 10; | ^^^^^^^^^^^^^^^^^^^ help: use: `(-10..=10).contains(&x)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:52:5 + --> $DIR/range_contains.rs:51:5 | LL | y >= -3. && y <= 3.; | ^^^^^^^^^^^^^^^^^^^ help: use: `(-3. ..=3.).contains(&y)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:57:30 + --> $DIR/range_contains.rs:56:30 | LL | (x >= 0) && (x <= 10) && (z >= 0) && (z <= 10); | ^^^^^^^^^^^^^^^^^^^^^ help: use: `(0..=10).contains(&z)` error: manual `RangeInclusive::contains` implementation - --> $DIR/range_contains.rs:57:5 + --> $DIR/range_contains.rs:56:5 | LL | (x >= 0) && (x <= 10) && (z >= 0) && (z <= 10); | ^^^^^^^^^^^^^^^^^^^^^ help: use: `(0..=10).contains(&x)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:58:29 + --> $DIR/range_contains.rs:57:29 | LL | (x < 0) || (x >= 10) || (z < 0) || (z >= 10); | ^^^^^^^^^^^^^^^^^^^^ help: use: `!(0..10).contains(&z)` error: manual `!Range::contains` implementation - --> $DIR/range_contains.rs:58:5 + --> $DIR/range_contains.rs:57:5 | LL | (x < 0) || (x >= 10) || (z < 0) || (z >= 10); | ^^^^^^^^^^^^^^^^^^^^ help: use: `!(0..10).contains(&x)` error: manual `Range::contains` implementation - --> $DIR/range_contains.rs:79:5 + --> $DIR/range_contains.rs:76:5 | LL | x >= 8 && x < 35; | ^^^^^^^^^^^^^^^^ help: use: `(8..35).contains(&x)` diff --git a/tests/ui/redundant_closure_call_fixable.fixed b/tests/ui/redundant_closure_call_fixable.fixed index 7cd687c95a00..c0e49ff4caa7 100644 --- a/tests/ui/redundant_closure_call_fixable.fixed +++ b/tests/ui/redundant_closure_call_fixable.fixed @@ -25,4 +25,16 @@ fn main() { x * y }; let d = async { something().await }; + + macro_rules! m { + () => { + 0 + }; + } + macro_rules! m2 { + () => { + m!() + }; + } + m2!(); } diff --git a/tests/ui/redundant_closure_call_fixable.rs b/tests/ui/redundant_closure_call_fixable.rs index 37e4d2238641..9e6e54348a8c 100644 --- a/tests/ui/redundant_closure_call_fixable.rs +++ b/tests/ui/redundant_closure_call_fixable.rs @@ -25,4 +25,16 @@ fn main() { x * y })(); let d = (async || something().await)(); + + macro_rules! m { + () => { + (|| 0)() + }; + } + macro_rules! m2 { + () => { + (|| m!())() + }; + } + m2!(); } diff --git a/tests/ui/redundant_closure_call_fixable.stderr b/tests/ui/redundant_closure_call_fixable.stderr index 56a8e57c0c36..d71bcba2a820 100644 --- a/tests/ui/redundant_closure_call_fixable.stderr +++ b/tests/ui/redundant_closure_call_fixable.stderr @@ -52,5 +52,27 @@ error: try not to call a closure in the expression where it is declared LL | let d = (async || something().await)(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async { something().await }` -error: aborting due to 4 previous errors +error: try not to call a closure in the expression where it is declared + --> $DIR/redundant_closure_call_fixable.rs:36:13 + | +LL | (|| m!())() + | ^^^^^^^^^^^ help: try doing something like: `m!()` +... +LL | m2!(); + | ----- in this macro invocation + | + = note: this error originates in the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: try not to call a closure in the expression where it is declared + --> $DIR/redundant_closure_call_fixable.rs:31:13 + | +LL | (|| 0)() + | ^^^^^^^^ help: try doing something like: `0` +... +LL | m2!(); + | ----- in this macro invocation + | + = note: this error originates in the macro `m` which comes from the expansion of the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 6 previous errors diff --git a/tests/ui/redundant_field_names.fixed b/tests/ui/redundant_field_names.fixed index 34ab552cb1d8..ec7f8ae923a7 100644 --- a/tests/ui/redundant_field_names.fixed +++ b/tests/ui/redundant_field_names.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::redundant_field_names)] #![allow(clippy::no_effect, dead_code, unused_variables)] @@ -72,16 +71,14 @@ fn issue_3476() { S { foo: foo:: }; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - let start = 0; let _ = RangeFrom { start: start }; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - let start = 0; let _ = RangeFrom { start }; } diff --git a/tests/ui/redundant_field_names.rs b/tests/ui/redundant_field_names.rs index a051b1f96f0f..73122016cf69 100644 --- a/tests/ui/redundant_field_names.rs +++ b/tests/ui/redundant_field_names.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::redundant_field_names)] #![allow(clippy::no_effect, dead_code, unused_variables)] @@ -72,16 +71,14 @@ fn issue_3476() { S { foo: foo:: }; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - let start = 0; let _ = RangeFrom { start: start }; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - let start = 0; let _ = RangeFrom { start: start }; } diff --git a/tests/ui/redundant_field_names.stderr b/tests/ui/redundant_field_names.stderr index 8b82e062b93a..00a72c50cf7d 100644 --- a/tests/ui/redundant_field_names.stderr +++ b/tests/ui/redundant_field_names.stderr @@ -1,5 +1,5 @@ error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:36:9 + --> $DIR/redundant_field_names.rs:35:9 | LL | gender: gender, | ^^^^^^^^^^^^^^ help: replace it with: `gender` @@ -7,43 +7,43 @@ LL | gender: gender, = note: `-D clippy::redundant-field-names` implied by `-D warnings` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:37:9 + --> $DIR/redundant_field_names.rs:36:9 | LL | age: age, | ^^^^^^^^ help: replace it with: `age` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:58:25 + --> $DIR/redundant_field_names.rs:57:25 | LL | let _ = RangeFrom { start: start }; | ^^^^^^^^^^^^ help: replace it with: `start` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:59:23 + --> $DIR/redundant_field_names.rs:58:23 | LL | let _ = RangeTo { end: end }; | ^^^^^^^^ help: replace it with: `end` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:60:21 + --> $DIR/redundant_field_names.rs:59:21 | LL | let _ = Range { start: start, end: end }; | ^^^^^^^^^^^^ help: replace it with: `start` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:60:35 + --> $DIR/redundant_field_names.rs:59:35 | LL | let _ = Range { start: start, end: end }; | ^^^^^^^^ help: replace it with: `end` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:62:32 + --> $DIR/redundant_field_names.rs:61:32 | LL | let _ = RangeToInclusive { end: end }; | ^^^^^^^^ help: replace it with: `end` error: redundant field names in struct initialization - --> $DIR/redundant_field_names.rs:86:25 + --> $DIR/redundant_field_names.rs:83:25 | LL | let _ = RangeFrom { start: start }; | ^^^^^^^^^^^^ help: replace it with: `start` diff --git a/tests/ui/redundant_static_lifetimes.fixed b/tests/ui/redundant_static_lifetimes.fixed index 42110dbe81e8..4c5846fe837e 100644 --- a/tests/ui/redundant_static_lifetimes.fixed +++ b/tests/ui/redundant_static_lifetimes.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #[derive(Debug)] @@ -56,14 +55,12 @@ impl Bar for Foo { const TRAIT_VAR: &'static str = "foo"; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - static V: &'static u8 = &16; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - static V: &u8 = &17; } diff --git a/tests/ui/redundant_static_lifetimes.rs b/tests/ui/redundant_static_lifetimes.rs index bc5200bc8625..64a66be1a83c 100644 --- a/tests/ui/redundant_static_lifetimes.rs +++ b/tests/ui/redundant_static_lifetimes.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![allow(unused)] #[derive(Debug)] @@ -56,14 +55,12 @@ impl Bar for Foo { const TRAIT_VAR: &'static str = "foo"; } +#[clippy::msrv = "1.16"] fn msrv_1_16() { - #![clippy::msrv = "1.16"] - static V: &'static u8 = &16; } +#[clippy::msrv = "1.17"] fn msrv_1_17() { - #![clippy::msrv = "1.17"] - static V: &'static u8 = &17; } diff --git a/tests/ui/redundant_static_lifetimes.stderr b/tests/ui/redundant_static_lifetimes.stderr index 735113460d28..0938ebf783ff 100644 --- a/tests/ui/redundant_static_lifetimes.stderr +++ b/tests/ui/redundant_static_lifetimes.stderr @@ -1,5 +1,5 @@ error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:9:17 + --> $DIR/redundant_static_lifetimes.rs:8:17 | LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` @@ -7,97 +7,97 @@ LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removin = note: `-D clippy::redundant-static-lifetimes` implied by `-D warnings` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:13:21 + --> $DIR/redundant_static_lifetimes.rs:12:21 | LL | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:15:32 + --> $DIR/redundant_static_lifetimes.rs:14:32 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:15:47 + --> $DIR/redundant_static_lifetimes.rs:14:47 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:17:17 + --> $DIR/redundant_static_lifetimes.rs:16:17 | LL | const VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:19:20 + --> $DIR/redundant_static_lifetimes.rs:18:20 | LL | const VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:21:19 + --> $DIR/redundant_static_lifetimes.rs:20:19 | LL | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:23:19 + --> $DIR/redundant_static_lifetimes.rs:22:19 | LL | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: constants have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:25:19 + --> $DIR/redundant_static_lifetimes.rs:24:19 | LL | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:27:25 + --> $DIR/redundant_static_lifetimes.rs:26:25 | LL | static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:31:29 + --> $DIR/redundant_static_lifetimes.rs:30:29 | LL | static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:33:25 + --> $DIR/redundant_static_lifetimes.rs:32:25 | LL | static STATIC_VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:35:28 + --> $DIR/redundant_static_lifetimes.rs:34:28 | LL | static STATIC_VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:37:27 + --> $DIR/redundant_static_lifetimes.rs:36:27 | LL | static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:39:27 + --> $DIR/redundant_static_lifetimes.rs:38:27 | LL | static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:41:27 + --> $DIR/redundant_static_lifetimes.rs:40:27 | LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: statics have by default a `'static` lifetime - --> $DIR/redundant_static_lifetimes.rs:68:16 + --> $DIR/redundant_static_lifetimes.rs:65:16 | LL | static V: &'static u8 = &17; | -^^^^^^^--- help: consider removing `'static`: `&u8` diff --git a/tests/ui/result_large_err.rs b/tests/ui/result_large_err.rs index 9dd27d6dc01a..1c12cebfd971 100644 --- a/tests/ui/result_large_err.rs +++ b/tests/ui/result_large_err.rs @@ -108,4 +108,10 @@ pub fn array_error() -> Result<(), ArrayError<(i32, T), U>> { Ok(()) } +// Issue #10005 +enum Empty {} +fn _empty_error() -> Result<(), Empty> { + Ok(()) +} + fn main() {} diff --git a/tests/ui/seek_from_current.fixed b/tests/ui/seek_from_current.fixed index 4b5303324bc6..1309c91b81c9 100644 --- a/tests/ui/seek_from_current.fixed +++ b/tests/ui/seek_from_current.fixed @@ -1,12 +1,11 @@ // run-rustfix #![warn(clippy::seek_from_current)] -#![feature(custom_inner_attributes)] use std::fs::File; use std::io::{self, Seek, SeekFrom, Write}; +#[clippy::msrv = "1.50"] fn _msrv_1_50() -> io::Result<()> { - #![clippy::msrv = "1.50"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.seek(SeekFrom::Current(0))?; @@ -14,8 +13,8 @@ fn _msrv_1_50() -> io::Result<()> { Ok(()) } +#[clippy::msrv = "1.51"] fn _msrv_1_51() -> io::Result<()> { - #![clippy::msrv = "1.51"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.stream_position()?; diff --git a/tests/ui/seek_from_current.rs b/tests/ui/seek_from_current.rs index f93639261a18..5d9b1424cf68 100644 --- a/tests/ui/seek_from_current.rs +++ b/tests/ui/seek_from_current.rs @@ -1,12 +1,11 @@ // run-rustfix #![warn(clippy::seek_from_current)] -#![feature(custom_inner_attributes)] use std::fs::File; use std::io::{self, Seek, SeekFrom, Write}; +#[clippy::msrv = "1.50"] fn _msrv_1_50() -> io::Result<()> { - #![clippy::msrv = "1.50"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.seek(SeekFrom::Current(0))?; @@ -14,8 +13,8 @@ fn _msrv_1_50() -> io::Result<()> { Ok(()) } +#[clippy::msrv = "1.51"] fn _msrv_1_51() -> io::Result<()> { - #![clippy::msrv = "1.51"] let mut f = File::create("foo.txt")?; f.write_all(b"Hi!")?; f.seek(SeekFrom::Current(0))?; diff --git a/tests/ui/seek_from_current.stderr b/tests/ui/seek_from_current.stderr index db1125b53cdf..c079f3611929 100644 --- a/tests/ui/seek_from_current.stderr +++ b/tests/ui/seek_from_current.stderr @@ -1,5 +1,5 @@ error: using `SeekFrom::Current` to start from current position - --> $DIR/seek_from_current.rs:21:5 + --> $DIR/seek_from_current.rs:20:5 | LL | f.seek(SeekFrom::Current(0))?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `f.stream_position()` diff --git a/tests/ui/seek_to_start_instead_of_rewind.fixed b/tests/ui/seek_to_start_instead_of_rewind.fixed index 464b6cdef639..9d0d1124c460 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.fixed +++ b/tests/ui/seek_to_start_instead_of_rewind.fixed @@ -1,6 +1,5 @@ // run-rustfix #![allow(unused)] -#![feature(custom_inner_attributes)] #![warn(clippy::seek_to_start_instead_of_rewind)] use std::fs::OpenOptions; @@ -94,9 +93,8 @@ fn main() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.54"] fn msrv_1_54() { - #![clippy::msrv = "1.54"] - let mut f = OpenOptions::new() .write(true) .read(true) @@ -115,9 +113,8 @@ fn msrv_1_54() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.55"] fn msrv_1_55() { - #![clippy::msrv = "1.55"] - let mut f = OpenOptions::new() .write(true) .read(true) diff --git a/tests/ui/seek_to_start_instead_of_rewind.rs b/tests/ui/seek_to_start_instead_of_rewind.rs index 68e09bd7c1f0..c5bc57cc3a74 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.rs +++ b/tests/ui/seek_to_start_instead_of_rewind.rs @@ -1,6 +1,5 @@ // run-rustfix #![allow(unused)] -#![feature(custom_inner_attributes)] #![warn(clippy::seek_to_start_instead_of_rewind)] use std::fs::OpenOptions; @@ -94,9 +93,8 @@ fn main() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.54"] fn msrv_1_54() { - #![clippy::msrv = "1.54"] - let mut f = OpenOptions::new() .write(true) .read(true) @@ -115,9 +113,8 @@ fn msrv_1_54() { assert_eq!(&buf, hello); } +#[clippy::msrv = "1.55"] fn msrv_1_55() { - #![clippy::msrv = "1.55"] - let mut f = OpenOptions::new() .write(true) .read(true) diff --git a/tests/ui/seek_to_start_instead_of_rewind.stderr b/tests/ui/seek_to_start_instead_of_rewind.stderr index de0eec5d909c..6cce025359fe 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.stderr +++ b/tests/ui/seek_to_start_instead_of_rewind.stderr @@ -1,5 +1,5 @@ error: used `seek` to go to the start of the stream - --> $DIR/seek_to_start_instead_of_rewind.rs:54:7 + --> $DIR/seek_to_start_instead_of_rewind.rs:53:7 | LL | t.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` @@ -7,13 +7,13 @@ LL | t.seek(SeekFrom::Start(0)); = note: `-D clippy::seek-to-start-instead-of-rewind` implied by `-D warnings` error: used `seek` to go to the start of the stream - --> $DIR/seek_to_start_instead_of_rewind.rs:59:7 + --> $DIR/seek_to_start_instead_of_rewind.rs:58:7 | LL | t.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` error: used `seek` to go to the start of the stream - --> $DIR/seek_to_start_instead_of_rewind.rs:131:7 + --> $DIR/seek_to_start_instead_of_rewind.rs:128:7 | LL | f.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` diff --git a/tests/ui/transmute_ptr_to_ref.fixed b/tests/ui/transmute_ptr_to_ref.fixed index e5fe9133f975..074dae5fb286 100644 --- a/tests/ui/transmute_ptr_to_ref.fixed +++ b/tests/ui/transmute_ptr_to_ref.fixed @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::transmute_ptr_to_ref)] #![allow(clippy::match_single_binding)] @@ -51,8 +50,8 @@ unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &' } } +#[clippy::msrv = "1.38"] unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.38"] let a = 0u32; let a = &a as *const u32; let _: &u32 = &*a; @@ -63,8 +62,8 @@ unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { } } +#[clippy::msrv = "1.37"] unsafe fn _under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.37"] let a = 0u32; let a = &a as *const u32; let _: &u32 = &*a; diff --git a/tests/ui/transmute_ptr_to_ref.rs b/tests/ui/transmute_ptr_to_ref.rs index fe49cdc324fd..2edc122cf471 100644 --- a/tests/ui/transmute_ptr_to_ref.rs +++ b/tests/ui/transmute_ptr_to_ref.rs @@ -1,6 +1,5 @@ // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::transmute_ptr_to_ref)] #![allow(clippy::match_single_binding)] @@ -51,8 +50,8 @@ unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &' } } +#[clippy::msrv = "1.38"] unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.38"] let a = 0u32; let a = &a as *const u32; let _: &u32 = std::mem::transmute(a); @@ -63,8 +62,8 @@ unsafe fn _meets_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { } } +#[clippy::msrv = "1.37"] unsafe fn _under_msrv<'a, 'b, 'c>(x: *const &'a u32) -> &'c &'b u32 { - #![clippy::msrv = "1.37"] let a = 0u32; let a = &a as *const u32; let _: &u32 = std::mem::transmute(a); diff --git a/tests/ui/transmute_ptr_to_ref.stderr b/tests/ui/transmute_ptr_to_ref.stderr index 10117ee9182a..b3e6c09d2d7a 100644 --- a/tests/ui/transmute_ptr_to_ref.stderr +++ b/tests/ui/transmute_ptr_to_ref.stderr @@ -1,5 +1,5 @@ error: transmute from a pointer type (`*const T`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:8:17 + --> $DIR/transmute_ptr_to_ref.rs:7:17 | LL | let _: &T = std::mem::transmute(p); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*p` @@ -7,127 +7,127 @@ LL | let _: &T = std::mem::transmute(p); = note: `-D clippy::transmute-ptr-to-ref` implied by `-D warnings` error: transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:11:21 + --> $DIR/transmute_ptr_to_ref.rs:10:21 | LL | let _: &mut T = std::mem::transmute(m); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *m` error: transmute from a pointer type (`*mut T`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:14:17 + --> $DIR/transmute_ptr_to_ref.rs:13:17 | LL | let _: &T = std::mem::transmute(m); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*m` error: transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:17:21 + --> $DIR/transmute_ptr_to_ref.rs:16:21 | LL | let _: &mut T = std::mem::transmute(p as *mut T); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(p as *mut T)` error: transmute from a pointer type (`*const U`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:20:17 + --> $DIR/transmute_ptr_to_ref.rs:19:17 | LL | let _: &T = std::mem::transmute(o); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(o as *const T)` error: transmute from a pointer type (`*mut U`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:23:21 + --> $DIR/transmute_ptr_to_ref.rs:22:21 | LL | let _: &mut T = std::mem::transmute(om); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(om as *mut T)` error: transmute from a pointer type (`*mut U`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:26:17 + --> $DIR/transmute_ptr_to_ref.rs:25:17 | LL | let _: &T = std::mem::transmute(om); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(om as *const T)` error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, u8>`) - --> $DIR/transmute_ptr_to_ref.rs:36:32 + --> $DIR/transmute_ptr_to_ref.rs:35:32 | LL | let _: &Foo = unsafe { std::mem::transmute::<_, &Foo<_>>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*raw.cast::>()` error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, &u8>`) - --> $DIR/transmute_ptr_to_ref.rs:38:33 + --> $DIR/transmute_ptr_to_ref.rs:37:33 | LL | let _: &Foo<&u8> = unsafe { std::mem::transmute::<_, &Foo<&_>>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*raw.cast::>()` error: transmute from a pointer type (`*const i32`) to a reference type (`&u8`) - --> $DIR/transmute_ptr_to_ref.rs:42:14 + --> $DIR/transmute_ptr_to_ref.rs:41:14 | LL | unsafe { std::mem::transmute::<_, Bar>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const u8)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:47:14 + --> $DIR/transmute_ptr_to_ref.rs:46:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:48:14 + --> $DIR/transmute_ptr_to_ref.rs:47:14 | LL | 1 => std::mem::transmute(y), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*y.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:49:14 + --> $DIR/transmute_ptr_to_ref.rs:48:14 | LL | 2 => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&'b u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:50:14 + --> $DIR/transmute_ptr_to_ref.rs:49:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(y), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*y.cast::<&'b u32>()` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:58:19 + --> $DIR/transmute_ptr_to_ref.rs:57:19 | LL | let _: &u32 = std::mem::transmute(a); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:59:19 + --> $DIR/transmute_ptr_to_ref.rs:58:19 | LL | let _: &u32 = std::mem::transmute::<_, &u32>(a); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a.cast::()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:61:14 + --> $DIR/transmute_ptr_to_ref.rs:60:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:62:14 + --> $DIR/transmute_ptr_to_ref.rs:61:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&'b u32>()` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:70:19 + --> $DIR/transmute_ptr_to_ref.rs:69:19 | LL | let _: &u32 = std::mem::transmute(a); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:71:19 + --> $DIR/transmute_ptr_to_ref.rs:70:19 | LL | let _: &u32 = std::mem::transmute::<_, &u32>(a); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(a as *const u32)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:73:14 + --> $DIR/transmute_ptr_to_ref.rs:72:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(x as *const () as *const &u32)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:74:14 + --> $DIR/transmute_ptr_to_ref.rs:73:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(x as *const () as *const &'b u32)` diff --git a/tests/ui/undocumented_unsafe_blocks.rs b/tests/ui/undocumented_unsafe_blocks.rs index cbc6768033ec..c05eb447b2eb 100644 --- a/tests/ui/undocumented_unsafe_blocks.rs +++ b/tests/ui/undocumented_unsafe_blocks.rs @@ -1,6 +1,6 @@ // aux-build:proc_macro_unsafe.rs -#![warn(clippy::undocumented_unsafe_blocks)] +#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)] #![allow(clippy::let_unit_value, clippy::missing_safety_doc)] extern crate proc_macro_unsafe; diff --git a/tests/ui/undocumented_unsafe_blocks.stderr b/tests/ui/undocumented_unsafe_blocks.stderr index ba4de9806d17..d1c1bb5ffeac 100644 --- a/tests/ui/undocumented_unsafe_blocks.stderr +++ b/tests/ui/undocumented_unsafe_blocks.stderr @@ -239,6 +239,19 @@ LL | unsafe impl TrailingComment for () {} // SAFETY: | = help: consider adding a safety comment on the preceding line +error: constant item has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:471:5 + | +LL | const BIG_NUMBER: i32 = 1000000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:470:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + = note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings` + error: unsafe impl missing a safety comment --> $DIR/undocumented_unsafe_blocks.rs:472:5 | @@ -271,6 +284,24 @@ LL | unsafe {}; | = help: consider adding a safety comment on the preceding line +error: statement has unnecessary safety comment + --> $DIR/undocumented_unsafe_blocks.rs:501:5 + | +LL | / let _ = { +LL | | if unsafe { true } { +LL | | todo!(); +LL | | } else { +... | +LL | | } +LL | | }; + | |______^ + | +help: consider removing the safety comment + --> $DIR/undocumented_unsafe_blocks.rs:500:5 + | +LL | // SAFETY: this is more than one level away, so it should warn + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: unsafe block missing a safety comment --> $DIR/undocumented_unsafe_blocks.rs:502:12 | @@ -287,5 +318,5 @@ LL | let bar = unsafe {}; | = help: consider adding a safety comment on the preceding line -error: aborting due to 34 previous errors +error: aborting due to 36 previous errors diff --git a/tests/ui/uninlined_format_args.fixed b/tests/ui/uninlined_format_args.fixed index 106274479751..9d08e80cf9a5 100644 --- a/tests/ui/uninlined_format_args.fixed +++ b/tests/ui/uninlined_format_args.fixed @@ -1,6 +1,5 @@ // aux-build:proc_macro_with_span.rs // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::uninlined_format_args)] #![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] #![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] @@ -44,9 +43,7 @@ fn tester(fn_arg: i32) { println!("val='{local_i32}'"); // space+tab println!("val='{local_i32}'"); // tab+space println!( - "val='{ - }'", - local_i32 + "val='{local_i32}'" ); println!("{local_i32}"); println!("{fn_arg}"); @@ -57,11 +54,11 @@ fn tester(fn_arg: i32) { println!("{local_i32:<3}"); println!("{local_i32:#010x}"); println!("{local_f64:.1}"); - println!("Hello {} is {local_f64:.local_i32$}", "x"); - println!("Hello {local_i32} is {local_f64:.*}", 5); - println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("Hello {} is {:.*}", "x", local_i32, local_f64); + println!("Hello {} is {:.*}", local_i32, 5, local_f64); + println!("Hello {} is {2:.*}", local_i32, 5, local_f64); println!("{local_i32} {local_f64}"); - println!("{local_i32}, {}", local_opt.unwrap()); + println!("{}, {}", local_i32, local_opt.unwrap()); println!("{val}"); println!("{val}"); println!("{} {1}", local_i32, 42); @@ -110,8 +107,7 @@ fn tester(fn_arg: i32) { println!("{local_f64:width$.prec$}"); println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); println!( - "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", - local_i32, width, prec, + "{local_i32:width$.prec$} {local_i32:prec$.width$} {width:local_i32$.prec$} {width:prec$.local_i32$} {prec:local_i32$.width$} {prec:width$.local_i32$}", ); println!( "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}", @@ -142,9 +138,7 @@ fn tester(fn_arg: i32) { println!(no_param_str!(), local_i32); println!( - "{}", - // comment with a comma , in it - val, + "{val}", ); println!("{val}"); @@ -169,14 +163,14 @@ fn main() { tester(42); } +#[clippy::msrv = "1.57"] fn _under_msrv() { - #![clippy::msrv = "1.57"] let local_i32 = 1; println!("don't expand='{}'", local_i32); } +#[clippy::msrv = "1.58"] fn _meets_msrv() { - #![clippy::msrv = "1.58"] let local_i32 = 1; println!("expand='{local_i32}'"); } diff --git a/tests/ui/uninlined_format_args.rs b/tests/ui/uninlined_format_args.rs index 8e495ebd083a..35b3677a8968 100644 --- a/tests/ui/uninlined_format_args.rs +++ b/tests/ui/uninlined_format_args.rs @@ -1,6 +1,5 @@ // aux-build:proc_macro_with_span.rs // run-rustfix -#![feature(custom_inner_attributes)] #![warn(clippy::uninlined_format_args)] #![allow(named_arguments_used_positionally, unused_imports, unused_macros, unused_variables)] #![allow(clippy::eq_op, clippy::format_in_format_args, clippy::print_literal)] @@ -169,14 +168,14 @@ fn main() { tester(42); } +#[clippy::msrv = "1.57"] fn _under_msrv() { - #![clippy::msrv = "1.57"] let local_i32 = 1; println!("don't expand='{}'", local_i32); } +#[clippy::msrv = "1.58"] fn _meets_msrv() { - #![clippy::msrv = "1.58"] let local_i32 = 1; println!("expand='{}'", local_i32); } diff --git a/tests/ui/uninlined_format_args.stderr b/tests/ui/uninlined_format_args.stderr index 2ce3b7fa960c..a12abf8bef8a 100644 --- a/tests/ui/uninlined_format_args.stderr +++ b/tests/ui/uninlined_format_args.stderr @@ -1,5 +1,5 @@ error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:41:5 + --> $DIR/uninlined_format_args.rs:40:5 | LL | println!("val='{}'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -12,7 +12,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:42:5 + --> $DIR/uninlined_format_args.rs:41:5 | LL | println!("val='{ }'", local_i32); // 3 spaces | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -24,7 +24,7 @@ LL + println!("val='{local_i32}'"); // 3 spaces | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:43:5 + --> $DIR/uninlined_format_args.rs:42:5 | LL | println!("val='{ }'", local_i32); // tab | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -36,7 +36,7 @@ LL + println!("val='{local_i32}'"); // tab | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:44:5 + --> $DIR/uninlined_format_args.rs:43:5 | LL | println!("val='{ }'", local_i32); // space+tab | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -48,7 +48,7 @@ LL + println!("val='{local_i32}'"); // space+tab | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:45:5 + --> $DIR/uninlined_format_args.rs:44:5 | LL | println!("val='{ }'", local_i32); // tab+space | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -60,7 +60,17 @@ LL + println!("val='{local_i32}'"); // tab+space | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:51:5 + --> $DIR/uninlined_format_args.rs:45:5 + | +LL | / println!( +LL | | "val='{ +LL | | }'", +LL | | local_i32 +LL | | ); + | |_____^ + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:50:5 | LL | println!("{}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -72,7 +82,7 @@ LL + println!("{local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:52:5 + --> $DIR/uninlined_format_args.rs:51:5 | LL | println!("{}", fn_arg); | ^^^^^^^^^^^^^^^^^^^^^^ @@ -84,7 +94,7 @@ LL + println!("{fn_arg}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:53:5 + --> $DIR/uninlined_format_args.rs:52:5 | LL | println!("{:?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -96,7 +106,7 @@ LL + println!("{local_i32:?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:54:5 + --> $DIR/uninlined_format_args.rs:53:5 | LL | println!("{:#?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -108,7 +118,7 @@ LL + println!("{local_i32:#?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:55:5 + --> $DIR/uninlined_format_args.rs:54:5 | LL | println!("{:4}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -120,7 +130,7 @@ LL + println!("{local_i32:4}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:56:5 + --> $DIR/uninlined_format_args.rs:55:5 | LL | println!("{:04}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -132,7 +142,7 @@ LL + println!("{local_i32:04}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:57:5 + --> $DIR/uninlined_format_args.rs:56:5 | LL | println!("{:<3}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -144,7 +154,7 @@ LL + println!("{local_i32:<3}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:58:5 + --> $DIR/uninlined_format_args.rs:57:5 | LL | println!("{:#010x}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -156,7 +166,7 @@ LL + println!("{local_i32:#010x}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:59:5 + --> $DIR/uninlined_format_args.rs:58:5 | LL | println!("{:.1}", local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -167,45 +177,9 @@ LL - println!("{:.1}", local_f64); LL + println!("{local_f64:.1}"); | -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:60:5 - | -LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); -LL + println!("Hello {} is {local_f64:.local_i32$}", "x"); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:61:5 - | -LL | println!("Hello {} is {:.*}", local_i32, 5, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("Hello {} is {:.*}", local_i32, 5, local_f64); -LL + println!("Hello {local_i32} is {local_f64:.*}", 5); - | - error: variables can be used directly in the `format!` string --> $DIR/uninlined_format_args.rs:62:5 | -LL | println!("Hello {} is {2:.*}", local_i32, 5, local_f64); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("Hello {} is {2:.*}", local_i32, 5, local_f64); -LL + println!("Hello {local_i32} is {local_f64:.*}", 5); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:63:5 - | LL | println!("{} {}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | @@ -218,18 +192,6 @@ LL + println!("{local_i32} {local_f64}"); error: variables can be used directly in the `format!` string --> $DIR/uninlined_format_args.rs:64:5 | -LL | println!("{}, {}", local_i32, local_opt.unwrap()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: change this to - | -LL - println!("{}, {}", local_i32, local_opt.unwrap()); -LL + println!("{local_i32}, {}", local_opt.unwrap()); - | - -error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:65:5 - | LL | println!("{}", val); | ^^^^^^^^^^^^^^^^^^^ | @@ -240,7 +202,7 @@ LL + println!("{val}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:66:5 + --> $DIR/uninlined_format_args.rs:65:5 | LL | println!("{}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^ @@ -252,7 +214,7 @@ LL + println!("{val}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:68:5 + --> $DIR/uninlined_format_args.rs:67:5 | LL | println!("val='{/t }'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -264,7 +226,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:69:5 + --> $DIR/uninlined_format_args.rs:68:5 | LL | println!("val='{/n }'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -276,7 +238,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:70:5 + --> $DIR/uninlined_format_args.rs:69:5 | LL | println!("val='{local_i32}'", local_i32 = local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -288,7 +250,7 @@ LL + println!("val='{local_i32}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:71:5 + --> $DIR/uninlined_format_args.rs:70:5 | LL | println!("val='{local_i32}'", local_i32 = fn_arg); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -300,7 +262,7 @@ LL + println!("val='{fn_arg}'"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:72:5 + --> $DIR/uninlined_format_args.rs:71:5 | LL | println!("{0}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -312,7 +274,7 @@ LL + println!("{local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:73:5 + --> $DIR/uninlined_format_args.rs:72:5 | LL | println!("{0:?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -324,7 +286,7 @@ LL + println!("{local_i32:?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:74:5 + --> $DIR/uninlined_format_args.rs:73:5 | LL | println!("{0:#?}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -336,7 +298,7 @@ LL + println!("{local_i32:#?}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:75:5 + --> $DIR/uninlined_format_args.rs:74:5 | LL | println!("{0:04}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -348,7 +310,7 @@ LL + println!("{local_i32:04}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:76:5 + --> $DIR/uninlined_format_args.rs:75:5 | LL | println!("{0:<3}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -360,7 +322,7 @@ LL + println!("{local_i32:<3}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:77:5 + --> $DIR/uninlined_format_args.rs:76:5 | LL | println!("{0:#010x}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -372,7 +334,7 @@ LL + println!("{local_i32:#010x}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:78:5 + --> $DIR/uninlined_format_args.rs:77:5 | LL | println!("{0:.1}", local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -384,7 +346,7 @@ LL + println!("{local_f64:.1}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:79:5 + --> $DIR/uninlined_format_args.rs:78:5 | LL | println!("{0} {0}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -396,7 +358,7 @@ LL + println!("{local_i32} {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:80:5 + --> $DIR/uninlined_format_args.rs:79:5 | LL | println!("{1} {} {0} {}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -408,7 +370,7 @@ LL + println!("{local_f64} {local_i32} {local_i32} {local_f64}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:81:5 + --> $DIR/uninlined_format_args.rs:80:5 | LL | println!("{0} {1}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -420,7 +382,7 @@ LL + println!("{local_i32} {local_f64}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:82:5 + --> $DIR/uninlined_format_args.rs:81:5 | LL | println!("{1} {0}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -432,7 +394,7 @@ LL + println!("{local_f64} {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:83:5 + --> $DIR/uninlined_format_args.rs:82:5 | LL | println!("{1} {0} {1} {0}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -444,7 +406,7 @@ LL + println!("{local_f64} {local_i32} {local_f64} {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:85:5 + --> $DIR/uninlined_format_args.rs:84:5 | LL | println!("{v}", v = local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -456,7 +418,7 @@ LL + println!("{local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:86:5 + --> $DIR/uninlined_format_args.rs:85:5 | LL | println!("{local_i32:0$}", width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -468,7 +430,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:87:5 + --> $DIR/uninlined_format_args.rs:86:5 | LL | println!("{local_i32:w$}", w = width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -480,7 +442,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:88:5 + --> $DIR/uninlined_format_args.rs:87:5 | LL | println!("{local_i32:.0$}", prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -492,7 +454,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:89:5 + --> $DIR/uninlined_format_args.rs:88:5 | LL | println!("{local_i32:.p$}", p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -504,7 +466,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:90:5 + --> $DIR/uninlined_format_args.rs:89:5 | LL | println!("{:0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -516,7 +478,7 @@ LL + println!("{val:val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:91:5 + --> $DIR/uninlined_format_args.rs:90:5 | LL | println!("{0:0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -528,7 +490,7 @@ LL + println!("{val:val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:92:5 + --> $DIR/uninlined_format_args.rs:91:5 | LL | println!("{:0$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -540,7 +502,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:93:5 + --> $DIR/uninlined_format_args.rs:92:5 | LL | println!("{0:0$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -552,7 +514,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:94:5 + --> $DIR/uninlined_format_args.rs:93:5 | LL | println!("{0:0$.v$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -564,7 +526,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:95:5 + --> $DIR/uninlined_format_args.rs:94:5 | LL | println!("{0:v$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -576,7 +538,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:96:5 + --> $DIR/uninlined_format_args.rs:95:5 | LL | println!("{v:0$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -588,7 +550,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:97:5 + --> $DIR/uninlined_format_args.rs:96:5 | LL | println!("{v:v$.0$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -600,7 +562,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:98:5 + --> $DIR/uninlined_format_args.rs:97:5 | LL | println!("{v:0$.v$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -612,7 +574,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:99:5 + --> $DIR/uninlined_format_args.rs:98:5 | LL | println!("{v:v$.v$}", v = val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -624,7 +586,7 @@ LL + println!("{val:val$.val$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:100:5 + --> $DIR/uninlined_format_args.rs:99:5 | LL | println!("{:0$}", width); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -636,7 +598,7 @@ LL + println!("{width:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:101:5 + --> $DIR/uninlined_format_args.rs:100:5 | LL | println!("{:1$}", local_i32, width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -648,7 +610,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:102:5 + --> $DIR/uninlined_format_args.rs:101:5 | LL | println!("{:w$}", w = width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -660,7 +622,7 @@ LL + println!("{width:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:103:5 + --> $DIR/uninlined_format_args.rs:102:5 | LL | println!("{:w$}", local_i32, w = width); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -672,7 +634,7 @@ LL + println!("{local_i32:width$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:104:5 + --> $DIR/uninlined_format_args.rs:103:5 | LL | println!("{:.0$}", prec); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -684,7 +646,7 @@ LL + println!("{prec:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:105:5 + --> $DIR/uninlined_format_args.rs:104:5 | LL | println!("{:.1$}", local_i32, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -696,7 +658,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:106:5 + --> $DIR/uninlined_format_args.rs:105:5 | LL | println!("{:.p$}", p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -708,7 +670,7 @@ LL + println!("{prec:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:107:5 + --> $DIR/uninlined_format_args.rs:106:5 | LL | println!("{:.p$}", local_i32, p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -720,7 +682,7 @@ LL + println!("{local_i32:.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:108:5 + --> $DIR/uninlined_format_args.rs:107:5 | LL | println!("{:0$.1$}", width, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -732,7 +694,7 @@ LL + println!("{width:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:109:5 + --> $DIR/uninlined_format_args.rs:108:5 | LL | println!("{:0$.w$}", width, w = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -744,7 +706,7 @@ LL + println!("{width:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:110:5 + --> $DIR/uninlined_format_args.rs:109:5 | LL | println!("{:1$.2$}", local_f64, width, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -756,7 +718,7 @@ LL + println!("{local_f64:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:111:5 + --> $DIR/uninlined_format_args.rs:110:5 | LL | println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -768,7 +730,16 @@ LL + println!("{local_f64:width$.prec$} {local_f64} {width} {prec}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:123:5 + --> $DIR/uninlined_format_args.rs:111:5 + | +LL | / println!( +LL | | "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}", +LL | | local_i32, width, prec, +LL | | ); + | |_____^ + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:122:5 | LL | println!("Width = {}, value with width = {:0$}", local_i32, local_f64); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -780,7 +751,7 @@ LL + println!("Width = {local_i32}, value with width = {local_f64:local_i32$ | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:124:5 + --> $DIR/uninlined_format_args.rs:123:5 | LL | println!("{:w$.p$}", local_i32, w = width, p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -792,7 +763,7 @@ LL + println!("{local_i32:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:125:5 + --> $DIR/uninlined_format_args.rs:124:5 | LL | println!("{:w$.p$}", w = width, p = prec); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -804,7 +775,7 @@ LL + println!("{width:width$.prec$}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:126:20 + --> $DIR/uninlined_format_args.rs:125:20 | LL | println!("{}", format!("{}", local_i32)); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -816,7 +787,17 @@ LL + println!("{}", format!("{local_i32}")); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:149:5 + --> $DIR/uninlined_format_args.rs:143:5 + | +LL | / println!( +LL | | "{}", +LL | | // comment with a comma , in it +LL | | val, +LL | | ); + | |_____^ + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:148:5 | LL | println!("{}", /* comment with a comma , in it */ val); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -828,7 +809,7 @@ LL + println!("{val}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:155:9 + --> $DIR/uninlined_format_args.rs:154:9 | LL | panic!("p1 {}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -840,7 +821,7 @@ LL + panic!("p1 {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:158:9 + --> $DIR/uninlined_format_args.rs:157:9 | LL | panic!("p2 {0}", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -852,7 +833,7 @@ LL + panic!("p2 {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:161:9 + --> $DIR/uninlined_format_args.rs:160:9 | LL | panic!("p3 {local_i32}", local_i32 = local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -864,7 +845,7 @@ LL + panic!("p3 {local_i32}"); | error: variables can be used directly in the `format!` string - --> $DIR/uninlined_format_args.rs:181:5 + --> $DIR/uninlined_format_args.rs:180:5 | LL | println!("expand='{}'", local_i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -875,5 +856,5 @@ LL - println!("expand='{}'", local_i32); LL + println!("expand='{local_i32}'"); | -error: aborting due to 73 previous errors +error: aborting due to 72 previous errors diff --git a/tests/ui/unnecessary_cast.fixed b/tests/ui/unnecessary_cast.fixed index ec8c6abfab91..2f7e2997e739 100644 --- a/tests/ui/unnecessary_cast.fixed +++ b/tests/ui/unnecessary_cast.fixed @@ -41,6 +41,17 @@ fn main() { // do not lint cast to alias type 1 as I32Alias; &1 as &I32Alias; + + // issue #9960 + macro_rules! bind_var { + ($id:ident, $e:expr) => {{ + let $id = 0usize; + let _ = $e != 0usize; + let $id = 0isize; + let _ = $e != 0usize; + }} + } + bind_var!(x, (x as usize) + 1); } type I32Alias = i32; @@ -85,6 +96,9 @@ mod fixable { let _ = 1 as I32Alias; let _ = &1 as &I32Alias; + + let x = 1i32; + let _ = &{ x }; } type I32Alias = i32; diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs index 5213cdc269bd..54dd46ba59f1 100644 --- a/tests/ui/unnecessary_cast.rs +++ b/tests/ui/unnecessary_cast.rs @@ -41,6 +41,17 @@ fn main() { // do not lint cast to alias type 1 as I32Alias; &1 as &I32Alias; + + // issue #9960 + macro_rules! bind_var { + ($id:ident, $e:expr) => {{ + let $id = 0usize; + let _ = $e != 0usize; + let $id = 0isize; + let _ = $e != 0usize; + }} + } + bind_var!(x, (x as usize) + 1); } type I32Alias = i32; @@ -85,6 +96,9 @@ mod fixable { let _ = 1 as I32Alias; let _ = &1 as &I32Alias; + + let x = 1i32; + let _ = &(x as i32); } type I32Alias = i32; diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr index e5c3dd5e53f8..fcee4ee2a65c 100644 --- a/tests/ui/unnecessary_cast.stderr +++ b/tests/ui/unnecessary_cast.stderr @@ -49,136 +49,142 @@ LL | 1_f32 as f32; | ^^^^^^^^^^^^ help: try: `1_f32` error: casting integer literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:53:9 + --> $DIR/unnecessary_cast.rs:64:9 | LL | 100 as f32; | ^^^^^^^^^^ help: try: `100_f32` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:54:9 + --> $DIR/unnecessary_cast.rs:65:9 | LL | 100 as f64; | ^^^^^^^^^^ help: try: `100_f64` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:55:9 + --> $DIR/unnecessary_cast.rs:66:9 | LL | 100_i32 as f64; | ^^^^^^^^^^^^^^ help: try: `100_f64` error: casting integer literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:56:17 + --> $DIR/unnecessary_cast.rs:67:17 | LL | let _ = -100 as f32; | ^^^^^^^^^^^ help: try: `-100_f32` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:57:17 + --> $DIR/unnecessary_cast.rs:68:17 | LL | let _ = -100 as f64; | ^^^^^^^^^^^ help: try: `-100_f64` error: casting integer literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:58:17 + --> $DIR/unnecessary_cast.rs:69:17 | LL | let _ = -100_i32 as f64; | ^^^^^^^^^^^^^^^ help: try: `-100_f64` error: casting float literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:59:9 + --> $DIR/unnecessary_cast.rs:70:9 | LL | 100. as f32; | ^^^^^^^^^^^ help: try: `100_f32` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:60:9 + --> $DIR/unnecessary_cast.rs:71:9 | LL | 100. as f64; | ^^^^^^^^^^^ help: try: `100_f64` error: casting integer literal to `u32` is unnecessary - --> $DIR/unnecessary_cast.rs:72:9 + --> $DIR/unnecessary_cast.rs:83:9 | LL | 1 as u32; | ^^^^^^^^ help: try: `1_u32` error: casting integer literal to `i32` is unnecessary - --> $DIR/unnecessary_cast.rs:73:9 + --> $DIR/unnecessary_cast.rs:84:9 | LL | 0x10 as i32; | ^^^^^^^^^^^ help: try: `0x10_i32` error: casting integer literal to `usize` is unnecessary - --> $DIR/unnecessary_cast.rs:74:9 + --> $DIR/unnecessary_cast.rs:85:9 | LL | 0b10 as usize; | ^^^^^^^^^^^^^ help: try: `0b10_usize` error: casting integer literal to `u16` is unnecessary - --> $DIR/unnecessary_cast.rs:75:9 + --> $DIR/unnecessary_cast.rs:86:9 | LL | 0o73 as u16; | ^^^^^^^^^^^ help: try: `0o73_u16` error: casting integer literal to `u32` is unnecessary - --> $DIR/unnecessary_cast.rs:76:9 + --> $DIR/unnecessary_cast.rs:87:9 | LL | 1_000_000_000 as u32; | ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:78:9 + --> $DIR/unnecessary_cast.rs:89:9 | LL | 1.0 as f64; | ^^^^^^^^^^ help: try: `1.0_f64` error: casting float literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:79:9 + --> $DIR/unnecessary_cast.rs:90:9 | LL | 0.5 as f32; | ^^^^^^^^^^ help: try: `0.5_f32` error: casting integer literal to `i32` is unnecessary - --> $DIR/unnecessary_cast.rs:83:17 + --> $DIR/unnecessary_cast.rs:94:17 | LL | let _ = -1 as i32; | ^^^^^^^^^ help: try: `-1_i32` error: casting float literal to `f32` is unnecessary - --> $DIR/unnecessary_cast.rs:84:17 + --> $DIR/unnecessary_cast.rs:95:17 | LL | let _ = -1.0 as f32; | ^^^^^^^^^^^ help: try: `-1.0_f32` +error: casting to the same type is unnecessary (`i32` -> `i32`) + --> $DIR/unnecessary_cast.rs:101:18 + | +LL | let _ = &(x as i32); + | ^^^^^^^^^^ help: try: `{ x }` + error: casting integer literal to `i32` is unnecessary - --> $DIR/unnecessary_cast.rs:93:22 + --> $DIR/unnecessary_cast.rs:107:22 | LL | let _: i32 = -(1) as i32; | ^^^^^^^^^^^ help: try: `-1_i32` error: casting integer literal to `i64` is unnecessary - --> $DIR/unnecessary_cast.rs:95:22 + --> $DIR/unnecessary_cast.rs:109:22 | LL | let _: i64 = -(1) as i64; | ^^^^^^^^^^^ help: try: `-1_i64` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:102:22 + --> $DIR/unnecessary_cast.rs:116:22 | LL | let _: f64 = (-8.0 as f64).exp(); | ^^^^^^^^^^^^^ help: try: `(-8.0_f64)` error: casting float literal to `f64` is unnecessary - --> $DIR/unnecessary_cast.rs:104:23 + --> $DIR/unnecessary_cast.rs:118:23 | LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior | ^^^^^^^^^^^^ help: try: `8.0_f64` error: casting to the same type is unnecessary (`f32` -> `f32`) - --> $DIR/unnecessary_cast.rs:112:20 + --> $DIR/unnecessary_cast.rs:126:20 | LL | let _num = foo() as f32; | ^^^^^^^^^^^^ help: try: `foo()` -error: aborting due to 30 previous errors +error: aborting due to 31 previous errors diff --git a/tests/ui/unnecessary_lazy_eval.fixed b/tests/ui/unnecessary_lazy_eval.fixed index ce4a82e02174..22e9bd8bdc51 100644 --- a/tests/ui/unnecessary_lazy_eval.fixed +++ b/tests/ui/unnecessary_lazy_eval.fixed @@ -33,6 +33,14 @@ impl Drop for Issue9427 { } } +struct Issue9427FollowUp; + +impl Drop for Issue9427FollowUp { + fn drop(&mut self) { + panic!("side effect drop"); + } +} + fn main() { let astronomers_pi = 10; let ext_arr: [usize; 1] = [2]; @@ -87,6 +95,7 @@ fn main() { // Should not lint - bool let _ = (0 == 1).then(|| Issue9427(0)); // Issue9427 has a significant drop + let _ = false.then(|| Issue9427FollowUp); // Issue9427FollowUp has a significant drop // should not lint, bind_instead_of_map takes priority let _ = Some(10).and_then(|idx| Some(ext_arr[idx])); @@ -133,13 +142,13 @@ fn main() { let _: Result = res.or(Ok(astronomers_pi)); let _: Result = res.or(Ok(ext_str.some_field)); let _: Result = res. - // some lines - // some lines - // some lines - // some lines - // some lines - // some lines - or(Ok(ext_str.some_field)); + // some lines + // some lines + // some lines + // some lines + // some lines + // some lines + or(Ok(ext_str.some_field)); // neither bind_instead_of_map nor unnecessary_lazy_eval applies here let _: Result = res.and_then(|x| Err(x)); diff --git a/tests/ui/unnecessary_lazy_eval.rs b/tests/ui/unnecessary_lazy_eval.rs index 59cdf6628546..8726d84a23fc 100644 --- a/tests/ui/unnecessary_lazy_eval.rs +++ b/tests/ui/unnecessary_lazy_eval.rs @@ -33,6 +33,14 @@ impl Drop for Issue9427 { } } +struct Issue9427FollowUp; + +impl Drop for Issue9427FollowUp { + fn drop(&mut self) { + panic!("side effect drop"); + } +} + fn main() { let astronomers_pi = 10; let ext_arr: [usize; 1] = [2]; @@ -87,6 +95,7 @@ fn main() { // Should not lint - bool let _ = (0 == 1).then(|| Issue9427(0)); // Issue9427 has a significant drop + let _ = false.then(|| Issue9427FollowUp); // Issue9427FollowUp has a significant drop // should not lint, bind_instead_of_map takes priority let _ = Some(10).and_then(|idx| Some(ext_arr[idx])); @@ -133,13 +142,13 @@ fn main() { let _: Result = res.or_else(|_| Ok(astronomers_pi)); let _: Result = res.or_else(|_| Ok(ext_str.some_field)); let _: Result = res. - // some lines - // some lines - // some lines - // some lines - // some lines - // some lines - or_else(|_| Ok(ext_str.some_field)); + // some lines + // some lines + // some lines + // some lines + // some lines + // some lines + or_else(|_| Ok(ext_str.some_field)); // neither bind_instead_of_map nor unnecessary_lazy_eval applies here let _: Result = res.and_then(|x| Err(x)); diff --git a/tests/ui/unnecessary_lazy_eval.stderr b/tests/ui/unnecessary_lazy_eval.stderr index 8a9ece4aa7e5..0339755442c5 100644 --- a/tests/ui/unnecessary_lazy_eval.stderr +++ b/tests/ui/unnecessary_lazy_eval.stderr @@ -1,5 +1,5 @@ error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:48:13 + --> $DIR/unnecessary_lazy_eval.rs:56:13 | LL | let _ = opt.unwrap_or_else(|| 2); | ^^^^-------------------- @@ -9,7 +9,7 @@ LL | let _ = opt.unwrap_or_else(|| 2); = note: `-D clippy::unnecessary-lazy-evaluations` implied by `-D warnings` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:49:13 + --> $DIR/unnecessary_lazy_eval.rs:57:13 | LL | let _ = opt.unwrap_or_else(|| astronomers_pi); | ^^^^--------------------------------- @@ -17,7 +17,7 @@ LL | let _ = opt.unwrap_or_else(|| astronomers_pi); | help: use `unwrap_or(..)` instead: `unwrap_or(astronomers_pi)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:50:13 + --> $DIR/unnecessary_lazy_eval.rs:58:13 | LL | let _ = opt.unwrap_or_else(|| ext_str.some_field); | ^^^^------------------------------------- @@ -25,7 +25,7 @@ LL | let _ = opt.unwrap_or_else(|| ext_str.some_field); | help: use `unwrap_or(..)` instead: `unwrap_or(ext_str.some_field)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:52:13 + --> $DIR/unnecessary_lazy_eval.rs:60:13 | LL | let _ = opt.and_then(|_| ext_opt); | ^^^^--------------------- @@ -33,7 +33,7 @@ LL | let _ = opt.and_then(|_| ext_opt); | help: use `and(..)` instead: `and(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:53:13 + --> $DIR/unnecessary_lazy_eval.rs:61:13 | LL | let _ = opt.or_else(|| ext_opt); | ^^^^------------------- @@ -41,7 +41,7 @@ LL | let _ = opt.or_else(|| ext_opt); | help: use `or(..)` instead: `or(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:54:13 + --> $DIR/unnecessary_lazy_eval.rs:62:13 | LL | let _ = opt.or_else(|| None); | ^^^^---------------- @@ -49,7 +49,7 @@ LL | let _ = opt.or_else(|| None); | help: use `or(..)` instead: `or(None)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:55:13 + --> $DIR/unnecessary_lazy_eval.rs:63:13 | LL | let _ = opt.get_or_insert_with(|| 2); | ^^^^------------------------ @@ -57,7 +57,7 @@ LL | let _ = opt.get_or_insert_with(|| 2); | help: use `get_or_insert(..)` instead: `get_or_insert(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:56:13 + --> $DIR/unnecessary_lazy_eval.rs:64:13 | LL | let _ = opt.ok_or_else(|| 2); | ^^^^---------------- @@ -65,7 +65,7 @@ LL | let _ = opt.ok_or_else(|| 2); | help: use `ok_or(..)` instead: `ok_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:57:13 + --> $DIR/unnecessary_lazy_eval.rs:65:13 | LL | let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2))); | ^^^^^^^^^^^^^^^^^------------------------------- @@ -73,7 +73,7 @@ LL | let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2))); | help: use `unwrap_or(..)` instead: `unwrap_or(Some((1, 2)))` error: unnecessary closure used with `bool::then` - --> $DIR/unnecessary_lazy_eval.rs:58:13 + --> $DIR/unnecessary_lazy_eval.rs:66:13 | LL | let _ = cond.then(|| astronomers_pi); | ^^^^^----------------------- @@ -81,7 +81,7 @@ LL | let _ = cond.then(|| astronomers_pi); | help: use `then_some(..)` instead: `then_some(astronomers_pi)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:61:13 + --> $DIR/unnecessary_lazy_eval.rs:69:13 | LL | let _ = Some(10).unwrap_or_else(|| 2); | ^^^^^^^^^-------------------- @@ -89,7 +89,7 @@ LL | let _ = Some(10).unwrap_or_else(|| 2); | help: use `unwrap_or(..)` instead: `unwrap_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:62:13 + --> $DIR/unnecessary_lazy_eval.rs:70:13 | LL | let _ = Some(10).and_then(|_| ext_opt); | ^^^^^^^^^--------------------- @@ -97,7 +97,7 @@ LL | let _ = Some(10).and_then(|_| ext_opt); | help: use `and(..)` instead: `and(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:63:28 + --> $DIR/unnecessary_lazy_eval.rs:71:28 | LL | let _: Option = None.or_else(|| ext_opt); | ^^^^^------------------- @@ -105,7 +105,7 @@ LL | let _: Option = None.or_else(|| ext_opt); | help: use `or(..)` instead: `or(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:64:13 + --> $DIR/unnecessary_lazy_eval.rs:72:13 | LL | let _ = None.get_or_insert_with(|| 2); | ^^^^^------------------------ @@ -113,7 +113,7 @@ LL | let _ = None.get_or_insert_with(|| 2); | help: use `get_or_insert(..)` instead: `get_or_insert(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:65:35 + --> $DIR/unnecessary_lazy_eval.rs:73:35 | LL | let _: Result = None.ok_or_else(|| 2); | ^^^^^---------------- @@ -121,7 +121,7 @@ LL | let _: Result = None.ok_or_else(|| 2); | help: use `ok_or(..)` instead: `ok_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:66:28 + --> $DIR/unnecessary_lazy_eval.rs:74:28 | LL | let _: Option = None.or_else(|| None); | ^^^^^---------------- @@ -129,7 +129,7 @@ LL | let _: Option = None.or_else(|| None); | help: use `or(..)` instead: `or(None)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:69:13 + --> $DIR/unnecessary_lazy_eval.rs:77:13 | LL | let _ = deep.0.unwrap_or_else(|| 2); | ^^^^^^^-------------------- @@ -137,7 +137,7 @@ LL | let _ = deep.0.unwrap_or_else(|| 2); | help: use `unwrap_or(..)` instead: `unwrap_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:70:13 + --> $DIR/unnecessary_lazy_eval.rs:78:13 | LL | let _ = deep.0.and_then(|_| ext_opt); | ^^^^^^^--------------------- @@ -145,7 +145,7 @@ LL | let _ = deep.0.and_then(|_| ext_opt); | help: use `and(..)` instead: `and(ext_opt)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:71:13 + --> $DIR/unnecessary_lazy_eval.rs:79:13 | LL | let _ = deep.0.or_else(|| None); | ^^^^^^^---------------- @@ -153,7 +153,7 @@ LL | let _ = deep.0.or_else(|| None); | help: use `or(..)` instead: `or(None)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:72:13 + --> $DIR/unnecessary_lazy_eval.rs:80:13 | LL | let _ = deep.0.get_or_insert_with(|| 2); | ^^^^^^^------------------------ @@ -161,7 +161,7 @@ LL | let _ = deep.0.get_or_insert_with(|| 2); | help: use `get_or_insert(..)` instead: `get_or_insert(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:73:13 + --> $DIR/unnecessary_lazy_eval.rs:81:13 | LL | let _ = deep.0.ok_or_else(|| 2); | ^^^^^^^---------------- @@ -169,7 +169,7 @@ LL | let _ = deep.0.ok_or_else(|| 2); | help: use `ok_or(..)` instead: `ok_or(2)` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:96:28 + --> $DIR/unnecessary_lazy_eval.rs:105:28 | LL | let _: Option = None.or_else(|| Some(3)); | ^^^^^------------------- @@ -177,7 +177,7 @@ LL | let _: Option = None.or_else(|| Some(3)); | help: use `or(..)` instead: `or(Some(3))` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:97:13 + --> $DIR/unnecessary_lazy_eval.rs:106:13 | LL | let _ = deep.0.or_else(|| Some(3)); | ^^^^^^^------------------- @@ -185,7 +185,7 @@ LL | let _ = deep.0.or_else(|| Some(3)); | help: use `or(..)` instead: `or(Some(3))` error: unnecessary closure used to substitute value for `Option::None` - --> $DIR/unnecessary_lazy_eval.rs:98:13 + --> $DIR/unnecessary_lazy_eval.rs:107:13 | LL | let _ = opt.or_else(|| Some(3)); | ^^^^------------------- @@ -193,7 +193,7 @@ LL | let _ = opt.or_else(|| Some(3)); | help: use `or(..)` instead: `or(Some(3))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:104:13 + --> $DIR/unnecessary_lazy_eval.rs:113:13 | LL | let _ = res2.unwrap_or_else(|_| 2); | ^^^^^--------------------- @@ -201,7 +201,7 @@ LL | let _ = res2.unwrap_or_else(|_| 2); | help: use `unwrap_or(..)` instead: `unwrap_or(2)` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:105:13 + --> $DIR/unnecessary_lazy_eval.rs:114:13 | LL | let _ = res2.unwrap_or_else(|_| astronomers_pi); | ^^^^^---------------------------------- @@ -209,7 +209,7 @@ LL | let _ = res2.unwrap_or_else(|_| astronomers_pi); | help: use `unwrap_or(..)` instead: `unwrap_or(astronomers_pi)` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:106:13 + --> $DIR/unnecessary_lazy_eval.rs:115:13 | LL | let _ = res2.unwrap_or_else(|_| ext_str.some_field); | ^^^^^-------------------------------------- @@ -217,7 +217,7 @@ LL | let _ = res2.unwrap_or_else(|_| ext_str.some_field); | help: use `unwrap_or(..)` instead: `unwrap_or(ext_str.some_field)` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:128:35 + --> $DIR/unnecessary_lazy_eval.rs:137:35 | LL | let _: Result = res.and_then(|_| Err(2)); | ^^^^-------------------- @@ -225,7 +225,7 @@ LL | let _: Result = res.and_then(|_| Err(2)); | help: use `and(..)` instead: `and(Err(2))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:129:35 + --> $DIR/unnecessary_lazy_eval.rs:138:35 | LL | let _: Result = res.and_then(|_| Err(astronomers_pi)); | ^^^^--------------------------------- @@ -233,7 +233,7 @@ LL | let _: Result = res.and_then(|_| Err(astronomers_pi)); | help: use `and(..)` instead: `and(Err(astronomers_pi))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:130:35 + --> $DIR/unnecessary_lazy_eval.rs:139:35 | LL | let _: Result = res.and_then(|_| Err(ext_str.some_field)); | ^^^^------------------------------------- @@ -241,7 +241,7 @@ LL | let _: Result = res.and_then(|_| Err(ext_str.some_field)) | help: use `and(..)` instead: `and(Err(ext_str.some_field))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:132:35 + --> $DIR/unnecessary_lazy_eval.rs:141:35 | LL | let _: Result = res.or_else(|_| Ok(2)); | ^^^^------------------ @@ -249,7 +249,7 @@ LL | let _: Result = res.or_else(|_| Ok(2)); | help: use `or(..)` instead: `or(Ok(2))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:133:35 + --> $DIR/unnecessary_lazy_eval.rs:142:35 | LL | let _: Result = res.or_else(|_| Ok(astronomers_pi)); | ^^^^------------------------------- @@ -257,7 +257,7 @@ LL | let _: Result = res.or_else(|_| Ok(astronomers_pi)); | help: use `or(..)` instead: `or(Ok(astronomers_pi))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:134:35 + --> $DIR/unnecessary_lazy_eval.rs:143:35 | LL | let _: Result = res.or_else(|_| Ok(ext_str.some_field)); | ^^^^----------------------------------- @@ -265,19 +265,19 @@ LL | let _: Result = res.or_else(|_| Ok(ext_str.some_field)); | help: use `or(..)` instead: `or(Ok(ext_str.some_field))` error: unnecessary closure used to substitute value for `Result::Err` - --> $DIR/unnecessary_lazy_eval.rs:135:35 + --> $DIR/unnecessary_lazy_eval.rs:144:35 | LL | let _: Result = res. | ___________________________________^ -LL | | // some lines -LL | | // some lines -LL | | // some lines +LL | | // some lines +LL | | // some lines +LL | | // some lines ... | -LL | | // some lines -LL | | or_else(|_| Ok(ext_str.some_field)); - | |_________----------------------------------^ - | | - | help: use `or(..)` instead: `or(Ok(ext_str.some_field))` +LL | | // some lines +LL | | or_else(|_| Ok(ext_str.some_field)); + | |_____----------------------------------^ + | | + | help: use `or(..)` instead: `or(Ok(ext_str.some_field))` error: aborting due to 34 previous errors diff --git a/tests/ui/unnecessary_operation.fixed b/tests/ui/unnecessary_operation.fixed index bf0ec8deb345..d37163570abe 100644 --- a/tests/ui/unnecessary_operation.fixed +++ b/tests/ui/unnecessary_operation.fixed @@ -76,4 +76,13 @@ fn main() { DropStruct { ..get_drop_struct() }; DropEnum::Tuple(get_number()); DropEnum::Struct { field: get_number() }; + + // Issue #9954 + fn one() -> i8 { + 1 + } + macro_rules! use_expr { + ($($e:expr),*) => {{ $($e;)* }} + } + use_expr!(isize::MIN / -(one() as isize), i8::MIN / -one()); } diff --git a/tests/ui/unnecessary_operation.rs b/tests/ui/unnecessary_operation.rs index 08cb9ab522ee..a14fd4bca0ef 100644 --- a/tests/ui/unnecessary_operation.rs +++ b/tests/ui/unnecessary_operation.rs @@ -80,4 +80,13 @@ fn main() { DropStruct { ..get_drop_struct() }; DropEnum::Tuple(get_number()); DropEnum::Struct { field: get_number() }; + + // Issue #9954 + fn one() -> i8 { + 1 + } + macro_rules! use_expr { + ($($e:expr),*) => {{ $($e;)* }} + } + use_expr!(isize::MIN / -(one() as isize), i8::MIN / -one()); } diff --git a/tests/ui/unnecessary_safety_comment.rs b/tests/ui/unnecessary_safety_comment.rs new file mode 100644 index 000000000000..7fefea7051d6 --- /dev/null +++ b/tests/ui/unnecessary_safety_comment.rs @@ -0,0 +1,51 @@ +#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)] +#![allow(clippy::let_unit_value, clippy::missing_safety_doc)] + +mod unsafe_items_invalid_comment { + // SAFETY: + const CONST: u32 = 0; + // SAFETY: + static STATIC: u32 = 0; + // SAFETY: + struct Struct; + // SAFETY: + enum Enum {} + // SAFETY: + mod module {} +} + +mod unnecessary_from_macro { + trait T {} + + macro_rules! no_safety_comment { + ($t:ty) => { + impl T for $t {} + }; + } + + // FIXME: This is not caught + // Safety: unnecessary + no_safety_comment!(()); + + macro_rules! with_safety_comment { + ($t:ty) => { + // Safety: unnecessary + impl T for $t {} + }; + } + + with_safety_comment!(i32); +} + +fn unnecessary_on_stmt_and_expr() -> u32 { + // SAFETY: unnecessary + let num = 42; + + // SAFETY: unnecessary + if num > 24 {} + + // SAFETY: unnecessary + 24 +} + +fn main() {} diff --git a/tests/ui/unnecessary_safety_comment.stderr b/tests/ui/unnecessary_safety_comment.stderr new file mode 100644 index 000000000000..7b2af67d64c7 --- /dev/null +++ b/tests/ui/unnecessary_safety_comment.stderr @@ -0,0 +1,115 @@ +error: constant item has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:6:5 + | +LL | const CONST: u32 = 0; + | ^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:5:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + = note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings` + +error: static item has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:8:5 + | +LL | static STATIC: u32 = 0; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:7:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: struct has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:10:5 + | +LL | struct Struct; + | ^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:9:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: enum has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:12:5 + | +LL | enum Enum {} + | ^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:11:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: module has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:14:5 + | +LL | mod module {} + | ^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:13:5 + | +LL | // SAFETY: + | ^^^^^^^^^^ + +error: impl has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:33:13 + | +LL | impl T for $t {} + | ^^^^^^^^^^^^^^^^ +... +LL | with_safety_comment!(i32); + | ------------------------- in this macro invocation + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:32:13 + | +LL | // Safety: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: expression has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:48:5 + | +LL | 24 + | ^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:47:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: statement has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:42:5 + | +LL | let num = 42; + | ^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:41:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: statement has unnecessary safety comment + --> $DIR/unnecessary_safety_comment.rs:45:5 + | +LL | if num > 24 {} + | ^^^^^^^^^^^^^^ + | +help: consider removing the safety comment + --> $DIR/unnecessary_safety_comment.rs:44:5 + | +LL | // SAFETY: unnecessary + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 9 previous errors + diff --git a/tests/ui/unnecessary_to_owned.fixed b/tests/ui/unnecessary_to_owned.fixed index fe09aad06bc8..ddeda795f817 100644 --- a/tests/ui/unnecessary_to_owned.fixed +++ b/tests/ui/unnecessary_to_owned.fixed @@ -2,7 +2,6 @@ #![allow(clippy::needless_borrow, clippy::ptr_arg)] #![warn(clippy::unnecessary_to_owned)] -#![feature(custom_inner_attributes)] use std::borrow::Cow; use std::ffi::{CStr, CString, OsStr, OsString}; @@ -215,14 +214,14 @@ fn get_file_path(_file_type: &FileType) -> Result, V: ?Sized>(K, PhantomData); + + impl, V: ?Sized> Key { + pub fn new(key: K) -> Key { + Key(key, PhantomData) + } + } + + pub fn pkh(pkh: &[u8]) -> Key, String> { + Key::new([b"pkh-", pkh].concat().to_vec()) + } +} + +mod issue_9771b { + #![allow(dead_code)] + + pub struct Key>(K); + + pub fn from(c: &[u8]) -> Key> { + let v = [c].concat(); + Key(v.to_vec()) + } +} diff --git a/tests/ui/unnecessary_to_owned.rs b/tests/ui/unnecessary_to_owned.rs index 3de6d0903c0f..95d2576733cd 100644 --- a/tests/ui/unnecessary_to_owned.rs +++ b/tests/ui/unnecessary_to_owned.rs @@ -2,7 +2,6 @@ #![allow(clippy::needless_borrow, clippy::ptr_arg)] #![warn(clippy::unnecessary_to_owned)] -#![feature(custom_inner_attributes)] use std::borrow::Cow; use std::ffi::{CStr, CString, OsStr, OsString}; @@ -215,14 +214,14 @@ fn get_file_path(_file_type: &FileType) -> Result, V: ?Sized>(K, PhantomData); + + impl, V: ?Sized> Key { + pub fn new(key: K) -> Key { + Key(key, PhantomData) + } + } + + pub fn pkh(pkh: &[u8]) -> Key, String> { + Key::new([b"pkh-", pkh].concat().to_vec()) + } +} + +mod issue_9771b { + #![allow(dead_code)] + + pub struct Key>(K); + + pub fn from(c: &[u8]) -> Key> { + let v = [c].concat(); + Key(v.to_vec()) + } +} diff --git a/tests/ui/unnecessary_to_owned.stderr b/tests/ui/unnecessary_to_owned.stderr index 02bf45a33fbe..4918fe355986 100644 --- a/tests/ui/unnecessary_to_owned.stderr +++ b/tests/ui/unnecessary_to_owned.stderr @@ -1,66 +1,66 @@ error: redundant clone - --> $DIR/unnecessary_to_owned.rs:151:64 + --> $DIR/unnecessary_to_owned.rs:150:64 | LL | require_c_str(&CString::from_vec_with_nul(vec![0]).unwrap().to_owned()); | ^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:151:20 + --> $DIR/unnecessary_to_owned.rs:150:20 | LL | require_c_str(&CString::from_vec_with_nul(vec![0]).unwrap().to_owned()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: `-D clippy::redundant-clone` implied by `-D warnings` error: redundant clone - --> $DIR/unnecessary_to_owned.rs:152:40 + --> $DIR/unnecessary_to_owned.rs:151:40 | LL | require_os_str(&OsString::from("x").to_os_string()); | ^^^^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:152:21 + --> $DIR/unnecessary_to_owned.rs:151:21 | LL | require_os_str(&OsString::from("x").to_os_string()); | ^^^^^^^^^^^^^^^^^^^ error: redundant clone - --> $DIR/unnecessary_to_owned.rs:153:48 + --> $DIR/unnecessary_to_owned.rs:152:48 | LL | require_path(&std::path::PathBuf::from("x").to_path_buf()); | ^^^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:153:19 + --> $DIR/unnecessary_to_owned.rs:152:19 | LL | require_path(&std::path::PathBuf::from("x").to_path_buf()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant clone - --> $DIR/unnecessary_to_owned.rs:154:35 + --> $DIR/unnecessary_to_owned.rs:153:35 | LL | require_str(&String::from("x").to_string()); | ^^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:154:18 + --> $DIR/unnecessary_to_owned.rs:153:18 | LL | require_str(&String::from("x").to_string()); | ^^^^^^^^^^^^^^^^^ error: redundant clone - --> $DIR/unnecessary_to_owned.rs:155:39 + --> $DIR/unnecessary_to_owned.rs:154:39 | LL | require_slice(&[String::from("x")].to_owned()); | ^^^^^^^^^^^ help: remove this | note: this value is dropped without further use - --> $DIR/unnecessary_to_owned.rs:155:20 + --> $DIR/unnecessary_to_owned.rs:154:20 | LL | require_slice(&[String::from("x")].to_owned()); | ^^^^^^^^^^^^^^^^^^^ error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:60:36 + --> $DIR/unnecessary_to_owned.rs:59:36 | LL | require_c_str(&Cow::from(c_str).into_owned()); | ^^^^^^^^^^^^^ help: remove this @@ -68,415 +68,415 @@ LL | require_c_str(&Cow::from(c_str).into_owned()); = note: `-D clippy::unnecessary-to-owned` implied by `-D warnings` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:61:19 + --> $DIR/unnecessary_to_owned.rs:60:19 | LL | require_c_str(&c_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_os_string` - --> $DIR/unnecessary_to_owned.rs:63:20 + --> $DIR/unnecessary_to_owned.rs:62:20 | LL | require_os_str(&os_str.to_os_string()); | ^^^^^^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:64:38 + --> $DIR/unnecessary_to_owned.rs:63:38 | LL | require_os_str(&Cow::from(os_str).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:65:20 + --> $DIR/unnecessary_to_owned.rs:64:20 | LL | require_os_str(&os_str.to_owned()); | ^^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_path_buf` - --> $DIR/unnecessary_to_owned.rs:67:18 + --> $DIR/unnecessary_to_owned.rs:66:18 | LL | require_path(&path.to_path_buf()); | ^^^^^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:68:34 + --> $DIR/unnecessary_to_owned.rs:67:34 | LL | require_path(&Cow::from(path).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:69:18 + --> $DIR/unnecessary_to_owned.rs:68:18 | LL | require_path(&path.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:71:17 + --> $DIR/unnecessary_to_owned.rs:70:17 | LL | require_str(&s.to_string()); | ^^^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:72:30 + --> $DIR/unnecessary_to_owned.rs:71:30 | LL | require_str(&Cow::from(s).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:73:17 + --> $DIR/unnecessary_to_owned.rs:72:17 | LL | require_str(&s.to_owned()); | ^^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:74:17 + --> $DIR/unnecessary_to_owned.rs:73:17 | LL | require_str(&x_ref.to_string()); | ^^^^^^^^^^^^^^^^^^ help: use: `x_ref.as_ref()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:76:19 + --> $DIR/unnecessary_to_owned.rs:75:19 | LL | require_slice(&slice.to_vec()); | ^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:77:36 + --> $DIR/unnecessary_to_owned.rs:76:36 | LL | require_slice(&Cow::from(slice).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:78:19 + --> $DIR/unnecessary_to_owned.rs:77:19 | LL | require_slice(&array.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `array.as_ref()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:79:19 + --> $DIR/unnecessary_to_owned.rs:78:19 | LL | require_slice(&array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref.as_ref()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:80:19 + --> $DIR/unnecessary_to_owned.rs:79:19 | LL | require_slice(&slice.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `into_owned` - --> $DIR/unnecessary_to_owned.rs:83:42 + --> $DIR/unnecessary_to_owned.rs:82:42 | LL | require_x(&Cow::::Owned(x.clone()).into_owned()); | ^^^^^^^^^^^^^ help: remove this error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:86:25 + --> $DIR/unnecessary_to_owned.rs:85:25 | LL | require_deref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:87:26 + --> $DIR/unnecessary_to_owned.rs:86:26 | LL | require_deref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:88:24 + --> $DIR/unnecessary_to_owned.rs:87:24 | LL | require_deref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:89:23 + --> $DIR/unnecessary_to_owned.rs:88:23 | LL | require_deref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:90:25 + --> $DIR/unnecessary_to_owned.rs:89:25 | LL | require_deref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:92:30 + --> $DIR/unnecessary_to_owned.rs:91:30 | LL | require_impl_deref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:93:31 + --> $DIR/unnecessary_to_owned.rs:92:31 | LL | require_impl_deref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:94:29 + --> $DIR/unnecessary_to_owned.rs:93:29 | LL | require_impl_deref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:95:28 + --> $DIR/unnecessary_to_owned.rs:94:28 | LL | require_impl_deref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:96:30 + --> $DIR/unnecessary_to_owned.rs:95:30 | LL | require_impl_deref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:98:29 + --> $DIR/unnecessary_to_owned.rs:97:29 | LL | require_deref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:98:43 + --> $DIR/unnecessary_to_owned.rs:97:43 | LL | require_deref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:99:29 + --> $DIR/unnecessary_to_owned.rs:98:29 | LL | require_deref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:99:47 + --> $DIR/unnecessary_to_owned.rs:98:47 | LL | require_deref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:101:26 + --> $DIR/unnecessary_to_owned.rs:100:26 | LL | require_as_ref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:102:27 + --> $DIR/unnecessary_to_owned.rs:101:27 | LL | require_as_ref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:103:25 + --> $DIR/unnecessary_to_owned.rs:102:25 | LL | require_as_ref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:104:24 + --> $DIR/unnecessary_to_owned.rs:103:24 | LL | require_as_ref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:105:24 + --> $DIR/unnecessary_to_owned.rs:104:24 | LL | require_as_ref_str(x.to_owned()); | ^^^^^^^^^^^^ help: use: `&x` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:106:26 + --> $DIR/unnecessary_to_owned.rs:105:26 | LL | require_as_ref_slice(array.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:107:26 + --> $DIR/unnecessary_to_owned.rs:106:26 | LL | require_as_ref_slice(array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:108:26 + --> $DIR/unnecessary_to_owned.rs:107:26 | LL | require_as_ref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:110:31 + --> $DIR/unnecessary_to_owned.rs:109:31 | LL | require_impl_as_ref_c_str(c_str.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `c_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:111:32 + --> $DIR/unnecessary_to_owned.rs:110:32 | LL | require_impl_as_ref_os_str(os_str.to_owned()); | ^^^^^^^^^^^^^^^^^ help: use: `os_str` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:112:30 + --> $DIR/unnecessary_to_owned.rs:111:30 | LL | require_impl_as_ref_path(path.to_owned()); | ^^^^^^^^^^^^^^^ help: use: `path` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:113:29 + --> $DIR/unnecessary_to_owned.rs:112:29 | LL | require_impl_as_ref_str(s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:114:29 + --> $DIR/unnecessary_to_owned.rs:113:29 | LL | require_impl_as_ref_str(x.to_owned()); | ^^^^^^^^^^^^ help: use: `&x` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:115:31 + --> $DIR/unnecessary_to_owned.rs:114:31 | LL | require_impl_as_ref_slice(array.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:116:31 + --> $DIR/unnecessary_to_owned.rs:115:31 | LL | require_impl_as_ref_slice(array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:117:31 + --> $DIR/unnecessary_to_owned.rs:116:31 | LL | require_impl_as_ref_slice(slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:119:30 + --> $DIR/unnecessary_to_owned.rs:118:30 | LL | require_as_ref_str_slice(s.to_owned(), array.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:119:44 + --> $DIR/unnecessary_to_owned.rs:118:44 | LL | require_as_ref_str_slice(s.to_owned(), array.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:120:30 + --> $DIR/unnecessary_to_owned.rs:119:30 | LL | require_as_ref_str_slice(s.to_owned(), array_ref.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:120:44 + --> $DIR/unnecessary_to_owned.rs:119:44 | LL | require_as_ref_str_slice(s.to_owned(), array_ref.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:121:30 + --> $DIR/unnecessary_to_owned.rs:120:30 | LL | require_as_ref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:121:44 + --> $DIR/unnecessary_to_owned.rs:120:44 | LL | require_as_ref_str_slice(s.to_owned(), slice.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:122:30 + --> $DIR/unnecessary_to_owned.rs:121:30 | LL | require_as_ref_slice_str(array.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `array` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:122:48 + --> $DIR/unnecessary_to_owned.rs:121:48 | LL | require_as_ref_slice_str(array.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:123:30 + --> $DIR/unnecessary_to_owned.rs:122:30 | LL | require_as_ref_slice_str(array_ref.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^^^^^ help: use: `array_ref` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:123:52 + --> $DIR/unnecessary_to_owned.rs:122:52 | LL | require_as_ref_slice_str(array_ref.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:124:30 + --> $DIR/unnecessary_to_owned.rs:123:30 | LL | require_as_ref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^^^^^ help: use: `slice` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:124:48 + --> $DIR/unnecessary_to_owned.rs:123:48 | LL | require_as_ref_slice_str(slice.to_owned(), s.to_owned()); | ^^^^^^^^^^^^ help: use: `s` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:126:20 + --> $DIR/unnecessary_to_owned.rs:125:20 | LL | let _ = x.join(&x_ref.to_string()); | ^^^^^^^^^^^^^^^^^^ help: use: `x_ref` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:128:13 + --> $DIR/unnecessary_to_owned.rs:127:13 | LL | let _ = slice.to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:129:13 + --> $DIR/unnecessary_to_owned.rs:128:13 | LL | let _ = slice.to_owned().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:130:13 + --> $DIR/unnecessary_to_owned.rs:129:13 | LL | let _ = [std::path::PathBuf::new()][..].to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:131:13 + --> $DIR/unnecessary_to_owned.rs:130:13 | LL | let _ = [std::path::PathBuf::new()][..].to_owned().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:133:13 + --> $DIR/unnecessary_to_owned.rs:132:13 | LL | let _ = IntoIterator::into_iter(slice.to_vec()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:134:13 + --> $DIR/unnecessary_to_owned.rs:133:13 | LL | let _ = IntoIterator::into_iter(slice.to_owned()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:135:13 + --> $DIR/unnecessary_to_owned.rs:134:13 | LL | let _ = IntoIterator::into_iter([std::path::PathBuf::new()][..].to_vec()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_owned` - --> $DIR/unnecessary_to_owned.rs:136:13 + --> $DIR/unnecessary_to_owned.rs:135:13 | LL | let _ = IntoIterator::into_iter([std::path::PathBuf::new()][..].to_owned()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:198:14 + --> $DIR/unnecessary_to_owned.rs:197:14 | LL | for t in file_types.to_vec() { | ^^^^^^^^^^^^^^^^^^^ @@ -492,25 +492,25 @@ LL + let path = match get_file_path(t) { | error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:221:14 + --> $DIR/unnecessary_to_owned.rs:220:14 | LL | let _ = &["x"][..].to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().cloned()` error: unnecessary use of `to_vec` - --> $DIR/unnecessary_to_owned.rs:226:14 + --> $DIR/unnecessary_to_owned.rs:225:14 | LL | let _ = &["x"][..].to_vec().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().copied()` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:273:24 + --> $DIR/unnecessary_to_owned.rs:272:24 | LL | Box::new(build(y.to_string())) | ^^^^^^^^^^^^^ help: use: `y` error: unnecessary use of `to_string` - --> $DIR/unnecessary_to_owned.rs:381:12 + --> $DIR/unnecessary_to_owned.rs:380:12 | LL | id("abc".to_string()) | ^^^^^^^^^^^^^^^^^ help: use: `"abc"` diff --git a/tests/ui/doc_unnecessary_unsafe.rs b/tests/ui/unnecessary_unsafety_doc.rs similarity index 98% rename from tests/ui/doc_unnecessary_unsafe.rs rename to tests/ui/unnecessary_unsafety_doc.rs index d9e9363b0f4b..c160e31afd33 100644 --- a/tests/ui/doc_unnecessary_unsafe.rs +++ b/tests/ui/unnecessary_unsafety_doc.rs @@ -1,6 +1,7 @@ // aux-build:doc_unsafe_macros.rs #![allow(clippy::let_unit_value)] +#![warn(clippy::unnecessary_safety_doc)] #[macro_use] extern crate doc_unsafe_macros; diff --git a/tests/ui/doc_unnecessary_unsafe.stderr b/tests/ui/unnecessary_unsafety_doc.stderr similarity index 81% rename from tests/ui/doc_unnecessary_unsafe.stderr rename to tests/ui/unnecessary_unsafety_doc.stderr index 83b2efbb346b..72898c93fa11 100644 --- a/tests/ui/doc_unnecessary_unsafe.stderr +++ b/tests/ui/unnecessary_unsafety_doc.stderr @@ -1,5 +1,5 @@ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:18:1 + --> $DIR/unnecessary_unsafety_doc.rs:19:1 | LL | pub fn apocalypse(universe: &mut ()) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -7,31 +7,31 @@ LL | pub fn apocalypse(universe: &mut ()) { = note: `-D clippy::unnecessary-safety-doc` implied by `-D warnings` error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:44:5 + --> $DIR/unnecessary_unsafety_doc.rs:45:5 | LL | pub fn republished() { | ^^^^^^^^^^^^^^^^^^^^ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:57:5 + --> $DIR/unnecessary_unsafety_doc.rs:58:5 | LL | fn documented(self); | ^^^^^^^^^^^^^^^^^^^^ error: docs for safe trait have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:67:1 + --> $DIR/unnecessary_unsafety_doc.rs:68:1 | LL | pub trait DocumentedSafeTrait { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:95:5 + --> $DIR/unnecessary_unsafety_doc.rs:96:5 | LL | pub fn documented() -> Self { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: safe function's docs have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:122:9 + --> $DIR/unnecessary_unsafety_doc.rs:123:9 | LL | pub fn drive() { | ^^^^^^^^^^^^^^ @@ -42,7 +42,7 @@ LL | very_safe!(); = note: this error originates in the macro `very_safe` (in Nightly builds, run with -Z macro-backtrace for more info) error: docs for safe trait have unnecessary `# Safety` section - --> $DIR/doc_unnecessary_unsafe.rs:146:1 + --> $DIR/unnecessary_unsafety_doc.rs:147:1 | LL | pub trait DocumentedSafeTraitWithImplementationHeader { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/unnested_or_patterns.fixed b/tests/ui/unnested_or_patterns.fixed index 9786c7b12128..0a8e7b34dfa4 100644 --- a/tests/ui/unnested_or_patterns.fixed +++ b/tests/ui/unnested_or_patterns.fixed @@ -1,6 +1,6 @@ // run-rustfix -#![feature(box_patterns, custom_inner_attributes)] +#![feature(box_patterns)] #![warn(clippy::unnested_or_patterns)] #![allow(clippy::cognitive_complexity, clippy::match_ref_pats, clippy::upper_case_acronyms)] #![allow(unreachable_patterns, irrefutable_let_patterns, unused)] @@ -34,14 +34,12 @@ fn main() { if let S { x: 0, y, .. } | S { y, x: 1 } = (S { x: 0, y: 1 }) {} } +#[clippy::msrv = "1.52"] fn msrv_1_52() { - #![clippy::msrv = "1.52"] - if let [1] | [52] = [0] {} } +#[clippy::msrv = "1.53"] fn msrv_1_53() { - #![clippy::msrv = "1.53"] - if let [1 | 53] = [0] {} } diff --git a/tests/ui/unnested_or_patterns.rs b/tests/ui/unnested_or_patterns.rs index f57322396d4a..2c454adfe89d 100644 --- a/tests/ui/unnested_or_patterns.rs +++ b/tests/ui/unnested_or_patterns.rs @@ -1,6 +1,6 @@ // run-rustfix -#![feature(box_patterns, custom_inner_attributes)] +#![feature(box_patterns)] #![warn(clippy::unnested_or_patterns)] #![allow(clippy::cognitive_complexity, clippy::match_ref_pats, clippy::upper_case_acronyms)] #![allow(unreachable_patterns, irrefutable_let_patterns, unused)] @@ -34,14 +34,12 @@ fn main() { if let S { x: 0, y, .. } | S { y, x: 1 } = (S { x: 0, y: 1 }) {} } +#[clippy::msrv = "1.52"] fn msrv_1_52() { - #![clippy::msrv = "1.52"] - if let [1] | [52] = [0] {} } +#[clippy::msrv = "1.53"] fn msrv_1_53() { - #![clippy::msrv = "1.53"] - if let [1] | [53] = [0] {} } diff --git a/tests/ui/unnested_or_patterns.stderr b/tests/ui/unnested_or_patterns.stderr index fbc12fff0b0e..a1f193db555a 100644 --- a/tests/ui/unnested_or_patterns.stderr +++ b/tests/ui/unnested_or_patterns.stderr @@ -176,7 +176,7 @@ LL | if let S { x: 0 | 1, y } = (S { x: 0, y: 1 }) {} | ~~~~~~~~~~~~~~~~~ error: unnested or-patterns - --> $DIR/unnested_or_patterns.rs:46:12 + --> $DIR/unnested_or_patterns.rs:44:12 | LL | if let [1] | [53] = [0] {} | ^^^^^^^^^^ diff --git a/tests/ui/unused_rounding.fixed b/tests/ui/unused_rounding.fixed index 38fe6c34cfec..f6f734c05ed5 100644 --- a/tests/ui/unused_rounding.fixed +++ b/tests/ui/unused_rounding.fixed @@ -11,4 +11,7 @@ fn main() { let _ = 3.3_f32.round(); let _ = 3.3_f64.round(); let _ = 3.0_f32; + + let _ = 3_3.0_0_f32; + let _ = 3_3.0_1_f64.round(); } diff --git a/tests/ui/unused_rounding.rs b/tests/ui/unused_rounding.rs index a5cac64d023a..a0267d8144aa 100644 --- a/tests/ui/unused_rounding.rs +++ b/tests/ui/unused_rounding.rs @@ -11,4 +11,7 @@ fn main() { let _ = 3.3_f32.round(); let _ = 3.3_f64.round(); let _ = 3.0_f32.round(); + + let _ = 3_3.0_0_f32.round(); + let _ = 3_3.0_1_f64.round(); } diff --git a/tests/ui/unused_rounding.stderr b/tests/ui/unused_rounding.stderr index 1eeb5d1de883..b867996fe576 100644 --- a/tests/ui/unused_rounding.stderr +++ b/tests/ui/unused_rounding.stderr @@ -24,5 +24,11 @@ error: used the `round` method with a whole number float LL | let _ = 3.0_f32.round(); | ^^^^^^^^^^^^^^^ help: remove the `round` method call: `3.0_f32` -error: aborting due to 4 previous errors +error: used the `round` method with a whole number float + --> $DIR/unused_rounding.rs:15:13 + | +LL | let _ = 3_3.0_0_f32.round(); + | ^^^^^^^^^^^^^^^^^^^ help: remove the `round` method call: `3_3.0_0_f32` + +error: aborting due to 5 previous errors diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index 3b54fe9d5ff3..0a6166571ebe 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -1,7 +1,6 @@ // run-rustfix // aux-build:proc_macro_derive.rs -#![feature(custom_inner_attributes)] #![warn(clippy::use_self)] #![allow(dead_code, unreachable_code)] #![allow( @@ -619,9 +618,8 @@ mod issue6902 { } } +#[clippy::msrv = "1.36"] fn msrv_1_36() { - #![clippy::msrv = "1.36"] - enum E { A, } @@ -635,9 +633,8 @@ fn msrv_1_36() { } } +#[clippy::msrv = "1.37"] fn msrv_1_37() { - #![clippy::msrv = "1.37"] - enum E { A, } diff --git a/tests/ui/use_self.rs b/tests/ui/use_self.rs index bf87633cd2d8..39c2b431f7fb 100644 --- a/tests/ui/use_self.rs +++ b/tests/ui/use_self.rs @@ -1,7 +1,6 @@ // run-rustfix // aux-build:proc_macro_derive.rs -#![feature(custom_inner_attributes)] #![warn(clippy::use_self)] #![allow(dead_code, unreachable_code)] #![allow( @@ -619,9 +618,8 @@ mod issue6902 { } } +#[clippy::msrv = "1.36"] fn msrv_1_36() { - #![clippy::msrv = "1.36"] - enum E { A, } @@ -635,9 +633,8 @@ fn msrv_1_36() { } } +#[clippy::msrv = "1.37"] fn msrv_1_37() { - #![clippy::msrv = "1.37"] - enum E { A, } diff --git a/tests/ui/use_self.stderr b/tests/ui/use_self.stderr index 16fb0609242c..48364c40c3b2 100644 --- a/tests/ui/use_self.stderr +++ b/tests/ui/use_self.stderr @@ -1,5 +1,5 @@ error: unnecessary structure name repetition - --> $DIR/use_self.rs:23:21 + --> $DIR/use_self.rs:22:21 | LL | fn new() -> Foo { | ^^^ help: use the applicable keyword: `Self` @@ -7,247 +7,247 @@ LL | fn new() -> Foo { = note: `-D clippy::use-self` implied by `-D warnings` error: unnecessary structure name repetition - --> $DIR/use_self.rs:24:13 + --> $DIR/use_self.rs:23:13 | LL | Foo {} | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:26:22 + --> $DIR/use_self.rs:25:22 | LL | fn test() -> Foo { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:27:13 + --> $DIR/use_self.rs:26:13 | LL | Foo::new() | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:32:25 + --> $DIR/use_self.rs:31:25 | LL | fn default() -> Foo { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:33:13 + --> $DIR/use_self.rs:32:13 | LL | Foo::new() | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:98:24 + --> $DIR/use_self.rs:97:24 | LL | fn bad(foos: &[Foo]) -> impl Iterator { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:98:55 + --> $DIR/use_self.rs:97:55 | LL | fn bad(foos: &[Foo]) -> impl Iterator { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:113:13 + --> $DIR/use_self.rs:112:13 | LL | TS(0) | ^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:148:29 + --> $DIR/use_self.rs:147:29 | LL | fn bar() -> Bar { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:149:21 + --> $DIR/use_self.rs:148:21 | LL | Bar { foo: Foo {} } | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:160:21 + --> $DIR/use_self.rs:159:21 | LL | fn baz() -> Foo { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:161:13 + --> $DIR/use_self.rs:160:13 | LL | Foo {} | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:178:21 + --> $DIR/use_self.rs:177:21 | LL | let _ = Enum::B(42); | ^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:179:21 + --> $DIR/use_self.rs:178:21 | LL | let _ = Enum::C { field: true }; | ^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:180:21 + --> $DIR/use_self.rs:179:21 | LL | let _ = Enum::A; | ^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:222:13 + --> $DIR/use_self.rs:221:13 | LL | nested::A::fun_1(); | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:223:13 + --> $DIR/use_self.rs:222:13 | LL | nested::A::A; | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:225:13 + --> $DIR/use_self.rs:224:13 | LL | nested::A {}; | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:244:13 + --> $DIR/use_self.rs:243:13 | LL | TestStruct::from_something() | ^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:258:25 + --> $DIR/use_self.rs:257:25 | LL | async fn g() -> S { | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:259:13 + --> $DIR/use_self.rs:258:13 | LL | S {} | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:263:16 + --> $DIR/use_self.rs:262:16 | LL | &p[S::A..S::B] | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:263:22 + --> $DIR/use_self.rs:262:22 | LL | &p[S::A..S::B] | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:286:29 + --> $DIR/use_self.rs:285:29 | LL | fn foo(value: T) -> Foo { | ^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:287:13 + --> $DIR/use_self.rs:286:13 | LL | Foo:: { value } | ^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:459:13 + --> $DIR/use_self.rs:458:13 | LL | A::new::(submod::B {}) | ^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:496:13 + --> $DIR/use_self.rs:495:13 | LL | S2::new() | ^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:533:17 + --> $DIR/use_self.rs:532:17 | LL | Foo::Bar => unimplemented!(), | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:534:17 + --> $DIR/use_self.rs:533:17 | LL | Foo::Baz => unimplemented!(), | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:540:20 + --> $DIR/use_self.rs:539:20 | LL | if let Foo::Bar = self { | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:564:17 + --> $DIR/use_self.rs:563:17 | LL | Something::Num(n) => *n, | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:565:17 + --> $DIR/use_self.rs:564:17 | LL | Something::TupleNums(n, _m) => *n, | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:566:17 + --> $DIR/use_self.rs:565:17 | LL | Something::StructNums { one, two: _ } => *one, | ^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:572:17 + --> $DIR/use_self.rs:571:17 | LL | crate::issue8845::Something::Num(n) => *n, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:573:17 + --> $DIR/use_self.rs:572:17 | LL | crate::issue8845::Something::TupleNums(n, _m) => *n, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:574:17 + --> $DIR/use_self.rs:573:17 | LL | crate::issue8845::Something::StructNums { one, two: _ } => *one, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:590:17 + --> $DIR/use_self.rs:589:17 | LL | let Foo(x) = self; | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:595:17 + --> $DIR/use_self.rs:594:17 | LL | let crate::issue8845::Foo(x) = self; | ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:602:17 + --> $DIR/use_self.rs:601:17 | LL | let Bar { x, .. } = self; | ^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:607:17 + --> $DIR/use_self.rs:606:17 | LL | let crate::issue8845::Bar { x, .. } = self; | ^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` error: unnecessary structure name repetition - --> $DIR/use_self.rs:648:17 + --> $DIR/use_self.rs:645:17 | LL | E::A => {}, | ^ help: use the applicable keyword: `Self` diff --git a/triagebot.toml b/triagebot.toml index 80c30393832c..acb476ee6962 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -4,9 +4,26 @@ allow-unauthenticated = [ "good-first-issue" ] -[assign] - # Allows shortcuts like `@rustbot ready` # # See https://github.com/rust-lang/triagebot/wiki/Shortcuts [shortcut] + +[autolabel."S-waiting-on-review"] +new_pr = true + +[assign] +contributing_url = "https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md" + +[assign.owners] +"/.github" = ["@flip1995"] +"*" = [ + "@flip1995", + "@Manishearth", + "@llogiq", + "@giraffate", + "@xFrednet", + "@Alexendoo", + "@dswij", + "@Jarcho", +] From 05477ff8df2056e7301f28930e72bd13e983504d Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sat, 26 Nov 2022 19:21:07 +0300 Subject: [PATCH 039/321] Fix manual_let_else produces a wrong suggestion with or-patterns --- clippy_lints/src/manual_let_else.rs | 7 ++++++- tests/ui/manual_let_else_match.rs | 7 +++++++ tests/ui/manual_let_else_match.stderr | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/manual_let_else.rs b/clippy_lints/src/manual_let_else.rs index 874d36ca9f4e..9c6f8b43c078 100644 --- a/clippy_lints/src/manual_let_else.rs +++ b/clippy_lints/src/manual_let_else.rs @@ -151,7 +151,12 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat: } else { format!("{{ {sn_else} }}") }; - let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};"); + let sn_bl = if matches!(pat.kind, PatKind::Or(..)) { + format!("({sn_pat})") + } else { + sn_pat.into_owned() + }; + let sugg = format!("let {sn_bl} = {sn_expr} else {else_bl};"); diag.span_suggestion(span, "consider writing", sugg, app); }, ); diff --git a/tests/ui/manual_let_else_match.rs b/tests/ui/manual_let_else_match.rs index 93c86ca24fea..28caed9d79df 100644 --- a/tests/ui/manual_let_else_match.rs +++ b/tests/ui/manual_let_else_match.rs @@ -64,6 +64,13 @@ fn fire() { Ok(v) => v, Err(()) => return, }; + + let f = Variant::Bar(1); + + let _value = match f { + Variant::Bar(_) | Variant::Baz(_) => (), + _ => return, + }; } fn not_fire() { diff --git a/tests/ui/manual_let_else_match.stderr b/tests/ui/manual_let_else_match.stderr index 38be5ac54547..cd5e9a9ac39c 100644 --- a/tests/ui/manual_let_else_match.stderr +++ b/tests/ui/manual_let_else_match.stderr @@ -25,7 +25,7 @@ LL | / let v = match h() { LL | | (Some(_), Some(_)) | (None, None) => continue, LL | | (Some(v), None) | (None, Some(v)) => v, LL | | }; - | |__________^ help: consider writing: `let (Some(v), None) | (None, Some(v)) = h() else { continue };` + | |__________^ help: consider writing: `let ((Some(v), None) | (None, Some(v))) = h() else { continue };` error: this could be rewritten as `let...else` --> $DIR/manual_let_else_match.rs:49:9 @@ -34,7 +34,7 @@ LL | / let v = match build_enum() { LL | | _ => continue, LL | | Variant::Bar(v) | Variant::Baz(v) => v, LL | | }; - | |__________^ help: consider writing: `let Variant::Bar(v) | Variant::Baz(v) = build_enum() else { continue };` + | |__________^ help: consider writing: `let (Variant::Bar(v) | Variant::Baz(v)) = build_enum() else { continue };` error: this could be rewritten as `let...else` --> $DIR/manual_let_else_match.rs:57:5 @@ -54,5 +54,14 @@ LL | | Err(()) => return, LL | | }; | |______^ help: consider writing: `let Ok(v) = f().map_err(|_| ()) else { return };` -error: aborting due to 6 previous errors +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else_match.rs:70:5 + | +LL | / let _value = match f { +LL | | Variant::Bar(_) | Variant::Baz(_) => (), +LL | | _ => return, +LL | | }; + | |______^ help: consider writing: `let (Variant::Bar(_) | Variant::Baz(_)) = f else { return };` + +error: aborting due to 7 previous errors From a21b5b25f6aef692968470b9e2aae7600d1f07b4 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Thu, 1 Dec 2022 12:17:38 -0500 Subject: [PATCH 040/321] Don't lint `string_lit_as_bytes` in match scrutinees --- clippy_lints/src/strings.rs | 38 +++++++++++++++++++----------- tests/ui/string_lit_as_bytes.fixed | 6 +++++ tests/ui/string_lit_as_bytes.rs | 6 +++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index f4705481d4e6..bc18cad6d381 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg}; use clippy_utils::source::{snippet, snippet_with_applicability}; use clippy_utils::ty::is_type_lang_item; +use clippy_utils::{get_expr_use_or_unification_node, peel_blocks, SpanlessEq}; use clippy_utils::{get_parent_expr, is_lint_allowed, match_function_call, method_calls, paths}; -use clippy_utils::{peel_blocks, SpanlessEq}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::def_id::DefId; -use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, QPath}; +use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, Node, QPath}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty; @@ -249,6 +249,7 @@ const MAX_LENGTH_BYTE_STRING_LIT: usize = 32; declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES, STRING_FROM_UTF8_AS_BYTES]); impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes { + #[expect(clippy::too_many_lines)] fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { use rustc_ast::LitKind; @@ -316,18 +317,27 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes { && lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT && !receiver.span.from_expansion() { - span_lint_and_sugg( - cx, - STRING_LIT_AS_BYTES, - e.span, - "calling `as_bytes()` on a string literal", - "consider using a byte string literal instead", - format!( - "b{}", - snippet_with_applicability(cx, receiver.span, r#""foo""#, &mut applicability) - ), - applicability, - ); + if let Some((parent, id)) = get_expr_use_or_unification_node(cx.tcx, e) + && let Node::Expr(parent) = parent + && let ExprKind::Match(scrutinee, ..) = parent.kind + && scrutinee.hir_id == id + { + // Don't lint. Byte strings produce `&[u8; N]` whereas `as_bytes()` produces + // `&[u8]`. This change would prevent matching with different sized slices. + } else { + span_lint_and_sugg( + cx, + STRING_LIT_AS_BYTES, + e.span, + "calling `as_bytes()` on a string literal", + "consider using a byte string literal instead", + format!( + "b{}", + snippet_with_applicability(cx, receiver.span, r#""foo""#, &mut applicability) + ), + applicability, + ); + } } } } diff --git a/tests/ui/string_lit_as_bytes.fixed b/tests/ui/string_lit_as_bytes.fixed index df2256e4f97d..506187fc1257 100644 --- a/tests/ui/string_lit_as_bytes.fixed +++ b/tests/ui/string_lit_as_bytes.fixed @@ -25,6 +25,12 @@ fn str_lit_as_bytes() { let includestr = include_bytes!("string_lit_as_bytes.rs"); let _ = b"string with newline\t\n"; + + let _ = match "x".as_bytes() { + b"xx" => 0, + [b'x', ..] => 1, + _ => 2, + }; } fn main() {} diff --git a/tests/ui/string_lit_as_bytes.rs b/tests/ui/string_lit_as_bytes.rs index c6bf8f732ed9..2c339f1ddb81 100644 --- a/tests/ui/string_lit_as_bytes.rs +++ b/tests/ui/string_lit_as_bytes.rs @@ -25,6 +25,12 @@ fn str_lit_as_bytes() { let includestr = include_str!("string_lit_as_bytes.rs").as_bytes(); let _ = "string with newline\t\n".as_bytes(); + + let _ = match "x".as_bytes() { + b"xx" => 0, + [b'x', ..] => 1, + _ => 2, + }; } fn main() {} From 47fb67fa086d88a878c11d7edc38f322f3b98fb9 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Thu, 1 Dec 2022 12:30:19 -0500 Subject: [PATCH 041/321] Don't lint `manual_assert` in `else if` --- clippy_lints/src/manual_assert.rs | 6 +++++- tests/ui/manual_assert.edition2018.fixed | 5 +++++ tests/ui/manual_assert.edition2021.fixed | 5 +++++ tests/ui/manual_assert.edition2021.stderr | 2 +- tests/ui/manual_assert.rs | 5 +++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/manual_assert.rs b/clippy_lints/src/manual_assert.rs index b8ed9b9ec18f..4277455a3a21 100644 --- a/clippy_lints/src/manual_assert.rs +++ b/clippy_lints/src/manual_assert.rs @@ -2,7 +2,7 @@ use crate::rustc_lint::LintContext; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::macros::{root_macro_call, FormatArgsExpn}; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{peel_blocks_with_stmt, span_extract_comment, sugg}; +use clippy_utils::{is_else_clause, peel_blocks_with_stmt, span_extract_comment, sugg}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, UnOp}; use rustc_lint::{LateContext, LateLintPass}; @@ -47,6 +47,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert { if cx.tcx.item_name(macro_call.def_id) == sym::panic; if !cx.tcx.sess.source_map().is_multiline(cond.span); if let Some(format_args) = FormatArgsExpn::find_nested(cx, then, macro_call.expn); + // Don't change `else if foo { panic!(..) }` to `else { assert!(foo, ..) }` as it just + // shuffles the condition around. + // Should this have a config value? + if !is_else_clause(cx.tcx, expr); then { let mut applicability = Applicability::MachineApplicable; let format_args_snip = snippet_with_applicability(cx, format_args.inputs_span(), "..", &mut applicability); diff --git a/tests/ui/manual_assert.edition2018.fixed b/tests/ui/manual_assert.edition2018.fixed index c9a819ba5354..638320dd6eec 100644 --- a/tests/ui/manual_assert.edition2018.fixed +++ b/tests/ui/manual_assert.edition2018.fixed @@ -62,6 +62,11 @@ fn main() { panic!("panic5"); } assert!(!a.is_empty(), "with expansion {}", one!()); + if a.is_empty() { + let _ = 0; + } else if a.len() == 1 { + panic!("panic6"); + } } fn issue7730(a: u8) { diff --git a/tests/ui/manual_assert.edition2021.fixed b/tests/ui/manual_assert.edition2021.fixed index 2f62de51cadc..8c7e919bf62a 100644 --- a/tests/ui/manual_assert.edition2021.fixed +++ b/tests/ui/manual_assert.edition2021.fixed @@ -50,6 +50,11 @@ fn main() { assert!(!(b.is_empty() || a.is_empty()), "panic4"); assert!(!(a.is_empty() || !b.is_empty()), "panic5"); assert!(!a.is_empty(), "with expansion {}", one!()); + if a.is_empty() { + let _ = 0; + } else if a.len() == 1 { + panic!("panic6"); + } } fn issue7730(a: u8) { diff --git a/tests/ui/manual_assert.edition2021.stderr b/tests/ui/manual_assert.edition2021.stderr index 237638ee1344..3555ac29243a 100644 --- a/tests/ui/manual_assert.edition2021.stderr +++ b/tests/ui/manual_assert.edition2021.stderr @@ -65,7 +65,7 @@ LL | | } | |_____^ help: try instead: `assert!(!a.is_empty(), "with expansion {}", one!());` error: only a `panic!` in `if`-then statement - --> $DIR/manual_assert.rs:73:5 + --> $DIR/manual_assert.rs:78:5 | LL | / if a > 2 { LL | | // comment diff --git a/tests/ui/manual_assert.rs b/tests/ui/manual_assert.rs index 6a4cc2468d41..f037c5b8405c 100644 --- a/tests/ui/manual_assert.rs +++ b/tests/ui/manual_assert.rs @@ -66,6 +66,11 @@ fn main() { if a.is_empty() { panic!("with expansion {}", one!()) } + if a.is_empty() { + let _ = 0; + } else if a.len() == 1 { + panic!("panic6"); + } } fn issue7730(a: u8) { From 6481d37bb9f8506362d54b216ffcabba38735aca Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 29 Nov 2022 13:35:44 +1100 Subject: [PATCH 042/321] Add `StrStyle` to `ast::LitKind::ByteStr`. This is required to distinguish between cooked and raw byte string literals in an `ast::LitKind`, without referring to an adjacent `token::Lit`. It's a prerequisite for the next commit. --- clippy_lints/src/invalid_utf8_in_unchecked.rs | 2 +- clippy_lints/src/large_include_file.rs | 2 +- clippy_lints/src/matches/match_same_arms.rs | 2 +- clippy_lints/src/utils/author.rs | 2 +- clippy_utils/src/check_proc_macro.rs | 4 +++- clippy_utils/src/consts.rs | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/invalid_utf8_in_unchecked.rs b/clippy_lints/src/invalid_utf8_in_unchecked.rs index e0a607f9a95b..6a4861747d26 100644 --- a/clippy_lints/src/invalid_utf8_in_unchecked.rs +++ b/clippy_lints/src/invalid_utf8_in_unchecked.rs @@ -33,7 +33,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidUtf8InUnchecked { if let Some([arg]) = match_function_call(cx, expr, &paths::STR_FROM_UTF8_UNCHECKED) { match &arg.kind { ExprKind::Lit(Spanned { node: lit, .. }) => { - if let LitKind::ByteStr(bytes) = &lit + if let LitKind::ByteStr(bytes, _) = &lit && std::str::from_utf8(bytes).is_err() { lint(cx, expr.span); diff --git a/clippy_lints/src/large_include_file.rs b/clippy_lints/src/large_include_file.rs index 84dd61a1e4b0..424c0d9e7982 100644 --- a/clippy_lints/src/large_include_file.rs +++ b/clippy_lints/src/large_include_file.rs @@ -60,7 +60,7 @@ impl LateLintPass<'_> for LargeIncludeFile { then { let len = match &lit.node { // include_bytes - LitKind::ByteStr(bstr) => bstr.len(), + LitKind::ByteStr(bstr, _) => bstr.len(), // include_str LitKind::Str(sym, _) => sym.as_str().len(), _ => return, diff --git a/clippy_lints/src/matches/match_same_arms.rs b/clippy_lints/src/matches/match_same_arms.rs index 168c1e4d2e60..158e6caa4de5 100644 --- a/clippy_lints/src/matches/match_same_arms.rs +++ b/clippy_lints/src/matches/match_same_arms.rs @@ -282,7 +282,7 @@ impl<'a> NormalizedPat<'a> { // TODO: Handle negative integers. They're currently treated as a wild match. ExprKind::Lit(lit) => match lit.node { LitKind::Str(sym, _) => Self::LitStr(sym), - LitKind::ByteStr(ref bytes) => Self::LitBytes(bytes), + LitKind::ByteStr(ref bytes, _) => Self::LitBytes(bytes), LitKind::Byte(val) => Self::LitInt(val.into()), LitKind::Char(val) => Self::LitInt(val.into()), LitKind::Int(val, _) => Self::LitInt(val), diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index 0c052d86eda4..bd7daf0773ca 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -299,7 +299,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> { }; kind!("Float(_, {float_ty})"); }, - LitKind::ByteStr(ref vec) => { + LitKind::ByteStr(ref vec, _) => { bind!(self, vec); kind!("ByteStr(ref {vec})"); chain!(self, "let [{:?}] = **{vec}", vec.value); diff --git a/clippy_utils/src/check_proc_macro.rs b/clippy_utils/src/check_proc_macro.rs index c6bf98b7b8bb..43f0df145f0e 100644 --- a/clippy_utils/src/check_proc_macro.rs +++ b/clippy_utils/src/check_proc_macro.rs @@ -69,7 +69,9 @@ fn lit_search_pat(lit: &LitKind) -> (Pat, Pat) { LitKind::Str(_, StrStyle::Cooked) => (Pat::Str("\""), Pat::Str("\"")), LitKind::Str(_, StrStyle::Raw(0)) => (Pat::Str("r"), Pat::Str("\"")), LitKind::Str(_, StrStyle::Raw(_)) => (Pat::Str("r#"), Pat::Str("#")), - LitKind::ByteStr(_) => (Pat::Str("b\""), Pat::Str("\"")), + LitKind::ByteStr(_, StrStyle::Cooked) => (Pat::Str("b\""), Pat::Str("\"")), + LitKind::ByteStr(_, StrStyle::Raw(0)) => (Pat::Str("br\""), Pat::Str("\"")), + LitKind::ByteStr(_, StrStyle::Raw(_)) => (Pat::Str("br#\""), Pat::Str("#")), LitKind::Byte(_) => (Pat::Str("b'"), Pat::Str("'")), LitKind::Char(_) => (Pat::Str("'"), Pat::Str("'")), LitKind::Int(_, LitIntType::Signed(IntTy::Isize)) => (Pat::Num, Pat::Str("isize")), diff --git a/clippy_utils/src/consts.rs b/clippy_utils/src/consts.rs index 315aea9aa091..7a637d32babe 100644 --- a/clippy_utils/src/consts.rs +++ b/clippy_utils/src/consts.rs @@ -210,7 +210,7 @@ pub fn lit_to_mir_constant(lit: &LitKind, ty: Option>) -> Constant { match *lit { LitKind::Str(ref is, _) => Constant::Str(is.to_string()), LitKind::Byte(b) => Constant::Int(u128::from(b)), - LitKind::ByteStr(ref s) => Constant::Binary(Lrc::clone(s)), + LitKind::ByteStr(ref s, _) => Constant::Binary(Lrc::clone(s)), LitKind::Char(c) => Constant::Char(c), LitKind::Int(n, _) => Constant::Int(n), LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty { From ab8c6beb8526f9dbf3934835681e1ff9e951ea5b Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Thu, 1 Dec 2022 23:05:53 -0500 Subject: [PATCH 043/321] Don't lint `implicit_clone` when the type doesn't implement clone --- clippy_lints/src/methods/implicit_clone.rs | 4 +++- tests/ui/implicit_clone.fixed | 10 ++++++++++ tests/ui/implicit_clone.rs | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/methods/implicit_clone.rs b/clippy_lints/src/methods/implicit_clone.rs index 429cdc1918d7..06ecbce4e70e 100644 --- a/clippy_lints/src/methods/implicit_clone.rs +++ b/clippy_lints/src/methods/implicit_clone.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_context; -use clippy_utils::ty::peel_mid_ty_refs; +use clippy_utils::ty::{implements_trait, peel_mid_ty_refs}; use clippy_utils::{is_diag_item_method, is_diag_trait_item}; use if_chain::if_chain; use rustc_errors::Applicability; @@ -19,6 +19,8 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv let (input_type, ref_count) = peel_mid_ty_refs(input_type); if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did())); if return_type == input_type; + if let Some(clone_trait) = cx.tcx.lang_items().clone_trait(); + if implements_trait(cx, return_type, clone_trait, &[]); then { let mut app = Applicability::MachineApplicable; let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0; diff --git a/tests/ui/implicit_clone.fixed b/tests/ui/implicit_clone.fixed index 33770fc2a2cf..51b1afbe5ac8 100644 --- a/tests/ui/implicit_clone.fixed +++ b/tests/ui/implicit_clone.fixed @@ -115,4 +115,14 @@ fn main() { let pathbuf_ref = &pathbuf_ref; let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&&PathBuf` let _ = (**pathbuf_ref).clone(); + + struct NoClone; + impl ToOwned for NoClone { + type Owned = Self; + fn to_owned(&self) -> Self { + NoClone + } + } + let no_clone = &NoClone; + let _ = no_clone.to_owned(); } diff --git a/tests/ui/implicit_clone.rs b/tests/ui/implicit_clone.rs index fc896525bd27..8a9027433d95 100644 --- a/tests/ui/implicit_clone.rs +++ b/tests/ui/implicit_clone.rs @@ -115,4 +115,14 @@ fn main() { let pathbuf_ref = &pathbuf_ref; let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&&PathBuf` let _ = pathbuf_ref.to_path_buf(); + + struct NoClone; + impl ToOwned for NoClone { + type Owned = Self; + fn to_owned(&self) -> Self { + NoClone + } + } + let no_clone = &NoClone; + let _ = no_clone.to_owned(); } From fa4288af1fc379e5f83fb697a10b6a6758d5caf7 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Fri, 2 Dec 2022 16:26:15 +0100 Subject: [PATCH 044/321] Add missing slash to produce function documentation --- clippy_utils/src/ty.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index bfb2d472a393..81b08ae5600d 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -30,7 +30,7 @@ use std::iter; use crate::{match_def_path, path_res, paths}; -// Checks if the given type implements copy. +/// Checks if the given type implements copy. pub fn is_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { ty.is_copy_modulo_regions(cx.tcx, cx.param_env) } From 6ba2cda79a33522d89c7c89f814f2788496616b9 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Thu, 1 Dec 2022 23:29:47 -0500 Subject: [PATCH 045/321] Fix `zero_ptr` suggestion for `no_std` crates --- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/misc.rs | 66 +++++++++++++++++++++------------ tests/ui/zero_ptr_no_std.fixed | 21 +++++++++++ tests/ui/zero_ptr_no_std.rs | 21 +++++++++++ tests/ui/zero_ptr_no_std.stderr | 26 +++++++++++++ 5 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 tests/ui/zero_ptr_no_std.fixed create mode 100644 tests/ui/zero_ptr_no_std.rs create mode 100644 tests/ui/zero_ptr_no_std.stderr diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 7b17d8a156d5..11177f830abd 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -538,7 +538,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(needless_bool::NeedlessBool)); store.register_late_pass(|_| Box::new(needless_bool::BoolComparison)); store.register_late_pass(|_| Box::new(needless_for_each::NeedlessForEach)); - store.register_late_pass(|_| Box::new(misc::MiscLints)); + store.register_late_pass(|_| Box::::default()); store.register_late_pass(|_| Box::new(eta_reduction::EtaReduction)); store.register_late_pass(|_| Box::new(mut_mut::MutMut)); store.register_late_pass(|_| Box::new(mut_reference::UnnecessaryMutPassed)); diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index 516dee20f8b1..9f4beb92b9d2 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -9,12 +9,14 @@ use rustc_hir::{ }; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::lint::in_external_macro; -use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::hygiene::DesugaringKind; use rustc_span::source_map::{ExpnKind, Span}; use clippy_utils::sugg::Sugg; -use clippy_utils::{get_parent_expr, in_constant, is_integer_literal, iter_input_pats, last_path_segment, SpanlessEq}; +use clippy_utils::{ + get_parent_expr, in_constant, is_integer_literal, is_no_std_crate, iter_input_pats, last_path_segment, SpanlessEq, +}; declare_clippy_lint! { /// ### What it does @@ -120,14 +122,28 @@ declare_clippy_lint! { "using `0 as *{const, mut} T`" } -declare_lint_pass!(MiscLints => [ +pub struct LintPass { + std_or_core: &'static str, +} +impl Default for LintPass { + fn default() -> Self { + Self { std_or_core: "std" } + } +} +impl_lint_pass!(LintPass => [ TOPLEVEL_REF_ARG, USED_UNDERSCORE_BINDING, SHORT_CIRCUIT_STATEMENT, ZERO_PTR, ]); -impl<'tcx> LateLintPass<'tcx> for MiscLints { +impl<'tcx> LateLintPass<'tcx> for LintPass { + fn check_crate(&mut self, cx: &LateContext<'_>) { + if is_no_std_crate(cx) { + self.std_or_core = "core"; + } + } + fn check_fn( &mut self, cx: &LateContext<'tcx>, @@ -231,7 +247,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if let ExprKind::Cast(e, ty) = expr.kind { - check_cast(cx, expr.span, e, ty); + self.check_cast(cx, expr.span, e, ty); return; } if in_attributes_expansion(expr) || expr.span.is_desugaring(DesugaringKind::Await) { @@ -310,26 +326,28 @@ fn non_macro_local(cx: &LateContext<'_>, res: def::Res) -> bool { } } -fn check_cast(cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) { - if_chain! { - if let TyKind::Ptr(ref mut_ty) = ty.kind; - if is_integer_literal(e, 0); - if !in_constant(cx, e.hir_id); - then { - let (msg, sugg_fn) = match mut_ty.mutbl { - Mutability::Mut => ("`0 as *mut _` detected", "std::ptr::null_mut"), - Mutability::Not => ("`0 as *const _` detected", "std::ptr::null"), - }; +impl LintPass { + fn check_cast(&self, cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) { + if_chain! { + if let TyKind::Ptr(ref mut_ty) = ty.kind; + if is_integer_literal(e, 0); + if !in_constant(cx, e.hir_id); + then { + let (msg, sugg_fn) = match mut_ty.mutbl { + Mutability::Mut => ("`0 as *mut _` detected", "ptr::null_mut"), + Mutability::Not => ("`0 as *const _` detected", "ptr::null"), + }; - let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind { - (format!("{sugg_fn}()"), Applicability::MachineApplicable) - } else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) { - (format!("{sugg_fn}::<{mut_ty_snip}>()"), Applicability::MachineApplicable) - } else { - // `MaybeIncorrect` as type inference may not work with the suggested code - (format!("{sugg_fn}()"), Applicability::MaybeIncorrect) - }; - span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl); + let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind { + (format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MachineApplicable) + } else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) { + (format!("{}::{sugg_fn}::<{mut_ty_snip}>()", self.std_or_core), Applicability::MachineApplicable) + } else { + // `MaybeIncorrect` as type inference may not work with the suggested code + (format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MaybeIncorrect) + }; + span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl); + } } } } diff --git a/tests/ui/zero_ptr_no_std.fixed b/tests/ui/zero_ptr_no_std.fixed new file mode 100644 index 000000000000..8906c776977a --- /dev/null +++ b/tests/ui/zero_ptr_no_std.fixed @@ -0,0 +1,21 @@ +// run-rustfix + +#![feature(lang_items, start, libc)] +#![no_std] +#![deny(clippy::zero_ptr)] + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + let _ = core::ptr::null::(); + let _ = core::ptr::null_mut::(); + let _: *const u8 = core::ptr::null(); + 0 +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[lang = "eh_personality"] +extern "C" fn eh_personality() {} diff --git a/tests/ui/zero_ptr_no_std.rs b/tests/ui/zero_ptr_no_std.rs new file mode 100644 index 000000000000..379c1b18d299 --- /dev/null +++ b/tests/ui/zero_ptr_no_std.rs @@ -0,0 +1,21 @@ +// run-rustfix + +#![feature(lang_items, start, libc)] +#![no_std] +#![deny(clippy::zero_ptr)] + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + let _ = 0 as *const usize; + let _ = 0 as *mut f64; + let _: *const u8 = 0 as *const _; + 0 +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[lang = "eh_personality"] +extern "C" fn eh_personality() {} diff --git a/tests/ui/zero_ptr_no_std.stderr b/tests/ui/zero_ptr_no_std.stderr new file mode 100644 index 000000000000..d92bb4a6528d --- /dev/null +++ b/tests/ui/zero_ptr_no_std.stderr @@ -0,0 +1,26 @@ +error: `0 as *const _` detected + --> $DIR/zero_ptr_no_std.rs:9:13 + | +LL | let _ = 0 as *const usize; + | ^^^^^^^^^^^^^^^^^ help: try: `core::ptr::null::()` + | +note: the lint level is defined here + --> $DIR/zero_ptr_no_std.rs:5:9 + | +LL | #![deny(clippy::zero_ptr)] + | ^^^^^^^^^^^^^^^^ + +error: `0 as *mut _` detected + --> $DIR/zero_ptr_no_std.rs:10:13 + | +LL | let _ = 0 as *mut f64; + | ^^^^^^^^^^^^^ help: try: `core::ptr::null_mut::()` + +error: `0 as *const _` detected + --> $DIR/zero_ptr_no_std.rs:11:24 + | +LL | let _: *const u8 = 0 as *const _; + | ^^^^^^^^^^^^^ help: try: `core::ptr::null()` + +error: aborting due to 3 previous errors + From cb420080acf6d546c0e172ca54f0e645b3cfaf44 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Fri, 2 Dec 2022 20:39:38 -0500 Subject: [PATCH 046/321] Add test for #10021 --- tests/ui/unnecessary_to_owned.fixed | 20 ++++++++++++++++++++ tests/ui/unnecessary_to_owned.rs | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/ui/unnecessary_to_owned.fixed b/tests/ui/unnecessary_to_owned.fixed index ddeda795f817..345f6d604c4f 100644 --- a/tests/ui/unnecessary_to_owned.fixed +++ b/tests/ui/unnecessary_to_owned.fixed @@ -454,3 +454,23 @@ mod issue_9771b { Key(v.to_vec()) } } + +// This is a watered down version of the code in: https://github.com/oxigraph/rio +// The ICE is triggered by the call to `to_owned` on this line: +// https://github.com/oxigraph/rio/blob/66635b9ff8e5423e58932353fa40d6e64e4820f7/testsuite/src/parser_evaluator.rs#L116 +mod issue_10021 { + #![allow(unused)] + + pub struct Iri(T); + + impl> Iri { + pub fn parse(iri: T) -> Result { + unimplemented!() + } + } + + pub fn parse_w3c_rdf_test_file(url: &str) -> Result<(), ()> { + let base_iri = Iri::parse(url.to_owned())?; + Ok(()) + } +} diff --git a/tests/ui/unnecessary_to_owned.rs b/tests/ui/unnecessary_to_owned.rs index 95d2576733cd..7eb53df39e5b 100644 --- a/tests/ui/unnecessary_to_owned.rs +++ b/tests/ui/unnecessary_to_owned.rs @@ -454,3 +454,23 @@ mod issue_9771b { Key(v.to_vec()) } } + +// This is a watered down version of the code in: https://github.com/oxigraph/rio +// The ICE is triggered by the call to `to_owned` on this line: +// https://github.com/oxigraph/rio/blob/66635b9ff8e5423e58932353fa40d6e64e4820f7/testsuite/src/parser_evaluator.rs#L116 +mod issue_10021 { + #![allow(unused)] + + pub struct Iri(T); + + impl> Iri { + pub fn parse(iri: T) -> Result { + unimplemented!() + } + } + + pub fn parse_w3c_rdf_test_file(url: &str) -> Result<(), ()> { + let base_iri = Iri::parse(url.to_owned())?; + Ok(()) + } +} From 2701a4076f3d18edc06781d7a9b3091ee5dbd05f Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Fri, 2 Dec 2022 20:41:29 -0500 Subject: [PATCH 047/321] Fix #10021 --- clippy_lints/src/methods/unnecessary_to_owned.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index 17b0507682ae..9263f0519724 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -386,14 +386,12 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty< Node::Expr(parent_expr) => { if let Some((callee_def_id, call_substs, recv, call_args)) = get_callee_substs_and_args(cx, parent_expr) { - if Some(callee_def_id) == cx.tcx.lang_items().into_future_fn() { - return false; - } - let fn_sig = cx.tcx.fn_sig(callee_def_id).skip_binder(); if let Some(arg_index) = recv.into_iter().chain(call_args).position(|arg| arg.hir_id == expr.hir_id) && let Some(param_ty) = fn_sig.inputs().get(arg_index) && let ty::Param(ParamTy { index: param_index , ..}) = param_ty.kind() + // https://github.com/rust-lang/rust-clippy/issues/9504 and https://github.com/rust-lang/rust-clippy/issues/10021 + && (*param_index as usize) < call_substs.len() { if fn_sig .inputs() From eec5039f09bc7992fbacaf0778adf85dcad54997 Mon Sep 17 00:00:00 2001 From: naosense Date: Mon, 28 Nov 2022 17:40:00 +0800 Subject: [PATCH 048/321] fix test --- clippy_lints/src/indexing_slicing.rs | 2 +- .../suppress_lint_in_const/clippy.toml | 2 +- tests/ui-toml/suppress_lint_in_const/test.rs | 49 ++++++++++++- .../suppress_lint_in_const/test.stderr | 70 +++++++++++++++++-- tests/ui/indexing_slicing_index.rs | 6 +- tests/ui/indexing_slicing_index.stderr | 28 ++------ 6 files changed, 122 insertions(+), 35 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 1a8ac43ac894..eebfb753a0c5 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -166,7 +166,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { // Catchall non-range index, i.e., [n] or [n << m] if let ty::Array(..) = ty.kind() { // Index is a const block. - if self.suppress_restriction_lint_in_const && let ExprKind::ConstBlock(..) = index.kind { + if let ExprKind::ConstBlock(..) = index.kind { return; } // Index is a constant uint. diff --git a/tests/ui-toml/suppress_lint_in_const/clippy.toml b/tests/ui-toml/suppress_lint_in_const/clippy.toml index d6b6fc7f2688..1b9384d7e3ee 100644 --- a/tests/ui-toml/suppress_lint_in_const/clippy.toml +++ b/tests/ui-toml/suppress_lint_in_const/clippy.toml @@ -1 +1 @@ -suppress-restriction-lint-in-const = false +suppress-restriction-lint-in-const = true diff --git a/tests/ui-toml/suppress_lint_in_const/test.rs b/tests/ui-toml/suppress_lint_in_const/test.rs index e5f4ca7cc902..5a2df9f6c5d9 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.rs +++ b/tests/ui-toml/suppress_lint_in_const/test.rs @@ -1,4 +1,51 @@ +#![feature(inline_const)] #![warn(clippy::indexing_slicing)] +// We also check the out_of_bounds_indexing lint here, because it lints similar things and +// we want to avoid false positives. +#![warn(clippy::out_of_bounds_indexing)] +#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)] + +const ARR: [i32; 2] = [1, 2]; +const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. +const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. + +const fn idx() -> usize { + 1 +} +const fn idx4() -> usize { + 4 +} + +fn main() { + let x = [1, 2, 3, 4]; + let index: usize = 1; + x[index]; + x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + + x[0]; // Ok, should not produce stderr. + x[3]; // Ok, should not produce stderr. + x[const { idx() }]; // Ok, should not produce stderr. + x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + const { &ARR[idx()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + + let y = &x; + y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021 + y[4]; // Ok, rustc will handle references too. + + let v = vec![0; 5]; + v[0]; + v[10]; + v[1 << 3]; + + const N: usize = 15; // Out of bounds + const M: usize = 3; // In bounds + x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. + x[M]; // Ok, should not produce stderr. + v[N]; + v[M]; +} /// An opaque integer representation pub struct Integer<'a> { @@ -11,5 +58,3 @@ impl<'a> Integer<'a> { self.value[0] & 0b1000_0000 != 0 } } - -fn main() {} diff --git a/tests/ui-toml/suppress_lint_in_const/test.stderr b/tests/ui-toml/suppress_lint_in_const/test.stderr index c2acfed559d0..bc178b7e1319 100644 --- a/tests/ui-toml/suppress_lint_in_const/test.stderr +++ b/tests/ui-toml/suppress_lint_in_const/test.stderr @@ -1,12 +1,70 @@ -error: indexing may panic - --> $DIR/test.rs:11:9 +error[E0080]: evaluation of `main::{constant#3}` failed + --> $DIR/test.rs:31:14 | -LL | self.value[0] & 0b1000_0000 != 0 - | ^^^^^^^^^^^^^ +LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + | ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 + +note: erroneous constant used + --> $DIR/test.rs:31:5 + | +LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true. + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: indexing may panic + --> $DIR/test.rs:22:5 + | +LL | x[index]; + | ^^^^^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead - = note: the suggestion might not be applicable in constant blocks = note: `-D clippy::indexing-slicing` implied by `-D warnings` -error: aborting due to previous error +error: indexing may panic + --> $DIR/test.rs:38:5 + | +LL | v[0]; + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead +error: indexing may panic + --> $DIR/test.rs:39:5 + | +LL | v[10]; + | ^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/test.rs:40:5 + | +LL | v[1 << 3]; + | ^^^^^^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/test.rs:46:5 + | +LL | v[N]; + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error: indexing may panic + --> $DIR/test.rs:47:5 + | +LL | v[M]; + | ^^^^ + | + = help: consider using `.get(n)` or `.get_mut(n)` instead + +error[E0080]: evaluation of constant value failed + --> $DIR/test.rs:10:24 + | +LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. + | ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 + +error: aborting due to 8 previous errors + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/indexing_slicing_index.rs b/tests/ui/indexing_slicing_index.rs index 4476e0eb9220..26abc9edb5e4 100644 --- a/tests/ui/indexing_slicing_index.rs +++ b/tests/ui/indexing_slicing_index.rs @@ -6,7 +6,7 @@ #![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)] const ARR: [i32; 2] = [1, 2]; -const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr. +const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false. const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. const fn idx() -> usize { @@ -27,8 +27,8 @@ fn main() { x[3]; // Ok, should not produce stderr. x[const { idx() }]; // Ok, should not produce stderr. x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. - const { &ARR[idx()] }; // Ok, should not produce stderr. - const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. + const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. + const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. let y = &x; y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021 diff --git a/tests/ui/indexing_slicing_index.stderr b/tests/ui/indexing_slicing_index.stderr index 84e1f65623c3..8fd77913a3fd 100644 --- a/tests/ui/indexing_slicing_index.stderr +++ b/tests/ui/indexing_slicing_index.stderr @@ -1,7 +1,7 @@ error: indexing may panic --> $DIR/indexing_slicing_index.rs:9:20 | -LL | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr. +LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead @@ -20,13 +20,13 @@ LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. error[E0080]: evaluation of `main::{constant#3}` failed --> $DIR/indexing_slicing_index.rs:31:14 | -LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. +LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 note: erroneous constant used --> $DIR/indexing_slicing_index.rs:31:5 | -LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. +LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^^^^^^^^^^^^^ error: indexing may panic @@ -37,26 +37,10 @@ LL | x[index]; | = help: consider using `.get(n)` or `.get_mut(n)` instead -error: indexing may panic - --> $DIR/indexing_slicing_index.rs:28:5 - | -LL | x[const { idx() }]; // Ok, should not produce stderr. - | ^^^^^^^^^^^^^^^^^^ - | - = help: consider using `.get(n)` or `.get_mut(n)` instead - -error: indexing may panic - --> $DIR/indexing_slicing_index.rs:29:5 - | -LL | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. - | ^^^^^^^^^^^^^^^^^^^ - | - = help: consider using `.get(n)` or `.get_mut(n)` instead - error: indexing may panic --> $DIR/indexing_slicing_index.rs:30:14 | -LL | const { &ARR[idx()] }; // Ok, should not produce stderr. +LL | const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead @@ -65,7 +49,7 @@ LL | const { &ARR[idx()] }; // Ok, should not produce stderr. error: indexing may panic --> $DIR/indexing_slicing_index.rs:31:14 | -LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. +LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false. | ^^^^^^^^^^^ | = help: consider using `.get(n)` or `.get_mut(n)` instead @@ -117,6 +101,6 @@ error[E0080]: evaluation of constant value failed LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. | ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4 -error: aborting due to 14 previous errors +error: aborting due to 12 previous errors For more information about this error, try `rustc --explain E0080`. From 20ec2ceab8450e1a0611fbc54a17a03ffc0bd39b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Dec 2022 11:02:10 +0100 Subject: [PATCH 049/321] Add test case for blocks with semicolon inside and outside a block --- clippy_lints/src/semicolon_block.rs | 9 ++++++--- tests/ui/semicolon_inside_block.fixed | 2 ++ tests/ui/semicolon_inside_block.rs | 2 ++ tests/ui/semicolon_outside_block.fixed | 2 ++ tests/ui/semicolon_outside_block.rs | 2 ++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index d3cab68137c4..8f1d1490e1f0 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -8,11 +8,13 @@ use rustc_span::Span; declare_clippy_lint! { /// ### What it does /// - /// Suggests moving the semicolon from a block inside of the block to its kast expression. + /// Suggests moving the semicolon after a block to the inside of the block, after its last + /// expression. /// /// ### Why is this bad? /// - /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests inside the block. + /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine + /// and this lint suggests inside the block. /// Take a look at `semicolon_outside_block` for the other alternative. /// /// ### Example @@ -40,7 +42,8 @@ declare_clippy_lint! { /// /// ### Why is this bad? /// - /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests outside the block. + /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine + /// and this lint suggests outside the block. /// Take a look at `semicolon_inside_block` for the other alternative. /// /// ### Example diff --git a/tests/ui/semicolon_inside_block.fixed b/tests/ui/semicolon_inside_block.fixed index 4cd112dd5e12..42e97e1ca358 100644 --- a/tests/ui/semicolon_inside_block.fixed +++ b/tests/ui/semicolon_inside_block.fixed @@ -79,5 +79,7 @@ fn main() { unit_fn_block() }; + { unit_fn_block(); }; + unit_fn_block() } diff --git a/tests/ui/semicolon_inside_block.rs b/tests/ui/semicolon_inside_block.rs index 7512125c051d..f40848f702e1 100644 --- a/tests/ui/semicolon_inside_block.rs +++ b/tests/ui/semicolon_inside_block.rs @@ -79,5 +79,7 @@ fn main() { unit_fn_block() }; + { unit_fn_block(); }; + unit_fn_block() } diff --git a/tests/ui/semicolon_outside_block.fixed b/tests/ui/semicolon_outside_block.fixed index 5bc18faaad8e..091eaa7518e9 100644 --- a/tests/ui/semicolon_outside_block.fixed +++ b/tests/ui/semicolon_outside_block.fixed @@ -79,5 +79,7 @@ fn main() { unit_fn_block() }; + { unit_fn_block(); }; + unit_fn_block() } diff --git a/tests/ui/semicolon_outside_block.rs b/tests/ui/semicolon_outside_block.rs index 0a4293238763..7ce46431fac9 100644 --- a/tests/ui/semicolon_outside_block.rs +++ b/tests/ui/semicolon_outside_block.rs @@ -79,5 +79,7 @@ fn main() { unit_fn_block() }; + { unit_fn_block(); }; + unit_fn_block() } From 00192d299d474bf211f29d61fede738aad9f5686 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Tue, 6 Dec 2022 09:47:59 +0100 Subject: [PATCH 050/321] Add beta-nominated label to triagebot Also non-maintainers should be able to label PRs with `beta-nominated`. --- triagebot.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/triagebot.toml b/triagebot.toml index acb476ee6962..6f50ef932e11 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -1,7 +1,7 @@ [relabel] allow-unauthenticated = [ "A-*", "C-*", "E-*", "I-*", "L-*", "P-*", "S-*", "T-*", - "good-first-issue" + "good-first-issue", "beta-nominated" ] # Allows shortcuts like `@rustbot ready` From 49f3c0b73689eed49df9ef822f9fd437db93ce9e Mon Sep 17 00:00:00 2001 From: Jacob Bramley Date: Mon, 21 Nov 2022 16:05:51 +0000 Subject: [PATCH 051/321] Check AArch64 branch-protection earlier in the pipeline. As suggested in #93516. --- compiler/rustc_codegen_llvm/src/context.rs | 8 +++----- compiler/rustc_codegen_llvm/src/errors.rs | 4 ---- .../locales/en-US/codegen_llvm.ftl | 3 --- .../rustc_error_messages/locales/en-US/session.ftl | 2 ++ compiler/rustc_session/src/errors.rs | 4 ++++ compiler/rustc_session/src/session.rs | 12 ++++++++---- ...ranch-protection-missing-pac-ret.BADTARGET.stderr | 2 +- .../branch-protection-missing-pac-ret.rs | 2 +- 8 files changed, 19 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 4dcc7cd54477..a22a67ad7d34 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -3,7 +3,6 @@ use crate::back::write::to_llvm_code_model; use crate::callee::get_fn; use crate::coverageinfo; use crate::debuginfo; -use crate::errors::BranchProtectionRequiresAArch64; use crate::llvm; use crate::llvm_util; use crate::type_::Type; @@ -275,10 +274,9 @@ pub unsafe fn create_module<'ll>( } } - if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { - if sess.target.arch != "aarch64" { - sess.emit_err(BranchProtectionRequiresAArch64); - } else { + // AArch64-only options (checked in rustc_session). + if sess.target.arch == "aarch64" { + if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Error, diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index fddfbb23c67d..de508f917f33 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -51,10 +51,6 @@ pub(crate) struct SymbolAlreadyDefined<'a> { pub symbol_name: &'a str, } -#[derive(Diagnostic)] -#[diag(codegen_llvm_branch_protection_requires_aarch64)] -pub(crate) struct BranchProtectionRequiresAArch64; - #[derive(Diagnostic)] #[diag(codegen_llvm_invalid_minimum_alignment)] pub(crate) struct InvalidMinimumAlignment { diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index e273476b60bb..86f71a611ab0 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -17,9 +17,6 @@ codegen_llvm_instrument_coverage_requires_llvm_12 = codegen_llvm_symbol_already_defined = symbol `{$symbol_name}` is already defined -codegen_llvm_branch_protection_requires_aarch64 = - -Zbranch-protection is only supported on aarch64 - codegen_llvm_invalid_minimum_alignment = invalid minimum global alignment: {$err} diff --git a/compiler/rustc_error_messages/locales/en-US/session.ftl b/compiler/rustc_error_messages/locales/en-US/session.ftl index 983eb926213e..ab9e8b6baae6 100644 --- a/compiler/rustc_error_messages/locales/en-US/session.ftl +++ b/compiler/rustc_error_messages/locales/en-US/session.ftl @@ -41,6 +41,8 @@ session_unsupported_dwarf_version = requested DWARF version {$dwarf_version} is session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored +session_branch_protection_requires_aarch64 = `-Zbranch-protection` is only supported on aarch64 + session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index 8cb9e1a6f1ae..fd7dabf75ef2 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -115,6 +115,10 @@ pub struct StackProtectorNotSupportedForTarget<'a> { pub target_triple: &'a TargetTriple, } +#[derive(Diagnostic)] +#[diag(session_branch_protection_requires_aarch64)] +pub(crate) struct BranchProtectionRequiresAArch64; + #[derive(Diagnostic)] #[diag(session_split_debuginfo_unstable_platform)] pub struct SplitDebugInfoUnstablePlatform { diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index e99e460913ef..ce4a2e3d82d8 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -3,10 +3,10 @@ use crate::code_stats::CodeStats; pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo}; use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, SwitchWithOptPath}; use crate::errors::{ - CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, LinkerPluginToWindowsNotSupported, - NotCircumventFeature, ProfileSampleUseFileDoesNotExist, ProfileUseFileDoesNotExist, - SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported, SkippingConstChecks, - SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget, + BranchProtectionRequiresAArch64, CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, + LinkerPluginToWindowsNotSupported, NotCircumventFeature, ProfileSampleUseFileDoesNotExist, + ProfileUseFileDoesNotExist, SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported, + SkippingConstChecks, SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget, TargetRequiresUnwindTables, UnleashedFeatureHelp, UnstableVirtualFunctionElimination, UnsupportedDwarfVersion, }; @@ -1542,6 +1542,10 @@ fn validate_commandline_args_with_session_available(sess: &Session) { } } + if sess.opts.unstable_opts.branch_protection.is_some() && sess.target.arch != "aarch64" { + sess.emit_err(BranchProtectionRequiresAArch64); + } + if let Some(dwarf_version) = sess.opts.unstable_opts.dwarf_version { if dwarf_version > 5 { sess.emit_err(UnsupportedDwarfVersion { dwarf_version }); diff --git a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr index 6bd9c6a0276f..52a591902646 100644 --- a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr +++ b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr @@ -1,4 +1,4 @@ -error: -Zbranch-protection is only supported on aarch64 +error: `-Zbranch-protection` is only supported on aarch64 error: aborting due to previous error diff --git a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs index 4bc4919bc935..1d7ec5cba17c 100644 --- a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs +++ b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs @@ -3,7 +3,7 @@ // [BADFLAGS] check-fail // [BADFLAGS] needs-llvm-components: aarch64 // [BADTARGET] compile-flags: --target=x86_64-unknown-linux-gnu -Zbranch-protection=bti -// [BADTARGET] build-fail +// [BADTARGET] check-fail // [BADTARGET] needs-llvm-components: x86 #![crate_type = "lib"] From f4d51b8ca954d9353e5a250646c219d2e597b8cb Mon Sep 17 00:00:00 2001 From: Jacob Bramley Date: Mon, 21 Nov 2022 16:18:55 +0000 Subject: [PATCH 052/321] Correct branch-protection documentation. The branch-protection codegen option has always been treated as an error for non-AArch64 targets. --- src/doc/unstable-book/src/compiler-flags/branch-protection.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/unstable-book/src/compiler-flags/branch-protection.md b/src/doc/unstable-book/src/compiler-flags/branch-protection.md index 85403748e1dc..851dbead44b4 100644 --- a/src/doc/unstable-book/src/compiler-flags/branch-protection.md +++ b/src/doc/unstable-book/src/compiler-flags/branch-protection.md @@ -1,7 +1,7 @@ # `branch-protection` This option lets you enable branch authentication instructions on AArch64. -This option is ignored for non-AArch64 architectures. +This option is only accepted when targeting AArch64 architectures. It takes some combination of the following values, separated by a `,`. - `pac-ret` - Enable pointer authentication for non-leaf functions. From f170b1f01ab9e5faabc9c9b0080065603033b4c0 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Tue, 6 Dec 2022 19:13:53 +0100 Subject: [PATCH 053/321] Use ubuntu-20.04 instead of ubuntu-latest --- .github/workflows/clippy.yml | 2 +- .github/workflows/clippy_bors.yml | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index b99213011971..a0fb23e76641 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -30,7 +30,7 @@ env: jobs: base: # NOTE: If you modify this job, make sure you copy the changes to clippy_bors.yml - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: # Setup diff --git a/.github/workflows/clippy_bors.yml b/.github/workflows/clippy_bors.yml index 6448b2d4068d..03e42e06744b 100644 --- a/.github/workflows/clippy_bors.yml +++ b/.github/workflows/clippy_bors.yml @@ -19,7 +19,7 @@ defaults: jobs: changelog: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master @@ -53,12 +53,12 @@ jobs: needs: changelog strategy: matrix: - os: [ubuntu-latest, windows-latest, macos-latest] + os: [ubuntu-20.04, windows-latest, macos-latest] host: [x86_64-unknown-linux-gnu, i686-unknown-linux-gnu, x86_64-apple-darwin, x86_64-pc-windows-msvc] exclude: - - os: ubuntu-latest + - os: ubuntu-20.04 host: x86_64-apple-darwin - - os: ubuntu-latest + - os: ubuntu-20.04 host: x86_64-pc-windows-msvc - os: macos-latest host: x86_64-unknown-linux-gnu @@ -147,7 +147,7 @@ jobs: metadata_collection: needs: changelog - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: # Setup @@ -166,7 +166,7 @@ jobs: integration_build: needs: changelog - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: # Setup @@ -224,7 +224,7 @@ jobs: - 'rust-lang-nursery/failure' - 'rust-lang/log' - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: # Setup @@ -265,7 +265,7 @@ jobs: end-success: name: bors test finished if: github.event.pusher.name == 'bors' && success() - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 needs: [changelog, base, metadata_collection, integration_build, integration] steps: @@ -275,7 +275,7 @@ jobs: end-failure: name: bors test finished if: github.event.pusher.name == 'bors' && (failure() || cancelled()) - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 needs: [changelog, base, metadata_collection, integration_build, integration] steps: From cf312ae1f26e815dc1a898d634b001b8f315448e Mon Sep 17 00:00:00 2001 From: Steven Tang Date: Wed, 7 Dec 2022 21:45:49 +1100 Subject: [PATCH 054/321] Add tests for #41731 Closes #41731 --- .../issue-41731-infinite-macro-print.rs | 15 ++++++++ .../issue-41731-infinite-macro-print.stderr | 38 +++++++++++++++++++ .../issue-41731-infinite-macro-println.rs | 15 ++++++++ .../issue-41731-infinite-macro-println.stderr | 38 +++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 src/test/ui/infinite/issue-41731-infinite-macro-print.rs create mode 100644 src/test/ui/infinite/issue-41731-infinite-macro-print.stderr create mode 100644 src/test/ui/infinite/issue-41731-infinite-macro-println.rs create mode 100644 src/test/ui/infinite/issue-41731-infinite-macro-println.stderr diff --git a/src/test/ui/infinite/issue-41731-infinite-macro-print.rs b/src/test/ui/infinite/issue-41731-infinite-macro-print.rs new file mode 100644 index 000000000000..d52e6e7e9eb8 --- /dev/null +++ b/src/test/ui/infinite/issue-41731-infinite-macro-print.rs @@ -0,0 +1,15 @@ +// compile-flags: -Z trace-macros + +#![recursion_limit = "5"] + +fn main() { + macro_rules! stack { + ($overflow:expr) => { + print!(stack!($overflow)); + //~^ ERROR recursion limit reached while expanding + //~| ERROR format argument must be a string literal + }; + } + + stack!("overflow"); +} diff --git a/src/test/ui/infinite/issue-41731-infinite-macro-print.stderr b/src/test/ui/infinite/issue-41731-infinite-macro-print.stderr new file mode 100644 index 000000000000..e30b2039d69c --- /dev/null +++ b/src/test/ui/infinite/issue-41731-infinite-macro-print.stderr @@ -0,0 +1,38 @@ +error: recursion limit reached while expanding `$crate::format_args!` + --> $DIR/issue-41731-infinite-macro-print.rs:14:5 + | +LL | stack!("overflow"); + | ^^^^^^^^^^^^^^^^^^ + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "10"]` attribute to your crate (`issue_41731_infinite_macro_print`) + = note: this error originates in the macro `print` which comes from the expansion of the macro `stack` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: trace_macro + --> $DIR/issue-41731-infinite-macro-print.rs:14:5 + | +LL | stack!("overflow"); + | ^^^^^^^^^^^^^^^^^^ + | + = note: expanding `stack! { "overflow" }` + = note: to `print! (stack! ("overflow")) ;` + = note: expanding `print! { stack! ("overflow") }` + = note: to `{ $crate :: io :: _print($crate :: format_args! (stack! ("overflow"))) ; }` + = note: expanding `stack! { "overflow" }` + = note: to `print! (stack! ("overflow")) ;` + = note: expanding `print! { stack! ("overflow") }` + = note: to `{ $crate :: io :: _print($crate :: format_args! (stack! ("overflow"))) ; }` + +error: format argument must be a string literal + --> $DIR/issue-41731-infinite-macro-print.rs:14:5 + | +LL | stack!("overflow"); + | ^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `print` which comes from the expansion of the macro `stack` (in Nightly builds, run with -Z macro-backtrace for more info) +help: you might be missing a string literal to format with + | +LL | print!("{}", stack!($overflow)); + | +++++ + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/infinite/issue-41731-infinite-macro-println.rs b/src/test/ui/infinite/issue-41731-infinite-macro-println.rs new file mode 100644 index 000000000000..3c2b7ee023b5 --- /dev/null +++ b/src/test/ui/infinite/issue-41731-infinite-macro-println.rs @@ -0,0 +1,15 @@ +// compile-flags: -Z trace-macros + +#![recursion_limit = "5"] + +fn main() { + macro_rules! stack { + ($overflow:expr) => { + println!(stack!($overflow)); + //~^ ERROR recursion limit reached while expanding + //~| ERROR format argument must be a string literal + }; + } + + stack!("overflow"); +} diff --git a/src/test/ui/infinite/issue-41731-infinite-macro-println.stderr b/src/test/ui/infinite/issue-41731-infinite-macro-println.stderr new file mode 100644 index 000000000000..66b466dafa0d --- /dev/null +++ b/src/test/ui/infinite/issue-41731-infinite-macro-println.stderr @@ -0,0 +1,38 @@ +error: recursion limit reached while expanding `$crate::format_args_nl!` + --> $DIR/issue-41731-infinite-macro-println.rs:14:5 + | +LL | stack!("overflow"); + | ^^^^^^^^^^^^^^^^^^ + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "10"]` attribute to your crate (`issue_41731_infinite_macro_println`) + = note: this error originates in the macro `println` which comes from the expansion of the macro `stack` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: trace_macro + --> $DIR/issue-41731-infinite-macro-println.rs:14:5 + | +LL | stack!("overflow"); + | ^^^^^^^^^^^^^^^^^^ + | + = note: expanding `stack! { "overflow" }` + = note: to `println! (stack! ("overflow")) ;` + = note: expanding `println! { stack! ("overflow") }` + = note: to `{ $crate :: io :: _print($crate :: format_args_nl! (stack! ("overflow"))) ; }` + = note: expanding `stack! { "overflow" }` + = note: to `println! (stack! ("overflow")) ;` + = note: expanding `println! { stack! ("overflow") }` + = note: to `{ $crate :: io :: _print($crate :: format_args_nl! (stack! ("overflow"))) ; }` + +error: format argument must be a string literal + --> $DIR/issue-41731-infinite-macro-println.rs:14:5 + | +LL | stack!("overflow"); + | ^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `println` which comes from the expansion of the macro `stack` (in Nightly builds, run with -Z macro-backtrace for more info) +help: you might be missing a string literal to format with + | +LL | println!("{}", stack!($overflow)); + | +++++ + +error: aborting due to 2 previous errors + From bb6a0aa8ed2051510ea2b6a997c2b2c9df8102e5 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Wed, 7 Dec 2022 12:29:13 +0100 Subject: [PATCH 055/321] Revert "Use ubuntu-20.04 instead of ubuntu-latest" This reverts commit f170b1f01ab9e5faabc9c9b0080065603033b4c0. --- .github/workflows/clippy.yml | 2 +- .github/workflows/clippy_bors.yml | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index a0fb23e76641..b99213011971 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -30,7 +30,7 @@ env: jobs: base: # NOTE: If you modify this job, make sure you copy the changes to clippy_bors.yml - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: # Setup diff --git a/.github/workflows/clippy_bors.yml b/.github/workflows/clippy_bors.yml index 03e42e06744b..6448b2d4068d 100644 --- a/.github/workflows/clippy_bors.yml +++ b/.github/workflows/clippy_bors.yml @@ -19,7 +19,7 @@ defaults: jobs: changelog: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master @@ -53,12 +53,12 @@ jobs: needs: changelog strategy: matrix: - os: [ubuntu-20.04, windows-latest, macos-latest] + os: [ubuntu-latest, windows-latest, macos-latest] host: [x86_64-unknown-linux-gnu, i686-unknown-linux-gnu, x86_64-apple-darwin, x86_64-pc-windows-msvc] exclude: - - os: ubuntu-20.04 + - os: ubuntu-latest host: x86_64-apple-darwin - - os: ubuntu-20.04 + - os: ubuntu-latest host: x86_64-pc-windows-msvc - os: macos-latest host: x86_64-unknown-linux-gnu @@ -147,7 +147,7 @@ jobs: metadata_collection: needs: changelog - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: # Setup @@ -166,7 +166,7 @@ jobs: integration_build: needs: changelog - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: # Setup @@ -224,7 +224,7 @@ jobs: - 'rust-lang-nursery/failure' - 'rust-lang/log' - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: # Setup @@ -265,7 +265,7 @@ jobs: end-success: name: bors test finished if: github.event.pusher.name == 'bors' && success() - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest needs: [changelog, base, metadata_collection, integration_build, integration] steps: @@ -275,7 +275,7 @@ jobs: end-failure: name: bors test finished if: github.event.pusher.name == 'bors' && (failure() || cancelled()) - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest needs: [changelog, base, metadata_collection, integration_build, integration] steps: From 1c03cd3fa34d6b317f33a2fd40e3d222a4d89a85 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Wed, 7 Dec 2022 12:30:13 +0100 Subject: [PATCH 056/321] Don't install dependencies on i386 --- .github/workflows/clippy_bors.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/clippy_bors.yml b/.github/workflows/clippy_bors.yml index 6448b2d4068d..1bc457a94793 100644 --- a/.github/workflows/clippy_bors.yml +++ b/.github/workflows/clippy_bors.yml @@ -82,13 +82,6 @@ jobs: with: github_token: "${{ secrets.github_token }}" - - name: Install dependencies (Linux-i686) - run: | - sudo dpkg --add-architecture i386 - sudo apt-get update - sudo apt-get install gcc-multilib libssl-dev:i386 libgit2-dev:i386 - if: matrix.host == 'i686-unknown-linux-gnu' - - name: Checkout uses: actions/checkout@v3.0.2 From 591c18d2f01c140b55eed01ab33b96373a4ab230 Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Wed, 7 Dec 2022 17:37:08 +0000 Subject: [PATCH 057/321] Add 1.58 MSRV for `collapsible_str_replace` --- clippy_lints/src/methods/mod.rs | 5 +++- clippy_utils/src/msrvs.rs | 2 +- tests/ui/collapsible_str_replace.fixed | 11 ++++++++ tests/ui/collapsible_str_replace.rs | 11 ++++++++ tests/ui/collapsible_str_replace.stderr | 34 +++++++++++++++---------- 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d2913680cbb7..cb7229ba8a4b 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3672,7 +3672,10 @@ impl Methods { no_effect_replace::check(cx, expr, arg1, arg2); // Check for repeated `str::replace` calls to perform `collapsible_str_replace` lint - if name == "replace" && let Some(("replace", ..)) = method_call(recv) { + if self.msrv.meets(msrvs::PATTERN_TRAIT_CHAR_ARRAY) + && name == "replace" + && let Some(("replace", ..)) = method_call(recv) + { collapsible_str_replace::check(cx, expr, arg1, arg2); } }, diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs index 12a512f78a69..ba5bc9c3135d 100644 --- a/clippy_utils/src/msrvs.rs +++ b/clippy_utils/src/msrvs.rs @@ -21,7 +21,7 @@ macro_rules! msrv_aliases { msrv_aliases! { 1,65,0 { LET_ELSE } 1,62,0 { BOOL_THEN_SOME } - 1,58,0 { FORMAT_ARGS_CAPTURE } + 1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY } 1,53,0 { OR_PATTERNS, MANUAL_BITS, BTREE_MAP_RETAIN, BTREE_SET_RETAIN, ARRAY_INTO_ITERATOR } 1,52,0 { STR_SPLIT_ONCE, REM_EUCLID_CONST } 1,51,0 { BORROW_AS_PTR, SEEK_FROM_CURRENT, UNSIGNED_ABS } diff --git a/tests/ui/collapsible_str_replace.fixed b/tests/ui/collapsible_str_replace.fixed index 49fc9a9629e2..9792ae9ed6b8 100644 --- a/tests/ui/collapsible_str_replace.fixed +++ b/tests/ui/collapsible_str_replace.fixed @@ -1,5 +1,6 @@ // run-rustfix +#![allow(unused)] #![warn(clippy::collapsible_str_replace)] fn get_filter() -> char { @@ -71,3 +72,13 @@ fn main() { .replace('u', iter.next().unwrap()) .replace('s', iter.next().unwrap()); } + +#[clippy::msrv = "1.57"] +fn msrv_1_57() { + let _ = "".replace('a', "1.57").replace('b', "1.57"); +} + +#[clippy::msrv = "1.58"] +fn msrv_1_58() { + let _ = "".replace(['a', 'b'], "1.58"); +} diff --git a/tests/ui/collapsible_str_replace.rs b/tests/ui/collapsible_str_replace.rs index e3e25c4146ff..baee185b79ea 100644 --- a/tests/ui/collapsible_str_replace.rs +++ b/tests/ui/collapsible_str_replace.rs @@ -1,5 +1,6 @@ // run-rustfix +#![allow(unused)] #![warn(clippy::collapsible_str_replace)] fn get_filter() -> char { @@ -74,3 +75,13 @@ fn main() { .replace('u', iter.next().unwrap()) .replace('s', iter.next().unwrap()); } + +#[clippy::msrv = "1.57"] +fn msrv_1_57() { + let _ = "".replace('a', "1.57").replace('b', "1.57"); +} + +#[clippy::msrv = "1.58"] +fn msrv_1_58() { + let _ = "".replace('a', "1.58").replace('b', "1.58"); +} diff --git a/tests/ui/collapsible_str_replace.stderr b/tests/ui/collapsible_str_replace.stderr index 8e3daf3b898a..223358cf53f3 100644 --- a/tests/ui/collapsible_str_replace.stderr +++ b/tests/ui/collapsible_str_replace.stderr @@ -1,5 +1,5 @@ error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:19:27 + --> $DIR/collapsible_str_replace.rs:20:27 | LL | let _ = "hesuo worpd".replace('s', "l").replace('u', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace(['s', 'u'], "l")` @@ -7,19 +7,19 @@ LL | let _ = "hesuo worpd".replace('s', "l").replace('u', "l"); = note: `-D clippy::collapsible-str-replace` implied by `-D warnings` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:21:27 + --> $DIR/collapsible_str_replace.rs:22:27 | LL | let _ = "hesuo worpd".replace('s', l).replace('u', l); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace(['s', 'u'], l)` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:23:27 + --> $DIR/collapsible_str_replace.rs:24:27 | LL | let _ = "hesuo worpd".replace('s', "l").replace('u', "l").replace('p', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace(['s', 'u', 'p'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:26:10 + --> $DIR/collapsible_str_replace.rs:27:10 | LL | .replace('s', "l") | __________^ @@ -29,58 +29,64 @@ LL | | .replace('d', "l"); | |__________________________^ help: replace with: `replace(['s', 'u', 'p', 'd'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:31:27 + --> $DIR/collapsible_str_replace.rs:32:27 | LL | let _ = "hesuo world".replace(s, "l").replace('u', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace([s, 'u'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:33:27 + --> $DIR/collapsible_str_replace.rs:34:27 | LL | let _ = "hesuo worpd".replace(s, "l").replace('u', "l").replace('p', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace([s, 'u', 'p'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:35:27 + --> $DIR/collapsible_str_replace.rs:36:27 | LL | let _ = "hesuo worpd".replace(s, "l").replace(u, "l").replace('p', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace([s, u, 'p'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:37:27 + --> $DIR/collapsible_str_replace.rs:38:27 | LL | let _ = "hesuo worpd".replace(s, "l").replace(u, "l").replace(p, "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace([s, u, p], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:39:27 + --> $DIR/collapsible_str_replace.rs:40:27 | LL | let _ = "hesuo worlp".replace('s', "l").replace('u', "l").replace('p', "d"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace(['s', 'u'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:41:45 + --> $DIR/collapsible_str_replace.rs:42:45 | LL | let _ = "hesuo worpd".replace('s', "x").replace('u', "l").replace('p', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace(['u', 'p'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:44:47 + --> $DIR/collapsible_str_replace.rs:45:47 | LL | let _ = "hesudo worpd".replace("su", "l").replace('d', "l").replace('p', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace(['d', 'p'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:46:28 + --> $DIR/collapsible_str_replace.rs:47:28 | LL | let _ = "hesudo worpd".replace(d, "l").replace('p', "l").replace("su", "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace([d, 'p'], "l")` error: used consecutive `str::replace` call - --> $DIR/collapsible_str_replace.rs:48:27 + --> $DIR/collapsible_str_replace.rs:49:27 | LL | let _ = "hesuo world".replace(get_filter(), "l").replace('s', "l"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace([get_filter(), 's'], "l")` -error: aborting due to 13 previous errors +error: used consecutive `str::replace` call + --> $DIR/collapsible_str_replace.rs:86:16 + | +LL | let _ = "".replace('a', "1.58").replace('b', "1.58"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `replace(['a', 'b'], "1.58")` + +error: aborting due to 14 previous errors From cdfc5051b1286938f56160243c28538f79dce6b1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 25 Nov 2022 15:41:39 +0100 Subject: [PATCH 058/321] Fix missing const expression items visit --- src/librustdoc/visit_ast.rs | 517 +++++++++++++++++++----------------- 1 file changed, 280 insertions(+), 237 deletions(-) diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 22068ebe041c..b94b64a0bd5c 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -5,8 +5,11 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefId; +use rustc_hir::intravisit::{walk_item, Visitor}; use rustc_hir::Node; use rustc_hir::CRATE_HIR_ID; +use rustc_middle::hir::map::Map; +use rustc_middle::hir::nested_filter; use rustc_middle::ty::TyCtxt; use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; use rustc_span::symbol::{kw, sym, Symbol}; @@ -57,9 +60,6 @@ pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool false } -// Also, is there some reason that this doesn't use the 'visit' -// framework from syntax?. - pub(crate) struct RustdocVisitor<'a, 'tcx> { cx: &'a mut core::DocContext<'tcx>, view_item_stack: FxHashSet, @@ -67,6 +67,8 @@ pub(crate) struct RustdocVisitor<'a, 'tcx> { /// Are the current module and all of its parents public? inside_public_path: bool, exact_paths: FxHashMap>, + modules: Vec>, + map: Map<'tcx>, } impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { @@ -74,12 +76,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // If the root is re-exported, terminate all recursion. let mut stack = FxHashSet::default(); stack.insert(hir::CRATE_HIR_ID); + let om = Module::new( + cx.tcx.crate_name(LOCAL_CRATE), + hir::CRATE_HIR_ID, + cx.tcx.hir().root_module().spans.inner_span, + ); + let map = cx.tcx.hir(); + RustdocVisitor { cx, view_item_stack: stack, inlining: false, inside_public_path: true, exact_paths: FxHashMap::default(), + modules: vec![om], + map, } } @@ -88,13 +99,226 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did)); } + /// Tries to resolve the target of a `pub use` statement and inlines the + /// target if it is defined locally and would not be documented otherwise, + /// or when it is specifically requested with `please_inline`. + /// (the latter is the case when the import is marked `doc(inline)`) + /// + /// Cross-crate inlining occurs later on during crate cleaning + /// and follows different rules. + /// + /// Returns `true` if the target has been inlined. + fn maybe_inline_local( + &mut self, + id: hir::HirId, + res: Res, + renamed: Option, + glob: bool, + please_inline: bool, + ) -> bool { + debug!("maybe_inline_local res: {:?}", res); + + if self.cx.output_format.is_json() { + return false; + } + + let tcx = self.cx.tcx; + let Some(res_did) = res.opt_def_id() else { + return false; + }; + + let use_attrs = tcx.hir().attrs(id); + // Don't inline `doc(hidden)` imports so they can be stripped at a later stage. + let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline) + || use_attrs.lists(sym::doc).has_word(sym::hidden); + + // For cross-crate impl inlining we need to know whether items are + // reachable in documentation -- a previously unreachable item can be + // made reachable by cross-crate inlining which we're checking here. + // (this is done here because we need to know this upfront). + if !res_did.is_local() && !is_no_inline { + crate::visit_lib::lib_embargo_visit_item(self.cx, res_did); + return false; + } + + let res_hir_id = match res_did.as_local() { + Some(n) => tcx.hir().local_def_id_to_hir_id(n), + None => return false, + }; + + let is_private = + !self.cx.cache.effective_visibilities.is_directly_public(self.cx.tcx, res_did); + let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id); + + // Only inline if requested or if the item would otherwise be stripped. + if (!please_inline && !is_private && !is_hidden) || is_no_inline { + return false; + } + + if !self.view_item_stack.insert(res_hir_id) { + return false; + } + + let ret = match tcx.hir().get(res_hir_id) { + Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => { + let prev = mem::replace(&mut self.inlining, true); + for &i in m.item_ids { + let i = self.cx.tcx.hir().item(i); + self.visit_item_inner(i, None, Some(id)); + } + self.inlining = prev; + true + } + Node::Item(it) if !glob => { + let prev = mem::replace(&mut self.inlining, true); + self.visit_item_inner(it, renamed, Some(id)); + self.inlining = prev; + true + } + Node::ForeignItem(it) if !glob => { + let prev = mem::replace(&mut self.inlining, true); + self.visit_foreign_item_inner(it, renamed); + self.inlining = prev; + true + } + _ => false, + }; + self.view_item_stack.remove(&res_hir_id); + ret + } + + fn visit_item_inner( + &mut self, + item: &'tcx hir::Item<'_>, + renamed: Option, + parent_id: Option, + ) -> bool { + debug!("visiting item {:?}", item); + let name = renamed.unwrap_or(item.ident.name); + + let def_id = item.owner_id.to_def_id(); + let is_pub = self.cx.tcx.visibility(def_id).is_public(); + + if is_pub { + self.store_path(item.owner_id.to_def_id()); + } + + match item.kind { + hir::ItemKind::ForeignMod { items, .. } => { + for item in items { + let item = self.cx.tcx.hir().foreign_item(item.id); + self.visit_foreign_item_inner(item, None); + } + } + // If we're inlining, skip private items or item reexported as "_". + _ if self.inlining && (!is_pub || renamed == Some(kw::Underscore)) => {} + hir::ItemKind::GlobalAsm(..) => {} + hir::ItemKind::Use(_, hir::UseKind::ListStem) => {} + hir::ItemKind::Use(path, kind) => { + for &res in &path.res { + // Struct and variant constructors and proc macro stubs always show up alongside + // their definitions, we've already processed them so just discard these. + if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { + continue; + } + + let attrs = self.cx.tcx.hir().attrs(item.hir_id()); + + // If there was a private module in the current path then don't bother inlining + // anything as it will probably be stripped anyway. + if is_pub && self.inside_public_path { + let please_inline = attrs.iter().any(|item| match item.meta_item_list() { + Some(ref list) if item.has_name(sym::doc) => { + list.iter().any(|i| i.has_name(sym::inline)) + } + _ => false, + }); + let is_glob = kind == hir::UseKind::Glob; + let ident = if is_glob { None } else { Some(name) }; + if self.maybe_inline_local( + item.hir_id(), + res, + ident, + is_glob, + om, + please_inline, + ) { + continue; + } + } + + self.modules.last_mut().unwrap().items.push((item, renamed, parent_id)); + } + } + hir::ItemKind::Macro(ref macro_def, _) => { + // `#[macro_export] macro_rules!` items are handled separately in `visit()`, + // above, since they need to be documented at the module top level. Accordingly, + // we only want to handle macros if one of three conditions holds: + // + // 1. This macro was defined by `macro`, and thus isn't covered by the case + // above. + // 2. This macro isn't marked with `#[macro_export]`, and thus isn't covered + // by the case above. + // 3. We're inlining, since a reexport where inlining has been requested + // should be inlined even if it is also documented at the top level. + + let def_id = item.owner_id.to_def_id(); + let is_macro_2_0 = !macro_def.macro_rules; + let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export); + + if is_macro_2_0 || nonexported || self.inlining { + self.modules.last_mut().unwrap().items.push((item, renamed, None)); + } + } + hir::ItemKind::Mod(ref m) => { + self.enter_mod(item.hir_id(), m, name); + } + hir::ItemKind::Fn(..) + | hir::ItemKind::ExternCrate(..) + | hir::ItemKind::Enum(..) + | hir::ItemKind::Struct(..) + | hir::ItemKind::Union(..) + | hir::ItemKind::TyAlias(..) + | hir::ItemKind::OpaqueTy(..) + | hir::ItemKind::Static(..) + | hir::ItemKind::Trait(..) + | hir::ItemKind::TraitAlias(..) => { + self.modules.last_mut().unwrap().items.push((item, renamed, parent_id)) + } + hir::ItemKind::Const(..) => { + // Underscore constants do not correspond to a nameable item and + // so are never useful in documentation. + if name != kw::Underscore { + self.modules.last_mut().unwrap().items.push((item, renamed, parent_id)); + } + } + hir::ItemKind::Impl(impl_) => { + // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick + // them up regardless of where they're located. + if !self.inlining && impl_.of_trait.is_none() { + self.modules.last_mut().unwrap().items.push((item, None, None)); + } + } + } + true + } + + fn visit_foreign_item_inner( + &mut self, + item: &'tcx hir::ForeignItem<'_>, + renamed: Option, + ) { + // If inlining we only want to include public functions. + if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() { + self.modules.last_mut().unwrap().foreigns.push((item, renamed)); + } + } + pub(crate) fn visit(mut self) -> Module<'tcx> { - let mut top_level_module = self.visit_mod_contents( - hir::CRATE_HIR_ID, - self.cx.tcx.hir().root_module(), - self.cx.tcx.crate_name(LOCAL_CRATE), - None, - ); + let root_module = self.cx.tcx.hir().root_module(); + self.visit_mod_contents(CRATE_HIR_ID, root_module); + + let mut top_level_module = self.modules.pop().unwrap(); // `#[macro_export] macro_rules!` items are reexported at the top level of the // crate, regardless of where they're defined. We want to document the @@ -151,24 +375,34 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { top_level_module } - fn visit_mod_contents( - &mut self, - id: hir::HirId, - m: &'tcx hir::Mod<'tcx>, - name: Symbol, - parent_id: Option, - ) -> Module<'tcx> { - let mut om = Module::new(name, id, m.spans.inner_span); + /// This method will create a new module and push it onto the "modules stack" then call + /// `visit_mod_contents`. Once done, it'll remove it from the "modules stack" and instead + /// add into into the list of modules of the current module. + fn enter_mod(&mut self, id: hir::HirId, m: &'tcx hir::Mod<'tcx>, name: Symbol) { + self.modules.push(Module::new(name, id, m.spans.inner_span)); + + self.visit_mod_contents(id, m); + + let last = self.modules.pop().unwrap(); + self.modules.last_mut().unwrap().mods.push(last); + } + + /// This method will go through the given module items in two passes: + /// 1. The items which are not glob imports/reexports. + /// 2. The glob imports/reexports. + fn visit_mod_contents(&mut self, id: hir::HirId, m: &'tcx hir::Mod<'tcx>) { + debug!("Going through module {:?}", m); let def_id = self.cx.tcx.hir().local_def_id(id).to_def_id(); // Keep track of if there were any private modules in the path. let orig_inside_public_path = self.inside_public_path; self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public(); + + // Reimplementation of `walk_mod`: for &i in m.item_ids { let item = self.cx.tcx.hir().item(i); - if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) { - continue; + if !matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) { + self.visit_item(item); } - self.visit_item(item, None, &mut om, parent_id); } for &i in m.item_ids { let item = self.cx.tcx.hir().item(i); @@ -176,225 +410,34 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // Later passes in rustdoc will de-duplicate by name and kind, so if glob- // imported items appear last, then they'll be the ones that get discarded. if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) { - self.visit_item(item, None, &mut om, parent_id); + self.visit_item(item); } } self.inside_public_path = orig_inside_public_path; - om - } - - /// Tries to resolve the target of a `pub use` statement and inlines the - /// target if it is defined locally and would not be documented otherwise, - /// or when it is specifically requested with `please_inline`. - /// (the latter is the case when the import is marked `doc(inline)`) - /// - /// Cross-crate inlining occurs later on during crate cleaning - /// and follows different rules. - /// - /// Returns `true` if the target has been inlined. - fn maybe_inline_local( - &mut self, - id: hir::HirId, - res: Res, - renamed: Option, - glob: bool, - om: &mut Module<'tcx>, - please_inline: bool, - ) -> bool { - debug!("maybe_inline_local res: {:?}", res); - - if self.cx.output_format.is_json() { - return false; - } - - let tcx = self.cx.tcx; - let Some(res_did) = res.opt_def_id() else { - return false; - }; - - let use_attrs = tcx.hir().attrs(id); - // Don't inline `doc(hidden)` imports so they can be stripped at a later stage. - let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline) - || use_attrs.lists(sym::doc).has_word(sym::hidden); - - // For cross-crate impl inlining we need to know whether items are - // reachable in documentation -- a previously unreachable item can be - // made reachable by cross-crate inlining which we're checking here. - // (this is done here because we need to know this upfront). - if !res_did.is_local() && !is_no_inline { - crate::visit_lib::lib_embargo_visit_item(self.cx, res_did); - return false; - } - - let res_hir_id = match res_did.as_local() { - Some(n) => tcx.hir().local_def_id_to_hir_id(n), - None => return false, - }; - - let is_private = - !self.cx.cache.effective_visibilities.is_directly_public(self.cx.tcx, res_did); - let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id); - - // Only inline if requested or if the item would otherwise be stripped. - if (!please_inline && !is_private && !is_hidden) || is_no_inline { - return false; - } - - if !self.view_item_stack.insert(res_hir_id) { - return false; - } - - let ret = match tcx.hir().get(res_hir_id) { - Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => { - let prev = mem::replace(&mut self.inlining, true); - for &i in m.item_ids { - let i = self.cx.tcx.hir().item(i); - self.visit_item(i, None, om, Some(id)); - } - self.inlining = prev; - true - } - Node::Item(it) if !glob => { - let prev = mem::replace(&mut self.inlining, true); - self.visit_item(it, renamed, om, Some(id)); - self.inlining = prev; - true - } - Node::ForeignItem(it) if !glob => { - let prev = mem::replace(&mut self.inlining, true); - self.visit_foreign_item(it, renamed, om); - self.inlining = prev; - true - } - _ => false, - }; - self.view_item_stack.remove(&res_hir_id); - ret - } - - fn visit_item( - &mut self, - item: &'tcx hir::Item<'_>, - renamed: Option, - om: &mut Module<'tcx>, - parent_id: Option, - ) { - debug!("visiting item {:?}", item); - let name = renamed.unwrap_or(item.ident.name); - - let def_id = item.owner_id.to_def_id(); - let is_pub = self.cx.tcx.visibility(def_id).is_public(); - - if is_pub { - self.store_path(item.owner_id.to_def_id()); - } - - match item.kind { - hir::ItemKind::ForeignMod { items, .. } => { - for item in items { - let item = self.cx.tcx.hir().foreign_item(item.id); - self.visit_foreign_item(item, None, om); - } - } - // If we're inlining, skip private items or item reexported as "_". - _ if self.inlining && (!is_pub || renamed == Some(kw::Underscore)) => {} - hir::ItemKind::GlobalAsm(..) => {} - hir::ItemKind::Use(_, hir::UseKind::ListStem) => {} - hir::ItemKind::Use(path, kind) => { - for &res in &path.res { - // Struct and variant constructors and proc macro stubs always show up alongside - // their definitions, we've already processed them so just discard these. - if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res { - continue; - } - - let attrs = self.cx.tcx.hir().attrs(item.hir_id()); - - // If there was a private module in the current path then don't bother inlining - // anything as it will probably be stripped anyway. - if is_pub && self.inside_public_path { - let please_inline = attrs.iter().any(|item| match item.meta_item_list() { - Some(ref list) if item.has_name(sym::doc) => { - list.iter().any(|i| i.has_name(sym::inline)) - } - _ => false, - }); - let is_glob = kind == hir::UseKind::Glob; - let ident = if is_glob { None } else { Some(name) }; - if self.maybe_inline_local( - item.hir_id(), - res, - ident, - is_glob, - om, - please_inline, - ) { - continue; - } - } - - om.items.push((item, renamed, parent_id)) - } - } - hir::ItemKind::Macro(ref macro_def, _) => { - // `#[macro_export] macro_rules!` items are handled separately in `visit()`, - // above, since they need to be documented at the module top level. Accordingly, - // we only want to handle macros if one of three conditions holds: - // - // 1. This macro was defined by `macro`, and thus isn't covered by the case - // above. - // 2. This macro isn't marked with `#[macro_export]`, and thus isn't covered - // by the case above. - // 3. We're inlining, since a reexport where inlining has been requested - // should be inlined even if it is also documented at the top level. - - let def_id = item.owner_id.to_def_id(); - let is_macro_2_0 = !macro_def.macro_rules; - let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export); - - if is_macro_2_0 || nonexported || self.inlining { - om.items.push((item, renamed, None)); - } - } - hir::ItemKind::Mod(ref m) => { - om.mods.push(self.visit_mod_contents(item.hir_id(), m, name, parent_id)); - } - hir::ItemKind::Fn(..) - | hir::ItemKind::ExternCrate(..) - | hir::ItemKind::Enum(..) - | hir::ItemKind::Struct(..) - | hir::ItemKind::Union(..) - | hir::ItemKind::TyAlias(..) - | hir::ItemKind::OpaqueTy(..) - | hir::ItemKind::Static(..) - | hir::ItemKind::Trait(..) - | hir::ItemKind::TraitAlias(..) => om.items.push((item, renamed, parent_id)), - hir::ItemKind::Const(..) => { - // Underscore constants do not correspond to a nameable item and - // so are never useful in documentation. - if name != kw::Underscore { - om.items.push((item, renamed, parent_id)); - } - } - hir::ItemKind::Impl(impl_) => { - // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick - // them up regardless of where they're located. - if !self.inlining && impl_.of_trait.is_none() { - om.items.push((item, None, None)); - } - } - } - } - - fn visit_foreign_item( - &mut self, - item: &'tcx hir::ForeignItem<'_>, - renamed: Option, - om: &mut Module<'tcx>, - ) { - // If inlining we only want to include public functions. - if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() { - om.foreigns.push((item, renamed)); - } + } +} + +// We need to implement this visitor so it'll go everywhere and retrieve items we're interested in +// such as impl blocks in const blocks. +impl<'a, 'tcx> Visitor<'tcx> for RustdocVisitor<'a, 'tcx> { + type NestedFilter = nested_filter::All; + + fn nested_visit_map(&mut self) -> Self::Map { + self.map + } + + fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) { + let parent_id = if self.modules.len() > 1 { + Some(self.modules[self.modules.len() - 2].id) + } else { + None + }; + if self.visit_item_inner(i, None, parent_id) { + walk_item(self, i); + } + } + + fn visit_mod(&mut self, _: &hir::Mod<'tcx>, _: Span, _: hir::HirId) { + // handled in `visit_item_inner` } } From 9cce0bc583ee2cff88935ce0e08d8ec1eb1239a8 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 25 Nov 2022 15:42:09 +0100 Subject: [PATCH 059/321] Add regression test for impl blocks in const expr --- src/test/rustdoc/impl-in-const-block.rs | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/rustdoc/impl-in-const-block.rs diff --git a/src/test/rustdoc/impl-in-const-block.rs b/src/test/rustdoc/impl-in-const-block.rs new file mode 100644 index 000000000000..b44e71352466 --- /dev/null +++ b/src/test/rustdoc/impl-in-const-block.rs @@ -0,0 +1,43 @@ +// Regression test for #83026. +// The goal of this test is to ensure that impl blocks inside +// const expressions are documented as well. + +#![crate_name = "foo"] + +// @has 'foo/struct.A.html' +// @has - '//*[@id="method.new"]/*[@class="code-header"]' 'pub fn new() -> A' +// @has - '//*[@id="method.bar"]/*[@class="code-header"]' 'pub fn bar(&self)' +// @has - '//*[@id="method.woo"]/*[@class="code-header"]' 'pub fn woo(&self)' +// @has - '//*[@id="method.yoo"]/*[@class="code-header"]' 'pub fn yoo()' +// @has - '//*[@id="method.yuu"]/*[@class="code-header"]' 'pub fn yuu()' +pub struct A; + +const _: () = { + impl A { + const FOO: () = { + impl A { + pub fn woo(&self) {} + } + }; + + pub fn new() -> A { + A + } + } +}; +pub const X: () = { + impl A { + pub fn bar(&self) {} + } +}; + +fn foo() { + impl A { + pub fn yoo() {} + } + const _: () = { + impl A { + pub fn yuu() {} + } + }; +} From 9c46173895430c63066731440e00faf0ab2195dd Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 25 Nov 2022 16:31:43 +0100 Subject: [PATCH 060/321] Update newly failing UI tests --- .../infinite-recursive-type-impl-trait-return.rs | 4 +--- ...inite-recursive-type-impl-trait-return.stderr | 16 ++++++++++++++++ .../infinite-recursive-type-impl-trait.rs | 5 +---- .../infinite-recursive-type-impl-trait.stderr | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.stderr create mode 100644 src/test/rustdoc-ui/infinite-recursive-type-impl-trait.stderr diff --git a/src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.rs b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.rs index 4b1e04234c87..939da186fbcd 100644 --- a/src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.rs +++ b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.rs @@ -1,12 +1,10 @@ -// check-pass // normalize-stderr-test: "`.*`" -> "`DEF_ID`" // normalize-stdout-test: "`.*`" -> "`DEF_ID`" // edition:2018 pub async fn f() -> impl std::fmt::Debug { - // rustdoc doesn't care that this is infinitely sized #[derive(Debug)] - enum E { + enum E { //~ ERROR This(E), Unit, } diff --git a/src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.stderr b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.stderr new file mode 100644 index 000000000000..aff7402bc91c --- /dev/null +++ b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.stderr @@ -0,0 +1,16 @@ +error[E0072]: recursive type `DEF_ID` has infinite size + --> $DIR/infinite-recursive-type-impl-trait-return.rs:7:5 + | +LL | enum E { + | ^^^^^^ +LL | This(E), + | - recursive without indirection + | +help: insert some indirection (e.g., a `DEF_ID`) to break the cycle + | +LL | This(Box), + | ++++ + + +error: aborting due to previous error + +For more information about this error, try `DEF_ID`. diff --git a/src/test/rustdoc-ui/infinite-recursive-type-impl-trait.rs b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait.rs index ac79582fb3f0..ac5172574986 100644 --- a/src/test/rustdoc-ui/infinite-recursive-type-impl-trait.rs +++ b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait.rs @@ -1,8 +1,5 @@ -// check-pass - fn f() -> impl Sized { - // rustdoc doesn't care that this is infinitely sized - enum E { + enum E { //~ ERROR V(E), } unimplemented!() diff --git a/src/test/rustdoc-ui/infinite-recursive-type-impl-trait.stderr b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait.stderr new file mode 100644 index 000000000000..a61577bd14af --- /dev/null +++ b/src/test/rustdoc-ui/infinite-recursive-type-impl-trait.stderr @@ -0,0 +1,16 @@ +error[E0072]: recursive type `f::E` has infinite size + --> $DIR/infinite-recursive-type-impl-trait.rs:2:5 + | +LL | enum E { + | ^^^^^^ +LL | V(E), + | - recursive without indirection + | +help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle + | +LL | V(Box), + | ++++ + + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0072`. From eb93d1bedeab64c6f5d661df6a309a5b8a9273ca Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 25 Nov 2022 20:37:14 +0100 Subject: [PATCH 061/321] Improve code readability --- src/librustdoc/html/render/write_shared.rs | 2 +- src/librustdoc/visit_ast.rs | 36 +++++++++++++--------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 94d8a9feca69..eaf149a43005 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -137,7 +137,7 @@ pub(super) fn write_shared( Ok((ret, krates)) } - /// Read a file and return all lines that match the "{crate}":{data},\ format, + /// Read a file and return all lines that match the "{crate}":{data},\ format, /// and return a tuple `(Vec, Vec)`. /// /// This forms the payload of files that look like this: diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index b94b64a0bd5c..fd50e59460d3 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -187,6 +187,16 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { ret } + #[inline] + fn add_to_current_mod( + &mut self, + item: &'tcx hir::Item<'_>, + renamed: Option, + parent_id: Option, + ) { + self.modules.last_mut().unwrap().items.push((item, renamed, parent_id)) + } + fn visit_item_inner( &mut self, item: &'tcx hir::Item<'_>, @@ -247,7 +257,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - self.modules.last_mut().unwrap().items.push((item, renamed, parent_id)); + self.add_to_current_mod(item, renamed, parent_id); } } hir::ItemKind::Macro(ref macro_def, _) => { @@ -267,7 +277,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let nonexported = !self.cx.tcx.has_attr(def_id, sym::macro_export); if is_macro_2_0 || nonexported || self.inlining { - self.modules.last_mut().unwrap().items.push((item, renamed, None)); + self.add_to_current_mod(item, renamed, None); } } hir::ItemKind::Mod(ref m) => { @@ -283,20 +293,20 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { | hir::ItemKind::Static(..) | hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) => { - self.modules.last_mut().unwrap().items.push((item, renamed, parent_id)) + self.add_to_current_mod(item, renamed, parent_id); } hir::ItemKind::Const(..) => { // Underscore constants do not correspond to a nameable item and // so are never useful in documentation. if name != kw::Underscore { - self.modules.last_mut().unwrap().items.push((item, renamed, parent_id)); + self.add_to_current_mod(item, renamed, parent_id); } } hir::ItemKind::Impl(impl_) => { // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick // them up regardless of where they're located. if !self.inlining && impl_.of_trait.is_none() { - self.modules.last_mut().unwrap().items.push((item, None, None)); + self.add_to_current_mod(item, None, None); } } } @@ -333,15 +343,13 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { // macro in the same module. let mut inserted = FxHashSet::default(); for export in self.cx.tcx.module_reexports(CRATE_DEF_ID).unwrap_or(&[]) { - if let Res::Def(DefKind::Macro(_), def_id) = export.res { - if let Some(local_def_id) = def_id.as_local() { - if self.cx.tcx.has_attr(def_id, sym::macro_export) { - if inserted.insert(def_id) { - let item = self.cx.tcx.hir().expect_item(local_def_id); - top_level_module.items.push((item, None, None)); - } - } - } + if let Res::Def(DefKind::Macro(_), def_id) = export.res && + let Some(local_def_id) = def_id.as_local() && + self.cx.tcx.has_attr(def_id, sym::macro_export) && + inserted.insert(def_id) + { + let item = self.cx.tcx.hir().expect_item(local_def_id); + top_level_module.items.push((item, None, None)); } } From a9d582f51f547583380f2f2894ae0c799b609a86 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 25 Nov 2022 20:46:55 +0100 Subject: [PATCH 062/321] Speed up execution a bit by removing some walks --- src/librustdoc/visit_ast.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index fd50e59460d3..e03f0b7f4ebe 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -250,7 +250,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { res, ident, is_glob, - om, please_inline, ) { continue; @@ -446,6 +445,26 @@ impl<'a, 'tcx> Visitor<'tcx> for RustdocVisitor<'a, 'tcx> { } fn visit_mod(&mut self, _: &hir::Mod<'tcx>, _: Span, _: hir::HirId) { - // handled in `visit_item_inner` + // Handled in `visit_item_inner` + } + + fn visit_use(&mut self, _: &hir::UsePath<'tcx>, _: hir::HirId) { + // Handled in `visit_item_inner` + } + + fn visit_path(&mut self, _: &hir::Path<'tcx>, _: hir::HirId) { + // Handled in `visit_item_inner` + } + + fn visit_label(&mut self, _: &rustc_ast::Label) { + // Unneeded. + } + + fn visit_infer(&mut self, _: &hir::InferArg) { + // Unneeded. + } + + fn visit_lifetime(&mut self, _: &hir::Lifetime) { + // Unneeded. } } From a954d6334d000225ae38f65f5f9e9c182e6764ae Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 7 Dec 2022 20:29:46 +0100 Subject: [PATCH 063/321] Improve code --- src/librustdoc/visit_ast.rs | 183 ++++++++++++++++++------------------ 1 file changed, 90 insertions(+), 93 deletions(-) diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index e03f0b7f4ebe..310a01194eaf 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -8,7 +8,6 @@ use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{walk_item, Visitor}; use rustc_hir::Node; use rustc_hir::CRATE_HIR_ID; -use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::ty::TyCtxt; use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; @@ -68,7 +67,6 @@ pub(crate) struct RustdocVisitor<'a, 'tcx> { inside_public_path: bool, exact_paths: FxHashMap>, modules: Vec>, - map: Map<'tcx>, } impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { @@ -81,7 +79,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { hir::CRATE_HIR_ID, cx.tcx.hir().root_module().spans.inner_span, ); - let map = cx.tcx.hir(); RustdocVisitor { cx, @@ -90,7 +87,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { inside_public_path: true, exact_paths: FxHashMap::default(), modules: vec![om], - map, } } @@ -99,6 +95,95 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { self.exact_paths.entry(did).or_insert_with(|| def_id_to_path(tcx, did)); } + pub(crate) fn visit(mut self) -> Module<'tcx> { + let root_module = self.cx.tcx.hir().root_module(); + self.visit_mod_contents(CRATE_HIR_ID, root_module); + + let mut top_level_module = self.modules.pop().unwrap(); + + // `#[macro_export] macro_rules!` items are reexported at the top level of the + // crate, regardless of where they're defined. We want to document the + // top level rexport of the macro, not its original definition, since + // the rexport defines the path that a user will actually see. Accordingly, + // we add the rexport as an item here, and then skip over the original + // definition in `visit_item()` below. + // + // We also skip `#[macro_export] macro_rules!` that have already been inserted, + // it can happen if within the same module a `#[macro_export] macro_rules!` + // is declared but also a reexport of itself producing two exports of the same + // macro in the same module. + let mut inserted = FxHashSet::default(); + for export in self.cx.tcx.module_reexports(CRATE_DEF_ID).unwrap_or(&[]) { + if let Res::Def(DefKind::Macro(_), def_id) = export.res && + let Some(local_def_id) = def_id.as_local() && + self.cx.tcx.has_attr(def_id, sym::macro_export) && + inserted.insert(def_id) + { + let item = self.cx.tcx.hir().expect_item(local_def_id); + top_level_module.items.push((item, None, None)); + } + } + + self.cx.cache.hidden_cfg = self + .cx + .tcx + .hir() + .attrs(CRATE_HIR_ID) + .iter() + .filter(|attr| attr.has_name(sym::doc)) + .flat_map(|attr| attr.meta_item_list().into_iter().flatten()) + .filter(|attr| attr.has_name(sym::cfg_hide)) + .flat_map(|attr| { + attr.meta_item_list() + .unwrap_or(&[]) + .iter() + .filter_map(|attr| { + Cfg::parse(attr.meta_item()?) + .map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg)) + .ok() + }) + .collect::>() + }) + .chain( + [Cfg::Cfg(sym::test, None), Cfg::Cfg(sym::doc, None), Cfg::Cfg(sym::doctest, None)] + .into_iter(), + ) + .collect(); + + self.cx.cache.exact_paths = self.exact_paths; + top_level_module + } + + /// This method will go through the given module items in two passes: + /// 1. The items which are not glob imports/reexports. + /// 2. The glob imports/reexports. + fn visit_mod_contents(&mut self, id: hir::HirId, m: &'tcx hir::Mod<'tcx>) { + debug!("Going through module {:?}", m); + let def_id = self.cx.tcx.hir().local_def_id(id).to_def_id(); + // Keep track of if there were any private modules in the path. + let orig_inside_public_path = self.inside_public_path; + self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public(); + + // Reimplementation of `walk_mod` because we need to do it in two passes (explanations in + // the second loop): + for &i in m.item_ids { + let item = self.cx.tcx.hir().item(i); + if !matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) { + self.visit_item(item); + } + } + for &i in m.item_ids { + let item = self.cx.tcx.hir().item(i); + // To match the way import precedence works, visit glob imports last. + // Later passes in rustdoc will de-duplicate by name and kind, so if glob- + // imported items appear last, then they'll be the ones that get discarded. + if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) { + self.visit_item(item); + } + } + self.inside_public_path = orig_inside_public_path; + } + /// Tries to resolve the target of a `pub use` statement and inlines the /// target if it is defined locally and would not be documented otherwise, /// or when it is specifically requested with `please_inline`. @@ -323,65 +408,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } - pub(crate) fn visit(mut self) -> Module<'tcx> { - let root_module = self.cx.tcx.hir().root_module(); - self.visit_mod_contents(CRATE_HIR_ID, root_module); - - let mut top_level_module = self.modules.pop().unwrap(); - - // `#[macro_export] macro_rules!` items are reexported at the top level of the - // crate, regardless of where they're defined. We want to document the - // top level rexport of the macro, not its original definition, since - // the rexport defines the path that a user will actually see. Accordingly, - // we add the rexport as an item here, and then skip over the original - // definition in `visit_item()` below. - // - // We also skip `#[macro_export] macro_rules!` that have already been inserted, - // it can happen if within the same module a `#[macro_export] macro_rules!` - // is declared but also a reexport of itself producing two exports of the same - // macro in the same module. - let mut inserted = FxHashSet::default(); - for export in self.cx.tcx.module_reexports(CRATE_DEF_ID).unwrap_or(&[]) { - if let Res::Def(DefKind::Macro(_), def_id) = export.res && - let Some(local_def_id) = def_id.as_local() && - self.cx.tcx.has_attr(def_id, sym::macro_export) && - inserted.insert(def_id) - { - let item = self.cx.tcx.hir().expect_item(local_def_id); - top_level_module.items.push((item, None, None)); - } - } - - self.cx.cache.hidden_cfg = self - .cx - .tcx - .hir() - .attrs(CRATE_HIR_ID) - .iter() - .filter(|attr| attr.has_name(sym::doc)) - .flat_map(|attr| attr.meta_item_list().into_iter().flatten()) - .filter(|attr| attr.has_name(sym::cfg_hide)) - .flat_map(|attr| { - attr.meta_item_list() - .unwrap_or(&[]) - .iter() - .filter_map(|attr| { - Cfg::parse(attr.meta_item()?) - .map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg)) - .ok() - }) - .collect::>() - }) - .chain( - [Cfg::Cfg(sym::test, None), Cfg::Cfg(sym::doc, None), Cfg::Cfg(sym::doctest, None)] - .into_iter(), - ) - .collect(); - - self.cx.cache.exact_paths = self.exact_paths; - top_level_module - } - /// This method will create a new module and push it onto the "modules stack" then call /// `visit_mod_contents`. Once done, it'll remove it from the "modules stack" and instead /// add into into the list of modules of the current module. @@ -393,35 +419,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let last = self.modules.pop().unwrap(); self.modules.last_mut().unwrap().mods.push(last); } - - /// This method will go through the given module items in two passes: - /// 1. The items which are not glob imports/reexports. - /// 2. The glob imports/reexports. - fn visit_mod_contents(&mut self, id: hir::HirId, m: &'tcx hir::Mod<'tcx>) { - debug!("Going through module {:?}", m); - let def_id = self.cx.tcx.hir().local_def_id(id).to_def_id(); - // Keep track of if there were any private modules in the path. - let orig_inside_public_path = self.inside_public_path; - self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public(); - - // Reimplementation of `walk_mod`: - for &i in m.item_ids { - let item = self.cx.tcx.hir().item(i); - if !matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) { - self.visit_item(item); - } - } - for &i in m.item_ids { - let item = self.cx.tcx.hir().item(i); - // To match the way import precedence works, visit glob imports last. - // Later passes in rustdoc will de-duplicate by name and kind, so if glob- - // imported items appear last, then they'll be the ones that get discarded. - if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) { - self.visit_item(item); - } - } - self.inside_public_path = orig_inside_public_path; - } } // We need to implement this visitor so it'll go everywhere and retrieve items we're interested in @@ -430,7 +427,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RustdocVisitor<'a, 'tcx> { type NestedFilter = nested_filter::All; fn nested_visit_map(&mut self) -> Self::Map { - self.map + self.cx.tcx.hir() } fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) { From a99e97af97f5f86e1892ec2bf633105384ed8bee Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Thu, 8 Dec 2022 01:30:07 -0500 Subject: [PATCH 064/321] Add 0..=isize::MAX range metadata to size loads from vtables --- compiler/rustc_abi/src/lib.rs | 12 ++++++ compiler/rustc_codegen_ssa/src/glue.rs | 3 ++ .../rustc_codegen_ssa/src/mir/intrinsic.rs | 12 ++++-- src/test/codegen/dst-vtable-align-nonzero.rs | 18 +++++++++ src/test/codegen/dst-vtable-size-range.rs | 37 +++++++++++++++++++ 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/test/codegen/dst-vtable-size-range.rs diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index e14c9ea9a5d1..27b8377a9e00 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -775,6 +775,18 @@ impl Integer { } } + /// Returns the largest signed value that can be represented by this Integer. + #[inline] + pub fn signed_max(self) -> i128 { + match self { + I8 => i8::MAX as i128, + I16 => i16::MAX as i128, + I32 => i32::MAX as i128, + I64 => i64::MAX as i128, + I128 => i128::MAX, + } + } + /// Finds the smallest Integer type which can represent the signed value. #[inline] pub fn fit_signed(x: i128) -> Integer { diff --git a/compiler/rustc_codegen_ssa/src/glue.rs b/compiler/rustc_codegen_ssa/src/glue.rs index 6015d48decae..0f6e6032f9bf 100644 --- a/compiler/rustc_codegen_ssa/src/glue.rs +++ b/compiler/rustc_codegen_ssa/src/glue.rs @@ -29,6 +29,9 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN) .get_usize(bx, vtable); + // Size is always <= isize::MAX. + let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128; + bx.range_metadata(size, WrappingRange { start: 0, end: size_bound }); // Alignment is always nonzero. bx.range_metadata(align, WrappingRange { start: 1, end: !0 }); diff --git a/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs b/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs index 215edbe02c08..a75609260eda 100644 --- a/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs +++ b/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs @@ -110,10 +110,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { _ => bug!(), }; let value = meth::VirtualIndex::from_index(idx).get_usize(bx, vtable); - if name == sym::vtable_align { + match name { + // Size is always <= isize::MAX. + sym::vtable_size => { + let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128; + bx.range_metadata(value, WrappingRange { start: 0, end: size_bound }); + }, // Alignment is always nonzero. - bx.range_metadata(value, WrappingRange { start: 1, end: !0 }); - }; + sym::vtable_align => bx.range_metadata(value, WrappingRange { start: 1, end: !0 }), + _ => {} + } value } sym::pref_align_of diff --git a/src/test/codegen/dst-vtable-align-nonzero.rs b/src/test/codegen/dst-vtable-align-nonzero.rs index 14c4c3f30f94..7ebb4173d563 100644 --- a/src/test/codegen/dst-vtable-align-nonzero.rs +++ b/src/test/codegen/dst-vtable-align-nonzero.rs @@ -1,6 +1,7 @@ // compile-flags: -O #![crate_type = "lib"] +#![feature(core_intrinsics)] // This test checks that we annotate alignment loads from vtables with nonzero range metadata, // and that this allows LLVM to eliminate redundant `align >= 1` checks. @@ -42,4 +43,21 @@ pub fn does_not_eliminate_runtime_check_when_align_2( &x.dst } +// CHECK-LABEL: @align_load_from_align_of_val +#[no_mangle] +pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize { + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] + core::mem::align_of_val(x) +} + +// CHECK-LABEL: @align_load_from_vtable_align_intrinsic +#[no_mangle] +pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize { + let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] + let align = core::intrinsics::vtable_align(vtable); + // make this function unique so it doesn't get merged with the previous + align + 1 +} + // CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0} diff --git a/src/test/codegen/dst-vtable-size-range.rs b/src/test/codegen/dst-vtable-size-range.rs new file mode 100644 index 000000000000..cec5876b3483 --- /dev/null +++ b/src/test/codegen/dst-vtable-size-range.rs @@ -0,0 +1,37 @@ +// compile-flags: -O + +#![crate_type = "lib"] +#![feature(core_intrinsics)] + +// Check that we annotate size loads from vtables with 0..(isize::MAX + 1) range metadata. + +pub trait Trait { + fn f(&self); +} + +// Note that rustc uses inclusive bounds, but LLVM uses exclusive bounds for range metadata. +// CHECK-LABEL: @generate_exclusive_bound +#[no_mangle] +pub fn generate_exclusive_bound() -> usize { + // CHECK: ret [[USIZE:i[0-9]+]] [[EXCLUSIVE_BOUND:[-0-9]+]] + isize::MAX as usize + 1 +} + +// CHECK-LABEL: @size_load_from_size_of_val +#[no_mangle] +pub fn size_load_from_size_of_val(x: &dyn Trait) -> usize { + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META:![0-9]+]] + core::mem::size_of_val(x) +} + +// CHECK-LABEL: @size_load_from_vtable_size_intrinsic +#[no_mangle] +pub unsafe fn size_load_from_vtable_size_intrinsic(x: &dyn Trait) -> usize { + let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); + // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] + let size = core::intrinsics::vtable_size(vtable); + // make this function unique so it doesn't get merged with the previous + size + 1 +} + +// CHECK: [[RANGE_META]] = !{[[USIZE]] 0, [[USIZE]] [[EXCLUSIVE_BOUND]]} From 2423ea2c5f430616f593fcffe43f17a3f669d881 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 8 Dec 2022 16:55:57 +0900 Subject: [PATCH 065/321] add a test for #103095 --- src/test/ui/borrowck/issue-103095.rs | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/ui/borrowck/issue-103095.rs diff --git a/src/test/ui/borrowck/issue-103095.rs b/src/test/ui/borrowck/issue-103095.rs new file mode 100644 index 000000000000..0340f39243fa --- /dev/null +++ b/src/test/ui/borrowck/issue-103095.rs @@ -0,0 +1,30 @@ +// check-pass + +trait FnOnceForGenericRef: FnOnce(&T) -> Self::FnOutput { + type FnOutput; +} + +impl R> FnOnceForGenericRef for F { + type FnOutput = R; +} + +struct Data> { + value: Option, + output: Option, +} + +impl> Data { + fn new(value: T, f: D) -> Self { + let output = f(&value); + Self { + value: Some(value), + output: Some(output), + } + } +} + +fn test() { + Data::new(String::new(), |_| {}); +} + +fn main() {} From 5479fe5f70bb32f037ff97de03ed185bdf2f54b7 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Thu, 8 Dec 2022 18:12:15 +0530 Subject: [PATCH 066/321] Add read_to_end for AnonPipe Add `read_to_end` method for `sys::{target}::pipe::AnonPipe`. This allows having a more optimized version of `read_to_end` for ChildStdout. Signed-off-by: Ayush Singh --- library/std/src/process.rs | 4 ++++ library/std/src/sys/unix/pipe.rs | 4 ++++ library/std/src/sys/unsupported/pipe.rs | 4 ++++ library/std/src/sys/windows/pipe.rs | 6 +++++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 400d25beb26f..1ff7bba85d23 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -362,6 +362,10 @@ impl Read for ChildStdout { fn is_read_vectored(&self) -> bool { self.inner.is_read_vectored() } + + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + self.inner.read_to_end(buf) + } } impl AsInner for ChildStdout { diff --git a/library/std/src/sys/unix/pipe.rs b/library/std/src/sys/unix/pipe.rs index a56c275c9420..a744d0ab6404 100644 --- a/library/std/src/sys/unix/pipe.rs +++ b/library/std/src/sys/unix/pipe.rs @@ -58,6 +58,10 @@ impl AnonPipe { self.0.is_read_vectored() } + pub fn read_to_end(&self, buf: &mut Vec) -> io::Result { + self.0.read_to_end(buf) + } + pub fn write(&self, buf: &[u8]) -> io::Result { self.0.write(buf) } diff --git a/library/std/src/sys/unsupported/pipe.rs b/library/std/src/sys/unsupported/pipe.rs index 25514c2322fa..75ce75467963 100644 --- a/library/std/src/sys/unsupported/pipe.rs +++ b/library/std/src/sys/unsupported/pipe.rs @@ -15,6 +15,10 @@ impl AnonPipe { self.0 } + pub fn read_to_end(&self, buf: &mut Vec) -> io::Result { + self.0 + } + pub fn write(&self, _buf: &[u8]) -> io::Result { self.0 } diff --git a/library/std/src/sys/windows/pipe.rs b/library/std/src/sys/windows/pipe.rs index 9f26acc45205..7b25edaa556f 100644 --- a/library/std/src/sys/windows/pipe.rs +++ b/library/std/src/sys/windows/pipe.rs @@ -1,7 +1,7 @@ use crate::os::windows::prelude::*; use crate::ffi::OsStr; -use crate::io::{self, IoSlice, IoSliceMut}; +use crate::io::{self, IoSlice, IoSliceMut, Read}; use crate::mem; use crate::path::Path; use crate::ptr; @@ -261,6 +261,10 @@ impl AnonPipe { self.inner.is_read_vectored() } + pub fn read_to_end(&self, buf: &mut Vec) -> io::Result { + self.handle().read_to_end(buf) + } + pub fn write(&self, buf: &[u8]) -> io::Result { unsafe { let len = crate::cmp::min(buf.len(), c::DWORD::MAX as usize) as c::DWORD; From 2d444a92f6f2da7f9f4c99a439485d31a9df199d Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Fri, 2 Dec 2022 20:41:29 -0500 Subject: [PATCH 067/321] Fix #10021 --- clippy_lints/src/methods/unnecessary_to_owned.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/methods/unnecessary_to_owned.rs b/clippy_lints/src/methods/unnecessary_to_owned.rs index 17b0507682ae..9263f0519724 100644 --- a/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -386,14 +386,12 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty< Node::Expr(parent_expr) => { if let Some((callee_def_id, call_substs, recv, call_args)) = get_callee_substs_and_args(cx, parent_expr) { - if Some(callee_def_id) == cx.tcx.lang_items().into_future_fn() { - return false; - } - let fn_sig = cx.tcx.fn_sig(callee_def_id).skip_binder(); if let Some(arg_index) = recv.into_iter().chain(call_args).position(|arg| arg.hir_id == expr.hir_id) && let Some(param_ty) = fn_sig.inputs().get(arg_index) && let ty::Param(ParamTy { index: param_index , ..}) = param_ty.kind() + // https://github.com/rust-lang/rust-clippy/issues/9504 and https://github.com/rust-lang/rust-clippy/issues/10021 + && (*param_index as usize) < call_substs.len() { if fn_sig .inputs() From 1f92f97e5acb3e400af406b17639cfae3926dd3b Mon Sep 17 00:00:00 2001 From: Caio Date: Thu, 8 Dec 2022 17:41:49 -0300 Subject: [PATCH 068/321] [arithmetic-side-effects]: Consider user-provided pairs --- clippy_lints/src/lib.rs | 13 +- .../src/operators/arithmetic_side_effects.rs | 74 ++++++++--- clippy_lints/src/operators/mod.rs | 3 - clippy_lints/src/utils/conf.rs | 43 +++++- .../arithmetic_side_effects_allowed.rs | 123 +++++++++++++++--- .../arithmetic_side_effects_allowed.stderr | 58 +++++++++ .../clippy.toml | 12 +- .../toml_unknown_key/conf_unknown_key.stderr | 2 + tests/ui/arithmetic_side_effects.stderr | 24 +--- 9 files changed, 286 insertions(+), 66 deletions(-) create mode 100644 tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.stderr diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3fe39488ab82..8facb78e35e1 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -508,9 +508,20 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: } let arithmetic_side_effects_allowed = conf.arithmetic_side_effects_allowed.clone(); + let arithmetic_side_effects_allowed_binary = conf.arithmetic_side_effects_allowed_binary.clone(); + let arithmetic_side_effects_allowed_unary = conf.arithmetic_side_effects_allowed_unary.clone(); store.register_late_pass(move |_| { Box::new(operators::arithmetic_side_effects::ArithmeticSideEffects::new( - arithmetic_side_effects_allowed.clone(), + arithmetic_side_effects_allowed + .iter() + .flat_map(|el| [[el.clone(), "*".to_string()], ["*".to_string(), el.clone()]]) + .chain(arithmetic_side_effects_allowed_binary.clone()) + .collect(), + arithmetic_side_effects_allowed + .iter() + .chain(arithmetic_side_effects_allowed_unary.iter()) + .cloned() + .collect(), )) }); store.register_late_pass(|_| Box::new(utils::dump_hir::DumpHir)); diff --git a/clippy_lints/src/operators/arithmetic_side_effects.rs b/clippy_lints/src/operators/arithmetic_side_effects.rs index 20b82d81a2ae..4fbc8398e373 100644 --- a/clippy_lints/src/operators/arithmetic_side_effects.rs +++ b/clippy_lints/src/operators/arithmetic_side_effects.rs @@ -5,25 +5,26 @@ use clippy_utils::{ peel_hir_expr_refs, }; use rustc_ast as ast; -use rustc_data_structures::fx::FxHashSet; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir as hir; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::Ty; use rustc_session::impl_lint_pass; use rustc_span::source_map::{Span, Spanned}; -const HARD_CODED_ALLOWED: &[&str] = &[ - "&str", - "f32", - "f64", - "std::num::Saturating", - "std::num::Wrapping", - "std::string::String", +const HARD_CODED_ALLOWED_BINARY: &[[&str; 2]] = &[ + ["f32", "f32"], + ["f64", "f64"], + ["std::num::Saturating", "std::num::Saturating"], + ["std::num::Wrapping", "std::num::Wrapping"], + ["std::string::String", "&str"], ]; +const HARD_CODED_ALLOWED_UNARY: &[&str] = &["f32", "f64", "std::num::Saturating", "std::num::Wrapping"]; #[derive(Debug)] pub struct ArithmeticSideEffects { - allowed: FxHashSet, + allowed_binary: FxHashMap>, + allowed_unary: FxHashSet, // Used to check whether expressions are constants, such as in enum discriminants and consts const_span: Option, expr_span: Option, @@ -33,19 +34,55 @@ impl_lint_pass!(ArithmeticSideEffects => [ARITHMETIC_SIDE_EFFECTS]); impl ArithmeticSideEffects { #[must_use] - pub fn new(mut allowed: FxHashSet) -> Self { - allowed.extend(HARD_CODED_ALLOWED.iter().copied().map(String::from)); + pub fn new(user_allowed_binary: Vec<[String; 2]>, user_allowed_unary: Vec) -> Self { + let mut allowed_binary: FxHashMap> = <_>::default(); + for [lhs, rhs] in user_allowed_binary.into_iter().chain( + HARD_CODED_ALLOWED_BINARY + .iter() + .copied() + .map(|[lhs, rhs]| [lhs.to_string(), rhs.to_string()]), + ) { + allowed_binary.entry(lhs).or_default().insert(rhs); + } + let allowed_unary = user_allowed_unary + .into_iter() + .chain(HARD_CODED_ALLOWED_UNARY.iter().copied().map(String::from)) + .collect(); Self { - allowed, + allowed_binary, + allowed_unary, const_span: None, expr_span: None, } } - /// Checks if the given `expr` has any of the inner `allowed` elements. - fn is_allowed_ty(&self, ty: Ty<'_>) -> bool { - self.allowed - .contains(ty.to_string().split('<').next().unwrap_or_default()) + /// Checks if the lhs and the rhs types of a binary operation like "addition" or + /// "multiplication" are present in the inner set of allowed types. + fn has_allowed_binary(&self, lhs_ty: Ty<'_>, rhs_ty: Ty<'_>) -> bool { + let lhs_ty_string = lhs_ty.to_string(); + let lhs_ty_string_elem = lhs_ty_string.split('<').next().unwrap_or_default(); + let rhs_ty_string = rhs_ty.to_string(); + let rhs_ty_string_elem = rhs_ty_string.split('<').next().unwrap_or_default(); + if let Some(rhs_from_specific) = self.allowed_binary.get(lhs_ty_string_elem) + && { + let rhs_has_allowed_ty = rhs_from_specific.contains(rhs_ty_string_elem); + rhs_has_allowed_ty || rhs_from_specific.contains("*") + } + { + true + } else if let Some(rhs_from_glob) = self.allowed_binary.get("*") { + rhs_from_glob.contains(rhs_ty_string_elem) + } else { + false + } + } + + /// Checks if the type of an unary operation like "negation" is present in the inner set of + /// allowed types. + fn has_allowed_unary(&self, ty: Ty<'_>) -> bool { + let ty_string = ty.to_string(); + let ty_string_elem = ty_string.split('<').next().unwrap_or_default(); + self.allowed_unary.contains(ty_string_elem) } // For example, 8i32 or &i64::MAX. @@ -97,8 +134,7 @@ impl ArithmeticSideEffects { }; let lhs_ty = cx.typeck_results().expr_ty(lhs); let rhs_ty = cx.typeck_results().expr_ty(rhs); - let lhs_and_rhs_have_the_same_ty = lhs_ty == rhs_ty; - if lhs_and_rhs_have_the_same_ty && self.is_allowed_ty(lhs_ty) && self.is_allowed_ty(rhs_ty) { + if self.has_allowed_binary(lhs_ty, rhs_ty) { return; } let has_valid_op = if Self::is_integral(lhs_ty) && Self::is_integral(rhs_ty) { @@ -137,7 +173,7 @@ impl ArithmeticSideEffects { return; } let ty = cx.typeck_results().expr_ty(expr).peel_refs(); - if self.is_allowed_ty(ty) { + if self.has_allowed_unary(ty) { return; } let actual_un_expr = peel_hir_expr_refs(un_expr).0; diff --git a/clippy_lints/src/operators/mod.rs b/clippy_lints/src/operators/mod.rs index b8a20d5ebe9b..eba230da6c39 100644 --- a/clippy_lints/src/operators/mod.rs +++ b/clippy_lints/src/operators/mod.rs @@ -90,9 +90,6 @@ declare_clippy_lint! { /// use rust_decimal::Decimal; /// let _n = Decimal::MAX + Decimal::MAX; /// ``` - /// - /// ### Allowed types - /// Custom allowed types can be specified through the "arithmetic-side-effects-allowed" filter. #[clippy::version = "1.64.0"] pub ARITHMETIC_SIDE_EFFECTS, restriction, diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index b6dc8cd7ab11..d1dde069970f 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -205,10 +205,49 @@ macro_rules! define_Conf { } define_Conf! { - /// Lint: Arithmetic. + /// Lint: ARITHMETIC_SIDE_EFFECTS. /// - /// Suppress checking of the passed type names. + /// Suppress checking of the passed type names in all types of operations. + /// + /// If a specific operation is desired, consider using `arithmetic_side_effects_allowed_binary` or `arithmetic_side_effects_allowed_unary` instead. + /// + /// #### Example + /// + /// ```toml + /// arithmetic-side-effects-allowed = ["SomeType", "AnotherType"] + /// ``` + /// + /// #### Noteworthy + /// + /// A type, say `SomeType`, listed in this configuration has the same behavior of `["SomeType" , "*"], ["*", "SomeType"]` in `arithmetic_side_effects_allowed_binary`. (arithmetic_side_effects_allowed: rustc_data_structures::fx::FxHashSet = <_>::default()), + /// Lint: ARITHMETIC_SIDE_EFFECTS. + /// + /// Suppress checking of the passed type pair names in binary operations like addition or + /// multiplication. + /// + /// Supports the "*" wildcard to indicate that a certain type won't trigger the lint regardless + /// of the involved counterpart. For example, `["SomeType", "*"]` or `["*", "AnotherType"]`. + /// + /// Pairs are asymmetric, which means that `["SomeType", "AnotherType"]` is not the same as + /// `["AnotherType", "SomeType"]`. + /// + /// #### Example + /// + /// ```toml + /// arithmetic-side-effects-allowed-binary = [["SomeType" , "f32"], ["AnotherType", "*"]] + /// ``` + (arithmetic_side_effects_allowed_binary: Vec<[String; 2]> = <_>::default()), + /// Lint: ARITHMETIC_SIDE_EFFECTS. + /// + /// Suppress checking of the passed type names in unary operations like "negation" (`-`). + /// + /// #### Example + /// + /// ```toml + /// arithmetic-side-effects-allowed-unary = ["SomeType", "AnotherType"] + /// ``` + (arithmetic_side_effects_allowed_unary: rustc_data_structures::fx::FxHashSet = <_>::default()), /// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UNUSED_SELF, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX. /// /// Suppress lints whenever the suggested change would cause breakage for other crates. diff --git a/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs b/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs index e8a023ab1764..36db9e54a228 100644 --- a/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs +++ b/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs @@ -2,32 +2,117 @@ use core::ops::{Add, Neg}; -#[derive(Clone, Copy)] -struct Point { - x: i32, - y: i32, +macro_rules! create { + ($name:ident) => { + #[allow(clippy::arithmetic_side_effects)] + #[derive(Clone, Copy)] + struct $name; + + impl Add<$name> for $name { + type Output = $name; + fn add(self, other: $name) -> Self::Output { + todo!() + } + } + + impl Add for $name { + type Output = $name; + fn add(self, other: i32) -> Self::Output { + todo!() + } + } + + impl Add<$name> for i32 { + type Output = $name; + fn add(self, other: $name) -> Self::Output { + todo!() + } + } + + impl Add for $name { + type Output = $name; + fn add(self, other: i64) -> Self::Output { + todo!() + } + } + + impl Add<$name> for i64 { + type Output = $name; + fn add(self, other: $name) -> Self::Output { + todo!() + } + } + + impl Neg for $name { + type Output = $name; + fn neg(self) -> Self::Output { + todo!() + } + } + }; } -impl Add for Point { - type Output = Self; +create!(Foo); +create!(Bar); +create!(Baz); +create!(OutOfNames); - fn add(self, other: Self) -> Self { - todo!() - } +fn lhs_and_rhs_are_equal() { + // is explicitly on the list + let _ = OutOfNames + OutOfNames; + // is explicitly on the list + let _ = Foo + Foo; + // is implicitly on the list + let _ = Bar + Bar; + // not on the list + let _ = Baz + Baz; } -impl Neg for Point { - type Output = Self; +fn lhs_is_different() { + // is explicitly on the list + let _ = 1i32 + OutOfNames; + // is explicitly on the list + let _ = 1i32 + Foo; + // is implicitly on the list + let _ = 1i32 + Bar; + // not on the list + let _ = 1i32 + Baz; - fn neg(self) -> Self::Output { - todo!() - } + // not on the list + let _ = 1i64 + Foo; + // is implicitly on the list + let _ = 1i64 + Bar; + // not on the list + let _ = 1i64 + Baz; } -fn main() { - let _ = Point { x: 1, y: 0 } + Point { x: 2, y: 3 }; +fn rhs_is_different() { + // is explicitly on the list + let _ = OutOfNames + 1i32; + // is explicitly on the list + let _ = Foo + 1i32; + // is implicitly on the list + let _ = Bar + 1i32; + // not on the list + let _ = Baz + 1i32; - let point: Point = Point { x: 1, y: 0 }; - let _ = point + point; - let _ = -point; + // not on the list + let _ = Foo + 1i64; + // is implicitly on the list + let _ = Bar + 1i64; + // not on the list + let _ = Baz + 1i64; } + +fn unary() { + // is explicitly on the list + let _ = -OutOfNames; + // is specifically on the list + let _ = -Foo; + // not on the list + let _ = -Bar; + // not on the list + let _ = -Baz; +} + +fn main() {} diff --git a/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.stderr b/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.stderr new file mode 100644 index 000000000000..ad89534aa1b0 --- /dev/null +++ b/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.stderr @@ -0,0 +1,58 @@ +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:68:13 + | +LL | let _ = Baz + Baz; + | ^^^^^^^^^ + | + = note: `-D clippy::arithmetic-side-effects` implied by `-D warnings` + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:79:13 + | +LL | let _ = 1i32 + Baz; + | ^^^^^^^^^^ + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:82:13 + | +LL | let _ = 1i64 + Foo; + | ^^^^^^^^^^ + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:86:13 + | +LL | let _ = 1i64 + Baz; + | ^^^^^^^^^^ + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:97:13 + | +LL | let _ = Baz + 1i32; + | ^^^^^^^^^^ + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:100:13 + | +LL | let _ = Foo + 1i64; + | ^^^^^^^^^^ + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:104:13 + | +LL | let _ = Baz + 1i64; + | ^^^^^^^^^^ + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:113:13 + | +LL | let _ = -Bar; + | ^^^^ + +error: arithmetic operation that can potentially result in unexpected side-effects + --> $DIR/arithmetic_side_effects_allowed.rs:115:13 + | +LL | let _ = -Baz; + | ^^^^ + +error: aborting due to 9 previous errors + diff --git a/tests/ui-toml/arithmetic_side_effects_allowed/clippy.toml b/tests/ui-toml/arithmetic_side_effects_allowed/clippy.toml index e736256f29a4..89cbea7ecfe4 100644 --- a/tests/ui-toml/arithmetic_side_effects_allowed/clippy.toml +++ b/tests/ui-toml/arithmetic_side_effects_allowed/clippy.toml @@ -1 +1,11 @@ -arithmetic-side-effects-allowed = ["Point"] +arithmetic-side-effects-allowed = [ + "OutOfNames" +] +arithmetic-side-effects-allowed-binary = [ + ["Foo", "Foo"], + ["Foo", "i32"], + ["i32", "Foo"], + ["Bar", "*"], + ["*", "Bar"], +] +arithmetic-side-effects-allowed-unary = ["Foo"] diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 01a5e962c949..d8329f9c61ba 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -6,6 +6,8 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie allow-unwrap-in-tests allowed-scripts arithmetic-side-effects-allowed + arithmetic-side-effects-allowed-binary + arithmetic-side-effects-allowed-unary array-size-threshold avoid-breaking-exported-api await-holding-invalid-types diff --git a/tests/ui/arithmetic_side_effects.stderr b/tests/ui/arithmetic_side_effects.stderr index 0259a0824e79..9fe4b7cf28d8 100644 --- a/tests/ui/arithmetic_side_effects.stderr +++ b/tests/ui/arithmetic_side_effects.stderr @@ -1,28 +1,10 @@ -error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:78:13 - | -LL | let _ = String::new() + ""; - | ^^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::arithmetic-side-effects` implied by `-D warnings` - -error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:86:27 - | -LL | let inferred_string = string + ""; - | ^^^^^^^^^^^ - -error: arithmetic operation that can potentially result in unexpected side-effects - --> $DIR/arithmetic_side_effects.rs:90:13 - | -LL | let _ = inferred_string + ""; - | ^^^^^^^^^^^^^^^^^^^^ - error: arithmetic operation that can potentially result in unexpected side-effects --> $DIR/arithmetic_side_effects.rs:165:5 | LL | _n += 1; | ^^^^^^^ + | + = note: `-D clippy::arithmetic-side-effects` implied by `-D warnings` error: arithmetic operation that can potentially result in unexpected side-effects --> $DIR/arithmetic_side_effects.rs:166:5 @@ -348,5 +330,5 @@ error: arithmetic operation that can potentially result in unexpected side-effec LL | _n = -&_n; | ^^^^ -error: aborting due to 58 previous errors +error: aborting due to 55 previous errors From e01d944c6c33a76cdbd1a257743d8c41a8203e89 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Thu, 8 Dec 2022 19:33:14 -0500 Subject: [PATCH 069/321] disable mergefunc instead of making fns unique --- src/test/codegen/dst-vtable-align-nonzero.rs | 6 ++---- src/test/codegen/dst-vtable-size-range.rs | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/test/codegen/dst-vtable-align-nonzero.rs b/src/test/codegen/dst-vtable-align-nonzero.rs index 7ebb4173d563..54f6e7f992fe 100644 --- a/src/test/codegen/dst-vtable-align-nonzero.rs +++ b/src/test/codegen/dst-vtable-align-nonzero.rs @@ -1,4 +1,4 @@ -// compile-flags: -O +// compile-flags: -O -Z merge-functions=disabled #![crate_type = "lib"] #![feature(core_intrinsics)] @@ -55,9 +55,7 @@ pub fn align_load_from_align_of_val(x: &dyn Trait) -> usize { pub unsafe fn align_load_from_vtable_align_intrinsic(x: &dyn Trait) -> usize { let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] - let align = core::intrinsics::vtable_align(vtable); - // make this function unique so it doesn't get merged with the previous - align + 1 + core::intrinsics::vtable_align(vtable) } // CHECK: [[RANGE_META]] = !{[[USIZE]] 1, [[USIZE]] 0} diff --git a/src/test/codegen/dst-vtable-size-range.rs b/src/test/codegen/dst-vtable-size-range.rs index cec5876b3483..671c8abdebd2 100644 --- a/src/test/codegen/dst-vtable-size-range.rs +++ b/src/test/codegen/dst-vtable-size-range.rs @@ -1,4 +1,4 @@ -// compile-flags: -O +// compile-flags: -O -Z merge-functions=disabled #![crate_type = "lib"] #![feature(core_intrinsics)] @@ -29,9 +29,7 @@ pub fn size_load_from_size_of_val(x: &dyn Trait) -> usize { pub unsafe fn size_load_from_vtable_size_intrinsic(x: &dyn Trait) -> usize { let (data, vtable): (*const (), *const ()) = core::mem::transmute(x); // CHECK: {{%[0-9]+}} = load [[USIZE]], {{.+}} !range [[RANGE_META]] - let size = core::intrinsics::vtable_size(vtable); - // make this function unique so it doesn't get merged with the previous - size + 1 + core::intrinsics::vtable_size(vtable) } // CHECK: [[RANGE_META]] = !{[[USIZE]] 0, [[USIZE]] [[EXCLUSIVE_BOUND]]} From 7e79c575e9676e27d2ccee3ef40b177a99a86cea Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 9 Dec 2022 12:26:01 +0000 Subject: [PATCH 070/321] Help rust-analyzer normalize query return types --- compiler/rustc_middle/src/query/keys.rs | 98 ++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index 880632561b9e..a96bc115e3b4 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -15,7 +15,7 @@ use rustc_span::{Span, DUMMY_SP}; /// The `Key` trait controls what types can legally be used as the key /// for a query. pub trait Key: Sized { - type CacheSelector = DefaultCacheSelector; + type CacheSelector; /// Given an instance of this key, what crate is it referring to? /// This is used to find the provider. @@ -37,6 +37,8 @@ pub trait Key: Sized { } impl Key for () { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -48,6 +50,8 @@ impl Key for () { } impl<'tcx> Key for ty::InstanceDef<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -59,6 +63,8 @@ impl<'tcx> Key for ty::InstanceDef<'tcx> { } impl<'tcx> Key for ty::Instance<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -70,6 +76,8 @@ impl<'tcx> Key for ty::Instance<'tcx> { } impl<'tcx> Key for mir::interpret::GlobalId<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -81,6 +89,8 @@ impl<'tcx> Key for mir::interpret::GlobalId<'tcx> { } impl<'tcx> Key for (Ty<'tcx>, Option>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -92,6 +102,8 @@ impl<'tcx> Key for (Ty<'tcx>, Option>) { } impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -145,6 +157,8 @@ impl Key for LocalDefId { } impl Key for DefId { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.krate == LOCAL_CRATE @@ -159,6 +173,8 @@ impl Key for DefId { } impl Key for ty::WithOptConstParam { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -169,6 +185,8 @@ impl Key for ty::WithOptConstParam { } impl Key for SimplifiedType { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -179,6 +197,8 @@ impl Key for SimplifiedType { } impl Key for (DefId, DefId) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0.krate == LOCAL_CRATE @@ -189,6 +209,8 @@ impl Key for (DefId, DefId) { } impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -199,6 +221,8 @@ impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) { } impl Key for (DefId, LocalDefId) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0.krate == LOCAL_CRATE @@ -209,6 +233,8 @@ impl Key for (DefId, LocalDefId) { } impl Key for (LocalDefId, DefId) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -219,6 +245,8 @@ impl Key for (LocalDefId, DefId) { } impl Key for (LocalDefId, LocalDefId) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -229,6 +257,8 @@ impl Key for (LocalDefId, LocalDefId) { } impl Key for (DefId, Option) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0.krate == LOCAL_CRATE @@ -243,6 +273,8 @@ impl Key for (DefId, Option) { } impl Key for (DefId, LocalDefId, Ident) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0.krate == LOCAL_CRATE @@ -253,6 +285,8 @@ impl Key for (DefId, LocalDefId, Ident) { } impl Key for (CrateNum, DefId) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0 == LOCAL_CRATE @@ -263,6 +297,8 @@ impl Key for (CrateNum, DefId) { } impl Key for (CrateNum, SimplifiedType) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0 == LOCAL_CRATE @@ -273,6 +309,8 @@ impl Key for (CrateNum, SimplifiedType) { } impl Key for (DefId, SimplifiedType) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0.krate == LOCAL_CRATE @@ -283,6 +321,8 @@ impl Key for (DefId, SimplifiedType) { } impl<'tcx> Key for SubstsRef<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -293,6 +333,8 @@ impl<'tcx> Key for SubstsRef<'tcx> { } impl<'tcx> Key for (DefId, SubstsRef<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0.krate == LOCAL_CRATE @@ -303,6 +345,8 @@ impl<'tcx> Key for (DefId, SubstsRef<'tcx>) { } impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { (self.0).def.did.krate == LOCAL_CRATE @@ -313,6 +357,8 @@ impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) { } impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -323,6 +369,8 @@ impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) { } impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.1.def_id().krate == LOCAL_CRATE @@ -333,6 +381,8 @@ impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) { } impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -343,6 +393,8 @@ impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) { } impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -353,6 +405,8 @@ impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> { } impl<'tcx> Key for ty::PolyTraitRef<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.def_id().krate == LOCAL_CRATE @@ -363,6 +417,8 @@ impl<'tcx> Key for ty::PolyTraitRef<'tcx> { } impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.def_id().krate == LOCAL_CRATE @@ -373,6 +429,8 @@ impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> { } impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.0.def_id().krate == LOCAL_CRATE @@ -383,6 +441,8 @@ impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) { } impl<'tcx> Key for GenericArg<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -393,6 +453,8 @@ impl<'tcx> Key for GenericArg<'tcx> { } impl<'tcx> Key for mir::ConstantKind<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -403,6 +465,8 @@ impl<'tcx> Key for mir::ConstantKind<'tcx> { } impl<'tcx> Key for ty::Const<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -413,6 +477,8 @@ impl<'tcx> Key for ty::Const<'tcx> { } impl<'tcx> Key for Ty<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -429,6 +495,8 @@ impl<'tcx> Key for Ty<'tcx> { } impl<'tcx> Key for TyAndLayout<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -439,6 +507,8 @@ impl<'tcx> Key for TyAndLayout<'tcx> { } impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -449,6 +519,8 @@ impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) { } impl<'tcx> Key for &'tcx ty::List> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -459,6 +531,8 @@ impl<'tcx> Key for &'tcx ty::List> { } impl<'tcx> Key for ty::ParamEnv<'tcx> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -469,6 +543,8 @@ impl<'tcx> Key for ty::ParamEnv<'tcx> { } impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { self.value.query_crate_is_local() @@ -479,6 +555,8 @@ impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> { } impl Key for Symbol { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -489,6 +567,8 @@ impl Key for Symbol { } impl Key for Option { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -501,6 +581,8 @@ impl Key for Option { /// Canonical query goals correspond to abstract trait operations that /// are not tied to any crate in particular. impl<'tcx, T> Key for Canonical<'tcx, T> { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -512,6 +594,8 @@ impl<'tcx, T> Key for Canonical<'tcx, T> { } impl Key for (Symbol, u32, u32) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -523,6 +607,8 @@ impl Key for (Symbol, u32, u32) { } impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -534,6 +620,8 @@ impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) { } impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -545,6 +633,8 @@ impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) { } impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -556,6 +646,8 @@ impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List>) { } impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -567,6 +659,8 @@ impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List>) { } impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true @@ -578,6 +672,8 @@ impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) { } impl Key for HirId { + type CacheSelector = DefaultCacheSelector; + #[inline(always)] fn query_crate_is_local(&self) -> bool { true From dc50bb0961e1033b9a0058800e563932b34b5e3a Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sat, 3 Dec 2022 16:03:27 -0800 Subject: [PATCH 071/321] Remove unneeded field from `SwitchTargets` --- clippy_utils/src/qualify_min_const_fn.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/clippy_utils/src/qualify_min_const_fn.rs b/clippy_utils/src/qualify_min_const_fn.rs index 480e8e55cf39..e053a9dc8881 100644 --- a/clippy_utils/src/qualify_min_const_fn.rs +++ b/clippy_utils/src/qualify_min_const_fn.rs @@ -303,7 +303,6 @@ fn check_terminator<'tcx>( TerminatorKind::SwitchInt { discr, - switch_ty: _, targets: _, } => check_operand(tcx, discr, span, body), From 4c80f210c38b78ddce983c2db16132a37bffd829 Mon Sep 17 00:00:00 2001 From: Hannah Town Date: Fri, 9 Dec 2022 13:29:50 -0500 Subject: [PATCH 072/321] Add lint `almost_complete_range` This replaces and expands `almost_complete_letter_range`. --- CHANGELOG.md | 1 + ...tter_range.rs => almost_complete_range.rs} | 24 +- clippy_lints/src/declared_lints.rs | 2 +- clippy_lints/src/lib.rs | 4 +- clippy_lints/src/renamed_lints.rs | 1 + tests/ui/almost_complete_letter_range.stderr | 113 --------- ...ange.fixed => almost_complete_range.fixed} | 49 +++- ...tter_range.rs => almost_complete_range.rs} | 49 +++- tests/ui/almost_complete_range.stderr | 235 ++++++++++++++++++ tests/ui/auxiliary/macro_rules.rs | 4 +- .../needless_parens_on_range_literals.fixed | 2 +- tests/ui/needless_parens_on_range_literals.rs | 2 +- tests/ui/rename.fixed | 2 + tests/ui/rename.rs | 2 + tests/ui/rename.stderr | 94 +++---- 15 files changed, 391 insertions(+), 193 deletions(-) rename clippy_lints/src/{almost_complete_letter_range.rs => almost_complete_range.rs} (85%) delete mode 100644 tests/ui/almost_complete_letter_range.stderr rename tests/ui/{almost_complete_letter_range.fixed => almost_complete_range.fixed} (56%) rename tests/ui/{almost_complete_letter_range.rs => almost_complete_range.rs} (57%) create mode 100644 tests/ui/almost_complete_range.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index bc41e70fe1f3..70a09db074e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3875,6 +3875,7 @@ Released 2018-09-13 [`alloc_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#alloc_instead_of_core [`allow_attributes_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason [`almost_complete_letter_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_letter_range +[`almost_complete_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_range [`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped [`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant [`arithmetic_side_effects`]: https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects diff --git a/clippy_lints/src/almost_complete_letter_range.rs b/clippy_lints/src/almost_complete_range.rs similarity index 85% rename from clippy_lints/src/almost_complete_letter_range.rs rename to clippy_lints/src/almost_complete_range.rs index 52beaf504a4e..42e14b5cd945 100644 --- a/clippy_lints/src/almost_complete_letter_range.rs +++ b/clippy_lints/src/almost_complete_range.rs @@ -10,8 +10,8 @@ use rustc_span::Span; declare_clippy_lint! { /// ### What it does - /// Checks for ranges which almost include the entire range of letters from 'a' to 'z', but - /// don't because they're a half open range. + /// Checks for ranges which almost include the entire range of letters from 'a' to 'z' + /// or digits from '0' to '9', but don't because they're a half open range. /// /// ### Why is this bad? /// This (`'a'..'z'`) is almost certainly a typo meant to include all letters. @@ -25,21 +25,21 @@ declare_clippy_lint! { /// let _ = 'a'..='z'; /// ``` #[clippy::version = "1.63.0"] - pub ALMOST_COMPLETE_LETTER_RANGE, + pub ALMOST_COMPLETE_RANGE, suspicious, - "almost complete letter range" + "almost complete range" } -impl_lint_pass!(AlmostCompleteLetterRange => [ALMOST_COMPLETE_LETTER_RANGE]); +impl_lint_pass!(AlmostCompleteRange => [ALMOST_COMPLETE_RANGE]); -pub struct AlmostCompleteLetterRange { +pub struct AlmostCompleteRange { msrv: Msrv, } -impl AlmostCompleteLetterRange { +impl AlmostCompleteRange { pub fn new(msrv: Msrv) -> Self { Self { msrv } } } -impl EarlyLintPass for AlmostCompleteLetterRange { +impl EarlyLintPass for AlmostCompleteRange { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) { if let ExprKind::Range(Some(start), Some(end), RangeLimits::HalfOpen) = &e.kind { let ctxt = e.span.ctxt(); @@ -87,14 +87,18 @@ fn check_range(cx: &EarlyContext<'_>, span: Span, start: &Expr, end: &Expr, sugg Ok(LitKind::Byte(b'A') | LitKind::Char('A')), Ok(LitKind::Byte(b'Z') | LitKind::Char('Z')), ) + | ( + Ok(LitKind::Byte(b'0') | LitKind::Char('0')), + Ok(LitKind::Byte(b'9') | LitKind::Char('9')), + ) ) && !in_external_macro(cx.sess(), span) { span_lint_and_then( cx, - ALMOST_COMPLETE_LETTER_RANGE, + ALMOST_COMPLETE_RANGE, span, - "almost complete ascii letter range", + "almost complete ascii range", |diag| { if let Some((span, sugg)) = sugg { diag.span_suggestion( diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 77c1da993dcc..3cd7d1d7e722 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -35,7 +35,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::utils::internal_lints::produce_ice::PRODUCE_ICE_INFO, #[cfg(feature = "internal")] crate::utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH_INFO, - crate::almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE_INFO, + crate::almost_complete_range::ALMOST_COMPLETE_RANGE_INFO, crate::approx_const::APPROX_CONSTANT_INFO, crate::as_conversions::AS_CONVERSIONS_INFO, crate::asm_syntax::INLINE_ASM_X86_ATT_SYNTAX_INFO, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index bea28f7620b8..39850d598038 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -66,7 +66,7 @@ mod declared_lints; mod renamed_lints; // begin lints modules, do not remove this comment, it’s used in `update_lints` -mod almost_complete_letter_range; +mod almost_complete_range; mod approx_const; mod as_conversions; mod asm_syntax; @@ -876,7 +876,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(rc_clone_in_vec_init::RcCloneInVecInit)); store.register_early_pass(|| Box::::default()); store.register_early_pass(|| Box::new(unused_rounding::UnusedRounding)); - store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv()))); + store.register_early_pass(move || Box::new(almost_complete_range::AlmostCompleteRange::new(msrv()))); store.register_late_pass(|_| Box::new(swap_ptr_to_ref::SwapPtrToRef)); store.register_late_pass(|_| Box::new(mismatching_type_param_order::TypeParamMismatch)); store.register_late_pass(|_| Box::new(read_zero_byte_vec::ReadZeroByteVec)); diff --git a/clippy_lints/src/renamed_lints.rs b/clippy_lints/src/renamed_lints.rs index 8e214218f23a..72c25592609b 100644 --- a/clippy_lints/src/renamed_lints.rs +++ b/clippy_lints/src/renamed_lints.rs @@ -2,6 +2,7 @@ #[rustfmt::skip] pub static RENAMED_LINTS: &[(&str, &str)] = &[ + ("clippy::almost_complete_letter_range", "clippy::almost_complete_range"), ("clippy::blacklisted_name", "clippy::disallowed_names"), ("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions"), ("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions"), diff --git a/tests/ui/almost_complete_letter_range.stderr b/tests/ui/almost_complete_letter_range.stderr deleted file mode 100644 index 9abf6d6c5e7d..000000000000 --- a/tests/ui/almost_complete_letter_range.stderr +++ /dev/null @@ -1,113 +0,0 @@ -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:29:17 - | -LL | let _ = ('a') ..'z'; - | ^^^^^^--^^^ - | | - | help: use an inclusive range: `..=` - | - = note: `-D clippy::almost-complete-letter-range` implied by `-D warnings` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:30:17 - | -LL | let _ = 'A' .. ('Z'); - | ^^^^--^^^^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:36:13 - | -LL | let _ = (b'a')..(b'z'); - | ^^^^^^--^^^^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:37:13 - | -LL | let _ = b'A'..b'Z'; - | ^^^^--^^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:42:13 - | -LL | let _ = a!()..'z'; - | ^^^^--^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:45:9 - | -LL | b'a'..b'z' if true => 1, - | ^^^^--^^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:46:9 - | -LL | b'A'..b'Z' if true => 2, - | ^^^^--^^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:53:9 - | -LL | 'a'..'z' if true => 1, - | ^^^--^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:54:9 - | -LL | 'A'..'Z' if true => 2, - | ^^^--^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:22:17 - | -LL | let _ = 'a'..'z'; - | ^^^--^^^ - | | - | help: use an inclusive range: `..=` -... -LL | b!(); - | ---- in this macro invocation - | - = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:67:9 - | -LL | 'a'..'z' => 1, - | ^^^--^^^ - | | - | help: use an inclusive range: `...` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:74:13 - | -LL | let _ = 'a'..'z'; - | ^^^--^^^ - | | - | help: use an inclusive range: `..=` - -error: almost complete ascii letter range - --> $DIR/almost_complete_letter_range.rs:76:9 - | -LL | 'a'..'z' => 1, - | ^^^--^^^ - | | - | help: use an inclusive range: `..=` - -error: aborting due to 13 previous errors - diff --git a/tests/ui/almost_complete_letter_range.fixed b/tests/ui/almost_complete_range.fixed similarity index 56% rename from tests/ui/almost_complete_letter_range.fixed rename to tests/ui/almost_complete_range.fixed index adcbd4d5134d..6046addf7196 100644 --- a/tests/ui/almost_complete_letter_range.fixed +++ b/tests/ui/almost_complete_range.fixed @@ -4,9 +4,10 @@ #![feature(exclusive_range_pattern)] #![feature(stmt_expr_attributes)] -#![warn(clippy::almost_complete_letter_range)] +#![warn(clippy::almost_complete_range)] #![allow(ellipsis_inclusive_range_patterns)] #![allow(clippy::needless_parens_on_range_literals)] +#![allow(clippy::double_parens)] #[macro_use] extern crate macro_rules; @@ -16,10 +17,22 @@ macro_rules! a { 'a' }; } +macro_rules! A { + () => { + 'A' + }; +} +macro_rules! zero { + () => { + '0' + }; +} macro_rules! b { () => { let _ = 'a'..='z'; + let _ = 'A'..='Z'; + let _ = '0'..='9'; }; } @@ -28,36 +41,46 @@ fn main() { { let _ = ('a') ..='z'; let _ = 'A' ..= ('Z'); + let _ = ((('0'))) ..= ('9'); } let _ = 'b'..'z'; let _ = 'B'..'Z'; + let _ = '1'..'9'; let _ = (b'a')..=(b'z'); let _ = b'A'..=b'Z'; + let _ = b'0'..=b'9'; let _ = b'b'..b'z'; let _ = b'B'..b'Z'; + let _ = b'1'..b'9'; let _ = a!()..='z'; + let _ = A!()..='Z'; + let _ = zero!()..='9'; let _ = match 0u8 { b'a'..=b'z' if true => 1, b'A'..=b'Z' if true => 2, - b'b'..b'z' => 3, - b'B'..b'Z' => 4, - _ => 5, + b'0'..=b'9' if true => 3, + b'b'..b'z' => 4, + b'B'..b'Z' => 5, + b'1'..b'9' => 6, + _ => 7, }; let _ = match 'x' { 'a'..='z' if true => 1, 'A'..='Z' if true => 2, - 'b'..'z' => 3, - 'B'..'Z' => 4, - _ => 5, + '0'..='9' if true => 3, + 'b'..'z' => 4, + 'B'..'Z' => 5, + '1'..'9' => 6, + _ => 7, }; - almost_complete_letter_range!(); + almost_complete_range!(); b!(); } @@ -65,15 +88,21 @@ fn main() { fn _under_msrv() { let _ = match 'a' { 'a'...'z' => 1, - _ => 2, + 'A'...'Z' => 2, + '0'...'9' => 3, + _ => 4, }; } #[clippy::msrv = "1.26"] fn _meets_msrv() { let _ = 'a'..='z'; + let _ = 'A'..='Z'; + let _ = '0'..='9'; let _ = match 'a' { 'a'..='z' => 1, - _ => 2, + 'A'..='Z' => 1, + '0'..='9' => 3, + _ => 4, }; } diff --git a/tests/ui/almost_complete_letter_range.rs b/tests/ui/almost_complete_range.rs similarity index 57% rename from tests/ui/almost_complete_letter_range.rs rename to tests/ui/almost_complete_range.rs index 9979316eca42..ae7e07ab872b 100644 --- a/tests/ui/almost_complete_letter_range.rs +++ b/tests/ui/almost_complete_range.rs @@ -4,9 +4,10 @@ #![feature(exclusive_range_pattern)] #![feature(stmt_expr_attributes)] -#![warn(clippy::almost_complete_letter_range)] +#![warn(clippy::almost_complete_range)] #![allow(ellipsis_inclusive_range_patterns)] #![allow(clippy::needless_parens_on_range_literals)] +#![allow(clippy::double_parens)] #[macro_use] extern crate macro_rules; @@ -16,10 +17,22 @@ macro_rules! a { 'a' }; } +macro_rules! A { + () => { + 'A' + }; +} +macro_rules! zero { + () => { + '0' + }; +} macro_rules! b { () => { let _ = 'a'..'z'; + let _ = 'A'..'Z'; + let _ = '0'..'9'; }; } @@ -28,36 +41,46 @@ fn main() { { let _ = ('a') ..'z'; let _ = 'A' .. ('Z'); + let _ = ((('0'))) .. ('9'); } let _ = 'b'..'z'; let _ = 'B'..'Z'; + let _ = '1'..'9'; let _ = (b'a')..(b'z'); let _ = b'A'..b'Z'; + let _ = b'0'..b'9'; let _ = b'b'..b'z'; let _ = b'B'..b'Z'; + let _ = b'1'..b'9'; let _ = a!()..'z'; + let _ = A!()..'Z'; + let _ = zero!()..'9'; let _ = match 0u8 { b'a'..b'z' if true => 1, b'A'..b'Z' if true => 2, - b'b'..b'z' => 3, - b'B'..b'Z' => 4, - _ => 5, + b'0'..b'9' if true => 3, + b'b'..b'z' => 4, + b'B'..b'Z' => 5, + b'1'..b'9' => 6, + _ => 7, }; let _ = match 'x' { 'a'..'z' if true => 1, 'A'..'Z' if true => 2, - 'b'..'z' => 3, - 'B'..'Z' => 4, - _ => 5, + '0'..'9' if true => 3, + 'b'..'z' => 4, + 'B'..'Z' => 5, + '1'..'9' => 6, + _ => 7, }; - almost_complete_letter_range!(); + almost_complete_range!(); b!(); } @@ -65,15 +88,21 @@ fn main() { fn _under_msrv() { let _ = match 'a' { 'a'..'z' => 1, - _ => 2, + 'A'..'Z' => 2, + '0'..'9' => 3, + _ => 4, }; } #[clippy::msrv = "1.26"] fn _meets_msrv() { let _ = 'a'..'z'; + let _ = 'A'..'Z'; + let _ = '0'..'9'; let _ = match 'a' { 'a'..'z' => 1, - _ => 2, + 'A'..'Z' => 1, + '0'..'9' => 3, + _ => 4, }; } diff --git a/tests/ui/almost_complete_range.stderr b/tests/ui/almost_complete_range.stderr new file mode 100644 index 000000000000..a7a532878502 --- /dev/null +++ b/tests/ui/almost_complete_range.stderr @@ -0,0 +1,235 @@ +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:42:17 + | +LL | let _ = ('a') ..'z'; + | ^^^^^^--^^^ + | | + | help: use an inclusive range: `..=` + | + = note: `-D clippy::almost-complete-range` implied by `-D warnings` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:43:17 + | +LL | let _ = 'A' .. ('Z'); + | ^^^^--^^^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:44:17 + | +LL | let _ = ((('0'))) .. ('9'); + | ^^^^^^^^^^--^^^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:51:13 + | +LL | let _ = (b'a')..(b'z'); + | ^^^^^^--^^^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:52:13 + | +LL | let _ = b'A'..b'Z'; + | ^^^^--^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:53:13 + | +LL | let _ = b'0'..b'9'; + | ^^^^--^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:59:13 + | +LL | let _ = a!()..'z'; + | ^^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:60:13 + | +LL | let _ = A!()..'Z'; + | ^^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:61:13 + | +LL | let _ = zero!()..'9'; + | ^^^^^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:64:9 + | +LL | b'a'..b'z' if true => 1, + | ^^^^--^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:65:9 + | +LL | b'A'..b'Z' if true => 2, + | ^^^^--^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:66:9 + | +LL | b'0'..b'9' if true => 3, + | ^^^^--^^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:74:9 + | +LL | 'a'..'z' if true => 1, + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:75:9 + | +LL | 'A'..'Z' if true => 2, + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:76:9 + | +LL | '0'..'9' if true => 3, + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:33:17 + | +LL | let _ = 'a'..'z'; + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` +... +LL | b!(); + | ---- in this macro invocation + | + = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:34:17 + | +LL | let _ = 'A'..'Z'; + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` +... +LL | b!(); + | ---- in this macro invocation + | + = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:35:17 + | +LL | let _ = '0'..'9'; + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` +... +LL | b!(); + | ---- in this macro invocation + | + = note: this error originates in the macro `b` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:90:9 + | +LL | 'a'..'z' => 1, + | ^^^--^^^ + | | + | help: use an inclusive range: `...` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:91:9 + | +LL | 'A'..'Z' => 2, + | ^^^--^^^ + | | + | help: use an inclusive range: `...` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:92:9 + | +LL | '0'..'9' => 3, + | ^^^--^^^ + | | + | help: use an inclusive range: `...` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:99:13 + | +LL | let _ = 'a'..'z'; + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:100:13 + | +LL | let _ = 'A'..'Z'; + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:101:13 + | +LL | let _ = '0'..'9'; + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:103:9 + | +LL | 'a'..'z' => 1, + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:104:9 + | +LL | 'A'..'Z' => 1, + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: almost complete ascii range + --> $DIR/almost_complete_range.rs:105:9 + | +LL | '0'..'9' => 3, + | ^^^--^^^ + | | + | help: use an inclusive range: `..=` + +error: aborting due to 27 previous errors + diff --git a/tests/ui/auxiliary/macro_rules.rs b/tests/ui/auxiliary/macro_rules.rs index ef3ca9aea380..1e5f20e8c39b 100644 --- a/tests/ui/auxiliary/macro_rules.rs +++ b/tests/ui/auxiliary/macro_rules.rs @@ -142,8 +142,10 @@ macro_rules! equatable_if_let { } #[macro_export] -macro_rules! almost_complete_letter_range { +macro_rules! almost_complete_range { () => { let _ = 'a'..'z'; + let _ = 'A'..'Z'; + let _ = '0'..'9'; }; } diff --git a/tests/ui/needless_parens_on_range_literals.fixed b/tests/ui/needless_parens_on_range_literals.fixed index 1bd75c806bc9..f11330a8916d 100644 --- a/tests/ui/needless_parens_on_range_literals.fixed +++ b/tests/ui/needless_parens_on_range_literals.fixed @@ -2,7 +2,7 @@ // edition:2018 #![warn(clippy::needless_parens_on_range_literals)] -#![allow(clippy::almost_complete_letter_range)] +#![allow(clippy::almost_complete_range)] fn main() { let _ = 'a'..='z'; diff --git a/tests/ui/needless_parens_on_range_literals.rs b/tests/ui/needless_parens_on_range_literals.rs index 7abb8a1adc1b..671c0009e23b 100644 --- a/tests/ui/needless_parens_on_range_literals.rs +++ b/tests/ui/needless_parens_on_range_literals.rs @@ -2,7 +2,7 @@ // edition:2018 #![warn(clippy::needless_parens_on_range_literals)] -#![allow(clippy::almost_complete_letter_range)] +#![allow(clippy::almost_complete_range)] fn main() { let _ = ('a')..=('z'); diff --git a/tests/ui/rename.fixed b/tests/ui/rename.fixed index 689928f04794..2f76b5752960 100644 --- a/tests/ui/rename.fixed +++ b/tests/ui/rename.fixed @@ -4,6 +4,7 @@ // run-rustfix +#![allow(clippy::almost_complete_range)] #![allow(clippy::disallowed_names)] #![allow(clippy::blocks_in_if_conditions)] #![allow(clippy::box_collection)] @@ -37,6 +38,7 @@ #![allow(temporary_cstring_as_ptr)] #![allow(unknown_lints)] #![allow(unused_labels)] +#![warn(clippy::almost_complete_range)] #![warn(clippy::disallowed_names)] #![warn(clippy::blocks_in_if_conditions)] #![warn(clippy::blocks_in_if_conditions)] diff --git a/tests/ui/rename.rs b/tests/ui/rename.rs index b74aa650ffd4..699c0ff464e9 100644 --- a/tests/ui/rename.rs +++ b/tests/ui/rename.rs @@ -4,6 +4,7 @@ // run-rustfix +#![allow(clippy::almost_complete_range)] #![allow(clippy::disallowed_names)] #![allow(clippy::blocks_in_if_conditions)] #![allow(clippy::box_collection)] @@ -37,6 +38,7 @@ #![allow(temporary_cstring_as_ptr)] #![allow(unknown_lints)] #![allow(unused_labels)] +#![warn(clippy::almost_complete_letter_range)] #![warn(clippy::blacklisted_name)] #![warn(clippy::block_in_if_condition_expr)] #![warn(clippy::block_in_if_condition_stmt)] diff --git a/tests/ui/rename.stderr b/tests/ui/rename.stderr index 622a32c5908a..9af58dc75a68 100644 --- a/tests/ui/rename.stderr +++ b/tests/ui/rename.stderr @@ -1,244 +1,250 @@ -error: lint `clippy::blacklisted_name` has been renamed to `clippy::disallowed_names` - --> $DIR/rename.rs:40:9 +error: lint `clippy::almost_complete_letter_range` has been renamed to `clippy::almost_complete_range` + --> $DIR/rename.rs:41:9 | -LL | #![warn(clippy::blacklisted_name)] - | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_names` +LL | #![warn(clippy::almost_complete_letter_range)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::almost_complete_range` | = note: `-D renamed-and-removed-lints` implied by `-D warnings` +error: lint `clippy::blacklisted_name` has been renamed to `clippy::disallowed_names` + --> $DIR/rename.rs:42:9 + | +LL | #![warn(clippy::blacklisted_name)] + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_names` + error: lint `clippy::block_in_if_condition_expr` has been renamed to `clippy::blocks_in_if_conditions` - --> $DIR/rename.rs:41:9 + --> $DIR/rename.rs:43:9 | LL | #![warn(clippy::block_in_if_condition_expr)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::blocks_in_if_conditions` error: lint `clippy::block_in_if_condition_stmt` has been renamed to `clippy::blocks_in_if_conditions` - --> $DIR/rename.rs:42:9 + --> $DIR/rename.rs:44:9 | LL | #![warn(clippy::block_in_if_condition_stmt)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::blocks_in_if_conditions` error: lint `clippy::box_vec` has been renamed to `clippy::box_collection` - --> $DIR/rename.rs:43:9 + --> $DIR/rename.rs:45:9 | LL | #![warn(clippy::box_vec)] | ^^^^^^^^^^^^^^^ help: use the new name: `clippy::box_collection` error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes` - --> $DIR/rename.rs:44:9 + --> $DIR/rename.rs:46:9 | LL | #![warn(clippy::const_static_lifetime)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes` error: lint `clippy::cyclomatic_complexity` has been renamed to `clippy::cognitive_complexity` - --> $DIR/rename.rs:45:9 + --> $DIR/rename.rs:47:9 | LL | #![warn(clippy::cyclomatic_complexity)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity` error: lint `clippy::disallowed_method` has been renamed to `clippy::disallowed_methods` - --> $DIR/rename.rs:46:9 + --> $DIR/rename.rs:48:9 | LL | #![warn(clippy::disallowed_method)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_methods` error: lint `clippy::disallowed_type` has been renamed to `clippy::disallowed_types` - --> $DIR/rename.rs:47:9 + --> $DIR/rename.rs:49:9 | LL | #![warn(clippy::disallowed_type)] | ^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::disallowed_types` error: lint `clippy::eval_order_dependence` has been renamed to `clippy::mixed_read_write_in_expression` - --> $DIR/rename.rs:48:9 + --> $DIR/rename.rs:50:9 | LL | #![warn(clippy::eval_order_dependence)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::mixed_read_write_in_expression` error: lint `clippy::identity_conversion` has been renamed to `clippy::useless_conversion` - --> $DIR/rename.rs:49:9 + --> $DIR/rename.rs:51:9 | LL | #![warn(clippy::identity_conversion)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::useless_conversion` error: lint `clippy::if_let_some_result` has been renamed to `clippy::match_result_ok` - --> $DIR/rename.rs:50:9 + --> $DIR/rename.rs:52:9 | LL | #![warn(clippy::if_let_some_result)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::match_result_ok` error: lint `clippy::logic_bug` has been renamed to `clippy::overly_complex_bool_expr` - --> $DIR/rename.rs:51:9 + --> $DIR/rename.rs:53:9 | LL | #![warn(clippy::logic_bug)] | ^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::overly_complex_bool_expr` error: lint `clippy::new_without_default_derive` has been renamed to `clippy::new_without_default` - --> $DIR/rename.rs:52:9 + --> $DIR/rename.rs:54:9 | LL | #![warn(clippy::new_without_default_derive)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::new_without_default` error: lint `clippy::option_and_then_some` has been renamed to `clippy::bind_instead_of_map` - --> $DIR/rename.rs:53:9 + --> $DIR/rename.rs:55:9 | LL | #![warn(clippy::option_and_then_some)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::bind_instead_of_map` error: lint `clippy::option_expect_used` has been renamed to `clippy::expect_used` - --> $DIR/rename.rs:54:9 + --> $DIR/rename.rs:56:9 | LL | #![warn(clippy::option_expect_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::expect_used` error: lint `clippy::option_map_unwrap_or` has been renamed to `clippy::map_unwrap_or` - --> $DIR/rename.rs:55:9 + --> $DIR/rename.rs:57:9 | LL | #![warn(clippy::option_map_unwrap_or)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or` error: lint `clippy::option_map_unwrap_or_else` has been renamed to `clippy::map_unwrap_or` - --> $DIR/rename.rs:56:9 + --> $DIR/rename.rs:58:9 | LL | #![warn(clippy::option_map_unwrap_or_else)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or` error: lint `clippy::option_unwrap_used` has been renamed to `clippy::unwrap_used` - --> $DIR/rename.rs:57:9 + --> $DIR/rename.rs:59:9 | LL | #![warn(clippy::option_unwrap_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::unwrap_used` error: lint `clippy::ref_in_deref` has been renamed to `clippy::needless_borrow` - --> $DIR/rename.rs:58:9 + --> $DIR/rename.rs:60:9 | LL | #![warn(clippy::ref_in_deref)] | ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::needless_borrow` error: lint `clippy::result_expect_used` has been renamed to `clippy::expect_used` - --> $DIR/rename.rs:59:9 + --> $DIR/rename.rs:61:9 | LL | #![warn(clippy::result_expect_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::expect_used` error: lint `clippy::result_map_unwrap_or_else` has been renamed to `clippy::map_unwrap_or` - --> $DIR/rename.rs:60:9 + --> $DIR/rename.rs:62:9 | LL | #![warn(clippy::result_map_unwrap_or_else)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::map_unwrap_or` error: lint `clippy::result_unwrap_used` has been renamed to `clippy::unwrap_used` - --> $DIR/rename.rs:61:9 + --> $DIR/rename.rs:63:9 | LL | #![warn(clippy::result_unwrap_used)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::unwrap_used` error: lint `clippy::single_char_push_str` has been renamed to `clippy::single_char_add_str` - --> $DIR/rename.rs:62:9 + --> $DIR/rename.rs:64:9 | LL | #![warn(clippy::single_char_push_str)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::single_char_add_str` error: lint `clippy::stutter` has been renamed to `clippy::module_name_repetitions` - --> $DIR/rename.rs:63:9 + --> $DIR/rename.rs:65:9 | LL | #![warn(clippy::stutter)] | ^^^^^^^^^^^^^^^ help: use the new name: `clippy::module_name_repetitions` error: lint `clippy::to_string_in_display` has been renamed to `clippy::recursive_format_impl` - --> $DIR/rename.rs:64:9 + --> $DIR/rename.rs:66:9 | LL | #![warn(clippy::to_string_in_display)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::recursive_format_impl` error: lint `clippy::zero_width_space` has been renamed to `clippy::invisible_characters` - --> $DIR/rename.rs:65:9 + --> $DIR/rename.rs:67:9 | LL | #![warn(clippy::zero_width_space)] | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::invisible_characters` error: lint `clippy::drop_bounds` has been renamed to `drop_bounds` - --> $DIR/rename.rs:66:9 + --> $DIR/rename.rs:68:9 | LL | #![warn(clippy::drop_bounds)] | ^^^^^^^^^^^^^^^^^^^ help: use the new name: `drop_bounds` error: lint `clippy::for_loop_over_option` has been renamed to `for_loops_over_fallibles` - --> $DIR/rename.rs:67:9 + --> $DIR/rename.rs:69:9 | LL | #![warn(clippy::for_loop_over_option)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` error: lint `clippy::for_loop_over_result` has been renamed to `for_loops_over_fallibles` - --> $DIR/rename.rs:68:9 + --> $DIR/rename.rs:70:9 | LL | #![warn(clippy::for_loop_over_result)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` error: lint `clippy::for_loops_over_fallibles` has been renamed to `for_loops_over_fallibles` - --> $DIR/rename.rs:69:9 + --> $DIR/rename.rs:71:9 | LL | #![warn(clippy::for_loops_over_fallibles)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` error: lint `clippy::into_iter_on_array` has been renamed to `array_into_iter` - --> $DIR/rename.rs:70:9 + --> $DIR/rename.rs:72:9 | LL | #![warn(clippy::into_iter_on_array)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `array_into_iter` error: lint `clippy::invalid_atomic_ordering` has been renamed to `invalid_atomic_ordering` - --> $DIR/rename.rs:71:9 + --> $DIR/rename.rs:73:9 | LL | #![warn(clippy::invalid_atomic_ordering)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_atomic_ordering` error: lint `clippy::invalid_ref` has been renamed to `invalid_value` - --> $DIR/rename.rs:72:9 + --> $DIR/rename.rs:74:9 | LL | #![warn(clippy::invalid_ref)] | ^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_value` error: lint `clippy::let_underscore_drop` has been renamed to `let_underscore_drop` - --> $DIR/rename.rs:73:9 + --> $DIR/rename.rs:75:9 | LL | #![warn(clippy::let_underscore_drop)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `let_underscore_drop` error: lint `clippy::mem_discriminant_non_enum` has been renamed to `enum_intrinsics_non_enums` - --> $DIR/rename.rs:74:9 + --> $DIR/rename.rs:76:9 | LL | #![warn(clippy::mem_discriminant_non_enum)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `enum_intrinsics_non_enums` error: lint `clippy::panic_params` has been renamed to `non_fmt_panics` - --> $DIR/rename.rs:75:9 + --> $DIR/rename.rs:77:9 | LL | #![warn(clippy::panic_params)] | ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `non_fmt_panics` error: lint `clippy::positional_named_format_parameters` has been renamed to `named_arguments_used_positionally` - --> $DIR/rename.rs:76:9 + --> $DIR/rename.rs:78:9 | LL | #![warn(clippy::positional_named_format_parameters)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `named_arguments_used_positionally` error: lint `clippy::temporary_cstring_as_ptr` has been renamed to `temporary_cstring_as_ptr` - --> $DIR/rename.rs:77:9 + --> $DIR/rename.rs:79:9 | LL | #![warn(clippy::temporary_cstring_as_ptr)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `temporary_cstring_as_ptr` error: lint `clippy::unknown_clippy_lints` has been renamed to `unknown_lints` - --> $DIR/rename.rs:78:9 + --> $DIR/rename.rs:80:9 | LL | #![warn(clippy::unknown_clippy_lints)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unknown_lints` error: lint `clippy::unused_label` has been renamed to `unused_labels` - --> $DIR/rename.rs:79:9 + --> $DIR/rename.rs:81:9 | LL | #![warn(clippy::unused_label)] | ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unused_labels` -error: aborting due to 40 previous errors +error: aborting due to 41 previous errors From e5010c996e9cb9fa09fefb4e3d31608915869c78 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 10 Dec 2022 18:35:24 +0900 Subject: [PATCH 073/321] uninlined_format_args: Ignore assert! and debug_assert! before 2021 edition --- clippy_lints/src/format_args.rs | 8 +++--- clippy_utils/src/macros.rs | 6 +++++ ...nlined_format_args_panic.edition2018.fixed | 3 +++ ...nlined_format_args_panic.edition2021.fixed | 3 +++ ...lined_format_args_panic.edition2021.stderr | 26 ++++++++++++++++++- tests/ui/uninlined_format_args_panic.rs | 3 +++ 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index f0995a81329d..aa182a3230bb 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -2,7 +2,8 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; use clippy_utils::is_diag_trait_item; use clippy_utils::macros::FormatParamKind::{Implicit, Named, NamedInline, Numbered, Starred}; use clippy_utils::macros::{ - is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam, FormatParamUsage, + is_assert_macro, is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam, + FormatParamUsage, }; use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::source::snippet_opt; @@ -290,8 +291,9 @@ fn check_uninlined_args( if args.format_string.span.from_expansion() { return; } - if call_site.edition() < Edition2021 && is_panic(cx, def_id) { - // panic! before 2021 edition considers a single string argument as non-format + if call_site.edition() < Edition2021 && (is_panic(cx, def_id) || is_assert_macro(cx, def_id)) { + // panic!, assert!, and debug_assert! before 2021 edition considers a single string argument as + // non-format return; } diff --git a/clippy_utils/src/macros.rs b/clippy_utils/src/macros.rs index d13b34a66cca..77c5f1155423 100644 --- a/clippy_utils/src/macros.rs +++ b/clippy_utils/src/macros.rs @@ -208,6 +208,12 @@ pub fn is_panic(cx: &LateContext<'_>, def_id: DefId) -> bool { ) } +/// Is `def_id` of `assert!` or `debug_assert!` +pub fn is_assert_macro(cx: &LateContext<'_>, def_id: DefId) -> bool { + let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return false }; + matches!(name, sym::assert_macro | sym::debug_assert_macro) +} + pub enum PanicExpn<'a> { /// No arguments - `panic!()` Empty, diff --git a/tests/ui/uninlined_format_args_panic.edition2018.fixed b/tests/ui/uninlined_format_args_panic.edition2018.fixed index 96cc0877960e..52b5343c351e 100644 --- a/tests/ui/uninlined_format_args_panic.edition2018.fixed +++ b/tests/ui/uninlined_format_args_panic.edition2018.fixed @@ -26,4 +26,7 @@ fn main() { panic!("p4 {var}"); } } + + assert!(var == 1, "p5 {}", var); + debug_assert!(var == 1, "p6 {}", var); } diff --git a/tests/ui/uninlined_format_args_panic.edition2021.fixed b/tests/ui/uninlined_format_args_panic.edition2021.fixed index faf8ca4d3a79..ee72065e28ab 100644 --- a/tests/ui/uninlined_format_args_panic.edition2021.fixed +++ b/tests/ui/uninlined_format_args_panic.edition2021.fixed @@ -26,4 +26,7 @@ fn main() { panic!("p4 {var}"); } } + + assert!(var == 1, "p5 {var}"); + debug_assert!(var == 1, "p6 {var}"); } diff --git a/tests/ui/uninlined_format_args_panic.edition2021.stderr b/tests/ui/uninlined_format_args_panic.edition2021.stderr index 0f09c45f4132..fc7b125080e7 100644 --- a/tests/ui/uninlined_format_args_panic.edition2021.stderr +++ b/tests/ui/uninlined_format_args_panic.edition2021.stderr @@ -47,5 +47,29 @@ LL - panic!("p3 {var}", var = var); LL + panic!("p3 {var}"); | -error: aborting due to 4 previous errors +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args_panic.rs:30:5 + | +LL | assert!(var == 1, "p5 {}", var); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - assert!(var == 1, "p5 {}", var); +LL + assert!(var == 1, "p5 {var}"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args_panic.rs:31:5 + | +LL | debug_assert!(var == 1, "p6 {}", var); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - debug_assert!(var == 1, "p6 {}", var); +LL + debug_assert!(var == 1, "p6 {var}"); + | + +error: aborting due to 6 previous errors diff --git a/tests/ui/uninlined_format_args_panic.rs b/tests/ui/uninlined_format_args_panic.rs index 6421c5bbed2f..b4a0a0f496e4 100644 --- a/tests/ui/uninlined_format_args_panic.rs +++ b/tests/ui/uninlined_format_args_panic.rs @@ -26,4 +26,7 @@ fn main() { panic!("p4 {var}"); } } + + assert!(var == 1, "p5 {}", var); + debug_assert!(var == 1, "p6 {}", var); } From 55f1698a6caa8ed4f74fa4decb918cbf173d7a44 Mon Sep 17 00:00:00 2001 From: alexey semenyuk Date: Sat, 10 Dec 2022 19:15:08 +0300 Subject: [PATCH 074/321] Fix badge --- book/src/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/src/README.md b/book/src/README.md index 6248d588a890..23867df8efe1 100644 --- a/book/src/README.md +++ b/book/src/README.md @@ -1,6 +1,6 @@ # Clippy -[![Clippy Test](https://github.com/rust-lang/rust-clippy/workflows/Clippy%20Test/badge.svg?branch=auto&event=push)](https://github.com/rust-lang/rust-clippy/actions?query=workflow%3A%22Clippy+Test%22+event%3Apush+branch%3Aauto) +[![Clippy Test](https://github.com/rust-lang/rust-clippy/workflows/Clippy%20Test%20(bors)/badge.svg?branch=auto&event=push)](https://github.com/rust-lang/rust-clippy/actions?query=workflow%3A%22Clippy+Test+(bors)%22+event%3Apush+branch%3Aauto) [![License: MIT OR Apache-2.0](https://img.shields.io/crates/l/clippy.svg)](https://github.com/rust-lang/rust-clippy#license) A collection of lints to catch common mistakes and improve your From a94793d8d17e4cfe2e727c30c36e174b8d6b6ee3 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Thu, 8 Dec 2022 18:22:33 +0530 Subject: [PATCH 075/321] Implement blocking output This allows decoupling `Command::spawn` and `Command::output`. This is useful for targets which do support launching programs in blocking mode but do not support multitasking (Eg: UEFI). This was originally conceived when working on https://github.com/rust-lang/rust/pull/100316 Signed-off-by: Ayush Singh --- library/std/src/process.rs | 6 ++-- .../src/sys/unix/process/process_fuchsia.rs | 5 +++ .../std/src/sys/unix/process/process_unix.rs | 5 +++ .../sys/unix/process/process_unsupported.rs | 4 +++ .../src/sys/unix/process/process_vxworks.rs | 5 +++ library/std/src/sys/unsupported/pipe.rs | 2 +- library/std/src/sys/unsupported/process.rs | 4 +++ library/std/src/sys/windows/process.rs | 5 +++ library/std/src/sys_common/process.rs | 31 ++++++++++++++++++- 9 files changed, 61 insertions(+), 6 deletions(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 1ff7bba85d23..17aff342c159 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -911,10 +911,8 @@ impl Command { /// ``` #[stable(feature = "process", since = "1.0.0")] pub fn output(&mut self) -> io::Result { - self.inner - .spawn(imp::Stdio::MakePipe, false) - .map(Child::from_inner) - .and_then(|p| p.wait_with_output()) + let (status, stdout, stderr) = self.inner.output()?; + Ok(Output { status: ExitStatus(status), stdout, stderr }) } /// Executes a command as a child process, waiting for it to finish and diff --git a/library/std/src/sys/unix/process/process_fuchsia.rs b/library/std/src/sys/unix/process/process_fuchsia.rs index 66ea3db2015a..4c99d758c93a 100644 --- a/library/std/src/sys/unix/process/process_fuchsia.rs +++ b/library/std/src/sys/unix/process/process_fuchsia.rs @@ -35,6 +35,11 @@ impl Command { Ok((Process { handle: Handle::new(process_handle) }, ours)) } + pub fn output(&mut self) -> io::Result<(ExitStatus, Vec, Vec)> { + let (proc, pipes) = self.spawn(Stdio::MakePipe, false)?; + crate::sys_common::process::wait_with_output(proc, pipes) + } + pub fn exec(&mut self, default: Stdio) -> io::Error { if self.saw_nul() { return io::const_io_error!( diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs index 56a805cef731..45616850a371 100644 --- a/library/std/src/sys/unix/process/process_unix.rs +++ b/library/std/src/sys/unix/process/process_unix.rs @@ -132,6 +132,11 @@ impl Command { } } + pub fn output(&mut self) -> io::Result<(ExitStatus, Vec, Vec)> { + let (proc, pipes) = self.spawn(Stdio::MakePipe, false)?; + crate::sys_common::process::wait_with_output(proc, pipes) + } + // Attempts to fork the process. If successful, returns Ok((0, -1)) // in the child, and Ok((child_pid, -1)) in the parent. #[cfg(not(target_os = "linux"))] diff --git a/library/std/src/sys/unix/process/process_unsupported.rs b/library/std/src/sys/unix/process/process_unsupported.rs index 72f9f3f9ca74..f28ca58d0203 100644 --- a/library/std/src/sys/unix/process/process_unsupported.rs +++ b/library/std/src/sys/unix/process/process_unsupported.rs @@ -20,6 +20,10 @@ impl Command { unsupported() } + pub fn output(&mut self) -> io::Result<(ExitStatus, Vec, Vec)> { + unsupported() + } + pub fn exec(&mut self, _default: Stdio) -> io::Error { unsupported_err() } diff --git a/library/std/src/sys/unix/process/process_vxworks.rs b/library/std/src/sys/unix/process/process_vxworks.rs index 200ef6719679..f549d37c3011 100644 --- a/library/std/src/sys/unix/process/process_vxworks.rs +++ b/library/std/src/sys/unix/process/process_vxworks.rs @@ -108,6 +108,11 @@ impl Command { } } + pub fn output(&mut self) -> io::Result<(ExitStatus, Vec, Vec)> { + let (proc, pipes) = self.spawn(Stdio::MakePipe, false)?; + crate::sys_common::process::wait_with_output(proc, pipes) + } + pub fn exec(&mut self, default: Stdio) -> io::Error { let ret = Command::spawn(self, default, false); match ret { diff --git a/library/std/src/sys/unsupported/pipe.rs b/library/std/src/sys/unsupported/pipe.rs index 75ce75467963..0bba673b458c 100644 --- a/library/std/src/sys/unsupported/pipe.rs +++ b/library/std/src/sys/unsupported/pipe.rs @@ -15,7 +15,7 @@ impl AnonPipe { self.0 } - pub fn read_to_end(&self, buf: &mut Vec) -> io::Result { + pub fn read_to_end(&self, _buf: &mut Vec) -> io::Result { self.0 } diff --git a/library/std/src/sys/unsupported/process.rs b/library/std/src/sys/unsupported/process.rs index 633f17c054bc..a494f2d6b4c1 100644 --- a/library/std/src/sys/unsupported/process.rs +++ b/library/std/src/sys/unsupported/process.rs @@ -75,6 +75,10 @@ impl Command { ) -> io::Result<(Process, StdioPipes)> { unsupported() } + + pub fn output(&mut self) -> io::Result<(ExitStatus, Vec, Vec)> { + unsupported() + } } impl From for Stdio { diff --git a/library/std/src/sys/windows/process.rs b/library/std/src/sys/windows/process.rs index 31e9b34fb9ef..10bc949e1f45 100644 --- a/library/std/src/sys/windows/process.rs +++ b/library/std/src/sys/windows/process.rs @@ -351,6 +351,11 @@ impl Command { )) } } + + pub fn output(&mut self) -> io::Result<(ExitStatus, Vec, Vec)> { + let (proc, pipes) = self.spawn(Stdio::MakePipe, false)?; + crate::sys_common::process::wait_with_output(proc, pipes) + } } impl fmt::Debug for Command { diff --git a/library/std/src/sys_common/process.rs b/library/std/src/sys_common/process.rs index 9f978789a623..ae11412067b5 100644 --- a/library/std/src/sys_common/process.rs +++ b/library/std/src/sys_common/process.rs @@ -4,7 +4,9 @@ use crate::collections::BTreeMap; use crate::env; use crate::ffi::{OsStr, OsString}; -use crate::sys::process::EnvKey; +use crate::io; +use crate::sys::pipe::read2; +use crate::sys::process::{EnvKey, ExitStatus, Process, StdioPipes}; // Stores a set of changes to an environment #[derive(Clone, Debug)] @@ -117,3 +119,30 @@ impl<'a> ExactSizeIterator for CommandEnvs<'a> { self.iter.is_empty() } } + +pub fn wait_with_output( + mut process: Process, + mut pipes: StdioPipes, +) -> io::Result<(ExitStatus, Vec, Vec)> { + drop(pipes.stdin.take()); + + let (mut stdout, mut stderr) = (Vec::new(), Vec::new()); + match (pipes.stdout.take(), pipes.stderr.take()) { + (None, None) => {} + (Some(out), None) => { + let res = out.read_to_end(&mut stdout); + res.unwrap(); + } + (None, Some(err)) => { + let res = err.read_to_end(&mut stderr); + res.unwrap(); + } + (Some(out), Some(err)) => { + let res = read2(out, &mut stdout, err, &mut stderr); + res.unwrap(); + } + } + + let status = process.wait()?; + Ok((status, stdout, stderr)) +} From 295f5f514bf423e79ad67ed99c6d90944b299c10 Mon Sep 17 00:00:00 2001 From: Tim Neumann Date: Sun, 11 Dec 2022 19:05:37 +0000 Subject: [PATCH 076/321] KCFI test: Also support LLVM 16 output --- ...izer-kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/codegen/sanitizer-kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi.rs b/src/test/codegen/sanitizer-kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi.rs index 0afd9727517e..8e0d02550ee9 100644 --- a/src/test/codegen/sanitizer-kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi.rs +++ b/src/test/codegen/sanitizer-kcfi-emit-kcfi-operand-bundle-itanium-cxx-abi.rs @@ -20,24 +20,21 @@ impl Copy for i32 {} pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 { // CHECK-LABEL: define{{.*}}foo - // FIXME(rcvalle): Change to !kcfi_type when Rust is updated to LLVM 16 - // CHECK-SAME: {{.*}}! ![[TYPE1:[0-9]+]] + // CHECK-SAME: {{.*}}!{{|kcfi_type}} ![[TYPE1:[0-9]+]] // CHECK: call i32 %f(i32 %arg){{.*}}[ "kcfi"(i32 -1666898348) ] f(arg) } pub fn bar(f: fn(i32, i32) -> i32, arg1: i32, arg2: i32) -> i32 { // CHECK-LABEL: define{{.*}}bar - // FIXME(rcvalle): Change to !kcfi_type when Rust is updated to LLVM 16 - // CHECK-SAME: {{.*}}! ![[TYPE2:[0-9]+]] + // CHECK-SAME: {{.*}}!{{|kcfi_type}} ![[TYPE2:[0-9]+]] // CHECK: call i32 %f(i32 %arg1, i32 %arg2){{.*}}[ "kcfi"(i32 -1789026986) ] f(arg1, arg2) } pub fn baz(f: fn(i32, i32, i32) -> i32, arg1: i32, arg2: i32, arg3: i32) -> i32 { // CHECK-LABEL: define{{.*}}baz - // FIXME(rcvalle): Change to !kcfi_type when Rust is updated to LLVM 16 - // CHECK-SAME: {{.*}}! ![[TYPE3:[0-9]+]] + // CHECK-SAME: {{.*}}!{{|kcfi_type}} ![[TYPE3:[0-9]+]] // CHECK: call i32 %f(i32 %arg1, i32 %arg2, i32 %arg3){{.*}}[ "kcfi"(i32 1248878270) ] f(arg1, arg2, arg3) } From 2bb6bd6546a4ad3bafaefd1ffb48f265a4f37bcf Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Fri, 2 Dec 2022 00:20:39 +0530 Subject: [PATCH 077/321] Add batch flag to remote-test-server When using this flag, the stdout and stderr is sent in a single batch instead of being streamed. It also used `Command::output` instead of `Command::spawn`. This is useful for targets that might support std but not threading (Eg: UEFI). Signed-off-by: Ayush Singh --- src/tools/remote-test-server/src/main.rs | 49 +++++++++++++++++------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/tools/remote-test-server/src/main.rs b/src/tools/remote-test-server/src/main.rs index ec992da68121..b81b85361915 100644 --- a/src/tools/remote-test-server/src/main.rs +++ b/src/tools/remote-test-server/src/main.rs @@ -44,6 +44,7 @@ static TEST: AtomicUsize = AtomicUsize::new(0); struct Config { verbose: bool, sequential: bool, + batch: bool, bind: SocketAddr, } @@ -52,6 +53,7 @@ impl Config { Config { verbose: false, sequential: false, + batch: false, bind: if cfg!(target_os = "android") || cfg!(windows) { ([0, 0, 0, 0], 12345).into() } else { @@ -73,6 +75,7 @@ impl Config { } "--bind" => next_is_bind = true, "--sequential" => config.sequential = true, + "--batch" => config.batch = true, "--verbose" | "-v" => config.verbose = true, "--help" | "-h" => { show_help(); @@ -98,6 +101,7 @@ fn show_help() { OPTIONS: --bind : Specify IP address and port to listen for requests, e.g. "0.0.0.0:12345" --sequential Run only one test at a time + --batch Send stdout and stderr in batch instead of streaming -v, --verbose Show status messages -h, --help Show this help screen "#, @@ -268,22 +272,30 @@ fn handle_run(socket: TcpStream, work: &Path, tmp: &Path, lock: &Mutex<()>, conf // Some tests assume RUST_TEST_TMPDIR exists cmd.env("RUST_TEST_TMPDIR", tmp.to_owned()); - // Spawn the child and ferry over stdout/stderr to the socket in a framed - // fashion (poor man's style) - let mut child = - t!(cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()); - drop(lock); - let mut stdout = child.stdout.take().unwrap(); - let mut stderr = child.stderr.take().unwrap(); let socket = Arc::new(Mutex::new(reader.into_inner())); - let socket2 = socket.clone(); - let thread = thread::spawn(move || my_copy(&mut stdout, 0, &*socket2)); - my_copy(&mut stderr, 1, &*socket); - thread.join().unwrap(); + + let status = if config.batch { + let child = + t!(cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).output()); + batch_copy(&child.stdout, 0, &*socket); + batch_copy(&child.stderr, 1, &*socket); + child.status + } else { + // Spawn the child and ferry over stdout/stderr to the socket in a framed + // fashion (poor man's style) + let mut child = + t!(cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()); + drop(lock); + let mut stdout = child.stdout.take().unwrap(); + let mut stderr = child.stderr.take().unwrap(); + let socket2 = socket.clone(); + let thread = thread::spawn(move || my_copy(&mut stdout, 0, &*socket2)); + my_copy(&mut stderr, 1, &*socket); + thread.join().unwrap(); + t!(child.wait()) + }; // Finally send over the exit status. - let status = t!(child.wait()); - let (which, code) = get_status_code(&status); t!(socket.lock().unwrap().write_all(&[ @@ -356,6 +368,17 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex) { } } +fn batch_copy(buf: &[u8], which: u8, dst: &Mutex) { + let n = buf.len(); + let mut dst = dst.lock().unwrap(); + t!(dst.write_all(&[which, (n >> 24) as u8, (n >> 16) as u8, (n >> 8) as u8, (n >> 0) as u8,])); + if n > 0 { + t!(dst.write_all(buf)); + // Marking buf finished + t!(dst.write_all(&[which, 0, 0, 0, 0,])); + } +} + fn read_u32(r: &mut dyn Read) -> u32 { let mut len = [0; 4]; t!(r.read_exact(&mut len)); From 46f6e39ac61241729b81671ed8fb917b8bb2e9e5 Mon Sep 17 00:00:00 2001 From: raffimolero <49224759+raffimolero@users.noreply.github.com> Date: Mon, 12 Dec 2022 12:28:40 +0800 Subject: [PATCH 078/321] add assert messages if chunks/windows are length 0 --- library/core/src/slice/mod.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index d9281a9252c0..9e89c9f832fa 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -893,7 +893,7 @@ impl [T] { #[stable(feature = "chunks_exact", since = "1.31.0")] #[inline] pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> { - assert_ne!(chunk_size, 0); + assert_ne!(chunk_size, 0, "chunks cannot have a size of zero"); ChunksExact::new(self, chunk_size) } @@ -935,7 +935,7 @@ impl [T] { #[stable(feature = "chunks_exact", since = "1.31.0")] #[inline] pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> { - assert_ne!(chunk_size, 0); + assert_ne!(chunk_size, 0, "chunks cannot have a size of zero"); ChunksExactMut::new(self, chunk_size) } @@ -1006,7 +1006,7 @@ impl [T] { #[inline] #[must_use] pub fn as_chunks(&self) -> (&[[T; N]], &[T]) { - assert_ne!(N, 0); + assert_ne!(N, 0, "chunks cannot have a size of zero"); let len = self.len() / N; let (multiple_of_n, remainder) = self.split_at(len * N); // SAFETY: We already panicked for zero, and ensured by construction @@ -1037,7 +1037,7 @@ impl [T] { #[inline] #[must_use] pub fn as_rchunks(&self) -> (&[T], &[[T; N]]) { - assert_ne!(N, 0); + assert_ne!(N, 0, "chunks cannot have a size of zero"); let len = self.len() / N; let (remainder, multiple_of_n) = self.split_at(self.len() - len * N); // SAFETY: We already panicked for zero, and ensured by construction @@ -1076,7 +1076,7 @@ impl [T] { #[unstable(feature = "array_chunks", issue = "74985")] #[inline] pub fn array_chunks(&self) -> ArrayChunks<'_, T, N> { - assert_ne!(N, 0); + assert_ne!(N, 0, "chunks cannot have a size of zero"); ArrayChunks::new(self) } @@ -1155,7 +1155,7 @@ impl [T] { #[inline] #[must_use] pub fn as_chunks_mut(&mut self) -> (&mut [[T; N]], &mut [T]) { - assert_ne!(N, 0); + assert_ne!(N, 0, "chunks cannot have a size of zero"); let len = self.len() / N; let (multiple_of_n, remainder) = self.split_at_mut(len * N); // SAFETY: We already panicked for zero, and ensured by construction @@ -1192,7 +1192,7 @@ impl [T] { #[inline] #[must_use] pub fn as_rchunks_mut(&mut self) -> (&mut [T], &mut [[T; N]]) { - assert_ne!(N, 0); + assert_ne!(N, 0, "chunks cannot have a size of zero"); let len = self.len() / N; let (remainder, multiple_of_n) = self.split_at_mut(self.len() - len * N); // SAFETY: We already panicked for zero, and ensured by construction @@ -1233,7 +1233,7 @@ impl [T] { #[unstable(feature = "array_chunks", issue = "74985")] #[inline] pub fn array_chunks_mut(&mut self) -> ArrayChunksMut<'_, T, N> { - assert_ne!(N, 0); + assert_ne!(N, 0, "chunks cannot have a size of zero"); ArrayChunksMut::new(self) } @@ -1265,7 +1265,7 @@ impl [T] { #[unstable(feature = "array_windows", issue = "75027")] #[inline] pub fn array_windows(&self) -> ArrayWindows<'_, T, N> { - assert_ne!(N, 0); + assert_ne!(N, 0, "windows cannot have a size of zero"); ArrayWindows::new(self) } From 158894464c5e4b143c7b307f932d9eb51afc24ee Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 12 Dec 2022 11:10:19 +0100 Subject: [PATCH 079/321] interpret: add read_machine_[ui]size convenience methods --- .../src/interpret/intrinsics.rs | 8 ++--- .../rustc_const_eval/src/interpret/operand.rs | 11 +++++++ .../src/interpret/projection.rs | 4 +-- src/tools/miri/src/eval.rs | 2 +- src/tools/miri/src/shims/env.rs | 2 +- src/tools/miri/src/shims/foreign_items.rs | 32 +++++++++---------- src/tools/miri/src/shims/intrinsics/mod.rs | 4 +-- src/tools/miri/src/shims/mod.rs | 2 +- .../miri/src/shims/unix/foreign_items.rs | 18 +++++------ src/tools/miri/src/shims/unix/fs.rs | 8 ++--- .../src/shims/unix/linux/foreign_items.rs | 6 ++-- src/tools/miri/src/shims/unix/macos/dlsym.rs | 2 +- .../src/shims/unix/macos/foreign_items.rs | 4 +-- src/tools/miri/src/shims/unix/thread.rs | 4 +-- src/tools/miri/src/shims/windows/dlsym.rs | 4 +-- .../miri/src/shims/windows/foreign_items.rs | 16 +++++----- src/tools/miri/src/shims/windows/sync.rs | 2 +- src/tools/miri/src/shims/windows/thread.rs | 2 +- 18 files changed, 71 insertions(+), 60 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index b9be7fa48000..7031a0b45d6e 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -305,7 +305,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } sym::offset => { let ptr = self.read_pointer(&args[0])?; - let offset_count = self.read_scalar(&args[1])?.to_machine_isize(self)?; + let offset_count = self.read_machine_isize(&args[1])?; let pointee_ty = substs.type_at(0); let offset_ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?; @@ -313,7 +313,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } sym::arith_offset => { let ptr = self.read_pointer(&args[0])?; - let offset_count = self.read_scalar(&args[1])?.to_machine_isize(self)?; + let offset_count = self.read_machine_isize(&args[1])?; let pointee_ty = substs.type_at(0); let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap(); @@ -670,7 +670,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { count: &OpTy<'tcx, >::Provenance>, nonoverlapping: bool, ) -> InterpResult<'tcx> { - let count = self.read_scalar(&count)?.to_machine_usize(self)?; + let count = self.read_machine_usize(&count)?; let layout = self.layout_of(src.layout.ty.builtin_deref(true).unwrap().ty)?; let (size, align) = (layout.size, layout.align.abi); // `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max), @@ -698,7 +698,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let dst = self.read_pointer(&dst)?; let byte = self.read_scalar(&byte)?.to_u8()?; - let count = self.read_scalar(&count)?.to_machine_usize(self)?; + let count = self.read_machine_usize(&count)?; // `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max), // but no actual allocation can be big enough for the difference to be noticeable. diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 221e359d24ab..68a3318ab4e0 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -407,6 +407,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Ok(self.read_immediate(op)?.to_scalar()) } + // Pointer-sized reads are fairly common and need target layout access, so we wrap them in + // convenience functions. + /// Read a pointer from a place. pub fn read_pointer( &self, @@ -414,6 +417,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) -> InterpResult<'tcx, Pointer>> { self.read_scalar(op)?.to_pointer(self) } + /// Read a pointer-sized unsigned integer from a place. + pub fn read_machine_usize(&self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx, u64> { + self.read_scalar(op)?.to_machine_usize(self) + } + /// Read a pointer-sized signed integer from a place. + pub fn read_machine_isize(&self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx, i64> { + self.read_scalar(op)?.to_machine_isize(self) + } /// Turn the wide MPlace into a string (must already be dereferenced!) pub fn read_str(&self, mplace: &MPlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx, &str> { diff --git a/compiler/rustc_const_eval/src/interpret/projection.rs b/compiler/rustc_const_eval/src/interpret/projection.rs index 2ffd73eef3ef..291464ab58ae 100644 --- a/compiler/rustc_const_eval/src/interpret/projection.rs +++ b/compiler/rustc_const_eval/src/interpret/projection.rs @@ -363,7 +363,7 @@ where Index(local) => { let layout = self.layout_of(self.tcx.types.usize)?; let n = self.local_to_op(self.frame(), local, Some(layout))?; - let n = self.read_scalar(&n)?.to_machine_usize(self)?; + let n = self.read_machine_usize(&n)?; self.place_index(base, n)? } ConstantIndex { offset, min_length, from_end } => { @@ -392,7 +392,7 @@ where Index(local) => { let layout = self.layout_of(self.tcx.types.usize)?; let n = self.local_to_op(self.frame(), local, Some(layout))?; - let n = self.read_scalar(&n)?.to_machine_usize(self)?; + let n = self.read_machine_usize(&n)?; self.operand_index(base, n)? } ConstantIndex { offset, min_length, from_end } => { diff --git a/src/tools/miri/src/eval.rs b/src/tools/miri/src/eval.rs index 7b4973f3b9da..30288e5a999b 100644 --- a/src/tools/miri/src/eval.rs +++ b/src/tools/miri/src/eval.rs @@ -233,7 +233,7 @@ impl MainThreadState { this.machine.main_fn_ret_place.unwrap().ptr, this.machine.layouts.isize, ); - let exit_code = this.read_scalar(&ret_place.into())?.to_machine_isize(this)?; + let exit_code = this.read_machine_isize(&ret_place.into())?; // Need to call this ourselves since we are not going to return to the scheduler // loop, and we want the main thread TLS to not show up as memory leaks. this.terminate_active_thread()?; diff --git a/src/tools/miri/src/shims/env.rs b/src/tools/miri/src/shims/env.rs index 80fb4ff2fe98..218aa89b3f9b 100644 --- a/src/tools/miri/src/shims/env.rs +++ b/src/tools/miri/src/shims/env.rs @@ -321,7 +321,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { this.assert_target_os_is_unix("getcwd"); let buf = this.read_pointer(buf_op)?; - let size = this.read_scalar(size_op)?.to_machine_usize(&*this.tcx)?; + let size = this.read_machine_usize(size_op)?; if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { this.reject_in_isolation("`getcwd`", reject_with)?; diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs index 8370e02b588a..b7ed63e17c5b 100644 --- a/src/tools/miri/src/shims/foreign_items.rs +++ b/src/tools/miri/src/shims/foreign_items.rs @@ -485,14 +485,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { // Standard C allocation "malloc" => { let [size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; - let size = this.read_scalar(size)?.to_machine_usize(this)?; + let size = this.read_machine_usize(size)?; let res = this.malloc(size, /*zero_init:*/ false, MiriMemoryKind::C)?; this.write_pointer(res, dest)?; } "calloc" => { let [items, len] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; - let items = this.read_scalar(items)?.to_machine_usize(this)?; - let len = this.read_scalar(len)?.to_machine_usize(this)?; + let items = this.read_machine_usize(items)?; + let len = this.read_machine_usize(len)?; let size = items.checked_mul(len).ok_or_else(|| err_ub_format!("overflow during calloc size computation"))?; let res = this.malloc(size, /*zero_init:*/ true, MiriMemoryKind::C)?; @@ -506,7 +506,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "realloc" => { let [old_ptr, new_size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let old_ptr = this.read_pointer(old_ptr)?; - let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?; + let new_size = this.read_machine_usize(new_size)?; let res = this.realloc(old_ptr, new_size, MiriMemoryKind::C)?; this.write_pointer(res, dest)?; } @@ -514,8 +514,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { // Rust allocation "__rust_alloc" | "miri_alloc" => { let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?; - let size = this.read_scalar(size)?.to_machine_usize(this)?; - let align = this.read_scalar(align)?.to_machine_usize(this)?; + let size = this.read_machine_usize(size)?; + let align = this.read_machine_usize(align)?; let default = |this: &mut MiriInterpCx<'mir, 'tcx>| { Self::check_alloc_request(size, align)?; @@ -546,8 +546,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { } "__rust_alloc_zeroed" => { let [size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?; - let size = this.read_scalar(size)?.to_machine_usize(this)?; - let align = this.read_scalar(align)?.to_machine_usize(this)?; + let size = this.read_machine_usize(size)?; + let align = this.read_machine_usize(align)?; return this.emulate_allocator(Symbol::intern("__rg_alloc_zeroed"), |this| { Self::check_alloc_request(size, align)?; @@ -566,8 +566,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "__rust_dealloc" | "miri_dealloc" => { let [ptr, old_size, align] = this.check_shim(abi, Abi::Rust, link_name, args)?; let ptr = this.read_pointer(ptr)?; - let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?; - let align = this.read_scalar(align)?.to_machine_usize(this)?; + let old_size = this.read_machine_usize(old_size)?; + let align = this.read_machine_usize(align)?; let default = |this: &mut MiriInterpCx<'mir, 'tcx>| { let memory_kind = match link_name.as_str() { @@ -596,9 +596,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "__rust_realloc" => { let [ptr, old_size, align, new_size] = this.check_shim(abi, Abi::Rust, link_name, args)?; let ptr = this.read_pointer(ptr)?; - let old_size = this.read_scalar(old_size)?.to_machine_usize(this)?; - let align = this.read_scalar(align)?.to_machine_usize(this)?; - let new_size = this.read_scalar(new_size)?.to_machine_usize(this)?; + let old_size = this.read_machine_usize(old_size)?; + let align = this.read_machine_usize(align)?; + let new_size = this.read_machine_usize(new_size)?; // No need to check old_size; we anyway check that they match the allocation. return this.emulate_allocator(Symbol::intern("__rg_realloc"), |this| { @@ -621,7 +621,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [left, right, n] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let left = this.read_pointer(left)?; let right = this.read_pointer(right)?; - let n = Size::from_bytes(this.read_scalar(n)?.to_machine_usize(this)?); + let n = Size::from_bytes(this.read_machine_usize(n)?); let result = { let left_bytes = this.read_bytes_ptr_strip_provenance(left, n)?; @@ -641,7 +641,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let ptr = this.read_pointer(ptr)?; let val = this.read_scalar(val)?.to_i32()?; - let num = this.read_scalar(num)?.to_machine_usize(this)?; + let num = this.read_machine_usize(num)?; // The docs say val is "interpreted as unsigned char". #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] let val = val as u8; @@ -664,7 +664,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let ptr = this.read_pointer(ptr)?; let val = this.read_scalar(val)?.to_i32()?; - let num = this.read_scalar(num)?.to_machine_usize(this)?; + let num = this.read_machine_usize(num)?; // The docs say val is "interpreted as unsigned char". #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] let val = val as u8; diff --git a/src/tools/miri/src/shims/intrinsics/mod.rs b/src/tools/miri/src/shims/intrinsics/mod.rs index 5ea82adb9c69..1b97a9d20de0 100644 --- a/src/tools/miri/src/shims/intrinsics/mod.rs +++ b/src/tools/miri/src/shims/intrinsics/mod.rs @@ -111,7 +111,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let ty_layout = this.layout_of(ty)?; let val_byte = this.read_scalar(val_byte)?.to_u8()?; let ptr = this.read_pointer(ptr)?; - let count = this.read_scalar(count)?.to_machine_usize(this)?; + let count = this.read_machine_usize(count)?; // `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max), // but no actual allocation can be big enough for the difference to be noticeable. let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| { @@ -124,7 +124,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [ptr, mask] = check_arg_count(args)?; let ptr = this.read_pointer(ptr)?; - let mask = this.read_scalar(mask)?.to_machine_usize(this)?; + let mask = this.read_machine_usize(mask)?; let masked_addr = Size::from_bytes(ptr.addr().bytes() & mask); diff --git a/src/tools/miri/src/shims/mod.rs b/src/tools/miri/src/shims/mod.rs index b6efad6b5ee0..39db97b72e2c 100644 --- a/src/tools/miri/src/shims/mod.rs +++ b/src/tools/miri/src/shims/mod.rs @@ -80,7 +80,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { return Ok(false); } - let req_align = this.read_scalar(align_op)?.to_machine_usize(this)?; + let req_align = this.read_machine_usize(align_op)?; // Stop if the alignment is not a power of two. if !req_align.is_power_of_two() { diff --git a/src/tools/miri/src/shims/unix/foreign_items.rs b/src/tools/miri/src/shims/unix/foreign_items.rs index d746f9df90ac..63cc132f3fc9 100644 --- a/src/tools/miri/src/shims/unix/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/foreign_items.rs @@ -78,7 +78,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [fd, buf, count] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let fd = this.read_scalar(fd)?.to_i32()?; let buf = this.read_pointer(buf)?; - let count = this.read_scalar(count)?.to_machine_usize(this)?; + let count = this.read_machine_usize(count)?; let result = this.read(fd, buf, count)?; this.write_scalar(Scalar::from_machine_isize(result, this), dest)?; } @@ -86,7 +86,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [fd, buf, n] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let fd = this.read_scalar(fd)?.to_i32()?; let buf = this.read_pointer(buf)?; - let count = this.read_scalar(n)?.to_machine_usize(this)?; + let count = this.read_machine_usize(n)?; trace!("Called write({:?}, {:?}, {:?})", fd, buf, count); let result = this.write(fd, buf, count)?; // Now, `result` is the value we return back to the program. @@ -157,8 +157,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [fd, offset, len, advice] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; this.read_scalar(fd)?.to_i32()?; - this.read_scalar(offset)?.to_machine_isize(this)?; - this.read_scalar(len)?.to_machine_isize(this)?; + this.read_machine_isize(offset)?; + this.read_machine_isize(len)?; this.read_scalar(advice)?.to_i32()?; // fadvise is only informational, we can ignore it. this.write_null(dest)?; @@ -191,8 +191,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "posix_memalign" => { let [ret, align, size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let ret = this.deref_operand(ret)?; - let align = this.read_scalar(align)?.to_machine_usize(this)?; - let size = this.read_scalar(size)?.to_machine_usize(this)?; + let align = this.read_machine_usize(align)?; + let size = this.read_machine_usize(size)?; // Align must be power of 2, and also at least ptr-sized (POSIX rules). // But failure to adhere to this is not UB, it's an error condition. if !align.is_power_of_two() || align < this.pointer_size().bytes() { @@ -216,7 +216,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { // Dynamic symbol loading "dlsym" => { let [handle, symbol] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; - this.read_scalar(handle)?.to_machine_usize(this)?; + this.read_machine_usize(handle)?; let symbol = this.read_pointer(symbol)?; let symbol_name = this.read_c_str(symbol)?; if let Some(dlsym) = Dlsym::from_str(symbol_name, &this.tcx.sess.target.os)? { @@ -472,7 +472,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [errnum, buf, buflen] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; let errnum = this.read_scalar(errnum)?; let buf = this.read_pointer(buf)?; - let buflen = this.read_scalar(buflen)?.to_machine_usize(this)?; + let buflen = this.read_machine_usize(buflen)?; let error = this.try_errnum_to_io_error(errnum)?; let formatted = match error { @@ -565,7 +565,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let uid = this.read_scalar(uid)?.to_u32()?; let pwd = this.deref_operand(pwd)?; let buf = this.read_pointer(buf)?; - let buflen = this.read_scalar(buflen)?.to_machine_usize(this)?; + let buflen = this.read_machine_usize(buflen)?; let result = this.deref_operand(result)?; // Must be for "us". diff --git a/src/tools/miri/src/shims/unix/fs.rs b/src/tools/miri/src/shims/unix/fs.rs index 988627db5611..5af1b354e7bb 100644 --- a/src/tools/miri/src/shims/unix/fs.rs +++ b/src/tools/miri/src/shims/unix/fs.rs @@ -1293,7 +1293,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { this.assert_target_os("linux", "readdir64"); - let dirp = this.read_scalar(dirp_op)?.to_machine_usize(this)?; + let dirp = this.read_machine_usize(dirp_op)?; // Reject if isolation is enabled. if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { @@ -1385,7 +1385,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { this.assert_target_os("macos", "readdir_r"); - let dirp = this.read_scalar(dirp_op)?.to_machine_usize(this)?; + let dirp = this.read_machine_usize(dirp_op)?; // Reject if isolation is enabled. if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { @@ -1478,7 +1478,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { fn closedir(&mut self, dirp_op: &OpTy<'tcx, Provenance>) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - let dirp = this.read_scalar(dirp_op)?.to_machine_usize(this)?; + let dirp = this.read_machine_usize(dirp_op)?; // Reject if isolation is enabled. if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { @@ -1642,7 +1642,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let pathname = this.read_path_from_c_str(this.read_pointer(pathname_op)?)?; let buf = this.read_pointer(buf_op)?; - let bufsize = this.read_scalar(bufsize_op)?.to_machine_usize(this)?; + let bufsize = this.read_machine_usize(bufsize_op)?; // Reject if isolation is enabled. if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { diff --git a/src/tools/miri/src/shims/unix/linux/foreign_items.rs b/src/tools/miri/src/shims/unix/linux/foreign_items.rs index 34076e842d55..acf47fe92480 100644 --- a/src/tools/miri/src/shims/unix/linux/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/linux/foreign_items.rs @@ -99,7 +99,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "incorrect number of arguments for syscall: got 0, expected at least 1" ); } - match this.read_scalar(&args[0])?.to_machine_usize(this)? { + match this.read_machine_usize(&args[0])? { // `libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)` // is called if a `HashMap` is created the regular way (e.g. HashMap). id if id == sys_getrandom => { @@ -147,7 +147,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let [pid, cpusetsize, mask] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; this.read_scalar(pid)?.to_i32()?; - this.read_scalar(cpusetsize)?.to_machine_usize(this)?; + this.read_machine_usize(cpusetsize)?; this.deref_operand(mask)?; // FIXME: we just return an error; `num_cpus` then falls back to `sysconf`. let einval = this.eval_libc("EINVAL")?; @@ -179,7 +179,7 @@ fn getrandom<'tcx>( dest: &PlaceTy<'tcx, Provenance>, ) -> InterpResult<'tcx> { let ptr = this.read_pointer(ptr)?; - let len = this.read_scalar(len)?.to_machine_usize(this)?; + let len = this.read_machine_usize(len)?; // The only supported flags are GRND_RANDOM and GRND_NONBLOCK, // neither of which have any effect on our current PRNG. diff --git a/src/tools/miri/src/shims/unix/macos/dlsym.rs b/src/tools/miri/src/shims/unix/macos/dlsym.rs index 18804b45efca..44b9af79005a 100644 --- a/src/tools/miri/src/shims/unix/macos/dlsym.rs +++ b/src/tools/miri/src/shims/unix/macos/dlsym.rs @@ -39,7 +39,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { Dlsym::getentropy => { let [ptr, len] = check_arg_count(args)?; let ptr = this.read_pointer(ptr)?; - let len = this.read_scalar(len)?.to_machine_usize(this)?; + let len = this.read_machine_usize(len)?; this.gen_random(ptr, len)?; this.write_null(dest)?; } diff --git a/src/tools/miri/src/shims/unix/macos/foreign_items.rs b/src/tools/miri/src/shims/unix/macos/foreign_items.rs index 221dc39697f9..2554fc779845 100644 --- a/src/tools/miri/src/shims/unix/macos/foreign_items.rs +++ b/src/tools/miri/src/shims/unix/macos/foreign_items.rs @@ -161,13 +161,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { // Querying system information "pthread_get_stackaddr_np" => { let [thread] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; - this.read_scalar(thread)?.to_machine_usize(this)?; + this.read_machine_usize(thread)?; let stack_addr = Scalar::from_uint(STACK_ADDR, this.pointer_size()); this.write_scalar(stack_addr, dest)?; } "pthread_get_stacksize_np" => { let [thread] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; - this.read_scalar(thread)?.to_machine_usize(this)?; + this.read_machine_usize(thread)?; let stack_size = Scalar::from_uint(STACK_SIZE, this.pointer_size()); this.write_scalar(stack_size, dest)?; } diff --git a/src/tools/miri/src/shims/unix/thread.rs b/src/tools/miri/src/shims/unix/thread.rs index 5b9dc90f0f00..2cb4858fdfde 100644 --- a/src/tools/miri/src/shims/unix/thread.rs +++ b/src/tools/miri/src/shims/unix/thread.rs @@ -42,7 +42,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { throw_unsup_format!("Miri supports pthread_join only with retval==NULL"); } - let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?; + let thread_id = this.read_machine_usize(thread)?; this.join_thread_exclusive(thread_id.try_into().expect("thread ID should fit in u32"))?; Ok(0) @@ -51,7 +51,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { fn pthread_detach(&mut self, thread: &OpTy<'tcx, Provenance>) -> InterpResult<'tcx, i32> { let this = self.eval_context_mut(); - let thread_id = this.read_scalar(thread)?.to_machine_usize(this)?; + let thread_id = this.read_machine_usize(thread)?; this.detach_thread( thread_id.try_into().expect("thread ID should fit in u32"), /*allow_terminated_joined*/ false, diff --git a/src/tools/miri/src/shims/windows/dlsym.rs b/src/tools/miri/src/shims/windows/dlsym.rs index 4b2a90723c79..857cf1ae7037 100644 --- a/src/tools/miri/src/shims/windows/dlsym.rs +++ b/src/tools/miri/src/shims/windows/dlsym.rs @@ -67,10 +67,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { byte_offset, _key, ] = check_arg_count(args)?; - let handle = this.read_scalar(handle)?.to_machine_isize(this)?; + let handle = this.read_machine_isize(handle)?; let buf = this.read_pointer(buf)?; let n = this.read_scalar(n)?.to_u32()?; - let byte_offset = this.read_scalar(byte_offset)?.to_machine_usize(this)?; // is actually a pointer + let byte_offset = this.read_machine_usize(byte_offset)?; // is actually a pointer let io_status_block = this.deref_operand(io_status_block)?; if byte_offset != 0 { diff --git a/src/tools/miri/src/shims/windows/foreign_items.rs b/src/tools/miri/src/shims/windows/foreign_items.rs index e16749c986b1..656f1a4ae722 100644 --- a/src/tools/miri/src/shims/windows/foreign_items.rs +++ b/src/tools/miri/src/shims/windows/foreign_items.rs @@ -73,9 +73,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "HeapAlloc" => { let [handle, flags, size] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?; - this.read_scalar(handle)?.to_machine_isize(this)?; + this.read_machine_isize(handle)?; let flags = this.read_scalar(flags)?.to_u32()?; - let size = this.read_scalar(size)?.to_machine_usize(this)?; + let size = this.read_machine_usize(size)?; let zero_init = (flags & 0x00000008) != 0; // HEAP_ZERO_MEMORY let res = this.malloc(size, zero_init, MiriMemoryKind::WinHeap)?; this.write_pointer(res, dest)?; @@ -83,7 +83,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "HeapFree" => { let [handle, flags, ptr] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?; - this.read_scalar(handle)?.to_machine_isize(this)?; + this.read_machine_isize(handle)?; this.read_scalar(flags)?.to_u32()?; let ptr = this.read_pointer(ptr)?; this.free(ptr, MiriMemoryKind::WinHeap)?; @@ -92,10 +92,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "HeapReAlloc" => { let [handle, flags, ptr, size] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?; - this.read_scalar(handle)?.to_machine_isize(this)?; + this.read_machine_isize(handle)?; this.read_scalar(flags)?.to_u32()?; let ptr = this.read_pointer(ptr)?; - let size = this.read_scalar(size)?.to_machine_usize(this)?; + let size = this.read_machine_usize(size)?; let res = this.realloc(ptr, size, MiriMemoryKind::WinHeap)?; this.write_pointer(res, dest)?; } @@ -298,7 +298,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { #[allow(non_snake_case)] let [hModule, lpProcName] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?; - this.read_scalar(hModule)?.to_machine_isize(this)?; + this.read_machine_isize(hModule)?; let name = this.read_c_str(this.read_pointer(lpProcName)?)?; if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.os)? { let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym)); @@ -356,7 +356,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { // `term` needs this, so we fake it. let [console, buffer_info] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?; - this.read_scalar(console)?.to_machine_isize(this)?; + this.read_machine_isize(console)?; this.deref_operand(buffer_info)?; // Indicate an error. // FIXME: we should set last_error, but to what? @@ -432,7 +432,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { "GetConsoleMode" if this.frame_in_std() => { let [console, mode] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?; - this.read_scalar(console)?.to_machine_isize(this)?; + this.read_machine_isize(console)?; this.deref_operand(mode)?; // Indicate an error. this.write_null(dest)?; diff --git a/src/tools/miri/src/shims/windows/sync.rs b/src/tools/miri/src/shims/windows/sync.rs index 6b043c6d2c9e..7892f35f7b05 100644 --- a/src/tools/miri/src/shims/windows/sync.rs +++ b/src/tools/miri/src/shims/windows/sync.rs @@ -273,7 +273,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let ptr = this.read_pointer(ptr_op)?; let compare = this.read_pointer(compare_op)?; - let size = this.read_scalar(size_op)?.to_machine_usize(this)?; + let size = this.read_machine_usize(size_op)?; let timeout_ms = this.read_scalar(timeout_op)?.to_u32()?; let thread = this.get_active_thread(); diff --git a/src/tools/miri/src/shims/windows/thread.rs b/src/tools/miri/src/shims/windows/thread.rs index 25a5194caa09..1dbc848b0305 100644 --- a/src/tools/miri/src/shims/windows/thread.rs +++ b/src/tools/miri/src/shims/windows/thread.rs @@ -21,7 +21,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let security = this.read_pointer(security_op)?; // stacksize is ignored, but still needs to be a valid usize - this.read_scalar(stacksize_op)?.to_machine_usize(this)?; + this.read_machine_usize(stacksize_op)?; let start_routine = this.read_pointer(start_op)?; let func_arg = this.read_immediate(arg_op)?; let flags = this.read_scalar(flags_op)?.to_u32()?; From aa1ab5147e319a045d87547738e614913f01cf8e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 12 Dec 2022 16:51:39 +0000 Subject: [PATCH 080/321] Update crossbeam This fixes builds for Gentoo --- Cargo.lock | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d05a09f0389..fb95df0bb6fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -966,9 +966,9 @@ dependencies = [ [[package]] name = "crossbeam-deque" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e" +checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc" dependencies = [ "cfg-if 1.0.0", "crossbeam-epoch", @@ -977,25 +977,24 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.6" +version = "0.9.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97242a70df9b89a65d0b6df3c4bf5b9ce03c5b7309019777fbde37e7537f8762" +checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a" dependencies = [ + "autocfg", "cfg-if 1.0.0", "crossbeam-utils", - "lazy_static", "memoffset", "scopeguard", ] [[package]] name = "crossbeam-utils" -version = "0.8.8" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf124c720b7686e3c2663cf54062ab0f68a88af2fb6a030e87e30bf721fcb38" +checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" dependencies = [ "cfg-if 1.0.0", - "lazy_static", ] [[package]] @@ -2379,9 +2378,9 @@ dependencies = [ [[package]] name = "memoffset" -version = "0.6.5" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" dependencies = [ "autocfg", ] @@ -5401,7 +5400,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 1.0.0", + "cfg-if 0.1.10", "rand 0.8.5", "static_assertions", ] From ba4dd464f5548aa3180dbe1e8144062221d6463d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 12 Dec 2022 17:06:13 -0500 Subject: [PATCH 081/321] fs: Fix #50619 (again) and add a regression test Bug #50619 was fixed by adding an end_of_stream flag in #50630. Unfortunately, that fix only applied to the readdir_r() path. When I switched Linux to use readdir() in #92778, I inadvertently reintroduced the bug on that platform. Other platforms that had always used readdir() were presumably never fixed. This patch enables end_of_stream for all platforms, and adds a Linux-specific regression test that should hopefully prevent the bug from being reintroduced again. --- library/std/src/fs/tests.rs | 26 ++++++++++++++++ library/std/src/sys/unix/fs.rs | 57 ++++++++++++---------------------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs index 4748ac9d97ef..6d42adca5314 100644 --- a/library/std/src/fs/tests.rs +++ b/library/std/src/fs/tests.rs @@ -1567,3 +1567,29 @@ fn test_eq_direntry_metadata() { assert_eq!(ft1, ft2); } } + +/// Regression test for https://github.com/rust-lang/rust/issues/50619. +#[test] +#[cfg(target_os = "linux")] +fn test_read_dir_infinite_loop() { + use crate::process::Command; + use crate::thread::sleep; + use crate::time::Duration; + + // Create a child process + let Ok(child) = Command::new("echo").spawn() else { return }; + + // Wait for it to (probably) become a zombie. We can't use wait() because + // that will reap the process. + sleep(Duration::from_millis(10)); + + // open() on this path will succeed, but readdir() will fail + let id = child.id(); + let path = format!("/proc/{id}/net"); + + // Skip the test if we can't open the directory in the first place + let Ok(dir) = fs::read_dir(path) else { return }; + + // Iterate through the directory + for _ in dir {} +} diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index fb8d06c66820..26a99f913774 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -243,17 +243,15 @@ struct InnerReadDir { pub struct ReadDir { inner: Arc, - #[cfg(not(any( - target_os = "android", - target_os = "linux", - target_os = "solaris", - target_os = "illumos", - target_os = "fuchsia", - target_os = "redox", - )))] end_of_stream: bool, } +impl ReadDir { + fn new(inner: InnerReadDir) -> Self { + Self { inner: Arc::new(inner), end_of_stream: false } + } +} + struct Dir(*mut libc::DIR); unsafe impl Send for Dir {} @@ -594,6 +592,10 @@ impl Iterator for ReadDir { target_os = "illumos" ))] fn next(&mut self) -> Option> { + if self.end_of_stream { + return None; + } + unsafe { loop { // As of POSIX.1-2017, readdir() is not required to be thread safe; only @@ -604,8 +606,12 @@ impl Iterator for ReadDir { super::os::set_errno(0); let entry_ptr = readdir64(self.inner.dirp.0); if entry_ptr.is_null() { - // null can mean either the end is reached or an error occurred. - // So we had to clear errno beforehand to check for an error now. + // We either encountered an error, or reached the end. Either way, + // the next call to next() should return None. + self.end_of_stream = true; + + // To distinguish between errors and end-of-directory, we had to clear + // errno beforehand to check for an error now. return match super::os::errno() { 0 => None, e => Some(Err(Error::from_raw_os_error(e))), @@ -1363,18 +1369,7 @@ pub fn readdir(path: &Path) -> io::Result { } else { let root = path.to_path_buf(); let inner = InnerReadDir { dirp: Dir(ptr), root }; - Ok(ReadDir { - inner: Arc::new(inner), - #[cfg(not(any( - target_os = "android", - target_os = "linux", - target_os = "solaris", - target_os = "illumos", - target_os = "fuchsia", - target_os = "redox", - )))] - end_of_stream: false, - }) + Ok(ReadDir::new(inner)) } } @@ -1755,7 +1750,6 @@ mod remove_dir_impl { use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; use crate::os::unix::prelude::{OwnedFd, RawFd}; use crate::path::{Path, PathBuf}; - use crate::sync::Arc; use crate::sys::common::small_c_string::run_path_with_cstr; use crate::sys::{cvt, cvt_r}; @@ -1827,21 +1821,8 @@ mod remove_dir_impl { // a valid root is not needed because we do not call any functions involving the full path // of the DirEntrys. let dummy_root = PathBuf::new(); - Ok(( - ReadDir { - inner: Arc::new(InnerReadDir { dirp, root: dummy_root }), - #[cfg(not(any( - target_os = "android", - target_os = "linux", - target_os = "solaris", - target_os = "illumos", - target_os = "fuchsia", - target_os = "redox", - )))] - end_of_stream: false, - }, - new_parent_fd, - )) + let inner = InnerReadDir { dirp, root: dummy_root }; + Ok((ReadDir::new(inner), new_parent_fd)) } #[cfg(any( From 5dea1d1c6e03102e5a42c03e50a7ad8107b20411 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 26 Oct 2022 21:50:25 +0000 Subject: [PATCH 082/321] EarlyBinder nits --- compiler/rustc_hir_analysis/src/astconv/mod.rs | 14 +++++--------- compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs | 1 + compiler/rustc_infer/src/infer/outlives/verify.rs | 11 +++++------ compiler/rustc_middle/src/ty/util.rs | 11 +++++++++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 66906b331da2..01ffa28ca1ab 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -26,11 +26,9 @@ use rustc_hir::intravisit::{walk_generics, Visitor as _}; use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin}; use rustc_middle::middle::stability::AllowUnstable; use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, SubstsRef}; -use rustc_middle::ty::DynKind; use rustc_middle::ty::GenericParamDefKind; -use rustc_middle::ty::{ - self, Const, DefIdTree, EarlyBinder, IsSuggestable, Ty, TyCtxt, TypeVisitable, -}; +use rustc_middle::ty::{self, Const, DefIdTree, IsSuggestable, Ty, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{DynKind, EarlyBinder}; use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, BARE_TRAIT_OBJECTS}; use rustc_span::edition::Edition; use rustc_span::lev_distance::find_best_match_for_name; @@ -490,7 +488,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { self.astconv .normalize_ty( self.span, - EarlyBinder(tcx.at(self.span).type_of(param.def_id)) + tcx.at(self.span) + .bound_type_of(param.def_id) .subst(tcx, substs), ) .into() @@ -1258,10 +1257,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { item_segment: &hir::PathSegment<'_>, ) -> Ty<'tcx> { let substs = self.ast_path_substs_for_ty(span, did, item_segment); - self.normalize_ty( - span, - EarlyBinder(self.tcx().at(span).type_of(did)).subst(self.tcx(), substs), - ) + self.normalize_ty(span, self.tcx().at(span).bound_type_of(did).subst(self.tcx(), substs)) } fn conv_object_ty_poly_trait_ref( diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 952d27262591..5ca986f23ff5 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -336,6 +336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { /// Basically whenever we are converting from a type scheme into /// the fn body space, we always want to normalize associated /// types as well. This function combines the two. + // FIXME(compiler-errors): Remove this. fn instantiate_type_scheme(&self, span: Span, substs: SubstsRef<'tcx>, value: T) -> T where T: TypeFoldable<'tcx>, diff --git a/compiler/rustc_infer/src/infer/outlives/verify.rs b/compiler/rustc_infer/src/infer/outlives/verify.rs index f470b2eb8c19..644b34fa1b6d 100644 --- a/compiler/rustc_infer/src/infer/outlives/verify.rs +++ b/compiler/rustc_infer/src/infer/outlives/verify.rs @@ -5,7 +5,7 @@ use crate::infer::{GenericKind, VerifyBound}; use rustc_data_structures::sso::SsoHashSet; use rustc_hir::def_id::DefId; use rustc_middle::ty::GenericArg; -use rustc_middle::ty::{self, EarlyBinder, OutlivesPredicate, SubstsRef, Ty, TyCtxt}; +use rustc_middle::ty::{self, OutlivesPredicate, SubstsRef, Ty, TyCtxt}; use smallvec::smallvec; @@ -304,14 +304,13 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { substs: SubstsRef<'tcx>, ) -> impl Iterator> { let tcx = self.tcx; - let bounds = tcx.item_bounds(def_id); - trace!("{:#?}", bounds); + let bounds = tcx.bound_item_bounds(def_id); + trace!("{:#?}", bounds.0); bounds - .into_iter() + .subst_iter(tcx, substs) .filter_map(|p| p.to_opt_type_outlives()) .filter_map(|p| p.no_bound_vars()) - .map(|b| b.1) - .map(move |r| EarlyBinder(r).subst(tcx, substs)) + .map(|OutlivesPredicate(_, r)| r) } /// Searches through a predicate list for a predicate `T: 'a`. diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 9ea8dc6e69fd..ce2519787ec8 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -4,9 +4,10 @@ use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags; use crate::mir; use crate::ty::layout::IntegerExt; use crate::ty::{ - self, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, - TypeVisitable, + self, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, + TypeSuperFoldable, TypeVisitable, }; +use crate::ty::query::TyCtxtAt; use crate::ty::{GenericArgKind, SubstsRef}; use rustc_apfloat::Float as _; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -770,6 +771,12 @@ impl<'tcx> TyCtxt<'tcx> { } } +impl<'tcx> TyCtxtAt<'tcx> { + pub fn bound_type_of(self, def_id: DefId) -> ty::EarlyBinder> { + ty::EarlyBinder(self.type_of(def_id)) + } +} + struct OpaqueTypeExpander<'tcx> { // Contains the DefIds of the opaque types that are currently being // expanded. When we expand an opaque type we insert the DefId of From f705d646738ea8b1238e1c9a58c16532cc4d73a6 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 26 Oct 2022 23:53:47 +0000 Subject: [PATCH 083/321] Remove instantiate_type_scheme --- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 35 ++++--------------- compiler/rustc_hir_typeck/src/method/mod.rs | 6 ++-- compiler/rustc_middle/src/ty/util.rs | 8 ++--- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 5ca986f23ff5..bbb2a9d64129 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -22,7 +22,7 @@ use rustc_middle::ty::error::TypeError; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::{ - self, AdtKind, CanonicalUserType, DefIdTree, EarlyBinder, GenericParamDefKind, Ty, UserType, + self, AdtKind, CanonicalUserType, DefIdTree, GenericParamDefKind, Ty, UserType, }; use rustc_middle::ty::{GenericArgKind, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts}; use rustc_session::lint; @@ -333,23 +333,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - /// Basically whenever we are converting from a type scheme into - /// the fn body space, we always want to normalize associated - /// types as well. This function combines the two. - // FIXME(compiler-errors): Remove this. - fn instantiate_type_scheme(&self, span: Span, substs: SubstsRef<'tcx>, value: T) -> T - where - T: TypeFoldable<'tcx>, - { - debug!("instantiate_type_scheme(value={:?}, substs={:?})", value, substs); - let value = EarlyBinder(value).subst(self.tcx, substs); - let result = self.normalize(span, value); - debug!("instantiate_type_scheme = {:?}", result); - result - } - - /// As `instantiate_type_scheme`, but for the bounds found in a - /// generic type scheme. + /// Instantiates and normalizes the bounds for a given item pub(in super::super) fn instantiate_bounds( &self, span: Span, @@ -1161,10 +1145,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let def_id = res.def_id(); - // The things we are substituting into the type should not contain - // escaping late-bound regions, and nor should the base type scheme. - let ty = tcx.type_of(def_id); - let arg_count = GenericArgCountResult { explicit_late_bound, correct: if infer_args_for_err.is_empty() { @@ -1287,8 +1267,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }, ) }); - assert!(!substs.has_escaping_bound_vars()); - assert!(!ty.has_escaping_bound_vars()); // First, store the "user substs" for later. self.write_user_type_annotation_from_substs(hir_id, def_id, substs, user_self_ty); @@ -1297,7 +1275,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Substitute the values for the type parameters into the type of // the referenced item. - let ty_substituted = self.instantiate_type_scheme(span, &substs, ty); + let ty = tcx.bound_type_of(def_id); + assert!(!substs.has_escaping_bound_vars()); + assert!(!ty.0.has_escaping_bound_vars()); + let ty_substituted = self.normalize(span, ty.subst(tcx, substs)); if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { // In the case of `Foo::method` and `>::method`, if `method` @@ -1305,9 +1286,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // type parameters, which we can infer by unifying the provided `Self` // with the substituted impl type. // This also occurs for an enum variant on a type alias. - let ty = tcx.type_of(impl_def_id); - - let impl_ty = self.instantiate_type_scheme(span, &substs, ty); + let impl_ty = self.normalize(span, tcx.bound_type_of(impl_def_id).subst(tcx, substs)); match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) { Ok(ok) => self.register_infer_ok_obligations(ok), Err(_) => { diff --git a/compiler/rustc_hir_typeck/src/method/mod.rs b/compiler/rustc_hir_typeck/src/method/mod.rs index b15c086ffad5..274abf8177fd 100644 --- a/compiler/rustc_hir_typeck/src/method/mod.rs +++ b/compiler/rustc_hir_typeck/src/method/mod.rs @@ -456,9 +456,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Instantiate late-bound regions and substitute the trait // parameters into the method type to get the actual method type. // - // N.B., instantiate late-bound regions first so that - // `instantiate_type_scheme` can normalize associated types that - // may reference those regions. + // N.B., instantiate late-bound regions before normalizing the + // function signature so that normalization does not need to deal + // with bound regions. let fn_sig = tcx.bound_fn_sig(def_id); let fn_sig = fn_sig.subst(self.tcx, substs); let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig); diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index ce2519787ec8..54804d6a40d4 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -3,11 +3,11 @@ use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags; use crate::mir; use crate::ty::layout::IntegerExt; -use crate::ty::{ - self, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, - TypeSuperFoldable, TypeVisitable, -}; use crate::ty::query::TyCtxtAt; +use crate::ty::{ + self, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, + TypeVisitable, +}; use crate::ty::{GenericArgKind, SubstsRef}; use rustc_apfloat::Float as _; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; From 25c9718c04e43b5aa18b6345e0c384f9d1236e2c Mon Sep 17 00:00:00 2001 From: naosense Date: Fri, 9 Dec 2022 11:40:50 +0800 Subject: [PATCH 084/321] check ranges with .contains calls --- clippy_lints/src/manual_is_ascii_check.rs | 75 +++++++++++++---------- tests/ui/manual_is_ascii_check.fixed | 1 + tests/ui/manual_is_ascii_check.rs | 1 + tests/ui/manual_is_ascii_check.stderr | 16 +++-- 4 files changed, 54 insertions(+), 39 deletions(-) diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs index 5ab049d8d133..39e7145b4792 100644 --- a/clippy_lints/src/manual_is_ascii_check.rs +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -1,11 +1,11 @@ use clippy_utils::msrvs::{self, Msrv}; -use clippy_utils::{diagnostics::span_lint_and_sugg, in_constant, macros::root_macro_call, source::snippet}; +use clippy_utils::{diagnostics::span_lint_and_sugg, higher, in_constant, macros::root_macro_call, source::snippet}; use rustc_ast::LitKind::{Byte, Char}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, PatKind, RangeEnd}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::{def_id::DefId, sym}; +use rustc_span::{def_id::DefId, sym, Span}; declare_clippy_lint! { /// ### What it does @@ -75,47 +75,54 @@ impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck { return; } - let Some(macro_call) = root_macro_call(expr.span) else { return }; - - if is_matches_macro(cx, macro_call.def_id) { + if let Some(macro_call) = root_macro_call(expr.span) + && is_matches_macro(cx, macro_call.def_id) { if let ExprKind::Match(recv, [arm, ..], _) = expr.kind { let range = check_pat(&arm.pat.kind); - - if let Some(sugg) = match range { - CharRange::UpperChar => Some("is_ascii_uppercase"), - CharRange::LowerChar => Some("is_ascii_lowercase"), - CharRange::FullChar => Some("is_ascii_alphabetic"), - CharRange::Digit => Some("is_ascii_digit"), - CharRange::Otherwise => None, - } { - let default_snip = ".."; - // `snippet_with_applicability` may set applicability to `MaybeIncorrect` for - // macro span, so we check applicability manually by comparing `recv` is not default. - let recv = snippet(cx, recv.span, default_snip); - - let applicability = if recv == default_snip { - Applicability::HasPlaceholders - } else { - Applicability::MachineApplicable - }; - - span_lint_and_sugg( - cx, - MANUAL_IS_ASCII_CHECK, - macro_call.span, - "manual check for common ascii range", - "try", - format!("{recv}.{sugg}()"), - applicability, - ); - } + check_is_ascii(cx, macro_call.span, recv, &range); } + } else if let ExprKind::MethodCall(path, receiver, [arg], ..) = expr.kind + && path.ident.name == sym!(contains) + && let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::Range::hir(receiver) { + let range = check_range(start, end); + check_is_ascii(cx, expr.span, arg, &range); } } extract_msrv_attr!(LateContext); } +fn check_is_ascii(cx: &LateContext<'_>, span: Span, recv: &Expr<'_>, range: &CharRange) { + if let Some(sugg) = match range { + CharRange::UpperChar => Some("is_ascii_uppercase"), + CharRange::LowerChar => Some("is_ascii_lowercase"), + CharRange::FullChar => Some("is_ascii_alphabetic"), + CharRange::Digit => Some("is_ascii_digit"), + CharRange::Otherwise => None, + } { + let default_snip = ".."; + // `snippet_with_applicability` may set applicability to `MaybeIncorrect` for + // macro span, so we check applicability manually by comparing `recv` is not default. + let recv = snippet(cx, recv.span, default_snip); + + let applicability = if recv == default_snip { + Applicability::HasPlaceholders + } else { + Applicability::MachineApplicable + }; + + span_lint_and_sugg( + cx, + MANUAL_IS_ASCII_CHECK, + span, + "manual check for common ascii range", + "try", + format!("{recv}.{sugg}()"), + applicability, + ); + } +} + fn check_pat(pat_kind: &PatKind<'_>) -> CharRange { match pat_kind { PatKind::Or(pats) => { diff --git a/tests/ui/manual_is_ascii_check.fixed b/tests/ui/manual_is_ascii_check.fixed index 231ba83b1426..bfba6dd7cd8d 100644 --- a/tests/ui/manual_is_ascii_check.fixed +++ b/tests/ui/manual_is_ascii_check.fixed @@ -15,6 +15,7 @@ fn main() { assert!('x'.is_ascii_alphabetic()); assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); + assert!(&b'0'.is_ascii_digit()); } #[clippy::msrv = "1.23"] diff --git a/tests/ui/manual_is_ascii_check.rs b/tests/ui/manual_is_ascii_check.rs index 39ee6151c56f..c929f30f729e 100644 --- a/tests/ui/manual_is_ascii_check.rs +++ b/tests/ui/manual_is_ascii_check.rs @@ -15,6 +15,7 @@ fn main() { assert!(matches!('x', 'A'..='Z' | 'a'..='z')); assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); + assert!((b'0'..=b'9').contains(&b'0')); } #[clippy::msrv = "1.23"] diff --git a/tests/ui/manual_is_ascii_check.stderr b/tests/ui/manual_is_ascii_check.stderr index 397cbe05c822..888924f93861 100644 --- a/tests/ui/manual_is_ascii_check.stderr +++ b/tests/ui/manual_is_ascii_check.stderr @@ -43,28 +43,34 @@ LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:29:13 + --> $DIR/manual_is_ascii_check.rs:18:13 + | +LL | assert!((b'0'..=b'9').contains(&b'0')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&b'0'.is_ascii_digit()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:30:13 | LL | assert!(matches!(b'1', b'0'..=b'9')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'1'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:30:13 + --> $DIR/manual_is_ascii_check.rs:31:13 | LL | assert!(matches!('X', 'A'..='Z')); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'X'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:31:13 + --> $DIR/manual_is_ascii_check.rs:32:13 | LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:41:23 + --> $DIR/manual_is_ascii_check.rs:42:23 | LL | const FOO: bool = matches!('x', '0'..='9'); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_digit()` -error: aborting due to 11 previous errors +error: aborting due to 12 previous errors From de92da297466f974b63fb8157419c0616cbfd558 Mon Sep 17 00:00:00 2001 From: naosense Date: Mon, 12 Dec 2022 18:58:02 +0800 Subject: [PATCH 085/321] add more test, limits check --- clippy_lints/src/manual_is_ascii_check.rs | 4 ++- tests/ui/manual_is_ascii_check.fixed | 7 ++++ tests/ui/manual_is_ascii_check.rs | 7 ++++ tests/ui/manual_is_ascii_check.stderr | 42 +++++++++++++++++++---- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs index 39e7145b4792..eaaaf0c65812 100644 --- a/clippy_lints/src/manual_is_ascii_check.rs +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -1,5 +1,6 @@ use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::{diagnostics::span_lint_and_sugg, higher, in_constant, macros::root_macro_call, source::snippet}; +use rustc_ast::ast::RangeLimits; use rustc_ast::LitKind::{Byte, Char}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, PatKind, RangeEnd}; @@ -83,7 +84,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck { } } else if let ExprKind::MethodCall(path, receiver, [arg], ..) = expr.kind && path.ident.name == sym!(contains) - && let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::Range::hir(receiver) { + && let Some(higher::Range { start: Some(start), end: Some(end), limits: RangeLimits::Closed }) + = higher::Range::hir(receiver) { let range = check_range(start, end); check_is_ascii(cx, expr.span, arg, &range); } diff --git a/tests/ui/manual_is_ascii_check.fixed b/tests/ui/manual_is_ascii_check.fixed index bfba6dd7cd8d..b2f45aba59aa 100644 --- a/tests/ui/manual_is_ascii_check.fixed +++ b/tests/ui/manual_is_ascii_check.fixed @@ -15,7 +15,14 @@ fn main() { assert!('x'.is_ascii_alphabetic()); assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); + assert!(&b'0'.is_ascii_digit()); + assert!(&b'a'.is_ascii_lowercase()); + assert!(&b'A'.is_ascii_uppercase()); + + assert!(&'0'.is_ascii_digit()); + assert!(&'a'.is_ascii_lowercase()); + assert!(&'A'.is_ascii_uppercase()); } #[clippy::msrv = "1.23"] diff --git a/tests/ui/manual_is_ascii_check.rs b/tests/ui/manual_is_ascii_check.rs index c929f30f729e..7f1ee88fc743 100644 --- a/tests/ui/manual_is_ascii_check.rs +++ b/tests/ui/manual_is_ascii_check.rs @@ -15,7 +15,14 @@ fn main() { assert!(matches!('x', 'A'..='Z' | 'a'..='z')); assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); + assert!((b'0'..=b'9').contains(&b'0')); + assert!((b'a'..=b'z').contains(&b'a')); + assert!((b'A'..=b'Z').contains(&b'A')); + + assert!(('0'..='9').contains(&'0')); + assert!(('a'..='z').contains(&'a')); + assert!(('A'..='Z').contains(&'A')); } #[clippy::msrv = "1.23"] diff --git a/tests/ui/manual_is_ascii_check.stderr b/tests/ui/manual_is_ascii_check.stderr index 888924f93861..797952a3aba5 100644 --- a/tests/ui/manual_is_ascii_check.stderr +++ b/tests/ui/manual_is_ascii_check.stderr @@ -43,34 +43,64 @@ LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:18:13 + --> $DIR/manual_is_ascii_check.rs:19:13 | LL | assert!((b'0'..=b'9').contains(&b'0')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&b'0'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:30:13 + --> $DIR/manual_is_ascii_check.rs:20:13 + | +LL | assert!((b'a'..=b'z').contains(&b'a')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&b'a'.is_ascii_lowercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:21:13 + | +LL | assert!((b'A'..=b'Z').contains(&b'A')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&b'A'.is_ascii_uppercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:23:13 + | +LL | assert!(('0'..='9').contains(&'0')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'0'.is_ascii_digit()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:24:13 + | +LL | assert!(('a'..='z').contains(&'a')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'a'.is_ascii_lowercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:25:13 + | +LL | assert!(('A'..='Z').contains(&'A')); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'A'.is_ascii_uppercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:37:13 | LL | assert!(matches!(b'1', b'0'..=b'9')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'1'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:31:13 + --> $DIR/manual_is_ascii_check.rs:38:13 | LL | assert!(matches!('X', 'A'..='Z')); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'X'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:32:13 + --> $DIR/manual_is_ascii_check.rs:39:13 | LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:42:23 + --> $DIR/manual_is_ascii_check.rs:49:23 | LL | const FOO: bool = matches!('x', '0'..='9'); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_digit()` -error: aborting due to 12 previous errors +error: aborting due to 17 previous errors From 55fdd1e78c73b67adce581fbf09aa0a1b6f07c61 Mon Sep 17 00:00:00 2001 From: naosense Date: Tue, 13 Dec 2022 10:50:49 +0800 Subject: [PATCH 086/321] replace reference with value --- clippy_lints/src/manual_is_ascii_check.rs | 8 ++++++-- tests/ui/manual_is_ascii_check.fixed | 12 ++++++------ tests/ui/manual_is_ascii_check.stderr | 12 ++++++------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs index eaaaf0c65812..b1578627b50b 100644 --- a/clippy_lints/src/manual_is_ascii_check.rs +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -3,7 +3,7 @@ use clippy_utils::{diagnostics::span_lint_and_sugg, higher, in_constant, macros: use rustc_ast::ast::RangeLimits; use rustc_ast::LitKind::{Byte, Char}; use rustc_errors::Applicability; -use rustc_hir::{Expr, ExprKind, PatKind, RangeEnd}; +use rustc_hir::{BorrowKind, Expr, ExprKind, PatKind, RangeEnd}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{def_id::DefId, sym, Span}; @@ -86,8 +86,12 @@ impl<'tcx> LateLintPass<'tcx> for ManualIsAsciiCheck { && path.ident.name == sym!(contains) && let Some(higher::Range { start: Some(start), end: Some(end), limits: RangeLimits::Closed }) = higher::Range::hir(receiver) { - let range = check_range(start, end); + let range = check_range(start, end); + if let ExprKind::AddrOf(BorrowKind::Ref, _, e) = arg.kind { + check_is_ascii(cx, expr.span, e, &range); + } else { check_is_ascii(cx, expr.span, arg, &range); + } } } diff --git a/tests/ui/manual_is_ascii_check.fixed b/tests/ui/manual_is_ascii_check.fixed index b2f45aba59aa..b5e8b9c19cbe 100644 --- a/tests/ui/manual_is_ascii_check.fixed +++ b/tests/ui/manual_is_ascii_check.fixed @@ -16,13 +16,13 @@ fn main() { assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); - assert!(&b'0'.is_ascii_digit()); - assert!(&b'a'.is_ascii_lowercase()); - assert!(&b'A'.is_ascii_uppercase()); + assert!(b'0'.is_ascii_digit()); + assert!(b'a'.is_ascii_lowercase()); + assert!(b'A'.is_ascii_uppercase()); - assert!(&'0'.is_ascii_digit()); - assert!(&'a'.is_ascii_lowercase()); - assert!(&'A'.is_ascii_uppercase()); + assert!('0'.is_ascii_digit()); + assert!('a'.is_ascii_lowercase()); + assert!('A'.is_ascii_uppercase()); } #[clippy::msrv = "1.23"] diff --git a/tests/ui/manual_is_ascii_check.stderr b/tests/ui/manual_is_ascii_check.stderr index 797952a3aba5..ae747d33b585 100644 --- a/tests/ui/manual_is_ascii_check.stderr +++ b/tests/ui/manual_is_ascii_check.stderr @@ -46,37 +46,37 @@ error: manual check for common ascii range --> $DIR/manual_is_ascii_check.rs:19:13 | LL | assert!((b'0'..=b'9').contains(&b'0')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&b'0'.is_ascii_digit()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'0'.is_ascii_digit()` error: manual check for common ascii range --> $DIR/manual_is_ascii_check.rs:20:13 | LL | assert!((b'a'..=b'z').contains(&b'a')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&b'a'.is_ascii_lowercase()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'a'.is_ascii_lowercase()` error: manual check for common ascii range --> $DIR/manual_is_ascii_check.rs:21:13 | LL | assert!((b'A'..=b'Z').contains(&b'A')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&b'A'.is_ascii_uppercase()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'A'.is_ascii_uppercase()` error: manual check for common ascii range --> $DIR/manual_is_ascii_check.rs:23:13 | LL | assert!(('0'..='9').contains(&'0')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'0'.is_ascii_digit()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'0'.is_ascii_digit()` error: manual check for common ascii range --> $DIR/manual_is_ascii_check.rs:24:13 | LL | assert!(('a'..='z').contains(&'a')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'a'.is_ascii_lowercase()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'a'.is_ascii_lowercase()` error: manual check for common ascii range --> $DIR/manual_is_ascii_check.rs:25:13 | LL | assert!(('A'..='Z').contains(&'A')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'A'.is_ascii_uppercase()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'A'.is_ascii_uppercase()` error: manual check for common ascii range --> $DIR/manual_is_ascii_check.rs:37:13 From 949d0709bd814c1191ab8c0e6c689db3f7ff07e4 Mon Sep 17 00:00:00 2001 From: naosense Date: Tue, 13 Dec 2022 11:11:52 +0800 Subject: [PATCH 087/321] improve document --- clippy_lints/src/manual_is_ascii_check.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs index b1578627b50b..6ee94551d70e 100644 --- a/clippy_lints/src/manual_is_ascii_check.rs +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -24,6 +24,14 @@ declare_clippy_lint! { /// assert!(matches!(b'X', b'A'..=b'Z')); /// assert!(matches!('2', '0'..='9')); /// assert!(matches!('x', 'A'..='Z' | 'a'..='z')); + /// + /// assert!((b'0'..=b'9').contains(&b'0')); + /// assert!((b'a'..=b'z').contains(&b'a')); + /// assert!((b'A'..=b'Z').contains(&b'A')); + /// + /// assert!(('0'..='9').contains(&'0')); + /// assert!(('a'..='z').contains(&'a')); + /// assert!(('A'..='Z').contains(&'A')); /// } /// ``` /// Use instead: @@ -33,6 +41,14 @@ declare_clippy_lint! { /// assert!(b'X'.is_ascii_uppercase()); /// assert!('2'.is_ascii_digit()); /// assert!('x'.is_ascii_alphabetic()); + /// + /// assert!(b'0'.is_ascii_digit()); + /// assert!(b'a'.is_ascii_lowercase()); + /// assert!(b'A'.is_ascii_uppercase()); + /// + /// assert!('0'.is_ascii_digit()); + /// assert!('a'.is_ascii_lowercase()); + /// assert!('A'.is_ascii_uppercase()); /// } /// ``` #[clippy::version = "1.66.0"] From 1f862c2ad31a64beb86a2c6b0c1d2a1947b8173b Mon Sep 17 00:00:00 2001 From: naosense Date: Tue, 13 Dec 2022 16:50:09 +0800 Subject: [PATCH 088/321] remove assert macro --- clippy_lints/src/manual_is_ascii_check.rs | 20 +++---- tests/ui/manual_is_ascii_check.fixed | 17 +++--- tests/ui/manual_is_ascii_check.rs | 17 +++--- tests/ui/manual_is_ascii_check.stderr | 64 +++++++++++++++-------- 4 files changed, 69 insertions(+), 49 deletions(-) diff --git a/clippy_lints/src/manual_is_ascii_check.rs b/clippy_lints/src/manual_is_ascii_check.rs index 6ee94551d70e..d9ef7dffa020 100644 --- a/clippy_lints/src/manual_is_ascii_check.rs +++ b/clippy_lints/src/manual_is_ascii_check.rs @@ -25,13 +25,9 @@ declare_clippy_lint! { /// assert!(matches!('2', '0'..='9')); /// assert!(matches!('x', 'A'..='Z' | 'a'..='z')); /// - /// assert!((b'0'..=b'9').contains(&b'0')); - /// assert!((b'a'..=b'z').contains(&b'a')); - /// assert!((b'A'..=b'Z').contains(&b'A')); - /// - /// assert!(('0'..='9').contains(&'0')); - /// assert!(('a'..='z').contains(&'a')); - /// assert!(('A'..='Z').contains(&'A')); + /// ('0'..='9').contains(&'0'); + /// ('a'..='z').contains(&'a'); + /// ('A'..='Z').contains(&'A'); /// } /// ``` /// Use instead: @@ -42,13 +38,9 @@ declare_clippy_lint! { /// assert!('2'.is_ascii_digit()); /// assert!('x'.is_ascii_alphabetic()); /// - /// assert!(b'0'.is_ascii_digit()); - /// assert!(b'a'.is_ascii_lowercase()); - /// assert!(b'A'.is_ascii_uppercase()); - /// - /// assert!('0'.is_ascii_digit()); - /// assert!('a'.is_ascii_lowercase()); - /// assert!('A'.is_ascii_uppercase()); + /// '0'.is_ascii_digit(); + /// 'a'.is_ascii_lowercase(); + /// 'A'.is_ascii_uppercase(); /// } /// ``` #[clippy::version = "1.66.0"] diff --git a/tests/ui/manual_is_ascii_check.fixed b/tests/ui/manual_is_ascii_check.fixed index b5e8b9c19cbe..5b2b44c2fdb2 100644 --- a/tests/ui/manual_is_ascii_check.fixed +++ b/tests/ui/manual_is_ascii_check.fixed @@ -16,13 +16,18 @@ fn main() { assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); - assert!(b'0'.is_ascii_digit()); - assert!(b'a'.is_ascii_lowercase()); - assert!(b'A'.is_ascii_uppercase()); + b'0'.is_ascii_digit(); + b'a'.is_ascii_lowercase(); + b'A'.is_ascii_uppercase(); - assert!('0'.is_ascii_digit()); - assert!('a'.is_ascii_lowercase()); - assert!('A'.is_ascii_uppercase()); + '0'.is_ascii_digit(); + 'a'.is_ascii_lowercase(); + 'A'.is_ascii_uppercase(); + + let cool_letter = &'g'; + cool_letter.is_ascii_digit(); + cool_letter.is_ascii_lowercase(); + cool_letter.is_ascii_uppercase(); } #[clippy::msrv = "1.23"] diff --git a/tests/ui/manual_is_ascii_check.rs b/tests/ui/manual_is_ascii_check.rs index 7f1ee88fc743..c9433f33a1b6 100644 --- a/tests/ui/manual_is_ascii_check.rs +++ b/tests/ui/manual_is_ascii_check.rs @@ -16,13 +16,18 @@ fn main() { assert!(matches!('x', 'A'..='Z' | 'a'..='z' | '_')); - assert!((b'0'..=b'9').contains(&b'0')); - assert!((b'a'..=b'z').contains(&b'a')); - assert!((b'A'..=b'Z').contains(&b'A')); + (b'0'..=b'9').contains(&b'0'); + (b'a'..=b'z').contains(&b'a'); + (b'A'..=b'Z').contains(&b'A'); - assert!(('0'..='9').contains(&'0')); - assert!(('a'..='z').contains(&'a')); - assert!(('A'..='Z').contains(&'A')); + ('0'..='9').contains(&'0'); + ('a'..='z').contains(&'a'); + ('A'..='Z').contains(&'A'); + + let cool_letter = &'g'; + ('0'..='9').contains(cool_letter); + ('a'..='z').contains(cool_letter); + ('A'..='Z').contains(cool_letter); } #[clippy::msrv = "1.23"] diff --git a/tests/ui/manual_is_ascii_check.stderr b/tests/ui/manual_is_ascii_check.stderr index ae747d33b585..ee60188506d6 100644 --- a/tests/ui/manual_is_ascii_check.stderr +++ b/tests/ui/manual_is_ascii_check.stderr @@ -43,64 +43,82 @@ LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:19:13 + --> $DIR/manual_is_ascii_check.rs:19:5 | -LL | assert!((b'0'..=b'9').contains(&b'0')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'0'.is_ascii_digit()` +LL | (b'0'..=b'9').contains(&b'0'); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'0'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:20:13 + --> $DIR/manual_is_ascii_check.rs:20:5 | -LL | assert!((b'a'..=b'z').contains(&b'a')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'a'.is_ascii_lowercase()` +LL | (b'a'..=b'z').contains(&b'a'); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'a'.is_ascii_lowercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:21:13 + --> $DIR/manual_is_ascii_check.rs:21:5 | -LL | assert!((b'A'..=b'Z').contains(&b'A')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'A'.is_ascii_uppercase()` +LL | (b'A'..=b'Z').contains(&b'A'); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'A'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:23:13 + --> $DIR/manual_is_ascii_check.rs:23:5 | -LL | assert!(('0'..='9').contains(&'0')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'0'.is_ascii_digit()` +LL | ('0'..='9').contains(&'0'); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'0'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:24:13 + --> $DIR/manual_is_ascii_check.rs:24:5 | -LL | assert!(('a'..='z').contains(&'a')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'a'.is_ascii_lowercase()` +LL | ('a'..='z').contains(&'a'); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'a'.is_ascii_lowercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:25:13 + --> $DIR/manual_is_ascii_check.rs:25:5 | -LL | assert!(('A'..='Z').contains(&'A')); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'A'.is_ascii_uppercase()` +LL | ('A'..='Z').contains(&'A'); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'A'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:37:13 + --> $DIR/manual_is_ascii_check.rs:28:5 + | +LL | ('0'..='9').contains(cool_letter); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cool_letter.is_ascii_digit()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:29:5 + | +LL | ('a'..='z').contains(cool_letter); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cool_letter.is_ascii_lowercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:30:5 + | +LL | ('A'..='Z').contains(cool_letter); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cool_letter.is_ascii_uppercase()` + +error: manual check for common ascii range + --> $DIR/manual_is_ascii_check.rs:42:13 | LL | assert!(matches!(b'1', b'0'..=b'9')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b'1'.is_ascii_digit()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:38:13 + --> $DIR/manual_is_ascii_check.rs:43:13 | LL | assert!(matches!('X', 'A'..='Z')); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'X'.is_ascii_uppercase()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:39:13 + --> $DIR/manual_is_ascii_check.rs:44:13 | LL | assert!(matches!('x', 'A'..='Z' | 'a'..='z')); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_alphabetic()` error: manual check for common ascii range - --> $DIR/manual_is_ascii_check.rs:49:23 + --> $DIR/manual_is_ascii_check.rs:54:23 | LL | const FOO: bool = matches!('x', '0'..='9'); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `'x'.is_ascii_digit()` -error: aborting due to 17 previous errors +error: aborting due to 20 previous errors From 73d374f3e7d5fe9635a23937cdc2f7e1d1c29224 Mon Sep 17 00:00:00 2001 From: Jacob Bramley Date: Tue, 13 Dec 2022 17:04:02 +0000 Subject: [PATCH 089/321] bug! if branch-protection makes it to non-AArch64 codegen. --- compiler/rustc_codegen_llvm/src/context.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index a22a67ad7d34..f6fcdc90cb61 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -274,9 +274,8 @@ pub unsafe fn create_module<'ll>( } } - // AArch64-only options (checked in rustc_session). - if sess.target.arch == "aarch64" { - if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { + if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { + if sess.target.arch == "aarch64" { llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Error, @@ -302,6 +301,11 @@ pub unsafe fn create_module<'ll>( "sign-return-address-with-bkey\0".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); + } else { + bug!( + "branch-protection used on non-AArch64 target; \ + this should be checked in rustc_session." + ); } } From 8b2a7da3b0d5ef813c3ecbdde2a550eedef27712 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 12 Dec 2022 15:47:32 +0100 Subject: [PATCH 090/321] Rename `assert_uninit_valid` intrinsic It's not about "uninit" anymore but about "filling with 0x01 bytes" so the name should at least try to reflect that. --- compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs | 6 ++++-- compiler/rustc_codegen_ssa/src/mir/block.rs | 6 +++--- compiler/rustc_const_eval/src/interpret/intrinsics.rs | 6 ++++-- compiler/rustc_hir_analysis/src/check/intrinsic.rs | 8 ++++---- compiler/rustc_span/src/symbol.rs | 2 +- library/core/src/intrinsics.rs | 7 ++++--- library/core/src/mem/mod.rs | 3 ++- src/test/ui/consts/assert-type-intrinsics.rs | 2 +- src/test/ui/consts/assert-type-intrinsics.stderr | 4 ++-- 9 files changed, 25 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs index 0302b843aa22..e4a27f1bb6d4 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs @@ -713,7 +713,7 @@ fn codegen_regular_intrinsic_call<'tcx>( let res = CValue::by_val(swap(&mut fx.bcx, val), arg.layout()); ret.write_cvalue(fx, res); } - sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => { + sym::assert_inhabited | sym::assert_zero_valid | sym::assert_mem_uninitialized_valid => { intrinsic_args!(fx, args => (); intrinsic); let layout = fx.layout_of(substs.type_at(0)); @@ -742,7 +742,9 @@ fn codegen_regular_intrinsic_call<'tcx>( return; } - if intrinsic == sym::assert_uninit_valid && !fx.tcx.permits_uninit_init(layout) { + if intrinsic == sym::assert_mem_uninitialized_valid + && !fx.tcx.permits_uninit_init(layout) + { with_no_trimmed_paths!({ crate::base::codegen_panic( fx, diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index f3f5ddb52d6a..a1cd43f749ec 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -666,12 +666,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { enum AssertIntrinsic { Inhabited, ZeroValid, - UninitValid, + MemUninitializedValid, } let panic_intrinsic = intrinsic.and_then(|i| match i { sym::assert_inhabited => Some(AssertIntrinsic::Inhabited), sym::assert_zero_valid => Some(AssertIntrinsic::ZeroValid), - sym::assert_uninit_valid => Some(AssertIntrinsic::UninitValid), + sym::assert_mem_uninitialized_valid => Some(AssertIntrinsic::MemUninitializedValid), _ => None, }); if let Some(intrinsic) = panic_intrinsic { @@ -682,7 +682,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let do_panic = match intrinsic { Inhabited => layout.abi.is_uninhabited(), ZeroValid => !bx.tcx().permits_zero_init(layout), - UninitValid => !bx.tcx().permits_uninit_init(layout), + MemUninitializedValid => !bx.tcx().permits_uninit_init(layout), }; Some(if do_panic { let msg_str = with_no_visible_paths!({ diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index 7940efcd2b11..27abc2b97b02 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -432,7 +432,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { sym::transmute => { self.copy_op(&args[0], dest, /*allow_transmute*/ true)?; } - sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => { + sym::assert_inhabited + | sym::assert_zero_valid + | sym::assert_mem_uninitialized_valid => { let ty = instance.substs.type_at(0); let layout = self.layout_of(ty)?; @@ -464,7 +466,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } } - if intrinsic_name == sym::assert_uninit_valid { + if intrinsic_name == sym::assert_mem_uninitialized_valid { let should_panic = !self.tcx.permits_uninit_init(layout); if should_panic { diff --git a/compiler/rustc_hir_analysis/src/check/intrinsic.rs b/compiler/rustc_hir_analysis/src/check/intrinsic.rs index 69e54b41d4c0..598dc2dca5c6 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsic.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsic.rs @@ -75,7 +75,7 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir sym::abort | sym::assert_inhabited | sym::assert_zero_valid - | sym::assert_uninit_valid + | sym::assert_mem_uninitialized_valid | sym::size_of | sym::min_align_of | sym::needs_drop @@ -193,9 +193,9 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) { } sym::rustc_peek => (1, vec![param(0)], param(0)), sym::caller_location => (0, vec![], tcx.caller_location_ty()), - sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => { - (1, Vec::new(), tcx.mk_unit()) - } + sym::assert_inhabited + | sym::assert_zero_valid + | sym::assert_mem_uninitialized_valid => (1, Vec::new(), tcx.mk_unit()), sym::forget => (1, vec![param(0)], tcx.mk_unit()), sym::transmute => (2, vec![param(0)], param(1)), sym::prefetch_read_data diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 1fcf8c7a8bf1..c97340983c01 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -377,9 +377,9 @@ symbols! { assert_eq_macro, assert_inhabited, assert_macro, + assert_mem_uninitialized_valid, assert_ne_macro, assert_receiver_is_total_eq, - assert_uninit_valid, assert_zero_valid, asserting, associated_const_equality, diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index 7ed7d767f2fb..ed58a7f17993 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -959,13 +959,14 @@ extern "rust-intrinsic" { #[rustc_safe_intrinsic] pub fn assert_zero_valid(); - /// A guard for unsafe functions that cannot ever be executed if `T` has invalid - /// bit patterns: This will statically either panic, or do nothing. + /// A guard for `std::mem::uninitialized`. Checks whether a repeated bit pattern `0x01` + /// is legal for `T`: This will statically either panic, or do nothing. /// /// This intrinsic does not have a stable counterpart. #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")] #[rustc_safe_intrinsic] - pub fn assert_uninit_valid(); + #[cfg(not(bootstrap))] + pub fn assert_mem_uninitialized_valid(); /// Gets a reference to a static `Location` indicating where it was called. /// diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 383bdc7b6e2e..5e01ccc07d8f 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -682,7 +682,8 @@ pub unsafe fn zeroed() -> T { pub unsafe fn uninitialized() -> T { // SAFETY: the caller must guarantee that an uninitialized value is valid for `T`. unsafe { - intrinsics::assert_uninit_valid::(); + #[cfg(not(bootstrap))] // If the compiler hits this itself then it deserves the UB. + intrinsics::assert_mem_uninitialized_valid::(); let mut val = MaybeUninit::::uninit(); // Fill memory with 0x01, as an imperfect mitigation for old code that uses this function on diff --git a/src/test/ui/consts/assert-type-intrinsics.rs b/src/test/ui/consts/assert-type-intrinsics.rs index 263d1ae6a3ec..b4fd423becd9 100644 --- a/src/test/ui/consts/assert-type-intrinsics.rs +++ b/src/test/ui/consts/assert-type-intrinsics.rs @@ -13,7 +13,7 @@ fn main() { //~^ERROR: evaluation of constant value failed }; const _BAD2: () = { - intrinsics::assert_uninit_valid::<&'static i32>(); + intrinsics::assert_mem_uninitialized_valid::<&'static i32>(); //~^ERROR: evaluation of constant value failed }; const _BAD3: () = { diff --git a/src/test/ui/consts/assert-type-intrinsics.stderr b/src/test/ui/consts/assert-type-intrinsics.stderr index f92f9fda069a..70aec91e2262 100644 --- a/src/test/ui/consts/assert-type-intrinsics.stderr +++ b/src/test/ui/consts/assert-type-intrinsics.stderr @@ -7,8 +7,8 @@ LL | MaybeUninit::::uninit().assume_init(); error[E0080]: evaluation of constant value failed --> $DIR/assert-type-intrinsics.rs:16:9 | -LL | intrinsics::assert_uninit_valid::<&'static i32>(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to leave type `&i32` uninitialized, which is invalid +LL | intrinsics::assert_mem_uninitialized_valid::<&'static i32>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to leave type `&i32` uninitialized, which is invalid error[E0080]: evaluation of constant value failed --> $DIR/assert-type-intrinsics.rs:20:9 From ad55e4c9721f3283205f1518facaae5e66380e45 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 21:09:39 +0000 Subject: [PATCH 091/321] Use ty::OpaqueTy everywhere --- clippy_lints/src/future_not_send.rs | 8 ++++---- clippy_utils/src/ty.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/future_not_send.rs b/clippy_lints/src/future_not_send.rs index 61934a914263..8a7a65c86001 100644 --- a/clippy_lints/src/future_not_send.rs +++ b/clippy_lints/src/future_not_send.rs @@ -4,7 +4,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, FnDecl, HirId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{Clause, EarlyBinder, Opaque, PredicateKind}; +use rustc_middle::ty::{Clause, EarlyBinder, Opaque, OpaqueTy, PredicateKind}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{sym, Span}; use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt; @@ -62,11 +62,11 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { return; } let ret_ty = return_ty(cx, hir_id); - if let Opaque(id, subst) = *ret_ty.kind() { - let preds = cx.tcx.explicit_item_bounds(id); + if let Opaque(OpaqueTy { def_id, substs }) = *ret_ty.kind() { + let preds = cx.tcx.explicit_item_bounds(def_id); let mut is_future = false; for &(p, _span) in preds { - let p = EarlyBinder(p).subst(cx.tcx, subst); + let p = EarlyBinder(p).subst(cx.tcx, substs); if let Some(trait_pred) = p.to_opt_poly_trait_pred() { if Some(trait_pred.skip_binder().trait_ref.def_id) == cx.tcx.lang_items().future_trait() { is_future = true; diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index bfb2d472a393..f5f70b195c98 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -79,7 +79,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' return true; } - if let ty::Opaque(def_id, _) = *inner_ty.kind() { + if let ty::Opaque(ty::OpaqueTy { def_id, substs: _ }) = *inner_ty.kind() { for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { match predicate.kind().skip_binder() { // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through @@ -250,7 +250,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { is_must_use_ty(cx, *ty) }, ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)), - ty::Opaque(def_id, _) => { + ty::Opaque(ty::OpaqueTy { def_id, substs: _ }) => { for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) { if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() { if cx.tcx.has_attr(trait_predicate.trait_ref.def_id, sym::must_use) { @@ -631,7 +631,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option Some(ExprFnSig::Sig(cx.tcx.bound_fn_sig(id).subst(cx.tcx, subs), Some(id))), - ty::Opaque(id, _) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(id), cx.tcx.opt_parent(id)), + ty::Opaque(ty::OpaqueTy{ def_id, substs: _ }) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(def_id), cx.tcx.opt_parent(def_id)), ty::FnPtr(sig) => Some(ExprFnSig::Sig(sig, None)), ty::Dynamic(bounds, _, _) => { let lang_items = cx.tcx.lang_items(); From a274e7e9a2d6373d1989d187cdf8475e55be9f6f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 21:21:20 +0000 Subject: [PATCH 092/321] ProjectionTy.item_def_id -> ProjectionTy.def_id --- clippy_lints/src/dereference.rs | 2 +- clippy_lints/src/len_zero.rs | 2 +- clippy_lints/src/methods/needless_collect.rs | 2 +- clippy_utils/src/ty.rs | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index 38329659e02b..ad5a1b2beb70 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -1330,7 +1330,7 @@ fn replace_types<'tcx>( && let Some(term_ty) = projection_predicate.term.ty() && let ty::Param(term_param_ty) = term_ty.kind() { - let item_def_id = projection_predicate.projection_ty.item_def_id; + let item_def_id = projection_predicate.projection_ty.def_id; let assoc_item = cx.tcx.associated_item(item_def_id); let projection = cx.tcx .mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(new_ty, [])); diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index 4c133c06a157..982f99c27163 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -493,7 +493,7 @@ fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { .filter_by_name_unhygienic(is_empty) .any(|item| is_is_empty(cx, item)) }), - ty::Projection(ref proj) => has_is_empty_impl(cx, proj.item_def_id), + ty::Projection(ref proj) => has_is_empty_impl(cx, proj.def_id), ty::Adt(id, _) => has_is_empty_impl(cx, id.did()), ty::Array(..) | ty::Slice(..) | ty::Str => true, _ => false, diff --git a/clippy_lints/src/methods/needless_collect.rs b/clippy_lints/src/methods/needless_collect.rs index b088e642e0e9..f4d3ef3b7425 100644 --- a/clippy_lints/src/methods/needless_collect.rs +++ b/clippy_lints/src/methods/needless_collect.rs @@ -151,7 +151,7 @@ fn iterates_same_ty<'tcx>(cx: &LateContext<'tcx>, iter_ty: Ty<'tcx>, collect_ty: && let Some(into_iter_item_proj) = make_projection(cx.tcx, into_iter_trait, item, [collect_ty]) && let Ok(into_iter_item_ty) = cx.tcx.try_normalize_erasing_regions( cx.param_env, - cx.tcx.mk_projection(into_iter_item_proj.item_def_id, into_iter_item_proj.substs) + cx.tcx.mk_projection(into_iter_item_proj.def_id, into_iter_item_proj.substs) ) { iter_item_ty == into_iter_item_ty diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index f5f70b195c98..11e41d1958ce 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -685,7 +685,7 @@ fn sig_from_bounds<'tcx>( inputs = Some(i); }, PredicateKind::Clause(ty::Clause::Projection(p)) - if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output() + if Some(p.projection_ty.def_id) == lang_items.fn_once_output() && p.projection_ty.self_ty() == ty => { if output.is_some() { @@ -708,7 +708,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> O for (pred, _) in cx .tcx - .bound_explicit_item_bounds(ty.item_def_id) + .bound_explicit_item_bounds(ty.def_id) .subst_iter_copied(cx.tcx, ty.substs) { match pred.kind().skip_binder() { @@ -726,7 +726,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> O inputs = Some(i); }, PredicateKind::Clause(ty::Clause::Projection(p)) - if Some(p.projection_ty.item_def_id) == lang_items.fn_once_output() => + if Some(p.projection_ty.def_id) == lang_items.fn_once_output() => { if output.is_some() { // Multiple different fn trait impls. Is this even allowed? @@ -1041,7 +1041,7 @@ pub fn make_projection<'tcx>( Some(ProjectionTy { substs, - item_def_id: assoc_item.def_id, + def_id: assoc_item.def_id, }) } helper( @@ -1081,7 +1081,7 @@ pub fn make_normalized_projection<'tcx>( ); return None; } - match tcx.try_normalize_erasing_regions(param_env, tcx.mk_projection(ty.item_def_id, ty.substs)) { + match tcx.try_normalize_erasing_regions(param_env, tcx.mk_projection(ty.def_id, ty.substs)) { Ok(ty) => Some(ty), Err(e) => { debug_assert!(false, "failed to normalize type `{ty}`: {e:#?}"); From 89b884054334c2f0334c4a6218fcb489813a6633 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 21:32:01 +0000 Subject: [PATCH 093/321] squash OpaqueTy and ProjectionTy into AliasTy --- clippy_lints/src/future_not_send.rs | 4 ++-- clippy_utils/src/ty.rs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/future_not_send.rs b/clippy_lints/src/future_not_send.rs index 8a7a65c86001..3ff774867b1e 100644 --- a/clippy_lints/src/future_not_send.rs +++ b/clippy_lints/src/future_not_send.rs @@ -4,7 +4,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, FnDecl, HirId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{Clause, EarlyBinder, Opaque, OpaqueTy, PredicateKind}; +use rustc_middle::ty::{AliasTy, Clause, EarlyBinder, Opaque, PredicateKind}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{sym, Span}; use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt; @@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { return; } let ret_ty = return_ty(cx, hir_id); - if let Opaque(OpaqueTy { def_id, substs }) = *ret_ty.kind() { + if let Opaque(AliasTy { def_id, substs }) = *ret_ty.kind() { let preds = cx.tcx.explicit_item_bounds(def_id); let mut is_future = false; for &(p, _span) in preds { diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 11e41d1958ce..bddab7eca53b 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -17,7 +17,7 @@ use rustc_lint::LateContext; use rustc_middle::mir::interpret::{ConstValue, Scalar}; use rustc_middle::ty::{ self, AdtDef, AssocKind, Binder, BoundRegion, DefIdTree, FnSig, IntTy, List, ParamEnv, Predicate, PredicateKind, - ProjectionTy, Region, RegionKind, SubstsRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, UintTy, + AliasTy, Region, RegionKind, SubstsRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, UintTy, VariantDef, VariantDiscr, }; use rustc_middle::ty::{GenericArg, GenericArgKind}; @@ -79,7 +79,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' return true; } - if let ty::Opaque(ty::OpaqueTy { def_id, substs: _ }) = *inner_ty.kind() { + if let ty::Opaque(ty::AliasTy { def_id, substs: _ }) = *inner_ty.kind() { for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { match predicate.kind().skip_binder() { // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through @@ -250,7 +250,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { is_must_use_ty(cx, *ty) }, ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)), - ty::Opaque(ty::OpaqueTy { def_id, substs: _ }) => { + ty::Opaque(ty::AliasTy { def_id, substs: _ }) => { for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) { if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() { if cx.tcx.has_attr(trait_predicate.trait_ref.def_id, sym::must_use) { @@ -631,7 +631,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option Some(ExprFnSig::Sig(cx.tcx.bound_fn_sig(id).subst(cx.tcx, subs), Some(id))), - ty::Opaque(ty::OpaqueTy{ def_id, substs: _ }) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(def_id), cx.tcx.opt_parent(def_id)), + ty::Opaque(ty::AliasTy { def_id, substs: _ }) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(def_id), cx.tcx.opt_parent(def_id)), ty::FnPtr(sig) => Some(ExprFnSig::Sig(sig, None)), ty::Dynamic(bounds, _, _) => { let lang_items = cx.tcx.lang_items(); @@ -701,7 +701,7 @@ fn sig_from_bounds<'tcx>( inputs.map(|ty| ExprFnSig::Trait(ty, output, predicates_id)) } -fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: ProjectionTy<'tcx>) -> Option> { +fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: AliasTy<'tcx>) -> Option> { let mut inputs = None; let mut output = None; let lang_items = cx.tcx.lang_items(); @@ -980,13 +980,13 @@ pub fn make_projection<'tcx>( container_id: DefId, assoc_ty: Symbol, substs: impl IntoIterator>>, -) -> Option> { +) -> Option> { fn helper<'tcx>( tcx: TyCtxt<'tcx>, container_id: DefId, assoc_ty: Symbol, substs: SubstsRef<'tcx>, - ) -> Option> { + ) -> Option> { let Some(assoc_item) = tcx .associated_items(container_id) .find_by_name_and_kind(tcx, Ident::with_dummy_span(assoc_ty), AssocKind::Type, container_id) @@ -1039,7 +1039,7 @@ pub fn make_projection<'tcx>( } } - Some(ProjectionTy { + Some(AliasTy { substs, def_id: assoc_item.def_id, }) @@ -1065,7 +1065,7 @@ pub fn make_normalized_projection<'tcx>( assoc_ty: Symbol, substs: impl IntoIterator>>, ) -> Option> { - fn helper<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: ProjectionTy<'tcx>) -> Option> { + fn helper<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: AliasTy<'tcx>) -> Option> { #[cfg(debug_assertions)] if let Some((i, subst)) = ty .substs From 957ab6ae52706e3428ca56e727eed9d3333d8170 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 26 Nov 2022 21:51:55 +0000 Subject: [PATCH 094/321] Combine projection and opaque into alias --- clippy_lints/src/dereference.rs | 8 ++++---- clippy_lints/src/future_not_send.rs | 4 ++-- clippy_lints/src/len_zero.rs | 2 +- clippy_utils/src/qualify_min_const_fn.rs | 2 +- clippy_utils/src/ty.rs | 8 ++++---- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index ad5a1b2beb70..31183266acfc 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -1244,7 +1244,7 @@ fn is_mixed_projection_predicate<'tcx>( let mut projection_ty = projection_predicate.projection_ty; loop { match projection_ty.self_ty().kind() { - ty::Projection(inner_projection_ty) => { + ty::Alias(ty::Projection, inner_projection_ty) => { projection_ty = *inner_projection_ty; } ty::Param(param_ty) => { @@ -1390,8 +1390,8 @@ fn ty_auto_deref_stability<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, precedenc continue; }, ty::Param(_) => TyPosition::new_deref_stable_for_result(precedence, ty), - ty::Projection(_) if ty.has_non_region_param() => TyPosition::new_deref_stable_for_result(precedence, ty), - ty::Infer(_) | ty::Error(_) | ty::Bound(..) | ty::Opaque(..) | ty::Placeholder(_) | ty::Dynamic(..) => { + ty::Alias(ty::Projection, _) if ty.has_non_region_param() => TyPosition::new_deref_stable_for_result(precedence, ty), + ty::Infer(_) | ty::Error(_) | ty::Bound(..) | ty::Alias(ty::Opaque, ..) | ty::Placeholder(_) | ty::Dynamic(..) => { Position::ReborrowStable(precedence).into() }, ty::Adt(..) if ty.has_placeholders() || ty.has_opaque_types() => { @@ -1417,7 +1417,7 @@ fn ty_auto_deref_stability<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, precedenc | ty::Closure(..) | ty::Never | ty::Tuple(_) - | ty::Projection(_) => { + | ty::Alias(ty::Projection, _) => { Position::DerefStable(precedence, ty.is_sized(cx.tcx, cx.param_env.without_caller_bounds())).into() }, }; diff --git a/clippy_lints/src/future_not_send.rs b/clippy_lints/src/future_not_send.rs index 3ff774867b1e..fcdac90fc237 100644 --- a/clippy_lints/src/future_not_send.rs +++ b/clippy_lints/src/future_not_send.rs @@ -4,7 +4,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, FnDecl, HirId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{AliasTy, Clause, EarlyBinder, Opaque, PredicateKind}; +use rustc_middle::ty::{self, AliasTy, Clause, EarlyBinder, PredicateKind}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{sym, Span}; use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt; @@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { return; } let ret_ty = return_ty(cx, hir_id); - if let Opaque(AliasTy { def_id, substs }) = *ret_ty.kind() { + if let ty::Alias(ty::Opaque, AliasTy { def_id, substs }) = *ret_ty.kind() { let preds = cx.tcx.explicit_item_bounds(def_id); let mut is_future = false; for &(p, _span) in preds { diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index 982f99c27163..73841f9aa9a2 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -493,7 +493,7 @@ fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { .filter_by_name_unhygienic(is_empty) .any(|item| is_is_empty(cx, item)) }), - ty::Projection(ref proj) => has_is_empty_impl(cx, proj.def_id), + ty::Alias(ty::Projection, ref proj) => has_is_empty_impl(cx, proj.def_id), ty::Adt(id, _) => has_is_empty_impl(cx, id.did()), ty::Array(..) | ty::Slice(..) | ty::Str => true, _ => false, diff --git a/clippy_utils/src/qualify_min_const_fn.rs b/clippy_utils/src/qualify_min_const_fn.rs index e053a9dc8881..8bf542ada04d 100644 --- a/clippy_utils/src/qualify_min_const_fn.rs +++ b/clippy_utils/src/qualify_min_const_fn.rs @@ -82,7 +82,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult { ty::Ref(_, _, hir::Mutability::Mut) => { return Err((span, "mutable references in const fn are unstable".into())); }, - ty::Opaque(..) => return Err((span, "`impl Trait` in const fn is unstable".into())), + ty::Alias(ty::Opaque, ..) => return Err((span, "`impl Trait` in const fn is unstable".into())), ty::FnPtr(..) => { return Err((span, "function pointers in const fn are unstable".into())); }, diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index bddab7eca53b..33f3b3af3dc0 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -79,7 +79,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' return true; } - if let ty::Opaque(ty::AliasTy { def_id, substs: _ }) = *inner_ty.kind() { + if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }) = *inner_ty.kind() { for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { match predicate.kind().skip_binder() { // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through @@ -250,7 +250,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { is_must_use_ty(cx, *ty) }, ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)), - ty::Opaque(ty::AliasTy { def_id, substs: _ }) => { + ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }) => { for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) { if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() { if cx.tcx.has_attr(trait_predicate.trait_ref.def_id, sym::must_use) { @@ -631,7 +631,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option Some(ExprFnSig::Sig(cx.tcx.bound_fn_sig(id).subst(cx.tcx, subs), Some(id))), - ty::Opaque(ty::AliasTy { def_id, substs: _ }) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(def_id), cx.tcx.opt_parent(def_id)), + ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(def_id), cx.tcx.opt_parent(def_id)), ty::FnPtr(sig) => Some(ExprFnSig::Sig(sig, None)), ty::Dynamic(bounds, _, _) => { let lang_items = cx.tcx.lang_items(); @@ -650,7 +650,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option None, } }, - ty::Projection(proj) => match cx.tcx.try_normalize_erasing_regions(cx.param_env, ty) { + ty::Alias(ty::Projection, proj) => match cx.tcx.try_normalize_erasing_regions(cx.param_env, ty) { Ok(normalized_ty) if normalized_ty != ty => ty_sig(cx, normalized_ty), _ => sig_for_projection(cx, proj).or_else(|| sig_from_bounds(cx, ty, cx.param_env.caller_bounds(), None)), }, From 2532c56d8643c9ee343da8dff60828fcfe77d065 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Tue, 13 Dec 2022 18:29:22 +0100 Subject: [PATCH 095/321] Update version attribute for 1.66 lints --- clippy_lints/src/box_default.rs | 2 +- clippy_lints/src/casts/mod.rs | 2 +- clippy_lints/src/disallowed_macros.rs | 2 +- clippy_lints/src/format_args.rs | 2 +- clippy_lints/src/implicit_saturating_add.rs | 2 +- clippy_lints/src/methods/mod.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/box_default.rs b/clippy_lints/src/box_default.rs index 36daceabe0be..91900542af83 100644 --- a/clippy_lints/src/box_default.rs +++ b/clippy_lints/src/box_default.rs @@ -30,7 +30,7 @@ declare_clippy_lint! { /// ```rust /// let x: Box = Box::default(); /// ``` - #[clippy::version = "1.65.0"] + #[clippy::version = "1.66.0"] pub BOX_DEFAULT, perf, "Using Box::new(T::default()) instead of Box::default()" diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs index c6d505c4a181..161e3a698e9e 100644 --- a/clippy_lints/src/casts/mod.rs +++ b/clippy_lints/src/casts/mod.rs @@ -641,7 +641,7 @@ declare_clippy_lint! { /// ```rust,ignore /// let _: = 0_u64; /// ``` - #[clippy::version = "1.64.0"] + #[clippy::version = "1.66.0"] pub CAST_NAN_TO_INT, suspicious, "casting a known floating-point NaN into an integer" diff --git a/clippy_lints/src/disallowed_macros.rs b/clippy_lints/src/disallowed_macros.rs index 68122b4cef57..1971cab64ef3 100644 --- a/clippy_lints/src/disallowed_macros.rs +++ b/clippy_lints/src/disallowed_macros.rs @@ -47,7 +47,7 @@ declare_clippy_lint! { /// value: usize, /// } /// ``` - #[clippy::version = "1.65.0"] + #[clippy::version = "1.66.0"] pub DISALLOWED_MACROS, style, "use of a disallowed macro" diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs index f0995a81329d..08976bde8829 100644 --- a/clippy_lints/src/format_args.rs +++ b/clippy_lints/src/format_args.rs @@ -122,7 +122,7 @@ declare_clippy_lint! { /// /// If a format string contains a numbered argument that cannot be inlined /// nothing will be suggested, e.g. `println!("{0}={1}", var, 1+2)`. - #[clippy::version = "1.65.0"] + #[clippy::version = "1.66.0"] pub UNINLINED_FORMAT_ARGS, style, "using non-inlined variables in `format!` calls" diff --git a/clippy_lints/src/implicit_saturating_add.rs b/clippy_lints/src/implicit_saturating_add.rs index bf1351829c6a..6e19343931ec 100644 --- a/clippy_lints/src/implicit_saturating_add.rs +++ b/clippy_lints/src/implicit_saturating_add.rs @@ -31,7 +31,7 @@ declare_clippy_lint! { /// /// u = u.saturating_add(1); /// ``` - #[clippy::version = "1.65.0"] + #[clippy::version = "1.66.0"] pub IMPLICIT_SATURATING_ADD, style, "Perform saturating addition instead of implicitly checking max bound of data type" diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d2913680cbb7..30883ba8d051 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -3059,7 +3059,7 @@ declare_clippy_lint! { /// let map: HashMap = HashMap::new(); /// let values = map.values().collect::>(); /// ``` - #[clippy::version = "1.65.0"] + #[clippy::version = "1.66.0"] pub ITER_KV_MAP, complexity, "iterating on map using `iter` when `keys` or `values` would do" From 2855a0f117c86d89b5ee4ec5eba50cd1700c6e08 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Tue, 13 Dec 2022 18:30:49 +0100 Subject: [PATCH 096/321] Changelog for Rust 1.66 :santa: --- CHANGELOG.md | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23912bb3ed6b..cc3f785bb37e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,180 @@ document. ## Unreleased / Beta / In Rust Nightly -[b52fb523...master](https://github.com/rust-lang/rust-clippy/compare/b52fb523...master) +[4f142aa1...master](https://github.com/rust-lang/rust-clippy/compare/4f142aa1...master) + +## Rust 1.66 + +Current stable, released 2022-11-03 + +[b52fb523...4f142aa1](https://github.com/rust-lang/rust-clippy/compare/b52fb523...4f142aa1) + +### New Lints + +* [`manual_clamp`] + [#9484](https://github.com/rust-lang/rust-clippy/pull/9484) +* [`missing_trait_methods`] + [#9670](https://github.com/rust-lang/rust-clippy/pull/9670) +* [`unused_format_specs`] + [#9637](https://github.com/rust-lang/rust-clippy/pull/9637) +* [`iter_kv_map`] + [#9409](https://github.com/rust-lang/rust-clippy/pull/9409) +* [`manual_filter`] + [#9451](https://github.com/rust-lang/rust-clippy/pull/9451) +* [`box_default`] + [#9511](https://github.com/rust-lang/rust-clippy/pull/9511) +* [`implicit_saturating_add`] + [#9549](https://github.com/rust-lang/rust-clippy/pull/9549) +* [`as_ptr_cast_mut`] + [#9572](https://github.com/rust-lang/rust-clippy/pull/9572) +* [`disallowed_macros`] + [#9495](https://github.com/rust-lang/rust-clippy/pull/9495) +* [`partial_pub_fields`] + [#9658](https://github.com/rust-lang/rust-clippy/pull/9658) +* [`uninlined_format_args`] + [#9233](https://github.com/rust-lang/rust-clippy/pull/9233) +* [`cast_nan_to_int`] + [#9617](https://github.com/rust-lang/rust-clippy/pull/9617) + +### Moves and Deprecations + +* `positional_named_format_parameters` was uplifted to rustc under the new name + `named_arguments_used_positionally` + [#8518](https://github.com/rust-lang/rust-clippy/pull/8518) +* Moved [`implicit_saturating_sub`] to `style` (Now warn-by-default) + [#9584](https://github.com/rust-lang/rust-clippy/pull/9584) +* Moved `derive_partial_eq_without_eq` to `nursery` (now allow-by-default) + [#9536](https://github.com/rust-lang/rust-clippy/pull/9536) + +### Enhancements + +* [`nonstandard_macro_braces`]: Now includes `matches!()` in the default lint config + [#9471](https://github.com/rust-lang/rust-clippy/pull/9471) +* [`suboptimal_flops`]: Now supports multiplication and subtraction operations + [#9581](https://github.com/rust-lang/rust-clippy/pull/9581) +* [`arithmetic_side_effects`]: Now detects cases with literals behind references + [#9587](https://github.com/rust-lang/rust-clippy/pull/9587) +* [`upper_case_acronyms`]: Now also checks enum names + [#9580](https://github.com/rust-lang/rust-clippy/pull/9580) +* [`needless_borrowed_reference`]: Now lints nested patterns + [#9573](https://github.com/rust-lang/rust-clippy/pull/9573) +* [`unnecessary_cast`]: Now works for non-trivial non-literal expressions + [#9576](https://github.com/rust-lang/rust-clippy/pull/9576) +* [`arithmetic_side_effects`]: Now detects operations with custom types + [#9559](https://github.com/rust-lang/rust-clippy/pull/9559) +* [`disallowed_methods`], [`disallowed_types`]: Not correctly lints types, functions and macros + with the same path + [#9495](https://github.com/rust-lang/rust-clippy/pull/9495) +* [`self_named_module_files`], [`mod_module_files`]: Now take remapped path prefixes into account + [#9475](https://github.com/rust-lang/rust-clippy/pull/9475) +* [`bool_to_int_with_if`]: Now detects the inverse if case + [#9476](https://github.com/rust-lang/rust-clippy/pull/9476) + +### False Positive Fixes + +* [`arithmetic_side_effects`]: Now allows operations that can't overflow + [#9474](https://github.com/rust-lang/rust-clippy/pull/9474) +* [`unnecessary_lazy_evaluations`]: No longer lints in external macros + [#9486](https://github.com/rust-lang/rust-clippy/pull/9486) +* [`needless_borrow`], [`explicit_auto_deref`]: No longer lint on unions that require the reference + [#9490](https://github.com/rust-lang/rust-clippy/pull/9490) +* [`almost_complete_letter_range`]: No longer lints in external macros + [#9467](https://github.com/rust-lang/rust-clippy/pull/9467) +* [`drop_copy`]: No longer lints on idiomatic cases in match arms + [#9491](https://github.com/rust-lang/rust-clippy/pull/9491) +* [`question_mark`]: No longer lints in const context + [#9487](https://github.com/rust-lang/rust-clippy/pull/9487) +* [`collapsible_if`]: Suggestion now work in macros + [#9410](https://github.com/rust-lang/rust-clippy/pull/9410) +* [`std_instead_of_core`]: No longer triggers on unstable modules + [#9545](https://github.com/rust-lang/rust-clippy/pull/9545) +* [`unused_peekable`]: No longer lints, if the peak is done in a closure or function + [#9465](https://github.com/rust-lang/rust-clippy/pull/9465) +* [`useless_attribute`]: No longer lints on `#[allow]` attributes for [`unsafe_removed_from_name`] + [#9593](https://github.com/rust-lang/rust-clippy/pull/9593) +* [`unnecessary_lazy_evaluations`]: No longer suggest switching to early evaluation when type has + custom `Drop` implementation + [#9551](https://github.com/rust-lang/rust-clippy/pull/9551) +* [`unnecessary_cast`]: No longer lints on negative hexadecimal literals when cast as floats + [#9609](https://github.com/rust-lang/rust-clippy/pull/9609) +* [`use_self`]: No longer lints in proc macros + [#9454](https://github.com/rust-lang/rust-clippy/pull/9454) +* [`never_loop`]: Now takes `let ... else` statements into consideration. + [#9496](https://github.com/rust-lang/rust-clippy/pull/9496) +* [`default_numeric_fallback`]: Now ignores constants + [#9636](https://github.com/rust-lang/rust-clippy/pull/9636) +* [`uninit_vec`]: No longer lints `Vec::set_len(0)` + [#9519](https://github.com/rust-lang/rust-clippy/pull/9519) +* [`arithmetic_side_effects`]: Now ignores references + [9507](https://github.com/rust-lang/rust-clippy/pull/9507) +* [`large_stack_arrays`]: No longer lints inside static items + [#9466](https://github.com/rust-lang/rust-clippy/pull/9466) +* [`ref_option_ref`]: No longer lints if the inner reference is mutable + [#9684](https://github.com/rust-lang/rust-clippy/pull/9684) +* [`ptr_arg`]: No longer lints if the argument is used as an incomplete trait object + [#9645](https://github.com/rust-lang/rust-clippy/pull/9645) +* [`should_implement_trait`]: Now also works for `default` methods + [#9546](https://github.com/rust-lang/rust-clippy/pull/9546) + +### Suggestion Fixes/Improvements + +* [`derivable_impls`]: The suggestion is now machine applicable + [#9429](https://github.com/rust-lang/rust-clippy/pull/9429) +* [`match_single_binding`]: The suggestion now handles scrutinies with side effects better + [#9601](https://github.com/rust-lang/rust-clippy/pull/9601) +* [`zero_prefixed_literal`]: Only suggests using octal numbers, if this is possible + [#9652](https://github.com/rust-lang/rust-clippy/pull/9652) +* [`rc_buffer`]: The suggestion is no longer machine applicable to avoid semantic changes + [#9633](https://github.com/rust-lang/rust-clippy/pull/9633) +* [`print_literal`], [`write_literal`], [`uninlined_format_args`]: The suggestion now ignores + comments after the macro call. + [#9586](https://github.com/rust-lang/rust-clippy/pull/9586) +* [`expect_fun_call`]:Improved the suggestion for `format!` calls with captured variables + [#9586](https://github.com/rust-lang/rust-clippy/pull/9586) +* [`nonstandard_macro_braces`]: The suggestion is now machine applicable and will no longer + replace brackets inside the macro argument. + [#9499](https://github.com/rust-lang/rust-clippy/pull/9499) +* [`from_over_into`]: The suggestion is now a machine applicable and contains explanations + [#9649](https://github.com/rust-lang/rust-clippy/pull/9649) +* [`needless_return`]: The automatic suggestion now removes all required semicolons + [#9497](https://github.com/rust-lang/rust-clippy/pull/9497) +* [`to_string_in_format_args`]: The suggestion now keeps parenthesis around values + [#9590](https://github.com/rust-lang/rust-clippy/pull/9590) +* [`manual_assert`]: The suggestion now preserves comments + [#9479](https://github.com/rust-lang/rust-clippy/pull/9479) +* [`redundant_allocation`]: The suggestion applicability is now marked `MaybeIncorrect` to + avoid semantic changes + [#9634](https://github.com/rust-lang/rust-clippy/pull/9634) +* [`assertions_on_result_states`]: The suggestion has been corrected, for cases where the + `assert!` is not in a statement. + [#9453](https://github.com/rust-lang/rust-clippy/pull/9453) +* [`nonminimal_bool`]: The suggestion no longer expands macros + [#9457](https://github.com/rust-lang/rust-clippy/pull/9457) +* [`collapsible_match`]: Now specifies field names, when a struct is destructed + [#9685](https://github.com/rust-lang/rust-clippy/pull/9685) +* [`unnecessary_cast`]: The suggestion now adds parenthesis for negative numbers + [#9577](https://github.com/rust-lang/rust-clippy/pull/9577) +* [`redundant_closure`]: The suggestion now works for `impl FnMut` arguments + [#9556](https://github.com/rust-lang/rust-clippy/pull/9556) + +### ICE Fixes + +* [`unnecessary_to_owned`]: Avoid ICEs in favor of false negatives if information is missing + [#9505](https://github.com/rust-lang/rust-clippy/pull/9505) +* [`manual_range_contains`]: No longer ICEs on values behind references + [#9627](https://github.com/rust-lang/rust-clippy/pull/9627) +* [`needless_pass_by_value`]: No longer ICEs on unsized `dyn Fn` arguments + [#9531](https://github.com/rust-lang/rust-clippy/pull/9531) +* `*_interior_mutable_const` lints: no longer ICE on const unions containing `!Freeze` types + +### Others + +* Released `rustc_tools_util` for version information on `Crates.io`. (Further adjustments will + not be published as part of this changelog) ## Rust 1.65 -Current stable, released 2022-11-03 +Released 2022-11-03 [3c7e7dbc...b52fb523](https://github.com/rust-lang/rust-clippy/compare/3c7e7dbc...b52fb523) From f2d03667914528a672c476b3718b0a0ead0736da Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Tue, 13 Dec 2022 20:53:14 -0800 Subject: [PATCH 097/321] Use `expose_addr()` in `fmt::Pointer` --- library/core/src/fmt/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 48b6177434bc..7e76ef42adc8 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -2471,8 +2471,8 @@ impl Display for char { #[stable(feature = "rust1", since = "1.0.0")] impl Pointer for *const T { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - // Cast is needed here because `.addr()` requires `T: Sized`. - pointer_fmt_inner((*self as *const ()).addr(), f) + // Cast is needed here because `.expose_addr()` requires `T: Sized`. + pointer_fmt_inner((*self as *const ()).expose_addr(), f) } } From f9b56846ef42fa516c5fc53bce84835a5bc108a0 Mon Sep 17 00:00:00 2001 From: joboet Date: Wed, 14 Dec 2022 13:55:30 +0100 Subject: [PATCH 098/321] std: use a more efficient `Once` on platforms without threads --- library/std/src/sys/unsupported/mod.rs | 1 + library/std/src/sys/unsupported/once.rs | 89 +++++++++++++++++++ library/std/src/sys/wasi/mod.rs | 2 + library/std/src/sys/wasm/mod.rs | 2 + library/std/src/sys_common/once/mod.rs | 27 ++---- .../sys_common/once/{generic.rs => queue.rs} | 0 6 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 library/std/src/sys/unsupported/once.rs rename library/std/src/sys_common/once/{generic.rs => queue.rs} (100%) diff --git a/library/std/src/sys/unsupported/mod.rs b/library/std/src/sys/unsupported/mod.rs index 7bf6d40b76da..15b22c620d58 100644 --- a/library/std/src/sys/unsupported/mod.rs +++ b/library/std/src/sys/unsupported/mod.rs @@ -9,6 +9,7 @@ pub mod fs; pub mod io; pub mod locks; pub mod net; +pub mod once; pub mod os; #[path = "../unix/os_str.rs"] pub mod os_str; diff --git a/library/std/src/sys/unsupported/once.rs b/library/std/src/sys/unsupported/once.rs new file mode 100644 index 000000000000..b4bb4975f41c --- /dev/null +++ b/library/std/src/sys/unsupported/once.rs @@ -0,0 +1,89 @@ +use crate::cell::Cell; +use crate::sync as public; + +pub struct Once { + state: Cell, +} + +pub struct OnceState { + poisoned: bool, + set_state_to: Cell, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum State { + Incomplete, + Poisoned, + Running, + Complete, +} + +struct CompletionGuard<'a> { + state: &'a Cell, + set_state_on_drop_to: State, +} + +impl<'a> Drop for CompletionGuard<'a> { + fn drop(&mut self) { + self.state.set(self.set_state_on_drop_to); + } +} + +// Safety: threads are not supported on this platform. +unsafe impl Sync for Once {} + +impl Once { + #[inline] + #[rustc_const_stable(feature = "const_once_new", since = "1.32.0")] + pub const fn new() -> Once { + Once { state: Cell::new(State::Incomplete) } + } + + #[inline] + pub fn is_completed(&self) -> bool { + self.state.get() == State::Complete + } + + #[cold] + #[track_caller] + pub fn call(&self, ignore_poisoning: bool, f: &mut impl FnMut(&public::OnceState)) { + let state = self.state.get(); + match state { + State::Poisoned if !ignore_poisoning => { + // Panic to propagate the poison. + panic!("Once instance has previously been poisoned"); + } + State::Incomplete | State::Poisoned => { + self.state.set(State::Running); + // `guard` will set the new state on drop. + let mut guard = + CompletionGuard { state: &self.state, set_state_on_drop_to: State::Poisoned }; + // Run the function, letting it know if we're poisoned or not. + let f_state = public::OnceState { + inner: OnceState { + poisoned: state == State::Poisoned, + set_state_to: Cell::new(State::Complete), + }, + }; + f(&f_state); + guard.set_state_on_drop_to = f_state.inner.set_state_to.get(); + } + State::Running => { + panic!("one-time initialization may not be performed recursively"); + } + State::Complete => {} + } + } +} + +impl OnceState { + #[inline] + pub fn is_poisoned(&self) -> bool { + self.poisoned + } + + #[inline] + pub fn poison(&self) { + self.set_state_to.set(State::Poisoned) + } +} diff --git a/library/std/src/sys/wasi/mod.rs b/library/std/src/sys/wasi/mod.rs index c8c47763a340..1dc3f2b20266 100644 --- a/library/std/src/sys/wasi/mod.rs +++ b/library/std/src/sys/wasi/mod.rs @@ -32,6 +32,8 @@ pub mod io; #[path = "../unsupported/locks/mod.rs"] pub mod locks; pub mod net; +#[path = "../unsupported/once.rs"] +pub mod once; pub mod os; #[path = "../unix/os_str.rs"] pub mod os_str; diff --git a/library/std/src/sys/wasm/mod.rs b/library/std/src/sys/wasm/mod.rs index d68c3e5f1dfb..77ebe3c4ac6f 100644 --- a/library/std/src/sys/wasm/mod.rs +++ b/library/std/src/sys/wasm/mod.rs @@ -66,6 +66,8 @@ cfg_if::cfg_if! { } else { #[path = "../unsupported/locks/mod.rs"] pub mod locks; + #[path = "../unsupported/once.rs"] + pub mod once; #[path = "../unsupported/thread.rs"] pub mod thread; } diff --git a/library/std/src/sys_common/once/mod.rs b/library/std/src/sys_common/once/mod.rs index 8742e68cc7ac..359697d83131 100644 --- a/library/std/src/sys_common/once/mod.rs +++ b/library/std/src/sys_common/once/mod.rs @@ -6,22 +6,6 @@ // As a result, we end up implementing it ourselves in the standard library. // This also gives us the opportunity to optimize the implementation a bit which // should help the fast path on call sites. -// -// So to recap, the guarantees of a Once are that it will call the -// initialization closure at most once, and it will never return until the one -// that's running has finished running. This means that we need some form of -// blocking here while the custom callback is running at the very least. -// Additionally, we add on the restriction of **poisoning**. Whenever an -// initialization closure panics, the Once enters a "poisoned" state which means -// that all future calls will immediately panic as well. -// -// So to implement this, one might first reach for a `Mutex`, but those cannot -// be put into a `static`. It also gets a lot harder with poisoning to figure -// out when the mutex needs to be deallocated because it's not after the closure -// finishes, but after the first successful closure finishes. -// -// All in all, this is instead implemented with atomics and lock-free -// operations! Whee! cfg_if::cfg_if! { if #[cfg(any( @@ -36,8 +20,15 @@ cfg_if::cfg_if! { ))] { mod futex; pub use futex::{Once, OnceState}; + } else if #[cfg(any( + windows, + target_family = "unix", + all(target_vendor = "fortanix", target_env = "sgx"), + target_os = "solid_asp3", + ))] { + mod queue; + pub use queue::{Once, OnceState}; } else { - mod generic; - pub use generic::{Once, OnceState}; + pub use crate::sys::once::{Once, OnceState}; } } diff --git a/library/std/src/sys_common/once/generic.rs b/library/std/src/sys_common/once/queue.rs similarity index 100% rename from library/std/src/sys_common/once/generic.rs rename to library/std/src/sys_common/once/queue.rs From 1550a2506d1ad6e3810aa806a6fee59ff4aa6074 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 14 Dec 2022 09:54:55 -0500 Subject: [PATCH 099/321] fs/tests: Explicitly kill the zombie rather than sleeping until it dies --- library/std/src/fs/tests.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs index 6d42adca5314..5c6a16c4bb16 100644 --- a/library/std/src/fs/tests.rs +++ b/library/std/src/fs/tests.rs @@ -1572,16 +1572,18 @@ fn test_eq_direntry_metadata() { #[test] #[cfg(target_os = "linux")] fn test_read_dir_infinite_loop() { + use crate::io::ErrorKind; use crate::process::Command; - use crate::thread::sleep; - use crate::time::Duration; - // Create a child process - let Ok(child) = Command::new("echo").spawn() else { return }; + // Create a zombie child process + let Ok(mut child) = Command::new("echo").spawn() else { return }; - // Wait for it to (probably) become a zombie. We can't use wait() because - // that will reap the process. - sleep(Duration::from_millis(10)); + // Make sure the process is (un)dead + match child.kill() { + // InvalidInput means the child already exited + Err(e) if e.kind() != ErrorKind::InvalidInput => return, + _ => {} + } // open() on this path will succeed, but readdir() will fail let id = child.id(); From 9fb7c5ae5e66703240ce4242f55b66a066e0a93c Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 14 Dec 2022 10:01:42 -0500 Subject: [PATCH 100/321] fs/tests: Fail fast on duplicate errors rather than looping indefinitely --- library/std/src/fs/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs index 5c6a16c4bb16..b385ebde4397 100644 --- a/library/std/src/fs/tests.rs +++ b/library/std/src/fs/tests.rs @@ -1592,6 +1592,6 @@ fn test_read_dir_infinite_loop() { // Skip the test if we can't open the directory in the first place let Ok(dir) = fs::read_dir(path) else { return }; - // Iterate through the directory - for _ in dir {} + // Check for duplicate errors + assert!(dir.filter(|e| e.is_err()).take(2).count() < 2); } From 65069d5c5b6db1bdf74e4f9243db7ef82d6a3776 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 13 Dec 2022 11:07:42 +0000 Subject: [PATCH 101/321] Ensure no one constructs `AliasTy`s themselves --- clippy_lints/src/future_not_send.rs | 2 +- clippy_utils/src/ty.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/future_not_send.rs b/clippy_lints/src/future_not_send.rs index fcdac90fc237..989f83cf80d5 100644 --- a/clippy_lints/src/future_not_send.rs +++ b/clippy_lints/src/future_not_send.rs @@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { return; } let ret_ty = return_ty(cx, hir_id); - if let ty::Alias(ty::Opaque, AliasTy { def_id, substs }) = *ret_ty.kind() { + if let ty::Alias(ty::Opaque, AliasTy { def_id, substs, .. }) = *ret_ty.kind() { let preds = cx.tcx.explicit_item_bounds(def_id); let mut is_future = false; for &(p, _span) in preds { diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 33f3b3af3dc0..a6bcb134d408 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -79,7 +79,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' return true; } - if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }) = *inner_ty.kind() { + if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *inner_ty.kind() { for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { match predicate.kind().skip_binder() { // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through @@ -250,7 +250,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { is_must_use_ty(cx, *ty) }, ty::Tuple(substs) => substs.iter().any(|ty| is_must_use_ty(cx, ty)), - ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }) => { + ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => { for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) { if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() { if cx.tcx.has_attr(trait_predicate.trait_ref.def_id, sym::must_use) { @@ -631,7 +631,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option Some(ExprFnSig::Sig(cx.tcx.bound_fn_sig(id).subst(cx.tcx, subs), Some(id))), - ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(def_id), cx.tcx.opt_parent(def_id)), + ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => sig_from_bounds(cx, ty, cx.tcx.item_bounds(def_id), cx.tcx.opt_parent(def_id)), ty::FnPtr(sig) => Some(ExprFnSig::Sig(sig, None)), ty::Dynamic(bounds, _, _) => { let lang_items = cx.tcx.lang_items(); @@ -1039,10 +1039,10 @@ pub fn make_projection<'tcx>( } } - Some(AliasTy { + Some(tcx.mk_alias_ty( + assoc_item.def_id, substs, - def_id: assoc_item.def_id, - }) + )) } helper( tcx, From fa87abf963d2fe8c1c24ef68ee764f9faeda0d47 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 13 Dec 2022 11:25:31 +0000 Subject: [PATCH 102/321] Remove TraitRef::new --- clippy_lints/src/derive.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index 9e596ca8157e..3f0b165f2b60 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -15,7 +15,7 @@ use rustc_middle::hir::nested_filter; use rustc_middle::traits::Reveal; use rustc_middle::ty::{ self, Binder, BoundConstness, Clause, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate, - TraitRef, Ty, TyCtxt, + Ty, TyCtxt, }; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; @@ -513,9 +513,9 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> tcx.mk_predicates(ty_predicates.iter().map(|&(p, _)| p).chain( params.iter().filter(|&&(_, needs_eq)| needs_eq).map(|&(param, _)| { tcx.mk_predicate(Binder::dummy(PredicateKind::Clause(Clause::Trait(TraitPredicate { - trait_ref: TraitRef::new( + trait_ref: tcx.mk_trait_ref( eq_trait_id, - tcx.mk_substs(std::iter::once(tcx.mk_param_from_def(param))), + [tcx.mk_param_from_def(param)], ), constness: BoundConstness::NotConst, polarity: ImplPolarity::Positive, From d0db3279ab251db3612204cc1e3f386e8beae996 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 14 Dec 2022 18:00:56 +0000 Subject: [PATCH 103/321] Don't bug if we're trying to cast dyn* to a nother type --- compiler/rustc_hir_typeck/src/cast.rs | 4 +++- src/test/ui/dyn-star/dyn-to-rigid.rs | 11 +++++++++++ src/test/ui/dyn-star/dyn-to-rigid.stderr | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/dyn-star/dyn-to-rigid.rs create mode 100644 src/test/ui/dyn-star/dyn-to-rigid.stderr diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index b050ad20afbd..042a50f2fd42 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -847,13 +847,15 @@ impl<'a, 'tcx> CastCheck<'tcx> { (Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast), - (_, DynStar) | (DynStar, _) => { + (_, DynStar) => { if fcx.tcx.features().dyn_star { bug!("should be handled by `try_coerce`") } else { Err(CastError::IllegalCast) } } + + (DynStar, _) => Err(CastError::IllegalCast), } } diff --git a/src/test/ui/dyn-star/dyn-to-rigid.rs b/src/test/ui/dyn-star/dyn-to-rigid.rs new file mode 100644 index 000000000000..e80ee15902ee --- /dev/null +++ b/src/test/ui/dyn-star/dyn-to-rigid.rs @@ -0,0 +1,11 @@ +#![feature(dyn_star)] +#![allow(incomplete_features)] + +trait Tr {} + +fn f(x: dyn* Tr) -> usize { + x as usize + //~^ ERROR casting `(dyn* Tr + 'static)` as `usize` is invalid +} + +fn main() {} diff --git a/src/test/ui/dyn-star/dyn-to-rigid.stderr b/src/test/ui/dyn-star/dyn-to-rigid.stderr new file mode 100644 index 000000000000..588e6d97e5ca --- /dev/null +++ b/src/test/ui/dyn-star/dyn-to-rigid.stderr @@ -0,0 +1,9 @@ +error[E0606]: casting `(dyn* Tr + 'static)` as `usize` is invalid + --> $DIR/dyn-to-rigid.rs:7:5 + | +LL | x as usize + | ^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0606`. From bcaf210575f8b8ca4ea7fd37ef2ef40e7c263ad9 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 14 Dec 2022 18:24:21 +0000 Subject: [PATCH 104/321] bail in collect_trait_impl_trait_tys if signatures reference errors --- .../src/check/compare_method.rs | 2 ++ .../ui/async-await/in-trait/bad-signatures.rs | 16 ++++++++++++ .../in-trait/bad-signatures.stderr | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/test/ui/async-await/in-trait/bad-signatures.rs create mode 100644 src/test/ui/async-await/in-trait/bad-signatures.stderr diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index ba7d31cea2e2..13bd034a7b55 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -373,6 +373,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>( tcx.fn_sig(impl_m.def_id), ), ); + impl_sig.error_reported()?; let impl_return_ty = impl_sig.output(); // Normalize the trait signature with liberated bound vars, passing it through @@ -387,6 +388,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>( ) .fold_with(&mut collector); let trait_sig = ocx.normalize(&norm_cause, param_env, unnormalized_trait_sig); + trait_sig.error_reported()?; let trait_return_ty = trait_sig.output(); let wf_tys = FxIndexSet::from_iter( diff --git a/src/test/ui/async-await/in-trait/bad-signatures.rs b/src/test/ui/async-await/in-trait/bad-signatures.rs new file mode 100644 index 000000000000..b86f1d1c1358 --- /dev/null +++ b/src/test/ui/async-await/in-trait/bad-signatures.rs @@ -0,0 +1,16 @@ +// edition:2021 + +#![feature(async_fn_in_trait)] +//~^ WARN the feature `async_fn_in_trait` is incomplete + +trait MyTrait { + async fn bar(&abc self); + //~^ ERROR expected identifier, found keyword `self` + //~| ERROR expected one of `:`, `@`, or `|`, found keyword `self` +} + +impl MyTrait for () { + async fn bar(&self) {} +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/bad-signatures.stderr b/src/test/ui/async-await/in-trait/bad-signatures.stderr new file mode 100644 index 000000000000..e0ba7b53ec41 --- /dev/null +++ b/src/test/ui/async-await/in-trait/bad-signatures.stderr @@ -0,0 +1,26 @@ +error: expected identifier, found keyword `self` + --> $DIR/bad-signatures.rs:7:23 + | +LL | async fn bar(&abc self); + | ^^^^ expected identifier, found keyword + +error: expected one of `:`, `@`, or `|`, found keyword `self` + --> $DIR/bad-signatures.rs:7:23 + | +LL | async fn bar(&abc self); + | -----^^^^ + | | | + | | expected one of `:`, `@`, or `|` + | help: declare the type after the parameter binding: `: ` + +warning: the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/bad-signatures.rs:3:12 + | +LL | #![feature(async_fn_in_trait)] + | ^^^^^^^^^^^^^^^^^ + | + = note: see issue #91611 for more information + = note: `#[warn(incomplete_features)]` on by default + +error: aborting due to 2 previous errors; 1 warning emitted + From c99f1b7413e8abccc07197838aafda6ce3788e26 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 14 Dec 2022 12:46:47 -0600 Subject: [PATCH 105/321] Run `x test tidy` sooner in mingw-check It takes less time to run than the other tests and is more likely to fail. `expand-yaml-anchors` is still run first to make sure the CI files are internally consistent. --- src/ci/docker/host-x86_64/mingw-check/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ci/docker/host-x86_64/mingw-check/Dockerfile b/src/ci/docker/host-x86_64/mingw-check/Dockerfile index 40caa7c50135..720c5c493948 100644 --- a/src/ci/docker/host-x86_64/mingw-check/Dockerfile +++ b/src/ci/docker/host-x86_64/mingw-check/Dockerfile @@ -40,10 +40,10 @@ COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/ ENV RUN_CHECK_WITH_PARALLEL_QUERIES 1 ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \ + python3 ../x.py test --stage 0 src/tools/tidy && \ python3 ../x.py check --target=i686-pc-windows-gnu --host=i686-pc-windows-gnu --all-targets && \ python3 ../x.py build --stage 0 src/tools/build-manifest && \ python3 ../x.py test --stage 0 src/tools/compiletest && \ - python3 ../x.py test --stage 2 src/tools/tidy && \ python3 ../x.py test --stage 0 core alloc std test proc_macro && \ # Build both public and internal documentation. RUSTDOCFLAGS="--document-private-items" python3 ../x.py doc --stage 0 library/test && \ From 71019aa07613724f1421edc4ead49b04728a7959 Mon Sep 17 00:00:00 2001 From: Fridtjof Stoldt Date: Wed, 14 Dec 2022 08:22:17 +0100 Subject: [PATCH 106/321] Address review comments <3 Co-authored-by: Takayuki Nakata Co-authored-by: Alex Macleod --- CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc3f785bb37e..f0a9f8c77a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ document. ## Rust 1.66 -Current stable, released 2022-11-03 +Current stable, released 2022-12-15 [b52fb523...4f142aa1](https://github.com/rust-lang/rust-clippy/compare/b52fb523...4f142aa1) @@ -110,8 +110,8 @@ Current stable, released 2022-11-03 [#9636](https://github.com/rust-lang/rust-clippy/pull/9636) * [`uninit_vec`]: No longer lints `Vec::set_len(0)` [#9519](https://github.com/rust-lang/rust-clippy/pull/9519) -* [`arithmetic_side_effects`]: Now ignores references - [9507](https://github.com/rust-lang/rust-clippy/pull/9507) +* [`arithmetic_side_effects`]: Now ignores references to integer types + [#9507](https://github.com/rust-lang/rust-clippy/pull/9507) * [`large_stack_arrays`]: No longer lints inside static items [#9466](https://github.com/rust-lang/rust-clippy/pull/9466) * [`ref_option_ref`]: No longer lints if the inner reference is mutable @@ -171,6 +171,7 @@ Current stable, released 2022-11-03 * [`needless_pass_by_value`]: No longer ICEs on unsized `dyn Fn` arguments [#9531](https://github.com/rust-lang/rust-clippy/pull/9531) * `*_interior_mutable_const` lints: no longer ICE on const unions containing `!Freeze` types + [#9539](https://github.com/rust-lang/rust-clippy/pull/9539) ### Others From ff41359e6535ca7dc1261f282b1601136fedee21 Mon Sep 17 00:00:00 2001 From: b-naber Date: Fri, 9 Dec 2022 18:08:56 +0100 Subject: [PATCH 107/321] address review --- compiler/rustc_middle/src/mir/syntax.rs | 9 +- .../src/build/expr/as_place.rs | 135 +++++++----------- .../src/build/expr/as_rvalue.rs | 4 +- .../rustc_mir_build/src/build/matches/util.rs | 5 +- 4 files changed, 61 insertions(+), 92 deletions(-) diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 004dd004c723..b7a5f9c8757b 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -875,6 +875,13 @@ pub struct Place<'tcx> { pub projection: &'tcx List>, } +/// The different kinds of projections that can be used in the projection of a `Place`. +/// +/// `T1` is the generic type for a field projection. For an actual projection on a `Place` +/// this parameter will always be `Ty`, but the field type can be unavailable when +/// building (by using `PlaceBuilder`) places that correspond to upvars. +/// `T2` is the generic type for an `OpaqueCast` (is generic since it's abstracted over +/// in dataflow analysis, see `AbstractElem`). #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)] pub enum ProjectionElem { @@ -942,7 +949,7 @@ pub enum ProjectionElem { /// and the index is a local. pub type PlaceElem<'tcx> = ProjectionElem, Ty<'tcx>>; -/// Alias for projections that appear in `PlaceBuilder::UpVar`, for which +/// Alias for projections that appear in `PlaceBuilder::Upvar`, for which /// we cannot provide any field types. pub type UpvarProjectionElem<'tcx> = ProjectionElem>; diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 3f52334b7247..8a35478dd8b3 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -33,7 +33,7 @@ pub(in crate::build) enum PlaceBuilder<'tcx> { /// Denotes the start of a `Place`. /// /// We use `PlaceElem` since this has all `Field` types available. - Local(Local, Vec>), + Local { local: Local, projection: Vec> }, /// When building place for an expression within a closure, the place might start off a /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture @@ -67,11 +67,11 @@ pub(in crate::build) enum PlaceBuilder<'tcx> { /// /// Note: in contrast to `PlaceBuilder::Local` we have not yet determined all `Field` types /// and will only do so once converting to `PlaceBuilder::Local`. - UpVar(UpVar, Vec>), + Upvar { upvar: Upvar, projection: Vec> }, } #[derive(Copy, Clone, Debug, PartialEq)] -pub(crate) struct UpVar { +pub(crate) struct Upvar { var_hir_id: LocalVarId, closure_def_id: LocalDefId, } @@ -222,36 +222,7 @@ fn to_upvars_resolved_place_builder<'tcx>( upvar_projection, ); - debug_assert!({ - let builder = upvar_resolved_place_builder.clone(); - let mut valid_conversion = true; - match builder { - PlaceBuilder::Local(_, projections) => { - for proj in projections.iter() { - match proj { - ProjectionElem::Field(_, field_ty) => { - if matches!(field_ty.kind(), ty::Infer(..)) { - debug!( - "field ty should have been converted for projection {:?} in PlaceBuilder {:?}", - proj, - upvar_resolved_place_builder.clone() - ); - - valid_conversion = false; - break; - } - } - _ => {} - } - } - } - PlaceBuilder::UpVar(..) => { - unreachable!() - } - } - - valid_conversion - }); + assert!(matches!(upvar_resolved_place_builder, PlaceBuilder::Local { .. })); Some(upvar_resolved_place_builder) } @@ -269,9 +240,9 @@ fn strip_prefix<'a, 'tcx>( ) -> impl Iterator> + 'a { let mut iter = projections .iter() + .copied() // Filter out opaque casts, they are unnecessary in the prefix. - .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))) - .map(|elem| *elem); + .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))); for projection in prefix_projections { debug!(?projection, ?projection.ty); @@ -305,8 +276,8 @@ impl<'tcx> PlaceBuilder<'tcx> { pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option> { let resolved = self.resolve_upvar(cx); let builder = resolved.as_ref().unwrap_or(self); - let PlaceBuilder::Local(local, projection) = builder else { return None }; - let projection = cx.tcx.intern_place_elems(&projection); + let PlaceBuilder::Local{local, ref projection} = builder else { return None }; + let projection = cx.tcx.intern_place_elems(projection); Some(Place { local: *local, projection }) } @@ -324,40 +295,31 @@ impl<'tcx> PlaceBuilder<'tcx> { &self, cx: &Builder<'_, 'tcx>, ) -> Option> { - let PlaceBuilder::UpVar( UpVar {var_hir_id, closure_def_id }, projection) = self else { + let PlaceBuilder::Upvar{ upvar: Upvar {var_hir_id, closure_def_id }, projection} = self else { return None; }; to_upvars_resolved_place_builder(cx, *var_hir_id, *closure_def_id, &projection) } - pub(crate) fn get_local_projection(&self) -> &[PlaceElem<'tcx>] { - match self { - Self::Local(_, projection) => projection, - Self::UpVar(..) => { - bug!("get_local_projection_mut can only be called on PlaceBuilder::Local") - } - } - } - #[instrument(skip(cx), level = "debug")] pub(crate) fn field(self, cx: &Builder<'_, 'tcx>, f: Field) -> Self { - let field_ty = match self.clone() { - PlaceBuilder::Local(local, projection) => { - let base_place = PlaceBuilder::Local(local, projection); + match self.clone() { + PlaceBuilder::Local { local, projection } => { + let base_place = PlaceBuilder::Local { local, projection }; let PlaceTy { ty, variant_index } = base_place.to_place(cx).ty(&cx.local_decls, cx.tcx); let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty); - PlaceBuilder::compute_field_ty(cx, f, base_ty, variant_index) - } - PlaceBuilder::UpVar(..) => { - let dummy_ty = cx.tcx.mk_ty_infer(ty::FreshTy(0)); - dummy_ty - } - }; + let field_ty = PlaceBuilder::compute_field_ty(cx, f, base_ty, variant_index); - self.project(ProjectionElem::Field(f, field_ty)) + self.project(ProjectionElem::Field(f, field_ty)) + } + PlaceBuilder::Upvar { upvar, mut projection } => { + projection.push(ProjectionElem::Field(f, ())); + PlaceBuilder::Upvar { upvar, projection } + } + } } pub(crate) fn deref(self) -> Self { @@ -375,13 +337,13 @@ impl<'tcx> PlaceBuilder<'tcx> { #[instrument(level = "debug")] pub(crate) fn project(self, elem: PlaceElem<'tcx>) -> Self { let result = match self { - PlaceBuilder::Local(local, mut proj) => { - proj.push(elem); - PlaceBuilder::Local(local, proj) + PlaceBuilder::Local { local, mut projection } => { + projection.push(elem); + PlaceBuilder::Local { local, projection } } - PlaceBuilder::UpVar(upvar, mut proj) => { - proj.push(elem.into()); - PlaceBuilder::UpVar(upvar, proj) + PlaceBuilder::Upvar { upvar, mut projection } => { + projection.push(elem.into()); + PlaceBuilder::Upvar { upvar, projection } } }; @@ -392,14 +354,14 @@ impl<'tcx> PlaceBuilder<'tcx> { /// Same as `.clone().project(..)` but more efficient pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self { match self { - PlaceBuilder::Local(local, proj) => PlaceBuilder::Local( - *local, - Vec::from_iter(proj.iter().copied().chain([elem.into()])), - ), - PlaceBuilder::UpVar(upvar, proj) => PlaceBuilder::UpVar( - *upvar, - Vec::from_iter(proj.iter().copied().chain([elem.into()])), - ), + PlaceBuilder::Local { local, projection } => PlaceBuilder::Local { + local: *local, + projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])), + }, + PlaceBuilder::Upvar { upvar, projection } => PlaceBuilder::Upvar { + upvar: *upvar, + projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])), + }, } } @@ -463,7 +425,11 @@ impl<'tcx> PlaceBuilder<'tcx> { f_ty } else { let Some(f_ty) = substs.as_generator().prefix_tys().nth(field.index()) else { - bug!("expected to take index {:?} in {:?}", field.index(), substs.as_generator().prefix_tys().collect::>()); + bug!( + "expected to take index {:?} in {:?}", + field.index(), + substs.as_generator().prefix_tys().collect::>() + ); }; f_ty @@ -475,7 +441,7 @@ impl<'tcx> PlaceBuilder<'tcx> { cx.tcx.normalize_erasing_regions(cx.param_env, field_ty) } - /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::UpVar` whose upvars + /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::Upvar` whose upvars /// are resolved. This function takes two kinds of projections: `local_projection` /// contains the projections of the captured upvar and `upvar_projection` the /// projections that are applied to the captured upvar. The main purpose of this @@ -516,19 +482,19 @@ impl<'tcx> PlaceBuilder<'tcx> { } } - PlaceBuilder::Local(local, local_projection) + PlaceBuilder::Local { local, projection: local_projection } } } impl<'tcx> From for PlaceBuilder<'tcx> { fn from(local: Local) -> Self { - Self::Local(local, Vec::new()) + Self::Local { local, projection: Vec::new() } } } impl<'tcx> From> for PlaceBuilder<'tcx> { fn from(p: Place<'tcx>) -> Self { - Self::Local(p.local, p.projection.to_vec()) + Self::Local { local: p.local, projection: p.projection.to_vec() } } } @@ -564,13 +530,7 @@ fn project_ty<'tcx>( (ty, None) } ProjectionElem::Downcast(_, variant_idx) => (ty, Some(variant_idx)), - ProjectionElem::Field(_, ty) => { - if matches!(ty.kind(), ty::Infer(..)) { - bug!("Field ty should have been resolved"); - } - - (ty, None) - } + ProjectionElem::Field(_, ty) => (ty, None), ProjectionElem::OpaqueCast(..) => bug!("didn't expect OpaqueCast"), } } @@ -836,7 +796,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } /// Lower a captured upvar. Note we might not know the actual capture index, - /// so we create a place starting from `UpVar`, which will be resolved + /// so we create a place starting from `Upvar`, which will be resolved /// once all projections that allow us to identify a capture have been applied. fn lower_captured_upvar( &mut self, @@ -844,7 +804,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { closure_def_id: LocalDefId, var_hir_id: LocalVarId, ) -> BlockAnd> { - block.and(PlaceBuilder::UpVar(UpVar { var_hir_id, closure_def_id }, vec![])) + block.and(PlaceBuilder::Upvar { + upvar: Upvar { var_hir_id, closure_def_id }, + projection: vec![], + }) } /// Lower an index expression diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 618aa9e31095..b3fd054a00a5 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -654,11 +654,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // We are capturing a path that starts off a local variable in the parent. // The mutability of the current capture is same as the mutability // of the local declaration in the parent. - PlaceBuilder::Local(local, _) => this.local_decls[local].mutability, + PlaceBuilder::Local { local, .. } => this.local_decls[local].mutability, // Parent is a closure and we are capturing a path that is captured // by the parent itself. The mutability of the current capture // is same as that of the capture in the parent closure. - PlaceBuilder::UpVar(..) => { + PlaceBuilder::Upvar { .. } => { let enclosing_upvars_resolved = arg_place_builder.to_place(this); match enclosing_upvars_resolved.as_ref() { diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index 27a7b03465f6..e48c6b2457be 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -108,9 +108,8 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> { // Only add the OpaqueCast projection if the given place is an opaque type and the // expected type from the pattern is not. let may_need_cast = match place { - PlaceBuilder::Local(local, _) => { - let ty = - Place::ty_from(local, place.get_local_projection(), &cx.local_decls, cx.tcx).ty; + PlaceBuilder::Local { local, ref projection } => { + let ty = Place::ty_from(local, projection, &cx.local_decls, cx.tcx).ty; ty != pattern.ty && ty.has_opaque_types() } _ => true, From b134d1108ffc92bfb5081d31e5e41141f918e942 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Wed, 10 Aug 2022 18:19:07 -0400 Subject: [PATCH 108/321] Implement `From` for f32, f64 --- library/core/src/convert/num.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index 9c0d7e9a1e89..45e2f711c6c9 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -168,6 +168,26 @@ impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0" // Float -> Float impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] } +// bool -> Float +#[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")] +#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")] +impl const From for f32 { + /// Converts `bool` to `f32` losslessly. + #[inline] + fn from(small: bool) -> Self { + small as u8 as Self + } +} +#[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")] +#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")] +impl const From for f64 { + /// Converts `bool` to `f64` losslessly. + #[inline] + fn from(small: bool) -> Self { + small as u8 as Self + } +} + // no possible bounds violation macro_rules! try_from_unbounded { ($source:ty, $($target:ty),*) => {$( From e723fc4f564efa916beeaa59de3c55ccd9006a64 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 15 Dec 2022 15:13:19 +1100 Subject: [PATCH 109/321] Merge `SimplifiedTypeGen` into `SimplifiedType`. `SimplifiedTypeGen` is the only instantiation used, so we don't need the generic parameter. --- clippy_utils/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 90192f46cbfa..652f8b4d3c56 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -97,7 +97,7 @@ use rustc_middle::hir::place::PlaceBase; use rustc_middle::ty as rustc_ty; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow}; use rustc_middle::ty::binding::BindingMode; -use rustc_middle::ty::fast_reject::SimplifiedTypeGen::{ +use rustc_middle::ty::fast_reject::SimplifiedType::{ ArraySimplifiedType, BoolSimplifiedType, CharSimplifiedType, FloatSimplifiedType, IntSimplifiedType, PtrSimplifiedType, SliceSimplifiedType, StrSimplifiedType, UintSimplifiedType, }; From 8ba24aedd971b8b76aa559e266fb6d0d9a3a686f Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 15 Dec 2022 15:04:23 +0900 Subject: [PATCH 110/321] Add regression test for #55976 Signed-off-by: Yuki Okushi --- src/test/ui/codegen/issue-55976.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/ui/codegen/issue-55976.rs diff --git a/src/test/ui/codegen/issue-55976.rs b/src/test/ui/codegen/issue-55976.rs new file mode 100644 index 000000000000..3142704b78cf --- /dev/null +++ b/src/test/ui/codegen/issue-55976.rs @@ -0,0 +1,13 @@ +// run-pass +// ^-- The above is needed as this issue is related to LLVM/codegen. +// min-llvm-version:15.0.0 +// ^-- The above is needed as this issue is fixed by the opaque pointers. + +fn main() { + type_error(|x| &x); +} + +fn type_error( + _selector: for<'a> fn(&'a Vec Fn(&'b u8)>>) -> &'a Vec>, +) { +} From 5da1a04278ffddfb9252ee4a0eb9b1f19738673d Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Thu, 15 Dec 2022 00:51:34 +0000 Subject: [PATCH 111/321] Allow `impl ~const Trait` opaque types --- .../rustc_ast_passes/src/ast_validation.rs | 8 +---- .../const-impl-trait.rs | 34 ++++++++++++++++++ .../tilde-const-invalid-places.rs | 17 --------- .../tilde-const-invalid-places.stderr | 36 ++----------------- 4 files changed, 37 insertions(+), 58 deletions(-) create mode 100644 src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index eb9c841d80c1..55ea12d25ea2 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -42,7 +42,6 @@ enum SelfSemantic { /// What is the context that prevents using `~const`? enum DisallowTildeConstContext<'a> { TraitObject, - ImplTrait, Fn(FnKind<'a>), } @@ -187,11 +186,7 @@ impl<'a> AstValidator<'a> { fn with_impl_trait(&mut self, outer: Option, f: impl FnOnce(&mut Self)) { let old = mem::replace(&mut self.outer_impl_trait, outer); - if outer.is_some() { - self.with_banned_tilde_const(DisallowTildeConstContext::ImplTrait, f); - } else { - f(self); - } + f(self); self.outer_impl_trait = old; } @@ -1384,7 +1379,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> { let mut err = self.err_handler().struct_span_err(bound.span(), "`~const` is not allowed here"); match reason { DisallowTildeConstContext::TraitObject => err.note("trait objects cannot have `~const` trait bounds"), - DisallowTildeConstContext::ImplTrait => err.note("`impl Trait`s cannot have `~const` trait bounds"), DisallowTildeConstContext::Fn(FnKind::Closure(..)) => err.note("closures cannot have `~const` trait bounds"), DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => err.span_note(ident.span, "this function is not `const`, so it cannot have `~const` trait bounds"), }; diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs b/src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs new file mode 100644 index 000000000000..2059f6d79a57 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs @@ -0,0 +1,34 @@ +// check-pass +#![feature(associated_type_bounds, const_trait_impl, const_cmp)] + +use std::marker::Destruct; + +const fn cmp(a: &impl ~const PartialEq) -> bool { + a == a +} + +const fn wrap(x: impl ~const PartialEq + ~const Destruct) -> impl ~const PartialEq + ~const Destruct { + x +} + +const _: () = { + assert!(cmp(&0xDEADBEEFu32)); + assert!(cmp(&())); + assert!(wrap(123) == wrap(123)); + assert!(wrap(123) != wrap(456)); +}; + +#[const_trait] +trait T {} +struct S; +impl const T for S {} + +const fn rpit() -> impl ~const T { S } + +const fn apit(_: impl ~const T + ~const Destruct) {} + +const fn rpit_assoc_bound() -> impl IntoIterator { Some(S) } + +const fn apit_assoc_bound(_: impl IntoIterator + ~const Destruct) {} + +fn main() {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs b/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs index 5bd52151f42a..95f7aaba0fc3 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs @@ -1,23 +1,6 @@ #![feature(const_trait_impl)] #![feature(associated_type_bounds)] -#[const_trait] -trait T {} -struct S; -impl T for S {} - -fn rpit() -> impl ~const T { S } -//~^ ERROR `~const` is not allowed - -fn apit(_: impl ~const T) {} -//~^ ERROR `~const` is not allowed - -fn rpit_assoc_bound() -> impl IntoIterator { Some(S) } -//~^ ERROR `~const` is not allowed - -fn apit_assoc_bound(_: impl IntoIterator) {} -//~^ ERROR `~const` is not allowed - struct TildeQuestion(std::marker::PhantomData); //~^ ERROR `~const` and `?` are mutually exclusive diff --git a/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr b/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr index 84867cb4a534..d20f146df3f1 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/tilde-const-invalid-places.stderr @@ -1,40 +1,8 @@ -error: `~const` is not allowed here - --> $DIR/tilde-const-invalid-places.rs:9:19 - | -LL | fn rpit() -> impl ~const T { S } - | ^^^^^^^^ - | - = note: `impl Trait`s cannot have `~const` trait bounds - -error: `~const` is not allowed here - --> $DIR/tilde-const-invalid-places.rs:12:17 - | -LL | fn apit(_: impl ~const T) {} - | ^^^^^^^^ - | - = note: `impl Trait`s cannot have `~const` trait bounds - -error: `~const` is not allowed here - --> $DIR/tilde-const-invalid-places.rs:15:50 - | -LL | fn rpit_assoc_bound() -> impl IntoIterator { Some(S) } - | ^^^^^^^^ - | - = note: `impl Trait`s cannot have `~const` trait bounds - -error: `~const` is not allowed here - --> $DIR/tilde-const-invalid-places.rs:18:48 - | -LL | fn apit_assoc_bound(_: impl IntoIterator) {} - | ^^^^^^^^ - | - = note: `impl Trait`s cannot have `~const` trait bounds - error: `~const` and `?` are mutually exclusive - --> $DIR/tilde-const-invalid-places.rs:21:25 + --> $DIR/tilde-const-invalid-places.rs:4:25 | LL | struct TildeQuestion(std::marker::PhantomData); | ^^^^^^^^^^^^^ -error: aborting due to 5 previous errors +error: aborting due to previous error From 004b885c0afe34d6f8e6649f1faa8bbef656172c Mon Sep 17 00:00:00 2001 From: xFrednet Date: Thu, 15 Dec 2022 12:42:08 +0100 Subject: [PATCH 112/321] rustc_tools_util: changelog and 0.3.0 release --- Cargo.toml | 4 ++-- rustc_tools_util/CHANGELOG.md | 6 ++++++ rustc_tools_util/Cargo.toml | 2 +- rustc_tools_util/README.md | 7 +++++-- rustc_tools_util/src/lib.rs | 12 +++++------- 5 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 rustc_tools_util/CHANGELOG.md diff --git a/Cargo.toml b/Cargo.toml index 698ff035a861..4400f4c0aadb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ path = "src/driver.rs" [dependencies] clippy_lints = { path = "clippy_lints" } semver = "1.0" -rustc_tools_util = {version = "0.2.1", path = "./rustc_tools_util"} +rustc_tools_util = "0.3.0" tempfile = { version = "3.2", optional = true } termize = "0.1" @@ -56,7 +56,7 @@ tokio = { version = "1", features = ["io-util"] } rustc-semver = "1.1" [build-dependencies] -rustc_tools_util = {version = "0.2.1", path = "./rustc_tools_util"} +rustc_tools_util = "0.3.0" [features] deny-warnings = ["clippy_lints/deny-warnings"] diff --git a/rustc_tools_util/CHANGELOG.md b/rustc_tools_util/CHANGELOG.md new file mode 100644 index 000000000000..1b351da2e7bc --- /dev/null +++ b/rustc_tools_util/CHANGELOG.md @@ -0,0 +1,6 @@ +# Changelog + +## Version 0.3.0 + +* Added `setup_version_info!();` macro for automated scripts. +* `get_version_info!()` no longer requires the user to import `rustc_tools_util::VersionInfo` and `std::env` diff --git a/rustc_tools_util/Cargo.toml b/rustc_tools_util/Cargo.toml index 89c3d6aaa89e..877049ae7d0e 100644 --- a/rustc_tools_util/Cargo.toml +++ b/rustc_tools_util/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustc_tools_util" -version = "0.2.1" +version = "0.3.0" description = "small helper to generate version information for git packages" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" diff --git a/rustc_tools_util/README.md b/rustc_tools_util/README.md index 6204ca174f35..eefc661f9635 100644 --- a/rustc_tools_util/README.md +++ b/rustc_tools_util/README.md @@ -13,10 +13,10 @@ build = "build.rs" List rustc_tools_util as regular AND build dependency. ````toml [dependencies] -rustc_tools_util = "0.2.1" +rustc_tools_util = "0.3.0" [build-dependencies] -rustc_tools_util = "0.2.1" +rustc_tools_util = "0.3.0" ```` In `build.rs`, generate the data in your `main()` @@ -44,6 +44,9 @@ This gives the following output in clippy: This project is part of the rust-lang/rust-clippy repository. The source code can be found under `./rustc_tools_util/`. +The changelog for `rustc_tools_util` is available under: +[`rustc_tools_util/CHANGELOG.md`](https://github.com/rust-lang/rust-clippy/blob/master/rustc_tools_util/CHANGELOG.md) + ## License Copyright 2014-2022 The Rust Project Developers diff --git a/rustc_tools_util/src/lib.rs b/rustc_tools_util/src/lib.rs index 5e856319c886..4c1d8c3733df 100644 --- a/rustc_tools_util/src/lib.rs +++ b/rustc_tools_util/src/lib.rs @@ -1,7 +1,5 @@ #![cfg_attr(feature = "deny-warnings", deny(warnings))] -use std::env; - /// This macro creates the version string during compilation from the /// current environment #[macro_export] @@ -121,7 +119,7 @@ pub fn get_commit_date() -> Option { #[must_use] pub fn get_channel() -> String { - match env::var("CFG_RELEASE_CHANNEL") { + match std::env::var("CFG_RELEASE_CHANNEL") { Ok(channel) => channel, Err(_) => { // if that failed, try to ask rustc -V, do some parsing and find out @@ -156,8 +154,8 @@ mod test { fn test_struct_local() { let vi = get_version_info!(); assert_eq!(vi.major, 0); - assert_eq!(vi.minor, 2); - assert_eq!(vi.patch, 1); + assert_eq!(vi.minor, 3); + assert_eq!(vi.patch, 0); assert_eq!(vi.crate_name, "rustc_tools_util"); // hard to make positive tests for these since they will always change assert!(vi.commit_hash.is_none()); @@ -167,7 +165,7 @@ mod test { #[test] fn test_display_local() { let vi = get_version_info!(); - assert_eq!(vi.to_string(), "rustc_tools_util 0.2.1"); + assert_eq!(vi.to_string(), "rustc_tools_util 0.3.0"); } #[test] @@ -176,7 +174,7 @@ mod test { let s = format!("{vi:?}"); assert_eq!( s, - "VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 2, patch: 1 }" + "VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 3, patch: 0 }" ); } } From 9671dd239d37cf6001eaea1f85f9f81200b8ade6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Thu, 15 Dec 2022 14:05:01 +0100 Subject: [PATCH 113/321] doc: Fix a few small issues * A few typos around generic types (`;` vs `,`) * Use inline code formatting for code fragments * One instance of wrong wording --- library/alloc/src/collections/vec_deque/mod.rs | 2 +- library/core/src/array/mod.rs | 4 ++-- library/core/src/iter/traits/iterator.rs | 2 +- library/core/src/slice/mod.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 4b9bd74d3924..be615b70ced9 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -2541,7 +2541,7 @@ impl VecDeque { /// The deque is assumed to be partitioned according to the given predicate. /// This means that all elements for which the predicate returns true are at the start of the deque /// and all elements for which the predicate returns false are at the end. - /// For example, [7, 15, 3, 5, 4, 12, 6] is a partitioned under the predicate x % 2 != 0 + /// For example, `[7, 15, 3, 5, 4, 12, 6]` is partitioned under the predicate `x % 2 != 0` /// (all odd numbers are at the start, all even at the end). /// /// If the deque is not partitioned, the returned result is unspecified and meaningless, diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 94a1a1d32bcd..2825e0bbb438 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -69,7 +69,7 @@ where /// if any element creation was unsuccessful. /// /// The return type of this function depends on the return type of the closure. -/// If you return `Result` from the closure, you'll get a `Result<[T; N]; E>`. +/// If you return `Result` from the closure, you'll get a `Result<[T; N], E>`. /// If you return `Option` from the closure, you'll get an `Option<[T; N]>`. /// /// # Arguments @@ -522,7 +522,7 @@ impl [T; N] { /// return an array the same size as `self` or the first error encountered. /// /// The return type of this function depends on the return type of the closure. - /// If you return `Result` from the closure, you'll get a `Result<[T; N]; E>`. + /// If you return `Result` from the closure, you'll get a `Result<[T; N], E>`. /// If you return `Option` from the closure, you'll get an `Option<[T; N]>`. /// /// # Examples diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 1cdee992137d..bac836292f8f 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2734,7 +2734,7 @@ pub trait Iterator { /// the first true result or the first error. /// /// The return type of this method depends on the return type of the closure. - /// If you return `Result` from the closure, you'll get a `Result; E>`. + /// If you return `Result` from the closure, you'll get a `Result, E>`. /// If you return `Option` from the closure, you'll get an `Option>`. /// /// # Examples diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index d9281a9252c0..8bd2ed45c0a2 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -3795,7 +3795,7 @@ impl [T] { /// The slice is assumed to be partitioned according to the given predicate. /// This means that all elements for which the predicate returns true are at the start of the slice /// and all elements for which the predicate returns false are at the end. - /// For example, [7, 15, 3, 5, 4, 12, 6] is a partitioned under the predicate x % 2 != 0 + /// For example, `[7, 15, 3, 5, 4, 12, 6]` is partitioned under the predicate `x % 2 != 0` /// (all odd numbers are at the start, all even at the end). /// /// If this slice is not partitioned, the returned result is unspecified and meaningless, From a8b9e00518aef62e20ff2ab79801d200c869ea8f Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Thu, 15 Dec 2022 14:17:38 +0000 Subject: [PATCH 114/321] fix tidy and add rpitit test --- .../const-impl-trait.rs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs b/src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs index 2059f6d79a57..0622f96e70d8 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/const-impl-trait.rs @@ -1,5 +1,11 @@ // check-pass -#![feature(associated_type_bounds, const_trait_impl, const_cmp)] +#![allow(incomplete_features)] +#![feature( + associated_type_bounds, + const_trait_impl, + const_cmp, + return_position_impl_trait_in_trait, +)] use std::marker::Destruct; @@ -7,15 +13,30 @@ const fn cmp(a: &impl ~const PartialEq) -> bool { a == a } -const fn wrap(x: impl ~const PartialEq + ~const Destruct) -> impl ~const PartialEq + ~const Destruct { +const fn wrap(x: impl ~const PartialEq + ~const Destruct) + -> impl ~const PartialEq + ~const Destruct +{ x } +#[const_trait] +trait Foo { + fn huh() -> impl ~const PartialEq + ~const Destruct + Copy; +} + +impl const Foo for () { + fn huh() -> impl ~const PartialEq + ~const Destruct + Copy { + 123 + } +} + const _: () = { assert!(cmp(&0xDEADBEEFu32)); assert!(cmp(&())); assert!(wrap(123) == wrap(123)); assert!(wrap(123) != wrap(456)); + let x = <() as Foo>::huh(); + assert!(x == x); }; #[const_trait] From 7574c9837199aebc4f3d8757cb2999a4f3604659 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Thu, 15 Dec 2022 18:51:18 +0000 Subject: [PATCH 115/321] Fix new_return_no_self with recursive bounds --- clippy_utils/src/ty.rs | 90 +++++++++++++++++++-------------- tests/ui/new_ret_no_self.rs | 23 +++++++++ tests/ui/new_ret_no_self.stderr | 42 ++++++++++----- 3 files changed, 105 insertions(+), 50 deletions(-) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 81b08ae5600d..6ba916306eeb 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -69,50 +69,66 @@ pub fn contains_adt_constructor<'tcx>(ty: Ty<'tcx>, adt: AdtDef<'tcx>) -> bool { /// This method also recurses into opaque type predicates, so call it with `impl Trait` and `U` /// will also return `true`. pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, needle: Ty<'tcx>) -> bool { - ty.walk().any(|inner| match inner.unpack() { - GenericArgKind::Type(inner_ty) => { - if inner_ty == needle { - return true; - } + fn contains_ty_adt_constructor_opaque_inner<'tcx>( + cx: &LateContext<'tcx>, + ty: Ty<'tcx>, + needle: Ty<'tcx>, + seen: &mut FxHashSet, + ) -> bool { + ty.walk().any(|inner| match inner.unpack() { + GenericArgKind::Type(inner_ty) => { + if inner_ty == needle { + return true; + } - if inner_ty.ty_adt_def() == needle.ty_adt_def() { - return true; - } + if inner_ty.ty_adt_def() == needle.ty_adt_def() { + return true; + } - if let ty::Opaque(def_id, _) = *inner_ty.kind() { - for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { - match predicate.kind().skip_binder() { - // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through - // and check substituions to find `U`. - ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => { - if trait_predicate - .trait_ref - .substs - .types() - .skip(1) // Skip the implicit `Self` generic parameter - .any(|ty| contains_ty_adt_constructor_opaque(cx, ty, needle)) - { - return true; - } - }, - // For `impl Trait`, it will register a predicate of `::Assoc = U`, - // so we check the term for `U`. - ty::PredicateKind::Clause(ty::Clause::Projection(projection_predicate)) => { - if let ty::TermKind::Ty(ty) = projection_predicate.term.unpack() { - if contains_ty_adt_constructor_opaque(cx, ty, needle) { + if let ty::Opaque(def_id, _) = *inner_ty.kind() { + if !seen.insert(def_id) { + return false; + } + + for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) { + match predicate.kind().skip_binder() { + // For `impl Trait`, it will register a predicate of `T: Trait`, so we go through + // and check substituions to find `U`. + ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => { + if trait_predicate + .trait_ref + .substs + .types() + .skip(1) // Skip the implicit `Self` generic parameter + .any(|ty| contains_ty_adt_constructor_opaque_inner(cx, ty, needle, seen)) + { return true; } - }; - }, - _ => (), + }, + // For `impl Trait`, it will register a predicate of `::Assoc = U`, + // so we check the term for `U`. + ty::PredicateKind::Clause(ty::Clause::Projection(projection_predicate)) => { + if let ty::TermKind::Ty(ty) = projection_predicate.term.unpack() { + if contains_ty_adt_constructor_opaque_inner(cx, ty, needle, seen) { + return true; + } + }; + }, + _ => (), + } } } - } - false - }, - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, - }) + false + }, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, + }) + } + + // A hash set to ensure that the same opaque type (`impl Trait` in RPIT or TAIT) is not + // visited twice. + let mut seen = FxHashSet::default(); + contains_ty_adt_constructor_opaque_inner(cx, ty, needle, &mut seen) } /// Resolves `::Item` for `T` diff --git a/tests/ui/new_ret_no_self.rs b/tests/ui/new_ret_no_self.rs index f69982d63a89..beec42f08bb0 100644 --- a/tests/ui/new_ret_no_self.rs +++ b/tests/ui/new_ret_no_self.rs @@ -1,3 +1,4 @@ +#![feature(type_alias_impl_trait)] #![warn(clippy::new_ret_no_self)] #![allow(dead_code)] @@ -400,3 +401,25 @@ mod issue7344 { } } } + +mod issue10041 { + struct Bomb; + + impl Bomb { + // Hidden default generic paramter. + pub fn new() -> impl PartialOrd { + 0i32 + } + } + + // TAIT with self-referencing bounds + type X = impl std::ops::Add; + + struct Bomb2; + + impl Bomb2 { + pub fn new() -> X { + 0i32 + } + } +} diff --git a/tests/ui/new_ret_no_self.stderr b/tests/ui/new_ret_no_self.stderr index bc13be47927b..2eaebfb5cac5 100644 --- a/tests/ui/new_ret_no_self.stderr +++ b/tests/ui/new_ret_no_self.stderr @@ -1,5 +1,5 @@ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:49:5 + --> $DIR/new_ret_no_self.rs:50:5 | LL | / pub fn new(_: String) -> impl R { LL | | S3 @@ -9,7 +9,7 @@ LL | | } = note: `-D clippy::new-ret-no-self` implied by `-D warnings` error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:81:5 + --> $DIR/new_ret_no_self.rs:82:5 | LL | / pub fn new() -> u32 { LL | | unimplemented!(); @@ -17,7 +17,7 @@ LL | | } | |_____^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:90:5 + --> $DIR/new_ret_no_self.rs:91:5 | LL | / pub fn new(_: String) -> u32 { LL | | unimplemented!(); @@ -25,7 +25,7 @@ LL | | } | |_____^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:126:5 + --> $DIR/new_ret_no_self.rs:127:5 | LL | / pub fn new() -> (u32, u32) { LL | | unimplemented!(); @@ -33,7 +33,7 @@ LL | | } | |_____^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:153:5 + --> $DIR/new_ret_no_self.rs:154:5 | LL | / pub fn new() -> *mut V { LL | | unimplemented!(); @@ -41,7 +41,7 @@ LL | | } | |_____^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:171:5 + --> $DIR/new_ret_no_self.rs:172:5 | LL | / pub fn new() -> Option { LL | | unimplemented!(); @@ -49,19 +49,19 @@ LL | | } | |_____^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:224:9 + --> $DIR/new_ret_no_self.rs:225:9 | LL | fn new() -> String; | ^^^^^^^^^^^^^^^^^^^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:236:9 + --> $DIR/new_ret_no_self.rs:237:9 | LL | fn new(_: String) -> String; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:271:9 + --> $DIR/new_ret_no_self.rs:272:9 | LL | / fn new() -> (u32, u32) { LL | | unimplemented!(); @@ -69,7 +69,7 @@ LL | | } | |_________^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:298:9 + --> $DIR/new_ret_no_self.rs:299:9 | LL | / fn new() -> *mut V { LL | | unimplemented!(); @@ -77,7 +77,7 @@ LL | | } | |_________^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:368:9 + --> $DIR/new_ret_no_self.rs:369:9 | LL | / fn new(t: T) -> impl Into { LL | | 1 @@ -85,12 +85,28 @@ LL | | } | |_________^ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:389:9 + --> $DIR/new_ret_no_self.rs:390:9 | LL | / fn new(t: T) -> impl Trait2<(), i32> { LL | | unimplemented!() LL | | } | |_________^ -error: aborting due to 12 previous errors +error: methods called `new` usually return `Self` + --> $DIR/new_ret_no_self.rs:410:9 + | +LL | / pub fn new() -> impl PartialOrd { +LL | | 0i32 +LL | | } + | |_________^ + +error: methods called `new` usually return `Self` + --> $DIR/new_ret_no_self.rs:421:9 + | +LL | / pub fn new() -> X { +LL | | 0i32 +LL | | } + | |_________^ + +error: aborting due to 14 previous errors From f28a8ca1dce6b8e8ce7d89bdb2030d74d06039c4 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 15 Dec 2022 12:05:27 -0700 Subject: [PATCH 116/321] rustdoc: simplify CSS for codeblock tooltips Instead of making its parts `display: none` and then changing it on hover, just make the pseudo-element itself on hover. --- src/librustdoc/html/static/css/rustdoc.css | 18 ++++++------------ src/test/rustdoc-gui/codeblock-tooltip.goml | 6 +++--- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index bc1e15b35937..0cdb164146ae 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1114,8 +1114,7 @@ pre.rust .doccomment { top: 5px; } -.example-wrap .tooltip::after { - display: none; +.example-wrap .tooltip:hover::after { text-align: center; padding: 5px 3px 3px 3px; border-radius: 6px; @@ -1130,35 +1129,30 @@ pre.rust .doccomment { color: var(--tooltip-color); } -.example-wrap .tooltip::before { +.example-wrap .tooltip:hover::before { content: " "; position: absolute; top: 50%; left: 16px; margin-top: -5px; - display: none; z-index: 1; border: 5px solid transparent; border-right-color: var(--tooltip-background-color); } -.example-wrap.ignore .tooltip::after { +.example-wrap.ignore .tooltip:hover::after { content: "This example is not tested"; } -.example-wrap.compile_fail .tooltip::after { +.example-wrap.compile_fail .tooltip:hover::after { content: "This example deliberately fails to compile"; } -.example-wrap.should_panic .tooltip::after { +.example-wrap.should_panic .tooltip:hover::after { content: "This example panics"; } -.example-wrap.edition .tooltip::after { +.example-wrap.edition .tooltip:hover::after { content: "This code runs with edition " attr(data-edition); } -.example-wrap .tooltip:hover::before, .example-wrap .tooltip:hover::after { - display: inline; -} - .example-wrap.compile_fail .tooltip, .example-wrap.should_panic .tooltip, .example-wrap.ignore .tooltip { diff --git a/src/test/rustdoc-gui/codeblock-tooltip.goml b/src/test/rustdoc-gui/codeblock-tooltip.goml index 4d923be3e786..aab27394eb1f 100644 --- a/src/test/rustdoc-gui/codeblock-tooltip.goml +++ b/src/test/rustdoc-gui/codeblock-tooltip.goml @@ -20,7 +20,7 @@ define-function: ( {"border-left": "2px solid rgba(255, 0, 0, 0.5)"}, )), - ("move-cursor-to", ".docblock .example-wrap.compile_fail"), + ("move-cursor-to", ".docblock .example-wrap.compile_fail .tooltip"), ("assert-css", ( ".docblock .example-wrap.compile_fail .tooltip", @@ -60,7 +60,7 @@ define-function: ( {"border-left": "2px solid rgba(255, 0, 0, 0.5)"}, )), - ("move-cursor-to", ".docblock .example-wrap.should_panic"), + ("move-cursor-to", ".docblock .example-wrap.should_panic .tooltip"), ("assert-css", ( ".docblock .example-wrap.should_panic .tooltip", @@ -100,7 +100,7 @@ define-function: ( {"border-left": "2px solid rgba(255, 142, 0, 0.6)"}, )), - ("move-cursor-to", ".docblock .example-wrap.ignore"), + ("move-cursor-to", ".docblock .example-wrap.ignore .tooltip"), ("assert-css", ( ".docblock .example-wrap.ignore .tooltip", From 88aaaa6135413f8679ad0a33b8a50acda93a8c14 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 15 Dec 2022 12:27:32 -0700 Subject: [PATCH 117/321] rustdoc: remove unused CSS `.sub-settings` Obsoleted when 9625ed8be7fa66c3ee5f78180a3d5911817096f6 changed the DOM. --- src/librustdoc/html/static/css/settings.css | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/librustdoc/html/static/css/settings.css b/src/librustdoc/html/static/css/settings.css index 1f6fb961e918..875a260c8115 100644 --- a/src/librustdoc/html/static/css/settings.css +++ b/src/librustdoc/html/static/css/settings.css @@ -59,12 +59,6 @@ cursor: pointer; } -.setting-line > .sub-settings { - padding-left: 42px; - width: 100%; - display: block; -} - #settings .setting-line { margin: 1.2em 0.6em; } From fded03ee2d421359e49a69b6afa7d7f65ef9f597 Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Fri, 16 Dec 2022 08:36:40 +1300 Subject: [PATCH 118/321] docs: rewrite E0158 error-code docs for clarity --- .../src/error_codes/E0158.md | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0158.md b/compiler/rustc_error_codes/src/error_codes/E0158.md index 0a9ef9c39385..03b93d925c19 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0158.md +++ b/compiler/rustc_error_codes/src/error_codes/E0158.md @@ -1,38 +1,53 @@ -An associated const has been referenced in a pattern. +An associated `const`, `const` parameter or `static` has been referenced +in a pattern. Erroneous code example: ```compile_fail,E0158 -enum EFoo { A, B, C, D } - -trait Foo { - const X: EFoo; +enum Foo { + One, + Two } -fn test(arg: EFoo) { +trait Bar { + const X: Foo; +} + +fn test(arg: Foo) { match arg { - A::X => { // error! - println!("A::X"); - } + A::X => println!("A::X"), // error: E0158: associated consts cannot be + // referenced in patterns + Foo::Two => println!("Two") } } ``` -`const` and `static` mean different things. A `const` is a compile-time -constant, an alias for a literal value. This property means you can match it -directly within a pattern. +Associated `const`s cannot be referenced in patterns because it is impossible +for the compiler to prove exhaustiveness (that some pattern will always match). +Take the above example, because Rust does type checking in the *generic* +method, not the *monomorphized* specific instance. So because `Bar` could have +theoretically infinite implementations, there's no way to always be sure that +`A::X` is `Foo::One`. So this code must be rejected. Even if code can be +proven exhaustive by a programmer, the compiler cannot currently prove this. -The `static` keyword, on the other hand, guarantees a fixed location in memory. -This does not always mean that the value is constant. For example, a global -mutex can be declared `static` as well. +The same holds true of `const` parameters and `static`s. -If you want to match against a `static`, consider using a guard instead: +If you want to match against an associated `const`, `const` parameter or +`static` consider using a guard instead: ``` -static FORTY_TWO: i32 = 42; +trait Trait { + const X: char; +} -match Some(42) { - Some(x) if x == FORTY_TWO => {} - _ => {} +static FOO: char = 'j'; + +fn test(arg: char) { + match arg { + c if c == A::X => println!("A::X"), + c if c == Y => println!("Y"), + c if c == FOO => println!("FOO"), + _ => () + } } ``` From 605f77b7d0cb991f2f63c02ceb9dfea72091f398 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 15 Dec 2022 19:21:39 +0800 Subject: [PATCH 119/321] fix #105732, Fix ICE calling method on auto trait --- .../rustc_hir_typeck/src/method/suggest.rs | 10 +++++++ src/test/ui/methods/issues/issue-105732.rs | 13 +++++++++ .../ui/methods/issues/issue-105732.stderr | 28 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/test/ui/methods/issues/issue-105732.rs create mode 100644 src/test/ui/methods/issues/issue-105732.stderr diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 41cd6bf314eb..27915ad22d32 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -688,6 +688,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let entry = spanned_predicates.entry(spans); entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p); } + Some(Node::Item(hir::Item { + kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..), + span: item_span, + .. + })) => { + tcx.sess.delay_span_bug( + *item_span, + "auto trait is invoked with no method error, but no error reported?", + ); + } Some(_) => unreachable!(), None => (), } diff --git a/src/test/ui/methods/issues/issue-105732.rs b/src/test/ui/methods/issues/issue-105732.rs new file mode 100644 index 000000000000..98b7a8d0d04e --- /dev/null +++ b/src/test/ui/methods/issues/issue-105732.rs @@ -0,0 +1,13 @@ +#![feature(auto_traits)] + +auto trait Foo { + fn g(&self); //~ ERROR auto traits cannot have associated items +} + +trait Bar { + fn f(&self) { + self.g(); //~ ERROR the method `g` exists for reference `&Self`, but its trait bounds were not satisfied + } +} + +fn main() {} diff --git a/src/test/ui/methods/issues/issue-105732.stderr b/src/test/ui/methods/issues/issue-105732.stderr new file mode 100644 index 000000000000..fb2bdf47de7e --- /dev/null +++ b/src/test/ui/methods/issues/issue-105732.stderr @@ -0,0 +1,28 @@ +error[E0380]: auto traits cannot have associated items + --> $DIR/issue-105732.rs:4:8 + | +LL | auto trait Foo { + | --- auto trait cannot have associated items +LL | fn g(&self); + | ---^-------- help: remove these associated items + +error[E0599]: the method `g` exists for reference `&Self`, but its trait bounds were not satisfied + --> $DIR/issue-105732.rs:9:14 + | +LL | self.g(); + | ^ + | + = note: the following trait bounds were not satisfied: + `Self: Foo` + which is required by `&Self: Foo` + `&Self: Foo` + = help: items from traits can only be used if the type parameter is bounded by the trait +help: the following trait defines an item `g`, perhaps you need to add a supertrait for it: + | +LL | trait Bar: Foo { + | +++++ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0380, E0599. +For more information about an error, try `rustc --explain E0380`. From a84483eb59ac7e42998ade7ee1cf3456bed024ee Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 15 Dec 2022 16:29:52 -0700 Subject: [PATCH 120/321] rustdoc: name the source page sidebar-toggle `#src-sidebar-toggle` The old name doesn't get across where it's really supposed to be used. --- src/librustdoc/html/static/css/rustdoc.css | 20 ++++----- .../html/static/js/source-script.js | 2 +- src/test/rustdoc-gui/code-sidebar-toggle.goml | 4 +- src/test/rustdoc-gui/cursor.goml | 2 +- .../sidebar-source-code-display.goml | 44 +++++++++---------- src/test/rustdoc-gui/source-code-page.goml | 2 +- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index bc1e15b35937..8f3d9f8c6e24 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -396,15 +396,15 @@ img { overflow-y: hidden; } -.source .sidebar, #sidebar-toggle, #source-sidebar { +.source .sidebar, #src-sidebar-toggle, #source-sidebar { background-color: var(--sidebar-background-color); } -#sidebar-toggle > button:hover, #sidebar-toggle > button:focus { +#src-sidebar-toggle > button:hover, #src-sidebar-toggle > button:focus { background-color: var(--sidebar-background-color-hover); } -.source .sidebar > *:not(#sidebar-toggle) { +.source .sidebar > *:not(#src-sidebar-toggle) { visibility: hidden; } @@ -413,7 +413,7 @@ img { flex-basis: 300px; } -.source-sidebar-expanded .source .sidebar > *:not(#sidebar-toggle) { +.source-sidebar-expanded .source .sidebar > *:not(#src-sidebar-toggle) { visibility: visible; } @@ -1291,7 +1291,7 @@ a.test-arrow:hover { font-size: 1rem; } -#sidebar-toggle { +#src-sidebar-toggle { position: sticky; top: 0; left: 0; @@ -1320,7 +1320,7 @@ a.test-arrow:hover { #source-sidebar div.files > a.selected { background-color: var(--source-sidebar-background-selected); } -#sidebar-toggle > button { +#src-sidebar-toggle > button { font-size: inherit; font-weight: bold; background: none; @@ -1722,7 +1722,7 @@ in storage.js left: -11px; } - #sidebar-toggle { + #src-sidebar-toggle { position: fixed; left: 1px; top: 100px; @@ -1736,7 +1736,7 @@ in storage.js border-left: 0; } - .source-sidebar-expanded #sidebar-toggle { + .source-sidebar-expanded #src-sidebar-toggle { left: unset; top: unset; width: unset; @@ -1847,10 +1847,10 @@ in storage.js width: 35px; } - #sidebar-toggle { + #src-sidebar-toggle { top: 10px; } - .source-sidebar-expanded #sidebar-toggle { + .source-sidebar-expanded #src-sidebar-toggle { top: unset; } } diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js index 5db768c1c575..0e1c864e62d8 100644 --- a/src/librustdoc/html/static/js/source-script.js +++ b/src/librustdoc/html/static/js/source-script.js @@ -83,7 +83,7 @@ function toggleSidebar() { function createSidebarToggle() { const sidebarToggle = document.createElement("div"); - sidebarToggle.id = "sidebar-toggle"; + sidebarToggle.id = "src-sidebar-toggle"; const inner = document.createElement("button"); diff --git a/src/test/rustdoc-gui/code-sidebar-toggle.goml b/src/test/rustdoc-gui/code-sidebar-toggle.goml index 00a0ea1e1a27..df665bd46c03 100644 --- a/src/test/rustdoc-gui/code-sidebar-toggle.goml +++ b/src/test/rustdoc-gui/code-sidebar-toggle.goml @@ -1,7 +1,7 @@ // This test checks that the source code pages sidebar toggle is working as expected. goto: "file://" + |DOC_PATH| + "/test_docs/index.html" click: ".srclink" -wait-for: "#sidebar-toggle" -click: "#sidebar-toggle" +wait-for: "#src-sidebar-toggle" +click: "#src-sidebar-toggle" fail: true assert-css: ("#source-sidebar", { "left": "-300px" }) diff --git a/src/test/rustdoc-gui/cursor.goml b/src/test/rustdoc-gui/cursor.goml index b2e91cb81fba..cb6716a76f5c 100644 --- a/src/test/rustdoc-gui/cursor.goml +++ b/src/test/rustdoc-gui/cursor.goml @@ -21,4 +21,4 @@ assert-css: (".sidebar-menu-toggle", {"cursor": "pointer"}) // the sidebar toggle button on the source code pages goto: "file://" + |DOC_PATH| + "/src/lib2/lib.rs.html" -assert-css: ("#sidebar-toggle > button", {"cursor": "pointer"}) +assert-css: ("#src-sidebar-toggle > button", {"cursor": "pointer"}) diff --git a/src/test/rustdoc-gui/sidebar-source-code-display.goml b/src/test/rustdoc-gui/sidebar-source-code-display.goml index 40ae4af81be4..df4506e11196 100644 --- a/src/test/rustdoc-gui/sidebar-source-code-display.goml +++ b/src/test/rustdoc-gui/sidebar-source-code-display.goml @@ -2,18 +2,18 @@ javascript: false goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html" // Since the javascript is disabled, there shouldn't be a toggle. -assert-false: "#sidebar-toggle" +assert-false: "#src-sidebar-toggle" wait-for-css: (".sidebar", {"display": "none"}) // Let's retry with javascript enabled. javascript: true reload: -wait-for: "#sidebar-toggle" -assert-css: ("#sidebar-toggle", {"visibility": "visible"}) -assert-css: (".sidebar > *:not(#sidebar-toggle)", {"visibility": "hidden"}) +wait-for: "#src-sidebar-toggle" +assert-css: ("#src-sidebar-toggle", {"visibility": "visible"}) +assert-css: (".sidebar > *:not(#src-sidebar-toggle)", {"visibility": "hidden"}) // Let's expand the sidebar now. -click: "#sidebar-toggle" -wait-for-css: ("#sidebar-toggle", {"visibility": "visible"}) +click: "#src-sidebar-toggle" +wait-for-css: ("#src-sidebar-toggle", {"visibility": "visible"}) // We now check that opening the sidebar and clicking a link will leave it open. // The behavior here on desktop is different than the behavior on mobile, @@ -38,25 +38,25 @@ define-function: ( [ ("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}), ("reload"), - ("wait-for-css", ("#sidebar-toggle", {"visibility": "visible"})), + ("wait-for-css", ("#src-sidebar-toggle", {"visibility": "visible"})), ("assert-css", ( "#source-sidebar details[open] > .files a.selected", {"color": |color_hover|, "background-color": |background|}, )), // Without hover or focus. - ("assert-css", ("#sidebar-toggle > button", {"background-color": |background_toggle|})), + ("assert-css", ("#src-sidebar-toggle > button", {"background-color": |background_toggle|})), // With focus. - ("focus", "#sidebar-toggle > button"), + ("focus", "#src-sidebar-toggle > button"), ("assert-css", ( - "#sidebar-toggle > button:focus", + "#src-sidebar-toggle > button:focus", {"background-color": |background_toggle_hover|}, )), ("focus", ".search-input"), // With hover. - ("move-cursor-to", "#sidebar-toggle > button"), + ("move-cursor-to", "#src-sidebar-toggle > button"), ("assert-css", ( - "#sidebar-toggle > button:hover", + "#src-sidebar-toggle > button:hover", {"background-color": |background_toggle_hover|}, )), @@ -151,16 +151,16 @@ call-function: ("check-colors", { size: (500, 700) reload: // Waiting for the sidebar to be displayed... -wait-for-css: ("#sidebar-toggle", {"visibility": "visible"}) +wait-for-css: ("#src-sidebar-toggle", {"visibility": "visible"}) // We now check it takes the full size of the display. assert-property: ("body", {"clientWidth": "500", "clientHeight": "700"}) assert-property: (".sidebar", {"clientWidth": "500", "clientHeight": "700"}) // We now check the display of the toggle once the sidebar is expanded. -assert-property: ("#sidebar-toggle", {"clientWidth": "500", "clientHeight": "39"}) +assert-property: ("#src-sidebar-toggle", {"clientWidth": "500", "clientHeight": "39"}) assert-css: ( - "#sidebar-toggle", + "#src-sidebar-toggle", { "border-top-width": "0px", "border-right-width": "0px", @@ -170,28 +170,28 @@ assert-css: ( ) // We now check that the scroll position is kept when opening the sidebar. -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" wait-for-css: (".sidebar", {"width": "0px"}) // We scroll to line 117 to change the scroll position. scroll-to: '//*[@id="117"]' assert-window-property: {"pageYOffset": "2542"} // Expanding the sidebar... -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" wait-for-css: (".sidebar", {"width": "500px"}) -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" wait-for-css: (".sidebar", {"width": "0px"}) // The "scrollTop" property should be the same. assert-window-property: {"pageYOffset": "2542"} // We now check that the scroll position is restored if the window is resized. size: (500, 700) -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" wait-for-css: ("#source-sidebar", {"visibility": "visible"}) assert-window-property: {"pageYOffset": "0"} size: (900, 900) assert-window-property: {"pageYOffset": "2542"} size: (500, 700) -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" wait-for-css: ("#source-sidebar", {"visibility": "hidden"}) // We now check that opening the sidebar and clicking a link will close it. @@ -199,7 +199,7 @@ wait-for-css: ("#source-sidebar", {"visibility": "hidden"}) // but common sense dictates that if you have a list of files that fills the entire screen, and // you click one of them, you probably want to actually see the file's contents, and not just // make it the current selection. -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" wait-for-css: ("#source-sidebar", {"visibility": "visible"}) assert-local-storage: {"rustdoc-source-sidebar-show": "true"} click: ".sidebar a.selected" @@ -210,6 +210,6 @@ assert-local-storage: {"rustdoc-source-sidebar-show": "false"} size: (1000, 1000) wait-for-css: ("#source-sidebar", {"visibility": "hidden"}) assert-local-storage: {"rustdoc-source-sidebar-show": "false"} -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" wait-for-css: ("#source-sidebar", {"visibility": "visible"}) assert-local-storage: {"rustdoc-source-sidebar-show": "true"} diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml index b3b837ad377c..25da74e5173e 100644 --- a/src/test/rustdoc-gui/source-code-page.goml +++ b/src/test/rustdoc-gui/source-code-page.goml @@ -97,7 +97,7 @@ assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH) // Checking the source code sidebar. // First we "open" it. -click: "#sidebar-toggle" +click: "#src-sidebar-toggle" assert: ".source-sidebar-expanded" // We check that the first entry of the sidebar is collapsed From adf53d4b068e216f0705ba2baae4cc21dd0988a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Fri, 16 Dec 2022 00:00:00 +0000 Subject: [PATCH 121/321] Remove dead code after destination propagation --- compiler/rustc_mir_transform/src/dest_prop.rs | 7 ++ .../unreachable.f.DestinationPropagation.diff | 86 +++++++++++++++++++ src/test/mir-opt/dest-prop/unreachable.rs | 18 ++++ 3 files changed, 111 insertions(+) create mode 100644 src/test/mir-opt/dest-prop/unreachable.f.DestinationPropagation.diff create mode 100644 src/test/mir-opt/dest-prop/unreachable.rs diff --git a/compiler/rustc_mir_transform/src/dest_prop.rs b/compiler/rustc_mir_transform/src/dest_prop.rs index 3e45319431ce..74d8337653f0 100644 --- a/compiler/rustc_mir_transform/src/dest_prop.rs +++ b/compiler/rustc_mir_transform/src/dest_prop.rs @@ -129,6 +129,7 @@ use std::collections::hash_map::{Entry, OccupiedEntry}; +use crate::simplify::remove_dead_blocks; use crate::MirPass; use rustc_data_structures::fx::FxHashMap; use rustc_index::bit_set::BitSet; @@ -235,6 +236,12 @@ impl<'tcx> MirPass<'tcx> for DestinationPropagation { apply_merges(body, tcx, &merges, &merged_locals); } + if round_count != 0 { + // Merging can introduce overlap between moved arguments and/or call destination in an + // unreachable code, which validator considers to be ill-formed. + remove_dead_blocks(tcx, body); + } + trace!(round_count); } } diff --git a/src/test/mir-opt/dest-prop/unreachable.f.DestinationPropagation.diff b/src/test/mir-opt/dest-prop/unreachable.f.DestinationPropagation.diff new file mode 100644 index 000000000000..9ea756c27129 --- /dev/null +++ b/src/test/mir-opt/dest-prop/unreachable.f.DestinationPropagation.diff @@ -0,0 +1,86 @@ +- // MIR for `f` before DestinationPropagation ++ // MIR for `f` after DestinationPropagation + + fn f(_1: T) -> () { + debug a => _1; // in scope 0 at $DIR/unreachable.rs:+0:19: +0:20 + let mut _0: (); // return place in scope 0 at $DIR/unreachable.rs:+0:25: +0:25 + let _2: T; // in scope 0 at $DIR/unreachable.rs:+1:9: +1:10 + let mut _3: bool; // in scope 0 at $DIR/unreachable.rs:+2:8: +2:13 + let _4: (); // in scope 0 at $DIR/unreachable.rs:+3:9: +3:16 + let mut _5: T; // in scope 0 at $DIR/unreachable.rs:+3:11: +3:12 + let mut _6: T; // in scope 0 at $DIR/unreachable.rs:+3:14: +3:15 + let _7: (); // in scope 0 at $DIR/unreachable.rs:+5:9: +5:16 + let mut _8: T; // in scope 0 at $DIR/unreachable.rs:+5:11: +5:12 + let mut _9: T; // in scope 0 at $DIR/unreachable.rs:+5:14: +5:15 + scope 1 { +- debug b => _2; // in scope 1 at $DIR/unreachable.rs:+1:9: +1:10 ++ debug b => _1; // in scope 1 at $DIR/unreachable.rs:+1:9: +1:10 + } + + bb0: { +- StorageLive(_2); // scope 0 at $DIR/unreachable.rs:+1:9: +1:10 +- _2 = _1; // scope 0 at $DIR/unreachable.rs:+1:13: +1:14 ++ nop; // scope 0 at $DIR/unreachable.rs:+1:9: +1:10 ++ nop; // scope 0 at $DIR/unreachable.rs:+1:13: +1:14 + StorageLive(_3); // scope 1 at $DIR/unreachable.rs:+2:8: +2:13 + _3 = const false; // scope 1 at $DIR/unreachable.rs:+2:8: +2:13 +- goto -> bb3; // scope 1 at $DIR/unreachable.rs:+2:8: +2:13 ++ goto -> bb1; // scope 1 at $DIR/unreachable.rs:+2:8: +2:13 + } + + bb1: { +- StorageLive(_4); // scope 1 at $DIR/unreachable.rs:+3:9: +3:16 +- StorageLive(_5); // scope 1 at $DIR/unreachable.rs:+3:11: +3:12 +- _5 = _1; // scope 1 at $DIR/unreachable.rs:+3:11: +3:12 +- StorageLive(_6); // scope 1 at $DIR/unreachable.rs:+3:14: +3:15 +- _6 = _2; // scope 1 at $DIR/unreachable.rs:+3:14: +3:15 +- _4 = g::(move _5, move _6) -> bb2; // scope 1 at $DIR/unreachable.rs:+3:9: +3:16 +- // mir::Constant +- // + span: $DIR/unreachable.rs:11:9: 11:10 +- // + literal: Const { ty: fn(T, T) {g::}, val: Value() } +- } +- +- bb2: { +- StorageDead(_6); // scope 1 at $DIR/unreachable.rs:+3:15: +3:16 +- StorageDead(_5); // scope 1 at $DIR/unreachable.rs:+3:15: +3:16 +- StorageDead(_4); // scope 1 at $DIR/unreachable.rs:+3:16: +3:17 +- _0 = const (); // scope 1 at $DIR/unreachable.rs:+2:14: +4:6 +- goto -> bb5; // scope 1 at $DIR/unreachable.rs:+2:5: +6:6 +- } +- +- bb3: { + StorageLive(_7); // scope 1 at $DIR/unreachable.rs:+5:9: +5:16 +- StorageLive(_8); // scope 1 at $DIR/unreachable.rs:+5:11: +5:12 +- _8 = _2; // scope 1 at $DIR/unreachable.rs:+5:11: +5:12 ++ nop; // scope 1 at $DIR/unreachable.rs:+5:11: +5:12 ++ nop; // scope 1 at $DIR/unreachable.rs:+5:11: +5:12 + StorageLive(_9); // scope 1 at $DIR/unreachable.rs:+5:14: +5:15 +- _9 = _2; // scope 1 at $DIR/unreachable.rs:+5:14: +5:15 +- _7 = g::(move _8, move _9) -> bb4; // scope 1 at $DIR/unreachable.rs:+5:9: +5:16 ++ _9 = _1; // scope 1 at $DIR/unreachable.rs:+5:14: +5:15 ++ _7 = g::(move _1, move _9) -> bb2; // scope 1 at $DIR/unreachable.rs:+5:9: +5:16 + // mir::Constant + // + span: $DIR/unreachable.rs:13:9: 13:10 + // + literal: Const { ty: fn(T, T) {g::}, val: Value() } + } + +- bb4: { ++ bb2: { + StorageDead(_9); // scope 1 at $DIR/unreachable.rs:+5:15: +5:16 +- StorageDead(_8); // scope 1 at $DIR/unreachable.rs:+5:15: +5:16 ++ nop; // scope 1 at $DIR/unreachable.rs:+5:15: +5:16 + StorageDead(_7); // scope 1 at $DIR/unreachable.rs:+5:16: +5:17 + _0 = const (); // scope 1 at $DIR/unreachable.rs:+4:12: +6:6 +- goto -> bb5; // scope 1 at $DIR/unreachable.rs:+2:5: +6:6 ++ goto -> bb3; // scope 1 at $DIR/unreachable.rs:+2:5: +6:6 + } + +- bb5: { ++ bb3: { + StorageDead(_3); // scope 1 at $DIR/unreachable.rs:+6:5: +6:6 +- StorageDead(_2); // scope 0 at $DIR/unreachable.rs:+7:1: +7:2 ++ nop; // scope 0 at $DIR/unreachable.rs:+7:1: +7:2 + return; // scope 0 at $DIR/unreachable.rs:+7:2: +7:2 + } + } + diff --git a/src/test/mir-opt/dest-prop/unreachable.rs b/src/test/mir-opt/dest-prop/unreachable.rs new file mode 100644 index 000000000000..32b5def984a8 --- /dev/null +++ b/src/test/mir-opt/dest-prop/unreachable.rs @@ -0,0 +1,18 @@ +// Check that unreachable code is removed after the destination propagation. +// Regression test for issue #105428. +// +// compile-flags: --crate-type=lib -Zmir-opt-level=0 +// compile-flags: -Zmir-enable-passes=+ConstProp,+SimplifyConstCondition-after-const-prop,+DestinationPropagation + +// EMIT_MIR unreachable.f.DestinationPropagation.diff +pub fn f(a: T) { + let b = a; + if false { + g(a, b); + } else { + g(b, b); + } +} + +#[inline(never)] +pub fn g(_: T, _: T) {} From 02cbc017d7c0febeecc91cb6c10a6b3128d7a623 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 25 Nov 2022 15:33:51 +0000 Subject: [PATCH 122/321] Rename ConstS to ConstData --- compiler/rustc_middle/src/arena.rs | 2 +- compiler/rustc_middle/src/ty/codec.rs | 2 +- compiler/rustc_middle/src/ty/consts.rs | 8 ++++---- compiler/rustc_middle/src/ty/context.rs | 10 +++++----- compiler/rustc_middle/src/ty/mod.rs | 6 +++--- compiler/rustc_middle/src/ty/subst.rs | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index 6de68841fe91..75282f958b53 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -93,7 +93,7 @@ macro_rules! arena_types { // Interned types [] tys: rustc_type_ir::WithCachedTypeInfo>, [] predicates: rustc_type_ir::WithCachedTypeInfo>, - [] consts: rustc_middle::ty::ConstS<'tcx>, + [] consts: rustc_middle::ty::ConstData<'tcx>, // Note that this deliberately duplicates items in the `rustc_hir::arena`, // since we need to allocate this type on both the `rustc_hir` arena diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 75f2d45eadb8..8cc8286c1dbe 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -310,7 +310,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> impl<'tcx, D: TyDecoder>> Decodable for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Self { - let consts: ty::ConstS<'tcx> = Decodable::decode(decoder); + let consts: ty::ConstData<'tcx> = Decodable::decode(decoder); decoder.interner().mk_const(consts.kind, consts.ty) } } diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index c2be08e497e5..41053871cd57 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -14,10 +14,10 @@ pub use int::*; pub use kind::*; pub use valtree::*; -/// Use this rather than `ConstS`, whenever possible. +/// Use this rather than `ConstData, whenever possible. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)] #[rustc_pass_by_value] -pub struct Const<'tcx>(pub Interned<'tcx, ConstS<'tcx>>); +pub struct Const<'tcx>(pub Interned<'tcx, ConstData<'tcx>>); impl<'tcx> fmt::Debug for Const<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -30,13 +30,13 @@ impl<'tcx> fmt::Debug for Const<'tcx> { /// Typed constant value. #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, TyEncodable, TyDecodable)] -pub struct ConstS<'tcx> { +pub struct ConstData<'tcx> { pub ty: Ty<'tcx>, pub kind: ConstKind<'tcx>, } #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -static_assert_size!(ConstS<'_>, 40); +static_assert_size!(ConstData<'_>, 40); impl<'tcx> Const<'tcx> { #[inline] diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 173c5ed4feef..8f4d56c65b98 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -17,7 +17,7 @@ use crate::thir::Thir; use crate::traits; use crate::ty::query::{self, TyCtxtAt}; use crate::ty::{ - self, AdtDef, AdtDefData, AdtKind, Binder, Const, ConstS, DefIdTree, FloatTy, FloatVar, + self, AdtDef, AdtDefData, AdtKind, Binder, Const, ConstData, DefIdTree, FloatTy, FloatVar, FloatVid, GenericParamDefKind, ImplPolarity, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVar, TyVid, TypeAndMut, TypeckResults, UintTy, @@ -140,7 +140,7 @@ pub struct CtxtInterners<'tcx> { predicates: InternedSet<'tcx, List>>, projs: InternedSet<'tcx, List>, place_elems: InternedSet<'tcx, List>>, - const_: InternedSet<'tcx, ConstS<'tcx>>, + const_: InternedSet<'tcx, ConstData<'tcx>>, const_allocation: InternedSet<'tcx, Allocation>, bound_variable_kinds: InternedSet<'tcx, List>, layout: InternedSet<'tcx, LayoutS>, @@ -331,7 +331,7 @@ impl<'tcx> CommonConsts<'tcx> { }; CommonConsts { - unit: mk_const(ty::ConstS { + unit: mk_const(ty::ConstData { kind: ty::ConstKind::Value(ty::ValTree::zst()), ty: types.unit, }), @@ -1601,7 +1601,7 @@ macro_rules! direct_interners { direct_interners! { region: mk_region(RegionKind<'tcx>): Region -> Region<'tcx>, - const_: mk_const_internal(ConstS<'tcx>): Const -> Const<'tcx>, + const_: mk_const_internal(ConstData<'tcx>): Const -> Const<'tcx>, const_allocation: intern_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>, layout: intern_layout(LayoutS): Layout -> Layout<'tcx>, adt_def: intern_adt_def(AdtDefData): AdtDef -> AdtDef<'tcx>, @@ -1976,7 +1976,7 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn mk_const(self, kind: impl Into>, ty: Ty<'tcx>) -> Const<'tcx> { - self.mk_const_internal(ty::ConstS { kind: kind.into(), ty }) + self.mk_const_internal(ty::ConstData { kind: kind.into(), ty }) } #[inline] diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 7290f0ae7c03..a8e1253e6705 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -80,7 +80,7 @@ pub use self::closure::{ CAPTURE_STRUCT_LOCAL, }; pub use self::consts::{ - Const, ConstInt, ConstKind, ConstS, Expr, InferConst, ScalarInt, UnevaluatedConst, ValTree, + Const, ConstData, ConstInt, ConstKind, Expr, InferConst, ScalarInt, UnevaluatedConst, ValTree, }; pub use self::context::{ tls, CtxtInterners, DeducedParamAttrs, FreeRegionInfo, GlobalCtxt, Lift, OnDiskCache, TyCtxt, @@ -945,7 +945,7 @@ impl<'tcx> Term<'tcx> { &*((ptr & !TAG_MASK) as *const WithCachedTypeInfo>), ))), CONST_TAG => TermKind::Const(ty::Const(Interned::new_unchecked( - &*((ptr & !TAG_MASK) as *const ty::ConstS<'tcx>), + &*((ptr & !TAG_MASK) as *const ty::ConstData<'tcx>), ))), _ => core::intrinsics::unreachable(), } @@ -991,7 +991,7 @@ impl<'tcx> TermKind<'tcx> { TermKind::Const(ct) => { // Ensure we can use the tag bits. assert_eq!(mem::align_of_val(&*ct.0.0) & TAG_MASK, 0); - (CONST_TAG, ct.0.0 as *const ty::ConstS<'tcx> as usize) + (CONST_TAG, ct.0.0 as *const ty::ConstData<'tcx> as usize) } }; diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index f8385c470160..a04b15f8cf13 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -90,7 +90,7 @@ impl<'tcx> GenericArgKind<'tcx> { GenericArgKind::Const(ct) => { // Ensure we can use the tag bits. assert_eq!(mem::align_of_val(&*ct.0.0) & TAG_MASK, 0); - (CONST_TAG, ct.0.0 as *const ty::ConstS<'tcx> as usize) + (CONST_TAG, ct.0.0 as *const ty::ConstData<'tcx> as usize) } }; @@ -166,7 +166,7 @@ impl<'tcx> GenericArg<'tcx> { &*((ptr & !TAG_MASK) as *const WithCachedTypeInfo>), ))), CONST_TAG => GenericArgKind::Const(ty::Const(Interned::new_unchecked( - &*((ptr & !TAG_MASK) as *const ty::ConstS<'tcx>), + &*((ptr & !TAG_MASK) as *const ty::ConstData<'tcx>), ))), _ => intrinsics::unreachable(), } From b1706699177e1c1ec55cb173bda7449e9760c264 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 16 Dec 2022 10:09:23 +0000 Subject: [PATCH 123/321] Make a field only as public as it needs to be --- compiler/rustc_middle/src/ty/consts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 41053871cd57..e5abc38046ce 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -17,7 +17,7 @@ pub use valtree::*; /// Use this rather than `ConstData, whenever possible. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)] #[rustc_pass_by_value] -pub struct Const<'tcx>(pub Interned<'tcx, ConstData<'tcx>>); +pub struct Const<'tcx>(pub(super) Interned<'tcx, ConstData<'tcx>>); impl<'tcx> fmt::Debug for Const<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { From 75ebd691c8b230c38a487ffb5e10f585c45f0873 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 16 Dec 2022 20:55:06 +0900 Subject: [PATCH 124/321] remove unused stderr files --- .../defaults/complex-unord-param.min.stderr | 8 -------- src/test/ui/const-generics/type-after-const-ok.min.stderr | 8 -------- 2 files changed, 16 deletions(-) delete mode 100644 src/test/ui/const-generics/defaults/complex-unord-param.min.stderr delete mode 100644 src/test/ui/const-generics/type-after-const-ok.min.stderr diff --git a/src/test/ui/const-generics/defaults/complex-unord-param.min.stderr b/src/test/ui/const-generics/defaults/complex-unord-param.min.stderr deleted file mode 100644 index 8e8d26a00043..000000000000 --- a/src/test/ui/const-generics/defaults/complex-unord-param.min.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: type parameters must be declared prior to const parameters - --> $DIR/complex-unord-param.rs:8:41 - | -LL | struct NestedArrays<'a, const N: usize, A: 'a, const M: usize, T:'a =u32> { - | ---------------------^----------------------^--------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, A: 'a, T: 'a = u32, const N: usize, const M: usize>` - -error: aborting due to previous error - diff --git a/src/test/ui/const-generics/type-after-const-ok.min.stderr b/src/test/ui/const-generics/type-after-const-ok.min.stderr deleted file mode 100644 index ad38754c7412..000000000000 --- a/src/test/ui/const-generics/type-after-const-ok.min.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: type parameters must be declared prior to const parameters - --> $DIR/type-after-const-ok.rs:8:26 - | -LL | struct A(T); - | -----------------^- help: reorder the parameters: lifetimes, then types, then consts: `` - -error: aborting due to previous error - From 14b508fbec6d9fdebda3397b0df908d829bbdb0b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 16 Dec 2022 14:51:48 +0100 Subject: [PATCH 125/321] Don't add "Read more" link if there is no extra content --- src/librustdoc/html/markdown.rs | 27 +++++++++++++++++++++------ src/librustdoc/html/render/mod.rs | 5 +++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index b141820fe423..aeaee524fd45 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -567,11 +567,12 @@ struct SummaryLine<'a, I: Iterator>> { inner: I, started: bool, depth: u32, + skipped_tags: u32, } impl<'a, I: Iterator>> SummaryLine<'a, I> { fn new(iter: I) -> Self { - SummaryLine { inner: iter, started: false, depth: 0 } + SummaryLine { inner: iter, started: false, depth: 0, skipped_tags: 0 } } } @@ -601,6 +602,7 @@ impl<'a, I: Iterator>> Iterator for SummaryLine<'a, I> { let is_allowed_tag = match event { Event::Start(ref c) => { if is_forbidden_tag(c) { + self.skipped_tags += 1; return None; } self.depth += 1; @@ -608,6 +610,7 @@ impl<'a, I: Iterator>> Iterator for SummaryLine<'a, I> { } Event::End(ref c) => { if is_forbidden_tag(c) { + self.skipped_tags += 1; return None; } self.depth -= 1; @@ -616,6 +619,9 @@ impl<'a, I: Iterator>> Iterator for SummaryLine<'a, I> { } _ => true, }; + if !is_allowed_tag { + self.skipped_tags += 1; + } return if !is_allowed_tag { if is_start { Some(Event::Start(Tag::Paragraph)) @@ -1096,11 +1102,11 @@ impl MarkdownItemInfo<'_> { } impl MarkdownSummaryLine<'_> { - pub(crate) fn into_string(self) -> String { + pub(crate) fn into_string_with_has_more_content(self) -> (String, bool) { let MarkdownSummaryLine(md, links) = self; // This is actually common enough to special-case if md.is_empty() { - return String::new(); + return (String::new(), false); } let mut replacer = |broken_link: BrokenLink<'_>| { @@ -1110,17 +1116,26 @@ impl MarkdownSummaryLine<'_> { .map(|link| (link.href.as_str().into(), link.new_text.as_str().into())) }; - let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer)); + let p = Parser::new_with_broken_link_callback(md, summary_opts(), Some(&mut replacer)) + .peekable(); + let mut summary = SummaryLine::new(p); let mut s = String::new(); - let without_paragraphs = LinkReplacer::new(SummaryLine::new(p), links).filter(|event| { + let without_paragraphs = LinkReplacer::new(&mut summary, links).filter(|event| { !matches!(event, Event::Start(Tag::Paragraph) | Event::End(Tag::Paragraph)) }); html::push_html(&mut s, without_paragraphs); - s + let has_more_content = + matches!(summary.inner.peek(), Some(Event::Start(_))) || summary.skipped_tags > 0; + + (s, has_more_content) + } + + pub(crate) fn into_string(self) -> String { + self.into_string_with_has_more_content().0 } } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 80fbe9c1f066..146e5010e4e4 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -467,9 +467,10 @@ fn document_short( return; } if let Some(s) = item.doc_value() { - let mut summary_html = MarkdownSummaryLine(&s, &item.links(cx)).into_string(); + let (mut summary_html, has_more_content) = + MarkdownSummaryLine(&s, &item.links(cx)).into_string_with_has_more_content(); - if s.contains('\n') { + if has_more_content { let link = format!(r#" Read more"#, assoc_href_attr(item, link, cx)); if let Some(idx) = summary_html.rfind("

") { From 80059e1b7274fc7ba8d9201ea075fd1c7b7608f1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 16 Dec 2022 14:52:25 +0100 Subject: [PATCH 126/321] Add test for presence of read more links --- src/test/rustdoc/read-more-unneeded.rs | 34 ++++++++++++++++++++++++++ src/test/rustdoc/trait-impl.rs | 2 -- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/test/rustdoc/read-more-unneeded.rs diff --git a/src/test/rustdoc/read-more-unneeded.rs b/src/test/rustdoc/read-more-unneeded.rs new file mode 100644 index 000000000000..0303e4442614 --- /dev/null +++ b/src/test/rustdoc/read-more-unneeded.rs @@ -0,0 +1,34 @@ +// Regression test for https://github.com/rust-lang/rust/issues/105677. +// This test ensures that the "Read more" link is only generated when +// there is actually more documentation to read after the short summary. + +#![crate_name = "foo"] + +pub trait MyFrom { + /// # Hello + /// ## Yolo + /// more! + fn try_from1(); + /// a + /// b + /// c + fn try_from2(); + /// a + /// + /// b + /// + /// c + fn try_from3(); +} + +pub struct NonZero; + +// @has 'foo/struct.NonZero.html' +impl MyFrom for NonZero { + // @matches - '//*[@class="docblock"]' '^Hello Read more$' + fn try_from1() {} + // @matches - '//*[@class="docblock"]' '^a\sb\sc$' + fn try_from2() {} + // @matches - '//*[@class="docblock"]' '^a Read more$' + fn try_from3() {} +} diff --git a/src/test/rustdoc/trait-impl.rs b/src/test/rustdoc/trait-impl.rs index 195cdf009b99..9cf3226f738c 100644 --- a/src/test/rustdoc/trait-impl.rs +++ b/src/test/rustdoc/trait-impl.rs @@ -30,8 +30,6 @@ impl Trait for Struct { // @has - '//*[@id="method.b"]/../../div[@class="docblock"]' 'These docs contain' // @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a' 'reference link' // @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a/@href' 'https://example.com' - // @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a' 'Read more' - // @has - '//*[@id="method.b"]/../../div[@class="docblock"]/a/@href' 'trait.Trait.html#tymethod.b' fn b() {} // @!has - '//*[@id="method.c"]/../../div[@class="docblock"]' 'code block' From 08a0e71ec9de799f9d867348c24e1193b570d159 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 16 Dec 2022 04:05:26 +0000 Subject: [PATCH 127/321] Detect inherent associated types not having CamelCase Fixes #105341. --- compiler/rustc_lint/src/nonstandard_style.rs | 12 +++++++++++- src/test/ui/associated-inherent-types/style.rs | 12 ++++++++++++ src/test/ui/associated-inherent-types/style.stderr | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/associated-inherent-types/style.rs create mode 100644 src/test/ui/associated-inherent-types/style.stderr diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index 7e50801f80c7..91fcd6d690ee 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -175,13 +175,23 @@ impl EarlyLintPass for NonCamelCaseTypes { return; } - match it.kind { + match &it.kind { ast::ItemKind::TyAlias(..) | ast::ItemKind::Enum(..) | ast::ItemKind::Struct(..) | ast::ItemKind::Union(..) => self.check_case(cx, "type", &it.ident), ast::ItemKind::Trait(..) => self.check_case(cx, "trait", &it.ident), ast::ItemKind::TraitAlias(..) => self.check_case(cx, "trait alias", &it.ident), + + // N.B. This check is only for inherent associated types, so that we don't lint against + // trait impls where we should have warned for the trait definition already. + ast::ItemKind::Impl(box ast::Impl { of_trait: None, items, .. }) => { + for it in items { + if let ast::AssocItemKind::Type(..) = it.kind { + self.check_case(cx, "associated type", &it.ident); + } + } + } _ => (), } } diff --git a/src/test/ui/associated-inherent-types/style.rs b/src/test/ui/associated-inherent-types/style.rs new file mode 100644 index 000000000000..8775bd19e1f9 --- /dev/null +++ b/src/test/ui/associated-inherent-types/style.rs @@ -0,0 +1,12 @@ +#![feature(inherent_associated_types)] +#![allow(incomplete_features, dead_code)] +#![deny(non_camel_case_types)] + +struct S; + +impl S { + type typ = (); + //~^ ERROR associated type `typ` should have an upper camel case name +} + +fn main() {} diff --git a/src/test/ui/associated-inherent-types/style.stderr b/src/test/ui/associated-inherent-types/style.stderr new file mode 100644 index 000000000000..f83061f8c421 --- /dev/null +++ b/src/test/ui/associated-inherent-types/style.stderr @@ -0,0 +1,14 @@ +error: associated type `typ` should have an upper camel case name + --> $DIR/style.rs:8:10 + | +LL | type typ = (); + | ^^^ help: convert the identifier to upper camel case: `Typ` + | +note: the lint level is defined here + --> $DIR/style.rs:3:9 + | +LL | #![deny(non_camel_case_types)] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + From 452c745518246484807bd8c73a30400a10cd5057 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 16 Dec 2022 15:10:48 +0000 Subject: [PATCH 128/321] Add a comment warning against using associated type defaults <3 --- compiler/rustc_middle/src/query/keys.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index a96bc115e3b4..e4bb3ce3d5a9 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -15,6 +15,14 @@ use rustc_span::{Span, DUMMY_SP}; /// The `Key` trait controls what types can legally be used as the key /// for a query. pub trait Key: Sized { + // N.B. Most of the keys down below have `type CacheSelector = DefaultCacheSelector;`, + // it would be reasonable to use associated type defaults, to remove the duplication... + // + // ...But r-a doesn't support them yet and using a default here causes r-a to not infer + // return types of queries which is very annoying. Thus, until r-a support associated + // type defaults, plese restrain from using them here <3 + // + // r-a issue: type CacheSelector; /// Given an instance of this key, what crate is it referring to? From 97c12e0460270bd6b0a6d52b8677f24c5b2d6879 Mon Sep 17 00:00:00 2001 From: Eric Wu Date: Fri, 16 Dec 2022 10:22:29 -0500 Subject: [PATCH 129/321] fix logic in IncrementVisitor There used to be a logical bug where IncrementVisitor would completely stop checking an expression/block after seeing a continue statement. This led to issue #10058 where a variable incremented (or otherwise modified) after any continue statement would still be considered incremented only once. The solution is to continue scanning the expression after seeing a `continue` statement, but increment self.depth so that the Visitor thinks that the rest of the loop is within a conditional. --- clippy_lints/src/loops/utils.rs | 10 +++------- tests/ui/explicit_counter_loop.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/loops/utils.rs b/clippy_lints/src/loops/utils.rs index b6f4cf7bbb37..28ee24309cc4 100644 --- a/clippy_lints/src/loops/utils.rs +++ b/clippy_lints/src/loops/utils.rs @@ -25,7 +25,6 @@ pub(super) struct IncrementVisitor<'a, 'tcx> { cx: &'a LateContext<'tcx>, // context reference states: HirIdMap, // incremented variables depth: u32, // depth of conditional expressions - done: bool, } impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> { @@ -34,7 +33,6 @@ impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> { cx, states: HirIdMap::default(), depth: 0, - done: false, } } @@ -51,10 +49,6 @@ impl<'a, 'tcx> IncrementVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> { fn visit_expr(&mut self, expr: &'tcx Expr<'_>) { - if self.done { - return; - } - // If node is a variable if let Some(def_id) = path_to_local(expr) { if let Some(parent) = get_parent_expr(self.cx, expr) { @@ -95,7 +89,9 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> { walk_expr(self, expr); self.depth -= 1; } else if let ExprKind::Continue(_) = expr.kind { - self.done = true; + // If we see a `continue` block, then we increment depth so that the IncrementVisitor + // state will be set to DontWarn if we see the variable being modified anywhere afterwards. + self.depth += 1; } else { walk_expr(self, expr); } diff --git a/tests/ui/explicit_counter_loop.rs b/tests/ui/explicit_counter_loop.rs index 6eddc01e2c47..46565a97f005 100644 --- a/tests/ui/explicit_counter_loop.rs +++ b/tests/ui/explicit_counter_loop.rs @@ -189,3 +189,33 @@ mod issue_7920 { } } } + +mod issue_10058 { + pub fn test() { + // should not lint since we are increasing counter potentially more than once in the loop + let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1]; + let mut counter = 0; + for value in values { + counter += 1; + + if value == 0 { + continue; + } + + counter += 1; + } + } + + pub fn test2() { + // should not lint since we are increasing counter potentially more than once in the loop + let values = [0, 1, 0, 1, 1, 1, 0, 1, 0, 1]; + let mut counter = 0; + for value in values { + counter += 1; + + if value != 0 { + counter += 1; + } + } + } +} From f309934b5ee074a74229546d859252dd7e946b2d Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 16 Dec 2022 10:11:37 -0700 Subject: [PATCH 130/321] rustdoc: clean up margin CSS for scraped examples * This stops applying a margin to the additional example links. Because these links are `display: inline`, it doesn't actually do anything. * This switches from using a margin-bottom with a special exception for the examples themselves, plus an additional margin on the hide button, to instead using just margin-top on the examples, with an exception for the first one. No user-visible changes should result from this. --- src/librustdoc/html/static/css/rustdoc.css | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index a527e9feaec3..7b0e6439cfea 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -2009,7 +2009,6 @@ in storage.js .more-examples-toggle .hide-more { margin-left: 25px; - margin-bottom: 5px; cursor: pointer; } @@ -2037,18 +2036,14 @@ in storage.js height: 100%; } -.more-scraped-examples .scraped-example { - margin-bottom: 20px; -} - -.more-scraped-examples .scraped-example:last-child { - margin-bottom: 0; -} - -.example-links a { +.more-scraped-examples .scraped-example, .example-links { margin-top: 20px; } +.more-scraped-examples .scraped-example:first-child { + margin-top: 5px; +} + .example-links ul { margin-bottom: 0; } From fe528829866f08bac8cf6615085ec4609db352e9 Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Fri, 16 Dec 2022 18:24:14 +1300 Subject: [PATCH 131/321] docs: add long error explanation for error E0320 --- compiler/rustc_error_codes/src/error_codes.rs | 2 +- .../src/error_codes/E0320.md | 27 +++++++++++++++++++ .../dropck_no_diverge_on_nonregular_1.stderr | 1 + .../dropck_no_diverge_on_nonregular_2.stderr | 1 + .../dropck_no_diverge_on_nonregular_3.stderr | 1 + ...-38591-non-regular-dropck-recursion.stderr | 1 + 6 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0320.md diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 31a709c36d4b..67c512e98d68 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -163,6 +163,7 @@ E0311: include_str!("./error_codes/E0311.md"), E0312: include_str!("./error_codes/E0312.md"), E0316: include_str!("./error_codes/E0316.md"), E0317: include_str!("./error_codes/E0317.md"), +E0320: include_str!("./error_codes/E0320.md"), E0321: include_str!("./error_codes/E0321.md"), E0322: include_str!("./error_codes/E0322.md"), E0323: include_str!("./error_codes/E0323.md"), @@ -575,7 +576,6 @@ E0791: include_str!("./error_codes/E0791.md"), // E0314, // closure outlives stack frame // E0315, // cannot invoke closure outside of its lifetime // E0319, // trait impls for defaulted traits allowed just for structs/enums - E0320, // recursive overflow during dropck // E0372, // coherence not object safe E0377, // the trait `CoerceUnsized` may only be implemented for a coercion // between structures with the same definition diff --git a/compiler/rustc_error_codes/src/error_codes/E0320.md b/compiler/rustc_error_codes/src/error_codes/E0320.md new file mode 100644 index 000000000000..e6e1b7c19a56 --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0320.md @@ -0,0 +1,27 @@ +Recursion limit reached while creating drop-check rules. + +Example of erroneous code: + +```compile_fail,E0320 +enum A { + B, + C(T, Box>) +} + +fn foo() { + A::::B; // error: overflow while adding drop-check rules for A +} +``` + +The Rust compiler must be able to reason about how a type is [`Drop`]ped, and +by extension the types of its fields, to be able to generate the glue to +properly drop a value. The code example above shows a type where this inference +is impossible because it is recursive. Note that this is *not* the same as +[E0072](E0072.html), where a type has an infinite size; the type here has a +finite size but any attempt to `Drop` it would recurse infinitely. For more +information, read [the `Drop` docs](../std/ops/trait.Drop.html). + +It is not possible to define a type with recursive drop-check rules. All such +recursion must be removed. + +[`Drop`]: ../std/ops/trait.Drop.html diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr index c447e2f7987c..3e39d15f9b0c 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_1.stderr @@ -8,3 +8,4 @@ LL | let ft = error: aborting due to previous error +For more information about this error, try `rustc --explain E0320`. diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr index cd4706dd903f..dbb743544713 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_2.stderr @@ -8,3 +8,4 @@ LL | let ft = error: aborting due to previous error +For more information about this error, try `rustc --explain E0320`. diff --git a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr index 18cd1b6cd413..deaf116b647a 100644 --- a/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr +++ b/src/test/ui/dropck/dropck_no_diverge_on_nonregular_3.stderr @@ -16,3 +16,4 @@ LL | Some(Wrapper::Simple::); error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0320`. diff --git a/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr b/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr index 1da29be43db8..002dfe115b03 100644 --- a/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr +++ b/src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.stderr @@ -8,3 +8,4 @@ LL | fn f(x: S) {} error: aborting due to previous error +For more information about this error, try `rustc --explain E0320`. From 3d852f60aae5e720719626b367a83db6e839d5cd Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Fri, 16 Dec 2022 19:33:24 +0000 Subject: [PATCH 132/321] Test documentation of hidden items in std --- src/ci/docker/host-x86_64/mingw-check/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ci/docker/host-x86_64/mingw-check/Dockerfile b/src/ci/docker/host-x86_64/mingw-check/Dockerfile index 40caa7c50135..076a427c988b 100644 --- a/src/ci/docker/host-x86_64/mingw-check/Dockerfile +++ b/src/ci/docker/host-x86_64/mingw-check/Dockerfile @@ -46,7 +46,7 @@ ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \ python3 ../x.py test --stage 2 src/tools/tidy && \ python3 ../x.py test --stage 0 core alloc std test proc_macro && \ # Build both public and internal documentation. - RUSTDOCFLAGS="--document-private-items" python3 ../x.py doc --stage 0 library/test && \ + RUSTDOCFLAGS=\"--document-private-items --document-hidden-items\" python3 ../x.py doc --stage 0 library/test && \ /scripts/validate-toolstate.sh && \ /scripts/validate-error-codes.sh && \ reuse lint && \ From 8534fd30892fc6e29d497da70798d2d7954aa09b Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Fri, 16 Dec 2022 19:46:14 +0000 Subject: [PATCH 133/321] Fix intra-doc link --- library/core/src/fmt/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 48b6177434bc..122da675f7e4 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -405,7 +405,7 @@ impl<'a> Arguments<'a> { /// 1. The `pieces` slice must be at least as long as `fmt`. /// 2. Every [`rt::v1::Argument::position`] value within `fmt` must be a /// valid index of `args`. - /// 3. Every [`Count::Param`] within `fmt` must contain a valid index of + /// 3. Every [`rt::v1::Count::Param`] within `fmt` must contain a valid index of /// `args`. #[doc(hidden)] #[inline] From d2f4a9ca25a198fd3d7580d7c14fd94a447269a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Fri, 16 Dec 2022 20:37:53 +0000 Subject: [PATCH 134/321] don't copy symbols from dylibs with -Zdylib-lto --- compiler/rustc_codegen_ssa/src/back/link.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 882430694e16..fe2e4b36cd0f 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -253,7 +253,7 @@ pub fn each_linked_rlib( }; for &cnum in crates { match fmts.get(cnum.as_usize() - 1) { - Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue, + Some(&Linkage::NotLinked | &Linkage::Dynamic | &Linkage::IncludedFromDylib) => continue, Some(_) => {} None => return Err(errors::LinkRlibError::MissingFormat), } From be5685bb4d2190b6f5274d6d23dce3542cdf95b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Fri, 16 Dec 2022 20:38:32 +0000 Subject: [PATCH 135/321] add simulated non-regression test for issue 105637 --- src/test/ui/lto/auxiliary/thinlto-dylib.rs | 23 ++++++++++++++++++ src/test/ui/lto/issue-105637.rs | 28 ++++++++++++++++++++++ src/test/ui/lto/issue-105637.run.stderr | 1 + 3 files changed, 52 insertions(+) create mode 100644 src/test/ui/lto/auxiliary/thinlto-dylib.rs create mode 100644 src/test/ui/lto/issue-105637.rs create mode 100644 src/test/ui/lto/issue-105637.run.stderr diff --git a/src/test/ui/lto/auxiliary/thinlto-dylib.rs b/src/test/ui/lto/auxiliary/thinlto-dylib.rs new file mode 100644 index 000000000000..9d17c35dafc2 --- /dev/null +++ b/src/test/ui/lto/auxiliary/thinlto-dylib.rs @@ -0,0 +1,23 @@ +// Auxiliary crate for test issue-105637: the LTOed dylib which had duplicate symbols from libstd, +// breaking the panic hook feature. +// +// This simulates the `rustc_driver` crate, and the main crate simulates rustc's main binary hooking +// into this driver. + +// compile-flags: -Zdylib-lto -C lto=thin + +use std::panic; + +pub fn main() { + // Install the hook we want to see executed + panic::set_hook(Box::new(|_| { + eprintln!("LTOed auxiliary crate panic hook"); + })); + + // Trigger the panic hook with an ICE + run_compiler(); +} + +fn run_compiler() { + panic!("ICEing"); +} diff --git a/src/test/ui/lto/issue-105637.rs b/src/test/ui/lto/issue-105637.rs new file mode 100644 index 000000000000..0d9f0bec00fd --- /dev/null +++ b/src/test/ui/lto/issue-105637.rs @@ -0,0 +1,28 @@ +// Regression test for issue #105637: `-Zdylib-lto` with LTO duplicated symbols from other dylibs, +// in this case from libstd. +// +// That manifested as both `rustc_driver` and rustc's "main" (`compiler/rustc`) having their own +// `std::panicking::HOOK` static, and the hook in rustc's main (the default stdlib's) being executed +// when rustc ICEs, instead of the overriden hook from `rustc_driver` (which also displays the query +// stack and information on how to open a GH issue for the encountered ICE). +// +// In this test, we reproduce this setup by installing a panic hook in both the main and an LTOed +// dylib: the last hook set should be the one being executed, the dylib's. + +// aux-build: thinlto-dylib.rs +// run-fail +// check-run-results + +extern crate thinlto_dylib; + +use std::panic; + +fn main() { + // We don't want to see this panic hook executed + std::panic::set_hook(Box::new(|_| { + eprintln!("main crate panic hook"); + })); + + // Have the LTOed dylib install its own hook and panic, we want to see its hook executed. + thinlto_dylib::main(); +} diff --git a/src/test/ui/lto/issue-105637.run.stderr b/src/test/ui/lto/issue-105637.run.stderr new file mode 100644 index 000000000000..43388e7763ee --- /dev/null +++ b/src/test/ui/lto/issue-105637.run.stderr @@ -0,0 +1 @@ +LTOed auxiliary crate panic hook From c68d2e4b879abc32d3fca6179d344d08dc1c73f6 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Fri, 16 Dec 2022 16:50:43 -0500 Subject: [PATCH 136/321] Realistic `Path::as_mut_os_str` doctest --- library/std/src/path.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index a835b855dddf..73b5056e9326 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2039,12 +2039,12 @@ impl Path { /// #![feature(path_as_mut_os_str)] /// use std::path::{Path, PathBuf}; /// - /// let mut path = PathBuf::from("/Foo.TXT").into_boxed_path(); + /// let mut path = PathBuf::from("Foo.TXT"); /// - /// assert_ne!(&*path, Path::new("/foo.txt")); + /// assert_ne!(path, Path::new("foo.txt")); /// /// path.as_mut_os_str().make_ascii_lowercase(); - /// assert_eq!(&*path, Path::new("/foo.txt")); + /// assert_eq!(path, Path::new("foo.txt")); /// ``` #[unstable(feature = "path_as_mut_os_str", issue = "105021")] #[must_use] From 8751d3b2e9a2fa903822a1f9061c0fe2c4c0bef2 Mon Sep 17 00:00:00 2001 From: Collin Baker Date: Fri, 16 Dec 2022 17:32:29 -0500 Subject: [PATCH 137/321] Make enum-match.rs test robust against variable name changes https://reviews.llvm.org/D140192 caused the LLVM variable generated for enum discriminant checks to be named differently (%narrow vs %1). This adjusts the test CHECK directives to match any name. --- src/test/codegen/enum-match.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/codegen/enum-match.rs b/src/test/codegen/enum-match.rs index 44f1b408d21b..827eb20154af 100644 --- a/src/test/codegen/enum-match.rs +++ b/src/test/codegen/enum-match.rs @@ -34,8 +34,8 @@ pub enum Enum1 { // CHECK: define i8 @match1{{.*}} // CHECK-NEXT: start: -// CHECK-NEXT: %1 = {{.*}}call i8 @llvm.usub.sat.i8(i8 %0, i8 1) -// CHECK-NEXT: switch i8 %1, label {{.*}} [ +// CHECK-NEXT: [[DISCR:%.*]] = {{.*}}call i8 @llvm.usub.sat.i8(i8 %0, i8 1) +// CHECK-NEXT: switch i8 [[DISCR]], label {{.*}} [ #[no_mangle] pub fn match1(e: Enum1) -> u8 { use Enum1::*; From af7f2222d334145d37172d2c45d01277f86c6041 Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Sat, 17 Dec 2022 00:19:59 +0100 Subject: [PATCH 138/321] core: ensure `no_fp_fmt_parse` builds are warning-free Rust recently introduced a new `unused_imports` warning in `no_fp_fmt_parse` builds, which was fixed in https://github.com/rust-lang/rust/pull/105434. To avoid accumulating more over time, let's keep the builds warning-free. This ensures projects compiling `core` with this custom config do not see the warnings in the future and that they can keep enabling `-Dwarnings`. Similarly, https://github.com/rust-lang/rust/pull/98652 did it for `alloc`'s `no_global_oom_handling`. Signed-off-by: Miguel Ojeda --- src/test/run-make-fulldeps/core-no-fp-fmt-parse/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-make-fulldeps/core-no-fp-fmt-parse/Makefile b/src/test/run-make-fulldeps/core-no-fp-fmt-parse/Makefile index d083aaa66200..ec05ebea5558 100644 --- a/src/test/run-make-fulldeps/core-no-fp-fmt-parse/Makefile +++ b/src/test/run-make-fulldeps/core-no-fp-fmt-parse/Makefile @@ -1,4 +1,4 @@ include ../tools.mk all: - $(RUSTC) --edition=2021 --crate-type=rlib ../../../../library/core/src/lib.rs --cfg no_fp_fmt_parse + $(RUSTC) --edition=2021 -Dwarnings --crate-type=rlib ../../../../library/core/src/lib.rs --cfg no_fp_fmt_parse From 3d849ae44c0eabea326bd8ccccc3a6ab702115ff Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Fri, 16 Dec 2022 19:41:40 -0800 Subject: [PATCH 139/321] Support call and drop terminators in custom mir --- .../src/build/custom/parse/instruction.rs | 49 ++++++++ library/core/src/intrinsics/mir.rs | 39 ++++++- ...terminators.assert_nonzero.built.after.mir | 17 +++ .../terminators.direct_call.built.after.mir | 16 +++ .../terminators.drop_first.built.after.mir | 13 +++ .../terminators.drop_second.built.after.mir | 13 +++ .../terminators.indirect_call.built.after.mir | 13 +++ .../mir-opt/building/custom/terminators.rs | 108 ++++++++++++++++++ 8 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 src/test/mir-opt/building/custom/terminators.assert_nonzero.built.after.mir create mode 100644 src/test/mir-opt/building/custom/terminators.direct_call.built.after.mir create mode 100644 src/test/mir-opt/building/custom/terminators.drop_first.built.after.mir create mode 100644 src/test/mir-opt/building/custom/terminators.drop_second.built.after.mir create mode 100644 src/test/mir-opt/building/custom/terminators.indirect_call.built.after.mir create mode 100644 src/test/mir-opt/building/custom/terminators.rs diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs index 2f26499a3b6e..ecc3e4de8d1d 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs @@ -42,6 +42,29 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { @call("mir_goto", args) => { Ok(TerminatorKind::Goto { target: self.parse_block(args[0])? } ) }, + @call("mir_unreachable", _args) => { + Ok(TerminatorKind::Unreachable) + }, + @call("mir_drop", args) => { + Ok(TerminatorKind::Drop { + place: self.parse_place(args[0])?, + target: self.parse_block(args[1])?, + unwind: None, + }) + }, + @call("mir_drop_and_replace", args) => { + Ok(TerminatorKind::DropAndReplace { + place: self.parse_place(args[0])?, + value: self.parse_operand(args[1])?, + target: self.parse_block(args[2])?, + unwind: None, + }) + }, + @call("mir_call", args) => { + let destination = self.parse_place(args[0])?; + let target = self.parse_block(args[1])?; + self.parse_call(args[2], destination, target) + }, ExprKind::Match { scrutinee, arms } => { let discr = self.parse_operand(*scrutinee)?; self.parse_match(arms, expr.span).map(|t| TerminatorKind::SwitchInt { discr, targets: t }) @@ -86,6 +109,32 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { Ok(SwitchTargets::new(values.into_iter().zip(targets), otherwise)) } + fn parse_call( + &self, + expr_id: ExprId, + destination: Place<'tcx>, + target: BasicBlock, + ) -> PResult> { + parse_by_kind!(self, expr_id, _, "function call", + ExprKind::Call { fun, args, from_hir_call, fn_span, .. } => { + let fun = self.parse_operand(*fun)?; + let args = args + .iter() + .map(|arg| self.parse_operand(*arg)) + .collect::>>()?; + Ok(TerminatorKind::Call { + func: fun, + args, + destination, + target: Some(target), + cleanup: None, + from_hir_call: *from_hir_call, + fn_span: *fn_span, + }) + }, + ) + } + fn parse_rvalue(&self, expr_id: ExprId) -> PResult> { parse_by_kind!(self, expr_id, _, "rvalue", @call("mir_discriminant", args) => self.parse_place(args[0]).map(Rvalue::Discriminant), diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs index 0910ce599b7c..e08a15571fcb 100644 --- a/library/core/src/intrinsics/mir.rs +++ b/library/core/src/intrinsics/mir.rs @@ -44,7 +44,8 @@ //! if you want your MIR to be modified by the full MIR pipeline, or `#![custom_mir(dialect = //! "runtime", phase = "optimized")] if you don't. //! -//! [dialect docs]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.MirPhase.html +//! [dialect docs]: +//! https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.MirPhase.html //! //! The input to the [`mir!`] macro is: //! @@ -99,6 +100,30 @@ //! Return() //! }) //! } +//! +//! #[custom_mir(dialect = "runtime", phase = "optimized")] +//! fn push_and_pop(v: &mut Vec, value: T) { +//! mir!( +//! let unused; +//! let popped; +//! +//! { +//! Call(unused, pop, Vec::push(v, value)) +//! } +//! +//! pop = { +//! Call(popped, drop, Vec::pop(v)) +//! } +//! +//! drop = { +//! Drop(popped, ret) +//! } +//! +//! ret = { +//! Return() +//! } +//! ) +//! } //! ``` //! //! We can also set off compilation failures that happen in sufficiently late stages of the @@ -195,10 +220,16 @@ //! //! #### Terminators //! -//! - [`Goto`] and [`Return`] have associated functions. +//! Custom MIR does not currently support cleanup blocks or non-trivial unwind paths. As such, there +//! are no resume and abort terminators, and terminators that might unwind do not have any way to +//! indicate the unwind block. +//! +//! - [`Goto`], [`Return`], [`Unreachable`], [`Drop`](Drop()), and [`DropAndReplace`] have associated functions. //! - `match some_int_operand` becomes a `SwitchInt`. Each arm should be `literal => basic_block` //! - The exception is the last arm, which must be `_ => basic_block` and corresponds to the //! otherwise branch. +//! - [`Call`] has an associated function as well. The third argument of this function is a normal +//! function call expresion, for example `my_other_function(a, 5)`. //! #![unstable( @@ -223,6 +254,10 @@ macro_rules! define { define!("mir_return", fn Return() -> BasicBlock); define!("mir_goto", fn Goto(destination: BasicBlock) -> BasicBlock); +define!("mir_unreachable", fn Unreachable() -> BasicBlock); +define!("mir_drop", fn Drop(place: T, goto: BasicBlock)); +define!("mir_drop_and_replace", fn DropAndReplace(place: T, value: T, goto: BasicBlock)); +define!("mir_call", fn Call(place: T, goto: BasicBlock, call: T)); define!("mir_retag", fn Retag(place: T)); define!("mir_retag_raw", fn RetagRaw(place: T)); define!("mir_move", fn Move(place: T) -> T); diff --git a/src/test/mir-opt/building/custom/terminators.assert_nonzero.built.after.mir b/src/test/mir-opt/building/custom/terminators.assert_nonzero.built.after.mir new file mode 100644 index 000000000000..a1a27226b4e9 --- /dev/null +++ b/src/test/mir-opt/building/custom/terminators.assert_nonzero.built.after.mir @@ -0,0 +1,17 @@ +// MIR for `assert_nonzero` after built + +fn assert_nonzero(_1: i32) -> () { + let mut _0: (); // return place in scope 0 at $DIR/terminators.rs:+0:27: +0:27 + + bb0: { + switchInt(_1) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/terminators.rs:+3:13: +6:14 + } + + bb1: { + unreachable; // scope 0 at $DIR/terminators.rs:+10:13: +10:26 + } + + bb2: { + return; // scope 0 at $DIR/terminators.rs:+14:13: +14:21 + } +} diff --git a/src/test/mir-opt/building/custom/terminators.direct_call.built.after.mir b/src/test/mir-opt/building/custom/terminators.direct_call.built.after.mir new file mode 100644 index 000000000000..1b2345a965ed --- /dev/null +++ b/src/test/mir-opt/building/custom/terminators.direct_call.built.after.mir @@ -0,0 +1,16 @@ +// MIR for `direct_call` after built + +fn direct_call(_1: i32) -> i32 { + let mut _0: i32; // return place in scope 0 at $DIR/terminators.rs:+0:27: +0:30 + + bb0: { + _0 = ident::(_1) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:42 + // mir::Constant + // + span: $DIR/terminators.rs:15:33: 15:38 + // + literal: Const { ty: fn(i32) -> i32 {ident::}, val: Value() } + } + + bb1: { + return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 + } +} diff --git a/src/test/mir-opt/building/custom/terminators.drop_first.built.after.mir b/src/test/mir-opt/building/custom/terminators.drop_first.built.after.mir new file mode 100644 index 000000000000..c903e5946961 --- /dev/null +++ b/src/test/mir-opt/building/custom/terminators.drop_first.built.after.mir @@ -0,0 +1,13 @@ +// MIR for `drop_first` after built + +fn drop_first(_1: WriteOnDrop<'_>, _2: WriteOnDrop<'_>) -> () { + let mut _0: (); // return place in scope 0 at $DIR/terminators.rs:+0:59: +0:59 + + bb0: { + replace(_1 <- move _2) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:49 + } + + bb1: { + return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 + } +} diff --git a/src/test/mir-opt/building/custom/terminators.drop_second.built.after.mir b/src/test/mir-opt/building/custom/terminators.drop_second.built.after.mir new file mode 100644 index 000000000000..f14246f2d120 --- /dev/null +++ b/src/test/mir-opt/building/custom/terminators.drop_second.built.after.mir @@ -0,0 +1,13 @@ +// MIR for `drop_second` after built + +fn drop_second(_1: WriteOnDrop<'_>, _2: WriteOnDrop<'_>) -> () { + let mut _0: (); // return place in scope 0 at $DIR/terminators.rs:+0:60: +0:60 + + bb0: { + drop(_2) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:30 + } + + bb1: { + return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 + } +} diff --git a/src/test/mir-opt/building/custom/terminators.indirect_call.built.after.mir b/src/test/mir-opt/building/custom/terminators.indirect_call.built.after.mir new file mode 100644 index 000000000000..2f1b14069abe --- /dev/null +++ b/src/test/mir-opt/building/custom/terminators.indirect_call.built.after.mir @@ -0,0 +1,13 @@ +// MIR for `indirect_call` after built + +fn indirect_call(_1: i32, _2: fn(i32) -> i32) -> i32 { + let mut _0: i32; // return place in scope 0 at $DIR/terminators.rs:+0:48: +0:51 + + bb0: { + _0 = _2(_1) -> bb1; // scope 0 at $DIR/terminators.rs:+3:13: +3:38 + } + + bb1: { + return; // scope 0 at $DIR/terminators.rs:+7:13: +7:21 + } +} diff --git a/src/test/mir-opt/building/custom/terminators.rs b/src/test/mir-opt/building/custom/terminators.rs new file mode 100644 index 000000000000..c23233fcf9ac --- /dev/null +++ b/src/test/mir-opt/building/custom/terminators.rs @@ -0,0 +1,108 @@ +#![feature(custom_mir, core_intrinsics)] + +extern crate core; +use core::intrinsics::mir::*; + +fn ident(t: T) -> T { + t +} + +// EMIT_MIR terminators.direct_call.built.after.mir +#[custom_mir(dialect = "built")] +fn direct_call(x: i32) -> i32 { + mir!( + { + Call(RET, retblock, ident(x)) + } + + retblock = { + Return() + } + ) +} + +// EMIT_MIR terminators.indirect_call.built.after.mir +#[custom_mir(dialect = "built")] +fn indirect_call(x: i32, f: fn(i32) -> i32) -> i32 { + mir!( + { + Call(RET, retblock, f(x)) + } + + retblock = { + Return() + } + ) +} + +struct WriteOnDrop<'a>(&'a mut i32, i32); + +impl<'a> Drop for WriteOnDrop<'a> { + fn drop(&mut self) { + *self.0 = self.1; + } +} + +// EMIT_MIR terminators.drop_first.built.after.mir +#[custom_mir(dialect = "built")] +fn drop_first<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) { + mir!( + { + DropAndReplace(a, Move(b), retblock) + } + + retblock = { + Return() + } + ) +} + +// EMIT_MIR terminators.drop_second.built.after.mir +#[custom_mir(dialect = "built")] +fn drop_second<'a>(a: WriteOnDrop<'a>, b: WriteOnDrop<'a>) { + mir!( + { + Drop(b, retblock) + } + + retblock = { + Return() + } + ) +} + +// EMIT_MIR terminators.assert_nonzero.built.after.mir +#[custom_mir(dialect = "built")] +fn assert_nonzero(a: i32) { + mir!( + { + match a { + 0 => unreachable, + _ => retblock + } + } + + unreachable = { + Unreachable() + } + + retblock = { + Return() + } + ) +} + +fn main() { + assert_eq!(direct_call(5), 5); + assert_eq!(indirect_call(5, ident), 5); + + let mut a = 0; + let mut b = 0; + drop_first(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1)); + assert_eq!((a, b), (1, 0)); + + let mut a = 0; + let mut b = 0; + drop_second(WriteOnDrop(&mut a, 1), WriteOnDrop(&mut b, 1)); + assert_eq!((a, b), (0, 1)); +} From 7f9438910c2c86813b597dfc518990198691d170 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Sat, 17 Dec 2022 12:20:56 +0100 Subject: [PATCH 140/321] str.lines() docstring: clarify that line endings are not returned Previously, the str.lines() docstring stated that lines are split at line endings, but not whether those were returned or not. This new version of the docstring states this explicitly, avoiding the need of getting to doctests to get an answer to this FAQ. --- library/core/src/str/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 45fd2caae52f..863ded5e5ec0 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -970,8 +970,10 @@ impl str { /// An iterator over the lines of a string, as string slices. /// - /// Lines are ended with either a newline (`\n`) or a carriage return with - /// a line feed (`\r\n`). + /// Lines are split at line endings that are either newlines (`\n`) or + /// sequences of a carriage return followed by a line feed (`\r\n`). + /// + /// Line terminators are not included in the lines returned by the iterator. /// /// The final line ending is optional. A string that ends with a final line /// ending will return the same lines as an otherwise identical string From c39849a34dae51d104f197894ed5a9f02dd62d75 Mon Sep 17 00:00:00 2001 From: feniljain Date: Sat, 17 Dec 2022 17:24:03 +0530 Subject: [PATCH 141/321] fix: not suggest seek_to_start_instead_of_rewind when expr is used --- clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs | 6 +++++- tests/ui/seek_to_start_instead_of_rewind.fixed | 6 ++++++ tests/ui/seek_to_start_instead_of_rewind.rs | 6 ++++++ tests/ui/seek_to_start_instead_of_rewind.stderr | 2 +- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs b/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs index 7e3bed1e41a9..660b7049cce9 100644 --- a/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs +++ b/clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::ty::implements_trait; -use clippy_utils::{get_trait_def_id, match_def_path, paths}; +use clippy_utils::{get_trait_def_id, is_expr_used_or_unified, match_def_path, paths}; use rustc_ast::ast::{LitIntType, LitKind}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; @@ -19,6 +19,10 @@ pub(super) fn check<'tcx>( // Get receiver type let ty = cx.typeck_results().expr_ty(recv).peel_refs(); + if is_expr_used_or_unified(cx.tcx, expr) { + return; + } + if let Some(seek_trait_id) = get_trait_def_id(cx, &paths::STD_IO_SEEK) && implements_trait(cx, ty, seek_trait_id, &[]) && let ExprKind::Call(func, args1) = arg.kind && diff --git a/tests/ui/seek_to_start_instead_of_rewind.fixed b/tests/ui/seek_to_start_instead_of_rewind.fixed index 9d0d1124c460..713cff604a1d 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.fixed +++ b/tests/ui/seek_to_start_instead_of_rewind.fixed @@ -70,6 +70,12 @@ fn seek_to_end(t: &mut T) { t.seek(SeekFrom::End(0)); } +// This should NOT trigger clippy warning because +// expr is used here +fn seek_to_start_in_let(t: &mut T) { + let a = t.seek(SeekFrom::Start(0)).unwrap(); +} + fn main() { let mut f = OpenOptions::new() .write(true) diff --git a/tests/ui/seek_to_start_instead_of_rewind.rs b/tests/ui/seek_to_start_instead_of_rewind.rs index c5bc57cc3a74..467003a1a66f 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.rs +++ b/tests/ui/seek_to_start_instead_of_rewind.rs @@ -70,6 +70,12 @@ fn seek_to_end(t: &mut T) { t.seek(SeekFrom::End(0)); } +// This should NOT trigger clippy warning because +// expr is used here +fn seek_to_start_in_let(t: &mut T) { + let a = t.seek(SeekFrom::Start(0)).unwrap(); +} + fn main() { let mut f = OpenOptions::new() .write(true) diff --git a/tests/ui/seek_to_start_instead_of_rewind.stderr b/tests/ui/seek_to_start_instead_of_rewind.stderr index 6cce025359fe..342ec00fe729 100644 --- a/tests/ui/seek_to_start_instead_of_rewind.stderr +++ b/tests/ui/seek_to_start_instead_of_rewind.stderr @@ -13,7 +13,7 @@ LL | t.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` error: used `seek` to go to the start of the stream - --> $DIR/seek_to_start_instead_of_rewind.rs:128:7 + --> $DIR/seek_to_start_instead_of_rewind.rs:134:7 | LL | f.seek(SeekFrom::Start(0)); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `rewind()` From 1f1d23cf5162e621132e069fc48d5167364be0df Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Sat, 17 Dec 2022 13:57:41 +0100 Subject: [PATCH 142/321] Bump Clippy version -> 0.1.68 --- Cargo.toml | 2 +- clippy_lints/Cargo.toml | 2 +- clippy_utils/Cargo.toml | 2 +- declare_clippy_lint/Cargo.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4400f4c0aadb..e1b15cc49da4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy" -version = "0.1.67" +version = "0.1.68" description = "A bunch of helpful lints to avoid common pitfalls in Rust" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" diff --git a/clippy_lints/Cargo.toml b/clippy_lints/Cargo.toml index dcadd012a44d..38a87017635b 100644 --- a/clippy_lints/Cargo.toml +++ b/clippy_lints/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy_lints" -version = "0.1.67" +version = "0.1.68" description = "A bunch of helpful lints to avoid common pitfalls in Rust" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" diff --git a/clippy_utils/Cargo.toml b/clippy_utils/Cargo.toml index fb9f4740ecc5..ac6a566b9cd3 100644 --- a/clippy_utils/Cargo.toml +++ b/clippy_utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy_utils" -version = "0.1.67" +version = "0.1.68" edition = "2021" publish = false diff --git a/declare_clippy_lint/Cargo.toml b/declare_clippy_lint/Cargo.toml index 082570f1fe5d..c01e1062cb54 100644 --- a/declare_clippy_lint/Cargo.toml +++ b/declare_clippy_lint/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "declare_clippy_lint" -version = "0.1.67" +version = "0.1.68" edition = "2021" publish = false From 7198989d45e4e47ccc30077c522468cac172766c Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Sat, 17 Dec 2022 13:57:48 +0100 Subject: [PATCH 143/321] Bump nightly version -> 2022-12-17 --- rust-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain b/rust-toolchain index 19fee38db46e..8e21cef32abb 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-12-01" +channel = "nightly-2022-12-17" components = ["cargo", "llvm-tools", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"] From 10f2a6de758f1b9d3fe7da81181d420d586854a6 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Sat, 17 Dec 2022 14:21:37 +0100 Subject: [PATCH 144/321] Update Cargo.lock --- Cargo.lock | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d05a09f0389..75c3df5b2b74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -394,7 +394,7 @@ dependencies = [ "directories", "rustc-build-sysroot", "rustc-workspace-hack", - "rustc_tools_util", + "rustc_tools_util 0.2.1", "rustc_version", "serde", "serde_json", @@ -658,7 +658,7 @@ dependencies = [ [[package]] name = "clippy" -version = "0.1.67" +version = "0.1.68" dependencies = [ "clippy_lints", "clippy_utils", @@ -673,7 +673,7 @@ dependencies = [ "regex", "rustc-semver", "rustc-workspace-hack", - "rustc_tools_util", + "rustc_tools_util 0.3.0", "semver", "serde", "syn", @@ -700,7 +700,7 @@ dependencies = [ [[package]] name = "clippy_lints" -version = "0.1.67" +version = "0.1.68" dependencies = [ "cargo_metadata 0.14.0", "clippy_utils", @@ -723,7 +723,7 @@ dependencies = [ [[package]] name = "clippy_utils" -version = "0.1.67" +version = "0.1.68" dependencies = [ "arrayvec", "if_chain", @@ -1068,7 +1068,7 @@ checksum = "a0afaad2b26fa326569eb264b1363e8ae3357618c43982b3f285f0774ce76b69" [[package]] name = "declare_clippy_lint" -version = "0.1.67" +version = "0.1.68" dependencies = [ "itertools", "quote", @@ -4429,6 +4429,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "598f48ce2a421542b3e64828aa742b687cc1b91d2f96591cfdb7ac5988cd6366" +[[package]] +name = "rustc_tools_util" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ba09476327c4b70ccefb6180f046ef588c26a24cf5d269a9feba316eb4f029f" + [[package]] name = "rustc_trait_selection" version = "0.0.0" From f7bfc4879390117e850da74ad73eb9f9df588350 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Tue, 22 Nov 2022 00:25:43 +0100 Subject: [PATCH 145/321] optimize line-by-line style checks in tidy --- src/tools/tidy/src/style.rs | 59 ++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/src/tools/tidy/src/style.rs b/src/tools/tidy/src/style.rs index e3a094caf919..f91e38262f64 100644 --- a/src/tools/tidy/src/style.rs +++ b/src/tools/tidy/src/style.rs @@ -17,7 +17,7 @@ //! `// ignore-tidy-CHECK-NAME`. use crate::walk::{filter_dirs, walk}; -use regex::Regex; +use regex::{Regex, RegexSet}; use std::path::Path; /// Error code markdown is restricted to 80 columns because they can be @@ -225,6 +225,7 @@ pub fn check(path: &Path, bad: &mut bool) { .chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:x}", v))) .chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:X}", v))) .collect(); + let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap(); walk(path, &mut skip, &mut |entry, contents| { let file = entry.path(); let filename = file.file_name().unwrap().to_string_lossy(); @@ -281,7 +282,27 @@ pub fn check(path: &Path, bad: &mut bool) { let mut trailing_new_lines = 0; let mut lines = 0; let mut last_safety_comment = false; + let is_test = file.components().any(|c| c.as_os_str() == "tests"); + // scanning the whole file for multiple needles at once is more efficient than + // executing lines times needles separate searches. + let any_problematic_line = problematic_regex.is_match(contents); for (i, line) in contents.split('\n').enumerate() { + if line.is_empty() { + if i == 0 { + leading_new_lines = true; + } + trailing_new_lines += 1; + continue; + } else { + trailing_new_lines = 0; + } + + let trimmed = line.trim(); + + if !trimmed.starts_with("//") { + lines += 1; + } + let mut err = |msg: &str| { tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg); }; @@ -308,28 +329,29 @@ pub fn check(path: &Path, bad: &mut bool) { suppressible_tidy_err!(err, skip_cr, "CR character"); } if filename != "style.rs" { - if line.contains("TODO") { + if trimmed.contains("TODO") { err("TODO is deprecated; use FIXME") } - if line.contains("//") && line.contains(" XXX") { + if trimmed.contains("//") && trimmed.contains(" XXX") { err("XXX is deprecated; use FIXME") } - for s in problematic_consts_strings.iter() { - if line.contains(s) { - err("Don't use magic numbers that spell things (consider 0x12345678)"); + if any_problematic_line { + for s in problematic_consts_strings.iter() { + if trimmed.contains(s) { + err("Don't use magic numbers that spell things (consider 0x12345678)"); + } } } } - let is_test = || file.components().any(|c| c.as_os_str() == "tests"); // for now we just check libcore - if line.contains("unsafe {") && !line.trim().starts_with("//") && !last_safety_comment { - if file.components().any(|c| c.as_os_str() == "core") && !is_test() { + if trimmed.contains("unsafe {") && !trimmed.starts_with("//") && !last_safety_comment { + if file.components().any(|c| c.as_os_str() == "core") && !is_test { suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe"); } } - if line.contains("// SAFETY:") { + if trimmed.contains("// SAFETY:") { last_safety_comment = true; - } else if line.trim().starts_with("//") || line.trim().is_empty() { + } else if trimmed.starts_with("//") || trimmed.is_empty() { // keep previous value } else { last_safety_comment = false; @@ -337,7 +359,8 @@ pub fn check(path: &Path, bad: &mut bool) { if (line.starts_with("// Copyright") || line.starts_with("# Copyright") || line.starts_with("Copyright")) - && (line.contains("Rust Developers") || line.contains("Rust Project Developers")) + && (trimmed.contains("Rust Developers") + || trimmed.contains("Rust Project Developers")) { suppressible_tidy_err!( err, @@ -351,18 +374,6 @@ pub fn check(path: &Path, bad: &mut bool) { if filename.ends_with(".cpp") && line.contains("llvm_unreachable") { err(LLVM_UNREACHABLE_INFO); } - if line.is_empty() { - if i == 0 { - leading_new_lines = true; - } - trailing_new_lines += 1; - } else { - trailing_new_lines = 0; - } - - if !line.trim().starts_with("//") { - lines += 1; - } } if leading_new_lines { let mut err = |_| { From 5620afc4e088beb299dfdcc3c3a903ac1f80cc9f Mon Sep 17 00:00:00 2001 From: The 8472 Date: Tue, 22 Nov 2022 00:30:05 +0100 Subject: [PATCH 146/321] poll tidy threads for completion before waiting --- src/tools/tidy/src/main.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/tools/tidy/src/main.rs b/src/tools/tidy/src/main.rs index b0b11cafca5a..6714c63ee62a 100644 --- a/src/tools/tidy/src/main.rs +++ b/src/tools/tidy/src/main.rs @@ -35,15 +35,26 @@ fn main() { let bad = std::sync::Arc::new(AtomicBool::new(false)); + let drain_handles = |handles: &mut VecDeque>| { + // poll all threads for completion before awaiting the oldest one + for i in (0..handles.len()).rev() { + if handles[i].is_finished() { + handles.swap_remove_back(i).unwrap().join().unwrap(); + } + } + + while handles.len() >= concurrency.get() { + handles.pop_front().unwrap().join().unwrap(); + } + }; + scope(|s| { let mut handles: VecDeque> = VecDeque::with_capacity(concurrency.get()); macro_rules! check { ($p:ident $(, $args:expr)* ) => { - while handles.len() >= concurrency.get() { - handles.pop_front().unwrap().join().unwrap(); - } + drain_handles(&mut handles); let handle = s.spawn(|| { let mut flag = false; @@ -97,9 +108,8 @@ fn main() { check!(alphabetical, &library_path); let collected = { - while handles.len() >= concurrency.get() { - handles.pop_front().unwrap().join().unwrap(); - } + drain_handles(&mut handles); + let mut flag = false; let r = features::check(&src_path, &compiler_path, &library_path, &mut flag, verbose); if flag { From ab7d76965168f1104488549feaebd24ffa69b3b7 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Tue, 22 Nov 2022 00:38:50 +0100 Subject: [PATCH 147/321] poll rustfmt child processes --- src/bootstrap/format.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/bootstrap/format.rs b/src/bootstrap/format.rs index 5e7264fe765a..b2f6afead798 100644 --- a/src/bootstrap/format.rs +++ b/src/bootstrap/format.rs @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::sync::mpsc::SyncSender; -fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl FnMut() { +fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl FnMut(bool) -> bool { let mut cmd = Command::new(&rustfmt); // avoid the submodule config paths from coming into play, // we only allow a single global config for the workspace for now @@ -23,7 +23,13 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F let cmd_debug = format!("{:?}", cmd); let mut cmd = cmd.spawn().expect("running rustfmt"); // poor man's async: return a closure that'll wait for rustfmt's completion - move || { + move |block: bool| -> bool { + if !block { + match cmd.try_wait() { + Ok(Some(_)) => {} + _ => return false, + } + } let status = cmd.wait().unwrap(); if !status.success() { eprintln!( @@ -34,6 +40,7 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F ); crate::detail_exit(1); } + true } } @@ -146,15 +153,23 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) { let child = rustfmt(&src, &rustfmt_path, paths.as_slice(), check); children.push_back(child); + // poll completion before waiting + for i in (0..children.len()).rev() { + if children[i](false) { + children.swap_remove_back(i); + break; + } + } + if children.len() >= max_processes { // await oldest child - children.pop_front().unwrap()(); + children.pop_front().unwrap()(true); } } // await remaining children for mut child in children { - child(); + child(true); } }); From 2a8513d221cfeb76c284a47e972961d22bdf65ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 17 Dec 2022 00:00:00 +0000 Subject: [PATCH 148/321] Replace visitor with a loop over blocks and statements --- .../src/cleanup_post_borrowck.rs | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs b/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs index 3378923c22c3..c33d72179ad1 100644 --- a/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs +++ b/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs @@ -19,21 +19,24 @@ //! [`Nop`]: rustc_middle::mir::StatementKind::Nop use crate::MirPass; -use rustc_middle::mir::visit::MutVisitor; -use rustc_middle::mir::{Body, BorrowKind, Location, Rvalue}; -use rustc_middle::mir::{Statement, StatementKind}; +use rustc_middle::mir::{Body, BorrowKind, Rvalue, StatementKind}; use rustc_middle::ty::TyCtxt; pub struct CleanupNonCodegenStatements; -pub struct DeleteNonCodegenStatements<'tcx> { - tcx: TyCtxt<'tcx>, -} - impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements { - fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { - let mut delete = DeleteNonCodegenStatements { tcx }; - delete.visit_body_preserves_cfg(body); + fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { + for basic_block in body.basic_blocks.as_mut_preserves_cfg() { + for statement in basic_block.statements.iter_mut() { + match statement.kind { + StatementKind::AscribeUserType(..) + | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _))) + | StatementKind::FakeRead(..) => statement.make_nop(), + _ => (), + } + } + } + body.user_type_annotations.raw.clear(); for decl in &mut body.local_decls { @@ -41,19 +44,3 @@ impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements { } } } - -impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements<'tcx> { - fn tcx(&self) -> TyCtxt<'tcx> { - self.tcx - } - - fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) { - match statement.kind { - StatementKind::AscribeUserType(..) - | StatementKind::Assign(box (_, Rvalue::Ref(_, BorrowKind::Shallow, _))) - | StatementKind::FakeRead(..) => statement.make_nop(), - _ => (), - } - self.super_statement(statement, location); - } -} From 82f05446a549b42af2ae544da05bb5503c3bd8ec Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sat, 20 Aug 2022 14:28:43 +0300 Subject: [PATCH 149/321] Migrate "function cannot return without recursing" diagnostic --- Cargo.lock | 1 + .../locales/en-US/mir_build.ftl | 5 +++++ compiler/rustc_error_messages/src/lib.rs | 1 + compiler/rustc_mir_build/Cargo.toml | 1 + compiler/rustc_mir_build/src/errors.rs | 13 +++++++++++++ compiler/rustc_mir_build/src/lib.rs | 1 + compiler/rustc_mir_build/src/lints.rs | 19 +++++-------------- 7 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 compiler/rustc_error_messages/locales/en-US/mir_build.ftl create mode 100644 compiler/rustc_mir_build/src/errors.rs diff --git a/Cargo.lock b/Cargo.lock index 5d05a09f0389..dbff82cf5360 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4070,6 +4070,7 @@ dependencies = [ "rustc_hir", "rustc_index", "rustc_infer", + "rustc_macros", "rustc_middle", "rustc_serialize", "rustc_session", diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl new file mode 100644 index 000000000000..b5bd2eb21a8a --- /dev/null +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -0,0 +1,5 @@ +mir_build_unconditional_recursion = function cannot return without recursing + .label = cannot return without recursing + .help = a `loop` may express intention better if this is on purpose + +mir_build_unconditional_recursion_call_site_label = recursive call site diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 418ba3c74d77..db62643bc24a 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -58,6 +58,7 @@ fluent_messages! { metadata => "../locales/en-US/metadata.ftl", middle => "../locales/en-US/middle.ftl", mir_dataflow => "../locales/en-US/mir_dataflow.ftl", + mir_build => "../locales/en-US/mir_build.ftl", monomorphize => "../locales/en-US/monomorphize.ftl", parse => "../locales/en-US/parse.ftl", passes => "../locales/en-US/passes.ftl", diff --git a/compiler/rustc_mir_build/Cargo.toml b/compiler/rustc_mir_build/Cargo.toml index 2baa3bfcb640..4ad3343d3031 100644 --- a/compiler/rustc_mir_build/Cargo.toml +++ b/compiler/rustc_mir_build/Cargo.toml @@ -17,6 +17,7 @@ rustc_index = { path = "../rustc_index" } rustc_errors = { path = "../rustc_errors" } rustc_hir = { path = "../rustc_hir" } rustc_infer = { path = "../rustc_infer" } +rustc_macros = { path = "../rustc_macros" } rustc_serialize = { path = "../rustc_serialize" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs new file mode 100644 index 000000000000..1d44db7eb3a7 --- /dev/null +++ b/compiler/rustc_mir_build/src/errors.rs @@ -0,0 +1,13 @@ +use rustc_macros::LintDiagnostic; +use rustc_span::Span; + +#[derive(LintDiagnostic)] +#[lint(mir_build::unconditional_recursion)] +#[help] +pub struct UnconditionalRecursion { + #[primary_span] + #[label] + pub span: Span, + #[label(mir_build::unconditional_recursion_call_site_label)] + pub call_sites: Vec, +} diff --git a/compiler/rustc_mir_build/src/lib.rs b/compiler/rustc_mir_build/src/lib.rs index 879752945953..2b05e92fdcf2 100644 --- a/compiler/rustc_mir_build/src/lib.rs +++ b/compiler/rustc_mir_build/src/lib.rs @@ -19,6 +19,7 @@ extern crate rustc_middle; mod build; mod check_unsafety; +mod errors; mod lints; pub mod thir; diff --git a/compiler/rustc_mir_build/src/lints.rs b/compiler/rustc_mir_build/src/lints.rs index b21f30efce80..383598fb094b 100644 --- a/compiler/rustc_mir_build/src/lints.rs +++ b/compiler/rustc_mir_build/src/lints.rs @@ -1,3 +1,4 @@ +use crate::errors::UnconditionalRecursion; use rustc_data_structures::graph::iterate::{ NodeStatus, TriColorDepthFirstSearch, TriColorVisitor, }; @@ -36,20 +37,10 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { let sp = tcx.def_span(def_id); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - tcx.struct_span_lint_hir( - UNCONDITIONAL_RECURSION, - hir_id, - sp, - "function cannot return without recursing", - |lint| { - lint.span_label(sp, "cannot return without recursing"); - // offer some help to the programmer. - for call_span in vis.reachable_recursive_calls { - lint.span_label(call_span, "recursive call site"); - } - lint.help("a `loop` may express intention better if this is on purpose") - }, - ); + tcx.emit_spanned_lint(UNCONDITIONAL_RECURSION, hir_id, sp, UnconditionalRecursion { + span: sp, + call_sites: vis.reachable_recursive_calls, + }); } } From 71fe52fed02f2555ee6d0e7631d250d53a1a9cae Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sat, 20 Aug 2022 23:54:58 +0300 Subject: [PATCH 150/321] Migrate "unsafe_op_in_unsafe_fn" lints --- .../locales/en-US/mir_build.ftl | 56 +++++++++++ .../rustc_mir_build/src/check_unsafety.rs | 92 +++++++++++++++++-- compiler/rustc_mir_build/src/errors.rs | 92 ++++++++++++++++++- compiler/rustc_mir_build/src/lints.rs | 10 +- 4 files changed, 236 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index b5bd2eb21a8a..2dfc4984786b 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -3,3 +3,59 @@ mir_build_unconditional_recursion = function cannot return without recursing .help = a `loop` may express intention better if this is on purpose mir_build_unconditional_recursion_call_site_label = recursive call site + +mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe = + call to unsafe function `{$function}` is unsafe and requires unsafe block (error E0133) + .note = consult the function's documentation for information on how to avoid undefined behavior + .label = call to unsafe function + +mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless = + call to unsafe function is unsafe and requires unsafe block (error E0133) + .note = consult the function's documentation for information on how to avoid undefined behavior + .label = call to unsafe function + +mir_build_unsafe_op_in_unsafe_fn_inline_assembly_requires_unsafe = + use of inline assembly is unsafe and requires unsafe block (error E0133) + .note = inline assembly is entirely unchecked and can cause undefined behavior + .label = use of inline assembly + +mir_build_unsafe_op_in_unsafe_fn_initializing_type_with_requires_unsafe = + initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe + block (error E0133) + .note = initializing a layout restricted type's field with a value outside the valid range is undefined behavior + .label = initializing type with `rustc_layout_scalar_valid_range` attr + +mir_build_unsafe_op_in_unsafe_fn_mutable_static_requires_unsafe = + use of mutable static is unsafe and requires unsafe block (error E0133) + .note = mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior + .label = use of mutable static + +mir_build_unsafe_op_in_unsafe_fn_extern_static_requires_unsafe = + use of extern static is unsafe and requires unsafe block (error E0133) + .note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior + .label = use of extern static + +mir_build_unsafe_op_in_unsafe_fn_deref_raw_pointer_requires_unsafe = + dereference of raw pointer is unsafe and requires unsafe block (error E0133) + .note = raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior + .label = dereference of raw pointer + +mir_build_unsafe_op_in_unsafe_fn_union_field_requires_unsafe = + access to union field is unsafe and requires unsafe block (error E0133) + .note = the field may not be properly initialized: using uninitialized data will cause undefined behavior + .label = access to union field + +mir_build_unsafe_op_in_unsafe_fn_mutation_of_layout_constrained_field_requires_unsafe = + mutation of layout constrained field is unsafe and requires unsafe block (error E0133) + .note = mutating layout constrained fields cannot statically be checked for valid values + .label = mutation of layout constrained field + +mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe = + borrow of layout constrained field with interior mutability is unsafe and requires unsafe block (error E0133) + .note = references to fields of layout constrained fields lose the constraints. Coupled with interior mutability, the field can be changed to invalid values + .label = borrow of layout constrained field with interior mutability + +mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe = + call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block (error E0133) + .note = can only be called if the required target features are available + .label = call to function with `#[target_feature]` diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 3bb1f51650ab..afdd862e5c7c 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -1,4 +1,5 @@ use crate::build::ExprCategory; +use crate::errors::*; use rustc_middle::thir::visit::{self, Visitor}; use rustc_errors::struct_span_err; @@ -83,15 +84,8 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { } SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {} SafetyContext::UnsafeFn => { - let (description, note) = kind.description_and_note(self.tcx); // unsafe_op_in_unsafe_fn is disallowed - self.tcx.struct_span_lint_hir( - UNSAFE_OP_IN_UNSAFE_FN, - self.hir_context, - span, - format!("{} is unsafe and requires unsafe block (error E0133)", description,), - |lint| lint.span_label(span, kind.simple_description()).note(note), - ) + kind.emit_unsafe_op_in_unsafe_fn_lint(self.tcx, self.hir_context, span); } SafetyContext::Safe => { let (description, note) = kind.description_and_note(self.tcx); @@ -536,6 +530,88 @@ enum UnsafeOpKind { use UnsafeOpKind::*; impl UnsafeOpKind { + pub fn emit_unsafe_op_in_unsafe_fn_lint( + &self, + tcx: TyCtxt<'_>, + hir_id: hir::HirId, + span: Span, + ) { + match self { + CallToUnsafeFunction(did) if did.is_some() => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe { + span, + function: &tcx.def_path_str(did.unwrap()), + }, + ), + CallToUnsafeFunction(..) => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { span }, + ), + UseOfInlineAssembly => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeUseOfInlineAssemblyRequiresUnsafe { span }, + ), + InitializingTypeWith => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeInitializingTypeWithRequiresUnsafe { span }, + ), + UseOfMutableStatic => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeUseOfMutableStaticRequiresUnsafe { span }, + ), + UseOfExternStatic => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeUseOfExternStaticRequiresUnsafe { span }, + ), + DerefOfRawPointer => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeDerefOfRawPointerRequiresUnsafe { span }, + ), + AccessToUnionField => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeAccessToUnionFieldRequiresUnsafe { span }, + ), + MutationOfLayoutConstrainedField => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeMutationOfLayoutConstrainedFieldRequiresUnsafe { span }, + ), + BorrowOfLayoutConstrainedField => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeBorrowOfLayoutConstrainedFieldRequiresUnsafe { span }, + ), + CallToFunctionWith(did) => tcx.emit_spanned_lint( + UNSAFE_OP_IN_UNSAFE_FN, + hir_id, + span, + UnsafeOpInUnsafeCallToFunctionWithRequiresUnsafe { + span, + function: &tcx.def_path_str(*did), + }, + ), + } + } + pub fn simple_description(&self) -> &'static str { match self { CallToUnsafeFunction(..) => "call to unsafe function", diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 1d44db7eb3a7..61131c0d733c 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -2,12 +2,100 @@ use rustc_macros::LintDiagnostic; use rustc_span::Span; #[derive(LintDiagnostic)] -#[lint(mir_build::unconditional_recursion)] +#[diag(mir_build::unconditional_recursion)] #[help] pub struct UnconditionalRecursion { - #[primary_span] #[label] pub span: Span, #[label(mir_build::unconditional_recursion_call_site_label)] pub call_sites: Vec, } + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> { + #[label] + pub span: Span, + pub function: &'a str, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless)] +#[note] +pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_inline_assembly_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_initializing_type_with_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_mutable_static_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_extern_static_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_deref_raw_pointer_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_union_field_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_mutation_of_layout_constrained_field_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe)] +pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe)] +#[note] +pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> { + #[label] + pub span: Span, + pub function: &'a str, +} diff --git a/compiler/rustc_mir_build/src/lints.rs b/compiler/rustc_mir_build/src/lints.rs index 383598fb094b..8529c64cd5cc 100644 --- a/compiler/rustc_mir_build/src/lints.rs +++ b/compiler/rustc_mir_build/src/lints.rs @@ -37,10 +37,12 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { let sp = tcx.def_span(def_id); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - tcx.emit_spanned_lint(UNCONDITIONAL_RECURSION, hir_id, sp, UnconditionalRecursion { - span: sp, - call_sites: vis.reachable_recursive_calls, - }); + tcx.emit_spanned_lint( + UNCONDITIONAL_RECURSION, + hir_id, + sp, + UnconditionalRecursion { span: sp, call_sites: vis.reachable_recursive_calls }, + ); } } From 64f3e4f195265a13648e5e201669ebb265bae421 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Wed, 24 Aug 2022 11:01:53 +0300 Subject: [PATCH 151/321] Migrate "requires unsafe" diagnostics --- .../locales/en-US/mir_build.ftl | 110 +++++++++ .../rustc_mir_build/src/check_unsafety.rs | 202 ++++++++-------- compiler/rustc_mir_build/src/errors.rs | 216 +++++++++++++++++- 3 files changed, 432 insertions(+), 96 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 2dfc4984786b..fd42cc6cb087 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -59,3 +59,113 @@ mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe = call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block (error E0133) .note = can only be called if the required target features are available .label = call to function with `#[target_feature]` + +mir_build_call_to_unsafe_fn_requires_unsafe = + call to unsafe function `{$function}` is unsafe and requires unsafe block + .note = consult the function's documentation for information on how to avoid undefined behavior + .label = call to unsafe function + +mir_build_call_to_unsafe_fn_requires_unsafe_nameless = + call to unsafe function is unsafe and requires unsafe block + .note = consult the function's documentation for information on how to avoid undefined behavior + .label = call to unsafe function + +mir_build_call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + call to unsafe function `{$function}` is unsafe and requires unsafe function or block + .note = consult the function's documentation for information on how to avoid undefined behavior + .label = call to unsafe function + +mir_build_call_to_unsafe_fn_requires_unsafe_nameless_unsafe_op_in_unsafe_fn_allowed = + call to unsafe function is unsafe and requires unsafe function or block + .note = consult the function's documentation for information on how to avoid undefined behavior + .label = call to unsafe function + +mir_build_inline_assembly_requires_unsafe = + use of inline assembly is unsafe and requires unsafe block + .note = inline assembly is entirely unchecked and can cause undefined behavior + .label = use of inline assembly + +mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + use of inline assembly is unsafe and requires unsafe function or block + .note = inline assembly is entirely unchecked and can cause undefined behavior + .label = use of inline assembly + +mir_build_initializing_type_with_requires_unsafe = + initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe block + .note = initializing a layout restricted type's field with a value outside the valid range is undefined behavior + .label = initializing type with `rustc_layout_scalar_valid_range` attr + +mir_build_initializing_type_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block + .note = initializing a layout restricted type's field with a value outside the valid range is undefined behavior + .label = initializing type with `rustc_layout_scalar_valid_range` attr + +mir_build_mutable_static_requires_unsafe = + use of mutable static is unsafe and requires unsafe block + .note = mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior + .label = use of mutable static + +mir_build_mutable_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + use of mutable static is unsafe and requires unsafe function or block + .note = mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior + .label = use of mutable static + +mir_build_extern_static_requires_unsafe = + use of extern static is unsafe and requires unsafe block + .note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior + .label = use of extern static + +mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + use of extern static is unsafe and requires unsafe function or block + .note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior + .label = use of extern static + +mir_build_deref_raw_pointer_requires_unsafe = + dereference of raw pointer is unsafe and requires unsafe block + .note = raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior + .label = dereference of raw pointer + +mir_build_deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + dereference of raw pointer is unsafe and requires unsafe function or block + .note = raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior + .label = dereference of raw pointer + +mir_build_union_field_requires_unsafe = + access to union field is unsafe and requires unsafe block + .note = the field may not be properly initialized: using uninitialized data will cause undefined behavior + .label = access to union field + +mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + access to union field is unsafe and requires unsafe function or block + .note = the field may not be properly initialized: using uninitialized data will cause undefined behavior + .label = access to union field + +mir_build_mutation_of_layout_constrained_field_requires_unsafe = + mutation of layout constrained field is unsafe and requires unsafe block + .note = mutating layout constrained fields cannot statically be checked for valid values + .label = mutation of layout constrained field + +mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + mutation of layout constrained field is unsafe and requires unsafe function or block + .note = mutating layout constrained fields cannot statically be checked for valid values + .label = mutation of layout constrained field + +mir_build_borrow_of_layout_constrained_field_requires_unsafe = + borrow of layout constrained field with interior mutability is unsafe and requires unsafe block + .note = references to fields of layout constrained fields lose the constraints. Coupled with interior mutability, the field can be changed to invalid values + .label = borrow of layout constrained field with interior mutability + +mir_build_borrow_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + borrow of layout constrained field with interior mutability is unsafe and requires unsafe function or block + .note = references to fields of layout constrained fields lose the constraints. Coupled with interior mutability, the field can be changed to invalid values + .label = borrow of layout constrained field with interior mutability + +mir_build_call_to_fn_with_requires_unsafe = + call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe block + .note = can only be called if the required target features are available + .label = call to function with `#[target_feature]` + +mir_build_call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = + call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe function or block + .note = can only be called if the required target features are available + .label = call to function with `#[target_feature]` diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index afdd862e5c7c..e57b8a7b4ce8 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -2,7 +2,6 @@ use crate::build::ExprCategory; use crate::errors::*; use rustc_middle::thir::visit::{self, Visitor}; -use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_middle::mir::BorrowKind; use rustc_middle::thir::*; @@ -13,7 +12,6 @@ use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::symbol::Symbol; use rustc_span::Span; -use std::borrow::Cow; use std::ops::Bound; struct UnsafetyVisitor<'a, 'tcx> { @@ -88,19 +86,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { kind.emit_unsafe_op_in_unsafe_fn_lint(self.tcx, self.hir_context, span); } SafetyContext::Safe => { - let (description, note) = kind.description_and_note(self.tcx); - let fn_sugg = if unsafe_op_in_unsafe_fn_allowed { " function or" } else { "" }; - struct_span_err!( - self.tcx.sess, - span, - E0133, - "{} is unsafe and requires unsafe{} block", - description, - fn_sugg, - ) - .span_label(span, kind.simple_description()) - .note(note) - .emit(); + kind.emit_requires_unsafe_err(self.tcx, span, unsafe_op_in_unsafe_fn_allowed); } } } @@ -556,55 +542,55 @@ impl UnsafeOpKind { UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeUseOfInlineAssemblyRequiresUnsafe { span }, + UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { span }, ), InitializingTypeWith => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeInitializingTypeWithRequiresUnsafe { span }, + UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { span }, ), UseOfMutableStatic => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeUseOfMutableStaticRequiresUnsafe { span }, + UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { span }, ), UseOfExternStatic => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeUseOfExternStaticRequiresUnsafe { span }, + UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { span }, ), DerefOfRawPointer => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeDerefOfRawPointerRequiresUnsafe { span }, + UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { span }, ), AccessToUnionField => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeAccessToUnionFieldRequiresUnsafe { span }, + UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { span }, ), MutationOfLayoutConstrainedField => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeMutationOfLayoutConstrainedFieldRequiresUnsafe { span }, + UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { span }, ), BorrowOfLayoutConstrainedField => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeBorrowOfLayoutConstrainedFieldRequiresUnsafe { span }, + UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { span }, ), CallToFunctionWith(did) => tcx.emit_spanned_lint( UNSAFE_OP_IN_UNSAFE_FN, hir_id, span, - UnsafeOpInUnsafeCallToFunctionWithRequiresUnsafe { + UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe { span, function: &tcx.def_path_str(*did), }, @@ -612,79 +598,105 @@ impl UnsafeOpKind { } } - pub fn simple_description(&self) -> &'static str { + pub fn emit_requires_unsafe_err( + &self, + tcx: TyCtxt<'_>, + span: Span, + unsafe_op_in_unsafe_fn_allowed: bool, + ) { match self { - CallToUnsafeFunction(..) => "call to unsafe function", - UseOfInlineAssembly => "use of inline assembly", - InitializingTypeWith => "initializing type with `rustc_layout_scalar_valid_range` attr", - UseOfMutableStatic => "use of mutable static", - UseOfExternStatic => "use of extern static", - DerefOfRawPointer => "dereference of raw pointer", - AccessToUnionField => "access to union field", - MutationOfLayoutConstrainedField => "mutation of layout constrained field", - BorrowOfLayoutConstrainedField => { - "borrow of layout constrained field with interior mutability" + CallToUnsafeFunction(did) if did.is_some() && unsafe_op_in_unsafe_fn_allowed => { + tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + span, + function: &tcx.def_path_str(did.unwrap()), + }); + } + CallToUnsafeFunction(did) if did.is_some() => { + tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafe { + span, + function: &tcx.def_path_str(did.unwrap()), + }); + } + CallToUnsafeFunction(..) if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess.emit_err( + CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { span }, + ); + } + CallToUnsafeFunction(..) => { + tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeNameless { span }); + } + UseOfInlineAssembly if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess + .emit_err(UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span }); + } + UseOfInlineAssembly => { + tcx.sess.emit_err(UseOfInlineAssemblyRequiresUnsafe { span }); + } + InitializingTypeWith if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess + .emit_err(InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span }); + } + InitializingTypeWith => { + tcx.sess.emit_err(InitializingTypeWithRequiresUnsafe { span }); + } + UseOfMutableStatic if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess + .emit_err(UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span }); + } + UseOfMutableStatic => { + tcx.sess.emit_err(UseOfMutableStaticRequiresUnsafe { span }); + } + UseOfExternStatic if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess + .emit_err(UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span }); + } + UseOfExternStatic => { + tcx.sess.emit_err(UseOfExternStaticRequiresUnsafe { span }); + } + DerefOfRawPointer if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess + .emit_err(DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span }); + } + DerefOfRawPointer => { + tcx.sess.emit_err(DerefOfRawPointerRequiresUnsafe { span }); + } + AccessToUnionField if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess + .emit_err(AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span }); + } + AccessToUnionField => { + tcx.sess.emit_err(AccessToUnionFieldRequiresUnsafe { span }); + } + MutationOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess.emit_err( + MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + span, + }, + ); + } + MutationOfLayoutConstrainedField => { + tcx.sess.emit_err(MutationOfLayoutConstrainedFieldRequiresUnsafe { span }); + } + BorrowOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess.emit_err( + BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span }, + ); + } + BorrowOfLayoutConstrainedField => { + tcx.sess.emit_err(BorrowOfLayoutConstrainedFieldRequiresUnsafe { span }); + } + CallToFunctionWith(did) if unsafe_op_in_unsafe_fn_allowed => { + tcx.sess.emit_err(CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + span, + function: &tcx.def_path_str(*did), + }); + } + CallToFunctionWith(did) => { + tcx.sess.emit_err(CallToFunctionWithRequiresUnsafe { + span, + function: &tcx.def_path_str(*did), + }); } - CallToFunctionWith(..) => "call to function with `#[target_feature]`", - } - } - - pub fn description_and_note(&self, tcx: TyCtxt<'_>) -> (Cow<'static, str>, &'static str) { - match self { - CallToUnsafeFunction(did) => ( - if let Some(did) = did { - Cow::from(format!("call to unsafe function `{}`", tcx.def_path_str(*did))) - } else { - Cow::Borrowed(self.simple_description()) - }, - "consult the function's documentation for information on how to avoid undefined \ - behavior", - ), - UseOfInlineAssembly => ( - Cow::Borrowed(self.simple_description()), - "inline assembly is entirely unchecked and can cause undefined behavior", - ), - InitializingTypeWith => ( - Cow::Borrowed(self.simple_description()), - "initializing a layout restricted type's field with a value outside the valid \ - range is undefined behavior", - ), - UseOfMutableStatic => ( - Cow::Borrowed(self.simple_description()), - "mutable statics can be mutated by multiple threads: aliasing violations or data \ - races will cause undefined behavior", - ), - UseOfExternStatic => ( - Cow::Borrowed(self.simple_description()), - "extern statics are not controlled by the Rust type system: invalid data, \ - aliasing violations or data races will cause undefined behavior", - ), - DerefOfRawPointer => ( - Cow::Borrowed(self.simple_description()), - "raw pointers may be null, dangling or unaligned; they can violate aliasing rules \ - and cause data races: all of these are undefined behavior", - ), - AccessToUnionField => ( - Cow::Borrowed(self.simple_description()), - "the field may not be properly initialized: using uninitialized data will cause \ - undefined behavior", - ), - MutationOfLayoutConstrainedField => ( - Cow::Borrowed(self.simple_description()), - "mutating layout constrained fields cannot statically be checked for valid values", - ), - BorrowOfLayoutConstrainedField => ( - Cow::Borrowed(self.simple_description()), - "references to fields of layout constrained fields lose the constraints. Coupled \ - with interior mutability, the field can be changed to invalid values", - ), - CallToFunctionWith(did) => ( - Cow::from(format!( - "call to function `{}` with `#[target_feature]`", - tcx.def_path_str(*did) - )), - "can only be called if the required target features are available", - ), } } } diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 61131c0d733c..260a9f6375bb 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -1,4 +1,4 @@ -use rustc_macros::LintDiagnostic; +use rustc_macros::{LintDiagnostic, SessionDiagnostic}; use rustc_span::Span; #[derive(LintDiagnostic)] @@ -99,3 +99,217 @@ pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> { pub span: Span, pub function: &'a str, } + +#[derive(SessionDiagnostic)] +#[diag(mir_build::call_to_unsafe_fn_requires_unsafe, code = "E0133")] +#[note] +pub struct CallToUnsafeFunctionRequiresUnsafe<'a> { + #[primary_span] + #[label] + pub span: Span, + pub function: &'a str, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::call_to_unsafe_fn_requires_unsafe_nameless, code = "E0133")] +#[note] +pub struct CallToUnsafeFunctionRequiresUnsafeNameless { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[note] +pub struct CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> { + #[primary_span] + #[label] + pub span: Span, + pub function: &'a str, +} + +#[derive(SessionDiagnostic)] +#[diag( + mir_build::call_to_unsafe_fn_requires_unsafe_nameless_unsafe_op_in_unsafe_fn_allowed, + code = "E0133" +)] +#[note] +pub struct CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::inline_assembly_requires_unsafe, code = "E0133")] +#[note] +pub struct UseOfInlineAssemblyRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[note] +pub struct UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::initializing_type_with_requires_unsafe, code = "E0133")] +#[note] +pub struct InitializingTypeWithRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag( + mir_build::initializing_type_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, + code = "E0133" +)] +#[note] +pub struct InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::mutable_static_requires_unsafe, code = "E0133")] +#[note] +pub struct UseOfMutableStaticRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::mutable_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[note] +pub struct UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::extern_static_requires_unsafe, code = "E0133")] +#[note] +pub struct UseOfExternStaticRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[note] +pub struct UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::deref_raw_pointer_requires_unsafe, code = "E0133")] +#[note] +pub struct DerefOfRawPointerRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[note] +pub struct DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::union_field_requires_unsafe, code = "E0133")] +#[note] +pub struct AccessToUnionFieldRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[note] +pub struct AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::mutation_of_layout_constrained_field_requires_unsafe, code = "E0133")] +#[note] +pub struct MutationOfLayoutConstrainedFieldRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag( + mir_build::mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, + code = "E0133" +)] +#[note] +pub struct MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::borrow_of_layout_constrained_field_requires_unsafe, code = "E0133")] +#[note] +pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafe { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag( + mir_build::borrow_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, + code = "E0133" +)] +#[note] +pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::call_to_fn_with_requires_unsafe, code = "E0133")] +#[note] +pub struct CallToFunctionWithRequiresUnsafe<'a> { + #[primary_span] + #[label] + pub span: Span, + pub function: &'a str, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[note] +pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> { + #[primary_span] + #[label] + pub span: Span, + pub function: &'a str, +} From 71a9cb9b7ebc1a4d56f8d837ea8b4b696fae9e89 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Wed, 24 Aug 2022 20:16:50 +0300 Subject: [PATCH 152/321] Migrate "unused unsafe" lint --- .../locales/en-US/mir_build.ftl | 6 +++++ .../rustc_mir_build/src/check_unsafety.rs | 24 ++++++++++-------- compiler/rustc_mir_build/src/errors.rs | 25 ++++++++++++++++++- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index fd42cc6cb087..9dab6f8e8832 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -169,3 +169,9 @@ mir_build_call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed = call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe function or block .note = can only be called if the required target features are available .label = call to function with `#[target_feature]` + +mir_build_unused_unsafe = unnecessary `unsafe` block + .label = unnecessary `unsafe` block + +mir_build_unused_unsafe_enclosing_block_label = because it's nested under this `unsafe` block +mir_build_unused_unsafe_enclosing_fn_label = because it's nested under this `unsafe` fn diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index e57b8a7b4ce8..99e96ff77ced 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -45,7 +45,9 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { self.warn_unused_unsafe( hir_id, block_span, - Some((self.tcx.sess.source_map().guess_head_span(enclosing_span), "block")), + Some(UnusedUnsafeEnclosing::Block { + span: self.tcx.sess.source_map().guess_head_span(enclosing_span), + }), ); f(self); } else { @@ -59,7 +61,9 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { hir_id, span, if self.unsafe_op_in_unsafe_fn_allowed() { - self.body_unsafety.unsafe_fn_sig_span().map(|span| (span, "fn")) + self.body_unsafety + .unsafe_fn_sig_span() + .map(|span| UnusedUnsafeEnclosing::Function { span }) } else { None }, @@ -95,17 +99,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { &self, hir_id: hir::HirId, block_span: Span, - enclosing_unsafe: Option<(Span, &'static str)>, + enclosing_unsafe: Option, ) { let block_span = self.tcx.sess.source_map().guess_head_span(block_span); - let msg = "unnecessary `unsafe` block"; - self.tcx.struct_span_lint_hir(UNUSED_UNSAFE, hir_id, block_span, msg, |lint| { - lint.span_label(block_span, msg); - if let Some((span, kind)) = enclosing_unsafe { - lint.span_label(span, format!("because it's nested under this `unsafe` {}", kind)); - } - lint - }); + self.tcx.emit_spanned_lint( + UNUSED_UNSAFE, + hir_id, + block_span, + UnusedUnsafe { span: block_span, enclosing: enclosing_unsafe }, + ); } /// Whether the `unsafe_op_in_unsafe_fn` lint is `allow`ed at the current HIR node. diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 260a9f6375bb..a3e871512266 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -1,4 +1,4 @@ -use rustc_macros::{LintDiagnostic, SessionDiagnostic}; +use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; use rustc_span::Span; #[derive(LintDiagnostic)] @@ -313,3 +313,26 @@ pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> { pub span: Span, pub function: &'a str, } + +#[derive(LintDiagnostic)] +#[diag(mir_build::unused_unsafe)] +pub struct UnusedUnsafe { + #[label] + pub span: Span, + #[subdiagnostic] + pub enclosing: Option, +} + +#[derive(SessionSubdiagnostic)] +pub enum UnusedUnsafeEnclosing { + #[label(mir_build::unused_unsafe_enclosing_block_label)] + Block { + #[primary_span] + span: Span, + }, + #[label(mir_build::unused_unsafe_enclosing_fn_label)] + Function { + #[primary_span] + span: Span, + }, +} From c7bfd007194a0f4062a8809996b60f2700dd4652 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Fri, 26 Aug 2022 16:38:21 +0300 Subject: [PATCH 153/321] Migrate "non-exhaustive patterns: type is non-empty" diagnostic --- .../locales/en-US/mir_build.ftl | 8 ++ compiler/rustc_mir_build/src/errors.rs | 93 +++++++++++++++++++ .../src/thir/pattern/check_match.rs | 18 ++-- .../rustc_mir_build/src/thir/pattern/mod.rs | 1 + 4 files changed, 113 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 9dab6f8e8832..5fb89dcccaee 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -175,3 +175,11 @@ mir_build_unused_unsafe = unnecessary `unsafe` block mir_build_unused_unsafe_enclosing_block_label = because it's nested under this `unsafe` block mir_build_unused_unsafe_enclosing_fn_label = because it's nested under this `unsafe` fn + +mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type `{$ty}` is non-empty + .def_note = `{$peeled_ty}` defined here + .type_note = the matched value is of type `{$ty}` + .non_exhaustive_type_note = the matched value is of type `{$ty}`, which is marked as non-exhaustive + .reference_note = references are always considered inhabited + .suggestion = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown + .help = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index a3e871512266..9e63141d7160 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -1,4 +1,8 @@ +use crate::thir::pattern::MatchCheckCtxt; +use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan}; use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; +use rustc_middle::ty::{self, Ty}; +use rustc_session::{parse::ParseSess, SessionDiagnostic}; use rustc_span::Span; #[derive(LintDiagnostic)] @@ -336,3 +340,92 @@ pub enum UnusedUnsafeEnclosing { span: Span, }, } + +pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> { + pub cx: &'m MatchCheckCtxt<'p, 'tcx>, + pub expr_span: Span, + pub span: Span, + pub ty: Ty<'tcx>, +} + +impl<'a> SessionDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> { + fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'_, ErrorGuaranteed> { + let mut diag = sess.span_diagnostic.struct_span_err_with_code( + self.span, + rustc_errors::fluent::mir_build::non_exhaustive_patterns_type_not_empty, + error_code!(E0004), + ); + + let peeled_ty = self.ty.peel_refs(); + diag.set_arg("ty", self.ty); + diag.set_arg("peeled_ty", peeled_ty); + + if let ty::Adt(def, _) = peeled_ty.kind() { + let def_span = self + .cx + .tcx + .hir() + .get_if_local(def.did()) + .and_then(|node| node.ident()) + .map(|ident| ident.span) + .unwrap_or_else(|| self.cx.tcx.def_span(def.did())); + + // workaround to make test pass + let mut span: MultiSpan = def_span.into(); + span.push_span_label(def_span, ""); + + diag.span_note(span, rustc_errors::fluent::mir_build::def_note); + } + + let is_variant_list_non_exhaustive = match self.ty.kind() { + ty::Adt(def, _) if def.is_variant_list_non_exhaustive() && !def.did().is_local() => { + true + } + _ => false, + }; + + if is_variant_list_non_exhaustive { + diag.note(rustc_errors::fluent::mir_build::non_exhaustive_type_note); + } else { + diag.note(rustc_errors::fluent::mir_build::type_note); + } + + if let ty::Ref(_, sub_ty, _) = self.ty.kind() { + if self.cx.tcx.is_ty_uninhabited_from(self.cx.module, *sub_ty, self.cx.param_env) { + diag.note(rustc_errors::fluent::mir_build::reference_note); + } + } + + let mut suggestion = None; + let sm = self.cx.tcx.sess.source_map(); + if self.span.eq_ctxt(self.expr_span) { + // Get the span for the empty match body `{}`. + let (indentation, more) = if let Some(snippet) = sm.indentation_before(self.span) { + (format!("\n{}", snippet), " ") + } else { + (" ".to_string(), "") + }; + suggestion = Some(( + self.span.shrink_to_hi().with_hi(self.expr_span.hi()), + format!( + " {{{indentation}{more}_ => todo!(),{indentation}}}", + indentation = indentation, + more = more, + ), + )); + } + + if let Some((span, sugg)) = suggestion { + diag.span_suggestion_verbose( + span, + rustc_errors::fluent::mir_build::suggestion, + sugg, + Applicability::HasPlaceholders, + ); + } else { + diag.help(rustc_errors::fluent::mir_build::help); + } + + diag + } +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index e369dba55242..c818a2a3a898 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -4,6 +4,8 @@ use super::usefulness::{ }; use super::{PatCtxt, PatternError}; +use crate::errors::NonExhaustivePatternsTypeNotEmpty; + use rustc_arena::TypedArena; use rustc_ast::Mutability; use rustc_errors::{ @@ -760,15 +762,17 @@ fn non_exhaustive_match<'p, 'tcx>( // informative. let mut err; let pattern; - let mut patterns_len = 0; + let patterns_len; if is_empty_match && !non_empty_enum { - err = create_e0004( - cx.tcx.sess, - sp, - format!("non-exhaustive patterns: type `{}` is non-empty", scrut_ty), - ); - pattern = "_".to_string(); + cx.tcx.sess.emit_err(NonExhaustivePatternsTypeNotEmpty { + cx, + expr_span, + span: sp, + ty: scrut_ty, + }); + return; } else { + // FIXME: migration of this diagnostic will require list support let joined_patterns = joined_uncovered_patterns(cx, &witnesses); err = create_e0004( cx.tcx.sess, diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 48a231a6cd6b..5aaf6c907e39 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -6,6 +6,7 @@ mod deconstruct_pat; mod usefulness; pub(crate) use self::check_match::check_match; +pub(crate) use self::usefulness::MatchCheckCtxt; use crate::thir::util::UserAnnotatedTyHelpers; From 98442b69055024c50c38bad11a18f71cc3858c84 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Fri, 26 Aug 2022 19:30:33 +0300 Subject: [PATCH 154/321] Migrate pattern inlining error diagnostics --- .../locales/en-US/mir_build.ftl | 8 ++++++ compiler/rustc_mir_build/src/errors.rs | 28 +++++++++++++++++++ .../src/thir/pattern/check_match.rs | 18 ++++-------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 5fb89dcccaee..610e16ab7d8e 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -183,3 +183,11 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type .reference_note = references are always considered inhabited .suggestion = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown .help = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern + +mir_build_static_in_pattern = statics cannot be referenced in patterns + +mir_build_assoc_const_in_pattern = associated consts cannot be referenced in patterns + +mir_build_const_param_in_pattern = const parameters cannot be referenced in patterns + +mir_build_non_const_path = runtime values cannot be referenced in patterns diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 9e63141d7160..ceaf057bb465 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -429,3 +429,31 @@ impl<'a> SessionDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> diag } } + +#[derive(SessionDiagnostic)] +#[diag(mir_build::static_in_pattern, code = "E0158")] +pub struct StaticInPattern { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::assoc_const_in_pattern, code = "E0158")] +pub struct AssocConstInPattern { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::const_param_in_pattern, code = "E0158")] +pub struct ConstParamInPattern { + #[primary_span] + pub span: Span, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::non_const_path, code = "E0080")] +pub struct NonConstPath { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index c818a2a3a898..42c878710291 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -4,7 +4,7 @@ use super::usefulness::{ }; use super::{PatCtxt, PatternError}; -use crate::errors::NonExhaustivePatternsTypeNotEmpty; +use crate::errors::*; use rustc_arena::TypedArena; use rustc_ast::Mutability; @@ -109,28 +109,20 @@ impl PatCtxt<'_, '_> { for error in &self.errors { match *error { PatternError::StaticInPattern(span) => { - self.span_e0158(span, "statics cannot be referenced in patterns") + self.tcx.sess.emit_err(StaticInPattern { span }); } PatternError::AssocConstInPattern(span) => { - self.span_e0158(span, "associated consts cannot be referenced in patterns") + self.tcx.sess.emit_err(AssocConstInPattern { span }); } PatternError::ConstParamInPattern(span) => { - self.span_e0158(span, "const parameters cannot be referenced in patterns") + self.tcx.sess.emit_err(ConstParamInPattern { span }); } PatternError::NonConstPath(span) => { - rustc_middle::mir::interpret::struct_error( - self.tcx.at(span), - "runtime values cannot be referenced in patterns", - ) - .emit(); + self.tcx.sess.emit_err(NonConstPath { span }); } } } } - - fn span_e0158(&self, span: Span, text: &str) { - struct_span_err!(self.tcx.sess, span, E0158, "{}", text).emit(); - } } impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> { From b694e6649ef5883e36c1bbac0c9890db5dd96792 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sat, 27 Aug 2022 22:06:06 +0300 Subject: [PATCH 155/321] Migrate unreachable pattern diagnostic --- .../locales/en-US/mir_build.ftl | 4 ++++ compiler/rustc_mir_build/src/errors.rs | 9 +++++++++ .../src/thir/pattern/check_match.rs | 14 ++++++-------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 610e16ab7d8e..8ff5c9b3fdeb 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -191,3 +191,7 @@ mir_build_assoc_const_in_pattern = associated consts cannot be referenced in pat mir_build_const_param_in_pattern = const parameters cannot be referenced in patterns mir_build_non_const_path = runtime values cannot be referenced in patterns + +mir_build_unreachable_pattern = unreachable pattern + .label = unreachable pattern + .catchall_label = matches any value diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index ceaf057bb465..3049ebc5b037 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -457,3 +457,12 @@ pub struct NonConstPath { #[primary_span] pub span: Span, } + +#[derive(LintDiagnostic)] +#[diag(mir_build::unreachable_pattern)] +pub struct UnreachablePattern { + #[label] + pub span: Option, + #[label(mir_build::catchall_label)] + pub catchall: Option, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 42c878710291..b21cd0d1404d 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -605,14 +605,12 @@ fn pat_is_catchall(pat: &DeconstructedPat<'_, '_>) -> bool { } fn unreachable_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, catchall: Option) { - tcx.struct_span_lint_hir(UNREACHABLE_PATTERNS, id, span, "unreachable pattern", |lint| { - if let Some(catchall) = catchall { - // We had a catchall pattern, hint at that. - lint.span_label(span, "unreachable pattern"); - lint.span_label(catchall, "matches any value"); - } - lint - }); + tcx.emit_spanned_lint( + UNREACHABLE_PATTERNS, + id, + span, + UnreachablePattern { span: if catchall.is_some() { Some(span) } else { None }, catchall }, + ); } fn irrefutable_let_pattern(tcx: TyCtxt<'_>, id: HirId, span: Span) { From d5f821eeb085fab83c6592b018d2e99033b8c2ab Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sat, 27 Aug 2022 22:17:10 +0300 Subject: [PATCH 156/321] Migrate "constant pattern depends on generic parameter" diagnostic --- compiler/rustc_error_messages/locales/en-US/mir_build.ftl | 3 +++ compiler/rustc_mir_build/src/errors.rs | 7 +++++++ compiler/rustc_mir_build/src/thir/pattern/mod.rs | 7 ++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 8ff5c9b3fdeb..4504d557da3f 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -195,3 +195,6 @@ mir_build_non_const_path = runtime values cannot be referenced in patterns mir_build_unreachable_pattern = unreachable pattern .label = unreachable pattern .catchall_label = matches any value + +mir_build_const_pattern_depends_on_generic_parameter = + constant pattern depends on a generic parameter diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 3049ebc5b037..e6ed824b8bb9 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -466,3 +466,10 @@ pub struct UnreachablePattern { #[label(mir_build::catchall_label)] pub catchall: Option, } + +#[derive(SessionDiagnostic)] +#[diag(mir_build::const_pattern_depends_on_generic_parameter)] +pub struct ConstPatternDependsOnGenericParameter { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 5aaf6c907e39..2711835f4b4d 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -8,6 +8,7 @@ mod usefulness; pub(crate) use self::check_match::check_match; pub(crate) use self::usefulness::MatchCheckCtxt; +use crate::errors::ConstPatternDependsOnGenericParameter; use crate::thir::util::UserAnnotatedTyHelpers; use rustc_errors::struct_span_err; @@ -549,7 +550,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { Err(ErrorHandled::TooGeneric) => { // While `Reported | Linted` cases will have diagnostics emitted already // it is not true for TooGeneric case, so we need to give user more information. - self.tcx.sess.span_err(span, "constant pattern depends on a generic parameter"); + self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span }); pat_from_kind(PatKind::Wild) } Err(_) => { @@ -583,9 +584,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { _ => bug!("Expected ConstKind::Param"), }, mir::ConstantKind::Val(_, _) => self.const_to_pat(value, id, span, false).kind, - mir::ConstantKind::Unevaluated(..) => { + mir::ConstKind::Unevaluated(_) => { // If we land here it means the const can't be evaluated because it's `TooGeneric`. - self.tcx.sess.span_err(span, "constant pattern depends on a generic parameter"); + self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span }); return PatKind::Wild; } } From 513e3995e0d0e72690ad6eae925217cfc83b3eb3 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sat, 27 Aug 2022 22:25:09 +0300 Subject: [PATCH 157/321] Migrate "could not evaluate const pattern" diagnostic --- compiler/rustc_error_messages/locales/en-US/mir_build.ftl | 2 ++ compiler/rustc_mir_build/src/errors.rs | 7 +++++++ compiler/rustc_mir_build/src/thir/pattern/mod.rs | 6 +++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 4504d557da3f..87e7f348ee08 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -198,3 +198,5 @@ mir_build_unreachable_pattern = unreachable pattern mir_build_const_pattern_depends_on_generic_parameter = constant pattern depends on a generic parameter + +mir_build_could_not_eval_const_pattern = could not evaluate constant pattern diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index e6ed824b8bb9..0d7e62e616f3 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -473,3 +473,10 @@ pub struct ConstPatternDependsOnGenericParameter { #[primary_span] pub span: Span, } + +#[derive(SessionDiagnostic)] +#[diag(mir_build::could_not_eval_const_pattern)] +pub struct CouldNotEvalConstPattern { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 2711835f4b4d..c04bea24b581 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -8,7 +8,7 @@ mod usefulness; pub(crate) use self::check_match::check_match; pub(crate) use self::usefulness::MatchCheckCtxt; -use crate::errors::ConstPatternDependsOnGenericParameter; +use crate::errors::*; use crate::thir::util::UserAnnotatedTyHelpers; use rustc_errors::struct_span_err; @@ -503,7 +503,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { } Err(_) => { - self.tcx.sess.span_err(span, "could not evaluate constant pattern"); + self.tcx.sess.emit_err(CouldNotEvalConstPattern { span }); return pat_from_kind(PatKind::Wild); } }; @@ -554,7 +554,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { pat_from_kind(PatKind::Wild) } Err(_) => { - self.tcx.sess.span_err(span, "could not evaluate constant pattern"); + self.tcx.sess.emit_err(CouldNotEvalConstPattern { span }); pat_from_kind(PatKind::Wild) } } From 4b70784176052a7fe6e7fc205bab67c829de0bc1 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sat, 27 Aug 2022 22:48:18 +0300 Subject: [PATCH 158/321] Migrate lower range bound diagnostics --- .../locales/en-US/mir_build.ftl | 7 +++++ compiler/rustc_mir_build/src/errors.rs | 17 +++++++++++ .../rustc_mir_build/src/thir/pattern/mod.rs | 29 ++++--------------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 87e7f348ee08..6bfab0774fea 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -200,3 +200,10 @@ mir_build_const_pattern_depends_on_generic_parameter = constant pattern depends on a generic parameter mir_build_could_not_eval_const_pattern = could not evaluate constant pattern + +mir_build_lower_range_bound_must_be_less_than_or_equal_to_upper = + lower range bound must be less than or equal to upper + .label = lower bound larger than upper bound + .teach_note = When matching against a range, the compiler verifies that the range is non-empty. Range patterns include both end-points, so this is equivalent to requiring the start of the range to be less than or equal to the end of the range. + +mir_build_lower_range_bound_must_be_less_than_upper = lower range bound must be less than upper diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 0d7e62e616f3..96728970c5be 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -480,3 +480,20 @@ pub struct CouldNotEvalConstPattern { #[primary_span] pub span: Span, } + +#[derive(SessionDiagnostic)] +#[diag(mir_build::lower_range_bound_must_be_less_than_or_equal_to_upper, code = "E0030")] +pub struct LowerRangeBoundMustBeLessThanOrEqualToUpper { + #[primary_span] + #[label] + pub span: Span, + #[note(mir_build::teach_note)] + pub teach: Option<()>, +} + +#[derive(SessionDiagnostic)] +#[diag(mir_build::lower_range_bound_must_be_less_than_upper, code = "E0579")] +pub struct LowerRangeBoundMustBeLessThanUpper { + #[primary_span] + pub span: Span, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index c04bea24b581..b7d1dc8c9565 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -11,7 +11,7 @@ pub(crate) use self::usefulness::MatchCheckCtxt; use crate::errors::*; use crate::thir::util::UserAnnotatedTyHelpers; -use rustc_errors::struct_span_err; +use rustc_errors::error_code; use rustc_hir as hir; use rustc_hir::def::{CtorOf, DefKind, Res}; use rustc_hir::pat_util::EnumerateAndAdjustIterator; @@ -141,13 +141,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { } // `x..y` where `x >= y`. The range is empty => error. (RangeEnd::Excluded, _) => { - struct_span_err!( - self.tcx.sess, - span, - E0579, - "lower range bound must be less than upper" - ) - .emit(); + self.tcx.sess.emit_err(LowerRangeBoundMustBeLessThanUpper { span }); PatKind::Wild } // `x..=y` where `x == y`. @@ -158,23 +152,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { } // `x..=y` where `x > y` hence the range is empty => error. (RangeEnd::Included, _) => { - let mut err = struct_span_err!( - self.tcx.sess, + self.tcx.sess.emit_err(LowerRangeBoundMustBeLessThanOrEqualToUpper { span, - E0030, - "lower range bound must be less than or equal to upper" - ); - err.span_label(span, "lower bound larger than upper bound"); - if self.tcx.sess.teach(&err.get_code().unwrap()) { - err.note( - "When matching against a range, the compiler \ - verifies that the range is non-empty. Range \ - patterns include both end-points, so this is \ - equivalent to requiring the start of the range \ - to be less than or equal to the end of the range.", - ); - } - err.emit(); + teach: if self.tcx.sess.teach(&error_code!(E0030)) { Some(()) } else { None }, + }); PatKind::Wild } } From 6f82a00aa1a3dc26e2ba919e539bbc2504ebf22a Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Sun, 28 Aug 2022 21:48:09 +0300 Subject: [PATCH 159/321] Migrate leading/trailing irrefutable let pattern diagnostics --- .../locales/en-US/mir_build.ftl | 26 ++++++++++++++ compiler/rustc_mir_build/src/errors.rs | 16 +++++++++ .../src/thir/pattern/check_match.rs | 35 ++++++------------- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 6bfab0774fea..280b82b4ea40 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -207,3 +207,29 @@ mir_build_lower_range_bound_must_be_less_than_or_equal_to_upper = .teach_note = When matching against a range, the compiler verifies that the range is non-empty. Range patterns include both end-points, so this is equivalent to requiring the start of the range to be less than or equal to the end of the range. mir_build_lower_range_bound_must_be_less_than_upper = lower range bound must be less than upper + +mir_build_leading_irrefutable_let_patterns = leading irrefutable {$count -> + [one] pattern + *[other] patterns + } in let chain + .note = {$count -> + [one] this pattern + *[other] these patterns + } will always match + .help = consider moving {$count -> + [one] it + *[other] them + } outside of the construct + +mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count -> + [one] pattern + *[other] patterns + } in let chain + .note = {$count -> + [one] this pattern + *[other] these patterns + } will always match + .help = consider moving {$count -> + [one] it + *[other] them + } into the body diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 96728970c5be..3331e785b95f 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -497,3 +497,19 @@ pub struct LowerRangeBoundMustBeLessThanUpper { #[primary_span] pub span: Span, } + +#[derive(LintDiagnostic)] +#[diag(mir_build::leading_irrefutable_let_patterns)] +#[note] +#[help] +pub struct LeadingIrrefutableLetPatterns { + pub count: usize, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::trailing_irrefutable_let_patterns)] +#[note] +#[help] +pub struct TrailingIrrefutableLetPatterns { + pub count: usize, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index b21cd0d1404d..8e1a3f10f4ac 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -339,29 +339,6 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> { ); return true; } - let lint_affix = |affix: &[Option<(Span, bool)>], kind, suggestion| { - let span_start = affix[0].unwrap().0; - let span_end = affix.last().unwrap().unwrap().0; - let span = span_start.to(span_end); - let cnt = affix.len(); - let s = pluralize!(cnt); - cx.tcx.struct_span_lint_hir( - IRREFUTABLE_LET_PATTERNS, - top, - span, - format!("{kind} irrefutable pattern{s} in let chain"), - |lint| { - lint.note(format!( - "{these} pattern{s} will always match", - these = pluralize!("this", cnt), - )) - .help(format!( - "consider moving {} {suggestion}", - if cnt > 1 { "them" } else { "it" } - )) - }, - ); - }; if let Some(until) = chain_refutabilities.iter().position(|r| !matches!(*r, Some((_, false)))) && until > 0 { // The chain has a non-zero prefix of irrefutable `let` statements. @@ -375,13 +352,21 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> { if !matches!(let_source, LetSource::WhileLet | LetSource::IfLetGuard) { // Emit the lint let prefix = &chain_refutabilities[..until]; - lint_affix(prefix, "leading", "outside of the construct"); + let span_start = prefix[0].unwrap().0; + let span_end = prefix.last().unwrap().unwrap().0; + let span = span_start.to(span_end); + let count = prefix.len(); + cx.tcx.emit_spanned_lint(IRREFUTABLE_LET_PATTERNS, top, span, LeadingIrrefutableLetPatterns { count }); } } if let Some(from) = chain_refutabilities.iter().rposition(|r| !matches!(*r, Some((_, false)))) && from != (chain_refutabilities.len() - 1) { // The chain has a non-empty suffix of irrefutable `let` statements let suffix = &chain_refutabilities[from + 1..]; - lint_affix(suffix, "trailing", "into the body"); + let span_start = suffix[0].unwrap().0; + let span_end = suffix.last().unwrap().unwrap().0; + let span = span_start.to(span_end); + let count = suffix.len(); + cx.tcx.emit_spanned_lint(IRREFUTABLE_LET_PATTERNS, top, span, TrailingIrrefutableLetPatterns { count }); } true } From e1c5073c07e52ebf9e8217d8bb4aaf795eda7a49 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Mon, 29 Aug 2022 10:06:50 +0300 Subject: [PATCH 160/321] Migrate pattern bindings with variant name lint --- .../locales/en-US/mir_build.ftl | 4 +++ compiler/rustc_mir_build/src/errors.rs | 11 ++++++- .../src/thir/pattern/check_match.rs | 32 ++++++------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 280b82b4ea40..539794086ee2 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -233,3 +233,7 @@ mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count -> [one] it *[other] them } into the body + +mir_build_bindings_with_variant_name = + pattern binding `{$ident}` is named the same as one of the variants of the type `{$ty_path}` + .suggestion = to match on the variant, qualify the path diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 3331e785b95f..085511a9d7c0 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -3,7 +3,7 @@ use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; use rustc_middle::ty::{self, Ty}; use rustc_session::{parse::ParseSess, SessionDiagnostic}; -use rustc_span::Span; +use rustc_span::{symbol::Ident, Span}; #[derive(LintDiagnostic)] #[diag(mir_build::unconditional_recursion)] @@ -513,3 +513,12 @@ pub struct LeadingIrrefutableLetPatterns { pub struct TrailingIrrefutableLetPatterns { pub count: usize, } + +#[derive(LintDiagnostic)] +#[diag(mir_build::bindings_with_variant_name, code = "E0170")] +pub struct BindingsWithVariantName { + #[suggestion(code = "{ty_path}::{ident}", applicability = "machine-applicable")] + pub suggestion: Option, + pub ty_path: String, + pub ident: Ident, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 8e1a3f10f4ac..a62c1a232fac 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -9,8 +9,8 @@ use crate::errors::*; use rustc_arena::TypedArena; use rustc_ast::Mutability; use rustc_errors::{ - error_code, pluralize, struct_span_err, Applicability, DelayDm, Diagnostic, DiagnosticBuilder, - ErrorGuaranteed, MultiSpan, + pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, + MultiSpan, }; use rustc_hir as hir; use rustc_hir::def::*; @@ -547,32 +547,20 @@ fn check_for_bindings_named_same_as_variants( }) { let variant_count = edef.variants().len(); - cx.tcx.struct_span_lint_hir( + let ty_path = cx.tcx.def_path_str(edef.did()); + cx.tcx.emit_spanned_lint( BINDINGS_WITH_VARIANT_NAME, p.hir_id, p.span, - DelayDm(|| format!( - "pattern binding `{}` is named the same as one \ - of the variants of the type `{}`", - ident, cx.tcx.def_path_str(edef.did()) - )), - |lint| { - let ty_path = cx.tcx.def_path_str(edef.did()); - lint.code(error_code!(E0170)); - + BindingsWithVariantName { // If this is an irrefutable pattern, and there's > 1 variant, // then we can't actually match on this. Applying the below // suggestion would produce code that breaks on `check_irrefutable`. - if rf == Refutable || variant_count == 1 { - lint.span_suggestion( - p.span, - "to match on the variant, qualify the path", - format!("{}::{}", ty_path, ident), - Applicability::MachineApplicable, - ); - } - - lint + suggestion: if rf == Refutable || variant_count == 1 { + Some(p.span) + } else { None }, + ty_path, + ident, }, ) } From eeef05b318d1473ebedb90c84642b699e0a9b3e8 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Mon, 29 Aug 2022 11:21:25 +0300 Subject: [PATCH 161/321] Migrate irrefutable let pattern diagnostics --- .../locales/en-US/mir_build.ftl | 50 +++++++++++++ compiler/rustc_mir_build/src/errors.rs | 40 +++++++++++ .../src/thir/pattern/check_match.rs | 70 ++++--------------- 3 files changed, 103 insertions(+), 57 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 539794086ee2..7555bd608498 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -237,3 +237,53 @@ mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count -> mir_build_bindings_with_variant_name = pattern binding `{$ident}` is named the same as one of the variants of the type `{$ty_path}` .suggestion = to match on the variant, qualify the path + +mir_build_irrefutable_let_patterns_generic_let = irrefutable `let` {$count -> + [one] pattern + *[other] patterns + } + .note = {$count -> + [one] this pattern + *[other] these patterns + } will always match, so the `let` is useless + .help = consider removing `let` + +mir_build_irrefutable_let_patterns_if_let = irrefutable `if let` {$count -> + [one] pattern + *[other] patterns + } + .note = {$count -> + [one] this pattern + *[other] these patterns + } will always match, so the `if let` is useless + .help = consider replacing the `if let` with a `let` + +mir_build_irrefutable_let_patterns_if_let_guard = irrefutable `if let` guard {$count -> + [one] pattern + *[other] patterns + } + .note = {$count -> + [one] this pattern + *[other] these patterns + } will always match, so the guard is useless + .help = consider removing the guard and adding a `let` inside the match arm + +mir_build_irrefutable_let_patterns_let_else = irrefutable `let...else` {$count -> + [one] pattern + *[other] patterns + } + .note = {$count -> + [one] this pattern + *[other] these patterns + } will always match, so the `else` clause is useless + .help = consider removing the `else` clause + +mir_build_irrefutable_let_patterns_while_let = irrefutable `while let` {$count -> + [one] pattern + *[other] patterns + } + .note = {$count -> + [one] this pattern + *[other] these patterns + } will always match, so the loop will never exit + .help = consider instead using a `loop {"{"} ... {"}"}` with a `let` inside it diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 085511a9d7c0..eecef26b94cb 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -522,3 +522,43 @@ pub struct BindingsWithVariantName { pub ty_path: String, pub ident: Ident, } + +#[derive(LintDiagnostic)] +#[diag(mir_build::irrefutable_let_patterns_generic_let)] +#[note] +#[help] +pub struct IrrefutableLetPatternsGenericLet { + pub count: usize, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::irrefutable_let_patterns_if_let)] +#[note] +#[help] +pub struct IrrefutableLetPatternsIfLet { + pub count: usize, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::irrefutable_let_patterns_if_let_guard)] +#[note] +#[help] +pub struct IrrefutableLetPatternsIfLetGuard { + pub count: usize, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::irrefutable_let_patterns_let_else)] +#[note] +#[help] +pub struct IrrefutableLetPatternsLetElse { + pub count: usize, +} + +#[derive(LintDiagnostic)] +#[diag(mir_build::irrefutable_let_patterns_while_let)] +#[note] +#[help] +pub struct IrrefutableLetPatternsWhileLet { + pub count: usize, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index a62c1a232fac..a488253b992b 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -598,68 +598,24 @@ fn irrefutable_let_patterns( count: usize, span: Span, ) { + let span = match source { + LetSource::LetElse(span) => span, + _ => span, + }; + macro_rules! emit_diag { - ( - $lint:expr, - $source_name:expr, - $note_sufix:expr, - $help_sufix:expr - ) => {{ - let s = pluralize!(count); - let these = pluralize!("this", count); - tcx.struct_span_lint_hir( - IRREFUTABLE_LET_PATTERNS, - id, - span, - format!("irrefutable {} pattern{s}", $source_name), - |lint| { - lint.note(&format!( - "{these} pattern{s} will always match, so the {}", - $note_sufix - )) - .help(concat!("consider ", $help_sufix)) - }, - ) + ($lint:tt) => {{ + tcx.emit_spanned_lint(IRREFUTABLE_LET_PATTERNS, id, span, $lint { count }); }}; } match source { - LetSource::GenericLet => { - emit_diag!(lint, "`let`", "`let` is useless", "removing `let`"); - } - LetSource::IfLet => { - emit_diag!( - lint, - "`if let`", - "`if let` is useless", - "replacing the `if let` with a `let`" - ); - } - LetSource::IfLetGuard => { - emit_diag!( - lint, - "`if let` guard", - "guard is useless", - "removing the guard and adding a `let` inside the match arm" - ); - } - LetSource::LetElse => { - emit_diag!( - lint, - "`let...else`", - "`else` clause is useless", - "removing the `else` clause" - ); - } - LetSource::WhileLet => { - emit_diag!( - lint, - "`while let`", - "loop will never exit", - "instead using a `loop { ... }` with a `let` inside it" - ); - } - }; + LetSource::GenericLet => emit_diag!(IrrefutableLetPatternsGenericLet), + LetSource::IfLet => emit_diag!(IrrefutableLetPatternsIfLet), + LetSource::IfLetGuard => emit_diag!(IrrefutableLetPatternsIfLetGuard), + LetSource::LetElse(..) => emit_diag!(IrrefutableLetPatternsLetElse), + LetSource::WhileLet => emit_diag!(IrrefutableLetPatternsWhileLet), + } } fn is_let_irrefutable<'p, 'tcx>( From 3a9a06311d8d2445d47f087ef68f702b643bc4df Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Tue, 30 Aug 2022 20:01:28 +0300 Subject: [PATCH 162/321] Migrate borrow of moved value diagnostic --- .../locales/en-US/mir_build.ftl | 6 +++++ compiler/rustc_mir_build/src/errors.rs | 16 +++++++++++++ .../src/thir/pattern/check_match.rs | 24 ++++++------------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 7555bd608498..118ca985b87f 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -287,3 +287,9 @@ mir_build_irrefutable_let_patterns_while_let = irrefutable `while let` {$count - *[other] these patterns } will always match, so the loop will never exit .help = consider instead using a `loop {"{"} ... {"}"}` with a `let` inside it + +mir_build_borrow_of_moved_value = borrow of moved value + .label = value moved into `{$name}` here + .occurs_because_label = move occurs because `{$name}` has type `{$ty}` which does not implement the `Copy` trait + .value_borrowed_label = value borrowed here after move + .suggest_borrowing = borrow this binding in the pattern to avoid moving the value diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index eecef26b94cb..639c07f41940 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -562,3 +562,19 @@ pub struct IrrefutableLetPatternsLetElse { pub struct IrrefutableLetPatternsWhileLet { pub count: usize, } + +#[derive(SessionDiagnostic)] +#[diag(mir_build::borrow_of_moved_value)] +pub struct BorrowOfMovedValue<'tcx> { + #[primary_span] + pub span: Span, + #[label] + #[label(mir_build::occurs_because_label)] + pub binding_span: Span, + #[label(mir_build::value_borrowed_label)] + pub conflicts_ref: Vec, + pub name: Ident, + pub ty: Ty<'tcx>, + #[suggestion(code = "ref ", applicability = "machine-applicable")] + pub suggest_borrowing: Option, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index a488253b992b..3b6b188bc6cf 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -962,24 +962,14 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa } }); if !conflicts_ref.is_empty() { - let occurs_because = format!( - "move occurs because `{}` has type `{}` which does not implement the `Copy` trait", + sess.emit_err(BorrowOfMovedValue { + span: pat.span, + binding_span, + conflicts_ref, name, - typeck_results.node_type(pat.hir_id), - ); - let mut err = sess.struct_span_err(pat.span, "borrow of moved value"); - err.span_label(binding_span, format!("value moved into `{}` here", name)) - .span_label(binding_span, occurs_because) - .span_labels(conflicts_ref, "value borrowed here after move"); - if pat.span.contains(binding_span) { - err.span_suggestion_verbose( - binding_span.shrink_to_lo(), - "borrow this binding in the pattern to avoid moving the value", - "ref ".to_string(), - Applicability::MachineApplicable, - ); - } - err.emit(); + ty: typeck_results.node_type(pat.hir_id), + suggest_borrowing: pat.span.contains(binding_span).then(|| binding_span.shrink_to_lo()), + }); } return; } From e71a722fa69974102e220444523b824da7eaa522 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Tue, 30 Aug 2022 20:38:10 +0300 Subject: [PATCH 163/321] Migrate multiple mut borrows diagnostic --- .../locales/en-US/mir_build.ftl | 6 ++++ compiler/rustc_mir_build/src/errors.rs | 34 +++++++++++++++++++ .../src/thir/pattern/check_match.rs | 14 ++++---- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 118ca985b87f..94e061a2af6a 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -293,3 +293,9 @@ mir_build_borrow_of_moved_value = borrow of moved value .occurs_because_label = move occurs because `{$name}` has type `{$ty}` which does not implement the `Copy` trait .value_borrowed_label = value borrowed here after move .suggest_borrowing = borrow this binding in the pattern to avoid moving the value + +mir_build_multiple_mut_borrows = cannot borrow value as mutable more than once at a time + .label = first mutable borrow, by `{$name}`, occurs here + .mutable_borrow = another mutable borrow, by `{$name_mut}`, occurs here + .immutable_borrow = also borrowed as immutable, by `{$name_immut}`, here + .moved = also moved into `{$name_moved}` here diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 639c07f41940..dd715fcfc6c5 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -578,3 +578,37 @@ pub struct BorrowOfMovedValue<'tcx> { #[suggestion(code = "ref ", applicability = "machine-applicable")] pub suggest_borrowing: Option, } + +#[derive(SessionDiagnostic)] +#[diag(mir_build::multiple_mut_borrows)] +pub struct MultipleMutBorrows { + #[primary_span] + pub span: Span, + #[label] + pub binding_span: Span, + #[subdiagnostic] + pub occurences: Vec, + pub name: Ident, +} + +#[derive(SessionSubdiagnostic)] +pub enum MultipleMutBorrowOccurence { + #[label(mir_build::mutable_borrow)] + Mutable { + #[primary_span] + span: Span, + name_mut: Ident, + }, + #[label(mir_build::immutable_borrow)] + Immutable { + #[primary_span] + span: Span, + name_immut: Ident, + }, + #[label(mir_build::moved)] + Moved { + #[primary_span] + span: Span, + name_moved: Ident, + }, +} diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 3b6b188bc6cf..374942ee798b 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -999,19 +999,19 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa // Report errors if any. if !conflicts_mut_mut.is_empty() { // Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`. - let mut err = sess - .struct_span_err(pat.span, "cannot borrow value as mutable more than once at a time"); - err.span_label(binding_span, format!("first mutable borrow, by `{}`, occurs here", name)); + let mut occurences = vec![]; + for (span, name) in conflicts_mut_mut { - err.span_label(span, format!("another mutable borrow, by `{}`, occurs here", name)); + occurences.push(MultipleMutBorrowOccurence::Mutable { span, name_mut: name }); } for (span, name) in conflicts_mut_ref { - err.span_label(span, format!("also borrowed as immutable, by `{}`, here", name)); + occurences.push(MultipleMutBorrowOccurence::Immutable { span, name_immut: name }); } for (span, name) in conflicts_move { - err.span_label(span, format!("also moved into `{}` here", name)); + occurences.push(MultipleMutBorrowOccurence::Moved { span, name_moved: name }); } - err.emit(); + + sess.emit_err(MultipleMutBorrows { span: pat.span, binding_span, occurences, name }); } else if !conflicts_mut_ref.is_empty() { // Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse. let (primary, also) = match mut_outer { From 1bdf1125292d9b0545581ea2a4473a2d80bd9813 Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Sat, 26 Nov 2022 21:22:49 +0100 Subject: [PATCH 164/321] Resolve various merge conflicts --- .../locales/en-US/mir_build.ftl | 2 +- compiler/rustc_error_messages/src/lib.rs | 2 +- compiler/rustc_mir_build/src/errors.rs | 228 +++++++++--------- .../src/thir/pattern/check_match.rs | 25 +- .../rustc_mir_build/src/thir/pattern/mod.rs | 2 +- 5 files changed, 129 insertions(+), 130 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index 94e061a2af6a..60d3d3e69abb 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -292,7 +292,7 @@ mir_build_borrow_of_moved_value = borrow of moved value .label = value moved into `{$name}` here .occurs_because_label = move occurs because `{$name}` has type `{$ty}` which does not implement the `Copy` trait .value_borrowed_label = value borrowed here after move - .suggest_borrowing = borrow this binding in the pattern to avoid moving the value + .suggestion = borrow this binding in the pattern to avoid moving the value mir_build_multiple_mut_borrows = cannot borrow value as mutable more than once at a time .label = first mutable borrow, by `{$name}`, occurs here diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index db62643bc24a..25d0e736e599 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -57,8 +57,8 @@ fluent_messages! { lint => "../locales/en-US/lint.ftl", metadata => "../locales/en-US/metadata.ftl", middle => "../locales/en-US/middle.ftl", - mir_dataflow => "../locales/en-US/mir_dataflow.ftl", mir_build => "../locales/en-US/mir_build.ftl", + mir_dataflow => "../locales/en-US/mir_dataflow.ftl", monomorphize => "../locales/en-US/monomorphize.ftl", parse => "../locales/en-US/parse.ftl", passes => "../locales/en-US/passes.ftl", diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index dd715fcfc6c5..f3bfafb37b77 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -1,22 +1,24 @@ use crate::thir::pattern::MatchCheckCtxt; -use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan}; -use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; +use rustc_errors::Handler; +use rustc_errors::{ + error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic, MultiSpan, +}; +use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_middle::ty::{self, Ty}; -use rustc_session::{parse::ParseSess, SessionDiagnostic}; use rustc_span::{symbol::Ident, Span}; #[derive(LintDiagnostic)] -#[diag(mir_build::unconditional_recursion)] +#[diag(mir_build_unconditional_recursion)] #[help] pub struct UnconditionalRecursion { #[label] pub span: Span, - #[label(mir_build::unconditional_recursion_call_site_label)] + #[label(mir_build_unconditional_recursion_call_site_label)] pub call_sites: Vec, } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> { #[label] @@ -25,7 +27,7 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafe<'a> { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_unsafe_fn_requires_unsafe_nameless)] #[note] pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { #[label] @@ -33,7 +35,7 @@ pub struct UnsafeOpInUnsafeFnCallToUnsafeFunctionRequiresUnsafeNameless { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_inline_assembly_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_inline_assembly_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { #[label] @@ -41,7 +43,7 @@ pub struct UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_initializing_type_with_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_initializing_type_with_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { #[label] @@ -49,7 +51,7 @@ pub struct UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_mutable_static_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_mutable_static_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { #[label] @@ -57,7 +59,7 @@ pub struct UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_extern_static_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_extern_static_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { #[label] @@ -65,7 +67,7 @@ pub struct UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_deref_raw_pointer_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_deref_raw_pointer_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { #[label] @@ -73,7 +75,7 @@ pub struct UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_union_field_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_union_field_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { #[label] @@ -81,7 +83,7 @@ pub struct UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_mutation_of_layout_constrained_field_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_mutation_of_layout_constrained_field_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { #[label] @@ -89,14 +91,14 @@ pub struct UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe)] pub struct UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { #[label] pub span: Span, } #[derive(LintDiagnostic)] -#[diag(mir_build::unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe)] +#[diag(mir_build_unsafe_op_in_unsafe_fn_call_to_fn_with_requires_unsafe)] #[note] pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> { #[label] @@ -104,8 +106,8 @@ pub struct UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe<'a> { pub function: &'a str, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::call_to_unsafe_fn_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_call_to_unsafe_fn_requires_unsafe, code = "E0133")] #[note] pub struct CallToUnsafeFunctionRequiresUnsafe<'a> { #[primary_span] @@ -114,8 +116,8 @@ pub struct CallToUnsafeFunctionRequiresUnsafe<'a> { pub function: &'a str, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::call_to_unsafe_fn_requires_unsafe_nameless, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_call_to_unsafe_fn_requires_unsafe_nameless, code = "E0133")] #[note] pub struct CallToUnsafeFunctionRequiresUnsafeNameless { #[primary_span] @@ -123,8 +125,8 @@ pub struct CallToUnsafeFunctionRequiresUnsafeNameless { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_call_to_unsafe_fn_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] #[note] pub struct CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> { #[primary_span] @@ -133,9 +135,9 @@ pub struct CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> { pub function: &'a str, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag( - mir_build::call_to_unsafe_fn_requires_unsafe_nameless_unsafe_op_in_unsafe_fn_allowed, + mir_build_call_to_unsafe_fn_requires_unsafe_nameless_unsafe_op_in_unsafe_fn_allowed, code = "E0133" )] #[note] @@ -145,8 +147,8 @@ pub struct CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::inline_assembly_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_inline_assembly_requires_unsafe, code = "E0133")] #[note] pub struct UseOfInlineAssemblyRequiresUnsafe { #[primary_span] @@ -154,8 +156,8 @@ pub struct UseOfInlineAssemblyRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] #[note] pub struct UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed { #[primary_span] @@ -163,8 +165,8 @@ pub struct UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::initializing_type_with_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_initializing_type_with_requires_unsafe, code = "E0133")] #[note] pub struct InitializingTypeWithRequiresUnsafe { #[primary_span] @@ -172,9 +174,9 @@ pub struct InitializingTypeWithRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag( - mir_build::initializing_type_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, + mir_build_initializing_type_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133" )] #[note] @@ -184,8 +186,8 @@ pub struct InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::mutable_static_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_mutable_static_requires_unsafe, code = "E0133")] #[note] pub struct UseOfMutableStaticRequiresUnsafe { #[primary_span] @@ -193,8 +195,8 @@ pub struct UseOfMutableStaticRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::mutable_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_mutable_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] #[note] pub struct UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { #[primary_span] @@ -202,8 +204,8 @@ pub struct UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::extern_static_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_extern_static_requires_unsafe, code = "E0133")] #[note] pub struct UseOfExternStaticRequiresUnsafe { #[primary_span] @@ -211,8 +213,8 @@ pub struct UseOfExternStaticRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] #[note] pub struct UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { #[primary_span] @@ -220,8 +222,8 @@ pub struct UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::deref_raw_pointer_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_deref_raw_pointer_requires_unsafe, code = "E0133")] #[note] pub struct DerefOfRawPointerRequiresUnsafe { #[primary_span] @@ -229,8 +231,8 @@ pub struct DerefOfRawPointerRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] #[note] pub struct DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed { #[primary_span] @@ -238,8 +240,8 @@ pub struct DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::union_field_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_union_field_requires_unsafe, code = "E0133")] #[note] pub struct AccessToUnionFieldRequiresUnsafe { #[primary_span] @@ -247,8 +249,8 @@ pub struct AccessToUnionFieldRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_union_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] #[note] pub struct AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { #[primary_span] @@ -256,8 +258,8 @@ pub struct AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::mutation_of_layout_constrained_field_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_mutation_of_layout_constrained_field_requires_unsafe, code = "E0133")] #[note] pub struct MutationOfLayoutConstrainedFieldRequiresUnsafe { #[primary_span] @@ -265,9 +267,9 @@ pub struct MutationOfLayoutConstrainedFieldRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag( - mir_build::mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, + mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133" )] #[note] @@ -277,8 +279,8 @@ pub struct MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllow pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::borrow_of_layout_constrained_field_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_borrow_of_layout_constrained_field_requires_unsafe, code = "E0133")] #[note] pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafe { #[primary_span] @@ -286,9 +288,9 @@ pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafe { pub span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[diag( - mir_build::borrow_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, + mir_build_borrow_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133" )] #[note] @@ -298,8 +300,8 @@ pub struct BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::call_to_fn_with_requires_unsafe, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_call_to_fn_with_requires_unsafe, code = "E0133")] #[note] pub struct CallToFunctionWithRequiresUnsafe<'a> { #[primary_span] @@ -308,8 +310,8 @@ pub struct CallToFunctionWithRequiresUnsafe<'a> { pub function: &'a str, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] +#[derive(Diagnostic)] +#[diag(mir_build_call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed, code = "E0133")] #[note] pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> { #[primary_span] @@ -319,7 +321,7 @@ pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> { } #[derive(LintDiagnostic)] -#[diag(mir_build::unused_unsafe)] +#[diag(mir_build_unused_unsafe)] pub struct UnusedUnsafe { #[label] pub span: Span, @@ -327,14 +329,14 @@ pub struct UnusedUnsafe { pub enclosing: Option, } -#[derive(SessionSubdiagnostic)] +#[derive(Subdiagnostic)] pub enum UnusedUnsafeEnclosing { - #[label(mir_build::unused_unsafe_enclosing_block_label)] + #[label(mir_build_unused_unsafe_enclosing_block_label)] Block { #[primary_span] span: Span, }, - #[label(mir_build::unused_unsafe_enclosing_fn_label)] + #[label(mir_build_unused_unsafe_enclosing_fn_label)] Function { #[primary_span] span: Span, @@ -348,11 +350,11 @@ pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> { pub ty: Ty<'tcx>, } -impl<'a> SessionDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> { - fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'_, ErrorGuaranteed> { - let mut diag = sess.span_diagnostic.struct_span_err_with_code( +impl<'a> IntoDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> { + fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'_, ErrorGuaranteed> { + let mut diag = handler.struct_span_err_with_code( self.span, - rustc_errors::fluent::mir_build::non_exhaustive_patterns_type_not_empty, + rustc_errors::fluent::mir_build_non_exhaustive_patterns_type_not_empty, error_code!(E0004), ); @@ -374,7 +376,7 @@ impl<'a> SessionDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> let mut span: MultiSpan = def_span.into(); span.push_span_label(def_span, ""); - diag.span_note(span, rustc_errors::fluent::mir_build::def_note); + diag.span_note(span, rustc_errors::fluent::def_note); } let is_variant_list_non_exhaustive = match self.ty.kind() { @@ -385,14 +387,14 @@ impl<'a> SessionDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> }; if is_variant_list_non_exhaustive { - diag.note(rustc_errors::fluent::mir_build::non_exhaustive_type_note); + diag.note(rustc_errors::fluent::non_exhaustive_type_note); } else { - diag.note(rustc_errors::fluent::mir_build::type_note); + diag.note(rustc_errors::fluent::type_note); } if let ty::Ref(_, sub_ty, _) = self.ty.kind() { - if self.cx.tcx.is_ty_uninhabited_from(self.cx.module, *sub_ty, self.cx.param_env) { - diag.note(rustc_errors::fluent::mir_build::reference_note); + if !sub_ty.is_inhabited_from(self.cx.tcx, self.cx.module, self.cx.param_env) { + diag.note(rustc_errors::fluent::reference_note); } } @@ -418,88 +420,88 @@ impl<'a> SessionDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> if let Some((span, sugg)) = suggestion { diag.span_suggestion_verbose( span, - rustc_errors::fluent::mir_build::suggestion, + rustc_errors::fluent::suggestion, sugg, Applicability::HasPlaceholders, ); } else { - diag.help(rustc_errors::fluent::mir_build::help); + diag.help(rustc_errors::fluent::help); } diag } } -#[derive(SessionDiagnostic)] -#[diag(mir_build::static_in_pattern, code = "E0158")] +#[derive(Diagnostic)] +#[diag(mir_build_static_in_pattern, code = "E0158")] pub struct StaticInPattern { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::assoc_const_in_pattern, code = "E0158")] +#[derive(Diagnostic)] +#[diag(mir_build_assoc_const_in_pattern, code = "E0158")] pub struct AssocConstInPattern { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::const_param_in_pattern, code = "E0158")] +#[derive(Diagnostic)] +#[diag(mir_build_const_param_in_pattern, code = "E0158")] pub struct ConstParamInPattern { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::non_const_path, code = "E0080")] +#[derive(Diagnostic)] +#[diag(mir_build_non_const_path, code = "E0080")] pub struct NonConstPath { #[primary_span] pub span: Span, } #[derive(LintDiagnostic)] -#[diag(mir_build::unreachable_pattern)] +#[diag(mir_build_unreachable_pattern)] pub struct UnreachablePattern { #[label] pub span: Option, - #[label(mir_build::catchall_label)] + #[label(catchall_label)] pub catchall: Option, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::const_pattern_depends_on_generic_parameter)] +#[derive(Diagnostic)] +#[diag(mir_build_const_pattern_depends_on_generic_parameter)] pub struct ConstPatternDependsOnGenericParameter { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::could_not_eval_const_pattern)] +#[derive(Diagnostic)] +#[diag(mir_build_could_not_eval_const_pattern)] pub struct CouldNotEvalConstPattern { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::lower_range_bound_must_be_less_than_or_equal_to_upper, code = "E0030")] +#[derive(Diagnostic)] +#[diag(mir_build_lower_range_bound_must_be_less_than_or_equal_to_upper, code = "E0030")] pub struct LowerRangeBoundMustBeLessThanOrEqualToUpper { #[primary_span] #[label] pub span: Span, - #[note(mir_build::teach_note)] + #[note(teach_note)] pub teach: Option<()>, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::lower_range_bound_must_be_less_than_upper, code = "E0579")] +#[derive(Diagnostic)] +#[diag(mir_build_lower_range_bound_must_be_less_than_upper, code = "E0579")] pub struct LowerRangeBoundMustBeLessThanUpper { #[primary_span] pub span: Span, } #[derive(LintDiagnostic)] -#[diag(mir_build::leading_irrefutable_let_patterns)] +#[diag(mir_build_leading_irrefutable_let_patterns)] #[note] #[help] pub struct LeadingIrrefutableLetPatterns { @@ -507,7 +509,7 @@ pub struct LeadingIrrefutableLetPatterns { } #[derive(LintDiagnostic)] -#[diag(mir_build::trailing_irrefutable_let_patterns)] +#[diag(mir_build_trailing_irrefutable_let_patterns)] #[note] #[help] pub struct TrailingIrrefutableLetPatterns { @@ -515,7 +517,7 @@ pub struct TrailingIrrefutableLetPatterns { } #[derive(LintDiagnostic)] -#[diag(mir_build::bindings_with_variant_name, code = "E0170")] +#[diag(mir_build_bindings_with_variant_name, code = "E0170")] pub struct BindingsWithVariantName { #[suggestion(code = "{ty_path}::{ident}", applicability = "machine-applicable")] pub suggestion: Option, @@ -524,7 +526,7 @@ pub struct BindingsWithVariantName { } #[derive(LintDiagnostic)] -#[diag(mir_build::irrefutable_let_patterns_generic_let)] +#[diag(mir_build_irrefutable_let_patterns_generic_let)] #[note] #[help] pub struct IrrefutableLetPatternsGenericLet { @@ -532,7 +534,7 @@ pub struct IrrefutableLetPatternsGenericLet { } #[derive(LintDiagnostic)] -#[diag(mir_build::irrefutable_let_patterns_if_let)] +#[diag(mir_build_irrefutable_let_patterns_if_let)] #[note] #[help] pub struct IrrefutableLetPatternsIfLet { @@ -540,7 +542,7 @@ pub struct IrrefutableLetPatternsIfLet { } #[derive(LintDiagnostic)] -#[diag(mir_build::irrefutable_let_patterns_if_let_guard)] +#[diag(mir_build_irrefutable_let_patterns_if_let_guard)] #[note] #[help] pub struct IrrefutableLetPatternsIfLetGuard { @@ -548,7 +550,7 @@ pub struct IrrefutableLetPatternsIfLetGuard { } #[derive(LintDiagnostic)] -#[diag(mir_build::irrefutable_let_patterns_let_else)] +#[diag(mir_build_irrefutable_let_patterns_let_else)] #[note] #[help] pub struct IrrefutableLetPatternsLetElse { @@ -556,22 +558,22 @@ pub struct IrrefutableLetPatternsLetElse { } #[derive(LintDiagnostic)] -#[diag(mir_build::irrefutable_let_patterns_while_let)] +#[diag(mir_build_irrefutable_let_patterns_while_let)] #[note] #[help] pub struct IrrefutableLetPatternsWhileLet { pub count: usize, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::borrow_of_moved_value)] +#[derive(Diagnostic)] +#[diag(mir_build_borrow_of_moved_value)] pub struct BorrowOfMovedValue<'tcx> { #[primary_span] pub span: Span, #[label] - #[label(mir_build::occurs_because_label)] + #[label(occurs_because_label)] pub binding_span: Span, - #[label(mir_build::value_borrowed_label)] + #[label(value_borrowed_label)] pub conflicts_ref: Vec, pub name: Ident, pub ty: Ty<'tcx>, @@ -579,33 +581,33 @@ pub struct BorrowOfMovedValue<'tcx> { pub suggest_borrowing: Option, } -#[derive(SessionDiagnostic)] -#[diag(mir_build::multiple_mut_borrows)] +#[derive(Diagnostic)] +#[diag(mir_build_multiple_mut_borrows)] pub struct MultipleMutBorrows { #[primary_span] pub span: Span, #[label] pub binding_span: Span, - #[subdiagnostic] + #[subdiagnostic(eager)] pub occurences: Vec, pub name: Ident, } -#[derive(SessionSubdiagnostic)] +#[derive(Subdiagnostic)] pub enum MultipleMutBorrowOccurence { - #[label(mir_build::mutable_borrow)] + #[label(mutable_borrow)] Mutable { #[primary_span] span: Span, name_mut: Ident, }, - #[label(mir_build::immutable_borrow)] + #[label(immutable_borrow)] Immutable { #[primary_span] span: Span, name_immut: Ident, }, - #[label(mir_build::moved)] + #[label(moved)] Moved { #[primary_span] span: Span, diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 374942ee798b..ddd52114ecfe 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -598,11 +598,6 @@ fn irrefutable_let_patterns( count: usize, span: Span, ) { - let span = match source { - LetSource::LetElse(span) => span, - _ => span, - }; - macro_rules! emit_diag { ($lint:tt) => {{ tcx.emit_spanned_lint(IRREFUTABLE_LET_PATTERNS, id, span, $lint { count }); @@ -613,7 +608,7 @@ fn irrefutable_let_patterns( LetSource::GenericLet => emit_diag!(IrrefutableLetPatternsGenericLet), LetSource::IfLet => emit_diag!(IrrefutableLetPatternsIfLet), LetSource::IfLetGuard => emit_diag!(IrrefutableLetPatternsIfLetGuard), - LetSource::LetElse(..) => emit_diag!(IrrefutableLetPatternsLetElse), + LetSource::LetElse => emit_diag!(IrrefutableLetPatternsLetElse), LetSource::WhileLet => emit_diag!(IrrefutableLetPatternsWhileLet), } } @@ -968,7 +963,10 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa conflicts_ref, name, ty: typeck_results.node_type(pat.hir_id), - suggest_borrowing: pat.span.contains(binding_span).then(|| binding_span.shrink_to_lo()), + suggest_borrowing: pat + .span + .contains(binding_span) + .then(|| binding_span.shrink_to_lo()), }); } return; @@ -1001,16 +999,15 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa // Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`. let mut occurences = vec![]; - for (span, name) in conflicts_mut_mut { - occurences.push(MultipleMutBorrowOccurence::Mutable { span, name_mut: name }); + for (span, name_mut) in conflicts_mut_mut { + occurences.push(MultipleMutBorrowOccurence::Mutable { span, name_mut }); } - for (span, name) in conflicts_mut_ref { - occurences.push(MultipleMutBorrowOccurence::Immutable { span, name_immut: name }); + for (span, name_immut) in conflicts_mut_ref { + occurences.push(MultipleMutBorrowOccurence::Immutable { span, name_immut }); } - for (span, name) in conflicts_move { - occurences.push(MultipleMutBorrowOccurence::Moved { span, name_moved: name }); + for (span, name_moved) in conflicts_move { + occurences.push(MultipleMutBorrowOccurence::Moved { span, name_moved }); } - sess.emit_err(MultipleMutBorrows { span: pat.span, binding_span, occurences, name }); } else if !conflicts_mut_ref.is_empty() { // Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse. diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index b7d1dc8c9565..2c775b397182 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -565,7 +565,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { _ => bug!("Expected ConstKind::Param"), }, mir::ConstantKind::Val(_, _) => self.const_to_pat(value, id, span, false).kind, - mir::ConstKind::Unevaluated(_) => { + mir::ConstantKind::Unevaluated(..) => { // If we land here it means the const can't be evaluated because it's `TooGeneric`. self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span }); return PatKind::Wild; From 6749ee4d7b7a2f6fd57cf15fdb7332d6e94289dc Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Sat, 17 Dec 2022 19:20:44 +0100 Subject: [PATCH 165/321] Remove (eager) --- compiler/rustc_mir_build/src/errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index f3bfafb37b77..68179001b916 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -588,7 +588,7 @@ pub struct MultipleMutBorrows { pub span: Span, #[label] pub binding_span: Span, - #[subdiagnostic(eager)] + #[subdiagnostic] pub occurences: Vec, pub name: Ident, } From 4c3efc7f1b2dc4a815d3690c6995bed5a8c63ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 17 Dec 2022 00:00:00 +0000 Subject: [PATCH 166/321] Rename CleanupNonCodegenStatements to CleanupPostBorrowck --- compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs | 4 ++-- compiler/rustc_mir_transform/src/lib.rs | 2 +- src/test/mir-opt/remove_fake_borrows.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs b/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs index c33d72179ad1..fe5295afaced 100644 --- a/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs +++ b/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs @@ -22,9 +22,9 @@ use crate::MirPass; use rustc_middle::mir::{Body, BorrowKind, Rvalue, StatementKind}; use rustc_middle::ty::TyCtxt; -pub struct CleanupNonCodegenStatements; +pub struct CleanupPostBorrowck; -impl<'tcx> MirPass<'tcx> for CleanupNonCodegenStatements { +impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck { fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { for basic_block in body.basic_blocks.as_mut_preserves_cfg() { for statement in basic_block.statements.iter_mut() { diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 93200b28830f..e28406ce67ea 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -497,7 +497,7 @@ fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &remove_false_edges::RemoveFalseEdges, &simplify_branches::SimplifyConstCondition::new("initial"), &remove_noop_landing_pads::RemoveNoopLandingPads, - &cleanup_post_borrowck::CleanupNonCodegenStatements, + &cleanup_post_borrowck::CleanupPostBorrowck, &simplify::SimplifyCfg::new("early-opt"), &deref_separator::Derefer, ]; diff --git a/src/test/mir-opt/remove_fake_borrows.rs b/src/test/mir-opt/remove_fake_borrows.rs index a980f386b696..d26c6f5d7e51 100644 --- a/src/test/mir-opt/remove_fake_borrows.rs +++ b/src/test/mir-opt/remove_fake_borrows.rs @@ -2,7 +2,7 @@ // ignore-wasm32-bare compiled with panic=abort by default -// EMIT_MIR remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff +// EMIT_MIR remove_fake_borrows.match_guard.CleanupPostBorrowck.diff fn match_guard(x: Option<&&i32>, c: bool) -> i32 { match x { Some(0) if c => 0, From 62f9084dfa58cfb395606128bc5d33ef483d1c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 17 Dec 2022 00:00:00 +0000 Subject: [PATCH 167/321] Remove false edges in CleanupPostBorrowck --- .../src/cleanup_post_borrowck.rs | 28 +++++++++--------- compiler/rustc_mir_transform/src/lib.rs | 5 +--- .../src/remove_false_edges.rs | 29 ------------------- 3 files changed, 16 insertions(+), 46 deletions(-) delete mode 100644 compiler/rustc_mir_transform/src/remove_false_edges.rs diff --git a/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs b/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs index fe5295afaced..d435d3ee69b7 100644 --- a/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs +++ b/compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs @@ -1,32 +1,26 @@ -//! This module provides a pass to replacing the following statements with -//! [`Nop`]s +//! This module provides a pass that removes parts of MIR that are no longer relevant after +//! analysis phase and borrowck. In particular, it removes false edges, user type annotations and +//! replaces following statements with [`Nop`]s: //! //! - [`AscribeUserType`] //! - [`FakeRead`] //! - [`Assign`] statements with a [`Shallow`] borrow //! -//! The `CleanFakeReadsAndBorrows` "pass" is actually implemented as two -//! traversals (aka visits) of the input MIR. The first traversal, -//! `DeleteAndRecordFakeReads`, deletes the fake reads and finds the -//! temporaries read by [`ForMatchGuard`] reads, and `DeleteFakeBorrows` -//! deletes the initialization of those temporaries. -//! //! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType -//! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow -//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead //! [`Assign`]: rustc_middle::mir::StatementKind::Assign -//! [`ForMatchGuard`]: rustc_middle::mir::FakeReadCause::ForMatchGuard +//! [`FakeRead`]: rustc_middle::mir::StatementKind::FakeRead //! [`Nop`]: rustc_middle::mir::StatementKind::Nop +//! [`Shallow`]: rustc_middle::mir::BorrowKind::Shallow use crate::MirPass; -use rustc_middle::mir::{Body, BorrowKind, Rvalue, StatementKind}; +use rustc_middle::mir::{Body, BorrowKind, Rvalue, StatementKind, TerminatorKind}; use rustc_middle::ty::TyCtxt; pub struct CleanupPostBorrowck; impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck { fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { - for basic_block in body.basic_blocks.as_mut_preserves_cfg() { + for basic_block in body.basic_blocks.as_mut() { for statement in basic_block.statements.iter_mut() { match statement.kind { StatementKind::AscribeUserType(..) @@ -35,6 +29,14 @@ impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck { _ => (), } } + let terminator = basic_block.terminator_mut(); + match terminator.kind { + TerminatorKind::FalseEdge { real_target, .. } + | TerminatorKind::FalseUnwind { real_target, .. } => { + terminator.kind = TerminatorKind::Goto { target: real_target }; + } + _ => {} + } } body.user_type_annotations.raw.clear(); diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index e28406ce67ea..aba5a8580f19 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -77,8 +77,6 @@ mod match_branches; mod multiple_return_terminators; mod normalize_array_len; mod nrvo; -// This pass is public to allow external drivers to perform MIR cleanup -pub mod remove_false_edges; mod remove_noop_landing_pads; mod remove_storage_markers; mod remove_uninit_drops; @@ -494,10 +492,9 @@ fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx> /// After this series of passes, no lifetime analysis based on borrowing can be done. fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { let passes: &[&dyn MirPass<'tcx>] = &[ - &remove_false_edges::RemoveFalseEdges, + &cleanup_post_borrowck::CleanupPostBorrowck, &simplify_branches::SimplifyConstCondition::new("initial"), &remove_noop_landing_pads::RemoveNoopLandingPads, - &cleanup_post_borrowck::CleanupPostBorrowck, &simplify::SimplifyCfg::new("early-opt"), &deref_separator::Derefer, ]; diff --git a/compiler/rustc_mir_transform/src/remove_false_edges.rs b/compiler/rustc_mir_transform/src/remove_false_edges.rs deleted file mode 100644 index 71f5ccf7e246..000000000000 --- a/compiler/rustc_mir_transform/src/remove_false_edges.rs +++ /dev/null @@ -1,29 +0,0 @@ -use rustc_middle::mir::{Body, TerminatorKind}; -use rustc_middle::ty::TyCtxt; - -use crate::MirPass; - -/// Removes `FalseEdge` and `FalseUnwind` terminators from the MIR. -/// -/// These are only needed for borrow checking, and can be removed afterwards. -/// -/// FIXME: This should probably have its own MIR phase. -pub struct RemoveFalseEdges; - -impl<'tcx> MirPass<'tcx> for RemoveFalseEdges { - fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) { - for block in body.basic_blocks_mut() { - let terminator = block.terminator_mut(); - terminator.kind = match terminator.kind { - TerminatorKind::FalseEdge { real_target, .. } => { - TerminatorKind::Goto { target: real_target } - } - TerminatorKind::FalseUnwind { real_target, .. } => { - TerminatorKind::Goto { target: real_target } - } - - _ => continue, - } - } - } -} From acaa2c1235080aa1dc66133f840f7b5aa37dd016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 17 Dec 2022 00:00:00 +0000 Subject: [PATCH 168/321] ./x.py test --bless --- ...e_borrows.match_guard.CleanupPostBorrowck.diff} | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) rename src/test/mir-opt/{remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff => remove_fake_borrows.match_guard.CleanupPostBorrowck.diff} (90%) diff --git a/src/test/mir-opt/remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff b/src/test/mir-opt/remove_fake_borrows.match_guard.CleanupPostBorrowck.diff similarity index 90% rename from src/test/mir-opt/remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff rename to src/test/mir-opt/remove_fake_borrows.match_guard.CleanupPostBorrowck.diff index bb5920b28ca9..0b3da98a5a19 100644 --- a/src/test/mir-opt/remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff +++ b/src/test/mir-opt/remove_fake_borrows.match_guard.CleanupPostBorrowck.diff @@ -1,5 +1,5 @@ -- // MIR for `match_guard` before CleanupNonCodegenStatements -+ // MIR for `match_guard` after CleanupNonCodegenStatements +- // MIR for `match_guard` before CleanupPostBorrowck ++ // MIR for `match_guard` after CleanupPostBorrowck fn match_guard(_1: Option<&&i32>, _2: bool) -> i32 { debug x => _1; // in scope 0 at $DIR/remove_fake_borrows.rs:+0:16: +0:17 @@ -29,7 +29,8 @@ } bb3: { - goto -> bb4; // scope 0 at $DIR/remove_fake_borrows.rs:+2:9: +2:16 +- falseEdge -> [real: bb4, imaginary: bb1]; // scope 0 at $DIR/remove_fake_borrows.rs:+2:9: +2:16 ++ goto -> bb4; // scope 0 at $DIR/remove_fake_borrows.rs:+2:9: +2:16 } bb4: { @@ -62,15 +63,12 @@ bb6: { StorageDead(_8); // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21 - goto -> bb1; // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21 +- falseEdge -> [real: bb1, imaginary: bb1]; // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21 ++ goto -> bb1; // scope 0 at $DIR/remove_fake_borrows.rs:+2:20: +2:21 } bb7: { return; // scope 0 at $DIR/remove_fake_borrows.rs:+5:2: +5:2 } - - bb8 (cleanup): { - resume; // scope 0 at $DIR/remove_fake_borrows.rs:+0:1: +5:2 - } } From ab2151cbf8270be61db8add6bfb1aea03230803f Mon Sep 17 00:00:00 2001 From: Evan Jones Date: Sat, 17 Dec 2022 13:43:08 -0500 Subject: [PATCH 169/321] std::fmt: Use args directly in example code The lint "clippy::uninlined_format_args" recommends inline variables in format strings. Fix two places in the docs that do not do this. I noticed this because I copy/pasted one example in to my project, then noticed this lint error. This fixes: error: variables can be used directly in the `format!` string --> src/main.rs:30:22 | 30 | let string = format!("{:.*}", decimals, magnitude); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: variables can be used directly in the `format!` string --> src/main.rs:39:2 | 39 | write!(&mut io::stdout(), "{}", args).unwrap(); --- library/alloc/src/fmt.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index 799ce9d5daa8..eadb35cb96d4 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -419,7 +419,7 @@ //! // documentation for details, and the function `pad` can be used //! // to pad strings. //! let decimals = f.precision().unwrap_or(3); -//! let string = format!("{:.*}", decimals, magnitude); +//! let string = format!("{magnitude:.decimals$}"); //! f.pad_integral(true, "", &string) //! } //! } @@ -518,7 +518,7 @@ //! write!(&mut some_writer, "{}", format_args!("print with a {}", "macro")); //! //! fn my_fmt_fn(args: fmt::Arguments) { -//! write!(&mut io::stdout(), "{}", args); +//! write!(&mut io::stdout(), "{args}"); //! } //! my_fmt_fn(format_args!(", or a {} too", "function")); //! ``` From f7e894c000d6526c32422c6282ce277c05499a94 Mon Sep 17 00:00:00 2001 From: mejrs <> Date: Sat, 17 Dec 2022 20:11:25 +0100 Subject: [PATCH 170/321] Try to fix ICE --- .../rustc_mir_build/src/thir/pattern/check_match.rs | 6 +++++- src/test/ui/lint/lint-uppercase-variables.rs | 10 +++++----- src/test/ui/lint/lint-uppercase-variables.stderr | 12 ++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index ddd52114ecfe..a94d8d6c6431 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -17,7 +17,9 @@ use rustc_hir::def::*; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{HirId, Pat}; +use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt}; + use rustc_session::lint::builtin::{ BINDINGS_WITH_VARIANT_NAME, IRREFUTABLE_LET_PATTERNS, UNREACHABLE_PATTERNS, }; @@ -547,7 +549,9 @@ fn check_for_bindings_named_same_as_variants( }) { let variant_count = edef.variants().len(); - let ty_path = cx.tcx.def_path_str(edef.did()); + let ty_path = with_no_trimmed_paths!({ + cx.tcx.def_path_str(edef.did()) + }); cx.tcx.emit_spanned_lint( BINDINGS_WITH_VARIANT_NAME, p.hir_id, diff --git a/src/test/ui/lint/lint-uppercase-variables.rs b/src/test/ui/lint/lint-uppercase-variables.rs index b590fa697adb..d4e88aa26436 100644 --- a/src/test/ui/lint/lint-uppercase-variables.rs +++ b/src/test/ui/lint/lint-uppercase-variables.rs @@ -20,19 +20,19 @@ fn main() { match foo::Foo::Foo { Foo => {} -//~^ ERROR variable `Foo` should have a snake case name -//~^^ WARN `Foo` is named the same as one of the variants of the type `Foo` -//~^^^ WARN unused variable: `Foo` + //~^ ERROR variable `Foo` should have a snake case name + //~^^ WARN `Foo` is named the same as one of the variants of the type `foo::Foo` + //~^^^ WARN unused variable: `Foo` } let Foo = foo::Foo::Foo; //~^ ERROR variable `Foo` should have a snake case name - //~^^ WARN `Foo` is named the same as one of the variants of the type `Foo` + //~^^ WARN `Foo` is named the same as one of the variants of the type `foo::Foo` //~^^^ WARN unused variable: `Foo` fn in_param(Foo: foo::Foo) {} //~^ ERROR variable `Foo` should have a snake case name - //~^^ WARN `Foo` is named the same as one of the variants of the type `Foo` + //~^^ WARN `Foo` is named the same as one of the variants of the type `foo::Foo` //~^^^ WARN unused variable: `Foo` test(1); diff --git a/src/test/ui/lint/lint-uppercase-variables.stderr b/src/test/ui/lint/lint-uppercase-variables.stderr index 71b24a835bcd..d476d856e24c 100644 --- a/src/test/ui/lint/lint-uppercase-variables.stderr +++ b/src/test/ui/lint/lint-uppercase-variables.stderr @@ -1,22 +1,22 @@ -warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `Foo` +warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `foo::Foo` --> $DIR/lint-uppercase-variables.rs:22:9 | LL | Foo => {} - | ^^^ help: to match on the variant, qualify the path: `Foo::Foo` + | ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo` | = note: `#[warn(bindings_with_variant_name)]` on by default -warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `Foo` +warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `foo::Foo` --> $DIR/lint-uppercase-variables.rs:28:9 | LL | let Foo = foo::Foo::Foo; - | ^^^ help: to match on the variant, qualify the path: `Foo::Foo` + | ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo` -warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `Foo` +warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `foo::Foo` --> $DIR/lint-uppercase-variables.rs:33:17 | LL | fn in_param(Foo: foo::Foo) {} - | ^^^ help: to match on the variant, qualify the path: `Foo::Foo` + | ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo` warning: unused variable: `Foo` --> $DIR/lint-uppercase-variables.rs:22:9 From 4042f550795c042aee3087b0e0dcb7083e7a9e00 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 17 Dec 2022 19:20:29 +0000 Subject: [PATCH 171/321] Don't ICE in check_must_not_suspend_ty for mismatched tuple arity --- .../rustc_hir_typeck/src/generator_interior/mod.rs | 5 +---- src/test/ui/lint/must_not_suspend/tuple-mismatch.rs | 9 +++++++++ .../ui/lint/must_not_suspend/tuple-mismatch.stderr | 12 ++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/lint/must_not_suspend/tuple-mismatch.rs create mode 100644 src/test/ui/lint/must_not_suspend/tuple-mismatch.stderr diff --git a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs index 93f2ceed777b..e4ac91befac6 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs @@ -607,10 +607,7 @@ fn check_must_not_suspend_ty<'tcx>( ty::Tuple(fields) => { let mut has_emitted = false; let comps = match data.expr.map(|e| &e.kind) { - Some(hir::ExprKind::Tup(comps)) => { - debug_assert_eq!(comps.len(), fields.len()); - Some(comps) - } + Some(hir::ExprKind::Tup(comps)) if comps.len() == fields.len() => Some(comps), _ => None, }; for (i, ty) in fields.iter().enumerate() { diff --git a/src/test/ui/lint/must_not_suspend/tuple-mismatch.rs b/src/test/ui/lint/must_not_suspend/tuple-mismatch.rs new file mode 100644 index 000000000000..c7e14e425613 --- /dev/null +++ b/src/test/ui/lint/must_not_suspend/tuple-mismatch.rs @@ -0,0 +1,9 @@ +#![feature(generators)] + +fn main() { + let _generator = || { + yield ((), ((), ())); + yield ((), ()); + //~^ ERROR mismatched types + }; +} diff --git a/src/test/ui/lint/must_not_suspend/tuple-mismatch.stderr b/src/test/ui/lint/must_not_suspend/tuple-mismatch.stderr new file mode 100644 index 000000000000..cca8cd9bd89f --- /dev/null +++ b/src/test/ui/lint/must_not_suspend/tuple-mismatch.stderr @@ -0,0 +1,12 @@ +error[E0308]: mismatched types + --> $DIR/tuple-mismatch.rs:6:20 + | +LL | yield ((), ()); + | ^^ expected tuple, found `()` + | + = note: expected tuple `((), ())` + found unit type `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From b8a84c2c53328757b407829da3049c6baa049a0e Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 14 Dec 2022 13:48:44 -0600 Subject: [PATCH 172/321] Make `RUN_CHECK_WITH_PARALLEL_QUERIES` the last thing to run This takes a long time and rarely fails. It also interferes with `retry make prepare`, the retry is unhelpful since `make prepare` turns into a no-op --- src/bootstrap/config.rs | 1 + src/ci/run.sh | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index 960fbdf75380..2906616ffad1 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -638,6 +638,7 @@ define_config! { dist_stage: Option = "dist-stage", bench_stage: Option = "bench-stage", patch_binaries_for_nix: Option = "patch-binaries-for-nix", + // NOTE: only parsed by bootstrap.py, `--feature build-metrics` enables metrics unconditionally metrics: Option = "metrics", } } diff --git a/src/ci/run.sh b/src/ci/run.sh index 7de06ec35c36..f05bb81d4a1e 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -56,6 +56,7 @@ fi if ! isCI || isCiBranch auto || isCiBranch beta || isCiBranch try || isCiBranch try-perf; then RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set build.print-step-timings --enable-verbose-tests" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set build.metrics" + HAS_METRICS=1 fi RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-sccache" @@ -157,13 +158,6 @@ trap datecheck EXIT # sccache server at the start of the build, but no need to worry if this fails. SCCACHE_IDLE_TIMEOUT=10800 sccache --start-server || true -if [ "$RUN_CHECK_WITH_PARALLEL_QUERIES" != "" ]; then - $SRC/configure --set rust.parallel-compiler - CARGO_INCREMENTAL=0 $PYTHON ../x.py check - rm -f config.toml - rm -rf build -fi - $SRC/configure $RUST_CONFIGURE_ARGS retry make prepare @@ -193,4 +187,21 @@ else do_make "$RUST_CHECK_TARGET" fi +if [ "$RUN_CHECK_WITH_PARALLEL_QUERIES" != "" ]; then + rm -f config.toml + $SRC/configure --set rust.parallel-compiler + + # Save the build metrics before we wipe the directory + if [ $HAS_METRICS = 1 ]; then + mv build/metrics.json . + fi + rm -rf build + if [ $HAS_METRICS = 1 ]; then + mkdir build + mv metrics.json build + fi + + CARGO_INCREMENTAL=0 $PYTHON ../x.py check +fi + sccache --show-stats || true From ee53452aeccae1bc162126ce3dabdecc534d445b Mon Sep 17 00:00:00 2001 From: Matthew House Date: Sat, 17 Dec 2022 15:15:57 -0500 Subject: [PATCH 173/321] Suggest a `T: Send` bound for `&mut T` upvars in `Send` generators --- .../src/traits/error_reporting/suggestions.rs | 39 ++++++++------- src/test/ui/generator/ref-upvar-not-send.rs | 31 ++++++++++++ .../ui/generator/ref-upvar-not-send.stderr | 50 +++++++++++++++++++ 3 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 src/test/ui/generator/ref-upvar-not-send.rs create mode 100644 src/test/ui/generator/ref-upvar-not-send.stderr diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index d47a5ea3e370..911798b72104 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2344,28 +2344,33 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } } GeneratorInteriorOrUpvar::Upvar(upvar_span) => { - // `Some(ref_ty)` if `target_ty` is `&T` and `T` fails to impl `Sync` - let refers_to_non_sync = match target_ty.kind() { - ty::Ref(_, ref_ty, _) => match self.evaluate_obligation(&obligation) { - Ok(eval) if !eval.may_apply() => Some(ref_ty), + // `Some((ref_ty, is_mut))` if `target_ty` is `&T` or `&mut T` and fails to impl `Send` + let non_send = match target_ty.kind() { + ty::Ref(_, ref_ty, mutability) => match self.evaluate_obligation(&obligation) { + Ok(eval) if !eval.may_apply() => Some((ref_ty, mutability.is_mut())), _ => None, }, _ => None, }; - let (span_label, span_note) = match refers_to_non_sync { - // if `target_ty` is `&T` and `T` fails to impl `Sync`, - // include suggestions to make `T: Sync` so that `&T: Send` - Some(ref_ty) => ( - format!( - "has type `{}` which {}, because `{}` is not `Sync`", - target_ty, trait_explanation, ref_ty - ), - format!( - "captured value {} because `&` references cannot be sent unless their referent is `Sync`", - trait_explanation - ), - ), + let (span_label, span_note) = match non_send { + // if `target_ty` is `&T` or `&mut T` and fails to impl `Send`, + // include suggestions to make `T: Sync` so that `&T: Send`, + // or to make `T: Send` so that `&mut T: Send` + Some((ref_ty, is_mut)) => { + let ref_ty_trait = if is_mut { "Send" } else { "Sync" }; + let ref_kind = if is_mut { "&mut" } else { "&" }; + ( + format!( + "has type `{}` which {}, because `{}` is not `{}`", + target_ty, trait_explanation, ref_ty, ref_ty_trait + ), + format!( + "captured value {} because `{}` references cannot be sent unless their referent is `{}`", + trait_explanation, ref_kind, ref_ty_trait + ), + ) + } None => ( format!("has type `{}` which {}", target_ty, trait_explanation), format!("captured value {}", trait_explanation), diff --git a/src/test/ui/generator/ref-upvar-not-send.rs b/src/test/ui/generator/ref-upvar-not-send.rs new file mode 100644 index 000000000000..eb9ef63ecfcb --- /dev/null +++ b/src/test/ui/generator/ref-upvar-not-send.rs @@ -0,0 +1,31 @@ +// For `Send` generators, suggest a `T: Sync` requirement for `&T` upvars, +// and suggest a `T: Send` requirement for `&mut T` upvars. + +#![feature(generators)] + +fn assert_send(_: T) {} +//~^ NOTE required by a bound in `assert_send` +//~| NOTE required by this bound in `assert_send` +//~| NOTE required by a bound in `assert_send` +//~| NOTE required by this bound in `assert_send` + +fn main() { + let x: &*mut () = &std::ptr::null_mut(); + let y: &mut *mut () = &mut std::ptr::null_mut(); + assert_send(move || { + //~^ ERROR generator cannot be sent between threads safely + //~| NOTE generator is not `Send` + yield; + let _x = x; + }); + //~^^ NOTE captured value is not `Send` because `&` references cannot be sent unless their referent is `Sync` + //~| NOTE has type `&*mut ()` which is not `Send`, because `*mut ()` is not `Sync` + assert_send(move || { + //~^ ERROR generator cannot be sent between threads safely + //~| NOTE generator is not `Send` + yield; + let _y = y; + }); + //~^^ NOTE captured value is not `Send` because `&mut` references cannot be sent unless their referent is `Send` + //~| NOTE has type `&mut *mut ()` which is not `Send`, because `*mut ()` is not `Send` +} diff --git a/src/test/ui/generator/ref-upvar-not-send.stderr b/src/test/ui/generator/ref-upvar-not-send.stderr new file mode 100644 index 000000000000..689ace67e34e --- /dev/null +++ b/src/test/ui/generator/ref-upvar-not-send.stderr @@ -0,0 +1,50 @@ +error: generator cannot be sent between threads safely + --> $DIR/ref-upvar-not-send.rs:15:17 + | +LL | assert_send(move || { + | _________________^ +LL | | +LL | | +LL | | yield; +LL | | let _x = x; +LL | | }); + | |_____^ generator is not `Send` + | + = help: the trait `Sync` is not implemented for `*mut ()` +note: captured value is not `Send` because `&` references cannot be sent unless their referent is `Sync` + --> $DIR/ref-upvar-not-send.rs:19:18 + | +LL | let _x = x; + | ^ has type `&*mut ()` which is not `Send`, because `*mut ()` is not `Sync` +note: required by a bound in `assert_send` + --> $DIR/ref-upvar-not-send.rs:6:19 + | +LL | fn assert_send(_: T) {} + | ^^^^ required by this bound in `assert_send` + +error: generator cannot be sent between threads safely + --> $DIR/ref-upvar-not-send.rs:23:17 + | +LL | assert_send(move || { + | _________________^ +LL | | +LL | | +LL | | yield; +LL | | let _y = y; +LL | | }); + | |_____^ generator is not `Send` + | + = help: within `[generator@$DIR/ref-upvar-not-send.rs:23:17: 23:24]`, the trait `Send` is not implemented for `*mut ()` +note: captured value is not `Send` because `&mut` references cannot be sent unless their referent is `Send` + --> $DIR/ref-upvar-not-send.rs:27:18 + | +LL | let _y = y; + | ^ has type `&mut *mut ()` which is not `Send`, because `*mut ()` is not `Send` +note: required by a bound in `assert_send` + --> $DIR/ref-upvar-not-send.rs:6:19 + | +LL | fn assert_send(_: T) {} + | ^^^^ required by this bound in `assert_send` + +error: aborting due to 2 previous errors + From 6f0fc2fd337757a02fa0d948e78638d66a46f518 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 24 Nov 2022 20:39:40 -0500 Subject: [PATCH 174/321] Symlink `build/host` -> `build/x86_64-unknown-linux-gnu` (as appropriate per target) This allows us to use a consistent path in the documentation, without having to worry about which platform people are using. --- src/bootstrap/lib.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 570fe6484e3d..f0c9a948727f 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -108,6 +108,7 @@ use std::collections::{HashMap, HashSet}; use std::env; use std::fs::{self, File}; use std::io; +use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::process::Command; use std::str; @@ -119,7 +120,9 @@ use once_cell::sync::OnceCell; use crate::builder::Kind; use crate::config::{LlvmLibunwind, TargetSelection}; -use crate::util::{exe, libdir, mtime, output, run, run_suppressed, try_run_suppressed, CiEnv}; +use crate::util::{ + exe, libdir, mtime, output, run, run_suppressed, symlink_dir, try_run_suppressed, CiEnv, +}; mod bolt; mod builder; @@ -586,6 +589,20 @@ impl Build { metadata::build(&mut build); } + // Make a symbolic link so we can use a consistent directory in the documentation. + let build_triple = build.out.join(&build.build.triple); + let host = build.out.join("host"); + if let Err(e) = symlink_dir(&build.config, &build_triple, &host) { + if e.kind() != ErrorKind::AlreadyExists { + panic!( + "symlink_dir({} => {}) failed with {}", + host.display(), + build_triple.display(), + e + ); + } + } + build } From f034cb431d88a89984eb75bdea120e34fabfc798 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 17 Dec 2022 20:59:23 +0000 Subject: [PATCH 175/321] print argument name in arg mismatch if possible --- compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs | 9 ++++++++- src/test/ui/argument-suggestions/basic.stderr | 4 ++-- src/test/ui/error-codes/E0057.stderr | 4 ++-- src/test/ui/higher-rank-trait-bounds/issue-58451.stderr | 4 ++-- src/test/ui/issues/issue-3044.stderr | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 8e520e563ff6..829697c4b9a6 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -28,7 +28,7 @@ use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::{self, DefIdTree, IsSuggestable, Ty, TypeSuperVisitable, TypeVisitor}; use rustc_session::Session; -use rustc_span::symbol::Ident; +use rustc_span::symbol::{kw, Ident}; use rustc_span::{self, sym, Span}; use rustc_trait_selection::traits::{self, ObligationCauseCode, SelectionContext}; @@ -1141,6 +1141,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { "()".to_string() } else if expected_ty.is_suggestable(tcx, false) { format!("/* {} */", expected_ty) + } else if let Some(fn_def_id) = fn_def_id + && self.tcx.def_kind(fn_def_id).is_fn_like() + && let self_implicit = matches!(call_expr.kind, hir::ExprKind::MethodCall(..)) as usize + && let Some(arg) = self.tcx.fn_arg_names(fn_def_id).get(expected_idx.as_usize() + self_implicit) + && arg.name != kw::SelfLower + { + format!("/* {} */", arg.name) } else { "/* value */".to_string() } diff --git a/src/test/ui/argument-suggestions/basic.stderr b/src/test/ui/argument-suggestions/basic.stderr index b118ce1bd0ea..062b3768858d 100644 --- a/src/test/ui/argument-suggestions/basic.stderr +++ b/src/test/ui/argument-suggestions/basic.stderr @@ -94,8 +94,8 @@ LL | let closure = |x| x; | ^^^ help: provide the argument | -LL | closure(/* value */); - | ~~~~~~~~~~~~~ +LL | closure(/* x */); + | ~~~~~~~~~ error: aborting due to 6 previous errors diff --git a/src/test/ui/error-codes/E0057.stderr b/src/test/ui/error-codes/E0057.stderr index bea226f09dcb..163737895fea 100644 --- a/src/test/ui/error-codes/E0057.stderr +++ b/src/test/ui/error-codes/E0057.stderr @@ -11,8 +11,8 @@ LL | let f = |x| x * 3; | ^^^ help: provide the argument | -LL | let a = f(/* value */); - | ~~~~~~~~~~~~~ +LL | let a = f(/* x */); + | ~~~~~~~~~ error[E0057]: this function takes 1 argument but 2 arguments were supplied --> $DIR/E0057.rs:5:13 diff --git a/src/test/ui/higher-rank-trait-bounds/issue-58451.stderr b/src/test/ui/higher-rank-trait-bounds/issue-58451.stderr index 09e25f4dc966..0f051be2128b 100644 --- a/src/test/ui/higher-rank-trait-bounds/issue-58451.stderr +++ b/src/test/ui/higher-rank-trait-bounds/issue-58451.stderr @@ -11,8 +11,8 @@ LL | fn f(i: I) | ^ ---- help: provide the argument | -LL | f(&[f(/* value */)]); - | ~~~~~~~~~~~~~ +LL | f(&[f(/* i */)]); + | ~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/issues/issue-3044.stderr b/src/test/ui/issues/issue-3044.stderr index 2b142f688ecb..1232b83c3917 100644 --- a/src/test/ui/issues/issue-3044.stderr +++ b/src/test/ui/issues/issue-3044.stderr @@ -13,7 +13,7 @@ help: provide the argument | LL ~ needlesArr.iter().fold(|x, y| { LL + -LL ~ }, /* value */); +LL ~ }, /* f */); | error: aborting due to previous error From ae68e1725d13c005674028f59f31187745b1329a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Sat, 17 Dec 2022 21:55:08 +0000 Subject: [PATCH 176/321] Re-enable ThinLTO for rustc on x86_64-apple-darwin dist builds This reverts commit ddb6fe2e1d270d23ec09c2cb435c22229830e97c. --- .github/workflows/ci.yml | 2 +- src/ci/github-actions/ci.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b29b3a418038..2b6e96b467e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -301,7 +301,7 @@ jobs: - name: dist-x86_64-apple env: SCRIPT: "./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin" - RUST_CONFIGURE_ARGS: "--enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false" + RUST_CONFIGURE_ARGS: "--enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin" RUSTC_RETRY_LINKER_ON_SEGFAULT: 1 MACOSX_DEPLOYMENT_TARGET: 10.7 SELECT_XCODE: /Applications/Xcode_13.4.1.app diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index 5a0397a3d123..d1ba46ad30de 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -467,7 +467,7 @@ jobs: - name: dist-x86_64-apple env: SCRIPT: ./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin - RUST_CONFIGURE_ARGS: --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false + RUST_CONFIGURE_ARGS: --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin RUSTC_RETRY_LINKER_ON_SEGFAULT: 1 MACOSX_DEPLOYMENT_TARGET: 10.7 SELECT_XCODE: /Applications/Xcode_13.4.1.app From c1181e12243f078b1cc562249569e6766d90f8f6 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 17 Dec 2022 22:32:57 +0000 Subject: [PATCH 177/321] Ensure param-env is const before calling eval_to_valtree --- .../rustc_const_eval/src/interpret/operand.rs | 6 ++-- src/test/ui/consts/issue-104396.rs | 36 +++++++++++++++++++ src/test/ui/consts/issue-104396.stderr | 11 ++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/consts/issue-104396.rs create mode 100644 src/test/ui/consts/issue-104396.stderr diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 221e359d24ab..5bc2e3dd4fc6 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -569,8 +569,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ty::ConstKind::Unevaluated(uv) => { let instance = self.resolve(uv.def, uv.substs)?; let cid = GlobalId { instance, promoted: None }; - self.ctfe_query(span, |tcx| tcx.eval_to_valtree(self.param_env.and(cid)))? - .unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}")) + self.ctfe_query(span, |tcx| { + tcx.eval_to_valtree(self.param_env.with_const().and(cid)) + })? + .unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}")) } ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => { span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {val:?}") diff --git a/src/test/ui/consts/issue-104396.rs b/src/test/ui/consts/issue-104396.rs new file mode 100644 index 000000000000..315b0cf0fd6b --- /dev/null +++ b/src/test/ui/consts/issue-104396.rs @@ -0,0 +1,36 @@ +// compile-flags: -Zmir-opt-level=3 +// check-pass + +#![feature(generic_const_exprs)] +//~^ WARN the feature `generic_const_exprs` is incomplete + +#[inline(always)] +fn from_fn_1 f32>(mut f: F) -> [f32; N] { + let mut result = [0.0; N]; + let mut i = 0; + while i < N { + result[i] = f(i); + i += 1; + } + result +} + +pub struct TestArray +where + [(); N / 2]:, +{ + array: [f32; N / 2], +} + +impl TestArray +where + [(); N / 2]:, +{ + fn from_fn_2 f32>(f: F) -> Self { + Self { array: from_fn_1(f) } + } +} + +fn main() { + TestArray::<4>::from_fn_2(|i| 0.0); +} diff --git a/src/test/ui/consts/issue-104396.stderr b/src/test/ui/consts/issue-104396.stderr new file mode 100644 index 000000000000..5856bee09a3f --- /dev/null +++ b/src/test/ui/consts/issue-104396.stderr @@ -0,0 +1,11 @@ +warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/issue-104396.rs:4:12 + | +LL | #![feature(generic_const_exprs)] + | ^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #76560 for more information + = note: `#[warn(incomplete_features)]` on by default + +warning: 1 warning emitted + From fca829075fd76385ac6aa7bff957e7eebbf89ae1 Mon Sep 17 00:00:00 2001 From: Benjamin Tong Date: Sun, 1 May 2022 00:50:00 +0800 Subject: [PATCH 178/321] Remove special cases for setup subcommand - Remove setup special-casing in Flags::parse --- src/bootstrap/builder.rs | 15 +++++++-- src/bootstrap/flags.rs | 6 ++-- src/bootstrap/lib.rs | 4 --- src/bootstrap/setup.rs | 72 +++++++++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 8ee6d49da8f0..8f701b7d0a17 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -19,6 +19,7 @@ use crate::flags::{Color, Subcommand}; use crate::install; use crate::native; use crate::run; +use crate::setup; use crate::test; use crate::tool::{self, SourceType}; use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir, output, t}; @@ -433,8 +434,11 @@ impl<'a> ShouldRun<'a> { // single alias, which does not correspond to any on-disk path pub fn alias(mut self, alias: &str) -> Self { + // exceptional case for `Kind::Setup` because its `library` + // and `compiler` options would otherwise naively match with + // `compiler` and `library` folders respectively. assert!( - !self.builder.src.join(alias).exists(), + self.kind == Kind::Setup || !self.builder.src.join(alias).exists(), "use `builder.path()` for real paths: {}", alias ); @@ -757,8 +761,9 @@ impl<'a> Builder<'a> { run::CollectLicenseMetadata, run::GenerateCopyright, ), + Kind::Setup => describe!(setup::Profile), // These commands either don't use paths, or they're special-cased in Build::build() - Kind::Clean | Kind::Format | Kind::Setup => vec![], + Kind::Clean | Kind::Format => vec![], } } @@ -821,7 +826,11 @@ impl<'a> Builder<'a> { Subcommand::Install { ref paths } => (Kind::Install, &paths[..]), Subcommand::Run { ref paths, .. } => (Kind::Run, &paths[..]), Subcommand::Format { .. } => (Kind::Format, &[][..]), - Subcommand::Clean { .. } | Subcommand::Setup { .. } => { + Subcommand::Setup { profile: ref path } => ( + Kind::Setup, + path.as_ref().map_or([].as_slice(), |path| std::slice::from_ref(path)), + ), + Subcommand::Clean { .. } => { panic!() } }; diff --git a/src/bootstrap/flags.rs b/src/bootstrap/flags.rs index 37a8eb884efb..851cb5ecf4c2 100644 --- a/src/bootstrap/flags.rs +++ b/src/bootstrap/flags.rs @@ -143,7 +143,7 @@ pub enum Subcommand { args: Vec, }, Setup { - profile: Option, + profile: Option, }, } @@ -351,7 +351,7 @@ To learn more about a subcommand, run `./x.py -h`", // fn usage() let usage = |exit_code: i32, opts: &Options, verbose: bool, subcommand_help: &str| -> ! { - let config = Config::parse(&["build".to_string()]); + let config = Config::parse(&["setup".to_string()]); let build = Build::new(config); let paths = Builder::get_help(&build, subcommand); @@ -621,7 +621,7 @@ Arguments: } Kind::Setup => { let profile = if paths.len() > 1 { - println!("\nat most one profile can be passed to setup\n"); + eprintln!("\nerror: At most one profile can be passed to setup\n"); usage(1, &opts, verbose, &subcommand_help) } else if let Some(path) = paths.pop() { let profile_string = t!(path.into_os_string().into_string().map_err( diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 570fe6484e3d..651e8dded68e 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -713,10 +713,6 @@ impl Build { return clean::clean(self, all); } - if let Subcommand::Setup { profile } = &self.config.cmd { - return setup::setup(&self.config, *profile); - } - // Download rustfmt early so that it can be used in rust-analyzer configs. let _ = &builder::Builder::new(&self).initial_rustfmt(); diff --git a/src/bootstrap/setup.rs b/src/bootstrap/setup.rs index c7f98a7d0d14..57426ce3d510 100644 --- a/src/bootstrap/setup.rs +++ b/src/bootstrap/setup.rs @@ -1,3 +1,4 @@ +use crate::builder::{Builder, RunConfig, ShouldRun, Step}; use crate::Config; use crate::{t, VERSION}; use std::env::consts::EXE_SUFFIX; @@ -9,7 +10,7 @@ use std::process::Command; use std::str::FromStr; use std::{fmt, fs, io}; -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] pub enum Profile { Compiler, Codegen, @@ -48,6 +49,16 @@ impl Profile { } out } + + pub fn as_str(&self) -> &'static str { + match self { + Profile::Compiler => "compiler", + Profile::Codegen => "codegen", + Profile::Library => "library", + Profile::Tools => "tools", + Profile::User => "user", + } + } } impl FromStr for Profile { @@ -69,24 +80,58 @@ impl FromStr for Profile { impl fmt::Display for Profile { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Profile::Compiler => write!(f, "compiler"), - Profile::Codegen => write!(f, "codegen"), - Profile::Library => write!(f, "library"), - Profile::User => write!(f, "user"), - Profile::Tools => write!(f, "tools"), - } + f.write_str(self.as_str()) } } -pub fn setup(config: &Config, profile: Option) { - let profile = profile.unwrap_or_else(|| t!(interactive_path())); +impl Step for Profile { + type Output = (); + const DEFAULT: bool = true; + + fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> { + for choice in Profile::all() { + run = run.alias(choice.as_str()); + } + run + } + + fn make_run(run: RunConfig<'_>) { + // for Profile, `run.paths` will have 1 and only 1 element + // this is because we only accept at most 1 path from user input. + // If user calls `x.py setup` without arguments, the interactive TUI + // will guide user to provide one. + let profile = if run.paths.len() > 1 { + // HACK: `builder` runs this step with all paths if no path was passed. + t!(interactive_path()) + } else { + run.paths + .first() + .unwrap() + .assert_single_path() + .path + .as_path() + .as_os_str() + .to_str() + .unwrap() + .parse() + .unwrap() + }; + + run.builder.ensure(profile); + } + + fn run(self, builder: &Builder<'_>) { + setup(&builder.build.config, self) + } +} + +pub fn setup(config: &Config, profile: Profile) { let stage_path = ["build", config.build.rustc_target_arg(), "stage1"].join(&MAIN_SEPARATOR.to_string()); if !rustup_installed() && profile != Profile::User { eprintln!("`rustup` is not installed; cannot link `stage1` toolchain"); - } else if stage_dir_exists(&stage_path[..]) { + } else if stage_dir_exists(&stage_path[..]) && !config.dry_run() { attempt_toolchain_link(&stage_path[..]); } @@ -104,7 +149,9 @@ pub fn setup(config: &Config, profile: Option) { Profile::User => &["dist", "build"], }; - t!(install_git_hook_maybe(&config)); + if !config.dry_run() { + t!(install_git_hook_maybe(&config)); + } println!(); @@ -144,6 +191,7 @@ fn setup_config_toml(path: &PathBuf, profile: Profile, config: &Config) { changelog-seen = {}\n", profile, VERSION ); + t!(fs::write(path, settings)); let include_path = profile.include_path(&config.src); From c754d057a67bb8779789ad48708632fcf87361b1 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 17 Dec 2022 17:18:13 -0600 Subject: [PATCH 179/321] Make the pre-push script work on directories with spaces As a secondary benefit, it's also a lot simpler. --- src/etc/pre-push.sh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/etc/pre-push.sh b/src/etc/pre-push.sh index be7de3ebaf57..2a3086338b4b 100755 --- a/src/etc/pre-push.sh +++ b/src/etc/pre-push.sh @@ -10,16 +10,8 @@ set -Eeuo pipefail # https://github.com/rust-lang/rust/issues/77620#issuecomment-705144570 unset GIT_DIR ROOT_DIR="$(git rev-parse --show-toplevel)" -COMMAND="$ROOT_DIR/x.py test tidy" -if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then - COMMAND="python $COMMAND" -elif ! command -v python &> /dev/null; then - COMMAND="python3 $COMMAND" -fi - -echo "Running pre-push script '$COMMAND'" +echo "Running pre-push script $ROOT_DIR/x test tidy" cd "$ROOT_DIR" - -$COMMAND +./x test tidy From 1ade25491e8849d521fdb655c40102b1ffa15ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 18 Dec 2022 00:29:25 +0100 Subject: [PATCH 180/321] remove redundant clone --- compiler/rustc_builtin_macros/src/global_allocator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs index 41531580c198..f8761653bf5b 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -35,7 +35,7 @@ pub fn expand( (item, true, ecx.with_def_site_ctxt(ty.span)) } else { ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics"); - return vec![orig_item.clone()] + return vec![orig_item]; }; // Generate a bunch of new items using the AllocFnFactory From a37d42133cddf1f82bf8c3d29b48b6ec694f6f8d Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 17 Dec 2022 18:41:14 -0800 Subject: [PATCH 181/321] Another `as_chunks` example I really liked this structure that dtolney brought up in #105316, so wanted to put it in the docs to help others use it. --- library/core/src/slice/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 8bd2ed45c0a2..2c469f61854f 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1002,6 +1002,17 @@ impl [T] { /// assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]); /// assert_eq!(remainder, &['m']); /// ``` + /// + /// If you expect the slice to be an exact multiple, you can combine + /// `let`-`else` with an empty slice pattern: + /// ``` + /// #![feature(slice_as_chunks)] + /// let slice = ['R', 'u', 's', 't']; + /// let (chunks, []) = slice.as_chunks::<2>() else { + /// panic!("slice didn't have even length") + /// }; + /// assert_eq!(chunks, &[['R', 'u'], ['s', 't']]); + /// ``` #[unstable(feature = "slice_as_chunks", issue = "74985")] #[inline] #[must_use] From fec9e9ecf1a34aa87bcae28c158a590f75e16a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 18 Dec 2022 14:25:55 +0100 Subject: [PATCH 182/321] don't clone Copy types --- compiler/rustc_borrowck/src/constraints/graph.rs | 2 +- compiler/rustc_borrowck/src/type_check/mod.rs | 2 +- compiler/rustc_hir_typeck/src/coercion.rs | 6 +++--- compiler/rustc_hir_typeck/src/demand.rs | 2 +- compiler/rustc_interface/src/queries.rs | 2 +- compiler/rustc_lint/src/builtin.rs | 4 ++-- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- compiler/rustc_query_impl/src/on_disk_cache.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 2 +- .../rustc_trait_selection/src/traits/error_reporting/mod.rs | 4 ++-- compiler/rustc_transmute/src/layout/nfa.rs | 4 ++-- 11 files changed, 16 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_borrowck/src/constraints/graph.rs b/compiler/rustc_borrowck/src/constraints/graph.rs index 385f153174c3..c780d047992c 100644 --- a/compiler/rustc_borrowck/src/constraints/graph.rs +++ b/compiler/rustc_borrowck/src/constraints/graph.rs @@ -148,7 +148,7 @@ impl<'s, 'tcx, D: ConstraintGraphDirecton> Iterator for Edges<'s, 'tcx, D> { if let Some(p) = self.pointer { self.pointer = self.graph.next_constraints[p]; - Some(self.constraints[p].clone()) + Some(self.constraints[p]) } else if let Some(next_static_idx) = self.next_static_idx { self.next_static_idx = if next_static_idx == (self.graph.first_constraints.len() - 1) { None diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 814bc275019c..8d4a720f8ce9 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -612,7 +612,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> { let locations = location.to_locations(); for constraint in constraints.outlives().iter() { - let mut constraint = constraint.clone(); + let mut constraint = *constraint; constraint.locations = locations; if let ConstraintCategory::Return(_) | ConstraintCategory::UseAsConst diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 36cf4791492d..8cdd12e4e347 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1548,7 +1548,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { cause, expected, found, - coercion_error.clone(), + coercion_error, fcx, parent_id, expression, @@ -1567,7 +1567,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { cause, expected, found, - coercion_error.clone(), + coercion_error, fcx, id, expression, @@ -1583,7 +1583,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { cause, expected, found, - coercion_error.clone(), + coercion_error, ); } } diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 6763e06c0cfe..4ac24aca25a5 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -163,7 +163,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let expr = expr.peel_drop_temps(); let cause = self.misc(expr.span); let expr_ty = self.resolve_vars_with_obligations(checked_ty); - let mut err = self.err_ctxt().report_mismatched_types(&cause, expected, expr_ty, e.clone()); + let mut err = self.err_ctxt().report_mismatched_types(&cause, expected, expr_ty, e); let is_insufficiently_polymorphic = matches!(e, TypeError::RegionsInsufficientlyPolymorphic(..)); diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 39e1f2204b00..1d0c7f5b7a38 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -127,7 +127,7 @@ impl<'tcx> Queries<'tcx> { pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, Lrc)>> { self.register_plugins.compute(|| { - let crate_name = self.crate_name()?.peek().clone(); + let crate_name = *self.crate_name()?.peek(); let krate = self.parse()?.take(); let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {}; diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 43862570e809..cd19e65b6fc3 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -270,7 +270,7 @@ impl<'tcx> LateLintPass<'tcx> for NonShorthandFieldPatterns { |lint| { let suggested_ident = format!("{}{}", binding_annot.prefix_str(), ident); - lint.set_arg("ident", ident.clone()).span_suggestion( + lint.set_arg("ident", ident).span_suggestion( fieldpat.span, fluent::suggestion, suggested_ident, @@ -2052,7 +2052,7 @@ impl KeywordIdents { ident.span, fluent::lint_builtin_keyword_idents, |lint| { - lint.set_arg("kw", ident.clone()).set_arg("next", next_edition).span_suggestion( + lint.set_arg("kw", ident).set_arg("next", next_edition).span_suggestion( ident.span, fluent::suggestion, format!("r#{}", ident), diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 856f5bc4645f..219753dbd16b 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -332,7 +332,7 @@ impl<'a, 'tcx> Encodable> for Symbol { s.emit_str(self.as_str()); } Entry::Occupied(o) => { - let x = o.get().clone(); + let x = *o.get(); s.emit_u8(SYMBOL_OFFSET); s.emit_usize(x); } diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index ac9653b90071..c4e605c18521 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -965,7 +965,7 @@ impl<'a, 'tcx> Encodable> for Symbol { s.emit_str(self.as_str()); } Entry::Occupied(o) => { - let x = o.get().clone(); + let x = *o.get(); s.emit_u8(SYMBOL_OFFSET); s.emit_usize(x); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 24e4b5bdd3f5..4861ee746aae 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1491,7 +1491,7 @@ impl<'a> Resolver<'a> { label_res_map: self.label_res_map.clone(), lifetimes_res_map: self.lifetimes_res_map.clone(), extra_lifetime_params_map: self.extra_lifetime_params_map.clone(), - next_node_id: self.next_node_id.clone(), + next_node_id: self.next_node_id, node_id_to_def_id: self.node_id_to_def_id.clone(), def_id_to_node_id: self.def_id_to_node_id.clone(), trait_map: self.trait_map.clone(), diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 2dd2c568bab9..929d813bfc35 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1574,7 +1574,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { &error.obligation.cause, expected_found.expected, expected_found.found, - err.clone(), + *err, ) .emit(); } @@ -1583,7 +1583,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { &error.obligation.cause, expected_found.expected, expected_found.found, - err.clone(), + *err, ); let code = error.obligation.cause.code().peel_derives().peel_match_impls(); if let ObligationCauseCode::BindingObligation(..) diff --git a/compiler/rustc_transmute/src/layout/nfa.rs b/compiler/rustc_transmute/src/layout/nfa.rs index c2bc47bc043a..78fcceb5f2cb 100644 --- a/compiler/rustc_transmute/src/layout/nfa.rs +++ b/compiler/rustc_transmute/src/layout/nfa.rs @@ -123,7 +123,7 @@ where let fix_state = |state| if state == other.start { self.accepting } else { state }; let entry = transitions.entry(fix_state(source)).or_default(); for (edge, destinations) in transition { - let entry = entry.entry(edge.clone()).or_default(); + let entry = entry.entry(edge).or_default(); for destination in destinations { entry.insert(fix_state(destination)); } @@ -147,7 +147,7 @@ where } let entry = transitions.entry(source).or_default(); for (edge, destinations) in transition { - let entry = entry.entry(edge.clone()).or_default(); + let entry = entry.entry(*edge).or_default(); for &(mut destination) in destinations { // if dest is accepting state of `other`, replace with accepting state of `self` if destination == other.accepting { From 6e52a0f42182b6f628329b32db951beb57bcb2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 18 Dec 2022 13:51:40 +0100 Subject: [PATCH 183/321] remove redundant fn params that were only "used" in recursion --- .../rustc_borrowck/src/region_infer/mod.rs | 29 ++----------------- .../rustc_hir_analysis/src/check/check.rs | 7 ++--- compiler/rustc_hir_typeck/src/pat.rs | 11 ++----- .../src/traits/auto_trait.rs | 7 +---- 4 files changed, 10 insertions(+), 44 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index e9c98bdc5149..0d03346ef0a5 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -831,7 +831,6 @@ impl<'tcx> RegionInferenceContext<'tcx> { if self.eval_verify_bound( infcx, param_env, - body, generic_ty, type_test.lower_bound, &type_test.verify_bound, @@ -962,14 +961,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { // where `ur` is a local bound -- we are sometimes in a // position to prove things that our caller cannot. See // #53570 for an example. - if self.eval_verify_bound( - infcx, - param_env, - body, - generic_ty, - ur, - &type_test.verify_bound, - ) { + if self.eval_verify_bound(infcx, param_env, generic_ty, ur, &type_test.verify_bound) { continue; } @@ -1190,7 +1182,6 @@ impl<'tcx> RegionInferenceContext<'tcx> { &self, infcx: &InferCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, - body: &Body<'tcx>, generic_ty: Ty<'tcx>, lower_bound: RegionVid, verify_bound: &VerifyBound<'tcx>, @@ -1213,25 +1204,11 @@ impl<'tcx> RegionInferenceContext<'tcx> { } VerifyBound::AnyBound(verify_bounds) => verify_bounds.iter().any(|verify_bound| { - self.eval_verify_bound( - infcx, - param_env, - body, - generic_ty, - lower_bound, - verify_bound, - ) + self.eval_verify_bound(infcx, param_env, generic_ty, lower_bound, verify_bound) }), VerifyBound::AllBounds(verify_bounds) => verify_bounds.iter().all(|verify_bound| { - self.eval_verify_bound( - infcx, - param_env, - body, - generic_ty, - lower_bound, - verify_bound, - ) + self.eval_verify_bound(infcx, param_env, generic_ty, lower_bound, verify_bound) }), } } diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index aa01feb3a1ea..eee0ba2e5ed2 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -99,18 +99,17 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, - span: Span, ) -> bool { // We don't just accept all !needs_drop fields, due to semver concerns. match ty.kind() { ty::Ref(..) => true, // references never drop (even mutable refs, which are non-Copy and hence fail the later check) ty::Tuple(tys) => { // allow tuples of allowed types - tys.iter().all(|ty| allowed_union_field(ty, tcx, param_env, span)) + tys.iter().all(|ty| allowed_union_field(ty, tcx, param_env)) } ty::Array(elem, _len) => { // Like `Copy`, we do *not* special-case length 0. - allowed_union_field(*elem, tcx, param_env, span) + allowed_union_field(*elem, tcx, param_env) } _ => { // Fallback case: allow `ManuallyDrop` and things that are `Copy`. @@ -124,7 +123,7 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b for field in &def.non_enum_variant().fields { let field_ty = field.ty(tcx, substs); - if !allowed_union_field(field_ty, tcx, param_env, span) { + if !allowed_union_field(field_ty, tcx, param_env) { let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) { // We are currently checking the type this field came from, so it must be local. Some(Node::Field(field)) => (field.span, field.ty.span), diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 6810353f9e77..d3e88b1b80ae 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -2130,7 +2130,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) { let ty = self.resolve_vars_if_possible(ti.expected); - let is_slice_or_array_or_vector = self.is_slice_or_array_or_vector(&mut err, snippet.clone(), ty); + let is_slice_or_array_or_vector = self.is_slice_or_array_or_vector(ty); match is_slice_or_array_or_vector.1.kind() { ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Option, adt_def.did()) @@ -2159,17 +2159,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err.emit(); } - fn is_slice_or_array_or_vector( - &self, - err: &mut Diagnostic, - snippet: String, - ty: Ty<'tcx>, - ) -> (bool, Ty<'tcx>) { + fn is_slice_or_array_or_vector(&self, ty: Ty<'tcx>) -> (bool, Ty<'tcx>) { match ty.kind() { ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did()) => { (true, ty) } - ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(err, snippet, *ty), + ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(*ty), ty::Slice(..) | ty::Array(..) => (true, ty), _ => (false, ty), } diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index aef2f8ff9911..948632ccc6c4 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -159,13 +159,12 @@ impl<'tcx> AutoTraitFinder<'tcx> { orig_env, orig_env, &mut fresh_preds, - false, ) else { return AutoTraitResult::NegativeImpl; }; let (full_env, full_user_env) = self - .evaluate_predicates(&infcx, trait_did, ty, new_env, user_env, &mut fresh_preds, true) + .evaluate_predicates(&infcx, trait_did, ty, new_env, user_env, &mut fresh_preds) .unwrap_or_else(|| { panic!("Failed to fully process: {:?} {:?} {:?}", ty, trait_did, orig_env) }); @@ -247,7 +246,6 @@ impl<'tcx> AutoTraitFinder<'tcx> { param_env: ty::ParamEnv<'tcx>, user_env: ty::ParamEnv<'tcx>, fresh_preds: &mut FxHashSet>, - only_projections: bool, ) -> Option<(ty::ParamEnv<'tcx>, ty::ParamEnv<'tcx>)> { let tcx = infcx.tcx; @@ -322,7 +320,6 @@ impl<'tcx> AutoTraitFinder<'tcx> { fresh_preds, &mut predicates, &mut select, - only_projections, ) { return None; } @@ -600,7 +597,6 @@ impl<'tcx> AutoTraitFinder<'tcx> { fresh_preds: &mut FxHashSet>, predicates: &mut VecDeque>, selcx: &mut SelectionContext<'_, 'tcx>, - only_projections: bool, ) -> bool { let dummy_cause = ObligationCause::dummy(); @@ -744,7 +740,6 @@ impl<'tcx> AutoTraitFinder<'tcx> { fresh_preds, predicates, selcx, - only_projections, ) { return false; } From 3af7df91fcbe4c5197e55dc78838aeb5aa978d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 18 Dec 2022 16:17:46 +0100 Subject: [PATCH 184/321] use &str / String literals instead of format!() --- .../src/diagnostics/conflict_errors.rs | 2 +- .../src/diagnostics/explain_borrow.rs | 2 +- .../src/diagnostics/region_errors.rs | 2 +- compiler/rustc_codegen_llvm/src/asm.rs | 2 +- compiler/rustc_hir_analysis/src/astconv/mod.rs | 2 +- compiler/rustc_hir_typeck/src/demand.rs | 2 +- compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs | 2 +- .../rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 6 +----- compiler/rustc_hir_typeck/src/method/suggest.rs | 2 +- .../rustc_infer/src/infer/error_reporting/mod.rs | 4 ++-- .../nice_region_error/placeholder_relation.rs | 14 ++++---------- .../rustc_lint/src/for_loops_over_fallibles.rs | 6 +++--- .../rustc_macros/src/diagnostics/diagnostic.rs | 12 ++++-------- compiler/rustc_middle/src/mir/pretty.rs | 8 ++++---- .../src/build/custom/parse/instruction.rs | 2 +- compiler/rustc_resolve/src/late/diagnostics.rs | 8 ++------ .../src/traits/const_evaluatable.rs | 8 ++++---- .../src/traits/error_reporting/mod.rs | 4 ++-- .../src/traits/error_reporting/suggestions.rs | 4 ++-- 19 files changed, 37 insertions(+), 55 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 72c0257756ef..8d5c5a7124f8 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -649,7 +649,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { if !assign_value.is_empty() { err.span_suggestion_verbose( sugg_span.shrink_to_hi(), - format!("consider assigning a value"), + "consider assigning a value", format!(" = {}", assign_value), Applicability::MaybeIncorrect, ); diff --git a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs index 304683618d83..00f5e8a83972 100644 --- a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs +++ b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs @@ -270,7 +270,7 @@ impl<'tcx> BorrowExplanation<'tcx> { for extra in extra_info { match extra { ExtraConstraintInfo::PlaceholderFromPredicate(span) => { - err.span_note(*span, format!("due to current limitations in the borrow checker, this implies a `'static` lifetime")); + err.span_note(*span, "due to current limitations in the borrow checker, this implies a `'static` lifetime"); } } } diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index f8ec5e5e7991..b5a0044e9e8c 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -472,7 +472,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { for extra in extra_info { match extra { ExtraConstraintInfo::PlaceholderFromPredicate(span) => { - diag.span_note(span, format!("due to current limitations in the borrow checker, this implies a `'static` lifetime")); + diag.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime"); } } } diff --git a/compiler/rustc_codegen_llvm/src/asm.rs b/compiler/rustc_codegen_llvm/src/asm.rs index 219a4f8fa895..606f710641fc 100644 --- a/compiler/rustc_codegen_llvm/src/asm.rs +++ b/compiler/rustc_codegen_llvm/src/asm.rs @@ -144,7 +144,7 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { // We prefer the latter because it matches the behavior of // Clang. if late && matches!(reg, InlineAsmRegOrRegClass::Reg(_)) { - constraints.push(format!("{}", reg_to_llvm(reg, Some(&in_value.layout)))); + constraints.push(reg_to_llvm(reg, Some(&in_value.layout)).to_string()); } else { constraints.push(format!("{}", op_idx[&idx])); } diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 71f26eb60c96..e4c16ef9efa1 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -2240,7 +2240,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { ), "s", ), - [only] => (format!("{only}"), ""), + [only] => (only.to_string(), ""), [] => unreachable!(), }; let last_span = *arg_spans.last().unwrap(); diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 6763e06c0cfe..f02db43f73af 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -406,7 +406,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } let note_about_variant_field_privacy = (field_is_local && !field_is_accessible) - .then(|| format!(" (its field is private, but it's local to this crate and its privacy can be changed)")); + .then(|| " (its field is private, but it's local to this crate and its privacy can be changed)".to_string()); let sole_field_ty = sole_field.ty(self.tcx, substs); if self.can_coerce(expr_ty, sole_field_ty) { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 8e520e563ff6..240044fe92d2 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -1013,7 +1013,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { args_span }; - labels.push((span, format!("multiple arguments are missing"))); + labels.push((span, "multiple arguments are missing".to_string())); suggestion_text = match suggestion_text { SuggestionText::None | SuggestionText::Provide(_) => { SuggestionText::Provide(true) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 407d6ac8544c..59c9d219d33d 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -319,11 +319,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - err.multipart_suggestion_verbose( - format!("use parentheses to call these"), - sugg, - applicability, - ); + err.multipart_suggestion_verbose("use parentheses to call these", sugg, applicability); true } else { diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index d40a66715a2e..7c5a9a333feb 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1007,7 +1007,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if def_kind == DefKind::AssocFn && lev_candidate.fn_has_self_parameter { err.span_suggestion( span, - &format!("there is a method with a similar name",), + "there is a method with a similar name", lev_candidate.name, Applicability::MaybeIncorrect, ); diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 397fa43175f7..269fc95420ad 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -184,7 +184,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>( let text = if br.has_name() { format!("the lifetime `{}` as defined here", br.name) } else { - format!("the anonymous lifetime as defined here") + "the anonymous lifetime as defined here".to_string() }; (text, sp) } @@ -203,7 +203,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>( sp = param.span; } let text = if name == kw::UnderscoreLifetime { - format!("the anonymous lifetime as defined here") + "the anonymous lifetime as defined here".to_string() } else { format!("the lifetime `{}` as defined here", name) }; diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_relation.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_relation.rs index c42240f21724..9534bce54ef0 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_relation.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_relation.rs @@ -44,7 +44,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> { ); } (Some(sub_span), Some(sup_span), _, Some(sup_symbol)) => { - err.span_note(sub_span, format!("the lifetime defined here...")); + err.span_note(sub_span, "the lifetime defined here..."); err.span_note( sup_span, format!("...must outlive the lifetime `{sup_symbol}` defined here"), @@ -55,17 +55,11 @@ impl<'tcx> NiceRegionError<'_, 'tcx> { sub_span, format!("the lifetime `{sub_symbol}` defined here..."), ); - err.span_note( - sup_span, - format!("...must outlive the lifetime defined here"), - ); + err.span_note(sup_span, "...must outlive the lifetime defined here"); } (Some(sub_span), Some(sup_span), _, _) => { - err.span_note(sub_span, format!("the lifetime defined here...")); - err.span_note( - sup_span, - format!("...must outlive the lifetime defined here"), - ); + err.span_note(sub_span, "the lifetime defined here..."); + err.span_note(sup_span, "...must outlive the lifetime defined here"); } _ => {} } diff --git a/compiler/rustc_lint/src/for_loops_over_fallibles.rs b/compiler/rustc_lint/src/for_loops_over_fallibles.rs index 4187850153cc..182734fa9fc8 100644 --- a/compiler/rustc_lint/src/for_loops_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loops_over_fallibles.rs @@ -71,11 +71,11 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles { ); } else { lint.multipart_suggestion_verbose( - format!("to check pattern in a loop use `while let`"), + "to check pattern in a loop use `while let`", vec![ // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts (expr.span.with_hi(pat.span.lo()), format!("while let {var}(")), - (pat.span.between(arg.span), format!(") = ")), + (pat.span.between(arg.span), ") = ".to_string()), ], Applicability::MaybeIncorrect ); @@ -95,7 +95,7 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles { vec![ // NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts (expr.span.with_hi(pat.span.lo()), format!("if let {var}(")), - (pat.span.between(arg.span), format!(") = ")), + (pat.span.between(arg.span), ") = ".to_string()), ], Applicability::MaybeIncorrect, ) diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic.rs b/compiler/rustc_macros/src/diagnostics/diagnostic.rs index 13f06fe74734..9ff944864047 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic.rs @@ -39,10 +39,8 @@ impl<'a> DiagnosticDerive<'a> { let init = match builder.slug.value_ref() { None => { span_err(builder.span, "diagnostic slug not specified") - .help(format!( - "specify the slug as the first argument to the `#[diag(...)]` \ - attribute, such as `#[diag(hir_analysis_example_error)]`", - )) + .help("specify the slug as the first argument to the `#[diag(...)]` \ + attribute, such as `#[diag(hir_analysis_example_error)]`") .emit(); return DiagnosticDeriveError::ErrorHandled.to_compile_error(); } @@ -133,10 +131,8 @@ impl<'a> LintDiagnosticDerive<'a> { match builder.slug.value_ref() { None => { span_err(builder.span, "diagnostic slug not specified") - .help(format!( - "specify the slug as the first argument to the attribute, such as \ - `#[diag(compiletest_example)]`", - )) + .help("specify the slug as the first argument to the attribute, such as \ + `#[diag(compiletest_example)]`") .emit(); DiagnosticDeriveError::ErrorHandled.to_compile_error() } diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 2a4ff4b8810d..1ebfdbbd6ef0 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -448,15 +448,15 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> { // FIXME: this is a poor version of `pretty_print_const_value`. let fmt_val = |val: &ConstValue<'tcx>| match val { - ConstValue::ZeroSized => format!(""), + ConstValue::ZeroSized => "".to_string(), ConstValue::Scalar(s) => format!("Scalar({:?})", s), - ConstValue::Slice { .. } => format!("Slice(..)"), - ConstValue::ByRef { .. } => format!("ByRef(..)"), + ConstValue::Slice { .. } => "Slice(..)".to_string(), + ConstValue::ByRef { .. } => "ByRef(..)".to_string(), }; let fmt_valtree = |valtree: &ty::ValTree<'tcx>| match valtree { ty::ValTree::Leaf(leaf) => format!("ValTree::Leaf({:?})", leaf), - ty::ValTree::Branch(_) => format!("ValTree::Branch(..)"), + ty::ValTree::Branch(_) => "ValTree::Branch(..)".to_string(), }; let val = match literal { diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs index ecc3e4de8d1d..7c39a93a8eb9 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs @@ -76,7 +76,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { let Some((otherwise, rest)) = arms.split_last() else { return Err(ParseError { span, - item_description: format!("no arms"), + item_description: "no arms".to_string(), expected: "at least one arm".to_string(), }) }; diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 49bbe37ee432..600308b6508d 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -277,11 +277,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { let override_suggestion = if ["true", "false"].contains(&item_str.to_string().to_lowercase().as_str()) { let item_typo = item_str.to_string().to_lowercase(); - Some(( - item_span, - "you may want to use a bool value instead", - format!("{}", item_typo), - )) + Some((item_span, "you may want to use a bool value instead", item_typo)) // FIXME(vincenzopalazzo): make the check smarter, // and maybe expand with levenshtein distance checks } else if item_str.as_str() == "printf" { @@ -2324,7 +2320,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { let message = format!("consider introducing lifetime `{}` here", name); should_continue = suggest(err, false, span, &message, sugg); } else { - let message = format!("consider introducing a named lifetime parameter"); + let message = "consider introducing a named lifetime parameter"; should_continue = suggest(err, false, span, &message, sugg); } } diff --git a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs index 7c9fde27420b..f8efe9bfa9f8 100644 --- a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs +++ b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs @@ -138,10 +138,10 @@ pub fn is_const_evaluatable<'tcx>( } else if uv.has_non_region_param() { NotConstEvaluatable::MentionsParam } else { - let guar = infcx.tcx.sess.delay_span_bug( - span, - format!("Missing value for constant, but no error reported?"), - ); + let guar = infcx + .tcx + .sess + .delay_span_bug(span, "Missing value for constant, but no error reported?"); NotConstEvaluatable::Error(guar) }; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 2dd2c568bab9..11eac0528134 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2332,9 +2332,9 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { // get rid of :: between Trait and // must be '::' between them, otherwise the parser won't accept the code suggestions.push((between_span, "".to_string(),)); - suggestions.push((generic_arg.span_ext.shrink_to_hi(), format!(">"))); + suggestions.push((generic_arg.span_ext.shrink_to_hi(), ">".to_string())); } else { - suggestions.push((trait_path_segment.ident.span.shrink_to_hi(), format!(">"))); + suggestions.push((trait_path_segment.ident.span.shrink_to_hi(), ">".to_string())); } err.multipart_suggestion( message, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index d47a5ea3e370..5a4c87c56d5d 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2740,7 +2740,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } ty::Closure(def_id, _) => err.span_note( self.tcx.def_span(def_id), - &format!("required because it's used within this closure"), + "required because it's used within this closure", ), _ => err.note(&msg), }; @@ -3386,7 +3386,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } err.span_note( multi_span, - format!("the method call chain might not have had the expected associated types"), + "the method call chain might not have had the expected associated types", ); } } From 0aa4cde747bccb1d1277f9203cfe2021feec341f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 18 Dec 2022 15:01:26 +0100 Subject: [PATCH 185/321] avoid .into() conversion to identical types --- compiler/rustc_builtin_macros/src/test.rs | 3 +-- compiler/rustc_const_eval/src/interpret/operand.rs | 11 ++++------- compiler/rustc_const_eval/src/interpret/operator.rs | 2 +- compiler/rustc_const_eval/src/interpret/place.rs | 2 +- compiler/rustc_infer/src/infer/mod.rs | 2 +- compiler/rustc_middle/src/ty/vtable.rs | 4 ++-- compiler/rustc_mir_build/src/build/expr/as_place.rs | 2 +- compiler/rustc_mir_transform/src/const_prop.rs | 4 ++-- compiler/rustc_parse/src/parser/mod.rs | 8 ++------ compiler/rustc_passes/src/lang_items.rs | 2 -- .../src/traits/error_reporting/mod.rs | 4 ++-- 11 files changed, 17 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index f5f02fc772ab..729ae4071e20 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -239,8 +239,7 @@ pub fn expand_test_or_bench( cx.attr_nested_word(sym::cfg, sym::test, attr_sp), // #[rustc_test_marker = "test_case_sort_key"] cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp), - ] - .into(), + ], // const $ident: test::TestDescAndFn = ast::ItemKind::Const( ast::Defaultness::Final, diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 221e359d24ab..f9e3a2bdc06f 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -39,7 +39,7 @@ pub enum Immediate { impl From> for Immediate { #[inline(always)] fn from(val: Scalar) -> Self { - Immediate::Scalar(val.into()) + Immediate::Scalar(val) } } @@ -53,7 +53,7 @@ impl Immediate { } pub fn new_slice(val: Scalar, len: u64, cx: &impl HasDataLayout) -> Self { - Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into()) + Immediate::ScalarPair(val, Scalar::from_machine_usize(len, cx)) } pub fn new_dyn_trait( @@ -61,7 +61,7 @@ impl Immediate { vtable: Pointer>, cx: &impl HasDataLayout, ) -> Self { - Immediate::ScalarPair(val.into(), Scalar::from_maybe_pointer(vtable, cx)) + Immediate::ScalarPair(val, Scalar::from_maybe_pointer(vtable, cx)) } #[inline] @@ -341,10 +341,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { alloc_range(b_offset, b_size), /*read_provenance*/ b.is_ptr(), )?; - Some(ImmTy { - imm: Immediate::ScalarPair(a_val.into(), b_val.into()), - layout: mplace.layout, - }) + Some(ImmTy { imm: Immediate::ScalarPair(a_val, b_val), layout: mplace.layout }) } _ => { // Neither a scalar nor scalar pair. diff --git a/compiler/rustc_const_eval/src/interpret/operator.rs b/compiler/rustc_const_eval/src/interpret/operator.rs index 949f95c5fa81..e8ff70e3a409 100644 --- a/compiler/rustc_const_eval/src/interpret/operator.rs +++ b/compiler/rustc_const_eval/src/interpret/operator.rs @@ -36,7 +36,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { if let Abi::ScalarPair(..) = dest.layout.abi { // We can use the optimized path and avoid `place_field` (which might do // `force_allocation`). - let pair = Immediate::ScalarPair(val.into(), Scalar::from_bool(overflowed).into()); + let pair = Immediate::ScalarPair(val, Scalar::from_bool(overflowed)); self.write_immediate(pair, dest)?; } else { assert!(self.tcx.sess.opts.unstable_opts.randomize_layout); diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index 905eb71bb18e..97a73e98abcb 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -141,7 +141,7 @@ impl MemPlace { match self.meta { MemPlaceMeta::None => Immediate::from(Scalar::from_maybe_pointer(self.ptr, cx)), MemPlaceMeta::Meta(meta) => { - Immediate::ScalarPair(Scalar::from_maybe_pointer(self.ptr, cx).into(), meta.into()) + Immediate::ScalarPair(Scalar::from_maybe_pointer(self.ptr, cx), meta) } } } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 996148a70908..268b3bf1dcd4 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1920,7 +1920,7 @@ impl<'tcx> TypeTrace<'tcx> { ) -> TypeTrace<'tcx> { TypeTrace { cause: cause.clone(), - values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a.into(), b.into())), + values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a, b)), } } diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index 802925dfb043..f77bd9f0c6ff 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -88,8 +88,8 @@ pub(super) fn vtable_allocation_provider<'tcx>( let fn_ptr = Pointer::from(fn_alloc_id); Scalar::from_pointer(fn_ptr, &tcx) } - VtblEntry::MetadataSize => Scalar::from_uint(size, ptr_size).into(), - VtblEntry::MetadataAlign => Scalar::from_uint(align, ptr_size).into(), + VtblEntry::MetadataSize => Scalar::from_uint(size, ptr_size), + VtblEntry::MetadataAlign => Scalar::from_uint(align, ptr_size), VtblEntry::Vacant => continue, VtblEntry::Method(instance) => { // Prepare the fn ptr we write into the vtable. diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 8a35478dd8b3..23a4f85386b8 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -356,7 +356,7 @@ impl<'tcx> PlaceBuilder<'tcx> { match self { PlaceBuilder::Local { local, projection } => PlaceBuilder::Local { local: *local, - projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])), + projection: Vec::from_iter(projection.iter().copied().chain([elem])), }, PlaceBuilder::Upvar { upvar, projection } => PlaceBuilder::Upvar { upvar: *upvar, diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 044b7ce65bd7..e384cfe16599 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -701,8 +701,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { BinOp::Mul if const_arg.layout.ty.is_integral() && arg_value == 0 => { if let Rvalue::CheckedBinaryOp(_, _) = rvalue { let val = Immediate::ScalarPair( - const_arg.to_scalar().into(), - Scalar::from_bool(false).into(), + const_arg.to_scalar(), + Scalar::from_bool(false), ); this.ecx.write_immediate(val, &dest) } else { diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index bebb012660a1..40b88788caa6 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -25,7 +25,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::util::case::Case; use rustc_ast::AttrId; use rustc_ast::DUMMY_NODE_ID; -use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, DelimArgs, Extern}; +use rustc_ast::{self as ast, AnonConst, AttrStyle, Const, DelimArgs, Extern}; use rustc_ast::{Async, AttrArgs, AttrArgsEq, Expr, ExprKind, MacDelimiter, Mutability, StrLit}; use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind}; use rustc_ast_pretty::pprust; @@ -1217,11 +1217,7 @@ impl<'a> Parser<'a> { value: self.mk_expr(blk.span, ExprKind::Block(blk, None)), }; let blk_span = anon_const.value.span; - Ok(self.mk_expr_with_attrs( - span.to(blk_span), - ExprKind::ConstBlock(anon_const), - AttrVec::from(attrs), - )) + Ok(self.mk_expr_with_attrs(span.to(blk_span), ExprKind::ConstBlock(anon_const), attrs)) } /// Parses mutability (`mut` or nothing). diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs index 99efed0b7fb4..9a40b847d855 100644 --- a/compiler/rustc_passes/src/lang_items.rs +++ b/compiler/rustc_passes/src/lang_items.rs @@ -83,7 +83,6 @@ impl<'tcx> LanguageItemCollector<'tcx> { .map(|p| p.display().to_string()) .collect::>() .join(", ") - .into() }; let first_defined_span = self.tcx.hir().span_if_local(original_def_id); let mut orig_crate_name = Empty; @@ -98,7 +97,6 @@ impl<'tcx> LanguageItemCollector<'tcx> { .map(|p| p.display().to_string()) .collect::>() .join(", ") - .into() }; if first_defined_span.is_none() { orig_crate_name = self.tcx.crate_name(original_def_id.krate); diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 2dd2c568bab9..3b1be1a7f995 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1735,8 +1735,8 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { values.map(|(_, is_normalized_ty_expected, normalized_ty, expected_ty)| { infer::ValuePairs::Terms(ExpectedFound::new( is_normalized_ty_expected, - normalized_ty.into(), - expected_ty.into(), + normalized_ty, + expected_ty, )) }), err, From a108d55ce634b6f71462c82966b791f18136360e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 18 Dec 2022 17:01:58 +0100 Subject: [PATCH 186/321] don't restuct references just to reborrow --- compiler/rustc_codegen_llvm/src/back/lto.rs | 2 +- compiler/rustc_codegen_ssa/src/back/link.rs | 2 +- compiler/rustc_codegen_ssa/src/back/linker.rs | 2 +- compiler/rustc_const_eval/src/interpret/cast.rs | 2 +- compiler/rustc_expand/src/mbe/macro_check.rs | 4 ++-- compiler/rustc_expand/src/mbe/macro_rules.rs | 2 +- compiler/rustc_hir_typeck/src/demand.rs | 10 +++++----- compiler/rustc_hir_typeck/src/expr.rs | 2 +- .../rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 2 +- .../rustc_incremental/src/assert_dep_graph.rs | 2 +- compiler/rustc_infer/src/traits/project.rs | 2 +- compiler/rustc_lint/src/context.rs | 10 +++++----- compiler/rustc_lint/src/unused.rs | 2 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 ++-- compiler/rustc_middle/src/mir/tcx.rs | 2 +- compiler/rustc_middle/src/ty/flags.rs | 4 ++-- compiler/rustc_middle/src/ty/relate.rs | 2 +- .../rustc_mir_build/src/build/matches/test.rs | 17 ++++++++--------- compiler/rustc_passes/src/hir_stats.rs | 4 ++-- .../rustc_resolve/src/build_reduced_graph.rs | 2 +- compiler/rustc_session/src/session.rs | 2 +- .../src/traits/error_reporting/mod.rs | 2 +- 22 files changed, 41 insertions(+), 42 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index e20dc906bce9..6c0faf37a63c 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -425,7 +425,7 @@ fn thin_lto( info!("going for that thin, thin LTO"); let green_modules: FxHashMap<_, _> = - cached_modules.iter().map(|&(_, ref wp)| (wp.cgu_name.clone(), wp.clone())).collect(); + cached_modules.iter().map(|(_, wp)| (wp.cgu_name.clone(), wp.clone())).collect(); let full_scope_len = modules.len() + serialized_modules.len() + cached_modules.len(); let mut thin_buffers = Vec::with_capacity(modules.len()); diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index fe2e4b36cd0f..a1c77ec0cfca 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -722,7 +722,7 @@ fn link_natively<'a>( linker::disable_localization(&mut cmd); - for &(ref k, ref v) in sess.target.link_env.as_ref() { + for (k, v) in sess.target.link_env.as_ref() { cmd.env(k.as_ref(), v.as_ref()); } for k in sess.target.link_env_remove.as_ref() { diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index f087d903e556..0268659d3b9a 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -108,7 +108,7 @@ pub fn get_linker<'a>( if sess.target.is_like_msvc { if let Some(ref tool) = msvc_tool { cmd.args(tool.args()); - for &(ref k, ref v) in tool.env() { + for (k, v) in tool.env() { if k == "PATH" { new_path.extend(env::split_paths(v)); msvc_changed_path = true; diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs index b1fdeb01b100..986b6d655300 100644 --- a/compiler/rustc_const_eval/src/interpret/cast.rs +++ b/compiler/rustc_const_eval/src/interpret/cast.rs @@ -332,7 +332,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Immediate::new_slice(ptr, length.eval_usize(*self.tcx, self.param_env), self); self.write_immediate(val, dest) } - (&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => { + (ty::Dynamic(data_a, ..), ty::Dynamic(data_b, ..)) => { let val = self.read_immediate(src)?; if data_a.principal() == data_b.principal() { // A NOP cast that doesn't actually change anything, should be allowed even with mismatching vtables. diff --git a/compiler/rustc_expand/src/mbe/macro_check.rs b/compiler/rustc_expand/src/mbe/macro_check.rs index 8994a2f78919..0b8847f827df 100644 --- a/compiler/rustc_expand/src/mbe/macro_check.rs +++ b/compiler/rustc_expand/src/mbe/macro_check.rs @@ -468,7 +468,7 @@ fn check_nested_occurrences( // We check that the meta-variable is correctly used. check_occurrences(sess, node_id, tt, macros, binders, ops, valid); } - (NestedMacroState::MacroName, &TokenTree::Delimited(_, ref del)) + (NestedMacroState::MacroName, TokenTree::Delimited(_, del)) if del.delim == Delimiter::Parenthesis => { state = NestedMacroState::MacroNameParen; @@ -483,7 +483,7 @@ fn check_nested_occurrences( valid, ); } - (NestedMacroState::MacroNameParen, &TokenTree::Delimited(_, ref del)) + (NestedMacroState::MacroNameParen, TokenTree::Delimited(_, del)) if del.delim == Delimiter::Brace => { state = NestedMacroState::Empty; diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index 2dbb90e2190f..320c533a66e5 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -792,7 +792,7 @@ impl<'tt> FirstSets<'tt> { TokenTree::Sequence(sp, ref seq_rep) => { let subfirst_owned; let subfirst = match self.first.get(&sp.entire()) { - Some(&Some(ref subfirst)) => subfirst, + Some(Some(subfirst)) => subfirst, Some(&None) => { subfirst_owned = self.first(&seq_rep.tts); &subfirst_owned diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 6763e06c0cfe..87abd01c7fd8 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -1275,7 +1275,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; match (&expected_ty.kind(), &checked_ty.kind()) { - (&ty::Int(ref exp), &ty::Int(ref found)) => { + (ty::Int(exp), ty::Int(found)) => { let (f2e_is_fallible, e2f_is_fallible) = match (exp.bit_width(), found.bit_width()) { (Some(exp), Some(found)) if exp < found => (true, false), @@ -1288,7 +1288,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { suggest_to_change_suffix_or_into(err, f2e_is_fallible, e2f_is_fallible); true } - (&ty::Uint(ref exp), &ty::Uint(ref found)) => { + (ty::Uint(exp), ty::Uint(found)) => { let (f2e_is_fallible, e2f_is_fallible) = match (exp.bit_width(), found.bit_width()) { (Some(exp), Some(found)) if exp < found => (true, false), @@ -1321,7 +1321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { suggest_to_change_suffix_or_into(err, f2e_is_fallible, e2f_is_fallible); true } - (&ty::Float(ref exp), &ty::Float(ref found)) => { + (ty::Float(exp), ty::Float(found)) => { if found.bit_width() < exp.bit_width() { suggest_to_change_suffix_or_into(err, false, true); } else if literal_is_ty_suffixed(expr) { @@ -1357,7 +1357,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } true } - (&ty::Float(ref exp), &ty::Uint(ref found)) => { + (ty::Float(exp), ty::Uint(found)) => { // if `found` is `None` (meaning found is `usize`), don't suggest `.into()` if exp.bit_width() > found.bit_width().unwrap_or(256) { err.multipart_suggestion_verbose( @@ -1386,7 +1386,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } true } - (&ty::Float(ref exp), &ty::Int(ref found)) => { + (ty::Float(exp), ty::Int(found)) => { // if `found` is `None` (meaning found is `isize`), don't suggest `.into()` if exp.bit_width() > found.bit_width().unwrap_or(256) { err.multipart_suggestion_verbose( diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 866090260b27..edbbb7272ac7 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -1874,7 +1874,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // I don't use 'is_range_literal' because only double-sided, half-open ranges count. if let ExprKind::Struct( QPath::LangItem(LangItem::Range, ..), - &[ref range_start, ref range_end], + [range_start, range_end], _, ) = last_expr_field.expr.kind && let variant_field = diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 407d6ac8544c..ca4c50c49aae 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -754,7 +754,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return true } } - &hir::FnRetTy::Return(ref ty) => { + hir::FnRetTy::Return(ty) => { // Only point to return type if the expected type is the return type, as if they // are not, the expectation must have been caused by something else. debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.kind); diff --git a/compiler/rustc_incremental/src/assert_dep_graph.rs b/compiler/rustc_incremental/src/assert_dep_graph.rs index 69e482ce854c..b4bf9f4bcc76 100644 --- a/compiler/rustc_incremental/src/assert_dep_graph.rs +++ b/compiler/rustc_incremental/src/assert_dep_graph.rs @@ -249,7 +249,7 @@ fn dump_graph(query: &DepGraphQuery) { // dump a .txt file with just the edges: let txt_path = format!("{}.txt", path); let mut file = BufWriter::new(File::create(&txt_path).unwrap()); - for &(ref source, ref target) in &edges { + for (source, target) in &edges { write!(file, "{:?} -> {:?}\n", source, target).unwrap(); } } diff --git a/compiler/rustc_infer/src/traits/project.rs b/compiler/rustc_infer/src/traits/project.rs index aade57be9fe6..ac455055b430 100644 --- a/compiler/rustc_infer/src/traits/project.rs +++ b/compiler/rustc_infer/src/traits/project.rs @@ -200,7 +200,7 @@ impl<'tcx> ProjectionCache<'_, 'tcx> { pub fn complete(&mut self, key: ProjectionCacheKey<'tcx>, result: EvaluationResult) { let mut map = self.map(); match map.get(&key) { - Some(&ProjectionCacheEntry::NormalizedTy { ref ty, complete: _ }) => { + Some(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => { info!("ProjectionCacheEntry::complete({:?}) - completing {:?}", key, ty); let mut ty = ty.clone(); if result.must_apply_considering_regions() { diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 40b2588388d6..0417f375588c 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -438,18 +438,18 @@ impl LintStore { return CheckLintNameResult::Tool(Ok(&lint_ids)); } }, - Some(&Id(ref id)) => return CheckLintNameResult::Tool(Ok(slice::from_ref(id))), + Some(Id(id)) => return CheckLintNameResult::Tool(Ok(slice::from_ref(id))), // If the lint was registered as removed or renamed by the lint tool, we don't need // to treat tool_lints and rustc lints different and can use the code below. _ => {} } } match self.by_name.get(&complete_name) { - Some(&Renamed(ref new_name, _)) => CheckLintNameResult::Warning( + Some(Renamed(new_name, _)) => CheckLintNameResult::Warning( format!("lint `{}` has been renamed to `{}`", complete_name, new_name), Some(new_name.to_owned()), ), - Some(&Removed(ref reason)) => CheckLintNameResult::Warning( + Some(Removed(reason)) => CheckLintNameResult::Warning( format!("lint `{}` has been removed: {}", complete_name, reason), None, ), @@ -470,7 +470,7 @@ impl LintStore { CheckLintNameResult::Ok(&lint_ids) } }, - Some(&Id(ref id)) => CheckLintNameResult::Ok(slice::from_ref(id)), + Some(Id(id)) => CheckLintNameResult::Ok(slice::from_ref(id)), Some(&Ignored) => CheckLintNameResult::Ok(&[]), } } @@ -513,7 +513,7 @@ impl LintStore { CheckLintNameResult::Tool(Err((Some(&lint_ids), complete_name))) } }, - Some(&Id(ref id)) => { + Some(Id(id)) => { CheckLintNameResult::Tool(Err((Some(slice::from_ref(id)), complete_name))) } Some(other) => { diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index d628a18dd01c..3b8df61a0eab 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -1279,7 +1279,7 @@ impl UnusedImportBraces { fn check_use_tree(&self, cx: &EarlyContext<'_>, use_tree: &ast::UseTree, item: &ast::Item) { if let ast::UseTreeKind::Nested(ref items) = use_tree.kind { // Recursively check nested UseTrees - for &(ref tree, _) in items { + for (tree, _) in items { self.check_use_tree(cx, tree, item); } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 856f5bc4645f..96f15a6f758f 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1849,7 +1849,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // the assumption that they are numbered 1 to n. // FIXME (#2166): This is not nearly enough to support correct versioning // but is enough to get transitive crate dependencies working. - self.lazy_array(deps.iter().map(|&(_, ref dep)| dep)) + self.lazy_array(deps.iter().map(|(_, dep)| dep)) } fn encode_lib_features(&mut self) -> LazyArray<(Symbol, Option)> { @@ -1986,7 +1986,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array( exported_symbols .iter() - .filter(|&&(ref exported_symbol, _)| match *exported_symbol { + .filter(|&(exported_symbol, _)| match *exported_symbol { ExportedSymbol::NoDefId(symbol_name) => symbol_name != metadata_symbol_name, _ => true, }) diff --git a/compiler/rustc_middle/src/mir/tcx.rs b/compiler/rustc_middle/src/mir/tcx.rs index e3ca1f41d7ea..1e289fc4abec 100644 --- a/compiler/rustc_middle/src/mir/tcx.rs +++ b/compiler/rustc_middle/src/mir/tcx.rs @@ -235,7 +235,7 @@ impl<'tcx> Operand<'tcx> { { match self { &Operand::Copy(ref l) | &Operand::Move(ref l) => l.ty(local_decls, tcx).ty, - &Operand::Constant(ref c) => c.literal.ty(), + Operand::Constant(c) => c.literal.ty(), } } } diff --git a/compiler/rustc_middle/src/ty/flags.rs b/compiler/rustc_middle/src/ty/flags.rs index d283ccc3ad8a..042b89bc4b01 100644 --- a/compiler/rustc_middle/src/ty/flags.rs +++ b/compiler/rustc_middle/src/ty/flags.rs @@ -95,7 +95,7 @@ impl FlagComputation { self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE); } - &ty::Generator(_, ref substs, _) => { + ty::Generator(_, substs, _) => { let substs = substs.as_generator(); let should_remove_further_specializable = !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE); @@ -186,7 +186,7 @@ impl FlagComputation { &ty::Slice(tt) => self.add_ty(tt), - &ty::RawPtr(ref m) => { + ty::RawPtr(m) => { self.add_ty(m.ty); } diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index c4116558bd27..4d34ca3d66b5 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -428,7 +428,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>( Ok(a) } - (&ty::Param(ref a_p), &ty::Param(ref b_p)) if a_p.index == b_p.index => Ok(a), + (ty::Param(a_p), ty::Param(b_p)) if a_p.index == b_p.index => Ok(a), (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => Ok(a), diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index 65a027111d78..6c10704c5db5 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -551,16 +551,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // // FIXME(#29623) we could use PatKind::Range to rule // things out here, in some cases. - ( - &TestKind::SwitchInt { switch_ty: _, ref options }, - &PatKind::Constant { ref value }, - ) if is_switch_ty(match_pair.pattern.ty) => { + (TestKind::SwitchInt { switch_ty: _, options }, PatKind::Constant { value }) + if is_switch_ty(match_pair.pattern.ty) => + { let index = options.get_index_of(value).unwrap(); self.candidate_without_match_pair(match_pair_index, candidate); Some(index) } - (&TestKind::SwitchInt { switch_ty: _, ref options }, &PatKind::Range(ref range)) => { + (TestKind::SwitchInt { switch_ty: _, options }, PatKind::Range(range)) => { let not_contained = self.values_not_contained_in_range(&*range, options).unwrap_or(false); @@ -578,7 +577,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ( &TestKind::Len { len: test_len, op: BinOp::Eq }, - &PatKind::Slice { ref prefix, ref slice, ref suffix }, + PatKind::Slice { prefix, slice, suffix }, ) => { let pat_len = (prefix.len() + suffix.len()) as u64; match (test_len.cmp(&pat_len), slice) { @@ -615,7 +614,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ( &TestKind::Len { len: test_len, op: BinOp::Ge }, - &PatKind::Slice { ref prefix, ref slice, ref suffix }, + PatKind::Slice { prefix, slice, suffix }, ) => { // the test is `$actual_len >= test_len` let pat_len = (prefix.len() + suffix.len()) as u64; @@ -651,7 +650,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } } - (&TestKind::Range(ref test), &PatKind::Range(ref pat)) => { + (TestKind::Range(test), PatKind::Range(pat)) => { use std::cmp::Ordering::*; if test == pat { @@ -678,7 +677,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { no_overlap } - (&TestKind::Range(ref range), &PatKind::Constant { value }) => { + (TestKind::Range(range), &PatKind::Constant { value }) => { if let Some(false) = self.const_range_contains(&*range, value) { // `value` is not contained in the testing range, // so `value` can be matched only if this test fails. diff --git a/compiler/rustc_passes/src/hir_stats.rs b/compiler/rustc_passes/src/hir_stats.rs index a7854cd49988..272386f313e8 100644 --- a/compiler/rustc_passes/src/hir_stats.rs +++ b/compiler/rustc_passes/src/hir_stats.rs @@ -121,7 +121,7 @@ impl<'k> StatCollector<'k> { fn print(&self, title: &str, prefix: &str) { let mut nodes: Vec<_> = self.nodes.iter().collect(); - nodes.sort_by_key(|&(_, ref node)| node.stats.count * node.stats.size); + nodes.sort_by_key(|(_, node)| node.stats.count * node.stats.size); let total_size = nodes.iter().map(|(_, node)| node.stats.count * node.stats.size).sum(); @@ -147,7 +147,7 @@ impl<'k> StatCollector<'k> { ); if !node.subnodes.is_empty() { let mut subnodes: Vec<_> = node.subnodes.iter().collect(); - subnodes.sort_by_key(|&(_, ref subnode)| subnode.count * subnode.size); + subnodes.sort_by_key(|(_, subnode)| subnode.count * subnode.size); for (label, subnode) in subnodes { let size = subnode.count * subnode.size; diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index f4a6a08df1c8..cf6359962686 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -576,7 +576,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { // Ensure there is at most one `self` in the list let self_spans = items .iter() - .filter_map(|&(ref use_tree, _)| { + .filter_map(|(use_tree, _)| { if let ast::UseTreeKind::Simple(..) = use_tree.kind { if use_tree.ident().name == kw::SelfLower { return Some(use_tree.span); diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index fdd883fbeed5..01a9b1000882 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1323,7 +1323,7 @@ pub fn build_session( let warnings_allow = sopts .lint_opts .iter() - .rfind(|&&(ref key, _)| *key == "warnings") + .rfind(|&(key, _)| *key == "warnings") .map_or(false, |&(_, level)| level == lint::Allow); let cap_lints_allow = sopts.lint_cap.map_or(false, |cap| cap == lint::Allow); let can_emit_warnings = !(warnings_allow || cap_lints_allow); diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 2dd2c568bab9..d0c842a0b890 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -226,7 +226,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { let arg_length = arguments.len(); let distinct = matches!(other, &[ArgKind::Tuple(..)]); match (arg_length, arguments.get(0)) { - (1, Some(&ArgKind::Tuple(_, ref fields))) => { + (1, Some(ArgKind::Tuple(_, fields))) => { format!("a single {}-tuple as argument", fields.len()) } _ => format!( From 8348e05644777d08d1fa3d245f9be4040e6dcb16 Mon Sep 17 00:00:00 2001 From: Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> Date: Sun, 18 Dec 2022 18:07:48 +0100 Subject: [PATCH 187/321] Make x use the x and x.ps1 scripts This removes another python search from bootstrap. --- src/tools/x/src/main.rs | 72 ++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/src/tools/x/src/main.rs b/src/tools/x/src/main.rs index 02c364dabf96..f07ff43efe98 100644 --- a/src/tools/x/src/main.rs +++ b/src/tools/x/src/main.rs @@ -1,51 +1,43 @@ -//! Run `x.py` from any subdirectory of a rust compiler checkout. +//! Run bootstrap from any subdirectory of a rust compiler checkout. //! //! We prefer `exec`, to avoid adding an extra process in the process tree. //! However, since `exec` isn't available on Windows, we indirect through //! `exec_or_status`, which will call `exec` on unix and `status` on Windows. //! -//! We use `python`, `python3`, or `python2` as the python interpreter to run -//! `x.py`, in that order of preference. +//! We use `powershell.exe x.ps1` on Windows, and `sh -c x` on Unix, those are +//! the ones that call `x.py`. We use `sh -c` on Unix, because it is a standard. +//! We also don't use `pwsh` on Windows, because it is not installed by default; use std::{ - env::{self, consts::EXE_EXTENSION}, - io, + env, io, + path::Path, process::{self, Command, ExitStatus}, }; -const PYTHON: &str = "python"; -const PYTHON2: &str = "python2"; -const PYTHON3: &str = "python3"; +#[cfg(windows)] +fn x_command(dir: &Path) -> Command { + let mut cmd = Command::new("powershell.exe"); + cmd.args([ + "-NoLogo", + "-NoProfile", + "-NonInteractive", + "-ExecutionPolicy", + "RemoteSigned", + "-Command", + "./x.ps1", + ]) + .current_dir(dir); + cmd +} -fn python() -> &'static str { - let val = match env::var_os("PATH") { - Some(val) => val, - None => return PYTHON, - }; +#[cfg(unix)] +fn x_command(dir: &Path) -> Command { + Command::new(dir.join("x")) +} - let mut python2 = false; - let mut python3 = false; - - for dir in env::split_paths(&val) { - // `python` should always take precedence over python2 / python3 if it exists - if dir.join(PYTHON).with_extension(EXE_EXTENSION).exists() { - return PYTHON; - } - - python2 |= dir.join(PYTHON2).with_extension(EXE_EXTENSION).exists(); - python3 |= dir.join(PYTHON3).with_extension(EXE_EXTENSION).exists(); - } - - // try 3 before 2 - if python3 { - PYTHON3 - } else if python2 { - PYTHON2 - } else { - // Python was not found on path, so exit - eprintln!("Unable to find python in your PATH. Please check it is installed."); - process::exit(1); - } +#[cfg(not(any(windows, unix)))] +fn x_command(_dir: &Path) -> Command { + compile_error!("Unsupported platform"); } #[cfg(unix)] @@ -72,15 +64,15 @@ fn main() { let candidate = dir.join("x.py"); if candidate.exists() { - let mut python = Command::new(python()); + let mut cmd = x_command(dir); - python.arg(&candidate).args(env::args().skip(1)).current_dir(dir); + cmd.args(env::args().skip(1)).current_dir(dir); - let result = exec_or_status(&mut python); + let result = exec_or_status(&mut cmd); match result { Err(error) => { - eprintln!("Failed to invoke `{}`: {}", candidate.display(), error); + eprintln!("Failed to invoke `{:?}`: {}", cmd, error); } Ok(status) => { process::exit(status.code().unwrap_or(1)); From 0c029ab07ced737730da1e6274ea9b3a2e4d0b41 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 18 Dec 2022 11:42:30 +0100 Subject: [PATCH 188/321] Update browser-ui-test version to reduce GUI tests flakyness --- .../docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version b/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version index 3f8dcd03d2db..475434e5aef8 100644 --- a/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version +++ b/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version @@ -1 +1 @@ -0.13.2 \ No newline at end of file +0.13.4 \ No newline at end of file From d59a2ac2bc42fb76d208da256ec0ed4887633319 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 18 Dec 2022 20:28:59 +0100 Subject: [PATCH 189/321] Revert "Introduce lowering_arena to avoid creating AST nodes on the fly" This reverts commit d9a1faaa9cff6eab069ea8e5cd7862d0ae48e231. This was originally part of a larger PR that has now been closed as a different approach is taken now. --- compiler/rustc_ast_lowering/src/item.rs | 4 +- compiler/rustc_ast_lowering/src/lib.rs | 55 +++++++++---------------- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 73065ab51635..9d4c2900eaf4 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1,6 +1,6 @@ use super::errors::{InvalidAbi, InvalidAbiSuggestion, MisplacedRelaxTraitBound}; use super::ResolverAstLoweringExt; -use super::{Arena, AstOwner, ImplTraitContext, ImplTraitPosition}; +use super::{AstOwner, ImplTraitContext, ImplTraitPosition}; use super::{FnDeclKind, LoweringContext, ParamMode}; use rustc_ast::ptr::P; @@ -24,7 +24,6 @@ use thin_vec::ThinVec; pub(super) struct ItemLowerer<'a, 'hir> { pub(super) tcx: TyCtxt<'hir>, pub(super) resolver: &'a mut ResolverAstLowering, - pub(super) ast_arena: &'a Arena<'static>, pub(super) ast_index: &'a IndexVec>, pub(super) owners: &'a mut IndexVec>>, } @@ -60,7 +59,6 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> { tcx: self.tcx, resolver: self.resolver, arena: self.tcx.hir_arena, - ast_arena: self.ast_arena, // HirId handling. bodies: Vec::new(), diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d67ede6e1302..fe0d0747be60 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -42,7 +42,6 @@ extern crate tracing; use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait, TraitFnAsync}; -use rustc_arena::declare_arena; use rustc_ast::ptr::P; use rustc_ast::visit; use rustc_ast::{self as ast, *}; @@ -94,13 +93,6 @@ struct LoweringContext<'a, 'hir> { /// Used to allocate HIR nodes. arena: &'hir hir::Arena<'hir>, - /// Used to allocate temporary AST nodes for use during lowering. - /// This allows us to create "fake" AST -- these nodes can sometimes - /// be allocated on the stack, but other times we need them to live longer - /// than the current stack frame, so they can be collected into vectors - /// and things like that. - ast_arena: &'a Arena<'static>, - /// Bodies inside the owner being lowered. bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>, /// Attributes inside the owner being lowered. @@ -146,15 +138,6 @@ struct LoweringContext<'a, 'hir> { generics_def_id_map: Vec>, } -declare_arena!([ - [] tys: rustc_ast::Ty, - [] aba: rustc_ast::AngleBracketedArgs, - [] ptr: rustc_ast::PolyTraitRef, - // This _marker field is needed because `declare_arena` creates `Arena<'tcx>` and we need to - // use `'tcx`. If we don't have this we get a compile error. - [] _marker: std::marker::PhantomData<&'tcx ()>, -]); - trait ResolverAstLoweringExt { fn legacy_const_generic_args(&self, expr: &Expr) -> Option>; fn get_partial_res(&self, id: NodeId) -> Option; @@ -442,13 +425,10 @@ pub fn lower_to_hir<'hir>(tcx: TyCtxt<'hir>, (): ()) -> hir::Crate<'hir> { tcx.definitions_untracked().def_index_count(), ); - let ast_arena = Arena::default(); - for def_id in ast_index.indices() { item::ItemLowerer { tcx, resolver: &mut resolver, - ast_arena: &ast_arena, ast_index: &ast_index, owners: &mut owners, } @@ -1001,8 +981,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } GenericArgs::Parenthesized(data) => { self.emit_bad_parenthesized_trait_in_assoc_ty(data); - let aba = self.ast_arena.aba.alloc(data.as_angle_bracketed_args()); - self.lower_angle_bracketed_parameter_data(aba, ParamMode::Explicit, itctx).0 + self.lower_angle_bracketed_parameter_data( + &data.as_angle_bracketed_args(), + ParamMode::Explicit, + itctx, + ) + .0 } }; gen_args_ctor.into_generic_args(self) @@ -1067,13 +1051,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.with_dyn_type_scope(false, |this| { let node_id = this.next_node_id(); - let ty = this.ast_arena.tys.alloc(Ty { - id: node_id, - kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()), - span: this.lower_span(constraint.span), - tokens: None, - }); - let ty = this.lower_ty(ty, itctx); + let ty = this.lower_ty( + &Ty { + id: node_id, + kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()), + span: this.lower_span(constraint.span), + tokens: None, + }, + itctx, + ); hir::TypeBindingKind::Equality { term: ty.into() } }) @@ -1217,13 +1203,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { && let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res() { let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| { - let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef { - bound_generic_params: vec![], - trait_ref: TraitRef { path: path.clone(), ref_id: t.id }, - span: t.span - }); let bound = this.lower_poly_trait_ref( - poly_trait_ref, + &PolyTraitRef { + bound_generic_params: vec![], + trait_ref: TraitRef { path: path.clone(), ref_id: t.id }, + span: t.span + }, itctx, ); let bounds = this.arena.alloc_from_iter([bound]); From b4d739ef1285bc983383ec726314d9455f6bfb77 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 18 Dec 2022 20:53:08 +0100 Subject: [PATCH 190/321] Use `#[derive]` instead of custom syntax in all `newtype_index` --- compiler/rustc_macros/src/newtype.rs | 12 ------------ compiler/rustc_middle/src/middle/region.rs | 2 +- compiler/rustc_middle/src/mir/coverage.rs | 10 +++++----- compiler/rustc_middle/src/mir/mod.rs | 10 +++++----- compiler/rustc_middle/src/mir/query.rs | 2 +- compiler/rustc_middle/src/ty/typeck_results.rs | 2 +- compiler/rustc_target/src/abi/mod.rs | 2 +- 7 files changed, 14 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index fd3f52251555..46f5ead5942b 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -1,11 +1,9 @@ use proc_macro2::{Span, TokenStream}; use quote::quote; use syn::parse::*; -use syn::punctuated::Punctuated; use syn::*; mod kw { - syn::custom_keyword!(derive); syn::custom_keyword!(DEBUG_FORMAT); syn::custom_keyword!(MAX); syn::custom_keyword!(ENCODABLE); @@ -57,16 +55,6 @@ impl Parse for Newtype { body.parse::()?; } else { loop { - if body.lookahead1().peek(kw::derive) { - body.parse::()?; - let derives; - bracketed!(derives in body); - let derives: Punctuated = - derives.parse_terminated(Path::parse)?; - try_comma()?; - derive_paths.extend(derives); - continue; - } if body.lookahead1().peek(kw::DEBUG_FORMAT) { body.parse::()?; body.parse::()?; diff --git a/compiler/rustc_middle/src/middle/region.rs b/compiler/rustc_middle/src/middle/region.rs index c886175c6ea0..dfe71dc92577 100644 --- a/compiler/rustc_middle/src/middle/region.rs +++ b/compiler/rustc_middle/src/middle/region.rs @@ -147,8 +147,8 @@ rustc_index::newtype_index! { /// /// * The subscope with `first_statement_index == 1` is scope of `c`, /// and thus does not include EXPR_2, but covers the `...`. + #[derive(HashStable)] pub struct FirstStatementIndex { - derive [HashStable] } } diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index 0b55757eb038..173c8967eec8 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -10,8 +10,8 @@ rustc_index::newtype_index! { /// CounterValueReference.as_u32() (which ascend from 1) or an ExpressionOperandId.as_u32() /// (which _*descend*_ from u32::MAX). Id value `0` (zero) represents a virtual counter with a /// constant value of `0`. + #[derive(HashStable)] pub struct ExpressionOperandId { - derive [HashStable] DEBUG_FORMAT = "ExpressionOperandId({})", MAX = 0xFFFF_FFFF, } @@ -32,8 +32,8 @@ impl ExpressionOperandId { } rustc_index::newtype_index! { + #[derive(HashStable)] pub struct CounterValueReference { - derive [HashStable] DEBUG_FORMAT = "CounterValueReference({})", MAX = 0xFFFF_FFFF, } @@ -56,8 +56,8 @@ rustc_index::newtype_index! { /// InjectedExpressionId.as_u32() converts to ExpressionOperandId.as_u32() /// /// Values descend from u32::MAX. + #[derive(HashStable)] pub struct InjectedExpressionId { - derive [HashStable] DEBUG_FORMAT = "InjectedExpressionId({})", MAX = 0xFFFF_FFFF, } @@ -67,8 +67,8 @@ rustc_index::newtype_index! { /// InjectedExpressionIndex.as_u32() translates to u32::MAX - ExpressionOperandId.as_u32() /// /// Values ascend from 0. + #[derive(HashStable)] pub struct InjectedExpressionIndex { - derive [HashStable] DEBUG_FORMAT = "InjectedExpressionIndex({})", MAX = 0xFFFF_FFFF, } @@ -78,8 +78,8 @@ rustc_index::newtype_index! { /// MappedExpressionIndex values ascend from zero, and are recalculated indexes based on their /// array position in the LLVM coverage map "Expressions" array, which is assembled during the /// "mapgen" process. They cannot be computed algorithmically, from the other `newtype_index`s. + #[derive(HashStable)] pub struct MappedExpressionIndex { - derive [HashStable] DEBUG_FORMAT = "MappedExpressionIndex({})", MAX = 0xFFFF_FFFF, } diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index e909b2f74aa1..0b4fc253fef8 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -654,8 +654,8 @@ impl SourceInfo { // Variables and temps rustc_index::newtype_index! { + #[derive(HashStable)] pub struct Local { - derive [HashStable] DEBUG_FORMAT = "_{}", const RETURN_PLACE = 0, } @@ -1146,8 +1146,8 @@ rustc_index::newtype_index! { /// https://rustc-dev-guide.rust-lang.org/appendix/background.html#what-is-a-dataflow-analysis /// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges /// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/ + #[derive(HashStable)] pub struct BasicBlock { - derive [HashStable] DEBUG_FORMAT = "bb{}", const START_BLOCK = 0, } @@ -1530,8 +1530,8 @@ rustc_index::newtype_index! { /// [wrapper]: https://rustc-dev-guide.rust-lang.org/appendix/glossary.html#newtype /// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg /// [mir-datatypes]: https://rustc-dev-guide.rust-lang.org/mir/index.html#mir-data-types + #[derive(HashStable)] pub struct Field { - derive [HashStable] DEBUG_FORMAT = "field[{}]" } } @@ -1757,8 +1757,8 @@ impl Debug for Place<'_> { // Scopes rustc_index::newtype_index! { + #[derive(HashStable)] pub struct SourceScope { - derive [HashStable] DEBUG_FORMAT = "scope[{}]", const OUTERMOST_SOURCE_SCOPE = 0, } @@ -2755,8 +2755,8 @@ impl<'tcx> TypeVisitable<'tcx> for UserTypeProjection { } rustc_index::newtype_index! { + #[derive(HashStable)] pub struct Promoted { - derive [HashStable] DEBUG_FORMAT = "promoted[{}]" } } diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index efd7357afc46..4db3ded34ead 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -130,8 +130,8 @@ pub struct UnsafetyCheckResult { } rustc_index::newtype_index! { + #[derive(HashStable)] pub struct GeneratorSavedLocal { - derive [HashStable] DEBUG_FORMAT = "_{}", } } diff --git a/compiler/rustc_middle/src/ty/typeck_results.rs b/compiler/rustc_middle/src/ty/typeck_results.rs index 4fe85d4366f3..1a8927558c9d 100644 --- a/compiler/rustc_middle/src/ty/typeck_results.rs +++ b/compiler/rustc_middle/src/ty/typeck_results.rs @@ -608,8 +608,8 @@ impl<'a, V> LocalTableInContextMut<'a, V> { } rustc_index::newtype_index! { + #[derive(HashStable)] pub struct UserTypeAnnotationIndex { - derive [HashStable] DEBUG_FORMAT = "UserType({})", const START_INDEX = 0, } diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs index 53c9878ab874..c60de3cb0b2b 100644 --- a/compiler/rustc_target/src/abi/mod.rs +++ b/compiler/rustc_target/src/abi/mod.rs @@ -20,8 +20,8 @@ impl ToJson for Endian { } rustc_index::newtype_index! { + #[derive(HashStable_Generic)] pub struct VariantIdx { - derive [HashStable_Generic] } } From 7e66d451ad49aa69dd830ea22943e6ffdc92eddd Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Sun, 18 Dec 2022 20:58:09 +1300 Subject: [PATCH 191/321] docs: add long-form error-code docs for E0460 --- compiler/rustc_error_codes/src/error_codes.rs | 2 +- .../src/error_codes/E0460.md | 71 +++++++++++++++++++ src/test/ui/svh/changing-crates.stderr | 1 + src/test/ui/svh/svh-change-lit.stderr | 1 + .../ui/svh/svh-change-significant-cfg.stderr | 1 + src/test/ui/svh/svh-change-trait-bound.stderr | 1 + src/test/ui/svh/svh-change-type-arg.stderr | 1 + src/test/ui/svh/svh-change-type-ret.stderr | 1 + src/test/ui/svh/svh-change-type-static.stderr | 1 + src/test/ui/svh/svh-use-trait.stderr | 1 + 10 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0460.md diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 67c512e98d68..59f691c7ec86 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -241,6 +241,7 @@ E0454: include_str!("./error_codes/E0454.md"), E0455: include_str!("./error_codes/E0455.md"), E0458: include_str!("./error_codes/E0458.md"), E0459: include_str!("./error_codes/E0459.md"), +E0460: include_str!("./error_codes/E0460.md"), E0463: include_str!("./error_codes/E0463.md"), E0464: include_str!("./error_codes/E0464.md"), E0466: include_str!("./error_codes/E0466.md"), @@ -593,7 +594,6 @@ E0791: include_str!("./error_codes/E0791.md"), // E0427, // merged into 530 // E0456, // plugin `..` is not available for triple `..` E0457, // plugin `..` only found in rlib format, but must be available... - E0460, // found possibly newer version of crate `..` E0461, // couldn't find crate `..` with expected target triple .. E0462, // found staticlib `..` instead of rlib or dylib E0465, // multiple .. candidates for `..` found diff --git a/compiler/rustc_error_codes/src/error_codes/E0460.md b/compiler/rustc_error_codes/src/error_codes/E0460.md new file mode 100644 index 000000000000..1687682b28ec --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0460.md @@ -0,0 +1,71 @@ +Found possibly newer version of crate `..` which `..` depends on. + +Consider these erroneous files: + +`a1.rs` +```ignore (needs-linkage-with-other-tests) +#![crate_name = "a"] + +pub fn foo() {} +``` + +`a2.rs` +```ignore (needs-linkage-with-other-tests) +#![crate_name = "a"] + +pub fn foo() { + println!("foo()"); +} +``` + +`b.rs` +```ignore (needs-linkage-with-other-tests) +#![crate_name = "b"] + +extern crate a; // linked with `a1.rs` + +pub fn foo() { + a::foo::(); +} +``` + +`main.rs` +```ignore (needs-linkage-with-other-tests) +extern crate a; // linked with `a2.rs` +extern crate b; // error: found possibly newer version of crate `a` which `b` + // depends on + +fn main() {} +``` + +The dependency graph of this program can be represented as follows: +```text + crate `main` + | + +-------------+ + | | + | v +depends: | crate `b` + `a` v1 | | + | | depends: + | | `a` v2 + v | + crate `a` <------+ +``` + +Crate `main` depends on crate `a` (version 1) and crate `b` which in turn +depends on crate `a` (version 2); this discrepancy in versions cannot be +reconciled. This difference in versions typically occurs when one crate is +compiled and linked, then updated and linked to another crate. The crate +"version" is a SVH (Strict Version Hash) of the crate in an +implementation-specific way. Note that this error can *only* occur when +directly compiling and linking with `rustc`; [Cargo] automatically resolves +dependencies, without using the compiler's own dependency management that +causes this issue. + +This error can be fixed by: +* Using [Cargo], the Rust package manager, automatically fixing this issue. +* Recompiling crate `a` so that both crate `b` and `main` have a uniform +version to depend on. + +[Cargo]: ../cargo/index.html diff --git a/src/test/ui/svh/changing-crates.stderr b/src/test/ui/svh/changing-crates.stderr index 7244919e86d5..caefdfc96f03 100644 --- a/src/test/ui/svh/changing-crates.stderr +++ b/src/test/ui/svh/changing-crates.stderr @@ -11,3 +11,4 @@ LL | extern crate b; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. diff --git a/src/test/ui/svh/svh-change-lit.stderr b/src/test/ui/svh/svh-change-lit.stderr index 1e97e9d0557d..5e890c6aa579 100644 --- a/src/test/ui/svh/svh-change-lit.stderr +++ b/src/test/ui/svh/svh-change-lit.stderr @@ -11,3 +11,4 @@ LL | extern crate b; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. diff --git a/src/test/ui/svh/svh-change-significant-cfg.stderr b/src/test/ui/svh/svh-change-significant-cfg.stderr index f04046f4c87b..dcc250d5216b 100644 --- a/src/test/ui/svh/svh-change-significant-cfg.stderr +++ b/src/test/ui/svh/svh-change-significant-cfg.stderr @@ -11,3 +11,4 @@ LL | extern crate b; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. diff --git a/src/test/ui/svh/svh-change-trait-bound.stderr b/src/test/ui/svh/svh-change-trait-bound.stderr index a778c61806a5..2035993d218e 100644 --- a/src/test/ui/svh/svh-change-trait-bound.stderr +++ b/src/test/ui/svh/svh-change-trait-bound.stderr @@ -11,3 +11,4 @@ LL | extern crate b; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. diff --git a/src/test/ui/svh/svh-change-type-arg.stderr b/src/test/ui/svh/svh-change-type-arg.stderr index f09babf93fd3..eef85aa95461 100644 --- a/src/test/ui/svh/svh-change-type-arg.stderr +++ b/src/test/ui/svh/svh-change-type-arg.stderr @@ -11,3 +11,4 @@ LL | extern crate b; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. diff --git a/src/test/ui/svh/svh-change-type-ret.stderr b/src/test/ui/svh/svh-change-type-ret.stderr index 0998cd4b5496..247f74e50df6 100644 --- a/src/test/ui/svh/svh-change-type-ret.stderr +++ b/src/test/ui/svh/svh-change-type-ret.stderr @@ -11,3 +11,4 @@ LL | extern crate b; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. diff --git a/src/test/ui/svh/svh-change-type-static.stderr b/src/test/ui/svh/svh-change-type-static.stderr index 9c48cbd30a50..78b54f227f0f 100644 --- a/src/test/ui/svh/svh-change-type-static.stderr +++ b/src/test/ui/svh/svh-change-type-static.stderr @@ -11,3 +11,4 @@ LL | extern crate b; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. diff --git a/src/test/ui/svh/svh-use-trait.stderr b/src/test/ui/svh/svh-use-trait.stderr index 5780cfef357d..d8a81864dcaa 100644 --- a/src/test/ui/svh/svh-use-trait.stderr +++ b/src/test/ui/svh/svh-use-trait.stderr @@ -11,3 +11,4 @@ LL | extern crate utb; error: aborting due to previous error +For more information about this error, try `rustc --explain E0460`. From 540c3f434f25b90b6f238aa520d5532909f711db Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Sun, 18 Dec 2022 22:00:35 +1300 Subject: [PATCH 192/321] docs: add long-form error-code docs for E0457 --- compiler/rustc_error_codes/src/error_codes.rs | 2 +- .../src/error_codes/E0457.md | 36 +++++++++++++++++++ src/test/ui-fulldeps/macro-crate-rlib.stderr | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0457.md diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 59f691c7ec86..883a4bbe8e82 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -239,6 +239,7 @@ E0452: include_str!("./error_codes/E0452.md"), E0453: include_str!("./error_codes/E0453.md"), E0454: include_str!("./error_codes/E0454.md"), E0455: include_str!("./error_codes/E0455.md"), +E0457: include_str!("./error_codes/E0457.md"), E0458: include_str!("./error_codes/E0458.md"), E0459: include_str!("./error_codes/E0459.md"), E0460: include_str!("./error_codes/E0460.md"), @@ -593,7 +594,6 @@ E0791: include_str!("./error_codes/E0791.md"), // E0421, // merged into 531 // E0427, // merged into 530 // E0456, // plugin `..` is not available for triple `..` - E0457, // plugin `..` only found in rlib format, but must be available... E0461, // couldn't find crate `..` with expected target triple .. E0462, // found staticlib `..` instead of rlib or dylib E0465, // multiple .. candidates for `..` found diff --git a/compiler/rustc_error_codes/src/error_codes/E0457.md b/compiler/rustc_error_codes/src/error_codes/E0457.md new file mode 100644 index 000000000000..53d384d36c42 --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0457.md @@ -0,0 +1,36 @@ +Plugin `..` only found in rlib format, but must be available in dylib format. + +Erroronous code example: + +`rlib-plugin.rs` +```ignore (needs-linkage-with-other-tests) +#![crate_type = "rlib"] +#![feature(rustc_private)] + +extern crate rustc_middle; +extern crate rustc_driver; + +use rustc_driver::plugin::Registry; + +#[no_mangle] +fn __rustc_plugin_registrar(_: &mut Registry) {} +``` + +`main.rs` +```ignore (needs-linkage-with-other-tests) +#![feature(plugin)] +#![plugin(rlib_plugin)] // error: plugin `rlib_plugin` only found in rlib + // format, but must be available in dylib + +fn main() {} +``` + +The compiler exposes a plugin interface to allow altering the compile process +(adding lints, etc). Plugins must be defined in their own crates (similar to +[proc-macro](../reference/procedural-macros.html) isolation) and then compiled +and linked to another crate. Plugin crates *must* be compiled to the +dynamically-linked dylib format, and not the statically-linked rlib format. +Learn more about different output types in +[this section](../reference/linkage.html) of the Rust reference. + +This error is easily fixed by recompiling the plugin crate in the dylib format. diff --git a/src/test/ui-fulldeps/macro-crate-rlib.stderr b/src/test/ui-fulldeps/macro-crate-rlib.stderr index 7b31f28a26e7..9c2b992b7655 100644 --- a/src/test/ui-fulldeps/macro-crate-rlib.stderr +++ b/src/test/ui-fulldeps/macro-crate-rlib.stderr @@ -6,3 +6,4 @@ LL | #![plugin(rlib_crate_test)] error: aborting due to previous error +For more information about this error, try `rustc --explain E0457`. From 88d5f7f4ce72e3489193fb41e687c53809b5c6bd Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 18 Dec 2022 21:02:14 +0100 Subject: [PATCH 193/321] Make `#[custom_encodable]` an attribute for `newtype_index` Makes the syntax a little more rusty. --- compiler/rustc_ast/src/ast.rs | 2 +- .../src/coherence/inherent_impls_overlap.rs | 2 +- compiler/rustc_lint/src/levels.rs | 2 +- compiler/rustc_macros/src/newtype.rs | 22 ++++++++++--------- compiler/rustc_span/src/def_id.rs | 5 ++--- compiler/rustc_span/src/hygiene.rs | 4 ++-- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index f933b9b161ca..49649188f585 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2554,8 +2554,8 @@ pub enum AttrStyle { } rustc_index::newtype_index! { + #[custom_encodable] pub struct AttrId { - ENCODABLE = custom DEBUG_FORMAT = "AttrId({})" } } diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs index 972769eb1970..6531e55c577a 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs @@ -198,8 +198,8 @@ impl<'tcx> InherentOverlapChecker<'tcx> { // entire graph when there are many connected regions. rustc_index::newtype_index! { + #[custom_encodable] pub struct RegionId { - ENCODABLE = custom } } struct ConnectedRegion { diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 847c356b83c0..7fed40b201b6 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -39,8 +39,8 @@ struct LintLevelSets { } rustc_index::newtype_index! { + #[custom_encodable] // we don't need encoding struct LintStackIndex { - ENCODABLE = custom, // we don't need encoding const COMMAND_LINE = 0, } } diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index 46f5ead5942b..2e28e9fb49d4 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -6,7 +6,6 @@ use syn::*; mod kw { syn::custom_keyword!(DEBUG_FORMAT); syn::custom_keyword!(MAX); - syn::custom_keyword!(ENCODABLE); syn::custom_keyword!(custom); syn::custom_keyword!(ORD_IMPL); } @@ -27,7 +26,7 @@ struct Newtype(TokenStream); impl Parse for Newtype { fn parse(input: ParseStream<'_>) -> Result { - let attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let vis: Visibility = input.parse()?; input.parse::()?; let name: Ident = input.parse()?; @@ -51,6 +50,17 @@ impl Parse for Newtype { Ok(()) }; + attrs.retain(|attr| match attr.path.get_ident() { + Some(ident) => match &*ident.to_string() { + "custom_encodable" => { + encodable = false; + false + } + _ => true, + }, + _ => true, + }); + if body.lookahead1().peek(Token![..]) { body.parse::()?; } else { @@ -81,14 +91,6 @@ impl Parse for Newtype { } continue; } - if body.lookahead1().peek(kw::ENCODABLE) { - body.parse::()?; - body.parse::()?; - body.parse::()?; - try_comma()?; - encodable = false; - continue; - } if body.lookahead1().peek(kw::ORD_IMPL) { body.parse::()?; body.parse::()?; diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index e62ce2c266aa..326dce7838db 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -10,8 +10,8 @@ use std::fmt; use std::hash::{Hash, Hasher}; rustc_index::newtype_index! { + #[custom_encodable] pub struct CrateNum { - ENCODABLE = custom DEBUG_FORMAT = "crate{}" } } @@ -194,9 +194,8 @@ rustc_index::newtype_index! { /// A DefIndex is an index into the hir-map for a crate, identifying a /// particular definition. It should really be considered an interned /// shorthand for a particular DefPath. + #[custom_encodable] // (only encodable in metadata) pub struct DefIndex { - ENCODABLE = custom // (only encodable in metadata) - DEBUG_FORMAT = "DefIndex({})", /// The crate root is always assigned index 0 by the AST Map code, /// thanks to `NodeCollector::new`. diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 038699154c72..4fb56dac856f 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -61,8 +61,8 @@ pub struct SyntaxContextData { rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. + #[custom_encodable] pub struct ExpnIndex { - ENCODABLE = custom } } @@ -82,8 +82,8 @@ impl fmt::Debug for ExpnId { rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. + #[custom_encodable] pub struct LocalExpnId { - ENCODABLE = custom ORD_IMPL = custom DEBUG_FORMAT = "expn{}" } From 93429948cf0e6fe8aac3802a8fbc73a121463994 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 18 Dec 2022 21:06:44 +0100 Subject: [PATCH 194/321] Make `#[no_ord_impl]` an attribute in `newtype_index` --- compiler/rustc_macros/src/newtype.rs | 12 ++++-------- compiler/rustc_span/src/hygiene.rs | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index 2e28e9fb49d4..59cedef08eac 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -7,7 +7,6 @@ mod kw { syn::custom_keyword!(DEBUG_FORMAT); syn::custom_keyword!(MAX); syn::custom_keyword!(custom); - syn::custom_keyword!(ORD_IMPL); } #[derive(Debug)] @@ -56,6 +55,10 @@ impl Parse for Newtype { encodable = false; false } + "no_ord_impl" => { + ord = false; + false + } _ => true, }, _ => true, @@ -91,13 +94,6 @@ impl Parse for Newtype { } continue; } - if body.lookahead1().peek(kw::ORD_IMPL) { - body.parse::()?; - body.parse::()?; - body.parse::()?; - ord = false; - continue; - } // We've parsed everything that the user provided, so we're done if body.is_empty() { diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 4fb56dac856f..2de559c5a708 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -83,8 +83,8 @@ impl fmt::Debug for ExpnId { rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. #[custom_encodable] + #[no_ord_impl] pub struct LocalExpnId { - ORD_IMPL = custom DEBUG_FORMAT = "expn{}" } } From 91c3c2040ce109d6e9ea00ffb8e8f69f3fd6d9bb Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 18 Dec 2022 21:22:14 +0100 Subject: [PATCH 195/321] Make `#[max]` an attribute in `newtype_index` --- compiler/rustc_index/src/vec/tests.rs | 5 ++++- compiler/rustc_macros/src/newtype.rs | 21 ++++++++++--------- compiler/rustc_middle/src/mir/coverage.rs | 10 ++++----- .../src/dep_graph/serialized.rs | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_index/src/vec/tests.rs b/compiler/rustc_index/src/vec/tests.rs index 915d2e8bcb3f..e72863f6de11 100644 --- a/compiler/rustc_index/src/vec/tests.rs +++ b/compiler/rustc_index/src/vec/tests.rs @@ -3,7 +3,10 @@ // Allows the macro invocation below to work use crate as rustc_index; -rustc_macros::newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA }); +rustc_macros::newtype_index! { + #[max = 0xFFFF_FFFA] + struct MyIdx { } +} #[test] fn index_size_is_optimized() { diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index 59cedef08eac..99f3f638d2f4 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -59,6 +59,17 @@ impl Parse for Newtype { ord = false; false } + "max" => { + let Ok(Meta::NameValue(literal) )= attr.parse_meta() else { + panic!("#[max = NUMBER] attribute requires max value"); + }; + + if let Some(old) = max.replace(literal.lit) { + panic!("Specified multiple MAX: {:?}", old); + } + + false + } _ => true, }, _ => true, @@ -84,16 +95,6 @@ impl Parse for Newtype { } continue; } - if body.lookahead1().peek(kw::MAX) { - body.parse::()?; - body.parse::()?; - let val: Lit = body.parse()?; - try_comma()?; - if let Some(old) = max.replace(val) { - panic!("Specified multiple MAX: {:?}", old); - } - continue; - } // We've parsed everything that the user provided, so we're done if body.is_empty() { diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index 173c8967eec8..3025e2dd1342 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -11,9 +11,9 @@ rustc_index::newtype_index! { /// (which _*descend*_ from u32::MAX). Id value `0` (zero) represents a virtual counter with a /// constant value of `0`. #[derive(HashStable)] + #[max = 0xFFFF_FFFF] pub struct ExpressionOperandId { DEBUG_FORMAT = "ExpressionOperandId({})", - MAX = 0xFFFF_FFFF, } } @@ -33,9 +33,9 @@ impl ExpressionOperandId { rustc_index::newtype_index! { #[derive(HashStable)] + #[max = 0xFFFF_FFFF] pub struct CounterValueReference { DEBUG_FORMAT = "CounterValueReference({})", - MAX = 0xFFFF_FFFF, } } @@ -57,9 +57,9 @@ rustc_index::newtype_index! { /// /// Values descend from u32::MAX. #[derive(HashStable)] + #[max = 0xFFFF_FFFF] pub struct InjectedExpressionId { DEBUG_FORMAT = "InjectedExpressionId({})", - MAX = 0xFFFF_FFFF, } } @@ -68,9 +68,9 @@ rustc_index::newtype_index! { /// /// Values ascend from 0. #[derive(HashStable)] + #[max = 0xFFFF_FFFF] pub struct InjectedExpressionIndex { DEBUG_FORMAT = "InjectedExpressionIndex({})", - MAX = 0xFFFF_FFFF, } } @@ -79,9 +79,9 @@ rustc_index::newtype_index! { /// array position in the LLVM coverage map "Expressions" array, which is assembled during the /// "mapgen" process. They cannot be computed algorithmically, from the other `newtype_index`s. #[derive(HashStable)] + #[max = 0xFFFF_FFFF] pub struct MappedExpressionIndex { DEBUG_FORMAT = "MappedExpressionIndex({})", - MAX = 0xFFFF_FFFF, } } diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index d292f4beef2e..fbc0aeb6c802 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -27,8 +27,8 @@ use smallvec::SmallVec; // unused so that we can store multiple index types in `CompressedHybridIndex`, // and use those bits to encode which index type it contains. rustc_index::newtype_index! { + #[max = 0x7FFF_FFFF] pub struct SerializedDepNodeIndex { - MAX = 0x7FFF_FFFF } } From d679764fb6b84a5cb72f6a9c9fefce4c6b41f16c Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 18 Dec 2022 21:37:38 +0100 Subject: [PATCH 196/321] Make `#[debug_format]` an attribute in `newtype_index` This removes the `custom` format functionality as its only user was trivially migrated to using a normal format. If a new use case for a custom formatting impl pops up, you can add it back. --- compiler/rustc_ast/src/ast.rs | 2 +- compiler/rustc_ast/src/node_id.rs | 2 +- .../rustc_borrowck/src/constraints/mod.rs | 4 +- compiler/rustc_borrowck/src/dataflow.rs | 2 +- compiler/rustc_borrowck/src/location.rs | 2 +- .../rustc_borrowck/src/member_constraints.rs | 2 +- .../rustc_borrowck/src/region_infer/values.rs | 6 +- .../src/fn_ctxt/arg_matrix.rs | 4 +- .../src/generator_interior/drop_ranges/mod.rs | 4 +- .../infer/region_constraints/leak_check.rs | 4 +- compiler/rustc_macros/src/newtype.rs | 66 ++++++------------- compiler/rustc_middle/src/mir/coverage.rs | 10 +-- compiler/rustc_middle/src/mir/mod.rs | 10 +-- compiler/rustc_middle/src/mir/query.rs | 2 +- compiler/rustc_middle/src/thir.rs | 2 +- .../rustc_middle/src/ty/structural_impls.rs | 6 -- compiler/rustc_middle/src/ty/sty.rs | 2 +- .../rustc_middle/src/ty/typeck_results.rs | 2 +- .../rustc_mir_dataflow/src/move_paths/mod.rs | 6 +- .../rustc_mir_transform/src/coverage/graph.rs | 2 +- compiler/rustc_passes/src/liveness.rs | 4 +- compiler/rustc_span/src/def_id.rs | 4 +- compiler/rustc_span/src/hygiene.rs | 2 +- compiler/rustc_type_ir/src/lib.rs | 6 +- 24 files changed, 64 insertions(+), 92 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 49649188f585..4140bff6e299 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2555,8 +2555,8 @@ pub enum AttrStyle { rustc_index::newtype_index! { #[custom_encodable] + #[debug_format = "AttrId({})]"] pub struct AttrId { - DEBUG_FORMAT = "AttrId({})" } } diff --git a/compiler/rustc_ast/src/node_id.rs b/compiler/rustc_ast/src/node_id.rs index 7b5acc3f4859..8ba6b8c2c33b 100644 --- a/compiler/rustc_ast/src/node_id.rs +++ b/compiler/rustc_ast/src/node_id.rs @@ -8,8 +8,8 @@ rustc_index::newtype_index! { /// This is later turned into [`DefId`] and `HirId` for the HIR. /// /// [`DefId`]: rustc_span::def_id::DefId + #[debug_format = "NodeId({})"] pub struct NodeId { - DEBUG_FORMAT = "NodeId({})" } } diff --git a/compiler/rustc_borrowck/src/constraints/mod.rs b/compiler/rustc_borrowck/src/constraints/mod.rs index 84a93e5f72e9..0e5a874735ce 100644 --- a/compiler/rustc_borrowck/src/constraints/mod.rs +++ b/compiler/rustc_borrowck/src/constraints/mod.rs @@ -115,13 +115,13 @@ impl<'tcx> fmt::Debug for OutlivesConstraint<'tcx> { } rustc_index::newtype_index! { + #[debug_format = "OutlivesConstraintIndex({})"] pub struct OutlivesConstraintIndex { - DEBUG_FORMAT = "OutlivesConstraintIndex({})" } } rustc_index::newtype_index! { + #[debug_format = "ConstraintSccIndex({})"] pub struct ConstraintSccIndex { - DEBUG_FORMAT = "ConstraintSccIndex({})" } } diff --git a/compiler/rustc_borrowck/src/dataflow.rs b/compiler/rustc_borrowck/src/dataflow.rs index f825b1d8f70e..6f533cec9fad 100644 --- a/compiler/rustc_borrowck/src/dataflow.rs +++ b/compiler/rustc_borrowck/src/dataflow.rs @@ -108,8 +108,8 @@ impl_visitable! { } rustc_index::newtype_index! { + #[debug_format = "bw{}"] pub struct BorrowIndex { - DEBUG_FORMAT = "bw{}" } } diff --git a/compiler/rustc_borrowck/src/location.rs b/compiler/rustc_borrowck/src/location.rs index 9fa7e218b1b6..0614163474c5 100644 --- a/compiler/rustc_borrowck/src/location.rs +++ b/compiler/rustc_borrowck/src/location.rs @@ -20,8 +20,8 @@ pub struct LocationTable { } rustc_index::newtype_index! { + #[debug_format = "LocationIndex({})"] pub struct LocationIndex { - DEBUG_FORMAT = "LocationIndex({})" } } diff --git a/compiler/rustc_borrowck/src/member_constraints.rs b/compiler/rustc_borrowck/src/member_constraints.rs index b5e00f471d26..ccf6fcac9d2e 100644 --- a/compiler/rustc_borrowck/src/member_constraints.rs +++ b/compiler/rustc_borrowck/src/member_constraints.rs @@ -55,8 +55,8 @@ pub(crate) struct NllMemberConstraint<'tcx> { } rustc_index::newtype_index! { + #[debug_format = "MemberConstraintIndex({})"] pub(crate) struct NllMemberConstraintIndex { - DEBUG_FORMAT = "MemberConstraintIndex({})" } } diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 7498ddccf196..c3dfeedc205f 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -90,12 +90,14 @@ impl RegionValueElements { rustc_index::newtype_index! { /// A single integer representing a `Location` in the MIR control-flow /// graph. Constructed efficiently from `RegionValueElements`. - pub struct PointIndex { DEBUG_FORMAT = "PointIndex({})" } + #[debug_format = "PointIndex({})"] + pub struct PointIndex {} } rustc_index::newtype_index! { /// A single integer representing a `ty::Placeholder`. - pub struct PlaceholderIndex { DEBUG_FORMAT = "PlaceholderIndex({})" } + #[debug_format = "PlaceholderIndex({})"] + pub struct PlaceholderIndex {} } /// An individual element in a region value -- the value of a diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs index fc83994caf53..3f59e8ddf154 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs @@ -4,14 +4,14 @@ use rustc_index::vec::IndexVec; use rustc_middle::ty::error::TypeError; rustc_index::newtype_index! { + #[debug_format = "ExpectedIdx({})"] pub(crate) struct ExpectedIdx { - DEBUG_FORMAT = "ExpectedIdx({})", } } rustc_index::newtype_index! { + #[debug_format = "ProvidedIdx({})"] pub(crate) struct ProvidedIdx { - DEBUG_FORMAT = "ProvidedIdx({})", } } diff --git a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs index 2abcadcc9ce7..3763373ad68a 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs @@ -96,14 +96,14 @@ fn for_each_consumable<'tcx>(hir: Map<'tcx>, place: TrackedValue, mut f: impl Fn } rustc_index::newtype_index! { + #[debug_format = "id({})"] pub struct PostOrderId { - DEBUG_FORMAT = "id({})", } } rustc_index::newtype_index! { + #[debug_format = "hidx({})"] pub struct TrackedValueIndex { - DEBUG_FORMAT = "hidx({})", } } diff --git a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs index 22b4bbb17d47..64dc0e8f56d4 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs @@ -357,14 +357,14 @@ impl<'tcx> SccUniverse<'tcx> { } rustc_index::newtype_index! { + #[debug_format = "LeakCheckNode({})"] struct LeakCheckNode { - DEBUG_FORMAT = "LeakCheckNode({})" } } rustc_index::newtype_index! { + #[debug_format = "LeakCheckScc({})"] struct LeakCheckScc { - DEBUG_FORMAT = "LeakCheckScc({})" } } diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index 99f3f638d2f4..7b7e475da5b5 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -3,22 +3,6 @@ use quote::quote; use syn::parse::*; use syn::*; -mod kw { - syn::custom_keyword!(DEBUG_FORMAT); - syn::custom_keyword!(MAX); - syn::custom_keyword!(custom); -} - -#[derive(Debug)] -enum DebugFormat { - // The user will provide a custom `Debug` impl, so we shouldn't generate - // one - Custom, - // Use the specified format string in the generated `Debug` impl - // By default, this is "{}" - Format(String), -} - // We parse the input and emit the output in a single step. // This field stores the final macro output struct Newtype(TokenStream); @@ -35,7 +19,7 @@ impl Parse for Newtype { // Any additional `#[derive]` macro paths to apply let mut derive_paths: Vec = Vec::new(); - let mut debug_format: Option = None; + let mut debug_format: Option = None; let mut max = None; let mut consts = Vec::new(); let mut encodable = true; @@ -65,7 +49,18 @@ impl Parse for Newtype { }; if let Some(old) = max.replace(literal.lit) { - panic!("Specified multiple MAX: {:?}", old); + panic!("Specified multiple max: {:?}", old); + } + + false + } + "debug_format" => { + let Ok(Meta::NameValue(literal) )= attr.parse_meta() else { + panic!("#[debug_format = FMT] attribute requires a format"); + }; + + if let Some(old) = debug_format.replace(literal.lit) { + panic!("Specified multiple debug format options: {:?}", old); } false @@ -79,23 +74,6 @@ impl Parse for Newtype { body.parse::()?; } else { loop { - if body.lookahead1().peek(kw::DEBUG_FORMAT) { - body.parse::()?; - body.parse::()?; - let new_debug_format = if body.lookahead1().peek(kw::custom) { - body.parse::()?; - DebugFormat::Custom - } else { - let format_str: LitStr = body.parse()?; - DebugFormat::Format(format_str.value()) - }; - try_comma()?; - if let Some(old) = debug_format.replace(new_debug_format) { - panic!("Specified multiple debug format options: {:?}", old); - } - continue; - } - // We've parsed everything that the user provided, so we're done if body.is_empty() { break; @@ -112,7 +90,9 @@ impl Parse for Newtype { } } - let debug_format = debug_format.unwrap_or(DebugFormat::Format("{}".to_string())); + let debug_format = + debug_format.unwrap_or_else(|| Lit::Str(LitStr::new("{}", Span::call_site()))); + // shave off 256 indices at the end to allow space for packing these indices into enums let max = max.unwrap_or_else(|| Lit::Int(LitInt::new("0xFFFF_FF00", Span::call_site()))); @@ -167,18 +147,14 @@ impl Parse for Newtype { quote! {} }; - let debug_impl = match debug_format { - DebugFormat::Custom => quote! {}, - DebugFormat::Format(format) => { - quote! { - impl ::std::fmt::Debug for #name { - fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { - write!(fmt, #format, self.as_u32()) - } - } + let debug_impl = quote! { + impl ::std::fmt::Debug for #name { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + write!(fmt, #debug_format, self.as_u32()) } } }; + let spec_partial_eq_impl = if let Lit::Int(max) = &max { if let Ok(max_val) = max.base10_parse::() { quote! { diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index 3025e2dd1342..8806077175b8 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -12,8 +12,8 @@ rustc_index::newtype_index! { /// constant value of `0`. #[derive(HashStable)] #[max = 0xFFFF_FFFF] + #[debug_format = "ExpressionOperandId({})"] pub struct ExpressionOperandId { - DEBUG_FORMAT = "ExpressionOperandId({})", } } @@ -34,8 +34,8 @@ impl ExpressionOperandId { rustc_index::newtype_index! { #[derive(HashStable)] #[max = 0xFFFF_FFFF] + #[debug_format = "CounterValueReference({})"] pub struct CounterValueReference { - DEBUG_FORMAT = "CounterValueReference({})", } } @@ -58,8 +58,8 @@ rustc_index::newtype_index! { /// Values descend from u32::MAX. #[derive(HashStable)] #[max = 0xFFFF_FFFF] + #[debug_format = "InjectedExpressionId({})"] pub struct InjectedExpressionId { - DEBUG_FORMAT = "InjectedExpressionId({})", } } @@ -69,8 +69,8 @@ rustc_index::newtype_index! { /// Values ascend from 0. #[derive(HashStable)] #[max = 0xFFFF_FFFF] + #[debug_format = "InjectedExpressionIndex({})"] pub struct InjectedExpressionIndex { - DEBUG_FORMAT = "InjectedExpressionIndex({})", } } @@ -80,8 +80,8 @@ rustc_index::newtype_index! { /// "mapgen" process. They cannot be computed algorithmically, from the other `newtype_index`s. #[derive(HashStable)] #[max = 0xFFFF_FFFF] + #[debug_format = "MappedExpressionIndex({})"] pub struct MappedExpressionIndex { - DEBUG_FORMAT = "MappedExpressionIndex({})", } } diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 0b4fc253fef8..6057fdcaebba 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -655,8 +655,8 @@ impl SourceInfo { rustc_index::newtype_index! { #[derive(HashStable)] + #[debug_format = "_{}"] pub struct Local { - DEBUG_FORMAT = "_{}", const RETURN_PLACE = 0, } } @@ -1147,8 +1147,8 @@ rustc_index::newtype_index! { /// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges /// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/ #[derive(HashStable)] + #[debug_format = "bb{}"] pub struct BasicBlock { - DEBUG_FORMAT = "bb{}", const START_BLOCK = 0, } } @@ -1531,8 +1531,8 @@ rustc_index::newtype_index! { /// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg /// [mir-datatypes]: https://rustc-dev-guide.rust-lang.org/mir/index.html#mir-data-types #[derive(HashStable)] + #[debug_format = "field[{}]"] pub struct Field { - DEBUG_FORMAT = "field[{}]" } } @@ -1758,8 +1758,8 @@ impl Debug for Place<'_> { rustc_index::newtype_index! { #[derive(HashStable)] + #[debug_format = "scope[{}]"] pub struct SourceScope { - DEBUG_FORMAT = "scope[{}]", const OUTERMOST_SOURCE_SCOPE = 0, } } @@ -2756,8 +2756,8 @@ impl<'tcx> TypeVisitable<'tcx> for UserTypeProjection { rustc_index::newtype_index! { #[derive(HashStable)] + #[debug_format = "promoted[{}]"] pub struct Promoted { - DEBUG_FORMAT = "promoted[{}]" } } diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 4db3ded34ead..160e9691e69b 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -131,8 +131,8 @@ pub struct UnsafetyCheckResult { rustc_index::newtype_index! { #[derive(HashStable)] + #[debug_format = "_{}"] pub struct GeneratorSavedLocal { - DEBUG_FORMAT = "_{}", } } diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index 8bef9dfe099b..ad8f873bf9b7 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -35,8 +35,8 @@ macro_rules! thir_with_elements { $( newtype_index! { #[derive(HashStable)] + #[debug_format = $format] pub struct $id { - DEBUG_FORMAT = $format } } )* diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 3c6800cf293d..30073b541ecb 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -99,12 +99,6 @@ impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> { } } -impl fmt::Debug for ty::RegionVid { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "'_#{}r", self.index()) - } -} - impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { with_no_trimmed_paths!(fmt::Display::fmt(self, f)) diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 66aeebab88ba..3f22b0e5a9a0 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1378,8 +1378,8 @@ pub struct ConstVid<'tcx> { rustc_index::newtype_index! { /// A **region** (lifetime) **v**ariable **ID**. #[derive(HashStable)] + #[debug_format = "'_#{}r"] pub struct RegionVid { - DEBUG_FORMAT = custom, } } diff --git a/compiler/rustc_middle/src/ty/typeck_results.rs b/compiler/rustc_middle/src/ty/typeck_results.rs index 1a8927558c9d..8483b1745c7e 100644 --- a/compiler/rustc_middle/src/ty/typeck_results.rs +++ b/compiler/rustc_middle/src/ty/typeck_results.rs @@ -609,8 +609,8 @@ impl<'a, V> LocalTableInContextMut<'a, V> { rustc_index::newtype_index! { #[derive(HashStable)] + #[debug_format = "UserType({})"] pub struct UserTypeAnnotationIndex { - DEBUG_FORMAT = "UserType({})", const START_INDEX = 0, } } diff --git a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs index b36e268cf8bf..f286aff8420c 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs @@ -14,8 +14,8 @@ use self::abs_domain::{AbstractElem, Lift}; mod abs_domain; rustc_index::newtype_index! { + #[debug_format = "mp{}"] pub struct MovePathIndex { - DEBUG_FORMAT = "mp{}" } } @@ -26,14 +26,14 @@ impl polonius_engine::Atom for MovePathIndex { } rustc_index::newtype_index! { + #[debug_format = "mo{}"] pub struct MoveOutIndex { - DEBUG_FORMAT = "mo{}" } } rustc_index::newtype_index! { + #[debug_format = "in{}"] pub struct InitIndex { - DEBUG_FORMAT = "in{}" } } diff --git a/compiler/rustc_mir_transform/src/coverage/graph.rs b/compiler/rustc_mir_transform/src/coverage/graph.rs index 782129be088b..40ed6ceb839e 100644 --- a/compiler/rustc_mir_transform/src/coverage/graph.rs +++ b/compiler/rustc_mir_transform/src/coverage/graph.rs @@ -282,8 +282,8 @@ impl graph::WithPredecessors for CoverageGraph { rustc_index::newtype_index! { /// A node in the control-flow graph of CoverageGraph. + #[debug_format = "bcb{}"] pub(super) struct BasicCoverageBlock { - DEBUG_FORMAT = "bcb{}", const START_BCB = 0, } } diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index 1f65cc8b6096..bfbe65b5bfc3 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -108,14 +108,14 @@ use std::rc::Rc; mod rwu_table; rustc_index::newtype_index! { + #[debug_format = "v({})"] pub struct Variable { - DEBUG_FORMAT = "v({})", } } rustc_index::newtype_index! { + #[debug_format = "ln({})"] pub struct LiveNode { - DEBUG_FORMAT = "ln({})", } } diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 326dce7838db..5d15757f1ab3 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -11,8 +11,8 @@ use std::hash::{Hash, Hasher}; rustc_index::newtype_index! { #[custom_encodable] + #[debug_format = "crate{}"] pub struct CrateNum { - DEBUG_FORMAT = "crate{}" } } @@ -195,8 +195,8 @@ rustc_index::newtype_index! { /// particular definition. It should really be considered an interned /// shorthand for a particular DefPath. #[custom_encodable] // (only encodable in metadata) + #[debug_format = "DefIndex({})"] pub struct DefIndex { - DEBUG_FORMAT = "DefIndex({})", /// The crate root is always assigned index 0 by the AST Map code, /// thanks to `NodeCollector::new`. const CRATE_DEF_INDEX = 0, diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 2de559c5a708..f80b5d99df63 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -84,8 +84,8 @@ rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. #[custom_encodable] #[no_ord_impl] + #[debug_format = "expn{}"] pub struct LocalExpnId { - DEBUG_FORMAT = "expn{}" } } diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index c992dbccd62d..5a6ee6721d38 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -301,8 +301,8 @@ rustc_index::newtype_index! { /// /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index #[derive(HashStable_Generic)] + #[debug_format = "DebruijnIndex({})"] pub struct DebruijnIndex { - DEBUG_FORMAT = "DebruijnIndex({})", const INNERMOST = 0, } } @@ -499,8 +499,8 @@ pub struct FloatVarValue(pub FloatTy); rustc_index::newtype_index! { /// A **ty**pe **v**ariable **ID**. + #[debug_format = "_#{}t"] pub struct TyVid { - DEBUG_FORMAT = "_#{}t" } } @@ -788,8 +788,8 @@ rustc_index::newtype_index! { /// type -- an idealized representative of "types in general" that we /// use for checking generic functions. #[derive(HashStable_Generic)] + #[debug_format = "U{}"] pub struct UniverseIndex { - DEBUG_FORMAT = "U{}", } } From 8bfd6450c7b61ffee0fd7e21b538d00018a0e47e Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 18 Dec 2022 21:47:28 +0100 Subject: [PATCH 197/321] A few small cleanups for `newtype_index` Remove the `..` from the body, only a few invocations used it and it's inconsistent with rust syntax. Use `;` instead of `,` between consts. As the Rust syntax gods inteded. --- compiler/rustc_ast/src/ast.rs | 3 +- compiler/rustc_ast/src/node_id.rs | 3 +- .../rustc_borrowck/src/constraints/mod.rs | 6 +-- compiler/rustc_borrowck/src/dataflow.rs | 3 +- compiler/rustc_borrowck/src/location.rs | 3 +- .../rustc_borrowck/src/member_constraints.rs | 3 +- .../src/type_check/liveness/local_use_map.rs | 2 +- .../src/graph/dominators/mod.rs | 2 +- compiler/rustc_hir/src/hir_id.rs | 2 +- .../src/coherence/inherent_impls_overlap.rs | 4 +- .../src/fn_ctxt/arg_matrix.rs | 6 +-- .../src/generator_interior/drop_ranges/mod.rs | 6 +-- compiler/rustc_index/src/vec/tests.rs | 2 +- .../infer/region_constraints/leak_check.rs | 6 +-- compiler/rustc_lint/src/levels.rs | 2 +- compiler/rustc_macros/src/newtype.rs | 38 +++++++------------ compiler/rustc_middle/src/middle/region.rs | 3 +- compiler/rustc_middle/src/mir/coverage.rs | 12 ++---- compiler/rustc_middle/src/mir/mod.rs | 12 +++--- compiler/rustc_middle/src/mir/query.rs | 3 +- compiler/rustc_middle/src/thir.rs | 3 +- compiler/rustc_middle/src/ty/sty.rs | 5 +-- .../rustc_middle/src/ty/typeck_results.rs | 2 +- compiler/rustc_mir_build/src/build/mod.rs | 2 +- compiler/rustc_mir_build/src/build/scope.rs | 2 +- .../rustc_mir_dataflow/src/move_paths/mod.rs | 9 ++--- .../rustc_mir_transform/src/coverage/graph.rs | 2 +- compiler/rustc_passes/src/liveness.rs | 6 +-- .../rustc_query_system/src/dep_graph/graph.rs | 4 +- .../src/dep_graph/serialized.rs | 3 +- compiler/rustc_span/src/def_id.rs | 5 +-- compiler/rustc_span/src/hygiene.rs | 6 +-- compiler/rustc_span/src/symbol.rs | 2 +- compiler/rustc_target/src/abi/mod.rs | 3 +- compiler/rustc_type_ir/src/lib.rs | 8 ++-- 35 files changed, 68 insertions(+), 115 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 4140bff6e299..31596a1e9bf8 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2556,8 +2556,7 @@ pub enum AttrStyle { rustc_index::newtype_index! { #[custom_encodable] #[debug_format = "AttrId({})]"] - pub struct AttrId { - } + pub struct AttrId {} } impl Encodable for AttrId { diff --git a/compiler/rustc_ast/src/node_id.rs b/compiler/rustc_ast/src/node_id.rs index 8ba6b8c2c33b..daa82996b3d1 100644 --- a/compiler/rustc_ast/src/node_id.rs +++ b/compiler/rustc_ast/src/node_id.rs @@ -9,8 +9,7 @@ rustc_index::newtype_index! { /// /// [`DefId`]: rustc_span::def_id::DefId #[debug_format = "NodeId({})"] - pub struct NodeId { - } + pub struct NodeId {} } rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId); diff --git a/compiler/rustc_borrowck/src/constraints/mod.rs b/compiler/rustc_borrowck/src/constraints/mod.rs index 0e5a874735ce..1f0b8adeaf16 100644 --- a/compiler/rustc_borrowck/src/constraints/mod.rs +++ b/compiler/rustc_borrowck/src/constraints/mod.rs @@ -116,12 +116,10 @@ impl<'tcx> fmt::Debug for OutlivesConstraint<'tcx> { rustc_index::newtype_index! { #[debug_format = "OutlivesConstraintIndex({})"] - pub struct OutlivesConstraintIndex { - } + pub struct OutlivesConstraintIndex {} } rustc_index::newtype_index! { #[debug_format = "ConstraintSccIndex({})"] - pub struct ConstraintSccIndex { - } + pub struct ConstraintSccIndex {} } diff --git a/compiler/rustc_borrowck/src/dataflow.rs b/compiler/rustc_borrowck/src/dataflow.rs index 6f533cec9fad..8c4885770ad3 100644 --- a/compiler/rustc_borrowck/src/dataflow.rs +++ b/compiler/rustc_borrowck/src/dataflow.rs @@ -109,8 +109,7 @@ impl_visitable! { rustc_index::newtype_index! { #[debug_format = "bw{}"] - pub struct BorrowIndex { - } + pub struct BorrowIndex {} } /// `Borrows` stores the data used in the analyses that track the flow diff --git a/compiler/rustc_borrowck/src/location.rs b/compiler/rustc_borrowck/src/location.rs index 0614163474c5..288b7d85be2d 100644 --- a/compiler/rustc_borrowck/src/location.rs +++ b/compiler/rustc_borrowck/src/location.rs @@ -21,8 +21,7 @@ pub struct LocationTable { rustc_index::newtype_index! { #[debug_format = "LocationIndex({})"] - pub struct LocationIndex { - } + pub struct LocationIndex {} } #[derive(Copy, Clone, Debug)] diff --git a/compiler/rustc_borrowck/src/member_constraints.rs b/compiler/rustc_borrowck/src/member_constraints.rs index ccf6fcac9d2e..b63e286676ff 100644 --- a/compiler/rustc_borrowck/src/member_constraints.rs +++ b/compiler/rustc_borrowck/src/member_constraints.rs @@ -56,8 +56,7 @@ pub(crate) struct NllMemberConstraint<'tcx> { rustc_index::newtype_index! { #[debug_format = "MemberConstraintIndex({})"] - pub(crate) struct NllMemberConstraintIndex { - } + pub(crate) struct NllMemberConstraintIndex {} } impl Default for MemberConstraintSet<'_, ty::RegionVid> { diff --git a/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs b/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs index fda2cee43fbf..8023ef60d205 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs @@ -46,7 +46,7 @@ struct Appearance { } rustc_index::newtype_index! { - pub struct AppearanceIndex { .. } + pub struct AppearanceIndex {} } impl vll::LinkElem for Appearance { diff --git a/compiler/rustc_data_structures/src/graph/dominators/mod.rs b/compiler/rustc_data_structures/src/graph/dominators/mod.rs index 00913a483db0..94a8c1fc051d 100644 --- a/compiler/rustc_data_structures/src/graph/dominators/mod.rs +++ b/compiler/rustc_data_structures/src/graph/dominators/mod.rs @@ -22,7 +22,7 @@ struct PreOrderFrame { } rustc_index::newtype_index! { - struct PreorderIndex { .. } + struct PreorderIndex {} } pub fn dominators(graph: G) -> Dominators { diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index 060f40919f5c..03bcaa694682 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -138,7 +138,7 @@ rustc_index::newtype_index! { /// an "item-like" to something else can be implemented by a `Vec` instead of a /// tree or hash map. #[derive(HashStable_Generic)] - pub struct ItemLocalId { .. } + pub struct ItemLocalId {} } impl ItemLocalId { diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs index 6531e55c577a..a9331af4eab3 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs @@ -199,9 +199,9 @@ impl<'tcx> InherentOverlapChecker<'tcx> { rustc_index::newtype_index! { #[custom_encodable] - pub struct RegionId { - } + pub struct RegionId {} } + struct ConnectedRegion { idents: SmallVec<[Symbol; 8]>, impl_blocks: FxHashSet, diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs index 3f59e8ddf154..6f26afcaf168 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs @@ -5,14 +5,12 @@ use rustc_middle::ty::error::TypeError; rustc_index::newtype_index! { #[debug_format = "ExpectedIdx({})"] - pub(crate) struct ExpectedIdx { - } + pub(crate) struct ExpectedIdx {} } rustc_index::newtype_index! { #[debug_format = "ProvidedIdx({})"] - pub(crate) struct ProvidedIdx { - } + pub(crate) struct ProvidedIdx {} } impl ExpectedIdx { diff --git a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs index 3763373ad68a..2f55ea939fc0 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs @@ -97,14 +97,12 @@ fn for_each_consumable<'tcx>(hir: Map<'tcx>, place: TrackedValue, mut f: impl Fn rustc_index::newtype_index! { #[debug_format = "id({})"] - pub struct PostOrderId { - } + pub struct PostOrderId {} } rustc_index::newtype_index! { #[debug_format = "hidx({})"] - pub struct TrackedValueIndex { - } + pub struct TrackedValueIndex {} } /// Identifies a value whose drop state we need to track. diff --git a/compiler/rustc_index/src/vec/tests.rs b/compiler/rustc_index/src/vec/tests.rs index e72863f6de11..cb0f0db220d9 100644 --- a/compiler/rustc_index/src/vec/tests.rs +++ b/compiler/rustc_index/src/vec/tests.rs @@ -5,7 +5,7 @@ use crate as rustc_index; rustc_macros::newtype_index! { #[max = 0xFFFF_FFFA] - struct MyIdx { } + struct MyIdx {} } #[test] diff --git a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs index 64dc0e8f56d4..c46edc33ff40 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs @@ -358,14 +358,12 @@ impl<'tcx> SccUniverse<'tcx> { rustc_index::newtype_index! { #[debug_format = "LeakCheckNode({})"] - struct LeakCheckNode { - } + struct LeakCheckNode {} } rustc_index::newtype_index! { #[debug_format = "LeakCheckScc({})"] - struct LeakCheckScc { - } + struct LeakCheckScc {} } /// Represents the graph of constraints. For each `R1: R2` constraint we create diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 7fed40b201b6..e9d3d44a3f9f 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -41,7 +41,7 @@ struct LintLevelSets { rustc_index::newtype_index! { #[custom_encodable] // we don't need encoding struct LintStackIndex { - const COMMAND_LINE = 0, + const COMMAND_LINE = 0; } } diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs index 7b7e475da5b5..153473de6244 100644 --- a/compiler/rustc_macros/src/newtype.rs +++ b/compiler/rustc_macros/src/newtype.rs @@ -25,14 +25,6 @@ impl Parse for Newtype { let mut encodable = true; let mut ord = true; - // Parse an optional trailing comma - let try_comma = || -> Result<()> { - if body.lookahead1().peek(Token![,]) { - body.parse::()?; - } - Ok(()) - }; - attrs.retain(|attr| match attr.path.get_ident() { Some(ident) => match &*ident.to_string() { "custom_encodable" => { @@ -70,24 +62,20 @@ impl Parse for Newtype { _ => true, }); - if body.lookahead1().peek(Token![..]) { - body.parse::()?; - } else { - loop { - // We've parsed everything that the user provided, so we're done - if body.is_empty() { - break; - } - - // Otherwise, we are parsing a user-defined constant - let const_attrs = body.call(Attribute::parse_outer)?; - body.parse::()?; - let const_name: Ident = body.parse()?; - body.parse::()?; - let const_val: Expr = body.parse()?; - try_comma()?; - consts.push(quote! { #(#const_attrs)* #vis const #const_name: #name = #name::from_u32(#const_val); }); + loop { + // We've parsed everything that the user provided, so we're done + if body.is_empty() { + break; } + + // Otherwise, we are parsing a user-defined constant + let const_attrs = body.call(Attribute::parse_outer)?; + body.parse::()?; + let const_name: Ident = body.parse()?; + body.parse::()?; + let const_val: Expr = body.parse()?; + body.parse::()?; + consts.push(quote! { #(#const_attrs)* #vis const #const_name: #name = #name::from_u32(#const_val); }); } let debug_format = diff --git a/compiler/rustc_middle/src/middle/region.rs b/compiler/rustc_middle/src/middle/region.rs index dfe71dc92577..94ca38c0e758 100644 --- a/compiler/rustc_middle/src/middle/region.rs +++ b/compiler/rustc_middle/src/middle/region.rs @@ -148,8 +148,7 @@ rustc_index::newtype_index! { /// * The subscope with `first_statement_index == 1` is scope of `c`, /// and thus does not include EXPR_2, but covers the `...`. #[derive(HashStable)] - pub struct FirstStatementIndex { - } + pub struct FirstStatementIndex {} } // compilation error if size of `ScopeData` is not the same as a `u32` diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index 8806077175b8..e7bb3ab0bc35 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -35,8 +35,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[max = 0xFFFF_FFFF] #[debug_format = "CounterValueReference({})"] - pub struct CounterValueReference { - } + pub struct CounterValueReference {} } impl CounterValueReference { @@ -59,8 +58,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[max = 0xFFFF_FFFF] #[debug_format = "InjectedExpressionId({})"] - pub struct InjectedExpressionId { - } + pub struct InjectedExpressionId {} } rustc_index::newtype_index! { @@ -70,8 +68,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[max = 0xFFFF_FFFF] #[debug_format = "InjectedExpressionIndex({})"] - pub struct InjectedExpressionIndex { - } + pub struct InjectedExpressionIndex {} } rustc_index::newtype_index! { @@ -81,8 +78,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[max = 0xFFFF_FFFF] #[debug_format = "MappedExpressionIndex({})"] - pub struct MappedExpressionIndex { - } + pub struct MappedExpressionIndex {} } impl From for ExpressionOperandId { diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 6057fdcaebba..454cfc6e65ba 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -657,7 +657,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[debug_format = "_{}"] pub struct Local { - const RETURN_PLACE = 0, + const RETURN_PLACE = 0; } } @@ -1149,7 +1149,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[debug_format = "bb{}"] pub struct BasicBlock { - const START_BLOCK = 0, + const START_BLOCK = 0; } } @@ -1532,8 +1532,7 @@ rustc_index::newtype_index! { /// [mir-datatypes]: https://rustc-dev-guide.rust-lang.org/mir/index.html#mir-data-types #[derive(HashStable)] #[debug_format = "field[{}]"] - pub struct Field { - } + pub struct Field {} } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -1760,7 +1759,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[debug_format = "scope[{}]"] pub struct SourceScope { - const OUTERMOST_SOURCE_SCOPE = 0, + const OUTERMOST_SOURCE_SCOPE = 0; } } @@ -2757,8 +2756,7 @@ impl<'tcx> TypeVisitable<'tcx> for UserTypeProjection { rustc_index::newtype_index! { #[derive(HashStable)] #[debug_format = "promoted[{}]"] - pub struct Promoted { - } + pub struct Promoted {} } impl<'tcx> Debug for Constant<'tcx> { diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 160e9691e69b..a8a4532223c2 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -132,8 +132,7 @@ pub struct UnsafetyCheckResult { rustc_index::newtype_index! { #[derive(HashStable)] #[debug_format = "_{}"] - pub struct GeneratorSavedLocal { - } + pub struct GeneratorSavedLocal {} } /// The layout of generator state. diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index ad8f873bf9b7..ac903010c8d3 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -36,8 +36,7 @@ macro_rules! thir_with_elements { newtype_index! { #[derive(HashStable)] #[debug_format = $format] - pub struct $id { - } + pub struct $id {} } )* diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 3f22b0e5a9a0..e13b68c83b57 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1379,8 +1379,7 @@ rustc_index::newtype_index! { /// A **region** (lifetime) **v**ariable **ID**. #[derive(HashStable)] #[debug_format = "'_#{}r"] - pub struct RegionVid { - } + pub struct RegionVid {} } impl Atom for RegionVid { @@ -1391,7 +1390,7 @@ impl Atom for RegionVid { rustc_index::newtype_index! { #[derive(HashStable)] - pub struct BoundVar { .. } + pub struct BoundVar {} } #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)] diff --git a/compiler/rustc_middle/src/ty/typeck_results.rs b/compiler/rustc_middle/src/ty/typeck_results.rs index 8483b1745c7e..136a4906c58d 100644 --- a/compiler/rustc_middle/src/ty/typeck_results.rs +++ b/compiler/rustc_middle/src/ty/typeck_results.rs @@ -611,7 +611,7 @@ rustc_index::newtype_index! { #[derive(HashStable)] #[debug_format = "UserType({})"] pub struct UserTypeAnnotationIndex { - const START_INDEX = 0, + const START_INDEX = 0; } } diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 7af89dd472f8..c785dfb500fc 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -372,7 +372,7 @@ struct CFG<'tcx> { } rustc_index::newtype_index! { - struct ScopeId { .. } + struct ScopeId {} } #[derive(Debug)] diff --git a/compiler/rustc_mir_build/src/build/scope.rs b/compiler/rustc_mir_build/src/build/scope.rs index 33f49ffdaf64..c92634a609de 100644 --- a/compiler/rustc_mir_build/src/build/scope.rs +++ b/compiler/rustc_mir_build/src/build/scope.rs @@ -185,7 +185,7 @@ pub(crate) enum BreakableTarget { } rustc_index::newtype_index! { - struct DropIdx { .. } + struct DropIdx {} } const ROOT_NODE: DropIdx = DropIdx::from_u32(0); diff --git a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs index f286aff8420c..9b053985bedf 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs @@ -15,8 +15,7 @@ mod abs_domain; rustc_index::newtype_index! { #[debug_format = "mp{}"] - pub struct MovePathIndex { - } + pub struct MovePathIndex {} } impl polonius_engine::Atom for MovePathIndex { @@ -27,14 +26,12 @@ impl polonius_engine::Atom for MovePathIndex { rustc_index::newtype_index! { #[debug_format = "mo{}"] - pub struct MoveOutIndex { - } + pub struct MoveOutIndex {} } rustc_index::newtype_index! { #[debug_format = "in{}"] - pub struct InitIndex { - } + pub struct InitIndex {} } impl MoveOutIndex { diff --git a/compiler/rustc_mir_transform/src/coverage/graph.rs b/compiler/rustc_mir_transform/src/coverage/graph.rs index 40ed6ceb839e..78d28f1ebab7 100644 --- a/compiler/rustc_mir_transform/src/coverage/graph.rs +++ b/compiler/rustc_mir_transform/src/coverage/graph.rs @@ -284,7 +284,7 @@ rustc_index::newtype_index! { /// A node in the control-flow graph of CoverageGraph. #[debug_format = "bcb{}"] pub(super) struct BasicCoverageBlock { - const START_BCB = 0, + const START_BCB = 0; } } diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index bfbe65b5bfc3..b49432b79962 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -109,14 +109,12 @@ mod rwu_table; rustc_index::newtype_index! { #[debug_format = "v({})"] - pub struct Variable { - } + pub struct Variable {} } rustc_index::newtype_index! { #[debug_format = "ln({})"] - pub struct LiveNode { - } + pub struct LiveNode {} } #[derive(Copy, Clone, PartialEq, Debug)] diff --git a/compiler/rustc_query_system/src/dep_graph/graph.rs b/compiler/rustc_query_system/src/dep_graph/graph.rs index 0e7d628c1eb6..52957ee02223 100644 --- a/compiler/rustc_query_system/src/dep_graph/graph.rs +++ b/compiler/rustc_query_system/src/dep_graph/graph.rs @@ -37,7 +37,7 @@ pub struct DepGraph { } rustc_index::newtype_index! { - pub struct DepNodeIndex { .. } + pub struct DepNodeIndex {} } impl DepNodeIndex { @@ -974,7 +974,7 @@ pub struct WorkProduct { // Index type for `DepNodeData`'s edges. rustc_index::newtype_index! { - struct EdgeIndex { .. } + struct EdgeIndex {} } /// `CurrentDepGraph` stores the dependency graph for the current session. It diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index fbc0aeb6c802..a918328d4130 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -28,8 +28,7 @@ use smallvec::SmallVec; // and use those bits to encode which index type it contains. rustc_index::newtype_index! { #[max = 0x7FFF_FFFF] - pub struct SerializedDepNodeIndex { - } + pub struct SerializedDepNodeIndex {} } /// Data for use when recompiling the **current crate**. diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 5d15757f1ab3..221f65b66e6d 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -12,8 +12,7 @@ use std::hash::{Hash, Hasher}; rustc_index::newtype_index! { #[custom_encodable] #[debug_format = "crate{}"] - pub struct CrateNum { - } + pub struct CrateNum {} } /// Item definitions in the currently-compiled crate would have the `CrateNum` @@ -199,7 +198,7 @@ rustc_index::newtype_index! { pub struct DefIndex { /// The crate root is always assigned index 0 by the AST Map code, /// thanks to `NodeCollector::new`. - const CRATE_DEF_INDEX = 0, + const CRATE_DEF_INDEX = 0; } } diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index f80b5d99df63..c2d8287f2431 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -62,8 +62,7 @@ pub struct SyntaxContextData { rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. #[custom_encodable] - pub struct ExpnIndex { - } + pub struct ExpnIndex {} } /// A unique ID associated with a macro invocation and expansion. @@ -85,8 +84,7 @@ rustc_index::newtype_index! { #[custom_encodable] #[no_ord_impl] #[debug_format = "expn{}"] - pub struct LocalExpnId { - } + pub struct LocalExpnId {} } // To ensure correctness of incremental compilation, diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index ace095736c92..8014ae9c511f 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1801,7 +1801,7 @@ impl fmt::Display for MacroRulesNormalizedIdent { pub struct Symbol(SymbolIndex); rustc_index::newtype_index! { - struct SymbolIndex { .. } + struct SymbolIndex {} } impl Symbol { diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs index c60de3cb0b2b..88a0a1f8ecfd 100644 --- a/compiler/rustc_target/src/abi/mod.rs +++ b/compiler/rustc_target/src/abi/mod.rs @@ -21,8 +21,7 @@ impl ToJson for Endian { rustc_index::newtype_index! { #[derive(HashStable_Generic)] - pub struct VariantIdx { - } + pub struct VariantIdx {} } #[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)] diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 5a6ee6721d38..dd36a5c7a216 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -303,7 +303,7 @@ rustc_index::newtype_index! { #[derive(HashStable_Generic)] #[debug_format = "DebruijnIndex({})"] pub struct DebruijnIndex { - const INNERMOST = 0, + const INNERMOST = 0; } } @@ -500,8 +500,7 @@ pub struct FloatVarValue(pub FloatTy); rustc_index::newtype_index! { /// A **ty**pe **v**ariable **ID**. #[debug_format = "_#{}t"] - pub struct TyVid { - } + pub struct TyVid {} } /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**. @@ -789,8 +788,7 @@ rustc_index::newtype_index! { /// use for checking generic functions. #[derive(HashStable_Generic)] #[debug_format = "U{}"] - pub struct UniverseIndex { - } + pub struct UniverseIndex {} } impl UniverseIndex { From dd8897eb635abc1bb9e64876eae54e449df951ba Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 18 Dec 2022 21:41:08 +0000 Subject: [PATCH 198/321] Don't ICE in closure arg borrow suggestion --- .../src/traits/error_reporting/suggestions.rs | 8 +++---- .../suggestions/enum-variant-arg-mismatch.rs | 10 +++++++++ .../enum-variant-arg-mismatch.stderr | 22 +++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/suggestions/enum-variant-arg-mismatch.rs create mode 100644 src/test/ui/suggestions/enum-variant-arg-mismatch.stderr diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index d47a5ea3e370..6cab2db3e80e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1789,7 +1789,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { self.note_conflicting_closure_bounds(cause, &mut err); if let Some(found_node) = found_node { - hint_missing_borrow(span, found_span, found, expected, found_node, &mut err); + hint_missing_borrow(span, found, expected, found_node, &mut err); } err @@ -3455,7 +3455,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { /// Add a hint to add a missing borrow or remove an unnecessary one. fn hint_missing_borrow<'tcx>( span: Span, - found_span: Span, found: Ty<'tcx>, expected: Ty<'tcx>, found_node: Node<'_>, @@ -3474,9 +3473,8 @@ fn hint_missing_borrow<'tcx>( } }; - let fn_decl = found_node - .fn_decl() - .unwrap_or_else(|| span_bug!(found_span, "found node must be a function")); + // This could be a variant constructor, for example. + let Some(fn_decl) = found_node.fn_decl() else { return; }; let arg_spans = fn_decl.inputs.iter().map(|ty| ty.span); diff --git a/src/test/ui/suggestions/enum-variant-arg-mismatch.rs b/src/test/ui/suggestions/enum-variant-arg-mismatch.rs new file mode 100644 index 000000000000..8de5bae92fc2 --- /dev/null +++ b/src/test/ui/suggestions/enum-variant-arg-mismatch.rs @@ -0,0 +1,10 @@ +pub enum Sexpr<'a> { + Ident(&'a str), +} + +fn map<'a, F: Fn(String) -> Sexpr<'a>>(f: F) {} + +fn main() { + map(Sexpr::Ident); + //~^ ERROR type mismatch in function arguments +} diff --git a/src/test/ui/suggestions/enum-variant-arg-mismatch.stderr b/src/test/ui/suggestions/enum-variant-arg-mismatch.stderr new file mode 100644 index 000000000000..f76019b70001 --- /dev/null +++ b/src/test/ui/suggestions/enum-variant-arg-mismatch.stderr @@ -0,0 +1,22 @@ +error[E0631]: type mismatch in function arguments + --> $DIR/enum-variant-arg-mismatch.rs:8:9 + | +LL | Ident(&'a str), + | ----- found signature defined here +... +LL | map(Sexpr::Ident); + | --- ^^^^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(String) -> _` + found function signature `fn(&str) -> _` +note: required by a bound in `map` + --> $DIR/enum-variant-arg-mismatch.rs:5:15 + | +LL | fn map<'a, F: Fn(String) -> Sexpr<'a>>(f: F) {} + | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `map` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0631`. From 1da4a499122f59201cf761b79f9e8c6ded1cb91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 18 Dec 2022 12:45:56 +0100 Subject: [PATCH 199/321] clippy::complexity fixes filter_next needless_question_mark bind_instead_of_map manual_find derivable_impls map_identity redundant_slicing skip_while_next unnecessary_unwrap needless_bool --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- compiler/rustc_borrowck/src/lib.rs | 7 +----- compiler/rustc_codegen_llvm/src/builder.rs | 4 ++-- .../rustc_codegen_ssa/src/back/archive.rs | 2 +- .../src/fn_ctxt/suggestions.rs | 6 ++--- .../src/infer/lexical_region_resolve/mod.rs | 2 +- compiler/rustc_infer/src/infer/undo_log.rs | 8 +------ compiler/rustc_middle/src/middle/privacy.rs | 7 +----- compiler/rustc_mir_transform/src/sroa.rs | 2 +- compiler/rustc_monomorphize/src/collector.rs | 4 ++-- compiler/rustc_parse/src/parser/path.rs | 3 +-- compiler/rustc_passes/src/dead.rs | 1 - compiler/rustc_session/src/filesearch.rs | 4 ++-- .../src/typeid/typeid_itanium_cxx_abi.rs | 22 +++++++------------ .../src/maybe_transmutable/query_context.rs | 6 +---- 15 files changed, 26 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d67ede6e1302..d3a488224135 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -620,7 +620,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.impl_trait_defs = current_impl_trait_defs; self.impl_trait_bounds = current_impl_trait_bounds; - debug_assert!(self.children.iter().find(|(id, _)| id == &def_id).is_none()); + debug_assert!(!self.children.iter().any(|(id, _)| id == &def_id)); self.children.push((def_id, hir::MaybeOwner::Owner(info))); } diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 5289de9b0abf..d8c22fbe59f0 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -2059,12 +2059,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ) -> Option { let mpi = self.move_data.rev_lookup.find_local(local); let ii = &self.move_data.init_path_map[mpi]; - for &index in ii { - if flow_state.ever_inits.contains(index) { - return Some(index); - } - } - None + ii.into_iter().find(|&&index| flow_state.ever_inits.contains(index)).copied() } /// Adds the place into the used mutable variables set diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 853a8b82853f..5bf45a81e434 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -233,8 +233,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { // Set KCFI operand bundle let is_indirect_call = unsafe { llvm::LLVMIsAFunction(llfn).is_none() }; let kcfi_bundle = - if self.tcx.sess.is_sanitizer_kcfi_enabled() && fn_abi.is_some() && is_indirect_call { - let kcfi_typeid = kcfi_typeid_for_fnabi(self.tcx, fn_abi.unwrap()); + if self.tcx.sess.is_sanitizer_kcfi_enabled() && let Some(fn_abi) = fn_abi && is_indirect_call { + let kcfi_typeid = kcfi_typeid_for_fnabi(self.tcx, fn_abi); Some(llvm::OperandBundleDef::new("kcfi", &[self.const_u32(kcfi_typeid)])) } else { None diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index 5266d8858d47..6eb120157da0 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -123,7 +123,7 @@ fn try_filter_fat_archs<'a>( ) -> io::Result> { let archs = archs.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; - let desired = match archs.iter().filter(|a| a.architecture() == target_arch).next() { + let desired = match archs.iter().find(|a| a.architecture() == target_arch) { Some(a) => a, None => return Ok(None), }; diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 407d6ac8544c..c927d089e645 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1128,9 +1128,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } let hir = self.tcx.hir(); - let cond_parent = hir.parent_iter(expr.hir_id).skip_while(|(_, node)| { - matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(op, _, _), .. }) if op.node == hir::BinOpKind::And) - }).next(); + let cond_parent = hir.parent_iter(expr.hir_id).find(|(_, node)| { + !matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(op, _, _), .. }) if op.node == hir::BinOpKind::And) + }); // Don't suggest: // `let Some(_) = a.is_some() && b` // ++++++++++ diff --git a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs index ba990acfe6fc..da2c6fbc05f5 100644 --- a/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs +++ b/compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs @@ -488,7 +488,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> { // If this empty region is from a universe that can // name the placeholder, then the placeholder is // larger; otherwise, the only ancestor is `'static`. - if a_ui.can_name(placeholder.universe) { true } else { false } + return a_ui.can_name(placeholder.universe); } } } diff --git a/compiler/rustc_infer/src/infer/undo_log.rs b/compiler/rustc_infer/src/infer/undo_log.rs index 611961ab1cc9..955c54e85157 100644 --- a/compiler/rustc_infer/src/infer/undo_log.rs +++ b/compiler/rustc_infer/src/infer/undo_log.rs @@ -87,18 +87,12 @@ impl<'tcx> Rollback> for InferCtxtInner<'tcx> { /// The combined undo log for all the various unification tables. For each change to the storage /// for any kind of inference variable, we record an UndoLog entry in the vector here. -#[derive(Clone)] +#[derive(Clone, Default)] pub(crate) struct InferCtxtUndoLogs<'tcx> { logs: Vec>, num_open_snapshots: usize, } -impl Default for InferCtxtUndoLogs<'_> { - fn default() -> Self { - Self { logs: Default::default(), num_open_snapshots: Default::default() } - } -} - /// The UndoLogs trait defines how we undo a particular kind of action (of type T). We can undo any /// action that is convertible into an UndoLog (per the From impls above). impl<'tcx, T> UndoLogs for InferCtxtUndoLogs<'tcx> diff --git a/compiler/rustc_middle/src/middle/privacy.rs b/compiler/rustc_middle/src/middle/privacy.rs index fc08d58cc406..0e18ba73d712 100644 --- a/compiler/rustc_middle/src/middle/privacy.rs +++ b/compiler/rustc_middle/src/middle/privacy.rs @@ -103,12 +103,7 @@ impl EffectiveVisibilities { pub fn public_at_level(&self, id: LocalDefId) -> Option { self.effective_vis(id).and_then(|effective_vis| { - for level in Level::all_levels() { - if effective_vis.is_public_at_level(level) { - return Some(level); - } - } - None + Level::all_levels().into_iter().find(|&level| effective_vis.is_public_at_level(level)) }) } diff --git a/compiler/rustc_mir_transform/src/sroa.rs b/compiler/rustc_mir_transform/src/sroa.rs index 558a372fb1e8..3a2bf0515165 100644 --- a/compiler/rustc_mir_transform/src/sroa.rs +++ b/compiler/rustc_mir_transform/src/sroa.rs @@ -182,7 +182,7 @@ fn replace_flattened_locals<'tcx>( let mut fragments = IndexVec::new(); for (k, v) in &replacements.fields { fragments.ensure_contains_elem(k.local, || Vec::new()); - fragments[k.local].push((&k.projection[..], *v)); + fragments[k.local].push((k.projection, *v)); } debug!(?fragments); diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 10ea4d29cfe4..deeeb9af4eb9 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -595,8 +595,8 @@ fn check_recursion_limit<'tcx>( let def_path_str = tcx.def_path_str(def_id); let (shrunk, written_to_path) = shrunk_instance_name(tcx, &instance); let mut path = PathBuf::new(); - let was_written = if written_to_path.is_some() { - path = written_to_path.unwrap(); + let was_written = if let Some(written_to_path) = written_to_path { + path = written_to_path; Some(()) } else { None diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 2d432e3f5bd6..5333d3b8587d 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -277,8 +277,7 @@ impl<'a> Parser<'a> { if let Some(arg) = args .iter() .rev() - .skip_while(|arg| matches!(arg, AngleBracketedArg::Constraint(_))) - .next() + .find(|arg| !matches!(arg, AngleBracketedArg::Constraint(_))) { err.span_suggestion_verbose( arg.span().shrink_to_hi(), diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index a71ae717a508..f5556738bff9 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -787,7 +787,6 @@ impl<'tcx> DeadVisitor<'tcx> { let mut dead_codes = dead_codes .iter() .filter(|v| !v.name.as_str().starts_with('_')) - .map(|v| v) .collect::>(); if dead_codes.is_empty() { return; diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index 1855a49c1ecd..6f1b31ff9c3a 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -122,7 +122,7 @@ pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> { let target = crate::config::host_triple(); let mut sysroot_candidates: SmallVec<[PathBuf; 2]> = smallvec![get_or_default_sysroot().expect("Failed finding sysroot")]; - let path = current_dll_path().and_then(|s| Ok(s.canonicalize().map_err(|e| e.to_string())?)); + let path = current_dll_path().and_then(|s| s.canonicalize().map_err(|e| e.to_string())); if let Ok(dll) = path { // use `parent` twice to chop off the file name and then also the // directory containing the dll which should be either `lib` or `bin`. @@ -165,7 +165,7 @@ pub fn get_or_default_sysroot() -> Result { } fn default_from_rustc_driver_dll() -> Result { - let dll = current_dll_path().and_then(|s| Ok(canonicalize(s)))?; + let dll = current_dll_path().map(|s| canonicalize(s))?; // `dll` will be in one of the following two: // - compiler's libdir: $sysroot/lib/*.dll diff --git a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs index c9ddb084d63a..0845b1b6b096 100644 --- a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs +++ b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs @@ -99,13 +99,8 @@ fn is_c_void_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { ty::Adt(adt_def, ..) => { let def_id = adt_def.0.did; let crate_name = tcx.crate_name(def_id.krate); - if tcx.item_name(def_id).as_str() == "c_void" + tcx.item_name(def_id).as_str() == "c_void" && (crate_name == sym::core || crate_name == sym::std || crate_name == sym::libc) - { - true - } else { - false - } } _ => false, } @@ -267,8 +262,7 @@ fn encode_predicates<'tcx>( ) -> String { // E as part of vendor extended type let mut s = String::new(); - let predicates: Vec> = - predicates.iter().map(|predicate| predicate).collect(); + let predicates: Vec> = predicates.iter().collect(); for predicate in predicates { s.push_str(&encode_predicate(tcx, predicate, dict, options)); } @@ -322,7 +316,7 @@ fn encode_substs<'tcx>( ) -> String { // [IE] as part of vendor extended type let mut s = String::new(); - let substs: Vec> = substs.iter().map(|subst| subst).collect(); + let substs: Vec> = substs.iter().collect(); if !substs.is_empty() { s.push('I'); for subst in substs { @@ -703,11 +697,8 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio tcx.layout_of(param_env.and(ty)).map_or(false, |layout| layout.is_zst()); !is_zst }); - if field.is_none() { - // Transform repr(transparent) types without non-ZST field into () - ty = tcx.mk_unit(); - } else { - let ty0 = tcx.type_of(field.unwrap().did); + if let Some(field) = field { + let ty0 = tcx.type_of(field.did); // Generalize any repr(transparent) user-defined type that is either a pointer // or reference, and either references itself or any other type that contains or // references itself, to avoid a reference cycle. @@ -720,6 +711,9 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio } else { ty = transform_ty(tcx, ty0, options); } + } else { + // Transform repr(transparent) types without non-ZST field into () + ty = tcx.mk_unit(); } } else { ty = tcx.mk_adt(*adt_def, transform_substs(tcx, substs, options)); diff --git a/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs b/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs index adab343ac98a..e4f3e7928da5 100644 --- a/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs +++ b/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs @@ -76,11 +76,7 @@ mod rustc { } }; - let ret = if self.visibility(def_id).is_accessible_from(parent, *self) { - true - } else { - false - }; + let ret: bool = self.visibility(def_id).is_accessible_from(parent, *self); trace!(?ret, "ret"); ret From 4ab78da50e494d16fc386b865951dbf9db5c68be Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sun, 18 Dec 2022 23:39:39 +0000 Subject: [PATCH 200/321] Update cargo 11 commits in cc0a320879c17207bbfb96b5d778e28a2c62030d..c994a4a638370bc7e0ffcbb0e2865afdfa7d4415 2022-12-14 14:46:57 +0000 to 2022-12-18 21:50:58 +0000 - Fix examples of proc-macro crates being scraped for examples (rust-lang/cargo#11497) - Enable triagebot's relabel functionality (rust-lang/cargo#11498) - Revert "temporarily disable test `lto::test_profile`" (rust-lang/cargo#11495) - Bump to 0.69.0, update changelog (rust-lang/cargo#11493) - Fix typo (rust-lang/cargo#11491) - Display CPU info in CI (rust-lang/cargo#11488) - Fix collision_doc_profile test error (rust-lang/cargo#11489) - fix: Make auto-fix note work with `clippy` (rust-lang/cargo#11399) - fix(add): use the possessive in error message (rust-lang/cargo#11483) - Document home crate in contrib docs (rust-lang/cargo#11481) - Split up registry documentation into multiple sections (rust-lang/cargo#11480) --- Cargo.lock | 4 ++-- src/tools/cargo | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cce203ef728f..1d9a9240ccf2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,7 +294,7 @@ dependencies = [ [[package]] name = "cargo" -version = "0.68.0" +version = "0.69.0" dependencies = [ "anyhow", "bytesize", @@ -5407,7 +5407,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "rand 0.8.5", "static_assertions", ] diff --git a/src/tools/cargo b/src/tools/cargo index cc0a320879c1..c994a4a63837 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit cc0a320879c17207bbfb96b5d778e28a2c62030d +Subproject commit c994a4a638370bc7e0ffcbb0e2865afdfa7d4415 From 5fb2d63d0793a4c3c612d1e4a4ca6a184e6fe614 Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 19 Dec 2022 08:48:30 +0100 Subject: [PATCH 201/321] Fix `uninlined_format_args` in libtest --- library/test/src/cli.rs | 12 ++++-------- library/test/src/formatters/json.rs | 7 +++---- library/test/src/formatters/mod.rs | 2 +- library/test/src/formatters/pretty.rs | 2 +- library/test/src/lib.rs | 5 ++--- library/test/src/test_result.rs | 5 ++--- library/test/src/time.rs | 6 ++---- 7 files changed, 15 insertions(+), 24 deletions(-) diff --git a/library/test/src/cli.rs b/library/test/src/cli.rs index 524658bce139..796796e07a9c 100644 --- a/library/test/src/cli.rs +++ b/library/test/src/cli.rs @@ -354,8 +354,7 @@ fn get_shuffle_seed(matches: &getopts::Matches, allow_unstable: bool) -> OptPart Err(e) => { return Err(format!( "argument for --shuffle-seed must be a number \ - (error: {})", - e + (error: {e})" )); } }, @@ -383,8 +382,7 @@ fn get_test_threads(matches: &getopts::Matches) -> OptPartRes> { Err(e) => { return Err(format!( "argument for --test-threads must be a number > 0 \ - (error: {})", - e + (error: {e})" )); } }, @@ -418,8 +416,7 @@ fn get_format( Some(v) => { return Err(format!( "argument for --format must be pretty, terse, json or junit (was \ - {})", - v + {v})" )); } }; @@ -436,8 +433,7 @@ fn get_color_config(matches: &getopts::Matches) -> OptPartRes { Some(v) => { return Err(format!( "argument for --color must be auto, always, or never (was \ - {})", - v + {v})" )); } }; diff --git a/library/test/src/formatters/json.rs b/library/test/src/formatters/json.rs index c07fdafb167c..5526aadb67ff 100644 --- a/library/test/src/formatters/json.rs +++ b/library/test/src/formatters/json.rs @@ -53,7 +53,7 @@ impl JsonFormatter { self.write_message(&*format!(r#", "stdout": "{}""#, EscapedString(stdout)))?; } if let Some(extra) = extra { - self.write_message(&*format!(r#", {}"#, extra))?; + self.write_message(&*format!(r#", {extra}"#))?; } self.writeln_message(" }") } @@ -62,13 +62,12 @@ impl JsonFormatter { impl OutputFormatter for JsonFormatter { fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option) -> io::Result<()> { let shuffle_seed_json = if let Some(shuffle_seed) = shuffle_seed { - format!(r#", "shuffle_seed": {}"#, shuffle_seed) + format!(r#", "shuffle_seed": {shuffle_seed}"#) } else { String::new() }; self.writeln_message(&*format!( - r#"{{ "type": "suite", "event": "started", "test_count": {}{} }}"#, - test_count, shuffle_seed_json + r#"{{ "type": "suite", "event": "started", "test_count": {test_count}{shuffle_seed_json} }}"# )) } diff --git a/library/test/src/formatters/mod.rs b/library/test/src/formatters/mod.rs index cb80859759fa..cb67b6491a39 100644 --- a/library/test/src/formatters/mod.rs +++ b/library/test/src/formatters/mod.rs @@ -38,5 +38,5 @@ pub(crate) fn write_stderr_delimiter(test_output: &mut Vec, test_name: &Test Some(_) => test_output.push(b'\n'), None => (), } - writeln!(test_output, "---- {} stderr ----", test_name).unwrap(); + writeln!(test_output, "---- {test_name} stderr ----").unwrap(); } diff --git a/library/test/src/formatters/pretty.rs b/library/test/src/formatters/pretty.rs index 694202229802..0299c8b54335 100644 --- a/library/test/src/formatters/pretty.rs +++ b/library/test/src/formatters/pretty.rs @@ -47,7 +47,7 @@ impl PrettyFormatter { pub fn write_ignored(&mut self, message: Option<&'static str>) -> io::Result<()> { if let Some(message) = message { - self.write_short_result(&format!("ignored, {}", message), term::color::YELLOW) + self.write_short_result(&format!("ignored, {message}"), term::color::YELLOW) } else { self.write_short_result("ignored", term::color::YELLOW) } diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index 256c9e8d141e..f6a41bbb88c8 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -213,8 +213,7 @@ pub fn assert_test_result(result: T) -> Result<(), String> { } else { Err(format!( "the test returned a termination value with a non-zero status code \ - ({}) which indicates a failure", - code + ({code}) which indicates a failure" )) } } @@ -750,7 +749,7 @@ fn spawn_test_subprocess( })() { Ok(r) => r, Err(e) => { - write!(&mut test_output, "Unexpected error: {}", e).unwrap(); + write!(&mut test_output, "Unexpected error: {e}").unwrap(); TrFailed } }; diff --git a/library/test/src/test_result.rs b/library/test/src/test_result.rs index 7f44d6e3d0f1..7c5b0d6c0f72 100644 --- a/library/test/src/test_result.rs +++ b/library/test/src/test_result.rs @@ -44,9 +44,8 @@ pub fn calc_result<'a>( } else if let Some(panic_str) = maybe_panic_str { TestResult::TrFailedMsg(format!( r#"panic did not contain expected string - panic message: `{:?}`, - expected substring: `{:?}`"#, - panic_str, msg + panic message: `{panic_str:?}`, + expected substring: `{msg:?}`"# )) } else { TestResult::TrFailedMsg(format!( diff --git a/library/test/src/time.rs b/library/test/src/time.rs index 8c64e5d1b733..7fd69d7f7e73 100644 --- a/library/test/src/time.rs +++ b/library/test/src/time.rs @@ -107,16 +107,14 @@ impl TimeThreshold { let durations_str = env::var(env_var_name).ok()?; let (warn_str, critical_str) = durations_str.split_once(',').unwrap_or_else(|| { panic!( - "Duration variable {} expected to have 2 numbers separated by comma, but got {}", - env_var_name, durations_str + "Duration variable {env_var_name} expected to have 2 numbers separated by comma, but got {durations_str}" ) }); let parse_u64 = |v| { u64::from_str(v).unwrap_or_else(|_| { panic!( - "Duration value in variable {} is expected to be a number, but got {}", - env_var_name, v + "Duration value in variable {env_var_name} is expected to be a number, but got {v}" ) }) }; From 5ecac8ede670ac8045a199cd3bcd58bd940ec8d4 Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Mon, 19 Dec 2022 22:50:31 +1300 Subject: [PATCH 202/321] more markdown list formatting Co-authored-by: Guillaume Gomez --- compiler/rustc_error_codes/src/error_codes/E0460.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0460.md b/compiler/rustc_error_codes/src/error_codes/E0460.md index 1687682b28ec..001678a9bce0 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0460.md +++ b/compiler/rustc_error_codes/src/error_codes/E0460.md @@ -64,8 +64,8 @@ dependencies, without using the compiler's own dependency management that causes this issue. This error can be fixed by: -* Using [Cargo], the Rust package manager, automatically fixing this issue. -* Recompiling crate `a` so that both crate `b` and `main` have a uniform -version to depend on. + * Using [Cargo], the Rust package manager, automatically fixing this issue. + * Recompiling crate `a` so that both crate `b` and `main` have a uniform + version to depend on. [Cargo]: ../cargo/index.html From e62b75ef5f666de561726e44738de63ae4aa7726 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Dec 2022 11:51:46 +0000 Subject: [PATCH 203/321] Test that we don't add a new kind of breaking change with TAITs --- .../coherence_cross_crate_trait_decl.rs | 9 +++++++ .../coherence_cross_crate.rs | 24 +++++++++++++++++++ .../coherence_cross_crate.stderr | 13 ++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/test/ui/type-alias-impl-trait/auxiliary/coherence_cross_crate_trait_decl.rs create mode 100644 src/test/ui/type-alias-impl-trait/coherence_cross_crate.rs create mode 100644 src/test/ui/type-alias-impl-trait/coherence_cross_crate.stderr diff --git a/src/test/ui/type-alias-impl-trait/auxiliary/coherence_cross_crate_trait_decl.rs b/src/test/ui/type-alias-impl-trait/auxiliary/coherence_cross_crate_trait_decl.rs new file mode 100644 index 000000000000..712ed55438e6 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/auxiliary/coherence_cross_crate_trait_decl.rs @@ -0,0 +1,9 @@ +pub trait SomeTrait {} + +impl SomeTrait for () {} + +// Adding this `impl` would cause errors in this crate's dependent, +// so it would be a breaking change. We explicitly don't add this impl, +// as the dependent crate already assumes this impl exists and thus already +// does not compile. +//impl SomeTrait for i32 {} diff --git a/src/test/ui/type-alias-impl-trait/coherence_cross_crate.rs b/src/test/ui/type-alias-impl-trait/coherence_cross_crate.rs new file mode 100644 index 000000000000..a63e0a1ee6f7 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/coherence_cross_crate.rs @@ -0,0 +1,24 @@ +// aux-build: coherence_cross_crate_trait_decl.rs +// This test ensures that adding an `impl SomeTrait for i32` within +// `coherence_cross_crate_trait_decl` is not a breaking change, by +// making sure that even without such an impl this test fails to compile. + +#![feature(type_alias_impl_trait)] + +extern crate coherence_cross_crate_trait_decl; + +use coherence_cross_crate_trait_decl::SomeTrait; + +trait OtherTrait {} + +type Alias = impl SomeTrait; + +fn constrain() -> Alias { + () +} + +impl OtherTrait for Alias {} +impl OtherTrait for i32 {} +//~^ ERROR: conflicting implementations of trait `OtherTrait` for type `Alias` + +fn main() {} diff --git a/src/test/ui/type-alias-impl-trait/coherence_cross_crate.stderr b/src/test/ui/type-alias-impl-trait/coherence_cross_crate.stderr new file mode 100644 index 000000000000..63a3ce29cc77 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/coherence_cross_crate.stderr @@ -0,0 +1,13 @@ +error[E0119]: conflicting implementations of trait `OtherTrait` for type `Alias` + --> $DIR/coherence_cross_crate.rs:21:1 + | +LL | impl OtherTrait for Alias {} + | ------------------------- first implementation here +LL | impl OtherTrait for i32 {} + | ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Alias` + | + = note: upstream crates may add a new impl of trait `coherence_cross_crate_trait_decl::SomeTrait` for type `i32` in future versions + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0119`. From 7fc6b0c9f390bdfefeb484a45a4e7faca2d33d1e Mon Sep 17 00:00:00 2001 From: ch-iv Date: Tue, 8 Nov 2022 09:27:32 -0500 Subject: [PATCH 204/321] docs: improve pin docs Co-authored-by: <@ch-iv> --- library/core/src/pin.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 4524fa4c48d2..3f8acc8505ff 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -485,6 +485,16 @@ impl> Pin

{ /// /// Unlike `Pin::new_unchecked`, this method is safe because the pointer /// `P` dereferences to an [`Unpin`] type, which cancels the pinning guarantees. + /// + /// # Examples + /// + /// ``` + /// use std::pin::Pin; + /// + /// let mut val: u8 = 5; + /// // We can pin the value, since it doesn't care about being moved + /// let mut pinned: Pin<&mut u8> = Pin::new(&mut val); + /// ``` #[inline(always)] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin", since = "1.33.0")] @@ -496,8 +506,20 @@ impl> Pin

{ /// Unwraps this `Pin

` returning the underlying pointer. /// - /// This requires that the data inside this `Pin` is [`Unpin`] so that we + /// This requires that the data inside this `Pin` implements [`Unpin`] so that we /// can ignore the pinning invariants when unwrapping it. + /// + /// # Examples + /// + /// ``` + /// use std::pin::Pin; + /// + /// let mut val: u8 = 5; + /// let pinned: Pin<&mut u8> = Pin::new(&mut val); + /// // Unwrap the pin to get a reference to the value + /// let r = Pin::into_inner(pinned); + /// assert_eq!(*r, 5); + /// ``` #[inline(always)] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin_into_inner", since = "1.39.0")] @@ -707,6 +729,18 @@ impl Pin

{ /// /// This overwrites pinned data, but that is okay: its destructor gets /// run before being overwritten, so no pinning guarantee is violated. + /// + /// # Example + /// + /// ``` + /// use std::pin::Pin; + /// + /// let mut val: u8 = 5; + /// let mut pinned: Pin<&mut u8> = Pin::new(&mut val); + /// println!("{}", pinned); // 5 + /// pinned.as_mut().set(10); + /// println!("{}", pinned); // 10 + /// ``` #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] pub fn set(&mut self, value: P::Target) From e5c92bc2b6f0bd69856ad3c4d3056c7f0c0ad72d Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Dec 2022 14:48:45 +0000 Subject: [PATCH 205/321] Don't panic on stable since miri is not available there --- src/bootstrap/install.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/bootstrap/install.rs b/src/bootstrap/install.rs index c53d0d7e4cb7..1815a0973072 100644 --- a/src/bootstrap/install.rs +++ b/src/bootstrap/install.rs @@ -200,10 +200,14 @@ install!((self, builder, _config), install_sh(builder, "clippy", self.compiler.stage, Some(self.target), &tarball); }; Miri, alias = "miri", Self::should_build(_config), only_hosts: true, { - let tarball = builder - .ensure(dist::Miri { compiler: self.compiler, target: self.target }) - .expect("missing miri"); - install_sh(builder, "miri", self.compiler.stage, Some(self.target), &tarball); + if let Some(tarball) = builder.ensure(dist::Miri { compiler: self.compiler, target: self.target }) { + install_sh(builder, "miri", self.compiler.stage, Some(self.target), &tarball); + } else { + // Miri is only available on nightly + builder.info( + &format!("skipping Install miri stage{} ({})", self.compiler.stage, self.target), + ); + } }; LlvmTools, alias = "llvm-tools", Self::should_build(_config), only_hosts: true, { let tarball = builder From 8275d115fbc86a95a483fe3a344387b8b3883a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 19 Dec 2022 15:30:43 +0000 Subject: [PATCH 206/321] Revert "Auto merge of #103880 - b-naber:field-ty-mir, r=lcnr" This reverts commit 03770f0e2b60c02db8fcf52fed5fb36aac70cedc, reversing changes made to 01ef4b21dc5251b58bd9c6fd6face2ae95d56da1. --- compiler/rustc_middle/src/mir/mod.rs | 4 +- compiler/rustc_middle/src/mir/syntax.rs | 39 +- compiler/rustc_middle/src/mir/tcx.rs | 15 +- .../src/build/expr/as_place.rs | 357 ++++-------------- .../src/build/expr/as_rvalue.rs | 9 +- .../rustc_mir_build/src/build/expr/into.rs | 8 +- .../src/build/matches/simplify.rs | 6 +- .../rustc_mir_build/src/build/matches/test.rs | 3 +- .../rustc_mir_build/src/build/matches/util.rs | 11 +- .../src/move_paths/abs_domain.rs | 2 +- .../rustc_mir_dataflow/src/value_analysis.rs | 4 +- src/test/ui/mir/field-projection-invariant.rs | 24 -- src/test/ui/mir/field-ty-ascription-enums.rs | 15 - src/test/ui/mir/field-ty-ascription.rs | 37 -- 14 files changed, 100 insertions(+), 434 deletions(-) delete mode 100644 src/test/ui/mir/field-projection-invariant.rs delete mode 100644 src/test/ui/mir/field-ty-ascription-enums.rs delete mode 100644 src/test/ui/mir/field-ty-ascription.rs diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index e909b2f74aa1..bdaa586c6987 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -1488,7 +1488,7 @@ impl<'tcx> StatementKind<'tcx> { /////////////////////////////////////////////////////////////////////////// // Places -impl ProjectionElem { +impl ProjectionElem { /// Returns `true` if the target of this projection may refer to a different region of memory /// than the base. fn is_indirect(&self) -> bool { @@ -1517,7 +1517,7 @@ impl ProjectionElem { /// Alias for projections as they appear in `UserTypeProjection`, where we /// need neither the `V` parameter for `Index` nor the `T` for `Field`. -pub type ProjectionKind = ProjectionElem<(), (), ()>; +pub type ProjectionKind = ProjectionElem<(), ()>; rustc_index::newtype_index! { /// A [newtype'd][wrapper] index type in the MIR [control-flow graph][CFG] diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index a6ca04f5e627..99e59c770d75 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -890,18 +890,11 @@ pub struct Place<'tcx> { pub projection: &'tcx List>, } -/// The different kinds of projections that can be used in the projection of a `Place`. -/// -/// `T1` is the generic type for a field projection. For an actual projection on a `Place` -/// this parameter will always be `Ty`, but the field type can be unavailable when -/// building (by using `PlaceBuilder`) places that correspond to upvars. -/// `T2` is the generic type for an `OpaqueCast` (is generic since it's abstracted over -/// in dataflow analysis, see `AbstractElem`). #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)] -pub enum ProjectionElem { +pub enum ProjectionElem { Deref, - Field(Field, T1), + Field(Field, T), /// Index into a slice/array. /// /// Note that this does not also dereference, and so it does not exactly correspond to slice @@ -957,36 +950,12 @@ pub enum ProjectionElem { /// Like an explicit cast from an opaque type to a concrete type, but without /// requiring an intermediate variable. - OpaqueCast(T2), + OpaqueCast(T), } /// Alias for projections as they appear in places, where the base is a place /// and the index is a local. -pub type PlaceElem<'tcx> = ProjectionElem, Ty<'tcx>>; - -/// Alias for projections that appear in `PlaceBuilder::Upvar`, for which -/// we cannot provide any field types. -pub type UpvarProjectionElem<'tcx> = ProjectionElem>; - -impl<'tcx> From> for UpvarProjectionElem<'tcx> { - fn from(elem: PlaceElem<'tcx>) -> Self { - match elem { - ProjectionElem::Deref => ProjectionElem::Deref, - ProjectionElem::Field(field, _) => ProjectionElem::Field(field, ()), - ProjectionElem::Index(v) => ProjectionElem::Index(v), - ProjectionElem::ConstantIndex { offset, min_length, from_end } => { - ProjectionElem::ConstantIndex { offset, min_length, from_end } - } - ProjectionElem::Subslice { from, to, from_end } => { - ProjectionElem::Subslice { from, to, from_end } - } - ProjectionElem::Downcast(opt_sym, variant_idx) => { - ProjectionElem::Downcast(opt_sym, variant_idx) - } - ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), - } - } -} +pub type PlaceElem<'tcx> = ProjectionElem>; /////////////////////////////////////////////////////////////////////////// // Operands diff --git a/compiler/rustc_middle/src/mir/tcx.rs b/compiler/rustc_middle/src/mir/tcx.rs index 1e289fc4abec..8d2a8f33d6aa 100644 --- a/compiler/rustc_middle/src/mir/tcx.rs +++ b/compiler/rustc_middle/src/mir/tcx.rs @@ -28,8 +28,8 @@ impl<'tcx> PlaceTy<'tcx> { /// `place_ty.field_ty(tcx, f)` computes the type at a given field /// of a record or enum-variant. (Most clients of `PlaceTy` can /// instead just extract the relevant type directly from their - /// `PlaceElem`, but some instances of `ProjectionElem` do - /// not carry a `Ty` for `T1` or `T2`.) + /// `PlaceElem`, but some instances of `ProjectionElem` do + /// not carry a `Ty` for `T`.) /// /// Note that the resulting type has not been normalized. pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: Field) -> Ty<'tcx> { @@ -64,18 +64,17 @@ impl<'tcx> PlaceTy<'tcx> { /// `Ty` or downcast variant corresponding to that projection. /// The `handle_field` callback must map a `Field` to its `Ty`, /// (which should be trivial when `T` = `Ty`). - pub fn projection_ty_core( + pub fn projection_ty_core( self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, - elem: &ProjectionElem, - mut handle_field: impl FnMut(&Self, Field, T1) -> Ty<'tcx>, - mut handle_opaque_cast: impl FnMut(&Self, T2) -> Ty<'tcx>, + elem: &ProjectionElem, + mut handle_field: impl FnMut(&Self, Field, T) -> Ty<'tcx>, + mut handle_opaque_cast: impl FnMut(&Self, T) -> Ty<'tcx>, ) -> PlaceTy<'tcx> where V: ::std::fmt::Debug, - T1: ::std::fmt::Debug + Copy, - T2: ::std::fmt::Debug + Copy, + T: ::std::fmt::Debug + Copy, { if self.variant_index.is_some() && !matches!(elem, ProjectionElem::Field(..)) { bug!("cannot use non field projection on downcasted place") diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 23a4f85386b8..edd527286264 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -7,7 +7,6 @@ use rustc_hir::def_id::LocalDefId; use rustc_middle::hir::place::Projection as HirProjection; use rustc_middle::hir::place::ProjectionKind as HirProjectionKind; use rustc_middle::middle::region; -use rustc_middle::mir::tcx::PlaceTy; use rustc_middle::mir::AssertKind::BoundsCheck; use rustc_middle::mir::*; use rustc_middle::thir::*; @@ -19,31 +18,23 @@ use rustc_target::abi::VariantIdx; use rustc_index::vec::Idx; use std::assert_matches::assert_matches; -use std::convert::From; use std::iter; -/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a -/// place by pushing more and more projections onto the end, and then convert the final set into a -/// place using the `into_place` method. -/// -/// This is used internally when building a place for an expression like `a.b.c`. The fields `b` -/// and `c` can be progressively pushed onto the place builder that is created when converting `a`. -#[derive(Clone, Debug, PartialEq)] -pub(in crate::build) enum PlaceBuilder<'tcx> { +/// The "outermost" place that holds this value. +#[derive(Copy, Clone, Debug, PartialEq)] +pub(crate) enum PlaceBase { /// Denotes the start of a `Place`. - /// - /// We use `PlaceElem` since this has all `Field` types available. - Local { local: Local, projection: Vec> }, + Local(Local), /// When building place for an expression within a closure, the place might start off a /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture /// index (within the desugared closure) of the captured path until most of the projections - /// are applied. We use `PlaceBuilder::Upvar` to keep track of the root variable off of which the + /// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the /// captured path starts, the closure the capture belongs to and the trait the closure /// implements. /// - /// Once we have figured out the capture index, we can convert the place builder to - /// `PlaceBuilder::Local`. + /// Once we have figured out the capture index, we can convert the place builder to start from + /// `PlaceBase::Local`. /// /// Consider the following example /// ```rust @@ -64,16 +55,24 @@ pub(in crate::build) enum PlaceBuilder<'tcx> { /// /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to /// figure out that it is captured until all the `Field` projections are applied. - /// - /// Note: in contrast to `PlaceBuilder::Local` we have not yet determined all `Field` types - /// and will only do so once converting to `PlaceBuilder::Local`. - Upvar { upvar: Upvar, projection: Vec> }, + Upvar { + /// HirId of the upvar + var_hir_id: LocalVarId, + /// DefId of the closure + closure_def_id: LocalDefId, + }, } -#[derive(Copy, Clone, Debug, PartialEq)] -pub(crate) struct Upvar { - var_hir_id: LocalVarId, - closure_def_id: LocalDefId, +/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a +/// place by pushing more and more projections onto the end, and then convert the final set into a +/// place using the `to_place` method. +/// +/// This is used internally when building a place for an expression like `a.b.c`. The fields `b` +/// and `c` can be progressively pushed onto the place builder that is created when converting `a`. +#[derive(Clone, Debug, PartialEq)] +pub(in crate::build) struct PlaceBuilder<'tcx> { + base: PlaceBase, + projection: Vec>, } /// Given a list of MIR projections, convert them to list of HIR ProjectionKind. @@ -83,7 +82,7 @@ pub(crate) struct Upvar { /// part of a path that is captured by a closure. We stop applying projections once we see the first /// projection that isn't captured by a closure. fn convert_to_hir_projections_and_truncate_for_capture<'tcx>( - mir_projections: &[UpvarProjectionElem<'tcx>], + mir_projections: &[PlaceElem<'tcx>], ) -> Vec { let mut hir_projections = Vec::new(); let mut variant = None; @@ -157,7 +156,7 @@ fn is_ancestor_or_same_capture( fn find_capture_matching_projections<'a, 'tcx>( upvars: &'a CaptureMap<'tcx>, var_hir_id: LocalVarId, - projections: &[UpvarProjectionElem<'tcx>], + projections: &[PlaceElem<'tcx>], ) -> Option<(usize, &'a Capture<'tcx>)> { let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections); @@ -175,7 +174,7 @@ fn to_upvars_resolved_place_builder<'tcx>( cx: &Builder<'_, 'tcx>, var_hir_id: LocalVarId, closure_def_id: LocalDefId, - projection: &[UpvarProjectionElem<'tcx>], + projection: &[PlaceElem<'tcx>], ) -> Option> { let Some((capture_index, capture)) = find_capture_matching_projections( @@ -197,32 +196,23 @@ fn to_upvars_resolved_place_builder<'tcx>( var_hir_id, projection, ); } - return None; }; // Access the capture by accessing the field within the Closure struct. let capture_info = &cx.upvars[capture_index]; - let Place { local: upvar_resolved_local, projection: local_projection } = - capture_info.use_place; + let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place); // We used some of the projections to build the capture itself, // now we apply the remaining to the upvar resolved place. - let upvar_projection = strip_prefix( + trace!(?capture.captured_place, ?projection); + let remaining_projections = strip_prefix( capture.captured_place.place.base_ty, projection, &capture.captured_place.place.projections, ); - - let upvar_resolved_place_builder = PlaceBuilder::construct_local_place_builder( - cx, - upvar_resolved_local, - local_projection.as_slice(), - upvar_projection, - ); - - assert!(matches!(upvar_resolved_place_builder, PlaceBuilder::Local { .. })); + upvar_resolved_place_builder.projection.extend(remaining_projections); Some(upvar_resolved_place_builder) } @@ -235,17 +225,15 @@ fn to_upvars_resolved_place_builder<'tcx>( /// projection kinds are unsupported. fn strip_prefix<'a, 'tcx>( mut base_ty: Ty<'tcx>, - projections: &'a [UpvarProjectionElem<'tcx>], + projections: &'a [PlaceElem<'tcx>], prefix_projections: &[HirProjection<'tcx>], -) -> impl Iterator> + 'a { +) -> impl Iterator> + 'a { let mut iter = projections .iter() .copied() // Filter out opaque casts, they are unnecessary in the prefix. .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))); for projection in prefix_projections { - debug!(?projection, ?projection.ty); - match projection.kind { HirProjectionKind::Deref => { assert_matches!(iter.next(), Some(ProjectionElem::Deref)); @@ -260,10 +248,8 @@ fn strip_prefix<'a, 'tcx>( bug!("unexpected projection kind: {:?}", projection); } } - base_ty = projection.ty; } - iter } @@ -276,9 +262,9 @@ impl<'tcx> PlaceBuilder<'tcx> { pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option> { let resolved = self.resolve_upvar(cx); let builder = resolved.as_ref().unwrap_or(self); - let PlaceBuilder::Local{local, ref projection} = builder else { return None }; - let projection = cx.tcx.intern_place_elems(projection); - Some(Place { local: *local, projection }) + let PlaceBase::Local(local) = builder.base else { return None }; + let projection = cx.tcx.intern_place_elems(&builder.projection); + Some(Place { local, projection }) } /// Attempts to resolve the `PlaceBuilder`. @@ -295,31 +281,22 @@ impl<'tcx> PlaceBuilder<'tcx> { &self, cx: &Builder<'_, 'tcx>, ) -> Option> { - let PlaceBuilder::Upvar{ upvar: Upvar {var_hir_id, closure_def_id }, projection} = self else { + let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else { return None; }; - - to_upvars_resolved_place_builder(cx, *var_hir_id, *closure_def_id, &projection) + to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection) } - #[instrument(skip(cx), level = "debug")] - pub(crate) fn field(self, cx: &Builder<'_, 'tcx>, f: Field) -> Self { - match self.clone() { - PlaceBuilder::Local { local, projection } => { - let base_place = PlaceBuilder::Local { local, projection }; - let PlaceTy { ty, variant_index } = - base_place.to_place(cx).ty(&cx.local_decls, cx.tcx); - let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty); + pub(crate) fn base(&self) -> PlaceBase { + self.base + } - let field_ty = PlaceBuilder::compute_field_ty(cx, f, base_ty, variant_index); + pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] { + &self.projection + } - self.project(ProjectionElem::Field(f, field_ty)) - } - PlaceBuilder::Upvar { upvar, mut projection } => { - projection.push(ProjectionElem::Field(f, ())); - PlaceBuilder::Upvar { upvar, projection } - } - } + pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self { + self.project(PlaceElem::Field(f, ty)) } pub(crate) fn deref(self) -> Self { @@ -334,236 +311,35 @@ impl<'tcx> PlaceBuilder<'tcx> { self.project(PlaceElem::Index(index)) } - #[instrument(level = "debug")] - pub(crate) fn project(self, elem: PlaceElem<'tcx>) -> Self { - let result = match self { - PlaceBuilder::Local { local, mut projection } => { - projection.push(elem); - PlaceBuilder::Local { local, projection } - } - PlaceBuilder::Upvar { upvar, mut projection } => { - projection.push(elem.into()); - PlaceBuilder::Upvar { upvar, projection } - } - }; - - debug!(?result); - result + pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self { + self.projection.push(elem); + self } /// Same as `.clone().project(..)` but more efficient pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self { - match self { - PlaceBuilder::Local { local, projection } => PlaceBuilder::Local { - local: *local, - projection: Vec::from_iter(projection.iter().copied().chain([elem])), - }, - PlaceBuilder::Upvar { upvar, projection } => PlaceBuilder::Upvar { - upvar: *upvar, - projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])), - }, + Self { + base: self.base, + projection: Vec::from_iter(self.projection.iter().copied().chain([elem])), } } - - /// Similar to `Place::ty` but needed during mir building. - /// - /// Applies the projections in the `PlaceBuilder` to the base - /// type. - /// - /// Fallible as the root of this place may be an upvar for - /// which no base type can be determined. - #[instrument(skip(cx), level = "debug")] - fn compute_field_ty( - cx: &Builder<'_, 'tcx>, - field: Field, - base_ty: Ty<'tcx>, - variant_index: Option, - ) -> Ty<'tcx> { - let field_idx = field.as_usize(); - let field_ty = match base_ty.kind() { - ty::Adt(adt_def, substs) if adt_def.is_enum() => { - let variant_idx = variant_index.unwrap(); - adt_def.variant(variant_idx).fields[field_idx].ty(cx.tcx, substs) - } - ty::Adt(adt_def, substs) => adt_def - .all_fields() - .nth(field_idx) - .unwrap_or_else(|| { - bug!( - "expected to take field with idx {:?} of fields of {:?}", - field_idx, - adt_def - ) - }) - .ty(cx.tcx, substs), - ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { - bug!("expected to take field with idx {:?} of {:?}", field_idx, elems) - }), - ty::Closure(_, substs) => { - let substs = substs.as_closure(); - let Some(f_ty) = substs.upvar_tys().nth(field_idx) else { - bug!("expected to take field with idx {:?} of {:?}", field_idx, substs.upvar_tys().collect::>()); - }; - - f_ty - } - &ty::Generator(def_id, substs, _) => { - if let Some(var) = variant_index { - let gen_body = cx.tcx.optimized_mir(def_id); - let Some(layout) = gen_body.generator_layout() else { - bug!("No generator layout for {:?}", base_ty); - }; - - let Some(&local) = layout.variant_fields[var].get(field) else { - bug!("expected to take field {:?} of {:?}", field, layout.variant_fields[var]); - }; - - let Some(&f_ty) = layout.field_tys.get(local) else { - bug!("expected to get element for {:?} in {:?}", local, layout.field_tys); - }; - - f_ty - } else { - let Some(f_ty) = substs.as_generator().prefix_tys().nth(field.index()) else { - bug!( - "expected to take index {:?} in {:?}", - field.index(), - substs.as_generator().prefix_tys().collect::>() - ); - }; - - f_ty - } - } - _ => bug!("couldn't create field type, unexpected base type: {:?}", base_ty), - }; - - cx.tcx.normalize_erasing_regions(cx.param_env, field_ty) - } - - /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::Upvar` whose upvars - /// are resolved. This function takes two kinds of projections: `local_projection` - /// contains the projections of the captured upvar and `upvar_projection` the - /// projections that are applied to the captured upvar. The main purpose of this - /// function is to figure out the `Ty`s of the field projections in `upvar_projection`. - #[instrument(skip(cx, local, upvar_projection))] - fn construct_local_place_builder( - cx: &Builder<'_, 'tcx>, - local: Local, - local_projection: &[PlaceElem<'tcx>], - upvar_projection: impl Iterator>, - ) -> Self { - // We maintain a `Ty` to which we apply a projection in each iteration over `upvar_projection`. - // This `ancestor_ty` let's us infer the field type whenever we encounter a - // `ProjectionElem::Field`. - let (mut ancestor_ty, mut opt_variant_idx) = - local_projections_to_ty(cx, local, local_projection); - - // We add all projection elements we encounter to this `Vec`. - let mut local_projection = local_projection.to_vec(); - - for (i, proj) in upvar_projection.enumerate() { - debug!("i: {:?}, proj: {:?}, local_projection: {:?}", i, proj, local_projection); - match proj { - ProjectionElem::Field(field, _) => { - let field_ty = - PlaceBuilder::compute_field_ty(cx, field, ancestor_ty, opt_variant_idx); - debug!(?field_ty); - - local_projection.push(ProjectionElem::Field(field, field_ty)); - ancestor_ty = field_ty; - opt_variant_idx = None; - } - _ => { - let proj = upvar_proj_to_place_elem_no_field_proj(proj); - (ancestor_ty, opt_variant_idx) = project_ty(cx.tcx, ancestor_ty, proj); - local_projection.push(proj); - } - } - } - - PlaceBuilder::Local { local, projection: local_projection } - } } impl<'tcx> From for PlaceBuilder<'tcx> { fn from(local: Local) -> Self { - Self::Local { local, projection: Vec::new() } + Self { base: PlaceBase::Local(local), projection: Vec::new() } + } +} + +impl<'tcx> From for PlaceBuilder<'tcx> { + fn from(base: PlaceBase) -> Self { + Self { base, projection: Vec::new() } } } impl<'tcx> From> for PlaceBuilder<'tcx> { fn from(p: Place<'tcx>) -> Self { - Self::Local { local: p.local, projection: p.projection.to_vec() } - } -} - -fn project_ty<'tcx>( - tcx: TyCtxt<'tcx>, - ty: Ty<'tcx>, - elem: PlaceElem<'tcx>, -) -> (Ty<'tcx>, Option) { - match elem { - ProjectionElem::Deref => { - let updated_ty = ty - .builtin_deref(true) - .unwrap_or_else(|| bug!("deref projection of non-dereferenceable ty {:?}", ty)) - .ty; - - (updated_ty, None) - } - ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => { - (ty.builtin_index().unwrap(), None) - } - ProjectionElem::Subslice { from, to, from_end } => { - let ty = match ty.kind() { - ty::Slice(..) => ty, - ty::Array(inner, _) if !from_end => tcx.mk_array(*inner, (to - from) as u64), - ty::Array(inner, size) if from_end => { - let size = size.eval_usize(tcx, ty::ParamEnv::empty()); - let len = size - (from as u64) - (to as u64); - tcx.mk_array(*inner, len) - } - _ => bug!("cannot subslice non-array type: `{:?}`", ty), - }; - - (ty, None) - } - ProjectionElem::Downcast(_, variant_idx) => (ty, Some(variant_idx)), - ProjectionElem::Field(_, ty) => (ty, None), - ProjectionElem::OpaqueCast(..) => bug!("didn't expect OpaqueCast"), - } -} - -fn local_projections_to_ty<'a, 'tcx>( - cx: &'a Builder<'a, 'tcx>, - local: Local, - projection: &'a [PlaceElem<'tcx>], -) -> (Ty<'tcx>, Option) { - let local_ty = cx.local_decls.local_decls()[local].ty; - projection.iter().fold((local_ty, None), |ty_variant_idx, elem| { - let ty = ty_variant_idx.0; - project_ty(cx.tcx, ty, *elem) - }) -} - -// Converts an `UpvarProjectionElem` to `PlaceElem`, ICE'ing when being passed a -// field projection. -fn upvar_proj_to_place_elem_no_field_proj<'tcx>( - upvar_proj: UpvarProjectionElem<'tcx>, -) -> PlaceElem<'tcx> { - match upvar_proj { - ProjectionElem::Deref => ProjectionElem::Deref, - ProjectionElem::Index(i) => ProjectionElem::Index(i), - ProjectionElem::ConstantIndex { offset, min_length, from_end } => { - ProjectionElem::ConstantIndex { offset, min_length, from_end } - } - ProjectionElem::Subslice { from, to, from_end } => { - ProjectionElem::Subslice { from, to, from_end } - } - ProjectionElem::Downcast(ty, variant_idx) => ProjectionElem::Downcast(ty, variant_idx), - ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), - ProjectionElem::Field(..) => bug!("should not be called with `ProjectionElem::Field`"), + Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() } } } @@ -627,7 +403,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { self.expr_as_place(block, expr, Mutability::Not, None) } - #[instrument(skip(self, fake_borrow_temps), level = "debug")] fn expr_as_place( &mut self, mut block: BasicBlock, @@ -635,6 +410,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { mutability: Mutability, fake_borrow_temps: Option<&mut Vec>, ) -> BlockAnd> { + debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability); + let this = self; let expr_span = expr.span; let source_info = this.source_info(expr_span); @@ -648,13 +425,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let lhs = &this.thir[lhs]; let mut place_builder = unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,)); - debug!(?place_builder); if let ty::Adt(adt_def, _) = lhs.ty.kind() { if adt_def.is_enum() { place_builder = place_builder.downcast(*adt_def, variant_index); } } - block.and(place_builder.field(this, name)) + block.and(place_builder.field(name, expr.ty)) } ExprKind::Deref { arg } => { let place_builder = unpack!( @@ -796,7 +572,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } /// Lower a captured upvar. Note we might not know the actual capture index, - /// so we create a place starting from `Upvar`, which will be resolved + /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved /// once all projections that allow us to identify a capture have been applied. fn lower_captured_upvar( &mut self, @@ -804,10 +580,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { closure_def_id: LocalDefId, var_hir_id: LocalVarId, ) -> BlockAnd> { - block.and(PlaceBuilder::Upvar { - upvar: Upvar { var_hir_id, closure_def_id }, - projection: vec![], - }) + block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id })) } /// Lower an index expression @@ -898,8 +671,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { source_info: SourceInfo, ) { let tcx = self.tcx; - let place_ty = base_place.ty(&self.local_decls, tcx); + let place_ty = base_place.ty(&self.local_decls, tcx); if let ty::Slice(_) = place_ty.ty.kind() { // We need to create fake borrows to ensure that the bounds // check that we just did stays valid. Since we can't assign to diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index b420e8201714..c7b3eb44dc5f 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -4,8 +4,9 @@ use rustc_index::vec::Idx; use rustc_middle::ty::util::IntTypeExt; use rustc_target::abi::{Abi, Primitive}; +use crate::build::expr::as_place::PlaceBase; use crate::build::expr::category::{Category, RvalueFunc}; -use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary, PlaceBuilder}; +use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary}; use rustc_hir::lang_items::LangItem; use rustc_middle::middle::region; use rustc_middle::mir::AssertKind; @@ -650,15 +651,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let arg_place_builder = unpack!(block = this.as_place_builder(block, arg)); - let mutability = match arg_place_builder { + let mutability = match arg_place_builder.base() { // We are capturing a path that starts off a local variable in the parent. // The mutability of the current capture is same as the mutability // of the local declaration in the parent. - PlaceBuilder::Local { local, .. } => this.local_decls[local].mutability, + PlaceBase::Local(local) => this.local_decls[local].mutability, // Parent is a closure and we are capturing a path that is captured // by the parent itself. The mutability of the current capture // is same as that of the capture in the parent closure. - PlaceBuilder::Upvar { .. } => { + PlaceBase::Upvar { .. } => { let enclosing_upvars_resolved = arg_place_builder.to_place(this); match enclosing_upvars_resolved.as_ref() { diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index 895051d7590b..38b1fa91d0a6 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -355,13 +355,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // base-supplied field, generate an operand that // reads it from the base. iter::zip(field_names, &**field_types) - .map(|(n, _ty)| match fields_map.get(&n) { + .map(|(n, ty)| match fields_map.get(&n) { Some(v) => v.clone(), None => { - let place_builder = place_builder.clone(); - this.consume_by_copy_or_move( - place_builder.field(this, n).to_place(this), - ) + let place = place_builder.clone_project(PlaceElem::Field(n, *ty)); + this.consume_by_copy_or_move(place.to_place(this)) } }) .collect() diff --git a/compiler/rustc_mir_build/src/build/matches/simplify.rs b/compiler/rustc_mir_build/src/build/matches/simplify.rs index 36aa7693e827..f6b1955fdec4 100644 --- a/compiler/rustc_mir_build/src/build/matches/simplify.rs +++ b/compiler/rustc_mir_build/src/build/matches/simplify.rs @@ -272,9 +272,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { || !adt_def.is_variant_list_non_exhaustive()); if irrefutable { let place_builder = match_pair.place.downcast(adt_def, variant_index); - let field_match_pairs = - self.field_match_pairs(place_builder.clone(), subpatterns); - candidate.match_pairs.extend(field_match_pairs); + candidate + .match_pairs + .extend(self.field_match_pairs(place_builder, subpatterns)); Ok(()) } else { Err(match_pair) diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index 6c10704c5db5..46e14cc9ac3b 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -757,7 +757,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let downcast_place = match_pair.place.downcast(adt_def, variant_index); // `(x as Variant)` let consequent_match_pairs = subpatterns.iter().map(|subpattern| { // e.g., `(x as Variant).0` - let place = downcast_place.clone().field(self, subpattern.field); + let place = downcast_place + .clone_project(PlaceElem::Field(subpattern.field, subpattern.pattern.ty)); // e.g., `(x as Variant).0 @ P1` MatchPair::new(place, &subpattern.pattern, self) }); diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index d95dbfca78e8..cbd494862a01 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -1,3 +1,4 @@ +use crate::build::expr::as_place::PlaceBase; use crate::build::expr::as_place::PlaceBuilder; use crate::build::matches::MatchPair; use crate::build::Builder; @@ -16,8 +17,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { subpatterns .iter() .map(|fieldpat| { - let place = place.clone().field(self, fieldpat.field); - + let place = + place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty)); MatchPair::new(place, &fieldpat.pattern, self) }) .collect() @@ -106,9 +107,9 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> { // Only add the OpaqueCast projection if the given place is an opaque type and the // expected type from the pattern is not. - let may_need_cast = match place { - PlaceBuilder::Local { local, ref projection } => { - let ty = Place::ty_from(local, projection, &cx.local_decls, cx.tcx).ty; + let may_need_cast = match place.base() { + PlaceBase::Local(local) => { + let ty = Place::ty_from(local, place.projection(), &cx.local_decls, cx.tcx).ty; ty != pattern.ty && ty.has_opaque_types() } _ => true, diff --git a/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs b/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs index 5cfbbb1ac01e..7806e8f45d3a 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs @@ -18,7 +18,7 @@ use rustc_middle::ty::Ty; pub struct AbstractOperand; #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct AbstractType; -pub type AbstractElem = ProjectionElem; +pub type AbstractElem = ProjectionElem; pub trait Lift { type Abstract; diff --git a/compiler/rustc_mir_dataflow/src/value_analysis.rs b/compiler/rustc_mir_dataflow/src/value_analysis.rs index ab16b60f82df..7df011422641 100644 --- a/compiler/rustc_mir_dataflow/src/value_analysis.rs +++ b/compiler/rustc_mir_dataflow/src/value_analysis.rs @@ -777,10 +777,10 @@ pub enum TrackElem { Field(Field), } -impl TryFrom> for TrackElem { +impl TryFrom> for TrackElem { type Error = (); - fn try_from(value: ProjectionElem) -> Result { + fn try_from(value: ProjectionElem) -> Result { match value { ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)), _ => Err(()), diff --git a/src/test/ui/mir/field-projection-invariant.rs b/src/test/ui/mir/field-projection-invariant.rs deleted file mode 100644 index b5d6add043cb..000000000000 --- a/src/test/ui/mir/field-projection-invariant.rs +++ /dev/null @@ -1,24 +0,0 @@ -// build-pass -struct Inv<'a>(&'a mut &'a ()); -enum Foo { - Bar, - Var(T), -} -type Supertype = Foo fn(Inv<'a>, Inv<'a>)>; - -fn foo(x: Foo fn(Inv<'a>, Inv<'b>)>) { - match x { - Supertype::Bar => {} - Supertype::Var(x) => {} - } -} - -fn foo_nested(x: Foo fn(Inv<'a>, Inv<'b>)>>) { - match x { - Foo::Bar => {} - Foo::Var(Supertype::Bar) => {} - Foo::Var(Supertype::Var(x)) => {} - } -} - -fn main() {} diff --git a/src/test/ui/mir/field-ty-ascription-enums.rs b/src/test/ui/mir/field-ty-ascription-enums.rs deleted file mode 100644 index 179af6170906..000000000000 --- a/src/test/ui/mir/field-ty-ascription-enums.rs +++ /dev/null @@ -1,15 +0,0 @@ -// build-pass - -enum Foo { - Var(T), -} // `T` is covariant. - -fn foo<'b>(x: Foo fn(&'a ())>) { - let Foo::Var(x): Foo = x; -} - -fn foo_nested<'b>(x: Foo fn(&'a ())>>) { - let Foo::Var(Foo::Var(x)): Foo> = x; -} - -fn main() {} diff --git a/src/test/ui/mir/field-ty-ascription.rs b/src/test/ui/mir/field-ty-ascription.rs deleted file mode 100644 index 178c7916bc59..000000000000 --- a/src/test/ui/mir/field-ty-ascription.rs +++ /dev/null @@ -1,37 +0,0 @@ -// build-pass - -struct Foo(T); // `T` is covariant. - -struct Bar { - x: T, -} // `T` is covariant. - -fn bar<'b>(x: Bar fn(&'a ())>) { - let Bar { x }: Bar = x; -} - -fn bar_nested<'b>(x: Bar fn(&'a ())>>) { - let Bar { x: Bar { x } }: Bar> = x; -} - -fn bar_foo_nested<'b>(x: Bar fn(&'a ())>>) { - let Bar { x: Foo ( x ) }: Bar> = x; -} - -fn foo<'b>(x: Foo fn(&'a ())>) { - let Foo(y): Foo = x; -} - -fn foo_nested<'b>(x: Foo fn(&'a ())>>) { - let Foo(Foo(y)): Foo> = x; -} - -fn tuple<'b>(x: (u32, for<'a> fn(&'a ()))) { - let (_, y): (u32, fn(&'b ())) = x; -} - -fn tuple_nested<'b>(x: (u32, (u32, for<'a> fn(&'a ())))) { - let (_, (_, y)): (u32, (u32, fn(&'b ()))) = x; -} - -fn main() {} From ee1a905f00ab5ec426d45eb29e168a20dfe94498 Mon Sep 17 00:00:00 2001 From: Marcus Calhoun-Lopez Date: Mon, 19 Dec 2022 08:34:06 -0700 Subject: [PATCH 207/321] Fix arch flag on i686-apple-darwin i686-apple-darwin should use `-arch i386` instead of `-arch i686` --- src/bootstrap/native.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index f6c453ebe107..68f917d35285 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -600,6 +600,9 @@ fn configure_cmake( if target.starts_with("aarch64") { // macOS uses a different name for building arm64 cfg.define("CMAKE_OSX_ARCHITECTURES", "arm64"); + } else if target.starts_with("i686") { + // macOS uses a different name for building i386 + cfg.define("CMAKE_OSX_ARCHITECTURES", "i386"); } else { cfg.define("CMAKE_OSX_ARCHITECTURES", target.triple.split('-').next().unwrap()); } From bd12d151ee63290b317566db770465301e2dd2a9 Mon Sep 17 00:00:00 2001 From: Yiming Lei Date: Thu, 15 Dec 2022 22:25:11 -0800 Subject: [PATCH 208/321] add function to tell if the current ambiguity error matches a previous one in ambiguity_errors if 2 errors of the kind and ident and span of the ident, b1, b2 and misc1 misc2 are the same then these 2 ambiguity errors matched prevent identical ambiguity error from pushing into vector of ambiguity_errors this will fix #105177 --- compiler/rustc_resolve/src/lib.rs | 26 ++++++++++++++-- .../local-modularized-tricky-fail-1.rs | 1 - .../local-modularized-tricky-fail-1.stderr | 31 ++----------------- src/test/ui/imports/macros.rs | 1 - src/test/ui/imports/macros.stderr | 29 +++-------------- 5 files changed, 32 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 24e4b5bdd3f5..5d0b4c0419f0 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1686,6 +1686,24 @@ impl<'a> Resolver<'a> { .or_insert_with(|| self.arenas.alloc_name_resolution()) } + // Test if AmbiguityError ambi is any identical to any one inside ambiguity_errors + fn matches_previous_ambiguity_error(&mut self, ambi: &AmbiguityError<'_>) -> bool { + for ambiguity_error in &self.ambiguity_errors { + // if the span location and ident as well as its span are the same + if ambiguity_error.kind == ambi.kind + && ambiguity_error.ident == ambi.ident + && ambiguity_error.ident.span == ambi.ident.span + && ambiguity_error.b1.span == ambi.b1.span + && ambiguity_error.b2.span == ambi.b2.span + && ambiguity_error.misc1 == ambi.misc1 + && ambiguity_error.misc2 == ambi.misc2 + { + return true; + } + } + false + } + fn record_use( &mut self, ident: Ident, @@ -1693,14 +1711,18 @@ impl<'a> Resolver<'a> { is_lexical_scope: bool, ) { if let Some((b2, kind)) = used_binding.ambiguity { - self.ambiguity_errors.push(AmbiguityError { + let ambiguity_error = AmbiguityError { kind, ident, b1: used_binding, b2, misc1: AmbiguityErrorMisc::None, misc2: AmbiguityErrorMisc::None, - }); + }; + if !self.matches_previous_ambiguity_error(&ambiguity_error) { + // avoid dumplicated span information to be emitt out + self.ambiguity_errors.push(ambiguity_error); + } } if let NameBindingKind::Import { import, binding, ref used } = used_binding.kind { // Avoid marking `extern crate` items that refer to a name from extern prelude, diff --git a/src/test/ui/imports/local-modularized-tricky-fail-1.rs b/src/test/ui/imports/local-modularized-tricky-fail-1.rs index 37fe0eceed6b..29e9b8ec841f 100644 --- a/src/test/ui/imports/local-modularized-tricky-fail-1.rs +++ b/src/test/ui/imports/local-modularized-tricky-fail-1.rs @@ -26,7 +26,6 @@ mod inner1 { } exported!(); //~ ERROR `exported` is ambiguous - //~| ERROR `exported` is ambiguous mod inner2 { define_exported!(); diff --git a/src/test/ui/imports/local-modularized-tricky-fail-1.stderr b/src/test/ui/imports/local-modularized-tricky-fail-1.stderr index c048d2ea2042..20eadaaaa56b 100644 --- a/src/test/ui/imports/local-modularized-tricky-fail-1.stderr +++ b/src/test/ui/imports/local-modularized-tricky-fail-1.stderr @@ -23,33 +23,8 @@ LL | use inner1::*; = help: consider adding an explicit import of `exported` to disambiguate = note: this error originates in the macro `define_exported` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0659]: `exported` is ambiguous - --> $DIR/local-modularized-tricky-fail-1.rs:28:1 - | -LL | exported!(); - | ^^^^^^^^ ambiguous name - | - = note: ambiguous because of a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution -note: `exported` could refer to the macro defined here - --> $DIR/local-modularized-tricky-fail-1.rs:5:5 - | -LL | / macro_rules! exported { -LL | | () => () -LL | | } - | |_____^ -... -LL | define_exported!(); - | ------------------ in this macro invocation -note: `exported` could also refer to the macro imported here - --> $DIR/local-modularized-tricky-fail-1.rs:22:5 - | -LL | use inner1::*; - | ^^^^^^^^^ - = help: consider adding an explicit import of `exported` to disambiguate - = note: this error originates in the macro `define_exported` (in Nightly builds, run with -Z macro-backtrace for more info) - error[E0659]: `panic` is ambiguous - --> $DIR/local-modularized-tricky-fail-1.rs:36:5 + --> $DIR/local-modularized-tricky-fail-1.rs:35:5 | LL | panic!(); | ^^^^^ ambiguous name @@ -70,7 +45,7 @@ LL | define_panic!(); = note: this error originates in the macro `define_panic` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0659]: `include` is ambiguous - --> $DIR/local-modularized-tricky-fail-1.rs:47:1 + --> $DIR/local-modularized-tricky-fail-1.rs:46:1 | LL | include!(); | ^^^^^^^ ambiguous name @@ -90,6 +65,6 @@ LL | define_include!(); = help: use `crate::include` to refer to this macro unambiguously = note: this error originates in the macro `define_include` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0659`. diff --git a/src/test/ui/imports/macros.rs b/src/test/ui/imports/macros.rs index f39711898cdf..f2a22ad620b1 100644 --- a/src/test/ui/imports/macros.rs +++ b/src/test/ui/imports/macros.rs @@ -14,7 +14,6 @@ mod m1 { mod m2 { use two_macros::*; m! { //~ ERROR ambiguous - //~| ERROR ambiguous use foo::m; } } diff --git a/src/test/ui/imports/macros.stderr b/src/test/ui/imports/macros.stderr index 110548d1d6ae..e34e5359b48f 100644 --- a/src/test/ui/imports/macros.stderr +++ b/src/test/ui/imports/macros.stderr @@ -6,7 +6,7 @@ LL | m! { | = note: ambiguous because of a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution note: `m` could refer to the macro imported here - --> $DIR/macros.rs:18:13 + --> $DIR/macros.rs:17:13 | LL | use foo::m; | ^^^^^^ @@ -18,43 +18,24 @@ LL | use two_macros::*; = help: consider adding an explicit import of `m` to disambiguate error[E0659]: `m` is ambiguous - --> $DIR/macros.rs:16:5 - | -LL | m! { - | ^ ambiguous name - | - = note: ambiguous because of a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution -note: `m` could refer to the macro imported here - --> $DIR/macros.rs:18:13 - | -LL | use foo::m; - | ^^^^^^ -note: `m` could also refer to the macro imported here - --> $DIR/macros.rs:15:9 - | -LL | use two_macros::*; - | ^^^^^^^^^^^^^ - = help: consider adding an explicit import of `m` to disambiguate - -error[E0659]: `m` is ambiguous - --> $DIR/macros.rs:30:9 + --> $DIR/macros.rs:29:9 | LL | m! { | ^ ambiguous name | = note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution note: `m` could refer to the macro imported here - --> $DIR/macros.rs:31:17 + --> $DIR/macros.rs:30:17 | LL | use two_macros::n as m; | ^^^^^^^^^^^^^^^^^^ note: `m` could also refer to the macro imported here - --> $DIR/macros.rs:23:9 + --> $DIR/macros.rs:22:9 | LL | use two_macros::m; | ^^^^^^^^^^^^^ = help: use `self::m` to refer to this macro unambiguously -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0659`. From a213bb36c953d2c342d8ae9303704e57bc8aab33 Mon Sep 17 00:00:00 2001 From: lcnr Date: Sun, 4 Dec 2022 03:19:10 +0000 Subject: [PATCH 209/321] implement the skeleton of the updated trait solver --- .../src/infer/canonical/query_response.rs | 6 +- compiler/rustc_middle/src/infer/canonical.rs | 10 + compiler/rustc_middle/src/traits/query.rs | 2 +- .../src/traits/specialization_graph.rs | 1 + compiler/rustc_middle/src/ty/mod.rs | 41 ++- compiler/rustc_middle/src/ty/sty.rs | 12 +- compiler/rustc_middle/src/ty/subst.rs | 4 + compiler/rustc_trait_selection/src/lib.rs | 2 + .../rustc_trait_selection/src/solve/cache.rs | 257 ++++++++++++++ .../src/solve/fulfill.rs | 92 +++++ .../src/solve/infcx_ext.rs | 55 +++ .../rustc_trait_selection/src/solve/mod.rs | 308 +++++++++++++++++ .../src/solve/overflow.rs | 80 +++++ .../src/solve/project_goals.rs | 324 ++++++++++++++++++ .../src/solve/trait_goals.rs | 282 +++++++++++++++ .../src/traits/project.rs | 59 +--- .../src/traits/select/mod.rs | 14 +- .../traits/specialize/specialization_graph.rs | 49 +++ .../inductive-canonical-cycle.rs | 28 ++ .../inductive-canonical-cycle.stderr | 26 ++ triagebot.toml | 4 + 21 files changed, 1569 insertions(+), 87 deletions(-) create mode 100644 compiler/rustc_trait_selection/src/solve/cache.rs create mode 100644 compiler/rustc_trait_selection/src/solve/fulfill.rs create mode 100644 compiler/rustc_trait_selection/src/solve/infcx_ext.rs create mode 100644 compiler/rustc_trait_selection/src/solve/mod.rs create mode 100644 compiler/rustc_trait_selection/src/solve/overflow.rs create mode 100644 compiler/rustc_trait_selection/src/solve/project_goals.rs create mode 100644 compiler/rustc_trait_selection/src/solve/trait_goals.rs create mode 100644 src/test/ui/traits/solver-cycles/inductive-canonical-cycle.rs create mode 100644 src/test/ui/traits/solver-cycles/inductive-canonical-cycle.stderr diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 996b1c40e3fd..a722613e3310 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -151,7 +151,11 @@ impl<'tcx> InferCtxt<'tcx> { }) } - fn take_opaque_types_for_query_response(&self) -> Vec<(Ty<'tcx>, Ty<'tcx>)> { + /// FIXME: This method should only be used for canonical queries and therefore be private. + /// + /// As the new solver does canonicalization slightly differently, this is also used there + /// for now. This should hopefully change fairly soon. + pub fn take_opaque_types_for_query_response(&self) -> Vec<(Ty<'tcx>, Ty<'tcx>)> { self.inner .borrow_mut() .opaque_type_storage diff --git a/compiler/rustc_middle/src/infer/canonical.rs b/compiler/rustc_middle/src/infer/canonical.rs index 0331d764b38a..0b32f67a81e1 100644 --- a/compiler/rustc_middle/src/infer/canonical.rs +++ b/compiler/rustc_middle/src/infer/canonical.rs @@ -300,6 +300,16 @@ impl<'tcx, V> Canonical<'tcx, V> { let Canonical { max_universe, variables, value } = self; Canonical { max_universe, variables, value: map_op(value) } } + + /// Allows you to map the `value` of a canonical while keeping the same set of + /// bound variables. + /// + /// **WARNING:** This function is very easy to mis-use, hence the name! See + /// the comment of [Canonical::unchecked_map] for more details. + pub fn unchecked_rebind(self, value: W) -> Canonical<'tcx, W> { + let Canonical { max_universe, variables, value: _ } = self; + Canonical { max_universe, variables, value } + } } pub type QueryOutlivesConstraint<'tcx> = ( diff --git a/compiler/rustc_middle/src/traits/query.rs b/compiler/rustc_middle/src/traits/query.rs index 7380c62a6693..6a149be3137e 100644 --- a/compiler/rustc_middle/src/traits/query.rs +++ b/compiler/rustc_middle/src/traits/query.rs @@ -96,7 +96,7 @@ pub type CanonicalTypeOpProvePredicateGoal<'tcx> = pub type CanonicalTypeOpNormalizeGoal<'tcx, T> = Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::Normalize>>; -#[derive(Copy, Clone, Debug, HashStable)] +#[derive(Copy, Clone, Debug, HashStable, PartialEq, Eq)] pub struct NoSolution; pub type Fallible = Result; diff --git a/compiler/rustc_middle/src/traits/specialization_graph.rs b/compiler/rustc_middle/src/traits/specialization_graph.rs index cccedc9ec6ea..a4dd22801e69 100644 --- a/compiler/rustc_middle/src/traits/specialization_graph.rs +++ b/compiler/rustc_middle/src/traits/specialization_graph.rs @@ -180,6 +180,7 @@ impl Iterator for Ancestors<'_> { } /// Information about the most specialized definition of an associated item. +#[derive(Debug)] pub struct LeafDef { /// The associated item described by this `LeafDef`. pub item: ty::AssocItem, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index a8e1253e6705..f01d74539a12 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -535,6 +535,17 @@ impl<'tcx> Predicate<'tcx> { self } + #[instrument(level = "debug", skip(tcx), ret)] + pub fn is_coinductive(self, tcx: TyCtxt<'tcx>) -> bool { + match self.kind().skip_binder() { + ty::PredicateKind::Clause(ty::Clause::Trait(data)) => { + tcx.trait_is_coinductive(data.def_id()) + } + ty::PredicateKind::WellFormed(_) => true, + _ => false, + } + } + /// Whether this projection can be soundly normalized. /// /// Wf predicates must not be normalized, as normalization @@ -1018,6 +1029,24 @@ pub struct ProjectionPredicate<'tcx> { pub term: Term<'tcx>, } +impl<'tcx> ProjectionPredicate<'tcx> { + pub fn self_ty(self) -> Ty<'tcx> { + self.projection_ty.self_ty() + } + + pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ProjectionPredicate<'tcx> { + Self { projection_ty: self.projection_ty.with_self_ty(tcx, self_ty), ..self } + } + + pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId { + self.projection_ty.trait_def_id(tcx) + } + + pub fn def_id(self) -> DefId { + self.projection_ty.def_id + } +} + pub type PolyProjectionPredicate<'tcx> = Binder<'tcx, ProjectionPredicate<'tcx>>; impl<'tcx> PolyProjectionPredicate<'tcx> { @@ -1054,18 +1083,6 @@ impl<'tcx> PolyProjectionPredicate<'tcx> { } } -impl<'tcx> ProjectionPredicate<'tcx> { - pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { - Self { - projection_ty: tcx.mk_alias_ty( - self.projection_ty.def_id, - [self_ty.into()].into_iter().chain(self.projection_ty.substs.iter().skip(1)), - ), - ..self - } - } -} - pub trait ToPolyTraitRef<'tcx> { fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>; } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 66aeebab88ba..890a4da63878 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1169,7 +1169,7 @@ pub struct AliasTy<'tcx> { } impl<'tcx> AliasTy<'tcx> { - pub fn trait_def_id(&self, tcx: TyCtxt<'tcx>) -> DefId { + pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId { match tcx.def_kind(self.def_id) { DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.def_id), DefKind::ImplTraitPlaceholder => { @@ -1183,7 +1183,7 @@ impl<'tcx> AliasTy<'tcx> { /// For example, if this is a projection of `::Item<'a>`, /// then this function would return a `T: Iterator` trait reference and `['a]` as the own substs pub fn trait_ref_and_own_substs( - &self, + self, tcx: TyCtxt<'tcx>, ) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) { debug_assert!(matches!(tcx.def_kind(self.def_id), DefKind::AssocTy | DefKind::AssocConst)); @@ -1202,14 +1202,18 @@ impl<'tcx> AliasTy<'tcx> { /// WARNING: This will drop the substs for generic associated types /// consider calling [Self::trait_ref_and_own_substs] to get those /// as well. - pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> { + pub fn trait_ref(self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> { let def_id = self.trait_def_id(tcx); tcx.mk_trait_ref(def_id, self.substs.truncate_to(tcx, tcx.generics_of(def_id))) } - pub fn self_ty(&self) -> Ty<'tcx> { + pub fn self_ty(self) -> Ty<'tcx> { self.substs.type_at(0) } + + pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { + tcx.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs.iter().skip(1))) + } } #[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift)] diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index a04b15f8cf13..237b36701c2b 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -573,6 +573,10 @@ impl EarlyBinder { pub fn rebind(&self, value: U) -> EarlyBinder { EarlyBinder(value) } + + pub fn skip_binder(self) -> T { + self.0 + } } impl EarlyBinder> { diff --git a/compiler/rustc_trait_selection/src/lib.rs b/compiler/rustc_trait_selection/src/lib.rs index 975ff31a6078..a30d1df4ede5 100644 --- a/compiler/rustc_trait_selection/src/lib.rs +++ b/compiler/rustc_trait_selection/src/lib.rs @@ -19,6 +19,7 @@ #![feature(let_chains)] #![feature(if_let_guard)] #![feature(never_type)] +#![feature(result_option_inspect)] #![feature(type_alias_impl_trait)] #![recursion_limit = "512"] // For rustdoc @@ -37,4 +38,5 @@ extern crate smallvec; pub mod autoderef; pub mod errors; pub mod infer; +pub mod solve; pub mod traits; diff --git a/compiler/rustc_trait_selection/src/solve/cache.rs b/compiler/rustc_trait_selection/src/solve/cache.rs new file mode 100644 index 000000000000..993b79890669 --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/cache.rs @@ -0,0 +1,257 @@ +//! This module both handles the global cache which stores "finished" goals, +//! and the provisional cache which contains partially computed goals. +//! +//! The provisional cache is necessary when dealing with coinductive cycles. +//! +//! For more information about the provisional cache and coinduction in general, +//! check out the relevant section of the rustc-dev-guide. +//! +//! FIXME(@lcnr): Write that section, feel free to ping me if you need help here +//! before then or if I still haven't done that before January 2023. +use super::overflow::OverflowData; +use super::CanonicalGoal; +use super::{EvalCtxt, QueryResult}; + +use rustc_data_structures::fx::FxHashMap; +use rustc_middle::ty::TyCtxt; +use std::{cmp::Ordering, collections::hash_map::Entry}; + +#[derive(Debug, Clone)] +struct ProvisionalEntry<'tcx> { + // In case we have a coinductive cycle, this is the + // the currently least restrictive result of this goal. + response: QueryResult<'tcx>, + // The lowest element on the stack on which this result + // relies on. Starts out as just being the depth at which + // we've proven this obligation, but gets lowered to the + // depth of another goal if we rely on it in a cycle. + depth: usize, +} + +struct StackElem<'tcx> { + goal: CanonicalGoal<'tcx>, + has_been_used: bool, +} + +/// The cache used for goals which are currently in progress or which depend +/// on in progress results. +/// +/// Once we're done with a goal we can store it in the global trait solver +/// cache of the `TyCtxt`. For goals which we're currently proving, or which +/// have only been proven via a coinductive cycle using a goal still on our stack +/// we have to use this separate data structure. +/// +/// The current data structure is not perfect, so there may still be room for +/// improvement here. We have the following requirements: +/// +/// ## Is there is a provisional entry for the given goal: +/// +/// ```ignore (for syntax highlighting) +/// self.entries.get(goal) +/// ``` +/// +/// ## Get all goals on the stack involved in a cycle: +/// +/// ```ignore (for syntax highlighting) +/// let entry = self.entries.get(goal).unwrap(); +/// let involved_goals = self.stack.iter().skip(entry.depth); +/// ``` +/// +/// ## Capping the depth of all entries +/// +/// Needed whenever we encounter a cycle. The current implementation always +/// iterates over all entries instead of only the ones with a larger depth. +/// Changing this may result in notable performance improvements. +/// +/// ```ignore (for syntax highlighting) +/// let cycle_depth = self.entries.get(goal).unwrap().depth; +/// for e in &mut self.entries { +/// e.depth = e.depth.min(cycle_depth); +/// } +/// ``` +/// +/// ## Checking whether we have to rerun the current goal +/// +/// A goal has to be rerun if its provisional result was used in a cycle +/// and that result is different from its final result. We update +/// [StackElem::has_been_used] for the deepest stack element involved in a cycle. +/// +/// ## Moving all finished goals into the global cache +/// +/// If `stack_elem.has_been_used` is true, iterate over all entries, moving the ones +/// with equal depth. If not, simply move this single entry. +pub(super) struct ProvisionalCache<'tcx> { + stack: Vec>, + entries: FxHashMap, ProvisionalEntry<'tcx>>, +} + +impl<'tcx> ProvisionalCache<'tcx> { + pub(super) fn empty() -> ProvisionalCache<'tcx> { + ProvisionalCache { stack: Vec::new(), entries: Default::default() } + } + + pub(super) fn current_depth(&self) -> usize { + self.stack.len() + } +} + +impl<'tcx> EvalCtxt<'tcx> { + /// Tries putting the new goal on the stack, returning an error if it is already cached. + /// + /// This correctly updates the provisional cache if there is a cycle. + pub(super) fn try_push_stack( + &mut self, + goal: CanonicalGoal<'tcx>, + ) -> Result<(), QueryResult<'tcx>> { + // FIXME: start by checking the global cache + + // Look at the provisional cache to check for cycles. + let cache = &mut self.provisional_cache; + match cache.entries.entry(goal) { + // No entry, simply push this goal on the stack after dealing with overflow. + Entry::Vacant(v) => { + if self.overflow_data.has_overflow(cache.stack.len()) { + return Err(self.deal_with_overflow()); + } + + v.insert(ProvisionalEntry { + response: fixme_response_yes_no_constraints(), + depth: cache.stack.len(), + }); + cache.stack.push(StackElem { goal, has_been_used: false }); + Ok(()) + } + // We have a nested goal which relies on a goal `root` deeper in the stack. + // + // We first store that we may have to rerun `evaluate_goal` for `root` in case the + // provisional response is not equal to the final response. We also update the depth + // of all goals which recursively depend on our current goal to depend on `root` + // instead. + // + // Finally we can return either the provisional response for that goal if we have a + // coinductive cycle or an ambiguous result if the cycle is inductive. + Entry::Occupied(entry) => { + // FIXME: `ProvisionalEntry` should be `Copy`. + let entry = entry.get().clone(); + cache.stack[entry.depth].has_been_used = true; + for provisional_entry in cache.entries.values_mut() { + provisional_entry.depth = provisional_entry.depth.min(entry.depth); + } + + // NOTE: The goals on the stack aren't the only goals involved in this cycle. + // We can also depend on goals which aren't part of the stack but coinductively + // depend on the stack themselves. We already checked whether all the goals + // between these goals and their root on the stack. This means that as long as + // each goal in a cycle is checked for coinductivity by itself simply checking + // the stack is enough. + if cache.stack[entry.depth..] + .iter() + .all(|g| g.goal.value.predicate.is_coinductive(self.tcx)) + { + Err(entry.response) + } else { + Err(fixme_response_maybe_no_constraints()) + } + } + } + } + + /// We cannot simply store the result of [EvalCtxt::compute_goal] as we have to deal with + /// coinductive cycles. + /// + /// When we encounter a coinductive cycle, we have to prove the final result of that cycle + /// while we are still computing that result. Because of this we continously recompute the + /// cycle until the result of the previous iteration is equal to the final result, at which + /// point we are done. + /// + /// This function returns `true` if we were able to finalize the goal and `false` if it has + /// updated the provisional cache and we have to recompute the current goal. + /// + /// FIXME: Refer to the rustc-dev-guide entry once it exists. + pub(super) fn try_finalize_goal( + &mut self, + actual_goal: CanonicalGoal<'tcx>, + response: QueryResult<'tcx>, + ) -> bool { + let cache = &mut self.provisional_cache; + let StackElem { goal, has_been_used } = cache.stack.pop().unwrap(); + assert_eq!(goal, actual_goal); + + let provisional_entry = cache.entries.get_mut(&goal).unwrap(); + // Check whether the current stack entry is the root of a cycle. + // + // If so, we either move all participants of that cycle to the global cache + // or, in case the provisional response used in the cycle is not equal to the + // final response, have to recompute the goal after updating the provisional + // response to the final response of this iteration. + if has_been_used { + if provisional_entry.response == response { + // We simply drop all entries according to an immutable condition, so + // query instability is not a concern here. + #[allow(rustc::potential_query_instability)] + cache.entries.retain(|goal, entry| match entry.depth.cmp(&cache.stack.len()) { + Ordering::Less => true, + Ordering::Equal => { + Self::try_move_finished_goal_to_global_cache( + self.tcx, + &mut self.overflow_data, + &cache.stack, + // FIXME: these should be `Copy` :( + goal.clone(), + entry.response.clone(), + ); + false + } + Ordering::Greater => bug!("entry with greater depth than the current leaf"), + }); + + true + } else { + provisional_entry.response = response; + cache.stack.push(StackElem { goal, has_been_used: false }); + false + } + } else { + Self::try_move_finished_goal_to_global_cache( + self.tcx, + &mut self.overflow_data, + &cache.stack, + goal, + response, + ); + cache.entries.remove(&goal); + true + } + } + + fn try_move_finished_goal_to_global_cache( + tcx: TyCtxt<'tcx>, + overflow_data: &mut OverflowData, + stack: &[StackElem<'tcx>], + goal: CanonicalGoal<'tcx>, + response: QueryResult<'tcx>, + ) { + // We move goals to the global cache if we either did not hit an overflow or if it's + // the root goal as that will now always hit the same overflow limit. + // + // NOTE: We cannot move any non-root goals to the global cache even if their final result + // isn't impacted by the overflow as that goal still has unstable query dependencies + // because it didn't go its full depth. + // + // FIXME(@lcnr): We could still cache subtrees which are not impacted by overflow though. + // Tracking that info correctly isn't trivial, so I haven't implemented it for now. + let should_cache_globally = !overflow_data.did_overflow() || stack.is_empty(); + if should_cache_globally { + // FIXME: move the provisional entry to the global cache. + let _ = (tcx, goal, response); + } + } +} + +fn fixme_response_yes_no_constraints<'tcx>() -> QueryResult<'tcx> { + unimplemented!() +} + +fn fixme_response_maybe_no_constraints<'tcx>() -> QueryResult<'tcx> { + unimplemented!() +} diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs new file mode 100644 index 000000000000..80115d78d88d --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -0,0 +1,92 @@ +use std::mem; + +use rustc_data_structures::fx::FxHashMap; +use rustc_infer::{ + infer::InferCtxt, + traits::{query::NoSolution, FulfillmentError, PredicateObligation, TraitEngine}, +}; +use rustc_middle::ty; + +use super::{Certainty, EvalCtxt}; + +/// A trait engine using the new trait solver. +/// +/// This is mostly identical to how `evaluate_all` works inside of the +/// solver, except that the requirements are slightly different. +/// +/// Unlike `evaluate_all` it is possible to add new obligations later on +/// and we also have to track diagnostics information by using `Obligation` +/// instead of `Goal`. +/// +/// It is also likely that we want to use slightly different datastructures +/// here as this will have to deal with far more root goals than `evaluate_all`. +pub struct FulfillmentCtxt<'tcx> { + obligations: Vec>, +} + +impl<'tcx> FulfillmentCtxt<'tcx> { + pub fn new() -> FulfillmentCtxt<'tcx> { + FulfillmentCtxt { obligations: Vec::new() } + } +} + +impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> { + fn register_predicate_obligation( + &mut self, + _infcx: &InferCtxt<'tcx>, + obligation: PredicateObligation<'tcx>, + ) { + self.obligations.push(obligation); + } + + fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec> { + let errors = self.select_where_possible(infcx); + if !errors.is_empty() { + return errors; + } + + if self.obligations.is_empty() { + Vec::new() + } else { + unimplemented!("ambiguous obligations") + } + } + + fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec> { + let errors = Vec::new(); + for i in 0.. { + if !infcx.tcx.recursion_limit().value_within_limit(i) { + unimplemented!("overflow") + } + + let mut has_changed = false; + for o in mem::take(&mut self.obligations) { + let mut cx = EvalCtxt::new(infcx.tcx); + let (changed, certainty) = match cx.evaluate_goal(infcx, o.clone().into()) { + Ok(result) => result, + Err(NoSolution) => unimplemented!("error"), + }; + + has_changed |= changed; + match certainty { + Certainty::Yes => {} + Certainty::Maybe(_) => self.obligations.push(o), + } + } + + if !has_changed { + break; + } + } + + errors + } + + fn pending_obligations(&self) -> Vec> { + self.obligations.clone() + } + + fn relationships(&mut self) -> &mut FxHashMap { + unimplemented!("Should be moved out of `TraitEngine`") + } +} diff --git a/compiler/rustc_trait_selection/src/solve/infcx_ext.rs b/compiler/rustc_trait_selection/src/solve/infcx_ext.rs new file mode 100644 index 000000000000..436f4eea6625 --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/infcx_ext.rs @@ -0,0 +1,55 @@ +use rustc_infer::infer::canonical::CanonicalVarValues; +use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; +use rustc_infer::infer::InferCtxt; +use rustc_infer::traits::query::NoSolution; +use rustc_middle::ty::Ty; +use rustc_span::DUMMY_SP; + +use crate::solve::ExternalConstraints; + +use super::{Certainty, QueryResult, Response}; + +/// Methods used inside of the canonical queries of the solver. +pub(super) trait InferCtxtExt<'tcx> { + fn next_ty_infer(&self) -> Ty<'tcx>; + + fn make_canonical_response( + &self, + var_values: CanonicalVarValues<'tcx>, + certainty: Certainty, + ) -> QueryResult<'tcx>; +} + +impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { + fn next_ty_infer(&self) -> Ty<'tcx> { + self.next_ty_var(TypeVariableOrigin { + kind: TypeVariableOriginKind::MiscVariable, + span: DUMMY_SP, + }) + } + + fn make_canonical_response( + &self, + var_values: CanonicalVarValues<'tcx>, + certainty: Certainty, + ) -> QueryResult<'tcx> { + let external_constraints = take_external_constraints(self)?; + + Ok(self.canonicalize_response(Response { var_values, external_constraints, certainty })) + } +} + +#[instrument(level = "debug", skip(infcx), ret)] +fn take_external_constraints<'tcx>( + infcx: &InferCtxt<'tcx>, +) -> Result, NoSolution> { + let region_obligations = infcx.take_registered_region_obligations(); + let opaque_types = infcx.take_opaque_types_for_query_response(); + Ok(ExternalConstraints { + // FIXME: Now that's definitely wrong :) + // + // Should also do the leak check here I think + regions: drop(region_obligations), + opaque_types, + }) +} diff --git a/compiler/rustc_trait_selection/src/solve/mod.rs b/compiler/rustc_trait_selection/src/solve/mod.rs new file mode 100644 index 000000000000..5d6529f85423 --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/mod.rs @@ -0,0 +1,308 @@ +//! The new trait solver, currently still WIP. +//! +//! As a user of the trait system, you can use `TyCtxt::evaluate_goal` to +//! interact with this solver. +//! +//! For a high-level overview of how this solver works, check out the relevant +//! section of the rustc-dev-guide. +//! +//! FIXME(@lcnr): Write that section. If you read this before then ask me +//! about it on zulip. + +// FIXME: Instead of using `infcx.canonicalize_query` we have to add a new routine which +// preserves universes and creates a unique var (in the highest universe) for each +// appearance of a region. + +// FIXME: `CanonicalVarValues` should be interned and `Copy`. + +// FIXME: uses of `infcx.at` need to enable deferred projection equality once that's implemented. + +use std::mem; + +use rustc_infer::infer::canonical::OriginalQueryValues; +use rustc_infer::infer::{InferCtxt, TyCtxtInferExt}; +use rustc_infer::traits::query::NoSolution; +use rustc_infer::traits::Obligation; +use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues}; +use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{RegionOutlivesPredicate, ToPredicate, TypeOutlivesPredicate}; +use rustc_span::DUMMY_SP; + +use self::infcx_ext::InferCtxtExt; + +mod cache; +mod fulfill; +mod infcx_ext; +mod overflow; +mod project_goals; +mod trait_goals; + +pub use fulfill::FulfillmentCtxt; + +/// A goal is a statement, i.e. `predicate`, we want to prove +/// given some assumptions, i.e. `param_env`. +/// +/// Most of the time the `param_env` contains the `where`-bounds of the function +/// we're currently typechecking while the `predicate` is some trait bound. +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, TypeFoldable, TypeVisitable)] +pub struct Goal<'tcx, P> { + param_env: ty::ParamEnv<'tcx>, + predicate: P, +} + +impl<'tcx, P> Goal<'tcx, P> { + pub fn new( + tcx: TyCtxt<'tcx>, + param_env: ty::ParamEnv<'tcx>, + predicate: impl ToPredicate<'tcx, P>, + ) -> Goal<'tcx, P> { + Goal { param_env, predicate: predicate.to_predicate(tcx) } + } + + /// Updates the goal to one with a different `predicate` but the same `param_env`. + fn with(self, tcx: TyCtxt<'tcx>, predicate: impl ToPredicate<'tcx, Q>) -> Goal<'tcx, Q> { + Goal { param_env: self.param_env, predicate: predicate.to_predicate(tcx) } + } +} + +impl<'tcx, P> From> for Goal<'tcx, P> { + fn from(obligation: Obligation<'tcx, P>) -> Goal<'tcx, P> { + Goal { param_env: obligation.param_env, predicate: obligation.predicate } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Hash, TypeFoldable, TypeVisitable)] +pub struct Response<'tcx> { + pub var_values: CanonicalVarValues<'tcx>, + /// Additional constraints returned by this query. + pub external_constraints: ExternalConstraints<'tcx>, + pub certainty: Certainty, +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, TypeFoldable, TypeVisitable)] +pub enum Certainty { + Yes, + Maybe(MaybeCause), +} + +impl Certainty { + /// When proving multiple goals using **AND**, e.g. nested obligations for an impl, + /// use this function to unify the certainty of these goals + pub fn unify_and(self, other: Certainty) -> Certainty { + match (self, other) { + (Certainty::Yes, Certainty::Yes) => Certainty::Yes, + (Certainty::Yes, Certainty::Maybe(_)) => other, + (Certainty::Maybe(_), Certainty::Yes) => self, + (Certainty::Maybe(MaybeCause::Overflow), Certainty::Maybe(MaybeCause::Overflow)) => { + Certainty::Maybe(MaybeCause::Overflow) + } + // If at least one of the goals is ambiguous, hide the overflow as the ambiguous goal + // may still result in failure. + (Certainty::Maybe(MaybeCause::Ambiguity), Certainty::Maybe(_)) + | (Certainty::Maybe(_), Certainty::Maybe(MaybeCause::Ambiguity)) => { + Certainty::Maybe(MaybeCause::Ambiguity) + } + } + } +} + +/// Why we failed to evaluate a goal. +#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, TypeFoldable, TypeVisitable)] +pub enum MaybeCause { + /// We failed due to ambiguity. This ambiguity can either + /// be a true ambiguity, i.e. there are multiple different answers, + /// or we hit a case where we just don't bother, e.g. `?x: Trait` goals. + Ambiguity, + /// We gave up due to an overflow, most often by hitting the recursion limit. + Overflow, +} + +/// Additional constraints returned on success. +#[derive(Debug, PartialEq, Eq, Clone, Hash, TypeFoldable, TypeVisitable)] +pub struct ExternalConstraints<'tcx> { + // FIXME: implement this. + regions: (), + opaque_types: Vec<(Ty<'tcx>, Ty<'tcx>)>, +} + +type CanonicalGoal<'tcx, T = ty::Predicate<'tcx>> = Canonical<'tcx, Goal<'tcx, T>>; +type CanonicalResponse<'tcx> = Canonical<'tcx, Response<'tcx>>; +/// The result of evaluating a canonical query. +/// +/// FIXME: We use a different type than the existing canonical queries. This is because +/// we need to add a `Certainty` for `overflow` and may want to restructure this code without +/// having to worry about changes to currently used code. Once we've made progress on this +/// solver, merge the two responses again. +pub type QueryResult<'tcx> = Result, NoSolution>; + +pub trait TyCtxtExt<'tcx> { + fn evaluate_goal(self, goal: CanonicalGoal<'tcx>) -> QueryResult<'tcx>; +} + +impl<'tcx> TyCtxtExt<'tcx> for TyCtxt<'tcx> { + fn evaluate_goal(self, goal: CanonicalGoal<'tcx>) -> QueryResult<'tcx> { + let mut cx = EvalCtxt::new(self); + cx.evaluate_canonical_goal(goal) + } +} + +struct EvalCtxt<'tcx> { + tcx: TyCtxt<'tcx>, + + provisional_cache: cache::ProvisionalCache<'tcx>, + overflow_data: overflow::OverflowData, +} + +impl<'tcx> EvalCtxt<'tcx> { + fn new(tcx: TyCtxt<'tcx>) -> EvalCtxt<'tcx> { + EvalCtxt { + tcx, + provisional_cache: cache::ProvisionalCache::empty(), + overflow_data: overflow::OverflowData::new(tcx), + } + } + + /// Recursively evaluates `goal`, returning whether any inference vars have + /// been constrained and the certainty of the result. + fn evaluate_goal( + &mut self, + infcx: &InferCtxt<'tcx>, + goal: Goal<'tcx, ty::Predicate<'tcx>>, + ) -> Result<(bool, Certainty), NoSolution> { + let mut orig_values = OriginalQueryValues::default(); + let canonical_goal = infcx.canonicalize_query(goal, &mut orig_values); + let canonical_response = self.evaluate_canonical_goal(canonical_goal)?; + Ok(( + true, // FIXME: check whether `var_values` are an identity substitution. + fixme_instantiate_canonical_query_response(infcx, &orig_values, canonical_response), + )) + } + + fn evaluate_canonical_goal(&mut self, goal: CanonicalGoal<'tcx>) -> QueryResult<'tcx> { + match self.try_push_stack(goal) { + Ok(()) => {} + // Our goal is already on the stack, eager return. + Err(response) => return response, + } + + // We may have to repeatedly recompute the goal in case of coinductive cycles, + // check out the `cache` module for more information. + // + // FIXME: Similar to `evaluate_all`, this has to check for overflow. + loop { + let result = self.compute_goal(goal); + + // FIXME: `Response` should be `Copy` + if self.try_finalize_goal(goal, result.clone()) { + return result; + } + } + } + + fn compute_goal(&mut self, canonical_goal: CanonicalGoal<'tcx>) -> QueryResult<'tcx> { + // WARNING: We're looking at a canonical value without instantiating it here. + // + // We have to be incredibly careful to not change the order of bound variables or + // remove any. As we go from `Goal<'tcx, Predicate>` to `Goal` with the variants + // of `PredicateKind` this is the case and it is and faster than instantiating and + // recanonicalizing. + let Goal { param_env, predicate } = canonical_goal.value; + if let Some(kind) = predicate.kind().no_bound_vars() { + match kind { + ty::PredicateKind::Clause(ty::Clause::Trait(predicate)) => self.compute_trait_goal( + canonical_goal.unchecked_rebind(Goal { param_env, predicate }), + ), + ty::PredicateKind::Clause(ty::Clause::Projection(predicate)) => self + .compute_projection_goal( + canonical_goal.unchecked_rebind(Goal { param_env, predicate }), + ), + ty::PredicateKind::Clause(ty::Clause::TypeOutlives(predicate)) => self + .compute_type_outlives_goal( + canonical_goal.unchecked_rebind(Goal { param_env, predicate }), + ), + ty::PredicateKind::Clause(ty::Clause::RegionOutlives(predicate)) => self + .compute_region_outlives_goal( + canonical_goal.unchecked_rebind(Goal { param_env, predicate }), + ), + // FIXME: implement these predicates :) + ty::PredicateKind::WellFormed(_) + | ty::PredicateKind::ObjectSafe(_) + | ty::PredicateKind::ClosureKind(_, _, _) + | ty::PredicateKind::Subtype(_) + | ty::PredicateKind::Coerce(_) + | ty::PredicateKind::ConstEvaluatable(_) + | ty::PredicateKind::ConstEquate(_, _) + | ty::PredicateKind::TypeWellFormedFromEnv(_) + | ty::PredicateKind::Ambiguous => unimplemented!(), + } + } else { + let (infcx, goal, var_values) = + self.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &canonical_goal); + let kind = infcx.replace_bound_vars_with_placeholders(goal.predicate.kind()); + let goal = goal.with(self.tcx, ty::Binder::dummy(kind)); + let (_, certainty) = self.evaluate_goal(&infcx, goal)?; + infcx.make_canonical_response(var_values, certainty) + } + } + + fn compute_type_outlives_goal( + &mut self, + _goal: CanonicalGoal<'tcx, TypeOutlivesPredicate<'tcx>>, + ) -> QueryResult<'tcx> { + todo!() + } + + fn compute_region_outlives_goal( + &mut self, + _goal: CanonicalGoal<'tcx, RegionOutlivesPredicate<'tcx>>, + ) -> QueryResult<'tcx> { + todo!() + } +} + +impl<'tcx> EvalCtxt<'tcx> { + fn evaluate_all( + &mut self, + infcx: &InferCtxt<'tcx>, + mut goals: Vec>>, + ) -> Result { + let mut new_goals = Vec::new(); + self.repeat_while_none(|this| { + let mut has_changed = Err(Certainty::Yes); + for goal in goals.drain(..) { + let (changed, certainty) = match this.evaluate_goal(infcx, goal) { + Ok(result) => result, + Err(NoSolution) => return Some(Err(NoSolution)), + }; + + if changed { + has_changed = Ok(()); + } + + match certainty { + Certainty::Yes => {} + Certainty::Maybe(_) => { + new_goals.push(goal); + has_changed = has_changed.map_err(|c| c.unify_and(certainty)); + } + } + } + + match has_changed { + Ok(()) => { + mem::swap(&mut new_goals, &mut goals); + None + } + Err(certainty) => Some(Ok(certainty)), + } + }) + } +} + +fn fixme_instantiate_canonical_query_response<'tcx>( + _: &InferCtxt<'tcx>, + _: &OriginalQueryValues<'tcx>, + _: CanonicalResponse<'tcx>, +) -> Certainty { + unimplemented!() +} diff --git a/compiler/rustc_trait_selection/src/solve/overflow.rs b/compiler/rustc_trait_selection/src/solve/overflow.rs new file mode 100644 index 000000000000..8d73a83aec96 --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/overflow.rs @@ -0,0 +1,80 @@ +use rustc_infer::traits::query::NoSolution; +use rustc_middle::ty::TyCtxt; +use rustc_session::Limit; + +use super::{Certainty, EvalCtxt, MaybeCause, QueryResult}; + +/// When detecting a solver overflow, we return ambiguity. Overflow can be +/// *hidden* by either a fatal error in an **AND** or a trivial success in an **OR**. +/// +/// This is in issue in case of exponential blowup, e.g. if each goal on the stack +/// has multiple nested (overflowing) candidates. To deal with this, we reduce the limit +/// used by the solver when hitting the default limit for the first time. +/// +/// FIXME: Get tests where always using the `default_limit` results in a hang and refer +/// to them here. We can also improve the overflow strategy if necessary. +pub(super) struct OverflowData { + default_limit: Limit, + current_limit: Limit, + /// When proving an **AND** we have to repeatedly iterate over the yet unproven goals. + /// + /// Because of this each iteration also increases the depth in addition to the stack + /// depth. + additional_depth: usize, +} + +impl OverflowData { + pub(super) fn new(tcx: TyCtxt<'_>) -> OverflowData { + let default_limit = tcx.recursion_limit(); + OverflowData { default_limit, current_limit: default_limit, additional_depth: 0 } + } + + #[inline] + pub(super) fn did_overflow(&self) -> bool { + self.default_limit.0 != self.current_limit.0 + } + + #[inline] + pub(super) fn has_overflow(&self, depth: usize) -> bool { + self.current_limit.value_within_limit(depth + self.additional_depth) + } + + /// Updating the current limit when hitting overflow. + fn deal_with_overflow(&mut self) { + // When first hitting overflow we reduce the overflow limit + // for all future goals to prevent hangs if there's an exponental + // blowup. + self.current_limit.0 = self.default_limit.0 / 8; + } +} + +impl<'tcx> EvalCtxt<'tcx> { + pub(super) fn deal_with_overflow(&mut self) -> QueryResult<'tcx> { + self.overflow_data.deal_with_overflow(); + fixme_response_overflow_no_constraints() + } + + /// A `while`-loop which tracks overflow. + pub(super) fn repeat_while_none( + &mut self, + mut loop_body: impl FnMut(&mut Self) -> Option>, + ) -> Result { + let start_depth = self.overflow_data.additional_depth; + let depth = self.provisional_cache.current_depth(); + while !self.overflow_data.has_overflow(depth) { + if let Some(result) = loop_body(self) { + self.overflow_data.additional_depth = start_depth; + return result; + } + + self.overflow_data.additional_depth += 1; + } + self.overflow_data.additional_depth = start_depth; + self.overflow_data.deal_with_overflow(); + Ok(Certainty::Maybe(MaybeCause::Overflow)) + } +} + +fn fixme_response_overflow_no_constraints<'tcx>() -> QueryResult<'tcx> { + unimplemented!() +} diff --git a/compiler/rustc_trait_selection/src/solve/project_goals.rs b/compiler/rustc_trait_selection/src/solve/project_goals.rs new file mode 100644 index 000000000000..47a8f59ceb77 --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/project_goals.rs @@ -0,0 +1,324 @@ +use crate::traits::{specialization_graph, translate_substs}; + +use super::infcx_ext::InferCtxtExt; +use super::{ + fixme_instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty, + EvalCtxt, Goal, QueryResult, +}; +use rustc_errors::ErrorGuaranteed; +use rustc_hir::def::DefKind; +use rustc_hir::def_id::DefId; +use rustc_infer::infer::canonical::{CanonicalVarValues, OriginalQueryValues}; +use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt}; +use rustc_infer::traits::query::NoSolution; +use rustc_infer::traits::specialization_graph::LeafDef; +use rustc_infer::traits::{ObligationCause, Reveal}; +use rustc_middle::ty; +use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; +use rustc_middle::ty::ProjectionPredicate; +use rustc_middle::ty::TypeVisitable; +use rustc_span::DUMMY_SP; +use std::iter; + +// FIXME: Deduplicate the candidate code between projection and trait goal. + +/// Similar to [super::trait_goals::Candidate] but for `Projection` goals. +#[derive(Debug, Clone)] +struct Candidate<'tcx> { + source: CandidateSource, + result: CanonicalResponse<'tcx>, +} + +#[allow(dead_code)] // FIXME: implement and use all variants. +#[derive(Debug, Clone, Copy)] +enum CandidateSource { + Impl(DefId), + ParamEnv(usize), + Builtin, +} + +impl<'tcx> EvalCtxt<'tcx> { + pub(super) fn compute_projection_goal( + &mut self, + goal: CanonicalGoal<'tcx, ProjectionPredicate<'tcx>>, + ) -> QueryResult<'tcx> { + let candidates = self.assemble_and_evaluate_project_candidates(goal); + self.merge_project_candidates(candidates) + } + + fn assemble_and_evaluate_project_candidates( + &mut self, + goal: CanonicalGoal<'tcx, ProjectionPredicate<'tcx>>, + ) -> Vec> { + let (ref infcx, goal, var_values) = + self.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal); + let mut acx = AssemblyCtxt { cx: self, infcx, var_values, candidates: Vec::new() }; + + acx.assemble_candidates_after_normalizing_self_ty(goal); + acx.assemble_impl_candidates(goal); + acx.candidates + } + + fn merge_project_candidates( + &mut self, + mut candidates: Vec>, + ) -> QueryResult<'tcx> { + match candidates.len() { + 0 => return Err(NoSolution), + 1 => return Ok(candidates.pop().unwrap().result), + _ => {} + } + + if candidates.len() > 1 { + let mut i = 0; + 'outer: while i < candidates.len() { + for j in (0..candidates.len()).filter(|&j| i != j) { + if self.project_candidate_should_be_dropped_in_favor_of( + &candidates[i], + &candidates[j], + ) { + debug!(candidate = ?candidates[i], "Dropping candidate #{}/{}", i, candidates.len()); + candidates.swap_remove(i); + continue 'outer; + } + } + + debug!(candidate = ?candidates[i], "Retaining candidate #{}/{}", i, candidates.len()); + // If there are *STILL* multiple candidates, give up + // and report ambiguity. + i += 1; + if i > 1 { + debug!("multiple matches, ambig"); + // FIXME: return overflow if all candidates overflow, otherwise return ambiguity. + unimplemented!(); + } + } + } + + Ok(candidates.pop().unwrap().result) + } + + fn project_candidate_should_be_dropped_in_favor_of( + &self, + candidate: &Candidate<'tcx>, + other: &Candidate<'tcx>, + ) -> bool { + // FIXME: implement this + match (candidate.source, other.source) { + (CandidateSource::Impl(_), _) + | (CandidateSource::ParamEnv(_), _) + | (CandidateSource::Builtin, _) => unimplemented!(), + } + } +} + +/// Similar to [super::trait_goals::AssemblyCtxt] but for `Projection` goals. +struct AssemblyCtxt<'a, 'tcx> { + cx: &'a mut EvalCtxt<'tcx>, + infcx: &'a InferCtxt<'tcx>, + var_values: CanonicalVarValues<'tcx>, + candidates: Vec>, +} + +impl<'tcx> AssemblyCtxt<'_, 'tcx> { + fn try_insert_candidate(&mut self, source: CandidateSource, certainty: Certainty) { + match self.infcx.make_canonical_response(self.var_values.clone(), certainty) { + Ok(result) => self.candidates.push(Candidate { source, result }), + Err(NoSolution) => debug!(?source, ?certainty, "failed leakcheck"), + } + } + + fn assemble_candidates_after_normalizing_self_ty( + &mut self, + goal: Goal<'tcx, ProjectionPredicate<'tcx>>, + ) { + let tcx = self.cx.tcx; + let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.projection_ty.self_ty().kind() else { + return + }; + self.infcx.probe(|_| { + let normalized_ty = self.infcx.next_ty_infer(); + let normalizes_to_goal = goal.with( + tcx, + ty::Binder::dummy(ty::ProjectionPredicate { + projection_ty, + term: normalized_ty.into(), + }), + ); + let normalization_certainty = + match self.cx.evaluate_goal(&self.infcx, normalizes_to_goal) { + Ok((_, certainty)) => certainty, + Err(NoSolution) => return, + }; + + // NOTE: Alternatively we could call `evaluate_goal` here and only have a `Normalized` candidate. + // This doesn't work as long as we use `CandidateSource` in both winnowing and to resolve associated items. + let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty)); + let mut orig_values = OriginalQueryValues::default(); + let goal = self.infcx.canonicalize_query(goal, &mut orig_values); + let normalized_candidates = self.cx.assemble_and_evaluate_project_candidates(goal); + // Map each candidate from being canonical wrt the current inference context to being + // canonical wrt the caller. + for Candidate { source, result } in normalized_candidates { + self.infcx.probe(|_| { + let candidate_certainty = fixme_instantiate_canonical_query_response( + self.infcx, + &orig_values, + result, + ); + self.try_insert_candidate( + source, + normalization_certainty.unify_and(candidate_certainty), + ) + }) + } + }) + } + + fn assemble_impl_candidates(&mut self, goal: Goal<'tcx, ProjectionPredicate<'tcx>>) { + self.cx.tcx.for_each_relevant_impl( + goal.predicate.trait_def_id(self.cx.tcx), + goal.predicate.self_ty(), + |impl_def_id| self.consider_impl_candidate(goal, impl_def_id), + ); + } + + fn consider_impl_candidate( + &mut self, + goal: Goal<'tcx, ProjectionPredicate<'tcx>>, + impl_def_id: DefId, + ) { + let tcx = self.cx.tcx; + let goal_trait_ref = goal.predicate.projection_ty.trait_ref(tcx); + let impl_trait_ref = tcx.bound_impl_trait_ref(impl_def_id).unwrap(); + let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::AsPlaceholder }; + if iter::zip(goal_trait_ref.substs, impl_trait_ref.skip_binder().substs) + .any(|(goal, imp)| !drcx.generic_args_may_unify(goal, imp)) + { + return; + } + + self.infcx.probe(|_| { + let impl_substs = self.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); + let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs); + + let Ok(InferOk { obligations, .. }) = self + .infcx + .at(&ObligationCause::dummy(), goal.param_env) + .define_opaque_types(false) + .eq(goal_trait_ref, impl_trait_ref) + .map_err(|e| debug!("failed to equate trait refs: {e:?}")) + else { + return + }; + + let nested_goals = obligations.into_iter().map(|o| o.into()).collect(); + let Ok(trait_ref_certainty) = self.cx.evaluate_all(self.infcx, nested_goals) else { return }; + + let Some(assoc_def) = self.fetch_eligible_assoc_item_def( + goal.param_env, + goal_trait_ref, + goal.predicate.def_id(), + impl_def_id + ) else { + return + }; + + if !assoc_def.item.defaultness(tcx).has_value() { + tcx.sess.delay_span_bug( + tcx.def_span(assoc_def.item.def_id), + "missing value for assoc item in impl", + ); + } + + // Getting the right substitutions here is complex, e.g. given: + // - a goal ` as Trait>::Assoc` + // - the applicable impl `impl Trait for Vec` + // - and the impl which defines `Assoc` being `impl Trait for Vec` + // + // We first rebase the goal substs onto the impl, going from `[Vec, i32, u64]` + // to `[u32, u64]`. + // + // And then map these substs to the substs of the defining impl of `Assoc`, going + // from `[u32, u64]` to `[u32, i32, u64]`. + let impl_substs_with_gat = goal.predicate.projection_ty.substs.rebase_onto( + tcx, + goal_trait_ref.def_id, + impl_trait_ref.substs, + ); + let substs = translate_substs( + self.infcx, + goal.param_env, + impl_def_id, + impl_substs_with_gat, + assoc_def.defining_node, + ); + + // Finally we construct the actual value of the associated type. + let is_const = matches!(tcx.def_kind(assoc_def.item.def_id), DefKind::AssocConst); + let ty = tcx.bound_type_of(assoc_def.item.def_id); + let term: ty::EarlyBinder> = if is_const { + let identity_substs = ty::InternalSubsts::identity_for_item(tcx, assoc_def.item.def_id); + let did = ty::WithOptConstParam::unknown(assoc_def.item.def_id); + let kind = + ty::ConstKind::Unevaluated(ty::UnevaluatedConst::new(did, identity_substs)); + ty.map_bound(|ty| tcx.mk_const(kind, ty).into()) + } else { + ty.map_bound(|ty| ty.into()) + }; + + let Ok(InferOk { obligations, .. }) = self + .infcx + .at(&ObligationCause::dummy(), goal.param_env) + .define_opaque_types(false) + .eq(goal.predicate.term, term.subst(tcx, substs)) + .map_err(|e| debug!("failed to equate trait refs: {e:?}")) + else { + return + }; + + let nested_goals = obligations.into_iter().map(|o| o.into()).collect(); + let Ok(rhs_certainty) = self.cx.evaluate_all(self.infcx, nested_goals) else { return }; + + let certainty = trait_ref_certainty.unify_and(rhs_certainty); + self.try_insert_candidate(CandidateSource::Impl(impl_def_id), certainty); + }) + } + + /// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code. + /// + /// FIXME: We should merge these 3 implementations as it's likely that they otherwise + /// diverge. + #[instrument(level = "debug", skip(self, param_env), ret)] + fn fetch_eligible_assoc_item_def( + &self, + param_env: ty::ParamEnv<'tcx>, + goal_trait_ref: ty::TraitRef<'tcx>, + trait_assoc_def_id: DefId, + impl_def_id: DefId, + ) -> Option { + let node_item = + specialization_graph::assoc_def(self.cx.tcx, impl_def_id, trait_assoc_def_id) + .map_err(|ErrorGuaranteed { .. }| ()) + .ok()?; + + let eligible = if node_item.is_final() { + // Non-specializable items are always projectable. + true + } else { + // Only reveal a specializable default if we're past type-checking + // and the obligation is monomorphic, otherwise passes such as + // transmute checking and polymorphic MIR optimizations could + // get a result which isn't correct for all monomorphizations. + if param_env.reveal() == Reveal::All { + let poly_trait_ref = self.infcx.resolve_vars_if_possible(goal_trait_ref); + !poly_trait_ref.still_further_specializable() + } else { + debug!(?node_item.item.def_id, "not eligible due to default"); + false + } + }; + + if eligible { Some(node_item) } else { None } + } +} diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs new file mode 100644 index 000000000000..4ea9081bf46a --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -0,0 +1,282 @@ +//! Dealing with trait goals, i.e. `T: Trait<'a, U>`. + +use std::iter; + +use super::infcx_ext::InferCtxtExt; +use super::{ + fixme_instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty, + EvalCtxt, Goal, QueryResult, +}; +use rustc_hir::def_id::DefId; +use rustc_infer::infer::canonical::{CanonicalVarValues, OriginalQueryValues}; +use rustc_infer::infer::TyCtxtInferExt; +use rustc_infer::infer::{InferCtxt, InferOk}; +use rustc_infer::traits::query::NoSolution; +use rustc_infer::traits::ObligationCause; +use rustc_middle::ty; +use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; +use rustc_middle::ty::TraitPredicate; +use rustc_span::DUMMY_SP; + +/// A candidate is a possible way to prove a goal. +/// +/// It consists of both the `source`, which describes how that goal +/// would be proven, and the `result` when using the given `source`. +/// +/// For the list of possible candidates, please look at the documentation +/// of [CandidateSource]. +#[derive(Debug, Clone)] +pub(super) struct Candidate<'tcx> { + source: CandidateSource, + result: CanonicalResponse<'tcx>, +} + +#[allow(dead_code)] // FIXME: implement and use all variants. +#[derive(Debug, Clone, Copy)] +pub(super) enum CandidateSource { + /// Some user-defined impl with the given `DefId`. + Impl(DefId), + /// The n-th caller bound in the `param_env` of our goal. + /// + /// This is pretty much always a bound from the `where`-clauses of the + /// currently checked item. + ParamEnv(usize), + /// A bound on the `self_ty` in case it is a projection or an opaque type. + /// + /// # Examples + /// + /// ```ignore (for syntax highlighting) + /// trait Trait { + /// type Assoc: OtherTrait; + /// } + /// ``` + /// + /// We know that `::Assoc: OtherTrait` holds by looking at + /// the bounds on `Trait::Assoc`. + AliasBound(usize), + /// A builtin implementation for some specific traits, used in cases + /// where we cannot rely an ordinary library implementations. + /// + /// The most notable examples are `Sized`, `Copy` and `Clone`. This is also + /// used for the `DiscriminantKind` and `Pointee` trait, both of which have + /// an associated type. + Builtin, + /// An automatic impl for an auto trait, e.g. `Send`. These impls recursively look + /// at the constituent types of the `self_ty` to check whether the auto trait + /// is implemented for those. + AutoImpl, +} + +struct AssemblyCtxt<'a, 'tcx> { + cx: &'a mut EvalCtxt<'tcx>, + infcx: &'a InferCtxt<'tcx>, + var_values: CanonicalVarValues<'tcx>, + candidates: Vec>, +} + +impl<'tcx> EvalCtxt<'tcx> { + pub(super) fn compute_trait_goal( + &mut self, + goal: CanonicalGoal<'tcx, TraitPredicate<'tcx>>, + ) -> QueryResult<'tcx> { + let candidates = self.assemble_and_evaluate_trait_candidates(goal); + self.merge_trait_candidates_discard_reservation_impls(candidates) + } + + pub(super) fn assemble_and_evaluate_trait_candidates( + &mut self, + goal: CanonicalGoal<'tcx, TraitPredicate<'tcx>>, + ) -> Vec> { + let (ref infcx, goal, var_values) = + self.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal); + let mut acx = AssemblyCtxt { cx: self, infcx, var_values, candidates: Vec::new() }; + + acx.assemble_candidates_after_normalizing_self_ty(goal); + acx.assemble_impl_candidates(goal); + + // FIXME: Remaining candidates + acx.candidates + } + + #[instrument(level = "debug", skip(self), ret)] + pub(super) fn merge_trait_candidates_discard_reservation_impls( + &mut self, + mut candidates: Vec>, + ) -> QueryResult<'tcx> { + match candidates.len() { + 0 => return Err(NoSolution), + 1 => return Ok(self.discard_reservation_impl(candidates.pop().unwrap()).result), + _ => {} + } + + if candidates.len() > 1 { + let mut i = 0; + 'outer: while i < candidates.len() { + for j in (0..candidates.len()).filter(|&j| i != j) { + if self.trait_candidate_should_be_dropped_in_favor_of( + &candidates[i], + &candidates[j], + ) { + debug!(candidate = ?candidates[i], "Dropping candidate #{}/{}", i, candidates.len()); + candidates.swap_remove(i); + continue 'outer; + } + } + + debug!(candidate = ?candidates[i], "Retaining candidate #{}/{}", i, candidates.len()); + // If there are *STILL* multiple candidates, give up + // and report ambiguity. + i += 1; + if i > 1 { + debug!("multiple matches, ambig"); + // FIXME: return overflow if all candidates overflow, otherwise return ambiguity. + unimplemented!(); + } + } + } + + Ok(self.discard_reservation_impl(candidates.pop().unwrap()).result) + } + + fn trait_candidate_should_be_dropped_in_favor_of( + &self, + candidate: &Candidate<'tcx>, + other: &Candidate<'tcx>, + ) -> bool { + // FIXME: implement this + match (candidate.source, other.source) { + (CandidateSource::Impl(_), _) + | (CandidateSource::ParamEnv(_), _) + | (CandidateSource::AliasBound(_), _) + | (CandidateSource::Builtin, _) + | (CandidateSource::AutoImpl, _) => unimplemented!(), + } + } + + fn discard_reservation_impl(&self, candidate: Candidate<'tcx>) -> Candidate<'tcx> { + if let CandidateSource::Impl(def_id) = candidate.source { + if let ty::ImplPolarity::Reservation = self.tcx.impl_polarity(def_id) { + debug!("Selected reservation impl"); + // FIXME: reduce candidate to ambiguous + // FIXME: replace `var_values` with identity, yeet external constraints. + unimplemented!() + } + } + + candidate + } +} + +impl<'tcx> AssemblyCtxt<'_, 'tcx> { + /// Adds a new candidate using the current state of the inference context. + /// + /// This does require each assembly method to correctly use `probe` to not taint + /// the results of other candidates. + fn try_insert_candidate(&mut self, source: CandidateSource, certainty: Certainty) { + match self.infcx.make_canonical_response(self.var_values.clone(), certainty) { + Ok(result) => self.candidates.push(Candidate { source, result }), + Err(NoSolution) => debug!(?source, ?certainty, "failed leakcheck"), + } + } + + /// If the self type of a trait goal is a projection, computing the relevant candidates is difficult. + /// + /// To deal with this, we first try to normalize the self type and add the candidates for the normalized + /// self type to the list of candidates in case that succeeds. Note that we can't just eagerly return in + /// this case as projections as self types add ` + fn assemble_candidates_after_normalizing_self_ty( + &mut self, + goal: Goal<'tcx, TraitPredicate<'tcx>>, + ) { + let tcx = self.cx.tcx; + // FIXME: We also have to normalize opaque types, not sure where to best fit that in. + let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.self_ty().kind() else { + return + }; + self.infcx.probe(|_| { + let normalized_ty = self.infcx.next_ty_infer(); + let normalizes_to_goal = goal.with( + tcx, + ty::Binder::dummy(ty::ProjectionPredicate { + projection_ty, + term: normalized_ty.into(), + }), + ); + let normalization_certainty = + match self.cx.evaluate_goal(&self.infcx, normalizes_to_goal) { + Ok((_, certainty)) => certainty, + Err(NoSolution) => return, + }; + + // NOTE: Alternatively we could call `evaluate_goal` here and only have a `Normalized` candidate. + // This doesn't work as long as we use `CandidateSource` in both winnowing and to resolve associated items. + let goal = goal.with(tcx, goal.predicate.with_self_type(tcx, normalized_ty)); + let mut orig_values = OriginalQueryValues::default(); + let goal = self.infcx.canonicalize_query(goal, &mut orig_values); + let normalized_candidates = self.cx.assemble_and_evaluate_trait_candidates(goal); + + // Map each candidate from being canonical wrt the current inference context to being + // canonical wrt the caller. + for Candidate { source, result } in normalized_candidates { + self.infcx.probe(|_| { + let candidate_certainty = fixme_instantiate_canonical_query_response( + self.infcx, + &orig_values, + result, + ); + + // FIXME: This is a bit scary if the `normalizes_to_goal` overflows. + // + // If we have an ambiguous candidate it hides that normalization + // caused an overflow which may cause issues. + self.try_insert_candidate( + source, + normalization_certainty.unify_and(candidate_certainty), + ) + }) + } + }) + } + + fn assemble_impl_candidates(&mut self, goal: Goal<'tcx, TraitPredicate<'tcx>>) { + self.cx.tcx.for_each_relevant_impl( + goal.predicate.def_id(), + goal.predicate.self_ty(), + |impl_def_id| self.consider_impl_candidate(goal, impl_def_id), + ); + } + + fn consider_impl_candidate( + &mut self, + goal: Goal<'tcx, TraitPredicate<'tcx>>, + impl_def_id: DefId, + ) { + let impl_trait_ref = self.cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap(); + let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::AsPlaceholder }; + if iter::zip(goal.predicate.trait_ref.substs, impl_trait_ref.skip_binder().substs) + .any(|(goal, imp)| !drcx.generic_args_may_unify(goal, imp)) + { + return; + } + + self.infcx.probe(|_| { + let impl_substs = self.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); + let impl_trait_ref = impl_trait_ref.subst(self.cx.tcx, impl_substs); + + let Ok(InferOk { obligations, .. }) = self + .infcx + .at(&ObligationCause::dummy(), goal.param_env) + .define_opaque_types(false) + .eq(goal.predicate.trait_ref, impl_trait_ref) + .map_err(|e| debug!("failed to equate trait refs: {e:?}")) + else { + return + }; + + let nested_goals = obligations.into_iter().map(|o| o.into()).collect(); + + let Ok(certainty) = self.cx.evaluate_all(self.infcx, nested_goals) else { return }; + self.try_insert_candidate(CandidateSource::Impl(impl_def_id), certainty); + }) + } +} diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 84d7244c1db7..5276da2e49c7 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -25,7 +25,6 @@ use rustc_data_structures::sso::SsoHashSet; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_errors::ErrorGuaranteed; use rustc_hir::def::DefKind; -use rustc_hir::def_id::DefId; use rustc_hir::lang_items::LangItem; use rustc_infer::infer::at::At; use rustc_infer::infer::resolve::OpportunisticRegionResolver; @@ -1553,7 +1552,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // NOTE: This should be kept in sync with the similar code in // `rustc_ty_utils::instance::resolve_associated_item()`. let node_item = - assoc_def(selcx, impl_data.impl_def_id, obligation.predicate.def_id) + specialization_graph::assoc_def(selcx.tcx(), impl_data.impl_def_id, obligation.predicate.def_id) .map_err(|ErrorGuaranteed { .. }| ())?; if node_item.is_final() { @@ -2113,7 +2112,7 @@ fn confirm_impl_candidate<'cx, 'tcx>( let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap(); let param_env = obligation.param_env; - let Ok(assoc_ty) = assoc_def(selcx, impl_def_id, assoc_item_id) else { + let Ok(assoc_ty) = specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) else { return Progress { term: tcx.ty_error().into(), obligations: nested }; }; @@ -2210,7 +2209,7 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>( let mut obligations = data.nested; let trait_fn_def_id = tcx.impl_trait_in_trait_parent(obligation.predicate.def_id); - let Ok(leaf_def) = assoc_def(selcx, data.impl_def_id, trait_fn_def_id) else { + let Ok(leaf_def) = specialization_graph::assoc_def(tcx, data.impl_def_id, trait_fn_def_id) else { return Progress { term: tcx.ty_error().into(), obligations }; }; if !leaf_def.item.defaultness(tcx).has_value() { @@ -2347,58 +2346,6 @@ fn assoc_ty_own_obligations<'cx, 'tcx>( } } -/// Locate the definition of an associated type in the specialization hierarchy, -/// starting from the given impl. -/// -/// Based on the "projection mode", this lookup may in fact only examine the -/// topmost impl. See the comments for `Reveal` for more details. -fn assoc_def( - selcx: &SelectionContext<'_, '_>, - impl_def_id: DefId, - assoc_def_id: DefId, -) -> Result { - let tcx = selcx.tcx(); - let trait_def_id = tcx.impl_trait_ref(impl_def_id).unwrap().def_id; - let trait_def = tcx.trait_def(trait_def_id); - - // This function may be called while we are still building the - // specialization graph that is queried below (via TraitDef::ancestors()), - // so, in order to avoid unnecessary infinite recursion, we manually look - // for the associated item at the given impl. - // If there is no such item in that impl, this function will fail with a - // cycle error if the specialization graph is currently being built. - if let Some(&impl_item_id) = tcx.impl_item_implementor_ids(impl_def_id).get(&assoc_def_id) { - let item = tcx.associated_item(impl_item_id); - let impl_node = specialization_graph::Node::Impl(impl_def_id); - return Ok(specialization_graph::LeafDef { - item: *item, - defining_node: impl_node, - finalizing_node: if item.defaultness(tcx).is_default() { - None - } else { - Some(impl_node) - }, - }); - } - - let ancestors = trait_def.ancestors(tcx, impl_def_id)?; - if let Some(assoc_item) = ancestors.leaf_def(tcx, assoc_def_id) { - Ok(assoc_item) - } else { - // This is saying that neither the trait nor - // the impl contain a definition for this - // associated type. Normally this situation - // could only arise through a compiler bug -- - // if the user wrote a bad item name, it - // should have failed in astconv. - bug!( - "No associated type `{}` for {}", - tcx.item_name(assoc_def_id), - tcx.def_path_str(impl_def_id) - ) - } -} - pub(crate) trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized { fn from_poly_projection_predicate( selcx: &mut SelectionContext<'cx, 'tcx>, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 792933096b15..81e8f9e914c2 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -1171,19 +1171,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { where I: Iterator>, { - cycle.all(|predicate| self.coinductive_predicate(predicate)) - } - - fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool { - let result = match predicate.kind().skip_binder() { - ty::PredicateKind::Clause(ty::Clause::Trait(ref data)) => { - self.tcx().trait_is_coinductive(data.def_id()) - } - ty::PredicateKind::WellFormed(_) => true, - _ => false, - }; - debug!(?predicate, ?result, "coinductive_predicate"); - result + cycle.all(|predicate| predicate.is_coinductive(self.tcx())) } /// Further evaluates `candidate` to decide whether all type parameters match and whether nested diff --git a/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs b/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs index 4546c9533930..02b066777402 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/specialization_graph.rs @@ -1,6 +1,7 @@ use super::OverlapError; use crate::traits; +use rustc_errors::ErrorGuaranteed; use rustc_hir::def_id::DefId; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; use rustc_middle::ty::{self, TyCtxt, TypeVisitable}; @@ -379,3 +380,51 @@ impl<'tcx> GraphExt<'tcx> for Graph { self.children.entry(parent).or_default().insert_blindly(tcx, child); } } + +/// Locate the definition of an associated type in the specialization hierarchy, +/// starting from the given impl. +pub(crate) fn assoc_def( + tcx: TyCtxt<'_>, + impl_def_id: DefId, + assoc_def_id: DefId, +) -> Result { + let trait_def_id = tcx.impl_trait_ref(impl_def_id).unwrap().def_id; + let trait_def = tcx.trait_def(trait_def_id); + + // This function may be called while we are still building the + // specialization graph that is queried below (via TraitDef::ancestors()), + // so, in order to avoid unnecessary infinite recursion, we manually look + // for the associated item at the given impl. + // If there is no such item in that impl, this function will fail with a + // cycle error if the specialization graph is currently being built. + if let Some(&impl_item_id) = tcx.impl_item_implementor_ids(impl_def_id).get(&assoc_def_id) { + let &item = tcx.associated_item(impl_item_id); + let impl_node = Node::Impl(impl_def_id); + return Ok(LeafDef { + item, + defining_node: impl_node, + finalizing_node: if item.defaultness(tcx).is_default() { + None + } else { + Some(impl_node) + }, + }); + } + + let ancestors = trait_def.ancestors(tcx, impl_def_id)?; + if let Some(assoc_item) = ancestors.leaf_def(tcx, assoc_def_id) { + Ok(assoc_item) + } else { + // This is saying that neither the trait nor + // the impl contain a definition for this + // associated type. Normally this situation + // could only arise through a compiler bug -- + // if the user wrote a bad item name, it + // should have failed in astconv. + bug!( + "No associated type `{}` for {}", + tcx.item_name(assoc_def_id), + tcx.def_path_str(impl_def_id) + ) + } +} diff --git a/src/test/ui/traits/solver-cycles/inductive-canonical-cycle.rs b/src/test/ui/traits/solver-cycles/inductive-canonical-cycle.rs new file mode 100644 index 000000000000..a3bb76d7e3b7 --- /dev/null +++ b/src/test/ui/traits/solver-cycles/inductive-canonical-cycle.rs @@ -0,0 +1,28 @@ +// known-bug + +// This should compile but fails with the current solver. +// +// This checks that the new solver uses `Ambiguous` when hitting the +// inductive cycle here when proving `exists<^0, ^1> (): Trait<^0, ^1>` +// which requires proving `Trait` but that has the same +// canonical representation. +trait Trait {} + +impl Trait for () +where + (): Trait, + T: OtherTrait, +{} + +trait OtherTrait {} +impl OtherTrait for u32 {} + +fn require_trait() +where + (): Trait +{} + +fn main() { + require_trait::<_, _>(); + //~^ ERROR overflow evaluating +} diff --git a/src/test/ui/traits/solver-cycles/inductive-canonical-cycle.stderr b/src/test/ui/traits/solver-cycles/inductive-canonical-cycle.stderr new file mode 100644 index 000000000000..e4b84e07822d --- /dev/null +++ b/src/test/ui/traits/solver-cycles/inductive-canonical-cycle.stderr @@ -0,0 +1,26 @@ +error[E0275]: overflow evaluating the requirement `_: Sized` + --> $DIR/inductive-canonical-cycle.rs:26:5 + | +LL | require_trait::<_, _>(); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inductive_canonical_cycle`) +note: required for `()` to implement `Trait<_, _>` + --> $DIR/inductive-canonical-cycle.rs:11:12 + | +LL | impl Trait for () + | ^^^^^^^^^^^ ^^ + = note: 128 redundant requirements hidden + = note: required for `()` to implement `Trait<_, _>` +note: required by a bound in `require_trait` + --> $DIR/inductive-canonical-cycle.rs:22:9 + | +LL | fn require_trait() + | ------------- required by a bound in this +LL | where +LL | (): Trait + | ^^^^^^^^^^^ required by this bound in `require_trait` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/triagebot.toml b/triagebot.toml index 46a3bab42a17..c7158a51861e 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -342,6 +342,10 @@ cc = ["@BoxyUwU"] message = "Some changes occured in `rustc_ty_utils::consts.rs`" cc = ["@BoxyUwU"] +[mentions."compiler/rustc_trait_selection/src/solve] +message = "Some changes occurred to the core trait solver" +cc = ["@lcnr"] + [mentions."compiler/rustc_trait_selection/src/traits/engine.rs"] message = """ Some changes occurred in engine.rs, potentially modifying the public API \ From 9ab04a6b0c0a357ae960f5a529a5f6df88f6e751 Mon Sep 17 00:00:00 2001 From: rustbot <47979223+rustbot@users.noreply.github.com> Date: Mon, 19 Dec 2022 12:01:02 -0500 Subject: [PATCH 210/321] Update books --- src/doc/nomicon | 2 +- src/doc/rust-by-example | 2 +- src/doc/rustc-dev-guide | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/doc/nomicon b/src/doc/nomicon index ae406aa5287a..dd37e21ccee4 160000 --- a/src/doc/nomicon +++ b/src/doc/nomicon @@ -1 +1 @@ -Subproject commit ae406aa5287a9e025abb72343aaceec98458c117 +Subproject commit dd37e21ccee43918ed18a71581bb2af537ffe4fc diff --git a/src/doc/rust-by-example b/src/doc/rust-by-example index a9869b4a3c4c..995df09b65c5 160000 --- a/src/doc/rust-by-example +++ b/src/doc/rust-by-example @@ -1 +1 @@ -Subproject commit a9869b4a3c4cac3bc6099b41f088679e268400b8 +Subproject commit 995df09b65c582eb6290ab7ea5d9485983eb4c37 diff --git a/src/doc/rustc-dev-guide b/src/doc/rustc-dev-guide index e269950a57fa..8b42eb5f57d3 160000 --- a/src/doc/rustc-dev-guide +++ b/src/doc/rustc-dev-guide @@ -1 +1 @@ -Subproject commit e269950a57fa6fcda356426545fb5aa3691a7ced +Subproject commit 8b42eb5f57d3d8ed2257a22d0e850d9db52afed3 From f24c04c3f8f4ac0ef318fa1c415aded1f781ebdf Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 19 Dec 2022 10:18:10 -0700 Subject: [PATCH 211/321] rustdoc: force pre tags to have the default line height Fixes #105906 --- src/librustdoc/html/static/css/rustdoc.css | 1 + src/test/rustdoc-gui/codeblock-sub.goml | 5 +++++ src/test/rustdoc-gui/src/test_docs/lib.rs | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 src/test/rustdoc-gui/codeblock-sub.goml diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 022ed606cc3b..d392cc5ad5df 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -342,6 +342,7 @@ code, pre, a.test-arrow, .code-header { } pre { padding: 14px; + line-height: 1.5; /* https://github.com/rust-lang/rust/issues/105906 */ } .item-decl pre { overflow-x: auto; diff --git a/src/test/rustdoc-gui/codeblock-sub.goml b/src/test/rustdoc-gui/codeblock-sub.goml new file mode 100644 index 000000000000..cbd314d27915 --- /dev/null +++ b/src/test/rustdoc-gui/codeblock-sub.goml @@ -0,0 +1,5 @@ +// Test that code blocks nested within do not have a line height of 0. +goto: "file://" + |DOC_PATH| + "/test_docs/codeblock_sub/index.html" + +store-property: (codeblock_sub_1, "#codeblock-sub-1", "offsetHeight") +assert-property-false: ("#codeblock-sub-3", { "offsetHeight": |codeblock_sub_1| }) diff --git a/src/test/rustdoc-gui/src/test_docs/lib.rs b/src/test/rustdoc-gui/src/test_docs/lib.rs index f1b69d4dc1d4..51250439694b 100644 --- a/src/test/rustdoc-gui/src/test_docs/lib.rs +++ b/src/test/rustdoc-gui/src/test_docs/lib.rs @@ -455,3 +455,22 @@ impl TypeWithImplDoc { /// fn doc pub fn test_fn() {} } + +/// +/// +/// ``` +/// one +/// ``` +/// +/// +/// +/// +/// +/// ``` +/// one +/// two +/// three +/// ``` +/// +/// +pub mod codeblock_sub {} From 96154d7fa7ced3184b69645ed8b6e7c210ca15b4 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 11 Dec 2022 21:16:43 +0000 Subject: [PATCH 212/321] Add IMPLIED_BOUNDS_ENTAILMENT lint --- .../src/check/compare_method.rs | 75 +++++++++++++++++-- compiler/rustc_infer/src/infer/mod.rs | 6 +- .../src/infer/outlives/obligations.rs | 3 +- compiler/rustc_lint_defs/src/builtin.rs | 41 ++++++++++ ...plied-bounds-compatibility-unnormalized.rs | 20 +++++ ...d-bounds-compatibility-unnormalized.stderr | 23 ++++++ .../impl-implied-bounds-compatibility.rs | 19 +++++ .../impl-implied-bounds-compatibility.stderr | 23 ++++++ 8 files changed, 203 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs create mode 100644 src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr create mode 100644 src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs create mode 100644 src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index b69728c24aa5..23d3e6041efe 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -255,15 +255,15 @@ fn compare_predicate_entailment<'tcx>( let mut wf_tys = FxIndexSet::default(); - let impl_sig = infcx.replace_bound_vars_with_fresh_vars( + let unnormalized_impl_sig = infcx.replace_bound_vars_with_fresh_vars( impl_m_span, infer::HigherRankedType, tcx.fn_sig(impl_m.def_id), ); + let unnormalized_impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(unnormalized_impl_sig)); let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id); - let impl_sig = ocx.normalize(&norm_cause, param_env, impl_sig); - let impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(impl_sig)); + let impl_fty = ocx.normalize(&norm_cause, param_env, unnormalized_impl_fty); debug!("compare_impl_method: impl_fty={:?}", impl_fty); let trait_sig = tcx.bound_fn_sig(trait_m.def_id).subst(tcx, trait_to_placeholder_substs); @@ -312,21 +312,86 @@ fn compare_predicate_entailment<'tcx>( return Err(reported); } + // FIXME(compiler-errors): This can be removed when IMPLIED_BOUNDS_ENTAILMENT + // becomes a hard error. + let lint_infcx = infcx.fork(); + // Finally, resolve all regions. This catches wily misuses of // lifetime parameters. let outlives_environment = OutlivesEnvironment::with_bounds( param_env, Some(infcx), - infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys), + infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys.clone()), ); - infcx.check_region_obligations_and_report_errors( + if let Some(guar) = infcx.check_region_obligations_and_report_errors( impl_m.def_id.expect_local(), &outlives_environment, + ) { + return Err(guar); + } + + // FIXME(compiler-errors): This can be simplified when IMPLIED_BOUNDS_ENTAILMENT + // becomes a hard error (i.e. ideally we'd just register a WF obligation above...) + lint_implied_wf_entailment( + impl_m.def_id.expect_local(), + lint_infcx, + param_env, + unnormalized_impl_fty, + wf_tys, ); Ok(()) } +fn lint_implied_wf_entailment<'tcx>( + impl_m_def_id: LocalDefId, + infcx: InferCtxt<'tcx>, + param_env: ty::ParamEnv<'tcx>, + unnormalized_impl_fty: Ty<'tcx>, + wf_tys: FxIndexSet>, +) { + let ocx = ObligationCtxt::new(&infcx); + + // We need to check that the impl's args are well-formed given + // the hybrid param-env (impl + trait method where-clauses). + ocx.register_obligation(traits::Obligation::new( + infcx.tcx, + ObligationCause::dummy(), + param_env, + ty::Binder::dummy(ty::PredicateKind::WellFormed(unnormalized_impl_fty.into())), + )); + + let hir_id = infcx.tcx.hir().local_def_id_to_hir_id(impl_m_def_id); + let lint = || { + infcx.tcx.struct_span_lint_hir( + rustc_session::lint::builtin::IMPLIED_BOUNDS_ENTAILMENT, + hir_id, + infcx.tcx.def_span(impl_m_def_id), + "impl method assumes more implied bounds than the corresponding trait method", + |lint| lint, + ); + }; + + let errors = ocx.select_all_or_error(); + if !errors.is_empty() { + lint(); + } + + let outlives_environment = OutlivesEnvironment::with_bounds( + param_env, + Some(&infcx), + infcx.implied_bounds_tys(param_env, hir_id, wf_tys.clone()), + ); + infcx.process_registered_region_obligations( + outlives_environment.region_bound_pairs(), + param_env, + ); + + if !infcx.resolve_regions(&outlives_environment).is_empty() { + lint(); + } +} + fn compare_asyncness<'tcx>( tcx: TyCtxt<'tcx>, impl_m: &ty::AssocItem, diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 268b3bf1dcd4..a9ef91db059a 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1693,7 +1693,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { &self, generic_param_scope: LocalDefId, outlives_env: &OutlivesEnvironment<'tcx>, - ) { + ) -> Option { let errors = self.resolve_regions(outlives_env); if let None = self.tainted_by_errors() { @@ -1704,6 +1704,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // errors from silly ones. self.report_region_errors(generic_param_scope, &errors); } + + (!errors.is_empty()).then(|| { + self.tcx.sess.delay_span_bug(rustc_span::DUMMY_SP, "error should have been emitted") + }) } // [Note-Type-error-reporting] diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index ccae7165d80d..47bd1564f082 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -68,6 +68,7 @@ use crate::infer::{ }; use crate::traits::{ObligationCause, ObligationCauseCode}; use rustc_data_structures::undo_log::UndoLogs; +use rustc_errors::ErrorGuaranteed; use rustc_hir::def_id::DefId; use rustc_hir::def_id::LocalDefId; use rustc_middle::mir::ConstraintCategory; @@ -177,7 +178,7 @@ impl<'tcx> InferCtxt<'tcx> { &self, generic_param_scope: LocalDefId, outlives_env: &OutlivesEnvironment<'tcx>, - ) { + ) -> Option { self.process_registered_region_obligations( outlives_env.region_bound_pairs(), outlives_env.param_env, diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 33cb35e60ebb..c88158da7634 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -3998,3 +3998,44 @@ declare_lint! { Warn, "named arguments in format used positionally" } + +declare_lint! { + /// The `implied_bounds_entailment` lint detects cases where the arguments of an impl method + /// have stronger implied bounds than those from the trait method it's implementing. + /// + /// ### Example + /// + /// ```rust,compile_fail + /// #![deny(implied_bounds_entailment)] + /// + /// trait Trait { + /// fn get<'s>(s: &'s str, _: &'static &'static ()) -> &'static str; + /// } + /// + /// impl Trait for () { + /// fn get<'s>(s: &'s str, _: &'static &'s ()) -> &'static str { + /// s + /// } + /// } + /// + /// let val = <() as Trait>::get(&String::from("blah blah blah"), &&()); + /// println!("{}", val); + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// Neither the trait method, which provides no implied bounds about `'s`, nor the impl, + /// which can't name `'s`, requires the main function to prove that 's: 'static, but the + /// impl method is able to assume that 's: 'static within its own body. + /// + /// This can be used to implement an unsound API if used incorrectly. + pub IMPLIED_BOUNDS_ENTAILMENT, + Deny, + "impl method assumes more implied bounds than its corresponding trait method", + @future_incompatible = FutureIncompatibleInfo { + reference: "issue #105572 ", + reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow, + }; +} diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs new file mode 100644 index 000000000000..cb5d83abfb6f --- /dev/null +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs @@ -0,0 +1,20 @@ +trait Project { + type Ty; +} +impl Project for &'_ &'_ () { + type Ty = (); +} +trait Trait { + fn get<'s>(s: &'s str, _: ()) -> &'static str; +} +impl Trait for () { + fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str { + //~^ ERROR impl method assumes more implied bounds than the corresponding trait method + //~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + s + } +} +fn main() { + let val = <() as Trait>::get(&String::from("blah blah blah"), ()); + println!("{}", val); +} diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr new file mode 100644 index 000000000000..5a757901777f --- /dev/null +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr @@ -0,0 +1,23 @@ +error: impl method assumes more implied bounds than the corresponding trait method + --> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5 + | +LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #105572 + = note: `#[deny(implied_bounds_entailment)]` on by default + +error: aborting due to previous error + +Future incompatibility report: Future breakage diagnostic: +error: impl method assumes more implied bounds than the corresponding trait method + --> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5 + | +LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #105572 + = note: `#[deny(implied_bounds_entailment)]` on by default + diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs new file mode 100644 index 000000000000..2d7cc38d2637 --- /dev/null +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs @@ -0,0 +1,19 @@ +use std::cell::RefCell; + +pub struct MessageListeners<'a> { + listeners: RefCell>>, +} + +pub trait MessageListenersInterface { + fn listeners<'c>(&'c self) -> &'c MessageListeners<'c>; +} + +impl<'a> MessageListenersInterface for MessageListeners<'a> { + fn listeners<'b>(&'b self) -> &'a MessageListeners<'b> { + //~^ ERROR impl method assumes more implied bounds than the corresponding trait method + //~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + self + } +} + +fn main() {} diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr new file mode 100644 index 000000000000..b7dbfc8ab8c7 --- /dev/null +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr @@ -0,0 +1,23 @@ +error: impl method assumes more implied bounds than the corresponding trait method + --> $DIR/impl-implied-bounds-compatibility.rs:12:5 + | +LL | fn listeners<'b>(&'b self) -> &'a MessageListeners<'b> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #105572 + = note: `#[deny(implied_bounds_entailment)]` on by default + +error: aborting due to previous error + +Future incompatibility report: Future breakage diagnostic: +error: impl method assumes more implied bounds than the corresponding trait method + --> $DIR/impl-implied-bounds-compatibility.rs:12:5 + | +LL | fn listeners<'b>(&'b self) -> &'a MessageListeners<'b> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #105572 + = note: `#[deny(implied_bounds_entailment)]` on by default + From 9c4cf8d9d0da26b74782bdb29443d50f34f46ed0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 16 Dec 2022 03:06:21 +0000 Subject: [PATCH 213/321] Make Clippy test no longer unsound --- .../clippy/tests/ui/borrow_interior_mutable_const/others.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs b/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs index eefeb1decb69..7c57864245a9 100644 --- a/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs +++ b/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs @@ -42,7 +42,7 @@ impl StaticRef { impl std::ops::Deref for StaticRef { type Target = T; - fn deref(&self) -> &'static T { + fn deref(&self) -> &T { unsafe { &*self.ptr } } } From c40ededa10439e64f20ee45f3cdd50d893f20438 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 16 Dec 2022 19:30:32 +0000 Subject: [PATCH 214/321] Downgrade IMPLIED_BOUNDS_ENTAILMENT to warn by default, add it to builtin lint list --- compiler/rustc_lint_defs/src/builtin.rs | 9 +++++---- ...plied-bounds-compatibility-unnormalized.rs | 2 ++ ...d-bounds-compatibility-unnormalized.stderr | 19 ++++++------------- .../impl-implied-bounds-compatibility.rs | 2 ++ .../impl-implied-bounds-compatibility.stderr | 19 ++++++------------- 5 files changed, 21 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index c88158da7634..f7a4103f4d5c 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -3311,6 +3311,7 @@ declare_lint_pass! { FFI_UNWIND_CALLS, REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS, NAMED_ARGUMENTS_USED_POSITIONALLY, + IMPLIED_BOUNDS_ENTAILMENT, ] } @@ -4027,15 +4028,15 @@ declare_lint! { /// ### Explanation /// /// Neither the trait method, which provides no implied bounds about `'s`, nor the impl, - /// which can't name `'s`, requires the main function to prove that 's: 'static, but the - /// impl method is able to assume that 's: 'static within its own body. + /// requires the main function to prove that 's: 'static, but the impl method is allowed + /// to assume that `'s: 'static` within its own body. /// /// This can be used to implement an unsound API if used incorrectly. pub IMPLIED_BOUNDS_ENTAILMENT, - Deny, + Warn, "impl method assumes more implied bounds than its corresponding trait method", @future_incompatible = FutureIncompatibleInfo { reference: "issue #105572 ", - reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow, + reason: FutureIncompatibilityReason::FutureReleaseError, }; } diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs index cb5d83abfb6f..6ccbb5bb2665 100644 --- a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.rs @@ -1,3 +1,5 @@ +#![deny(implied_bounds_entailment)] + trait Project { type Ty; } diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr index 5a757901777f..0ac31c642eb1 100644 --- a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr @@ -1,23 +1,16 @@ error: impl method assumes more implied bounds than the corresponding trait method - --> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5 + --> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:13:5 | LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #105572 - = note: `#[deny(implied_bounds_entailment)]` on by default +note: the lint level is defined here + --> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:1:9 + | +LL | #![deny(implied_bounds_entailment)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error -Future incompatibility report: Future breakage diagnostic: -error: impl method assumes more implied bounds than the corresponding trait method - --> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5 - | -LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #105572 - = note: `#[deny(implied_bounds_entailment)]` on by default - diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs index 2d7cc38d2637..d097bc16a221 100644 --- a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.rs @@ -1,3 +1,5 @@ +#![deny(implied_bounds_entailment)] + use std::cell::RefCell; pub struct MessageListeners<'a> { diff --git a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr index b7dbfc8ab8c7..0dfa8167a996 100644 --- a/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr +++ b/src/test/ui/implied-bounds/impl-implied-bounds-compatibility.stderr @@ -1,23 +1,16 @@ error: impl method assumes more implied bounds than the corresponding trait method - --> $DIR/impl-implied-bounds-compatibility.rs:12:5 + --> $DIR/impl-implied-bounds-compatibility.rs:14:5 | LL | fn listeners<'b>(&'b self) -> &'a MessageListeners<'b> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #105572 - = note: `#[deny(implied_bounds_entailment)]` on by default +note: the lint level is defined here + --> $DIR/impl-implied-bounds-compatibility.rs:1:9 + | +LL | #![deny(implied_bounds_entailment)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error -Future incompatibility report: Future breakage diagnostic: -error: impl method assumes more implied bounds than the corresponding trait method - --> $DIR/impl-implied-bounds-compatibility.rs:12:5 - | -LL | fn listeners<'b>(&'b self) -> &'a MessageListeners<'b> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #105572 - = note: `#[deny(implied_bounds_entailment)]` on by default - From 5457db94108b3c3ca75e1875b7e60bc4ea6b4b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 19 Dec 2022 15:41:36 +0000 Subject: [PATCH 215/321] add non-regression test for issue 105809 --- src/test/ui/mir/issue-105809.rs | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/ui/mir/issue-105809.rs diff --git a/src/test/ui/mir/issue-105809.rs b/src/test/ui/mir/issue-105809.rs new file mode 100644 index 000000000000..57828feef2d6 --- /dev/null +++ b/src/test/ui/mir/issue-105809.rs @@ -0,0 +1,36 @@ +// Non-regression test ICE from issue #105809 and duplicates. + +// build-pass: the ICE is during codegen +// compile-flags: --edition 2018 -Zmir-opt-level=1 + +use std::{future::Future, pin::Pin}; + +// Create a `T` without affecting analysis like `loop {}`. +fn create() -> T { + loop {} +} + +async fn trivial_future() {} + +struct Connection { + _h: H, +} + +async fn complex_future(conn: &Connection) { + let small_fut = async move { + let _ = conn; + trivial_future().await; + }; + + let mut tuple = (small_fut,); + let (small_fut_again,) = &mut tuple; + let _ = small_fut_again; +} + +fn main() { + let mut fut = complex_future(&Connection { _h: () }); + + let mut cx = create(); + let future = unsafe { Pin::new_unchecked(&mut fut) }; + let _ = future.poll(&mut cx); +} From 581cbe4135995e2854587d88fbec20182d1cf714 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 19 Dec 2022 11:40:22 -0700 Subject: [PATCH 216/321] rustdoc: remove width-limiter from source pages, stop overriding CSS --- src/librustdoc/html/static/css/rustdoc.css | 4 ---- src/librustdoc/html/templates/page.html | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 022ed606cc3b..efb19467c870 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -322,10 +322,6 @@ main { margin-right: auto; } -.source .width-limiter { - max-width: unset; -} - details:not(.rustdoc-toggle) summary { margin-bottom: .6em; } diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/html/templates/page.html index aa3bf827db4b..bcaff957af2f 100644 --- a/src/librustdoc/html/templates/page.html +++ b/src/librustdoc/html/templates/page.html @@ -99,7 +99,7 @@ {{- sidebar|safe -}} {#- -#}

{#- -#} -
{#- -#} + {%- if page.css_class != "source" -%}
{%- endif -%} {#- -#}
{{- content|safe -}}
{#- -#} -
{#- -#} + {%- if page.css_class != "source" -%}
{%- endif -%}
{#- -#} {{- layout.external_html.after_content|safe -}}
Date: Mon, 19 Dec 2022 19:53:09 +0100 Subject: [PATCH 217/321] Simplify CSS for code examples code blocks --- src/librustdoc/html/static/css/rustdoc.css | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 022ed606cc3b..f376258105ae 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1976,10 +1976,7 @@ in storage.js } .scraped-example .code-wrapper .example-wrap { - display: grid; - grid-template-columns: max-content auto; width: 100%; - overflow-x: auto; overflow-y: hidden; margin-bottom: 0; } @@ -1988,13 +1985,6 @@ in storage.js overflow-x: hidden; } -.scraped-example .code-wrapper .example-wrap pre.rust { - overflow-x: inherit; - width: inherit; - overflow-y: hidden; -} - - .more-examples-toggle { max-width: calc(100% + 25px); margin-top: 10px; From 8c86773fd37b26924d53cea518a136860e620265 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 19 Dec 2022 19:04:55 +0000 Subject: [PATCH 218/321] Make fast-path for implied wf lint better --- .../src/check/compare_method.rs | 163 ++++++++++-------- 1 file changed, 92 insertions(+), 71 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index 23d3e6041efe..cddd307c13db 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -71,8 +71,14 @@ pub(crate) fn compare_impl_method<'tcx>( return; } - if let Err(_) = compare_predicate_entailment(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref) - { + if let Err(_) = compare_predicate_entailment( + tcx, + impl_m, + impl_m_span, + trait_m, + impl_trait_ref, + CheckImpliedWfMode::Check, + ) { return; } } @@ -150,6 +156,7 @@ fn compare_predicate_entailment<'tcx>( impl_m_span: Span, trait_m: &ty::AssocItem, impl_trait_ref: ty::TraitRef<'tcx>, + check_implied_wf: CheckImpliedWfMode, ) -> Result<(), ErrorGuaranteed> { let trait_to_impl_substs = impl_trait_ref.substs; @@ -304,92 +311,106 @@ fn compare_predicate_entailment<'tcx>( return Err(emitted); } - // Check that all obligations are satisfied by the implementation's - // version. - let errors = ocx.select_all_or_error(); - if !errors.is_empty() { - let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None); - return Err(reported); + if check_implied_wf == CheckImpliedWfMode::Check { + // We need to check that the impl's args are well-formed given + // the hybrid param-env (impl + trait method where-clauses). + ocx.register_obligation(traits::Obligation::new( + infcx.tcx, + ObligationCause::dummy(), + param_env, + ty::Binder::dummy(ty::PredicateKind::WellFormed(unnormalized_impl_fty.into())), + )); } - - // FIXME(compiler-errors): This can be removed when IMPLIED_BOUNDS_ENTAILMENT - // becomes a hard error. - let lint_infcx = infcx.fork(); - - // Finally, resolve all regions. This catches wily misuses of - // lifetime parameters. - let outlives_environment = OutlivesEnvironment::with_bounds( - param_env, - Some(infcx), - infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys.clone()), - ); - if let Some(guar) = infcx.check_region_obligations_and_report_errors( - impl_m.def_id.expect_local(), - &outlives_environment, - ) { - return Err(guar); - } - - // FIXME(compiler-errors): This can be simplified when IMPLIED_BOUNDS_ENTAILMENT - // becomes a hard error (i.e. ideally we'd just register a WF obligation above...) - lint_implied_wf_entailment( - impl_m.def_id.expect_local(), - lint_infcx, - param_env, - unnormalized_impl_fty, - wf_tys, - ); - - Ok(()) -} - -fn lint_implied_wf_entailment<'tcx>( - impl_m_def_id: LocalDefId, - infcx: InferCtxt<'tcx>, - param_env: ty::ParamEnv<'tcx>, - unnormalized_impl_fty: Ty<'tcx>, - wf_tys: FxIndexSet>, -) { - let ocx = ObligationCtxt::new(&infcx); - - // We need to check that the impl's args are well-formed given - // the hybrid param-env (impl + trait method where-clauses). - ocx.register_obligation(traits::Obligation::new( - infcx.tcx, - ObligationCause::dummy(), - param_env, - ty::Binder::dummy(ty::PredicateKind::WellFormed(unnormalized_impl_fty.into())), - )); - - let hir_id = infcx.tcx.hir().local_def_id_to_hir_id(impl_m_def_id); - let lint = || { + let emit_implied_wf_lint = || { infcx.tcx.struct_span_lint_hir( rustc_session::lint::builtin::IMPLIED_BOUNDS_ENTAILMENT, - hir_id, - infcx.tcx.def_span(impl_m_def_id), + impl_m_hir_id, + infcx.tcx.def_span(impl_m.def_id), "impl method assumes more implied bounds than the corresponding trait method", |lint| lint, ); }; + // Check that all obligations are satisfied by the implementation's + // version. let errors = ocx.select_all_or_error(); if !errors.is_empty() { - lint(); + match check_implied_wf { + CheckImpliedWfMode::Check => { + return compare_predicate_entailment( + tcx, + impl_m, + impl_m_span, + trait_m, + impl_trait_ref, + CheckImpliedWfMode::Skip, + ) + .map(|()| { + // If the skip-mode was successful, emit a lint. + emit_implied_wf_lint(); + }); + } + CheckImpliedWfMode::Skip => { + let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None); + return Err(reported); + } + } } - let outlives_environment = OutlivesEnvironment::with_bounds( + // Finally, resolve all regions. This catches wily misuses of + // lifetime parameters. + let outlives_env = OutlivesEnvironment::with_bounds( param_env, - Some(&infcx), - infcx.implied_bounds_tys(param_env, hir_id, wf_tys.clone()), + Some(infcx), + infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys.clone()), ); infcx.process_registered_region_obligations( - outlives_environment.region_bound_pairs(), - param_env, + outlives_env.region_bound_pairs(), + outlives_env.param_env, ); - - if !infcx.resolve_regions(&outlives_environment).is_empty() { - lint(); + let errors = infcx.resolve_regions(&outlives_env); + if !errors.is_empty() { + // FIXME(compiler-errors): This can be simplified when IMPLIED_BOUNDS_ENTAILMENT + // becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors` + match check_implied_wf { + CheckImpliedWfMode::Check => { + return compare_predicate_entailment( + tcx, + impl_m, + impl_m_span, + trait_m, + impl_trait_ref, + CheckImpliedWfMode::Skip, + ) + .map(|()| { + // If the skip-mode was successful, emit a lint. + emit_implied_wf_lint(); + }); + } + CheckImpliedWfMode::Skip => { + if infcx.tainted_by_errors().is_none() { + infcx.err_ctxt().report_region_errors(impl_m.def_id.expect_local(), &errors); + } + return Err(tcx + .sess + .delay_span_bug(rustc_span::DUMMY_SP, "error should have been emitted")); + } + } } + + Ok(()) +} + +#[derive(Debug, PartialEq, Eq)] +enum CheckImpliedWfMode { + /// Checks implied well-formedness of the impl method. If it fails, we will + /// re-check with `Skip`, and emit a lint if it succeeds. + Check, + /// Skips checking implied well-formedness of the impl method, but will emit + /// a lint if the `compare_predicate_entailment` succeeded. This means that + /// the reason that we had failed earlier during `Check` was due to the impl + /// having stronger requirements than the trait. + Skip, } fn compare_asyncness<'tcx>( From 8441ca5d818fa7c34eb77d523a2d28a3850b8ff8 Mon Sep 17 00:00:00 2001 From: Andrew Pollack Date: Mon, 19 Dec 2022 11:24:59 -0800 Subject: [PATCH 219/321] Revert "Replace usage of `ResumeTy` in async lowering with `Context`" --- compiler/rustc_ast_lowering/src/expr.rs | 57 +++++++------------ compiler/rustc_hir/src/lang_items.rs | 3 +- compiler/rustc_span/src/symbol.rs | 3 +- library/core/src/future/mod.rs | 10 +--- library/core/src/task/wake.rs | 1 - .../async-await-let-else.drop-tracking.stderr | 2 +- .../issue-68112.drop_tracking.stderr | 2 +- .../issue-68112.no_drop_tracking.stderr | 2 +- .../issue-69446-fnmut-capture.stderr | 3 - ...e-70935-complex-spans.drop_tracking.stderr | 2 +- ...l-drop-partial-reinit.drop_tracking.stderr | 2 +- ...rop-partial-reinit.no_drop_tracking.stderr | 2 +- .../closure-in-projection-issue-97405.rs | 4 +- .../closure-in-projection-issue-97405.stderr | 20 +++---- 14 files changed, 43 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index e86e807279d6..a3f5c18f2e75 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -16,7 +16,7 @@ use rustc_hir::def::Res; use rustc_hir::definitions::DefPathData; use rustc_session::errors::report_lit_error; use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned}; -use rustc_span::symbol::{kw, sym, Ident}; +use rustc_span::symbol::{sym, Ident}; use rustc_span::DUMMY_SP; use thin_vec::thin_vec; @@ -596,38 +596,14 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> hir::ExprKind<'hir> { let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span))); - // Resume argument type, which should be `&mut Context<'_>`. - // NOTE: Using the `'static` lifetime here is technically cheating. - // The `Future::poll` argument really is `&'a mut Context<'b>`, but we cannot - // express the fact that we are not storing it across yield-points yet, - // and we would thus run into lifetime errors. - // See . - // Our lowering makes sure we are not mis-using the `_task_context` input type - // in the sense that we are indeed not using it across yield points. We - // get a fresh `&mut Context` for each resume / call of `Future::poll`. - // This "cheating" was previously done with a `ResumeTy` that contained a raw - // pointer, and a `get_context` accessor that pulled the `Context` lifetimes - // out of thin air. - let context_lifetime_ident = Ident::with_dummy_span(kw::StaticLifetime); - let context_lifetime = self.arena.alloc(hir::Lifetime { - hir_id: self.next_id(), - ident: context_lifetime_ident, - res: hir::LifetimeName::Static, - }); - let context_path = - hir::QPath::LangItem(hir::LangItem::Context, self.lower_span(span), None); - let context_ty = hir::MutTy { - ty: self.arena.alloc(hir::Ty { - hir_id: self.next_id(), - kind: hir::TyKind::Path(context_path), - span: self.lower_span(span), - }), - mutbl: hir::Mutability::Mut, - }; + // Resume argument type: `ResumeTy` + let unstable_span = + self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone()); + let resume_ty = hir::QPath::LangItem(hir::LangItem::ResumeTy, unstable_span, None); let input_ty = hir::Ty { hir_id: self.next_id(), - kind: hir::TyKind::Rptr(context_lifetime, context_ty), - span: self.lower_span(span), + kind: hir::TyKind::Path(resume_ty), + span: unstable_span, }; // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`. @@ -686,9 +662,12 @@ impl<'hir> LoweringContext<'_, 'hir> { .map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))); let hir_id = self.lower_node_id(closure_node_id); - let unstable_span = - self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone()); if track_caller { + let unstable_span = self.mark_span_with_reason( + DesugaringKind::Async, + span, + self.allow_gen_future.clone(), + ); self.lower_attrs( hir_id, &[Attribute { @@ -731,7 +710,7 @@ impl<'hir> LoweringContext<'_, 'hir> { /// mut __awaitee => loop { /// match unsafe { ::std::future::Future::poll( /// <::std::pin::Pin>::new_unchecked(&mut __awaitee), - /// task_context, + /// ::std::future::get_context(task_context), /// ) } { /// ::std::task::Poll::Ready(result) => break result, /// ::std::task::Poll::Pending => {} @@ -772,7 +751,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // unsafe { // ::std::future::Future::poll( // ::std::pin::Pin::new_unchecked(&mut __awaitee), - // task_context, + // ::std::future::get_context(task_context), // ) // } let poll_expr = { @@ -790,10 +769,16 @@ impl<'hir> LoweringContext<'_, 'hir> { arena_vec![self; ref_mut_awaitee], Some(expr_hir_id), ); + let get_context = self.expr_call_lang_item_fn_mut( + gen_future_span, + hir::LangItem::GetContext, + arena_vec![self; task_context], + Some(expr_hir_id), + ); let call = self.expr_call_lang_item_fn( span, hir::LangItem::FuturePoll, - arena_vec![self; new_unchecked, task_context], + arena_vec![self; new_unchecked, get_context], Some(expr_hir_id), ); self.arena.alloc(self.expr_unsafe(call)) diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index b51257df713e..038509031b18 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -286,9 +286,10 @@ language_item_table! { // FIXME(swatinem): the following lang items are used for async lowering and // should become obsolete eventually. + ResumeTy, sym::ResumeTy, resume_ty, Target::Struct, GenericRequirement::None; IdentityFuture, sym::identity_future, identity_future_fn, Target::Fn, GenericRequirement::None; + GetContext, sym::get_context, get_context_fn, Target::Fn, GenericRequirement::None; - Context, sym::Context, context, Target::Struct, GenericRequirement::None; FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None; FromFrom, sym::from, from_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None; diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index ace095736c92..9de6d9dc483c 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -164,7 +164,6 @@ symbols! { Capture, Center, Clone, - Context, Continue, Copy, Count, @@ -264,6 +263,7 @@ symbols! { Relaxed, Release, Result, + ResumeTy, Return, Right, Rust, @@ -753,6 +753,7 @@ symbols! { generic_associated_types_extended, generic_const_exprs, generic_param_attrs, + get_context, global_allocator, global_asm, globs, diff --git a/library/core/src/future/mod.rs b/library/core/src/future/mod.rs index 2a8e12fd4cf6..f2b961d62e00 100644 --- a/library/core/src/future/mod.rs +++ b/library/core/src/future/mod.rs @@ -44,7 +44,7 @@ pub use poll_fn::{poll_fn, PollFn}; /// non-Send/Sync as well, and we don't want that. /// /// It also simplifies the HIR lowering of `.await`. -// FIXME(swatinem): This type can be removed when bumping the bootstrap compiler +#[cfg_attr(not(bootstrap), lang = "ResumeTy")] #[doc(hidden)] #[unstable(feature = "gen_future", issue = "50547")] #[derive(Debug, Copy, Clone)] @@ -61,7 +61,6 @@ unsafe impl Sync for ResumeTy {} /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`). // This is `const` to avoid extra errors after we recover from `const async fn` -// FIXME(swatinem): This fn can be removed when bumping the bootstrap compiler #[cfg_attr(bootstrap, lang = "from_generator")] #[doc(hidden)] #[unstable(feature = "gen_future", issue = "50547")] @@ -103,8 +102,7 @@ where GenFuture(gen) } -// FIXME(swatinem): This fn can be removed when bumping the bootstrap compiler -#[cfg_attr(bootstrap, lang = "get_context")] +#[lang = "get_context"] #[doc(hidden)] #[unstable(feature = "gen_future", issue = "50547")] #[must_use] @@ -115,10 +113,6 @@ pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> { unsafe { &mut *cx.0.as_ptr().cast() } } -// FIXME(swatinem): This fn is currently needed to work around shortcomings -// in type and lifetime inference. -// See the comment at the bottom of `LoweringContext::make_async_expr` and -// . #[cfg_attr(not(bootstrap), lang = "identity_future")] #[doc(hidden)] #[unstable(feature = "gen_future", issue = "50547")] diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 9ab9b0ba1c79..0cff972df3a5 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -174,7 +174,6 @@ impl RawWakerVTable { /// Currently, `Context` only serves to provide access to a [`&Waker`](Waker) /// which can be used to wake the current task. #[stable(feature = "futures_api", since = "1.36.0")] -#[cfg_attr(not(bootstrap), lang = "Context")] pub struct Context<'a> { waker: &'a Waker, // Ensure we future-proof against variance changes by forcing diff --git a/src/test/ui/async-await/async-await-let-else.drop-tracking.stderr b/src/test/ui/async-await/async-await-let-else.drop-tracking.stderr index f0f5245a3b42..fb83ca90a378 100644 --- a/src/test/ui/async-await/async-await-let-else.drop-tracking.stderr +++ b/src/test/ui/async-await/async-await-let-else.drop-tracking.stderr @@ -40,7 +40,7 @@ LL | async fn bar2(_: T) -> ! { LL | | panic!() LL | | } | |_^ - = note: required because it captures the following types: `&mut Context<'_>`, `Option`, `impl Future`, `()` + = note: required because it captures the following types: `ResumeTy`, `Option`, `impl Future`, `()` note: required because it's used within this `async fn` body --> $DIR/async-await-let-else.rs:21:32 | diff --git a/src/test/ui/async-await/issue-68112.drop_tracking.stderr b/src/test/ui/async-await/issue-68112.drop_tracking.stderr index 1c90bedae790..f2802698fd5b 100644 --- a/src/test/ui/async-await/issue-68112.drop_tracking.stderr +++ b/src/test/ui/async-await/issue-68112.drop_tracking.stderr @@ -57,7 +57,7 @@ note: required because it appears within the type `impl Future impl Future>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: required because it captures the following types: `&mut Context<'_>`, `impl Future>>`, `()`, `Ready` + = note: required because it captures the following types: `ResumeTy`, `impl Future>>`, `()`, `Ready` note: required because it's used within this `async` block --> $DIR/issue-68112.rs:60:20 | diff --git a/src/test/ui/async-await/issue-68112.no_drop_tracking.stderr b/src/test/ui/async-await/issue-68112.no_drop_tracking.stderr index e09ae7fedd80..38eb85b302fd 100644 --- a/src/test/ui/async-await/issue-68112.no_drop_tracking.stderr +++ b/src/test/ui/async-await/issue-68112.no_drop_tracking.stderr @@ -57,7 +57,7 @@ note: required because it appears within the type `impl Future impl Future>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: required because it captures the following types: `&mut Context<'_>`, `impl Future>>`, `()`, `i32`, `Ready` + = note: required because it captures the following types: `ResumeTy`, `impl Future>>`, `()`, `i32`, `Ready` note: required because it's used within this `async` block --> $DIR/issue-68112.rs:60:20 | diff --git a/src/test/ui/async-await/issue-69446-fnmut-capture.stderr b/src/test/ui/async-await/issue-69446-fnmut-capture.stderr index e6ad2f0d444b..3d2b0402bc52 100644 --- a/src/test/ui/async-await/issue-69446-fnmut-capture.stderr +++ b/src/test/ui/async-await/issue-69446-fnmut-capture.stderr @@ -14,9 +14,6 @@ LL | | }); | = note: `FnMut` closures only have access to their captured variables while they are executing... = note: ...therefore, they cannot allow references to captured variables to escape - = note: requirement occurs because of a mutable reference to `Context<'_>` - = note: mutable references are invariant over their type parameter - = help: see for more information about variance error: aborting due to previous error diff --git a/src/test/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr b/src/test/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr index a8fd97cde8f7..721234aa4a78 100644 --- a/src/test/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr +++ b/src/test/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr @@ -18,7 +18,7 @@ LL | async fn baz(_c: impl FnMut() -> T) where T: Future { | ___________________________________________________________________^ LL | | } | |_^ - = note: required because it captures the following types: `&mut Context<'_>`, `impl Future`, `()` + = note: required because it captures the following types: `ResumeTy`, `impl Future`, `()` note: required because it's used within this `async` block --> $DIR/issue-70935-complex-spans.rs:16:5 | diff --git a/src/test/ui/async-await/partial-drop-partial-reinit.drop_tracking.stderr b/src/test/ui/async-await/partial-drop-partial-reinit.drop_tracking.stderr index 25876d508401..17b4ef7bdc67 100644 --- a/src/test/ui/async-await/partial-drop-partial-reinit.drop_tracking.stderr +++ b/src/test/ui/async-await/partial-drop-partial-reinit.drop_tracking.stderr @@ -11,7 +11,7 @@ LL | async fn foo() { | = help: within `impl Future`, the trait `Send` is not implemented for `NotSend` = note: required because it appears within the type `(NotSend,)` - = note: required because it captures the following types: `&mut Context<'_>`, `(NotSend,)`, `()`, `impl Future` + = note: required because it captures the following types: `ResumeTy`, `(NotSend,)`, `()`, `impl Future` note: required because it's used within this `async fn` body --> $DIR/partial-drop-partial-reinit.rs:31:16 | diff --git a/src/test/ui/async-await/partial-drop-partial-reinit.no_drop_tracking.stderr b/src/test/ui/async-await/partial-drop-partial-reinit.no_drop_tracking.stderr index dba2a620779f..34d8a159f106 100644 --- a/src/test/ui/async-await/partial-drop-partial-reinit.no_drop_tracking.stderr +++ b/src/test/ui/async-await/partial-drop-partial-reinit.no_drop_tracking.stderr @@ -11,7 +11,7 @@ LL | async fn foo() { | = help: within `impl Future`, the trait `Send` is not implemented for `NotSend` = note: required because it appears within the type `(NotSend,)` - = note: required because it captures the following types: `&mut Context<'_>`, `(NotSend,)`, `impl Future`, `()` + = note: required because it captures the following types: `ResumeTy`, `(NotSend,)`, `impl Future`, `()` note: required because it's used within this `async fn` body --> $DIR/partial-drop-partial-reinit.rs:31:16 | diff --git a/src/test/ui/regions/closure-in-projection-issue-97405.rs b/src/test/ui/regions/closure-in-projection-issue-97405.rs index 88b1c1396516..e567d5c2723f 100644 --- a/src/test/ui/regions/closure-in-projection-issue-97405.rs +++ b/src/test/ui/regions/closure-in-projection-issue-97405.rs @@ -22,11 +22,11 @@ fn good_generic_fn() { // This should fail because `T` ends up in the upvars of the closure. fn bad_generic_fn(t: T) { assert_static(opaque(async move { t; }).next()); - //~^ ERROR the parameter type `T` may not live long enough + //~^ ERROR the associated type `::Item` may not live long enough assert_static(opaque(move || { t; }).next()); //~^ ERROR the associated type `::Item` may not live long enough assert_static(opaque(opaque(async move { t; }).next()).next()); - //~^ ERROR the parameter type `T` may not live long enough + //~^ ERROR the associated type `::Item` may not live long enough } fn main() {} diff --git a/src/test/ui/regions/closure-in-projection-issue-97405.stderr b/src/test/ui/regions/closure-in-projection-issue-97405.stderr index 907964aaf379..c08f1059ebf5 100644 --- a/src/test/ui/regions/closure-in-projection-issue-97405.stderr +++ b/src/test/ui/regions/closure-in-projection-issue-97405.stderr @@ -1,13 +1,11 @@ -error[E0310]: the parameter type `T` may not live long enough +error[E0310]: the associated type `::Item` may not live long enough --> $DIR/closure-in-projection-issue-97405.rs:24:5 | LL | assert_static(opaque(async move { t; }).next()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: consider adding an explicit lifetime bound... - | -LL | fn bad_generic_fn(t: T) { - | +++++++++ + = help: consider adding an explicit lifetime bound `::Item: 'static`... + = note: ...so that the type `::Item` will meet its required lifetime bounds error[E0310]: the associated type `::Item` may not live long enough --> $DIR/closure-in-projection-issue-97405.rs:26:5 @@ -18,16 +16,14 @@ LL | assert_static(opaque(move || { t; }).next()); = help: consider adding an explicit lifetime bound `::Item: 'static`... = note: ...so that the type `::Item` will meet its required lifetime bounds -error[E0310]: the parameter type `T` may not live long enough +error[E0310]: the associated type `::Item` may not live long enough --> $DIR/closure-in-projection-issue-97405.rs:28:5 | LL | assert_static(opaque(opaque(async move { t; }).next()).next()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | -help: consider adding an explicit lifetime bound... - | -LL | fn bad_generic_fn(t: T) { - | +++++++++ + = help: consider adding an explicit lifetime bound `::Item: 'static`... + = note: ...so that the type `::Item` will meet its required lifetime bounds error: aborting due to 3 previous errors From eb22d70aed39614dd9318e3fa1bf91846d74b8aa Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Tue, 6 Dec 2022 17:36:09 +0100 Subject: [PATCH 220/321] Implement va_list and va_arg for s390x FFI Following the s390x ELF ABI and based on the clang implementation, provide appropriate definitions of va_list in library/core/src/ffi/mod.rs and va_arg handling in compiler/rustc_codegen_llvm/src/va_arg.rs. Fixes the following test cases on s390x: src/test/run-make-fulldeps/c-link-to-rust-va-list-fn src/test/ui/abi/variadic-ffi.rs Fixes https://github.com/rust-lang/rust/issues/84628. --- compiler/rustc_codegen_llvm/src/va_arg.rs | 84 +++++++++++++++++++++++ library/core/src/ffi/mod.rs | 55 +++++++++++++-- 2 files changed, 134 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/va_arg.rs b/compiler/rustc_codegen_llvm/src/va_arg.rs index ceb3d5a84abf..b19398e68c26 100644 --- a/compiler/rustc_codegen_llvm/src/va_arg.rs +++ b/compiler/rustc_codegen_llvm/src/va_arg.rs @@ -175,6 +175,89 @@ fn emit_aapcs_va_arg<'ll, 'tcx>( val } +fn emit_s390x_va_arg<'ll, 'tcx>( + bx: &mut Builder<'_, 'll, 'tcx>, + list: OperandRef<'tcx, &'ll Value>, + target_ty: Ty<'tcx>, +) -> &'ll Value { + // Implementation of the s390x ELF ABI calling convention for va_args see + // https://github.com/IBM/s390x-abi (chapter 1.2.4) + let va_list_addr = list.immediate(); + let va_list_layout = list.deref(bx.cx).layout; + let va_list_ty = va_list_layout.llvm_type(bx); + let layout = bx.cx.layout_of(target_ty); + + let in_reg = bx.append_sibling_block("va_arg.in_reg"); + let in_mem = bx.append_sibling_block("va_arg.in_mem"); + let end = bx.append_sibling_block("va_arg.end"); + + // FIXME: vector ABI not yet supported. + let target_ty_size = bx.cx.size_of(target_ty).bytes(); + let indirect: bool = target_ty_size > 8 || !target_ty_size.is_power_of_two(); + let unpadded_size = if indirect { 8 } else { target_ty_size }; + let padded_size = 8; + let padding = padded_size - unpadded_size; + + let gpr_type = indirect || !layout.is_single_fp_element(bx.cx); + let (max_regs, reg_count_field, reg_save_index, reg_padding) = + if gpr_type { (5, 0, 2, padding) } else { (4, 1, 16, 0) }; + + // Check whether the value was passed in a register or in memory. + let reg_count = bx.struct_gep( + va_list_ty, + va_list_addr, + va_list_layout.llvm_field_index(bx.cx, reg_count_field), + ); + let reg_count_v = bx.load(bx.type_i64(), reg_count, Align::from_bytes(8).unwrap()); + let use_regs = bx.icmp(IntPredicate::IntULT, reg_count_v, bx.const_u64(max_regs)); + bx.cond_br(use_regs, in_reg, in_mem); + + // Emit code to load the value if it was passed in a register. + bx.switch_to_block(in_reg); + + // Work out the address of the value in the register save area. + let reg_ptr = + bx.struct_gep(va_list_ty, va_list_addr, va_list_layout.llvm_field_index(bx.cx, 3)); + let reg_ptr_v = bx.load(bx.type_i8p(), reg_ptr, bx.tcx().data_layout.pointer_align.abi); + let scaled_reg_count = bx.mul(reg_count_v, bx.const_u64(8)); + let reg_off = bx.add(scaled_reg_count, bx.const_u64(reg_save_index * 8 + reg_padding)); + let reg_addr = bx.gep(bx.type_i8(), reg_ptr_v, &[reg_off]); + + // Update the register count. + let new_reg_count_v = bx.add(reg_count_v, bx.const_u64(1)); + bx.store(new_reg_count_v, reg_count, Align::from_bytes(8).unwrap()); + bx.br(end); + + // Emit code to load the value if it was passed in memory. + bx.switch_to_block(in_mem); + + // Work out the address of the value in the argument overflow area. + let arg_ptr = + bx.struct_gep(va_list_ty, va_list_addr, va_list_layout.llvm_field_index(bx.cx, 2)); + let arg_ptr_v = bx.load(bx.type_i8p(), arg_ptr, bx.tcx().data_layout.pointer_align.abi); + let arg_off = bx.const_u64(padding); + let mem_addr = bx.gep(bx.type_i8(), arg_ptr_v, &[arg_off]); + + // Update the argument overflow area pointer. + let arg_size = bx.cx().const_u64(padded_size); + let new_arg_ptr_v = bx.inbounds_gep(bx.type_i8(), arg_ptr_v, &[arg_size]); + bx.store(new_arg_ptr_v, arg_ptr, bx.tcx().data_layout.pointer_align.abi); + bx.br(end); + + // Return the appropriate result. + bx.switch_to_block(end); + let val_addr = bx.phi(bx.type_i8p(), &[reg_addr, mem_addr], &[in_reg, in_mem]); + let val_type = layout.llvm_type(bx); + let val_addr = if indirect { + let ptr_type = bx.cx.type_ptr_to(val_type); + let ptr_addr = bx.bitcast(val_addr, bx.cx.type_ptr_to(ptr_type)); + bx.load(ptr_type, ptr_addr, bx.tcx().data_layout.pointer_align.abi) + } else { + bx.bitcast(val_addr, bx.cx.type_ptr_to(val_type)) + }; + bx.load(val_type, val_addr, layout.align.abi) +} + pub(super) fn emit_va_arg<'ll, 'tcx>( bx: &mut Builder<'_, 'll, 'tcx>, addr: OperandRef<'tcx, &'ll Value>, @@ -200,6 +283,7 @@ pub(super) fn emit_va_arg<'ll, 'tcx>( emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), true) } "aarch64" => emit_aapcs_va_arg(bx, addr, target_ty), + "s390x" => emit_s390x_va_arg(bx, addr, target_ty), // Windows x86_64 "x86_64" if target.is_like_windows => { let target_ty_size = bx.cx.size_of(target_ty).bytes(); diff --git a/library/core/src/ffi/mod.rs b/library/core/src/ffi/mod.rs index ec1eaa99f0b8..76daceecd7be 100644 --- a/library/core/src/ffi/mod.rs +++ b/library/core/src/ffi/mod.rs @@ -227,7 +227,12 @@ impl fmt::Debug for c_void { /// Basic implementation of a `va_list`. // The name is WIP, using `VaListImpl` for now. #[cfg(any( - all(not(target_arch = "aarch64"), not(target_arch = "powerpc"), not(target_arch = "x86_64")), + all( + not(target_arch = "aarch64"), + not(target_arch = "powerpc"), + not(target_arch = "s390x"), + not(target_arch = "x86_64") + ), all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")), target_family = "wasm", target_arch = "asmjs", @@ -251,7 +256,12 @@ pub struct VaListImpl<'f> { } #[cfg(any( - all(not(target_arch = "aarch64"), not(target_arch = "powerpc"), not(target_arch = "x86_64")), + all( + not(target_arch = "aarch64"), + not(target_arch = "powerpc"), + not(target_arch = "s390x"), + not(target_arch = "x86_64") + ), all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")), target_family = "wasm", target_arch = "asmjs", @@ -319,6 +329,25 @@ pub struct VaListImpl<'f> { _marker: PhantomData<&'f mut &'f c_void>, } +/// s390x ABI implementation of a `va_list`. +#[cfg(target_arch = "s390x")] +#[repr(C)] +#[derive(Debug)] +#[unstable( + feature = "c_variadic", + reason = "the `c_variadic` feature has not been properly tested on \ + all supported platforms", + issue = "44930" +)] +#[lang = "va_list"] +pub struct VaListImpl<'f> { + gpr: i64, + fpr: i64, + overflow_arg_area: *mut c_void, + reg_save_area: *mut c_void, + _marker: PhantomData<&'f mut &'f c_void>, +} + /// x86_64 ABI implementation of a `va_list`. #[cfg(all(target_arch = "x86_64", not(target_os = "uefi"), not(windows)))] #[repr(C)] @@ -352,6 +381,7 @@ pub struct VaList<'a, 'f: 'a> { all( not(target_arch = "aarch64"), not(target_arch = "powerpc"), + not(target_arch = "s390x"), not(target_arch = "x86_64") ), all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")), @@ -363,7 +393,12 @@ pub struct VaList<'a, 'f: 'a> { inner: VaListImpl<'f>, #[cfg(all( - any(target_arch = "aarch64", target_arch = "powerpc", target_arch = "x86_64"), + any( + target_arch = "aarch64", + target_arch = "powerpc", + target_arch = "s390x", + target_arch = "x86_64" + ), any(not(target_arch = "aarch64"), not(any(target_os = "macos", target_os = "ios"))), not(target_family = "wasm"), not(target_arch = "asmjs"), @@ -376,7 +411,12 @@ pub struct VaList<'a, 'f: 'a> { } #[cfg(any( - all(not(target_arch = "aarch64"), not(target_arch = "powerpc"), not(target_arch = "x86_64")), + all( + not(target_arch = "aarch64"), + not(target_arch = "powerpc"), + not(target_arch = "s390x"), + not(target_arch = "x86_64") + ), all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios")), target_family = "wasm", target_arch = "asmjs", @@ -398,7 +438,12 @@ impl<'f> VaListImpl<'f> { } #[cfg(all( - any(target_arch = "aarch64", target_arch = "powerpc", target_arch = "x86_64"), + any( + target_arch = "aarch64", + target_arch = "powerpc", + target_arch = "s390x", + target_arch = "x86_64" + ), any(not(target_arch = "aarch64"), not(any(target_os = "macos", target_os = "ios"))), not(target_family = "wasm"), not(target_arch = "asmjs"), From 55c4164fffb8c4389727cff554dd77012c624087 Mon Sep 17 00:00:00 2001 From: Arvind Mukund Date: Mon, 19 Dec 2022 19:06:30 -0800 Subject: [PATCH 221/321] Correct ModFlagBehavior for Aarch64 on LLVM-15 When building with Fat LTO and BTI enabled on aarch64, the BTI is set to `Module::Min` for alloc shim but is set to `Module::Error` for the crate. This was fine when we were using LLVM-14 but LLVM-15 changes it's behaviour to support for compiling with different `mbranch-protection` flags. Refer: https://github.com/rust-lang/llvm-project/commit/b0343a38a5910e980bb031e4014655d77cd0c162 --- compiler/rustc_codegen_llvm/src/context.rs | 8 ++++---- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index f3bff5d57161..9c7d3dabd6fb 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -283,26 +283,26 @@ pub unsafe fn create_module<'ll>( if sess.target.arch == "aarch64" { llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "branch-target-enforcement\0".as_ptr().cast(), bti.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "sign-return-address\0".as_ptr().cast(), pac_ret.is_some().into(), ); let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "sign-return-address-all\0".as_ptr().cast(), pac_opts.leaf.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "sign-return-address-with-bkey\0".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index e61dbe8b8fc5..6c78966a98d8 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -79,6 +79,7 @@ pub enum LLVMModFlagBehavior { Append = 5, AppendUnique = 6, Max = 7, + Min = 8, } // Consts for the LLVM CallConv type, pre-cast to usize. From 1b11ce26fa08b7f2a200fe1f4a21c7f580f354a5 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 20 Dec 2022 12:42:12 +0900 Subject: [PATCH 222/321] Add readable rustdoc display for tvOS and watchOS --- src/librustdoc/clean/cfg.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustdoc/clean/cfg.rs b/src/librustdoc/clean/cfg.rs index 1843a21205cf..f1853f3697df 100644 --- a/src/librustdoc/clean/cfg.rs +++ b/src/librustdoc/clean/cfg.rs @@ -507,7 +507,9 @@ impl<'a> fmt::Display for Display<'a> { "openbsd" => "OpenBSD", "redox" => "Redox", "solaris" => "Solaris", + "tvos" => "tvOS", "wasi" => "WASI", + "watchos" => "watchOS", "windows" => "Windows", _ => "", }, From e798fdf7befc40255ce46caa37a0e6c1e8756b6c Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Tue, 20 Dec 2022 18:31:15 +1300 Subject: [PATCH 223/321] docs/test: add UI test and long-form error docs for `E0377` --- compiler/rustc_error_codes/src/error_codes.rs | 3 +- .../src/error_codes/E0377.md | 29 +++++++++++++++++++ src/test/ui/error-codes/E0377.rs | 14 +++++++++ src/test/ui/error-codes/E0377.stderr | 9 ++++++ src/tools/tidy/src/error_codes_check.rs | 4 +-- 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0377.md create mode 100644 src/test/ui/error-codes/E0377.rs create mode 100644 src/test/ui/error-codes/E0377.stderr diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 883a4bbe8e82..4e149fc2b997 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -184,6 +184,7 @@ E0373: include_str!("./error_codes/E0373.md"), E0374: include_str!("./error_codes/E0374.md"), E0375: include_str!("./error_codes/E0375.md"), E0376: include_str!("./error_codes/E0376.md"), +E0377: include_str!("./error_codes/E0377.md"), E0378: include_str!("./error_codes/E0378.md"), E0379: include_str!("./error_codes/E0379.md"), E0380: include_str!("./error_codes/E0380.md"), @@ -579,8 +580,6 @@ E0791: include_str!("./error_codes/E0791.md"), // E0315, // cannot invoke closure outside of its lifetime // E0319, // trait impls for defaulted traits allowed just for structs/enums // E0372, // coherence not object safe - E0377, // the trait `CoerceUnsized` may only be implemented for a coercion - // between structures with the same definition // E0385, // {} in an aliasable location // E0402, // cannot use an outer type parameter in this context // E0406, // merged into 420 diff --git a/compiler/rustc_error_codes/src/error_codes/E0377.md b/compiler/rustc_error_codes/src/error_codes/E0377.md new file mode 100644 index 000000000000..b1d36406332b --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0377.md @@ -0,0 +1,29 @@ +The trait `CoerceUnsized` may only be implemented for a coercion between +structures with the same definition. + +Example of erroneous code: + +```compile_fail,E0377 +#![feature(coerce_unsized)] +use std::ops::CoerceUnsized; + +pub struct Foo { + field_with_unsized_type: T, +} + +pub struct Bar { + field_with_unsized_type: T, +} + +// error: the trait `CoerceUnsized` may only be implemented for a coercion +// between structures with the same definition +impl CoerceUnsized> for Foo where T: CoerceUnsized {} +``` + +When attempting to implement `CoerceUnsized`, the `impl` signature must look +like: `impl CoerceUnsized> for Type where T: CoerceUnsized`; +the *implementer* and *`CoerceUnsized` type parameter* must be the same +type. In this example, `Bar` and `Foo` (even though structurally identical) +are *not* the same type and are rejected. Learn more about the `CoerceUnsized` +trait and DST coercion in +[the `CoerceUnsized` docs](../std/ops/trait.CoerceUnsized.html). diff --git a/src/test/ui/error-codes/E0377.rs b/src/test/ui/error-codes/E0377.rs new file mode 100644 index 000000000000..6da2c20956ad --- /dev/null +++ b/src/test/ui/error-codes/E0377.rs @@ -0,0 +1,14 @@ +#![feature(coerce_unsized)] +use std::ops::CoerceUnsized; + +pub struct Foo { + field_with_unsized_type: T, +} + +pub struct Bar { + field_with_unsized_type: T, +} + +impl CoerceUnsized> for Foo where T: CoerceUnsized {} //~ ERROR E0377 + +fn main() {} diff --git a/src/test/ui/error-codes/E0377.stderr b/src/test/ui/error-codes/E0377.stderr new file mode 100644 index 000000000000..bf7d8c8d39d2 --- /dev/null +++ b/src/test/ui/error-codes/E0377.stderr @@ -0,0 +1,9 @@ +error[E0377]: the trait `CoerceUnsized` may only be implemented for a coercion between structures with the same definition; expected `Foo`, found `Bar` + --> $DIR/E0377.rs:12:1 + | +LL | impl CoerceUnsized> for Foo where T: CoerceUnsized {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0377`. diff --git a/src/tools/tidy/src/error_codes_check.rs b/src/tools/tidy/src/error_codes_check.rs index 610e322e1296..1b119e4113e3 100644 --- a/src/tools/tidy/src/error_codes_check.rs +++ b/src/tools/tidy/src/error_codes_check.rs @@ -11,8 +11,8 @@ use regex::Regex; // A few of those error codes can't be tested but all the others can and *should* be tested! const EXEMPTED_FROM_TEST: &[&str] = &[ - "E0313", "E0377", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523", - "E0554", "E0640", "E0717", "E0729", "E0789", + "E0313", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523", "E0554", + "E0640", "E0717", "E0729", "E0789", ]; // Some error codes don't have any tests apparently... From 8c73ce6611e08c09c2ba6344c1fc9168e2daed55 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 19 Dec 2022 23:08:38 -0800 Subject: [PATCH 224/321] Update coerce_unsized tracking issue from #27732 to #18598 Issue #27732 was closed as a duplicate of #18598. Signed-off-by: Anders Kaseorg --- library/alloc/src/boxed.rs | 2 +- library/alloc/src/rc.rs | 4 ++-- library/alloc/src/sync.rs | 4 ++-- library/core/src/cell.rs | 12 ++++++------ library/core/src/marker.rs | 2 +- library/core/src/ops/mod.rs | 2 +- library/core/src/ops/unsize.rs | 20 ++++++++++---------- library/core/src/ptr/non_null.rs | 2 +- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index e5f6b0c0c65d..b154688fb087 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -2033,7 +2033,7 @@ impl + ?Sized, A: Allocator> Fn for Box { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized, A: Allocator> CoerceUnsized> for Box {} #[unstable(feature = "dispatch_from_dyn", issue = "none")] diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 38e31b1802a4..80a5913daa6e 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -336,7 +336,7 @@ impl UnwindSafe for Rc {} #[stable(feature = "rc_ref_unwind_safe", since = "1.58.0")] impl RefUnwindSafe for Rc {} -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized> CoerceUnsized> for Rc {} #[unstable(feature = "dispatch_from_dyn", issue = "none")] @@ -2190,7 +2190,7 @@ impl !marker::Send for Weak {} #[stable(feature = "rc_weak", since = "1.4.0")] impl !marker::Sync for Weak {} -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized> CoerceUnsized> for Weak {} #[unstable(feature = "dispatch_from_dyn", issue = "none")] diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index f7dc4d1094ca..ddcd863aa3e3 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -254,7 +254,7 @@ unsafe impl Sync for Arc {} #[stable(feature = "catch_unwind", since = "1.9.0")] impl UnwindSafe for Arc {} -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized> CoerceUnsized> for Arc {} #[unstable(feature = "dispatch_from_dyn", issue = "none")] @@ -306,7 +306,7 @@ unsafe impl Send for Weak {} #[stable(feature = "arc_weak", since = "1.4.0")] unsafe impl Sync for Weak {} -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized> CoerceUnsized> for Weak {} #[unstable(feature = "dispatch_from_dyn", issue = "none")] impl, U: ?Sized> DispatchFromDyn> for Weak {} diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index 47cce2aa39b0..b4e173ce03d8 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -568,7 +568,7 @@ impl Cell { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U> CoerceUnsized> for Cell {} impl Cell<[T]> { @@ -1266,7 +1266,7 @@ impl const From for RefCell { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U> CoerceUnsized> for RefCell {} struct BorrowRef<'b> { @@ -1492,7 +1492,7 @@ impl<'b, T: ?Sized> Ref<'b, T> { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'b, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized> for Ref<'b, T> {} #[stable(feature = "std_guard_impls", since = "1.20.0")] @@ -1738,7 +1738,7 @@ impl DerefMut for RefMut<'_, T> { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'b, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized> for RefMut<'b, T> {} #[stable(feature = "std_guard_impls", since = "1.20.0")] @@ -2074,7 +2074,7 @@ impl const From for UnsafeCell { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U> CoerceUnsized> for UnsafeCell {} /// [`UnsafeCell`], but [`Sync`]. @@ -2164,7 +2164,7 @@ impl const From for SyncUnsafeCell { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] //#[unstable(feature = "sync_unsafe_cell", issue = "95439")] impl, U> CoerceUnsized> for SyncUnsafeCell {} diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 42c342801976..4b85c1112b94 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -126,7 +126,7 @@ pub trait Sized { /// [`Rc`]: ../../std/rc/struct.Rc.html /// [RFC982]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md /// [nomicon-coerce]: ../../nomicon/coercions.html -#[unstable(feature = "unsize", issue = "27732")] +#[unstable(feature = "unsize", issue = "18598")] #[lang = "unsize"] #[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)] pub trait Unsize { diff --git a/library/core/src/ops/mod.rs b/library/core/src/ops/mod.rs index eb2a92f4644d..97d9b750d92f 100644 --- a/library/core/src/ops/mod.rs +++ b/library/core/src/ops/mod.rs @@ -201,7 +201,7 @@ pub(crate) use self::try_trait::{ChangeOutputType, NeverShortCircuit}; #[unstable(feature = "generator_trait", issue = "43122")] pub use self::generator::{Generator, GeneratorState}; -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] pub use self::unsize::CoerceUnsized; #[unstable(feature = "dispatch_from_dyn", issue = "none")] diff --git a/library/core/src/ops/unsize.rs b/library/core/src/ops/unsize.rs index a920b9165c18..b51f12580ea4 100644 --- a/library/core/src/ops/unsize.rs +++ b/library/core/src/ops/unsize.rs @@ -31,41 +31,41 @@ use crate::marker::Unsize; /// [dst-coerce]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md /// [unsize]: crate::marker::Unsize /// [nomicon-coerce]: ../../nomicon/coercions.html -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] #[lang = "coerce_unsized"] pub trait CoerceUnsized { // Empty. } // &mut T -> &mut U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {} // &mut T -> &U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {} // &mut T -> *mut U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {} // &mut T -> *const U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {} // &T -> &U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'a, 'b: 'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} // &T -> *const U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl<'a, T: ?Sized + Unsize, U: ?Sized> CoerceUnsized<*const U> for &'a T {} // *mut T -> *mut U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} // *mut T -> *const U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized> CoerceUnsized<*const U> for *mut T {} // *const T -> *const U -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl, U: ?Sized> CoerceUnsized<*const U> for *const T {} /// `DispatchFromDyn` is used in the implementation of object safety checks (specifically allowing diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index c4348169c78c..af79d4bbd836 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -712,7 +712,7 @@ impl const Clone for NonNull { #[stable(feature = "nonnull", since = "1.25.0")] impl Copy for NonNull {} -#[unstable(feature = "coerce_unsized", issue = "27732")] +#[unstable(feature = "coerce_unsized", issue = "18598")] impl CoerceUnsized> for NonNull where T: Unsize {} #[unstable(feature = "dispatch_from_dyn", issue = "none")] From babd174aceb848e6dd3f67d9a8808cefe8b49193 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 20 Dec 2022 08:22:31 +0100 Subject: [PATCH 225/321] Improve description of struct-fields GUI test --- src/test/rustdoc-gui/struct-fields.goml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/rustdoc-gui/struct-fields.goml b/src/test/rustdoc-gui/struct-fields.goml index 3ec60b58cfde..fa3e16cb81ef 100644 --- a/src/test/rustdoc-gui/struct-fields.goml +++ b/src/test/rustdoc-gui/struct-fields.goml @@ -1,5 +1,5 @@ +// This test ensures that each field is on its own line (In other words, they have display: block). goto: "file://" + |DOC_PATH| + "/test_docs/struct.StructWithPublicUndocumentedFields.html" -// Both fields must be on their own line. In other words, they have display: block. store-property: (first_top, "//*[@id='structfield.first']", "offsetTop") assert-property-false: ("//*[@id='structfield.second']", { "offsetTop": |first_top| }) From 750bf36c33bb6d2d4d7fdc609020507e5f966037 Mon Sep 17 00:00:00 2001 From: lcnr Date: Mon, 19 Dec 2022 07:01:38 +0000 Subject: [PATCH 226/321] dedup assembly --- .../src/solve/assembly.rs | 150 +++++++++++++ .../rustc_trait_selection/src/solve/mod.rs | 1 + .../src/solve/project_goals.rs | 208 ++++++----------- .../src/solve/trait_goals.rs | 212 +++++------------- 4 files changed, 270 insertions(+), 301 deletions(-) create mode 100644 compiler/rustc_trait_selection/src/solve/assembly.rs diff --git a/compiler/rustc_trait_selection/src/solve/assembly.rs b/compiler/rustc_trait_selection/src/solve/assembly.rs new file mode 100644 index 000000000000..e9ddad11ff23 --- /dev/null +++ b/compiler/rustc_trait_selection/src/solve/assembly.rs @@ -0,0 +1,150 @@ +//! Code shared by trait and projection goals for candidate assembly. + +use super::infcx_ext::InferCtxtExt; +use super::{ + fixme_instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty, + EvalCtxt, Goal, +}; +use rustc_hir::def_id::DefId; +use rustc_infer::infer::TyCtxtInferExt; +use rustc_infer::infer::{ + canonical::{CanonicalVarValues, OriginalQueryValues}, + InferCtxt, +}; +use rustc_infer::traits::query::NoSolution; +use rustc_middle::ty::TypeFoldable; +use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_span::DUMMY_SP; +use std::fmt::Debug; + +/// A candidate is a possible way to prove a goal. +/// +/// It consists of both the `source`, which describes how that goal would be proven, +/// and the `result` when using the given `source`. +/// +/// For the list of possible candidates, please look at the documentation of +/// [super::trait_goals::CandidateSource] and [super::project_goals::CandidateSource]. +#[derive(Debug, Clone)] +pub(super) struct Candidate<'tcx, G: GoalKind<'tcx>> { + pub(super) source: G::CandidateSource, + pub(super) result: CanonicalResponse<'tcx>, +} + +pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy { + type CandidateSource: Debug + Copy; + + fn self_ty(self) -> Ty<'tcx>; + + fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self; + + fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId; + + fn consider_impl_candidate( + acx: &mut AssemblyCtxt<'_, 'tcx, Self>, + goal: Goal<'tcx, Self>, + impl_def_id: DefId, + ); +} + +/// An abstraction which correctly deals with the canonical results for candidates. +/// +/// It also deduplicates the behavior between trait and projection predicates. +pub(super) struct AssemblyCtxt<'a, 'tcx, G: GoalKind<'tcx>> { + pub(super) cx: &'a mut EvalCtxt<'tcx>, + pub(super) infcx: &'a InferCtxt<'tcx>, + var_values: CanonicalVarValues<'tcx>, + candidates: Vec>, +} + +impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> { + pub(super) fn assemble_and_evaluate_candidates( + cx: &'a mut EvalCtxt<'tcx>, + goal: CanonicalGoal<'tcx, G>, + ) -> Vec> { + let (ref infcx, goal, var_values) = + cx.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal); + let mut acx = AssemblyCtxt { cx, infcx, var_values, candidates: Vec::new() }; + + acx.assemble_candidates_after_normalizing_self_ty(goal); + + acx.assemble_impl_candidates(goal); + + acx.candidates + } + + pub(super) fn try_insert_candidate( + &mut self, + source: G::CandidateSource, + certainty: Certainty, + ) { + match self.infcx.make_canonical_response(self.var_values.clone(), certainty) { + Ok(result) => self.candidates.push(Candidate { source, result }), + Err(NoSolution) => debug!(?source, ?certainty, "failed leakcheck"), + } + } + + /// If the self type of a goal is a projection, computing the relevant candidates is difficult. + /// + /// To deal with this, we first try to normalize the self type and add the candidates for the normalized + /// self type to the list of candidates in case that succeeds. Note that we can't just eagerly return in + /// this case as projections as self types add ` + fn assemble_candidates_after_normalizing_self_ty(&mut self, goal: Goal<'tcx, G>) { + let tcx = self.cx.tcx; + // FIXME: We also have to normalize opaque types, not sure where to best fit that in. + let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.self_ty().kind() else { + return + }; + self.infcx.probe(|_| { + let normalized_ty = self.infcx.next_ty_infer(); + let normalizes_to_goal = goal.with( + tcx, + ty::Binder::dummy(ty::ProjectionPredicate { + projection_ty, + term: normalized_ty.into(), + }), + ); + let normalization_certainty = + match self.cx.evaluate_goal(&self.infcx, normalizes_to_goal) { + Ok((_, certainty)) => certainty, + Err(NoSolution) => return, + }; + + // NOTE: Alternatively we could call `evaluate_goal` here and only have a `Normalized` candidate. + // This doesn't work as long as we use `CandidateSource` in both winnowing and to resolve associated items. + let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty)); + let mut orig_values = OriginalQueryValues::default(); + let goal = self.infcx.canonicalize_query(goal, &mut orig_values); + let normalized_candidates = + AssemblyCtxt::assemble_and_evaluate_candidates(self.cx, goal); + + // Map each candidate from being canonical wrt the current inference context to being + // canonical wrt the caller. + for Candidate { source, result } in normalized_candidates { + self.infcx.probe(|_| { + let candidate_certainty = fixme_instantiate_canonical_query_response( + &self.infcx, + &orig_values, + result, + ); + + // FIXME: This is a bit scary if the `normalizes_to_goal` overflows. + // + // If we have an ambiguous candidate it hides that normalization + // caused an overflow which may cause issues. + self.try_insert_candidate( + source, + normalization_certainty.unify_and(candidate_certainty), + ) + }) + } + }) + } + + fn assemble_impl_candidates(&mut self, goal: Goal<'tcx, G>) { + self.cx.tcx.for_each_relevant_impl( + goal.predicate.trait_def_id(self.cx.tcx), + goal.predicate.self_ty(), + |impl_def_id| G::consider_impl_candidate(self, goal, impl_def_id), + ); + } +} diff --git a/compiler/rustc_trait_selection/src/solve/mod.rs b/compiler/rustc_trait_selection/src/solve/mod.rs index 5d6529f85423..7f5e3208f4e7 100644 --- a/compiler/rustc_trait_selection/src/solve/mod.rs +++ b/compiler/rustc_trait_selection/src/solve/mod.rs @@ -30,6 +30,7 @@ use rustc_span::DUMMY_SP; use self::infcx_ext::InferCtxtExt; +mod assembly; mod cache; mod fulfill; mod infcx_ext; diff --git a/compiler/rustc_trait_selection/src/solve/project_goals.rs b/compiler/rustc_trait_selection/src/solve/project_goals.rs index 47a8f59ceb77..b50f42c4d941 100644 --- a/compiler/rustc_trait_selection/src/solve/project_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/project_goals.rs @@ -1,64 +1,40 @@ use crate::traits::{specialization_graph, translate_substs}; -use super::infcx_ext::InferCtxtExt; -use super::{ - fixme_instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty, - EvalCtxt, Goal, QueryResult, -}; +use super::assembly::{self, AssemblyCtxt}; +use super::{CanonicalGoal, EvalCtxt, Goal, QueryResult}; use rustc_errors::ErrorGuaranteed; use rustc_hir::def::DefKind; use rustc_hir::def_id::DefId; -use rustc_infer::infer::canonical::{CanonicalVarValues, OriginalQueryValues}; -use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt}; +use rustc_infer::infer::{InferCtxt, InferOk}; use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::specialization_graph::LeafDef; use rustc_infer::traits::{ObligationCause, Reveal}; -use rustc_middle::ty; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::ProjectionPredicate; use rustc_middle::ty::TypeVisitable; +use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::DUMMY_SP; use std::iter; -// FIXME: Deduplicate the candidate code between projection and trait goal. - -/// Similar to [super::trait_goals::Candidate] but for `Projection` goals. -#[derive(Debug, Clone)] -struct Candidate<'tcx> { - source: CandidateSource, - result: CanonicalResponse<'tcx>, -} - #[allow(dead_code)] // FIXME: implement and use all variants. #[derive(Debug, Clone, Copy)] -enum CandidateSource { +pub(super) enum CandidateSource { Impl(DefId), ParamEnv(usize), Builtin, } +type Candidate<'tcx> = assembly::Candidate<'tcx, ProjectionPredicate<'tcx>>; + impl<'tcx> EvalCtxt<'tcx> { pub(super) fn compute_projection_goal( &mut self, goal: CanonicalGoal<'tcx, ProjectionPredicate<'tcx>>, ) -> QueryResult<'tcx> { - let candidates = self.assemble_and_evaluate_project_candidates(goal); + let candidates = AssemblyCtxt::assemble_and_evaluate_candidates(self, goal); self.merge_project_candidates(candidates) } - fn assemble_and_evaluate_project_candidates( - &mut self, - goal: CanonicalGoal<'tcx, ProjectionPredicate<'tcx>>, - ) -> Vec> { - let (ref infcx, goal, var_values) = - self.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal); - let mut acx = AssemblyCtxt { cx: self, infcx, var_values, candidates: Vec::new() }; - - acx.assemble_candidates_after_normalizing_self_ty(goal); - acx.assemble_impl_candidates(goal); - acx.candidates - } - fn merge_project_candidates( &mut self, mut candidates: Vec>, @@ -112,83 +88,27 @@ impl<'tcx> EvalCtxt<'tcx> { } } -/// Similar to [super::trait_goals::AssemblyCtxt] but for `Projection` goals. -struct AssemblyCtxt<'a, 'tcx> { - cx: &'a mut EvalCtxt<'tcx>, - infcx: &'a InferCtxt<'tcx>, - var_values: CanonicalVarValues<'tcx>, - candidates: Vec>, -} +impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> { + type CandidateSource = CandidateSource; -impl<'tcx> AssemblyCtxt<'_, 'tcx> { - fn try_insert_candidate(&mut self, source: CandidateSource, certainty: Certainty) { - match self.infcx.make_canonical_response(self.var_values.clone(), certainty) { - Ok(result) => self.candidates.push(Candidate { source, result }), - Err(NoSolution) => debug!(?source, ?certainty, "failed leakcheck"), - } + fn self_ty(self) -> Ty<'tcx> { + self.self_ty() } - fn assemble_candidates_after_normalizing_self_ty( - &mut self, - goal: Goal<'tcx, ProjectionPredicate<'tcx>>, - ) { - let tcx = self.cx.tcx; - let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.projection_ty.self_ty().kind() else { - return - }; - self.infcx.probe(|_| { - let normalized_ty = self.infcx.next_ty_infer(); - let normalizes_to_goal = goal.with( - tcx, - ty::Binder::dummy(ty::ProjectionPredicate { - projection_ty, - term: normalized_ty.into(), - }), - ); - let normalization_certainty = - match self.cx.evaluate_goal(&self.infcx, normalizes_to_goal) { - Ok((_, certainty)) => certainty, - Err(NoSolution) => return, - }; - - // NOTE: Alternatively we could call `evaluate_goal` here and only have a `Normalized` candidate. - // This doesn't work as long as we use `CandidateSource` in both winnowing and to resolve associated items. - let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty)); - let mut orig_values = OriginalQueryValues::default(); - let goal = self.infcx.canonicalize_query(goal, &mut orig_values); - let normalized_candidates = self.cx.assemble_and_evaluate_project_candidates(goal); - // Map each candidate from being canonical wrt the current inference context to being - // canonical wrt the caller. - for Candidate { source, result } in normalized_candidates { - self.infcx.probe(|_| { - let candidate_certainty = fixme_instantiate_canonical_query_response( - self.infcx, - &orig_values, - result, - ); - self.try_insert_candidate( - source, - normalization_certainty.unify_and(candidate_certainty), - ) - }) - } - }) + fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { + self.with_self_ty(tcx, self_ty) } - fn assemble_impl_candidates(&mut self, goal: Goal<'tcx, ProjectionPredicate<'tcx>>) { - self.cx.tcx.for_each_relevant_impl( - goal.predicate.trait_def_id(self.cx.tcx), - goal.predicate.self_ty(), - |impl_def_id| self.consider_impl_candidate(goal, impl_def_id), - ); + fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId { + self.trait_def_id(tcx) } fn consider_impl_candidate( - &mut self, + acx: &mut AssemblyCtxt<'_, 'tcx, ProjectionPredicate<'tcx>>, goal: Goal<'tcx, ProjectionPredicate<'tcx>>, impl_def_id: DefId, ) { - let tcx = self.cx.tcx; + let tcx = acx.cx.tcx; let goal_trait_ref = goal.predicate.projection_ty.trait_ref(tcx); let impl_trait_ref = tcx.bound_impl_trait_ref(impl_def_id).unwrap(); let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::AsPlaceholder }; @@ -198,11 +118,11 @@ impl<'tcx> AssemblyCtxt<'_, 'tcx> { return; } - self.infcx.probe(|_| { - let impl_substs = self.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); + acx.infcx.probe(|_| { + let impl_substs = acx.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs); - let Ok(InferOk { obligations, .. }) = self + let Ok(InferOk { obligations, .. }) = acx .infcx .at(&ObligationCause::dummy(), goal.param_env) .define_opaque_types(false) @@ -213,9 +133,10 @@ impl<'tcx> AssemblyCtxt<'_, 'tcx> { }; let nested_goals = obligations.into_iter().map(|o| o.into()).collect(); - let Ok(trait_ref_certainty) = self.cx.evaluate_all(self.infcx, nested_goals) else { return }; + let Ok(trait_ref_certainty) = acx.cx.evaluate_all(acx.infcx, nested_goals) else { return }; - let Some(assoc_def) = self.fetch_eligible_assoc_item_def( + let Some(assoc_def) = fetch_eligible_assoc_item_def( + acx.infcx, goal.param_env, goal_trait_ref, goal.predicate.def_id(), @@ -247,7 +168,7 @@ impl<'tcx> AssemblyCtxt<'_, 'tcx> { impl_trait_ref.substs, ); let substs = translate_substs( - self.infcx, + acx.infcx, goal.param_env, impl_def_id, impl_substs_with_gat, @@ -267,7 +188,7 @@ impl<'tcx> AssemblyCtxt<'_, 'tcx> { ty.map_bound(|ty| ty.into()) }; - let Ok(InferOk { obligations, .. }) = self + let Ok(InferOk { obligations, .. }) = acx .infcx .at(&ObligationCause::dummy(), goal.param_env) .define_opaque_types(false) @@ -278,47 +199,46 @@ impl<'tcx> AssemblyCtxt<'_, 'tcx> { }; let nested_goals = obligations.into_iter().map(|o| o.into()).collect(); - let Ok(rhs_certainty) = self.cx.evaluate_all(self.infcx, nested_goals) else { return }; + let Ok(rhs_certainty) = acx.cx.evaluate_all(acx.infcx, nested_goals) else { return }; let certainty = trait_ref_certainty.unify_and(rhs_certainty); - self.try_insert_candidate(CandidateSource::Impl(impl_def_id), certainty); + acx.try_insert_candidate(CandidateSource::Impl(impl_def_id), certainty); }) } - - /// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code. - /// - /// FIXME: We should merge these 3 implementations as it's likely that they otherwise - /// diverge. - #[instrument(level = "debug", skip(self, param_env), ret)] - fn fetch_eligible_assoc_item_def( - &self, - param_env: ty::ParamEnv<'tcx>, - goal_trait_ref: ty::TraitRef<'tcx>, - trait_assoc_def_id: DefId, - impl_def_id: DefId, - ) -> Option { - let node_item = - specialization_graph::assoc_def(self.cx.tcx, impl_def_id, trait_assoc_def_id) - .map_err(|ErrorGuaranteed { .. }| ()) - .ok()?; - - let eligible = if node_item.is_final() { - // Non-specializable items are always projectable. - true - } else { - // Only reveal a specializable default if we're past type-checking - // and the obligation is monomorphic, otherwise passes such as - // transmute checking and polymorphic MIR optimizations could - // get a result which isn't correct for all monomorphizations. - if param_env.reveal() == Reveal::All { - let poly_trait_ref = self.infcx.resolve_vars_if_possible(goal_trait_ref); - !poly_trait_ref.still_further_specializable() - } else { - debug!(?node_item.item.def_id, "not eligible due to default"); - false - } - }; - - if eligible { Some(node_item) } else { None } - } +} + +/// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code. +/// +/// FIXME: We should merge these 3 implementations as it's likely that they otherwise +/// diverge. +#[instrument(level = "debug", skip(infcx, param_env), ret)] +fn fetch_eligible_assoc_item_def<'tcx>( + infcx: &InferCtxt<'tcx>, + param_env: ty::ParamEnv<'tcx>, + goal_trait_ref: ty::TraitRef<'tcx>, + trait_assoc_def_id: DefId, + impl_def_id: DefId, +) -> Option { + let node_item = specialization_graph::assoc_def(infcx.tcx, impl_def_id, trait_assoc_def_id) + .map_err(|ErrorGuaranteed { .. }| ()) + .ok()?; + + let eligible = if node_item.is_final() { + // Non-specializable items are always projectable. + true + } else { + // Only reveal a specializable default if we're past type-checking + // and the obligation is monomorphic, otherwise passes such as + // transmute checking and polymorphic MIR optimizations could + // get a result which isn't correct for all monomorphizations. + if param_env.reveal() == Reveal::All { + let poly_trait_ref = infcx.resolve_vars_if_possible(goal_trait_ref); + !poly_trait_ref.still_further_specializable() + } else { + debug!(?node_item.item.def_id, "not eligible due to default"); + false + } + }; + + if eligible { Some(node_item) } else { None } } diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs index 4ea9081bf46a..10b45a77dabb 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -2,35 +2,17 @@ use std::iter; -use super::infcx_ext::InferCtxtExt; -use super::{ - fixme_instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty, - EvalCtxt, Goal, QueryResult, -}; +use super::assembly::{self, AssemblyCtxt}; +use super::{CanonicalGoal, EvalCtxt, Goal, QueryResult}; use rustc_hir::def_id::DefId; -use rustc_infer::infer::canonical::{CanonicalVarValues, OriginalQueryValues}; -use rustc_infer::infer::TyCtxtInferExt; -use rustc_infer::infer::{InferCtxt, InferOk}; +use rustc_infer::infer::InferOk; use rustc_infer::traits::query::NoSolution; use rustc_infer::traits::ObligationCause; -use rustc_middle::ty; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::TraitPredicate; +use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::DUMMY_SP; -/// A candidate is a possible way to prove a goal. -/// -/// It consists of both the `source`, which describes how that goal -/// would be proven, and the `result` when using the given `source`. -/// -/// For the list of possible candidates, please look at the documentation -/// of [CandidateSource]. -#[derive(Debug, Clone)] -pub(super) struct Candidate<'tcx> { - source: CandidateSource, - result: CanonicalResponse<'tcx>, -} - #[allow(dead_code)] // FIXME: implement and use all variants. #[derive(Debug, Clone, Copy)] pub(super) enum CandidateSource { @@ -67,11 +49,56 @@ pub(super) enum CandidateSource { AutoImpl, } -struct AssemblyCtxt<'a, 'tcx> { - cx: &'a mut EvalCtxt<'tcx>, - infcx: &'a InferCtxt<'tcx>, - var_values: CanonicalVarValues<'tcx>, - candidates: Vec>, +type Candidate<'tcx> = assembly::Candidate<'tcx, TraitPredicate<'tcx>>; + +impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { + type CandidateSource = CandidateSource; + + fn self_ty(self) -> Ty<'tcx> { + self.self_ty() + } + + fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { + self.with_self_ty(tcx, self_ty) + } + + fn trait_def_id(self, _: TyCtxt<'tcx>) -> DefId { + self.def_id() + } + + fn consider_impl_candidate( + acx: &mut AssemblyCtxt<'_, 'tcx, Self>, + goal: Goal<'tcx, TraitPredicate<'tcx>>, + impl_def_id: DefId, + ) { + let impl_trait_ref = acx.cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap(); + let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::AsPlaceholder }; + if iter::zip(goal.predicate.trait_ref.substs, impl_trait_ref.skip_binder().substs) + .any(|(goal, imp)| !drcx.generic_args_may_unify(goal, imp)) + { + return; + } + + acx.infcx.probe(|_| { + let impl_substs = acx.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); + let impl_trait_ref = impl_trait_ref.subst(acx.cx.tcx, impl_substs); + + let Ok(InferOk { obligations, .. }) = acx + .infcx + .at(&ObligationCause::dummy(), goal.param_env) + .define_opaque_types(false) + .eq(goal.predicate.trait_ref, impl_trait_ref) + .map_err(|e| debug!("failed to equate trait refs: {e:?}")) + else { + return + }; + + let nested_goals = obligations.into_iter().map(|o| o.into()).collect(); + + let Ok(certainty) = acx.cx.evaluate_all(acx.infcx, nested_goals) else { return }; + acx.try_insert_candidate(CandidateSource::Impl(impl_def_id), certainty); + }) + } } impl<'tcx> EvalCtxt<'tcx> { @@ -79,25 +106,10 @@ impl<'tcx> EvalCtxt<'tcx> { &mut self, goal: CanonicalGoal<'tcx, TraitPredicate<'tcx>>, ) -> QueryResult<'tcx> { - let candidates = self.assemble_and_evaluate_trait_candidates(goal); + let candidates = AssemblyCtxt::assemble_and_evaluate_candidates(self, goal); self.merge_trait_candidates_discard_reservation_impls(candidates) } - pub(super) fn assemble_and_evaluate_trait_candidates( - &mut self, - goal: CanonicalGoal<'tcx, TraitPredicate<'tcx>>, - ) -> Vec> { - let (ref infcx, goal, var_values) = - self.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal); - let mut acx = AssemblyCtxt { cx: self, infcx, var_values, candidates: Vec::new() }; - - acx.assemble_candidates_after_normalizing_self_ty(goal); - acx.assemble_impl_candidates(goal); - - // FIXME: Remaining candidates - acx.candidates - } - #[instrument(level = "debug", skip(self), ret)] pub(super) fn merge_trait_candidates_discard_reservation_impls( &mut self, @@ -166,117 +178,3 @@ impl<'tcx> EvalCtxt<'tcx> { candidate } } - -impl<'tcx> AssemblyCtxt<'_, 'tcx> { - /// Adds a new candidate using the current state of the inference context. - /// - /// This does require each assembly method to correctly use `probe` to not taint - /// the results of other candidates. - fn try_insert_candidate(&mut self, source: CandidateSource, certainty: Certainty) { - match self.infcx.make_canonical_response(self.var_values.clone(), certainty) { - Ok(result) => self.candidates.push(Candidate { source, result }), - Err(NoSolution) => debug!(?source, ?certainty, "failed leakcheck"), - } - } - - /// If the self type of a trait goal is a projection, computing the relevant candidates is difficult. - /// - /// To deal with this, we first try to normalize the self type and add the candidates for the normalized - /// self type to the list of candidates in case that succeeds. Note that we can't just eagerly return in - /// this case as projections as self types add ` - fn assemble_candidates_after_normalizing_self_ty( - &mut self, - goal: Goal<'tcx, TraitPredicate<'tcx>>, - ) { - let tcx = self.cx.tcx; - // FIXME: We also have to normalize opaque types, not sure where to best fit that in. - let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.self_ty().kind() else { - return - }; - self.infcx.probe(|_| { - let normalized_ty = self.infcx.next_ty_infer(); - let normalizes_to_goal = goal.with( - tcx, - ty::Binder::dummy(ty::ProjectionPredicate { - projection_ty, - term: normalized_ty.into(), - }), - ); - let normalization_certainty = - match self.cx.evaluate_goal(&self.infcx, normalizes_to_goal) { - Ok((_, certainty)) => certainty, - Err(NoSolution) => return, - }; - - // NOTE: Alternatively we could call `evaluate_goal` here and only have a `Normalized` candidate. - // This doesn't work as long as we use `CandidateSource` in both winnowing and to resolve associated items. - let goal = goal.with(tcx, goal.predicate.with_self_type(tcx, normalized_ty)); - let mut orig_values = OriginalQueryValues::default(); - let goal = self.infcx.canonicalize_query(goal, &mut orig_values); - let normalized_candidates = self.cx.assemble_and_evaluate_trait_candidates(goal); - - // Map each candidate from being canonical wrt the current inference context to being - // canonical wrt the caller. - for Candidate { source, result } in normalized_candidates { - self.infcx.probe(|_| { - let candidate_certainty = fixme_instantiate_canonical_query_response( - self.infcx, - &orig_values, - result, - ); - - // FIXME: This is a bit scary if the `normalizes_to_goal` overflows. - // - // If we have an ambiguous candidate it hides that normalization - // caused an overflow which may cause issues. - self.try_insert_candidate( - source, - normalization_certainty.unify_and(candidate_certainty), - ) - }) - } - }) - } - - fn assemble_impl_candidates(&mut self, goal: Goal<'tcx, TraitPredicate<'tcx>>) { - self.cx.tcx.for_each_relevant_impl( - goal.predicate.def_id(), - goal.predicate.self_ty(), - |impl_def_id| self.consider_impl_candidate(goal, impl_def_id), - ); - } - - fn consider_impl_candidate( - &mut self, - goal: Goal<'tcx, TraitPredicate<'tcx>>, - impl_def_id: DefId, - ) { - let impl_trait_ref = self.cx.tcx.bound_impl_trait_ref(impl_def_id).unwrap(); - let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::AsPlaceholder }; - if iter::zip(goal.predicate.trait_ref.substs, impl_trait_ref.skip_binder().substs) - .any(|(goal, imp)| !drcx.generic_args_may_unify(goal, imp)) - { - return; - } - - self.infcx.probe(|_| { - let impl_substs = self.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); - let impl_trait_ref = impl_trait_ref.subst(self.cx.tcx, impl_substs); - - let Ok(InferOk { obligations, .. }) = self - .infcx - .at(&ObligationCause::dummy(), goal.param_env) - .define_opaque_types(false) - .eq(goal.predicate.trait_ref, impl_trait_ref) - .map_err(|e| debug!("failed to equate trait refs: {e:?}")) - else { - return - }; - - let nested_goals = obligations.into_iter().map(|o| o.into()).collect(); - - let Ok(certainty) = self.cx.evaluate_all(self.infcx, nested_goals) else { return }; - self.try_insert_candidate(CandidateSource::Impl(impl_def_id), certainty); - }) - } -} From 082ca1e46133f9720ca3f0342a6d43c5bcbd0510 Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Fri, 16 Dec 2022 15:59:47 +1300 Subject: [PATCH 227/321] docs: add long error explanation for error E0472 --- compiler/rustc_error_codes/src/error_codes.rs | 2 +- .../src/error_codes/E0472.md | 31 +++++++++++++++++++ src/test/ui/asm/bad-arch.mirunsafeck.stderr | 1 + src/test/ui/asm/bad-arch.thirunsafeck.stderr | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0472.md diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 883a4bbe8e82..2835e62fd834 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -248,6 +248,7 @@ E0464: include_str!("./error_codes/E0464.md"), E0466: include_str!("./error_codes/E0466.md"), E0468: include_str!("./error_codes/E0468.md"), E0469: include_str!("./error_codes/E0469.md"), +E0472: include_str!("./error_codes/E0472.md"), E0477: include_str!("./error_codes/E0477.md"), E0478: include_str!("./error_codes/E0478.md"), E0482: include_str!("./error_codes/E0482.md"), @@ -600,7 +601,6 @@ E0791: include_str!("./error_codes/E0791.md"), // E0467, // removed // E0470, // removed // E0471, // constant evaluation error (in pattern) - E0472, // llvm_asm! is unsupported on this target // E0473, // dereference of reference outside its lifetime // E0474, // captured variable `..` does not outlive the enclosing closure // E0475, // index of slice outside its lifetime diff --git a/compiler/rustc_error_codes/src/error_codes/E0472.md b/compiler/rustc_error_codes/src/error_codes/E0472.md new file mode 100644 index 000000000000..0005cd41911a --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0472.md @@ -0,0 +1,31 @@ +Inline assembly (`asm!`) is not supported on this target. + +Example of erroneous code: + +```ignore (cannot-change-target) +// compile-flags: --target sparc64-unknown-linux-gnu +#![no_std] + +use core::arch::asm; + +fn main() { + unsafe { + asm!(""); // error: inline assembly is not supported on this target + } +} +``` + +The Rust compiler does not support inline assembly, with the `asm!` macro +(previously `llvm_asm!`), for all targets. All Tier 1 targets do support this +macro but support among Tier 2 and 3 targets is not guaranteed (even when they +have `std` support). Note that this error is related to +`error[E0658]: inline assembly is not stable yet on this architecture`, but +distinct in that with `E0472` support is not planned or in progress. + +There is no way to easily fix this issue, however: + * Consider if you really need inline assembly, is there some other way to + achieve your goal (intrinsics, etc)? + * Consider writing your assembly externally, linking with it and calling it + from Rust. + * Consider contributing to and help + integrate support for your target! diff --git a/src/test/ui/asm/bad-arch.mirunsafeck.stderr b/src/test/ui/asm/bad-arch.mirunsafeck.stderr index 4aa27180758e..d7af296152f7 100644 --- a/src/test/ui/asm/bad-arch.mirunsafeck.stderr +++ b/src/test/ui/asm/bad-arch.mirunsafeck.stderr @@ -14,3 +14,4 @@ LL | global_asm!(""); error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0472`. diff --git a/src/test/ui/asm/bad-arch.thirunsafeck.stderr b/src/test/ui/asm/bad-arch.thirunsafeck.stderr index 4aa27180758e..d7af296152f7 100644 --- a/src/test/ui/asm/bad-arch.thirunsafeck.stderr +++ b/src/test/ui/asm/bad-arch.thirunsafeck.stderr @@ -14,3 +14,4 @@ LL | global_asm!(""); error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0472`. From a115a59cb2716f06c7511084c5678a85a32d446c Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 20 Dec 2022 18:24:04 +0900 Subject: [PATCH 228/321] Add regression test for #102206 Signed-off-by: Yuki Okushi --- .../ui/async-await/issues/issue-102206.rs | 8 +++++++ .../ui/async-await/issues/issue-102206.stderr | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/test/ui/async-await/issues/issue-102206.rs create mode 100644 src/test/ui/async-await/issues/issue-102206.stderr diff --git a/src/test/ui/async-await/issues/issue-102206.rs b/src/test/ui/async-await/issues/issue-102206.rs new file mode 100644 index 000000000000..a3a2ebc58961 --- /dev/null +++ b/src/test/ui/async-await/issues/issue-102206.rs @@ -0,0 +1,8 @@ +// edition:2021 + +async fn foo() {} + +fn main() { + std::mem::size_of_val(foo()); + //~^ ERROR: mismatched types +} diff --git a/src/test/ui/async-await/issues/issue-102206.stderr b/src/test/ui/async-await/issues/issue-102206.stderr new file mode 100644 index 000000000000..2ab790ac761a --- /dev/null +++ b/src/test/ui/async-await/issues/issue-102206.stderr @@ -0,0 +1,23 @@ +error[E0308]: mismatched types + --> $DIR/issue-102206.rs:6:27 + | +LL | std::mem::size_of_val(foo()); + | --------------------- ^^^^^ + | | | + | | expected reference, found opaque type + | | help: consider borrowing here: `&foo()` + | arguments to this function are incorrect + | +note: while checking the return type of the `async fn` + --> $DIR/issue-102206.rs:3:16 + | +LL | async fn foo() {} + | ^ checked the `Output` of this `async fn`, found opaque type + = note: expected reference `&_` + found opaque type `impl Future` +note: function defined here + --> $SRC_DIR/core/src/mem/mod.rs:LL:COL + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From 17d7d71d9416077ec3c005bbc4b9a59bf95335e1 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 20 Dec 2022 18:48:02 +0900 Subject: [PATCH 229/321] Add regression test for #80816 Signed-off-by: Yuki Okushi --- src/test/ui/inference/issue-80816.rs | 54 ++++++++++++++++++++++++ src/test/ui/inference/issue-80816.stderr | 27 ++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/test/ui/inference/issue-80816.rs create mode 100644 src/test/ui/inference/issue-80816.stderr diff --git a/src/test/ui/inference/issue-80816.rs b/src/test/ui/inference/issue-80816.rs new file mode 100644 index 000000000000..ead320a4fe42 --- /dev/null +++ b/src/test/ui/inference/issue-80816.rs @@ -0,0 +1,54 @@ +#![allow(unreachable_code)] + +use std::marker::PhantomData; +use std::ops::Deref; +use std::sync::Arc; + +pub struct Guard { + _phantom: PhantomData, +} +impl Deref for Guard { + type Target = T; + fn deref(&self) -> &T { + unimplemented!() + } +} + +pub struct DirectDeref(T); +impl Deref for DirectDeref> { + type Target = T; + fn deref(&self) -> &T { + unimplemented!() + } +} + +pub trait Access { + type Guard: Deref; + fn load(&self) -> Self::Guard { + unimplemented!() + } +} +impl, P: Deref> Access for P { + //~^ NOTE: required for `Arc>>` to implement `Access<_>` + type Guard = A::Guard; +} +impl Access for ArcSwapAny { + //~^ NOTE: multiple `impl`s satisfying `ArcSwapAny>: Access<_>` found + type Guard = Guard; +} +impl Access for ArcSwapAny> { + type Guard = DirectDeref>; +} + +pub struct ArcSwapAny { + _phantom_arc: PhantomData, +} + +pub fn foo() { + let s: Arc>> = unimplemented!(); + let guard: Guard> = s.load(); + //~^ ERROR: type annotations needed + //~| HELP: try using a fully qualified path to specify the expected types +} + +fn main() {} diff --git a/src/test/ui/inference/issue-80816.stderr b/src/test/ui/inference/issue-80816.stderr new file mode 100644 index 000000000000..bd833340df4c --- /dev/null +++ b/src/test/ui/inference/issue-80816.stderr @@ -0,0 +1,27 @@ +error[E0283]: type annotations needed + --> $DIR/issue-80816.rs:49:38 + | +LL | let guard: Guard> = s.load(); + | ^^^^ + | +note: multiple `impl`s satisfying `ArcSwapAny>: Access<_>` found + --> $DIR/issue-80816.rs:35:1 + | +LL | impl Access for ArcSwapAny { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Access for ArcSwapAny> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required for `Arc>>` to implement `Access<_>` + --> $DIR/issue-80816.rs:31:45 + | +LL | impl, P: Deref> Access for P { + | ^^^^^^^^^ ^ +help: try using a fully qualified path to specify the expected types + | +LL | let guard: Guard> = >> as Access>::load(&s); + | ++++++++++++++++++++++++++++++++++++++++++++++++++ ~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0283`. From 8f88cecab64504e9f8317e96bba41feb4ac10a29 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 20 Dec 2022 18:52:30 +0900 Subject: [PATCH 230/321] Add regression test for #57404 Signed-off-by: Yuki Okushi --- src/test/ui/typeck/issue-57404.rs | 7 +++++++ src/test/ui/typeck/issue-57404.stderr | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/test/ui/typeck/issue-57404.rs create mode 100644 src/test/ui/typeck/issue-57404.stderr diff --git a/src/test/ui/typeck/issue-57404.rs b/src/test/ui/typeck/issue-57404.rs new file mode 100644 index 000000000000..ecabca66a00c --- /dev/null +++ b/src/test/ui/typeck/issue-57404.rs @@ -0,0 +1,7 @@ +#![feature(unboxed_closures)] +#![feature(fn_traits)] + +fn main() { + let handlers: Option FnMut<&'a mut (), Output=()>>> = None; + handlers.unwrap().as_mut().call_mut(&mut ()); //~ ERROR: `&mut ()` is not a tuple +} diff --git a/src/test/ui/typeck/issue-57404.stderr b/src/test/ui/typeck/issue-57404.stderr new file mode 100644 index 000000000000..5065ac32ad2b --- /dev/null +++ b/src/test/ui/typeck/issue-57404.stderr @@ -0,0 +1,16 @@ +error[E0277]: `&mut ()` is not a tuple + --> $DIR/issue-57404.rs:6:41 + | +LL | handlers.unwrap().as_mut().call_mut(&mut ()); + | -------- -^^^^^^ + | | | + | | the trait `Tuple` is not implemented for `&mut ()` + | | help: consider removing the leading `&`-reference + | required by a bound introduced by this call + | +note: required by a bound in `call_mut` + --> $SRC_DIR/core/src/ops/function.rs:LL:COL + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. From b859b8b62bb080c315abd3d6ea10a15eddce0ce4 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 20 Dec 2022 13:03:45 +0000 Subject: [PATCH 231/321] Bump `cfg-if` to `1.0` --- Cargo.lock | 4 ++-- compiler/rustc_data_structures/Cargo.toml | 8 ++++++-- compiler/rustc_span/Cargo.toml | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d9a9240ccf2..3d09068dbdf3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3590,7 +3590,7 @@ version = "0.0.0" dependencies = [ "arrayvec", "bitflags", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "ena", "indexmap", "jobserver", @@ -4374,7 +4374,7 @@ dependencies = [ name = "rustc_span" version = "0.0.0" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "md-5", "rustc_arena", "rustc_data_structures", diff --git a/compiler/rustc_data_structures/Cargo.toml b/compiler/rustc_data_structures/Cargo.toml index 5afce15e26bf..0366fb0a148c 100644 --- a/compiler/rustc_data_structures/Cargo.toml +++ b/compiler/rustc_data_structures/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] arrayvec = { version = "0.7", default-features = false } bitflags = "1.2.1" -cfg-if = "0.1.2" +cfg-if = "1.0" ena = "0.14" indexmap = { version = "1.9.1" } jobserver_crate = { version = "0.1.13", package = "jobserver" } @@ -21,7 +21,11 @@ rustc-hash = "1.1.0" rustc_index = { path = "../rustc_index", package = "rustc_index" } rustc_macros = { path = "../rustc_macros" } rustc_serialize = { path = "../rustc_serialize" } -smallvec = { version = "1.8.1", features = ["const_generics", "union", "may_dangle"] } +smallvec = { version = "1.8.1", features = [ + "const_generics", + "union", + "may_dangle", +] } stable_deref_trait = "1.0.0" stacker = "0.1.15" tempfile = "3.2" diff --git a/compiler/rustc_span/Cargo.toml b/compiler/rustc_span/Cargo.toml index 48a2ab0f904a..5ce2577b63c1 100644 --- a/compiler/rustc_span/Cargo.toml +++ b/compiler/rustc_span/Cargo.toml @@ -13,7 +13,7 @@ rustc_index = { path = "../rustc_index" } rustc_arena = { path = "../rustc_arena" } scoped-tls = "1.0" unicode-width = "0.1.4" -cfg-if = "0.1.2" +cfg-if = "1.0" tracing = "0.1" sha1 = { package = "sha-1", version = "0.10.0" } sha2 = "0.10.1" From 8b5a96ec45886bc42090fe313150e648e488eee1 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Dec 2022 10:45:35 +0000 Subject: [PATCH 232/321] Some tracing cleanups --- compiler/rustc_borrowck/src/type_check/mod.rs | 6 ++---- compiler/rustc_middle/src/mir/tcx.rs | 7 +++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 8d4a720f8ce9..36397227d88d 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1153,6 +1153,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { category: ConstraintCategory<'tcx>, ) -> Fallible<()> { let annotated_type = self.user_type_annotations[user_ty.base].inferred_ty; + trace!(?annotated_type); let mut curr_projected_ty = PlaceTy::from_ty(annotated_type); let tcx = self.infcx.tcx; @@ -1170,10 +1171,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { ); curr_projected_ty = projected_ty; } - debug!( - "user_ty base: {:?} freshened: {:?} projs: {:?} yields: {:?}", - user_ty.base, annotated_type, user_ty.projs, curr_projected_ty - ); + trace!(?curr_projected_ty); let ty = curr_projected_ty.ty; self.relate_types(ty, v.xform(ty::Variance::Contravariant), a, locations, category)?; diff --git a/compiler/rustc_middle/src/mir/tcx.rs b/compiler/rustc_middle/src/mir/tcx.rs index 8d2a8f33d6aa..599f0b9d3fab 100644 --- a/compiler/rustc_middle/src/mir/tcx.rs +++ b/compiler/rustc_middle/src/mir/tcx.rs @@ -32,8 +32,9 @@ impl<'tcx> PlaceTy<'tcx> { /// not carry a `Ty` for `T`.) /// /// Note that the resulting type has not been normalized. + #[instrument(level = "debug", skip(tcx), ret)] pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: Field) -> Ty<'tcx> { - let answer = match self.ty.kind() { + match self.ty.kind() { ty::Adt(adt_def, substs) => { let variant_def = match self.variant_index { None => adt_def.non_enum_variant(), @@ -47,9 +48,7 @@ impl<'tcx> PlaceTy<'tcx> { } ty::Tuple(tys) => tys[f.index()], _ => bug!("extracting field of non-tuple non-adt: {:?}", self), - }; - debug!("field_ty self: {:?} f: {:?} yields: {:?}", self, f, answer); - answer + } } /// Convenience wrapper around `projection_ty_core` for From 562d846ff03af63ede8e12c0b02046fa54889ee3 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Dec 2022 12:10:26 +0000 Subject: [PATCH 233/321] Make it easier to debug where a region error was created --- .../src/diagnostics/region_errors.rs | 20 +++++++++++++++++-- .../rustc_borrowck/src/region_infer/mod.rs | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index b5a0044e9e8c..500ce9038bbb 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -22,7 +22,7 @@ use rustc_middle::ty::Region; use rustc_middle::ty::TypeVisitor; use rustc_middle::ty::{self, RegionVid, Ty}; use rustc_span::symbol::{kw, Ident}; -use rustc_span::Span; +use rustc_span::{Span, DUMMY_SP}; use crate::borrowck_errors; use crate::session_diagnostics::{ @@ -70,7 +70,23 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> { /// /// Usually we expect this to either be empty or contain a small number of items, so we can avoid /// allocation most of the time. -pub(crate) type RegionErrors<'tcx> = Vec>; +#[derive(Default)] +pub(crate) struct RegionErrors<'tcx>(Vec>); + +impl<'tcx> RegionErrors<'tcx> { + #[track_caller] + pub fn push(&mut self, val: impl Into>) { + let val = val.into(); + ty::tls::with(|tcx| tcx.sess.delay_span_bug(DUMMY_SP, "{val:?}")); + self.0.push(val); + } + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + pub fn into_iter(self) -> impl Iterator> { + self.0.into_iter() + } +} #[derive(Clone, Debug)] pub(crate) enum RegionErrorKind<'tcx> { diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 0d03346ef0a5..7ccc3cc28963 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -562,7 +562,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { let mir_def_id = body.source.def_id(); self.propagate_constraints(body); - let mut errors_buffer = RegionErrors::new(); + let mut errors_buffer = RegionErrors::default(); // If this is a closure, we can propagate unsatisfied // `outlives_requirements` to our creator, so create a vector From 8b1530260c66069ce1f5cd8d5d32a98b9e1b49cb Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Dec 2022 12:49:10 +0000 Subject: [PATCH 234/321] Replace a `find` with a loop to simplify the logic. --- .../rustc_borrowck/src/region_infer/mod.rs | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 7ccc3cc28963..931732af9041 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -1647,26 +1647,29 @@ impl<'tcx> RegionInferenceContext<'tcx> { let longer_fr_scc = self.constraint_sccs.scc(longer_fr); debug!("check_bound_universal_region: longer_fr_scc={:?}", longer_fr_scc,); - // If we have some bound universal region `'a`, then the only - // elements it can contain is itself -- we don't know anything - // else about it! - let Some(error_element) = ({ - self.scc_values.elements_contained_in(longer_fr_scc).find(|element| match element { - RegionElement::Location(_) => true, - RegionElement::RootUniversalRegion(_) => true, - RegionElement::PlaceholderRegion(placeholder1) => placeholder != *placeholder1, - }) - }) else { - return; - }; - debug!("check_bound_universal_region: error_element = {:?}", error_element); + for error_element in self.scc_values.elements_contained_in(longer_fr_scc) { + match error_element { + RegionElement::Location(_) | RegionElement::RootUniversalRegion(_) => {} + // If we have some bound universal region `'a`, then the only + // elements it can contain is itself -- we don't know anything + // else about it! + RegionElement::PlaceholderRegion(placeholder1) => { + if placeholder == placeholder1 { + continue; + } + } + } - // Find the region that introduced this `error_element`. - errors_buffer.push(RegionErrorKind::BoundUniversalRegionError { - longer_fr, - error_element, - placeholder, - }); + errors_buffer.push(RegionErrorKind::BoundUniversalRegionError { + longer_fr, + error_element, + placeholder, + }); + + // Stop after the first error, it gets too noisy otherwise, and does not provide more information. + break; + } + debug!("check_bound_universal_region: all bounds satisfied"); } #[instrument(level = "debug", skip(self, infcx, errors_buffer))] From e405dabf7dc99cbcd5f483592e8f9b5ea621d110 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Dec 2022 13:14:03 +0000 Subject: [PATCH 235/321] Some style nits --- compiler/rustc_borrowck/src/type_check/mod.rs | 2 +- compiler/rustc_mir_build/src/build/expr/as_constant.rs | 4 ++-- compiler/rustc_mir_build/src/build/matches/mod.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 36397227d88d..2973f8b7f85f 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1163,7 +1163,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { tcx, self.param_env, proj, - |this, field, _| { + |this, field, ()| { let ty = this.field_ty(tcx, field); self.normalize(ty, locations) }, diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 3b7ed818dc9b..1d96893c7a3e 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -66,14 +66,14 @@ pub fn as_constant_inner<'tcx>( let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty); - Constant { span, user_ty: user_ty, literal } + Constant { span, user_ty, literal } } ExprKind::ZstLiteral { ref user_ty } => { let user_ty = user_ty.as_ref().map(push_cuta).flatten(); let literal = ConstantKind::Val(ConstValue::ZeroSized, ty); - Constant { span, user_ty: user_ty, literal } + Constant { span, user_ty, literal } } ExprKind::NamedConst { def_id, substs, ref user_ty } => { let user_ty = user_ty.as_ref().map(push_cuta).flatten(); diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs index 7edcd46a34f2..f90aba80bf3c 100644 --- a/compiler/rustc_mir_build/src/build/matches/mod.rs +++ b/compiler/rustc_mir_build/src/build/matches/mod.rs @@ -2210,7 +2210,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { BindingMode::ByValue => ty::BindingMode::BindByValue(mutability), BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability), }; - let local = LocalDecl::<'tcx> { + let local = LocalDecl { mutability, ty: var_ty, user_ty: if user_ty.is_empty() { None } else { Some(Box::new(user_ty)) }, From c9588d5bf8db14d80249b029b07f79026956ae85 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 19 Dec 2022 13:14:22 +0000 Subject: [PATCH 236/321] Hackily fix an opaque type ICE --- .../rustc_borrowck/src/diagnostics/region_errors.rs | 10 ++++++---- compiler/rustc_borrowck/src/region_infer/mod.rs | 2 +- compiler/rustc_borrowck/src/type_check/mod.rs | 6 ++++++ src/test/ui/type-alias-impl-trait/destructuring.rs | 10 ++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/type-alias-impl-trait/destructuring.rs diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index 500ce9038bbb..bcc8afbfd952 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -18,9 +18,9 @@ use rustc_infer::infer::{ use rustc_middle::hir::place::PlaceBase; use rustc_middle::mir::{ConstraintCategory, ReturnConstraint}; use rustc_middle::ty::subst::InternalSubsts; -use rustc_middle::ty::Region; use rustc_middle::ty::TypeVisitor; use rustc_middle::ty::{self, RegionVid, Ty}; +use rustc_middle::ty::{Region, TyCtxt}; use rustc_span::symbol::{kw, Ident}; use rustc_span::{Span, DUMMY_SP}; @@ -70,14 +70,16 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> { /// /// Usually we expect this to either be empty or contain a small number of items, so we can avoid /// allocation most of the time. -#[derive(Default)] -pub(crate) struct RegionErrors<'tcx>(Vec>); +pub(crate) struct RegionErrors<'tcx>(Vec>, TyCtxt<'tcx>); impl<'tcx> RegionErrors<'tcx> { + pub fn new(tcx: TyCtxt<'tcx>) -> Self { + Self(vec![], tcx) + } #[track_caller] pub fn push(&mut self, val: impl Into>) { let val = val.into(); - ty::tls::with(|tcx| tcx.sess.delay_span_bug(DUMMY_SP, "{val:?}")); + self.1.sess.delay_span_bug(DUMMY_SP, "{val:?}"); self.0.push(val); } pub fn is_empty(&self) -> bool { diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 931732af9041..308f6e19a73e 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -562,7 +562,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { let mir_def_id = body.source.def_id(); self.propagate_constraints(body); - let mut errors_buffer = RegionErrors::default(); + let mut errors_buffer = RegionErrors::new(infcx.tcx); // If this is a closure, we can propagate unsatisfied // `outlives_requirements` to our creator, so create a vector diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 2973f8b7f85f..247607ff29e2 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1159,6 +1159,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { let tcx = self.infcx.tcx; for proj in &user_ty.projs { + if let ty::Alias(ty::Opaque, ..) = curr_projected_ty.ty.kind() { + // There is nothing that we can compare here if we go through an opaque type. + // We're always in its defining scope as we can otherwise not project through + // it, so we're constraining it anyways. + return Ok(()); + } let projected_ty = curr_projected_ty.projection_ty_core( tcx, self.param_env, diff --git a/src/test/ui/type-alias-impl-trait/destructuring.rs b/src/test/ui/type-alias-impl-trait/destructuring.rs new file mode 100644 index 000000000000..b752e58380a8 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/destructuring.rs @@ -0,0 +1,10 @@ +#![feature(type_alias_impl_trait)] + +// check-pass + +// issue: https://github.com/rust-lang/rust/issues/104551 + +fn main() { + type T = impl Sized; + let (_a, _b): T = (1u32, 2u32); +} From f1ef038ae401d8ee7611b2f9fb2aec3af4e9241f Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 30 Nov 2022 11:09:22 +0000 Subject: [PATCH 237/321] use `track_caller` to show where the panic is actually from --- compiler/rustc_middle/src/ty/subst.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index a04b15f8cf13..2071d0102726 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -400,6 +400,7 @@ impl<'tcx> InternalSubsts<'tcx> { } #[inline] + #[track_caller] pub fn type_at(&self, i: usize) -> Ty<'tcx> { if let GenericArgKind::Type(ty) = self[i].unpack() { ty @@ -409,6 +410,7 @@ impl<'tcx> InternalSubsts<'tcx> { } #[inline] + #[track_caller] pub fn region_at(&self, i: usize) -> ty::Region<'tcx> { if let GenericArgKind::Lifetime(lt) = self[i].unpack() { lt @@ -418,6 +420,7 @@ impl<'tcx> InternalSubsts<'tcx> { } #[inline] + #[track_caller] pub fn const_at(&self, i: usize) -> ty::Const<'tcx> { if let GenericArgKind::Const(ct) = self[i].unpack() { ct @@ -427,6 +430,7 @@ impl<'tcx> InternalSubsts<'tcx> { } #[inline] + #[track_caller] pub fn type_for_def(&self, def: &ty::GenericParamDef) -> GenericArg<'tcx> { self.type_at(def.index as usize).into() } From 872a6da935afa11c9b836d685efd591ca7a64481 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 6 Oct 2022 11:44:42 +0000 Subject: [PATCH 238/321] Remove an unused function --- compiler/rustc_middle/src/ty/generics.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index 2e70ac256a71..705adecd3b90 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -70,14 +70,6 @@ impl GenericParamDef { } } - pub fn has_default(&self) -> bool { - match self.kind { - GenericParamDefKind::Type { has_default, .. } - | GenericParamDefKind::Const { has_default } => has_default, - GenericParamDefKind::Lifetime => false, - } - } - pub fn is_anonymous_lifetime(&self) -> bool { match self.kind { GenericParamDefKind::Lifetime => { From c787de3bbd307cccbfaeb56421ad7ad898330676 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 11 Oct 2022 19:11:56 +0000 Subject: [PATCH 239/321] Fix some `~const` usage in libcore --- library/core/src/const_closure.rs | 4 ++-- library/core/src/hash/mod.rs | 4 ++-- library/core/src/ops/index.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/core/src/const_closure.rs b/library/core/src/const_closure.rs index 151c8e6d8986..920c31233c18 100644 --- a/library/core/src/const_closure.rs +++ b/library/core/src/const_closure.rs @@ -51,7 +51,7 @@ macro_rules! impl_fn_mut_tuple { impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const FnOnce for ConstFnMutClosure<($(&'a mut $var),*), Function> where - Function: ~const Fn(($(&mut $var),*), ClosureArguments) -> ClosureReturnValue+ ~const Destruct, + Function: ~const Fn(($(&mut $var),*), ClosureArguments) -> ClosureReturnValue + ~const Destruct, { type Output = ClosureReturnValue; @@ -64,7 +64,7 @@ macro_rules! impl_fn_mut_tuple { impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const FnMut for ConstFnMutClosure<($(&'a mut $var),*), Function> where - Function: ~const Fn(($(&mut $var),*), ClosureArguments)-> ClosureReturnValue, + Function: ~const Fn(($(&mut $var),*), ClosureArguments)-> ClosureReturnValue + ~const Destruct, { extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output { #[allow(non_snake_case)] diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index c755afa39eb6..71a0d1825efe 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -199,7 +199,7 @@ pub trait Hash { /// println!("Hash is {:x}!", hasher.finish()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - fn hash(&self, state: &mut H); + fn hash(&self, state: &mut H); /// Feeds a slice of this type into the given [`Hasher`]. /// @@ -980,7 +980,7 @@ mod impls { #[rustc_const_unstable(feature = "const_hash", issue = "104061")] impl const Hash for &mut T { #[inline] - fn hash(&self, state: &mut H) { + fn hash(&self, state: &mut H) { (**self).hash(state); } } diff --git a/library/core/src/ops/index.rs b/library/core/src/ops/index.rs index 5e3dc48b6ca1..228efb0bc0a5 100644 --- a/library/core/src/ops/index.rs +++ b/library/core/src/ops/index.rs @@ -165,7 +165,7 @@ see chapter in The Book : Index { +pub trait IndexMut: ~const Index { /// Performs the mutable indexing (`container[index]`) operation. /// /// # Panics From b0ed631ad47a15df6e23f4a34fd2f75162341889 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 10 Nov 2022 08:47:16 +0000 Subject: [PATCH 240/321] Some hir cleanups --- compiler/rustc_middle/src/hir/map/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 0450abed51b0..cf5ebad40328 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -408,7 +408,7 @@ impl<'hir> Map<'hir> { /// item (possibly associated), a closure, or a `hir::AnonConst`. pub fn body_owner(self, BodyId { hir_id }: BodyId) -> HirId { let parent = self.get_parent_node(hir_id); - assert!(self.find(parent).map_or(false, |n| is_body_owner(n, hir_id))); + assert!(self.find(parent).map_or(false, |n| is_body_owner(n, hir_id)), "{hir_id:?}"); parent } @@ -419,7 +419,7 @@ impl<'hir> Map<'hir> { /// Given a `LocalDefId`, returns the `BodyId` associated with it, /// if the node is a body owner, otherwise returns `None`. pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option { - self.get_if_local(id.to_def_id()).map(associated_body).flatten() + self.find_by_def_id(id).and_then(associated_body) } /// Given a body owner's id, returns the `BodyId` associated with it. From b2b859be8ccb2951ffbba7de7461111d9a62883a Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 12 Oct 2022 14:16:31 +0000 Subject: [PATCH 241/321] Some track_caller additions --- compiler/rustc_middle/src/hir/map/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index cf5ebad40328..f5cb89fa6246 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -170,6 +170,7 @@ impl<'hir> Map<'hir> { } #[inline] + #[track_caller] pub fn local_def_id(self, hir_id: HirId) -> LocalDefId { self.opt_local_def_id(hir_id).unwrap_or_else(|| { bug!( @@ -310,6 +311,7 @@ impl<'hir> Map<'hir> { } } + #[track_caller] pub fn get_parent_node(self, hir_id: HirId) -> HirId { self.find_parent_node(hir_id) .unwrap_or_else(|| bug!("No parent for node {:?}", self.node_to_string(hir_id))) @@ -334,12 +336,14 @@ impl<'hir> Map<'hir> { } /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found. + #[track_caller] pub fn get(self, id: HirId) -> Node<'hir> { self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id)) } /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found. #[inline] + #[track_caller] pub fn get_by_def_id(self, id: LocalDefId) -> Node<'hir> { self.find_by_def_id(id).unwrap_or_else(|| bug!("couldn't find {:?} in the HIR map", id)) } @@ -377,6 +381,7 @@ impl<'hir> Map<'hir> { self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies[&id.hir_id.local_id] } + #[track_caller] pub fn fn_decl_by_hir_id(self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> { if let Some(node) = self.find(hir_id) { node.fn_decl() @@ -385,6 +390,7 @@ impl<'hir> Map<'hir> { } } + #[track_caller] pub fn fn_sig_by_hir_id(self, hir_id: HirId) -> Option<&'hir FnSig<'hir>> { if let Some(node) = self.find(hir_id) { node.fn_sig() @@ -393,6 +399,7 @@ impl<'hir> Map<'hir> { } } + #[track_caller] pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId { for (_, node) in self.parent_iter(hir_id) { if let Some(body) = associated_body(node) { @@ -423,6 +430,7 @@ impl<'hir> Map<'hir> { } /// Given a body owner's id, returns the `BodyId` associated with it. + #[track_caller] pub fn body_owned_by(self, id: LocalDefId) -> BodyId { self.maybe_body_owned_by(id).unwrap_or_else(|| { let hir_id = self.local_def_id_to_hir_id(id); From fedcc739c6788dbfbb1bfa07cacceaa9fa924382 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 11 Oct 2022 11:05:41 +0000 Subject: [PATCH 242/321] tracing: make flag checking less noisy --- compiler/rustc_middle/src/ty/visit.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index b302572f3cab..d5553f84f751 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -88,9 +88,11 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone { self.has_vars_bound_at_or_above(ty::INNERMOST) } - #[instrument(level = "trace", ret)] fn has_type_flags(&self, flags: TypeFlags) -> bool { - self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value() == Some(FoundFlags) + let res = + self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value() == Some(FoundFlags); + trace!(?self, ?flags, ?res, "has_type_flags"); + res } fn has_projections(&self) -> bool { self.has_type_flags(TypeFlags::HAS_PROJECTION) @@ -560,10 +562,8 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor { type BreakTy = FoundFlags; #[inline] - #[instrument(skip(self), level = "trace", ret)] fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow { let flags = t.flags(); - trace!(t.flags=?t.flags()); if flags.intersects(self.flags) { ControlFlow::Break(FoundFlags) } else { @@ -572,10 +572,8 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor { } #[inline] - #[instrument(skip(self), level = "trace", ret)] fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow { let flags = r.type_flags(); - trace!(r.flags=?flags); if flags.intersects(self.flags) { ControlFlow::Break(FoundFlags) } else { @@ -584,7 +582,6 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor { } #[inline] - #[instrument(level = "trace", ret)] fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow { let flags = FlagComputation::for_const(c); trace!(r.flags=?flags); @@ -596,14 +593,7 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor { } #[inline] - #[instrument(level = "trace", ret)] fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow { - debug!( - "HasTypeFlagsVisitor: predicate={:?} predicate.flags={:?} self.flags={:?}", - predicate, - predicate.flags(), - self.flags - ); if predicate.flags().intersects(self.flags) { ControlFlow::Break(FoundFlags) } else { From ed61be60da3b5dd28abd4ec4956df9eb896183c9 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 21 Sep 2022 08:14:31 +0000 Subject: [PATCH 243/321] Some ICE debugging aids --- compiler/rustc_middle/src/ty/subst.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index a04b15f8cf13..eeb2afb03492 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -348,7 +348,7 @@ impl<'tcx> InternalSubsts<'tcx> { substs.reserve(defs.params.len()); for param in &defs.params { let kind = mk_kind(param, substs); - assert_eq!(param.index as usize, substs.len()); + assert_eq!(param.index as usize, substs.len(), "{substs:#?}, {defs:#?}"); substs.push(kind); } } From 531f3c9ee584c7b7a5ed6e35383c10858bcbb049 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 20 Dec 2022 15:26:42 +0000 Subject: [PATCH 244/321] Bump packed_simd_2 and getrandom v0.2 Remove old versions of `cfg-if` by bumping `packed_simd_2` and `getrandom v0.2` versions ```console > cargo update --package packed_simd_2 --package getrandom@0.2.0 Updating crates.io index Removing cfg-if v0.1.10 Updating getrandom v0.2.0 -> v0.2.8 Updating packed_simd_2 v0.3.4 -> v0.3.8 ``` `packed_simd_2` is only used as a dependency `bytecount`. The bigger jump is `getrandom@0.2.0`, but it's still semver compatible and there doesn't seem to be any worrying changes (see the [changelog](https://github.com/rust-random/getrandom/blob/master/CHANGELOG.md)). Note that this doesn't affect `getrandom@0.1.16`, which is already using the latest version of `cfg-if` (besides, there are already plans to remove that entirely). --- Cargo.lock | 94 +++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d09068dbdf3..d381c3916d92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,7 +30,7 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43bb833f0bf979d8475d38fbf09ed3b8a55e1885fe93ad3f93239fc6a4f17b98" dependencies = [ - "getrandom 0.2.0", + "getrandom 0.2.8", "once_cell", "version_check", ] @@ -181,7 +181,7 @@ checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7" dependencies = [ "addr2line", "cc", - "cfg-if 1.0.0", + "cfg-if", "libc", "miniz_oxide", "object", @@ -502,12 +502,6 @@ dependencies = [ "jobserver", ] -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.0" @@ -951,7 +945,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -960,7 +954,7 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crossbeam-utils", ] @@ -970,7 +964,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crossbeam-epoch", "crossbeam-utils", ] @@ -982,7 +976,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a" dependencies = [ "autocfg", - "cfg-if 1.0.0", + "cfg-if", "crossbeam-utils", "memoffset", "scopeguard", @@ -994,7 +988,7 @@ version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -1138,7 +1132,7 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "dirs-sys-next", ] @@ -1341,7 +1335,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "redox_syscall", "winapi", @@ -1359,7 +1353,7 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39522e96686d38f4bc984b9198e3a0613264abaebaff2c5c918bfa6b6da09af" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "crc32fast", "libc", "libz-sys", @@ -1598,20 +1592,20 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "wasi 0.9.0+wasi-snapshot-preview1", ] [[package]] name = "getrandom" -version = "0.2.0" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libc", - "wasi 0.9.0+wasi-snapshot-preview1", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -1966,7 +1960,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -2150,7 +2144,7 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0cf036d15402bea3c5d4de17b3fce76b3e4a56ebc1f577be0e7a72f7c607cf0" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "winapi", ] @@ -2251,7 +2245,7 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -2433,7 +2427,7 @@ version = "0.1.0" dependencies = [ "colored", "env_logger 0.9.0", - "getrandom 0.2.0", + "getrandom 0.2.8", "lazy_static", "libc", "libffi", @@ -2529,7 +2523,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a518809ac14b25b569624d0268eba1e88498f71615893dca57982bed7621abb" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -2555,7 +2549,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c7ae222234c30df141154f159066c5093ff73b63204dcda7121eb082fc56a95" dependencies = [ "bitflags", - "cfg-if 1.0.0", + "cfg-if", "foreign-types", "libc", "once_cell", @@ -2625,11 +2619,11 @@ checksum = "decf7381921fea4dcb2549c5667eda59b3ec297ab7e2b5fc33eac69d2e7da87b" [[package]] name = "packed_simd_2" -version = "0.3.4" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3278e0492f961fd4ae70909f56b2723a7e8d01a228427294e19cdfdebda89a17" +checksum = "a1914cd452d8fccd6f9db48147b29fd4ae05bea9dc5d9ad578509f72415de282" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "libm", ] @@ -2638,7 +2632,7 @@ name = "panic_abort" version = "0.0.0" dependencies = [ "alloc", - "cfg-if 1.0.0", + "cfg-if", "compiler_builtins", "core", "libc", @@ -2649,7 +2643,7 @@ name = "panic_unwind" version = "0.0.0" dependencies = [ "alloc", - "cfg-if 1.0.0", + "cfg-if", "compiler_builtins", "core", "libc", @@ -2683,7 +2677,7 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "instant", "libc", "redox_syscall", @@ -2697,7 +2691,7 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "redox_syscall", "smallvec", @@ -3030,7 +3024,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" dependencies = [ - "getrandom 0.2.0", + "getrandom 0.2.8", ] [[package]] @@ -3099,7 +3093,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" dependencies = [ - "getrandom 0.2.0", + "getrandom 0.2.8", "redox_syscall", ] @@ -3590,7 +3584,7 @@ version = "0.0.0" dependencies = [ "arrayvec", "bitflags", - "cfg-if 1.0.0", + "cfg-if", "ena", "indexmap", "jobserver", @@ -4374,7 +4368,7 @@ dependencies = [ name = "rustc_span" version = "0.0.0" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "md-5", "rustc_arena", "rustc_data_structures", @@ -4783,7 +4777,7 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -4794,7 +4788,7 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99c3bd8169c58782adad9290a9af5939994036b76187f7b4f0e6de91dbbfc0ec" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "cpufeatures", "digest", ] @@ -4936,7 +4930,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce" dependencies = [ "cc", - "cfg-if 1.0.0", + "cfg-if", "libc", "psm", "winapi", @@ -4954,7 +4948,7 @@ version = "0.0.0" dependencies = [ "addr2line", "alloc", - "cfg-if 1.0.0", + "cfg-if", "compiler_builtins", "core", "dlmalloc", @@ -4978,7 +4972,7 @@ dependencies = [ name = "std_detect" version = "0.1.5" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "compiler_builtins", "libc", "rustc-std-workspace-alloc", @@ -5085,7 +5079,7 @@ version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "fastrand", "libc", "redox_syscall", @@ -5138,7 +5132,7 @@ dependencies = [ name = "test" version = "0.0.0" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "core", "getopts", "libc", @@ -5154,7 +5148,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0639d10d8f4615f223a57275cf40f9bdb7cfbb806bcb7f7cc56e3beb55a576eb" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "getopts", "libc", "num_cpus", @@ -5321,7 +5315,7 @@ version = "0.1.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -5407,7 +5401,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "rand 0.8.5", "static_assertions", ] @@ -5654,7 +5648,7 @@ name = "unwind" version = "0.0.0" dependencies = [ "cc", - "cfg-if 1.0.0", + "cfg-if", "compiler_builtins", "core", "libc", @@ -5691,7 +5685,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" dependencies = [ - "getrandom 0.2.0", + "getrandom 0.2.8", ] [[package]] From 168e3da8126f78d213e17de7fb548a9bf76ee393 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 20 Dec 2022 10:32:35 -0700 Subject: [PATCH 245/321] rustdoc: prevent CSS layout of line numbers shrinking into nothing --- src/librustdoc/html/static/css/rustdoc.css | 1 + .../rustdoc-gui/scrape-examples-layout.goml | 35 +++++++++++++++++++ .../scrape_examples/examples/check-many-1.rs | 10 ++++++ .../scrape_examples/examples/check-many-2.rs | 12 ++++++- .../scrape_examples/examples/check-many-3.rs | 12 ++++++- .../scrape_examples/examples/check-many-4.rs | 10 ++++++ .../scrape_examples/examples/check-many-5.rs | 10 ++++++ .../scrape_examples/examples/check-many-6.rs | 10 ++++++ .../scrape_examples/examples/check-many-7.rs | 10 ++++++ 9 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/test/rustdoc-gui/scrape-examples-layout.goml diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 11e34a3fc7d1..055a5665cf0f 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -543,6 +543,7 @@ ul.block, .block li { .rustdoc .example-wrap > pre.example-line-numbers, .rustdoc .example-wrap > pre.src-line-numbers { flex-grow: 0; + min-width: fit-content; /* prevent collapsing into nothing in truncated scraped examples */ overflow: initial; text-align: right; -webkit-user-select: none; diff --git a/src/test/rustdoc-gui/scrape-examples-layout.goml b/src/test/rustdoc-gui/scrape-examples-layout.goml new file mode 100644 index 000000000000..988c911b7839 --- /dev/null +++ b/src/test/rustdoc-gui/scrape-examples-layout.goml @@ -0,0 +1,35 @@ +// Check that the line number column has the correct layout. +goto: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html" + +// Check that it's not zero. +assert-property-false: ( + ".more-scraped-examples .scraped-example .code-wrapper .src-line-numbers", + {"clientWidth": "0"} +) + +// Check that examples with very long lines have the same width as ones that don't. +store-property: ( + clientWidth, + ".more-scraped-examples .scraped-example:nth-child(1) .code-wrapper .src-line-numbers", + "clientWidth" +) + +assert-property: ( + ".more-scraped-examples .scraped-example:nth-child(2) .code-wrapper .src-line-numbers", + {"clientWidth": |clientWidth|} +) + +assert-property: ( + ".more-scraped-examples .scraped-example:nth-child(3) .code-wrapper .src-line-numbers", + {"clientWidth": |clientWidth|} +) + +assert-property: ( + ".more-scraped-examples .scraped-example:nth-child(4) .code-wrapper .src-line-numbers", + {"clientWidth": |clientWidth|} +) + +assert-property: ( + ".more-scraped-examples .scraped-example:nth-child(5) .code-wrapper .src-line-numbers", + {"clientWidth": |clientWidth|} +) diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs index 1d1bc5002aa8..81a48ac50c81 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-1.rs @@ -1,3 +1,13 @@ fn main() { + // all examples have same line count + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); scrape_examples::test_many(); } diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs index 1d1bc5002aa8..c9fdf68d3be0 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-2.rs @@ -1,3 +1,13 @@ fn main() { - scrape_examples::test_many(); + // ignore-tidy-linelength + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ } diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs index 1d1bc5002aa8..c9fdf68d3be0 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-3.rs @@ -1,3 +1,13 @@ fn main() { - scrape_examples::test_many(); + // ignore-tidy-linelength + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ + scrape_examples::test_many(); /* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. */ } diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs index 1d1bc5002aa8..81a48ac50c81 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-4.rs @@ -1,3 +1,13 @@ fn main() { + // all examples have same line count + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); scrape_examples::test_many(); } diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs index 1d1bc5002aa8..81a48ac50c81 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-5.rs @@ -1,3 +1,13 @@ fn main() { + // all examples have same line count + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); scrape_examples::test_many(); } diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs index 1d1bc5002aa8..81a48ac50c81 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-6.rs @@ -1,3 +1,13 @@ fn main() { + // all examples have same line count + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); scrape_examples::test_many(); } diff --git a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs index 1d1bc5002aa8..81a48ac50c81 100644 --- a/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs +++ b/src/test/rustdoc-gui/src/scrape_examples/examples/check-many-7.rs @@ -1,3 +1,13 @@ fn main() { + // all examples have same line count + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); + scrape_examples::test_many(); scrape_examples::test_many(); } From 738b0c06730e4e2c74901d554dcb7d1b40b5cd0a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 20 Dec 2022 18:30:12 +0000 Subject: [PATCH 246/321] Re-enable fn trait call notation error --- compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs | 2 +- src/test/ui/unboxed-closures/non-tupled-call.rs | 17 +++++++++++++++++ .../ui/unboxed-closures/non-tupled-call.stderr | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/unboxed-closures/non-tupled-call.rs create mode 100644 src/test/ui/unboxed-closures/non-tupled-call.stderr diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index d1e0964112bc..877680053f09 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -214,7 +214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { "cannot use call notation; the first type parameter \ for the function trait is neither a tuple nor unit" ) - .delay_as_bug(); + .emit(); (self.err_args(provided_args.len()), None) } } diff --git a/src/test/ui/unboxed-closures/non-tupled-call.rs b/src/test/ui/unboxed-closures/non-tupled-call.rs new file mode 100644 index 000000000000..08bea4f1678b --- /dev/null +++ b/src/test/ui/unboxed-closures/non-tupled-call.rs @@ -0,0 +1,17 @@ +#![feature(fn_traits, unboxed_closures, tuple_trait)] + +use std::default::Default; +use std::marker::Tuple; + +fn wrap(func: impl Fn) { + let x: P = Default::default(); + // Should be: `func.call(x);` + func(x); + //~^ ERROR cannot use call notation; the first type parameter for the function trait is neither a tuple nor unit +} + +fn foo() {} + +fn main() { + wrap(foo); +} diff --git a/src/test/ui/unboxed-closures/non-tupled-call.stderr b/src/test/ui/unboxed-closures/non-tupled-call.stderr new file mode 100644 index 000000000000..35ac9ebe2910 --- /dev/null +++ b/src/test/ui/unboxed-closures/non-tupled-call.stderr @@ -0,0 +1,9 @@ +error[E0059]: cannot use call notation; the first type parameter for the function trait is neither a tuple nor unit + --> $DIR/non-tupled-call.rs:9:5 + | +LL | func(x); + | ^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0059`. From 1c5b53be1c5ea2f95b1b3e4327c69f14127a923c Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 20 Dec 2022 18:59:52 +0000 Subject: [PATCH 247/321] Avoid going through the happy path in case of non-fn builtin calls --- compiler/rustc_hir_typeck/src/callee.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 7a5191b77f1d..4ec71a78a003 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -4,7 +4,7 @@ use super::{Expectation, FnCtxt, TupleArgumentsFlag}; use crate::type_error_struct; use rustc_ast::util::parser::PREC_POSTFIX; -use rustc_errors::{struct_span_err, Applicability, Diagnostic, StashKey}; +use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed, StashKey}; use rustc_hir as hir; use rustc_hir::def::{self, CtorKind, Namespace, Res}; use rustc_hir::def_id::DefId; @@ -424,21 +424,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } - self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs); + let err = self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs); - // This is the "default" function signature, used in case of error. - // In that case, we check each argument against "error" in order to - // set up all the node type bindings. - ( - ty::Binder::dummy(self.tcx.mk_fn_sig( - self.err_args(arg_exprs.len()).into_iter(), - self.tcx.ty_error(), - false, - hir::Unsafety::Normal, - abi::Abi::Rust, - )), - None, - ) + return self.tcx.ty_error_with_guaranteed(err); } }; @@ -591,7 +579,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { callee_expr: &'tcx hir::Expr<'tcx>, callee_ty: Ty<'tcx>, arg_exprs: &'tcx [hir::Expr<'tcx>], - ) { + ) -> ErrorGuaranteed { let mut unit_variant = None; if let hir::ExprKind::Path(qpath) = &callee_expr.kind && let Res::Def(def::DefKind::Ctor(kind, CtorKind::Const), _) @@ -720,7 +708,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err.span_label(span, label); } } - err.emit(); + err.emit() } fn confirm_deferred_closure_call( From b29a9e3b3f415591240c800a0e7a274cc49e2465 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 20 Dec 2022 12:44:22 -0700 Subject: [PATCH 248/321] rustdoc: simplify section anchor CSS Since f50bf8636e3b0296db82e631fe95c84324a46ccc changed anchors to be always positioned absolute, specifying it on hover as well is redundant. --- src/librustdoc/html/static/css/rustdoc.css | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 11e34a3fc7d1..30d70f1a0994 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -689,14 +689,10 @@ a { position: relative; } -.small-section-header:hover > .anchor { +.small-section-header:hover > .anchor, .impl:hover > .anchor, +.trait-impl:hover > .anchor, .variant:hover > .anchor { display: initial; } - -.impl:hover > .anchor, .trait-impl:hover > .anchor, .variant:hover > .anchor { - display: inline-block; - position: absolute; -} .anchor { display: none; position: absolute; From 5480ac540cdb88a1512334c5680ccf7cd76a38c8 Mon Sep 17 00:00:00 2001 From: Arvind Mukund Date: Tue, 20 Dec 2022 11:44:12 -0800 Subject: [PATCH 249/321] Use `Error` behavior for LLVM versions prior to 15 CI fails when building with LLVM-13. This raises unknown behavior constant `8` from IRVerifier. --- compiler/rustc_codegen_llvm/src/context.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 9c7d3dabd6fb..d9ccba07a346 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -280,29 +280,35 @@ pub unsafe fn create_module<'ll>( } if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { + let behavior = if llvm_version >= (15, 0, 0) { + llvm::LLVMModFlagBehavior::Min + } else { + llvm::LLVMModFlagBehavior::Error + }; + if sess.target.arch == "aarch64" { llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "branch-target-enforcement\0".as_ptr().cast(), bti.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "sign-return-address\0".as_ptr().cast(), pac_ret.is_some().into(), ); let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "sign-return-address-all\0".as_ptr().cast(), pac_opts.leaf.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "sign-return-address-with-bkey\0".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); From a65ec44779ab2c01ba937b40496ca648626d0308 Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Sat, 17 Dec 2022 02:50:08 +0100 Subject: [PATCH 250/321] Add `-Zno-jump-tables` This flag mimics GCC/Clang's `-fno-jump-tables` [1][2], which makes the codegen backend avoid generating jump tables when lowering switches. In the case of LLVM, the `"no-jump-tables"="true"` function attribute is added to every function. The kernel currently needs it for x86 when enabling IBT [3], as well as for Alpha (plus VDSO objects in MIPS/LoongArch). [1] https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fno-jump-tables [2] https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fjump-tables [3] https://github.com/torvalds/linux/blob/v6.1/arch/x86/Makefile#L75-L83 Signed-off-by: Miguel Ojeda --- compiler/rustc_codegen_llvm/src/attributes.rs | 9 +++++ compiler/rustc_interface/src/tests.rs | 1 + compiler/rustc_session/src/options.rs | 2 ++ src/test/assembly/x86_64-no-jump-tables.rs | 34 +++++++++++++++++++ src/test/codegen/no-jump-tables.rs | 22 ++++++++++++ src/test/rustdoc-ui/z-help.stdout | 1 + 6 files changed, 69 insertions(+) create mode 100644 src/test/assembly/x86_64-no-jump-tables.rs create mode 100644 src/test/codegen/no-jump-tables.rs diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index f3bdacf60855..487eead22b89 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -137,6 +137,14 @@ fn instrument_function_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribu } } +fn nojumptables_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> { + if !cx.sess().opts.unstable_opts.no_jump_tables { + return None; + } + + Some(llvm::CreateAttrStringValue(cx.llcx, "no-jump-tables", "true")) +} + fn probestack_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> { // Currently stack probes seem somewhat incompatible with the address // sanitizer and thread sanitizer. With asan we're already protected from @@ -293,6 +301,7 @@ pub fn from_fn_attrs<'ll, 'tcx>( // FIXME: none of these three functions interact with source level attributes. to_add.extend(frame_pointer_type_attr(cx)); to_add.extend(instrument_function_attr(cx)); + to_add.extend(nojumptables_attr(cx)); to_add.extend(probestack_attr(cx)); to_add.extend(stackprotector_attr(cx)); diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index e903cb86dd20..ff2196d58577 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -754,6 +754,7 @@ fn test_unstable_options_tracking_hash() { tracked!(move_size_limit, Some(4096)); tracked!(mutable_noalias, Some(true)); tracked!(no_generate_arange_section, true); + tracked!(no_jump_tables, true); tracked!(no_link, true); tracked!(no_profiler_runtime, true); tracked!(no_unique_section_names, true); diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 9e130287104f..40bc669707aa 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1421,6 +1421,8 @@ options! { "run all passes except codegen; no output"), no_generate_arange_section: bool = (false, parse_no_flag, [TRACKED], "omit DWARF address ranges that give faster lookups"), + no_jump_tables: bool = (false, parse_no_flag, [TRACKED], + "disable the jump tables and lookup tables that can be generated from a switch case lowering"), no_leak_check: bool = (false, parse_no_flag, [UNTRACKED], "disable the 'leak check' for subtyping; unsound, but useful for tests"), no_link: bool = (false, parse_no_flag, [TRACKED], diff --git a/src/test/assembly/x86_64-no-jump-tables.rs b/src/test/assembly/x86_64-no-jump-tables.rs new file mode 100644 index 000000000000..007c3591a4a6 --- /dev/null +++ b/src/test/assembly/x86_64-no-jump-tables.rs @@ -0,0 +1,34 @@ +// Test that jump tables are (not) emitted when the `-Zno-jump-tables` +// flag is (not) set. + +// revisions: unset set +// assembly-output: emit-asm +// compile-flags: -O +// [set] compile-flags: -Zno-jump-tables +// only-x86_64 + +#![crate_type = "lib"] + +extern "C" { + fn bar1(); + fn bar2(); + fn bar3(); + fn bar4(); + fn bar5(); + fn bar6(); +} + +// CHECK-LABEL: foo: +#[no_mangle] +pub unsafe fn foo(x: i32) { + // unset: LJTI0_0 + // set-NOT: LJTI0_0 + match x { + 1 => bar1(), + 2 => bar2(), + 3 => bar3(), + 4 => bar4(), + 5 => bar5(), + _ => bar6(), + } +} diff --git a/src/test/codegen/no-jump-tables.rs b/src/test/codegen/no-jump-tables.rs new file mode 100644 index 000000000000..8e2cb47566ea --- /dev/null +++ b/src/test/codegen/no-jump-tables.rs @@ -0,0 +1,22 @@ +// Test that the `no-jump-tables` function attribute are (not) emitted when +// the `-Zno-jump-tables` flag is (not) set. + +// revisions: unset set +// needs-llvm-components: x86 +// compile-flags: --target x86_64-unknown-linux-gnu +// [set] compile-flags: -Zno-jump-tables + +#![crate_type = "lib"] +#![feature(no_core, lang_items)] +#![no_core] + +#[lang = "sized"] +trait Sized {} + +#[no_mangle] +pub fn foo() { + // CHECK: @foo() unnamed_addr #0 + + // unset-NOT: attributes #0 = { {{.*}}"no-jump-tables"="true"{{.*}} } + // set: attributes #0 = { {{.*}}"no-jump-tables"="true"{{.*}} } +} diff --git a/src/test/rustdoc-ui/z-help.stdout b/src/test/rustdoc-ui/z-help.stdout index 3537e669608d..53677b183770 100644 --- a/src/test/rustdoc-ui/z-help.stdout +++ b/src/test/rustdoc-ui/z-help.stdout @@ -92,6 +92,7 @@ -Z no-analysis=val -- parse and expand the source, but run no analysis -Z no-codegen=val -- run all passes except codegen; no output -Z no-generate-arange-section=val -- omit DWARF address ranges that give faster lookups + -Z no-jump-tables=val -- disable the jump tables and lookup tables that can be generated from a switch case lowering -Z no-leak-check=val -- disable the 'leak check' for subtyping; unsound, but useful for tests -Z no-link=val -- compile without linking -Z no-parallel-llvm=val -- run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO) From 3dde32ca974721daa4dfd526226d6c9e707fd8af Mon Sep 17 00:00:00 2001 From: Jeremy Stucki Date: Tue, 20 Dec 2022 22:10:40 +0100 Subject: [PATCH 251/321] rustc: Remove needless lifetimes --- compiler/rustc_ast/src/util/comments.rs | 2 +- compiler/rustc_ast_lowering/src/lib.rs | 2 +- compiler/rustc_borrowck/src/consumers.rs | 6 +- .../src/diagnostics/find_all_local_uses.rs | 2 +- compiler/rustc_borrowck/src/lib.rs | 5 +- .../src/type_check/free_region_relations.rs | 6 +- compiler/rustc_builtin_macros/src/asm.rs | 2 +- compiler/rustc_codegen_gcc/src/base.rs | 2 +- compiler/rustc_codegen_gcc/src/common.rs | 2 +- compiler/rustc_codegen_gcc/src/lib.rs | 2 +- .../src/coverageinfo/mapgen.rs | 4 +- .../src/debuginfo/metadata.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 6 +- compiler/rustc_codegen_ssa/src/back/link.rs | 8 +-- .../src/back/symbol_export.rs | 6 +- .../rustc_codegen_ssa/src/debuginfo/mod.rs | 2 +- compiler/rustc_codegen_ssa/src/meth.rs | 2 +- .../rustc_codegen_ssa/src/target_features.rs | 2 +- .../rustc_data_structures/src/profiling.rs | 11 ++-- .../src/transitive_relation.rs | 2 +- compiler/rustc_data_structures/src/unord.rs | 6 +- compiler/rustc_error_messages/src/lib.rs | 4 +- compiler/rustc_expand/src/mbe/quoted.rs | 2 +- compiler/rustc_graphviz/src/lib.rs | 2 +- .../rustc_hir_analysis/src/check/check.rs | 14 ++-- .../src/check/compare_method.rs | 4 +- compiler/rustc_hir_analysis/src/check/mod.rs | 6 +- .../rustc_hir_analysis/src/check/wfcheck.rs | 2 +- .../src/coherence/builtin.rs | 4 +- compiler/rustc_hir_analysis/src/collect.rs | 2 +- .../src/collect/lifetimes.rs | 4 +- .../src/collect/predicates_of.rs | 6 +- .../src/impl_wf_check/min_specialization.rs | 6 +- .../src/outlives/implicit_infer.rs | 6 +- compiler/rustc_hir_typeck/src/coercion.rs | 2 +- .../rustc_hir_typeck/src/expr_use_visitor.rs | 4 +- .../src/generator_interior/drop_ranges/mod.rs | 2 +- compiler/rustc_hir_typeck/src/lib.rs | 4 +- compiler/rustc_hir_typeck/src/op.rs | 2 +- compiler/rustc_hir_typeck/src/upvar.rs | 32 ++++----- .../rustc_incremental/src/assert_dep_graph.rs | 5 +- .../src/infer/error_reporting/mod.rs | 4 +- compiler/rustc_lint/src/non_fmt_panic.rs | 2 +- compiler/rustc_lint_defs/src/builtin.rs | 2 +- .../src/diagnostics/subdiagnostic.rs | 2 +- .../rustc_macros/src/diagnostics/utils.rs | 2 +- compiler/rustc_metadata/src/creader.rs | 4 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- .../rustc_middle/src/dep_graph/dep_node.rs | 2 +- compiler/rustc_middle/src/hir/map/mod.rs | 4 +- compiler/rustc_middle/src/middle/stability.rs | 4 +- compiler/rustc_middle/src/mir/mod.rs | 4 +- compiler/rustc_middle/src/mir/pretty.rs | 4 +- compiler/rustc_middle/src/mir/spanview.rs | 19 ++---- compiler/rustc_middle/src/mir/traversal.rs | 2 +- compiler/rustc_middle/src/traits/chalk.rs | 65 ++++++++----------- .../src/traits/specialization_graph.rs | 8 +-- compiler/rustc_middle/src/ty/closure.rs | 5 +- compiler/rustc_middle/src/ty/consts.rs | 2 +- compiler/rustc_middle/src/ty/consts/int.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 4 +- compiler/rustc_middle/src/ty/flags.rs | 2 +- compiler/rustc_middle/src/ty/layout.rs | 2 +- compiler/rustc_middle/src/ty/util.rs | 2 +- .../src/build/expr/as_place.rs | 4 +- compiler/rustc_mir_build/src/build/mod.rs | 12 ++-- .../rustc_mir_build/src/check_unsafety.rs | 8 +-- compiler/rustc_mir_build/src/thir/cx/mod.rs | 11 ++-- .../src/thir/pattern/const_to_pat.rs | 2 +- .../src/thir/pattern/deconstruct_pat.rs | 4 +- .../rustc_mir_dataflow/src/impls/liveness.rs | 4 +- .../rustc_mir_dataflow/src/value_analysis.rs | 2 +- .../rustc_mir_transform/src/check_unsafety.rs | 6 +- .../rustc_mir_transform/src/coverage/mod.rs | 6 +- .../rustc_mir_transform/src/coverage/query.rs | 4 +- .../rustc_mir_transform/src/coverage/tests.rs | 2 +- .../src/deduce_param_attrs.rs | 2 +- compiler/rustc_mir_transform/src/dest_prop.rs | 2 +- compiler/rustc_mir_transform/src/lib.rs | 32 ++++----- .../rustc_mir_transform/src/simplify_try.rs | 2 +- .../src/partitioning/mod.rs | 7 +- compiler/rustc_passes/src/dead.rs | 16 ++--- .../rustc_passes/src/debugger_visualizer.rs | 6 +- compiler/rustc_passes/src/diagnostic_items.rs | 10 +-- compiler/rustc_passes/src/layout_test.rs | 2 +- compiler/rustc_passes/src/reachable.rs | 4 +- compiler/rustc_passes/src/stability.rs | 2 +- compiler/rustc_passes/src/weak_lang_items.rs | 4 +- .../rustc_query_impl/src/on_disk_cache.rs | 2 +- .../rustc_query_impl/src/profiling_support.rs | 2 +- .../rustc_query_system/src/query/plumbing.rs | 4 +- compiler/rustc_save_analysis/src/lib.rs | 6 +- compiler/rustc_session/src/parse.rs | 12 ++-- compiler/rustc_session/src/utils.rs | 2 +- .../src/typeid/typeid_itanium_cxx_abi.rs | 2 +- .../rustc_target/src/abi/call/loongarch.rs | 4 +- compiler/rustc_target/src/abi/call/riscv.rs | 4 +- compiler/rustc_target/src/spec/mod.rs | 2 +- .../src/traits/coherence.rs | 8 +-- .../src/traits/error_reporting/suggestions.rs | 2 +- .../rustc_trait_selection/src/traits/mod.rs | 5 +- .../src/traits/outlives_bounds.rs | 2 +- .../src/traits/vtable.rs | 2 +- compiler/rustc_traits/src/chalk/db.rs | 2 +- .../src/normalize_erasing_regions.rs | 2 +- compiler/rustc_ty_utils/src/consts.rs | 14 ++-- compiler/rustc_ty_utils/src/implied_bounds.rs | 2 +- compiler/rustc_ty_utils/src/layout.rs | 4 +- compiler/rustc_ty_utils/src/ty.rs | 5 +- 109 files changed, 266 insertions(+), 320 deletions(-) diff --git a/compiler/rustc_ast/src/util/comments.rs b/compiler/rustc_ast/src/util/comments.rs index 35454c3a6709..275ed02c2b9f 100644 --- a/compiler/rustc_ast/src/util/comments.rs +++ b/compiler/rustc_ast/src/util/comments.rs @@ -51,7 +51,7 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol { if i != 0 || j != lines.len() { Some((i, j)) } else { None } } - fn get_horizontal_trim<'a>(lines: &'a [&str], kind: CommentKind) -> Option { + fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option { let mut i = usize::MAX; let mut first = true; diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 0ef784a4453d..db2527a200c3 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -414,7 +414,7 @@ fn compute_hir_hash( }) } -pub fn lower_to_hir<'hir>(tcx: TyCtxt<'hir>, (): ()) -> hir::Crate<'hir> { +pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> { let sess = tcx.sess; let krate = tcx.untracked_crate.steal(); let mut resolver = tcx.resolver_for_lowering(()).steal(); diff --git a/compiler/rustc_borrowck/src/consumers.rs b/compiler/rustc_borrowck/src/consumers.rs index 86da767f3227..becc04bbdab0 100644 --- a/compiler/rustc_borrowck/src/consumers.rs +++ b/compiler/rustc_borrowck/src/consumers.rs @@ -28,10 +28,10 @@ pub use super::{ /// that shows how to do this at `src/test/run-make/obtain-borrowck/`. /// /// * Polonius is highly unstable, so expect regular changes in its signature or other details. -pub fn get_body_with_borrowck_facts<'tcx>( - tcx: TyCtxt<'tcx>, +pub fn get_body_with_borrowck_facts( + tcx: TyCtxt<'_>, def: ty::WithOptConstParam, -) -> BodyWithBorrowckFacts<'tcx> { +) -> BodyWithBorrowckFacts<'_> { let (input_body, promoted) = tcx.mir_promoted(def); let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(def.did)).build(); let input_body: &Body<'_> = &input_body.borrow(); diff --git a/compiler/rustc_borrowck/src/diagnostics/find_all_local_uses.rs b/compiler/rustc_borrowck/src/diagnostics/find_all_local_uses.rs index 498e9834354b..2c4d953f011f 100644 --- a/compiler/rustc_borrowck/src/diagnostics/find_all_local_uses.rs +++ b/compiler/rustc_borrowck/src/diagnostics/find_all_local_uses.rs @@ -9,7 +9,7 @@ use rustc_middle::mir::{Body, Local, Location}; /// Find all uses of (including assignments to) a [`Local`]. /// /// Uses `BTreeSet` so output is deterministic. -pub(super) fn find<'tcx>(body: &Body<'tcx>, local: Local) -> BTreeSet { +pub(super) fn find(body: &Body<'_>, local: Local) -> BTreeSet { let mut visitor = AllLocalUsesVisitor { for_local: local, uses: BTreeSet::default() }; visitor.visit_body(body); visitor.uses diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index d8c22fbe59f0..168b798788b4 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -124,10 +124,7 @@ pub fn provide(providers: &mut Providers) { }; } -fn mir_borrowck<'tcx>( - tcx: TyCtxt<'tcx>, - def: ty::WithOptConstParam, -) -> &'tcx BorrowCheckResult<'tcx> { +fn mir_borrowck(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) -> &BorrowCheckResult<'_> { let (input_body, promoted) = tcx.mir_promoted(def); debug!("run query mir_borrowck: {}", tcx.def_path_str(def.did.to_def_id())); diff --git a/compiler/rustc_borrowck/src/type_check/free_region_relations.rs b/compiler/rustc_borrowck/src/type_check/free_region_relations.rs index 14cfc3613bf0..09cf870bcf35 100644 --- a/compiler/rustc_borrowck/src/type_check/free_region_relations.rs +++ b/compiler/rustc_borrowck/src/type_check/free_region_relations.rs @@ -85,7 +85,7 @@ impl UniversalRegionRelations<'_> { /// outlives `fr` and (b) is not local. /// /// (*) If there are multiple competing choices, we return all of them. - pub(crate) fn non_local_upper_bounds<'a>(&'a self, fr: RegionVid) -> Vec { + pub(crate) fn non_local_upper_bounds(&self, fr: RegionVid) -> Vec { debug!("non_local_upper_bound(fr={:?})", fr); let res = self.non_local_bounds(&self.inverse_outlives, fr); assert!(!res.is_empty(), "can't find an upper bound!?"); @@ -148,9 +148,9 @@ impl UniversalRegionRelations<'_> { /// Helper for `non_local_upper_bounds` and `non_local_lower_bounds`. /// Repeatedly invokes `postdom_parent` until we find something that is not /// local. Returns `None` if we never do so. - fn non_local_bounds<'a>( + fn non_local_bounds( &self, - relation: &'a TransitiveRelation, + relation: &TransitiveRelation, fr0: RegionVid, ) -> Vec { // This method assumes that `fr0` is one of the universally diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index 900c4427424a..925392b500ad 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -352,7 +352,7 @@ pub fn parse_asm_args<'a>( /// /// This function must be called immediately after the option token is parsed. /// Otherwise, the suggestion will be incorrect. -fn err_duplicate_option<'a>(p: &mut Parser<'a>, symbol: Symbol, span: Span) { +fn err_duplicate_option(p: &mut Parser<'_>, symbol: Symbol, span: Span) { let mut err = p .sess .span_diagnostic diff --git a/compiler/rustc_codegen_gcc/src/base.rs b/compiler/rustc_codegen_gcc/src/base.rs index 8f9f6f98faf8..d464bd3d12a0 100644 --- a/compiler/rustc_codegen_gcc/src/base.rs +++ b/compiler/rustc_codegen_gcc/src/base.rs @@ -52,7 +52,7 @@ pub fn linkage_to_gcc(linkage: Linkage) -> FunctionType { } } -pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_128bit_integers: bool) -> (ModuleCodegen, u64) { +pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol, supports_128bit_integers: bool) -> (ModuleCodegen, u64) { let prof_timer = tcx.prof.generic_activity("codegen_module"); let start_time = Instant::now(); diff --git a/compiler/rustc_codegen_gcc/src/common.rs b/compiler/rustc_codegen_gcc/src/common.rs index aa1c271c31cb..d7df1347121a 100644 --- a/compiler/rustc_codegen_gcc/src/common.rs +++ b/compiler/rustc_codegen_gcc/src/common.rs @@ -44,7 +44,7 @@ pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> context.new_array_constructor(None, typ, &elements) } -pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool { +pub fn type_is_pointer(typ: Type) -> bool { typ.get_pointee().is_some() } diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index b9600da5c39d..bf1da38312f7 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -161,7 +161,7 @@ impl ExtraBackendMethods for GccCodegenBackend { mods } - fn compile_codegen_unit<'tcx>(&self, tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (ModuleCodegen, u64) { + fn compile_codegen_unit(&self, tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen, u64) { base::compile_codegen_unit(tcx, cgu_name, *self.supports_128bit_integers.lock().expect("lock")) } diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index 86580d05d416..393bf30e9f83 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -30,7 +30,7 @@ use std::ffi::CString; /// implementing this Rust version, and though the format documentation is very explicit and /// detailed, some undocumented details in Clang's implementation (that may or may not be important) /// were also replicated for Rust's Coverage Map. -pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) { +pub fn finalize(cx: &CodegenCx<'_, '_>) { let tcx = cx.tcx; // Ensure the installed version of LLVM supports at least Coverage Map @@ -284,7 +284,7 @@ fn save_function_record( /// "code coverage dead code cgu" during the partitioning process. This prevents us from generating /// code regions for the same function more than once which can lead to linker errors regarding /// duplicate symbols. -fn add_unused_functions<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) { +fn add_unused_functions(cx: &CodegenCx<'_, '_>) { assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu()); let tcx = cx.tcx; diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index a9e3dcf4cb39..48e3a812e4f2 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -111,7 +111,7 @@ macro_rules! return_if_di_node_created_in_meantime { /// Extract size and alignment from a TyAndLayout. #[inline] -fn size_and_align_of<'tcx>(ty_and_layout: TyAndLayout<'tcx>) -> (Size, Align) { +fn size_and_align_of(ty_and_layout: TyAndLayout<'_>) -> (Size, Align) { (ty_and_layout.size, ty_and_layout.align.abi) } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index e61dbe8b8fc5..0db45b6157ab 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -2389,11 +2389,11 @@ extern "C" { pub fn LLVMRustSetDataLayoutFromTargetMachine<'a>(M: &'a Module, TM: &'a TargetMachine); - pub fn LLVMRustBuildOperandBundleDef<'a>( + pub fn LLVMRustBuildOperandBundleDef( Name: *const c_char, - Inputs: *const &'a Value, + Inputs: *const &'_ Value, NumInputs: c_uint, - ) -> &'a mut OperandBundleDef<'a>; + ) -> &mut OperandBundleDef<'_>; pub fn LLVMRustFreeOperandBundleDef<'a>(Bundle: &'a mut OperandBundleDef<'a>); pub fn LLVMRustPositionBuilderAtStart<'a>(B: &Builder<'a>, BB: &'a BasicBlock); diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index a1c77ec0cfca..edde1537b81e 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -607,21 +607,21 @@ fn link_dwarf_object<'a>( } impl ThorinSession { - fn alloc_mmap<'arena>(&'arena self, data: Mmap) -> &'arena Mmap { + fn alloc_mmap(&self, data: Mmap) -> &Mmap { (*self.arena_mmap.alloc(data)).borrow() } } impl thorin::Session for ThorinSession { - fn alloc_data<'arena>(&'arena self, data: Vec) -> &'arena [u8] { + fn alloc_data(&self, data: Vec) -> &[u8] { (*self.arena_data.alloc(data)).borrow() } - fn alloc_relocation<'arena>(&'arena self, data: Relocations) -> &'arena Relocations { + fn alloc_relocation(&self, data: Relocations) -> &Relocations { (*self.arena_relocations.alloc(data)).borrow() } - fn read_input<'arena>(&'arena self, path: &Path) -> std::io::Result<&'arena [u8]> { + fn read_input(&self, path: &Path) -> std::io::Result<&[u8]> { let file = File::open(&path)?; let mmap = (unsafe { Mmap::map(file) })?; Ok(self.alloc_mmap(mmap)) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 22f534d909ab..8cb7d74b90d4 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -163,10 +163,10 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b tcx.reachable_non_generics(def_id.krate).contains_key(&def_id) } -fn exported_symbols_provider_local<'tcx>( - tcx: TyCtxt<'tcx>, +fn exported_symbols_provider_local( + tcx: TyCtxt<'_>, cnum: CrateNum, -) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportInfo)] { +) -> &[(ExportedSymbol<'_>, SymbolExportInfo)] { assert_eq!(cnum, LOCAL_CRATE); if !tcx.sess.opts.output_types.should_codegen() { diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs b/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs index 6e3f4f0b8ef0..60e9b40e8fb4 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/mod.rs @@ -10,7 +10,7 @@ pub mod type_names; /// NOTE: This is somewhat inconsistent right now: For empty enums and enums with a single /// fieldless variant, we generate DW_TAG_struct_type, although a /// DW_TAG_enumeration_type would be a better fit. -pub fn wants_c_like_enum_debuginfo<'tcx>(enum_type_and_layout: TyAndLayout<'tcx>) -> bool { +pub fn wants_c_like_enum_debuginfo(enum_type_and_layout: TyAndLayout<'_>) -> bool { match enum_type_and_layout.ty.kind() { ty::Adt(adt_def, _) => { if !adt_def.is_enum() { diff --git a/compiler/rustc_codegen_ssa/src/meth.rs b/compiler/rustc_codegen_ssa/src/meth.rs index d96ca921f1f4..2421acab4715 100644 --- a/compiler/rustc_codegen_ssa/src/meth.rs +++ b/compiler/rustc_codegen_ssa/src/meth.rs @@ -65,7 +65,7 @@ impl<'a, 'tcx> VirtualIndex { /// This takes a valid `self` receiver type and extracts the principal trait /// ref of the type. -fn expect_dyn_trait_in_self<'tcx>(ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> { +fn expect_dyn_trait_in_self(ty: Ty<'_>) -> ty::PolyExistentialTraitRef<'_> { for arg in ty.peel_refs().walk() { if let GenericArgKind::Type(ty) = arg.unpack() { if let ty::Dynamic(data, _, _) = ty.kind() { diff --git a/compiler/rustc_codegen_ssa/src/target_features.rs b/compiler/rustc_codegen_ssa/src/target_features.rs index 0dabe96b6027..da69fc8ecf77 100644 --- a/compiler/rustc_codegen_ssa/src/target_features.rs +++ b/compiler/rustc_codegen_ssa/src/target_features.rs @@ -419,7 +419,7 @@ pub fn from_target_feature( /// Computes the set of target features used in a function for the purposes of /// inline assembly. -fn asm_target_features<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx FxHashSet { +fn asm_target_features(tcx: TyCtxt<'_>, did: DefId) -> &FxHashSet { let mut target_features = tcx.sess.unstable_target_features.clone(); if tcx.def_kind(did).has_codegen_attrs() { let attrs = tcx.codegen_fn_attrs(did); diff --git a/compiler/rustc_data_structures/src/profiling.rs b/compiler/rustc_data_structures/src/profiling.rs index 1d4014f05acd..16296b224897 100644 --- a/compiler/rustc_data_structures/src/profiling.rs +++ b/compiler/rustc_data_structures/src/profiling.rs @@ -205,10 +205,7 @@ impl SelfProfilerRef { /// VerboseTimingGuard returned from this call is dropped. In addition to recording /// a measureme event, "verbose" generic activities also print a timing entry to /// stderr if the compiler is invoked with -Ztime-passes. - pub fn verbose_generic_activity<'a>( - &'a self, - event_label: &'static str, - ) -> VerboseTimingGuard<'a> { + pub fn verbose_generic_activity(&self, event_label: &'static str) -> VerboseTimingGuard<'_> { let message = if self.print_verbose_generic_activities { Some(event_label.to_owned()) } else { None }; @@ -216,11 +213,11 @@ impl SelfProfilerRef { } /// Like `verbose_generic_activity`, but with an extra arg. - pub fn verbose_generic_activity_with_arg<'a, A>( - &'a self, + pub fn verbose_generic_activity_with_arg( + &self, event_label: &'static str, event_arg: A, - ) -> VerboseTimingGuard<'a> + ) -> VerboseTimingGuard<'_> where A: Borrow + Into, { diff --git a/compiler/rustc_data_structures/src/transitive_relation.rs b/compiler/rustc_data_structures/src/transitive_relation.rs index cf616203842a..1ff0d58df140 100644 --- a/compiler/rustc_data_structures/src/transitive_relation.rs +++ b/compiler/rustc_data_structures/src/transitive_relation.rs @@ -199,7 +199,7 @@ impl TransitiveRelation { /// Viewing the relation as a graph, computes the "mutual /// immediate postdominator" of a set of points (if one /// exists). See `postdom_upper_bound` for details. - pub fn mutual_immediate_postdominator<'a>(&'a self, mut mubs: Vec) -> Option { + pub fn mutual_immediate_postdominator(&self, mut mubs: Vec) -> Option { loop { match mubs.len() { 0 => return None, diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs index c015f1232cd9..14257e4d5c60 100644 --- a/compiler/rustc_data_structures/src/unord.rs +++ b/compiler/rustc_data_structures/src/unord.rs @@ -178,7 +178,7 @@ impl UnordSet { } #[inline] - pub fn items<'a>(&'a self) -> UnordItems<&'a V, impl Iterator> { + pub fn items(&self) -> UnordItems<&V, impl Iterator> { UnordItems(self.inner.iter()) } @@ -255,7 +255,7 @@ impl UnordMap { } #[inline] - pub fn items<'a>(&'a self) -> UnordItems<(&'a K, &'a V), impl Iterator> { + pub fn items(&self) -> UnordItems<(&K, &V), impl Iterator> { UnordItems(self.inner.iter()) } @@ -311,7 +311,7 @@ impl UnordBag { } #[inline] - pub fn items<'a>(&'a self) -> UnordItems<&'a V, impl Iterator> { + pub fn items(&self) -> UnordItems<&V, impl Iterator> { UnordItems(self.inner.iter()) } diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 25d0e736e599..f2f36a3541f0 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -549,9 +549,7 @@ fn icu_locale_from_unic_langid(lang: LanguageIdentifier) -> Option( - l: Vec>, -) -> FluentValue<'source> { +pub fn fluent_value_from_str_list_sep_by_and(l: Vec>) -> FluentValue<'_> { // Fluent requires 'static value here for its AnyEq usages. #[derive(Clone, PartialEq, Debug)] struct FluentStrListSepByAnd(Vec); diff --git a/compiler/rustc_expand/src/mbe/quoted.rs b/compiler/rustc_expand/src/mbe/quoted.rs index ee17d54f629d..878284f5928d 100644 --- a/compiler/rustc_expand/src/mbe/quoted.rs +++ b/compiler/rustc_expand/src/mbe/quoted.rs @@ -356,7 +356,7 @@ fn parse_sep_and_kleene_op( // `$$` or a meta-variable is the lhs of a macro but shouldn't. // // For example, `macro_rules! foo { ( ${length()} ) => {} }` -fn span_dollar_dollar_or_metavar_in_the_lhs_err<'sess>(sess: &'sess ParseSess, token: &Token) { +fn span_dollar_dollar_or_metavar_in_the_lhs_err(sess: &ParseSess, token: &Token) { sess.span_diagnostic .span_err(token.span, &format!("unexpected token: {}", pprust::token_to_string(token))); sess.span_diagnostic.span_note_without_error( diff --git a/compiler/rustc_graphviz/src/lib.rs b/compiler/rustc_graphviz/src/lib.rs index 1f8268cc17c5..d7730935f201 100644 --- a/compiler/rustc_graphviz/src/lib.rs +++ b/compiler/rustc_graphviz/src/lib.rs @@ -164,7 +164,7 @@ //! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> { //! dot::Id::new(format!("N{}", n)).unwrap() //! } -//! fn node_label<'b>(&'b self, n: &Nd) -> dot::LabelText<'b> { +//! fn node_label(&self, n: &Nd) -> dot::LabelText { //! dot::LabelText::LabelStr(self.nodes[*n].into()) //! } //! fn edge_label<'b>(&'b self, _: &Ed) -> dot::LabelText<'b> { diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index eee0ba2e5ed2..a714663741be 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -162,7 +162,7 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b } /// Check that a `static` is inhabited. -fn check_static_inhabited<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { +fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) { // Make sure statics are inhabited. // Other parts of the compiler assume that there are no uninhabited places. In principle it // would be enough to check this for `extern` statics, as statics with an initializer will @@ -212,7 +212,7 @@ fn check_static_inhabited<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { /// Checks that an opaque type does not contain cycles and does not use `Self` or `T::Foo` /// projections that would result in "inheriting lifetimes". -fn check_opaque<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) { +fn check_opaque(tcx: TyCtxt<'_>, id: hir::ItemId) { let item = tcx.hir().item(id); let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else { tcx.sess.delay_span_bug(tcx.hir().span(id.hir_id()), "expected opaque item"); @@ -245,8 +245,8 @@ fn check_opaque<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) { /// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result /// in "inheriting lifetimes". #[instrument(level = "debug", skip(tcx, span))] -pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>( - tcx: TyCtxt<'tcx>, +pub(super) fn check_opaque_for_inheriting_lifetimes( + tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span, ) { @@ -496,7 +496,7 @@ fn is_enum_of_nonnullable_ptr<'tcx>( matches!(field.ty(tcx, substs).kind(), ty::FnPtr(..) | ty::Ref(..)) } -fn check_static_linkage<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { +fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) { if tcx.codegen_fn_attrs(def_id).import_linkage.is_some() { if match tcx.type_of(def_id).kind() { ty::RawPtr(_) => false, @@ -508,7 +508,7 @@ fn check_static_linkage<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { } } -fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) { +fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) { debug!( "check_item_type(it.def_id={:?}, it.name={})", id.owner_id, @@ -1160,7 +1160,7 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>) } #[allow(trivial_numeric_casts)] -fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { +fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) { let def = tcx.adt_def(def_id); def.destructor(tcx); // force the destructor to be evaluated diff --git a/compiler/rustc_hir_analysis/src/check/compare_method.rs b/compiler/rustc_hir_analysis/src/check/compare_method.rs index cddd307c13db..c6bda9b46410 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_method.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_method.rs @@ -1517,8 +1517,8 @@ fn compare_generic_param_kinds<'tcx>( } /// Use `tcx.compare_assoc_const_impl_item_with_trait_item` instead -pub(crate) fn raw_compare_const_impl<'tcx>( - tcx: TyCtxt<'tcx>, +pub(crate) fn raw_compare_const_impl( + tcx: TyCtxt<'_>, (impl_const_item_def, trait_const_item_def): (LocalDefId, DefId), ) -> Result<(), ErrorGuaranteed> { let impl_const_item = tcx.associated_item(impl_const_item_def); diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 57f0cae12bb3..ed2aed293a77 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -115,10 +115,10 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option { /// Given a `DefId` for an opaque type in return position, find its parent item's return /// expressions. -fn get_owner_return_paths<'tcx>( - tcx: TyCtxt<'tcx>, +fn get_owner_return_paths( + tcx: TyCtxt<'_>, def_id: LocalDefId, -) -> Option<(LocalDefId, ReturnsVisitor<'tcx>)> { +) -> Option<(LocalDefId, ReturnsVisitor<'_>)> { let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let parent_id = tcx.hir().get_parent_item(hir_id).def_id; tcx.hir().find_by_def_id(parent_id).and_then(|node| node.body_id()).map(|body_id| { diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 94d333c336ef..aedc736b0239 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1673,7 +1673,7 @@ fn check_method_receiver<'tcx>( } } -fn e0307<'tcx>(tcx: TyCtxt<'tcx>, span: Span, receiver_ty: Ty<'_>) { +fn e0307(tcx: TyCtxt<'_>, span: Span, receiver_ty: Ty<'_>) { struct_span_err!( tcx.sess.diagnostic(), span, diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs index 193ecdb16788..2790d91572be 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs @@ -171,7 +171,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { } } -fn visit_implementation_of_coerce_unsized<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) { +fn visit_implementation_of_coerce_unsized(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_coerce_unsized: impl_did={:?}", impl_did); // Just compute this for the side-effects, in particular reporting @@ -181,7 +181,7 @@ fn visit_implementation_of_coerce_unsized<'tcx>(tcx: TyCtxt<'tcx>, impl_did: Loc tcx.at(span).coerce_unsized_info(impl_did); } -fn visit_implementation_of_dispatch_from_dyn<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) { +fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did); let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did); diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 0c4649cea14e..b7d599f57fd4 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -839,7 +839,7 @@ fn convert_variant( ) } -fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> { +fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtDef<'_> { use rustc_hir::*; let def_id = def_id.expect_local(); diff --git a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs index b4ad3467e7d8..b63a8ef698d1 100644 --- a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs +++ b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs @@ -276,7 +276,7 @@ fn resolve_lifetimes(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveLife rl } -fn late_region_as_bound_region<'tcx>(tcx: TyCtxt<'tcx>, region: &Region) -> ty::BoundVariableKind { +fn late_region_as_bound_region(tcx: TyCtxt<'_>, region: &Region) -> ty::BoundVariableKind { match region { Region::LateBound(_, _, def_id) => { let name = tcx.hir().name(tcx.hir().local_def_id_to_hir_id(def_id.expect_local())); @@ -1018,7 +1018,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { } } -fn object_lifetime_default<'tcx>(tcx: TyCtxt<'tcx>, param_def_id: DefId) -> ObjectLifetimeDefault { +fn object_lifetime_default(tcx: TyCtxt<'_>, param_def_id: DefId) -> ObjectLifetimeDefault { debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam); let param_def_id = param_def_id.expect_local(); let parent_def_id = tcx.local_parent(param_def_id); diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 79d75231e5d7..0943350e2d47 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -318,10 +318,10 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP } } -fn const_evaluatable_predicates_of<'tcx>( - tcx: TyCtxt<'tcx>, +fn const_evaluatable_predicates_of( + tcx: TyCtxt<'_>, def_id: LocalDefId, -) -> FxIndexSet<(ty::Predicate<'tcx>, Span)> { +) -> FxIndexSet<(ty::Predicate<'_>, Span)> { struct ConstCollector<'tcx> { tcx: TyCtxt<'tcx>, preds: FxIndexSet<(ty::Predicate<'tcx>, Span)>, diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs index fd8e8ed7ba60..b60fc276178b 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs @@ -157,11 +157,11 @@ fn check_constness(tcx: TyCtxt<'_>, impl1_def_id: LocalDefId, impl2_node: Node, /// ``` /// /// Would return `S1 = [C]` and `S2 = [Vec, C]`. -fn get_impl_substs<'tcx>( - tcx: TyCtxt<'tcx>, +fn get_impl_substs( + tcx: TyCtxt<'_>, impl1_def_id: LocalDefId, impl2_node: Node, -) -> Option<(SubstsRef<'tcx>, SubstsRef<'tcx>)> { +) -> Option<(SubstsRef<'_>, SubstsRef<'_>)> { let infcx = &tcx.infer_ctxt().build(); let ocx = ObligationCtxt::new(infcx); let param_env = tcx.param_env(impl1_def_id); diff --git a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs index af8d7e851586..a46f2a94cd28 100644 --- a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs +++ b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs @@ -13,9 +13,9 @@ use super::utils::*; /// `global_inferred_outlives`: this is initially the empty map that /// was generated by walking the items in the crate. This will /// now be filled with inferred predicates. -pub(super) fn infer_predicates<'tcx>( - tcx: TyCtxt<'tcx>, -) -> FxHashMap>> { +pub(super) fn infer_predicates( + tcx: TyCtxt<'_>, +) -> FxHashMap>> { debug!("infer_predicates"); let mut explicit_map = ExplicitPredicatesMap::new(); diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 8cdd12e4e347..b0cd4a16e986 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -118,7 +118,7 @@ fn identity(_: Ty<'_>) -> Vec> { vec![] } -fn simple<'tcx>(kind: Adjust<'tcx>) -> impl FnOnce(Ty<'tcx>) -> Vec> { +fn simple<'tcx>(kind: Adjust<'tcx>) -> impl FnOnce(Ty<'tcx>) -> Vec> { move |target| vec![Adjustment { kind, target }] } diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 03b174c77955..7774ffc9b979 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -756,8 +756,8 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { /// - When reporting the Place back to the Delegate, ensure that the UpvarId uses the enclosing /// closure as the DefId. fn walk_captures(&mut self, closure_expr: &hir::Closure<'_>) { - fn upvar_is_local_variable<'tcx>( - upvars: Option<&'tcx FxIndexMap>, + fn upvar_is_local_variable( + upvars: Option<&FxIndexMap>, upvar_id: hir::HirId, body_owner_is_closure: bool, ) -> bool { diff --git a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs index 2f55ea939fc0..f7b493bc2242 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs @@ -79,7 +79,7 @@ pub fn compute_drop_ranges<'a, 'tcx>( /// result of `foo`. On the other hand, if `place` points to `x` then `f` will /// be called both on the `ExprKind::Path` node that represents the expression /// as well as the HirId of the local `x` itself. -fn for_each_consumable<'tcx>(hir: Map<'tcx>, place: TrackedValue, mut f: impl FnMut(TrackedValue)) { +fn for_each_consumable(hir: Map<'_>, place: TrackedValue, mut f: impl FnMut(TrackedValue)) { f(place); let node = hir.find(place.hir_id()); if let Some(Node::Expr(expr)) = node { diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 5b2352cda34b..b06927f9662b 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -462,8 +462,8 @@ fn fatally_break_rust(sess: &Session) { )); } -fn has_expected_num_generic_args<'tcx>( - tcx: TyCtxt<'tcx>, +fn has_expected_num_generic_args( + tcx: TyCtxt<'_>, trait_did: Option, expected: usize, ) -> bool { diff --git a/compiler/rustc_hir_typeck/src/op.rs b/compiler/rustc_hir_typeck/src/op.rs index b12d84af4adb..9f0d175c4c66 100644 --- a/compiler/rustc_hir_typeck/src/op.rs +++ b/compiler/rustc_hir_typeck/src/op.rs @@ -904,7 +904,7 @@ enum Op { } /// Dereferences a single level of immutable referencing. -fn deref_ty_if_possible<'tcx>(ty: Ty<'tcx>) -> Ty<'tcx> { +fn deref_ty_if_possible(ty: Ty<'_>) -> Ty<'_> { match ty.kind() { ty::Ref(_, ty, hir::Mutability::Not) => *ty, _ => ty, diff --git a/compiler/rustc_hir_typeck/src/upvar.rs b/compiler/rustc_hir_typeck/src/upvar.rs index 0f46972019e5..a9347991e7f9 100644 --- a/compiler/rustc_hir_typeck/src/upvar.rs +++ b/compiler/rustc_hir_typeck/src/upvar.rs @@ -1675,7 +1675,7 @@ fn apply_capture_kind_on_capture_ty<'tcx>( } /// Returns the Span of where the value with the provided HirId would be dropped -fn drop_location_span<'tcx>(tcx: TyCtxt<'tcx>, hir_id: hir::HirId) -> Span { +fn drop_location_span(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> Span { let owner_id = tcx.hir().get_enclosing_scope(hir_id).unwrap(); let owner_node = tcx.hir().get(owner_id); @@ -1843,10 +1843,10 @@ fn restrict_precision_for_drop_types<'a, 'tcx>( /// - No projections are applied to raw pointers, since these require unsafe blocks. We capture /// them completely. /// - No projections are applied on top of Union ADTs, since these require unsafe blocks. -fn restrict_precision_for_unsafe<'tcx>( - mut place: Place<'tcx>, +fn restrict_precision_for_unsafe( + mut place: Place<'_>, mut curr_mode: ty::UpvarCapture, -) -> (Place<'tcx>, ty::UpvarCapture) { +) -> (Place<'_>, ty::UpvarCapture) { if place.base_ty.is_unsafe_ptr() { truncate_place_to_len_and_update_capture_kind(&mut place, &mut curr_mode, 0); } @@ -1876,10 +1876,10 @@ fn restrict_precision_for_unsafe<'tcx>( /// - No Index projections are captured, since arrays are captured completely. /// - No unsafe block is required to capture `place` /// Returns the truncated place and updated capture mode. -fn restrict_capture_precision<'tcx>( - place: Place<'tcx>, +fn restrict_capture_precision( + place: Place<'_>, curr_mode: ty::UpvarCapture, -) -> (Place<'tcx>, ty::UpvarCapture) { +) -> (Place<'_>, ty::UpvarCapture) { let (mut place, mut curr_mode) = restrict_precision_for_unsafe(place, curr_mode); if place.projections.is_empty() { @@ -1904,10 +1904,10 @@ fn restrict_capture_precision<'tcx>( } /// Truncate deref of any reference. -fn adjust_for_move_closure<'tcx>( - mut place: Place<'tcx>, +fn adjust_for_move_closure( + mut place: Place<'_>, mut kind: ty::UpvarCapture, -) -> (Place<'tcx>, ty::UpvarCapture) { +) -> (Place<'_>, ty::UpvarCapture) { let first_deref = place.projections.iter().position(|proj| proj.kind == ProjectionKind::Deref); if let Some(idx) = first_deref { @@ -1919,10 +1919,10 @@ fn adjust_for_move_closure<'tcx>( /// Adjust closure capture just that if taking ownership of data, only move data /// from enclosing stack frame. -fn adjust_for_non_move_closure<'tcx>( - mut place: Place<'tcx>, +fn adjust_for_non_move_closure( + mut place: Place<'_>, mut kind: ty::UpvarCapture, -) -> (Place<'tcx>, ty::UpvarCapture) { +) -> (Place<'_>, ty::UpvarCapture) { let contains_deref = place.projections.iter().position(|proj| proj.kind == ProjectionKind::Deref); @@ -2225,10 +2225,10 @@ fn determine_place_ancestry_relation<'tcx>( /// // it is constrained to `'a` /// } /// ``` -fn truncate_capture_for_optimization<'tcx>( - mut place: Place<'tcx>, +fn truncate_capture_for_optimization( + mut place: Place<'_>, mut curr_mode: ty::UpvarCapture, -) -> (Place<'tcx>, ty::UpvarCapture) { +) -> (Place<'_>, ty::UpvarCapture) { let is_shared_ref = |ty: Ty<'_>| matches!(ty.kind(), ty::Ref(.., hir::Mutability::Not)); // Find the right-most deref (if any). All the projections that come after this diff --git a/compiler/rustc_incremental/src/assert_dep_graph.rs b/compiler/rustc_incremental/src/assert_dep_graph.rs index b4bf9f4bcc76..67b4d6d6959f 100644 --- a/compiler/rustc_incremental/src/assert_dep_graph.rs +++ b/compiler/rustc_incremental/src/assert_dep_graph.rs @@ -432,10 +432,7 @@ fn walk_between<'q>( } } -fn filter_edges<'q>( - query: &'q DepGraphQuery, - nodes: &FxHashSet, -) -> Vec<(DepKind, DepKind)> { +fn filter_edges(query: &DepGraphQuery, nodes: &FxHashSet) -> Vec<(DepKind, DepKind)> { let uniq: FxHashSet<_> = query .edges() .into_iter() diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 269fc95420ad..96a976fb89e0 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2199,10 +2199,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { ); } - fn binding_suggestion<'tcx, S: fmt::Display>( + fn binding_suggestion( err: &mut Diagnostic, type_param_span: Option<(Span, bool)>, - bound_kind: GenericKind<'tcx>, + bound_kind: GenericKind<'_>, sub: S, add_lt_sugg: Option<(Span, String)>, ) { diff --git a/compiler/rustc_lint/src/non_fmt_panic.rs b/compiler/rustc_lint/src/non_fmt_panic.rs index c1820ac4d1ea..8dccfe0046c4 100644 --- a/compiler/rustc_lint/src/non_fmt_panic.rs +++ b/compiler/rustc_lint/src/non_fmt_panic.rs @@ -304,7 +304,7 @@ fn check_panic_str<'tcx>( /// Given the span of `some_macro!(args);`, gives the span of `(` and `)`, /// and the type of (opening) delimiter used. -fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Span, char)> { +fn find_delimiters(cx: &LateContext<'_>, span: Span) -> Option<(Span, Span, char)> { let snippet = cx.sess().parse_sess.source_map().span_to_snippet(span).ok()?; let (open, open_ch) = snippet.char_indices().find(|&(_, c)| "([{".contains(c))?; let close = snippet.rfind(|c| ")]}".contains(c))?; diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index f7a4103f4d5c..67868ded0b86 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -1405,7 +1405,7 @@ declare_lint! { /// struct S; /// /// impl S { - /// fn late<'a, 'b>(self, _: &'a u8, _: &'b u8) {} + /// fn late(self, _: &u8, _: &u8) {} /// } /// /// fn main() { diff --git a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs index 446aebe4f83f..767db3673225 100644 --- a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs @@ -29,7 +29,7 @@ impl SubdiagnosticDeriveBuilder { Self { diag, f } } - pub(crate) fn into_tokens<'a>(self, mut structure: Structure<'a>) -> TokenStream { + pub(crate) fn into_tokens(self, mut structure: Structure<'_>) -> TokenStream { let implementation = { let ast = structure.ast(); let span = ast.span().unwrap(); diff --git a/compiler/rustc_macros/src/diagnostics/utils.rs b/compiler/rustc_macros/src/diagnostics/utils.rs index da90233523ca..4ff9c777ad85 100644 --- a/compiler/rustc_macros/src/diagnostics/utils.rs +++ b/compiler/rustc_macros/src/diagnostics/utils.rs @@ -385,7 +385,7 @@ impl quote::ToTokens for Applicability { /// Build the mapping of field names to fields. This allows attributes to peek values from /// other fields. -pub(super) fn build_field_mapping<'v>(variant: &VariantInfo<'v>) -> HashMap { +pub(super) fn build_field_mapping(variant: &VariantInfo<'_>) -> HashMap { let mut fields_map = FieldMap::new(); for binding in variant.bindings() { if let Some(ident) = &binding.ast().ident { diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 9ce3ff98ba98..b34dc0df1e28 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -520,8 +520,8 @@ impl<'a> CrateLoader<'a> { })) } - fn resolve_crate<'b>( - &'b mut self, + fn resolve_crate( + &mut self, name: Symbol, span: Span, dep_kind: CrateDepKind, diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 4af423f2a22e..d5d31bc3edc9 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1093,7 +1093,7 @@ fn should_encode_const(def_kind: DefKind) -> bool { } } -fn should_encode_trait_impl_trait_tys<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { +fn should_encode_trait_impl_trait_tys(tcx: TyCtxt<'_>, def_id: DefId) -> bool { if tcx.def_kind(def_id) != DefKind::AssocFn { return false; } diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 6b5568269185..865bb70afb50 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -175,7 +175,7 @@ impl DepNodeExt for DepNode { /// DepNode. Condition (2) might not be fulfilled if a DepNode /// refers to something from the previous compilation session that /// has been removed. - fn extract_def_id<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option { + fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option { if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash { Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()), &mut || { panic!("Failed to extract DefId: {:?} {}", self.kind, self.hash) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 0450abed51b0..cfdeb76ca269 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -18,7 +18,7 @@ use rustc_span::Span; use rustc_target::spec::abi::Abi; #[inline] -pub fn associated_body<'hir>(node: Node<'hir>) -> Option { +pub fn associated_body(node: Node<'_>) -> Option { match node { Node::Item(Item { kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body), @@ -41,7 +41,7 @@ pub fn associated_body<'hir>(node: Node<'hir>) -> Option { } } -fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool { +fn is_body_owner(node: Node<'_>, hir_id: HirId) -> bool { match associated_body(node) { Some(b) => b.hir_id == hir_id, None => false, diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index 61bc089e431b..0836f236e248 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -223,8 +223,8 @@ pub fn deprecation_message_and_lint( ) } -pub fn early_report_deprecation<'a>( - lint_buffer: &'a mut LintBuffer, +pub fn early_report_deprecation( + lint_buffer: &mut LintBuffer, message: &str, suggestion: Option, lint: &'static Lint, diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index ffdf61d42442..50e41e3e0063 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -1767,9 +1767,9 @@ impl SourceScope { /// Finds the original HirId this MIR item came from. /// This is necessary after MIR optimizations, as otherwise we get a HirId /// from the function that was inlined instead of the function call site. - pub fn lint_root<'tcx>( + pub fn lint_root( self, - source_scopes: &IndexVec>, + source_scopes: &IndexVec>, ) -> Option { let mut data = &source_scopes[self]; // FIXME(oli-obk): we should be able to just walk the `inlined_parent_scope`, but it diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 1ebfdbbd6ef0..40289af257ff 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -88,7 +88,7 @@ pub fn dump_mir<'tcx, F>( dump_matched_mir_node(tcx, pass_num, pass_name, disambiguator, body, extra_data); } -pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, def_id: DefId) -> bool { +pub fn dump_enabled(tcx: TyCtxt<'_>, pass_name: &str, def_id: DefId) -> bool { let Some(ref filters) = tcx.sess.opts.unstable_opts.dump_mir else { return false; }; @@ -421,7 +421,7 @@ impl<'tcx> ExtraComments<'tcx> { } } -fn use_verbose<'tcx>(ty: Ty<'tcx>, fn_def: bool) -> bool { +fn use_verbose(ty: Ty<'_>, fn_def: bool) -> bool { match *ty.kind() { ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_) => false, // Unit type diff --git a/compiler/rustc_middle/src/mir/spanview.rs b/compiler/rustc_middle/src/mir/spanview.rs index 4e06d91012c0..887ee5715754 100644 --- a/compiler/rustc_middle/src/mir/spanview.rs +++ b/compiler/rustc_middle/src/mir/spanview.rs @@ -230,7 +230,7 @@ where } /// Format a string showing the start line and column, and end line and column within a file. -pub fn source_range_no_file<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String { +pub fn source_range_no_file(tcx: TyCtxt<'_>, span: Span) -> String { let source_map = tcx.sess.source_map(); let start = source_map.lookup_char_pos(span.lo()); let end = source_map.lookup_char_pos(span.hi()); @@ -322,7 +322,7 @@ fn block_span_viewable<'tcx>( Some(SpanViewable { bb, span, id, tooltip }) } -fn compute_block_span<'tcx>(data: &BasicBlockData<'tcx>, body_span: Span) -> Span { +fn compute_block_span(data: &BasicBlockData<'_>, body_span: Span) -> Span { let mut span = data.terminator().source_info.span; for statement_span in data.statements.iter().map(|statement| statement.source_info.span) { // Only combine Spans from the root context, and within the function's body_span. @@ -522,12 +522,7 @@ where } #[inline(always)] -fn write_coverage_gap<'tcx, W>( - tcx: TyCtxt<'tcx>, - lo: BytePos, - hi: BytePos, - w: &mut W, -) -> io::Result<()> +fn write_coverage_gap(tcx: TyCtxt<'_>, lo: BytePos, hi: BytePos, w: &mut W) -> io::Result<()> where W: Write, { @@ -582,8 +577,8 @@ where Ok(()) } -fn make_html_snippet<'tcx>( - tcx: TyCtxt<'tcx>, +fn make_html_snippet( + tcx: TyCtxt<'_>, span: Span, some_viewable: Option<&SpanViewable>, ) -> Option { @@ -664,7 +659,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span { if to_pos >= span.hi() { span } else { span.with_hi(cmp::max(span.lo(), to_pos)) } } -fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span { +fn fn_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span { let fn_decl_span = tcx.def_span(def_id); if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) { if fn_decl_span.eq_ctxt(body_span) { fn_decl_span.to(body_span) } else { body_span } @@ -673,7 +668,7 @@ fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span { } } -fn hir_body<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<&'tcx rustc_hir::Body<'tcx>> { +fn hir_body(tcx: TyCtxt<'_>, def_id: DefId) -> Option<&rustc_hir::Body<'_>> { let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local"); hir::map::associated_body(hir_node).map(|fn_body_id| tcx.hir().body(fn_body_id)) } diff --git a/compiler/rustc_middle/src/mir/traversal.rs b/compiler/rustc_middle/src/mir/traversal.rs index 55b2c592795b..0b461d1ce41c 100644 --- a/compiler/rustc_middle/src/mir/traversal.rs +++ b/compiler/rustc_middle/src/mir/traversal.rs @@ -302,7 +302,7 @@ pub fn reachable<'a, 'tcx>( } /// Returns a `BitSet` containing all basic blocks reachable from the `START_BLOCK`. -pub fn reachable_as_bitset<'tcx>(body: &Body<'tcx>) -> BitSet { +pub fn reachable_as_bitset(body: &Body<'_>) -> BitSet { let mut iter = preorder(body); (&mut iter).for_each(drop); iter.visited diff --git a/compiler/rustc_middle/src/traits/chalk.rs b/compiler/rustc_middle/src/traits/chalk.rs index 6d4af8bea628..dd75b0d9ebc2 100644 --- a/compiler/rustc_middle/src/traits/chalk.rs +++ b/compiler/rustc_middle/src/traits/chalk.rs @@ -210,7 +210,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { Box::new(chalk_ir::TyData { kind: ty, flags: flags }) } - fn ty_data<'a>(self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData { + fn ty_data(self, ty: &Self::InternedType) -> &chalk_ir::TyData { ty } @@ -218,10 +218,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { Box::new(lifetime) } - fn lifetime_data<'a>( - self, - lifetime: &'a Self::InternedLifetime, - ) -> &'a chalk_ir::LifetimeData { + fn lifetime_data(self, lifetime: &Self::InternedLifetime) -> &chalk_ir::LifetimeData { &lifetime } @@ -229,7 +226,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { Box::new(constant) } - fn const_data<'a>(self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData { + fn const_data(self, constant: &Self::InternedConst) -> &chalk_ir::ConstData { &constant } @@ -246,10 +243,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { Box::new(data) } - fn generic_arg_data<'a>( - self, - data: &'a Self::InternedGenericArg, - ) -> &'a chalk_ir::GenericArgData { + fn generic_arg_data(self, data: &Self::InternedGenericArg) -> &chalk_ir::GenericArgData { &data } @@ -257,7 +251,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { Box::new(goal) } - fn goal_data<'a>(self, goal: &'a Self::InternedGoal) -> &'a chalk_ir::GoalData { + fn goal_data(self, goal: &Self::InternedGoal) -> &chalk_ir::GoalData { &goal } @@ -268,7 +262,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn goals_data<'a>(self, goals: &'a Self::InternedGoals) -> &'a [chalk_ir::Goal] { + fn goals_data(self, goals: &Self::InternedGoals) -> &[chalk_ir::Goal] { goals } @@ -279,10 +273,10 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn substitution_data<'a>( + fn substitution_data( self, - substitution: &'a Self::InternedSubstitution, - ) -> &'a [chalk_ir::GenericArg] { + substitution: &Self::InternedSubstitution, + ) -> &[chalk_ir::GenericArg] { substitution } @@ -293,10 +287,10 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { Box::new(data) } - fn program_clause_data<'a>( + fn program_clause_data( self, - clause: &'a Self::InternedProgramClause, - ) -> &'a chalk_ir::ProgramClauseData { + clause: &Self::InternedProgramClause, + ) -> &chalk_ir::ProgramClauseData { &clause } @@ -307,10 +301,10 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn program_clauses_data<'a>( + fn program_clauses_data( self, - clauses: &'a Self::InternedProgramClauses, - ) -> &'a [chalk_ir::ProgramClause] { + clauses: &Self::InternedProgramClauses, + ) -> &[chalk_ir::ProgramClause] { clauses } @@ -321,10 +315,10 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn quantified_where_clauses_data<'a>( + fn quantified_where_clauses_data( self, - clauses: &'a Self::InternedQuantifiedWhereClauses, - ) -> &'a [chalk_ir::QuantifiedWhereClause] { + clauses: &Self::InternedQuantifiedWhereClauses, + ) -> &[chalk_ir::QuantifiedWhereClause] { clauses } @@ -335,10 +329,10 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn variable_kinds_data<'a>( + fn variable_kinds_data( self, - parameter_kinds: &'a Self::InternedVariableKinds, - ) -> &'a [chalk_ir::VariableKind] { + parameter_kinds: &Self::InternedVariableKinds, + ) -> &[chalk_ir::VariableKind] { parameter_kinds } @@ -349,10 +343,10 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn canonical_var_kinds_data<'a>( + fn canonical_var_kinds_data( self, - canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, - ) -> &'a [chalk_ir::CanonicalVarKind] { + canonical_var_kinds: &Self::InternedCanonicalVarKinds, + ) -> &[chalk_ir::CanonicalVarKind] { canonical_var_kinds } @@ -363,10 +357,10 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn constraints_data<'a>( + fn constraints_data( self, - constraints: &'a Self::InternedConstraints, - ) -> &'a [chalk_ir::InEnvironment>] { + constraints: &Self::InternedConstraints, + ) -> &[chalk_ir::InEnvironment>] { constraints } @@ -377,10 +371,7 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> { data.into_iter().collect::, _>>() } - fn variances_data<'a>( - self, - variances: &'a Self::InternedVariances, - ) -> &'a [chalk_ir::Variance] { + fn variances_data(self, variances: &Self::InternedVariances) -> &[chalk_ir::Variance] { variances } } diff --git a/compiler/rustc_middle/src/traits/specialization_graph.rs b/compiler/rustc_middle/src/traits/specialization_graph.rs index cccedc9ec6ea..7b0979acd5bb 100644 --- a/compiler/rustc_middle/src/traits/specialization_graph.rs +++ b/compiler/rustc_middle/src/traits/specialization_graph.rs @@ -60,7 +60,7 @@ pub enum OverlapMode { } impl OverlapMode { - pub fn get<'tcx>(tcx: TyCtxt<'tcx>, trait_id: DefId) -> OverlapMode { + pub fn get(tcx: TyCtxt<'_>, trait_id: DefId) -> OverlapMode { let with_negative_coherence = tcx.features().with_negative_coherence; let strict_coherence = tcx.has_attr(trait_id, sym::rustc_strict_coherence); @@ -253,11 +253,11 @@ impl<'tcx> Ancestors<'tcx> { /// /// Returns `Err` if an error was reported while building the specialization /// graph. -pub fn ancestors<'tcx>( - tcx: TyCtxt<'tcx>, +pub fn ancestors( + tcx: TyCtxt<'_>, trait_def_id: DefId, start_from_impl: DefId, -) -> Result, ErrorGuaranteed> { +) -> Result, ErrorGuaranteed> { let specialization_graph = tcx.specialization_graph_of(trait_def_id); if let Some(reported) = specialization_graph.has_errored { diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index d00553cbad13..6ade8935fc84 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -238,10 +238,7 @@ impl<'tcx> CapturedPlace<'tcx> { } } -fn symbols_for_closure_captures<'tcx>( - tcx: TyCtxt<'tcx>, - def_id: (LocalDefId, LocalDefId), -) -> Vec { +fn symbols_for_closure_captures(tcx: TyCtxt<'_>, def_id: (LocalDefId, LocalDefId)) -> Vec { let typeck_results = tcx.typeck(def_id.0); let captures = typeck_results.closure_min_captures_flattened(def_id.1); captures.into_iter().map(|captured_place| captured_place.to_symbol(tcx)).collect() diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index e5abc38046ce..152a7e9d43fa 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -239,7 +239,7 @@ impl<'tcx> Const<'tcx> { } } -pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Const<'tcx> { +pub fn const_param_default(tcx: TyCtxt<'_>, def_id: DefId) -> Const<'_> { let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) { hir::Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { default: Some(ac), .. }, diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index 2a8a4d598882..48958e0d9e91 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -232,7 +232,7 @@ impl ScalarInt { } #[inline] - pub fn try_to_machine_usize<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Result { + pub fn try_to_machine_usize(&self, tcx: TyCtxt<'_>) -> Result { Ok(self.to_bits(tcx.data_layout.pointer_size)? as u64) } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 8f4d56c65b98..5de414077a2b 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1518,7 +1518,7 @@ impl<'tcx, T: 'tcx + ?Sized> IntoPointer for InternedInSet<'tcx, T> { #[allow(rustc::usage_of_ty_tykind)] impl<'tcx, T> Borrow for InternedInSet<'tcx, WithCachedTypeInfo> { - fn borrow<'a>(&'a self) -> &'a T { + fn borrow(&self) -> &T { &self.0.internee } } @@ -1541,7 +1541,7 @@ impl<'tcx, T: Hash> Hash for InternedInSet<'tcx, WithCachedTypeInfo> { } impl<'tcx, T> Borrow<[T]> for InternedInSet<'tcx, List> { - fn borrow<'a>(&'a self) -> &'a [T] { + fn borrow(&self) -> &[T] { &self.0[..] } } diff --git a/compiler/rustc_middle/src/ty/flags.rs b/compiler/rustc_middle/src/ty/flags.rs index 042b89bc4b01..8306d670a65d 100644 --- a/compiler/rustc_middle/src/ty/flags.rs +++ b/compiler/rustc_middle/src/ty/flags.rs @@ -22,7 +22,7 @@ impl FlagComputation { result } - pub fn for_predicate<'tcx>(binder: ty::Binder<'tcx, ty::PredicateKind<'_>>) -> FlagComputation { + pub fn for_predicate(binder: ty::Binder<'_, ty::PredicateKind<'_>>) -> FlagComputation { let mut result = FlagComputation::new(); result.add_predicate(binder); result diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 7f66b993646a..9e0ca44d0981 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -993,7 +993,7 @@ where /// might (from a foreign exception or similar). #[inline] #[tracing::instrument(level = "debug", skip(tcx))] -pub fn fn_can_unwind<'tcx>(tcx: TyCtxt<'tcx>, fn_def_id: Option, abi: SpecAbi) -> bool { +pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option, abi: SpecAbi) -> bool { if let Some(did) = fn_def_id { // Special attribute for functions which can't unwind. if tcx.codegen_fn_attrs(did).flags.contains(CodegenFnAttrFlags::NEVER_UNWIND) { diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 5d0af1d4eadc..6d3b94c1fdbf 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -1248,7 +1248,7 @@ pub fn needs_drop_components<'tcx>( } } -pub fn is_trivially_const_drop<'tcx>(ty: Ty<'tcx>) -> bool { +pub fn is_trivially_const_drop(ty: Ty<'_>) -> bool { match *ty.kind() { ty::Bool | ty::Char diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index edd527286264..e22fa6365dcb 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -81,8 +81,8 @@ pub(in crate::build) struct PlaceBuilder<'tcx> { /// ProjectionElems `Downcast`, `ConstantIndex`, `Index`, or `Subslice` because those will never be /// part of a path that is captured by a closure. We stop applying projections once we see the first /// projection that isn't captured by a closure. -fn convert_to_hir_projections_and_truncate_for_capture<'tcx>( - mir_projections: &[PlaceElem<'tcx>], +fn convert_to_hir_projections_and_truncate_for_capture( + mir_projections: &[PlaceElem<'_>], ) -> Vec { let mut hir_projections = Vec::new(); let mut variant = None; diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index c785dfb500fc..9daf68a15f4b 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -28,10 +28,10 @@ use rustc_target::spec::abi::Abi; use super::lints; -pub(crate) fn mir_built<'tcx>( - tcx: TyCtxt<'tcx>, +pub(crate) fn mir_built( + tcx: TyCtxt<'_>, def: ty::WithOptConstParam, -) -> &'tcx rustc_data_structures::steal::Steal> { +) -> &rustc_data_structures::steal::Steal> { if let Some(def) = def.try_upgrade(tcx) { return tcx.mir_built(def); } @@ -625,12 +625,12 @@ fn construct_const<'a, 'tcx>( /// /// This is required because we may still want to run MIR passes on an item /// with type errors, but normal MIR construction can't handle that in general. -fn construct_error<'tcx>( - tcx: TyCtxt<'tcx>, +fn construct_error( + tcx: TyCtxt<'_>, def: LocalDefId, body_owner_kind: hir::BodyOwnerKind, err: ErrorGuaranteed, -) -> Body<'tcx> { +) -> Body<'_> { let span = tcx.def_span(def); let hir_id = tcx.hir().local_def_id_to_hir_id(def); let generator_kind = tcx.generator_kind(def); diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 99e96ff77ced..3c311729a52a 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -703,7 +703,7 @@ impl UnsafeOpKind { } } -pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam) { +pub fn check_unsafety(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) { // THIR unsafeck is gated under `-Z thir-unsafeck` if !tcx.sess.opts.unstable_opts.thir_unsafeck { return; @@ -749,7 +749,7 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { +pub(crate) fn thir_check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) { if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) { tcx.thir_check_unsafety_for_const_arg(def) } else { @@ -757,8 +757,8 @@ pub(crate) fn thir_check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) { } } -pub(crate) fn thir_check_unsafety_for_const_arg<'tcx>( - tcx: TyCtxt<'tcx>, +pub(crate) fn thir_check_unsafety_for_const_arg( + tcx: TyCtxt<'_>, (did, param_did): (LocalDefId, DefId), ) { check_unsafety(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) }) diff --git a/compiler/rustc_mir_build/src/thir/cx/mod.rs b/compiler/rustc_mir_build/src/thir/cx/mod.rs index b5c4b7b137d4..a355e1bdab5f 100644 --- a/compiler/rustc_mir_build/src/thir/cx/mod.rs +++ b/compiler/rustc_mir_build/src/thir/cx/mod.rs @@ -18,10 +18,10 @@ use rustc_middle::thir::*; use rustc_middle::ty::{self, RvalueScopes, TyCtxt}; use rustc_span::Span; -pub(crate) fn thir_body<'tcx>( - tcx: TyCtxt<'tcx>, +pub(crate) fn thir_body( + tcx: TyCtxt<'_>, owner_def: ty::WithOptConstParam, -) -> Result<(&'tcx Steal>, ExprId), ErrorGuaranteed> { +) -> Result<(&Steal>, ExprId), ErrorGuaranteed> { let hir = tcx.hir(); let body = hir.body(hir.body_owned_by(owner_def.did)); let mut cx = Cx::new(tcx, owner_def); @@ -52,10 +52,7 @@ pub(crate) fn thir_body<'tcx>( Ok((tcx.alloc_steal_thir(cx.thir), expr)) } -pub(crate) fn thir_tree<'tcx>( - tcx: TyCtxt<'tcx>, - owner_def: ty::WithOptConstParam, -) -> String { +pub(crate) fn thir_tree(tcx: TyCtxt<'_>, owner_def: ty::WithOptConstParam) -> String { match thir_body(tcx, owner_def) { Ok((thir, _)) => format!("{:#?}", thir.steal()), Err(_) => "error".into(), diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index 7e1f708b0d6a..6470efab2e93 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -70,7 +70,7 @@ mod fallback_to_const_ref { /// hoops to get a reference to the value. pub(super) struct FallbackToConstRef(()); - pub(super) fn fallback_to_const_ref<'tcx>(c2p: &super::ConstToPat<'tcx>) -> FallbackToConstRef { + pub(super) fn fallback_to_const_ref(c2p: &super::ConstToPat<'_>) -> FallbackToConstRef { assert!(c2p.behind_reference.get()); FallbackToConstRef(()) } diff --git a/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs index 18e9c69c4870..a95349d76709 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs @@ -404,7 +404,7 @@ impl SplitIntRange { } /// Iterate over the contained ranges. - fn iter<'a>(&'a self) -> impl Iterator + Captures<'a> { + fn iter(&self) -> impl Iterator + Captures<'_> { use IntBorder::*; let self_range = Self::to_borders(self.range.clone()); @@ -612,7 +612,7 @@ impl SplitVarLenSlice { } /// Iterate over the partition of this slice. - fn iter<'a>(&'a self) -> impl Iterator + Captures<'a> { + fn iter(&self) -> impl Iterator + Captures<'_> { let smaller_lengths = match self.array_len { // The only admissible fixed-length slice is one of the array size. Whether `max_slice` // is fixed-length or variable-length, it will be the only relevant slice to output diff --git a/compiler/rustc_mir_dataflow/src/impls/liveness.rs b/compiler/rustc_mir_dataflow/src/impls/liveness.rs index 3e08a8799ef9..923dc16c11b0 100644 --- a/compiler/rustc_mir_dataflow/src/impls/liveness.rs +++ b/compiler/rustc_mir_dataflow/src/impls/liveness.rs @@ -149,7 +149,7 @@ enum DefUse { } impl DefUse { - fn apply<'tcx>(trans: &mut impl GenKill, place: Place<'tcx>, context: PlaceContext) { + fn apply(trans: &mut impl GenKill, place: Place<'_>, context: PlaceContext) { match DefUse::for_place(place, context) { Some(DefUse::Def) => trans.kill(place.local), Some(DefUse::Use) => trans.gen(place.local), @@ -157,7 +157,7 @@ impl DefUse { } } - fn for_place<'tcx>(place: Place<'tcx>, context: PlaceContext) -> Option { + fn for_place(place: Place<'_>, context: PlaceContext) -> Option { match context { PlaceContext::NonUse(_) => None, diff --git a/compiler/rustc_mir_dataflow/src/value_analysis.rs b/compiler/rustc_mir_dataflow/src/value_analysis.rs index 7df011422641..fe5ee4011ab8 100644 --- a/compiler/rustc_mir_dataflow/src/value_analysis.rs +++ b/compiler/rustc_mir_dataflow/src/value_analysis.rs @@ -823,7 +823,7 @@ fn iter_fields<'tcx>( } /// Returns all locals with projections that have their reference or address taken. -fn excluded_locals<'tcx>(body: &Body<'tcx>) -> IndexVec { +fn excluded_locals(body: &Body<'_>) -> IndexVec { struct Collector { result: IndexVec, } diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index 9c22b5df73ce..adf6ae4c7270 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -490,10 +490,10 @@ fn check_unused_unsafe( unused_unsafes } -fn unsafety_check_result<'tcx>( - tcx: TyCtxt<'tcx>, +fn unsafety_check_result( + tcx: TyCtxt<'_>, def: ty::WithOptConstParam, -) -> &'tcx UnsafetyCheckResult { +) -> &UnsafetyCheckResult { debug!("unsafety_violations({:?})", def); // N.B., this borrow is valid because all the consumers of diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 604810144887..1468afc64563 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -533,10 +533,10 @@ fn make_code_region( } } -fn fn_sig_and_body<'tcx>( - tcx: TyCtxt<'tcx>, +fn fn_sig_and_body( + tcx: TyCtxt<'_>, def_id: DefId, -) -> (Option<&'tcx rustc_hir::FnSig<'tcx>>, &'tcx rustc_hir::Body<'tcx>) { +) -> (Option<&rustc_hir::FnSig<'_>>, &rustc_hir::Body<'_>) { // FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back // to HIR for it. let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local"); diff --git a/compiler/rustc_mir_transform/src/coverage/query.rs b/compiler/rustc_mir_transform/src/coverage/query.rs index dc1e68b253e7..3bd7f31b45dc 100644 --- a/compiler/rustc_mir_transform/src/coverage/query.rs +++ b/compiler/rustc_mir_transform/src/coverage/query.rs @@ -136,7 +136,7 @@ fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) -> coverage_visitor.info } -fn covered_code_regions<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Vec<&'tcx CodeRegion> { +fn covered_code_regions(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<&CodeRegion> { let body = mir_body(tcx, def_id); body.basic_blocks .iter() @@ -163,7 +163,7 @@ fn is_inlined(body: &Body<'_>, statement: &Statement<'_>) -> bool { /// This function ensures we obtain the correct MIR for the given item irrespective of /// whether that means const mir or runtime mir. For `const fn` this opts for runtime /// mir. -fn mir_body<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx mir::Body<'tcx> { +fn mir_body(tcx: TyCtxt<'_>, def_id: DefId) -> &mir::Body<'_> { let id = ty::WithOptConstParam::unknown(def_id); let def = ty::InstanceDef::Item(id); tcx.instance_mir(def) diff --git a/compiler/rustc_mir_transform/src/coverage/tests.rs b/compiler/rustc_mir_transform/src/coverage/tests.rs index eba6a2b34e47..8f2bb11639f1 100644 --- a/compiler/rustc_mir_transform/src/coverage/tests.rs +++ b/compiler/rustc_mir_transform/src/coverage/tests.rs @@ -169,7 +169,7 @@ impl<'tcx> MockBlocks<'tcx> { } } -fn debug_basic_blocks<'tcx>(mir_body: &Body<'tcx>) -> String { +fn debug_basic_blocks(mir_body: &Body) -> String { format!( "{:?}", mir_body diff --git a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs b/compiler/rustc_mir_transform/src/deduce_param_attrs.rs index 92f1fff6beb9..ddab7bbb2e30 100644 --- a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs +++ b/compiler/rustc_mir_transform/src/deduce_param_attrs.rs @@ -129,7 +129,7 @@ impl<'tcx> Visitor<'tcx> for DeduceReadOnly { } /// Returns true if values of a given type will never be passed indirectly, regardless of ABI. -fn type_will_always_be_passed_directly<'tcx>(ty: Ty<'tcx>) -> bool { +fn type_will_always_be_passed_directly(ty: Ty<'_>) -> bool { matches!( ty.kind(), ty::Bool diff --git a/compiler/rustc_mir_transform/src/dest_prop.rs b/compiler/rustc_mir_transform/src/dest_prop.rs index 74d8337653f0..08e296a83712 100644 --- a/compiler/rustc_mir_transform/src/dest_prop.rs +++ b/compiler/rustc_mir_transform/src/dest_prop.rs @@ -658,7 +658,7 @@ impl WriteInfo { } } - fn add_place<'tcx>(&mut self, place: Place<'tcx>) { + fn add_place(&mut self, place: Place<'_>) { self.writes.push(place.local); } diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index aba5a8580f19..ae79c2290f6d 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -266,10 +266,7 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) -> /// Make MIR ready for const evaluation. This is run on all MIR, not just on consts! /// FIXME(oli-obk): it's unclear whether we still need this phase (and its corresponding query). /// We used to have this for pre-miri MIR based const eval. -fn mir_const<'tcx>( - tcx: TyCtxt<'tcx>, - def: ty::WithOptConstParam, -) -> &'tcx Steal> { +fn mir_const(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) -> &Steal> { if let Some(def) = def.try_upgrade(tcx) { return tcx.mir_const(def); } @@ -308,10 +305,10 @@ fn mir_const<'tcx>( } /// Compute the main MIR body and the list of MIR bodies of the promoteds. -fn mir_promoted<'tcx>( - tcx: TyCtxt<'tcx>, +fn mir_promoted( + tcx: TyCtxt<'_>, def: ty::WithOptConstParam, -) -> (&'tcx Steal>, &'tcx Steal>>) { +) -> (&Steal>, &Steal>>) { if let Some(def) = def.try_upgrade(tcx) { return tcx.mir_promoted(def); } @@ -350,7 +347,7 @@ fn mir_promoted<'tcx>( } /// Compute the MIR that is used during CTFE (and thus has no optimizations run on it) -fn mir_for_ctfe<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Body<'tcx> { +fn mir_for_ctfe(tcx: TyCtxt<'_>, def_id: DefId) -> &Body<'_> { let did = def_id.expect_local(); if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) { tcx.mir_for_ctfe_of_const_arg(def) @@ -364,10 +361,7 @@ fn mir_for_ctfe<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Body<'tcx> { /// we'd get cycle errors with `mir_for_ctfe`, because typeck would need to typeck /// the const parameter while type checking the main body, which in turn would try /// to type check the main body again. -fn mir_for_ctfe_of_const_arg<'tcx>( - tcx: TyCtxt<'tcx>, - (did, param_did): (LocalDefId, DefId), -) -> &'tcx Body<'tcx> { +fn mir_for_ctfe_of_const_arg(tcx: TyCtxt<'_>, (did, param_did): (LocalDefId, DefId)) -> &Body<'_> { tcx.arena.alloc(inner_mir_for_ctfe( tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) }, @@ -424,10 +418,10 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) - /// Obtain just the main MIR (no promoteds) and run some cleanups on it. This also runs /// mir borrowck *before* doing so in order to ensure that borrowck can be run and doesn't /// end up missing the source MIR due to stealing happening. -fn mir_drops_elaborated_and_const_checked<'tcx>( - tcx: TyCtxt<'tcx>, +fn mir_drops_elaborated_and_const_checked( + tcx: TyCtxt<'_>, def: ty::WithOptConstParam, -) -> &'tcx Steal> { +) -> &Steal> { if let Some(def) = def.try_upgrade(tcx) { return tcx.mir_drops_elaborated_and_const_checked(def); } @@ -597,7 +591,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { } /// Optimize the MIR and prepare it for codegen. -fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> { +fn optimized_mir(tcx: TyCtxt<'_>, did: DefId) -> &Body<'_> { let did = did.expect_local(); assert_eq!(ty::WithOptConstParam::try_lookup(did, tcx), None); tcx.arena.alloc(inner_optimized_mir(tcx, did)) @@ -634,10 +628,10 @@ fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> { /// Fetch all the promoteds of an item and prepare their MIR bodies to be ready for /// constant evaluation once all substitutions become known. -fn promoted_mir<'tcx>( - tcx: TyCtxt<'tcx>, +fn promoted_mir( + tcx: TyCtxt<'_>, def: ty::WithOptConstParam, -) -> &'tcx IndexVec> { +) -> &IndexVec> { if tcx.is_constructor(def.did.to_def_id()) { return tcx.arena.alloc(IndexVec::new()); } diff --git a/compiler/rustc_mir_transform/src/simplify_try.rs b/compiler/rustc_mir_transform/src/simplify_try.rs index baeb620ef240..e4f3ace9a93d 100644 --- a/compiler/rustc_mir_transform/src/simplify_try.rs +++ b/compiler/rustc_mir_transform/src/simplify_try.rs @@ -532,7 +532,7 @@ struct VarField<'tcx> { } /// Match on `((_LOCAL as Variant).FIELD: TY)`. -fn match_variant_field_place<'tcx>(place: Place<'tcx>) -> Option<(Local, VarField<'tcx>)> { +fn match_variant_field_place(place: Place<'_>) -> Option<(Local, VarField<'_>)> { match place.as_ref() { PlaceRef { local, diff --git a/compiler/rustc_monomorphize/src/partitioning/mod.rs b/compiler/rustc_monomorphize/src/partitioning/mod.rs index 38e1d98e44e1..a55a3fe5ccaa 100644 --- a/compiler/rustc_monomorphize/src/partitioning/mod.rs +++ b/compiler/rustc_monomorphize/src/partitioning/mod.rs @@ -345,10 +345,7 @@ where } } -fn collect_and_partition_mono_items<'tcx>( - tcx: TyCtxt<'tcx>, - (): (), -) -> (&'tcx DefIdSet, &'tcx [CodegenUnit<'tcx>]) { +fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[CodegenUnit<'_>]) { let collection_mode = match tcx.sess.opts.unstable_opts.print_mono_items { Some(ref s) => { let mode_string = s.to_lowercase(); @@ -541,7 +538,7 @@ fn dump_mono_items_stats<'tcx>( Ok(()) } -fn codegened_and_inlined_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx DefIdSet { +fn codegened_and_inlined_items(tcx: TyCtxt<'_>, (): ()) -> &DefIdSet { let (items, cgus) = tcx.collect_and_partition_mono_items(()); let mut visited = DefIdSet::default(); let mut result = items.clone(); diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index f5556738bff9..edb0e4367f27 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -571,7 +571,7 @@ fn check_item<'tcx>( } } -fn check_trait_item<'tcx>(tcx: TyCtxt<'tcx>, worklist: &mut Vec, id: hir::TraitItemId) { +fn check_trait_item(tcx: TyCtxt<'_>, worklist: &mut Vec, id: hir::TraitItemId) { use hir::TraitItemKind::{Const, Fn}; if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) { let trait_item = tcx.hir().trait_item(id); @@ -583,11 +583,7 @@ fn check_trait_item<'tcx>(tcx: TyCtxt<'tcx>, worklist: &mut Vec, id: } } -fn check_foreign_item<'tcx>( - tcx: TyCtxt<'tcx>, - worklist: &mut Vec, - id: hir::ForeignItemId, -) { +fn check_foreign_item(tcx: TyCtxt<'_>, worklist: &mut Vec, id: hir::ForeignItemId) { if matches!(tcx.def_kind(id.owner_id), DefKind::Static(_) | DefKind::Fn) && has_allow_dead_code_or_lang_attr(tcx, id.hir_id()) { @@ -595,8 +591,8 @@ fn check_foreign_item<'tcx>( } } -fn create_and_seed_worklist<'tcx>( - tcx: TyCtxt<'tcx>, +fn create_and_seed_worklist( + tcx: TyCtxt<'_>, ) -> (Vec, FxHashMap) { let effective_visibilities = &tcx.effective_visibilities(()); // see `MarkSymbolVisitor::struct_constructors` @@ -626,8 +622,8 @@ fn create_and_seed_worklist<'tcx>( (worklist, struct_constructors) } -fn live_symbols_and_ignored_derived_traits<'tcx>( - tcx: TyCtxt<'tcx>, +fn live_symbols_and_ignored_derived_traits( + tcx: TyCtxt<'_>, (): (), ) -> (FxHashSet, FxHashMap>) { let (worklist, struct_constructors) = create_and_seed_worklist(tcx); diff --git a/compiler/rustc_passes/src/debugger_visualizer.rs b/compiler/rustc_passes/src/debugger_visualizer.rs index 253b0a88e48a..aeacbaa67cb5 100644 --- a/compiler/rustc_passes/src/debugger_visualizer.rs +++ b/compiler/rustc_passes/src/debugger_visualizer.rs @@ -15,8 +15,8 @@ use std::sync::Arc; use crate::errors::DebugVisualizerUnreadable; -fn check_for_debugger_visualizer<'tcx>( - tcx: TyCtxt<'tcx>, +fn check_for_debugger_visualizer( + tcx: TyCtxt<'_>, hir_id: HirId, debugger_visualizers: &mut FxHashSet, ) { @@ -69,7 +69,7 @@ fn check_for_debugger_visualizer<'tcx>( } /// Traverses and collects the debugger visualizers for a specific crate. -fn debugger_visualizers<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> Vec { +fn debugger_visualizers(tcx: TyCtxt<'_>, cnum: CrateNum) -> Vec { assert_eq!(cnum, LOCAL_CRATE); // Initialize the collector. diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index a72056e00b1e..10ffa87efe35 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -18,11 +18,7 @@ use rustc_span::symbol::{kw::Empty, sym, Symbol}; use crate::errors::{DuplicateDiagnosticItem, DuplicateDiagnosticItemInCrate}; -fn observe_item<'tcx>( - tcx: TyCtxt<'tcx>, - diagnostic_items: &mut DiagnosticItems, - def_id: LocalDefId, -) { +fn observe_item(tcx: TyCtxt<'_>, diagnostic_items: &mut DiagnosticItems, def_id: LocalDefId) { let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let attrs = tcx.hir().attrs(hir_id); if let Some(name) = extract(attrs) { @@ -63,7 +59,7 @@ fn extract(attrs: &[ast::Attribute]) -> Option { } /// Traverse and collect the diagnostic items in the current -fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> DiagnosticItems { +fn diagnostic_items(tcx: TyCtxt<'_>, cnum: CrateNum) -> DiagnosticItems { assert_eq!(cnum, LOCAL_CRATE); // Initialize the collector. @@ -92,7 +88,7 @@ fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> DiagnosticItems } /// Traverse and collect all the diagnostic items in all crates. -fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> DiagnosticItems { +fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems { // Initialize the collector. let mut items = DiagnosticItems::default(); diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index 5322baee7473..827d86780aa8 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -26,7 +26,7 @@ pub fn test_layout(tcx: TyCtxt<'_>) { } } -fn dump_layout_of<'tcx>(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId, attr: &Attribute) { +fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { let tcx = tcx; let param_env = tcx.param_env(item_def_id); let ty = tcx.type_of(item_def_id); diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index e7c3c712852d..ad0952203867 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -347,7 +347,7 @@ fn check_item<'tcx>( } } -fn has_custom_linkage<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool { +fn has_custom_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { // Anything which has custom linkage gets thrown on the worklist no // matter where it is in the crate, along with "special std symbols" // which are currently akin to allocator symbols. @@ -364,7 +364,7 @@ fn has_custom_linkage<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool { || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) } -fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet { +fn reachable_set(tcx: TyCtxt<'_>, (): ()) -> FxHashSet { let effective_visibilities = &tcx.effective_visibilities(()); let any_library = diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index da715523474f..96f7236de5cb 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -853,7 +853,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { /// Check whether a path is a `use` item that has been marked as unstable. /// /// See issue #94972 for details on why this is a special case -fn is_unstable_reexport<'tcx>(tcx: TyCtxt<'tcx>, id: hir::HirId) -> bool { +fn is_unstable_reexport(tcx: TyCtxt<'_>, id: hir::HirId) -> bool { // Get the LocalDefId so we can lookup the item to check the kind. let Some(def_id) = tcx.hir().opt_local_def_id(id) else { return false; }; diff --git a/compiler/rustc_passes/src/weak_lang_items.rs b/compiler/rustc_passes/src/weak_lang_items.rs index f0815fcd8db9..fc6372cf99ee 100644 --- a/compiler/rustc_passes/src/weak_lang_items.rs +++ b/compiler/rustc_passes/src/weak_lang_items.rs @@ -11,7 +11,7 @@ use crate::errors::{MissingLangItem, MissingPanicHandler, UnknownExternLangItem} /// Checks the crate for usage of weak lang items, returning a vector of all the /// language items required by this crate, but not defined yet. -pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) { +pub fn check_crate(tcx: TyCtxt<'_>, items: &mut lang_items::LanguageItems) { // These are never called by user code, they're generated by the compiler. // They will never implicitly be added to the `missing` array unless we do // so here. @@ -40,7 +40,7 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItem verify(tcx, items); } -fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) { +fn verify(tcx: TyCtxt<'_>, items: &lang_items::LanguageItems) { // We only need to check for the presence of weak lang items if we're // emitting something that's not an rlib. let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind { diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index c4e605c18521..2bcfdab03c81 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -227,7 +227,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> { *self.serialized_data.write() = None; } - fn serialize<'tcx>(&self, tcx: TyCtxt<'tcx>, encoder: FileEncoder) -> FileEncodeResult { + fn serialize(&self, tcx: TyCtxt<'_>, encoder: FileEncoder) -> FileEncodeResult { // Serializing the `DepGraph` should not modify it. tcx.dep_graph.with_ignore(|| { // Allocate `SourceFileIndex`es. diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index 81114f2cd82c..5f54bab9c314 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -278,7 +278,7 @@ pub(crate) fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>( /// If we are recording only summary data, the ids will point to /// just the query names. If we are recording query keys too, we /// allocate the corresponding strings here. -pub fn alloc_self_profile_query_strings<'tcx>(tcx: TyCtxt<'tcx>) { +pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) { if !tcx.prof.enabled() { return; } diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 848fa67e3df2..6110acd2c750 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -337,9 +337,9 @@ where /// which will be used if the query is not in the cache and we need /// to compute it. #[inline] -pub fn try_get_cached<'a, Tcx, C, R, OnHit>( +pub fn try_get_cached( tcx: Tcx, - cache: &'a C, + cache: &C, key: &C::Key, // `on_hit` can be called while holding a lock to the query cache on_hit: OnHit, diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs index 6c310abf10ac..6afd5fe5a7f2 100644 --- a/compiler/rustc_save_analysis/src/lib.rs +++ b/compiler/rustc_save_analysis/src/lib.rs @@ -957,10 +957,10 @@ impl SaveHandler for CallbackHandler<'_> { } } -pub fn process_crate<'l, 'tcx, H: SaveHandler>( - tcx: TyCtxt<'tcx>, +pub fn process_crate( + tcx: TyCtxt<'_>, cratename: Symbol, - input: &'l Input, + input: &Input, config: Option, mut handler: H, ) { diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index f9f4f2979c4e..2aa8ca9e4a91 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -122,7 +122,7 @@ pub fn feature_err_issue<'a>( /// Construct a future incompatibility diagnostic for a feature gate. /// /// This diagnostic is only a warning and *does not cause compilation to fail*. -pub fn feature_warn<'a>(sess: &'a ParseSess, feature: Symbol, span: Span, explain: &str) { +pub fn feature_warn(sess: &ParseSess, feature: Symbol, span: Span, explain: &str) { feature_warn_issue(sess, feature, span, GateIssue::Language, explain); } @@ -134,8 +134,8 @@ pub fn feature_warn<'a>(sess: &'a ParseSess, feature: Symbol, span: Span, explai /// Almost always, you want to use this for a language feature. If so, prefer `feature_warn`. #[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::untranslatable_diagnostic)] -pub fn feature_warn_issue<'a>( - sess: &'a ParseSess, +pub fn feature_warn_issue( + sess: &ParseSess, feature: Symbol, span: Span, issue: GateIssue, @@ -160,7 +160,7 @@ pub fn feature_warn_issue<'a>( } /// Adds the diagnostics for a feature to an existing error. -pub fn add_feature_diagnostics<'a>(err: &mut Diagnostic, sess: &'a ParseSess, feature: Symbol) { +pub fn add_feature_diagnostics(err: &mut Diagnostic, sess: &ParseSess, feature: Symbol) { add_feature_diagnostics_for_issue(err, sess, feature, GateIssue::Language); } @@ -169,9 +169,9 @@ pub fn add_feature_diagnostics<'a>(err: &mut Diagnostic, sess: &'a ParseSess, fe /// This variant allows you to control whether it is a library or language feature. /// Almost always, you want to use this for a language feature. If so, prefer /// `add_feature_diagnostics`. -pub fn add_feature_diagnostics_for_issue<'a>( +pub fn add_feature_diagnostics_for_issue( err: &mut Diagnostic, - sess: &'a ParseSess, + sess: &ParseSess, feature: Symbol, issue: GateIssue, ) { diff --git a/compiler/rustc_session/src/utils.rs b/compiler/rustc_session/src/utils.rs index e65b6891e322..b996d36a318c 100644 --- a/compiler/rustc_session/src/utils.rs +++ b/compiler/rustc_session/src/utils.rs @@ -3,7 +3,7 @@ use rustc_data_structures::profiling::VerboseTimingGuard; use std::path::{Path, PathBuf}; impl Session { - pub fn timer<'a>(&'a self, what: &'static str) -> VerboseTimingGuard<'a> { + pub fn timer(&self, what: &'static str) -> VerboseTimingGuard<'_> { self.prof.verbose_generic_activity(what) } pub fn time(&self, what: &'static str, f: impl FnOnce() -> R) -> R { diff --git a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs index 0845b1b6b096..493e31a688fc 100644 --- a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs +++ b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs @@ -338,7 +338,7 @@ fn encode_substs<'tcx>( } /// Encodes a ty:Ty name, including its crate and path disambiguators and names. -fn encode_ty_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> String { +fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String { // Encode for use in u[IE], where // is , using v0's without v0's extended form of paths: // diff --git a/compiler/rustc_target/src/abi/call/loongarch.rs b/compiler/rustc_target/src/abi/call/loongarch.rs index d29b479de5da..4a2d39cc7002 100644 --- a/compiler/rustc_target/src/abi/call/loongarch.rs +++ b/compiler/rustc_target/src/abi/call/loongarch.rs @@ -19,7 +19,7 @@ enum FloatConv { #[derive(Copy, Clone)] struct CannotUseFpConv; -fn is_loongarch_aggregate<'a, Ty>(arg: &ArgAbi<'a, Ty>) -> bool { +fn is_loongarch_aggregate(arg: &ArgAbi<'_, Ty>) -> bool { match arg.layout.abi { Abi::Vector { .. } => true, _ => arg.layout.is_aggregate(), @@ -290,7 +290,7 @@ fn classify_arg<'a, Ty, C>( } } -fn extend_integer_width<'a, Ty>(arg: &mut ArgAbi<'a, Ty>, xlen: u64) { +fn extend_integer_width(arg: &mut ArgAbi<'_, Ty>, xlen: u64) { if let Abi::Scalar(scalar) = arg.layout.abi { if let abi::Int(i, _) = scalar.primitive() { // 32-bit integers are always sign-extended diff --git a/compiler/rustc_target/src/abi/call/riscv.rs b/compiler/rustc_target/src/abi/call/riscv.rs index 1cb360f834e0..34280d38e340 100644 --- a/compiler/rustc_target/src/abi/call/riscv.rs +++ b/compiler/rustc_target/src/abi/call/riscv.rs @@ -25,7 +25,7 @@ enum FloatConv { #[derive(Copy, Clone)] struct CannotUseFpConv; -fn is_riscv_aggregate<'a, Ty>(arg: &ArgAbi<'a, Ty>) -> bool { +fn is_riscv_aggregate(arg: &ArgAbi<'_, Ty>) -> bool { match arg.layout.abi { Abi::Vector { .. } => true, _ => arg.layout.is_aggregate(), @@ -296,7 +296,7 @@ fn classify_arg<'a, Ty, C>( } } -fn extend_integer_width<'a, Ty>(arg: &mut ArgAbi<'a, Ty>, xlen: u64) { +fn extend_integer_width(arg: &mut ArgAbi<'_, Ty>, xlen: u64) { if let Abi::Scalar(scalar) = arg.layout.abi { if let abi::Int(i, _) = scalar.primitive() { // 32-bit integers are always sign-extended diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index be994eda14c0..988cd401f406 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1319,7 +1319,7 @@ pub struct Target { } impl Target { - pub fn parse_data_layout<'a>(&'a self) -> Result> { + pub fn parse_data_layout(&self) -> Result> { let mut dl = TargetDataLayout::parse_from_llvm_datalayout_string(&self.data_layout)?; // Perform consistency checks against the Target information. diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 7c569621cfeb..26757965c958 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -66,13 +66,13 @@ pub fn add_placeholder_note(err: &mut Diagnostic) { /// with a suitably-freshened `ImplHeader` with those types /// substituted. Otherwise, returns `None`. #[instrument(skip(tcx, skip_leak_check), level = "debug")] -pub fn overlapping_impls<'tcx>( - tcx: TyCtxt<'tcx>, +pub fn overlapping_impls( + tcx: TyCtxt<'_>, impl1_def_id: DefId, impl2_def_id: DefId, skip_leak_check: SkipLeakCheck, overlap_mode: OverlapMode, -) -> Option> { +) -> Option> { // Before doing expensive operations like entering an inference context, do // a quick check via fast_reject to tell if the impl headers could possibly // unify. @@ -283,7 +283,7 @@ fn implicit_negative<'cx, 'tcx>( /// Given impl1 and impl2 check if both impls are never satisfied by a common type (including /// where-clauses) If so, return true, they are disjoint and false otherwise. -fn negative_impl<'tcx>(tcx: TyCtxt<'tcx>, impl1_def_id: DefId, impl2_def_id: DefId) -> bool { +fn negative_impl(tcx: TyCtxt<'_>, impl1_def_id: DefId, impl2_def_id: DefId) -> bool { debug!("negative_impl(impl1_def_id={:?}, impl2_def_id={:?})", impl1_def_id, impl2_def_id); // Create an infcx, taking the predicates of impl1 as assumptions: diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 036e8f6d47bb..d6c681ebb2d8 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3483,7 +3483,7 @@ fn hint_missing_borrow<'tcx>( let arg_spans = fn_decl.inputs.iter().map(|ty| ty.span); - fn get_deref_type_and_refs<'tcx>(mut ty: Ty<'tcx>) -> (Ty<'tcx>, usize) { + fn get_deref_type_and_refs(mut ty: Ty<'_>) -> (Ty<'_>, usize) { let mut refs = 0; while let ty::Ref(_, new_ty, _) = ty.kind() { diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 2566d793d78d..c30531fa9066 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -484,10 +484,7 @@ fn subst_and_check_impossible_predicates<'tcx>( /// /// This only considers predicates that reference the impl's generics, and not /// those that reference the method's generics. -fn is_impossible_method<'tcx>( - tcx: TyCtxt<'tcx>, - (impl_def_id, trait_item_def_id): (DefId, DefId), -) -> bool { +fn is_impossible_method(tcx: TyCtxt<'_>, (impl_def_id, trait_item_def_id): (DefId, DefId)) -> bool { struct ReferencesOnlyParentGenerics<'tcx> { tcx: TyCtxt<'tcx>, generics: &'tcx ty::Generics, diff --git a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs index e1092a788e32..f2c5f730b31b 100644 --- a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs +++ b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs @@ -34,7 +34,7 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> { /// argument types are well-formed. This may imply certain relationships /// between generic parameters. For example: /// ``` - /// fn foo<'a,T>(x: &'a T) {} + /// fn foo(x: &T) {} /// ``` /// can only be called with a `'a` and `T` such that `&'a T` is WF. /// For `&'a T` to be WF, `T: 'a` must hold. So we can assume `T: 'a`. diff --git a/compiler/rustc_trait_selection/src/traits/vtable.rs b/compiler/rustc_trait_selection/src/traits/vtable.rs index 41ce6cdf789d..5ec9c2a24cd4 100644 --- a/compiler/rustc_trait_selection/src/traits/vtable.rs +++ b/compiler/rustc_trait_selection/src/traits/vtable.rs @@ -191,7 +191,7 @@ fn dump_vtable_entries<'tcx>( }); } -fn own_existential_vtable_entries<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId) -> &'tcx [DefId] { +fn own_existential_vtable_entries(tcx: TyCtxt<'_>, trait_def_id: DefId) -> &[DefId] { let trait_methods = tcx .associated_items(trait_def_id) .in_definition_order() diff --git a/compiler/rustc_traits/src/chalk/db.rs b/compiler/rustc_traits/src/chalk/db.rs index bc4a52c5040d..f288eb112582 100644 --- a/compiler/rustc_traits/src/chalk/db.rs +++ b/compiler/rustc_traits/src/chalk/db.rs @@ -719,7 +719,7 @@ impl<'tcx> chalk_ir::UnificationDatabase> for RustIrDatabase< /// var bound at index `0`. For types, we use a `BoundVar` index equal to /// the type parameter index. For regions, we use the `BoundRegionKind::BrNamed` /// variant (which has a `DefId`). -fn bound_vars_for_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx> { +fn bound_vars_for_item(tcx: TyCtxt<'_>, def_id: DefId) -> SubstsRef<'_> { InternalSubsts::for_item(tcx, def_id, |param, substs| match param.kind { ty::GenericParamDefKind::Type { .. } => tcx .mk_ty(ty::Bound( diff --git a/compiler/rustc_traits/src/normalize_erasing_regions.rs b/compiler/rustc_traits/src/normalize_erasing_regions.rs index 44fd8bfb31f2..6e6bc62a040d 100644 --- a/compiler/rustc_traits/src/normalize_erasing_regions.rs +++ b/compiler/rustc_traits/src/normalize_erasing_regions.rs @@ -54,7 +54,7 @@ fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + } } -fn not_outlives_predicate<'tcx>(p: ty::Predicate<'tcx>) -> bool { +fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool { match p.kind().skip_binder() { ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..)) | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(..)) => false, diff --git a/compiler/rustc_ty_utils/src/consts.rs b/compiler/rustc_ty_utils/src/consts.rs index f8ff31f971be..a9b4e1420ea0 100644 --- a/compiler/rustc_ty_utils/src/consts.rs +++ b/compiler/rustc_ty_utils/src/consts.rs @@ -267,8 +267,8 @@ struct IsThirPolymorphic<'a, 'tcx> { thir: &'a thir::Thir<'tcx>, } -fn error<'tcx>( - tcx: TyCtxt<'tcx>, +fn error( + tcx: TyCtxt<'_>, sub: GenericConstantTooComplexSub, root_span: Span, ) -> Result { @@ -281,8 +281,8 @@ fn error<'tcx>( Err(reported) } -fn maybe_supported_error<'tcx>( - tcx: TyCtxt<'tcx>, +fn maybe_supported_error( + tcx: TyCtxt<'_>, sub: GenericConstantTooComplexSub, root_span: Span, ) -> Result { @@ -349,10 +349,10 @@ impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> { } /// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead. -pub fn thir_abstract_const<'tcx>( - tcx: TyCtxt<'tcx>, +pub fn thir_abstract_const( + tcx: TyCtxt<'_>, def: ty::WithOptConstParam, -) -> Result>, ErrorGuaranteed> { +) -> Result>, ErrorGuaranteed> { if tcx.features().generic_const_exprs { match tcx.def_kind(def.did) { // FIXME(generic_const_exprs): We currently only do this for anonymous constants, diff --git a/compiler/rustc_ty_utils/src/implied_bounds.rs b/compiler/rustc_ty_utils/src/implied_bounds.rs index f0d8c240ea58..b7a24a22c53e 100644 --- a/compiler/rustc_ty_utils/src/implied_bounds.rs +++ b/compiler/rustc_ty_utils/src/implied_bounds.rs @@ -6,7 +6,7 @@ pub fn provide(providers: &mut ty::query::Providers) { *providers = ty::query::Providers { assumed_wf_types, ..*providers }; } -fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx ty::List> { +fn assumed_wf_types(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::List> { match tcx.def_kind(def_id) { DefKind::Fn => { let sig = tcx.fn_sig(def_id); diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index c761a4dbe450..c8c6acaa4539 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -489,8 +489,8 @@ enum SavedLocalEligibility { // of any variant. /// Compute the eligibility and assignment of each local. -fn generator_saved_local_eligibility<'tcx>( - info: &GeneratorLayout<'tcx>, +fn generator_saved_local_eligibility( + info: &GeneratorLayout<'_>, ) -> (BitSet, IndexVec) { use SavedLocalEligibility::*; diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 5279fc69a31b..e2d10f550c32 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -225,10 +225,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { /// that are assumed to be well-formed (because they come from the environment). /// /// Used only in chalk mode. -fn well_formed_types_in_env<'tcx>( - tcx: TyCtxt<'tcx>, - def_id: DefId, -) -> &'tcx ty::List> { +fn well_formed_types_in_env(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::List> { use rustc_hir::{ForeignItemKind, ImplItemKind, ItemKind, Node, TraitItemKind}; use rustc_middle::ty::subst::GenericArgKind; From 42d100aad02031121ae2390c0dc5197131e93740 Mon Sep 17 00:00:00 2001 From: Jeremy Stucki Date: Tue, 20 Dec 2022 22:28:22 +0100 Subject: [PATCH 252/321] Add missing anonymous lifetime --- compiler/rustc_mir_transform/src/coverage/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_transform/src/coverage/tests.rs b/compiler/rustc_mir_transform/src/coverage/tests.rs index 8f2bb11639f1..fa7f22303a8b 100644 --- a/compiler/rustc_mir_transform/src/coverage/tests.rs +++ b/compiler/rustc_mir_transform/src/coverage/tests.rs @@ -169,7 +169,7 @@ impl<'tcx> MockBlocks<'tcx> { } } -fn debug_basic_blocks(mir_body: &Body) -> String { +fn debug_basic_blocks(mir_body: &Body<'_>) -> String { format!( "{:?}", mir_body From 2c9d9bf381e0643ccb0a777d19473ab939ebf6ae Mon Sep 17 00:00:00 2001 From: Jeremy Stucki Date: Tue, 20 Dec 2022 22:34:42 +0100 Subject: [PATCH 253/321] Add missing anonymous lifetime --- compiler/rustc_codegen_gcc/src/common.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_gcc/src/common.rs b/compiler/rustc_codegen_gcc/src/common.rs index d7df1347121a..0afc56b4494d 100644 --- a/compiler/rustc_codegen_gcc/src/common.rs +++ b/compiler/rustc_codegen_gcc/src/common.rs @@ -44,7 +44,7 @@ pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) -> context.new_array_constructor(None, typ, &elements) } -pub fn type_is_pointer(typ: Type) -> bool { +pub fn type_is_pointer(typ: Type<'_>) -> bool { typ.get_pointee().is_some() } From fb89ae4092deb4b3b90f0bdc2d5467b4029ba258 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 20 Dec 2022 15:39:32 -0600 Subject: [PATCH 254/321] Remove unused `check-stage2-T-arm-linux-androideabi-H-x86_64-unknown-linux-gnu` It's not used anywhere in CI, and it seems of questionable use. It was first added in 0e272de69f4a9c889e5f1a024a88b3e1f60cb6c5, which looks like it's just intended for CI, not as a user-facing feature. --- src/bootstrap/mk/Makefile.in | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/bootstrap/mk/Makefile.in b/src/bootstrap/mk/Makefile.in index 9a08a7be0f5f..5b2aba9aa2dc 100644 --- a/src/bootstrap/mk/Makefile.in +++ b/src/bootstrap/mk/Makefile.in @@ -57,11 +57,6 @@ tidy: prepare: $(Q)$(BOOTSTRAP) build --stage 2 nonexistent/path/to/trigger/cargo/metadata -check-stage2-T-arm-linux-androideabi-H-x86_64-unknown-linux-gnu: - $(Q)$(BOOTSTRAP) test --stage 2 --target arm-linux-androideabi -check-stage2-T-x86_64-unknown-linux-musl-H-x86_64-unknown-linux-gnu: - $(Q)$(BOOTSTRAP) test --stage 2 --target x86_64-unknown-linux-musl - TESTS_IN_2 := \ src/test/ui \ src/tools/linkchecker From f5e776c3f7afc2020d9828b593bac9db27290376 Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Tue, 20 Dec 2022 14:51:24 -0800 Subject: [PATCH 255/321] Refer to "Waker" rather than "RawWaker" in `drop` comment --- library/core/src/task/wake.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 0cff972df3a5..1b7578376b42 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -104,7 +104,7 @@ pub struct RawWakerVTable { /// pointer. wake_by_ref: unsafe fn(*const ()), - /// This function gets called when a [`RawWaker`] gets dropped. + /// This function gets called when a [`Waker`] gets dropped. /// /// The implementation of this function must make sure to release any /// resources that are associated with this instance of a [`RawWaker`] and @@ -151,7 +151,7 @@ impl RawWakerVTable { /// /// # `drop` /// - /// This function gets called when a [`RawWaker`] gets dropped. + /// This function gets called when a [`Waker`] gets dropped. /// /// The implementation of this function must make sure to release any /// resources that are associated with this instance of a [`RawWaker`] and From 04926e0534be77ed1b33602057d60d0bb14c1d66 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Tue, 22 Nov 2022 21:15:27 +0000 Subject: [PATCH 256/321] Switch `#[track_caller]` back to a no-op unless feature gate is enabled This patch fixes a regression, in which `#[track_caller]`, which was previously a no-op, was changed to actually turn on the behavior. This should instead only happen behind the `closure_track_caller` feature gate. Also, add a warning for the user to understand how their code will compile depending on the feature gate being turned on or not. Fixes #104588 --- .../locales/en-US/lint.ftl | 2 + compiler/rustc_lint/src/builtin.rs | 45 ++++++++++- .../issue-104588-no-op-panic-track-caller.rs | 78 +++++++++++++++++++ ...sue-104588-no-op-panic-track-caller.stderr | 12 +++ .../issue-104588-no-op-track-caller.rs | 9 +++ .../issue-104588-no-op-track-caller.stderr | 12 +++ 6 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs create mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr create mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs create mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index 7e28f22c0ba8..3980f9a2a7a5 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -350,6 +350,8 @@ lint_builtin_mutable_transmutes = lint_builtin_unstable_features = unstable feature +lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled + lint_builtin_unreachable_pub = unreachable `pub` {$what} .suggestion = consider restricting its visibility .help = or consider exporting it for use by other crates diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index cd19e65b6fc3..d7d91a12e658 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -25,6 +25,7 @@ use crate::{ types::{transparent_newtype_field, CItemKind}, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext, }; +use hir::IsAsync; use rustc_ast::attr; use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::visit::{FnCtxt, FnKind}; @@ -40,7 +41,10 @@ use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, Gate use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID}; -use rustc_hir::{ForeignItemKind, GenericParamKind, HirId, Node, PatKind, PredicateOrigin}; +use rustc_hir::intravisit::FnKind as HirFnKind; +use rustc_hir::{ + Body, FnDecl, ForeignItemKind, GenericParamKind, HirId, Node, PatKind, PredicateOrigin, +}; use rustc_index::vec::Idx; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::layout::{LayoutError, LayoutOf}; @@ -1370,6 +1374,45 @@ impl<'tcx> LateLintPass<'tcx> for UnstableFeatures { } } +declare_lint! { + /// `#[track_caller]` is a no-op without corresponding feature flag + UNGATED_ASYNC_FN_TRACK_CALLER, + Warn, + "enabling track_caller on an async fn is a no-op unless the closure_track_caller feature is enabled" +} + +declare_lint_pass!( + /// Explains corresponding feature flag must be enabled for the `#[track_caller] attribute to + /// do anything + UngatedAsyncFnTrackCaller => [UNGATED_ASYNC_FN_TRACK_CALLER] +); + +impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller { + fn check_fn( + &mut self, + cx: &LateContext<'_>, + fn_kind: HirFnKind<'_>, + _: &'tcx FnDecl<'_>, + _: &'tcx Body<'_>, + span: Span, + hir_id: HirId, + ) { + if let HirFnKind::ItemFn(_, _, _) = fn_kind && fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller { + // Now, check if the function has the `#[track_caller]` attribute + let attrs = cx.tcx.hir().attrs(hir_id); + let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller)); + if let Some(attr) = maybe_track_caller { + cx.struct_span_lint( + UNGATED_ASYNC_FN_TRACK_CALLER, + span.with_hi(attr.span.hi()), + fluent::lint_ungated_async_fn_track_caller, + |lint| lint, + ); + } + } + } +} + declare_lint! { /// The `unreachable_pub` lint triggers for `pub` items not reachable from /// the crate root. diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs b/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs new file mode 100644 index 000000000000..5ef40408e269 --- /dev/null +++ b/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs @@ -0,0 +1,78 @@ +// run-pass +// edition:2021 +// needs-unwind + + +use std::future::Future; +use std::panic; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll, Wake}; +use std::thread::{self, Thread}; + +/// A waker that wakes up the current thread when called. +struct ThreadWaker(Thread); + +impl Wake for ThreadWaker { + fn wake(self: Arc) { + self.0.unpark(); + } +} + +/// Run a future to completion on the current thread. +fn block_on(fut: impl Future) -> T { + // Pin the future so it can be polled. + let mut fut = Box::pin(fut); + + // Create a new context to be passed to the future. + let t = thread::current(); + let waker = Arc::new(ThreadWaker(t)).into(); + let mut cx = Context::from_waker(&waker); + + // Run the future to completion. + loop { + match fut.as_mut().poll(&mut cx) { + Poll::Ready(res) => return res, + Poll::Pending => thread::park(), + } + } +} + +async fn bar() { + panic!() +} + +async fn foo() { + bar().await +} + +#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled [ungated_async_fn_track_caller] +async fn bar_track_caller() { + panic!() +} + +async fn foo_track_caller() { + bar_track_caller().await +} + +fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 { + let loc = Arc::new(Mutex::new(None)); + + let hook = panic::take_hook(); + { + let loc = loc.clone(); + panic::set_hook(Box::new(move |info| { + *loc.lock().unwrap() = info.location().map(|loc| loc.line()) + })); + } + panic::catch_unwind(f).unwrap_err(); + panic::set_hook(hook); + let x = loc.lock().unwrap().unwrap(); + x +} + +fn main() { + assert_eq!(panicked_at(|| block_on(foo())), 41); + // Since the `closure_track_caller` feature is not enabled, the + // `track_caller annotation does nothing. + assert_eq!(panicked_at(|| block_on(foo_track_caller())), 50); +} diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr b/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr new file mode 100644 index 000000000000..5bfd9ed8490f --- /dev/null +++ b/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr @@ -0,0 +1,12 @@ +warning: `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled + --> $DIR/issue-104588-no-op-panic-track-caller.rs:48:16 + | +LL | #[track_caller] + | ________________^ +LL | | async fn bar_track_caller() { + | |_ + | + = note: `#[warn(ungated_async_fn_track_caller)]` on by default + +warning: 1 warning emitted + diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs new file mode 100644 index 000000000000..146d3c9ec53d --- /dev/null +++ b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs @@ -0,0 +1,9 @@ +// check-pass +// edition:2021 + +#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled +async fn foo() {} + +fn main() { + foo(); +} diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr new file mode 100644 index 000000000000..bf66cc9ea90f --- /dev/null +++ b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr @@ -0,0 +1,12 @@ +warning: `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled + --> $DIR/issue-104588-no-op-track-caller.rs:4:16 + | +LL | #[track_caller] + | ________________^ +LL | | async fn foo() {} + | |_ + | + = note: `#[warn(ungated_async_fn_track_caller)]` on by default + +warning: 1 warning emitted + From dc2c4ce578bd3bee824e1b30789cfa6cb777e059 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Thu, 24 Nov 2022 03:39:47 +0000 Subject: [PATCH 257/321] Update code based on PR comments This patch does the following: - Refactor some repeated lines into a single one - Split the `ungated_async_fn_caller` lint into multiple lines, and make one of those lines only print out on nightly - Use test revisions instead of copying an existing test --- .../locales/en-US/lint.ftl | 3 +- compiler/rustc_lint/src/builtin.rs | 12 ++- .../issue-104588-no-op-panic-track-caller.rs | 78 ------------------- ...sue-104588-no-op-panic-track-caller.stderr | 12 --- .../issue-104588-no-op-track-caller.rs | 2 +- .../issue-104588-no-op-track-caller.stderr | 12 +-- .../panic-track-caller.nofeat.stderr | 14 ++++ .../track-caller/panic-track-caller.rs | 24 ++++-- 8 files changed, 51 insertions(+), 106 deletions(-) delete mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs delete mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr create mode 100644 src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index 3980f9a2a7a5..a36a511bd4aa 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -350,7 +350,8 @@ lint_builtin_mutable_transmutes = lint_builtin_unstable_features = unstable feature -lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled +lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op + .suggestion = enable this feature lint_builtin_unreachable_pub = unreachable `pub` {$what} .suggestion = consider restricting its visibility diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index d7d91a12e658..e90572cb2388 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1404,10 +1404,16 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller { if let Some(attr) = maybe_track_caller { cx.struct_span_lint( UNGATED_ASYNC_FN_TRACK_CALLER, - span.with_hi(attr.span.hi()), + attr.span, fluent::lint_ungated_async_fn_track_caller, - |lint| lint, - ); + |lint| { + lint.span_label(span, "this function will not propagate the caller location"); + if cx.tcx.sess.is_nightly_build() { + lint.span_suggestion(attr.span, fluent::suggestion, "#[closure_track_caller]", Applicability::MachineApplicable); + } + lint + } + ); } } } diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs b/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs deleted file mode 100644 index 5ef40408e269..000000000000 --- a/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.rs +++ /dev/null @@ -1,78 +0,0 @@ -// run-pass -// edition:2021 -// needs-unwind - - -use std::future::Future; -use std::panic; -use std::sync::{Arc, Mutex}; -use std::task::{Context, Poll, Wake}; -use std::thread::{self, Thread}; - -/// A waker that wakes up the current thread when called. -struct ThreadWaker(Thread); - -impl Wake for ThreadWaker { - fn wake(self: Arc) { - self.0.unpark(); - } -} - -/// Run a future to completion on the current thread. -fn block_on(fut: impl Future) -> T { - // Pin the future so it can be polled. - let mut fut = Box::pin(fut); - - // Create a new context to be passed to the future. - let t = thread::current(); - let waker = Arc::new(ThreadWaker(t)).into(); - let mut cx = Context::from_waker(&waker); - - // Run the future to completion. - loop { - match fut.as_mut().poll(&mut cx) { - Poll::Ready(res) => return res, - Poll::Pending => thread::park(), - } - } -} - -async fn bar() { - panic!() -} - -async fn foo() { - bar().await -} - -#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled [ungated_async_fn_track_caller] -async fn bar_track_caller() { - panic!() -} - -async fn foo_track_caller() { - bar_track_caller().await -} - -fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 { - let loc = Arc::new(Mutex::new(None)); - - let hook = panic::take_hook(); - { - let loc = loc.clone(); - panic::set_hook(Box::new(move |info| { - *loc.lock().unwrap() = info.location().map(|loc| loc.line()) - })); - } - panic::catch_unwind(f).unwrap_err(); - panic::set_hook(hook); - let x = loc.lock().unwrap().unwrap(); - x -} - -fn main() { - assert_eq!(panicked_at(|| block_on(foo())), 41); - // Since the `closure_track_caller` feature is not enabled, the - // `track_caller annotation does nothing. - assert_eq!(panicked_at(|| block_on(foo_track_caller())), 50); -} diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr b/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr deleted file mode 100644 index 5bfd9ed8490f..000000000000 --- a/src/test/ui/async-await/track-caller/issue-104588-no-op-panic-track-caller.stderr +++ /dev/null @@ -1,12 +0,0 @@ -warning: `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled - --> $DIR/issue-104588-no-op-panic-track-caller.rs:48:16 - | -LL | #[track_caller] - | ________________^ -LL | | async fn bar_track_caller() { - | |_ - | - = note: `#[warn(ungated_async_fn_track_caller)]` on by default - -warning: 1 warning emitted - diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs index 146d3c9ec53d..6443e14296da 100644 --- a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs +++ b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs @@ -1,7 +1,7 @@ // check-pass // edition:2021 -#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled +#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op async fn foo() {} fn main() { diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr index bf66cc9ea90f..bd39c9d092ca 100644 --- a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr +++ b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr @@ -1,10 +1,10 @@ -warning: `#[track_caller]` on async functions is a no-op, unless the `closure_track_caller` feature is enabled - --> $DIR/issue-104588-no-op-track-caller.rs:4:16 +warning: `#[track_caller]` on async functions is a no-op + --> $DIR/issue-104588-no-op-track-caller.rs:4:1 | -LL | #[track_caller] - | ________________^ -LL | | async fn foo() {} - | |_ +LL | #[track_caller] + | ^^^^^^^^^^^^^^^ help: enable this feature: `#[closure_track_caller]` +LL | async fn foo() {} + | ----------------- this function will not propagate the caller location | = note: `#[warn(ungated_async_fn_track_caller)]` on by default diff --git a/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr new file mode 100644 index 000000000000..e0201f2238dd --- /dev/null +++ b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr @@ -0,0 +1,14 @@ +warning: `#[track_caller]` on async functions is a no-op + --> $DIR/panic-track-caller.rs:50:1 + | +LL | #[track_caller] + | ^^^^^^^^^^^^^^^ help: enable this feature: `#[closure_track_caller]` +LL | / async fn bar_track_caller() { +LL | | panic!() +LL | | } + | |_- this function will not propagate the caller location + | + = note: `#[warn(ungated_async_fn_track_caller)]` on by default + +warning: 1 warning emitted + diff --git a/src/test/ui/async-await/track-caller/panic-track-caller.rs b/src/test/ui/async-await/track-caller/panic-track-caller.rs index 066cf97628fa..ee37e64be4fa 100644 --- a/src/test/ui/async-await/track-caller/panic-track-caller.rs +++ b/src/test/ui/async-await/track-caller/panic-track-caller.rs @@ -1,7 +1,9 @@ // run-pass // edition:2021 +// revisions: feat nofeat // needs-unwind -#![feature(closure_track_caller, async_closure, stmt_expr_attributes)] +#![feature(async_closure, stmt_expr_attributes)] +#![cfg_attr(feat, feature(closure_track_caller))] use std::future::Future; use std::panic; @@ -45,7 +47,7 @@ async fn foo() { bar().await } -#[track_caller] +#[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op async fn bar_track_caller() { panic!() } @@ -91,8 +93,20 @@ fn panicked_at(f: impl FnOnce() + panic::UnwindSafe) -> u32 { } fn main() { - assert_eq!(panicked_at(|| block_on(foo())), 41); - assert_eq!(panicked_at(|| block_on(foo_track_caller())), 54); - assert_eq!(panicked_at(|| block_on(foo_assoc())), 67); + assert_eq!(panicked_at(|| block_on(foo())), 43); + + #[cfg(feat)] + assert_eq!(panicked_at(|| block_on(foo_track_caller())), 56); + #[cfg(nofeat)] + assert_eq!(panicked_at(|| block_on(foo_track_caller())), 52); + + #[cfg(feat)] + assert_eq!(panicked_at(|| block_on(foo_assoc())), 69); + #[cfg(nofeat)] + assert_eq!(panicked_at(|| block_on(foo_assoc())), 64); + + #[cfg(feat)] + assert_eq!(panicked_at(|| block_on(foo_closure())), 76); + #[cfg(feat)] assert_eq!(panicked_at(|| block_on(foo_closure())), 74); } From e28a07a0a157b63dc11e9f590484d5332866623a Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Thu, 24 Nov 2022 03:48:27 +0000 Subject: [PATCH 258/321] update wording of lint --- compiler/rustc_error_messages/locales/en-US/lint.ftl | 2 +- compiler/rustc_lint/src/builtin.rs | 2 +- .../track-caller/issue-104588-no-op-track-caller.stderr | 2 +- .../async-await/track-caller/panic-track-caller.nofeat.stderr | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index a36a511bd4aa..0d5507835a9e 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -351,7 +351,7 @@ lint_builtin_mutable_transmutes = lint_builtin_unstable_features = unstable feature lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op - .suggestion = enable this feature + .suggestion = enable this unstable feature lint_builtin_unreachable_pub = unreachable `pub` {$what} .suggestion = consider restricting its visibility diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index e90572cb2388..c353f7425165 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1409,7 +1409,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller { |lint| { lint.span_label(span, "this function will not propagate the caller location"); if cx.tcx.sess.is_nightly_build() { - lint.span_suggestion(attr.span, fluent::suggestion, "#[closure_track_caller]", Applicability::MachineApplicable); + lint.span_suggestion(attr.span, fluent::suggestion, "closure_track_caller", Applicability::MachineApplicable); } lint } diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr index bd39c9d092ca..b41077a0b924 100644 --- a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr +++ b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr @@ -2,7 +2,7 @@ warning: `#[track_caller]` on async functions is a no-op --> $DIR/issue-104588-no-op-track-caller.rs:4:1 | LL | #[track_caller] - | ^^^^^^^^^^^^^^^ help: enable this feature: `#[closure_track_caller]` + | ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller` LL | async fn foo() {} | ----------------- this function will not propagate the caller location | diff --git a/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr index e0201f2238dd..71d4b00cc6e8 100644 --- a/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr +++ b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr @@ -2,7 +2,7 @@ warning: `#[track_caller]` on async functions is a no-op --> $DIR/panic-track-caller.rs:50:1 | LL | #[track_caller] - | ^^^^^^^^^^^^^^^ help: enable this feature: `#[closure_track_caller]` + | ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller` LL | / async fn bar_track_caller() { LL | | panic!() LL | | } From 2d060034f0fd5f5780c7fd41046901b8e0cdf7e8 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Wed, 7 Dec 2022 20:13:22 +0000 Subject: [PATCH 259/321] Update track_caller logic/lint after rebase --- compiler/rustc_ast_lowering/src/expr.rs | 53 ++++++++++--------- compiler/rustc_lint/src/builtin.rs | 16 ++++-- compiler/rustc_lint/src/lib.rs | 1 + .../panic-track-caller.nofeat.stderr | 12 ++++- .../track-caller/panic-track-caller.rs | 7 +-- 5 files changed, 52 insertions(+), 37 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index a3f5c18f2e75..a58c74dbc333 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -656,34 +656,35 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ExprKind::Closure(c) }; - let track_caller = self - .attrs - .get(&outer_hir_id.local_id) - .map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))); - let hir_id = self.lower_node_id(closure_node_id); - if track_caller { - let unstable_span = self.mark_span_with_reason( - DesugaringKind::Async, - span, - self.allow_gen_future.clone(), - ); - self.lower_attrs( - hir_id, - &[Attribute { - kind: AttrKind::Normal(ptr::P(NormalAttr { - item: AttrItem { - path: Path::from_ident(Ident::new(sym::track_caller, span)), - args: AttrArgs::Empty, + let unstable_span = self.mark_span_with_reason( + DesugaringKind::Async, + span, + self.allow_gen_future.clone(), + ); + if self.tcx.features().closure_track_caller { + let track_caller = self + .attrs + .get(&outer_hir_id.local_id) + .map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))); + if track_caller { + self.lower_attrs( + hir_id, + &[Attribute { + kind: AttrKind::Normal(ptr::P(NormalAttr { + item: AttrItem { + path: Path::from_ident(Ident::new(sym::track_caller, span)), + args: AttrArgs::Empty, + tokens: None, + }, tokens: None, - }, - tokens: None, - })), - id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(), - style: AttrStyle::Outer, - span: unstable_span, - }], - ); + })), + id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(), + style: AttrStyle::Outer, + span: unstable_span, + }], + ); + } } let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) }; diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index c353f7425165..6d3e33f2b629 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1397,7 +1397,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller { span: Span, hir_id: HirId, ) { - if let HirFnKind::ItemFn(_, _, _) = fn_kind && fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller { + if fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller { // Now, check if the function has the `#[track_caller]` attribute let attrs = cx.tcx.hir().attrs(hir_id); let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller)); @@ -1407,12 +1407,20 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller { attr.span, fluent::lint_ungated_async_fn_track_caller, |lint| { - lint.span_label(span, "this function will not propagate the caller location"); + lint.span_label( + span, + "this function will not propagate the caller location", + ); if cx.tcx.sess.is_nightly_build() { - lint.span_suggestion(attr.span, fluent::suggestion, "closure_track_caller", Applicability::MachineApplicable); + lint.span_suggestion( + attr.span, + fluent::suggestion, + "closure_track_caller", + Applicability::MachineApplicable, + ); } lint - } + }, ); } } diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 11022eb80ea5..1275d6f223c7 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -219,6 +219,7 @@ late_lint_methods!( // May Depend on constants elsewhere UnusedBrokenConst: UnusedBrokenConst, UnstableFeatures: UnstableFeatures, + UngatedAsyncFnTrackCaller: UngatedAsyncFnTrackCaller, ArrayIntoIter: ArrayIntoIter::default(), DropTraitConstraints: DropTraitConstraints, TemporaryCStringAsPtr: TemporaryCStringAsPtr, diff --git a/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr index 71d4b00cc6e8..f313658c4464 100644 --- a/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr +++ b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr @@ -10,5 +10,15 @@ LL | | } | = note: `#[warn(ungated_async_fn_track_caller)]` on by default -warning: 1 warning emitted +warning: `#[track_caller]` on async functions is a no-op + --> $DIR/panic-track-caller.rs:62:5 + | +LL | #[track_caller] + | ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller` +LL | / async fn bar_assoc() { +LL | | panic!(); +LL | | } + | |_____- this function will not propagate the caller location + +warning: 2 warnings emitted diff --git a/src/test/ui/async-await/track-caller/panic-track-caller.rs b/src/test/ui/async-await/track-caller/panic-track-caller.rs index ee37e64be4fa..02077db7c629 100644 --- a/src/test/ui/async-await/track-caller/panic-track-caller.rs +++ b/src/test/ui/async-await/track-caller/panic-track-caller.rs @@ -59,7 +59,7 @@ async fn foo_track_caller() { struct Foo; impl Foo { - #[track_caller] + #[track_caller] //[nofeat]~ WARN `#[track_caller]` on async functions is a no-op async fn bar_assoc() { panic!(); } @@ -104,9 +104,4 @@ fn main() { assert_eq!(panicked_at(|| block_on(foo_assoc())), 69); #[cfg(nofeat)] assert_eq!(panicked_at(|| block_on(foo_assoc())), 64); - - #[cfg(feat)] - assert_eq!(panicked_at(|| block_on(foo_closure())), 76); - #[cfg(feat)] - assert_eq!(panicked_at(|| block_on(foo_closure())), 74); } From f702e89f9de9ffb651ffb3b6b8dde5d9ffd1e4d5 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Wed, 7 Dec 2022 20:26:56 +0000 Subject: [PATCH 260/321] Add lint doc comment --- compiler/rustc_lint/src/builtin.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 6d3e33f2b629..7c16bc1eade6 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1375,7 +1375,26 @@ impl<'tcx> LateLintPass<'tcx> for UnstableFeatures { } declare_lint! { - /// `#[track_caller]` is a no-op without corresponding feature flag + /// The `ungated_async_fn_track_caller` lint warns when the + /// `#[track_caller]` attribute is used on an async function, method, or + /// closure, without enabling the corresponding unstable feature flag. + /// + /// ### Example + /// + /// ```rust + /// #[track_caller] + /// async fn foo() {} + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// The attribute must be used in conjunction with the + /// [`closure_track_caller` feature flag]. Otherwise, the `#[track_caller]` + /// annotation will function as as no-op. + /// + /// [`closure_track_caller` feature flag]: https://doc.rust-lang.org/beta/unstable-book/language-features/closure-track-caller.html UNGATED_ASYNC_FN_TRACK_CALLER, Warn, "enabling track_caller on an async fn is a no-op unless the closure_track_caller feature is enabled" From 9650a4168f2d5f4edb3338a484ab5c8d1a4b0e52 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Wed, 21 Dec 2022 03:13:28 +0000 Subject: [PATCH 261/321] Improve code based on feedback. This patch improves the readability of some of the code by using if-let-chains. Also, make use of the `add_feature_diagnostics` function. --- compiler/rustc_ast_lowering/src/expr.rs | 42 +++++++++---------- .../locales/en-US/lint.ftl | 2 +- compiler/rustc_lint/src/builtin.rs | 26 +++++------- .../issue-104588-no-op-track-caller.rs | 9 ---- .../issue-104588-no-op-track-caller.stderr | 12 ------ .../panic-track-caller.nofeat.stderr | 9 +++- 6 files changed, 38 insertions(+), 62 deletions(-) delete mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs delete mode 100644 src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index a58c74dbc333..805050e681b0 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -662,29 +662,27 @@ impl<'hir> LoweringContext<'_, 'hir> { span, self.allow_gen_future.clone(), ); - if self.tcx.features().closure_track_caller { - let track_caller = self - .attrs - .get(&outer_hir_id.local_id) - .map_or(false, |attrs| attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))); - if track_caller { - self.lower_attrs( - hir_id, - &[Attribute { - kind: AttrKind::Normal(ptr::P(NormalAttr { - item: AttrItem { - path: Path::from_ident(Ident::new(sym::track_caller, span)), - args: AttrArgs::Empty, - tokens: None, - }, + + if self.tcx.features().closure_track_caller + && let Some(attrs) = self.attrs.get(&outer_hir_id.local_id) + && attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)) + { + self.lower_attrs( + hir_id, + &[Attribute { + kind: AttrKind::Normal(ptr::P(NormalAttr { + item: AttrItem { + path: Path::from_ident(Ident::new(sym::track_caller, span)), + args: AttrArgs::Empty, tokens: None, - })), - id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(), - style: AttrStyle::Outer, - span: unstable_span, - }], - ); - } + }, + tokens: None, + })), + id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(), + style: AttrStyle::Outer, + span: unstable_span, + }], + ); } let generator = hir::Expr { hir_id, kind: generator_kind, span: self.lower_span(span) }; diff --git a/compiler/rustc_error_messages/locales/en-US/lint.ftl b/compiler/rustc_error_messages/locales/en-US/lint.ftl index 0d5507835a9e..2eb409a5ddd5 100644 --- a/compiler/rustc_error_messages/locales/en-US/lint.ftl +++ b/compiler/rustc_error_messages/locales/en-US/lint.ftl @@ -351,7 +351,7 @@ lint_builtin_mutable_transmutes = lint_builtin_unstable_features = unstable feature lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op - .suggestion = enable this unstable feature + .label = this function will not propagate the caller location lint_builtin_unreachable_pub = unreachable `pub` {$what} .suggestion = consider restricting its visibility diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 7c16bc1eade6..d6de6e70ead8 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1416,33 +1416,27 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller { span: Span, hir_id: HirId, ) { - if fn_kind.asyncness() == IsAsync::Async && !cx.tcx.features().closure_track_caller { + if fn_kind.asyncness() == IsAsync::Async + && !cx.tcx.features().closure_track_caller + && let attrs = cx.tcx.hir().attrs(hir_id) // Now, check if the function has the `#[track_caller]` attribute - let attrs = cx.tcx.hir().attrs(hir_id); - let maybe_track_caller = attrs.iter().find(|attr| attr.has_name(sym::track_caller)); - if let Some(attr) = maybe_track_caller { + && let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller)) + { cx.struct_span_lint( UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, fluent::lint_ungated_async_fn_track_caller, |lint| { - lint.span_label( - span, - "this function will not propagate the caller location", + lint.span_label(span, fluent::label); + rustc_session::parse::add_feature_diagnostics( + lint, + &cx.tcx.sess.parse_sess, + sym::closure_track_caller, ); - if cx.tcx.sess.is_nightly_build() { - lint.span_suggestion( - attr.span, - fluent::suggestion, - "closure_track_caller", - Applicability::MachineApplicable, - ); - } lint }, ); } - } } } diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs deleted file mode 100644 index 6443e14296da..000000000000 --- a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.rs +++ /dev/null @@ -1,9 +0,0 @@ -// check-pass -// edition:2021 - -#[track_caller] //~ WARN `#[track_caller]` on async functions is a no-op -async fn foo() {} - -fn main() { - foo(); -} diff --git a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr b/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr deleted file mode 100644 index b41077a0b924..000000000000 --- a/src/test/ui/async-await/track-caller/issue-104588-no-op-track-caller.stderr +++ /dev/null @@ -1,12 +0,0 @@ -warning: `#[track_caller]` on async functions is a no-op - --> $DIR/issue-104588-no-op-track-caller.rs:4:1 - | -LL | #[track_caller] - | ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller` -LL | async fn foo() {} - | ----------------- this function will not propagate the caller location - | - = note: `#[warn(ungated_async_fn_track_caller)]` on by default - -warning: 1 warning emitted - diff --git a/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr index f313658c4464..51ea225f4cbd 100644 --- a/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr +++ b/src/test/ui/async-await/track-caller/panic-track-caller.nofeat.stderr @@ -2,23 +2,28 @@ warning: `#[track_caller]` on async functions is a no-op --> $DIR/panic-track-caller.rs:50:1 | LL | #[track_caller] - | ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller` + | ^^^^^^^^^^^^^^^ LL | / async fn bar_track_caller() { LL | | panic!() LL | | } | |_- this function will not propagate the caller location | + = note: see issue #87417 for more information + = help: add `#![feature(closure_track_caller)]` to the crate attributes to enable = note: `#[warn(ungated_async_fn_track_caller)]` on by default warning: `#[track_caller]` on async functions is a no-op --> $DIR/panic-track-caller.rs:62:5 | LL | #[track_caller] - | ^^^^^^^^^^^^^^^ help: enable this unstable feature: `closure_track_caller` + | ^^^^^^^^^^^^^^^ LL | / async fn bar_assoc() { LL | | panic!(); LL | | } | |_____- this function will not propagate the caller location + | + = note: see issue #87417 for more information + = help: add `#![feature(closure_track_caller)]` to the crate attributes to enable warning: 2 warnings emitted From 2f5334dff2cf7be2f1cc33565c55925b58950694 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 21 Dec 2022 05:03:11 +0000 Subject: [PATCH 262/321] forgot a return in drop tracking handle_uninhabited_return --- .../drop_ranges/cfg_build.rs | 1 + ...=> unresolved-ct-var.drop_tracking.stderr} | 22 +++--- src/test/ui/generator/unresolved-ct-var.rs | 2 + .../generator/unresolved-ct-var.stock.stderr | 78 +++++++++++++++++++ 4 files changed, 92 insertions(+), 11 deletions(-) rename src/test/ui/generator/{unresolved-ct-var.stderr => unresolved-ct-var.drop_tracking.stderr} (88%) create mode 100644 src/test/ui/generator/unresolved-ct-var.stock.stderr diff --git a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs index fd8ea1ad7bff..16806fdba4fb 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs @@ -233,6 +233,7 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> { self.tcx() .sess .delay_span_bug(expr.span, format!("could not resolve infer vars in `{ty}`")); + return; } let ty = self.tcx().erase_regions(ty); let m = self.tcx().parent_module(expr.hir_id).to_def_id(); diff --git a/src/test/ui/generator/unresolved-ct-var.stderr b/src/test/ui/generator/unresolved-ct-var.drop_tracking.stderr similarity index 88% rename from src/test/ui/generator/unresolved-ct-var.stderr rename to src/test/ui/generator/unresolved-ct-var.drop_tracking.stderr index fdf00dfad7ab..a328c43765db 100644 --- a/src/test/ui/generator/unresolved-ct-var.stderr +++ b/src/test/ui/generator/unresolved-ct-var.drop_tracking.stderr @@ -1,5 +1,5 @@ error[E0277]: `[(); _]` is not a future - --> $DIR/unresolved-ct-var.rs:6:44 + --> $DIR/unresolved-ct-var.rs:8:44 | LL | let s = std::array::from_fn(|_| ()).await; | ---------------------------^^^^^^ @@ -13,61 +13,61 @@ LL | let s = std::array::from_fn(|_| ()).await; = note: required for `[(); _]` to implement `IntoFuture` error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:6:17 + --> $DIR/unresolved-ct-var.rs:8:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:6:44 + --> $DIR/unresolved-ct-var.rs:8:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:6:17 + --> $DIR/unresolved-ct-var.rs:8:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:6:44 + --> $DIR/unresolved-ct-var.rs:8:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:6:17 + --> $DIR/unresolved-ct-var.rs:8:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:6:44 + --> $DIR/unresolved-ct-var.rs:8:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:6:17 + --> $DIR/unresolved-ct-var.rs:8:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:6:44 + --> $DIR/unresolved-ct-var.rs:8:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:6:17 + --> $DIR/unresolved-ct-var.rs:8:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:6:44 + --> $DIR/unresolved-ct-var.rs:8:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ diff --git a/src/test/ui/generator/unresolved-ct-var.rs b/src/test/ui/generator/unresolved-ct-var.rs index 0a1570fc2395..6720f4658872 100644 --- a/src/test/ui/generator/unresolved-ct-var.rs +++ b/src/test/ui/generator/unresolved-ct-var.rs @@ -1,5 +1,7 @@ // incremental // edition:2021 +// revisions: drop_tracking stock +//[drop_tracking] compile-flags: -Zdrop-tracking fn main() { let _ = async { diff --git a/src/test/ui/generator/unresolved-ct-var.stock.stderr b/src/test/ui/generator/unresolved-ct-var.stock.stderr new file mode 100644 index 000000000000..a328c43765db --- /dev/null +++ b/src/test/ui/generator/unresolved-ct-var.stock.stderr @@ -0,0 +1,78 @@ +error[E0277]: `[(); _]` is not a future + --> $DIR/unresolved-ct-var.rs:8:44 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ---------------------------^^^^^^ + | | | + | | `[(); _]` is not a future + | | help: remove the `.await` + | this call returns `[(); _]` + | + = help: the trait `Future` is not implemented for `[(); _]` + = note: [(); _] must be a future or must implement `IntoFuture` to be awaited + = note: required for `[(); _]` to implement `IntoFuture` + +error[E0698]: type inside `async` block must be known in this context + --> $DIR/unresolved-ct-var.rs:8:17 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` + | +note: the type is part of the `async` block because of this `await` + --> $DIR/unresolved-ct-var.rs:8:44 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^ + +error[E0698]: type inside `async` block must be known in this context + --> $DIR/unresolved-ct-var.rs:8:17 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` + | +note: the type is part of the `async` block because of this `await` + --> $DIR/unresolved-ct-var.rs:8:44 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^ + +error[E0698]: type inside `async` block must be known in this context + --> $DIR/unresolved-ct-var.rs:8:17 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` + | +note: the type is part of the `async` block because of this `await` + --> $DIR/unresolved-ct-var.rs:8:44 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^ + +error[E0698]: type inside `async` block must be known in this context + --> $DIR/unresolved-ct-var.rs:8:17 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` + | +note: the type is part of the `async` block because of this `await` + --> $DIR/unresolved-ct-var.rs:8:44 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^ + +error[E0698]: type inside `async` block must be known in this context + --> $DIR/unresolved-ct-var.rs:8:17 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` + | +note: the type is part of the `async` block because of this `await` + --> $DIR/unresolved-ct-var.rs:8:44 + | +LL | let s = std::array::from_fn(|_| ()).await; + | ^^^^^^ + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0277, E0698. +For more information about an error, try `rustc --explain E0277`. From c8ebff6aeea5bc9242269e5f072442db62be734b Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 21 Dec 2022 05:07:50 +0000 Subject: [PATCH 263/321] Remove some unnecessary try_map_bound --- .../src/traits/error_reporting/suggestions.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 036e8f6d47bb..60f67491364d 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3185,14 +3185,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx) && let Some(pred) = predicates.predicates.get(*idx) { - if let Ok(trait_pred) = pred.kind().try_map_bound(|pred| match pred { - ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => Ok(trait_pred), - _ => Err(()), - }) - && let Ok(trait_predicate) = predicate.kind().try_map_bound(|pred| match pred { - ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => Ok(trait_pred), - _ => Err(()), - }) + if let Some(trait_pred) = pred.to_opt_poly_trait_pred() + && let Some(trait_predicate) = predicate.to_opt_poly_trait_pred() { let mut c = CollectAllMismatches { infcx: self.infcx, From 0c09e2bf5afb535aa7e84bbcd8cce509a0e015b1 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 21 Dec 2022 05:36:26 +0000 Subject: [PATCH 264/321] Substitute things correctly --- .../src/traits/error_reporting/suggestions.rs | 3 ++- src/test/ui/iterators/invalid-iterator-chain.stderr | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 60f67491364d..1edc9357e12c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3182,7 +3182,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let mut type_diffs = vec![]; if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref() - && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx) + && let Some(node_substs) = typeck_results.node_substs_opt(call_hir_id) + && let predicates = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_substs) && let Some(pred) = predicates.predicates.get(*idx) { if let Some(trait_pred) = pred.to_opt_poly_trait_pred() diff --git a/src/test/ui/iterators/invalid-iterator-chain.stderr b/src/test/ui/iterators/invalid-iterator-chain.stderr index d76a4bfb7b3e..f3dceca7e413 100644 --- a/src/test/ui/iterators/invalid-iterator-chain.stderr +++ b/src/test/ui/iterators/invalid-iterator-chain.stderr @@ -52,14 +52,14 @@ LL | .sum::(), > note: the method call chain might not have had the expected associated types - --> $DIR/invalid-iterator-chain.rs:20:14 + --> $DIR/invalid-iterator-chain.rs:25:14 | LL | vec![0, 1] | ---------- this expression has type `Vec<{integer}>` LL | .iter() | ------ `Iterator::Item` is `&{integer}` here LL | .map(|x| x * 2) - | ^^^^^^^^^^^^^^ `Iterator::Item` changed to `{integer}` here + | -------------- `Iterator::Item` changed to `{integer}` here LL | .map(|x| x as f64) | ----------------- `Iterator::Item` changed to `f64` here LL | .map(|x| x as i64) @@ -84,14 +84,14 @@ LL | .sum::(), > note: the method call chain might not have had the expected associated types - --> $DIR/invalid-iterator-chain.rs:32:14 + --> $DIR/invalid-iterator-chain.rs:33:14 | LL | vec![0, 1] | ---------- this expression has type `Vec<{integer}>` LL | .iter() | ------ `Iterator::Item` is `&{integer}` here LL | .map(|x| x * 2) - | ^^^^^^^^^^^^^^ `Iterator::Item` changed to `{integer}` here + | -------------- `Iterator::Item` changed to `{integer}` here LL | .map(|x| x as f64) | ^^^^^^^^^^^^^^^^^ `Iterator::Item` changed to `f64` here LL | .filter(|x| *x > 0.0) From 978dd2e3b804ebbb39684aaabcea5c0a39bd59d9 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 21 Dec 2022 05:53:55 +0000 Subject: [PATCH 265/321] Rename things to be a bit clearer --- .../src/traits/error_reporting/suggestions.rs | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 1edc9357e12c..ba1ef3248667 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3147,7 +3147,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { err: &mut Diagnostic, parent_code: &ObligationCauseCode<'tcx>, param_env: ty::ParamEnv<'tcx>, - predicate: ty::Predicate<'tcx>, + failed_pred: ty::Predicate<'tcx>, call_hir_id: HirId, ) { let tcx = self.tcx; @@ -3183,31 +3183,28 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref() && let Some(node_substs) = typeck_results.node_substs_opt(call_hir_id) - && let predicates = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_substs) - && let Some(pred) = predicates.predicates.get(*idx) + && let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_substs) + && let Some(where_pred) = where_clauses.predicates.get(*idx) { - if let Some(trait_pred) = pred.to_opt_poly_trait_pred() - && let Some(trait_predicate) = predicate.to_opt_poly_trait_pred() + if let Some(where_pred) = where_pred.to_opt_poly_trait_pred() + && let Some(failed_pred) = failed_pred.to_opt_poly_trait_pred() { let mut c = CollectAllMismatches { infcx: self.infcx, param_env, errors: vec![], }; - if let Ok(_) = c.relate(trait_pred, trait_predicate) { + if let Ok(_) = c.relate(where_pred, failed_pred) { type_diffs = c.errors; } - } else if let ty::PredicateKind::Clause( - ty::Clause::Projection(proj) - ) = pred.kind().skip_binder() - && let ty::PredicateKind::Clause( - ty::Clause::Projection(projection) - ) = predicate.kind().skip_binder() + } else if let Some(where_pred) = where_pred.to_opt_poly_projection_pred() + && let Some(failed_pred) = failed_pred.to_opt_poly_projection_pred() + && let Some(found) = failed_pred.skip_binder().term.ty() { type_diffs = vec![ Sorts(ty::error::ExpectedFound { - expected: self.tcx.mk_ty(ty::Alias(ty::Projection, proj.projection_ty)), - found: projection.term.ty().unwrap(), + expected: self.tcx.mk_ty(ty::Alias(ty::Projection, where_pred.skip_binder().projection_ty)), + found, }), ]; } From c6ef53477ee68ace77bead81439971a71669b22f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 21 Dec 2022 05:42:48 +0000 Subject: [PATCH 266/321] CollectAllMismatches relation should respect int/float infer vars --- .../traits/error_reporting/method_chain.rs | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs index cb373d657721..27c207528c73 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs @@ -14,21 +14,27 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> { fn tag(&self) -> &'static str { "CollectAllMismatches" } + fn tcx(&self) -> TyCtxt<'tcx> { self.infcx.tcx } + fn intercrate(&self) -> bool { false } + fn param_env(&self) -> ty::ParamEnv<'tcx> { self.param_env } + fn a_is_expected(&self) -> bool { true - } // irrelevant + } + fn mark_ambiguous(&mut self) { bug!() } + fn relate_with_variance>( &mut self, _: ty::Variance, @@ -38,6 +44,7 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> { ) -> RelateResult<'tcx, T> { self.relate(a, b) } + fn regions( &mut self, a: ty::Region<'tcx>, @@ -45,15 +52,20 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> { ) -> RelateResult<'tcx, ty::Region<'tcx>> { Ok(a) } + fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { - if a == b || matches!(a.kind(), ty::Infer(_)) || matches!(b.kind(), ty::Infer(_)) { - return Ok(a); - } - relate::super_relate_tys(self, a, b).or_else(|e| { - self.errors.push(e); - Ok(a) + self.infcx.probe(|_| { + if a.is_ty_infer() || b.is_ty_infer() { + Ok(a) + } else { + self.infcx.super_combine_tys(self, a, b).or_else(|e| { + self.errors.push(e); + Ok(a) + }) + } }) } + fn consts( &mut self, a: ty::Const<'tcx>, @@ -64,6 +76,7 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> { } relate::super_relate_consts(self, a, b) // could do something similar here for constants! } + fn binders>( &mut self, a: ty::Binder<'tcx, T>, From 85a9d85435ec60b840f54a03460412c261517d12 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 21 Dec 2022 05:49:12 +0000 Subject: [PATCH 267/321] Don't call typeck if we have no typeck results This has a 10000000% chance of us causing a cycle if we're not careful --- .../src/traits/error_reporting/suggestions.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index ba1ef3248667..eb5e1d5ad5c1 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -335,7 +335,7 @@ pub trait TypeErrCtxtExt<'tcx> { err: &mut Diagnostic, trait_pred: ty::PolyTraitPredicate<'tcx>, ); - fn function_argument_obligation( + fn note_function_argument_obligation( &self, arg_hir_id: HirId, err: &mut Diagnostic, @@ -2909,7 +2909,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { ref parent_code, .. } => { - self.function_argument_obligation( + self.note_function_argument_obligation( arg_hir_id, err, parent_code, @@ -3141,7 +3141,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { ); } } - fn function_argument_obligation( + fn note_function_argument_obligation( &self, arg_hir_id: HirId, err: &mut Diagnostic, @@ -3152,12 +3152,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { ) { let tcx = self.tcx; let hir = tcx.hir(); - if let Some(Node::Expr(expr)) = hir.find(arg_hir_id) { - let parent_id = hir.get_parent_item(arg_hir_id); - let typeck_results: &TypeckResults<'tcx> = match &self.typeck_results { - Some(t) if t.hir_owner == parent_id => t, - _ => self.tcx.typeck(parent_id.def_id), - }; + if let Some(Node::Expr(expr)) = hir.find(arg_hir_id) + && let Some(typeck_results) = &self.typeck_results + { if let hir::Expr { kind: hir::ExprKind::Block(..), .. } = expr { let expr = expr.peel_blocks(); let ty = typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error()); @@ -3219,9 +3216,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { // If the expression we're calling on is a binding, we want to point at the // `let` when talking about the type. Otherwise we'll point at every part // of the method chain with the type. - self.point_at_chain(binding_expr, typeck_results, type_diffs, param_env, err); + self.point_at_chain(binding_expr, &typeck_results, type_diffs, param_env, err); } else { - self.point_at_chain(expr, typeck_results, type_diffs, param_env, err); + self.point_at_chain(expr, &typeck_results, type_diffs, param_env, err); } } let call_node = hir.find(call_hir_id); From 5538c92837ef66369ea80faf0badd1c85f1512c9 Mon Sep 17 00:00:00 2001 From: Ikko Ashimine Date: Wed, 21 Dec 2022 15:34:21 +0900 Subject: [PATCH 268/321] Fix typo in reading_half_a_pointer.rs gurantee -> guarantee --- src/tools/miri/tests/fail/reading_half_a_pointer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/miri/tests/fail/reading_half_a_pointer.rs b/src/tools/miri/tests/fail/reading_half_a_pointer.rs index 2d6691326247..7dd98eab7852 100644 --- a/src/tools/miri/tests/fail/reading_half_a_pointer.rs +++ b/src/tools/miri/tests/fail/reading_half_a_pointer.rs @@ -7,7 +7,7 @@ struct Data { ptr: &'static i32, } -// But we need to gurantee some alignment +// But we need to guarantee some alignment struct Wrapper { align: u64, data: Data, From 6f21ba4a06b7fa17113b7029c4aa7805db16aa32 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 21 Dec 2022 11:06:20 +0100 Subject: [PATCH 269/321] less specific wording --- library/core/src/intrinsics.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs index ed58a7f17993..a521905a9e73 100644 --- a/library/core/src/intrinsics.rs +++ b/library/core/src/intrinsics.rs @@ -959,8 +959,7 @@ extern "rust-intrinsic" { #[rustc_safe_intrinsic] pub fn assert_zero_valid(); - /// A guard for `std::mem::uninitialized`. Checks whether a repeated bit pattern `0x01` - /// is legal for `T`: This will statically either panic, or do nothing. + /// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing. /// /// This intrinsic does not have a stable counterpart. #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")] From 1286d982781c4d5bb009e3e8189ccbe0bcbf0f4b Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 20 Dec 2022 16:02:15 +0100 Subject: [PATCH 270/321] Don't explicitly set C++ standard for lld LLVM does this itself since 606cb8548a1b7763e0c8489c5efe66803a7ede72, and 14 is no longer the correct standard when building lld 16, causing build failures. --- src/bootstrap/native.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index f6c453ebe107..8052bde6024e 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -886,10 +886,6 @@ impl Step for Lld { ); } - // Explicitly set C++ standard, because upstream doesn't do so - // for standalone builds. - cfg.define("CMAKE_CXX_STANDARD", "14"); - cfg.build(); t!(File::create(&done_stamp)); From 32a31d8acaf8521712a49f53feabd7d98844ef73 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Wed, 21 Dec 2022 21:10:30 +0900 Subject: [PATCH 271/321] Add regression test for #96530 Signed-off-by: Yuki Okushi --- src/test/ui/typeck/issue-96530.rs | 20 ++++++++++++++++++++ src/test/ui/typeck/issue-96530.stderr | 9 +++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/test/ui/typeck/issue-96530.rs create mode 100644 src/test/ui/typeck/issue-96530.stderr diff --git a/src/test/ui/typeck/issue-96530.rs b/src/test/ui/typeck/issue-96530.rs new file mode 100644 index 000000000000..4ab93ab4938f --- /dev/null +++ b/src/test/ui/typeck/issue-96530.rs @@ -0,0 +1,20 @@ +struct Person { + first_name: String, + age: u32, +} + +fn first_woman(man: &Person) -> Person { + Person { + first_name: "Eve".to_string(), + ..man.clone() //~ ERROR: mismatched types + } +} + +fn main() { + let adam = Person { + first_name: "Adam".to_string(), + age: 0, + }; + + let eve = first_woman(&adam); +} diff --git a/src/test/ui/typeck/issue-96530.stderr b/src/test/ui/typeck/issue-96530.stderr new file mode 100644 index 000000000000..4b4568b1de9b --- /dev/null +++ b/src/test/ui/typeck/issue-96530.stderr @@ -0,0 +1,9 @@ +error[E0308]: mismatched types + --> $DIR/issue-96530.rs:9:11 + | +LL | ..man.clone() + | ^^^^^^^^^^^ expected struct `Person`, found `&Person` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From b60281f472b22d1787d5e8fcea1a7fb4a061b956 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Wed, 21 Dec 2022 13:41:28 +0100 Subject: [PATCH 272/321] Test that async blocks are UnwindSafe This was a regression from the reverted #105250 which is now covered by a test. --- .../ui/async-await/async-is-unwindsafe.rs | 30 +++++++++++++++ .../ui/async-await/async-is-unwindsafe.stderr | 38 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/test/ui/async-await/async-is-unwindsafe.rs create mode 100644 src/test/ui/async-await/async-is-unwindsafe.stderr diff --git a/src/test/ui/async-await/async-is-unwindsafe.rs b/src/test/ui/async-await/async-is-unwindsafe.rs new file mode 100644 index 000000000000..56ed2847292d --- /dev/null +++ b/src/test/ui/async-await/async-is-unwindsafe.rs @@ -0,0 +1,30 @@ +// edition:2018 + +fn is_unwindsafe(_: impl std::panic::UnwindSafe) {} + +fn main() { + // A normal future created by an async block takes a `&mut Context<'_>` argument. + // That should not leak through to the whole async block. + is_unwindsafe(async { + async {}.await; // this needs an inner await point + }); + + is_unwindsafe(async { + //~^ ERROR the type `&mut Context<'_>` may not be safely transferred across an unwind boundary + use std::ptr::null; + use std::task::{Context, RawWaker, RawWakerVTable, Waker}; + let waker = unsafe { + Waker::from_raw(RawWaker::new( + null(), + &RawWakerVTable::new(|_| todo!(), |_| todo!(), |_| todo!(), |_| todo!()), + )) + }; + let mut cx = Context::from_waker(&waker); + let cx_ref = &mut cx; + + async {}.await; // this needs an inner await point + + // in this case, `&mut Context<'_>` is *truly* alive across an await point + drop(cx_ref); + }); +} diff --git a/src/test/ui/async-await/async-is-unwindsafe.stderr b/src/test/ui/async-await/async-is-unwindsafe.stderr new file mode 100644 index 000000000000..d6404b30e74f --- /dev/null +++ b/src/test/ui/async-await/async-is-unwindsafe.stderr @@ -0,0 +1,38 @@ +error[E0277]: the type `&mut Context<'_>` may not be safely transferred across an unwind boundary + --> $DIR/async-is-unwindsafe.rs:12:19 + | +LL | is_unwindsafe(async { + | ___________________^ +LL | | +LL | | use std::ptr::null; +LL | | use std::task::{Context, RawWaker, RawWakerVTable, Waker}; +... | +LL | | drop(cx_ref); +LL | | }); + | | ^ + | | | + | |_____`&mut Context<'_>` may not be safely transferred across an unwind boundary + | within this `[async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6]` + | + = help: within `[async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6]`, the trait `UnwindSafe` is not implemented for `&mut Context<'_>` + = note: `UnwindSafe` is implemented for `&std::task::Context<'_>`, but not for `&mut std::task::Context<'_>` +note: future does not implement `UnwindSafe` as this value is used across an await + --> $DIR/async-is-unwindsafe.rs:25:17 + | +LL | let cx_ref = &mut cx; + | ------ has type `&mut Context<'_>` which does not implement `UnwindSafe` +LL | +LL | async {}.await; // this needs an inner await point + | ^^^^^^ await occurs here, with `cx_ref` maybe used later +... +LL | }); + | - `cx_ref` is later dropped here +note: required by a bound in `is_unwindsafe` + --> $DIR/async-is-unwindsafe.rs:3:26 + | +LL | fn is_unwindsafe(_: impl std::panic::UnwindSafe) {} + | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_unwindsafe` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. From 9c9fa567f8ca5bf43b481407ab78508652970594 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Wed, 21 Dec 2022 14:19:04 +0000 Subject: [PATCH 273/321] codegen tests: adapt patterns to also work with v0 symbol mangling No functional changes intended. These tests were failing under `new-symbol-mangling = true`. This adapts the patterns to work in this case. --- src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs | 3 ++- src/test/codegen/unwind-and-panic-abort.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs b/src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs index 8447bbeb1ed2..34fd401f9e42 100644 --- a/src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs +++ b/src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs @@ -9,7 +9,8 @@ // CHECK: @rust_item_that_can_unwind() unnamed_addr [[ATTR0:#[0-9]+]] #[no_mangle] pub unsafe extern "C-unwind" fn rust_item_that_can_unwind() { - // CHECK: call void @_ZN4core9panicking15panic_no_unwind + // Handle both legacy and v0 symbol mangling. + // CHECK: call void @{{.*core9panicking15panic_no_unwind}} may_unwind(); } diff --git a/src/test/codegen/unwind-and-panic-abort.rs b/src/test/codegen/unwind-and-panic-abort.rs index f238741e599f..b370191bf8c8 100644 --- a/src/test/codegen/unwind-and-panic-abort.rs +++ b/src/test/codegen/unwind-and-panic-abort.rs @@ -9,7 +9,8 @@ extern "C-unwind" { // CHECK: Function Attrs:{{.*}}nounwind // CHECK-NEXT: define{{.*}}void @foo -// CHECK: call void @_ZN4core9panicking15panic_no_unwind +// Handle both legacy and v0 symbol mangling. +// CHECK: call void @{{.*core9panicking15panic_no_unwind}} #[no_mangle] pub unsafe extern "C" fn foo() { bar(); From a9af75cdbc2f31fe0f68854d5b579616c4393839 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 21 Dec 2022 17:44:30 +0000 Subject: [PATCH 274/321] Give opaque types a better coherence error --- .../src/coherence/orphan.rs | 23 ++++++++++++------- .../ui/type-alias-impl-trait/coherence.stderr | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/coherence/orphan.rs b/compiler/rustc_hir_analysis/src/coherence/orphan.rs index cc5114dba5ef..c6d4aeefc80e 100644 --- a/compiler/rustc_hir_analysis/src/coherence/orphan.rs +++ b/compiler/rustc_hir_analysis/src/coherence/orphan.rs @@ -184,11 +184,19 @@ fn emit_orphan_check_error<'tcx>( ty::Adt(def, _) => tcx.mk_adt(*def, ty::List::empty()), _ => ty, }; - let this = "this".to_string(); - let (ty, postfix) = match &ty.kind() { - ty::Slice(_) => (this, " because slices are always foreign"), - ty::Array(..) => (this, " because arrays are always foreign"), - ty::Tuple(..) => (this, " because tuples are always foreign"), + let msg = |ty: &str, postfix: &str| { + format!("{ty} is not defined in the current crate{postfix}") + }; + let this = |name: &str| msg("this", &format!(" because {name} are always foreign")); + let msg = match &ty.kind() { + ty::Slice(_) => this("slices"), + ty::Array(..) => this("arrays"), + ty::Tuple(..) => this("tuples"), + ty::Alias(ty::Opaque, ..) => { + "type alias impl trait is treated as if it were foreign, \ + because its hidden type could be from a foreign crate" + .to_string() + } ty::RawPtr(ptr_ty) => { emit_newtype_suggestion_for_raw_ptr( full_impl_span, @@ -198,12 +206,11 @@ fn emit_orphan_check_error<'tcx>( &mut err, ); - (format!("`{}`", ty), " because raw pointers are always foreign") + msg(&format!("`{ty}`"), " because raw pointers are always foreign") } - _ => (format!("`{}`", ty), ""), + _ => msg(&format!("`{ty}`"), ""), }; - let msg = format!("{} is not defined in the current crate{}", ty, postfix); if is_target_ty { // Point at `D` in `impl for C in D` err.span_label(self_ty_span, &msg); diff --git a/src/test/ui/type-alias-impl-trait/coherence.stderr b/src/test/ui/type-alias-impl-trait/coherence.stderr index c923eb08ab31..00b0dbbb583f 100644 --- a/src/test/ui/type-alias-impl-trait/coherence.stderr +++ b/src/test/ui/type-alias-impl-trait/coherence.stderr @@ -4,7 +4,7 @@ error[E0117]: only traits defined in the current crate can be implemented for ar LL | impl foreign_crate::ForeignTrait for AliasOfForeignType {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------- | | | - | | `AliasOfForeignType` is not defined in the current crate + | | type alias impl trait is treated as if it were foreign, because its hidden type could be from a foreign crate | impl doesn't use only types from inside the current crate | = note: define and implement a trait or new type instead From 30fbfd5f05108d12f08711024db699e1b0632508 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Wed, 21 Dec 2022 17:27:45 +0100 Subject: [PATCH 275/321] Sort lint_groups in no_lint_suggestion The no_lint_suggestion routine passes a vector of lint group names to find_best_match_for_name. That routine depends on the sort order of its input vector, which matters in case multiple inputs are at the same Levenshtein distance to the target name. However, no_lint_suggestion currently just passes lint_groups.keys() as input vector - this is sorted in hash value order, which is not guaranteed to be stable, and in fact differs between big- and little-endian host platforms, causing test failures on s390x. To fix this, always sort the lint groups before using their names as input to find_best_match_for_name. In addition, deprecated lint groups should never be suggested, so filter those out. Fixes https://github.com/rust-lang/rust/issues/105379 --- compiler/rustc_lint/src/context.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 0417f375588c..3c9ad410663f 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -483,7 +483,16 @@ impl LintStore { return CheckLintNameResult::NoLint(Some(Symbol::intern(&name_lower))); } // ...if not, search for lints with a similar name - let groups = self.lint_groups.keys().copied().map(Symbol::intern); + // Note: find_best_match_for_name depends on the sort order of its input vector. + // To ensure deterministic output, sort elements of the lint_groups hash map. + // Also, never suggest deprecated lint groups. + let mut groups: Vec<_> = self + .lint_groups + .iter() + .filter_map(|(k, LintGroup { depr, .. })| if depr.is_none() { Some(k) } else { None }) + .collect(); + groups.sort(); + let groups = groups.iter().map(|k| Symbol::intern(k)); let lints = self.lints.iter().map(|l| Symbol::intern(&l.name_lower())); let names: Vec = groups.chain(lints).collect(); let suggestion = find_best_match_for_name(&names, Symbol::intern(&name_lower), None); From cb2c7bb833f439c224457442c83c3dfcba1709d6 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Wed, 21 Dec 2022 10:30:38 -0800 Subject: [PATCH 276/321] Clarify that raw retags are not permitted in Mir --- compiler/rustc_const_eval/src/transform/validate.rs | 9 ++++++--- compiler/rustc_middle/src/mir/syntax.rs | 7 +++++-- .../src/build/custom/parse/instruction.rs | 3 --- library/core/src/intrinsics/mir.rs | 1 - .../building/custom/references.immut_ref.built.after.mir | 7 +++---- .../building/custom/references.mut_ref.built.after.mir | 7 +++---- src/test/mir-opt/building/custom/references.rs | 2 -- 7 files changed, 17 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_const_eval/src/transform/validate.rs b/compiler/rustc_const_eval/src/transform/validate.rs index bb897b95b2c5..eb247a4cebe4 100644 --- a/compiler/rustc_const_eval/src/transform/validate.rs +++ b/compiler/rustc_const_eval/src/transform/validate.rs @@ -9,8 +9,8 @@ use rustc_middle::mir::visit::{PlaceContext, Visitor}; use rustc_middle::mir::{ traversal, AggregateKind, BasicBlock, BinOp, Body, BorrowKind, CastKind, CopyNonOverlapping, Local, Location, MirPass, MirPhase, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef, - ProjectionElem, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind, Terminator, - TerminatorKind, UnOp, START_BLOCK, + ProjectionElem, RetagKind, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind, + Terminator, TerminatorKind, UnOp, START_BLOCK, }; use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitable}; use rustc_mir_dataflow::impls::MaybeStorageLive; @@ -667,10 +667,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> { self.fail(location, "`Deinit`is not allowed until deaggregation"); } } - StatementKind::Retag(_, _) => { + StatementKind::Retag(kind, _) => { // FIXME(JakobDegen) The validator should check that `self.mir_phase < // DropsLowered`. However, this causes ICEs with generation of drop shims, which // seem to fail to set their `MirPhase` correctly. + if *kind == RetagKind::Raw { + self.fail(location, "explicit `RetagKind::Raw` is forbidden"); + } } StatementKind::StorageLive(..) | StatementKind::StorageDead(..) diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 99e59c770d75..bbb8148f5bc1 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -320,8 +320,11 @@ pub enum StatementKind<'tcx> { /// for /// more details. /// - /// For code that is not specific to stacked borrows, you should consider retags to read - /// and modify the place in an opaque way. + /// For code that is not specific to stacked borrows, you should consider retags to read and + /// modify the place in an opaque way. + /// + /// Explicit `RetagKind::Raw` is not permitted - it is implicit as a part of + /// `Rvalue::AddressOf`. Retag(RetagKind, Box>), /// Encodes a user's type ascription. These need to be preserved diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs index 7c39a93a8eb9..dca4906c07de 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs @@ -15,9 +15,6 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { @call("mir_retag", args) => { Ok(StatementKind::Retag(RetagKind::Default, Box::new(self.parse_place(args[0])?))) }, - @call("mir_retag_raw", args) => { - Ok(StatementKind::Retag(RetagKind::Raw, Box::new(self.parse_place(args[0])?))) - }, @call("mir_set_discriminant", args) => { let place = self.parse_place(args[0])?; let var = self.parse_integer_literal(args[1])? as u32; diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs index e08a15571fcb..9e7099ddfd1e 100644 --- a/library/core/src/intrinsics/mir.rs +++ b/library/core/src/intrinsics/mir.rs @@ -259,7 +259,6 @@ define!("mir_drop", fn Drop(place: T, goto: BasicBlock)); define!("mir_drop_and_replace", fn DropAndReplace(place: T, value: T, goto: BasicBlock)); define!("mir_call", fn Call(place: T, goto: BasicBlock, call: T)); define!("mir_retag", fn Retag(place: T)); -define!("mir_retag_raw", fn RetagRaw(place: T)); define!("mir_move", fn Move(place: T) -> T); define!("mir_static", fn Static(s: T) -> &'static T); define!("mir_static_mut", fn StaticMut(s: T) -> *mut T); diff --git a/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir b/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir index 4d38d45c0f47..f5ee11262354 100644 --- a/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir +++ b/src/test/mir-opt/building/custom/references.immut_ref.built.after.mir @@ -6,9 +6,8 @@ fn immut_ref(_1: &i32) -> &i32 { bb0: { _2 = &raw const (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:29 - Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24 - _0 = &(*_2); // scope 0 at $DIR/references.rs:+7:13: +7:23 - Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23 - return; // scope 0 at $DIR/references.rs:+9:13: +9:21 + _0 = &(*_2); // scope 0 at $DIR/references.rs:+6:13: +6:23 + Retag(_0); // scope 0 at $DIR/references.rs:+7:13: +7:23 + return; // scope 0 at $DIR/references.rs:+8:13: +8:21 } } diff --git a/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir b/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir index 01bc8a9cd358..8e2ffc33b1a0 100644 --- a/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir +++ b/src/test/mir-opt/building/custom/references.mut_ref.built.after.mir @@ -6,9 +6,8 @@ fn mut_ref(_1: &mut i32) -> &mut i32 { bb0: { _2 = &raw mut (*_1); // scope 0 at $DIR/references.rs:+5:13: +5:33 - Retag([raw] _2); // scope 0 at $DIR/references.rs:+6:13: +6:24 - _0 = &mut (*_2); // scope 0 at $DIR/references.rs:+7:13: +7:26 - Retag(_0); // scope 0 at $DIR/references.rs:+8:13: +8:23 - return; // scope 0 at $DIR/references.rs:+9:13: +9:21 + _0 = &mut (*_2); // scope 0 at $DIR/references.rs:+6:13: +6:26 + Retag(_0); // scope 0 at $DIR/references.rs:+7:13: +7:23 + return; // scope 0 at $DIR/references.rs:+8:13: +8:21 } } diff --git a/src/test/mir-opt/building/custom/references.rs b/src/test/mir-opt/building/custom/references.rs index c93f6ec624b3..a1c896de04cf 100644 --- a/src/test/mir-opt/building/custom/references.rs +++ b/src/test/mir-opt/building/custom/references.rs @@ -12,7 +12,6 @@ pub fn mut_ref(x: &mut i32) -> &mut i32 { { t = addr_of_mut!(*x); - RetagRaw(t); RET = &mut *t; Retag(RET); Return() @@ -28,7 +27,6 @@ pub fn immut_ref(x: &i32) -> &i32 { { t = addr_of!(*x); - RetagRaw(t); RET = & *t; Retag(RET); Return() From 34a4e5cc1219da042697cf3adb984eeec2f8cb80 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 21 Dec 2022 20:21:31 +0100 Subject: [PATCH 277/321] Add comment explaining what the scrape-examples-toggle.goml GUI test is about --- src/test/rustdoc-gui/scrape-examples-toggle.goml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/rustdoc-gui/scrape-examples-toggle.goml b/src/test/rustdoc-gui/scrape-examples-toggle.goml index ee720afb788f..a0b696ee3363 100644 --- a/src/test/rustdoc-gui/scrape-examples-toggle.goml +++ b/src/test/rustdoc-gui/scrape-examples-toggle.goml @@ -1,3 +1,4 @@ +// This tests checks that the "scraped examples" toggle is working as expected. goto: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html" // Clicking "More examples..." will open additional examples From 7c4c62047504aff1e17d4b2686116fb4de04797c Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Wed, 21 Dec 2022 11:46:13 -0800 Subject: [PATCH 278/321] Forbid `RetagKind::TwoPhase` as well --- compiler/rustc_const_eval/src/transform/validate.rs | 4 ++-- compiler/rustc_middle/src/mir/syntax.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_const_eval/src/transform/validate.rs b/compiler/rustc_const_eval/src/transform/validate.rs index eb247a4cebe4..94e1b95a0eb3 100644 --- a/compiler/rustc_const_eval/src/transform/validate.rs +++ b/compiler/rustc_const_eval/src/transform/validate.rs @@ -671,8 +671,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> { // FIXME(JakobDegen) The validator should check that `self.mir_phase < // DropsLowered`. However, this causes ICEs with generation of drop shims, which // seem to fail to set their `MirPhase` correctly. - if *kind == RetagKind::Raw { - self.fail(location, "explicit `RetagKind::Raw` is forbidden"); + if *kind == RetagKind::Raw || *kind == RetagKind::TwoPhase { + self.fail(location, format!("explicit `{:?}` is forbidden", kind)); } } StatementKind::StorageLive(..) diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index bbb8148f5bc1..bb03359b138f 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -323,8 +323,7 @@ pub enum StatementKind<'tcx> { /// For code that is not specific to stacked borrows, you should consider retags to read and /// modify the place in an opaque way. /// - /// Explicit `RetagKind::Raw` is not permitted - it is implicit as a part of - /// `Rvalue::AddressOf`. + /// Only `RetagKind::Default` and `RetagKind::FnEntry` are permitted. Retag(RetagKind, Box>), /// Encodes a user's type ascription. These need to be preserved From 5dfa6a8922245d0587c4acf3c9b004e5875066bf Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 21 Dec 2022 13:28:42 -0700 Subject: [PATCH 279/321] rustdoc: simplify link anchor to section expand JS --- src/librustdoc/html/static/js/main.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 3f97e4e2e39f..60e4e7492240 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -813,16 +813,14 @@ function loadCss(cssUrl) { hideSidebar(); }); - onEachLazy(document.getElementsByTagName("a"), el => { + onEachLazy(document.querySelectorAll("a[href^='#']"), el => { // For clicks on internal links ( tags with a hash property), we expand the section we're // jumping to *before* jumping there. We can't do this in onHashChange, because it changes // the height of the document so we wind up scrolled to the wrong place. - if (el.hash) { - el.addEventListener("click", () => { - expandSection(el.hash.slice(1)); - hideSidebar(); - }); - } + el.addEventListener("click", () => { + expandSection(el.hash.slice(1)); + hideSidebar(); + }); }); onEachLazy(document.querySelectorAll(".rustdoc-toggle > summary:not(.hideme)"), el => { From 102040ce76d588c0605e29577cdf8307acb4bb10 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Fri, 4 Nov 2022 00:05:15 -0700 Subject: [PATCH 280/321] Retag as FnEntry on `drop_in_place` --- compiler/rustc_mir_transform/src/shim.rs | 28 +++++++++++++++- ...place.Test.SimplifyCfg-make_shim.after.mir | 7 ++-- .../drop_in_place_protector.rs | 27 +++++++++++++++ .../drop_in_place_protector.stderr | 33 +++++++++++++++++++ .../stacked_borrows/drop_in_place_retag.rs | 19 +++++++++++ .../drop_in_place_retag.stderr | 29 ++++++++++++++++ .../fail/unaligned_pointers/drop_in_place.rs | 25 ++++++++++++++ .../unaligned_pointers/drop_in_place.stderr | 20 +++++++++++ .../miri/tests/pass/drop_in_place_null.rs | 7 ++++ 9 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs create mode 100644 src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr create mode 100644 src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs create mode 100644 src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr create mode 100644 src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.rs create mode 100644 src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr create mode 100644 src/tools/miri/tests/pass/drop_in_place_null.rs diff --git a/compiler/rustc_mir_transform/src/shim.rs b/compiler/rustc_mir_transform/src/shim.rs index f8b55c862875..aa89ff003810 100644 --- a/compiler/rustc_mir_transform/src/shim.rs +++ b/compiler/rustc_mir_transform/src/shim.rs @@ -176,7 +176,33 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option>) if ty.is_some() { // The first argument (index 0), but add 1 for the return value. - let dropee_ptr = Place::from(Local::new(1 + 0)); + let mut dropee_ptr = Place::from(Local::new(1 + 0)); + if tcx.sess.opts.unstable_opts.mir_emit_retag { + // We want to treat the function argument as if it was passed by `&mut`. As such, we + // generate + // ``` + // temp = &mut *arg; + // Retag(temp, FnEntry) + // ``` + // It's important that we do this first, before anything that depends on `dropee_ptr` + // has been put into the body. + let reborrow = Rvalue::Ref( + tcx.lifetimes.re_erased, + BorrowKind::Mut { allow_two_phase_borrow: false }, + tcx.mk_place_deref(dropee_ptr), + ); + let ref_ty = reborrow.ty(body.local_decls(), tcx); + dropee_ptr = body.local_decls.push(LocalDecl::new(ref_ty, span)).into(); + let new_statements = [ + StatementKind::Assign(Box::new((dropee_ptr, reborrow))), + StatementKind::Retag(RetagKind::FnEntry, Box::new(dropee_ptr)), + ]; + for s in new_statements { + body.basic_blocks_mut()[START_BLOCK] + .statements + .push(Statement { source_info, kind: s }); + } + } let patch = { let param_env = tcx.param_env_reveal_all_normalized(def_id); let mut elaborator = diff --git a/src/test/mir-opt/retag.core.ptr-drop_in_place.Test.SimplifyCfg-make_shim.after.mir b/src/test/mir-opt/retag.core.ptr-drop_in_place.Test.SimplifyCfg-make_shim.after.mir index 14f297e948be..f495f147be3d 100644 --- a/src/test/mir-opt/retag.core.ptr-drop_in_place.Test.SimplifyCfg-make_shim.after.mir +++ b/src/test/mir-opt/retag.core.ptr-drop_in_place.Test.SimplifyCfg-make_shim.after.mir @@ -3,11 +3,14 @@ fn std::ptr::drop_in_place(_1: *mut Test) -> () { let mut _0: (); // return place in scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 let mut _2: &mut Test; // in scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 - let mut _3: (); // in scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 + let mut _3: &mut Test; // in scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 + let mut _4: (); // in scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 bb0: { _2 = &mut (*_1); // scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 - _3 = ::drop(move _2) -> bb1; // scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 + Retag([fn entry] _2); // scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 + _3 = &mut (*_2); // scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 + _4 = ::drop(move _3) -> bb1; // scope 0 at $SRC_DIR/core/src/ptr/mod.rs:+0:1: +0:56 // mir::Constant // + span: $SRC_DIR/core/src/ptr/mod.rs:LL:COL // + literal: Const { ty: for<'a> fn(&'a mut Test) {::drop}, val: Value() } diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs new file mode 100644 index 000000000000..883361d05fc4 --- /dev/null +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs @@ -0,0 +1,27 @@ +//! Test that drop_in_place retags the entire place, +//! invalidating all aliases to it. + +// A zero-sized drop type -- the retagging of `fn drop` itself won't +// do anything (since it is zero-sized); we are entirely relying on the retagging +// in `drop_in_place` here. +#[repr(transparent)] +struct HasDrop; +impl Drop for HasDrop { + fn drop(&mut self) { + unsafe { + let _val = *P; + //~^ ERROR: /not granting access .* because that would remove .* which is protected/ + } + } +} + +static mut P: *mut u8 = core::ptr::null_mut(); + +fn main() { + unsafe { + let mut x = (HasDrop, 0u8); + let x = core::ptr::addr_of_mut!(x); + P = x.cast(); + core::ptr::drop_in_place(x); + } +} diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr new file mode 100644 index 000000000000..6d122ade4772 --- /dev/null +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr @@ -0,0 +1,33 @@ +error: Undefined Behavior: not granting access to tag because that would remove [Unique for ] which is protected because it is an argument of call ID + --> $DIR/drop_in_place_protector.rs:LL:CC + | +LL | let _val = *P; + | ^^ not granting access to tag because that would remove [Unique for ] which is protected because it is an argument of call ID + | + = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental + = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information +help: was created by a SharedReadWrite retag at offsets [0x0..0x1] + --> $DIR/drop_in_place_protector.rs:LL:CC + | +LL | let x = core::ptr::addr_of_mut!(x); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: is this argument + --> $DIR/drop_in_place_protector.rs:LL:CC + | +LL | core::ptr::drop_in_place(x); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: BACKTRACE: + = note: inside `::drop` at $DIR/drop_in_place_protector.rs:LL:CC + = note: inside `std::ptr::drop_in_place:: - shim(Some(HasDrop))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC + = note: inside `std::ptr::drop_in_place::<(HasDrop, u8)> - shim(Some((HasDrop, u8)))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC +note: inside `main` at $DIR/drop_in_place_protector.rs:LL:CC + --> $DIR/drop_in_place_protector.rs:LL:CC + | +LL | core::ptr::drop_in_place(x); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `core::ptr::addr_of_mut` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs new file mode 100644 index 000000000000..4cb870f1d97d --- /dev/null +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs @@ -0,0 +1,19 @@ +//! Test that drop_in_place mutably retags the entire place, +//! ensuring it is writeable + +//@error-pattern: /retag .* for Unique permission .* only grants SharedReadOnly permission/ + +#[repr(transparent)] +struct HasDrop; + +impl Drop for HasDrop { + fn drop(&mut self) {} +} + +fn main() { + unsafe { + let x = (0u8, HasDrop); + let x = core::ptr::addr_of!(x); + core::ptr::drop_in_place(x.cast_mut()); + } +} diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr new file mode 100644 index 000000000000..022b27d69b2c --- /dev/null +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr @@ -0,0 +1,29 @@ +error: Undefined Behavior: trying to retag from for Unique permission at ALLOC[0x0], but that tag only grants SharedReadOnly permission for this location + --> RUSTLIB/core/src/ptr/mod.rs:LL:CC + | +LL | pub unsafe fn drop_in_place(to_drop: *mut T) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | trying to retag from for Unique permission at ALLOC[0x0], but that tag only grants SharedReadOnly permission for this location + | this error occurs as part of FnEntry retag at ALLOC[0x0..0x1] + | + = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental + = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information +help: was created by a SharedReadOnly retag at offsets [0x0..0x1] + --> $DIR/drop_in_place_retag.rs:LL:CC + | +LL | let x = core::ptr::addr_of!(x); + | ^^^^^^^^^^^^^^^^^^^^^^ + = note: BACKTRACE: + = note: inside `std::ptr::drop_in_place::<(u8, HasDrop)> - shim(Some((u8, HasDrop)))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC +note: inside `main` at $DIR/drop_in_place_retag.rs:LL:CC + --> $DIR/drop_in_place_retag.rs:LL:CC + | +LL | core::ptr::drop_in_place(x.cast_mut()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `core::ptr::addr_of` (in Nightly builds, run with -Z macro-backtrace for more info) + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + diff --git a/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.rs b/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.rs new file mode 100644 index 000000000000..cf3a558bb994 --- /dev/null +++ b/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.rs @@ -0,0 +1,25 @@ +#[repr(transparent)] +struct HasDrop(u8); + +impl Drop for HasDrop { + fn drop(&mut self) {} +} + +#[repr(C, align(2))] +struct PartialDrop { + a: HasDrop, + b: u8, +} + +//@error-pattern: /alignment 2 is required/ +fn main() { + unsafe { + // Create an unaligned pointer + let mut x = [0_u16; 2]; + let p = core::ptr::addr_of_mut!(x).cast::(); + let p = p.add(1); + let p = p.cast::(); + + core::ptr::drop_in_place(p); + } +} diff --git a/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr b/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr new file mode 100644 index 000000000000..1081964987c0 --- /dev/null +++ b/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr @@ -0,0 +1,20 @@ +error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required + --> RUSTLIB/core/src/ptr/mod.rs:LL:CC + | +LL | pub unsafe fn drop_in_place(to_drop: *mut T) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required + | + = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior + = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information + = note: BACKTRACE: + = note: inside `std::ptr::drop_in_place:: - shim(Some(PartialDrop))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC +note: inside `main` at $DIR/drop_in_place.rs:LL:CC + --> $DIR/drop_in_place.rs:LL:CC + | +LL | core::ptr::drop_in_place(p); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + diff --git a/src/tools/miri/tests/pass/drop_in_place_null.rs b/src/tools/miri/tests/pass/drop_in_place_null.rs new file mode 100644 index 000000000000..aab070b69757 --- /dev/null +++ b/src/tools/miri/tests/pass/drop_in_place_null.rs @@ -0,0 +1,7 @@ +// Make sure that dropping types with no drop glue is DB even for invalid pointers. + +fn main() { + unsafe { + core::ptr::drop_in_place::(core::ptr::null_mut()); + } +} From c359ab0b5d0dbaab1a995d7aa9fb9c6512bb837b Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Tue, 6 Dec 2022 01:18:24 -0800 Subject: [PATCH 281/321] Retag argument to `drop_in_place` unconditionally --- compiler/rustc_mir_transform/src/shim.rs | 57 ++++++++++--------- .../drop_in_place_protector.rs | 2 +- .../drop_in_place_protector.stderr | 4 +- .../stacked_borrows/drop_in_place_retag.rs | 9 +-- .../drop_in_place_retag.stderr | 2 +- .../miri/tests/pass/drop_in_place_null.rs | 7 --- 6 files changed, 34 insertions(+), 47 deletions(-) delete mode 100644 src/tools/miri/tests/pass/drop_in_place_null.rs diff --git a/compiler/rustc_mir_transform/src/shim.rs b/compiler/rustc_mir_transform/src/shim.rs index aa89ff003810..dace540fa29d 100644 --- a/compiler/rustc_mir_transform/src/shim.rs +++ b/compiler/rustc_mir_transform/src/shim.rs @@ -174,35 +174,36 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option>) let mut body = new_body(source, blocks, local_decls_for_sig(&sig, span), sig.inputs().len(), span); - if ty.is_some() { - // The first argument (index 0), but add 1 for the return value. - let mut dropee_ptr = Place::from(Local::new(1 + 0)); - if tcx.sess.opts.unstable_opts.mir_emit_retag { - // We want to treat the function argument as if it was passed by `&mut`. As such, we - // generate - // ``` - // temp = &mut *arg; - // Retag(temp, FnEntry) - // ``` - // It's important that we do this first, before anything that depends on `dropee_ptr` - // has been put into the body. - let reborrow = Rvalue::Ref( - tcx.lifetimes.re_erased, - BorrowKind::Mut { allow_two_phase_borrow: false }, - tcx.mk_place_deref(dropee_ptr), - ); - let ref_ty = reborrow.ty(body.local_decls(), tcx); - dropee_ptr = body.local_decls.push(LocalDecl::new(ref_ty, span)).into(); - let new_statements = [ - StatementKind::Assign(Box::new((dropee_ptr, reborrow))), - StatementKind::Retag(RetagKind::FnEntry, Box::new(dropee_ptr)), - ]; - for s in new_statements { - body.basic_blocks_mut()[START_BLOCK] - .statements - .push(Statement { source_info, kind: s }); - } + // The first argument (index 0), but add 1 for the return value. + let mut dropee_ptr = Place::from(Local::new(1 + 0)); + if tcx.sess.opts.unstable_opts.mir_emit_retag { + // We want to treat the function argument as if it was passed by `&mut`. As such, we + // generate + // ``` + // temp = &mut *arg; + // Retag(temp, FnEntry) + // ``` + // It's important that we do this first, before anything that depends on `dropee_ptr` + // has been put into the body. + let reborrow = Rvalue::Ref( + tcx.lifetimes.re_erased, + BorrowKind::Mut { allow_two_phase_borrow: false }, + tcx.mk_place_deref(dropee_ptr), + ); + let ref_ty = reborrow.ty(body.local_decls(), tcx); + dropee_ptr = body.local_decls.push(LocalDecl::new(ref_ty, span)).into(); + let new_statements = [ + StatementKind::Assign(Box::new((dropee_ptr, reborrow))), + StatementKind::Retag(RetagKind::FnEntry, Box::new(dropee_ptr)), + ]; + for s in new_statements { + body.basic_blocks_mut()[START_BLOCK] + .statements + .push(Statement { source_info, kind: s }); } + } + + if ty.is_some() { let patch = { let param_env = tcx.param_env_reveal_all_normalized(def_id); let mut elaborator = diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs index 883361d05fc4..8cf63ee700b8 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.rs @@ -10,7 +10,7 @@ impl Drop for HasDrop { fn drop(&mut self) { unsafe { let _val = *P; - //~^ ERROR: /not granting access .* because that would remove .* which is protected/ + //~^ ERROR: /not granting access .* because that would remove .* which is strongly protected/ } } } diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr index 6d122ade4772..8b1740cd81be 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr @@ -1,8 +1,8 @@ -error: Undefined Behavior: not granting access to tag because that would remove [Unique for ] which is protected because it is an argument of call ID +error: Undefined Behavior: not granting access to tag because that would remove [Unique for ] which is strongly protected because it is an argument of call ID --> $DIR/drop_in_place_protector.rs:LL:CC | LL | let _val = *P; - | ^^ not granting access to tag because that would remove [Unique for ] which is protected because it is an argument of call ID + | ^^ not granting access to tag because that would remove [Unique for ] which is strongly protected because it is an argument of call ID | = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs index 4cb870f1d97d..e7d256b686d1 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs @@ -3,16 +3,9 @@ //@error-pattern: /retag .* for Unique permission .* only grants SharedReadOnly permission/ -#[repr(transparent)] -struct HasDrop; - -impl Drop for HasDrop { - fn drop(&mut self) {} -} - fn main() { unsafe { - let x = (0u8, HasDrop); + let x = 0u8; let x = core::ptr::addr_of!(x); core::ptr::drop_in_place(x.cast_mut()); } diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr index 022b27d69b2c..05648e44be97 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr @@ -15,7 +15,7 @@ help: was created by a SharedReadOnly retag at offsets [0x0..0x1] LL | let x = core::ptr::addr_of!(x); | ^^^^^^^^^^^^^^^^^^^^^^ = note: BACKTRACE: - = note: inside `std::ptr::drop_in_place::<(u8, HasDrop)> - shim(Some((u8, HasDrop)))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC + = note: inside `std::ptr::drop_in_place:: - shim(None)` at RUSTLIB/core/src/ptr/mod.rs:LL:CC note: inside `main` at $DIR/drop_in_place_retag.rs:LL:CC --> $DIR/drop_in_place_retag.rs:LL:CC | diff --git a/src/tools/miri/tests/pass/drop_in_place_null.rs b/src/tools/miri/tests/pass/drop_in_place_null.rs deleted file mode 100644 index aab070b69757..000000000000 --- a/src/tools/miri/tests/pass/drop_in_place_null.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Make sure that dropping types with no drop glue is DB even for invalid pointers. - -fn main() { - unsafe { - core::ptr::drop_in_place::(core::ptr::null_mut()); - } -} From 37e00165e408c0e0bca0072f282b790888254bc2 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Fri, 9 Dec 2022 19:05:49 -0800 Subject: [PATCH 282/321] Bless tests --- .../tests/fail/stacked_borrows/drop_in_place_protector.stderr | 2 +- .../tests/fail/stacked_borrows/drop_in_place_retag.stderr | 4 ++-- .../miri/tests/fail/unaligned_pointers/drop_in_place.stderr | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr index 8b1740cd81be..bd51a6645a67 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr @@ -20,7 +20,7 @@ LL | core::ptr::drop_in_place(x); = note: inside `::drop` at $DIR/drop_in_place_protector.rs:LL:CC = note: inside `std::ptr::drop_in_place:: - shim(Some(HasDrop))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC = note: inside `std::ptr::drop_in_place::<(HasDrop, u8)> - shim(Some((HasDrop, u8)))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC -note: inside `main` at $DIR/drop_in_place_protector.rs:LL:CC +note: inside `main` --> $DIR/drop_in_place_protector.rs:LL:CC | LL | core::ptr::drop_in_place(x); diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr index 05648e44be97..3f9e6708bdaa 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr @@ -5,7 +5,7 @@ LL | pub unsafe fn drop_in_place(to_drop: *mut T) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | trying to retag from for Unique permission at ALLOC[0x0], but that tag only grants SharedReadOnly permission for this location - | this error occurs as part of FnEntry retag at ALLOC[0x0..0x1] + | this error occurs as part of retag at ALLOC[0x0..0x1] | = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information @@ -16,7 +16,7 @@ LL | let x = core::ptr::addr_of!(x); | ^^^^^^^^^^^^^^^^^^^^^^ = note: BACKTRACE: = note: inside `std::ptr::drop_in_place:: - shim(None)` at RUSTLIB/core/src/ptr/mod.rs:LL:CC -note: inside `main` at $DIR/drop_in_place_retag.rs:LL:CC +note: inside `main` --> $DIR/drop_in_place_retag.rs:LL:CC | LL | core::ptr::drop_in_place(x.cast_mut()); diff --git a/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr b/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr index 1081964987c0..ef20b43c118f 100644 --- a/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr +++ b/src/tools/miri/tests/fail/unaligned_pointers/drop_in_place.stderr @@ -8,7 +8,7 @@ LL | pub unsafe fn drop_in_place(to_drop: *mut T) { = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: BACKTRACE: = note: inside `std::ptr::drop_in_place:: - shim(Some(PartialDrop))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC -note: inside `main` at $DIR/drop_in_place.rs:LL:CC +note: inside `main` --> $DIR/drop_in_place.rs:LL:CC | LL | core::ptr::drop_in_place(p); From ccbba0a60e3b094aeb48991cac9b6e342eb3e229 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Wed, 21 Dec 2022 23:22:56 +0000 Subject: [PATCH 283/321] Update track_caller tests; run fmt --- compiler/rustc_ast_lowering/src/expr.rs | 7 ++----- .../track-caller/async-closure-gate.rs | 1 - .../track-caller/async-closure-gate.stderr | 15 +-------------- .../track-caller/panic-track-caller.rs | 6 ++++++ 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 805050e681b0..3634e6e47ce1 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -657,11 +657,8 @@ impl<'hir> LoweringContext<'_, 'hir> { }; let hir_id = self.lower_node_id(closure_node_id); - let unstable_span = self.mark_span_with_reason( - DesugaringKind::Async, - span, - self.allow_gen_future.clone(), - ); + let unstable_span = + self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone()); if self.tcx.features().closure_track_caller && let Some(attrs) = self.attrs.get(&outer_hir_id.local_id) diff --git a/src/test/ui/async-await/track-caller/async-closure-gate.rs b/src/test/ui/async-await/track-caller/async-closure-gate.rs index 9593fdb1908e..d9d556855991 100644 --- a/src/test/ui/async-await/track-caller/async-closure-gate.rs +++ b/src/test/ui/async-await/track-caller/async-closure-gate.rs @@ -5,6 +5,5 @@ fn main() { let _ = #[track_caller] async || { //~^ ERROR `#[track_caller]` on closures is currently unstable [E0658] - //~| ERROR `#[track_caller]` on closures is currently unstable [E0658] }; } diff --git a/src/test/ui/async-await/track-caller/async-closure-gate.stderr b/src/test/ui/async-await/track-caller/async-closure-gate.stderr index be3d110eccdb..498f1b43b9bc 100644 --- a/src/test/ui/async-await/track-caller/async-closure-gate.stderr +++ b/src/test/ui/async-await/track-caller/async-closure-gate.stderr @@ -7,19 +7,6 @@ LL | let _ = #[track_caller] async || { = note: see issue #87417 for more information = help: add `#![feature(closure_track_caller)]` to the crate attributes to enable -error[E0658]: `#[track_caller]` on closures is currently unstable - --> $DIR/async-closure-gate.rs:6:38 - | -LL | let _ = #[track_caller] async || { - | ______________________________________^ -LL | | -LL | | -LL | | }; - | |_____^ - | - = note: see issue #87417 for more information - = help: add `#![feature(closure_track_caller)]` to the crate attributes to enable - -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/async-await/track-caller/panic-track-caller.rs b/src/test/ui/async-await/track-caller/panic-track-caller.rs index 02077db7c629..f45243b0ea6f 100644 --- a/src/test/ui/async-await/track-caller/panic-track-caller.rs +++ b/src/test/ui/async-await/track-caller/panic-track-caller.rs @@ -69,6 +69,9 @@ async fn foo_assoc() { Foo::bar_assoc().await } +// Since compilation is expected to fail for this fn when using +// `nofeat`, we test that separately in `async-closure-gate.rs` +#[cfg(feat)] async fn foo_closure() { let c = #[track_caller] async || { panic!(); @@ -104,4 +107,7 @@ fn main() { assert_eq!(panicked_at(|| block_on(foo_assoc())), 69); #[cfg(nofeat)] assert_eq!(panicked_at(|| block_on(foo_assoc())), 64); + + #[cfg(feat)] + assert_eq!(panicked_at(|| block_on(foo_closure())), 79); } From 0229281d03d92eec1b167377fec1e9978af1f704 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Wed, 21 Dec 2022 15:22:17 -0800 Subject: [PATCH 284/321] Don't run `Drop` terminators on types that do not have drop glue in const eval --- .../src/interpret/terminator.rs | 17 +++++++++++++---- compiler/rustc_middle/src/mir/syntax.rs | 13 ++++++------- .../fail/stacked_borrows/drop_in_place_retag.rs | 4 ++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/terminator.rs b/compiler/rustc_const_eval/src/interpret/terminator.rs index 0e7ffcdffc97..550c7a44c419 100644 --- a/compiler/rustc_const_eval/src/interpret/terminator.rs +++ b/compiler/rustc_const_eval/src/interpret/terminator.rs @@ -119,11 +119,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } Drop { place, target, unwind } => { - let place = self.eval_place(place)?; - let ty = place.layout.ty; - trace!("TerminatorKind::drop: {:?}, type {}", place, ty); - + let frame = self.frame(); + let ty = place.ty(&frame.body.local_decls, *self.tcx).ty; + let ty = self.subst_from_frame_and_normalize_erasing_regions(frame, ty)?; let instance = Instance::resolve_drop_in_place(*self.tcx, ty); + if let ty::InstanceDef::DropGlue(_, None) = instance.def { + // This is the branch we enter if and only if the dropped type has no drop glue + // whatsoever. This can happen as a result of monomorphizing a drop of a + // generic. In order to make sure that generic and non-generic code behaves + // roughly the same (and in keeping with Mir semantics) we do nothing here. + self.go_to_block(target); + return Ok(()); + } + let place = self.eval_place(place)?; + trace!("TerminatorKind::drop: {:?}, type {}", place, ty); self.drop_in_place(&place, instance, target, unwind)?; } diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 99e59c770d75..6e7d84410da9 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -562,14 +562,13 @@ pub enum TerminatorKind<'tcx> { Unreachable, /// The behavior of this statement differs significantly before and after drop elaboration. - /// After drop elaboration, `Drop` executes the drop glue for the specified place, after which - /// it continues execution/unwinds at the given basic blocks. It is possible that executing drop - /// glue is special - this would be part of Rust's memory model. (**FIXME**: due we have an - /// issue tracking if drop glue has any interesting semantics in addition to those of a function - /// call?) /// - /// `Drop` before drop elaboration is a *conditional* execution of the drop glue. Specifically, the - /// `Drop` will be executed if... + /// After drop elaboration: `Drop` terminators are a complete nop for types that have no drop + /// glue. For other types, `Drop` terminators behave exactly like a call to + /// `core::mem::drop_in_place` with a pointer to the given place. + /// + /// `Drop` before drop elaboration is a *conditional* execution of the drop glue. Specifically, + /// the `Drop` will be executed if... /// /// **Needs clarification**: End of that sentence. This in effect should document the exact /// behavior of drop elaboration. The following sounds vaguely right, but I'm not quite sure: diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs index e7d256b686d1..8180e2f03a79 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.rs @@ -1,5 +1,5 @@ -//! Test that drop_in_place mutably retags the entire place, -//! ensuring it is writeable +//! Test that drop_in_place mutably retags the entire place, even for a type that does not need +//! dropping, ensuring among other things that it is writeable //@error-pattern: /retag .* for Unique permission .* only grants SharedReadOnly permission/ From 2b661c33de0b3bc96b47915f589c74d71a755998 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 21 Dec 2022 18:03:31 -0700 Subject: [PATCH 285/321] rustdoc: simplify CSS and DOM for more-scraped-examples This gets rid of the more-scraped-examples-inner wrapper, instead nesting the children directly and using absolute positioning for the toggle line. --- src/librustdoc/html/render/mod.rs | 5 ++--- src/librustdoc/html/static/css/rustdoc.css | 17 ++++++----------- .../rustdoc-gui/scrape-examples-layout.goml | 12 ++++++------ 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 146e5010e4e4..8bccf68029aa 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -3005,8 +3005,7 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite \
Hide additional examples
\
\ -
\ -
" +
" ); // Only generate inline code for MAX_FULL_EXAMPLES number of examples. Otherwise we could @@ -3030,7 +3029,7 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite write!(w, "
"); } - write!(w, "
"); + write!(w, ""); } write!(w, ""); diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index b2f220b05711..21dc3eac5564 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1991,20 +1991,15 @@ in storage.js } .more-scraped-examples { - margin-left: 5px; - display: flex; - flex-direction: row; -} - -.more-scraped-examples-inner { - /* 20px is width of toggle-line + toggle-line-inner */ - width: calc(100% - 20px); + margin-left: 25px; + position: relative; } .toggle-line { - align-self: stretch; - margin-right: 10px; - margin-top: 5px; + position: absolute; + top: 5px; + bottom: 0; + right: calc(100% + 10px); padding: 0 4px; cursor: pointer; } diff --git a/src/test/rustdoc-gui/scrape-examples-layout.goml b/src/test/rustdoc-gui/scrape-examples-layout.goml index 988c911b7839..fde9a0ab0bc3 100644 --- a/src/test/rustdoc-gui/scrape-examples-layout.goml +++ b/src/test/rustdoc-gui/scrape-examples-layout.goml @@ -10,13 +10,8 @@ assert-property-false: ( // Check that examples with very long lines have the same width as ones that don't. store-property: ( clientWidth, - ".more-scraped-examples .scraped-example:nth-child(1) .code-wrapper .src-line-numbers", - "clientWidth" -) - -assert-property: ( ".more-scraped-examples .scraped-example:nth-child(2) .code-wrapper .src-line-numbers", - {"clientWidth": |clientWidth|} + "clientWidth" ) assert-property: ( @@ -33,3 +28,8 @@ assert-property: ( ".more-scraped-examples .scraped-example:nth-child(5) .code-wrapper .src-line-numbers", {"clientWidth": |clientWidth|} ) + +assert-property: ( + ".more-scraped-examples .scraped-example:nth-child(6) .code-wrapper .src-line-numbers", + {"clientWidth": |clientWidth|} +) From 8a4cbf4f7b7777b7553dd6ae05c7d57e5a20ebfa Mon Sep 17 00:00:00 2001 From: kadmin Date: Wed, 21 Dec 2022 23:54:29 +0000 Subject: [PATCH 286/321] Fix ICE Left a todo awhile ago (I think), so fill it in to print a const for `Term`s. --- src/librustdoc/html/format.rs | 8 ++++---- src/test/rustdoc/issue-105952.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/test/rustdoc/issue-105952.rs diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 39e2a9022267..5ad24bf26813 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1655,10 +1655,10 @@ impl clean::types::Term { &'a self, cx: &'a Context<'tcx>, ) -> impl fmt::Display + 'a + Captures<'tcx> { - match self { - clean::types::Term::Type(ty) => ty.print(cx), - _ => todo!(), - } + display_fn(move |f| match self { + clean::types::Term::Type(ty) => fmt::Display::fmt(&ty.print(cx), f), + clean::types::Term::Constant(ct) => fmt::Display::fmt(&ct.print(cx.tcx()), f), + }) } } diff --git a/src/test/rustdoc/issue-105952.rs b/src/test/rustdoc/issue-105952.rs new file mode 100644 index 000000000000..e3f1df0063df --- /dev/null +++ b/src/test/rustdoc/issue-105952.rs @@ -0,0 +1,14 @@ +#![crate_name = "foo"] + +#![feature(associated_const_equality)] +pub enum ParseMode { + Raw, +} +pub trait Parse { + const PARSE_MODE: ParseMode; +} +pub trait RenderRaw {} + +// @hasraw foo/trait.RenderRaw.html 'impl' +// @hasraw foo/trait.RenderRaw.html 'ParseMode::Raw' +impl> RenderRaw for T {} From f20f86ec4e9d7d545cd00e401a77f2268e26553b Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Wed, 21 Dec 2022 17:28:42 -0800 Subject: [PATCH 287/321] Change comment to doc comment --- compiler/rustc_resolve/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 5d0b4c0419f0..447a43ca8b29 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1686,7 +1686,7 @@ impl<'a> Resolver<'a> { .or_insert_with(|| self.arenas.alloc_name_resolution()) } - // Test if AmbiguityError ambi is any identical to any one inside ambiguity_errors + /// Test if AmbiguityError ambi is any identical to any one inside ambiguity_errors fn matches_previous_ambiguity_error(&mut self, ambi: &AmbiguityError<'_>) -> bool { for ambiguity_error in &self.ambiguity_errors { // if the span location and ident as well as its span are the same From 20052c83c037ee76dfc0e95f5c42c929250e6b71 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 17 Dec 2022 20:11:29 +0000 Subject: [PATCH 288/321] Suggest associated const on capitalization error --- compiler/rustc_hir_typeck/src/demand.rs | 45 ++++++---- compiler/rustc_hir_typeck/src/expr.rs | 10 +-- .../src/fn_ctxt/suggestions.rs | 84 ++++++++++++++++++- .../suggestions/assoc-ct-for-assoc-method.rs | 25 ++++++ .../assoc-ct-for-assoc-method.stderr | 47 +++++++++++ 5 files changed, 188 insertions(+), 23 deletions(-) create mode 100644 src/test/ui/suggestions/assoc-ct-for-assoc-method.rs create mode 100644 src/test/ui/suggestions/assoc-ct-for-assoc-method.stderr diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 479aaf2e1a7b..e68bd1297c87 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -23,6 +23,35 @@ use std::cmp::min; use std::iter; impl<'a, 'tcx> FnCtxt<'a, 'tcx> { + pub fn emit_type_mismatch_suggestions( + &self, + err: &mut Diagnostic, + expr: &hir::Expr<'tcx>, + expr_ty: Ty<'tcx>, + expected: Ty<'tcx>, + expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>, + _error: Option>, + ) { + if expr_ty == expected { + return; + } + + // Use `||` to give these suggestions a precedence + let _ = self.suggest_missing_parentheses(err, expr) + || self.suggest_associated_const(err, expr, expected) + || self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr) + || self.suggest_option_to_bool(err, expr, expr_ty, expected) + || self.suggest_compatible_variants(err, expr, expected, expr_ty) + || self.suggest_non_zero_new_unwrap(err, expr, expected, expr_ty) + || self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty) + || self.suggest_no_capture_closure(err, expected, expr_ty) + || self.suggest_boxing_when_appropriate(err, expr, expected, expr_ty) + || self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected) + || self.suggest_copied_or_cloned(err, expr, expr_ty, expected) + || self.suggest_into(err, expr, expr_ty, expected) + || self.suggest_floating_point_literal(err, expr, expected); + } + pub fn emit_coerce_suggestions( &self, err: &mut Diagnostic, @@ -37,21 +66,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } self.annotate_expected_due_to_let_ty(err, expr, error); - - // Use `||` to give these suggestions a precedence - let _ = self.suggest_missing_parentheses(err, expr) - || self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr) - || self.suggest_compatible_variants(err, expr, expected, expr_ty) - || self.suggest_non_zero_new_unwrap(err, expr, expected, expr_ty) - || self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty) - || self.suggest_no_capture_closure(err, expected, expr_ty) - || self.suggest_boxing_when_appropriate(err, expr, expected, expr_ty) - || self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected) - || self.suggest_copied_or_cloned(err, expr, expr_ty, expected) - || self.suggest_into(err, expr, expr_ty, expected) - || self.suggest_option_to_bool(err, expr, expr_ty, expected) - || self.suggest_floating_point_literal(err, expr, expected); - + self.emit_type_mismatch_suggestions(err, expr, expr_ty, expected, expected_ty_expr, error); self.note_type_is_not_clone(err, expected, expr_ty, expr); self.note_need_for_fn_pointer(err, expected, expr_ty); self.note_internal_mutation_in_method(err, expr, expected, expr_ty); diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index edbbb7272ac7..ae641b26eeed 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -104,16 +104,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) { - // FIXME(compiler-errors): We probably should fold some of the - // `suggest_` functions from `emit_coerce_suggestions` into here, - // since some of those aren't necessarily just coerce suggestions. - let _ = self.suggest_deref_ref_or_into( + let _ = self.emit_type_mismatch_suggestions( &mut err, expr.peel_drop_temps(), - expected_ty, ty, + expected_ty, None, - ) || self.suggest_option_to_bool(&mut err, expr, ty, expected_ty); + None, + ); extend_err(&mut err); err.emit(); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index c9d179de39f3..efec02446333 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1,6 +1,7 @@ use super::FnCtxt; use crate::errors::{AddReturnTypeSuggestion, ExpectedReturnTypeLabel}; +use crate::method::probe::{IsSuggestion, Mode, ProbeScope}; use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX}; use rustc_errors::{Applicability, Diagnostic, MultiSpan}; use rustc_hir as hir; @@ -15,10 +16,11 @@ use rustc_infer::traits::{self, StatementAsExpression}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::{ self, suggest_constraining_type_params, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty, + TypeVisitable, }; use rustc_session::errors::ExprParenthesesNeeded; -use rustc_span::symbol::sym; -use rustc_span::Span; +use rustc_span::symbol::{sym, Ident}; +use rustc_span::{Span, Symbol}; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::error_reporting::DefIdOrName; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _; @@ -1236,6 +1238,84 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } + pub(crate) fn suggest_associated_const( + &self, + err: &mut Diagnostic, + expr: &hir::Expr<'_>, + expected_ty: Ty<'tcx>, + ) -> bool { + let Some((DefKind::AssocFn, old_def_id)) = self.typeck_results.borrow().type_dependent_def(expr.hir_id) else { + return false; + }; + let old_item_name = self.tcx.item_name(old_def_id); + let capitalized_name = Symbol::intern(&old_item_name.as_str().to_uppercase()); + if old_item_name == capitalized_name { + return false; + } + let (item, segment) = match expr.kind { + hir::ExprKind::Path(QPath::Resolved( + Some(ty), + hir::Path { segments: [segment], .. }, + )) + | hir::ExprKind::Path(QPath::TypeRelative(ty, segment)) => { + let self_ty = >::ast_ty_to_ty(self, ty); + if let Ok(pick) = self.probe_for_name( + Mode::Path, + Ident::new(capitalized_name, segment.ident.span), + IsSuggestion(true), + self_ty, + expr.hir_id, + ProbeScope::TraitsInScope, + ) { + (pick.item, segment) + } else { + return false; + } + } + hir::ExprKind::Path(QPath::Resolved( + None, + hir::Path { segments: [.., segment], .. }, + )) => { + // we resolved through some path that doesn't end in the item name, + // better not do a bad suggestion by accident. + if old_item_name != segment.ident.name { + return false; + } + if let Some(item) = self + .tcx + .associated_items(self.tcx.parent(old_def_id)) + .filter_by_name_unhygienic(capitalized_name) + .next() + { + (*item, segment) + } else { + return false; + } + } + _ => return false, + }; + if item.def_id == old_def_id || self.tcx.def_kind(item.def_id) != DefKind::AssocConst { + // Same item + return false; + } + let item_ty = self.tcx.type_of(item.def_id); + // FIXME(compiler-errors): This check is *so* rudimentary + if item_ty.needs_subst() { + return false; + } + if self.can_coerce(item_ty, expected_ty) { + err.span_suggestion_verbose( + segment.ident.span, + format!("try referring to the associated const `{capitalized_name}` instead",), + capitalized_name, + Applicability::MachineApplicable, + ); + true + } else { + false + } + } + fn is_loop(&self, id: hir::HirId) -> bool { let node = self.tcx.hir().get(id); matches!(node, Node::Expr(Expr { kind: ExprKind::Loop(..), .. })) diff --git a/src/test/ui/suggestions/assoc-ct-for-assoc-method.rs b/src/test/ui/suggestions/assoc-ct-for-assoc-method.rs new file mode 100644 index 000000000000..fe2227769894 --- /dev/null +++ b/src/test/ui/suggestions/assoc-ct-for-assoc-method.rs @@ -0,0 +1,25 @@ +struct MyS; + +impl MyS { + const FOO: i32 = 1; + fn foo() -> MyS { + MyS + } +} + +fn main() { + let x: i32 = MyS::foo; + //~^ ERROR mismatched types + //~| HELP try referring to the + + let z: i32 = i32::max; + //~^ ERROR mismatched types + //~| HELP try referring to the + + // This example is still broken though... This is a hard suggestion to make, + // because we don't have access to the associated const probing code to make + // this suggestion where it's emitted, i.e. in trait selection. + let y: i32 = i32::max - 42; + //~^ ERROR cannot subtract + //~| HELP use parentheses +} diff --git a/src/test/ui/suggestions/assoc-ct-for-assoc-method.stderr b/src/test/ui/suggestions/assoc-ct-for-assoc-method.stderr new file mode 100644 index 000000000000..afef38f12967 --- /dev/null +++ b/src/test/ui/suggestions/assoc-ct-for-assoc-method.stderr @@ -0,0 +1,47 @@ +error[E0308]: mismatched types + --> $DIR/assoc-ct-for-assoc-method.rs:11:18 + | +LL | let x: i32 = MyS::foo; + | --- ^^^^^^^^ expected `i32`, found fn item + | | + | expected due to this + | + = note: expected type `i32` + found fn item `fn() -> MyS {MyS::foo}` +help: try referring to the associated const `FOO` instead + | +LL | let x: i32 = MyS::FOO; + | ~~~ + +error[E0308]: mismatched types + --> $DIR/assoc-ct-for-assoc-method.rs:15:18 + | +LL | let z: i32 = i32::max; + | --- ^^^^^^^^ expected `i32`, found fn item + | | + | expected due to this + | + = note: expected type `i32` + found fn item `fn(i32, i32) -> i32 {::max}` +help: try referring to the associated const `MAX` instead + | +LL | let z: i32 = i32::MAX; + | ~~~ + +error[E0369]: cannot subtract `{integer}` from `fn(i32, i32) -> i32 {::max}` + --> $DIR/assoc-ct-for-assoc-method.rs:22:27 + | +LL | let y: i32 = i32::max - 42; + | -------- ^ -- {integer} + | | + | fn(i32, i32) -> i32 {::max} + | +help: use parentheses to call this associated function + | +LL | let y: i32 = i32::max(/* i32 */, /* i32 */) - 42; + | ++++++++++++++++++++++ + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0308, E0369. +For more information about an error, try `rustc --explain E0308`. From 33ff610d1355832dd82bd5dc926e836eb8cf8d41 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 22 Dec 2022 11:38:39 +0100 Subject: [PATCH 289/321] Migrate search tab title color to CSS variable --- src/librustdoc/html/static/css/rustdoc.css | 1 + src/librustdoc/html/static/css/themes/ayu.css | 5 +---- src/librustdoc/html/static/css/themes/dark.css | 5 +---- src/librustdoc/html/static/css/themes/light.css | 5 +---- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 022ed606cc3b..1bca973bf717 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1287,6 +1287,7 @@ a.test-arrow:hover { #titles > button > div.count { display: inline-block; font-size: 1rem; + color: var(--search-tab-title-count-color); } #src-sidebar-toggle { diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index de0dfcd46904..1355ae9c2bac 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -45,6 +45,7 @@ Original by Dempfi (https://github.com/dempfi/ayu) --search-color: #fff; --search-results-alias-color: #c5c5c5; --search-results-grey-color: #999; + --search-tab-title-count-color: #888; --stab-background-color: #314559; --stab-code-color: #e6e1cf; --code-highlight-kw-color: #ff7733; @@ -175,10 +176,6 @@ pre, .rustdoc.source .example-wrap { border-bottom: 1px solid rgba(242, 151, 24, 0.3); } -#titles > button > div.count { - color: #888; -} - /* rules that this theme does not need to set, here to satisfy the rule checker */ /* note that a lot of these are partially set in some way (meaning they are set individually rather than as a group) */ diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index dd7fc6892537..84449542f22b 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -40,6 +40,7 @@ --search-color: #111; --search-results-alias-color: #fff; --search-results-grey-color: #ccc; + --search-tab-title-count-color: #888; --stab-background-color: #314559; --stab-code-color: #e6e1cf; --code-highlight-kw-color: #ab8ac1; @@ -96,10 +97,6 @@ background-color: #353535; } -#titles > button > div.count { - color: #888; -} - .scraped-example-list .scrape-help { border-color: #aaa; color: #eee; diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index b69d8a1cff95..68dc0ce539b0 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -40,6 +40,7 @@ --search-color: #000; --search-results-alias-color: #000; --search-results-grey-color: #999; + --search-tab-title-count-color: #888; --stab-background-color: #fff5d6; --stab-code-color: #000; --code-highlight-kw-color: #8959a8; @@ -93,10 +94,6 @@ border-top-color: #0089ff; } -#titles > button > div.count { - color: #888; -} - .scraped-example-list .scrape-help { border-color: #555; color: #333; From faebd7a788fd9f04743805b557a5321ee13bfbb0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 22 Dec 2022 11:38:59 +0100 Subject: [PATCH 290/321] Extend search GUI test to include search tab title color check --- src/test/rustdoc-gui/search-result-color.goml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/rustdoc-gui/search-result-color.goml b/src/test/rustdoc-gui/search-result-color.goml index dde43b1c980a..d124045608c1 100644 --- a/src/test/rustdoc-gui/search-result-color.goml +++ b/src/test/rustdoc-gui/search-result-color.goml @@ -66,6 +66,11 @@ reload: // Waiting for the search results to appear... wait-for: "#titles" +assert-css: ( + "#titles > button > div.count", + {"color": "rgb(136, 136, 136)"}, + ALL, +) assert-css: ( "//*[@class='desc'][text()='Just a normal struct.']", {"color": "rgb(197, 197, 197)"}, @@ -178,6 +183,11 @@ reload: // Waiting for the search results to appear... wait-for: "#titles" +assert-css: ( + "#titles > button > div.count", + {"color": "rgb(136, 136, 136)"}, + ALL, +) assert-css: ( "//*[@class='desc'][text()='Just a normal struct.']", {"color": "rgb(221, 221, 221)"}, @@ -275,6 +285,11 @@ reload: // Waiting for the search results to appear... wait-for: "#titles" +assert-css: ( + "#titles > button > div.count", + {"color": "rgb(136, 136, 136)"}, + ALL, +) assert-css: ( "//*[@class='desc'][text()='Just a normal struct.']", {"color": "rgb(0, 0, 0)"}, From 34ae96868fcbdb2d2fa4a0b269fab94279c13e6b Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 22 Dec 2022 20:29:20 +0900 Subject: [PATCH 291/321] Add regression test for #94293 Signed-off-by: Yuki Okushi --- .../generic_const_exprs/issue-94293.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/ui/const-generics/generic_const_exprs/issue-94293.rs diff --git a/src/test/ui/const-generics/generic_const_exprs/issue-94293.rs b/src/test/ui/const-generics/generic_const_exprs/issue-94293.rs new file mode 100644 index 000000000000..713c5d89a930 --- /dev/null +++ b/src/test/ui/const-generics/generic_const_exprs/issue-94293.rs @@ -0,0 +1,31 @@ +// check-pass + +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] +#![deny(const_evaluatable_unchecked)] + +pub struct If; +pub trait True {} +impl True for If {} + +pub struct FixedI8 { + pub bits: i8, +} + +impl PartialEq> for FixedI8 +where + If<{ FRAC_RHS <= 8 }>: True, +{ + fn eq(&self, _rhs: &FixedI8) -> bool { + unimplemented!() + } +} + +impl PartialEq for FixedI8 { + fn eq(&self, rhs: &i8) -> bool { + let rhs_as_fixed = FixedI8::<0> { bits: *rhs }; + PartialEq::eq(self, &rhs_as_fixed) + } +} + +fn main() {} From 59b3157c45048edca3cb94841d799d2ab1fe3c43 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 20 Dec 2022 17:07:04 +0100 Subject: [PATCH 292/321] Use LLVM_CMAKE_DIR for lld build LLVM_CONFIG_PATH is no longer supported as of LLVM 16, switch to using the cmake module instead. We separately return the llvm-config and cmake directory paths, because llvm-config always refers to the host binary, while the cmake directory is for the target triple. --- src/bootstrap/Cargo.toml | 5 -- src/bootstrap/bin/llvm-config-wrapper.rs | 24 ------ src/bootstrap/builder.rs | 2 +- src/bootstrap/compile.rs | 7 +- src/bootstrap/dist.rs | 7 +- src/bootstrap/native.rs | 105 ++++++++++------------- src/bootstrap/test.rs | 3 +- 7 files changed, 58 insertions(+), 95 deletions(-) delete mode 100644 src/bootstrap/bin/llvm-config-wrapper.rs diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index ccc7ec1fce9f..fafe82a9c128 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -29,11 +29,6 @@ name = "sccache-plus-cl" path = "bin/sccache-plus-cl.rs" test = false -[[bin]] -name = "llvm-config-wrapper" -path = "bin/llvm-config-wrapper.rs" -test = false - [dependencies] cmake = "0.1.38" fd-lock = "3.0.8" diff --git a/src/bootstrap/bin/llvm-config-wrapper.rs b/src/bootstrap/bin/llvm-config-wrapper.rs deleted file mode 100644 index 89984bb55dfd..000000000000 --- a/src/bootstrap/bin/llvm-config-wrapper.rs +++ /dev/null @@ -1,24 +0,0 @@ -// The sheer existence of this file is an awful hack. See the comments in -// `src/bootstrap/native.rs` for why this is needed when compiling LLD. - -use std::env; -use std::io::{self, Write}; -use std::process::{self, Command, Stdio}; - -fn main() { - let real_llvm_config = env::var_os("LLVM_CONFIG_REAL").unwrap(); - let mut cmd = Command::new(real_llvm_config); - cmd.args(env::args().skip(1)).stderr(Stdio::piped()); - let output = cmd.output().expect("failed to spawn llvm-config"); - let mut stdout = String::from_utf8_lossy(&output.stdout); - - if let Ok(to_replace) = env::var("LLVM_CONFIG_SHIM_REPLACE") { - if let Ok(replace_with) = env::var("LLVM_CONFIG_SHIM_REPLACE_WITH") { - stdout = stdout.replace(&to_replace, &replace_with).into(); - } - } - - print!("{}", stdout.replace("\\", "/")); - io::stdout().flush().unwrap(); - process::exit(output.status.code().unwrap_or(1)); -} diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 1d37d68c1d40..b9d06a77966d 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1068,7 +1068,7 @@ impl<'a> Builder<'a> { /// check build or dry-run, where there's no need to build all of LLVM. fn llvm_config(&self, target: TargetSelection) -> Option { if self.config.llvm_enabled() && self.kind != Kind::Check && !self.config.dry_run() { - let llvm_config = self.ensure(native::Llvm { target }); + let native::LlvmResult { llvm_config, .. } = self.ensure(native::Llvm { target }); if llvm_config.is_file() { return Some(llvm_config); } diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 0deed3f990d0..b62e0bfe4e0d 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -805,7 +805,7 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS if builder.is_rust_llvm(target) { cargo.env("LLVM_RUSTLLVM", "1"); } - let llvm_config = builder.ensure(native::Llvm { target }); + let native::LlvmResult { llvm_config, .. } = builder.ensure(native::Llvm { target }); cargo.env("LLVM_CONFIG", &llvm_config); if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) { cargo.env("CFG_LLVM_ROOT", s); @@ -1341,9 +1341,10 @@ impl Step for Assemble { } if builder.config.rust_codegen_backends.contains(&INTERNER.intern_str("llvm")) { - let llvm_config_bin = builder.ensure(native::Llvm { target: target_compiler.host }); + let native::LlvmResult { llvm_config, .. } = + builder.ensure(native::Llvm { target: target_compiler.host }); if !builder.config.dry_run() { - let llvm_bin_dir = output(Command::new(llvm_config_bin).arg("--bindir")); + let llvm_bin_dir = output(Command::new(llvm_config).arg("--bindir")); let llvm_bin_dir = Path::new(llvm_bin_dir.trim()); // Since we've already built the LLVM tools, install them to the sysroot. diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index 3cb0eccd324d..340aa78ebf9b 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -24,6 +24,7 @@ use crate::channel; use crate::compile; use crate::config::TargetSelection; use crate::doc::DocumentationFormat; +use crate::native; use crate::tarball::{GeneratedTarball, OverlayKind, Tarball}; use crate::tool::{self, Tool}; use crate::util::{exe, is_dylib, output, t, timeit}; @@ -1927,7 +1928,9 @@ fn maybe_install_llvm(builder: &Builder<'_>, target: TargetSelection, dst_libdir builder.install(&llvm_dylib_path, dst_libdir, 0o644); } !builder.config.dry_run() - } else if let Ok(llvm_config) = crate::native::prebuilt_llvm_config(builder, target) { + } else if let Ok(native::LlvmResult { llvm_config, .. }) = + native::prebuilt_llvm_config(builder, target) + { let mut cmd = Command::new(llvm_config); cmd.arg("--libfiles"); builder.verbose(&format!("running {:?}", cmd)); @@ -2137,7 +2140,7 @@ impl Step for Bootstrap { let tarball = Tarball::new(builder, "bootstrap", &target.triple); let bootstrap_outdir = &builder.bootstrap_out; - for file in &["bootstrap", "llvm-config-wrapper", "rustc", "rustdoc", "sccache-plus-cl"] { + for file in &["bootstrap", "rustc", "rustdoc", "sccache-plus-cl"] { tarball.add_file(bootstrap_outdir.join(exe(file, target)), "bootstrap/bin", 0o755); } diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index 8052bde6024e..b944c8210bec 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -24,9 +24,18 @@ use crate::util::get_clang_cl_resource_dir; use crate::util::{self, exe, output, t, up_to_date}; use crate::{CLang, GitRepo}; +#[derive(Clone)] +pub struct LlvmResult { + /// Path to llvm-config binary. + /// NB: This is always the host llvm-config! + pub llvm_config: PathBuf, + /// Path to LLVM cmake directory for the target. + pub llvm_cmake_dir: PathBuf, +} + pub struct Meta { stamp: HashStamp, - build_llvm_config: PathBuf, + res: LlvmResult, out_dir: PathBuf, root: String, } @@ -64,7 +73,7 @@ impl LdFlags { pub fn prebuilt_llvm_config( builder: &Builder<'_>, target: TargetSelection, -) -> Result { +) -> Result { builder.config.maybe_download_ci_llvm(); // If we're using a custom LLVM bail out here, but we can only use a @@ -72,7 +81,14 @@ pub fn prebuilt_llvm_config( if let Some(config) = builder.config.target_config.get(&target) { if let Some(ref s) = config.llvm_config { check_llvm_version(builder, s); - return Ok(s.to_path_buf()); + let llvm_config = s.to_path_buf(); + let mut llvm_cmake_dir = llvm_config.clone(); + llvm_cmake_dir.pop(); + llvm_cmake_dir.pop(); + llvm_cmake_dir.push("lib"); + llvm_cmake_dir.push("cmake"); + llvm_cmake_dir.push("llvm"); + return Ok(LlvmResult { llvm_config, llvm_cmake_dir }); } } @@ -84,8 +100,9 @@ pub fn prebuilt_llvm_config( llvm_config_ret_dir.push("build"); } llvm_config_ret_dir.push("bin"); - let build_llvm_config = llvm_config_ret_dir.join(exe("llvm-config", builder.config.build)); + let llvm_cmake_dir = out_dir.join("lib/cmake/llvm"); + let res = LlvmResult { llvm_config: build_llvm_config, llvm_cmake_dir }; let stamp = out_dir.join("llvm-finished-building"); let stamp = HashStamp::new(stamp, builder.in_tree_llvm_info.sha()); @@ -96,7 +113,7 @@ pub fn prebuilt_llvm_config( Using a potentially stale build of LLVM; \ This may not behave well.", ); - return Ok(build_llvm_config); + return Ok(res); } if stamp.is_done() { @@ -110,10 +127,10 @@ pub fn prebuilt_llvm_config( stamp.path.display() )); } - return Ok(build_llvm_config); + return Ok(res); } - Err(Meta { stamp, build_llvm_config, out_dir, root: root.into() }) + Err(Meta { stamp, res, out_dir, root: root.into() }) } /// This retrieves the LLVM sha we *want* to use, according to git history. @@ -223,7 +240,7 @@ pub struct Llvm { } impl Step for Llvm { - type Output = PathBuf; // path to llvm-config + type Output = LlvmResult; const ONLY_HOSTS: bool = true; @@ -236,7 +253,7 @@ impl Step for Llvm { } /// Compile LLVM for `target`. - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> LlvmResult { let target = self.target; let target_native = if self.target.starts_with("riscv") { // RISC-V target triples in Rust is not named the same as C compiler target triples. @@ -252,11 +269,10 @@ impl Step for Llvm { target.to_string() }; - let Meta { stamp, build_llvm_config, out_dir, root } = - match prebuilt_llvm_config(builder, target) { - Ok(p) => return p, - Err(m) => m, - }; + let Meta { stamp, res, out_dir, root } = match prebuilt_llvm_config(builder, target) { + Ok(p) => return p, + Err(m) => m, + }; builder.update_submodule(&Path::new("src").join("llvm-project")); if builder.llvm_link_shared() && target.contains("windows") { @@ -430,7 +446,8 @@ impl Step for Llvm { // https://llvm.org/docs/HowToCrossCompileLLVM.html if target != builder.config.build { - let llvm_config = builder.ensure(Llvm { target: builder.config.build }); + let LlvmResult { llvm_config, .. } = + builder.ensure(Llvm { target: builder.config.build }); if !builder.config.dry_run() { let llvm_bindir = output(Command::new(&llvm_config).arg("--bindir")); let host_bin = Path::new(llvm_bindir.trim()); @@ -480,7 +497,7 @@ impl Step for Llvm { // tools and libs on all platforms. if builder.config.dry_run() { - return build_llvm_config; + return res; } cfg.build(); @@ -490,7 +507,7 @@ impl Step for Llvm { // for a versioned path like libLLVM-14.dylib. Manually create a symbolic // link to make llvm-config happy. if builder.llvm_link_shared() && target.contains("apple-darwin") { - let mut cmd = Command::new(&build_llvm_config); + let mut cmd = Command::new(&res.llvm_config); let version = output(cmd.arg("--version")); let major = version.split('.').next().unwrap(); let lib_name = match llvm_version_suffix { @@ -509,18 +526,18 @@ impl Step for Llvm { // LLVM after a configuration change, so to rebuild it the build files have to be removed, // which will also remove these modified files. if builder.config.llvm_bolt_profile_generate { - instrument_with_bolt_inplace(&get_built_llvm_lib_path(&build_llvm_config)); + instrument_with_bolt_inplace(&get_built_llvm_lib_path(&res.llvm_config)); } if let Some(path) = &builder.config.llvm_bolt_profile_use { optimize_library_with_bolt_inplace( - &get_built_llvm_lib_path(&build_llvm_config), + &get_built_llvm_lib_path(&res.llvm_config), &Path::new(path), ); } t!(stamp.write()); - build_llvm_config + res } } @@ -803,7 +820,8 @@ impl Step for Lld { } let target = self.target; - let llvm_config = builder.ensure(Llvm { target: self.target }); + let LlvmResult { llvm_config, llvm_cmake_dir } = + builder.ensure(Llvm { target: self.target }); let out_dir = builder.lld_out(target); let done_stamp = out_dir.join("lld-finished-building"); @@ -834,22 +852,6 @@ impl Step for Lld { configure_cmake(builder, target, &mut cfg, true, ldflags); configure_llvm(builder, target, &mut cfg); - // This is an awful, awful hack. Discovered when we migrated to using - // clang-cl to compile LLVM/LLD it turns out that LLD, when built out of - // tree, will execute `llvm-config --cmakedir` and then tell CMake about - // that directory for later processing. Unfortunately if this path has - // forward slashes in it (which it basically always does on Windows) - // then CMake will hit a syntax error later on as... something isn't - // escaped it seems? - // - // Instead of attempting to fix this problem in upstream CMake and/or - // LLVM/LLD we just hack around it here. This thin wrapper will take the - // output from llvm-config and replace all instances of `\` with `/` to - // ensure we don't hit the same bugs with escaping. It means that you - // can't build on a system where your paths require `\` on Windows, but - // there's probably a lot of reasons you can't do that other than this. - let llvm_config_shim = env::current_exe().unwrap().with_file_name("llvm-config-wrapper"); - // Re-use the same flags as llvm to control the level of debug information // generated for lld. let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) { @@ -860,30 +862,15 @@ impl Step for Lld { cfg.out_dir(&out_dir) .profile(profile) - .env("LLVM_CONFIG_REAL", &llvm_config) - .define("LLVM_CONFIG_PATH", llvm_config_shim) + .define("LLVM_CMAKE_DIR", llvm_cmake_dir) .define("LLVM_INCLUDE_TESTS", "OFF"); - // While we're using this horrible workaround to shim the execution of - // llvm-config, let's just pile on more. I can't seem to figure out how - // to build LLD as a standalone project and also cross-compile it at the - // same time. It wants a natively executable `llvm-config` to learn - // about LLVM, but then it learns about all the host configuration of - // LLVM and tries to link to host LLVM libraries. - // - // To work around that we tell our shim to replace anything with the - // build target with the actual target instead. This'll break parts of - // LLD though which try to execute host tools, such as llvm-tblgen, so - // we specifically tell it where to find those. This is likely super - // brittle and will break over time. If anyone knows better how to - // cross-compile LLD it would be much appreciated to fix this! if target != builder.config.build { - cfg.env("LLVM_CONFIG_SHIM_REPLACE", &builder.config.build.triple) - .env("LLVM_CONFIG_SHIM_REPLACE_WITH", &target.triple) - .define( - "LLVM_TABLEGEN_EXE", - llvm_config.with_file_name("llvm-tblgen").with_extension(EXE_EXTENSION), - ); + // Use the host llvm-tblgen binary. + cfg.define( + "LLVM_TABLEGEN_EXE", + llvm_config.with_file_name("llvm-tblgen").with_extension(EXE_EXTENSION), + ); } cfg.build(); @@ -987,7 +974,7 @@ impl Step for Sanitizers { return runtimes; } - let llvm_config = builder.ensure(Llvm { target: builder.config.build }); + let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: builder.config.build }); if builder.config.dry_run() { return runtimes; } diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index 39cedfdac5f7..0d9c22e210f9 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -1575,7 +1575,8 @@ note: if you're sure you want to do this, please open an issue as to why. In the let mut llvm_components_passed = false; let mut copts_passed = false; if builder.config.llvm_enabled() { - let llvm_config = builder.ensure(native::Llvm { target: builder.config.build }); + let native::LlvmResult { llvm_config, .. } = + builder.ensure(native::Llvm { target: builder.config.build }); if !builder.config.dry_run() { let llvm_version = output(Command::new(&llvm_config).arg("--version")); let llvm_components = output(Command::new(&llvm_config).arg("--components")); From 0b3ffcbb0beed7bc0be168dadbff2904b85d3666 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 22 Dec 2022 10:34:27 -0600 Subject: [PATCH 293/321] Allow building std with cranelift - Don't pass llvm-specific args when using cranelift - Don't use `asm` in compiler_builtins when using cranelift --- compiler/rustc_codegen_llvm/src/intrinsic.rs | 2 +- src/bootstrap/builder.rs | 5 ++++- src/bootstrap/compile.rs | 13 ++++++++++--- src/bootstrap/config.rs | 4 ++++ src/bootstrap/lib.rs | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 907517bf6ce9..1ce48f82e1c9 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -567,7 +567,7 @@ fn codegen_msvc_try<'ll>( // module. // // When modifying, make sure that the type_name string exactly matches - // the one used in src/libpanic_unwind/seh.rs. + // the one used in library/panic_unwind/src/seh.rs. let type_info_vtable = bx.declare_global("??_7type_info@@6B@", bx.type_i8p()); let type_name = bx.const_bytes(b"rust_panic\0"); let type_info = diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 8ee6d49da8f0..dc02814374be 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1853,7 +1853,10 @@ impl<'a> Builder<'a> { }; if let Some(limit) = limit { - rustflags.arg(&format!("-Cllvm-args=-import-instr-limit={}", limit)); + if stage == 0 || self.config.default_codegen_backend().unwrap_or_default() == "llvm" + { + rustflags.arg(&format!("-Cllvm-args=-import-instr-limit={}", limit)); + } } } diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 0deed3f990d0..79c3f84a6ae1 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -321,8 +321,15 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car "" }; + let mut features = String::new(); + + // Cranelift doesn't support `asm`. + if stage != 0 && builder.config.default_codegen_backend().unwrap_or_default() == "cranelift" { + features += " compiler-builtins-no-asm"; + } + if builder.no_std(target) == Some(true) { - let mut features = "compiler-builtins-mem".to_string(); + features += " compiler-builtins-mem"; if !target.starts_with("bpf") { features.push_str(compiler_builtins_c_feature); } @@ -335,7 +342,7 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car .arg("--features") .arg(features); } else { - let mut features = builder.std_features(target); + features += &builder.std_features(target); features.push_str(compiler_builtins_c_feature); cargo @@ -754,7 +761,7 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS .env("CFG_RELEASE_CHANNEL", &builder.config.channel) .env("CFG_VERSION", builder.rust_version()); - if let Some(backend) = builder.config.rust_codegen_backends.get(0) { + if let Some(backend) = builder.config.default_codegen_backend() { cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", backend); } diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index 960fbdf75380..41c8f17ee157 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -1610,6 +1610,10 @@ impl Config { self.submodules.unwrap_or(rust_info.is_managed_git_subrepository()) } + pub fn default_codegen_backend(&self) -> Option> { + self.rust_codegen_backends.get(0).cloned() + } + /// Returns the commit to download, or `None` if we shouldn't download CI artifacts. fn download_ci_rustc_commit(&self, download_rustc: Option) -> Option { // If `download-rustc` is not set, default to rebuilding. diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 570fe6484e3d..63b9ab46e279 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -782,7 +782,7 @@ impl Build { /// Gets the space-separated set of activated features for the standard /// library. fn std_features(&self, target: TargetSelection) -> String { - let mut features = "panic-unwind".to_string(); + let mut features = " panic-unwind".to_string(); match self.config.llvm_libunwind(target) { LlvmLibunwind::InTree => features.push_str(" llvm-libunwind"), From 4566db335902599083bc622c82254f88870c6488 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Thu, 22 Dec 2022 15:47:14 +0000 Subject: [PATCH 294/321] Run `tidy` in its own job in PR CI This duplicates mingw-check into two jobs where one job runs `tidy` only while the other job does not. The tidy job will not cancel other jobs on failure. --- .github/workflows/ci.yml | 8 +++++ .../host-x86_64/mingw-check-tidy/Dockerfile | 36 +++++++++++++++++++ .../docker/host-x86_64/mingw-check/Dockerfile | 1 - src/ci/github-actions/ci.yml | 10 +++++- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2b6e96b467e3..23d3e71424b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,16 +41,24 @@ jobs: TOOLSTATE_REPO: "https://github.com/rust-lang-nursery/rust-toolstate" CACHE_DOMAIN: ci-caches.rust-lang.org if: "github.event_name == 'pull_request'" + continue-on-error: "${{ matrix.tidy }}" strategy: matrix: include: - name: mingw-check + tidy: false + os: ubuntu-20.04-xl + env: {} + - name: mingw-check-tidy + tidy: true os: ubuntu-20.04-xl env: {} - name: x86_64-gnu-llvm-13 + tidy: false os: ubuntu-20.04-xl env: {} - name: x86_64-gnu-tools + tidy: false env: CI_ONLY_WHEN_SUBMODULES_CHANGED: 1 os: ubuntu-20.04-xl diff --git a/src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile b/src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile new file mode 100644 index 000000000000..78fee152eb9d --- /dev/null +++ b/src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile @@ -0,0 +1,36 @@ +FROM ubuntu:18.04 +# FIXME: when bumping the version, remove the Python 3.6-specific changes in +# the reuse-requirements.in file, regenerate reuse-requirements.txt and remove +# this comment. + +RUN apt-get update && apt-get install -y --no-install-recommends \ + g++ \ + make \ + ninja-build \ + file \ + curl \ + ca-certificates \ + python3 \ + python3-pip \ + python3-pkg-resources \ + git \ + cmake \ + sudo \ + gdb \ + xz-utils \ + libssl-dev \ + pkg-config \ + mingw-w64 \ + && rm -rf /var/lib/apt/lists/* + +COPY scripts/sccache.sh /scripts/ +RUN sh /scripts/sccache.sh + +COPY host-x86_64/mingw-check/reuse-requirements.txt /tmp/ +RUN pip3 install --no-deps --no-cache-dir --require-hashes -r /tmp/reuse-requirements.txt + +COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/ +COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/ + +ENV RUN_CHECK_WITH_PARALLEL_QUERIES 1 +ENV SCRIPT python3 ../x.py test --stage 0 src/tools/tidy diff --git a/src/ci/docker/host-x86_64/mingw-check/Dockerfile b/src/ci/docker/host-x86_64/mingw-check/Dockerfile index 9e2568af13f6..adf6bb4b3775 100644 --- a/src/ci/docker/host-x86_64/mingw-check/Dockerfile +++ b/src/ci/docker/host-x86_64/mingw-check/Dockerfile @@ -40,7 +40,6 @@ COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/ ENV RUN_CHECK_WITH_PARALLEL_QUERIES 1 ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \ - python3 ../x.py test --stage 0 src/tools/tidy && \ python3 ../x.py check --target=i686-pc-windows-gnu --host=i686-pc-windows-gnu --all-targets && \ python3 ../x.py build --stage 0 src/tools/build-manifest && \ python3 ../x.py test --stage 0 src/tools/compiletest && \ diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index d1ba46ad30de..d33396dcc80f 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -287,19 +287,27 @@ jobs: env: <<: [*shared-ci-variables, *public-variables] if: github.event_name == 'pull_request' + continue-on-error: ${{ matrix.tidy }} strategy: matrix: include: - name: mingw-check <<: *job-linux-xl + tidy: false + + - name: mingw-check-tidy + <<: *job-linux-xl + tidy: true - name: x86_64-gnu-llvm-13 <<: *job-linux-xl + tidy: false - name: x86_64-gnu-tools + <<: *job-linux-xl + tidy: false env: CI_ONLY_WHEN_SUBMODULES_CHANGED: 1 - <<: *job-linux-xl auto: permissions: From 51ba7b4e43179f1262c7684ad3c0a04ddb703e9d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 22 Dec 2022 17:51:45 +0000 Subject: [PATCH 295/321] Use separate files instead of revisions --- .../unresolved-ct-var-drop-tracking.rs | 15 +++++++++++++ ...=> unresolved-ct-var-drop-tracking.stderr} | 22 +++++++++---------- src/test/ui/generator/unresolved-ct-var.rs | 2 -- ...acking.stderr => unresolved-ct-var.stderr} | 22 +++++++++---------- 4 files changed, 37 insertions(+), 24 deletions(-) create mode 100644 src/test/ui/generator/unresolved-ct-var-drop-tracking.rs rename src/test/ui/generator/{unresolved-ct-var.stock.stderr => unresolved-ct-var-drop-tracking.stderr} (84%) rename src/test/ui/generator/{unresolved-ct-var.drop_tracking.stderr => unresolved-ct-var.stderr} (88%) diff --git a/src/test/ui/generator/unresolved-ct-var-drop-tracking.rs b/src/test/ui/generator/unresolved-ct-var-drop-tracking.rs new file mode 100644 index 000000000000..a6589348d301 --- /dev/null +++ b/src/test/ui/generator/unresolved-ct-var-drop-tracking.rs @@ -0,0 +1,15 @@ +// incremental +// edition:2021 +// compile-flags: -Zdrop-tracking + +fn main() { + let _ = async { + let s = std::array::from_fn(|_| ()).await; + //~^ ERROR `[(); _]` is not a future + //~| ERROR type inside `async` block must be known in this context + //~| ERROR type inside `async` block must be known in this context + //~| ERROR type inside `async` block must be known in this context + //~| ERROR type inside `async` block must be known in this context + //~| ERROR type inside `async` block must be known in this context + }; +} diff --git a/src/test/ui/generator/unresolved-ct-var.stock.stderr b/src/test/ui/generator/unresolved-ct-var-drop-tracking.stderr similarity index 84% rename from src/test/ui/generator/unresolved-ct-var.stock.stderr rename to src/test/ui/generator/unresolved-ct-var-drop-tracking.stderr index a328c43765db..9e1fed54c548 100644 --- a/src/test/ui/generator/unresolved-ct-var.stock.stderr +++ b/src/test/ui/generator/unresolved-ct-var-drop-tracking.stderr @@ -1,5 +1,5 @@ error[E0277]: `[(); _]` is not a future - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:44 | LL | let s = std::array::from_fn(|_| ()).await; | ---------------------------^^^^^^ @@ -13,61 +13,61 @@ LL | let s = std::array::from_fn(|_| ()).await; = note: required for `[(); _]` to implement `IntoFuture` error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var-drop-tracking.rs:7:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ diff --git a/src/test/ui/generator/unresolved-ct-var.rs b/src/test/ui/generator/unresolved-ct-var.rs index 6720f4658872..0a1570fc2395 100644 --- a/src/test/ui/generator/unresolved-ct-var.rs +++ b/src/test/ui/generator/unresolved-ct-var.rs @@ -1,7 +1,5 @@ // incremental // edition:2021 -// revisions: drop_tracking stock -//[drop_tracking] compile-flags: -Zdrop-tracking fn main() { let _ = async { diff --git a/src/test/ui/generator/unresolved-ct-var.drop_tracking.stderr b/src/test/ui/generator/unresolved-ct-var.stderr similarity index 88% rename from src/test/ui/generator/unresolved-ct-var.drop_tracking.stderr rename to src/test/ui/generator/unresolved-ct-var.stderr index a328c43765db..fdf00dfad7ab 100644 --- a/src/test/ui/generator/unresolved-ct-var.drop_tracking.stderr +++ b/src/test/ui/generator/unresolved-ct-var.stderr @@ -1,5 +1,5 @@ error[E0277]: `[(); _]` is not a future - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var.rs:6:44 | LL | let s = std::array::from_fn(|_| ()).await; | ---------------------------^^^^^^ @@ -13,61 +13,61 @@ LL | let s = std::array::from_fn(|_| ()).await; = note: required for `[(); _]` to implement `IntoFuture` error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var.rs:6:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var.rs:6:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var.rs:6:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var.rs:6:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var.rs:6:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var.rs:6:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var.rs:6:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var.rs:6:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ error[E0698]: type inside `async` block must be known in this context - --> $DIR/unresolved-ct-var.rs:8:17 + --> $DIR/unresolved-ct-var.rs:6:17 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `from_fn` | note: the type is part of the `async` block because of this `await` - --> $DIR/unresolved-ct-var.rs:8:44 + --> $DIR/unresolved-ct-var.rs:6:44 | LL | let s = std::array::from_fn(|_| ()).await; | ^^^^^^ From f38bf5399d329497046f51fc06c0f27213de3044 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 22 Dec 2022 17:59:26 +0000 Subject: [PATCH 296/321] Take revision into account in src/test/ui incremental tests --- src/tools/compiletest/src/common.rs | 4 ++-- src/tools/compiletest/src/runtest.rs | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index 07b80b8baac1..a4f1167bf9d1 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -617,6 +617,6 @@ pub fn output_base_name(config: &Config, testpaths: &TestPaths, revision: Option /// Absolute path to the directory to use for incremental compilation. Example: /// /path/to/build/host-triple/test/ui/relative/testname.mode/testname.inc -pub fn incremental_dir(config: &Config, testpaths: &TestPaths) -> PathBuf { - output_base_name(config, testpaths, None).with_extension("inc") +pub fn incremental_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf { + output_base_name(config, testpaths, revision).with_extension("inc") } diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 72a43108dc48..95f6e047bf9e 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -117,8 +117,12 @@ pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) { } debug!("running {:?}", testpaths.file.display()); let mut props = TestProps::from_file(&testpaths.file, revision, &config); + + // For non-incremental (i.e. regular UI) tests, the incremental directory + // takes into account the revision name, since the revisions are independent + // of each other and can race. if props.incremental { - props.incremental_dir = Some(incremental_dir(&config, testpaths)); + props.incremental_dir = Some(incremental_dir(&config, testpaths, revision)); } let cx = TestCx { config: &config, props: &props, testpaths, revision }; From 164e22109b697ed0bc7dd62039362b2fb5482ef9 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 22 Dec 2022 11:16:29 -0600 Subject: [PATCH 297/321] Mark `proc_macro_decls_static` as always used This would have avoided a bug in https://github.com/rust-lang/rust/pull/104860. In practice this shouldn't matter since nothing uses the query other than the `dead_code` lint, but this isn't documented as an internal-only query so it seems nice for it to be accurate. I think for `dead_code` it doesn't matter because the relevant code is generated by `rustc_builtin_macros` and isn't linted. --- compiler/rustc_builtin_macros/src/proc_macro_harness.rs | 2 ++ src/test/ui/proc-macro/quote-debug.stdout | 1 + 2 files changed, 3 insertions(+) diff --git a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs index ece660cf6f64..b88de2246753 100644 --- a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs +++ b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs @@ -262,6 +262,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> { // use proc_macro::bridge::client::ProcMacro; // // #[rustc_proc_macro_decls] +// #[used] // #[allow(deprecated)] // static DECLS: &[ProcMacro] = &[ // ProcMacro::custom_derive($name_trait1, &[], ::$name1); @@ -364,6 +365,7 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P { ) .map(|mut i| { i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span)); + i.attrs.push(cx.attr_word(sym::used, span)); i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span)); i }); diff --git a/src/test/ui/proc-macro/quote-debug.stdout b/src/test/ui/proc-macro/quote-debug.stdout index d2cc5c6e2a3f..9f64a1e06b9d 100644 --- a/src/test/ui/proc-macro/quote-debug.stdout +++ b/src/test/ui/proc-macro/quote-debug.stdout @@ -42,6 +42,7 @@ const _: () = { extern crate proc_macro; #[rustc_proc_macro_decls] + #[used] #[allow(deprecated)] static _DECLS: &[proc_macro::bridge::client::ProcMacro] = &[]; }; From e5b278b702afc0fa26eda0517fd6907712790b5a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 5 Dec 2022 01:42:22 +0000 Subject: [PATCH 298/321] Deduplicate check_expr in builtin calls with error --- compiler/rustc_hir_typeck/src/callee.rs | 10 ++++- .../suggestions/fn-to-method-deeply-nested.rs | 13 +++++++ .../fn-to-method-deeply-nested.stderr | 39 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/suggestions/fn-to-method-deeply-nested.rs create mode 100644 src/test/ui/suggestions/fn-to-method-deeply-nested.stderr diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 4ec71a78a003..c54352779137 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -399,6 +399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } ty::FnPtr(sig) => (sig, None), _ => { + let mut skip_first_expr = false; if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &callee_expr.kind && let [segment] = path.segments && let Some(mut diag) = self @@ -421,11 +422,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return ty; } else { diag.emit(); + skip_first_expr = true; } } let err = self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs); + for arg in arg_exprs.iter().skip(skip_first_expr as usize) { + self.check_expr(arg); + } + return self.tcx.ty_error_with_guaranteed(err); } }; @@ -486,7 +492,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expected: Expectation<'tcx>, ) -> Option> { if let [callee_expr, rest @ ..] = arg_exprs { - let callee_ty = self.check_expr(callee_expr); + // This may happen recursively -- if so, avoid repeatedly checking the expr. + let callee_ty = self.typeck_results.borrow().expr_ty_adjusted_opt(callee_expr); + let callee_ty = callee_ty.unwrap_or_else(|| self.check_expr(callee_expr)); // First, do a probe with `IsSuggestion(true)` to avoid emitting // any strange errors. If it's successful, then we'll do a true // method lookup. diff --git a/src/test/ui/suggestions/fn-to-method-deeply-nested.rs b/src/test/ui/suggestions/fn-to-method-deeply-nested.rs new file mode 100644 index 000000000000..58ee3d6409a7 --- /dev/null +++ b/src/test/ui/suggestions/fn-to-method-deeply-nested.rs @@ -0,0 +1,13 @@ +fn main() -> Result<(), ()> { + a(b(c(d(e( + //~^ ERROR cannot find function `a` in this scope + //~| ERROR cannot find function `b` in this scope + //~| ERROR cannot find function `c` in this scope + //~| ERROR cannot find function `d` in this scope + //~| ERROR cannot find function `e` in this scope + z???????????????????????????????????????????????????????????????????????????????????????? + ????????????????????????????????????????????????????????????????????????????????????????? + ?????????????????????????????????????????????????????????????????? + //~^^^ ERROR cannot find value `z` in this scope + ))))) +} diff --git a/src/test/ui/suggestions/fn-to-method-deeply-nested.stderr b/src/test/ui/suggestions/fn-to-method-deeply-nested.stderr new file mode 100644 index 000000000000..ce813ea7abae --- /dev/null +++ b/src/test/ui/suggestions/fn-to-method-deeply-nested.stderr @@ -0,0 +1,39 @@ +error[E0425]: cannot find value `z` in this scope + --> $DIR/fn-to-method-deeply-nested.rs:8:9 + | +LL | z???????????????????????????????????????????????????????????????????????????????????????? + | ^ not found in this scope + +error[E0425]: cannot find function `e` in this scope + --> $DIR/fn-to-method-deeply-nested.rs:2:13 + | +LL | a(b(c(d(e( + | ^ not found in this scope + +error[E0425]: cannot find function `d` in this scope + --> $DIR/fn-to-method-deeply-nested.rs:2:11 + | +LL | a(b(c(d(e( + | ^ not found in this scope + +error[E0425]: cannot find function `c` in this scope + --> $DIR/fn-to-method-deeply-nested.rs:2:9 + | +LL | a(b(c(d(e( + | ^ not found in this scope + +error[E0425]: cannot find function `b` in this scope + --> $DIR/fn-to-method-deeply-nested.rs:2:7 + | +LL | a(b(c(d(e( + | ^ not found in this scope + +error[E0425]: cannot find function `a` in this scope + --> $DIR/fn-to-method-deeply-nested.rs:2:5 + | +LL | a(b(c(d(e( + | ^ not found in this scope + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0425`. From c01985387d8c00e04acc87d04d52e3a5e5c82c10 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 22 Dec 2022 12:43:27 -0600 Subject: [PATCH 299/321] Make `sess.bug` much less noisy Before: ``` Compiling core v0.0.0 (C:\Users\Joshua Nelson\src\rust2\library\core) error: internal compiler error: no warnings or errors encountered even though `delayed_good_path_bugs` issued error: internal compiler error: oops | = note: delayed at 0: std::backtrace_rs::backtrace::trace_unsynchronized 1: std::backtrace::Backtrace::disabled 2: std::backtrace::Backtrace::force_capture 3: ::delay_good_path_bug::<&str> 4: ::enter::> 5: RINvMs2_NtCsiwHPejSviHg_15rustc_interface7queriesNtNtB8_9interface8Compiler5enterNCNCNvCs7PhwInflpyf_12rustc_driver12run_compilers_0s0_0INtNtCslM5znELOk5i_4core6result6ResultINtNtB2f_6option6OptionNtB6_6LinkerENtCshthk7JDUYGg_12rustc_errors15ErrorGuarante 6: RINvCs6uSsza6NDuD_10rustc_span15with_source_mapINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedENCNCINvNtCsiwHPejSviHg_15rustc_interface9interface12run_compilerBJ_NCNvCs7PhwInflpyf_12rustc_driver12run_compilers_0E00EB 7: RINvMs_Cs9yvsqs6YnUZ_10scoped_tlsINtB5_9ScopedKeyNtCs6uSsza6NDuD_10rustc_span14SessionGlobalsE3setNCINvNtCsiwHPejSviHg_15rustc_interface9interface12run_compilerINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedENCNvCs7P 8: RINvNtNtCs5xszGlR5JQw_3std10sys_common9backtrace28___rust_begin_short_backtraceNCNCINvNtCsiwHPejSviHg_15rustc_interface4util31run_in_thread_pool_with_globalsNCINvNtB1o_9interface12run_compilerINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_ 9: RINvNtCs5xszGlR5JQw_3std9panicking3tryINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedEINtNtNtBF_5panic11unwind_safe16AssertUnwindSafeNCNCINvMNtB4_6threadNtB2S_7Builder16spawn_unchecked_NCNCINvNtCsiwHPejSviHg_15rustc_ 10: ::spawn_scoped::::{closure#0}, ()>::{closure#0}::{closure#0}, ()> 11: as core::ops::function::FnOnce>::call_once 12: std::sys::windows::thread::Thread::new 13: BaseThreadInitThunk 14: RtlUserThreadStart thread 'rustc' panicked at 'Box', compiler\rustc_errors\src\lib.rs:1610:13 stack backtrace: 0: 0x7fffbe935a15 - std::backtrace_rs::backtrace::trace_unsynchronized::h2a29ea0a35c1f799 1: 0x7fffbe968811 - ::fmt::h5e4ca8af47bfbcc0 2: 0x7fffbe98aeab - core::fmt::write::h152a9de2569e7dc7 3: 0x7fffbe961059 - std::io::Write::write_fmt::h2452e19093defac4 4: 0x7fffbe96866b - std::sys_common::backtrace::print::h550d034f7b77d60a 5: 0x7fffbe93be1b - std::panicking::default_hook::h3969ead74039b801 6: 0x7fffbe93bbad - std::panicking::default_hook::h3969ead74039b801 7: 0x7fffa5f0abe3 - rustc_driver[5b2ae43fbecaf219]::handle_options 8: 0x7fffbe93c25b - std::panicking::rust_panic_with_hook::h32bbe3ce24999160 9: 0x7fffa82baee5 - std[4086331e48bff2f6]::sys_common::backtrace::__rust_end_short_backtrace::::{closure#0}, !> 10: 0x7fffa82bae89 - std[4086331e48bff2f6]::sys_common::backtrace::__rust_end_short_backtrace::::{closure#0}, !> 11: 0x7fffa867ed89 - std[4086331e48bff2f6]::panicking::begin_panic:: 12: 0x7fffa8298de9 - ::write_fmt 13: 0x7fffa829b7e2 - ::flush_delayed::, ::decorate>, &str> 14: 0x7fffa8288638 - ::drop 15: 0x7fffa5eb90e1 - core[fda266aeadfb153a]::ptr::drop_in_place:: 16: 0x7fffa5ebe9fd - core[fda266aeadfb153a]::ptr::drop_in_place:: 17: 0x7fffa5ec0d7c - core[fda266aeadfb153a]::ptr::drop_in_place:: 18: 0x7fffa5ebe41c - core[fda266aeadfb153a]::ptr::drop_in_place:: 19: 0x7fffa5eea05f - RINvCs6uSsza6NDuD_10rustc_span15with_source_mapINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedENCNCINvNtCsiwHPejSviHg_15rustc_interface9interface12run_compilerBJ_NCNvCs7PhwInflpyf_12rustc_driver12run_compilers_0E00EB 20: 0x7fffa5edbe3a - RINvMs_Cs9yvsqs6YnUZ_10scoped_tlsINtB5_9ScopedKeyNtCs6uSsza6NDuD_10rustc_span14SessionGlobalsE3setNCINvNtCsiwHPejSviHg_15rustc_interface9interface12run_compilerINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedENCNvCs7P 21: 0x7fffa5ef4ec9 - RINvNtNtCs5xszGlR5JQw_3std10sys_common9backtrace28___rust_begin_short_backtraceNCNCINvNtCsiwHPejSviHg_15rustc_interface4util31run_in_thread_pool_with_globalsNCINvNtB1o_9interface12run_compilerINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_ 22: 0x7fffa5ee9935 - RINvNtCs5xszGlR5JQw_3std9panicking3tryINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedEINtNtNtBF_5panic11unwind_safe16AssertUnwindSafeNCNCINvMNtB4_6threadNtB2S_7Builder16spawn_unchecked_NCNCINvNtCsiwHPejSviHg_15rustc_ 23: 0x7fffa5f0d770 - ::spawn_scoped::::{closure#0}, ()>::{closure#0}::{closure#0}, ()> 24: 0x7fffbe95b35b - as core::ops::function::FnOnce>::call_once::hfcd927f7aebafa45 25: 0x7fffbe9331d3 - std::sys::windows::thread::Thread::new::hf8f4c920eaebd965 26: 0x7ff815877614 - BaseThreadInitThunk 27: 0x7ff8166e26a1 - RtlUserThreadStart note: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md note: rustc 1.68.0-dev running on x86_64-pc-windows-msvc note: compiler flags: --crate-type lib -C opt-level=3 -C embed-bitcode=no -C debuginfo=0 -Z unstable-options -C incremental=[REDACTED] -C symbol-mangling-version=legacy -Z unstable-options -Z unstable-options -Z macro-backtrace -C split-debuginfo=packed -C target-feature=+crt-static -C prefer-dynamic -C embed-bitcode=yes -Z crate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/") -Z binary-dep-depinfo -Z force-unstable-if-unmarked note: some of the compiler flags provided by cargo are hidden query stack during panic: end of query stack error: could not compile `core` ``` After: ``` Compiling core v0.0.0 (C:\Users\Joshua Nelson\src\rust2\library\core) error: internal compiler error: no warnings or errors encountered even though `delayed_good_path_bugs` issued error: internal compiler error: oops | = note: delayed at 0: std::backtrace_rs::backtrace::trace_unsynchronized 1: std::backtrace::Backtrace::disabled 2: std::backtrace::Backtrace::force_capture 3: ::delay_good_path_bug::<&str> 4: ::enter::> 5: RINvMs2_NtCsiwHPejSviHg_15rustc_interface7queriesNtNtB8_9interface8Compiler5enterNCNCNvCs7PhwInflpyf_12rustc_driver12run_compilers_0s0_0INtNtCslM5znELOk5i_4core6result6ResultINtNtB2f_6option6OptionNtB6_6LinkerENtCshthk7JDUYGg_12rustc_errors15ErrorGuarante 6: RINvCs6uSsza6NDuD_10rustc_span15with_source_mapINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedENCNCINvNtCsiwHPejSviHg_15rustc_interface9interface12run_compilerBJ_NCNvCs7PhwInflpyf_12rustc_driver12run_compilers_0E00EB 7: RINvMs_Cs9yvsqs6YnUZ_10scoped_tlsINtB5_9ScopedKeyNtCs6uSsza6NDuD_10rustc_span14SessionGlobalsE3setNCINvNtCsiwHPejSviHg_15rustc_interface9interface12run_compilerINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedENCNvCs7P 8: RINvNtNtCs5xszGlR5JQw_3std10sys_common9backtrace28___rust_begin_short_backtraceNCNCINvNtCsiwHPejSviHg_15rustc_interface4util31run_in_thread_pool_with_globalsNCINvNtB1o_9interface12run_compilerINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_ 9: RINvNtCs5xszGlR5JQw_3std9panicking3tryINtNtCslM5znELOk5i_4core6result6ResultuNtCshthk7JDUYGg_12rustc_errors15ErrorGuaranteedEINtNtNtBF_5panic11unwind_safe16AssertUnwindSafeNCNCINvMNtB4_6threadNtB2S_7Builder16spawn_unchecked_NCNCINvNtCsiwHPejSviHg_15rustc_ 10: ::spawn_scoped::::{closure#0}, ()>::{closure#0}::{closure#0}, ()> 11: as core::ops::function::FnOnce>::call_once 12: std::sys::windows::thread::Thread::new 13: BaseThreadInitThunk 14: RtlUserThreadStart note: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md note: rustc 1.68.0-dev running on x86_64-pc-windows-msvc note: compiler flags: --crate-type lib -C opt-level=3 -C embed-bitcode=no -C debuginfo=0 -Z unstable-options -C incremental=[REDACTED] -C symbol-mangling-version=legacy -Z unstable-options -Z unstable-options -Z macro-backtrace -C split-debuginfo=packed -C target-feature=+crt-static -C prefer-dynamic -C embed-bitcode=yes -Z crate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/") -Z binary-dep-depinfo -Z force-unstable-if-unmarked note: some of the compiler flags provided by cargo are hidden query stack during panic: end of query stack error: could not compile `core` ``` --- compiler/rustc_driver/src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 711eed2b2723..236f66eae72c 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -1199,10 +1199,13 @@ static DEFAULT_HOOK: LazyLock) + Sync + Send + }; // Invoke the default handler, which prints the actual panic message and optionally a backtrace - (*DEFAULT_HOOK)(info); + // Don't do this for `ExplicitBug`, which has an unhelpful message and backtrace. + if !info.payload().is::() { + (*DEFAULT_HOOK)(info); - // Separate the output with an empty line - eprintln!(); + // Separate the output with an empty line + eprintln!(); + } // Print the ICE message report_ice(info, BUG_REPORT_URL); From 898b702695a3fc907e0fff5017dbf744a315b8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 22 Dec 2022 00:00:00 +0000 Subject: [PATCH 300/321] Fix the issue number in comment for as_local_call_operand --- compiler/rustc_mir_build/src/build/expr/as_operand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/build/expr/as_operand.rs b/compiler/rustc_mir_build/src/build/expr/as_operand.rs index c8610af7038d..dbcb0132c9f8 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_operand.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_operand.rs @@ -72,7 +72,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// will actually provide a pointer to the interior of the box, and not move the `dyn Debug` /// value to the stack. /// - /// See #68034 for more details. + /// See #68304 for more details. pub(crate) fn as_local_call_operand( &mut self, block: BasicBlock, From e4330295d9bcb8ab302000eae60cd8b9a71f2d7a Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 22 Dec 2022 12:57:47 -0600 Subject: [PATCH 301/321] Give a more helpful error for "trimmed_def_paths construted" --- Cargo.lock | 1 + compiler/rustc_error_messages/src/lib.rs | 2 +- compiler/rustc_middle/Cargo.toml | 2 ++ compiler/rustc_middle/src/ty/print/pretty.rs | 8 +++++++- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 75c3df5b2b74..6f2f8c26f642 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4039,6 +4039,7 @@ dependencies = [ "rustc_ast", "rustc_attr", "rustc_data_structures", + "rustc_error_messages", "rustc_errors", "rustc_feature", "rustc_graphviz", diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 418ba3c74d77..0b351b7afc72 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -380,7 +380,7 @@ impl> From for DiagnosticMessage { } } -/// A workaround for "good path" ICEs when formatting types in disables lints. +/// A workaround for "good path" ICEs when formatting types in disabled lints. /// /// Delays formatting until `.into(): DiagnosticMessage` is used. pub struct DelayDm(pub F); diff --git a/compiler/rustc_middle/Cargo.toml b/compiler/rustc_middle/Cargo.toml index cf1ab47de861..543bd56a20c1 100644 --- a/compiler/rustc_middle/Cargo.toml +++ b/compiler/rustc_middle/Cargo.toml @@ -18,6 +18,8 @@ rustc_ast = { path = "../rustc_ast" } rustc_attr = { path = "../rustc_attr" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_errors = { path = "../rustc_errors" } +# Used for intra-doc links +rustc_error_messages = { path = "../rustc_error_messages" } rustc_feature = { path = "../rustc_feature" } rustc_graphviz = { path = "../rustc_graphviz" } rustc_hir = { path = "../rustc_hir" } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index a1d53506707c..c49e75d68ad3 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2883,13 +2883,19 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N /// `std::vec::Vec` to just `Vec`, as long as there is no other `Vec` importable anywhere. /// /// The implementation uses similar import discovery logic to that of 'use' suggestions. +/// +/// See also [`DelayDm`](rustc_error_messages::DelayDm) and [`with_no_trimmed_paths`]. fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> FxHashMap { let mut map: FxHashMap = FxHashMap::default(); if let TrimmedDefPaths::GoodPath = tcx.sess.opts.trimmed_def_paths { + // Trimming paths is expensive and not optimized, since we expect it to only be used for error reporting. + // // For good paths causing this bug, the `rustc_middle::ty::print::with_no_trimmed_paths` // wrapper can be used to suppress this query, in exchange for full paths being formatted. - tcx.sess.delay_good_path_bug("trimmed_def_paths constructed"); + tcx.sess.delay_good_path_bug( + "trimmed_def_paths constructed but no error emitted; use `DelayDm` for lints or `with_no_trimmed_paths` for debugging", + ); } let unique_symbols_rev: &mut FxHashMap<(Namespace, Symbol), Option> = From 103816d704133fc954344d551af04cb333b03e94 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 22 Dec 2022 13:21:48 -0600 Subject: [PATCH 302/321] Avoid running the `Profile` step twice on `x setup` Prevents runs like the following: ``` $ x setup Welcome to the Rust project! What do you want to do with x.py? a) library: Contribute to the standard library b) compiler: Contribute to the compiler itself c) codegen: Contribute to the compiler, and also modify LLVM or codegen d) tools: Contribute to tools which depend on the compiler, but do not modify it directly (e.g. rustdoc, clippy, miri) e) user: Install Rust from source Please choose one (a/b/c/d/e): b To get started, try one of the following commands: - `x.py check` - `x.py build` - `x.py test` For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html `x.py` will now use the configuration at /home/nilsh/projects/rustfast/src/bootstrap/defaults/config.compiler.toml Welcome to the Rust project! What do you want to do with x.py? ... ``` --- src/bootstrap/setup.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bootstrap/setup.rs b/src/bootstrap/setup.rs index 57426ce3d510..cd360cbef965 100644 --- a/src/bootstrap/setup.rs +++ b/src/bootstrap/setup.rs @@ -96,6 +96,10 @@ impl Step for Profile { } fn make_run(run: RunConfig<'_>) { + if run.builder.config.dry_run() { + return; + } + // for Profile, `run.paths` will have 1 and only 1 element // this is because we only accept at most 1 path from user input. // If user calls `x.py setup` without arguments, the interactive TUI From f94c926aecf226a12ffc5c7f09156390dde545ca Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Mon, 28 Nov 2022 18:10:44 -0500 Subject: [PATCH 303/321] Support documenting Cargo The primary motivation is to have the cargo docs show up on https://doc.rust-lang.org/nightly/nightly-rustc/cargo, but as a nice side effect this makes `x doc cargo` work locally. --- Cargo.lock | 117 ++++++++++++++++++++++++++++++++++----- Cargo.toml | 2 + src/bootstrap/builder.rs | 1 + src/bootstrap/doc.rs | 63 +++++++++++++++++---- src/bootstrap/lib.rs | 1 + 5 files changed, 160 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d09068dbdf3..62ae0cd85eac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,6 +77,15 @@ dependencies = [ "yansi-term", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + [[package]] name = "ansi_term" version = "0.12.1" @@ -707,7 +716,7 @@ dependencies = [ "declare_clippy_lint", "if_chain", "itertools", - "pulldown-cmark", + "pulldown-cmark 0.9.2", "quine-mc_cluskey", "regex-syntax", "rustc-semver", @@ -1028,6 +1037,16 @@ dependencies = [ "quote", ] +[[package]] +name = "ctor" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "curl" version = "0.4.44" @@ -1104,6 +1123,12 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" + [[package]] name = "digest" version = "0.10.2" @@ -1683,6 +1708,21 @@ dependencies = [ "serde", ] +[[package]] +name = "handlebars" +version = "3.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4498fc115fa7d34de968184e473529abb40eeb6be8bc5f7faba3d08c316cb3e3" +dependencies = [ + "log", + "pest", + "pest_derive", + "quick-error 2.0.1", + "serde", + "serde_json", + "walkdir", +] + [[package]] name = "handlebars" version = "4.3.3" @@ -1794,7 +1834,7 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" dependencies = [ - "quick-error", + "quick-error 1.2.3", ] [[package]] @@ -1990,9 +2030,9 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e394faa0efb47f9f227f1cd89978f854542b318a6f64fa695489c9c993056656" +checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" dependencies = [ "libc", "windows-sys", @@ -2000,9 +2040,9 @@ dependencies = [ [[package]] name = "is-terminal" -version = "0.4.0" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aae5bc6e2eb41c9def29a3e0f1306382807764b9b53112030eff57435667352d" +checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" dependencies = [ "hermit-abi 0.2.6", "io-lifetimes", @@ -2221,9 +2261,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f9f08d8963a6c613f4b1a78f4f4a4dbfadf8e6545b2d72861731e4858b8b47f" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" [[package]] name = "litemap" @@ -2328,12 +2368,12 @@ dependencies = [ "clap_complete", "elasticlunr-rs", "env_logger 0.9.0", - "handlebars", + "handlebars 4.3.3", "lazy_static", "log", "memchr", "opener", - "pulldown-cmark", + "pulldown-cmark 0.9.2", "regex", "serde", "serde_json", @@ -2343,6 +2383,19 @@ dependencies = [ "topological-sort", ] +[[package]] +name = "mdman" +version = "0.1.0" +dependencies = [ + "anyhow", + "handlebars 3.5.5", + "pretty_assertions", + "pulldown-cmark 0.7.2", + "same-file", + "serde_json", + "url", +] + [[package]] name = "measureme" version = "10.1.0" @@ -2617,6 +2670,15 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" +[[package]] +name = "output_vt100" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66" +dependencies = [ + "winapi", +] + [[package]] name = "owo-colors" version = "3.4.0" @@ -2858,6 +2920,18 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "pretty_assertions" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f81e1644e1b54f5a68959a29aa86cde704219254669da328ecfdf6a1f09d427" +dependencies = [ + "ansi_term 0.11.0", + "ctor", + "difference", + "output_vt100", +] + [[package]] name = "pretty_env_logger" version = "0.4.0" @@ -2933,6 +3007,17 @@ dependencies = [ "cc", ] +[[package]] +name = "pulldown-cmark" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55" +dependencies = [ + "bitflags", + "memchr", + "unicase", +] + [[package]] name = "pulldown-cmark" version = "0.9.2" @@ -2956,6 +3041,12 @@ version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + [[package]] name = "quine-mc_cluskey" version = "0.2.4" @@ -4545,7 +4636,7 @@ dependencies = [ "itertools", "minifier", "once_cell", - "pulldown-cmark", + "pulldown-cmark 0.9.2", "rayon", "regex", "rustdoc-json-types", @@ -5375,7 +5466,7 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245da694cc7fc4729f3f418b304cb57789f1bed2a78c575407ab8a23f53cb4d3" dependencies = [ - "ansi_term", + "ansi_term 0.12.1", "lazy_static", "matchers", "parking_lot 0.11.2", @@ -5394,7 +5485,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ce989c9962c7f61fe084dd4a230eec784649dfc2392467c790007c3a6e134e7" dependencies = [ - "ansi_term", + "ansi_term 0.12.1", "atty", "tracing-core", "tracing-log", diff --git a/Cargo.toml b/Cargo.toml index 000c10a1f906..1cec4a84480c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,8 @@ members = [ "src/tools/cargo/crates/credential/cargo-credential-1password", "src/tools/cargo/crates/credential/cargo-credential-macos-keychain", "src/tools/cargo/crates/credential/cargo-credential-wincred", + "src/tools/cargo/crates/mdman", + # "src/tools/cargo/crates/resolver-tests", "src/tools/rustdoc", "src/tools/rls", "src/tools/rustfmt", diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index b9d06a77966d..eb03a45583e0 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -702,6 +702,7 @@ impl<'a> Builder<'a> { doc::RustdocBook, doc::RustByExample, doc::RustcBook, + doc::Cargo, doc::CargoBook, doc::Clippy, doc::ClippyBook, diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index 2c6fd1e1d4dd..5838049aa5c7 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -729,7 +729,7 @@ impl Step for Rustc { } macro_rules! tool_doc { - ($tool: ident, $should_run: literal, $path: literal, [$($krate: literal),+ $(,)?] $(,)?) => { + ($tool: ident, $should_run: literal, $path: literal, $(rustc_tool = $rustc_tool:literal, )? $(in_tree = $in_tree:literal, )? [$($krate: literal),+ $(,)?] $(,)?) => { #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub struct $tool { target: TargetSelection, @@ -763,13 +763,24 @@ macro_rules! tool_doc { let out = builder.compiler_doc_out(target); t!(fs::create_dir_all(&out)); - // Build rustc docs so that we generate relative links. - builder.ensure(Rustc { stage, target }); - // Rustdoc needs the rustc sysroot available to build. - // FIXME: is there a way to only ensure `check::Rustc` here? Last time I tried it failed - // with strange errors, but only on a full bors test ... let compiler = builder.compiler(stage, builder.config.build); - builder.ensure(compile::Rustc::new(compiler, target)); + builder.ensure(compile::Std::new(compiler, target)); + + if true $(&& $rustc_tool)? { + // Build rustc docs so that we generate relative links. + builder.ensure(Rustc { stage, target }); + + // Rustdoc needs the rustc sysroot available to build. + // FIXME: is there a way to only ensure `check::Rustc` here? Last time I tried it failed + // with strange errors, but only on a full bors test ... + builder.ensure(compile::Rustc::new(compiler, target)); + } + + let source_type = if true $(&& $in_tree)? { + SourceType::InTree + } else { + SourceType::Submodule + }; builder.info( &format!( @@ -781,9 +792,15 @@ macro_rules! tool_doc { ); // Symlink compiler docs to the output directory of rustdoc documentation. - let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc"); - t!(fs::create_dir_all(&out_dir)); - t!(symlink_dir_force(&builder.config, &out, &out_dir)); + let out_dirs = [ + builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc"), + // Cargo uses a different directory for proc macros. + builder.stage_out(compiler, Mode::ToolRustc).join("doc"), + ]; + for out_dir in out_dirs { + t!(fs::create_dir_all(&out_dir)); + t!(symlink_dir_force(&builder.config, &out, &out_dir)); + } // Build cargo command. let mut cargo = prepare_tool_cargo( @@ -793,7 +810,7 @@ macro_rules! tool_doc { target, "doc", $path, - SourceType::InTree, + source_type, &[], ); @@ -824,6 +841,30 @@ tool_doc!( ); tool_doc!(Clippy, "clippy", "src/tools/clippy", ["clippy_utils"]); tool_doc!(Miri, "miri", "src/tools/miri", ["miri"]); +tool_doc!( + Cargo, + "cargo", + "src/tools/cargo", + rustc_tool = false, + in_tree = false, + [ + "cargo", + "cargo-platform", + "cargo-util", + "crates-io", + "cargo-test-macro", + "cargo-test-support", + "cargo-credential", + "cargo-credential-1password", + "mdman", + // FIXME: this trips a license check in tidy. + // "resolver-tests", + // FIXME: we should probably document these, but they're different per-platform so we can't use `tool_doc`. + // "cargo-credential-gnome-secret", + // "cargo-credential-macos-keychain", + // "cargo-credential-wincred", + ] +); #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)] pub struct ErrorIndex { diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 47fb4a38d05b..35a58e11df92 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -202,6 +202,7 @@ const EXTRA_CHECK_CFGS: &[(Option, &'static str, Option<&[&'static str]>)] (None, "bootstrap", None), (Some(Mode::Rustc), "parallel_compiler", None), (Some(Mode::ToolRustc), "parallel_compiler", None), + (Some(Mode::ToolRustc), "emulate_second_only_system", None), (Some(Mode::Codegen), "parallel_compiler", None), (Some(Mode::Std), "stdarch_intel_sde", None), (Some(Mode::Std), "no_fp_fmt_parse", None), From 66ed1812cfaf3c7dd47643140f8e1380204f7234 Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Tue, 20 Dec 2022 23:30:14 +1300 Subject: [PATCH 304/321] docs/test: add UI test and long-form error docs for E0462 --- compiler/rustc_error_codes/src/error_codes.rs | 2 +- .../src/error_codes/E0462.md | 32 +++++++++++++++++++ src/test/ui/error-codes/E0462.rs | 11 +++++++ src/test/ui/error-codes/E0462.stderr | 13 ++++++++ .../error-codes/auxiliary/found-staticlib.rs | 4 +++ src/tools/tidy/src/error_codes_check.rs | 4 +-- 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0462.md create mode 100644 src/test/ui/error-codes/E0462.rs create mode 100644 src/test/ui/error-codes/E0462.stderr create mode 100644 src/test/ui/error-codes/auxiliary/found-staticlib.rs diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 4e149fc2b997..86c8b788e9b8 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -244,6 +244,7 @@ E0457: include_str!("./error_codes/E0457.md"), E0458: include_str!("./error_codes/E0458.md"), E0459: include_str!("./error_codes/E0459.md"), E0460: include_str!("./error_codes/E0460.md"), +E0462: include_str!("./error_codes/E0462.md"), E0463: include_str!("./error_codes/E0463.md"), E0464: include_str!("./error_codes/E0464.md"), E0466: include_str!("./error_codes/E0466.md"), @@ -594,7 +595,6 @@ E0791: include_str!("./error_codes/E0791.md"), // E0427, // merged into 530 // E0456, // plugin `..` is not available for triple `..` E0461, // couldn't find crate `..` with expected target triple .. - E0462, // found staticlib `..` instead of rlib or dylib E0465, // multiple .. candidates for `..` found // E0467, // removed // E0470, // removed diff --git a/compiler/rustc_error_codes/src/error_codes/E0462.md b/compiler/rustc_error_codes/src/error_codes/E0462.md new file mode 100644 index 000000000000..4509cc6fad2d --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0462.md @@ -0,0 +1,32 @@ +Found `staticlib` `..` instead of `rlib` or `dylib`. + +Consider the following two files: + +`a.rs` +```ignore (cannot-link-with-other-tests) +#![crate_type = "staticlib"] + +fn foo() {} +``` + +`main.rs` +```ignore (cannot-link-with-other-tests) +extern crate a; + +fn main() { + a::foo(); +} +``` + +Crate `a` is compiled as a `staticlib`. A `staticlib` is a system-dependant +library only intended for linking with non-Rust applications (C programs). Note +that `staticlib`s include all upstream dependencies (`core`, `std`, other user +dependencies, etc) which makes them significantly larger than `dylib`s: +prefer `staticlib` for linking with C programs. Learn more about different +`crate_type`s in [this section of the Reference](../reference/linkage.html). + +This error can be fixed by: + * Using [Cargo](../cargo/index.html), the Rust package manager, automatically + fixing this issue. + * Recompiling the crate as a `rlib` or `dylib`; formats suitable for Rust + linking. diff --git a/src/test/ui/error-codes/E0462.rs b/src/test/ui/error-codes/E0462.rs new file mode 100644 index 000000000000..f839ee783b53 --- /dev/null +++ b/src/test/ui/error-codes/E0462.rs @@ -0,0 +1,11 @@ +// aux-build:found-staticlib.rs + +// normalize-stderr-test: "\.nll/" -> "/" +// normalize-stderr-test: "\\\?\\" -> "" +// normalize-stderr-test: "(lib)?found_staticlib\.[a-z]+" -> "libfound_staticlib.somelib" + +extern crate found_staticlib; //~ ERROR E0462 + +fn main() { + found_staticlib::foo(); +} diff --git a/src/test/ui/error-codes/E0462.stderr b/src/test/ui/error-codes/E0462.stderr new file mode 100644 index 000000000000..43e27965ffc5 --- /dev/null +++ b/src/test/ui/error-codes/E0462.stderr @@ -0,0 +1,13 @@ +error[E0462]: found staticlib `found_staticlib` instead of rlib or dylib + --> $DIR/E0462.rs:7:1 + | +LL | extern crate found_staticlib; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: the following crate versions were found: + crate `found_staticlib`: $TEST_BUILD_DIR/error-codes/E0462/auxiliary/libfound_staticlib.somelib + = help: please recompile that crate using --crate-type lib + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0462`. diff --git a/src/test/ui/error-codes/auxiliary/found-staticlib.rs b/src/test/ui/error-codes/auxiliary/found-staticlib.rs new file mode 100644 index 000000000000..04e2c59789d0 --- /dev/null +++ b/src/test/ui/error-codes/auxiliary/found-staticlib.rs @@ -0,0 +1,4 @@ +// no-prefer-dynamic +#![crate_type = "staticlib"] + +pub fn foo() {} diff --git a/src/tools/tidy/src/error_codes_check.rs b/src/tools/tidy/src/error_codes_check.rs index 1b119e4113e3..49fc2ceb3a27 100644 --- a/src/tools/tidy/src/error_codes_check.rs +++ b/src/tools/tidy/src/error_codes_check.rs @@ -11,8 +11,8 @@ use regex::Regex; // A few of those error codes can't be tested but all the others can and *should* be tested! const EXEMPTED_FROM_TEST: &[&str] = &[ - "E0313", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523", "E0554", - "E0640", "E0717", "E0729", "E0789", + "E0313", "E0461", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523", "E0554", "E0640", + "E0717", "E0729", "E0789", ]; // Some error codes don't have any tests apparently... From d846cf0971044d4720551190ce65aa7cbb112220 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Thu, 22 Dec 2022 23:21:00 +0100 Subject: [PATCH 305/321] A few metadata nits --- compiler/rustc_metadata/src/locator.rs | 8 ++----- compiler/rustc_metadata/src/rmeta/decoder.rs | 24 ++++++++------------ compiler/rustc_metadata/src/rmeta/mod.rs | 5 ---- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index 15546092e41a..dda5f4bac428 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -1011,11 +1011,7 @@ impl CrateError { sess.emit_err(SymbolConflictsOthers { span, crate_name: root_name }); } CrateError::StableCrateIdCollision(crate_name0, crate_name1) => { - sess.emit_err(StableCrateIdCollision { - span, - crate_name0: crate_name0, - crate_name1: crate_name1, - }); + sess.emit_err(StableCrateIdCollision { span, crate_name0, crate_name1 }); } CrateError::DlOpen(s) | CrateError::DlSym(s) => { sess.emit_err(DlError { span, err: s }); @@ -1074,7 +1070,7 @@ impl CrateError { } sess.emit_err(NoCrateWithTriple { span, - crate_name: crate_name, + crate_name, locator_triple: locator.triple.triple(), add_info, found_crates, diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 4370d4bd758d..99d8225a4c39 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -78,10 +78,6 @@ pub(crate) struct CrateMetadata { blob: MetadataBlob, // --- Some data pre-decoded from the metadata blob, usually for performance --- - /// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this - /// lifetime is only used behind `LazyValue`, `LazyArray`, or `LazyTable`, and therefore acts like a - /// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt` - /// is being used to decode those values. root: CrateRoot, /// Trait impl data. /// FIXME: Used only from queries and can use query cache, @@ -688,10 +684,10 @@ impl MetadataBlob { pub(crate) fn get_root(&self) -> CrateRoot { let slice = &self.blob()[..]; let offset = METADATA_HEADER.len(); - let pos = (((slice[offset + 0] as u32) << 24) - | ((slice[offset + 1] as u32) << 16) - | ((slice[offset + 2] as u32) << 8) - | ((slice[offset + 3] as u32) << 0)) as usize; + + let pos_bytes = slice[offset..][..4].try_into().unwrap(); + let pos = u32::from_be_bytes(pos_bytes) as usize; + LazyValue::::from_position(NonZeroUsize::new(pos).unwrap()).decode(self) } @@ -702,16 +698,14 @@ impl MetadataBlob { writeln!(out, "hash {} stable_crate_id {:?}", root.hash, root.stable_crate_id)?; writeln!(out, "proc_macro {:?}", root.proc_macro_data.is_some())?; writeln!(out, "=External Dependencies=")?; + for (i, dep) in root.crate_deps.decode(self).enumerate() { + let CrateDep { name, extra_filename, hash, host_hash, kind } = dep; + let number = i + 1; + writeln!( out, - "{} {}{} hash {} host_hash {:?} kind {:?}", - i + 1, - dep.name, - dep.extra_filename, - dep.hash, - dep.host_hash, - dep.kind + "{number} {name}{extra_filename} hash {hash} host_hash {host_hash:?} kind {kind:?}" )?; } write!(out, "\n")?; diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 6b60577c9023..26a41f633fff 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -416,11 +416,6 @@ struct VariantData { is_non_exhaustive: bool, } -#[derive(TyEncodable, TyDecodable)] -struct GeneratorData<'tcx> { - layout: mir::GeneratorLayout<'tcx>, -} - // Tags used for encoding Spans: const TAG_VALID_SPAN_LOCAL: u8 = 0; const TAG_VALID_SPAN_FOREIGN: u8 = 1; From d60309b311fc2290cd197880b79be1c3a91266c1 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 22 Dec 2022 15:52:34 -0700 Subject: [PATCH 306/321] rustdoc: use a more evocative name for CSS/JS `#titles` This renames the ID, which is only used in search results, to `#search-tabs`. Also changes the `.count` to a span, so it doesn't need its display mode to be overridden. --- src/librustdoc/html/static/css/rustdoc.css | 9 ++-- src/librustdoc/html/static/js/search.js | 16 +++---- src/test/rustdoc-gui/cursor.goml | 4 +- src/test/rustdoc-gui/search-filter.goml | 6 +-- src/test/rustdoc-gui/search-keyboard.goml | 2 +- src/test/rustdoc-gui/search-result-color.goml | 14 +++--- .../search-result-description.goml | 2 +- .../search-result-go-to-first.goml | 2 +- .../rustdoc-gui/search-result-keyword.goml | 2 +- .../search-tab-change-title-fn-sig.goml | 46 +++++++++---------- 10 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 3ea83cc41ef7..e6e82602c3a1 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1261,14 +1261,14 @@ a.test-arrow:hover { margin-right: auto; } -#titles { +#search-tabs { display: flex; flex-direction: row; gap: 1px; margin-bottom: 4px; } -#titles > button { +#search-tabs button { text-align: center; font-size: 1.125rem; border: 0; @@ -1278,8 +1278,7 @@ a.test-arrow:hover { color: inherit; } -#titles > button > div.count { - display: inline-block; +#search-tabs .count { font-size: 1rem; color: var(--search-tab-title-count-color); } @@ -1706,7 +1705,7 @@ in storage.js display: none !important; } - #titles > button > div.count { + #search-tabs .count { display: block; } diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 23ae4e97082e..1b8822b0b2b7 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -49,7 +49,7 @@ function printTab(nb) { let iter = 0; let foundCurrentTab = false; let foundCurrentResultSet = false; - onEachLazy(document.getElementById("titles").childNodes, elem => { + onEachLazy(document.getElementById("search-tabs").childNodes, elem => { if (nb === iter) { addClass(elem, "selected"); foundCurrentTab = true; @@ -1490,7 +1490,7 @@ function initSearch(rawSearchIndex) { function focusSearchResult() { const target = searchState.focusedByTab[searchState.currentTab] || document.querySelectorAll(".search-results.active a").item(0) || - document.querySelectorAll("#titles > button").item(searchState.currentTab); + document.querySelectorAll("#search-tabs button").item(searchState.currentTab); searchState.focusedByTab[searchState.currentTab] = null; if (target) { target.focus(); @@ -1645,9 +1645,9 @@ function initSearch(rawSearchIndex) { function makeTabHeader(tabNb, text, nbElems) { if (searchState.currentTab === tabNb) { return ""; + " (" + nbElems + ")"; } - return ""; + return ""; } /** @@ -1712,12 +1712,12 @@ function initSearch(rawSearchIndex) { let output = `

Results${crates}

`; if (results.query.error !== null) { output += `

Query parser error: "${results.query.error}".

`; - output += "
" + + output += "
" + makeTabHeader(0, "In Names", ret_others[1]) + "
"; currentTab = 0; } else if (results.query.foundElems <= 1 && results.query.returned.length === 0) { - output += "
" + + output += "
" + makeTabHeader(0, "In Names", ret_others[1]) + makeTabHeader(1, "In Parameters", ret_in_args[1]) + makeTabHeader(2, "In Return Types", ret_returned[1]) + @@ -1727,7 +1727,7 @@ function initSearch(rawSearchIndex) { results.query.elems.length === 0 ? "In Function Return Types" : results.query.returned.length === 0 ? "In Function Parameters" : "In Function Signatures"; - output += "
" + + output += "
" + makeTabHeader(0, signatureTabTitle, ret_others[1]) + "
"; currentTab = 0; @@ -1747,7 +1747,7 @@ function initSearch(rawSearchIndex) { search.appendChild(resultsElem); // Reset focused elements. searchState.showResults(search); - const elems = document.getElementById("titles").childNodes; + const elems = document.getElementById("search-tabs").childNodes; searchState.focusedByTab = []; let i = 0; for (const elem of elems) { diff --git a/src/test/rustdoc-gui/cursor.goml b/src/test/rustdoc-gui/cursor.goml index cb6716a76f5c..59b1397970b5 100644 --- a/src/test/rustdoc-gui/cursor.goml +++ b/src/test/rustdoc-gui/cursor.goml @@ -12,8 +12,8 @@ write: (".search-input", "Foo") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" -assert-css: ("#titles > button", {"cursor": "pointer"}) +wait-for: "#search-tabs" +assert-css: ("#search-tabs > button", {"cursor": "pointer"}) // mobile sidebar toggle button size: (500, 700) diff --git a/src/test/rustdoc-gui/search-filter.goml b/src/test/rustdoc-gui/search-filter.goml index e0228694eec5..e556da0c54eb 100644 --- a/src/test/rustdoc-gui/search-filter.goml +++ b/src/test/rustdoc-gui/search-filter.goml @@ -5,7 +5,7 @@ write: (".search-input", "test") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-text: ("#results .externcrate", "test_docs") wait-for: "#crate-search" @@ -17,7 +17,7 @@ press-key: "ArrowDown" press-key: "ArrowDown" press-key: "Enter" // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-document-property: ({"URL": "&filter-crate="}, CONTAINS) // We check that there is no more "test_docs" appearing. assert-false: "#results .externcrate" @@ -41,7 +41,7 @@ press-key: "ArrowUp" press-key: "ArrowUp" press-key: "Enter" // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-property: ("#crate-search", {"value": "all crates"}) // Checking that the URL parameter is taken into account for crate filtering. diff --git a/src/test/rustdoc-gui/search-keyboard.goml b/src/test/rustdoc-gui/search-keyboard.goml index be642fc49971..ed975664c66a 100644 --- a/src/test/rustdoc-gui/search-keyboard.goml +++ b/src/test/rustdoc-gui/search-keyboard.goml @@ -5,7 +5,7 @@ write: (".search-input", "Foo") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" // Now use the keyboard commands to switch to the third result. press-key: "ArrowDown" diff --git a/src/test/rustdoc-gui/search-result-color.goml b/src/test/rustdoc-gui/search-result-color.goml index d124045608c1..3c5fe9b74b74 100644 --- a/src/test/rustdoc-gui/search-result-color.goml +++ b/src/test/rustdoc-gui/search-result-color.goml @@ -65,9 +65,9 @@ local-storage: { reload: // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-css: ( - "#titles > button > div.count", + "#search-tabs > button > .count", {"color": "rgb(136, 136, 136)"}, ALL, ) @@ -182,9 +182,9 @@ local-storage: { reload: // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-css: ( - "#titles > button > div.count", + "#search-tabs > button > .count", {"color": "rgb(136, 136, 136)"}, ALL, ) @@ -284,9 +284,9 @@ local-storage: {"rustdoc-theme": "light", "rustdoc-use-system-theme": "false"} reload: // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-css: ( - "#titles > button > div.count", + "#search-tabs > button > .count", {"color": "rgb(136, 136, 136)"}, ALL, ) @@ -396,7 +396,7 @@ define-function: ( // To be SURE that the search will be run. ("press-key", 'Enter'), // Waiting for the search results to appear... - ("wait-for", "#titles"), + ("wait-for", "#search-tabs"), // Checking that the colors for the alias element are the ones expected. ("assert-css", (".result-name > .alias", {"color": |alias|})), ("assert-css", (".result-name > .alias > .grey", {"color": |grey|})), diff --git a/src/test/rustdoc-gui/search-result-description.goml b/src/test/rustdoc-gui/search-result-description.goml index 53a335b6335f..9fa2108045d3 100644 --- a/src/test/rustdoc-gui/search-result-description.goml +++ b/src/test/rustdoc-gui/search-result-description.goml @@ -1,5 +1,5 @@ // This test is to ensure that the codeblocks are correctly rendered in the search results. goto: "file://" + |DOC_PATH| + "/test_docs/index.html?search=some_more_function" // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-text: (".search-results .desc code", "format!") diff --git a/src/test/rustdoc-gui/search-result-go-to-first.goml b/src/test/rustdoc-gui/search-result-go-to-first.goml index eeddf5ef6e80..994fd87c9966 100644 --- a/src/test/rustdoc-gui/search-result-go-to-first.goml +++ b/src/test/rustdoc-gui/search-result-go-to-first.goml @@ -8,7 +8,7 @@ assert-text-false: (".fqn", "Struct test_docs::Foo") // We now check that we land on the search result page if "go_to_first" isn't set. goto: "file://" + |DOC_PATH| + "/test_docs/index.html?search=struct%3AFoo" // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" assert-text-false: (".fqn", "Struct test_docs::Foo") // Ensure that the search results are displayed, not the "normal" content. assert-css: ("#main-content", {"display": "none"}) diff --git a/src/test/rustdoc-gui/search-result-keyword.goml b/src/test/rustdoc-gui/search-result-keyword.goml index 66e63155a4ea..8c3577d9fd3d 100644 --- a/src/test/rustdoc-gui/search-result-keyword.goml +++ b/src/test/rustdoc-gui/search-result-keyword.goml @@ -4,7 +4,7 @@ write: (".search-input", "CookieMonster") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" +wait-for: "#search-tabs" // Note: The two next assert commands could be merged as one but readability would be // less good. // diff --git a/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml b/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml index a19dc6a8b402..1433dc4d7e5e 100644 --- a/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml +++ b/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml @@ -5,21 +5,21 @@ write: (".search-input", "Foo") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" -assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) -assert-text: ("#titles > button:nth-of-type(1)", "In Names", STARTS_WITH) +wait-for: "#search-tabs" +assert-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) +assert-text: ("#search-tabs > button:nth-of-type(1)", "In Names", STARTS_WITH) assert: "input.search-input:focus" // Use left-right keys press-key: "ArrowDown" assert: "#results > .search-results.active > a:nth-of-type(1):focus" press-key: "ArrowRight" -wait-for-attribute: ("#titles > button:nth-of-type(2)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(2)", {"class": "selected"}) press-key: "ArrowRight" -wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(3)", {"class": "selected"}) press-key: "ArrowRight" -wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) press-key: "ArrowLeft" -wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(3)", {"class": "selected"}) // Now try search-by-return goto: "file://" + |DOC_PATH| + "/test_docs/index.html" @@ -27,21 +27,21 @@ write: (".search-input", "-> String") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" -assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) -assert-text: ("#titles > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH) +wait-for: "#search-tabs" +assert-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) +assert-text: ("#search-tabs > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH) assert: "input.search-input:focus" // Use left-right keys press-key: "ArrowDown" assert: "#results > .search-results.active > a:nth-of-type(1):focus" press-key: "ArrowRight" -wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) press-key: "ArrowRight" -wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) press-key: "ArrowRight" -wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) press-key: "ArrowLeft" -wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) +wait-for-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) // Try with a search-by-return with no results goto: "file://" + |DOC_PATH| + "/test_docs/index.html" @@ -49,9 +49,9 @@ write: (".search-input", "-> Something") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" -assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) -assert-text: ("#titles > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH) +wait-for: "#search-tabs" +assert-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) +assert-text: ("#search-tabs > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH) // Try with a search-by-parameter goto: "file://" + |DOC_PATH| + "/test_docs/index.html" @@ -59,9 +59,9 @@ write: (".search-input", "usize pattern") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" -assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) -assert-text: ("#titles > button:nth-of-type(1)", "In Function Parameters", STARTS_WITH) +wait-for: "#search-tabs" +assert-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) +assert-text: ("#search-tabs > button:nth-of-type(1)", "In Function Parameters", STARTS_WITH) // Try with a search-by-parameter-and-return goto: "file://" + |DOC_PATH| + "/test_docs/index.html" @@ -69,6 +69,6 @@ write: (".search-input", "pattern -> str") // To be SURE that the search will be run. press-key: 'Enter' // Waiting for the search results to appear... -wait-for: "#titles" -assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"}) -assert-text: ("#titles > button:nth-of-type(1)", "In Function Signatures", STARTS_WITH) +wait-for: "#search-tabs" +assert-attribute: ("#search-tabs > button:nth-of-type(1)", {"class": "selected"}) +assert-text: ("#search-tabs > button:nth-of-type(1)", "In Function Signatures", STARTS_WITH) From 7c6aa7c59d3eb75fa00a04d5a6946c16098a902b Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 22 Dec 2022 17:17:32 -0700 Subject: [PATCH 307/321] rustdoc: fix race condition in GUI test --- src/test/rustdoc-gui/basic-code.goml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/rustdoc-gui/basic-code.goml b/src/test/rustdoc-gui/basic-code.goml index f4ba5a128459..108cf8abcb52 100644 --- a/src/test/rustdoc-gui/basic-code.goml +++ b/src/test/rustdoc-gui/basic-code.goml @@ -1,3 +1,4 @@ goto: "file://" + |DOC_PATH| + "/test_docs/index.html" click: ".srclink" +wait-for: ".src-line-numbers" assert-count: (".src-line-numbers", 1) From 81cde69a2ff7ef24de9e28917936cc60c3482127 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 22 Dec 2022 18:17:22 +0000 Subject: [PATCH 308/321] Eagerly evaluate args --- compiler/rustc_hir_typeck/src/callee.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index c54352779137..af14ee08a998 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -399,7 +399,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } ty::FnPtr(sig) => (sig, None), _ => { - let mut skip_first_expr = false; + for arg in arg_exprs { + self.check_expr(arg); + } + if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &callee_expr.kind && let [segment] = path.segments && let Some(mut diag) = self @@ -422,16 +425,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return ty; } else { diag.emit(); - skip_first_expr = true; } } let err = self.report_invalid_callee(call_expr, callee_expr, callee_ty, arg_exprs); - for arg in arg_exprs.iter().skip(skip_first_expr as usize) { - self.check_expr(arg); - } - return self.tcx.ty_error_with_guaranteed(err); } }; @@ -492,9 +490,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expected: Expectation<'tcx>, ) -> Option> { if let [callee_expr, rest @ ..] = arg_exprs { - // This may happen recursively -- if so, avoid repeatedly checking the expr. - let callee_ty = self.typeck_results.borrow().expr_ty_adjusted_opt(callee_expr); - let callee_ty = callee_ty.unwrap_or_else(|| self.check_expr(callee_expr)); + let callee_ty = self.typeck_results.borrow().expr_ty_adjusted_opt(callee_expr)?; + // First, do a probe with `IsSuggestion(true)` to avoid emitting // any strange errors. If it's successful, then we'll do a true // method lookup. From 69abe4458e9c2f348933e8ecf939e9b38ff79625 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 22 Dec 2022 18:36:46 +0000 Subject: [PATCH 309/321] Add tests --- src/test/ui/typeck/check-args-on-fn-err-2.rs | 5 ++++ .../ui/typeck/check-args-on-fn-err-2.stderr | 23 +++++++++++++++++++ src/test/ui/typeck/check-args-on-fn-err.rs | 6 +++++ .../ui/typeck/check-args-on-fn-err.stderr | 9 ++++++++ 4 files changed, 43 insertions(+) create mode 100644 src/test/ui/typeck/check-args-on-fn-err-2.rs create mode 100644 src/test/ui/typeck/check-args-on-fn-err-2.stderr create mode 100644 src/test/ui/typeck/check-args-on-fn-err.rs create mode 100644 src/test/ui/typeck/check-args-on-fn-err.stderr diff --git a/src/test/ui/typeck/check-args-on-fn-err-2.rs b/src/test/ui/typeck/check-args-on-fn-err-2.rs new file mode 100644 index 000000000000..af57dbe33177 --- /dev/null +++ b/src/test/ui/typeck/check-args-on-fn-err-2.rs @@ -0,0 +1,5 @@ +fn main() { + a((), 1i32 == 2u32); + //~^ ERROR cannot find function `a` in this scope + //~| ERROR mismatched types +} diff --git a/src/test/ui/typeck/check-args-on-fn-err-2.stderr b/src/test/ui/typeck/check-args-on-fn-err-2.stderr new file mode 100644 index 000000000000..301bb88dbacf --- /dev/null +++ b/src/test/ui/typeck/check-args-on-fn-err-2.stderr @@ -0,0 +1,23 @@ +error[E0308]: mismatched types + --> $DIR/check-args-on-fn-err-2.rs:2:19 + | +LL | a((), 1i32 == 2u32); + | ---- ^^^^ expected `i32`, found `u32` + | | + | expected because this is `i32` + | +help: change the type of the numeric literal from `u32` to `i32` + | +LL | a((), 1i32 == 2i32); + | ~~~ + +error[E0425]: cannot find function `a` in this scope + --> $DIR/check-args-on-fn-err-2.rs:2:5 + | +LL | a((), 1i32 == 2u32); + | ^ not found in this scope + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0308, E0425. +For more information about an error, try `rustc --explain E0308`. diff --git a/src/test/ui/typeck/check-args-on-fn-err.rs b/src/test/ui/typeck/check-args-on-fn-err.rs new file mode 100644 index 000000000000..04b98ddd9529 --- /dev/null +++ b/src/test/ui/typeck/check-args-on-fn-err.rs @@ -0,0 +1,6 @@ +fn main() { + unknown(1, |glyf| { + //~^ ERROR: cannot find function `unknown` in this scope + let actual = glyf; + }); +} diff --git a/src/test/ui/typeck/check-args-on-fn-err.stderr b/src/test/ui/typeck/check-args-on-fn-err.stderr new file mode 100644 index 000000000000..864d33e0e93b --- /dev/null +++ b/src/test/ui/typeck/check-args-on-fn-err.stderr @@ -0,0 +1,9 @@ +error[E0425]: cannot find function `unknown` in this scope + --> $DIR/check-args-on-fn-err.rs:2:5 + | +LL | unknown(1, |glyf| { + | ^^^^^^^ not found in this scope + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0425`. From 51fe24873fbd255e80063a97ecd406726c1e6675 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Mon, 19 Dec 2022 14:52:02 +0530 Subject: [PATCH 310/321] Use u32 methods instead of manual shifting Switch to `to_be_bytes()` and `from_be_bytes()` instead of manual shifting This was suggested [`here`](https://github.com/rust-lang/rust/pull/105145#discussion_r1051418964) Signed-off-by: Ayush Singh --- src/tools/remote-test-server/src/main.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/tools/remote-test-server/src/main.rs b/src/tools/remote-test-server/src/main.rs index c1450aedc314..3d61a0675590 100644 --- a/src/tools/remote-test-server/src/main.rs +++ b/src/tools/remote-test-server/src/main.rs @@ -365,13 +365,7 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex) { loop { let n = t!(src.read(&mut b)); let mut dst = dst.lock().unwrap(); - t!(dst.write_all(&[ - which, - (n >> 24) as u8, - (n >> 16) as u8, - (n >> 8) as u8, - (n >> 0) as u8, - ])); + t!(dst.write_all(&create_header(which, n as u32))); if n > 0 { t!(dst.write_all(&b[..n])); } else { @@ -383,7 +377,7 @@ fn my_copy(src: &mut dyn Read, which: u8, dst: &Mutex) { fn batch_copy(buf: &[u8], which: u8, dst: &Mutex) { let n = buf.len(); let mut dst = dst.lock().unwrap(); - t!(dst.write_all(&[which, (n >> 24) as u8, (n >> 16) as u8, (n >> 8) as u8, (n >> 0) as u8,])); + t!(dst.write_all(&create_header(which, n as u32))); if n > 0 { t!(dst.write_all(buf)); // Marking buf finished @@ -391,11 +385,13 @@ fn batch_copy(buf: &[u8], which: u8, dst: &Mutex) { } } +const fn create_header(which: u8, n: u32) -> [u8; 5] { + let bytes = n.to_be_bytes(); + [which, bytes[0], bytes[1], bytes[2], bytes[3]] +} + fn read_u32(r: &mut dyn Read) -> u32 { let mut len = [0; 4]; t!(r.read_exact(&mut len)); - ((len[0] as u32) << 24) - | ((len[1] as u32) << 16) - | ((len[2] as u32) << 8) - | ((len[3] as u32) << 0) + u32::from_be_bytes(len) } From d4344d2f91de09ef35494585d92eb66e284890f3 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 23 Dec 2022 08:05:00 -0300 Subject: [PATCH 311/321] Move tests --- src/test/ui/{issues => fn}/issue-3044.rs | 0 src/test/ui/{issues => fn}/issue-3044.stderr | 0 src/test/ui/{issues => fn}/issue-3904.rs | 0 src/test/ui/issues/issue-6470.rs | 17 ----------------- .../issue-36381.rs | 0 src/test/ui/{issues => macros}/issue-25385.rs | 0 .../ui/{issues => macros}/issue-25385.stderr | 0 src/test/ui/{issues => match}/issue-5530.rs | 0 .../auxiliary/issue-30535.rs | 0 src/test/ui/{issues => resolve}/issue-30535.rs | 0 .../ui/{issues => resolve}/issue-30535.stderr | 0 .../ui/{issues => resolve}/issue-39559-2.rs | 0 .../ui/{issues => resolve}/issue-39559-2.stderr | 0 src/test/ui/{issues => resolve}/issue-39559.rs | 0 .../ui/{issues => resolve}/issue-39559.stderr | 0 src/test/ui/{issues => static}/issue-18118-2.rs | 0 .../ui/{issues => static}/issue-18118-2.stderr | 0 src/test/ui/{issues => static}/issue-18118.rs | 0 .../ui/{issues => static}/issue-18118.stderr | 0 .../ui/{issues => test-attrs}/issue-52557.rs | 0 src/test/ui/{issues => typeck}/issue-22375.rs | 0 src/tools/tidy/src/ui_tests.rs | 2 +- 22 files changed, 1 insertion(+), 18 deletions(-) rename src/test/ui/{issues => fn}/issue-3044.rs (100%) rename src/test/ui/{issues => fn}/issue-3044.stderr (100%) rename src/test/ui/{issues => fn}/issue-3904.rs (100%) delete mode 100644 src/test/ui/issues/issue-6470.rs rename src/test/ui/{issues => late-bound-lifetimes}/issue-36381.rs (100%) rename src/test/ui/{issues => macros}/issue-25385.rs (100%) rename src/test/ui/{issues => macros}/issue-25385.stderr (100%) rename src/test/ui/{issues => match}/issue-5530.rs (100%) rename src/test/ui/{issues => resolve}/auxiliary/issue-30535.rs (100%) rename src/test/ui/{issues => resolve}/issue-30535.rs (100%) rename src/test/ui/{issues => resolve}/issue-30535.stderr (100%) rename src/test/ui/{issues => resolve}/issue-39559-2.rs (100%) rename src/test/ui/{issues => resolve}/issue-39559-2.stderr (100%) rename src/test/ui/{issues => resolve}/issue-39559.rs (100%) rename src/test/ui/{issues => resolve}/issue-39559.stderr (100%) rename src/test/ui/{issues => static}/issue-18118-2.rs (100%) rename src/test/ui/{issues => static}/issue-18118-2.stderr (100%) rename src/test/ui/{issues => static}/issue-18118.rs (100%) rename src/test/ui/{issues => static}/issue-18118.stderr (100%) rename src/test/ui/{issues => test-attrs}/issue-52557.rs (100%) rename src/test/ui/{issues => typeck}/issue-22375.rs (100%) diff --git a/src/test/ui/issues/issue-3044.rs b/src/test/ui/fn/issue-3044.rs similarity index 100% rename from src/test/ui/issues/issue-3044.rs rename to src/test/ui/fn/issue-3044.rs diff --git a/src/test/ui/issues/issue-3044.stderr b/src/test/ui/fn/issue-3044.stderr similarity index 100% rename from src/test/ui/issues/issue-3044.stderr rename to src/test/ui/fn/issue-3044.stderr diff --git a/src/test/ui/issues/issue-3904.rs b/src/test/ui/fn/issue-3904.rs similarity index 100% rename from src/test/ui/issues/issue-3904.rs rename to src/test/ui/fn/issue-3904.rs diff --git a/src/test/ui/issues/issue-6470.rs b/src/test/ui/issues/issue-6470.rs deleted file mode 100644 index 8c6192a59dba..000000000000 --- a/src/test/ui/issues/issue-6470.rs +++ /dev/null @@ -1,17 +0,0 @@ -// build-pass -#![allow(dead_code)] -#![allow(improper_ctypes)] -// pretty-expanded FIXME #23616 -#![allow(non_snake_case)] - -pub mod Bar { - pub struct Foo { - v: isize, - } - - extern "C" { - pub fn foo(v: *const Foo) -> Foo; - } -} - -pub fn main() {} diff --git a/src/test/ui/issues/issue-36381.rs b/src/test/ui/late-bound-lifetimes/issue-36381.rs similarity index 100% rename from src/test/ui/issues/issue-36381.rs rename to src/test/ui/late-bound-lifetimes/issue-36381.rs diff --git a/src/test/ui/issues/issue-25385.rs b/src/test/ui/macros/issue-25385.rs similarity index 100% rename from src/test/ui/issues/issue-25385.rs rename to src/test/ui/macros/issue-25385.rs diff --git a/src/test/ui/issues/issue-25385.stderr b/src/test/ui/macros/issue-25385.stderr similarity index 100% rename from src/test/ui/issues/issue-25385.stderr rename to src/test/ui/macros/issue-25385.stderr diff --git a/src/test/ui/issues/issue-5530.rs b/src/test/ui/match/issue-5530.rs similarity index 100% rename from src/test/ui/issues/issue-5530.rs rename to src/test/ui/match/issue-5530.rs diff --git a/src/test/ui/issues/auxiliary/issue-30535.rs b/src/test/ui/resolve/auxiliary/issue-30535.rs similarity index 100% rename from src/test/ui/issues/auxiliary/issue-30535.rs rename to src/test/ui/resolve/auxiliary/issue-30535.rs diff --git a/src/test/ui/issues/issue-30535.rs b/src/test/ui/resolve/issue-30535.rs similarity index 100% rename from src/test/ui/issues/issue-30535.rs rename to src/test/ui/resolve/issue-30535.rs diff --git a/src/test/ui/issues/issue-30535.stderr b/src/test/ui/resolve/issue-30535.stderr similarity index 100% rename from src/test/ui/issues/issue-30535.stderr rename to src/test/ui/resolve/issue-30535.stderr diff --git a/src/test/ui/issues/issue-39559-2.rs b/src/test/ui/resolve/issue-39559-2.rs similarity index 100% rename from src/test/ui/issues/issue-39559-2.rs rename to src/test/ui/resolve/issue-39559-2.rs diff --git a/src/test/ui/issues/issue-39559-2.stderr b/src/test/ui/resolve/issue-39559-2.stderr similarity index 100% rename from src/test/ui/issues/issue-39559-2.stderr rename to src/test/ui/resolve/issue-39559-2.stderr diff --git a/src/test/ui/issues/issue-39559.rs b/src/test/ui/resolve/issue-39559.rs similarity index 100% rename from src/test/ui/issues/issue-39559.rs rename to src/test/ui/resolve/issue-39559.rs diff --git a/src/test/ui/issues/issue-39559.stderr b/src/test/ui/resolve/issue-39559.stderr similarity index 100% rename from src/test/ui/issues/issue-39559.stderr rename to src/test/ui/resolve/issue-39559.stderr diff --git a/src/test/ui/issues/issue-18118-2.rs b/src/test/ui/static/issue-18118-2.rs similarity index 100% rename from src/test/ui/issues/issue-18118-2.rs rename to src/test/ui/static/issue-18118-2.rs diff --git a/src/test/ui/issues/issue-18118-2.stderr b/src/test/ui/static/issue-18118-2.stderr similarity index 100% rename from src/test/ui/issues/issue-18118-2.stderr rename to src/test/ui/static/issue-18118-2.stderr diff --git a/src/test/ui/issues/issue-18118.rs b/src/test/ui/static/issue-18118.rs similarity index 100% rename from src/test/ui/issues/issue-18118.rs rename to src/test/ui/static/issue-18118.rs diff --git a/src/test/ui/issues/issue-18118.stderr b/src/test/ui/static/issue-18118.stderr similarity index 100% rename from src/test/ui/issues/issue-18118.stderr rename to src/test/ui/static/issue-18118.stderr diff --git a/src/test/ui/issues/issue-52557.rs b/src/test/ui/test-attrs/issue-52557.rs similarity index 100% rename from src/test/ui/issues/issue-52557.rs rename to src/test/ui/test-attrs/issue-52557.rs diff --git a/src/test/ui/issues/issue-22375.rs b/src/test/ui/typeck/issue-22375.rs similarity index 100% rename from src/test/ui/issues/issue-22375.rs rename to src/test/ui/typeck/issue-22375.rs diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index 19e2528bb240..f746bdeffd7c 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -10,7 +10,7 @@ use std::path::Path; const ENTRY_LIMIT: usize = 1000; // FIXME: The following limits should be reduced eventually. const ROOT_ENTRY_LIMIT: usize = 939; -const ISSUES_ENTRY_LIMIT: usize = 2040; +const ISSUES_ENTRY_LIMIT: usize = 2020; fn check_entries(path: &Path, bad: &mut bool) { for dir in Walk::new(&path.join("test/ui")) { From f8b30084ac5ae2845175a89fa66f174d6eb871b2 Mon Sep 17 00:00:00 2001 From: gimbles Date: Fri, 23 Dec 2022 18:39:49 +0530 Subject: [PATCH 312/321] Use DepKind instead of &str --- compiler/rustc_middle/src/values.rs | 23 ++-- compiler/rustc_query_impl/src/plumbing.rs | 24 ++-- .../rustc_query_system/src/query/config.rs | 2 +- compiler/rustc_query_system/src/query/job.rs | 104 ++++++++++-------- compiler/rustc_query_system/src/query/mod.rs | 13 ++- .../rustc_query_system/src/query/plumbing.rs | 79 ++++++------- compiler/rustc_query_system/src/values.rs | 10 +- 7 files changed, 141 insertions(+), 114 deletions(-) diff --git a/compiler/rustc_middle/src/values.rs b/compiler/rustc_middle/src/values.rs index 70b98e59a8be..c242be570312 100644 --- a/compiler/rustc_middle/src/values.rs +++ b/compiler/rustc_middle/src/values.rs @@ -1,3 +1,4 @@ +use crate::dep_graph::DepKind; use rustc_data_structures::fx::FxHashSet; use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan}; use rustc_hir as hir; @@ -11,16 +12,16 @@ use rustc_span::Span; use std::fmt::Write; -impl<'tcx> Value> for Ty<'_> { - fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self { +impl<'tcx> Value, DepKind> for Ty<'_> { + fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self { // SAFETY: This is never called when `Self` is not `Ty<'tcx>`. // FIXME: Represent the above fact in the trait system somehow. unsafe { std::mem::transmute::, Ty<'_>>(tcx.ty_error()) } } } -impl<'tcx> Value> for ty::SymbolName<'_> { - fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self { +impl<'tcx> Value, DepKind> for ty::SymbolName<'_> { + fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self { // SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`. // FIXME: Represent the above fact in the trait system somehow. unsafe { @@ -31,12 +32,12 @@ impl<'tcx> Value> for ty::SymbolName<'_> { } } -impl<'tcx> Value> for ty::Binder<'_, ty::FnSig<'_>> { - fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo]) -> Self { +impl<'tcx> Value, DepKind> for ty::Binder<'_, ty::FnSig<'_>> { + fn from_cycle_error(tcx: TyCtxt<'tcx>, stack: &[QueryInfo]) -> Self { let err = tcx.ty_error(); let arity = if let Some(frame) = stack.get(0) - && frame.query.name == "fn_sig" + && frame.query.dep_kind == DepKind::fn_sig && let Some(def_id) = frame.query.def_id && let Some(node) = tcx.hir().get_if_local(def_id) && let Some(sig) = node.fn_sig() @@ -61,12 +62,12 @@ impl<'tcx> Value> for ty::Binder<'_, ty::FnSig<'_>> { } } -impl<'tcx> Value> for Representability { - fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo]) -> Self { +impl<'tcx> Value, DepKind> for Representability { + fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo]) -> Self { let mut item_and_field_ids = Vec::new(); let mut representable_ids = FxHashSet::default(); for info in cycle { - if info.query.name == "representability" + if info.query.dep_kind == DepKind::representability && let Some(field_id) = info.query.def_id && let Some(field_id) = field_id.as_local() && let Some(DefKind::Field) = info.query.def_kind @@ -80,7 +81,7 @@ impl<'tcx> Value> for Representability { } } for info in cycle { - if info.query.name == "representability_adt_ty" + if info.query.dep_kind == DepKind::representability_adt_ty && let Some(def_id) = info.query.ty_adt_id && let Some(def_id) = def_id.as_local() && !item_and_field_ids.iter().any(|&(id, _)| id == def_id) diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 8d5d84c5db48..9ffcc5672cc3 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -66,7 +66,7 @@ impl QueryContext for QueryCtxt<'_> { tls::with_related_context(**self, |icx| icx.query) } - fn try_collect_active_jobs(&self) -> Option { + fn try_collect_active_jobs(&self) -> Option> { self.queries.try_collect_active_jobs(**self) } @@ -195,7 +195,7 @@ impl<'tcx> QueryCtxt<'tcx> { #[derive(Clone, Copy)] pub(crate) struct QueryStruct<'tcx> { - pub try_collect_active_jobs: fn(QueryCtxt<'tcx>, &mut QueryMap) -> Option<()>, + pub try_collect_active_jobs: fn(QueryCtxt<'tcx>, &mut QueryMap) -> Option<()>, pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache), pub encode_query_results: Option, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>, @@ -313,7 +313,7 @@ pub(crate) fn create_query_frame< key: K, kind: DepKind, name: &'static str, -) -> QueryStackFrame { +) -> QueryStackFrame { // Disable visible paths printing for performance reasons. // Showing visible path instead of any path is not that important in production. let description = ty::print::with_no_visible_paths!( @@ -346,7 +346,7 @@ pub(crate) fn create_query_frame< }; let ty_adt_id = key.ty_adt_id(); - QueryStackFrame::new(name, description, span, def_id, def_kind, ty_adt_id, hash) + QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_adt_id, hash) } fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) @@ -378,7 +378,7 @@ fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool where Q: QueryConfig>, Q::Key: DepNodeParams>, - Q::Value: Value>, + Q::Value: Value, DepKind>, { // We must avoid ever having to call `force_from_dep_node()` for a // `DepNode::codegen_unit`: @@ -402,7 +402,7 @@ where #[cfg(debug_assertions)] let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered(); let tcx = QueryCtxt::from_tcx(tcx); - force_query::(tcx, key, dep_node); + force_query::(tcx, key, dep_node); true } else { false @@ -480,7 +480,7 @@ macro_rules! define_queries { type Cache = query_storage::$name<'tcx>; #[inline(always)] - fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState + fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState where QueryCtxt<'tcx>: 'a { &tcx.queries.$name @@ -587,9 +587,10 @@ macro_rules! define_queries { use $crate::plumbing::{QueryStruct, QueryCtxt}; use $crate::profiling_support::QueryKeyStringCache; use rustc_query_system::query::QueryMap; + use rustc_middle::dep_graph::DepKind; pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> { - fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap) -> Option<()> { + fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap) -> Option<()> { None } fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {} @@ -675,7 +676,8 @@ macro_rules! define_queries_struct { $( $(#[$attr])* $name: QueryState< - as QueryConfig>>::Key + as QueryConfig>>::Key, + rustc_middle::dep_graph::DepKind, >, )* } @@ -684,7 +686,7 @@ macro_rules! define_queries_struct { pub(crate) fn try_collect_active_jobs( &'tcx self, tcx: TyCtxt<'tcx>, - ) -> Option { + ) -> Option> { let tcx = QueryCtxt { tcx, queries: self }; let mut jobs = QueryMap::default(); @@ -718,7 +720,7 @@ macro_rules! define_queries_struct { mode: QueryMode, ) -> Option> { let qcx = QueryCtxt { tcx, queries: self }; - get_query::, _>(qcx, span, key, mode) + get_query::, _, rustc_middle::dep_graph::DepKind>(qcx, span, key, mode) })* } }; diff --git a/compiler/rustc_query_system/src/query/config.rs b/compiler/rustc_query_system/src/query/config.rs index 7d1b62ab1023..24c960765df8 100644 --- a/compiler/rustc_query_system/src/query/config.rs +++ b/compiler/rustc_query_system/src/query/config.rs @@ -21,7 +21,7 @@ pub trait QueryConfig { type Cache: QueryCache; // Don't use this method to access query results, instead use the methods on TyCtxt - fn query_state<'a>(tcx: Qcx) -> &'a QueryState + fn query_state<'a>(tcx: Qcx) -> &'a QueryState where Qcx: 'a; diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs index 49bbcf578045..d92e55413e51 100644 --- a/compiler/rustc_query_system/src/query/job.rs +++ b/compiler/rustc_query_system/src/query/job.rs @@ -1,6 +1,8 @@ +use crate::dep_graph::DepKind; use crate::error::CycleStack; use crate::query::plumbing::CycleError; use crate::query::{QueryContext, QueryStackFrame}; +use core::marker::PhantomData; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{ @@ -28,48 +30,48 @@ use { /// Represents a span and a query key. #[derive(Clone, Debug)] -pub struct QueryInfo { +pub struct QueryInfo { /// The span corresponding to the reason for which this query was required. pub span: Span, - pub query: QueryStackFrame, + pub query: QueryStackFrame, } -pub type QueryMap = FxHashMap; +pub type QueryMap = FxHashMap>; /// A value uniquely identifying an active query job. #[derive(Copy, Clone, Eq, PartialEq, Hash)] pub struct QueryJobId(pub NonZeroU64); impl QueryJobId { - fn query(self, map: &QueryMap) -> QueryStackFrame { + fn query(self, map: &QueryMap) -> QueryStackFrame { map.get(&self).unwrap().query.clone() } #[cfg(parallel_compiler)] - fn span(self, map: &QueryMap) -> Span { + fn span(self, map: &QueryMap) -> Span { map.get(&self).unwrap().job.span } #[cfg(parallel_compiler)] - fn parent(self, map: &QueryMap) -> Option { + fn parent(self, map: &QueryMap) -> Option { map.get(&self).unwrap().job.parent } #[cfg(parallel_compiler)] - fn latch<'a>(self, map: &'a QueryMap) -> Option<&'a QueryLatch> { + fn latch(self, map: &QueryMap) -> Option<&QueryLatch> { map.get(&self).unwrap().job.latch.as_ref() } } #[derive(Clone)] -pub struct QueryJobInfo { - pub query: QueryStackFrame, - pub job: QueryJob, +pub struct QueryJobInfo { + pub query: QueryStackFrame, + pub job: QueryJob, } /// Represents an active query job. #[derive(Clone)] -pub struct QueryJob { +pub struct QueryJob { pub id: QueryJobId, /// The span corresponding to the reason for which this query was required. @@ -80,10 +82,11 @@ pub struct QueryJob { /// The latch that is used to wait on this job. #[cfg(parallel_compiler)] - latch: Option, + latch: Option>, + spooky: core::marker::PhantomData, } -impl QueryJob { +impl QueryJob { /// Creates a new query job. #[inline] pub fn new(id: QueryJobId, span: Span, parent: Option) -> Self { @@ -93,11 +96,12 @@ impl QueryJob { parent, #[cfg(parallel_compiler)] latch: None, + spooky: PhantomData, } } #[cfg(parallel_compiler)] - pub(super) fn latch(&mut self) -> QueryLatch { + pub(super) fn latch(&mut self) -> QueryLatch { if self.latch.is_none() { self.latch = Some(QueryLatch::new()); } @@ -123,12 +127,12 @@ impl QueryJobId { #[cold] #[inline(never)] #[cfg(not(parallel_compiler))] - pub(super) fn find_cycle_in_stack( + pub(super) fn find_cycle_in_stack( &self, - query_map: QueryMap, + query_map: QueryMap, current_job: &Option, span: Span, - ) -> CycleError { + ) -> CycleError { // Find the waitee amongst `current_job` parents let mut cycle = Vec::new(); let mut current_job = Option::clone(current_job); @@ -162,14 +166,18 @@ impl QueryJobId { #[cold] #[inline(never)] - pub fn try_find_layout_root(&self, query_map: QueryMap) -> Option<(QueryJobInfo, usize)> { + pub fn try_find_layout_root( + &self, + query_map: QueryMap, + ) -> Option<(QueryJobInfo, usize)> { let mut last_layout = None; let mut current_id = Some(*self); let mut depth = 0; while let Some(id) = current_id { let info = query_map.get(&id).unwrap(); - if info.query.name == "layout_of" { + // FIXME: This string comparison should probably not be done. + if format!("{:?}", info.query.dep_kind) == "layout_of" { depth += 1; last_layout = Some((info.clone(), depth)); } @@ -180,15 +188,15 @@ impl QueryJobId { } #[cfg(parallel_compiler)] -struct QueryWaiter { +struct QueryWaiter { query: Option, condvar: Condvar, span: Span, - cycle: Lock>, + cycle: Lock>>, } #[cfg(parallel_compiler)] -impl QueryWaiter { +impl QueryWaiter { fn notify(&self, registry: &rayon_core::Registry) { rayon_core::mark_unblocked(registry); self.condvar.notify_one(); @@ -196,19 +204,19 @@ impl QueryWaiter { } #[cfg(parallel_compiler)] -struct QueryLatchInfo { +struct QueryLatchInfo { complete: bool, - waiters: Vec>, + waiters: Vec>>, } #[cfg(parallel_compiler)] #[derive(Clone)] -pub(super) struct QueryLatch { - info: Lrc>, +pub(super) struct QueryLatch { + info: Lrc>>, } #[cfg(parallel_compiler)] -impl QueryLatch { +impl QueryLatch { fn new() -> Self { QueryLatch { info: Lrc::new(Mutex::new(QueryLatchInfo { complete: false, waiters: Vec::new() })), @@ -216,7 +224,11 @@ impl QueryLatch { } /// Awaits for the query job to complete. - pub(super) fn wait_on(&self, query: Option, span: Span) -> Result<(), CycleError> { + pub(super) fn wait_on( + &self, + query: Option, + span: Span, + ) -> Result<(), CycleError> { let waiter = Lrc::new(QueryWaiter { query, span, cycle: Lock::new(None), condvar: Condvar::new() }); self.wait_on_inner(&waiter); @@ -231,7 +243,7 @@ impl QueryLatch { } /// Awaits the caller on this latch by blocking the current thread. - fn wait_on_inner(&self, waiter: &Lrc) { + fn wait_on_inner(&self, waiter: &Lrc>) { let mut info = self.info.lock(); if !info.complete { // We push the waiter on to the `waiters` list. It can be accessed inside @@ -265,7 +277,7 @@ impl QueryLatch { /// Removes a single waiter from the list of waiters. /// This is used to break query cycles. - fn extract_waiter(&self, waiter: usize) -> Lrc { + fn extract_waiter(&self, waiter: usize) -> Lrc> { let mut info = self.info.lock(); debug_assert!(!info.complete); // Remove the waiter from the list of waiters @@ -287,9 +299,14 @@ type Waiter = (QueryJobId, usize); /// required information to resume the waiter. /// If all `visit` calls returns None, this function also returns None. #[cfg(parallel_compiler)] -fn visit_waiters(query_map: &QueryMap, query: QueryJobId, mut visit: F) -> Option> +fn visit_waiters( + query_map: &QueryMap, + query: QueryJobId, + mut visit: F, +) -> Option> where F: FnMut(Span, QueryJobId) -> Option>, + D: DepKind, { // Visit the parent query which is a non-resumable waiter since it's on the same stack if let Some(parent) = query.parent(query_map) { @@ -318,8 +335,8 @@ where /// If a cycle is detected, this initial value is replaced with the span causing /// the cycle. #[cfg(parallel_compiler)] -fn cycle_check( - query_map: &QueryMap, +fn cycle_check( + query_map: &QueryMap, query: QueryJobId, span: Span, stack: &mut Vec<(Span, QueryJobId)>, @@ -359,8 +376,8 @@ fn cycle_check( /// from `query` without going through any of the queries in `visited`. /// This is achieved with a depth first search. #[cfg(parallel_compiler)] -fn connected_to_root( - query_map: &QueryMap, +fn connected_to_root( + query_map: &QueryMap, query: QueryJobId, visited: &mut FxHashSet, ) -> bool { @@ -382,9 +399,10 @@ fn connected_to_root( // Deterministically pick an query from a list #[cfg(parallel_compiler)] -fn pick_query<'a, T, F>(query_map: &QueryMap, queries: &'a [T], f: F) -> &'a T +fn pick_query<'a, T, F, D>(query_map: &QueryMap, queries: &'a [T], f: F) -> &'a T where F: Fn(&T) -> (Span, QueryJobId), + D: DepKind, { // Deterministically pick an entry point // FIXME: Sort this instead @@ -408,10 +426,10 @@ where /// If a cycle was not found, the starting query is removed from `jobs` and /// the function returns false. #[cfg(parallel_compiler)] -fn remove_cycle( - query_map: &QueryMap, +fn remove_cycle( + query_map: &QueryMap, jobs: &mut Vec, - wakelist: &mut Vec>, + wakelist: &mut Vec>>, ) -> bool { let mut visited = FxHashSet::default(); let mut stack = Vec::new(); @@ -513,7 +531,7 @@ fn remove_cycle( /// There may be multiple cycles involved in a deadlock, so this searches /// all active queries for cycles before finally resuming all the waiters at once. #[cfg(parallel_compiler)] -pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) { +pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) { let on_panic = OnDrop(|| { eprintln!("deadlock handler panicked, aborting process"); process::abort(); @@ -549,9 +567,9 @@ pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) { #[inline(never)] #[cold] -pub(crate) fn report_cycle<'a>( +pub(crate) fn report_cycle<'a, D: DepKind>( sess: &'a Session, - CycleError { usage, cycle: stack }: &CycleError, + CycleError { usage, cycle: stack }: &CycleError, ) -> DiagnosticBuilder<'a, ErrorGuaranteed> { assert!(!stack.is_empty()); @@ -617,7 +635,7 @@ pub fn print_query_stack( }; let mut diag = Diagnostic::new( Level::FailureNote, - &format!("#{} [{}] {}", i, query_info.query.name, query_info.query.description), + &format!("#{} [{:?}] {}", i, query_info.query.dep_kind, query_info.query.description), ); diag.span = query_info.job.span.into(); handler.force_print_diagnostic(diag); diff --git a/compiler/rustc_query_system/src/query/mod.rs b/compiler/rustc_query_system/src/query/mod.rs index 7f3dc50d234f..ce9179ea832e 100644 --- a/compiler/rustc_query_system/src/query/mod.rs +++ b/compiler/rustc_query_system/src/query/mod.rs @@ -14,6 +14,7 @@ pub use self::caches::{ mod config; pub use self::config::{QueryConfig, QueryVTable}; +use crate::dep_graph::DepKind; use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex}; use rustc_data_structures::sync::Lock; use rustc_errors::Diagnostic; @@ -26,37 +27,37 @@ use thin_vec::ThinVec; /// /// This is mostly used in case of cycles for error reporting. #[derive(Clone, Debug)] -pub struct QueryStackFrame { - pub name: &'static str, +pub struct QueryStackFrame { pub description: String, span: Option, pub def_id: Option, pub def_kind: Option, pub ty_adt_id: Option, + pub dep_kind: D, /// This hash is used to deterministically pick /// a query to remove cycles in the parallel compiler. #[cfg(parallel_compiler)] hash: u64, } -impl QueryStackFrame { +impl QueryStackFrame { #[inline] pub fn new( - name: &'static str, description: String, span: Option, def_id: Option, def_kind: Option, + dep_kind: D, ty_adt_id: Option, _hash: impl FnOnce() -> u64, ) -> Self { Self { - name, description, span, def_id, def_kind, ty_adt_id, + dep_kind, #[cfg(parallel_compiler)] hash: _hash(), } @@ -104,7 +105,7 @@ pub trait QueryContext: HasDepContext { /// Get the query information from the TLS context. fn current_query_job(&self) -> Option; - fn try_collect_active_jobs(&self) -> Option; + fn try_collect_active_jobs(&self) -> Option>; /// Load side effects associated to the node in the previous session. fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects; diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 848fa67e3df2..c3a2410dacaa 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -2,7 +2,7 @@ //! generate the actual methods on tcx which find and execute the provider, //! manage the caches, and so forth. -use crate::dep_graph::{DepContext, DepNode, DepNodeIndex, DepNodeParams}; +use crate::dep_graph::{DepContext, DepKind, DepNode, DepNodeIndex, DepNodeParams}; use crate::ich::StableHashingContext; use crate::query::caches::QueryCache; use crate::query::config::QueryVTable; @@ -31,26 +31,27 @@ use thin_vec::ThinVec; use super::QueryConfig; -pub struct QueryState { +pub struct QueryState { #[cfg(parallel_compiler)] - active: Sharded>, + active: Sharded>>, #[cfg(not(parallel_compiler))] - active: Lock>, + active: Lock>>, } /// Indicates the state of a query for a given key in a query map. -enum QueryResult { +enum QueryResult { /// An already executing query. The query job can be used to await for its completion. - Started(QueryJob), + Started(QueryJob), /// The query panicked. Queries trying to wait on this will raise a fatal error which will /// silently panic. Poisoned, } -impl QueryState +impl QueryState where K: Eq + Hash + Clone + Debug, + D: DepKind, { pub fn all_inactive(&self) -> bool { #[cfg(parallel_compiler)] @@ -67,8 +68,8 @@ where pub fn try_collect_active_jobs( &self, qcx: Qcx, - make_query: fn(Qcx, K) -> QueryStackFrame, - jobs: &mut QueryMap, + make_query: fn(Qcx, K) -> QueryStackFrame, + jobs: &mut QueryMap, ) -> Option<()> { #[cfg(parallel_compiler)] { @@ -102,34 +103,34 @@ where } } -impl Default for QueryState { - fn default() -> QueryState { +impl Default for QueryState { + fn default() -> QueryState { QueryState { active: Default::default() } } } /// A type representing the responsibility to execute the job in the `job` field. /// This will poison the relevant query if dropped. -struct JobOwner<'tcx, K> +struct JobOwner<'tcx, K, D: DepKind> where K: Eq + Hash + Clone, { - state: &'tcx QueryState, + state: &'tcx QueryState, key: K, id: QueryJobId, } #[cold] #[inline(never)] -fn mk_cycle( +fn mk_cycle( qcx: Qcx, - cycle_error: CycleError, + cycle_error: CycleError, handler: HandleCycleError, cache: &dyn crate::query::QueryStorage, ) -> R where - Qcx: QueryContext, - V: std::fmt::Debug + Value, + Qcx: QueryContext + crate::query::HasDepContext, + V: std::fmt::Debug + Value, R: Clone, { let error = report_cycle(qcx.dep_context().sess(), &cycle_error); @@ -139,13 +140,13 @@ where fn handle_cycle_error( tcx: Tcx, - cycle_error: &CycleError, + cycle_error: &CycleError, mut error: DiagnosticBuilder<'_, ErrorGuaranteed>, handler: HandleCycleError, ) -> V where Tcx: DepContext, - V: Value, + V: Value, { use HandleCycleError::*; match handler { @@ -165,7 +166,7 @@ where } } -impl<'tcx, K> JobOwner<'tcx, K> +impl<'tcx, K, D: DepKind> JobOwner<'tcx, K, D> where K: Eq + Hash + Clone, { @@ -180,12 +181,12 @@ where #[inline(always)] fn try_start<'b, Qcx>( qcx: &'b Qcx, - state: &'b QueryState, + state: &'b QueryState, span: Span, key: K, - ) -> TryGetJob<'b, K> + ) -> TryGetJob<'b, K, D> where - Qcx: QueryContext, + Qcx: QueryContext + crate::query::HasDepContext, { #[cfg(parallel_compiler)] let mut state_lock = state.active.get_shard_by_value(&key).lock(); @@ -280,9 +281,10 @@ where } } -impl<'tcx, K> Drop for JobOwner<'tcx, K> +impl<'tcx, K, D> Drop for JobOwner<'tcx, K, D> where K: Eq + Hash + Clone, + D: DepKind, { #[inline(never)] #[cold] @@ -308,19 +310,20 @@ where } #[derive(Clone)] -pub(crate) struct CycleError { +pub(crate) struct CycleError { /// The query and related span that uses the cycle. - pub usage: Option<(Span, QueryStackFrame)>, - pub cycle: Vec, + pub usage: Option<(Span, QueryStackFrame)>, + pub cycle: Vec>, } /// The result of `try_start`. -enum TryGetJob<'tcx, K> +enum TryGetJob<'tcx, K, D> where K: Eq + Hash + Clone, + D: DepKind, { /// The query is not yet started. Contains a guard to the cache eventually used to start it. - NotYetStarted(JobOwner<'tcx, K>), + NotYetStarted(JobOwner<'tcx, K, D>), /// The query was already completed. /// Returns the result of the query and its dep-node index @@ -329,7 +332,7 @@ where JobCompleted(TimingGuard<'tcx>), /// Trying to execute the query resulted in a cycle. - Cycle(CycleError), + Cycle(CycleError), } /// Checks if the query is already computed and in the cache. @@ -360,7 +363,7 @@ where fn try_execute_query( qcx: Qcx, - state: &QueryState, + state: &QueryState, cache: &C, span: Span, key: C::Key, @@ -370,11 +373,11 @@ fn try_execute_query( where C: QueryCache, C::Key: Clone + DepNodeParams, - C::Value: Value, + C::Value: Value, C::Stored: Debug + std::borrow::Borrow, Qcx: QueryContext, { - match JobOwner::<'_, C::Key>::try_start(&qcx, state, span, key.clone()) { + match JobOwner::<'_, C::Key, Qcx::DepKind>::try_start(&qcx, state, span, key.clone()) { TryGetJob::NotYetStarted(job) => { let (result, dep_node_index) = execute_job(qcx, key.clone(), dep_node, query, job.id); if query.feedable { @@ -739,11 +742,12 @@ pub enum QueryMode { Ensure, } -pub fn get_query(qcx: Qcx, span: Span, key: Q::Key, mode: QueryMode) -> Option +pub fn get_query(qcx: Qcx, span: Span, key: Q::Key, mode: QueryMode) -> Option where + D: DepKind, Q: QueryConfig, Q::Key: DepNodeParams, - Q::Value: Value, + Q::Value: Value, Qcx: QueryContext, { let query = Q::make_vtable(qcx, &key); @@ -772,11 +776,12 @@ where Some(result) } -pub fn force_query(qcx: Qcx, key: Q::Key, dep_node: DepNode) +pub fn force_query(qcx: Qcx, key: Q::Key, dep_node: DepNode) where + D: DepKind, Q: QueryConfig, Q::Key: DepNodeParams, - Q::Value: Value, + Q::Value: Value, Qcx: QueryContext, { // We may be concurrently trying both execute and force a query. diff --git a/compiler/rustc_query_system/src/values.rs b/compiler/rustc_query_system/src/values.rs index 214656abed4d..b6e2cfa3dca5 100644 --- a/compiler/rustc_query_system/src/values.rs +++ b/compiler/rustc_query_system/src/values.rs @@ -1,12 +1,12 @@ -use crate::dep_graph::DepContext; +use crate::dep_graph::{DepContext, DepKind}; use crate::query::QueryInfo; -pub trait Value: Sized { - fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo]) -> Self; +pub trait Value: Sized { + fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo]) -> Self; } -impl Value for T { - default fn from_cycle_error(tcx: Tcx, _: &[QueryInfo]) -> T { +impl Value for T { + default fn from_cycle_error(tcx: Tcx, _: &[QueryInfo]) -> T { tcx.sess().abort_if_errors(); // Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's // non-trivial to define it earlier. From d0f404d77a4efefa132346c507738d9a5c6e69b4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 23 Dec 2022 15:08:56 +0100 Subject: [PATCH 313/321] fix IntoIter::drop on high-alignment ZST --- library/alloc/src/vec/into_iter.rs | 21 +++++++++++---------- src/tools/miri/README.md | 1 + src/tools/miri/tests/pass/vec.rs | 18 +++++++++++------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 6bcde6d899ce..cd4ea829e378 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -40,7 +40,9 @@ pub struct IntoIter< // to avoid dropping the allocator twice we need to wrap it into ManuallyDrop pub(super) alloc: ManuallyDrop
, pub(super) ptr: *const T, - pub(super) end: *const T, + pub(super) end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that + // ptr == end is a quick test for the Iterator being empty, that works + // for both ZST and non-ZST. } #[stable(feature = "vec_intoiter_debug", since = "1.13.0")] @@ -132,7 +134,9 @@ impl IntoIter { /// Forgets to Drop the remaining elements while still allowing the backing allocation to be freed. pub(crate) fn forget_remaining_elements(&mut self) { - self.ptr = self.end; + // For th ZST case, it is crucial that we mutate `end` here, not `ptr`. + // `ptr` must stay aligned, while `end` may be unaligned. + self.end = self.ptr; } #[cfg(not(no_global_oom_handling))] @@ -184,10 +188,9 @@ impl Iterator for IntoIter { if self.ptr == self.end { None } else if T::IS_ZST { - // purposefully don't use 'ptr.offset' because for - // vectors with 0-size elements this would return the - // same pointer. - self.ptr = self.ptr.wrapping_byte_add(1); + // `ptr` has to stay where it is to remain aligned, so we reduce the length by 1 by + // reducing the `end`. + self.end = self.end.wrapping_byte_sub(1); // Make up a value of this ZST. Some(unsafe { mem::zeroed() }) @@ -214,10 +217,8 @@ impl Iterator for IntoIter { let step_size = self.len().min(n); let to_drop = ptr::slice_from_raw_parts_mut(self.ptr as *mut T, step_size); if T::IS_ZST { - // SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound - // effectively results in unsigned pointers representing positions 0..usize::MAX, - // which is valid for ZSTs. - self.ptr = self.ptr.wrapping_byte_add(step_size); + // See `next` for why we sub `end` here. + self.end = self.end.wrapping_byte_sub(step_size); } else { // SAFETY: the min() above ensures that step_size is in bounds self.ptr = unsafe { self.ptr.add(step_size) }; diff --git a/src/tools/miri/README.md b/src/tools/miri/README.md index dac0a9820b90..989a196b700c 100644 --- a/src/tools/miri/README.md +++ b/src/tools/miri/README.md @@ -639,6 +639,7 @@ Definite bugs found: * [Data race in `thread::scope`](https://github.com/rust-lang/rust/issues/98498) * [`regex` incorrectly handling unaligned `Vec` buffers](https://www.reddit.com/r/rust/comments/vq3mmu/comment/ienc7t0?context=3) * [Incorrect use of `compare_exchange_weak` in `once_cell`](https://github.com/matklad/once_cell/issues/186) +* [Dropping with unaligned pointers in `vec::IntoIter`](https://github.com/rust-lang/rust/pull/106084) Violations of [Stacked Borrows] found that are likely bugs (but Stacked Borrows is currently just an experiment): diff --git a/src/tools/miri/tests/pass/vec.rs b/src/tools/miri/tests/pass/vec.rs index 26732cec5eb9..a165c7c2fe79 100644 --- a/src/tools/miri/tests/pass/vec.rs +++ b/src/tools/miri/tests/pass/vec.rs @@ -37,15 +37,19 @@ fn vec_into_iter() -> u8 { } fn vec_into_iter_rev() -> u8 { - vec![1, 2, 3, 4].into_iter().map(|x| x * x).fold(0, |x, y| x + y) + vec![1, 2, 3, 4].into_iter().rev().map(|x| x * x).fold(0, |x, y| x + y) } -fn vec_into_iter_zst() -> usize { - vec![[0u64; 0], [0u64; 0]].into_iter().rev().map(|x| x.len()).sum() +fn vec_into_iter_zst() { + for _ in vec![[0u64; 0]].into_iter() {} + let v = vec![[0u64; 0], [0u64; 0]].into_iter().map(|x| x.len()).sum::(); + assert_eq!(v, 0); } -fn vec_into_iter_rev_zst() -> usize { - vec![[0u64; 0], [0u64; 0]].into_iter().rev().map(|x| x.len()).sum() +fn vec_into_iter_rev_zst() { + for _ in vec![[0u64; 0]; 5].into_iter().rev() {} + let v = vec![[0u64; 0], [0u64; 0]].into_iter().rev().map(|x| x.len()).sum::(); + assert_eq!(v, 0); } fn vec_iter_and_mut() { @@ -150,8 +154,8 @@ fn main() { assert_eq!(vec_into_iter(), 30); assert_eq!(vec_into_iter_rev(), 30); vec_iter_and_mut(); - assert_eq!(vec_into_iter_zst(), 0); - assert_eq!(vec_into_iter_rev_zst(), 0); + vec_into_iter_zst(); + vec_into_iter_rev_zst(); vec_iter_and_mut_rev(); assert_eq!(make_vec().capacity(), 4); From a48d2e178385a031eb4875deb09d728344d2912f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 23 Dec 2022 15:49:23 +0100 Subject: [PATCH 314/321] fix one more unaligned self.ptr, and add tests --- library/alloc/src/vec/into_iter.rs | 2 +- src/tools/miri/tests/pass/vec.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index cd4ea829e378..b207b3210f1a 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -251,7 +251,7 @@ impl Iterator for IntoIter { return Err(unsafe { array::IntoIter::new_unchecked(raw_ary, 0..len) }); } - self.ptr = self.ptr.wrapping_byte_add(N); + self.end = self.end.wrapping_byte_sub(N); // Safety: ditto return Ok(unsafe { raw_ary.transpose().assume_init() }); } diff --git a/src/tools/miri/tests/pass/vec.rs b/src/tools/miri/tests/pass/vec.rs index a165c7c2fe79..fb1d8e306f29 100644 --- a/src/tools/miri/tests/pass/vec.rs +++ b/src/tools/miri/tests/pass/vec.rs @@ -1,4 +1,6 @@ //@compile-flags: -Zmiri-strict-provenance +#![feature(iter_advance_by, iter_next_chunk)] + // Gather all references from a mutable iterator and make sure Miri notices if // using them is dangerous. fn test_all_refs<'a, T: 'a>(dummy: &mut T, iter: impl Iterator) { @@ -44,6 +46,18 @@ fn vec_into_iter_zst() { for _ in vec![[0u64; 0]].into_iter() {} let v = vec![[0u64; 0], [0u64; 0]].into_iter().map(|x| x.len()).sum::(); assert_eq!(v, 0); + + let mut it = vec![[0u64; 0], [0u64; 0]].into_iter(); + it.advance_by(1); + drop(it); + + let mut it = vec![[0u64; 0], [0u64; 0]].into_iter(); + it.next_chunk::<1>().unwrap(); + drop(it); + + let mut it = vec![[0u64; 0], [0u64; 0]].into_iter(); + it.next_chunk::<4>().unwrap_err(); + drop(it); } fn vec_into_iter_rev_zst() { From 1174aacf598d01b3e063668bb6ecd9c472688d02 Mon Sep 17 00:00:00 2001 From: Jeremy Stucki Date: Fri, 23 Dec 2022 15:59:53 +0100 Subject: [PATCH 315/321] Indicate anonymous lifetime --- compiler/rustc_graphviz/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_graphviz/src/lib.rs b/compiler/rustc_graphviz/src/lib.rs index d7730935f201..434f0a53b786 100644 --- a/compiler/rustc_graphviz/src/lib.rs +++ b/compiler/rustc_graphviz/src/lib.rs @@ -164,7 +164,7 @@ //! fn node_id(&'a self, n: &Nd) -> dot::Id<'a> { //! dot::Id::new(format!("N{}", n)).unwrap() //! } -//! fn node_label(&self, n: &Nd) -> dot::LabelText { +//! fn node_label(&self, n: &Nd) -> dot::LabelText<'_> { //! dot::LabelText::LabelStr(self.nodes[*n].into()) //! } //! fn edge_label<'b>(&'b self, _: &Ed) -> dot::LabelText<'b> { From 5d3c22dae5bc8bf8cfcd87470d2126d549fb8e2e Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 23 Dec 2022 17:05:07 +0000 Subject: [PATCH 316/321] Update cargo 7 commits in c994a4a638370bc7e0ffcbb0e2865afdfa7d4415..2381cbdb4e9b07090f552d34a44a529b6e620e44 2022-12-18 21:50:58 +0000 to 2022-12-23 12:19:27 +0000 - fix: deduplicate dependencies by artifact target (rust-lang/cargo#11478) - Add warning if potentially-scrapable examples are skipped due to dev-dependencies (rust-lang/cargo#11503) - Don't scrape examples from library targets by default (rust-lang/cargo#11499) - Stabilize terminal-width (rust-lang/cargo#11494) - Make sure that hash of `SourceId` is stable (rust-lang/cargo#11501) - Use workspace lockfile when running `cargo package` and `cargo publish` (rust-lang/cargo#11477) - Show `--help` if there is no man page for subcommand (rust-lang/cargo#11473) --- src/tools/cargo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/cargo b/src/tools/cargo index c994a4a63837..2381cbdb4e9b 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit c994a4a638370bc7e0ffcbb0e2865afdfa7d4415 +Subproject commit 2381cbdb4e9b07090f552d34a44a529b6e620e44 From ddd9591a8585a949c7bb965ab1b8a6c66671b130 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 23 Dec 2022 19:30:44 +0100 Subject: [PATCH 317/321] Use correct CSS pseudo-element selector --- src/librustdoc/html/static/css/themes/ayu.css | 4 ++-- src/librustdoc/html/static/css/themes/dark.css | 4 ++-- src/librustdoc/html/static/css/themes/light.css | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index 1355ae9c2bac..ce416f77afe9 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -215,10 +215,10 @@ pre.rust .kw-2, pre.rust .prelude-ty {} .scraped-example .example-wrap .rust span.highlight.focus { background: rgb(124, 75, 15); } -.scraped-example:not(.expanded) .code-wrapper:before { +.scraped-example:not(.expanded) .code-wrapper::before { background: linear-gradient(to bottom, rgba(15, 20, 25, 1), rgba(15, 20, 25, 0)); } -.scraped-example:not(.expanded) .code-wrapper:after { +.scraped-example:not(.expanded) .code-wrapper::after { background: linear-gradient(to top, rgba(15, 20, 25, 1), rgba(15, 20, 25, 0)); } .toggle-line-inner { diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index 84449542f22b..33d934ff3c31 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -111,10 +111,10 @@ .scraped-example .example-wrap .rust span.highlight.focus { background: rgb(124, 75, 15); } -.scraped-example:not(.expanded) .code-wrapper:before { +.scraped-example:not(.expanded) .code-wrapper::before { background: linear-gradient(to bottom, rgba(53, 53, 53, 1), rgba(53, 53, 53, 0)); } -.scraped-example:not(.expanded) .code-wrapper:after { +.scraped-example:not(.expanded) .code-wrapper::after { background: linear-gradient(to top, rgba(53, 53, 53, 1), rgba(53, 53, 53, 0)); } .toggle-line-inner { diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index 68dc0ce539b0..30e91077d33f 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -108,10 +108,10 @@ .scraped-example .example-wrap .rust span.highlight.focus { background: #f6fdb0; } -.scraped-example:not(.expanded) .code-wrapper:before { +.scraped-example:not(.expanded) .code-wrapper::before { background: linear-gradient(to bottom, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0)); } -.scraped-example:not(.expanded) .code-wrapper:after { +.scraped-example:not(.expanded) .code-wrapper::after { background: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0)); } .toggle-line-inner { From 1c05d4b02aa424e89b2f157db1a16d01878b5a4f Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 23 Dec 2022 11:42:47 -0700 Subject: [PATCH 318/321] rustdoc: remove no-op CSS from `.docblock-short` The rules `overflow:hidden` and `text-overflow:ellipses` only have an effect if overflow occurs, which cannot happen because it will just line wrap instead. These rules definitely became obsolete by https://github.com/rust-lang/rust/pull/77699, when the stylesheet was decidedly changed to have line wrapping in short docblocks, but given the bug it was fixing, this probably got broken earlier. --- src/librustdoc/html/static/css/rustdoc.css | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 3ea83cc41ef7..a482a55fe4b4 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -579,8 +579,6 @@ ul.block, .block li { .docblock-short { overflow-wrap: break-word; overflow-wrap: anywhere; - overflow: hidden; - text-overflow: ellipsis; } /* Wrap non-pre code blocks (`text`) but not (```text```). */ .docblock :not(pre) > code, From e97e55f87b6b887c6007a103c0c88353a1e78ddc Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 23 Dec 2022 23:28:02 +0000 Subject: [PATCH 319/321] Fix triagebot.toml --- triagebot.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/triagebot.toml b/triagebot.toml index c7158a51861e..22f09396efc6 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -342,7 +342,7 @@ cc = ["@BoxyUwU"] message = "Some changes occured in `rustc_ty_utils::consts.rs`" cc = ["@BoxyUwU"] -[mentions."compiler/rustc_trait_selection/src/solve] +[mentions."compiler/rustc_trait_selection/src/solve/"] message = "Some changes occurred to the core trait solver" cc = ["@lcnr"] From 0c14ad4d9fe83e737d7b8db0c2520ef54c99926e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 24 Dec 2022 09:58:29 +0100 Subject: [PATCH 320/321] Preparing for merge from rustc --- src/tools/miri/rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/miri/rust-version b/src/tools/miri/rust-version index e6c238023e40..ee75e7a29324 100644 --- a/src/tools/miri/rust-version +++ b/src/tools/miri/rust-version @@ -1 +1 @@ -a803f313fdf8f6eb2d674d7dfb3694a2b437ee1e +4f4d0586ad20c66a16d547581ca379beafece93a From e52e0d855799fe651922be4a038fe84fe9009c72 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 24 Dec 2022 10:12:28 +0100 Subject: [PATCH 321/321] fix warnings --- .../tests/fail/stacked_borrows/drop_in_place_protector.stderr | 2 +- .../miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr | 2 +- src/tools/miri/tests/pass/vec.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr index bd51a6645a67..3d0cef241c3e 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_protector.stderr @@ -16,7 +16,7 @@ help: is this argument | LL | core::ptr::drop_in_place(x); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: BACKTRACE: + = note: BACKTRACE (of the first span): = note: inside `::drop` at $DIR/drop_in_place_protector.rs:LL:CC = note: inside `std::ptr::drop_in_place:: - shim(Some(HasDrop))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC = note: inside `std::ptr::drop_in_place::<(HasDrop, u8)> - shim(Some((HasDrop, u8)))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC diff --git a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr index 3f9e6708bdaa..7f2917e79505 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr +++ b/src/tools/miri/tests/fail/stacked_borrows/drop_in_place_retag.stderr @@ -14,7 +14,7 @@ help: was created by a SharedReadOnly retag at offsets [0x0..0x1] | LL | let x = core::ptr::addr_of!(x); | ^^^^^^^^^^^^^^^^^^^^^^ - = note: BACKTRACE: + = note: BACKTRACE (of the first span): = note: inside `std::ptr::drop_in_place:: - shim(None)` at RUSTLIB/core/src/ptr/mod.rs:LL:CC note: inside `main` --> $DIR/drop_in_place_retag.rs:LL:CC diff --git a/src/tools/miri/tests/pass/vec.rs b/src/tools/miri/tests/pass/vec.rs index fb1d8e306f29..3a6655e2ba69 100644 --- a/src/tools/miri/tests/pass/vec.rs +++ b/src/tools/miri/tests/pass/vec.rs @@ -48,7 +48,7 @@ fn vec_into_iter_zst() { assert_eq!(v, 0); let mut it = vec![[0u64; 0], [0u64; 0]].into_iter(); - it.advance_by(1); + it.advance_by(1).unwrap(); drop(it); let mut it = vec![[0u64; 0], [0u64; 0]].into_iter();