diff options
520 files changed, 10407 insertions, 4700 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b12a0b855e2..210ec72a11e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -578,6 +578,7 @@ jobs: actions: write name: "try - ${{ matrix.name }}" env: + DIST_TRY_BUILD: 1 CI_JOB_NAME: "${{ matrix.name }}" CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse SCCACHE_BUCKET: rust-lang-ci-sccache2 diff --git a/Cargo.lock b/Cargo.lock index e7aa317ad75..416a1aea4a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -294,7 +294,7 @@ dependencies = [ "serde_json", "sha2", "tar", - "toml", + "toml 0.5.7", "xz2", ] @@ -311,14 +311,14 @@ dependencies = [ "indexmap", "serde", "serde_json", - "toml", + "toml 0.5.7", ] [[package]] name = "bumpalo" -version = "3.12.1" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" +checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" [[package]] name = "bytecount" @@ -581,7 +581,7 @@ checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" [[package]] name = "clippy" -version = "0.1.71" +version = "0.1.72" dependencies = [ "clap 4.2.1", "clippy_lints", @@ -596,7 +596,6 @@ dependencies = [ "quote", "regex", "rustc-semver", - "rustc-workspace-hack", "rustc_tools_util", "serde", "syn 2.0.8", @@ -604,7 +603,7 @@ dependencies = [ "termize", "tester", "tokio", - "toml", + "toml 0.7.4", "walkdir", ] @@ -623,7 +622,7 @@ dependencies = [ [[package]] name = "clippy_lints" -version = "0.1.71" +version = "0.1.72" dependencies = [ "arrayvec", "cargo_metadata 0.15.3", @@ -639,7 +638,7 @@ dependencies = [ "serde", "serde_json", "tempfile", - "toml", + "toml 0.7.4", "unicode-normalization", "unicode-script", "url", @@ -647,7 +646,7 @@ dependencies = [ [[package]] name = "clippy_utils" -version = "0.1.71" +version = "0.1.72" dependencies = [ "arrayvec", "if_chain", @@ -873,16 +872,6 @@ dependencies = [ ] [[package]] -name = "cstr" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b" -dependencies = [ - "proc-macro2", - "quote", -] - -[[package]] name = "ctrlc" version = "3.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -930,7 +919,7 @@ checksum = "a0afaad2b26fa326569eb264b1363e8ae3357618c43982b3f285f0774ce76b69" [[package]] name = "declare_clippy_lint" -version = "0.1.71" +version = "0.1.72" dependencies = [ "itertools", "quote", @@ -1220,7 +1209,7 @@ checksum = "8a3de6e8d11b22ff9edc6d916f890800597d60f8b2da1caf2955c274638d6412" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.2.10", "windows-sys 0.45.0", ] @@ -1762,7 +1751,6 @@ dependencies = [ "flate2", "num_cpus", "rayon", - "remove_dir_all", "tar", "walkdir", "xz2", @@ -2111,7 +2099,7 @@ dependencies = [ "serde_json", "shlex", "tempfile", - "toml", + "toml 0.5.7", "topological-sort", ] @@ -2446,7 +2434,7 @@ dependencies = [ "cfg-if", "instant", "libc", - "redox_syscall", + "redox_syscall 0.2.10", "smallvec", "winapi", ] @@ -2459,7 +2447,7 @@ checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.2.10", "smallvec", "windows-sys 0.42.0", ] @@ -2803,13 +2791,22 @@ dependencies = [ ] [[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags", +] + +[[package]] name = "redox_users" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" dependencies = [ "getrandom", - "redox_syscall", + "redox_syscall 0.2.10", ] [[package]] @@ -2862,15 +2859,6 @@ name = "remote-test-server" version = "0.1.0" [[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - -[[package]] name = "replace-version-placeholder" version = "0.1.0" dependencies = [ @@ -3180,7 +3168,6 @@ name = "rustc_codegen_llvm" version = "0.0.0" dependencies = [ "bitflags", - "cstr", "libc", "measureme", "object 0.31.1", @@ -4363,7 +4350,7 @@ dependencies = [ "serde_json", "term", "thiserror", - "toml", + "toml 0.5.7", "unicode-segmentation", "unicode-width", "unicode_categories", @@ -4485,6 +4472,15 @@ dependencies = [ ] [[package]] +name = "serde_spanned" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93107647184f6027e3b7dcb2e11034cf95ffa1e3a682c67951963ac69c1c007d" +dependencies = [ + "serde", +] + +[[package]] name = "sha1" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4781,16 +4777,15 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.3.0" +version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" +checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" dependencies = [ "cfg-if", "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi", + "redox_syscall 0.3.5", + "rustix", + "windows-sys 0.45.0", ] [[package]] @@ -4976,14 +4971,14 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.8.4" +version = "1.28.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50dae83881bc9b0403dd5b44ea9deed3e939856cc8722d5be37f0d6e5c6d53dd" +checksum = "94d7b1cfd2aa4011f2de74c2c4c63665e27a71006b0a192dcd2710272e73dfa2" dependencies = [ "autocfg", "bytes", - "memchr", "pin-project-lite", + "windows-sys 0.48.0", ] [[package]] @@ -4996,6 +4991,40 @@ dependencies = [ ] [[package]] +name = "toml" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6135d499e69981f9ff0ef2167955a5333c35e36f6937d382974566b3d5b94ec" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a76a9312f5ba4c2dec6b9161fdf25d87ad8a09256ccea5a556fef03c706a10f" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.19.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2380d56e8670370eee6566b0bfd4265f65b3f432e8c6d85623f728d4fa31f739" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + +[[package]] name = "topological-sort" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -5358,7 +5387,6 @@ dependencies = [ "idna", "matches", "percent-encoding", - "serde", ] [[package]] @@ -5686,6 +5714,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" [[package]] +name = "winnow" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61de7bac303dc551fe038e2b3cef0f571087a47571ea6e79a87692ac99b99699" +dependencies = [ + "memchr", +] + +[[package]] name = "writeable" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index 43db66a3c28..2ee63c286ba 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -209,7 +209,7 @@ pub enum TargetDataLayoutErrors<'a> { InvalidAddressSpace { addr_space: &'a str, cause: &'a str, err: ParseIntError }, InvalidBits { kind: &'a str, bit: &'a str, cause: &'a str, err: ParseIntError }, MissingAlignment { cause: &'a str }, - InvalidAlignment { cause: &'a str, err: String }, + InvalidAlignment { cause: &'a str, err: AlignFromBytesError }, InconsistentTargetArchitecture { dl: &'a str, target: &'a str }, InconsistentTargetPointerWidth { pointer_size: u64, target: u32 }, InvalidBitsSize { err: String }, @@ -640,30 +640,65 @@ impl fmt::Debug for Align { } } +#[derive(Clone, Copy)] +pub enum AlignFromBytesError { + NotPowerOfTwo(u64), + TooLarge(u64), +} + +impl AlignFromBytesError { + pub fn diag_ident(self) -> &'static str { + match self { + Self::NotPowerOfTwo(_) => "not_power_of_two", + Self::TooLarge(_) => "too_large", + } + } + + pub fn align(self) -> u64 { + let (Self::NotPowerOfTwo(align) | Self::TooLarge(align)) = self; + align + } +} + +impl fmt::Debug for AlignFromBytesError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for AlignFromBytesError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + AlignFromBytesError::NotPowerOfTwo(align) => write!(f, "`{align}` is not a power of 2"), + AlignFromBytesError::TooLarge(align) => write!(f, "`{align}` is too large"), + } + } +} + impl Align { pub const ONE: Align = Align { pow2: 0 }; pub const MAX: Align = Align { pow2: 29 }; #[inline] - pub fn from_bits(bits: u64) -> Result<Align, String> { + pub fn from_bits(bits: u64) -> Result<Align, AlignFromBytesError> { Align::from_bytes(Size::from_bits(bits).bytes()) } #[inline] - pub fn from_bytes(align: u64) -> Result<Align, String> { + pub fn from_bytes(align: u64) -> Result<Align, AlignFromBytesError> { // Treat an alignment of 0 bytes like 1-byte alignment. if align == 0 { return Ok(Align::ONE); } #[cold] - fn not_power_of_2(align: u64) -> String { - format!("`{}` is not a power of 2", align) + fn not_power_of_2(align: u64) -> AlignFromBytesError { + AlignFromBytesError::NotPowerOfTwo(align) } #[cold] - fn too_large(align: u64) -> String { - format!("`{}` is too large", align) + fn too_large(align: u64) -> AlignFromBytesError { + AlignFromBytesError::TooLarge(align) } let tz = align.trailing_zeros(); diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 5e0ab80c6ac..f52797c4f3f 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -71,9 +71,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let kind = match &e.kind { ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), - ExprKind::ConstBlock(anon_const) => { - let anon_const = self.lower_anon_const(anon_const); - hir::ExprKind::ConstBlock(anon_const) + ExprKind::ConstBlock(c) => { + let c = self.with_new_scopes(|this| hir::ConstBlock { + def_id: this.local_def_id(c.id), + hir_id: this.lower_node_id(c.id), + body: this.lower_const_body(c.value.span, Some(&c.value)), + }); + hir::ExprKind::ConstBlock(c) } ExprKind::Repeat(expr, count) => { let expr = self.lower_expr(expr); diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index 2e66c81eb0d..ce847906fb9 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -223,6 +223,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { }); } + fn visit_inline_const(&mut self, constant: &'hir ConstBlock) { + self.insert(DUMMY_SP, constant.hir_id, Node::ConstBlock(constant)); + + self.with_parent(constant.hir_id, |this| { + intravisit::walk_inline_const(this, constant); + }); + } + fn visit_expr(&mut self, expr: &'hir Expr<'hir>) { self.insert(expr.span, expr.hir_id, Node::Expr(expr)); diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 274f931e43f..2125349909e 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -2,7 +2,6 @@ use rustc_ast as ast; use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; use rustc_ast::{attr, AssocConstraint, AssocConstraintKind, NodeId}; use rustc_ast::{PatKind, RangeEnd}; -use rustc_errors::{Applicability, StashKey}; use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP}; use rustc_session::parse::{feature_err, feature_err_issue, feature_warn}; use rustc_session::Session; @@ -374,55 +373,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - fn visit_stmt(&mut self, stmt: &'a ast::Stmt) { - if let ast::StmtKind::Semi(expr) = &stmt.kind - && let ast::ExprKind::Assign(lhs, _, _) = &expr.kind - && let ast::ExprKind::Type(..) = lhs.kind - && self.sess.parse_sess.span_diagnostic.err_count() == 0 - && !self.features.type_ascription - && !lhs.span.allows_unstable(sym::type_ascription) - { - // When we encounter a statement of the form `foo: Ty = val;`, this will emit a type - // ascription error, but the likely intention was to write a `let` statement. (#78907). - feature_err( - &self.sess.parse_sess, - sym::type_ascription, - lhs.span, - "type ascription is experimental", - ).span_suggestion_verbose( - lhs.span.shrink_to_lo(), - "you might have meant to introduce a new binding", - "let ", - Applicability::MachineApplicable, - ).emit(); - } - visit::walk_stmt(self, stmt); - } - fn visit_expr(&mut self, e: &'a ast::Expr) { match e.kind { - ast::ExprKind::Type(..) => { - if self.sess.parse_sess.span_diagnostic.err_count() == 0 { - // To avoid noise about type ascription in common syntax errors, - // only emit if it is the *only* error. - gate_feature_post!( - &self, - type_ascription, - e.span, - "type ascription is experimental" - ); - } else { - // And if it isn't, cancel the early-pass warning. - if let Some(err) = self - .sess - .parse_sess - .span_diagnostic - .steal_diagnostic(e.span, StashKey::EarlySyntaxWarning) - { - err.cancel() - } - } - } ast::ExprKind::TryBlock(_) => { gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental"); } @@ -629,7 +581,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) { gate_all!(box_patterns, "box pattern syntax is experimental"); gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental"); gate_all!(try_blocks, "`try` blocks are unstable"); - gate_all!(type_ascription, "type ascription is experimental"); visit::walk_crate(&mut visitor, krate); } diff --git a/compiler/rustc_codegen_cranelift/src/common.rs b/compiler/rustc_codegen_cranelift/src/common.rs index 67fd6d793e0..7243cf6da23 100644 --- a/compiler/rustc_codegen_cranelift/src/common.rs +++ b/compiler/rustc_codegen_cranelift/src/common.rs @@ -6,6 +6,7 @@ use rustc_index::IndexVec; use rustc_middle::ty::layout::{ FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, }; +use rustc_span::source_map::Spanned; use rustc_span::SourceFile; use rustc_target::abi::call::FnAbi; use rustc_target::abi::{Integer, Primitive}; @@ -495,25 +496,16 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> { fn_abi_request: FnAbiRequest<'tcx>, ) -> ! { if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err { - self.0.sess.span_fatal(span, err.to_string()) + self.0.sess.emit_fatal(Spanned { span, node: err }) } else { match fn_abi_request { FnAbiRequest::OfFnPtr { sig, extra_args } => { - span_bug!( - span, - "`fn_abi_of_fn_ptr({}, {:?})` failed: {}", - sig, - extra_args, - err - ); + span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}"); } FnAbiRequest::OfInstance { instance, extra_args } => { span_bug!( span, - "`fn_abi_of_instance({}, {:?})` failed: {}", - instance, - extra_args, - err + "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}" ); } } diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs index 0a513b08b74..1e83c30bd67 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs @@ -475,9 +475,7 @@ fn codegen_regular_intrinsic_call<'tcx>( sym::unchecked_add | sym::unchecked_sub | sym::unchecked_mul - | sym::unchecked_div | sym::exact_div - | sym::unchecked_rem | sym::unchecked_shl | sym::unchecked_shr => { intrinsic_args!(fx, args => (x, y); intrinsic); @@ -487,8 +485,7 @@ fn codegen_regular_intrinsic_call<'tcx>( sym::unchecked_add => BinOp::Add, sym::unchecked_sub => BinOp::Sub, sym::unchecked_mul => BinOp::Mul, - sym::unchecked_div | sym::exact_div => BinOp::Div, - sym::unchecked_rem => BinOp::Rem, + sym::exact_div => BinOp::Div, sym::unchecked_shl => BinOp::Shl, sym::unchecked_shr => BinOp::Shr, _ => unreachable!(), diff --git a/compiler/rustc_codegen_gcc/src/consts.rs b/compiler/rustc_codegen_gcc/src/consts.rs index 873f652e6f1..33e3b0baa92 100644 --- a/compiler/rustc_codegen_gcc/src/consts.rs +++ b/compiler/rustc_codegen_gcc/src/consts.rs @@ -24,7 +24,7 @@ fn set_global_alignment<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, gv: LValue<'gcc> match Align::from_bits(min) { Ok(min) => align = align.max(min), Err(err) => { - cx.sess().emit_err(InvalidMinimumAlignment { err }); + cx.sess().emit_err(InvalidMinimumAlignment { err: err.to_string() }); } } } diff --git a/compiler/rustc_codegen_gcc/src/context.rs b/compiler/rustc_codegen_gcc/src/context.rs index 661681bdb50..08507e19652 100644 --- a/compiler/rustc_codegen_gcc/src/context.rs +++ b/compiler/rustc_codegen_gcc/src/context.rs @@ -477,7 +477,7 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> { #[inline] fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! { if let LayoutError::SizeOverflow(_) = err { - self.sess().emit_fatal(respan(span, err)) + self.sess().emit_fatal(respan(span, err.into_diagnostic())) } else { span_bug!(span, "failed to get layout for `{}`: {}", ty, err) } @@ -499,21 +499,12 @@ impl<'gcc, 'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> { } else { match fn_abi_request { FnAbiRequest::OfFnPtr { sig, extra_args } => { - span_bug!( - span, - "`fn_abi_of_fn_ptr({}, {:?})` failed: {}", - sig, - extra_args, - err - ); + span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}"); } FnAbiRequest::OfInstance { instance, extra_args } => { span_bug!( span, - "`fn_abi_of_instance({}, {:?})` failed: {}", - instance, - extra_args, - err + "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}" ); } } diff --git a/compiler/rustc_codegen_llvm/Cargo.toml b/compiler/rustc_codegen_llvm/Cargo.toml index ad51f2d0958..39ff3a0ba2d 100644 --- a/compiler/rustc_codegen_llvm/Cargo.toml +++ b/compiler/rustc_codegen_llvm/Cargo.toml @@ -8,7 +8,6 @@ test = false [dependencies] bitflags = "1.0" -cstr = "0.2" libc = "0.2" measureme = "10.0.0" object = { version = "0.31.1", default-features = false, features = [ diff --git a/compiler/rustc_codegen_llvm/messages.ftl b/compiler/rustc_codegen_llvm/messages.ftl index 55622fdb20a..de1622951fe 100644 --- a/compiler/rustc_codegen_llvm/messages.ftl +++ b/compiler/rustc_codegen_llvm/messages.ftl @@ -20,8 +20,12 @@ codegen_llvm_error_writing_def_file = codegen_llvm_from_llvm_diag = {$message} codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message} -codegen_llvm_invalid_minimum_alignment = - invalid minimum global alignment: {$err} + +codegen_llvm_invalid_minimum_alignment_not_power_of_two = + invalid minimum global alignment: {$align} is not power of 2 + +codegen_llvm_invalid_minimum_alignment_too_large = + invalid minimum global alignment: {$align} is too large codegen_llvm_load_bitcode = failed to load bitcode of module "{$name}" codegen_llvm_load_bitcode_with_llvm_err = failed to load bitcode of module "{$name}": {$llvm_err} diff --git a/compiler/rustc_codegen_llvm/src/allocator.rs b/compiler/rustc_codegen_llvm/src/allocator.rs index a57508815d6..ad0636894b7 100644 --- a/compiler/rustc_codegen_llvm/src/allocator.rs +++ b/compiler/rustc_codegen_llvm/src/allocator.rs @@ -77,7 +77,7 @@ pub(crate) unsafe fn codegen( llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty); llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden); - let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast()); + let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast()); let llbuilder = llvm::LLVMCreateBuilderInContext(llcx); llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb); @@ -129,7 +129,7 @@ pub(crate) unsafe fn codegen( attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]); llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden); - let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast()); + let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast()); let llbuilder = llvm::LLVMCreateBuilderInContext(llcx); llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb); diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 604f68eb6a4..8b05af7bed9 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -595,7 +595,7 @@ pub(crate) fn run_pass_manager( llvm::LLVMRustAddModuleFlag( module.module_llvm.llmod(), llvm::LLVMModFlagBehavior::Error, - "LTOPostLink\0".as_ptr().cast(), + c"LTOPostLink".as_ptr().cast(), 1, ); } diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index ca2eab28f87..53b4296802e 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -891,11 +891,11 @@ unsafe fn embed_bitcode( let llglobal = llvm::LLVMAddGlobal( llmod, common::val_ty(llconst), - "rustc.embedded.module\0".as_ptr().cast(), + c"rustc.embedded.module".as_ptr().cast(), ); llvm::LLVMSetInitializer(llglobal, llconst); - let section = if is_apple { "__LLVM,__bitcode\0" } else { ".llvmbc\0" }; + let section = if is_apple { c"__LLVM,__bitcode" } else { c".llvmbc" }; llvm::LLVMSetSection(llglobal, section.as_ptr().cast()); llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage); llvm::LLVMSetGlobalConstant(llglobal, llvm::True); @@ -904,10 +904,10 @@ unsafe fn embed_bitcode( let llglobal = llvm::LLVMAddGlobal( llmod, common::val_ty(llconst), - "rustc.embedded.cmdline\0".as_ptr().cast(), + c"rustc.embedded.cmdline".as_ptr().cast(), ); llvm::LLVMSetInitializer(llglobal, llconst); - let section = if is_apple { "__LLVM,__cmdline\0" } else { ".llvmcmd\0" }; + let section = if is_apple { c"__LLVM,__cmdline" } else { c".llvmcmd" }; llvm::LLVMSetSection(llglobal, section.as_ptr().cast()); llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage); } else { diff --git a/compiler/rustc_codegen_llvm/src/base.rs b/compiler/rustc_codegen_llvm/src/base.rs index 5b2bbdb4bde..2f7eb08ad3d 100644 --- a/compiler/rustc_codegen_llvm/src/base.rs +++ b/compiler/rustc_codegen_llvm/src/base.rs @@ -19,8 +19,6 @@ use crate::context::CodegenCx; use crate::llvm; use crate::value::Value; -use cstr::cstr; - use rustc_codegen_ssa::base::maybe_create_entry_wrapper; use rustc_codegen_ssa::mono_item::MonoItemExt; use rustc_codegen_ssa::traits::*; @@ -110,11 +108,11 @@ pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen // Create the llvm.used and llvm.compiler.used variables. if !cx.used_statics.borrow().is_empty() { - cx.create_used_variable_impl(cstr!("llvm.used"), &*cx.used_statics.borrow()); + cx.create_used_variable_impl(c"llvm.used", &*cx.used_statics.borrow()); } if !cx.compiler_used_statics.borrow().is_empty() { cx.create_used_variable_impl( - cstr!("llvm.compiler.used"), + c"llvm.compiler.used", &*cx.compiler_used_statics.borrow(), ); } diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 5968e70b1cc..b4aa001547c 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -6,7 +6,6 @@ use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, True} use crate::type_::Type; use crate::type_of::LayoutLlvmExt; use crate::value::Value; -use cstr::cstr; use libc::{c_char, c_uint}; use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind}; use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue}; @@ -25,7 +24,6 @@ use rustc_symbol_mangling::typeid::{kcfi_typeid_for_fnabi, typeid_for_fnabi, Typ use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange}; use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target}; use std::borrow::Cow; -use std::ffi::CStr; use std::iter; use std::ops::Deref; use std::ptr; @@ -45,13 +43,10 @@ impl Drop for Builder<'_, '_, '_> { } } -// FIXME(eddyb) use a checked constructor when they become `const fn`. -const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") }; - /// Empty string, to be used where LLVM expects an instruction name, indicating /// that the instruction is to be left unnamed (i.e. numbered, in textual IR). // FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer. -const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr(); +const UNNAMED: *const c_char = c"".as_ptr(); impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> { type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value; @@ -1010,14 +1005,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { } fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> { - let name = cstr!("cleanuppad"); let ret = unsafe { llvm::LLVMBuildCleanupPad( self.llbuilder, parent, args.as_ptr(), args.len() as c_uint, - name.as_ptr(), + c"cleanuppad".as_ptr(), ) }; Funclet::new(ret.expect("LLVM does not have support for cleanuppad")) @@ -1031,14 +1025,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { } fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> { - let name = cstr!("catchpad"); let ret = unsafe { llvm::LLVMBuildCatchPad( self.llbuilder, parent, args.as_ptr(), args.len() as c_uint, - name.as_ptr(), + c"catchpad".as_ptr(), ) }; Funclet::new(ret.expect("LLVM does not have support for catchpad")) @@ -1050,14 +1043,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { unwind: Option<&'ll BasicBlock>, handlers: &[&'ll BasicBlock], ) -> &'ll Value { - let name = cstr!("catchswitch"); let ret = unsafe { llvm::LLVMBuildCatchSwitch( self.llbuilder, parent, unwind, handlers.len() as c_uint, - name.as_ptr(), + c"catchswitch".as_ptr(), ) }; let ret = ret.expect("LLVM does not have support for catchswitch"); diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index 940358acde9..2087754c66b 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -1,12 +1,13 @@ use crate::base; use crate::common::{self, CodegenCx}; use crate::debuginfo; -use crate::errors::{InvalidMinimumAlignment, SymbolAlreadyDefined}; +use crate::errors::{ + InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined, +}; use crate::llvm::{self, True}; use crate::type_::Type; use crate::type_of::LayoutLlvmExt; use crate::value::Value; -use cstr::cstr; use rustc_codegen_ssa::traits::*; use rustc_hir::def_id::DefId; use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; @@ -19,7 +20,9 @@ use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::{self, Instance, Ty}; use rustc_middle::{bug, span_bug}; use rustc_session::config::Lto; -use rustc_target::abi::{Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange}; +use rustc_target::abi::{ + Align, AlignFromBytesError, HasDataLayout, Primitive, Scalar, Size, WrappingRange, +}; use std::ops::Range; pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<'_>) -> &'ll Value { @@ -129,9 +132,14 @@ fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align: if let Some(min) = cx.sess().target.min_global_align { match Align::from_bits(min) { Ok(min) => align = align.max(min), - Err(err) => { - cx.sess().emit_err(InvalidMinimumAlignment { err }); - } + Err(err) => match err { + AlignFromBytesError::NotPowerOfTwo(align) => { + cx.sess().emit_err(InvalidMinimumAlignmentNotPowerOfTwo { align }); + } + AlignFromBytesError::TooLarge(align) => { + cx.sess().emit_err(InvalidMinimumAlignmentTooLarge { align }); + } + }, } } unsafe { @@ -473,9 +481,9 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> { .all(|&byte| byte == 0); let sect_name = if all_bytes_are_zero { - cstr!("__DATA,__thread_bss") + c"__DATA,__thread_bss" } else { - cstr!("__DATA,__thread_data") + c"__DATA,__thread_data" }; llvm::LLVMSetSection(g, sect_name.as_ptr()); } @@ -504,7 +512,7 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> { let val = llvm::LLVMMetadataAsValue(self.llcx, meta); llvm::LLVMAddNamedMetadataOperand( self.llmod, - "wasm.custom_sections\0".as_ptr().cast(), + c"wasm.custom_sections".as_ptr().cast(), val, ); } diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 83101a85435..e8a7afcc632 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -8,7 +8,6 @@ use crate::llvm_util; use crate::type_::Type; use crate::value::Value; -use cstr::cstr; use rustc_codegen_ssa::base::wants_msvc_seh; use rustc_codegen_ssa::traits::*; use rustc_data_structures::base_n; @@ -224,36 +223,42 @@ pub unsafe fn create_module<'ll>( // If skipping the PLT is enabled, we need to add some module metadata // to ensure intrinsic calls don't use it. if !sess.needs_plt() { - let avoid_plt = "RtLibUseGOT\0".as_ptr().cast(); - llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1); + llvm::LLVMRustAddModuleFlag( + llmod, + llvm::LLVMModFlagBehavior::Warning, + c"RtLibUseGOT".as_ptr().cast(), + 1, + ); } // Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.) if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() { - let canonical_jump_tables = "CFI Canonical Jump Tables\0".as_ptr().cast(); llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Override, - canonical_jump_tables, + c"CFI Canonical Jump Tables".as_ptr().cast(), 1, ); } // Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.) if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() { - let enable_split_lto_unit = "EnableSplitLTOUnit\0".as_ptr().cast(); llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Override, - enable_split_lto_unit, + c"EnableSplitLTOUnit".as_ptr().cast(), 1, ); } // Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.) if sess.is_sanitizer_kcfi_enabled() { - let kcfi = "kcfi\0".as_ptr().cast(); - llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1); + llvm::LLVMRustAddModuleFlag( + llmod, + llvm::LLVMModFlagBehavior::Override, + c"kcfi".as_ptr().cast(), + 1, + ); } // Control Flow Guard is currently only supported by the MSVC linker on Windows. @@ -265,7 +270,7 @@ pub unsafe fn create_module<'ll>( llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Warning, - "cfguard\0".as_ptr() as *const _, + c"cfguard".as_ptr() as *const _, 1, ) } @@ -274,7 +279,7 @@ pub unsafe fn create_module<'ll>( llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Warning, - "cfguard\0".as_ptr() as *const _, + c"cfguard".as_ptr() as *const _, 2, ) } @@ -292,26 +297,26 @@ pub unsafe fn create_module<'ll>( llvm::LLVMRustAddModuleFlag( llmod, behavior, - "branch-target-enforcement\0".as_ptr().cast(), + c"branch-target-enforcement".as_ptr().cast(), bti.into(), ); llvm::LLVMRustAddModuleFlag( llmod, behavior, - "sign-return-address\0".as_ptr().cast(), + c"sign-return-address".as_ptr().cast(), pac_ret.is_some().into(), ); let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); llvm::LLVMRustAddModuleFlag( llmod, behavior, - "sign-return-address-all\0".as_ptr().cast(), + c"sign-return-address-all".as_ptr().cast(), pac_opts.leaf.into(), ); llvm::LLVMRustAddModuleFlag( llmod, behavior, - "sign-return-address-with-bkey\0".as_ptr().cast(), + c"sign-return-address-with-bkey".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); } else { @@ -327,7 +332,7 @@ pub unsafe fn create_module<'ll>( llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Override, - "cf-protection-branch\0".as_ptr().cast(), + c"cf-protection-branch".as_ptr().cast(), 1, ) } @@ -335,7 +340,7 @@ pub unsafe fn create_module<'ll>( llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Override, - "cf-protection-return\0".as_ptr().cast(), + c"cf-protection-return".as_ptr().cast(), 1, ) } @@ -344,7 +349,7 @@ pub unsafe fn create_module<'ll>( llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Error, - "Virtual Function Elim\0".as_ptr().cast(), + c"Virtual Function Elim".as_ptr().cast(), 1, ); } @@ -476,14 +481,13 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { } pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) { - let section = cstr!("llvm.metadata"); let array = self.const_array(self.type_ptr_to(self.type_i8()), values); unsafe { let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr()); llvm::LLVMSetInitializer(g, array); llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage); - llvm::LLVMSetSection(g, section.as_ptr()); + llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr()); } } } @@ -969,9 +973,9 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> { #[inline] fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! { if let LayoutError::SizeOverflow(_) = err { - self.sess().emit_fatal(Spanned { span, node: err }) + self.sess().emit_fatal(Spanned { span, node: err.into_diagnostic() }) } else { - span_bug!(span, "failed to get layout for `{}`: {}", ty, err) + span_bug!(span, "failed to get layout for `{ty}`: {err:?}") } } } @@ -991,21 +995,12 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> { } else { match fn_abi_request { FnAbiRequest::OfFnPtr { sig, extra_args } => { - span_bug!( - span, - "`fn_abi_of_fn_ptr({}, {:?})` failed: {}", - sig, - extra_args, - err - ); + span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",); } FnAbiRequest::OfInstance { instance, extra_args } => { span_bug!( span, - "`fn_abi_of_instance({}, {:?})` failed: {}", - instance, - extra_args, - err + "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}", ); } } diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs b/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs index 37f30917609..8be54b7eb71 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs @@ -38,7 +38,6 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, ' unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) }; section_var.unwrap_or_else(|| { - let section_name = b".debug_gdb_scripts\0"; let mut section_contents = Vec::new(); // Add the pretty printers for the standard library first. @@ -71,7 +70,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, ' let section_var = cx .define_global(section_var_name, llvm_type) .unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name)); - llvm::LLVMSetSection(section_var, section_name.as_ptr().cast()); + llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr().cast()); llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents)); llvm::LLVMSetGlobalConstant(section_var, llvm::True); llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index bd2fba12602..166454d3ae7 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -20,7 +20,6 @@ use crate::llvm::debuginfo::{ }; use crate::value::Value; -use cstr::cstr; use rustc_codegen_ssa::debuginfo::type_names::cpp_like_debuginfo; use rustc_codegen_ssa::debuginfo::type_names::VTableNameKind; use rustc_codegen_ssa::traits::*; @@ -812,7 +811,6 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>( let name_in_debuginfo = name_in_debuginfo.to_string_lossy(); let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped); - let flags = "\0"; let output_filenames = tcx.output_filenames(()); let split_name = if tcx.sess.target_can_use_split_dwarf() { output_filenames @@ -849,7 +847,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>( producer.as_ptr().cast(), producer.len(), tcx.sess.opts.optimize != config::OptLevel::No, - flags.as_ptr().cast(), + c"".as_ptr().cast(), 0, // NB: this doesn't actually have any perceptible effect, it seems. LLVM will instead // put the path supplied to `MCSplitDwarfFile` into the debug info of the final @@ -878,8 +876,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>( ); let val = llvm::LLVMMetadataAsValue(debug_context.llcontext, gcov_metadata); - let llvm_gcov_ident = cstr!("llvm.gcov"); - llvm::LLVMAddNamedMetadataOperand(debug_context.llmod, llvm_gcov_ident.as_ptr(), val); + llvm::LLVMAddNamedMetadataOperand(debug_context.llmod, c"llvm.gcov".as_ptr(), val); } // Insert `llvm.ident` metadata on the wasm targets since that will @@ -892,7 +889,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>( ); llvm::LLVMAddNamedMetadataOperand( debug_context.llmod, - cstr!("llvm.ident").as_ptr(), + c"llvm.ident".as_ptr(), llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1), ); } diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index c3f0a0033b0..aa7ae9355bc 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -113,7 +113,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { llvm::LLVMRustAddModuleFlag( self.llmod, llvm::LLVMModFlagBehavior::Warning, - "Dwarf Version\0".as_ptr().cast(), + c"Dwarf Version".as_ptr().cast(), dwarf_version, ); } else { @@ -121,17 +121,16 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { llvm::LLVMRustAddModuleFlag( self.llmod, llvm::LLVMModFlagBehavior::Warning, - "CodeView\0".as_ptr().cast(), + c"CodeView".as_ptr().cast(), 1, ) } // Prevent bitcode readers from deleting the debug info. - let ptr = "Debug Info Version\0".as_ptr(); llvm::LLVMRustAddModuleFlag( self.llmod, llvm::LLVMModFlagBehavior::Warning, - ptr.cast(), + c"Debug Info Version".as_ptr().cast(), llvm::LLVMRustDebugMetadataVersion(), ); } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index 6a9173ab450..44869ced1ae 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -50,9 +50,15 @@ pub(crate) struct SymbolAlreadyDefined<'a> { } #[derive(Diagnostic)] -#[diag(codegen_llvm_invalid_minimum_alignment)] -pub(crate) struct InvalidMinimumAlignment { - pub err: String, +#[diag(codegen_llvm_invalid_minimum_alignment_not_power_of_two)] +pub(crate) struct InvalidMinimumAlignmentNotPowerOfTwo { + pub align: u64, +} + +#[derive(Diagnostic)] +#[diag(codegen_llvm_invalid_minimum_alignment_too_large)] +pub(crate) struct InvalidMinimumAlignmentTooLarge { + pub align: u64, } #[derive(Diagnostic)] diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index 805843e5863..ff9909c720e 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -11,6 +11,7 @@ #![feature(let_chains)] #![feature(never_type)] #![feature(impl_trait_in_assoc_type)] +#![feature(c_str_literals)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] #![deny(rustc::untranslatable_diagnostic)] diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs index 6297f91341d..e91f7b86e5e 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs @@ -93,8 +93,7 @@ fn push_debuginfo_type_name<'tcx>( Err(e) => { // Computing the layout can still fail here, e.g. if the target architecture // cannot represent the type. See https://github.com/rust-lang/rust/issues/94961. - // FIXME: migrate once `rustc_middle::mir::interpret::InterpError` is translatable. - tcx.sess.fatal(format!("{}", e)); + tcx.sess.emit_fatal(e.into_diagnostic()); } } } else { diff --git a/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs b/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs index 1479242f23a..9ac2424e76b 100644 --- a/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs +++ b/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs @@ -211,8 +211,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { args[1].val.unaligned_volatile_store(bx, dst); return; } - | sym::unchecked_div - | sym::unchecked_rem | sym::unchecked_shl | sym::unchecked_shr | sym::unchecked_add @@ -229,20 +227,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { bx.exactudiv(args[0].immediate(), args[1].immediate()) } } - sym::unchecked_div => { - if signed { - bx.sdiv(args[0].immediate(), args[1].immediate()) - } else { - bx.udiv(args[0].immediate(), args[1].immediate()) - } - } - sym::unchecked_rem => { - if signed { - bx.srem(args[0].immediate(), args[1].immediate()) - } else { - bx.urem(args[0].immediate(), args[1].immediate()) - } - } sym::unchecked_shl => bx.shl(args[0].immediate(), args[1].immediate()), sym::unchecked_shr => { if signed { diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl index 917bd829572..bf660c59cab 100644 --- a/compiler/rustc_const_eval/messages.ftl +++ b/compiler/rustc_const_eval/messages.ftl @@ -1,8 +1,142 @@ +const_eval_address_space_full = + there are no more free addresses in the address space +const_eval_align_check_failed = accessing memory with alignment {$has}, but alignment {$required} is required +const_eval_align_offset_invalid_align = + `align_offset` called with non-power-of-two align: {$target_align} + +const_eval_alignment_check_failed = + accessing memory with alignment {$has}, but alignment {$required} is required +const_eval_already_reported = + an error has already been reported elsewhere (this should not usually be printed) +const_eval_assume_false = + `assume` called with `false` + +const_eval_await_non_const = + cannot convert `{$ty}` into a future in {const_eval_const_context}s +const_eval_bounds_check_failed = + indexing out of bounds: the len is {$len} but the index is {$index} +const_eval_box_to_mut = {$front_matter}: encountered a box pointing to mutable memory in a constant +const_eval_box_to_static = {$front_matter}: encountered a box pointing to a static variable in a constant +const_eval_box_to_uninhabited = {$front_matter}: encountered a box pointing to uninhabited type {$ty} +const_eval_call_nonzero_intrinsic = + `{$name}` called on 0 + +const_eval_closure_call = + closures need an RFC before allowed to be called in {const_eval_const_context}s +const_eval_closure_fndef_not_const = + function defined here, but it is not `const` +const_eval_closure_non_const = + cannot call non-const closure in {const_eval_const_context}s +const_eval_consider_dereferencing = + consider dereferencing here +const_eval_const_accesses_static = constant accesses static + +const_eval_const_context = {$kind -> + [const] constant + [static] static + [const_fn] constant function + *[other] {""} +} + +const_eval_copy_nonoverlapping_overlapping = + `copy_nonoverlapping` called on overlapping ranges + +const_eval_dangling_box_no_provenance = {$front_matter}: encountered a dangling box ({$pointer} has no provenance) +const_eval_dangling_box_out_of_bounds = {$front_matter}: encountered a dangling box (going beyond the bounds of its allocation) +const_eval_dangling_box_use_after_free = {$front_matter}: encountered a dangling box (use-after-free) +const_eval_dangling_int_pointer = + {$bad_pointer_message}: {$pointer} is a dangling pointer (it has no provenance) +const_eval_dangling_null_pointer = + {$bad_pointer_message}: null pointer is a dangling pointer (it has no provenance) +const_eval_dangling_ptr_in_final = encountered dangling pointer in final constant + +const_eval_dangling_ref_no_provenance = {$front_matter}: encountered a dangling reference ({$pointer} has no provenance) +const_eval_dangling_ref_out_of_bounds = {$front_matter}: encountered a dangling reference (going beyond the bounds of its allocation) +const_eval_dangling_ref_use_after_free = {$front_matter}: encountered a dangling reference (use-after-free) +const_eval_dead_local = + accessing a dead local variable +const_eval_dealloc_immutable = + deallocating immutable allocation {$alloc} + +const_eval_dealloc_incorrect_layout = + incorrect layout on deallocation: {$alloc} has size {$size} and alignment {$align}, but gave size {$size_found} and alignment {$align_found} + +const_eval_dealloc_kind_mismatch = + deallocating {$alloc}, which is {$alloc_kind} memory, using {$kind} deallocation operation + +const_eval_deref_coercion_non_const = + cannot perform deref coercion on `{$ty}` in {const_eval_const_context}s + .note = attempting to deref into `{$target_ty}` + .target_note = deref defined here +const_eval_deref_function_pointer = + accessing {$allocation} which contains a function +const_eval_deref_test = dereferencing pointer failed +const_eval_deref_vtable_pointer = + accessing {$allocation} which contains a vtable +const_eval_different_allocations = + `{$name}` called on pointers into different allocations + +const_eval_division_by_zero = + dividing by zero +const_eval_division_overflow = + overflow in signed division (dividing MIN by -1) +const_eval_double_storage_live = + StorageLive on a local that was already live + +const_eval_dyn_call_not_a_method = + `dyn` call trying to call something that is not a method + +const_eval_dyn_call_vtable_mismatch = + `dyn` call on a pointer whose vtable does not match its type + +const_eval_dyn_star_call_vtable_mismatch = + `dyn*` call on a pointer whose vtable does not match its type + +const_eval_erroneous_constant = + erroneous constant used + +const_eval_error = {$error_kind -> + [static] could not evaluate static initializer + [const] evaluation of constant value failed + [const_with_path] evaluation of `{$instance}` failed + *[other] {""} +} + +const_eval_exact_div_has_remainder = + exact_div: {$a} cannot be divided by {$b} without remainder + +const_eval_expected_non_ptr = {$front_matter}: encountered `{$value}`, but expected plain (non-pointer) bytes +const_eval_fn_ptr_call = + function pointers need an RFC before allowed to be called in {const_eval_const_context}s +const_eval_for_loop_into_iter_non_const = + cannot convert `{$ty}` into an iterator in {const_eval_const_context}s + +const_eval_frame_note = {$times -> + [0] {const_eval_frame_note_inner} + *[other] [... {$times} additional calls {const_eval_frame_note_inner} ...] +} + +const_eval_frame_note_inner = inside {$where_ -> + [closure] closure + [instance] `{$instance}` + *[other] {""} +} + +const_eval_in_bounds_test = out-of-bounds pointer use +const_eval_incompatible_calling_conventions = + calling a function with calling convention {$callee_conv} using calling convention {$caller_conv} + +const_eval_incompatible_return_types = + calling a function with return type {$callee_ty} passing return place of type {$caller_ty} + +const_eval_incompatible_types = + calling a function with argument of type {$callee_ty} passing data of type {$caller_ty} + const_eval_interior_mutability_borrow = cannot borrow here, since the borrowed element may contain interior mutability const_eval_interior_mutable_data_refer = - {$kind}s cannot refer to interior mutable data + {const_eval_const_context}s cannot refer to interior mutable data .label = this borrow of an interior mutable value may end up in the final value .help = to fix this, the value can be extracted to a separate `static` item and then referenced .teach_note = @@ -10,25 +144,163 @@ const_eval_interior_mutable_data_refer = This would make multiple uses of a constant to be able to see different values and allow circumventing the `Send` and `Sync` requirements for shared mutable data, which is unsound. +const_eval_invalid_align = + align has to be a power of 2 + +const_eval_invalid_align_details = + invalid align passed to `{$name}`: {$align} is {$err_kind -> + [not_power_of_two] not a power of 2 + [too_large] too large + *[other] {""} + } + +const_eval_invalid_bool = + interpreting an invalid 8-bit value as a bool: 0x{$value} +const_eval_invalid_box_meta = {$front_matter}: encountered invalid box metadata: total size is bigger than largest supported object +const_eval_invalid_box_slice_meta = {$front_matter}: encountered invalid box metadata: slice is bigger than largest supported object +const_eval_invalid_char = + interpreting an invalid 32-bit value as a char: 0x{$value} +const_eval_invalid_dealloc = + deallocating {$alloc_id}, which is {$kind -> + [fn] a function + [vtable] a vtable + [static_mem] static memory + *[other] {""} + } + +const_eval_invalid_enum_tag = {$front_matter}: encountered {$value}, but expected a valid enum tag +const_eval_invalid_fn_ptr = {$front_matter}: encountered {$value}, but expected a function pointer +const_eval_invalid_function_pointer = + using {$pointer} as function pointer but it does not point to a function +const_eval_invalid_meta = + invalid metadata in wide pointer: total size is bigger than largest supported object +const_eval_invalid_meta_slice = + invalid metadata in wide pointer: slice is bigger than largest supported object +const_eval_invalid_ref_meta = {$front_matter}: encountered invalid reference metadata: total size is bigger than largest supported object +const_eval_invalid_ref_slice_meta = {$front_matter}: encountered invalid reference metadata: slice is bigger than largest supported object +const_eval_invalid_str = + this string is not valid UTF-8: {$err} +const_eval_invalid_tag = + enum value has invalid tag: {$tag} +const_eval_invalid_transmute = + transmuting from {$src_bytes}-byte type to {$dest_bytes}-byte type: `{$src}` -> `{$dest}` + +const_eval_invalid_uninit_bytes = + reading memory at {$alloc}{$access}, but memory is uninitialized at {$uninit}, and this operation requires initialized memory +const_eval_invalid_uninit_bytes_unknown = + using uninitialized data, but this operation requires initialized memory +const_eval_invalid_value = constructing invalid value +const_eval_invalid_value_with_path = constructing invalid value at {$path} +## The `front_matter`s here refer to either `middle_invalid_value` or `middle_invalid_value_with_path`. + +const_eval_invalid_vtable_pointer = + using {$pointer} as vtable pointer but it does not point to a vtable + +const_eval_invalid_vtable_ptr = {$front_matter}: encountered {$value}, but expected a vtable pointer + +const_eval_live_drop = + destructor of `{$dropped_ty}` cannot be evaluated at compile-time + .label = the destructor for this type cannot be evaluated in {const_eval_const_context}s + .dropped_at_label = value is dropped here + const_eval_long_running = constant evaluation is taking a long time .note = this lint makes sure the compiler doesn't get stuck due to infinite loops in const eval. If your compilation actually takes a long time, you can safely allow the lint. .label = the const evaluator is currently interpreting this expression .help = the constant being evaluated + const_eval_max_num_nodes_in_const = maximum number of nodes exceeded in constant {$global_const_id} +const_eval_memory_access_test = memory access failed +const_eval_memory_exhausted = + tried to allocate more memory than available to compiler +const_eval_modified_global = + modifying a static's initial value from another static's initializer + const_eval_mut_deref = - mutation through a reference is not allowed in {$kind}s + mutation through a reference is not allowed in {const_eval_const_context}s +const_eval_mutable_ref_in_const = {$front_matter}: encountered mutable reference in a `const` +const_eval_never_val = {$front_matter}: encountered a value of the never type `!` const_eval_non_const_fmt_macro_call = - cannot call non-const formatting macro in {$kind}s + cannot call non-const formatting macro in {const_eval_const_context}s const_eval_non_const_fn_call = - cannot call non-const fn `{$def_path_str}` in {$kind}s + cannot call non-const fn `{$def_path_str}` in {const_eval_const_context}s + +const_eval_non_const_impl = + impl defined here, but it is not `const` + +const_eval_noreturn_asm_returned = + returned from noreturn inline assembly + +const_eval_not_enough_caller_args = + calling a function with fewer arguments than it requires + +const_eval_null_box = {$front_matter}: encountered a null box +const_eval_null_fn_ptr = {$front_matter}: encountered a null function pointer +const_eval_null_ref = {$front_matter}: encountered a null reference +const_eval_nullable_ptr_out_of_range = {$front_matter}: encountered a potentially null pointer, but expected something that cannot possibly fail to be {$in_range} +const_eval_nullary_intrinsic_fail = + could not evaluate nullary intrinsic + +const_eval_offset_from_overflow = + `{$name}` called when first pointer is too far ahead of second + +const_eval_offset_from_test = out-of-bounds `offset_from` +const_eval_offset_from_underflow = + `{$name}` called when first pointer is too far before second + +const_eval_operator_non_const = + cannot call non-const operator in {const_eval_const_context}s +const_eval_out_of_range = {$front_matter}: encountered {$value}, but expected something {$in_range} +const_eval_overflow = + overflow executing `{$name}` + +const_eval_overflow_shift = + overflowing shift by {$val} in `{$name}` + +const_eval_panic = + the evaluated program panicked at '{$msg}', {$file}:{$line}:{$col} const_eval_panic_non_str = argument to `panic!()` in a const context must have type `&str` +const_eval_partial_pointer_copy = + unable to copy parts of a pointer from memory at {$ptr} +const_eval_partial_pointer_overwrite = + unable to overwrite parts of a pointer in memory at {$ptr} +const_eval_pointer_arithmetic_overflow = + overflowing in-bounds pointer arithmetic +const_eval_pointer_arithmetic_test = out-of-bounds pointer arithmetic +const_eval_pointer_out_of_bounds = + {$bad_pointer_message}: {$alloc_id} has size {$alloc_size}, so pointer to {$ptr_size} {$ptr_size -> + [1] byte + *[many] bytes + } starting at offset {$ptr_offset} is out-of-bounds +const_eval_pointer_use_after_free = + pointer to {$allocation} was dereferenced after this allocation got freed +const_eval_ptr_as_bytes_1 = + this code performed an operation that depends on the underlying bytes representing a pointer +const_eval_ptr_as_bytes_2 = + the absolute address of a pointer is not known at compile-time, so such operations are not supported +const_eval_ptr_out_of_range = {$front_matter}: encountered a pointer, but expected something that cannot possibly fail to be {$in_range} +const_eval_question_branch_non_const = + `?` cannot determine the branch of `{$ty}` in {const_eval_const_context}s + +const_eval_question_from_residual_non_const = + `?` cannot convert from residual of `{$ty}` in {const_eval_const_context}s + +const_eval_range = in the range {$lo}..={$hi} +const_eval_range_lower = greater or equal to {$lo} +const_eval_range_singular = equal to {$lo} +const_eval_range_upper = less or equal to {$hi} +const_eval_range_wrapping = less or equal to {$hi}, or greater or equal to {$lo} +const_eval_raw_bytes = the raw bytes of the constant (size: {$size}, align: {$align}) {"{"}{$bytes}{"}"} + +const_eval_raw_eq_with_provenance = + `raw_eq` on bytes with provenance + const_eval_raw_ptr_comparison = pointers cannot be reliably compared during const eval .note = see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information @@ -38,8 +310,36 @@ const_eval_raw_ptr_to_int = .note = at compile-time, pointers do not have an integer value .note2 = avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior +const_eval_read_extern_static = + cannot read from extern static ({$did}) +const_eval_read_pointer_as_bytes = + unable to turn pointer into raw bytes +const_eval_realloc_or_alloc_with_offset = + {$kind -> + [dealloc] deallocating + [realloc] reallocating + *[other] {""} + } {$ptr} which does not point to the beginning of an object + +const_eval_ref_to_mut = {$front_matter}: encountered a reference pointing to mutable memory in a constant +const_eval_ref_to_static = {$front_matter}: encountered a reference pointing to a static variable in a constant +const_eval_ref_to_uninhabited = {$front_matter}: encountered a reference pointing to uninhabited type {$ty} +const_eval_remainder_by_zero = + calculating the remainder with a divisor of zero +const_eval_remainder_overflow = + overflow in signed remainder (dividing MIN by -1) +const_eval_scalar_size_mismatch = + scalar size mismatch: expected {$target_size} bytes but got {$data_size} bytes instead +const_eval_size_of_unsized = + size_of called on unsized type `{$ty}` +const_eval_size_overflow = + overflow computing total size of `{$name}` + +const_eval_stack_frame_limit_reached = + reached the configured maximum number of stack frames + const_eval_static_access = - {$kind}s cannot refer to statics + {const_eval_const_context}s cannot refer to statics .help = consider extracting the value of the `static` to a `const`, and referring to that .teach_note = `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable. .teach_help = To fix this, the value can be extracted to a `const` and then used. @@ -47,27 +347,34 @@ const_eval_static_access = const_eval_thread_local_access = thread-local statics cannot be accessed at compile-time -const_eval_transient_mut_borrow = mutable references are not allowed in {$kind}s +const_eval_thread_local_static = + cannot access thread local static ({$did}) +const_eval_too_generic = + encountered overly generic constant +const_eval_too_many_caller_args = + calling a function with more arguments than it expected -const_eval_transient_mut_borrow_raw = raw mutable references are not allowed in {$kind}s +const_eval_transient_mut_borrow = mutable references are not allowed in {const_eval_const_context}s -const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in {$kind}s +const_eval_transient_mut_borrow_raw = raw mutable references are not allowed in {const_eval_const_context}s + +const_eval_try_block_from_output_non_const = + `try` block cannot convert `{$ty}` to the result in {const_eval_const_context}s +const_eval_unaligned_box = {$front_matter}: encountered an unaligned box (required {$required_bytes} byte alignment but found {$found_bytes}) +const_eval_unaligned_ref = {$front_matter}: encountered an unaligned reference (required {$required_bytes} byte alignment but found {$found_bytes}) +const_eval_unallowed_fn_pointer_call = function pointer calls are not allowed in {const_eval_const_context}s const_eval_unallowed_heap_allocations = - allocations are not allowed in {$kind}s - .label = allocation not allowed in {$kind}s + allocations are not allowed in {const_eval_const_context}s + .label = allocation not allowed in {const_eval_const_context}s .teach_note = The value of statics and constants must be known at compile time, and they live for the entire lifetime of a program. Creating a boxed value allocates memory on the heap at runtime, and therefore cannot be done at compile time. const_eval_unallowed_inline_asm = - inline assembly is not allowed in {$kind}s - + inline assembly is not allowed in {const_eval_const_context}s const_eval_unallowed_mutable_refs = - mutable references are not allowed in the final value of {$kind}s + mutable references are not allowed in the final value of {const_eval_const_context}s .teach_note = - References in statics and constants may only refer to immutable values. - - Statics are shared everywhere, and if they refer to mutable data one might violate memory safety since holding multiple mutable references to shared data is not allowed. @@ -75,7 +382,7 @@ const_eval_unallowed_mutable_refs = If you really want global mutable state, try using static mut or a global UnsafeCell. const_eval_unallowed_mutable_refs_raw = - raw mutable references are not allowed in the final value of {$kind}s + raw mutable references are not allowed in the final value of {const_eval_const_context}s .teach_note = References in statics and constants may only refer to immutable values. @@ -89,9 +396,59 @@ const_eval_unallowed_mutable_refs_raw = const_eval_unallowed_op_in_const_context = {$msg} +const_eval_undefined_behavior = + it is undefined behavior to use this value + +const_eval_undefined_behavior_note = + The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + +const_eval_uninhabited_enum_variant_written = + writing discriminant of an uninhabited enum +const_eval_uninhabited_val = {$front_matter}: encountered a value of uninhabited type `{$ty}` +const_eval_uninit = {$front_matter}: encountered uninitialized bytes +const_eval_uninit_bool = {$front_matter}: encountered uninitialized memory, but expected a boolean +const_eval_uninit_box = {$front_matter}: encountered uninitialized memory, but expected a box +const_eval_uninit_char = {$front_matter}: encountered uninitialized memory, but expected a unicode scalar value +const_eval_uninit_enum_tag = {$front_matter}: encountered uninitialized bytes, but expected a valid enum tag +const_eval_uninit_float = {$front_matter}: encountered uninitialized memory, but expected a floating point number +const_eval_uninit_fn_ptr = {$front_matter}: encountered uninitialized memory, but expected a function pointer +const_eval_uninit_init_scalar = {$front_matter}: encountered uninitialized memory, but expected initialized scalar value +const_eval_uninit_int = {$front_matter}: encountered uninitialized memory, but expected an integer +const_eval_uninit_raw_ptr = {$front_matter}: encountered uninitialized memory, but expected a raw pointer +const_eval_uninit_ref = {$front_matter}: encountered uninitialized memory, but expected a reference +const_eval_uninit_str = {$front_matter}: encountered uninitialized data in `str` +const_eval_uninit_unsized_local = + unsized local is used while uninitialized +const_eval_unreachable = entering unreachable code +const_eval_unreachable_unwind = + unwinding past a stack frame that does not allow unwinding + +const_eval_unsafe_cell = {$front_matter}: encountered `UnsafeCell` in a `const` +const_eval_unsigned_offset_from_overflow = + `ptr_offset_from_unsigned` called when first pointer has smaller offset than second: {$a_offset} < {$b_offset} + const_eval_unstable_const_fn = `{$def_path}` is not yet stable as a const fn const_eval_unstable_in_stable = const-stable function cannot use `#[feature({$gate})]` .unstable_sugg = if it is not part of the public API, make this function unstably const .bypass_sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks + +const_eval_unsupported_untyped_pointer = unsupported untyped pointer in constant + .note = memory only reachable via raw pointers is not supported + +const_eval_unterminated_c_string = + reading a null-terminated string starting at {$pointer} with no null found before end of allocation + +const_eval_unwind_past_top = + unwinding past the topmost frame of the stack + +const_eval_upcast_mismatch = + upcast on a pointer whose vtable does not match its type + +const_eval_validation_invalid_bool = {$front_matter}: encountered {$value}, but expected a boolean +const_eval_validation_invalid_char = {$front_matter}: encountered {$value}, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) +const_eval_write_to_read_only = + writing to {$allocation} which is read-only +const_eval_zst_pointer_out_of_bounds = + {$bad_pointer_message}: {$alloc_id} has size {$alloc_size}, so pointer at offset {$ptr_offset} is out-of-bounds diff --git a/compiler/rustc_const_eval/src/const_eval/error.rs b/compiler/rustc_const_eval/src/const_eval/error.rs index c591ff75ab8..7890d878d08 100644 --- a/compiler/rustc_const_eval/src/const_eval/error.rs +++ b/compiler/rustc_const_eval/src/const_eval/error.rs @@ -1,17 +1,15 @@ -use std::error::Error; -use std::fmt; +use std::mem; -use rustc_errors::Diagnostic; +use rustc_errors::{DiagnosticArgValue, DiagnosticMessage, IntoDiagnostic, IntoDiagnosticArg}; use rustc_middle::mir::AssertKind; -use rustc_middle::query::TyCtxtAt; +use rustc_middle::ty::TyCtxt; use rustc_middle::ty::{layout::LayoutError, ConstInt}; -use rustc_span::{Span, Symbol}; +use rustc_span::source_map::Spanned; +use rustc_span::{ErrorGuaranteed, Span, Symbol}; use super::InterpCx; -use crate::interpret::{ - struct_error, ErrorHandled, FrameInfo, InterpError, InterpErrorInfo, Machine, MachineStopType, - UnsupportedOpInfo, -}; +use crate::errors::{self, FrameNote, ReportErrorExt}; +use crate::interpret::{ErrorHandled, InterpError, InterpErrorInfo, Machine, MachineStopType}; /// The CTFE machine has some custom error kinds. #[derive(Clone, Debug)] @@ -23,7 +21,35 @@ pub enum ConstEvalErrKind { Abort(String), } -impl MachineStopType for ConstEvalErrKind {} +impl MachineStopType for ConstEvalErrKind { + fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; + use ConstEvalErrKind::*; + match self { + ConstAccessesStatic => const_eval_const_accesses_static, + ModifiedGlobal => const_eval_modified_global, + Panic { .. } => const_eval_panic, + AssertFailure(x) => x.diagnostic_message(), + Abort(msg) => msg.to_string().into(), + } + } + fn add_args( + self: Box<Self>, + adder: &mut dyn FnMut(std::borrow::Cow<'static, str>, DiagnosticArgValue<'static>), + ) { + use ConstEvalErrKind::*; + match *self { + ConstAccessesStatic | ModifiedGlobal | Abort(_) => {} + AssertFailure(kind) => kind.add_args(adder), + Panic { msg, line, col, file } => { + adder("msg".into(), msg.into_diagnostic_arg()); + adder("file".into(), file.into_diagnostic_arg()); + adder("line".into(), line.into_diagnostic_arg()); + adder("col".into(), col.into_diagnostic_arg()); + } + } + } +} // The errors become `MachineStop` with plain strings when being raised. // `ConstEvalErr` (in `librustc_middle/mir/interpret/error.rs`) knows to @@ -34,151 +60,117 @@ impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind { } } -impl fmt::Display for ConstEvalErrKind { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use self::ConstEvalErrKind::*; - match self { - ConstAccessesStatic => write!(f, "constant accesses static"), - ModifiedGlobal => { - write!(f, "modifying a static's initial value from another static's initializer") - } - AssertFailure(msg) => write!(f, "{:?}", msg), - Panic { msg, line, col, file } => { - write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col) - } - Abort(msg) => write!(f, "{}", msg), - } - } -} - -impl Error for ConstEvalErrKind {} +pub fn get_span_and_frames<'tcx, 'mir, M: Machine<'mir, 'tcx>>( + ecx: &InterpCx<'mir, 'tcx, M>, +) -> (Span, Vec<errors::FrameNote>) +where + 'tcx: 'mir, +{ + let mut stacktrace = ecx.generate_stacktrace(); + // Filter out `requires_caller_location` frames. + stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*ecx.tcx)); + let span = stacktrace.first().map(|f| f.span).unwrap_or(ecx.tcx.span); -/// When const-evaluation errors, this type is constructed with the resulting information, -/// and then used to emit the error as a lint or hard error. -#[derive(Debug)] -pub(super) struct ConstEvalErr<'tcx> { - pub span: Span, - pub error: InterpError<'tcx>, - pub stacktrace: Vec<FrameInfo<'tcx>>, -} + let mut frames = Vec::new(); -impl<'tcx> ConstEvalErr<'tcx> { - /// Turn an interpreter error into something to report to the user. - /// As a side-effect, if RUSTC_CTFE_BACKTRACE is set, this prints the backtrace. - /// Should be called only if the error is actually going to be reported! - pub fn new<'mir, M: Machine<'mir, 'tcx>>( - ecx: &InterpCx<'mir, 'tcx, M>, - error: InterpErrorInfo<'tcx>, - span: Option<Span>, - ) -> ConstEvalErr<'tcx> - where - 'tcx: 'mir, - { - error.print_backtrace(); - let mut stacktrace = ecx.generate_stacktrace(); - // Filter out `requires_caller_location` frames. - stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(*ecx.tcx)); - // If `span` is missing, use topmost remaining frame, or else the "root" span from `ecx.tcx`. - let span = span.or_else(|| stacktrace.first().map(|f| f.span)).unwrap_or(ecx.tcx.span); - ConstEvalErr { error: error.into_kind(), stacktrace, span } - } - - pub(super) fn report(&self, tcx: TyCtxtAt<'tcx>, message: &str) -> ErrorHandled { - self.report_decorated(tcx, message, |_| {}) - } - - #[instrument(level = "trace", skip(self, decorate))] - pub(super) fn decorate(&self, err: &mut Diagnostic, decorate: impl FnOnce(&mut Diagnostic)) { - trace!("reporting const eval failure at {:?}", self.span); - // Add some more context for select error types. - match self.error { - InterpError::Unsupported( - UnsupportedOpInfo::ReadPointerAsBytes - | UnsupportedOpInfo::PartialPointerOverwrite(_) - | UnsupportedOpInfo::PartialPointerCopy(_), - ) => { - err.help("this code performed an operation that depends on the underlying bytes representing a pointer"); - err.help("the absolute address of a pointer is not known at compile-time, so such operations are not supported"); + // Add notes to the backtrace. Don't print a single-line backtrace though. + if stacktrace.len() > 1 { + // Helper closure to print duplicated lines. + let mut add_frame = |mut frame: errors::FrameNote| { + frames.push(errors::FrameNote { times: 0, ..frame.clone() }); + // Don't print [... additional calls ...] if the number of lines is small + if frame.times < 3 { + let times = frame.times; + frame.times = 0; + frames.extend(std::iter::repeat(frame).take(times as usize)); + } else { + frames.push(frame); } - _ => {} - } - // Add spans for the stacktrace. Don't print a single-line backtrace though. - if self.stacktrace.len() > 1 { - // Helper closure to print duplicated lines. - let mut flush_last_line = |last_frame: Option<(String, _)>, times| { - if let Some((line, span)) = last_frame { - err.span_note(span, line.clone()); - // Don't print [... additional calls ...] if the number of lines is small - if times < 3 { - for _ in 0..times { - err.span_note(span, line.clone()); - } - } else { - err.span_note( - span, - format!("[... {} additional calls {} ...]", times, &line), - ); - } - } - }; + }; - let mut last_frame = None; - let mut times = 0; - for frame_info in &self.stacktrace { - let frame = (frame_info.to_string(), frame_info.span); - if last_frame.as_ref() == Some(&frame) { - times += 1; - } else { - flush_last_line(last_frame, times); + let mut last_frame: Option<errors::FrameNote> = None; + for frame_info in &stacktrace { + let frame = frame_info.as_note(*ecx.tcx); + match last_frame.as_mut() { + Some(last_frame) + if last_frame.span == frame.span + && last_frame.where_ == frame.where_ + && last_frame.instance == frame.instance => + { + last_frame.times += 1; + } + Some(last_frame) => { + add_frame(mem::replace(last_frame, frame)); + } + None => { last_frame = Some(frame); - times = 0; } } - flush_last_line(last_frame, times); } - // Let the caller attach any additional information it wants. - decorate(err); + if let Some(frame) = last_frame { + add_frame(frame); + } } - /// Create a diagnostic for this const eval error. - /// - /// Sets the message passed in via `message` and adds span labels with detailed error - /// information before handing control back to `decorate` to do any final annotations, - /// after which the diagnostic is emitted. - /// - /// If `lint_root.is_some()` report it as a lint, else report it as a hard error. - /// (Except that for some errors, we ignore all that -- see `must_error` below.) - #[instrument(skip(self, tcx, decorate), level = "debug")] - pub(super) fn report_decorated( - &self, - tcx: TyCtxtAt<'tcx>, - message: &str, - decorate: impl FnOnce(&mut Diagnostic), - ) -> ErrorHandled { - debug!("self.error: {:?}", self.error); - // Special handling for certain errors - match &self.error { - // Don't emit a new diagnostic for these errors - err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => { - ErrorHandled::TooGeneric - } - err_inval!(AlreadyReported(error_reported)) => ErrorHandled::Reported(*error_reported), - err_inval!(Layout(LayoutError::SizeOverflow(_))) => { - // We must *always* hard error on these, even if the caller wants just a lint. - // The `message` makes little sense here, this is a more serious error than the - // caller thinks anyway. - // See <https://github.com/rust-lang/rust/pull/63152>. - let mut err = struct_error(tcx, &self.error.to_string()); - self.decorate(&mut err, decorate); - ErrorHandled::Reported(err.emit().into()) - } - _ => { - // Report as hard error. - let mut err = struct_error(tcx, message); - err.span_label(self.span, self.error.to_string()); - self.decorate(&mut err, decorate); - ErrorHandled::Reported(err.emit().into()) + (span, frames) +} + +/// Create a diagnostic for a const eval error. +/// +/// This will use the `mk` function for creating the error which will get passed labels according to +/// the `InterpError` and the span and a stacktrace of current execution according to +/// `get_span_and_frames`. +pub(super) fn report<'tcx, C, F, E>( + tcx: TyCtxt<'tcx>, + error: InterpError<'tcx>, + span: Option<Span>, + get_span_and_frames: C, + mk: F, +) -> ErrorHandled +where + C: FnOnce() -> (Span, Vec<FrameNote>), + F: FnOnce(Span, Vec<FrameNote>) -> E, + E: IntoDiagnostic<'tcx, ErrorGuaranteed>, +{ + // Special handling for certain errors + match error { + // Don't emit a new diagnostic for these errors + err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => { + ErrorHandled::TooGeneric + } + err_inval!(AlreadyReported(error_reported)) => ErrorHandled::Reported(error_reported), + err_inval!(Layout(layout_error @ LayoutError::SizeOverflow(_))) => { + // We must *always* hard error on these, even if the caller wants just a lint. + // The `message` makes little sense here, this is a more serious error than the + // caller thinks anyway. + // See <https://github.com/rust-lang/rust/pull/63152>. + let (our_span, frames) = get_span_and_frames(); + let span = span.unwrap_or(our_span); + let mut err = + tcx.sess.create_err(Spanned { span, node: layout_error.into_diagnostic() }); + err.code(rustc_errors::error_code!(E0080)); + let Some((mut err, handler)) = err.into_diagnostic() else { + panic!("did not emit diag"); + }; + for frame in frames { + err.eager_subdiagnostic(handler, frame); } + + ErrorHandled::Reported(handler.emit_diagnostic(&mut err).unwrap().into()) + } + _ => { + // Report as hard error. + let (our_span, frames) = get_span_and_frames(); + let span = span.unwrap_or(our_span); + let err = mk(span, frames); + let mut err = tcx.sess.create_err(err); + + let msg = error.diagnostic_message(); + error.add_args(&tcx.sess.parse_sess.span_diagnostic, &mut err); + + // Use *our* span to label the interp error + err.span_label(our_span, msg); + ErrorHandled::Reported(err.emit().into()) } } } diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index e4d34b90018..8b8e8ff58e9 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -1,12 +1,12 @@ use crate::const_eval::CheckAlignment; -use std::borrow::Cow; +use crate::errors::ConstEvalError; use either::{Left, Right}; use rustc_hir::def::DefKind; use rustc_middle::mir; -use rustc_middle::mir::interpret::ErrorHandled; -use rustc_middle::mir::pretty::display_allocation; +use rustc_middle::mir::interpret::{ErrorHandled, InterpErrorInfo}; +use rustc_middle::mir::pretty::write_allocation_bytes; use rustc_middle::traits::Reveal; use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::print::with_no_trimmed_paths; @@ -14,7 +14,8 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_span::source_map::Span; use rustc_target::abi::{self, Abi}; -use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr}; +use super::{CompileTimeEvalContext, CompileTimeInterpreter}; +use crate::errors; use crate::interpret::eval_nullary_intrinsic; use crate::interpret::{ intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId, @@ -22,10 +23,6 @@ use crate::interpret::{ RefTracking, StackPopCleanup, }; -const NOTE_ON_UNDEFINED_BEHAVIOR_ERROR: &str = "The rules on what exactly is undefined behavior aren't clear, \ - so this check might be overzealous. Please open an issue on the rustc \ - repository if you believe it should not be considered undefined behavior."; - // Returns a pointer to where the result lives fn eval_body_using_ecx<'mir, 'tcx>( ecx: &mut CompileTimeEvalContext<'mir, 'tcx>, @@ -253,8 +250,14 @@ pub fn eval_to_const_value_raw_provider<'tcx>( }; return eval_nullary_intrinsic(tcx, key.param_env, def_id, substs).map_err(|error| { let span = tcx.def_span(def_id); - let error = ConstEvalErr { error: error.into_kind(), stacktrace: vec![], span }; - error.report(tcx.at(span), "could not evaluate nullary intrinsic") + + super::report( + tcx, + error.into_kind(), + Some(span), + || (span, vec![]), + |span, _| errors::NullaryIntrinsicError { span }, + ) }); } @@ -318,9 +321,11 @@ pub fn eval_to_allocation_raw_provider<'tcx>( let res = ecx.load_mir(cid.instance.def, cid.promoted); match res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, &body)) { Err(error) => { - let err = ConstEvalErr::new(&ecx, error, None); - let msg = if is_static { - Cow::from("could not evaluate static initializer") + let (error, backtrace) = error.into_parts(); + backtrace.print_backtrace(); + + let (kind, instance) = if is_static { + ("static", String::new()) } else { // If the current item has generics, we'd like to enrich the message with the // instance and its substs: to show the actual compile-time values, in addition to @@ -328,19 +333,29 @@ pub fn eval_to_allocation_raw_provider<'tcx>( let instance = &key.value.instance; if !instance.substs.is_empty() { let instance = with_no_trimmed_paths!(instance.to_string()); - let msg = format!("evaluation of `{}` failed", instance); - Cow::from(msg) + ("const_with_path", instance) } else { - Cow::from("evaluation of constant value failed") + ("const", String::new()) } }; - Err(err.report(ecx.tcx.at(err.span), &msg)) + Err(super::report( + *ecx.tcx, + error, + None, + || super::get_span_and_frames(&ecx), + |span, frames| ConstEvalError { + span, + error_kind: kind, + instance, + frame_notes: frames, + }, + )) } Ok(mplace) => { // Since evaluation had no errors, validate the resulting constant. // This is a separate `try` block to provide more targeted error reporting. - let validation = try { + let validation: Result<_, InterpErrorInfo<'_>> = try { let mut ref_tracking = RefTracking::new(mplace); let mut inner = false; while let Some((mplace, path)) = ref_tracking.todo.pop() { @@ -357,23 +372,37 @@ pub fn eval_to_allocation_raw_provider<'tcx>( } }; let alloc_id = mplace.ptr.provenance.unwrap(); + + // Validation failed, report an error. This is always a hard error. if let Err(error) = validation { - // Validation failed, report an error. This is always a hard error. - let err = ConstEvalErr::new(&ecx, error, None); - Err(err.report_decorated( - ecx.tcx, - "it is undefined behavior to use this value", - |diag| { - if matches!(err.error, InterpError::UndefinedBehavior(_)) { - diag.note(NOTE_ON_UNDEFINED_BEHAVIOR_ERROR); - } - diag.note(format!( - "the raw bytes of the constant ({}", - display_allocation( - *ecx.tcx, - ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner() - ) - )); + let (error, backtrace) = error.into_parts(); + backtrace.print_backtrace(); + + let ub_note = matches!(error, InterpError::UndefinedBehavior(_)).then(|| {}); + + let alloc = ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner(); + let mut bytes = String::new(); + if alloc.size() != abi::Size::ZERO { + bytes = "\n".into(); + // FIXME(translation) there might be pieces that are translatable. + write_allocation_bytes(*ecx.tcx, alloc, &mut bytes, " ").unwrap(); + } + let raw_bytes = errors::RawBytesNote { + size: alloc.size().bytes(), + align: alloc.align.bytes(), + bytes, + }; + + Err(super::report( + *ecx.tcx, + error, + None, + || super::get_span_and_frames(&ecx), + move |span, frames| errors::UndefinedBehavior { + span, + ub_note, + frames, + raw_bytes, }, )) } else { diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs index a8b6b98c96c..7391f567040 100644 --- a/compiler/rustc_const_eval/src/const_eval/machine.rs +++ b/compiler/rustc_const_eval/src/const_eval/machine.rs @@ -25,6 +25,7 @@ use crate::interpret::{ self, compile_time_machine, AllocId, ConstAllocation, FnVal, Frame, ImmTy, InterpCx, InterpResult, OpTy, PlaceTy, Pointer, Scalar, }; +use crate::{errors, fluent_generated as fluent}; use super::error::*; @@ -254,7 +255,10 @@ impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> { let target_align = self.read_scalar(&args[1])?.to_target_usize(self)?; if !target_align.is_power_of_two() { - throw_ub_format!("`align_offset` called with non-power-of-two align: {}", target_align); + throw_ub_custom!( + fluent::const_eval_align_offset_invalid_align, + target_align = target_align, + ); } match self.ptr_try_get_alloc_id(ptr) { @@ -360,15 +364,18 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, "`alignment_check_failed` called when no alignment check requested" ), CheckAlignment::FutureIncompat => { - let err = ConstEvalErr::new(ecx, err, None); - ecx.tcx.struct_span_lint_hir( + let (_, backtrace) = err.into_parts(); + backtrace.print_backtrace(); + let (span, frames) = super::get_span_and_frames(&ecx); + + ecx.tcx.emit_spanned_lint( INVALID_ALIGNMENT, ecx.stack().iter().find_map(|frame| frame.lint_root()).unwrap_or(CRATE_HIR_ID), - err.span, - err.error.to_string(), - |db| { - err.decorate(db, |_| {}); - db + span, + errors::AlignmentCheckFailed { + has: has.bytes(), + required: required.bytes(), + frames, }, ); Ok(()) @@ -482,7 +489,12 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, let align = match Align::from_bytes(align) { Ok(a) => a, - Err(err) => throw_ub_format!("align has to be a power of 2, {}", err), + Err(err) => throw_ub_custom!( + fluent::const_eval_invalid_align_details, + name = "const_allocate", + err_kind = err.diag_ident(), + align = err.align() + ), }; let ptr = ecx.allocate_ptr( @@ -500,7 +512,12 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, let size = Size::from_bytes(size); let align = match Align::from_bytes(align) { Ok(a) => a, - Err(err) => throw_ub_format!("align has to be a power of 2, {}", err), + Err(err) => throw_ub_custom!( + fluent::const_eval_invalid_align_details, + name = "const_deallocate", + err_kind = err.diag_ident(), + align = err.align() + ), }; // If an allocation is created in an another const, diff --git a/compiler/rustc_const_eval/src/const_eval/mod.rs b/compiler/rustc_const_eval/src/const_eval/mod.rs index b59ca8e2070..6e462d3a1e9 100644 --- a/compiler/rustc_const_eval/src/const_eval/mod.rs +++ b/compiler/rustc_const_eval/src/const_eval/mod.rs @@ -73,17 +73,8 @@ pub(crate) fn eval_to_valtree<'tcx>( let global_const_id = cid.display(tcx); match err { ValTreeCreationError::NodesOverflow => { - let msg = format!( - "maximum number of nodes exceeded in constant {}", - &global_const_id - ); - let mut diag = match tcx.hir().span_if_local(did) { - Some(span) => { - tcx.sess.create_err(MaxNumNodesInConstErr { span, global_const_id }) - } - None => tcx.sess.struct_err(msg), - }; - diag.emit(); + let span = tcx.hir().span_if_local(did); + tcx.sess.emit_err(MaxNumNodesInConstErr { span, global_const_id }); Ok(None) } diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index ad2e68e752d..eed3091d481 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -1,6 +1,24 @@ +use rustc_errors::{ + DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler, + IntoDiagnostic, +}; use rustc_hir::ConstContext; -use rustc_macros::{Diagnostic, LintDiagnostic}; +use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; +use rustc_middle::mir::interpret::{ + CheckInAllocMsg, ExpectedKind, InterpError, InvalidMetaKind, InvalidProgramInfo, PointerKind, + ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo, +}; +use rustc_middle::ty::{self, Ty}; use rustc_span::Span; +use rustc_target::abi::call::AdjustForForeignAbiError; +use rustc_target::abi::{Size, WrappingRange}; + +#[derive(Diagnostic)] +#[diag(const_eval_dangling_ptr_in_final)] +pub(crate) struct DanglingPtrInFinal { + #[primary_span] + pub span: Span, +} #[derive(Diagnostic)] #[diag(const_eval_unstable_in_stable)] @@ -92,7 +110,7 @@ pub(crate) struct TransientMutBorrowErrRaw { #[diag(const_eval_max_num_nodes_in_const)] pub(crate) struct MaxNumNodesInConstErr { #[primary_span] - pub span: Span, + pub span: Option<Span>, pub global_const_id: String, } @@ -176,6 +194,14 @@ pub(crate) struct UnallowedInlineAsm { } #[derive(Diagnostic)] +#[diag(const_eval_unsupported_untyped_pointer)] +#[note] +pub(crate) struct UnsupportedUntypedPointer { + #[primary_span] + pub span: Span, +} + +#[derive(Diagnostic)] #[diag(const_eval_interior_mutable_data_refer, code = "E0492")] pub(crate) struct InteriorMutableDataRefer { #[primary_span] @@ -212,3 +238,631 @@ pub struct LongRunningWarn { #[help] pub item_span: Span, } + +#[derive(Diagnostic)] +#[diag(const_eval_erroneous_constant)] +pub(crate) struct ErroneousConstUsed { + #[primary_span] + pub span: Span, +} + +#[derive(Subdiagnostic)] +#[note(const_eval_non_const_impl)] +pub(crate) struct NonConstImplNote { + #[primary_span] + pub span: Span, +} + +#[derive(Subdiagnostic, PartialEq, Eq, Clone)] +#[note(const_eval_frame_note)] +pub struct FrameNote { + #[primary_span] + pub span: Span, + pub times: i32, + pub where_: &'static str, + pub instance: String, +} + +#[derive(Subdiagnostic)] +#[note(const_eval_raw_bytes)] +pub struct RawBytesNote { + pub size: u64, + pub align: u64, + pub bytes: String, +} + +#[derive(Diagnostic)] +#[diag(const_eval_for_loop_into_iter_non_const, code = "E0015")] +pub struct NonConstForLoopIntoIter<'tcx> { + #[primary_span] + pub span: Span, + pub ty: Ty<'tcx>, + pub kind: ConstContext, +} + +#[derive(Diagnostic)] +#[diag(const_eval_question_branch_non_const, code = "E0015")] +pub struct NonConstQuestionBranch<'tcx> { + #[primary_span] + pub span: Span, + pub ty: Ty<'tcx>, + pub kind: ConstContext, +} + +#[derive(Diagnostic)] +#[diag(const_eval_question_from_residual_non_const, code = "E0015")] +pub struct NonConstQuestionFromResidual<'tcx> { + #[primary_span] + pub span: Span, + pub ty: Ty<'tcx>, + pub kind: ConstContext, +} + +#[derive(Diagnostic)] +#[diag(const_eval_try_block_from_output_non_const, code = "E0015")] +pub struct NonConstTryBlockFromOutput<'tcx> { + #[primary_span] + pub span: Span, + pub ty: Ty<'tcx>, + pub kind: ConstContext, +} + +#[derive(Diagnostic)] +#[diag(const_eval_await_non_const, code = "E0015")] +pub struct NonConstAwait<'tcx> { + #[primary_span] + pub span: Span, + pub ty: Ty<'tcx>, + pub kind: ConstContext, +} + +#[derive(Diagnostic)] +#[diag(const_eval_closure_non_const, code = "E0015")] +pub struct NonConstClosure { + #[primary_span] + pub span: Span, + pub kind: ConstContext, + #[subdiagnostic] + pub note: Option<NonConstClosureNote>, +} + +#[derive(Subdiagnostic)] +pub enum NonConstClosureNote { + #[note(const_eval_closure_fndef_not_const)] + FnDef { + #[primary_span] + span: Span, + }, + #[note(const_eval_fn_ptr_call)] + FnPtr, + #[note(const_eval_closure_call)] + Closure, +} + +#[derive(Subdiagnostic)] +#[multipart_suggestion(const_eval_consider_dereferencing, applicability = "machine-applicable")] +pub struct ConsiderDereferencing { + pub deref: String, + #[suggestion_part(code = "{deref}")] + pub span: Span, + #[suggestion_part(code = "{deref}")] + pub rhs_span: Span, +} + +#[derive(Diagnostic)] +#[diag(const_eval_operator_non_const, code = "E0015")] +pub struct NonConstOperator { + #[primary_span] + pub span: Span, + pub kind: ConstContext, + #[subdiagnostic] + pub sugg: Option<ConsiderDereferencing>, +} + +#[derive(Diagnostic)] +#[diag(const_eval_deref_coercion_non_const, code = "E0015")] +#[note] +pub struct NonConstDerefCoercion<'tcx> { + #[primary_span] + pub span: Span, + pub ty: Ty<'tcx>, + pub kind: ConstContext, + pub target_ty: Ty<'tcx>, + #[note(const_eval_target_note)] + pub deref_target: Option<Span>, +} + +#[derive(Diagnostic)] +#[diag(const_eval_live_drop, code = "E0493")] +pub struct LiveDrop<'tcx> { + #[primary_span] + #[label] + pub span: Span, + pub kind: ConstContext, + pub dropped_ty: Ty<'tcx>, + #[label(const_eval_dropped_at_label)] + pub dropped_at: Option<Span>, +} + +#[derive(LintDiagnostic)] +#[diag(const_eval_align_check_failed)] +pub struct AlignmentCheckFailed { + pub has: u64, + pub required: u64, + #[subdiagnostic] + pub frames: Vec<FrameNote>, +} + +#[derive(Diagnostic)] +#[diag(const_eval_error, code = "E0080")] +pub struct ConstEvalError { + #[primary_span] + pub span: Span, + /// One of "const", "const_with_path", and "static" + pub error_kind: &'static str, + pub instance: String, + #[subdiagnostic] + pub frame_notes: Vec<FrameNote>, +} + +#[derive(Diagnostic)] +#[diag(const_eval_nullary_intrinsic_fail)] +pub struct NullaryIntrinsicError { + #[primary_span] + pub span: Span, +} + +#[derive(Diagnostic)] +#[diag(const_eval_undefined_behavior, code = "E0080")] +pub struct UndefinedBehavior { + #[primary_span] + pub span: Span, + #[note(const_eval_undefined_behavior_note)] + pub ub_note: Option<()>, + #[subdiagnostic] + pub frames: Vec<FrameNote>, + #[subdiagnostic] + pub raw_bytes: RawBytesNote, +} + +pub trait ReportErrorExt { + /// Returns the diagnostic message for this error. + fn diagnostic_message(&self) -> DiagnosticMessage; + fn add_args<G: EmissionGuarantee>( + self, + handler: &Handler, + builder: &mut DiagnosticBuilder<'_, G>, + ); + + fn debug(self) -> String + where + Self: Sized, + { + ty::tls::with(move |tcx| { + let mut builder = tcx.sess.struct_allow(DiagnosticMessage::Str(String::new().into())); + let handler = &tcx.sess.parse_sess.span_diagnostic; + let message = self.diagnostic_message(); + self.add_args(handler, &mut builder); + let s = handler.eagerly_translate_to_string(message, builder.args()); + builder.cancel(); + s + }) + } +} + +fn bad_pointer_message(msg: CheckInAllocMsg, handler: &Handler) -> String { + use crate::fluent_generated::*; + + let msg = match msg { + CheckInAllocMsg::DerefTest => const_eval_deref_test, + CheckInAllocMsg::MemoryAccessTest => const_eval_memory_access_test, + CheckInAllocMsg::PointerArithmeticTest => const_eval_pointer_arithmetic_test, + CheckInAllocMsg::OffsetFromTest => const_eval_offset_from_test, + CheckInAllocMsg::InboundsTest => const_eval_in_bounds_test, + }; + + handler.eagerly_translate_to_string(msg, [].into_iter()) +} + +impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> { + fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; + use UndefinedBehaviorInfo::*; + match self { + Ub(msg) => msg.clone().into(), + Unreachable => const_eval_unreachable, + BoundsCheckFailed { .. } => const_eval_bounds_check_failed, + DivisionByZero => const_eval_division_by_zero, + RemainderByZero => const_eval_remainder_by_zero, + DivisionOverflow => const_eval_division_overflow, + RemainderOverflow => const_eval_remainder_overflow, + PointerArithOverflow => const_eval_pointer_arithmetic_overflow, + InvalidMeta(InvalidMetaKind::SliceTooBig) => const_eval_invalid_meta_slice, + InvalidMeta(InvalidMetaKind::TooBig) => const_eval_invalid_meta, + UnterminatedCString(_) => const_eval_unterminated_c_string, + PointerUseAfterFree(_) => const_eval_pointer_use_after_free, + PointerOutOfBounds { ptr_size: Size::ZERO, .. } => const_eval_zst_pointer_out_of_bounds, + PointerOutOfBounds { .. } => const_eval_pointer_out_of_bounds, + DanglingIntPointer(0, _) => const_eval_dangling_null_pointer, + DanglingIntPointer(_, _) => const_eval_dangling_int_pointer, + AlignmentCheckFailed { .. } => const_eval_alignment_check_failed, + WriteToReadOnly(_) => const_eval_write_to_read_only, + DerefFunctionPointer(_) => const_eval_deref_function_pointer, + DerefVTablePointer(_) => const_eval_deref_vtable_pointer, + InvalidBool(_) => const_eval_invalid_bool, + InvalidChar(_) => const_eval_invalid_char, + InvalidTag(_) => const_eval_invalid_tag, + InvalidFunctionPointer(_) => const_eval_invalid_function_pointer, + InvalidVTablePointer(_) => const_eval_invalid_vtable_pointer, + InvalidStr(_) => const_eval_invalid_str, + InvalidUninitBytes(None) => const_eval_invalid_uninit_bytes_unknown, + InvalidUninitBytes(Some(_)) => const_eval_invalid_uninit_bytes, + DeadLocal => const_eval_dead_local, + ScalarSizeMismatch(_) => const_eval_scalar_size_mismatch, + UninhabitedEnumVariantWritten => const_eval_uninhabited_enum_variant_written, + Validation(e) => e.diagnostic_message(), + Custom(x) => (x.msg)(), + } + } + + fn add_args<G: EmissionGuarantee>( + self, + handler: &Handler, + builder: &mut DiagnosticBuilder<'_, G>, + ) { + use UndefinedBehaviorInfo::*; + match self { + Ub(_) + | Unreachable + | DivisionByZero + | RemainderByZero + | DivisionOverflow + | RemainderOverflow + | PointerArithOverflow + | InvalidMeta(InvalidMetaKind::SliceTooBig) + | InvalidMeta(InvalidMetaKind::TooBig) + | InvalidUninitBytes(None) + | DeadLocal + | UninhabitedEnumVariantWritten => {} + BoundsCheckFailed { len, index } => { + builder.set_arg("len", len); + builder.set_arg("index", index); + } + UnterminatedCString(ptr) | InvalidFunctionPointer(ptr) | InvalidVTablePointer(ptr) => { + builder.set_arg("pointer", ptr); + } + PointerUseAfterFree(allocation) => { + builder.set_arg("allocation", allocation); + } + PointerOutOfBounds { alloc_id, alloc_size, ptr_offset, ptr_size, msg } => { + builder + .set_arg("alloc_id", alloc_id) + .set_arg("alloc_size", alloc_size.bytes()) + .set_arg("ptr_offset", ptr_offset) + .set_arg("ptr_size", ptr_size.bytes()) + .set_arg("bad_pointer_message", bad_pointer_message(msg, handler)); + } + DanglingIntPointer(ptr, msg) => { + if ptr != 0 { + builder.set_arg("pointer", format!("{ptr:#x}[noalloc]")); + } + + builder.set_arg("bad_pointer_message", bad_pointer_message(msg, handler)); + } + AlignmentCheckFailed { required, has } => { + builder.set_arg("required", required.bytes()); + builder.set_arg("has", has.bytes()); + } + WriteToReadOnly(alloc) | DerefFunctionPointer(alloc) | DerefVTablePointer(alloc) => { + builder.set_arg("allocation", alloc); + } + InvalidBool(b) => { + builder.set_arg("value", format!("{b:02x}")); + } + InvalidChar(c) => { + builder.set_arg("value", format!("{c:08x}")); + } + InvalidTag(tag) => { + builder.set_arg("tag", format!("{tag:x}")); + } + InvalidStr(err) => { + builder.set_arg("err", format!("{err}")); + } + InvalidUninitBytes(Some((alloc, info))) => { + builder.set_arg("alloc", alloc); + builder.set_arg("access", info.access); + builder.set_arg("uninit", info.uninit); + } + ScalarSizeMismatch(info) => { + builder.set_arg("target_size", info.target_size); + builder.set_arg("data_size", info.data_size); + } + Validation(e) => e.add_args(handler, builder), + Custom(custom) => { + (custom.add_args)(&mut |name, value| { + builder.set_arg(name, value); + }); + } + } + } +} + +impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { + fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; + use rustc_middle::mir::interpret::ValidationErrorKind::*; + match self.kind { + PtrToUninhabited { ptr_kind: PointerKind::Box, .. } => const_eval_box_to_uninhabited, + PtrToUninhabited { ptr_kind: PointerKind::Ref, .. } => const_eval_ref_to_uninhabited, + + PtrToStatic { ptr_kind: PointerKind::Box } => const_eval_box_to_static, + PtrToStatic { ptr_kind: PointerKind::Ref } => const_eval_ref_to_static, + + PtrToMut { ptr_kind: PointerKind::Box } => const_eval_box_to_mut, + PtrToMut { ptr_kind: PointerKind::Ref } => const_eval_ref_to_mut, + + ExpectedNonPtr { .. } => const_eval_expected_non_ptr, + MutableRefInConst => const_eval_mutable_ref_in_const, + NullFnPtr => const_eval_null_fn_ptr, + NeverVal => const_eval_never_val, + NullablePtrOutOfRange { .. } => const_eval_nullable_ptr_out_of_range, + PtrOutOfRange { .. } => const_eval_ptr_out_of_range, + OutOfRange { .. } => const_eval_out_of_range, + UnsafeCell => const_eval_unsafe_cell, + UninhabitedVal { .. } => const_eval_uninhabited_val, + InvalidEnumTag { .. } => const_eval_invalid_enum_tag, + UninitEnumTag => const_eval_uninit_enum_tag, + UninitStr => const_eval_uninit_str, + Uninit { expected: ExpectedKind::Bool } => const_eval_uninit_bool, + Uninit { expected: ExpectedKind::Reference } => const_eval_uninit_ref, + Uninit { expected: ExpectedKind::Box } => const_eval_uninit_box, + Uninit { expected: ExpectedKind::RawPtr } => const_eval_uninit_raw_ptr, + Uninit { expected: ExpectedKind::InitScalar } => const_eval_uninit_init_scalar, + Uninit { expected: ExpectedKind::Char } => const_eval_uninit_char, + Uninit { expected: ExpectedKind::Float } => const_eval_uninit_float, + Uninit { expected: ExpectedKind::Int } => const_eval_uninit_int, + Uninit { expected: ExpectedKind::FnPtr } => const_eval_uninit_fn_ptr, + UninitVal => const_eval_uninit, + InvalidVTablePtr { .. } => const_eval_invalid_vtable_ptr, + InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Box } => { + const_eval_invalid_box_slice_meta + } + InvalidMetaSliceTooLarge { ptr_kind: PointerKind::Ref } => { + const_eval_invalid_ref_slice_meta + } + + InvalidMetaTooLarge { ptr_kind: PointerKind::Box } => const_eval_invalid_box_meta, + InvalidMetaTooLarge { ptr_kind: PointerKind::Ref } => const_eval_invalid_ref_meta, + UnalignedPtr { ptr_kind: PointerKind::Ref, .. } => const_eval_unaligned_ref, + UnalignedPtr { ptr_kind: PointerKind::Box, .. } => const_eval_unaligned_box, + + NullPtr { ptr_kind: PointerKind::Box } => const_eval_null_box, + NullPtr { ptr_kind: PointerKind::Ref } => const_eval_null_ref, + DanglingPtrNoProvenance { ptr_kind: PointerKind::Box, .. } => { + const_eval_dangling_box_no_provenance + } + DanglingPtrNoProvenance { ptr_kind: PointerKind::Ref, .. } => { + const_eval_dangling_ref_no_provenance + } + DanglingPtrOutOfBounds { ptr_kind: PointerKind::Box } => { + const_eval_dangling_box_out_of_bounds + } + DanglingPtrOutOfBounds { ptr_kind: PointerKind::Ref } => { + const_eval_dangling_ref_out_of_bounds + } + DanglingPtrUseAfterFree { ptr_kind: PointerKind::Box } => { + const_eval_dangling_box_use_after_free + } + DanglingPtrUseAfterFree { ptr_kind: PointerKind::Ref } => { + const_eval_dangling_ref_use_after_free + } + InvalidBool { .. } => const_eval_validation_invalid_bool, + InvalidChar { .. } => const_eval_validation_invalid_char, + InvalidFnPtr { .. } => const_eval_invalid_fn_ptr, + } + } + + fn add_args<G: EmissionGuarantee>(self, handler: &Handler, err: &mut DiagnosticBuilder<'_, G>) { + use crate::fluent_generated as fluent; + use rustc_middle::mir::interpret::ValidationErrorKind::*; + + let message = if let Some(path) = self.path { + handler.eagerly_translate_to_string( + fluent::const_eval_invalid_value_with_path, + [("path".into(), DiagnosticArgValue::Str(path.into()))].iter().map(|(a, b)| (a, b)), + ) + } else { + handler.eagerly_translate_to_string(fluent::const_eval_invalid_value, [].into_iter()) + }; + + err.set_arg("front_matter", message); + + fn add_range_arg<G: EmissionGuarantee>( + r: WrappingRange, + max_hi: u128, + handler: &Handler, + err: &mut DiagnosticBuilder<'_, G>, + ) { + let WrappingRange { start: lo, end: hi } = r; + assert!(hi <= max_hi); + let msg = if lo > hi { + fluent::const_eval_range_wrapping + } else if lo == hi { + fluent::const_eval_range_singular + } else if lo == 0 { + assert!(hi < max_hi, "should not be printing if the range covers everything"); + fluent::const_eval_range_upper + } else if hi == max_hi { + assert!(lo > 0, "should not be printing if the range covers everything"); + fluent::const_eval_range_lower + } else { + fluent::const_eval_range + }; + + let args = [ + ("lo".into(), DiagnosticArgValue::Str(lo.to_string().into())), + ("hi".into(), DiagnosticArgValue::Str(hi.to_string().into())), + ]; + let args = args.iter().map(|(a, b)| (a, b)); + let message = handler.eagerly_translate_to_string(msg, args); + err.set_arg("in_range", message); + } + + match self.kind { + PtrToUninhabited { ty, .. } | UninhabitedVal { ty } => { + err.set_arg("ty", ty); + } + ExpectedNonPtr { value } + | InvalidEnumTag { value } + | InvalidVTablePtr { value } + | InvalidBool { value } + | InvalidChar { value } + | InvalidFnPtr { value } => { + err.set_arg("value", value); + } + NullablePtrOutOfRange { range, max_value } | PtrOutOfRange { range, max_value } => { + add_range_arg(range, max_value, handler, err) + } + OutOfRange { range, max_value, value } => { + err.set_arg("value", value); + add_range_arg(range, max_value, handler, err); + } + UnalignedPtr { required_bytes, found_bytes, .. } => { + err.set_arg("required_bytes", required_bytes); + err.set_arg("found_bytes", found_bytes); + } + DanglingPtrNoProvenance { pointer, .. } => { + err.set_arg("pointer", pointer); + } + NullPtr { .. } + | PtrToStatic { .. } + | PtrToMut { .. } + | MutableRefInConst + | NullFnPtr + | NeverVal + | UnsafeCell + | UninitEnumTag + | UninitStr + | Uninit { .. } + | UninitVal + | InvalidMetaSliceTooLarge { .. } + | InvalidMetaTooLarge { .. } + | DanglingPtrUseAfterFree { .. } + | DanglingPtrOutOfBounds { .. } => {} + } + } +} + +impl ReportErrorExt for UnsupportedOpInfo { + fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; + match self { + UnsupportedOpInfo::Unsupported(s) => s.clone().into(), + UnsupportedOpInfo::PartialPointerOverwrite(_) => const_eval_partial_pointer_overwrite, + UnsupportedOpInfo::PartialPointerCopy(_) => const_eval_partial_pointer_copy, + UnsupportedOpInfo::ReadPointerAsBytes => const_eval_read_pointer_as_bytes, + UnsupportedOpInfo::ThreadLocalStatic(_) => const_eval_thread_local_static, + UnsupportedOpInfo::ReadExternStatic(_) => const_eval_read_extern_static, + } + } + fn add_args<G: EmissionGuarantee>(self, _: &Handler, builder: &mut DiagnosticBuilder<'_, G>) { + use crate::fluent_generated::*; + + use UnsupportedOpInfo::*; + if let ReadPointerAsBytes | PartialPointerOverwrite(_) | PartialPointerCopy(_) = self { + builder.help(const_eval_ptr_as_bytes_1); + builder.help(const_eval_ptr_as_bytes_2); + } + match self { + Unsupported(_) | ReadPointerAsBytes => {} + PartialPointerOverwrite(ptr) | PartialPointerCopy(ptr) => { + builder.set_arg("ptr", ptr); + } + ThreadLocalStatic(did) | ReadExternStatic(did) => { + builder.set_arg("did", format!("{did:?}")); + } + } + } +} + +impl<'tcx> ReportErrorExt for InterpError<'tcx> { + fn diagnostic_message(&self) -> DiagnosticMessage { + match self { + InterpError::UndefinedBehavior(ub) => ub.diagnostic_message(), + InterpError::Unsupported(e) => e.diagnostic_message(), + InterpError::InvalidProgram(e) => e.diagnostic_message(), + InterpError::ResourceExhaustion(e) => e.diagnostic_message(), + InterpError::MachineStop(e) => e.diagnostic_message(), + } + } + fn add_args<G: EmissionGuarantee>( + self, + handler: &Handler, + builder: &mut DiagnosticBuilder<'_, G>, + ) { + match self { + InterpError::UndefinedBehavior(ub) => ub.add_args(handler, builder), + InterpError::Unsupported(e) => e.add_args(handler, builder), + InterpError::InvalidProgram(e) => e.add_args(handler, builder), + InterpError::ResourceExhaustion(e) => e.add_args(handler, builder), + InterpError::MachineStop(e) => e.add_args(&mut |name, value| { + builder.set_arg(name, value); + }), + } + } +} + +impl<'tcx> ReportErrorExt for InvalidProgramInfo<'tcx> { + fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; + match self { + InvalidProgramInfo::TooGeneric => const_eval_too_generic, + InvalidProgramInfo::AlreadyReported(_) => const_eval_already_reported, + InvalidProgramInfo::Layout(e) => e.diagnostic_message(), + InvalidProgramInfo::FnAbiAdjustForForeignAbi(_) => { + rustc_middle::error::middle_adjust_for_foreign_abi_error + } + InvalidProgramInfo::SizeOfUnsizedType(_) => const_eval_size_of_unsized, + InvalidProgramInfo::UninitUnsizedLocal => const_eval_uninit_unsized_local, + } + } + fn add_args<G: EmissionGuarantee>( + self, + handler: &Handler, + builder: &mut DiagnosticBuilder<'_, G>, + ) { + match self { + InvalidProgramInfo::TooGeneric + | InvalidProgramInfo::AlreadyReported(_) + | InvalidProgramInfo::UninitUnsizedLocal => {} + InvalidProgramInfo::Layout(e) => { + let diag: DiagnosticBuilder<'_, ()> = e.into_diagnostic().into_diagnostic(handler); + for (name, val) in diag.args() { + builder.set_arg(name.clone(), val.clone()); + } + diag.cancel(); + } + InvalidProgramInfo::FnAbiAdjustForForeignAbi( + AdjustForForeignAbiError::Unsupported { arch, abi }, + ) => { + builder.set_arg("arch", arch); + builder.set_arg("abi", abi.name()); + } + InvalidProgramInfo::SizeOfUnsizedType(ty) => { + builder.set_arg("ty", ty); + } + } + } +} + +impl ReportErrorExt for ResourceExhaustionInfo { + fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; + match self { + ResourceExhaustionInfo::StackFrameLimitReached => const_eval_stack_frame_limit_reached, + ResourceExhaustionInfo::MemoryExhausted => const_eval_memory_exhausted, + ResourceExhaustionInfo::AddressSpaceFull => const_eval_address_space_full, + } + } + fn add_args<G: EmissionGuarantee>(self, _: &Handler, _: &mut DiagnosticBuilder<'_, G>) {} +} diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs index 163e3f86993..b4db3dff3ff 100644 --- a/compiler/rustc_const_eval/src/interpret/cast.rs +++ b/compiler/rustc_const_eval/src/interpret/cast.rs @@ -14,6 +14,8 @@ use super::{ util::ensure_monomorphic_enough, FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy, }; +use crate::fluent_generated as fluent; + impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { pub fn cast( &mut self, @@ -138,12 +140,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { assert!(src.layout.is_sized()); assert!(dest.layout.is_sized()); if src.layout.size != dest.layout.size { - throw_ub_format!( - "transmuting from {}-byte type to {}-byte type: `{}` -> `{}`", - src.layout.size.bytes(), - dest.layout.size.bytes(), - src.layout.ty, - dest.layout.ty, + let src_bytes = src.layout.size.bytes(); + let dest_bytes = dest.layout.size.bytes(); + let src_ty = format!("{}", src.layout.ty); + let dest_ty = format!("{}", dest.layout.ty); + throw_ub_custom!( + fluent::const_eval_invalid_transmute, + src_bytes = src_bytes, + dest_bytes = dest_bytes, + src = src_ty, + dest = dest_ty, ); } @@ -363,7 +369,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let old_vptr = old_vptr.to_pointer(self)?; let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?; if old_trait != data_a.principal() { - throw_ub_format!("upcast on a pointer whose vtable does not match its type"); + throw_ub_custom!(fluent::const_eval_upcast_mismatch); } let new_vptr = self.get_vtable_ptr(ty, data_b.principal())?; self.write_immediate(Immediate::new_dyn_trait(old_data, new_vptr, self), dest) diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index cd43a53b1b3..36606ff69a6 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -1,6 +1,5 @@ use std::cell::Cell; -use std::fmt; -use std::mem; +use std::{fmt, mem}; use either::{Either, Left, Right}; @@ -8,7 +7,7 @@ use hir::CRATE_HIR_ID; use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData}; use rustc_index::IndexVec; use rustc_middle::mir; -use rustc_middle::mir::interpret::{ErrorHandled, InterpError, ReportedErrorInfo}; +use rustc_middle::mir::interpret::{ErrorHandled, InterpError, InvalidMetaKind, ReportedErrorInfo}; use rustc_middle::query::TyCtxtAt; use rustc_middle::ty::layout::{ self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers, @@ -25,6 +24,8 @@ use super::{ MemPlaceMeta, Memory, MemoryKind, Operand, Place, PlaceTy, PointerArithmetic, Provenance, Scalar, StackPopJump, }; +use crate::errors::{self, ErroneousConstUsed}; +use crate::fluent_generated as fluent; use crate::util; pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> { @@ -247,6 +248,7 @@ impl<'mir, 'tcx, Prov: Provenance, Extra> Frame<'mir, 'tcx, Prov, Extra> { } } +// FIXME: only used by miri, should be removed once translatable. impl<'tcx> fmt::Display for FrameInfo<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ty::tls::with(|tcx| { @@ -264,6 +266,21 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> { } } +impl<'tcx> FrameInfo<'tcx> { + pub fn as_note(&self, tcx: TyCtxt<'tcx>) -> errors::FrameNote { + let span = self.span; + if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::ClosureExpr { + errors::FrameNote { where_: "closure", span, instance: String::new(), times: 0 } + } else { + let instance = format!("{}", self.instance); + // Note: this triggers a `good_path_bug` state, which means that if we ever get here + // we must emit a diagnostic. We should never display a `FrameInfo` unless we + // actually want to emit a warning or error to the user. + errors::FrameNote { where_: "instance", span, instance, times: 0 } + } + } +} + impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for InterpCx<'mir, 'tcx, M> { #[inline] fn data_layout(&self) -> &TargetDataLayout { @@ -620,7 +637,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // Check if this brought us over the size limit. if size > self.max_size_of_val() { - throw_ub!(InvalidMeta("total size is bigger than largest supported object")); + throw_ub!(InvalidMeta(InvalidMetaKind::TooBig)); } Ok(Some((size, align))) } @@ -638,7 +655,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let size = elem.size.bytes().saturating_mul(len); // we rely on `max_size_of_val` being smaller than `u64::MAX`. let size = Size::from_bytes(size); if size > self.max_size_of_val() { - throw_ub!(InvalidMeta("slice is bigger than largest supported object")); + throw_ub!(InvalidMeta(InvalidMetaKind::SliceTooBig)); } Ok(Some((size, elem.align.abi))) } @@ -746,7 +763,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { mir::UnwindAction::Cleanup(block) => Left(mir::Location { block, statement_index: 0 }), mir::UnwindAction::Continue => Right(self.frame_mut().body.span), mir::UnwindAction::Unreachable => { - throw_ub_format!("unwinding past a stack frame that does not allow unwinding") + throw_ub_custom!(fluent::const_eval_unreachable_unwind); } mir::UnwindAction::Terminate => { self.frame_mut().loc = Right(self.frame_mut().body.span); @@ -785,7 +802,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } ); if unwinding && self.frame_idx() == 0 { - throw_ub_format!("unwinding past the topmost frame of the stack"); + throw_ub_custom!(fluent::const_eval_unwind_past_top); } // Copy return value. Must of course happen *before* we deallocate the locals. @@ -873,7 +890,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // StorageLive expects the local to be dead, and marks it live. let old = mem::replace(&mut self.frame_mut().locals[local].value, local_val); if !matches!(old, LocalValue::Dead) { - throw_ub_format!("StorageLive on a local that was already live"); + throw_ub_custom!(fluent::const_eval_double_storage_live); } Ok(()) } @@ -916,7 +933,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ErrorHandled::Reported(err) => { if !err.is_tainted_by_errors() && let Some(span) = span { // To make it easier to figure out where this error comes from, also add a note at the current location. - self.tcx.sess.span_note_without_error(span, "erroneous constant used"); + self.tcx.sess.emit_note(ErroneousConstUsed { span }); } err_inval!(AlreadyReported(err)) } diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index c2b82ba9b07..7b11ad33091 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -28,6 +28,7 @@ use super::{ ValueVisitor, }; use crate::const_eval; +use crate::errors::{DanglingPtrInFinal, UnsupportedUntypedPointer}; pub trait CompileTimeMachine<'mir, 'tcx, T> = Machine< 'mir, @@ -320,10 +321,12 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory } } +/// How a constant value should be interned. #[derive(Copy, Clone, Debug, PartialEq, Hash, Eq)] pub enum InternKind { /// The `mutability` of the static, ignoring the type which may have interior mutability. Static(hir::Mutability), + /// A `const` item Constant, Promoted, } @@ -388,8 +391,7 @@ pub fn intern_const_alloc_recursive< ecx.tcx.sess.delay_span_bug( ecx.tcx.span, format!( - "error during interning should later cause validation failure: {}", - error + "error during interning should later cause validation failure: {error:?}" ), ); } @@ -425,14 +427,16 @@ pub fn intern_const_alloc_recursive< // immutability is so important. alloc.mutability = Mutability::Not; } + // If it's a constant, we should not have any "leftovers" as everything + // is tracked by const-checking. + // FIXME: downgrade this to a warning? It rejects some legitimate consts, + // such as `const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;`. + // + // NOTE: it looks likes this code path is only reachable when we try to intern + // something that cannot be promoted, which in constants means values that have + // drop glue, such as the example above. InternKind::Constant => { - // If it's a constant, we should not have any "leftovers" as everything - // is tracked by const-checking. - // FIXME: downgrade this to a warning? It rejects some legitimate consts, - // such as `const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _;`. - ecx.tcx - .sess - .span_err(ecx.tcx.span, "untyped pointers are not allowed in constant"); + ecx.tcx.sess.emit_err(UnsupportedUntypedPointer { span: ecx.tcx.span }); // For better errors later, mark the allocation as immutable. alloc.mutability = Mutability::Not; } @@ -447,10 +451,7 @@ pub fn intern_const_alloc_recursive< } else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) { // Codegen does not like dangling pointers, and generally `tcx` assumes that // all allocations referenced anywhere actually exist. So, make sure we error here. - let reported = ecx - .tcx - .sess - .span_err(ecx.tcx.span, "encountered dangling pointer in final constant"); + let reported = ecx.tcx.sess.emit_err(DanglingPtrInFinal { span: ecx.tcx.span }); return Err(reported); } else if ecx.tcx.try_get_global_alloc(alloc_id).is_none() { // We have hit an `AllocId` that is neither in local or global memory and isn't diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index a77c699c22f..fffb9a7f264 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -22,6 +22,8 @@ use super::{ Pointer, }; +use crate::fluent_generated as fluent; + mod caller_location; fn numeric_intrinsic<Prov>(name: Symbol, bits: u128, kind: Primitive) -> Scalar<Prov> { @@ -198,15 +200,18 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ty ), }; - let (nonzero, intrinsic_name) = match intrinsic_name { + let (nonzero, actual_intrinsic_name) = match intrinsic_name { sym::cttz_nonzero => (true, sym::cttz), sym::ctlz_nonzero => (true, sym::ctlz), other => (false, other), }; if nonzero && bits == 0 { - throw_ub_format!("`{}_nonzero` called on 0", intrinsic_name); + throw_ub_custom!( + fluent::const_eval_call_nonzero_intrinsic, + name = intrinsic_name, + ); } - let out_val = numeric_intrinsic(intrinsic_name, bits, kind); + let out_val = numeric_intrinsic(actual_intrinsic_name, bits, kind); self.write_scalar(out_val, dest)?; } sym::saturating_add | sym::saturating_sub => { @@ -233,9 +238,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | sym::unchecked_shr | sym::unchecked_add | sym::unchecked_sub - | sym::unchecked_mul - | sym::unchecked_div - | sym::unchecked_rem => { + | sym::unchecked_mul => { let l = self.read_immediate(&args[0])?; let r = self.read_immediate(&args[1])?; let bin_op = match intrinsic_name { @@ -244,8 +247,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { sym::unchecked_add => BinOp::Add, sym::unchecked_sub => BinOp::Sub, sym::unchecked_mul => BinOp::Mul, - sym::unchecked_div => BinOp::Div, - sym::unchecked_rem => BinOp::Rem, _ => bug!(), }; let (val, overflowed, _ty) = self.overflowing_binary_op(bin_op, &l, &r)?; @@ -253,9 +254,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let layout = self.layout_of(substs.type_at(0))?; let r_val = r.to_scalar().to_bits(layout.size)?; if let sym::unchecked_shl | sym::unchecked_shr = intrinsic_name { - throw_ub_format!("overflowing shift by {} in `{}`", r_val, intrinsic_name); + throw_ub_custom!( + fluent::const_eval_overflow_shift, + val = r_val, + name = intrinsic_name + ); } else { - throw_ub_format!("overflow executing `{}`", intrinsic_name); + throw_ub_custom!(fluent::const_eval_overflow, name = intrinsic_name); } } self.write_scalar(val, dest)?; @@ -314,17 +319,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { (Err(_), _) | (_, Err(_)) => { // We managed to find a valid allocation for one pointer, but not the other. // That means they are definitely not pointing to the same allocation. - throw_ub_format!( - "`{}` called on pointers into different allocations", - intrinsic_name + throw_ub_custom!( + fluent::const_eval_different_allocations, + name = intrinsic_name, ); } (Ok((a_alloc_id, a_offset, _)), Ok((b_alloc_id, b_offset, _))) => { // Found allocation for both. They must be into the same allocation. if a_alloc_id != b_alloc_id { - throw_ub_format!( - "`{}` called on pointers into different allocations", - intrinsic_name + throw_ub_custom!( + fluent::const_eval_different_allocations, + name = intrinsic_name, ); } // Use these offsets for distance calculation. @@ -344,11 +349,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { if overflowed { // a < b if intrinsic_name == sym::ptr_offset_from_unsigned { - throw_ub_format!( - "`{}` called when first pointer has smaller offset than second: {} < {}", - intrinsic_name, - a_offset, - b_offset, + throw_ub_custom!( + fluent::const_eval_unsigned_offset_from_overflow, + a_offset = a_offset, + b_offset = b_offset, ); } // The signed form of the intrinsic allows this. If we interpret the @@ -356,9 +360,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // seems *positive*, they were more than isize::MAX apart. let dist = val.to_target_isize(self)?; if dist >= 0 { - throw_ub_format!( - "`{}` called when first pointer is too far before second", - intrinsic_name + throw_ub_custom!( + fluent::const_eval_offset_from_underflow, + name = intrinsic_name, ); } dist @@ -368,9 +372,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // If converting to isize produced a *negative* result, we had an overflow // because they were more than isize::MAX apart. if dist < 0 { - throw_ub_format!( - "`{}` called when first pointer is too far ahead of second", - intrinsic_name + throw_ub_custom!( + fluent::const_eval_offset_from_overflow, + name = intrinsic_name, ); } dist @@ -513,7 +517,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let op = self.eval_operand(op, None)?; let cond = self.read_scalar(&op)?.to_bool()?; if !cond { - throw_ub_format!("`assume` called with `false`"); + throw_ub_custom!(fluent::const_eval_assume_false); } Ok(()) } @@ -542,7 +546,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, &a, &b)?; assert!(!overflow); // All overflow is UB, so this should never return on overflow. if res.assert_bits(a.layout.size) != 0 { - throw_ub_format!("exact_div: {} cannot be divided by {} without remainder", a, b) + throw_ub_custom!( + fluent::const_eval_exact_div_has_remainder, + a = format!("{a}"), + b = format!("{b}") + ) } // `Rem` says this is all right, so we can let `Div` do its job. self.binop_ignore_overflow(BinOp::Div, &a, &b, dest) @@ -638,9 +646,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // `checked_mul` enforces a too small bound (the correct one would probably be target_isize_max), // but no actual allocation can be big enough for the difference to be noticeable. let size = size.checked_mul(count, self).ok_or_else(|| { - err_ub_format!( - "overflow computing total size of `{}`", - if nonoverlapping { "copy_nonoverlapping" } else { "copy" } + err_ub_custom!( + fluent::const_eval_size_overflow, + name = if nonoverlapping { "copy_nonoverlapping" } else { "copy" } ) })?; @@ -664,10 +672,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // `checked_mul` enforces a too small bound (the correct one would probably be target_isize_max), // but no actual allocation can be big enough for the difference to be noticeable. - let len = layout - .size - .checked_mul(count, self) - .ok_or_else(|| err_ub_format!("overflow computing total size of `write_bytes`"))?; + let len = layout.size.checked_mul(count, self).ok_or_else(|| { + err_ub_custom!(fluent::const_eval_size_overflow, name = "write_bytes") + })?; let bytes = std::iter::repeat(byte).take(len.bytes_usize()); self.write_bytes_ptr(dst, bytes) @@ -691,7 +698,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { return Ok(&[]); }; if alloc_ref.has_provenance() { - throw_ub_format!("`raw_eq` on bytes with provenance"); + throw_ub_custom!(fluent::const_eval_raw_eq_with_provenance); } alloc_ref.get_bytes_strip_provenance() }; diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index d5b6a581a79..1125d8d1f0e 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -19,6 +19,7 @@ use rustc_middle::ty::{self, Instance, ParamEnv, Ty, TyCtxt}; use rustc_target::abi::{Align, HasDataLayout, Size}; use crate::const_eval::CheckAlignment; +use crate::fluent_generated as fluent; use super::{ alloc_range, AllocBytes, AllocId, AllocMap, AllocRange, Allocation, CheckInAllocMsg, @@ -200,7 +201,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { align: Align, kind: MemoryKind<M::MemoryKind>, ) -> InterpResult<'tcx, Pointer<M::Provenance>> { - let alloc = Allocation::uninit(size, align, M::PANIC_ON_ALLOC_FAIL)?; + let alloc = if M::PANIC_ON_ALLOC_FAIL { + Allocation::uninit(size, align) + } else { + Allocation::try_uninit(size, align)? + }; self.allocate_raw_ptr(alloc, kind) } @@ -242,9 +247,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ) -> InterpResult<'tcx, Pointer<M::Provenance>> { let (alloc_id, offset, _prov) = self.ptr_get_alloc_id(ptr)?; if offset.bytes() != 0 { - throw_ub_format!( - "reallocating {:?} which does not point to the beginning of an object", - ptr + throw_ub_custom!( + fluent::const_eval_realloc_or_alloc_with_offset, + ptr = format!("{ptr:?}"), + kind = "realloc" ); } @@ -280,9 +286,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { trace!("deallocating: {alloc_id:?}"); if offset.bytes() != 0 { - throw_ub_format!( - "deallocating {:?} which does not point to the beginning of an object", - ptr + throw_ub_custom!( + fluent::const_eval_realloc_or_alloc_with_offset, + ptr = format!("{ptr:?}"), + kind = "dealloc", ); } @@ -290,13 +297,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // Deallocating global memory -- always an error return Err(match self.tcx.try_get_global_alloc(alloc_id) { Some(GlobalAlloc::Function(..)) => { - err_ub_format!("deallocating {alloc_id:?}, which is a function") + err_ub_custom!( + fluent::const_eval_invalid_dealloc, + alloc_id = alloc_id, + kind = "fn", + ) } Some(GlobalAlloc::VTable(..)) => { - err_ub_format!("deallocating {alloc_id:?}, which is a vtable") + err_ub_custom!( + fluent::const_eval_invalid_dealloc, + alloc_id = alloc_id, + kind = "vtable", + ) } Some(GlobalAlloc::Static(..) | GlobalAlloc::Memory(..)) => { - err_ub_format!("deallocating {alloc_id:?}, which is static memory") + err_ub_custom!( + fluent::const_eval_invalid_dealloc, + alloc_id = alloc_id, + kind = "static_mem" + ) } None => err_ub!(PointerUseAfterFree(alloc_id)), } @@ -304,21 +323,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { }; if alloc.mutability.is_not() { - throw_ub_format!("deallocating immutable allocation {alloc_id:?}"); + throw_ub_custom!(fluent::const_eval_dealloc_immutable, alloc = alloc_id,); } if alloc_kind != kind { - throw_ub_format!( - "deallocating {alloc_id:?}, which is {alloc_kind} memory, using {kind} deallocation operation" + throw_ub_custom!( + fluent::const_eval_dealloc_kind_mismatch, + alloc = alloc_id, + alloc_kind = format!("{alloc_kind}"), + kind = format!("{kind}"), ); } if let Some((size, align)) = old_size_and_align { if size != alloc.size() || align != alloc.align { - throw_ub_format!( - "incorrect layout on deallocation: {alloc_id:?} has size {} and alignment {}, but gave size {} and alignment {}", - alloc.size().bytes(), - alloc.align.bytes(), - size.bytes(), - align.bytes(), + throw_ub_custom!( + fluent::const_eval_dealloc_incorrect_layout, + alloc = alloc_id, + size = alloc.size().bytes(), + align = alloc.align.bytes(), + size_found = size.bytes(), + align_found = align.bytes(), ) } } @@ -1166,7 +1189,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { if (src_offset <= dest_offset && src_offset + size > dest_offset) || (dest_offset <= src_offset && dest_offset + size > src_offset) { - throw_ub_format!("copy_nonoverlapping called on overlapping ranges") + throw_ub_custom!(fluent::const_eval_copy_nonoverlapping_overlapping); } } diff --git a/compiler/rustc_const_eval/src/interpret/terminator.rs b/compiler/rustc_const_eval/src/interpret/terminator.rs index 586e8f063ee..7269ff8d53c 100644 --- a/compiler/rustc_const_eval/src/interpret/terminator.rs +++ b/compiler/rustc_const_eval/src/interpret/terminator.rs @@ -15,6 +15,7 @@ use super::{ FnVal, ImmTy, Immediate, InterpCx, InterpResult, MPlaceTy, Machine, MemoryKind, OpTy, Operand, PlaceTy, Scalar, StackPopCleanup, }; +use crate::fluent_generated as fluent; impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { pub(super) fn eval_terminator( @@ -172,7 +173,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { InlineAsm { template, ref operands, options, destination, .. } => { M::eval_inline_asm(self, template, operands, options)?; if options.contains(InlineAsmOptions::NORETURN) { - throw_ub_format!("returned from noreturn inline assembly"); + throw_ub_custom!(fluent::const_eval_noreturn_asm_returned); } self.go_to_block( destination @@ -288,15 +289,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { return Ok(()); } // Find next caller arg. - let (caller_arg, caller_abi) = caller_args.next().ok_or_else(|| { - err_ub_format!("calling a function with fewer arguments than it requires") - })?; + let Some((caller_arg, caller_abi)) = caller_args.next() else { + throw_ub_custom!(fluent::const_eval_not_enough_caller_args); + }; // Now, check if !Self::check_argument_compat(caller_abi, callee_abi) { - throw_ub_format!( - "calling a function with argument of type {:?} passing data of type {:?}", - callee_arg.layout.ty, - caller_arg.layout.ty + let callee_ty = format!("{}", callee_arg.layout.ty); + let caller_ty = format!("{}", caller_arg.layout.ty); + throw_ub_custom!( + fluent::const_eval_incompatible_types, + callee_ty = callee_ty, + caller_ty = caller_ty, ) } // Special handling for unsized parameters. @@ -398,10 +401,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { if M::enforce_abi(self) { if caller_fn_abi.conv != callee_fn_abi.conv { - throw_ub_format!( - "calling a function with calling convention {:?} using calling convention {:?}", - callee_fn_abi.conv, - caller_fn_abi.conv + throw_ub_custom!( + fluent::const_eval_incompatible_calling_conventions, + callee_conv = format!("{:?}", callee_fn_abi.conv), + caller_conv = format!("{:?}", caller_fn_abi.conv), ) } } @@ -508,15 +511,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { "mismatch between callee ABI and callee body arguments" ); if caller_args.next().is_some() { - throw_ub_format!("calling a function with more arguments than it expected") + throw_ub_custom!(fluent::const_eval_too_many_caller_args); } // Don't forget to check the return type! if !Self::check_argument_compat(&caller_fn_abi.ret, &callee_fn_abi.ret) { - throw_ub_format!( - "calling a function with return type {:?} passing \ - return place of type {:?}", - callee_fn_abi.ret.layout.ty, - caller_fn_abi.ret.layout.ty, + let callee_ty = format!("{}", callee_fn_abi.ret.layout.ty); + let caller_ty = format!("{}", caller_fn_abi.ret.layout.ty); + throw_ub_custom!( + fluent::const_eval_incompatible_return_types, + callee_ty = callee_ty, + caller_ty = caller_ty, ) } }; @@ -587,9 +591,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let (recv, vptr) = self.unpack_dyn_star(&receiver_place.into())?; let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?; if dyn_trait != data.principal() { - throw_ub_format!( - "`dyn*` call on a pointer whose vtable does not match its type" - ); + throw_ub_custom!(fluent::const_eval_dyn_star_call_vtable_mismatch); } let recv = recv.assert_mem_place(); // we passed an MPlaceTy to `unpack_dyn_star` so we definitely still have one @@ -609,9 +611,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let vptr = receiver_place.meta.unwrap_meta().to_pointer(self)?; let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?; if dyn_trait != data.principal() { - throw_ub_format!( - "`dyn` call on a pointer whose vtable does not match its type" - ); + throw_ub_custom!(fluent::const_eval_dyn_call_vtable_mismatch); } // It might be surprising that we use a pointer as the receiver even if this @@ -623,7 +623,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // Now determine the actual method to call. We can do that in two different ways and // compare them to ensure everything fits. let Some(ty::VtblEntry::Method(fn_inst)) = self.get_vtable_entries(vptr)?.get(idx).copied() else { - throw_ub_format!("`dyn` call trying to call something that is not a method") + // FIXME(fee1-dead) these could be variants of the UB info enum instead of this + throw_ub_custom!(fluent::const_eval_dyn_call_not_a_method); }; trace!("Virtual call dispatches to {fn_inst:#?}"); if cfg!(debug_assertions) { diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 01b77289937..21c655988a0 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -4,7 +4,7 @@ //! That's useful because it means other passes (e.g. promotion) can rely on `const`s //! to be const-safe. -use std::fmt::{Display, Write}; +use std::fmt::Write; use std::num::NonZeroUsize; use either::{Left, Right}; @@ -12,7 +12,10 @@ use either::{Left, Right}; use rustc_ast::Mutability; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; -use rustc_middle::mir::interpret::InterpError; +use rustc_middle::mir::interpret::{ + ExpectedKind, InterpError, InvalidMetaKind, PointerKind, ValidationErrorInfo, + ValidationErrorKind, ValidationErrorKind::*, +}; use rustc_middle::ty; use rustc_middle::ty::layout::{LayoutOf, TyAndLayout}; use rustc_span::symbol::{sym, Symbol}; @@ -30,14 +33,7 @@ use super::{ }; macro_rules! throw_validation_failure { - ($where:expr, { $( $what_fmt:tt )* } $( expected { $( $expected_fmt:tt )* } )?) => {{ - let mut msg = String::new(); - msg.push_str("encountered "); - write!(&mut msg, $($what_fmt)*).unwrap(); - $( - msg.push_str(", but expected "); - write!(&mut msg, $($expected_fmt)*).unwrap(); - )? + ($where:expr, $kind: expr) => {{ let where_ = &$where; let path = if !where_.is_empty() { let mut path = String::new(); @@ -46,7 +42,8 @@ macro_rules! throw_validation_failure { } else { None }; - throw_ub!(ValidationFailure { path, msg }) + + throw_ub!(Validation(ValidationErrorInfo { path, kind: $kind })) }}; } @@ -82,22 +79,22 @@ macro_rules! throw_validation_failure { /// macro_rules! try_validation { ($e:expr, $where:expr, - $( $( $p:pat_param )|+ => { $( $what_fmt:tt )* } $( expected { $( $expected_fmt:tt )* } )? ),+ $(,)? + $( $( $p:pat_param )|+ => $kind: expr ),+ $(,)? ) => {{ match $e { Ok(x) => x, // We catch the error and turn it into a validation failure. We are okay with // allocation here as this can only slow down builds that fail anyway. - Err(e) => match e.kind() { + Err(e) => match e.into_parts() { $( - InterpError::UndefinedBehavior($($p)|+) => + (InterpError::UndefinedBehavior($($p)|+), _) => throw_validation_failure!( $where, - { $( $what_fmt )* } $( expected { $( $expected_fmt )* } )? + $kind ) ),+, #[allow(unreachable_patterns)] - _ => Err::<!, _>(e)?, + (e, rest) => Err::<!, _>($crate::interpret::InterpErrorInfo::from_parts(e, rest))?, } } }}; @@ -160,6 +157,7 @@ impl<T: Copy + Eq + Hash + std::fmt::Debug, PATH: Default> RefTracking<T, PATH> } } +// FIXME make this translatable as well? /// Format a path fn write_path(out: &mut String, path: &[PathElem]) { use self::PathElem::*; @@ -185,26 +183,6 @@ fn write_path(out: &mut String, path: &[PathElem]) { } } -// Formats such that a sentence like "expected something {}" to mean -// "expected something <in the given range>" makes sense. -fn wrapping_range_format(r: WrappingRange, max_hi: u128) -> String { - let WrappingRange { start: lo, end: hi } = r; - assert!(hi <= max_hi); - if lo > hi { - format!("less or equal to {}, or greater or equal to {}", hi, lo) - } else if lo == hi { - format!("equal to {}", lo) - } else if lo == 0 { - assert!(hi < max_hi, "should not be printing if the range covers everything"); - format!("less or equal to {}", hi) - } else if hi == max_hi { - assert!(lo > 0, "should not be printing if the range covers everything"); - format!("greater or equal to {}", lo) - } else { - format!("in the range {:?}", r) - } -} - struct ValidityVisitor<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> { /// The `path` may be pushed to, but the part that is present when a function /// starts must not be changed! `visit_fields` and `visit_array` rely on @@ -311,19 +289,19 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' fn read_immediate( &self, op: &OpTy<'tcx, M::Provenance>, - expected: impl Display, + expected: ExpectedKind, ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> { Ok(try_validation!( self.ecx.read_immediate(op), self.path, - InvalidUninitBytes(None) => { "uninitialized memory" } expected { "{expected}" } + InvalidUninitBytes(None) => Uninit { expected } )) } fn read_scalar( &self, op: &OpTy<'tcx, M::Provenance>, - expected: impl Display, + expected: ExpectedKind, ) -> InterpResult<'tcx, Scalar<M::Provenance>> { Ok(self.read_immediate(op, expected)?.to_scalar()) } @@ -342,8 +320,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' self.ecx.get_ptr_vtable(vtable), self.path, DanglingIntPointer(..) | - InvalidVTablePointer(..) => - { "{vtable}" } expected { "a vtable pointer" }, + InvalidVTablePointer(..) => InvalidVTablePtr { value: format!("{vtable}") } ); // FIXME: check if the type/trait match what ty::Dynamic says? } @@ -366,10 +343,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' fn check_safe_pointer( &mut self, value: &OpTy<'tcx, M::Provenance>, - kind: &str, + ptr_kind: PointerKind, ) -> InterpResult<'tcx> { - let place = - self.ecx.ref_to_mplace(&self.read_immediate(value, format_args!("a {kind}"))?)?; + let place = self.ecx.ref_to_mplace(&self.read_immediate(value, ptr_kind.into())?)?; // Handle wide pointers. // Check metadata early, for better diagnostics if place.layout.is_unsized() { @@ -379,7 +355,10 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' let size_and_align = try_validation!( self.ecx.size_and_align_of_mplace(&place), self.path, - InvalidMeta(msg) => { "invalid {} metadata: {}", kind, msg }, + InvalidMeta(msg) => match msg { + InvalidMetaKind::SliceTooBig => InvalidMetaSliceTooLarge { ptr_kind }, + InvalidMetaKind::TooBig => InvalidMetaTooLarge { ptr_kind }, + } ); let (size, align) = size_and_align // for the purpose of validity, consider foreign types to have @@ -395,31 +374,30 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message ), self.path, - AlignmentCheckFailed { required, has } => - { - "an unaligned {kind} (required {} byte alignment but found {})", - required.bytes(), - has.bytes(), - }, - DanglingIntPointer(0, _) => - { "a null {kind}" }, - DanglingIntPointer(i, _) => - { - "a dangling {kind} ({pointer} has no provenance)", - pointer = Pointer::<Option<AllocId>>::from_addr_invalid(*i), - }, - PointerOutOfBounds { .. } => - { "a dangling {kind} (going beyond the bounds of its allocation)" }, + AlignmentCheckFailed { required, has } => UnalignedPtr { + ptr_kind, + required_bytes: required.bytes(), + found_bytes: has.bytes() + }, + DanglingIntPointer(0, _) => NullPtr { ptr_kind }, + DanglingIntPointer(i, _) => DanglingPtrNoProvenance { + ptr_kind, + // FIXME this says "null pointer" when null but we need translate + pointer: format!("{}", Pointer::<Option<AllocId>>::from_addr_invalid(i)) + }, + PointerOutOfBounds { .. } => DanglingPtrOutOfBounds { + ptr_kind + }, // This cannot happen during const-eval (because interning already detects // dangling pointers), but it can happen in Miri. - PointerUseAfterFree(..) => - { "a dangling {kind} (use-after-free)" }, + PointerUseAfterFree(..) => DanglingPtrUseAfterFree { + ptr_kind, + }, ); // Do not allow pointers to uninhabited types. if place.layout.abi.is_uninhabited() { - throw_validation_failure!(self.path, - { "a {kind} pointing to uninhabited type {}", place.layout.ty } - ) + let ty = place.layout.ty; + throw_validation_failure!(self.path, PtrToUninhabited { ptr_kind, ty }) } // Recursive checking if let Some(ref_tracking) = self.ref_tracking.as_deref_mut() { @@ -441,9 +419,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // this check is so important. // This check is reachable when the const just referenced the static, // but never read it (so we never entered `before_access_global`). - throw_validation_failure!(self.path, - { "a {} pointing to a static variable in a constant", kind } - ); + throw_validation_failure!(self.path, PtrToStatic { ptr_kind }); } // We skip recursively checking other statics. These statics must be sound by // themselves, and the only way to get broken statics here is by using @@ -464,9 +440,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // This should be unreachable, but if someone manages to copy a pointer // out of a `static`, then that pointer might point to mutable memory, // and we would catch that here. - throw_validation_failure!(self.path, - { "a {} pointing to mutable memory in a constant", kind } - ); + throw_validation_failure!(self.path, PtrToMut { ptr_kind }); } } // Nothing to check for these. @@ -496,22 +470,24 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' let ty = value.layout.ty; match ty.kind() { ty::Bool => { - let value = self.read_scalar(value, "a boolean")?; + let value = self.read_scalar(value, ExpectedKind::Bool)?; try_validation!( value.to_bool(), self.path, - InvalidBool(..) => - { "{:x}", value } expected { "a boolean" }, + InvalidBool(..) => ValidationErrorKind::InvalidBool { + value: format!("{value:x}"), + } ); Ok(true) } ty::Char => { - let value = self.read_scalar(value, "a unicode scalar value")?; + let value = self.read_scalar(value, ExpectedKind::Char)?; try_validation!( value.to_char(), self.path, - InvalidChar(..) => - { "{:x}", value } expected { "a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)" }, + InvalidChar(..) => ValidationErrorKind::InvalidChar { + value: format!("{value:x}"), + } ); Ok(true) } @@ -521,16 +497,17 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' let value = self.read_scalar( value, if matches!(ty.kind(), ty::Float(..)) { - "a floating point number" + ExpectedKind::Float } else { - "an integer" + ExpectedKind::Int }, )?; // As a special exception we *do* match on a `Scalar` here, since we truly want // to know its underlying representation (and *not* cast it to an integer). if matches!(value, Scalar::Ptr(..)) { - throw_validation_failure!(self.path, - { "{:x}", value } expected { "plain (non-pointer) bytes" } + throw_validation_failure!( + self.path, + ExpectedNonPtr { value: format!("{value:x}") } ) } Ok(true) @@ -540,7 +517,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // actually enforce the strict rules for raw pointers (mostly because // that lets us re-use `ref_to_mplace`). let place = - self.ecx.ref_to_mplace(&self.read_immediate(value, "a raw pointer")?)?; + self.ecx.ref_to_mplace(&self.read_immediate(value, ExpectedKind::RawPtr)?)?; if place.layout.is_unsized() { self.check_wide_ptr_meta(place.meta, place.layout)?; } @@ -554,14 +531,14 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // a ZST). let layout = self.ecx.layout_of(*ty)?; if !layout.is_zst() { - throw_validation_failure!(self.path, { "mutable reference in a `const`" }); + throw_validation_failure!(self.path, MutableRefInConst); } } - self.check_safe_pointer(value, "reference")?; + self.check_safe_pointer(value, PointerKind::Ref)?; Ok(true) } ty::FnPtr(_sig) => { - let value = self.read_scalar(value, "a function pointer")?; + let value = self.read_scalar(value, ExpectedKind::FnPtr)?; // If we check references recursively, also check that this points to a function. if let Some(_) = self.ref_tracking { @@ -570,19 +547,20 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' self.ecx.get_ptr_fn(ptr), self.path, DanglingIntPointer(..) | - InvalidFunctionPointer(..) => - { "{ptr}" } expected { "a function pointer" }, + InvalidFunctionPointer(..) => InvalidFnPtr { + value: format!("{ptr}"), + }, ); // FIXME: Check if the signature matches } else { // Otherwise (for standalone Miri), we have to still check it to be non-null. if self.ecx.scalar_may_be_null(value)? { - throw_validation_failure!(self.path, { "a null function pointer" }); + throw_validation_failure!(self.path, NullFnPtr); } } Ok(true) } - ty::Never => throw_validation_failure!(self.path, { "a value of the never type `!`" }), + ty::Never => throw_validation_failure!(self.path, NeverVal), ty::Foreign(..) | ty::FnDef(..) => { // Nothing to check. Ok(true) @@ -629,12 +607,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' if start == 1 && end == max_value { // Only null is the niche. So make sure the ptr is NOT null. if self.ecx.scalar_may_be_null(scalar)? { - throw_validation_failure!(self.path, - { "a potentially null pointer" } - expected { - "something that cannot possibly fail to be {}", - wrapping_range_format(valid_range, max_value) - } + throw_validation_failure!( + self.path, + NullablePtrOutOfRange { range: valid_range, max_value } ) } else { return Ok(()); @@ -645,12 +620,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' } else { // Conservatively, we reject, because the pointer *could* have a bad // value. - throw_validation_failure!(self.path, - { "a pointer" } - expected { - "something that cannot possibly fail to be {}", - wrapping_range_format(valid_range, max_value) - } + throw_validation_failure!( + self.path, + PtrOutOfRange { range: valid_range, max_value } ) } } @@ -659,9 +631,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' if valid_range.contains(bits) { Ok(()) } else { - throw_validation_failure!(self.path, - { "{}", bits } - expected { "something {}", wrapping_range_format(valid_range, max_value) } + throw_validation_failure!( + self.path, + OutOfRange { value: format!("{bits}"), range: valid_range, max_value } ) } } @@ -685,10 +657,11 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> Ok(try_validation!( this.ecx.read_discriminant(op), this.path, - InvalidTag(val) => - { "{:x}", val } expected { "a valid enum tag" }, - InvalidUninitBytes(None) => - { "uninitialized bytes" } expected { "a valid enum tag" }, + InvalidTag(val) => InvalidEnumTag { + value: format!("{val:x}"), + }, + + InvalidUninitBytes(None) => UninitEnumTag, ) .1) }) @@ -730,7 +703,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> // Special check preventing `UnsafeCell` inside unions in the inner part of constants. if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { inner: true, .. })) { if !op.layout.ty.is_freeze(*self.ecx.tcx, self.ecx.param_env) { - throw_validation_failure!(self.path, { "`UnsafeCell` in a `const`" }); + throw_validation_failure!(self.path, UnsafeCell); } } Ok(()) @@ -738,7 +711,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> #[inline] fn visit_box(&mut self, op: &OpTy<'tcx, M::Provenance>) -> InterpResult<'tcx> { - self.check_safe_pointer(op, "box")?; + self.check_safe_pointer(op, PointerKind::Box)?; Ok(()) } @@ -756,7 +729,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { inner: true, .. })) && def.is_unsafe_cell() { - throw_validation_failure!(self.path, { "`UnsafeCell` in a `const`" }); + throw_validation_failure!(self.path, UnsafeCell); } } @@ -775,14 +748,13 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> // MyNewtype and then the scalar in there). match op.layout.abi { Abi::Uninhabited => { - throw_validation_failure!(self.path, - { "a value of uninhabited type {:?}", op.layout.ty } - ); + let ty = op.layout.ty; + throw_validation_failure!(self.path, UninhabitedVal { ty }); } Abi::Scalar(scalar_layout) => { if !scalar_layout.is_uninit_valid() { // There is something to check here. - let scalar = self.read_scalar(op, "initialized scalar value")?; + let scalar = self.read_scalar(op, ExpectedKind::InitScalar)?; self.visit_scalar(scalar, scalar_layout)?; } } @@ -792,7 +764,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> // the other must be init. if !a_layout.is_uninit_valid() && !b_layout.is_uninit_valid() { let (a, b) = - self.read_immediate(op, "initialized scalar value")?.to_scalar_pair(); + self.read_immediate(op, ExpectedKind::InitScalar)?.to_scalar_pair(); self.visit_scalar(a, a_layout)?; self.visit_scalar(b, b_layout)?; } @@ -822,7 +794,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> try_validation!( self.ecx.read_bytes_ptr_strip_provenance(mplace.ptr, Size::from_bytes(len)), self.path, - InvalidUninitBytes(..) => { "uninitialized data in `str`" }, + InvalidUninitBytes(..) => { UninitStr }, ); } ty::Array(tys, ..) | ty::Slice(tys) @@ -852,7 +824,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> Left(mplace) => mplace, Right(imm) => match *imm { Immediate::Uninit => - throw_validation_failure!(self.path, { "uninitialized bytes" }), + throw_validation_failure!(self.path, UninitVal), Immediate::Scalar(..) | Immediate::ScalarPair(..) => bug!("arrays/slices can never have Scalar/ScalarPair layout"), } @@ -888,7 +860,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M> .unwrap(); self.path.push(PathElem::ArrayElem(i)); - throw_validation_failure!(self.path, { "uninitialized bytes" }) + throw_validation_failure!(self.path, UninitVal) } // Propagate upwards (that will also check for unexpected errors). @@ -929,12 +901,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { match visitor.visit_value(&op) { Ok(()) => Ok(()), // Pass through validation failures. - Err(err) if matches!(err.kind(), err_ub!(ValidationFailure { .. })) => Err(err), + Err(err) if matches!(err.kind(), err_ub!(Validation { .. })) => Err(err), // Complain about any other kind of UB error -- those are bad because we'd like to // report them in a way that shows *where* in the value the issue lies. Err(err) if matches!(err.kind(), InterpError::UndefinedBehavior(_)) => { - err.print_backtrace(); - bug!("Unexpected Undefined Behavior error during validation: {}", err); + let (err, backtrace) = err.into_parts(); + backtrace.print_backtrace(); + bug!("Unexpected Undefined Behavior error during validation: {err:?}"); } // Pass through everything else. Err(err) => Err(err), diff --git a/compiler/rustc_const_eval/src/lib.rs b/compiler/rustc_const_eval/src/lib.rs index 0c48d99915a..8314f53ba57 100644 --- a/compiler/rustc_const_eval/src/lib.rs +++ b/compiler/rustc_const_eval/src/lib.rs @@ -4,6 +4,7 @@ Rust MIR: a lowered representation of Rust. */ +#![deny(rustc::untranslatable_diagnostic)] #![feature(assert_matches)] #![feature(box_patterns)] #![feature(decl_macro)] @@ -33,6 +34,8 @@ pub mod interpret; pub mod transform; pub mod util; +pub use errors::ReportErrorExt; + use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage}; use rustc_fluent_macro::fluent_messages; use rustc_middle::query::Providers; diff --git a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs index 21f3c2c8917..236e43bdfcc 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs @@ -2,9 +2,7 @@ use hir::def_id::LocalDefId; use hir::{ConstContext, LangItem}; -use rustc_errors::{ - error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, -}; +use rustc_errors::{error_code, DiagnosticBuilder, ErrorGuaranteed}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_infer::infer::TyCtxtInferExt; @@ -152,7 +150,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { if let Ok(Some(ImplSource::UserDefined(data))) = implsrc { let span = tcx.def_span(data.impl_def_id); - err.span_note(span, "impl defined here, but it is not `const`"); + err.subdiagnostic(errors::NonConstImplNote { span }); } } _ => {} @@ -166,26 +164,30 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { let mut err = match call_kind { CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => { macro_rules! error { - ($fmt:literal) => { - struct_span_err!(tcx.sess, span, E0015, $fmt, self_ty, ccx.const_kind()) + ($err:ident) => { + tcx.sess.create_err(errors::$err { + span, + ty: self_ty, + kind: ccx.const_kind(), + }) }; } let mut err = match kind { CallDesugaringKind::ForLoopIntoIter => { - error!("cannot convert `{}` into an iterator in {}s") + error!(NonConstForLoopIntoIter) } CallDesugaringKind::QuestionBranch => { - error!("`?` cannot determine the branch of `{}` in {}s") + error!(NonConstQuestionBranch) } CallDesugaringKind::QuestionFromResidual => { - error!("`?` cannot convert from residual of `{}` in {}s") + error!(NonConstQuestionFromResidual) } CallDesugaringKind::TryBlockFromOutput => { - error!("`try` block cannot convert `{}` to the result in {}s") + error!(NonConstTryBlockFromOutput) } CallDesugaringKind::Await => { - error!("cannot convert `{}` into a future in {}s") + error!(NonConstAwait) } }; @@ -193,49 +195,31 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { err } CallKind::FnCall { fn_trait_id, self_ty } => { - let mut err = struct_span_err!( - tcx.sess, - span, - E0015, - "cannot call non-const closure in {}s", - ccx.const_kind(), - ); - - match self_ty.kind() { + let note = match self_ty.kind() { FnDef(def_id, ..) => { let span = tcx.def_span(*def_id); if ccx.tcx.is_const_fn_raw(*def_id) { span_bug!(span, "calling const FnDef errored when it shouldn't"); } - err.span_note(span, "function defined here, but it is not `const`"); - } - FnPtr(..) => { - err.note(format!( - "function pointers need an RFC before allowed to be called in {}s", - ccx.const_kind() - )); + Some(errors::NonConstClosureNote::FnDef { span }) } - Closure(..) => { - err.note(format!( - "closures need an RFC before allowed to be called in {}s", - ccx.const_kind() - )); - } - _ => {} - } + FnPtr(..) => Some(errors::NonConstClosureNote::FnPtr), + Closure(..) => Some(errors::NonConstClosureNote::Closure), + _ => None, + }; + + let mut err = tcx.sess.create_err(errors::NonConstClosure { + span, + kind: ccx.const_kind(), + note, + }); diag_trait(&mut err, self_ty, fn_trait_id); err } CallKind::Operator { trait_id, self_ty, .. } => { - let mut err = struct_span_err!( - tcx.sess, - span, - E0015, - "cannot call non-const operator in {}s", - ccx.const_kind() - ); + let mut sugg = None; if Some(trait_id) == ccx.tcx.lang_items().eq_trait() { match (substs[0].unpack(), substs[1].unpack()) { @@ -260,14 +244,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { let rhs_pos = span.lo() + BytePos::from_usize(eq_idx + 2 + rhs_idx); let rhs_span = span.with_lo(rhs_pos).with_hi(rhs_pos); - err.multipart_suggestion( - "consider dereferencing here", - vec![ - (span.shrink_to_lo(), deref.clone()), - (rhs_span, deref), - ], - Applicability::MachineApplicable, - ); + sugg = Some(errors::ConsiderDereferencing { + deref, + span: span.shrink_to_lo(), + rhs_span, + }); } } } @@ -275,26 +256,29 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> { _ => {} } } - + let mut err = tcx.sess.create_err(errors::NonConstOperator { + span, + kind: ccx.const_kind(), + sugg, + }); diag_trait(&mut err, self_ty, trait_id); err } CallKind::DerefCoercion { deref_target, deref_target_ty, self_ty } => { - let mut err = struct_span_err!( - tcx.sess, - span, - E0015, - "cannot perform deref coercion on `{}` in {}s", - self_ty, - ccx.const_kind() - ); - - err.note(format!("attempting to deref into `{}`", deref_target_ty)); - // Check first whether the source is accessible (issue #87060) - if tcx.sess.source_map().is_span_accessible(deref_target) { - err.span_note(deref_target, "deref defined here"); - } + let target = if tcx.sess.source_map().is_span_accessible(deref_target) { + Some(deref_target) + } else { + None + }; + + let mut err = tcx.sess.create_err(errors::NonConstDerefCoercion { + span, + ty: self_ty, + kind: ccx.const_kind(), + target_ty: deref_target_ty, + deref_target: target, + }); diag_trait(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span))); err @@ -432,21 +416,12 @@ impl<'tcx> NonConstOp<'tcx> for LiveDrop<'tcx> { ccx: &ConstCx<'_, 'tcx>, span: Span, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { - let mut err = struct_span_err!( - ccx.tcx.sess, - span, - E0493, - "destructor of `{}` cannot be evaluated at compile-time", - self.dropped_ty, - ); - err.span_label( + ccx.tcx.sess.create_err(errors::LiveDrop { span, - format!("the destructor for this type cannot be evaluated in {}s", ccx.const_kind()), - ); - if let Some(span) = self.dropped_at { - err.span_label(span, "value is dropped here"); - } - err + dropped_ty: self.dropped_ty, + kind: ccx.const_kind(), + dropped_at: self.dropped_at, + }) } } diff --git a/compiler/rustc_data_structures/src/temp_dir.rs b/compiler/rustc_data_structures/src/temp_dir.rs index a780d2386a6..621d3011a2a 100644 --- a/compiler/rustc_data_structures/src/temp_dir.rs +++ b/compiler/rustc_data_structures/src/temp_dir.rs @@ -16,7 +16,7 @@ impl Drop for MaybeTempDir { // occur. let dir = unsafe { ManuallyDrop::take(&mut self.dir) }; if self.keep { - dir.into_path(); + let _ = dir.into_path(); } } } diff --git a/compiler/rustc_error_codes/src/error_codes/E0741.md b/compiler/rustc_error_codes/src/error_codes/E0741.md index 70d963cd41f..0c701052665 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0741.md +++ b/compiler/rustc_error_codes/src/error_codes/E0741.md @@ -10,15 +10,19 @@ struct A; struct B<const X: A>; // error! ``` -Only structural-match types (that is, types that derive `PartialEq` and `Eq`) -may be used as the types of const generic parameters. +Only structural-match types, which are types that derive `PartialEq` and `Eq` +and implement `ConstParamTy`, may be used as the types of const generic +parameters. -To fix the previous code example, we derive `PartialEq` and `Eq`: +To fix the previous code example, we derive `PartialEq`, `Eq`, and +`ConstParamTy`: ``` #![feature(adt_const_params)] -#[derive(PartialEq, Eq)] // We derive both traits here. +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] // We derive both traits here. struct A; struct B<const X: A>; // ok! diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 2a97c4ff7ae..f3ee83fd4d2 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -492,6 +492,10 @@ impl MultiSpan { replacements_occurred } + pub fn pop_span_label(&mut self) -> Option<(Span, DiagnosticMessage)> { + self.span_labels.pop() + } + /// Returns the strings to highlight. We always ensure that there /// is an entry for each of the primary spans -- for each primary /// span `P`, if there is at least one label with span `P`, we return diff --git a/compiler/rustc_errors/messages.ftl b/compiler/rustc_errors/messages.ftl index 33709734322..8e8223c3cf8 100644 --- a/compiler/rustc_errors/messages.ftl +++ b/compiler/rustc_errors/messages.ftl @@ -8,7 +8,11 @@ errors_target_invalid_address_space = invalid address space `{$addr_space}` for `{$cause}` in "data-layout": {$err} errors_target_invalid_alignment = - invalid alignment for `{$cause}` in "data-layout": {$err} + invalid alignment for `{$cause}` in "data-layout": `{$align}` is {$err_kind -> + [not_power_of_two] not a power of 2 + [too_large] too large + *[other] {""} + } errors_target_invalid_bits = invalid {$kind} `{$bit}` for `{$cause}` in "data-layout": {$err} diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 488f2d67ee5..ed0d06ed0ff 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -10,7 +10,7 @@ use rustc_lint_defs::{Applicability, LintExpectationId}; use rustc_span::symbol::Symbol; use rustc_span::{Span, DUMMY_SP}; use std::borrow::Cow; -use std::fmt; +use std::fmt::{self, Debug}; use std::hash::{Hash, Hasher}; use std::panic::Location; @@ -33,7 +33,7 @@ pub type DiagnosticArgName<'source> = Cow<'source, str>; #[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)] pub enum DiagnosticArgValue<'source> { Str(Cow<'source, str>), - Number(usize), + Number(i128), StrListSepByAnd(Vec<Cow<'source, str>>), } diff --git a/compiler/rustc_errors/src/diagnostic_builder.rs b/compiler/rustc_errors/src/diagnostic_builder.rs index 7d9d0c76450..08ff2cfba5c 100644 --- a/compiler/rustc_errors/src/diagnostic_builder.rs +++ b/compiler/rustc_errors/src/diagnostic_builder.rs @@ -115,36 +115,22 @@ pub trait EmissionGuarantee: Sized { ) -> DiagnosticBuilder<'_, Self>; } -/// Private module for sealing the `IsError` helper trait. -mod sealed_level_is_error { - use crate::Level; - - /// Sealed helper trait for statically checking that a `Level` is an error. - pub(crate) trait IsError<const L: Level> {} - - impl IsError<{ Level::Bug }> for () {} - impl IsError<{ Level::DelayedBug }> for () {} - impl IsError<{ Level::Fatal }> for () {} - // NOTE(eddyb) `Level::Error { lint: true }` is also an error, but lints - // don't need error guarantees, as their levels are always dynamic. - impl IsError<{ Level::Error { lint: false } }> for () {} -} - impl<'a> DiagnosticBuilder<'a, ErrorGuaranteed> { /// Convenience function for internal use, clients should use one of the /// `struct_*` methods on [`Handler`]. #[track_caller] - pub(crate) fn new_guaranteeing_error<M: Into<DiagnosticMessage>, const L: Level>( + pub(crate) fn new_guaranteeing_error<M: Into<DiagnosticMessage>>( handler: &'a Handler, message: M, - ) -> Self - where - (): sealed_level_is_error::IsError<L>, - { + ) -> Self { Self { inner: DiagnosticBuilderInner { state: DiagnosticBuilderState::Emittable(handler), - diagnostic: Box::new(Diagnostic::new_with_code(L, None, message)), + diagnostic: Box::new(Diagnostic::new_with_code( + Level::Error { lint: false }, + None, + message, + )), }, _marker: PhantomData, } @@ -203,9 +189,7 @@ impl EmissionGuarantee for ErrorGuaranteed { handler: &Handler, msg: impl Into<DiagnosticMessage>, ) -> DiagnosticBuilder<'_, Self> { - DiagnosticBuilder::new_guaranteeing_error::<_, { Level::Error { lint: false } }>( - handler, msg, - ) + DiagnosticBuilder::new_guaranteeing_error(handler, msg) } } diff --git a/compiler/rustc_errors/src/diagnostic_impls.rs b/compiler/rustc_errors/src/diagnostic_impls.rs index 65f8a61a30a..10fe7fc74a8 100644 --- a/compiler/rustc_errors/src/diagnostic_impls.rs +++ b/compiler/rustc_errors/src/diagnostic_impls.rs @@ -60,10 +60,8 @@ into_diagnostic_arg_using_display!( u8, i16, u16, - i32, u32, i64, - u64, i128, u128, std::io::Error, @@ -80,6 +78,18 @@ into_diagnostic_arg_using_display!( ExitStatus, ); +impl IntoDiagnosticArg for i32 { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Number(self.into()) + } +} + +impl IntoDiagnosticArg for u64 { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Number(self.into()) + } +} + impl IntoDiagnosticArg for bool { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { if self { @@ -134,7 +144,7 @@ impl IntoDiagnosticArg for PathBuf { impl IntoDiagnosticArg for usize { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { - DiagnosticArgValue::Number(self) + DiagnosticArgValue::Number(self as i128) } } @@ -147,9 +157,9 @@ impl IntoDiagnosticArg for PanicStrategy { impl IntoDiagnosticArg for hir::ConstContext { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { DiagnosticArgValue::Str(Cow::Borrowed(match self { - hir::ConstContext::ConstFn => "constant function", + hir::ConstContext::ConstFn => "const_fn", hir::ConstContext::Static(_) => "static", - hir::ConstContext::Const => "constant", + hir::ConstContext::Const => "const", })) } } @@ -254,7 +264,8 @@ impl IntoDiagnostic<'_, !> for TargetDataLayoutErrors<'_> { TargetDataLayoutErrors::InvalidAlignment { cause, err } => { diag = handler.struct_fatal(fluent::errors_target_invalid_alignment); diag.set_arg("cause", cause); - diag.set_arg("err", err); + diag.set_arg("err_kind", err.diag_ident()); + diag.set_arg("align", err.align()); diag } TargetDataLayoutErrors::InconsistentTargetArchitecture { dl, target } => { diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 6c5f3e62454..bf77ed81f9b 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -6,7 +6,6 @@ #![feature(array_windows)] #![feature(drain_filter)] #![feature(if_let_guard)] -#![feature(adt_const_params)] #![feature(let_chains)] #![feature(never_type)] #![feature(result_option_inspect)] @@ -845,7 +844,7 @@ impl Handler { &self, msg: impl Into<DiagnosticMessage>, ) -> DiagnosticBuilder<'_, ErrorGuaranteed> { - DiagnosticBuilder::new_guaranteeing_error::<_, { Level::Error { lint: false } }>(self, msg) + DiagnosticBuilder::new_guaranteeing_error(self, msg) } /// This should only be used by `rustc_middle::lint::struct_lint_level`. Do not use it for hard errors. diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index e8447310917..70fc66947df 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1675,6 +1675,14 @@ pub struct AnonConst { pub body: BodyId, } +/// An inline constant expression `const { something }`. +#[derive(Copy, Clone, Debug, HashStable_Generic)] +pub struct ConstBlock { + pub hir_id: HirId, + pub def_id: LocalDefId, + pub body: BodyId, +} + /// An expression. #[derive(Debug, Clone, Copy, HashStable_Generic)] pub struct Expr<'hir> { @@ -1922,7 +1930,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool { #[derive(Debug, Clone, Copy, HashStable_Generic)] pub enum ExprKind<'hir> { /// Allow anonymous constants from an inline `const` block - ConstBlock(AnonConst), + ConstBlock(ConstBlock), /// An array (e.g., `[a, b, c, d]`). Array(&'hir [Expr<'hir>]), /// A function call. @@ -3641,6 +3649,7 @@ pub enum Node<'hir> { Variant(&'hir Variant<'hir>), Field(&'hir FieldDef<'hir>), AnonConst(&'hir AnonConst), + ConstBlock(&'hir ConstBlock), Expr(&'hir Expr<'hir>), ExprField(&'hir ExprField<'hir>), Stmt(&'hir Stmt<'hir>), @@ -3695,6 +3704,7 @@ impl<'hir> Node<'hir> { Node::TypeBinding(b) => Some(b.ident), Node::Param(..) | Node::AnonConst(..) + | Node::ConstBlock(..) | Node::Expr(..) | Node::Stmt(..) | Node::Block(..) @@ -3758,7 +3768,7 @@ impl<'hir> Node<'hir> { }) | Node::Expr(Expr { kind: - ExprKind::ConstBlock(AnonConst { body, .. }) + ExprKind::ConstBlock(ConstBlock { body, .. }) | ExprKind::Closure(Closure { body, .. }) | ExprKind::Repeat(_, ArrayLen::Body(AnonConst { body, .. })), .. @@ -3878,6 +3888,13 @@ impl<'hir> Node<'hir> { this } + /// Expect a [`Node::ConstBlock`] or panic. + #[track_caller] + pub fn expect_inline_const(self) -> &'hir ConstBlock { + let Node::ConstBlock(this) = self else { self.expect_failed("an inline constant") }; + this + } + /// Expect a [`Node::Expr`] or panic. #[track_caller] pub fn expect_expr(self) -> &'hir Expr<'hir> { diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index df0047d82e1..f84c814bd92 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -335,6 +335,9 @@ pub trait Visitor<'v>: Sized { fn visit_anon_const(&mut self, c: &'v AnonConst) { walk_anon_const(self, c) } + fn visit_inline_const(&mut self, c: &'v ConstBlock) { + walk_inline_const(self, c) + } fn visit_expr(&mut self, ex: &'v Expr<'v>) { walk_expr(self, ex) } @@ -679,13 +682,18 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo visitor.visit_nested_body(constant.body); } +pub fn walk_inline_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v ConstBlock) { + visitor.visit_id(constant.hir_id); + visitor.visit_nested_body(constant.body); +} + pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) { visitor.visit_id(expression.hir_id); match expression.kind { ExprKind::Array(subexpressions) => { walk_list!(visitor, visit_expr, subexpressions); } - ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const), + ExprKind::ConstBlock(ref const_block) => visitor.visit_inline_const(const_block), ExprKind::Repeat(ref element, ref count) => { visitor.visit_expr(element); visitor.visit_array_length(count) diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 3b2c052e8f4..4a1f8ece7b2 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -177,7 +177,7 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) { } // Generic statics are rejected, but we still reach this case. Err(e) => { - tcx.sess.delay_span_bug(span, e.to_string()); + tcx.sess.delay_span_bug(span, format!("{e:?}")); return; } }; @@ -704,7 +704,7 @@ pub(super) fn check_specialization_validity<'tcx>( // grandparent. In that case, if parent is a `default impl`, inherited items use the // "defaultness" from the grandparent, else they are final. None => { - if tcx.impl_defaultness(parent_impl.def_id()).is_default() { + if tcx.defaultness(parent_impl.def_id()).is_default() { None } else { Some(Err(parent_impl.def_id())) @@ -803,7 +803,7 @@ fn check_impl_items_against_trait<'tcx>( .as_ref() .is_some_and(|node_item| node_item.item.defaultness(tcx).has_value()); - if !is_implemented && tcx.impl_defaultness(impl_id).is_final() { + if !is_implemented && tcx.defaultness(impl_id).is_final() { missing_items.push(tcx.associated_item(trait_item_id)); } diff --git a/compiler/rustc_hir_analysis/src/check/region.rs b/compiler/rustc_hir_analysis/src/check/region.rs index 6ab5556e951..5bd6fcb9612 100644 --- a/compiler/rustc_hir_analysis/src/check/region.rs +++ b/compiler/rustc_hir_analysis/src/check/region.rs @@ -392,7 +392,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h // Manually recurse over closures and inline consts, because they are the only // case of nested bodies that share the parent environment. hir::ExprKind::Closure(&hir::Closure { body, .. }) - | hir::ExprKind::ConstBlock(hir::AnonConst { body, .. }) => { + | hir::ExprKind::ConstBlock(hir::ConstBlock { body, .. }) => { let body = visitor.tcx.hir().body(body); visitor.visit_body(body); } diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index fff417fcb29..69e32c35ed8 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -829,83 +829,20 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) { let ty = tcx.type_of(param.def_id).subst_identity(); if tcx.features().adt_const_params { - if let Some(non_structural_match_ty) = - traits::search_for_adt_const_param_violation(param.span, tcx, ty) - { - // We use the same error code in both branches, because this is really the same - // issue: we just special-case the message for type parameters to make it - // clearer. - match non_structural_match_ty.kind() { - ty::Param(_) => { - // Const parameters may not have type parameters as their types, - // because we cannot be sure that the type parameter derives `PartialEq` - // and `Eq` (just implementing them is not enough for `structural_match`). - struct_span_err!( - tcx.sess, - hir_ty.span, - E0741, - "`{ty}` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be \ - used as the type of a const parameter", - ) - .span_label( - hir_ty.span, - format!("`{ty}` may not derive both `PartialEq` and `Eq`"), - ) - .note( - "it is not currently possible to use a type parameter as the type of a \ - const parameter", - ) - .emit(); - } - ty::Float(_) => { - struct_span_err!( - tcx.sess, - hir_ty.span, - E0741, - "`{ty}` is forbidden as the type of a const generic parameter", - ) - .note("floats do not derive `Eq` or `Ord`, which are required for const parameters") - .emit(); - } - ty::FnPtr(_) => { - struct_span_err!( - tcx.sess, - hir_ty.span, - E0741, - "using function pointers as const generic parameters is forbidden", - ) - .emit(); - } - ty::RawPtr(_) => { - struct_span_err!( - tcx.sess, - hir_ty.span, - E0741, - "using raw pointers as const generic parameters is forbidden", - ) - .emit(); - } - _ => { - let mut diag = struct_span_err!( - tcx.sess, - hir_ty.span, - E0741, - "`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \ - the type of a const parameter", - non_structural_match_ty, - ); - - if ty == non_structural_match_ty { - diag.span_label( - hir_ty.span, - format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"), - ); - } - - diag.emit(); - } - } - } + enter_wf_checking_ctxt(tcx, hir_ty.span, param.def_id, |wfcx| { + let trait_def_id = + tcx.require_lang_item(LangItem::ConstParamTy, Some(hir_ty.span)); + wfcx.register_bound( + ObligationCause::new( + hir_ty.span, + param.def_id, + ObligationCauseCode::ConstParam(ty), + ), + wfcx.param_env, + ty, + trait_def_id, + ); + }); } else { let err_ty_str; let mut is_ptr = true; diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 2f7d465839c..c7b9fc9a697 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -941,7 +941,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { match item { Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => { - if !tcx.impl_defaultness(item.id.owner_id).has_value() { + if !tcx.defaultness(item.id.owner_id).has_value() { tcx.sess.emit_err(errors::FunctionNotHaveDefaultImplementation { span: item.span, note_span: attr_span, diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs index ed60998ec8d..e92a0dcc1fb 100644 --- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs @@ -50,7 +50,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { // We do not allow generic parameters in anon consts if we are inside // of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed. None - } else if tcx.lazy_normalization() { + } else if tcx.features().generic_const_exprs { let parent_node = tcx.hir().get_parent(hir_id); if let Node::Variant(Variant { disr_expr: Some(constant), .. }) = parent_node && constant.hir_id == hir_id @@ -123,9 +123,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { { Some(parent_def_id.to_def_id()) } - Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) => { - Some(tcx.typeck_root_def_id(def_id.to_def_id())) - } // Exclude `GlobalAsm` here which cannot have generics. Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. }) if asm.operands.iter().any(|(op, _op_sp)| match op { @@ -142,7 +139,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { } } } - Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => { + Node::ConstBlock(_) + | Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => { Some(tcx.typeck_root_def_id(def_id.to_def_id())) } Node::Item(item) => match item.kind { @@ -339,17 +337,14 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { } // provide junk type parameter defs for const blocks. - if let Node::AnonConst(_) = node { - let parent_node = tcx.hir().get_parent(hir_id); - if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node { - params.push(ty::GenericParamDef { - index: next_index(), - name: Symbol::intern("<const_ty>"), - def_id: def_id.to_def_id(), - pure_wrt_drop: false, - kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false }, - }); - } + if let Node::ConstBlock(_) = node { + params.push(ty::GenericParamDef { + index: next_index(), + name: Symbol::intern("<const_ty>"), + def_id: def_id.to_def_id(), + pure_wrt_drop: false, + kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false }, + }); } let param_def_id_to_index = params.iter().map(|param| (param.def_id, param.index)).collect(); diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 70d950eddd8..dcb57902928 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -463,7 +463,7 @@ pub(super) fn explicit_predicates_of<'tcx>( } } } else { - if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() { + if matches!(def_kind, DefKind::AnonConst) && tcx.features().generic_const_exprs { let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let parent_def_id = tcx.hir().get_parent_item(hir_id); diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 65ab00fda81..c2b837fcfa6 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -34,12 +34,6 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> { Node::Ty(&Ty { kind: TyKind::Typeof(ref e), .. }) if e.hir_id == hir_id => { return tcx.typeck(def_id).node_type(e.hir_id) } - Node::Expr(&Expr { kind: ExprKind::ConstBlock(ref anon_const), .. }) - if anon_const.hir_id == hir_id => - { - let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id()); - return substs.as_inline_const().ty() - } Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. }) | Node::Item(&Item { kind: ItemKind::GlobalAsm(asm), .. }) if asm.operands.iter().any(|(op, _op_sp)| match op { @@ -435,7 +429,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty in_trait, .. }) => { - if in_trait && !tcx.impl_defaultness(owner).has_value() { + if in_trait && !tcx.defaultness(owner).has_value() { span_bug!( tcx.def_span(def_id), "tried to get type of this RPITIT with no definition" @@ -487,6 +481,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty Node::AnonConst(_) => anon_const_type_of(tcx, def_id), + Node::ConstBlock(_) => { + let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id()); + substs.as_inline_const().ty() + } + Node::GenericParam(param) => match ¶m.kind { GenericParamKind::Type { default: Some(ty), .. } | GenericParamKind::Const { ty, .. } => icx.to_ty(ty), diff --git a/compiler/rustc_hir_analysis/src/outlives/mod.rs b/compiler/rustc_hir_analysis/src/outlives/mod.rs index 2106d6ff07d..fdbb890ce3d 100644 --- a/compiler/rustc_hir_analysis/src/outlives/mod.rs +++ b/compiler/rustc_hir_analysis/src/outlives/mod.rs @@ -20,7 +20,8 @@ pub fn provide(providers: &mut Providers) { fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clause<'_>, Span)] { let id = tcx.hir().local_def_id_to_hir_id(item_def_id); - if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization() + if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) + && tcx.features().generic_const_exprs { if tcx.hir().opt_const_param_default_param_def_id(id).is_some() { // In `generics_of` we set the generics' parent to be our parent's parent which means that diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index d93e8efc1b5..ced46fe426c 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -84,6 +84,7 @@ impl<'a> State<'a> { Node::ImplItem(a) => self.print_impl_item(a), Node::Variant(a) => self.print_variant(a), Node::AnonConst(a) => self.print_anon_const(a), + Node::ConstBlock(a) => self.print_inline_const(a), Node::Expr(a) => self.print_expr(a), Node::ExprField(a) => self.print_expr_field(&a), Node::Stmt(a) => self.print_stmt(a), @@ -1095,10 +1096,10 @@ impl<'a> State<'a> { self.end() } - fn print_expr_anon_const(&mut self, anon_const: &hir::AnonConst) { + fn print_inline_const(&mut self, constant: &hir::ConstBlock) { self.ibox(INDENT_UNIT); self.word_space("const"); - self.print_anon_const(anon_const); + self.ann.nested(self, Nested::Body(constant.body)); self.end() } @@ -1370,7 +1371,7 @@ impl<'a> State<'a> { self.print_expr_vec(exprs); } hir::ExprKind::ConstBlock(ref anon_const) => { - self.print_expr_anon_const(anon_const); + self.print_inline_const(anon_const); } hir::ExprKind::Repeat(element, ref count) => { self.print_expr_repeat(element, count); diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 98c683f0200..7a1e830073a 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -689,8 +689,6 @@ impl<'a, 'tcx> CastCheck<'tcx> { fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'tcx>) { let t_cast = self.cast_ty; let t_expr = self.expr_ty; - let type_asc_or = - if fcx.tcx.features().type_ascription { "type ascription or " } else { "" }; let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() { ("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS) } else { @@ -711,7 +709,7 @@ impl<'a, 'tcx> CastCheck<'tcx> { |lint| { lint.help(format!( "cast can be replaced by coercion; this might \ - require {type_asc_or}a temporary variable" + require a temporary variable" )) }, ); diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 905781ec8f5..ba49e0c4161 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1595,7 +1595,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { Some(blk_id), ); if !fcx.tcx.features().unsized_locals { - unsized_return = self.is_return_ty_unsized(fcx, blk_id); + unsized_return = self.is_return_ty_definitely_unsized(fcx); } if let Some(expression) = expression && let hir::ExprKind::Loop(loop_blk, ..) = expression.kind { @@ -1614,8 +1614,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { None, ); if !fcx.tcx.features().unsized_locals { - let id = fcx.tcx.hir().parent_id(id); - unsized_return = self.is_return_ty_unsized(fcx, id); + unsized_return = self.is_return_ty_definitely_unsized(fcx); } } _ => { @@ -1896,15 +1895,24 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { err.help("you could instead create a new `enum` with a variant for each returned type"); } - fn is_return_ty_unsized<'a>(&self, fcx: &FnCtxt<'a, 'tcx>, blk_id: hir::HirId) -> bool { - if let Some((_, fn_decl, _)) = fcx.get_fn_decl(blk_id) - && let hir::FnRetTy::Return(ty) = fn_decl.output - && let ty = fcx.astconv().ast_ty_to_ty( ty) - && let ty::Dynamic(..) = ty.kind() - { - return true; + /// Checks whether the return type is unsized via an obligation, which makes + /// sure we consider `dyn Trait: Sized` where clauses, which are trivially + /// false but technically valid for typeck. + fn is_return_ty_definitely_unsized(&self, fcx: &FnCtxt<'_, 'tcx>) -> bool { + if let Some(sig) = fcx.body_fn_sig() { + !fcx.predicate_may_hold(&Obligation::new( + fcx.tcx, + ObligationCause::dummy(), + fcx.param_env, + ty::TraitRef::new( + fcx.tcx, + fcx.tcx.require_lang_item(hir::LangItem::Sized, None), + [sig.output()], + ), + )) + } else { + false } - false } pub fn complete<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> { diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 19ff77d8349..3c5feb1ba51 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -348,9 +348,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected), ExprKind::Array(args) => self.check_expr_array(args, expected, expr), - ExprKind::ConstBlock(ref anon_const) => { - self.check_expr_const_block(anon_const, expected, expr) - } + ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected, expr), ExprKind::Repeat(element, ref count) => { self.check_expr_repeat(element, count, expected, expr) } @@ -1368,20 +1366,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { fn check_expr_const_block( &self, - anon_const: &'tcx hir::AnonConst, + block: &'tcx hir::ConstBlock, expected: Expectation<'tcx>, _expr: &'tcx hir::Expr<'tcx>, ) -> Ty<'tcx> { - let body = self.tcx.hir().body(anon_const.body); + let body = self.tcx.hir().body(block.body); // Create a new function context. - let def_id = anon_const.def_id; + let def_id = block.def_id; let fcx = FnCtxt::new(self, self.param_env.with_const(), def_id); crate::GatherLocalsVisitor::new(&fcx).visit_body(body); let ty = fcx.check_expr_with_expectation(&body.value, expected); fcx.require_type_is_sized(ty, body.value.span, traits::ConstSized); - fcx.write_ty(anon_const.hir_id, ty); + fcx.write_ty(block.hir_id, ty); ty } @@ -3117,16 +3115,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } ty::Tuple(tys) => { - let fstr = field.as_str(); - - if let Ok(index) = fstr.parse::<usize>() { - if fstr == index.to_string() { - if let Some(&field_ty) = tys.get(index) { - field_indices.push(index.into()); - current_container = field_ty; + if let Ok(index) = field.as_str().parse::<usize>() + && field.name == sym::integer(index) + { + for ty in tys.iter().take(index + 1) { + self.require_type_is_sized(ty, expr.span, traits::MiscObligation); + } + if let Some(&field_ty) = tys.get(index) { + field_indices.push(index.into()); + current_container = field_ty; - continue; - } + continue; } } } 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 e4a62ec05ae..786a8c28f99 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 @@ -270,6 +270,7 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> { | hir::Node::Variant(..) | hir::Node::Field(..) | hir::Node::AnonConst(..) + | hir::Node::ConstBlock(..) | hir::Node::Stmt(..) | hir::Node::PathSegment(..) | hir::Node::Ty(..) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 9ee967dc7a9..7a484e7ddc5 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -2110,20 +2110,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | sym::Hash | sym::Debug => true, _ => false, - } && match trait_pred.trait_ref.substs.as_slice() { - // Only suggest deriving if lhs == rhs... - [lhs, rhs] => { - if let Some(lhs) = lhs.as_type() - && let Some(rhs) = rhs.as_type() - { - self.can_eq(self.param_env, lhs, rhs) - } else { - false - } - }, - // Unary ops can always be derived - [_] => true, - _ => false, }; if can_derive { let self_name = trait_pred.self_ty().to_string(); diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index b6b935de68c..ed532aa2e8b 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -227,7 +227,7 @@ impl<'tcx> InferCtxt<'tcx> { return self.unify_const_variable(vid, a, relation.param_env()); } (ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..)) - if self.tcx.lazy_normalization() => + if self.tcx.features().generic_const_exprs || self.tcx.trait_solver_next() => { relation.register_const_equate_obligation(a, b); return Ok(b); diff --git a/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs b/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs index d1f110472c9..9a8fac0fc1b 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs @@ -586,7 +586,7 @@ fn foo(&self) -> Self::T { String::new() } // FIXME: account for returning some type in a trait fn impl that has // an assoc type as a return type (#72076). if let hir::Defaultness::Default { has_value: true } = - tcx.impl_defaultness(item.id.owner_id) + tcx.defaultness(item.id.owner_id) { let assoc_ty = tcx.type_of(item.id.owner_id).subst_identity(); if self.infcx.can_eq(param_env, assoc_ty, found) { diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 358d412a4d8..213e8db66a0 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1317,10 +1317,14 @@ declare_lint! { /// /// ### Explanation /// - /// A bare `pub` visibility may be misleading if the item is not actually - /// publicly exported from the crate. The `pub(crate)` visibility is - /// recommended to be used instead, which more clearly expresses the intent - /// that the item is only visible within its own crate. + /// The `pub` keyword both expresses an intent for an item to be publicly available, and also + /// signals to the compiler to make the item publicly accessible. The intent can only be + /// satisfied, however, if all items which contain this item are *also* publicly accessible. + /// Thus, this lint serves to identify situations where the intent does not match the reality. + /// + /// If you wish the item to be accessible elsewhere within the crate, but not outside it, the + /// `pub(crate)` visibility is recommended to be used instead. This more clearly expresses the + /// intent that the item is only visible within its own crate. /// /// This lint is "allow" by default because it will trigger for a large /// amount existing Rust code, and has some false-positives. Eventually it diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs index cd6e3687460..2e6e84ad80e 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs @@ -14,6 +14,8 @@ use syn::Token; use syn::{parse_quote, spanned::Spanned, Attribute, Meta, Path, Type}; use synstructure::{BindingInfo, Structure, VariantInfo}; +use super::utils::SubdiagnosticVariant; + /// What kind of diagnostic is being derived - a fatal/error/warning or a lint? #[derive(Clone, PartialEq, Eq)] pub(crate) enum DiagnosticDeriveKind { @@ -150,19 +152,19 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> { fn parse_subdiag_attribute( &self, attr: &Attribute, - ) -> Result<Option<(SubdiagnosticKind, Path)>, DiagnosticDeriveError> { - let Some((subdiag, slug)) = SubdiagnosticKind::from_attr(attr, self)? else { + ) -> Result<Option<(SubdiagnosticKind, Path, bool)>, DiagnosticDeriveError> { + let Some(subdiag) = SubdiagnosticVariant::from_attr(attr, self)? else { // Some attributes aren't errors - like documentation comments - but also aren't // subdiagnostics. return Ok(None); }; - if let SubdiagnosticKind::MultipartSuggestion { .. } = subdiag { + if let SubdiagnosticKind::MultipartSuggestion { .. } = subdiag.kind { throw_invalid_attr!(attr, |diag| diag .help("consider creating a `Subdiagnostic` instead")); } - let slug = slug.unwrap_or_else(|| match subdiag { + let slug = subdiag.slug.unwrap_or_else(|| match subdiag.kind { SubdiagnosticKind::Label => parse_quote! { _subdiag::label }, SubdiagnosticKind::Note => parse_quote! { _subdiag::note }, SubdiagnosticKind::Help => parse_quote! { _subdiag::help }, @@ -171,7 +173,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> { SubdiagnosticKind::MultipartSuggestion { .. } => unreachable!(), }); - Ok(Some((subdiag, slug))) + Ok(Some((subdiag.kind, slug, subdiag.no_span))) } /// Establishes state in the `DiagnosticDeriveBuilder` resulting from the struct @@ -229,7 +231,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> { return Ok(tokens); } - let Some((subdiag, slug)) = self.parse_subdiag_attribute(attr)? else { + let Some((subdiag, slug, _no_span)) = self.parse_subdiag_attribute(attr)? else { // Some attributes aren't errors - like documentation comments - but also aren't // subdiagnostics. return Ok(quote! {}); @@ -380,7 +382,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> { _ => (), } - let Some((subdiag, slug)) = self.parse_subdiag_attribute(attr)? else { + let Some((subdiag, slug, _no_span)) = self.parse_subdiag_attribute(attr)? else { // Some attributes aren't errors - like documentation comments - but also aren't // subdiagnostics. return Ok(quote! {}); diff --git a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs index 374ba1a45c0..e3d9eb96574 100644 --- a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs @@ -14,6 +14,8 @@ use quote::{format_ident, quote}; use syn::{spanned::Spanned, Attribute, Meta, MetaList, Path}; use synstructure::{BindingInfo, Structure, VariantInfo}; +use super::utils::SubdiagnosticVariant; + /// The central struct for constructing the `add_to_diagnostic` method from an annotated struct. pub(crate) struct SubdiagnosticDeriveBuilder { diag: syn::Ident, @@ -180,11 +182,13 @@ impl<'a> FromIterator<&'a SubdiagnosticKind> for KindsStatistics { } impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { - fn identify_kind(&mut self) -> Result<Vec<(SubdiagnosticKind, Path)>, DiagnosticDeriveError> { + fn identify_kind( + &mut self, + ) -> Result<Vec<(SubdiagnosticKind, Path, bool)>, DiagnosticDeriveError> { let mut kind_slugs = vec![]; for attr in self.variant.ast().attrs { - let Some((kind, slug)) = SubdiagnosticKind::from_attr(attr, self)? else { + let Some(SubdiagnosticVariant { kind, slug, no_span }) = SubdiagnosticVariant::from_attr(attr, self)? else { // Some attributes aren't errors - like documentation comments - but also aren't // subdiagnostics. continue; @@ -202,7 +206,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { ); }; - kind_slugs.push((kind, slug)); + kind_slugs.push((kind, slug, no_span)); } Ok(kind_slugs) @@ -487,7 +491,8 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { } }; - let kind_stats: KindsStatistics = kind_slugs.iter().map(|(kind, _slug)| kind).collect(); + let kind_stats: KindsStatistics = + kind_slugs.iter().map(|(kind, _slug, _no_span)| kind).collect(); let init = if kind_stats.has_multipart_suggestion { quote! { let mut suggestions = Vec::new(); } @@ -508,13 +513,17 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { let diag = &self.parent.diag; let f = &self.parent.f; let mut calls = TokenStream::new(); - for (kind, slug) in kind_slugs { + for (kind, slug, no_span) in kind_slugs { let message = format_ident!("__message"); calls.extend( quote! { let #message = #f(#diag, crate::fluent_generated::#slug.into()); }, ); - let name = format_ident!("{}{}", if span_field.is_some() { "span_" } else { "" }, kind); + let name = format_ident!( + "{}{}", + if span_field.is_some() && !no_span { "span_" } else { "" }, + kind + ); let call = match kind { SubdiagnosticKind::Suggestion { suggestion_kind, @@ -566,7 +575,7 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { } } _ => { - if let Some(span) = span_field { + if let Some(span) = span_field && !no_span { quote! { #diag.#name(#span, #message); } } else { quote! { #diag.#name(#message); } diff --git a/compiler/rustc_macros/src/diagnostics/utils.rs b/compiler/rustc_macros/src/diagnostics/utils.rs index e2434981f8d..85dd9f6a3ce 100644 --- a/compiler/rustc_macros/src/diagnostics/utils.rs +++ b/compiler/rustc_macros/src/diagnostics/utils.rs @@ -597,14 +597,20 @@ pub(super) enum SubdiagnosticKind { }, } -impl SubdiagnosticKind { - /// Constructs a `SubdiagnosticKind` from a field or type attribute such as `#[note]`, - /// `#[error(parser::add_paren)]` or `#[suggestion(code = "...")]`. Returns the +pub(super) struct SubdiagnosticVariant { + pub(super) kind: SubdiagnosticKind, + pub(super) slug: Option<Path>, + pub(super) no_span: bool, +} + +impl SubdiagnosticVariant { + /// Constructs a `SubdiagnosticVariant` from a field or type attribute such as `#[note]`, + /// `#[error(parser::add_paren, no_span)]` or `#[suggestion(code = "...")]`. Returns the /// `SubdiagnosticKind` and the diagnostic slug, if specified. pub(super) fn from_attr( attr: &Attribute, fields: &impl HasFieldMap, - ) -> Result<Option<(SubdiagnosticKind, Option<Path>)>, DiagnosticDeriveError> { + ) -> Result<Option<SubdiagnosticVariant>, DiagnosticDeriveError> { // Always allow documentation comments. if is_doc_comment(attr) { return Ok(None); @@ -679,7 +685,7 @@ impl SubdiagnosticKind { | SubdiagnosticKind::Help | SubdiagnosticKind::Warn | SubdiagnosticKind::MultipartSuggestion { .. } => { - return Ok(Some((kind, None))); + return Ok(Some(SubdiagnosticVariant { kind, slug: None, no_span: false })); } SubdiagnosticKind::Suggestion { .. } => { throw_span_err!(span, "suggestion without `code = \"...\"`") @@ -696,11 +702,14 @@ impl SubdiagnosticKind { let mut first = true; let mut slug = None; + let mut no_span = false; list.parse_nested_meta(|nested| { if nested.input.is_empty() || nested.input.peek(Token![,]) { if first { slug = Some(nested.path); + } else if nested.path.is_ident("no_span") { + no_span = true; } else { span_err(nested.input.span().unwrap(), "a diagnostic slug must be the first argument to the attribute").emit(); } @@ -775,19 +784,19 @@ impl SubdiagnosticKind { (_, SubdiagnosticKind::Suggestion { .. }) => { span_err(path_span, "invalid nested attribute") .help( - "only `style`, `code` and `applicability` are valid nested attributes", + "only `no_span`, `style`, `code` and `applicability` are valid nested attributes", ) .emit(); has_errors = true; } (_, SubdiagnosticKind::MultipartSuggestion { .. }) => { span_err(path_span, "invalid nested attribute") - .help("only `style` and `applicability` are valid nested attributes") + .help("only `no_span`, `style` and `applicability` are valid nested attributes") .emit(); has_errors = true; } _ => { - span_err(path_span, "invalid nested attribute").emit(); + span_err(path_span, "only `no_span` is a valid nested attribute").emit(); has_errors = true; } } @@ -831,7 +840,7 @@ impl SubdiagnosticKind { | SubdiagnosticKind::Warn => {} } - Ok(Some((kind, slug))) + Ok(Some(SubdiagnosticVariant { kind, slug, no_span })) } } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index a15307e4345..243e4f5b659 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -231,7 +231,7 @@ provide! { tcx, def_id, other, cdata, opt_def_kind => { table_direct } impl_parent => { table } impl_polarity => { table_direct } - impl_defaultness => { table_direct } + defaultness => { table_direct } constness => { table_direct } coerce_unsized_info => { table } mir_const_qualif => { table } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 6ceb61e793e..cbc012c55c3 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1191,7 +1191,7 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> fn should_encode_const(def_kind: DefKind) -> bool { match def_kind { - DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => true, + DefKind::Const | DefKind::AssocConst | DefKind::AnonConst | DefKind::InlineConst => true, DefKind::Struct | DefKind::Union @@ -1210,7 +1210,6 @@ fn should_encode_const(def_kind: DefKind) -> bool { | DefKind::Closure | DefKind::Generator | DefKind::ConstParam - | DefKind::InlineConst | DefKind::AssocTy | DefKind::TyParam | DefKind::Trait @@ -1437,8 +1436,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id); let tcx = self.tcx; - let impl_defaultness = tcx.impl_defaultness(def_id.expect_local()); - self.tables.impl_defaultness.set_some(def_id.index, impl_defaultness); + let defaultness = tcx.defaultness(def_id.expect_local()); + self.tables.defaultness.set_some(def_id.index, defaultness); let trait_item = tcx.associated_item(def_id); self.tables.assoc_container.set_some(def_id.index, trait_item.container); @@ -1466,8 +1465,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id); let tcx = self.tcx; - let defaultness = self.tcx.impl_defaultness(def_id.expect_local()); - self.tables.impl_defaultness.set_some(def_id.index, defaultness); + let defaultness = self.tcx.defaultness(def_id.expect_local()); + self.tables.defaultness.set_some(def_id.index, defaultness); let impl_item = self.tcx.associated_item(def_id); self.tables.assoc_container.set_some(def_id.index, impl_item.container); @@ -1653,7 +1652,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { ); } hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => { - self.tables.impl_defaultness.set_some(def_id.index, *defaultness); + self.tables.defaultness.set_some(def_id.index, *defaultness); self.tables.constness.set_some(def_id.index, *constness); self.tables.impl_polarity.set_some(def_id.index, self.tcx.impl_polarity(def_id)); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 2da888f4468..e9b67c70c47 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -420,7 +420,7 @@ define_tables! { impl_parent: Table<DefIndex, RawDefId>, impl_polarity: Table<DefIndex, ty::ImplPolarity>, constness: Table<DefIndex, hir::Constness>, - impl_defaultness: Table<DefIndex, hir::Defaultness>, + defaultness: Table<DefIndex, hir::Defaultness>, // FIXME(eddyb) perhaps compute this on the fly if cheap enough? coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>, mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>, diff --git a/compiler/rustc_middle/messages.ftl b/compiler/rustc_middle/messages.ftl index 3d581daa925..bb7147ac80f 100644 --- a/compiler/rustc_middle/messages.ftl +++ b/compiler/rustc_middle/messages.ftl @@ -1,3 +1,38 @@ +middle_adjust_for_foreign_abi_error = + target architecture {$arch} does not support `extern {$abi}` ABI + +middle_assert_async_resume_after_panic = `async fn` resumed after panicking + +middle_assert_async_resume_after_return = `async fn` resumed after completion + +middle_assert_divide_by_zero = + attempt to divide `{$val}` by zero + +middle_assert_generator_resume_after_panic = generator resumed after panicking + +middle_assert_generator_resume_after_return = generator resumed after completion + +middle_assert_misaligned_ptr_deref = + misaligned pointer dereference: address must be a multiple of {$required} but is {$found} + +middle_assert_op_overflow = + attempt to compute `{$left} {$op} {$right}`, which would overflow + +middle_assert_overflow_neg = + attempt to negate `{$val}`, which would overflow + +middle_assert_remainder_by_zero = + attempt to calculate the remainder of `{$val}` with a divisor of zero + +middle_assert_shl_overflow = + attempt to shift left by `{$val}`, which would overflow + +middle_assert_shr_overflow = + attempt to shift right by `{$val}`, which would overflow + +middle_bounds_check = + index out of bounds: the length is {$len} but the index is {$index} + middle_cannot_be_normalized = unable to determine layout for `{$ty}` because `{$failure_ty}` cannot be normalized diff --git a/compiler/rustc_middle/src/error.rs b/compiler/rustc_middle/src/error.rs index 046186d274c..57b2de84b47 100644 --- a/compiler/rustc_middle/src/error.rs +++ b/compiler/rustc_middle/src/error.rs @@ -1,3 +1,7 @@ +use std::borrow::Cow; +use std::fmt; + +use rustc_errors::{DiagnosticArgValue, DiagnosticMessage}; use rustc_macros::Diagnostic; use rustc_span::{Span, Symbol}; @@ -88,3 +92,54 @@ pub(super) struct ConstNotUsedTraitAlias { #[primary_span] pub span: Span, } + +pub struct CustomSubdiagnostic<'a> { + pub msg: fn() -> DiagnosticMessage, + pub add_args: + Box<dyn FnOnce(&mut dyn FnMut(Cow<'static, str>, DiagnosticArgValue<'static>)) + 'a>, +} + +impl<'a> CustomSubdiagnostic<'a> { + pub fn label(x: fn() -> DiagnosticMessage) -> Self { + Self::label_and_then(x, |_| {}) + } + pub fn label_and_then< + F: FnOnce(&mut dyn FnMut(Cow<'static, str>, DiagnosticArgValue<'static>)) + 'a, + >( + msg: fn() -> DiagnosticMessage, + f: F, + ) -> Self { + Self { msg, add_args: Box::new(move |x| f(x)) } + } +} + +impl fmt::Debug for CustomSubdiagnostic<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CustomSubdiagnostic").finish_non_exhaustive() + } +} + +#[derive(Diagnostic)] +pub enum LayoutError<'tcx> { + #[diag(middle_unknown_layout)] + Unknown { ty: Ty<'tcx> }, + + #[diag(middle_values_too_big)] + Overflow { ty: Ty<'tcx> }, + + #[diag(middle_cannot_be_normalized)] + NormalizationFailure { ty: Ty<'tcx>, failure_ty: String }, + + #[diag(middle_cycle)] + Cycle, +} + +#[derive(Diagnostic)] +#[diag(middle_adjust_for_foreign_abi_error)] +pub struct UnsupportedFnAbi { + pub arch: Symbol, + pub abi: &'static str, +} + +/// Used by `rustc_const_eval` +pub use crate::fluent_generated::middle_adjust_for_foreign_abi_error; diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index d1ddc8fc1fd..5f2eb890c4e 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -44,6 +44,7 @@ pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> { } Node::AnonConst(constant) => Some((constant.def_id, constant.body)), + Node::ConstBlock(constant) => Some((constant.def_id, constant.body)), _ => None, } @@ -240,15 +241,8 @@ impl<'hir> Map<'hir> { None => bug!("constructor node without a constructor"), } } - Node::AnonConst(_) => { - let inline = match self.find_parent(hir_id) { - Some(Node::Expr(&Expr { - kind: ExprKind::ConstBlock(ref anon_const), .. - })) if anon_const.hir_id == hir_id => true, - _ => false, - }; - if inline { DefKind::InlineConst } else { DefKind::AnonConst } - } + Node::AnonConst(_) => DefKind::AnonConst, + Node::ConstBlock(_) => DefKind::InlineConst, Node::Field(_) => DefKind::Field, Node::Expr(expr) => match expr.kind { ExprKind::Closure(Closure { movability: None, .. }) => DefKind::Closure, @@ -1060,6 +1054,7 @@ impl<'hir> Map<'hir> { Node::Variant(variant) => variant.span, Node::Field(field) => field.span, Node::AnonConst(constant) => self.body(constant.body).value.span, + Node::ConstBlock(constant) => self.body(constant.body).value.span, Node::Expr(expr) => expr.span, Node::ExprField(field) => field.span, Node::Stmt(stmt) => stmt.span, @@ -1289,6 +1284,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String { format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id)) } Some(Node::AnonConst(_)) => node_str("const"), + Some(Node::ConstBlock(_)) => node_str("const"), Some(Node::Expr(_)) => node_str("expr"), Some(Node::ExprField(_)) => node_str("expr field"), Some(Node::Stmt(_)) => node_str("stmt"), @@ -1434,6 +1430,11 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> { intravisit::walk_anon_const(self, c) } + fn visit_inline_const(&mut self, c: &'hir ConstBlock) { + self.body_owners.push(c.def_id); + intravisit::walk_inline_const(self, c) + } + fn visit_expr(&mut self, ex: &'hir Expr<'hir>) { if let ExprKind::Closure(closure) = ex.kind { self.body_owners.push(closure.def_id); diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 22ee2a8c5e5..0d6c2eba06c 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -48,6 +48,7 @@ #![feature(associated_type_bounds)] #![feature(rustc_attrs)] #![feature(control_flow_enum)] +#![feature(trait_upcasting)] #![feature(trusted_step)] #![feature(try_blocks)] #![feature(try_reserve_kind)] @@ -86,7 +87,7 @@ mod macros; #[macro_use] pub mod arena; -pub(crate) mod error; +pub mod error; pub mod hir; pub mod infer; pub mod lint; diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 1a8e4826447..b8030d9db13 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -296,25 +296,13 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> { Allocation::from_bytes(slice, Align::ONE, Mutability::Not) } - /// Try to create an Allocation of `size` bytes, failing if there is not enough memory - /// available to the compiler to do so. - /// - /// If `panic_on_fail` is true, this will never return `Err`. - pub fn uninit<'tcx>(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'tcx, Self> { - let bytes = Bytes::zeroed(size, align).ok_or_else(|| { - // This results in an error that can happen non-deterministically, since the memory - // available to the compiler can change between runs. Normally queries are always - // deterministic. However, we can be non-deterministic here because all uses of const - // evaluation (including ConstProp!) will make compilation fail (via hard error - // or ICE) upon encountering a `MemoryExhausted` error. - if panic_on_fail { - panic!("Allocation::uninit called with panic_on_fail had allocation failure") - } - ty::tls::with(|tcx| { - tcx.sess.delay_span_bug(DUMMY_SP, "exhausted memory during interpretation") - }); - InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted) - })?; + fn uninit_inner<R>(size: Size, align: Align, fail: impl FnOnce() -> R) -> Result<Self, R> { + // This results in an error that can happen non-deterministically, since the memory + // available to the compiler can change between runs. Normally queries are always + // deterministic. However, we can be non-deterministic here because all uses of const + // evaluation (including ConstProp!) will make compilation fail (via hard error + // or ICE) upon encountering a `MemoryExhausted` error. + let bytes = Bytes::zeroed(size, align).ok_or_else(fail)?; Ok(Allocation { bytes, @@ -325,6 +313,28 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> { extra: (), }) } + + /// Try to create an Allocation of `size` bytes, failing if there is not enough memory + /// available to the compiler to do so. + pub fn try_uninit<'tcx>(size: Size, align: Align) -> InterpResult<'tcx, Self> { + Self::uninit_inner(size, align, || { + ty::tls::with(|tcx| { + tcx.sess.delay_span_bug(DUMMY_SP, "exhausted memory during interpretation") + }); + InterpError::ResourceExhaustion(ResourceExhaustionInfo::MemoryExhausted).into() + }) + } + + /// Try to create an Allocation of `size` bytes, panics if there is not enough memory + /// available to the compiler to do so. + pub fn uninit(size: Size, align: Align) -> Self { + match Self::uninit_inner(size, align, || { + panic!("Allocation::uninit called with panic_on_fail had allocation failure"); + }) { + Ok(x) => x, + Err(x) => x, + } + } } impl<Bytes: AllocBytes> Allocation<AllocId, (), Bytes> { diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 357bcca4419..2435bc59ec0 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -5,11 +5,15 @@ use crate::query::TyCtxtAt; use crate::ty::{layout, tls, Ty, ValTree}; use rustc_data_structures::sync::Lock; -use rustc_errors::{pluralize, struct_span_err, DiagnosticBuilder, ErrorGuaranteed}; +use rustc_errors::{ + struct_span_err, DiagnosticArgValue, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, + IntoDiagnosticArg, +}; use rustc_macros::HashStable; use rustc_session::CtfeBacktrace; use rustc_span::def_id::DefId; -use rustc_target::abi::{call, Align, Size}; +use rustc_target::abi::{call, Align, Size, WrappingRange}; +use std::borrow::Cow; use std::{any::Any, backtrace::Backtrace, fmt}; #[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)] @@ -91,21 +95,54 @@ pub struct InterpErrorInfo<'tcx>(Box<InterpErrorInfoInner<'tcx>>); #[derive(Debug)] struct InterpErrorInfoInner<'tcx> { kind: InterpError<'tcx>, + backtrace: InterpErrorBacktrace, +} + +#[derive(Debug)] +pub struct InterpErrorBacktrace { backtrace: Option<Box<Backtrace>>, } -impl fmt::Display for InterpErrorInfo<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0.kind) +impl InterpErrorBacktrace { + pub fn new() -> InterpErrorBacktrace { + let capture_backtrace = tls::with_opt(|tcx| { + if let Some(tcx) = tcx { + *Lock::borrow(&tcx.sess.ctfe_backtrace) + } else { + CtfeBacktrace::Disabled + } + }); + + let backtrace = match capture_backtrace { + CtfeBacktrace::Disabled => None, + CtfeBacktrace::Capture => Some(Box::new(Backtrace::force_capture())), + CtfeBacktrace::Immediate => { + // Print it now. + let backtrace = Backtrace::force_capture(); + print_backtrace(&backtrace); + None + } + }; + + InterpErrorBacktrace { backtrace } } -} -impl<'tcx> InterpErrorInfo<'tcx> { pub fn print_backtrace(&self) { - if let Some(backtrace) = self.0.backtrace.as_ref() { + if let Some(backtrace) = self.backtrace.as_ref() { print_backtrace(backtrace); } } +} + +impl<'tcx> InterpErrorInfo<'tcx> { + pub fn from_parts(kind: InterpError<'tcx>, backtrace: InterpErrorBacktrace) -> Self { + Self(Box::new(InterpErrorInfoInner { kind, backtrace })) + } + + pub fn into_parts(self) -> (InterpError<'tcx>, InterpErrorBacktrace) { + let InterpErrorInfo(box InterpErrorInfoInner { kind, backtrace }) = self; + (kind, backtrace) + } pub fn into_kind(self) -> InterpError<'tcx> { let InterpErrorInfo(box InterpErrorInfoInner { kind, .. }) = self; @@ -130,32 +167,17 @@ impl From<ErrorGuaranteed> for InterpErrorInfo<'_> { impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> { fn from(kind: InterpError<'tcx>) -> Self { - let capture_backtrace = tls::with_opt(|tcx| { - if let Some(tcx) = tcx { - *Lock::borrow(&tcx.sess.ctfe_backtrace) - } else { - CtfeBacktrace::Disabled - } - }); - - let backtrace = match capture_backtrace { - CtfeBacktrace::Disabled => None, - CtfeBacktrace::Capture => Some(Box::new(Backtrace::force_capture())), - CtfeBacktrace::Immediate => { - // Print it now. - let backtrace = Backtrace::force_capture(); - print_backtrace(&backtrace); - None - } - }; - - InterpErrorInfo(Box::new(InterpErrorInfoInner { kind, backtrace })) + InterpErrorInfo(Box::new(InterpErrorInfoInner { + kind, + backtrace: InterpErrorBacktrace::new(), + })) } } /// Error information for when the program we executed turned out not to actually be a valid /// program. This cannot happen in stand-alone Miri, but it can happen during CTFE/ConstProp /// where we work on generic code or execution does not have all information available. +#[derive(Debug)] pub enum InvalidProgramInfo<'tcx> { /// Resolution can fail if we are in a too generic context. TooGeneric, @@ -174,25 +196,6 @@ pub enum InvalidProgramInfo<'tcx> { UninitUnsizedLocal, } -impl fmt::Display for InvalidProgramInfo<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use InvalidProgramInfo::*; - match self { - TooGeneric => write!(f, "encountered overly generic constant"), - AlreadyReported(_) => { - write!( - f, - "an error has already been reported elsewhere (this should not usually be printed)" - ) - } - Layout(ref err) => write!(f, "{err}"), - FnAbiAdjustForForeignAbi(ref err) => write!(f, "{err}"), - SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{ty}`"), - UninitUnsizedLocal => write!(f, "unsized local is used while uninitialized"), - } - } -} - /// Details of why a pointer had to be in-bounds. #[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] pub enum CheckInAllocMsg { @@ -208,26 +211,25 @@ pub enum CheckInAllocMsg { InboundsTest, } -impl fmt::Display for CheckInAllocMsg { - /// When this is printed as an error the context looks like this: - /// "{msg}{pointer} is a dangling pointer". - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}", - match *self { - CheckInAllocMsg::DerefTest => "dereferencing pointer failed: ", - CheckInAllocMsg::MemoryAccessTest => "memory access failed: ", - CheckInAllocMsg::PointerArithmeticTest => "out-of-bounds pointer arithmetic: ", - CheckInAllocMsg::OffsetFromTest => "out-of-bounds offset_from: ", - CheckInAllocMsg::InboundsTest => "out-of-bounds pointer use: ", - } - ) +#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] +pub enum InvalidMetaKind { + /// Size of a `[T]` is too big + SliceTooBig, + /// Size of a DST is too big + TooBig, +} + +impl IntoDiagnosticArg for InvalidMetaKind { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(Cow::Borrowed(match self { + InvalidMetaKind::SliceTooBig => "slice_too_big", + InvalidMetaKind::TooBig => "too_big", + })) } } /// Details of an access to uninitialized bytes where it is not allowed. -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct UninitBytesAccess { /// Range of the original memory access. pub access: AllocRange, @@ -242,17 +244,32 @@ pub struct ScalarSizeMismatch { pub data_size: u64, } +macro_rules! impl_into_diagnostic_arg_through_debug { + ($($ty:ty),*$(,)?) => {$( + impl IntoDiagnosticArg for $ty { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(Cow::Owned(format!("{self:?}"))) + } + } + )*} +} + +// These types have nice `Debug` output so we can just use them in diagnostics. +impl_into_diagnostic_arg_through_debug! { + AllocId, + Pointer, + AllocRange, +} + /// Error information for when the program caused Undefined Behavior. -pub enum UndefinedBehaviorInfo { - /// Free-form case. Only for errors that are never caught! +#[derive(Debug)] +pub enum UndefinedBehaviorInfo<'a> { + /// Free-form case. Only for errors that are never caught! Used by miri Ub(String), /// Unreachable code was executed. Unreachable, /// A slice/array index projection went out-of-bounds. - BoundsCheckFailed { - len: u64, - index: u64, - }, + BoundsCheckFailed { len: u64, index: u64 }, /// Something was divided by 0 (x / 0). DivisionByZero, /// Something was "remainded" by 0 (x % 0). @@ -263,8 +280,8 @@ pub enum UndefinedBehaviorInfo { RemainderOverflow, /// Overflowing inbounds pointer arithmetic. PointerArithOverflow, - /// Invalid metadata in a wide pointer (using `str` to avoid allocations). - InvalidMeta(&'static str), + /// Invalid metadata in a wide pointer + InvalidMeta(InvalidMetaKind), /// Reading a C string that does not end within its allocation. UnterminatedCString(Pointer), /// Dereferencing a dangling pointer after it got freed. @@ -281,25 +298,13 @@ pub enum UndefinedBehaviorInfo { /// Using an integer as a pointer in the wrong way. DanglingIntPointer(u64, CheckInAllocMsg), /// Used a pointer with bad alignment. - AlignmentCheckFailed { - required: Align, - has: Align, - }, + AlignmentCheckFailed { required: Align, has: Align }, /// Writing to read-only memory. WriteToReadOnly(AllocId), - // Trying to access the data behind a function pointer. + /// Trying to access the data behind a function pointer. DerefFunctionPointer(AllocId), - // Trying to access the data behind a vtable pointer. + /// Trying to access the data behind a vtable pointer. DerefVTablePointer(AllocId), - /// The value validity check found a problem. - /// Should only be thrown by `validity.rs` and always point out which part of the value - /// is the problem. - ValidationFailure { - /// The "path" to the value in question, e.g. `.0[5].field` for a struct - /// field in the 6th element of an array that is the first element of a tuple. - path: Option<String>, - msg: String, - }, /// Using a non-boolean `u8` as bool. InvalidBool(u8), /// Using a non-character `u32` as character. @@ -320,110 +325,100 @@ pub enum UndefinedBehaviorInfo { ScalarSizeMismatch(ScalarSizeMismatch), /// A discriminant of an uninhabited enum variant is written. UninhabitedEnumVariantWritten, + /// Validation error. + Validation(ValidationErrorInfo<'a>), + // FIXME(fee1-dead) these should all be actual variants of the enum instead of dynamically + // dispatched + /// A custom (free-form) error, created by `err_ub_custom!`. + Custom(crate::error::CustomSubdiagnostic<'a>), +} + +#[derive(Debug, Clone, Copy)] +pub enum PointerKind { + Ref, + Box, +} + +impl IntoDiagnosticArg for PointerKind { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str( + match self { + Self::Ref => "ref", + Self::Box => "box", + } + .into(), + ) + } } -impl fmt::Display for UndefinedBehaviorInfo { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use UndefinedBehaviorInfo::*; - match self { - Ub(msg) => write!(f, "{msg}"), - Unreachable => write!(f, "entering unreachable code"), - BoundsCheckFailed { ref len, ref index } => { - write!(f, "indexing out of bounds: the len is {len} but the index is {index}") - } - DivisionByZero => write!(f, "dividing by zero"), - RemainderByZero => write!(f, "calculating the remainder with a divisor of zero"), - DivisionOverflow => write!(f, "overflow in signed division (dividing MIN by -1)"), - RemainderOverflow => write!(f, "overflow in signed remainder (dividing MIN by -1)"), - PointerArithOverflow => write!(f, "overflowing in-bounds pointer arithmetic"), - InvalidMeta(msg) => write!(f, "invalid metadata in wide pointer: {msg}"), - UnterminatedCString(p) => write!( - f, - "reading a null-terminated string starting at {p:?} with no null found before end of allocation", - ), - PointerUseAfterFree(a) => { - write!(f, "pointer to {a:?} was dereferenced after this allocation got freed") - } - PointerOutOfBounds { alloc_id, alloc_size, ptr_offset, ptr_size: Size::ZERO, msg } => { - write!( - f, - "{msg}{alloc_id:?} has size {alloc_size}, so pointer at offset {ptr_offset} is out-of-bounds", - alloc_size = alloc_size.bytes(), - ) - } - PointerOutOfBounds { alloc_id, alloc_size, ptr_offset, ptr_size, msg } => write!( - f, - "{msg}{alloc_id:?} has size {alloc_size}, so pointer to {ptr_size} byte{ptr_size_p} starting at offset {ptr_offset} is out-of-bounds", - alloc_size = alloc_size.bytes(), - ptr_size = ptr_size.bytes(), - ptr_size_p = pluralize!(ptr_size.bytes()), - ), - DanglingIntPointer(i, msg) => { - write!( - f, - "{msg}{pointer} is a dangling pointer (it has no provenance)", - pointer = Pointer::<Option<AllocId>>::from_addr_invalid(*i), - ) - } - AlignmentCheckFailed { required, has } => write!( - f, - "accessing memory with alignment {has}, but alignment {required} is required", - has = has.bytes(), - required = required.bytes() - ), - WriteToReadOnly(a) => write!(f, "writing to {a:?} which is read-only"), - DerefFunctionPointer(a) => write!(f, "accessing {a:?} which contains a function"), - DerefVTablePointer(a) => write!(f, "accessing {a:?} which contains a vtable"), - ValidationFailure { path: None, msg } => { - write!(f, "constructing invalid value: {msg}") - } - ValidationFailure { path: Some(path), msg } => { - write!(f, "constructing invalid value at {path}: {msg}") - } - InvalidBool(b) => { - write!(f, "interpreting an invalid 8-bit value as a bool: 0x{b:02x}") - } - InvalidChar(c) => { - write!(f, "interpreting an invalid 32-bit value as a char: 0x{c:08x}") - } - InvalidTag(val) => write!(f, "enum value has invalid tag: {val:x}"), - InvalidFunctionPointer(p) => { - write!(f, "using {p:?} as function pointer but it does not point to a function") - } - InvalidVTablePointer(p) => { - write!(f, "using {p:?} as vtable pointer but it does not point to a vtable") - } - InvalidStr(err) => write!(f, "this string is not valid UTF-8: {err}"), - InvalidUninitBytes(Some((alloc, info))) => write!( - f, - "reading memory at {alloc:?}{access:?}, \ - but memory is uninitialized at {uninit:?}, \ - and this operation requires initialized memory", - access = info.access, - uninit = info.uninit, - ), - InvalidUninitBytes(None) => write!( - f, - "using uninitialized data, but this operation requires initialized memory" - ), - DeadLocal => write!(f, "accessing a dead local variable"), - ScalarSizeMismatch(self::ScalarSizeMismatch { target_size, data_size }) => write!( - f, - "scalar size mismatch: expected {target_size} bytes but got {data_size} bytes instead", - ), - UninhabitedEnumVariantWritten => { - write!(f, "writing discriminant of an uninhabited enum") - } +#[derive(Debug)] +pub struct ValidationErrorInfo<'tcx> { + pub path: Option<String>, + pub kind: ValidationErrorKind<'tcx>, +} + +#[derive(Debug)] +pub enum ExpectedKind { + Reference, + Box, + RawPtr, + InitScalar, + Bool, + Char, + Float, + Int, + FnPtr, +} + +impl From<PointerKind> for ExpectedKind { + fn from(x: PointerKind) -> ExpectedKind { + match x { + PointerKind::Box => ExpectedKind::Box, + PointerKind::Ref => ExpectedKind::Reference, } } } +#[derive(Debug)] +pub enum ValidationErrorKind<'tcx> { + PtrToUninhabited { ptr_kind: PointerKind, ty: Ty<'tcx> }, + PtrToStatic { ptr_kind: PointerKind }, + PtrToMut { ptr_kind: PointerKind }, + ExpectedNonPtr { value: String }, + MutableRefInConst, + NullFnPtr, + NeverVal, + NullablePtrOutOfRange { range: WrappingRange, max_value: u128 }, + PtrOutOfRange { range: WrappingRange, max_value: u128 }, + OutOfRange { value: String, range: WrappingRange, max_value: u128 }, + UnsafeCell, + UninhabitedVal { ty: Ty<'tcx> }, + InvalidEnumTag { value: String }, + UninitEnumTag, + UninitStr, + Uninit { expected: ExpectedKind }, + UninitVal, + InvalidVTablePtr { value: String }, + InvalidMetaSliceTooLarge { ptr_kind: PointerKind }, + InvalidMetaTooLarge { ptr_kind: PointerKind }, + UnalignedPtr { ptr_kind: PointerKind, required_bytes: u64, found_bytes: u64 }, + NullPtr { ptr_kind: PointerKind }, + DanglingPtrNoProvenance { ptr_kind: PointerKind, pointer: String }, + DanglingPtrOutOfBounds { ptr_kind: PointerKind }, + DanglingPtrUseAfterFree { ptr_kind: PointerKind }, + InvalidBool { value: String }, + InvalidChar { value: String }, + InvalidFnPtr { value: String }, +} + /// Error information for when the program did something that might (or might not) be correct /// to do according to the Rust spec, but due to limitations in the interpreter, the /// operation could not be carried out. These limitations can differ between CTFE and the /// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses. +#[derive(Debug)] pub enum UnsupportedOpInfo { /// Free-form case. Only for errors that are never caught! + // FIXME still use translatable diagnostics Unsupported(String), // // The variants below are only reachable from CTFE/const prop, miri will never emit them. @@ -442,26 +437,9 @@ pub enum UnsupportedOpInfo { ReadExternStatic(DefId), } -impl fmt::Display for UnsupportedOpInfo { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use UnsupportedOpInfo::*; - match self { - Unsupported(ref msg) => write!(f, "{msg}"), - PartialPointerOverwrite(ptr) => { - write!(f, "unable to overwrite parts of a pointer in memory at {ptr:?}") - } - PartialPointerCopy(ptr) => { - write!(f, "unable to copy parts of a pointer from memory at {ptr:?}") - } - ReadPointerAsBytes => write!(f, "unable to turn pointer into raw bytes"), - ThreadLocalStatic(did) => write!(f, "cannot access thread local static ({did:?})"), - ReadExternStatic(did) => write!(f, "cannot read from extern static ({did:?})"), - } - } -} - /// Error information for when the program exhausted the resources granted to it /// by the interpreter. +#[derive(Debug)] pub enum ResourceExhaustionInfo { /// The stack grew too big. StackFrameLimitReached, @@ -471,47 +449,30 @@ pub enum ResourceExhaustionInfo { AddressSpaceFull, } -impl fmt::Display for ResourceExhaustionInfo { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use ResourceExhaustionInfo::*; - match self { - StackFrameLimitReached => { - write!(f, "reached the configured maximum number of stack frames") - } - MemoryExhausted => { - write!(f, "tried to allocate more memory than available to compiler") - } - AddressSpaceFull => { - write!(f, "there are no more free addresses in the address space") - } - } - } -} - -/// A trait to work around not having trait object upcasting. -pub trait AsAny: Any { - fn as_any(&self) -> &dyn Any; -} -impl<T: Any> AsAny for T { - #[inline(always)] - fn as_any(&self) -> &dyn Any { - self - } -} - /// A trait for machine-specific errors (or other "machine stop" conditions). -pub trait MachineStopType: AsAny + fmt::Display + Send {} +pub trait MachineStopType: Any + fmt::Debug + Send { + /// The diagnostic message for this error + fn diagnostic_message(&self) -> DiagnosticMessage; + /// Add diagnostic arguments by passing name and value pairs to `adder`, which are passed to + /// fluent for formatting the translated diagnostic message. + fn add_args( + self: Box<Self>, + adder: &mut dyn FnMut(Cow<'static, str>, DiagnosticArgValue<'static>), + ); +} impl dyn MachineStopType { #[inline(always)] pub fn downcast_ref<T: Any>(&self) -> Option<&T> { - self.as_any().downcast_ref() + let x: &dyn Any = self; + x.downcast_ref() } } +#[derive(Debug)] pub enum InterpError<'tcx> { /// The program caused undefined behavior. - UndefinedBehavior(UndefinedBehaviorInfo), + UndefinedBehavior(UndefinedBehaviorInfo<'tcx>), /// The program did something the interpreter does not support (some of these *might* be UB /// but the interpreter is not sure). Unsupported(UnsupportedOpInfo), @@ -527,26 +488,6 @@ pub enum InterpError<'tcx> { pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>; -impl fmt::Display for InterpError<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use InterpError::*; - match *self { - Unsupported(ref msg) => write!(f, "{msg}"), - InvalidProgram(ref msg) => write!(f, "{msg}"), - UndefinedBehavior(ref msg) => write!(f, "{msg}"), - ResourceExhaustion(ref msg) => write!(f, "{msg}"), - MachineStop(ref msg) => write!(f, "{msg}"), - } - } -} - -// Forward `Debug` to `Display`, so it does not look awful. -impl fmt::Debug for InterpError<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(self, f) - } -} - impl InterpError<'_> { /// Some errors do string formatting even if the error is never printed. /// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors, @@ -555,7 +496,7 @@ impl InterpError<'_> { matches!( self, InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_)) - | InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure { .. }) + | InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Validation { .. }) | InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) ) } diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 3620385fab1..2d2cfee1b21 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -89,6 +89,30 @@ macro_rules! throw_machine_stop { ($($tt:tt)*) => { do yeet err_machine_stop!($($tt)*) }; } +#[macro_export] +macro_rules! err_ub_custom { + ($msg:expr $(, $($name:ident = $value:expr),* $(,)?)?) => {{ + $( + let ($($name,)*) = ($($value,)*); + )? + err_ub!(Custom( + rustc_middle::error::CustomSubdiagnostic { + msg: || $msg, + add_args: Box::new(move |mut set_arg| { + $($( + set_arg(stringify!($name).into(), rustc_errors::IntoDiagnosticArg::into_diagnostic_arg($name)); + )*)? + }) + } + )) + }}; +} + +#[macro_export] +macro_rules! throw_ub_custom { + ($($tt:tt)*) => { do yeet err_ub_custom!($($tt)*) }; +} + mod allocation; mod error; mod pointer; @@ -119,9 +143,10 @@ use crate::ty::{self, Instance, Ty, TyCtxt}; pub use self::error::{ struct_error, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, - EvalToValTreeResult, InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, - MachineStopType, ReportedErrorInfo, ResourceExhaustionInfo, ScalarSizeMismatch, - UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo, + EvalToValTreeResult, ExpectedKind, InterpError, InterpErrorInfo, InterpResult, InvalidMetaKind, + InvalidProgramInfo, MachineStopType, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo, + ScalarSizeMismatch, UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo, + ValidationErrorInfo, ValidationErrorKind, }; pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar}; diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index 36dbbe4bf77..91caf9db336 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -375,7 +375,8 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> { #[inline(always)] #[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980) pub fn assert_bits(self, target_size: Size) -> u128 { - self.to_bits(target_size).unwrap() + self.to_bits(target_size) + .unwrap_or_else(|_| panic!("assertion failed: {self:?} fits {target_size:?}")) } pub fn to_bool(self) -> InterpResult<'tcx, bool> { diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 5c27bdec575..e929240bf30 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -15,7 +15,7 @@ use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex}; use crate::ty::{GenericArg, InternalSubsts, SubstsRef}; use rustc_data_structures::captures::Captures; -use rustc_errors::ErrorGuaranteed; +use rustc_errors::{DiagnosticArgValue, DiagnosticMessage, ErrorGuaranteed, IntoDiagnosticArg}; use rustc_hir::def::{CtorKind, Namespace}; use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; use rustc_hir::{self, GeneratorKind, ImplicitSelfKind}; @@ -1371,55 +1371,61 @@ impl<O> AssertKind<O> { _ => write!(f, "\"{}\"", self.description()), } } -} -impl<O: fmt::Debug> fmt::Debug for AssertKind<O> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + pub fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; use AssertKind::*; + match self { - BoundsCheck { ref len, ref index } => write!( - f, - "index out of bounds: the length is {:?} but the index is {:?}", - len, index - ), - OverflowNeg(op) => write!(f, "attempt to negate `{:#?}`, which would overflow", op), - DivisionByZero(op) => write!(f, "attempt to divide `{:#?}` by zero", op), - RemainderByZero(op) => write!( - f, - "attempt to calculate the remainder of `{:#?}` with a divisor of zero", - op - ), - Overflow(BinOp::Add, l, r) => { - write!(f, "attempt to compute `{:#?} + {:#?}`, which would overflow", l, r) - } - Overflow(BinOp::Sub, l, r) => { - write!(f, "attempt to compute `{:#?} - {:#?}`, which would overflow", l, r) - } - Overflow(BinOp::Mul, l, r) => { - write!(f, "attempt to compute `{:#?} * {:#?}`, which would overflow", l, r) - } - Overflow(BinOp::Div, l, r) => { - write!(f, "attempt to compute `{:#?} / {:#?}`, which would overflow", l, r) + BoundsCheck { .. } => middle_bounds_check, + Overflow(BinOp::Shl, _, _) => middle_assert_shl_overflow, + Overflow(BinOp::Shr, _, _) => middle_assert_shr_overflow, + Overflow(_, _, _) => middle_assert_op_overflow, + OverflowNeg(_) => middle_assert_overflow_neg, + DivisionByZero(_) => middle_assert_divide_by_zero, + RemainderByZero(_) => middle_assert_remainder_by_zero, + ResumedAfterReturn(GeneratorKind::Async(_)) => middle_assert_async_resume_after_return, + ResumedAfterReturn(GeneratorKind::Gen) => middle_assert_generator_resume_after_return, + ResumedAfterPanic(GeneratorKind::Async(_)) => middle_assert_async_resume_after_panic, + ResumedAfterPanic(GeneratorKind::Gen) => middle_assert_generator_resume_after_panic, + + MisalignedPointerDereference { .. } => middle_assert_misaligned_ptr_deref, + } + } + + pub fn add_args(self, adder: &mut dyn FnMut(Cow<'static, str>, DiagnosticArgValue<'static>)) + where + O: fmt::Debug, + { + use AssertKind::*; + + macro_rules! add { + ($name: expr, $value: expr) => { + adder($name.into(), $value.into_diagnostic_arg()); + }; + } + + match self { + BoundsCheck { len, index } => { + add!("len", format!("{len:?}")); + add!("index", format!("{index:?}")); } - Overflow(BinOp::Rem, l, r) => write!( - f, - "attempt to compute the remainder of `{:#?} % {:#?}`, which would overflow", - l, r - ), - Overflow(BinOp::Shr, _, r) => { - write!(f, "attempt to shift right by `{:#?}`, which would overflow", r) + Overflow(BinOp::Shl | BinOp::Shr, _, val) + | DivisionByZero(val) + | RemainderByZero(val) + | OverflowNeg(val) => { + add!("val", format!("{val:#?}")); } - Overflow(BinOp::Shl, _, r) => { - write!(f, "attempt to shift left by `{:#?}`, which would overflow", r) + Overflow(binop, left, right) => { + add!("op", binop.to_hir_binop().as_str()); + add!("left", format!("{left:#?}")); + add!("right", format!("{right:#?}")); } + ResumedAfterReturn(_) | ResumedAfterPanic(_) => {} MisalignedPointerDereference { required, found } => { - write!( - f, - "misaligned pointer dereference: address must be a multiple of {:?} but is {:?}", - required, found - ) + add!("required", format!("{required:#?}")); + add!("found", format!("{found:#?}")); } - _ => write!(f, "{}", self.description()), } } } diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index f31b343c947..1511e255235 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -291,10 +291,12 @@ impl<'tcx> CodegenUnit<'tcx> { self.primary = true; } + /// The order of these items is non-determinstic. pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> { &self.items } + /// The order of these items is non-determinstic. pub fn items_mut(&mut self) -> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> { &mut self.items } diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 62c3d8cf239..8477722ea39 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -846,7 +846,7 @@ fn write_allocation_newline( /// The `prefix` argument allows callers to add an arbitrary prefix before each line (even if there /// is only one line). Note that your prefix should contain a trailing space as the lines are /// printed directly after it. -fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( +pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( tcx: TyCtxt<'tcx>, alloc: &Allocation<Prov, Extra, Bytes>, w: &mut dyn std::fmt::Write, diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 3e474c1d377..1a65f74f4fe 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -801,7 +801,8 @@ pub enum UnwindAction { } /// Information about an assertion failure. -#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, TypeFoldable, TypeVisitable)] +#[derive(Clone, Hash, HashStable, PartialEq, Debug)] +#[derive(TyEncodable, TyDecodable, TypeFoldable, TypeVisitable)] pub enum AssertKind<O> { BoundsCheck { len: O, index: O }, Overflow(BinOp, O, O), @@ -1272,13 +1273,18 @@ pub enum BinOp { Mul, /// The `/` operator (division) /// - /// Division by zero is UB, because the compiler should have inserted checks - /// prior to this. + /// For integer types, division by zero is UB, as is `MIN / -1` for signed. + /// The compiler should have inserted checks prior to this. + /// + /// Floating-point division by zero is safe, and does not need guards. Div, /// The `%` operator (modulus) /// - /// Using zero as the modulus (second operand) is UB, because the compiler - /// should have inserted checks prior to this. + /// For integer types, using zero as the modulus (second operand) is UB, + /// as is `MIN % -1` for signed. + /// The compiler should have inserted checks prior to this. + /// + /// Floating-point remainder by zero is safe, and does not need guards. Rem, /// The `^` operator (bitwise xor) BitXor, diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 0b31c9bbf81..45e0b498fac 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1475,8 +1475,9 @@ rustc_queries! { desc { "getting traits in scope at a block" } } - query impl_defaultness(def_id: DefId) -> hir::Defaultness { - desc { |tcx| "looking up whether `{}` is a default impl", tcx.def_path_str(def_id) } + /// Returns whether the impl or associated function has the `default` keyword. + query defaultness(def_id: DefId) -> hir::Defaultness { + desc { |tcx| "looking up whether `{}` has `default`", tcx.def_path_str(def_id) } separate_provide_extern feedable } diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 0a903a76974..bf3872e81d4 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -445,6 +445,9 @@ pub enum ObligationCauseCode<'tcx> { /// Obligations to prove that a `std::ops::Drop` impl is not stronger than /// the ADT it's being implemented for. DropImpl, + + /// Requirement for a `const N: Ty` to implement `Ty: ConstParamTy` + ConstParam(Ty<'tcx>), } /// The 'location' at which we try to perform HIR-based wf checking. diff --git a/compiler/rustc_middle/src/traits/specialization_graph.rs b/compiler/rustc_middle/src/traits/specialization_graph.rs index c016f722750..dc2cd203560 100644 --- a/compiler/rustc_middle/src/traits/specialization_graph.rs +++ b/compiler/rustc_middle/src/traits/specialization_graph.rs @@ -228,7 +228,7 @@ impl<'tcx> Ancestors<'tcx> { if let Some(item) = node.item(tcx, trait_item_def_id) { if finalizing_node.is_none() { let is_specializable = item.defaultness(tcx).is_default() - || tcx.impl_defaultness(node.def_id()).is_default(); + || tcx.defaultness(node.def_id()).is_default(); if !is_specializable { finalizing_node = Some(node); diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index 090b769323a..cce609c261e 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -48,7 +48,7 @@ impl AssocItem { /// /// [`type_of`]: crate::ty::TyCtxt::type_of pub fn defaultness(&self, tcx: TyCtxt<'_>) -> hir::Defaultness { - tcx.impl_defaultness(self.def_id) + tcx.defaultness(self.def_id) } #[inline] diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index d1dbc531edf..1e43fab457e 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -1,5 +1,6 @@ use rustc_apfloat::ieee::{Double, Single}; use rustc_apfloat::Float; +use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_target::abi::Size; use std::fmt; @@ -113,6 +114,14 @@ impl std::fmt::Debug for ConstInt { } } +impl IntoDiagnosticArg for ConstInt { + // FIXME this simply uses the Debug impl, but we could probably do better by converting both + // to an inherent method that returns `Cow`. + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(format!("{self:?}").into()) + } +} + /// The raw bytes of a simple value. /// /// This is a packed struct in order to allow this type to be optimally embedded in enums diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index b05e791211d..bf6f21968d7 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -478,6 +478,17 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> { /// [rustc dev guide] for more details. /// /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/ty.html +/// +/// An implementation detail: `TyCtxt` is a wrapper type for [GlobalCtxt], +/// which is the struct that actually holds all the data. `TyCtxt` derefs to +/// `GlobalCtxt`, and in practice `TyCtxt` is passed around everywhere, and all +/// operations are done via `TyCtxt`. A `TyCtxt` is obtained for a `GlobalCtxt` +/// by calling `enter` with a closure `f`. That function creates both the +/// `TyCtxt`, and an `ImplicitCtxt` around it that is put into TLS. Within `f`: +/// - The `ImplicitCtxt` is available implicitly via TLS. +/// - The `TyCtxt` is available explicitly via the `tcx` parameter, and also +/// implicitly within the `ImplicitCtxt`. Explicit access is preferred when +/// possible. #[derive(Copy, Clone)] #[rustc_diagnostic_item = "TyCtxt"] #[rustc_pass_by_value] @@ -493,6 +504,7 @@ impl<'tcx> Deref for TyCtxt<'tcx> { } } +/// See [TyCtxt] for details about this type. pub struct GlobalCtxt<'tcx> { pub arena: &'tcx WorkerLocal<Arena<'tcx>>, pub hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>, @@ -1003,15 +1015,6 @@ impl<'tcx> TyCtxt<'tcx> { self.query_system.on_disk_cache.as_ref().map_or(Ok(0), |c| c.serialize(self, encoder)) } - /// If `true`, we should use lazy normalization for constants, otherwise - /// we still evaluate them eagerly. - #[inline] - pub fn lazy_normalization(self) -> bool { - let features = self.features(); - // Note: We only use lazy normalization for generic const expressions. - features.generic_const_exprs - } - #[inline] pub fn local_crate_exports_generics(self) -> bool { debug_assert!(self.sess.opts.share_generics()); @@ -1872,18 +1875,28 @@ impl<'tcx> TyCtxt<'tcx> { } #[inline] - pub fn mk_closure(self, closure_id: DefId, closure_substs: SubstsRef<'tcx>) -> Ty<'tcx> { - self.mk_ty_from_kind(Closure(closure_id, closure_substs)) + pub fn mk_closure(self, def_id: DefId, closure_substs: SubstsRef<'tcx>) -> Ty<'tcx> { + debug_assert_eq!( + closure_substs.len(), + self.generics_of(self.typeck_root_def_id(def_id)).count() + 3, + "closure constructed with incorrect substitutions" + ); + self.mk_ty_from_kind(Closure(def_id, closure_substs)) } #[inline] pub fn mk_generator( self, - id: DefId, + def_id: DefId, generator_substs: SubstsRef<'tcx>, movability: hir::Movability, ) -> Ty<'tcx> { - self.mk_ty_from_kind(Generator(id, generator_substs, movability)) + debug_assert_eq!( + generator_substs.len(), + self.generics_of(self.typeck_root_def_id(def_id)).count() + 5, + "generator constructed with incorrect number of substitutions" + ); + self.mk_ty_from_kind(Generator(def_id, generator_substs, movability)) } #[inline] diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index b5a743cfe34..c5a306fdf1f 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -1,8 +1,9 @@ -use crate::fluent_generated as fluent; +use crate::error::UnsupportedFnAbi; use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags; use crate::query::TyCtxtAt; use crate::ty::normalize_erasing_regions::NormalizationError; use crate::ty::{self, ReprOptions, Ty, TyCtxt, TypeVisitableExt}; +use rustc_error_messages::DiagnosticMessage; use rustc_errors::{DiagnosticBuilder, Handler, IntoDiagnostic}; use rustc_hir as hir; use rustc_hir::def_id::DefId; @@ -14,7 +15,7 @@ use rustc_target::abi::call::FnAbi; use rustc_target::abi::*; use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target}; -use std::cmp::{self}; +use std::cmp; use std::fmt; use std::num::NonZeroUsize; use std::ops::Bound; @@ -214,29 +215,29 @@ pub enum LayoutError<'tcx> { Cycle, } -impl IntoDiagnostic<'_, !> for LayoutError<'_> { - fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { - let mut diag = handler.struct_fatal(""); +impl<'tcx> LayoutError<'tcx> { + pub fn diagnostic_message(&self) -> DiagnosticMessage { + use crate::fluent_generated::*; + use LayoutError::*; + match self { + Unknown(_) => middle_unknown_layout, + SizeOverflow(_) => middle_values_too_big, + NormalizationFailure(_, _) => middle_cannot_be_normalized, + Cycle => middle_cycle, + } + } + pub fn into_diagnostic(self) -> crate::error::LayoutError<'tcx> { + use crate::error::LayoutError as E; + use LayoutError::*; match self { - LayoutError::Unknown(ty) => { - diag.set_arg("ty", ty); - diag.set_primary_message(fluent::middle_unknown_layout); - } - LayoutError::SizeOverflow(ty) => { - diag.set_arg("ty", ty); - diag.set_primary_message(fluent::middle_values_too_big); - } - LayoutError::NormalizationFailure(ty, e) => { - diag.set_arg("ty", ty); - diag.set_arg("failure_ty", e.get_type_for_failure()); - diag.set_primary_message(fluent::middle_cannot_be_normalized); - } - LayoutError::Cycle => { - diag.set_primary_message(fluent::middle_cycle); + Unknown(ty) => E::Unknown { ty }, + SizeOverflow(ty) => E::Overflow { ty }, + NormalizationFailure(ty, e) => { + E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() } } + Cycle => E::Cycle, } - diag } } @@ -330,11 +331,8 @@ impl<'tcx> SizeSkeleton<'tcx> { Ok(SizeSkeleton::Pointer { non_zero, tail: tcx.erase_regions(tail) }) } _ => bug!( - "SizeSkeleton::compute({}): layout errored ({}), yet \ - tail `{}` is not a type parameter or a projection", - ty, - err, - tail + "SizeSkeleton::compute({ty}): layout errored ({err:?}), yet \ + tail `{tail}` is not a type parameter or a projection", ), } } @@ -940,12 +938,8 @@ where TyMaybeWithLayout::Ty(field_ty) => { cx.tcx().layout_of(cx.param_env().and(field_ty)).unwrap_or_else(|e| { bug!( - "failed to get layout for `{}`: {},\n\ - despite it being a field (#{}) of an existing layout: {:#?}", - field_ty, - e, - i, - this + "failed to get layout for `{field_ty}`: {e:?},\n\ + despite it being a field (#{i}) of an existing layout: {this:#?}", ) }) } @@ -1262,21 +1256,18 @@ impl From<call::AdjustForForeignAbiError> for FnAbiError<'_> { } } -impl<'tcx> fmt::Display for FnAbiError<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +impl<'a, 'b> IntoDiagnostic<'a, !> for FnAbiError<'b> { + fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, !> { match self { - Self::Layout(err) => err.fmt(f), - Self::AdjustForForeignAbi(err) => err.fmt(f), + Self::Layout(e) => e.into_diagnostic().into_diagnostic(handler), + Self::AdjustForForeignAbi(call::AdjustForForeignAbiError::Unsupported { + arch, + abi, + }) => UnsupportedFnAbi { arch, abi: abi.name() }.into_diagnostic(handler), } } } -impl IntoDiagnostic<'_, !> for FnAbiError<'_> { - fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder<'_, !> { - handler.struct_fatal(self.to_string()) - } -} - // FIXME(eddyb) maybe use something like this for an unified `fn_abi_of`, not // just for error handling. #[derive(Debug)] diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index b9b1cd73a8b..443791d0af4 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -73,7 +73,7 @@ pub(super) fn vtable_allocation_provider<'tcx>( let ptr_align = tcx.data_layout.pointer_align.abi; let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap(); - let mut vtable = Allocation::uninit(vtable_size, ptr_align, /* panic_on_fail */ true).unwrap(); + let mut vtable = Allocation::uninit(vtable_size, ptr_align); // No need to do any alignment checks on the memory accesses below, because we know the // allocation is correctly aligned as we created it above. Also we're only offsetting by diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 8f6a069a7db..7f0c1d53f72 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -556,7 +556,7 @@ fn construct_const<'a, 'tcx>( span, .. }) => (*span, ty.span), - Node::AnonConst(_) => { + Node::AnonConst(_) | Node::ConstBlock(_) => { let span = tcx.def_span(def); (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 1bbe7b45c1e..8eeb055ed82 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -582,22 +582,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { /// Converts inline const patterns. fn lower_inline_const( &mut self, - anon_const: &'tcx hir::AnonConst, + block: &'tcx hir::ConstBlock, id: hir::HirId, span: Span, ) -> PatKind<'tcx> { let tcx = self.tcx; - let def_id = anon_const.def_id; - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - let body_id = match tcx.hir().get(hir_id) { - hir::Node::AnonConst(ac) => ac.body, - _ => span_bug!( - tcx.def_span(def_id.to_def_id()), - "from_inline_const can only process anonymous constants" - ), - }; + let def_id = block.def_id; + let body_id = block.body; let expr = &tcx.hir().body(body_id).value; - let ty = tcx.typeck(def_id).node_type(hir_id); + let ty = tcx.typeck(def_id).node_type(block.hir_id); // Special case inline consts that are just literals. This is solely // a performance optimization, as we could also just go through the regular diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index 069514d8a3b..2f851cd1eb5 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -421,10 +421,8 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> { intravisit::walk_block(self, block); } - fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) { - if matches!(self.tcx.def_kind(c.def_id), DefKind::InlineConst) { - self.visit_body(self.tcx.hir().body(c.body)) - } + fn visit_inline_const(&mut self, c: &'tcx hir::ConstBlock) { + self.visit_body(self.tcx.hir().body(c.body)) } fn visit_fn( diff --git a/compiler/rustc_mir_transform/src/const_goto.rs b/compiler/rustc_mir_transform/src/const_goto.rs index da101ca7ad2..024bea62098 100644 --- a/compiler/rustc_mir_transform/src/const_goto.rs +++ b/compiler/rustc_mir_transform/src/const_goto.rs @@ -28,7 +28,7 @@ pub struct ConstGoto; impl<'tcx> MirPass<'tcx> for ConstGoto { fn is_enabled(&self, sess: &rustc_session::Session) -> bool { - sess.mir_opt_level() >= 4 + sess.mir_opt_level() >= 2 } fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 1ba1951afde..1d43dbda0aa 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -4,6 +4,7 @@ use either::Right; use rustc_const_eval::const_eval::CheckAlignment; +use rustc_const_eval::ReportErrorExt; use rustc_data_structures::fx::FxHashSet; use rustc_hir::def::DefKind; use rustc_index::bit_set::BitSet; @@ -37,6 +38,7 @@ macro_rules! throw_machine_stop_str { ($($tt:tt)*) => {{ // We make a new local type for it. The type itself does not carry any information, // but its vtable (for the `MachineStopType` trait) does. + #[derive(Debug)] struct Zst; // Printing this type shows the desired string. impl std::fmt::Display for Zst { @@ -44,7 +46,17 @@ macro_rules! throw_machine_stop_str { write!(f, $($tt)*) } } - impl rustc_middle::mir::interpret::MachineStopType for Zst {} + + impl rustc_middle::mir::interpret::MachineStopType for Zst { + fn diagnostic_message(&self) -> rustc_errors::DiagnosticMessage { + self.to_string().into() + } + + fn add_args( + self: Box<Self>, + _: &mut dyn FnMut(std::borrow::Cow<'static, str>, rustc_errors::DiagnosticArgValue<'static>), + ) {} + } throw_machine_stop!(Zst) }}; } @@ -103,7 +115,14 @@ impl<'tcx> MirPass<'tcx> for ConstProp { // That would require a uniform one-def no-mutation analysis // and RPO (or recursing when needing the value of a local). let mut optimization_finder = ConstPropagator::new(body, dummy_body, tcx); - optimization_finder.visit_body(body); + + // Traverse the body in reverse post-order, to ensure that `FullConstProp` locals are + // assigned before being read. + let postorder = body.basic_blocks.postorder().to_vec(); + for bb in postorder.into_iter().rev() { + let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb]; + optimization_finder.visit_basic_block_data(bb, data); + } trace!("ConstProp done for {:?}", def_id); } @@ -367,7 +386,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { op } Err(e) => { - trace!("get_const failed: {}", e); + trace!("get_const failed: {:?}", e.into_kind().debug()); return None; } }; @@ -789,12 +808,6 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> { self.tcx } - fn visit_body(&mut self, body: &mut Body<'tcx>) { - for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() { - self.visit_basic_block_data(bb, data); - } - } - fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) { self.super_operand(operand, location); @@ -885,14 +898,23 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> { } } StatementKind::StorageLive(local) => { - let frame = self.ecx.frame_mut(); - frame.locals[local].value = - LocalValue::Live(interpret::Operand::Immediate(interpret::Immediate::Uninit)); - } - StatementKind::StorageDead(local) => { - let frame = self.ecx.frame_mut(); - frame.locals[local].value = LocalValue::Dead; + Self::remove_const(&mut self.ecx, local); } + // We do not need to mark dead locals as such. For `FullConstProp` locals, + // this allows to propagate the single assigned value in this case: + // ``` + // let x = SOME_CONST; + // if a { + // f(copy x); + // StorageDead(x); + // } else { + // g(copy x); + // StorageDead(x); + // } + // ``` + // + // This may propagate a constant where the local would be uninit or dead. + // In both cases, this does not matter, as those reads would be UB anyway. _ => {} } } diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index 0fe49b8a1bb..759650fe4db 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -9,6 +9,7 @@ use rustc_const_eval::interpret::Immediate; use rustc_const_eval::interpret::{ self, InterpCx, InterpResult, LocalValue, MemoryKind, OpTy, Scalar, StackPopCleanup, }; +use rustc_const_eval::ReportErrorExt; use rustc_hir::def::DefKind; use rustc_hir::HirId; use rustc_index::bit_set::BitSet; @@ -232,7 +233,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { op } Err(e) => { - trace!("get_const failed: {}", e); + trace!("get_const failed: {:?}", e.into_kind().debug()); return None; } }; @@ -272,8 +273,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { // dedicated error variants should be introduced instead. assert!( !error.kind().formatted_string(), - "const-prop encountered formatting error: {}", - error + "const-prop encountered formatting error: {error:?}", ); None } diff --git a/compiler/rustc_mir_transform/src/errors.rs b/compiler/rustc_mir_transform/src/errors.rs index 602e40d5131..22f71bb0851 100644 --- a/compiler/rustc_mir_transform/src/errors.rs +++ b/compiler/rustc_mir_transform/src/errors.rs @@ -163,7 +163,14 @@ impl<'a, P: std::fmt::Debug> DecorateLint<'a, ()> for AssertLint<P> { self, diag: &'b mut DiagnosticBuilder<'a, ()>, ) -> &'b mut DiagnosticBuilder<'a, ()> { - diag.span_label(self.span(), format!("{:?}", self.panic())); + let span = self.span(); + let assert_kind = self.panic(); + let message = assert_kind.diagnostic_message(); + assert_kind.add_args(&mut |name, value| { + diag.set_arg(name, value); + }); + diag.span_label(span, message); + diag } @@ -191,7 +198,7 @@ impl<P> AssertLint<P> { AssertLint::ArithmeticOverflow(sp, _) | AssertLint::UnconditionalPanic(sp, _) => *sp, } } - pub fn panic(&self) -> &AssertKind<P> { + pub fn panic(self) -> AssertKind<P> { match self { AssertLint::ArithmeticOverflow(_, p) | AssertLint::UnconditionalPanic(_, p) => p, } diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 55b9f084c39..5487b5987e0 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -145,13 +145,16 @@ impl<'tcx> Inliner<'tcx> { Ok(new_blocks) => { debug!("inlined {}", callsite.callee); self.changed = true; + + self.history.push(callsite.callee.def_id()); + self.process_blocks(caller_body, new_blocks); + self.history.pop(); + inlined_count += 1; if inlined_count == inline_limit { + debug!("inline count reached"); return; } - self.history.push(callsite.callee.def_id()); - self.process_blocks(caller_body, new_blocks); - self.history.pop(); } } } diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 54c138b6fbd..7d9f6c38e36 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -559,10 +559,13 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { // inst combine is after MatchBranchSimplification to clean up Ne(_1, false) &multiple_return_terminators::MultipleReturnTerminators, &instsimplify::InstSimplify, - &separate_const_switch::SeparateConstSwitch, &simplify::SimplifyLocals::BeforeConstProp, ©_prop::CopyProp, &ref_prop::ReferencePropagation, + // Perform `SeparateConstSwitch` after SSA-based analyses, as cloning blocks may + // destroy the SSA property. It should still happen before const-propagation, so the + // latter pass will leverage the created opportunities. + &separate_const_switch::SeparateConstSwitch, &const_prop::ConstProp, &dataflow_const_prop::DataflowConstProp, // diff --git a/compiler/rustc_mir_transform/src/lower_intrinsics.rs b/compiler/rustc_mir_transform/src/lower_intrinsics.rs index dae01e41e5f..3a7d58f7125 100644 --- a/compiler/rustc_mir_transform/src/lower_intrinsics.rs +++ b/compiler/rustc_mir_transform/src/lower_intrinsics.rs @@ -82,30 +82,35 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics { drop(args); terminator.kind = TerminatorKind::Goto { target }; } - sym::wrapping_add | sym::wrapping_sub | sym::wrapping_mul => { - if let Some(target) = *target { - let lhs; - let rhs; - { - let mut args = args.drain(..); - lhs = args.next().unwrap(); - rhs = args.next().unwrap(); - } - let bin_op = match intrinsic_name { - sym::wrapping_add => BinOp::Add, - sym::wrapping_sub => BinOp::Sub, - sym::wrapping_mul => BinOp::Mul, - _ => bug!("unexpected intrinsic"), - }; - block.statements.push(Statement { - source_info: terminator.source_info, - kind: StatementKind::Assign(Box::new(( - *destination, - Rvalue::BinaryOp(bin_op, Box::new((lhs, rhs))), - ))), - }); - terminator.kind = TerminatorKind::Goto { target }; + sym::wrapping_add + | sym::wrapping_sub + | sym::wrapping_mul + | sym::unchecked_div + | sym::unchecked_rem => { + let target = target.unwrap(); + let lhs; + let rhs; + { + let mut args = args.drain(..); + lhs = args.next().unwrap(); + rhs = args.next().unwrap(); } + let bin_op = match intrinsic_name { + sym::wrapping_add => BinOp::Add, + sym::wrapping_sub => BinOp::Sub, + sym::wrapping_mul => BinOp::Mul, + sym::unchecked_div => BinOp::Div, + sym::unchecked_rem => BinOp::Rem, + _ => bug!("unexpected intrinsic"), + }; + block.statements.push(Statement { + source_info: terminator.source_info, + kind: StatementKind::Assign(Box::new(( + *destination, + Rvalue::BinaryOp(bin_op, Box::new((lhs, rhs))), + ))), + }); + terminator.kind = TerminatorKind::Goto { target }; } sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => { if let Some(target) = *target { diff --git a/compiler/rustc_mir_transform/src/separate_const_switch.rs b/compiler/rustc_mir_transform/src/separate_const_switch.rs index 2479856b727..f35a5fb4276 100644 --- a/compiler/rustc_mir_transform/src/separate_const_switch.rs +++ b/compiler/rustc_mir_transform/src/separate_const_switch.rs @@ -46,7 +46,7 @@ pub struct SeparateConstSwitch; impl<'tcx> MirPass<'tcx> for SeparateConstSwitch { fn is_enabled(&self, sess: &rustc_session::Session) -> bool { - sess.mir_opt_level() >= 4 + sess.mir_opt_level() >= 2 } fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { diff --git a/compiler/rustc_mir_transform/src/sroa.rs b/compiler/rustc_mir_transform/src/sroa.rs index 2d77291293d..e4b3b8b9262 100644 --- a/compiler/rustc_mir_transform/src/sroa.rs +++ b/compiler/rustc_mir_transform/src/sroa.rs @@ -6,23 +6,29 @@ use rustc_middle::mir::visit::*; use rustc_middle::mir::*; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_mir_dataflow::value_analysis::{excluded_locals, iter_fields}; -use rustc_target::abi::FieldIdx; +use rustc_target::abi::{FieldIdx, ReprFlags, FIRST_VARIANT}; pub struct ScalarReplacementOfAggregates; impl<'tcx> MirPass<'tcx> for ScalarReplacementOfAggregates { fn is_enabled(&self, sess: &rustc_session::Session) -> bool { - sess.mir_opt_level() >= 3 + sess.mir_opt_level() >= 2 } #[instrument(level = "debug", skip(self, tcx, body))] fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { debug!(def_id = ?body.source.def_id()); + + // Avoid query cycles (generators require optimized MIR for layout). + if tcx.type_of(body.source.def_id()).subst_identity().is_generator() { + return; + } + let mut excluded = excluded_locals(body); let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id()); loop { debug!(?excluded); - let escaping = escaping_locals(&excluded, body); + let escaping = escaping_locals(tcx, param_env, &excluded, body); debug!(?escaping); let replacements = compute_flattening(tcx, param_env, body, escaping); debug!(?replacements); @@ -48,11 +54,45 @@ impl<'tcx> MirPass<'tcx> for ScalarReplacementOfAggregates { /// - the locals is a union or an enum; /// - the local's address is taken, and thus the relative addresses of the fields are observable to /// client code. -fn escaping_locals(excluded: &BitSet<Local>, body: &Body<'_>) -> BitSet<Local> { +fn escaping_locals<'tcx>( + tcx: TyCtxt<'tcx>, + param_env: ty::ParamEnv<'tcx>, + excluded: &BitSet<Local>, + body: &Body<'tcx>, +) -> BitSet<Local> { + let is_excluded_ty = |ty: Ty<'tcx>| { + if ty.is_union() || ty.is_enum() { + return true; + } + if let ty::Adt(def, _substs) = ty.kind() { + if def.repr().flags.contains(ReprFlags::IS_SIMD) { + // Exclude #[repr(simd)] types so that they are not de-optimized into an array + return true; + } + // We already excluded unions and enums, so this ADT must have one variant + let variant = def.variant(FIRST_VARIANT); + if variant.fields.len() > 1 { + // If this has more than one field, it cannot be a wrapper that only provides a + // niche, so we do not want to automatically exclude it. + return false; + } + let Ok(layout) = tcx.layout_of(param_env.and(ty)) else { + // We can't get the layout + return true; + }; + if layout.layout.largest_niche().is_some() { + // This type has a niche + return true; + } + } + // Default for non-ADTs + false + }; + let mut set = BitSet::new_empty(body.local_decls.len()); set.insert_range(RETURN_PLACE..=Local::from_usize(body.arg_count)); for (local, decl) in body.local_decls().iter_enumerated() { - if decl.ty.is_union() || decl.ty.is_enum() || excluded.contains(local) { + if excluded.contains(local) || is_excluded_ty(decl.ty) { set.insert(local); } } diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 09025c1ee33..f4ee7b75875 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -35,15 +35,15 @@ //! //! - A "mono item" is something that results in a function or global in //! the LLVM IR of a codegen unit. Mono items do not stand on their -//! own, they can reference other mono items. For example, if function +//! own, they can use other mono items. For example, if function //! `foo()` calls function `bar()` then the mono item for `foo()` -//! references the mono item for function `bar()`. In general, the -//! definition for mono item A referencing a mono item B is that -//! the LLVM artifact produced for A references the LLVM artifact produced +//! uses the mono item for function `bar()`. In general, the +//! definition for mono item A using a mono item B is that +//! the LLVM artifact produced for A uses the LLVM artifact produced //! for B. //! -//! - Mono items and the references between them form a directed graph, -//! where the mono items are the nodes and references form the edges. +//! - Mono items and the uses between them form a directed graph, +//! where the mono items are the nodes and uses form the edges. //! Let's call this graph the "mono item graph". //! //! - The mono item graph for a program contains all mono items @@ -53,12 +53,11 @@ //! mono item graph for the current crate. It runs in two phases: //! //! 1. Discover the roots of the graph by traversing the HIR of the crate. -//! 2. Starting from the roots, find neighboring nodes by inspecting the MIR +//! 2. Starting from the roots, find uses by inspecting the MIR //! representation of the item corresponding to a given node, until no more //! new nodes are found. //! //! ### Discovering roots -//! //! The roots of the mono item graph correspond to the public non-generic //! syntactic items in the source code. We find them by walking the HIR of the //! crate, and whenever we hit upon a public function, method, or static item, @@ -69,25 +68,23 @@ //! specified. Functions marked `#[no_mangle]` and functions called by inlinable //! functions also always act as roots.) //! -//! ### Finding neighbor nodes -//! Given a mono item node, we can discover neighbors by inspecting its -//! MIR. We walk the MIR and any time we hit upon something that signifies a -//! reference to another mono item, we have found a neighbor. Since the -//! mono item we are currently at is always monomorphic, we also know the -//! concrete type arguments of its neighbors, and so all neighbors again will be -//! monomorphic. The specific forms a reference to a neighboring node can take -//! in MIR are quite diverse. Here is an overview: +//! ### Finding uses +//! Given a mono item node, we can discover uses by inspecting its MIR. We walk +//! the MIR to find other mono items used by each mono item. Since the mono +//! item we are currently at is always monomorphic, we also know the concrete +//! type arguments of its used mono items. The specific forms a use can take in +//! MIR are quite diverse. Here is an overview: //! //! #### Calling Functions/Methods -//! The most obvious form of one mono item referencing another is a +//! The most obvious way for one mono item to use another is a //! function or method call (represented by a CALL terminator in MIR). But -//! calls are not the only thing that might introduce a reference between two +//! calls are not the only thing that might introduce a use between two //! function mono items, and as we will see below, they are just a //! specialization of the form described next, and consequently will not get any //! special treatment in the algorithm. //! //! #### Taking a reference to a function or method -//! A function does not need to actually be called in order to be a neighbor of +//! A function does not need to actually be called in order to be used by //! another function. It suffices to just take a reference in order to introduce //! an edge. Consider the following example: //! @@ -109,18 +106,18 @@ //! The MIR of none of these functions will contain an explicit call to //! `print_val::<i32>`. Nonetheless, in order to mono this program, we need //! an instance of this function. Thus, whenever we encounter a function or -//! method in operand position, we treat it as a neighbor of the current +//! method in operand position, we treat it as a use of the current //! mono item. Calls are just a special case of that. //! //! #### Drop glue //! Drop glue mono items are introduced by MIR drop-statements. The -//! generated mono item will again have drop-glue item neighbors if the +//! generated mono item will have additional drop-glue item uses if the //! type to be dropped contains nested values that also need to be dropped. It -//! might also have a function item neighbor for the explicit `Drop::drop` +//! might also have a function item use for the explicit `Drop::drop` //! implementation of its type. //! //! #### Unsizing Casts -//! A subtle way of introducing neighbor edges is by casting to a trait object. +//! A subtle way of introducing use edges is by casting to a trait object. //! Since the resulting fat-pointer contains a reference to a vtable, we need to //! instantiate all object-safe methods of the trait, as we need to store //! pointers to these functions even if they never get called anywhere. This can @@ -151,7 +148,7 @@ //! Mono item collection can be performed in one of two modes: //! //! - Lazy mode means that items will only be instantiated when actually -//! referenced. The goal is to produce the least amount of machine code +//! used. The goal is to produce the least amount of machine code //! possible. //! //! - Eager mode is meant to be used in conjunction with incremental compilation @@ -198,7 +195,6 @@ use rustc_session::lint::builtin::LARGE_ASSIGNMENTS; use rustc_session::Limit; use rustc_span::source_map::{dummy_spanned, respan, Span, Spanned, DUMMY_SP}; use rustc_target::abi::Size; -use std::ops::Range; use std::path::PathBuf; use crate::errors::{ @@ -211,66 +207,51 @@ pub enum MonoItemCollectionMode { Lazy, } -/// Maps every mono item to all mono items it references in its -/// body. -pub struct InliningMap<'tcx> { - // Maps a source mono item to the range of mono items - // accessed by it. - // The range selects elements within the `targets` vecs. - index: FxHashMap<MonoItem<'tcx>, Range<usize>>, - targets: Vec<MonoItem<'tcx>>, +pub struct UsageMap<'tcx> { + // Maps every mono item to the mono items used by it. + used_map: FxHashMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>>, + + // Maps every mono item to the mono items that use it. + user_map: FxHashMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>>, } type MonoItems<'tcx> = Vec<Spanned<MonoItem<'tcx>>>; -impl<'tcx> InliningMap<'tcx> { - fn new() -> InliningMap<'tcx> { - InliningMap { index: FxHashMap::default(), targets: Vec::new() } +impl<'tcx> UsageMap<'tcx> { + fn new() -> UsageMap<'tcx> { + UsageMap { used_map: FxHashMap::default(), user_map: FxHashMap::default() } } - fn record_accesses<'a>( + fn record_used<'a>( &mut self, - source: MonoItem<'tcx>, - new_targets: &'a [Spanned<MonoItem<'tcx>>], + user_item: MonoItem<'tcx>, + used_items: &'a [Spanned<MonoItem<'tcx>>], ) where 'tcx: 'a, { - let start_index = self.targets.len(); - let new_items_count = new_targets.len(); - - self.targets.reserve(new_items_count); - - for Spanned { node: mono_item, .. } in new_targets.into_iter() { - self.targets.push(*mono_item); + let used_items: Vec<_> = used_items.iter().map(|item| item.node).collect(); + for &used_item in used_items.iter() { + self.user_map.entry(used_item).or_default().push(user_item); } - let end_index = self.targets.len(); - assert!(self.index.insert(source, start_index..end_index).is_none()); + assert!(self.used_map.insert(user_item, used_items).is_none()); } - /// Internally iterate over all items referenced by `source` which will be - /// made available for inlining. - pub fn with_inlining_candidates<F>(&self, tcx: TyCtxt<'tcx>, source: MonoItem<'tcx>, mut f: F) - where - F: FnMut(MonoItem<'tcx>), - { - if let Some(range) = self.index.get(&source) { - for candidate in self.targets[range.clone()].iter() { - let is_inlined = candidate.instantiation_mode(tcx) == InstantiationMode::LocalCopy; - if is_inlined { - f(*candidate); - } - } - } + pub fn get_user_items(&self, item: MonoItem<'tcx>) -> Option<&[MonoItem<'tcx>]> { + self.user_map.get(&item).map(|items| items.as_slice()) } - /// Internally iterate over all items and the things each accesses. - pub fn iter_accesses<F>(&self, mut f: F) + /// Internally iterate over all inlined items used by `item`. + pub fn for_each_inlined_used_item<F>(&self, tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>, mut f: F) where - F: FnMut(MonoItem<'tcx>, &[MonoItem<'tcx>]), + F: FnMut(MonoItem<'tcx>), { - for (&accessor, range) in &self.index { - f(accessor, &self.targets[range.clone()]) + let used_items = self.used_map.get(&item).unwrap(); + for used_item in used_items.iter() { + let is_inlined = used_item.instantiation_mode(tcx) == InstantiationMode::LocalCopy; + if is_inlined { + f(*used_item); + } } } } @@ -279,7 +260,7 @@ impl<'tcx> InliningMap<'tcx> { pub fn collect_crate_mono_items( tcx: TyCtxt<'_>, mode: MonoItemCollectionMode, -) -> (FxHashSet<MonoItem<'_>>, InliningMap<'_>) { +) -> (FxHashSet<MonoItem<'_>>, UsageMap<'_>) { let _prof_timer = tcx.prof.generic_activity("monomorphization_collector"); let roots = @@ -288,12 +269,12 @@ pub fn collect_crate_mono_items( debug!("building mono item graph, beginning at roots"); let mut visited = MTLock::new(FxHashSet::default()); - let mut inlining_map = MTLock::new(InliningMap::new()); + let mut usage_map = MTLock::new(UsageMap::new()); let recursion_limit = tcx.recursion_limit(); { let visited: MTLockRef<'_, _> = &mut visited; - let inlining_map: MTLockRef<'_, _> = &mut inlining_map; + let usage_map: MTLockRef<'_, _> = &mut usage_map; tcx.sess.time("monomorphization_collector_graph_walk", || { par_for_each_in(roots, |root| { @@ -304,13 +285,13 @@ pub fn collect_crate_mono_items( visited, &mut recursion_depths, recursion_limit, - inlining_map, + usage_map, ); }); }); } - (visited.into_inner(), inlining_map.into_inner()) + (visited.into_inner(), usage_map.into_inner()) } // Find all non-generic items by walking the HIR. These items serve as roots to @@ -353,24 +334,23 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem< /// Collect all monomorphized items reachable from `starting_point`, and emit a note diagnostic if a /// post-monomorphization error is encountered during a collection step. -#[instrument(skip(tcx, visited, recursion_depths, recursion_limit, inlining_map), level = "debug")] +#[instrument(skip(tcx, visited, recursion_depths, recursion_limit, usage_map), level = "debug")] fn collect_items_rec<'tcx>( tcx: TyCtxt<'tcx>, - starting_point: Spanned<MonoItem<'tcx>>, + starting_item: Spanned<MonoItem<'tcx>>, visited: MTLockRef<'_, FxHashSet<MonoItem<'tcx>>>, recursion_depths: &mut DefIdMap<usize>, recursion_limit: Limit, - inlining_map: MTLockRef<'_, InliningMap<'tcx>>, + usage_map: MTLockRef<'_, UsageMap<'tcx>>, ) { - if !visited.lock_mut().insert(starting_point.node) { + if !visited.lock_mut().insert(starting_item.node) { // We've been here already, no need to search again. return; } - let mut neighbors = Vec::new(); + let mut used_items = Vec::new(); let recursion_depth_reset; - // // Post-monomorphization errors MVP // // We can encounter errors while monomorphizing an item, but we don't have a good way of @@ -396,7 +376,7 @@ fn collect_items_rec<'tcx>( // FIXME: don't rely on global state, instead bubble up errors. Note: this is very hard to do. let error_count = tcx.sess.diagnostic().err_count(); - match starting_point.node { + match starting_item.node { MonoItem::Static(def_id) => { let instance = Instance::mono(tcx, def_id); @@ -404,19 +384,19 @@ fn collect_items_rec<'tcx>( debug_assert!(should_codegen_locally(tcx, &instance)); let ty = instance.ty(tcx, ty::ParamEnv::reveal_all()); - visit_drop_use(tcx, ty, true, starting_point.span, &mut neighbors); + visit_drop_use(tcx, ty, true, starting_item.span, &mut used_items); recursion_depth_reset = None; if let Ok(alloc) = tcx.eval_static_initializer(def_id) { for &id in alloc.inner().provenance().ptrs().values() { - collect_miri(tcx, id, &mut neighbors); + collect_miri(tcx, id, &mut used_items); } } if tcx.needs_thread_local_shim(def_id) { - neighbors.push(respan( - starting_point.span, + used_items.push(respan( + starting_item.span, MonoItem::Fn(Instance { def: InstanceDef::ThreadLocalShim(def_id), substs: InternalSubsts::empty(), @@ -432,14 +412,14 @@ fn collect_items_rec<'tcx>( recursion_depth_reset = Some(check_recursion_limit( tcx, instance, - starting_point.span, + starting_item.span, recursion_depths, recursion_limit, )); check_type_length_limit(tcx, instance); rustc_data_structures::stack::ensure_sufficient_stack(|| { - collect_neighbours(tcx, instance, &mut neighbors); + collect_used_items(tcx, instance, &mut used_items); }); } MonoItem::GlobalAsm(item_id) => { @@ -457,13 +437,13 @@ fn collect_items_rec<'tcx>( hir::InlineAsmOperand::SymFn { anon_const } => { let fn_ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id); - visit_fn_use(tcx, fn_ty, false, *op_sp, &mut neighbors); + visit_fn_use(tcx, fn_ty, false, *op_sp, &mut used_items); } hir::InlineAsmOperand::SymStatic { path: _, def_id } => { let instance = Instance::mono(tcx, *def_id); if should_codegen_locally(tcx, &instance) { trace!("collecting static {:?}", def_id); - neighbors.push(dummy_spanned(MonoItem::Static(*def_id))); + used_items.push(dummy_spanned(MonoItem::Static(*def_id))); } } hir::InlineAsmOperand::In { .. } @@ -483,19 +463,19 @@ fn collect_items_rec<'tcx>( // Check for PMEs and emit a diagnostic if one happened. To try to show relevant edges of the // mono item graph. if tcx.sess.diagnostic().err_count() > error_count - && starting_point.node.is_generic_fn() - && starting_point.node.is_user_defined() + && starting_item.node.is_generic_fn() + && starting_item.node.is_user_defined() { - let formatted_item = with_no_trimmed_paths!(starting_point.node.to_string()); + let formatted_item = with_no_trimmed_paths!(starting_item.node.to_string()); tcx.sess.emit_note(EncounteredErrorWhileInstantiating { - span: starting_point.span, + span: starting_item.span, formatted_item, }); } - inlining_map.lock_mut().record_accesses(starting_point.node, &neighbors); + usage_map.lock_mut().record_used(starting_item.node, &used_items); - for neighbour in neighbors { - collect_items_rec(tcx, neighbour, visited, recursion_depths, recursion_limit, inlining_map); + for used_item in used_items { + collect_items_rec(tcx, used_item, visited, recursion_depths, recursion_limit, usage_map); } if let Some((def_id, depth)) = recursion_depth_reset { @@ -611,14 +591,14 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) { } } -struct MirNeighborCollector<'a, 'tcx> { +struct MirUsedCollector<'a, 'tcx> { tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, output: &'a mut MonoItems<'tcx>, instance: Instance<'tcx>, } -impl<'a, 'tcx> MirNeighborCollector<'a, 'tcx> { +impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> { pub fn monomorphize<T>(&self, value: T) -> T where T: TypeFoldable<TyCtxt<'tcx>>, @@ -632,7 +612,7 @@ impl<'a, 'tcx> MirNeighborCollector<'a, 'tcx> { } } -impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> { +impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> { fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) { debug!("visiting rvalue {:?}", *rvalue); @@ -1392,13 +1372,13 @@ fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut MonoIte /// Scans the MIR in order to find function calls, closures, and drop-glue. #[instrument(skip(tcx, output), level = "debug")] -fn collect_neighbours<'tcx>( +fn collect_used_items<'tcx>( tcx: TyCtxt<'tcx>, instance: Instance<'tcx>, output: &mut MonoItems<'tcx>, ) { let body = tcx.instance_mir(instance.def); - MirNeighborCollector { tcx, body: &body, output, instance }.visit_body(&body); + MirUsedCollector { tcx, body: &body, output, instance }.visit_body(&body); } #[instrument(skip(tcx, output), level = "debug")] diff --git a/compiler/rustc_monomorphize/src/lib.rs b/compiler/rustc_monomorphize/src/lib.rs index ecc50c3f664..89dadc782f2 100644 --- a/compiler/rustc_monomorphize/src/lib.rs +++ b/compiler/rustc_monomorphize/src/lib.rs @@ -1,4 +1,5 @@ #![feature(array_windows)] +#![feature(is_sorted)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] #![deny(rustc::untranslatable_diagnostic)] diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs index 0bacb58383d..79fcd62bc62 100644 --- a/compiler/rustc_monomorphize/src/partitioning.rs +++ b/compiler/rustc_monomorphize/src/partitioning.rs @@ -115,34 +115,38 @@ use rustc_middle::ty::{self, visit::TypeVisitableExt, InstanceDef, TyCtxt}; use rustc_session::config::{DumpMonoStatsFormat, SwitchWithOptPath}; use rustc_span::symbol::Symbol; -use crate::collector::InliningMap; +use crate::collector::UsageMap; use crate::collector::{self, MonoItemCollectionMode}; use crate::errors::{CouldntDumpMonoStats, SymbolAlreadyDefined, UnknownCguCollectionMode}; struct PartitioningCx<'a, 'tcx> { tcx: TyCtxt<'tcx>, target_cgu_count: usize, - inlining_map: &'a InliningMap<'tcx>, + usage_map: &'a UsageMap<'tcx>, } struct PlacedRootMonoItems<'tcx> { + /// The codegen units, sorted by name to make things deterministic. codegen_units: Vec<CodegenUnit<'tcx>>, + roots: FxHashSet<MonoItem<'tcx>>, internalization_candidates: FxHashSet<MonoItem<'tcx>>, } +// The output CGUs are sorted by name. fn partition<'tcx, I>( tcx: TyCtxt<'tcx>, mono_items: &mut I, max_cgu_count: usize, - inlining_map: &InliningMap<'tcx>, + usage_map: &UsageMap<'tcx>, ) -> Vec<CodegenUnit<'tcx>> where I: Iterator<Item = MonoItem<'tcx>>, { let _prof_timer = tcx.prof.generic_activity("cgu_partitioning"); - let cx = &PartitioningCx { tcx, target_cgu_count: max_cgu_count, inlining_map }; + let cx = &PartitioningCx { tcx, target_cgu_count: max_cgu_count, usage_map }; + // In the first step, we place all regular monomorphizations into their // respective 'home' codegen unit. Regular monomorphizations are all // functions and statics defined in the local crate. @@ -225,8 +229,8 @@ where dead_code_cgu.make_code_coverage_dead_code_cgu(); } - // Finally, sort by codegen unit name, so that we get deterministic results. - codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str())); + // Ensure CGUs are sorted by name, so that we get deterministic results. + assert!(codegen_units.is_sorted_by(|a, b| Some(a.name().as_str().cmp(b.name().as_str())))); debug_dump(tcx, "FINAL", &codegen_units); @@ -301,27 +305,22 @@ where codegen_units.insert(codegen_unit_name, CodegenUnit::new(codegen_unit_name)); } - let codegen_units = codegen_units.into_values().collect(); + let mut codegen_units: Vec<_> = codegen_units.into_values().collect(); + codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str())); + PlacedRootMonoItems { codegen_units, roots, internalization_candidates } } +// This function requires the CGUs to be sorted by name on input, and ensures +// they are sorted by name on return, for deterministic behaviour. fn merge_codegen_units<'tcx>( cx: &PartitioningCx<'_, 'tcx>, codegen_units: &mut Vec<CodegenUnit<'tcx>>, ) { assert!(cx.target_cgu_count >= 1); - // Note that at this point in time the `codegen_units` here may not be - // in a deterministic order (but we know they're deterministically the - // same set). We want this merging to produce a deterministic ordering - // of codegen units from the input. - // - // Due to basically how we've implemented the merging below (merge the - // two smallest into each other) we're sure to start off with a - // deterministic order (sorted by name). This'll mean that if two cgus - // have the same size the stable sort below will keep everything nice - // and deterministic. - codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str())); + // A sorted order here ensures merging is deterministic. + assert!(codegen_units.is_sorted_by(|a, b| Some(a.name().as_str().cmp(b.name().as_str())))); // This map keeps track of what got merged into what. let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> = @@ -400,10 +399,13 @@ fn merge_codegen_units<'tcx>( cgu.set_name(numbered_codegen_unit_name); } } + + // A sorted order here ensures what follows can be deterministic. + codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str())); } /// For symbol internalization, we need to know whether a symbol/mono-item is -/// accessed from outside the codegen unit it is defined in. This type is used +/// used from outside the codegen unit it is defined in. This type is used /// to keep track of that. #[derive(Clone, PartialEq, Eq, Debug)] enum MonoItemPlacement { @@ -420,33 +422,25 @@ fn place_inlined_mono_items<'tcx>( let single_codegen_unit = codegen_units.len() == 1; - for old_codegen_unit in codegen_units.iter_mut() { + for cgu in codegen_units.iter_mut() { // Collect all items that need to be available in this codegen unit. let mut reachable = FxHashSet::default(); - for root in old_codegen_unit.items().keys() { - follow_inlining(cx.tcx, *root, cx.inlining_map, &mut reachable); + for root in cgu.items().keys() { + // Insert the root item itself, plus all inlined items that are + // reachable from it without going via another root item. + reachable.insert(*root); + get_reachable_inlined_items(cx.tcx, *root, cx.usage_map, &mut reachable); } - let mut new_codegen_unit = CodegenUnit::new(old_codegen_unit.name()); - // Add all monomorphizations that are not already there. for mono_item in reachable { - if let Some(linkage) = old_codegen_unit.items().get(&mono_item) { - // This is a root, just copy it over. - new_codegen_unit.items_mut().insert(mono_item, *linkage); - } else { + if !cgu.items().contains_key(&mono_item) { if roots.contains(&mono_item) { - bug!( - "GloballyShared mono-item inlined into other CGU: \ - {:?}", - mono_item - ); + bug!("GloballyShared mono-item inlined into other CGU: {:?}", mono_item); } // This is a CGU-private copy. - new_codegen_unit - .items_mut() - .insert(mono_item, (Linkage::Internal, Visibility::Default)); + cgu.items_mut().insert(mono_item, (Linkage::Internal, Visibility::Default)); } if !single_codegen_unit { @@ -456,39 +450,32 @@ fn place_inlined_mono_items<'tcx>( Entry::Occupied(e) => { let placement = e.into_mut(); debug_assert!(match *placement { - MonoItemPlacement::SingleCgu { cgu_name } => { - cgu_name != new_codegen_unit.name() - } + MonoItemPlacement::SingleCgu { cgu_name } => cgu_name != cgu.name(), MonoItemPlacement::MultipleCgus => true, }); *placement = MonoItemPlacement::MultipleCgus; } Entry::Vacant(e) => { - e.insert(MonoItemPlacement::SingleCgu { - cgu_name: new_codegen_unit.name(), - }); + e.insert(MonoItemPlacement::SingleCgu { cgu_name: cgu.name() }); } } } } - - *old_codegen_unit = new_codegen_unit; } return mono_item_placements; - fn follow_inlining<'tcx>( + fn get_reachable_inlined_items<'tcx>( tcx: TyCtxt<'tcx>, - mono_item: MonoItem<'tcx>, - inlining_map: &InliningMap<'tcx>, + item: MonoItem<'tcx>, + usage_map: &UsageMap<'tcx>, visited: &mut FxHashSet<MonoItem<'tcx>>, ) { - if !visited.insert(mono_item) { - return; - } - - inlining_map.with_inlining_candidates(tcx, mono_item, |target| { - follow_inlining(tcx, target, inlining_map, visited); + usage_map.for_each_inlined_used_item(tcx, item, |inlined_item| { + let is_new = visited.insert(inlined_item); + if is_new { + get_reachable_inlined_items(tcx, inlined_item, usage_map, visited); + } }); } } @@ -502,7 +489,7 @@ fn internalize_symbols<'tcx>( if codegen_units.len() == 1 { // Fast path for when there is only one codegen unit. In this case we // can internalize all candidates, since there is nowhere else they - // could be accessed from. + // could be used from. for cgu in codegen_units { for candidate in &internalization_candidates { cgu.items_mut().insert(*candidate, (Linkage::Internal, Visibility::Default)); @@ -512,45 +499,36 @@ fn internalize_symbols<'tcx>( return; } - // Build a map from every monomorphization to all the monomorphizations that - // reference it. - let mut accessor_map: FxHashMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>> = Default::default(); - cx.inlining_map.iter_accesses(|accessor, accessees| { - for accessee in accessees { - accessor_map.entry(*accessee).or_default().push(accessor); - } - }); - // For each internalization candidates in each codegen unit, check if it is - // accessed from outside its defining codegen unit. + // used from outside its defining codegen unit. for cgu in codegen_units { let home_cgu = MonoItemPlacement::SingleCgu { cgu_name: cgu.name() }; - for (accessee, linkage_and_visibility) in cgu.items_mut() { - if !internalization_candidates.contains(accessee) { + for (item, linkage_and_visibility) in cgu.items_mut() { + if !internalization_candidates.contains(item) { // This item is no candidate for internalizing, so skip it. continue; } - debug_assert_eq!(mono_item_placements[accessee], home_cgu); + debug_assert_eq!(mono_item_placements[item], home_cgu); - if let Some(accessors) = accessor_map.get(accessee) { - if accessors + if let Some(user_items) = cx.usage_map.get_user_items(*item) { + if user_items .iter() - .filter_map(|accessor| { - // Some accessors might not have been + .filter_map(|user_item| { + // Some user mono items might not have been // instantiated. We can safely ignore those. - mono_item_placements.get(accessor) + mono_item_placements.get(user_item) }) .any(|placement| *placement != home_cgu) { - // Found an accessor from another CGU, so skip to the next - // item without marking this one as internal. + // Found a user from another CGU, so skip to the next item + // without marking this one as internal. continue; } } - // If we got here, we did not find any accesses from other CGUs, - // so it's fine to make this monomorphization internal. + // If we got here, we did not find any uses from other CGUs, so + // it's fine to make this monomorphization internal. *linkage_and_visibility = (Linkage::Internal, Visibility::Default); } } @@ -786,7 +764,7 @@ fn mono_item_visibility<'tcx>( } else { // If this isn't a generic function then we mark this a `Default` if // this is a reachable item, meaning that it's a symbol other crates may - // access when they link to us. + // use when they link to us. if tcx.is_reachable_non_generic(def_id.to_def_id()) { *can_be_internalized = false; debug_assert!(!is_generic); @@ -859,36 +837,46 @@ fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibilit _ => Visibility::Hidden, } } + fn debug_dump<'a, 'tcx: 'a>(tcx: TyCtxt<'tcx>, label: &str, cgus: &[CodegenUnit<'tcx>]) { let dump = move || { use std::fmt::Write; let num_cgus = cgus.len(); - let max = cgus.iter().map(|cgu| cgu.size_estimate()).max().unwrap(); - let min = cgus.iter().map(|cgu| cgu.size_estimate()).min().unwrap(); - let ratio = max as f64 / min as f64; + let num_items: usize = cgus.iter().map(|cgu| cgu.items().len()).sum(); + let total_size: usize = cgus.iter().map(|cgu| cgu.size_estimate()).sum(); + let max_size = cgus.iter().map(|cgu| cgu.size_estimate()).max().unwrap(); + let min_size = cgus.iter().map(|cgu| cgu.size_estimate()).min().unwrap(); + let max_min_size_ratio = max_size as f64 / min_size as f64; let s = &mut String::new(); let _ = writeln!( s, - "{label} ({num_cgus} CodegenUnits, max={max}, min={min}, max/min={ratio:.1}):" + "{label} ({num_items} items, total_size={total_size}; {num_cgus} CGUs, \ + max_size={max_size}, min_size={min_size}, max_size/min_size={max_min_size_ratio:.1}):" ); - for cgu in cgus { - let _ = - writeln!(s, "CodegenUnit {} estimated size {}:", cgu.name(), cgu.size_estimate()); + for (i, cgu) in cgus.iter().enumerate() { + let num_items = cgu.items().len(); + let _ = writeln!( + s, + "- CGU[{i}] {} ({num_items} items, size={}):", + cgu.name(), + cgu.size_estimate() + ); - for (mono_item, linkage) in cgu.items() { - let symbol_name = mono_item.symbol_name(tcx).name; + // The order of `cgu.items()` is non-deterministic; sort it by name + // to give deterministic output. + let mut items: Vec<_> = cgu.items().iter().collect(); + items.sort_by_key(|(item, _)| item.symbol_name(tcx).name); + for (item, linkage) in items { + let symbol_name = item.symbol_name(tcx).name; let symbol_hash_start = symbol_name.rfind('h'); let symbol_hash = symbol_hash_start.map_or("<no hash>", |i| &symbol_name[i..]); + let size = item.size_estimate(tcx); let _ = with_no_trimmed_paths!(writeln!( s, - " - {} [{:?}] [{}] estimated size {}", - mono_item, - linkage, - symbol_hash, - mono_item.size_estimate(tcx) + " - {item} [{linkage:?}] [{symbol_hash}] (size={size})" )); } @@ -956,7 +944,7 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co } }; - let (items, inlining_map) = collector::collect_crate_mono_items(tcx, collection_mode); + let (items, usage_map) = collector::collect_crate_mono_items(tcx, collection_mode); tcx.sess.abort_if_errors(); @@ -967,7 +955,7 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co tcx, &mut items.iter().copied(), tcx.sess.codegen_units(), - &inlining_map, + &usage_map, ); codegen_units[0].make_primary(); &*tcx.arena.alloc_from_iter(codegen_units) diff --git a/compiler/rustc_passes/src/check_const.rs b/compiler/rustc_passes/src/check_const.rs index 2357b0aadef..fc437c429fb 100644 --- a/compiler/rustc_passes/src/check_const.rs +++ b/compiler/rustc_passes/src/check_const.rs @@ -199,6 +199,11 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> { self.recurse_into(kind, None, |this| intravisit::walk_anon_const(this, anon)); } + fn visit_inline_const(&mut self, block: &'tcx hir::ConstBlock) { + let kind = Some(hir::ConstContext::Const); + self.recurse_into(kind, None, |this| intravisit::walk_inline_const(this, block)); + } + fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) { let owner = self.tcx.hir().body_owner_def_id(body.id()); let kind = self.tcx.hir().body_const_context(owner); diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 7812dcde44c..d5ac1cd9ce3 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -500,6 +500,17 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { self.in_pat = in_pat; } + + fn visit_inline_const(&mut self, c: &'tcx hir::ConstBlock) { + // When inline const blocks are used in pattern position, paths + // referenced by it should be considered as used. + let in_pat = mem::replace(&mut self.in_pat, false); + + self.live_symbols.insert(c.def_id); + intravisit::walk_inline_const(self, c); + + self.in_pat = in_pat; + } } fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index 5a1ae808e66..9971bdf45bb 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -93,7 +93,7 @@ fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { Err(layout_error) => { tcx.sess.emit_fatal(Spanned { - node: layout_error, + node: layout_error.into_diagnostic(), span: tcx.def_span(item_def_id.to_def_id()), }); } @@ -109,12 +109,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for UnwrapLayoutCx<'tcx> { type LayoutOfResult = TyAndLayout<'tcx>; fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! { - span_bug!( - span, - "`#[rustc_layout(..)]` test resulted in `layout_of({}) = Err({})`", - ty, - err - ); + span_bug!(span, "`#[rustc_layout(..)]` test resulted in `layout_of({ty}) = Err({err})`",); } } diff --git a/compiler/rustc_passes/src/loops.rs b/compiler/rustc_passes/src/loops.rs index 73cfe68e7f2..7c64df6a50e 100644 --- a/compiler/rustc_passes/src/loops.rs +++ b/compiler/rustc_passes/src/loops.rs @@ -24,7 +24,7 @@ enum Context { Closure(Span), AsyncClosure(Span), LabeledBlock, - AnonConst, + Constant, } #[derive(Copy, Clone)] @@ -53,7 +53,11 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> { } fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) { - self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c)); + self.with_context(Constant, |v| intravisit::walk_anon_const(v, c)); + } + + fn visit_inline_const(&mut self, c: &'hir hir::ConstBlock) { + self.with_context(Constant, |v| intravisit::walk_inline_const(v, c)); } fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) { @@ -192,7 +196,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> { AsyncClosure(closure_span) => { self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name }); } - Normal | AnonConst => { + Normal | Constant => { self.sess.emit_err(OutsideLoop { span, name, is_break: name == "break" }); } } diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 65dfdf31e54..0197b99c7db 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -751,7 +751,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { reach.generics().predicates(); if trait_item_ref.kind == AssocItemKind::Type - && !tcx.impl_defaultness(trait_item_ref.id.owner_id).has_value() + && !tcx.defaultness(trait_item_ref.id.owner_id).has_value() { // No type to visit. } else { @@ -1927,7 +1927,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> { let (check_ty, is_assoc_ty) = match assoc_item_kind { AssocItemKind::Const | AssocItemKind::Fn { .. } => (true, false), - AssocItemKind::Type => (self.tcx.impl_defaultness(def_id).has_value(), true), + AssocItemKind::Type => (self.tcx.defaultness(def_id).has_value(), true), }; check.in_assoc_ty = is_assoc_ty; check.generics().predicates(); 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 b245742e533..85825513ce9 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 @@ -501,7 +501,15 @@ fn encode_ty<'tcx>( ty::Array(ty0, len) => { // A<array-length><element-type> let mut s = String::from("A"); - let _ = write!(s, "{}", &len.kind().try_to_scalar().unwrap().to_u64().unwrap()); + let _ = write!( + s, + "{}", + &len.kind() + .try_to_scalar() + .unwrap() + .to_u64() + .unwrap_or_else(|_| panic!("failed to convert length to u64")) + ); s.push_str(&encode_ty(tcx, *ty0, dict, options)); compress(dict, DictKey::Ty(ty, TyQ::None), &mut s); typeid.push_str(&s); @@ -601,9 +609,7 @@ fn encode_ty<'tcx>( } // Function types - ty::FnDef(def_id, substs) - | ty::Closure(def_id, substs) - | ty::Generator(def_id, substs, ..) => { + ty::FnDef(def_id, substs) | ty::Closure(def_id, substs) => { // u<length><name>[I<element-type1..element-typeN>E], where <element-type> is <subst>, // as vendor extended type. let mut s = String::new(); @@ -614,6 +620,23 @@ fn encode_ty<'tcx>( typeid.push_str(&s); } + ty::Generator(def_id, substs, ..) => { + // u<length><name>[I<element-type1..element-typeN>E], where <element-type> is <subst>, + // as vendor extended type. + let mut s = String::new(); + let name = encode_ty_name(tcx, *def_id); + let _ = write!(s, "u{}{}", name.len(), &name); + // Encode parent substs only + s.push_str(&encode_substs( + tcx, + tcx.mk_substs(substs.as_generator().parent_substs()), + dict, + options, + )); + compress(dict, DictKey::Ty(ty, TyQ::None), &mut s); + typeid.push_str(&s); + } + // Pointer types ty::Ref(region, ty0, ..) => { // [U3mut]u3refI<element-type>E as vendor extended type qualifier and type @@ -674,12 +697,12 @@ fn encode_ty<'tcx>( } // Unexpected types - ty::Bound(..) + ty::Alias(..) + | ty::Bound(..) | ty::Error(..) | ty::GeneratorWitness(..) | ty::GeneratorWitnessMIR(..) | ty::Infer(..) - | ty::Alias(..) | ty::Placeholder(..) => { bug!("encode_ty: unexpected `{:?}`", ty.kind()); } @@ -732,7 +755,12 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio let mut ty = ty; match ty.kind() { - ty::Float(..) | ty::Char | ty::Str | ty::Never | ty::Foreign(..) => {} + ty::Float(..) + | ty::Char + | ty::Str + | ty::Never + | ty::Foreign(..) + | ty::GeneratorWitness(..) => {} ty::Bool => { if options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) { @@ -786,7 +814,12 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio } ty::Array(ty0, len) => { - let len = len.kind().try_to_scalar().unwrap().to_u64().unwrap(); + let len = len + .kind() + .try_to_scalar() + .unwrap() + .to_u64() + .unwrap_or_else(|_| panic!("failed to convert length to u64")); ty = tcx.mk_array(transform_ty(tcx, *ty0, options), len); } @@ -913,12 +946,18 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio ); } + ty::Alias(..) => { + ty = transform_ty( + tcx, + tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), ty), + options, + ); + } + ty::Bound(..) | ty::Error(..) - | ty::GeneratorWitness(..) | ty::GeneratorWitnessMIR(..) | ty::Infer(..) - | ty::Alias(..) | ty::Param(..) | ty::Placeholder(..) => { bug!("transform_ty: unexpected `{:?}`", ty.kind()); diff --git a/compiler/rustc_target/src/abi/call/mod.rs b/compiler/rustc_target/src/abi/call/mod.rs index 1ae11f5671c..c4abf6f4b5e 100644 --- a/compiler/rustc_target/src/abi/call/mod.rs +++ b/compiler/rustc_target/src/abi/call/mod.rs @@ -2,7 +2,6 @@ use crate::abi::{self, Abi, Align, FieldsShape, Size}; use crate::abi::{HasDataLayout, TyAbiInterface, TyAndLayout}; use crate::spec::{self, HasTargetSpec}; use rustc_span::Symbol; -use std::fmt; use std::str::FromStr; mod aarch64; @@ -633,16 +632,6 @@ pub enum AdjustForForeignAbiError { Unsupported { arch: Symbol, abi: spec::abi::Abi }, } -impl fmt::Display for AdjustForForeignAbiError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Unsupported { arch, abi } => { - write!(f, "target architecture {arch:?} does not support `extern {abi}` ABI") - } - } - } -} - impl<'a, Ty> FnAbi<'a, Ty> { pub fn adjust_for_foreign_abi<C>( &mut self, diff --git a/compiler/rustc_target/src/abi/call/x86_64.rs b/compiler/rustc_target/src/abi/call/x86_64.rs index 9427f27d1b7..74ef53915c9 100644 --- a/compiler/rustc_target/src/abi/call/x86_64.rs +++ b/compiler/rustc_target/src/abi/call/x86_64.rs @@ -1,5 +1,5 @@ // The classification code for the x86_64 ABI is taken from the clay language -// https://github.com/jckarter/clay/blob/master/compiler/src/externals.cpp +// https://github.com/jckarter/clay/blob/db0bd2702ab0b6e48965cd85f8859bbd5f60e48e/compiler/externals.cpp use crate::abi::call::{ArgAbi, CastTarget, FnAbi, Reg, RegKind}; use crate::abi::{self, Abi, HasDataLayout, Size, TyAbiInterface, TyAndLayout}; diff --git a/compiler/rustc_target/src/spec/x86_64_apple_ios.rs b/compiler/rustc_target/src/spec/x86_64_apple_ios.rs index 1dcb47056a4..061b6a96fc8 100644 --- a/compiler/rustc_target/src/spec/x86_64_apple_ios.rs +++ b/compiler/rustc_target/src/spec/x86_64_apple_ios.rs @@ -13,7 +13,7 @@ pub fn target() -> Target { .into(), arch: arch.target_arch(), options: TargetOptions { - max_atomic_width: Some(64), + max_atomic_width: Some(128), stack_probes: StackProbeType::X86, ..base }, diff --git a/compiler/rustc_target/src/spec/x86_64_apple_ios_macabi.rs b/compiler/rustc_target/src/spec/x86_64_apple_ios_macabi.rs index 9f3b0fab697..50f359c357b 100644 --- a/compiler/rustc_target/src/spec/x86_64_apple_ios_macabi.rs +++ b/compiler/rustc_target/src/spec/x86_64_apple_ios_macabi.rs @@ -15,7 +15,7 @@ pub fn target() -> Target { .into(), arch: arch.target_arch(), options: TargetOptions { - max_atomic_width: Some(64), + max_atomic_width: Some(128), stack_probes: StackProbeType::X86, ..base }, diff --git a/compiler/rustc_target/src/spec/x86_64_apple_tvos.rs b/compiler/rustc_target/src/spec/x86_64_apple_tvos.rs index 550ce0b9ce5..76de7d20c4c 100644 --- a/compiler/rustc_target/src/spec/x86_64_apple_tvos.rs +++ b/compiler/rustc_target/src/spec/x86_64_apple_tvos.rs @@ -9,7 +9,7 @@ pub fn target() -> Target { data_layout: "e-m:o-i64:64-f80:128-n8:16:32:64-S128".into(), arch: arch.target_arch(), options: TargetOptions { - max_atomic_width: Some(64), + max_atomic_width: Some(128), stack_probes: StackProbeType::X86, ..opts("tvos", arch) }, diff --git a/compiler/rustc_target/src/spec/x86_64_apple_watchos_sim.rs b/compiler/rustc_target/src/spec/x86_64_apple_watchos_sim.rs index 75ce02cba1d..5fcc00a86ff 100644 --- a/compiler/rustc_target/src/spec/x86_64_apple_watchos_sim.rs +++ b/compiler/rustc_target/src/spec/x86_64_apple_watchos_sim.rs @@ -10,7 +10,7 @@ pub fn target() -> Target { .into(), arch: arch.target_arch(), options: TargetOptions { - max_atomic_width: Some(64), + max_atomic_width: Some(128), stack_probes: StackProbeType::X86, forces_embed_bitcode: true, // Taken from a clang build on Xcode 11.4.1. diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs index f91c6727753..bc93b9e99ad 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs @@ -322,10 +322,13 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> { ty::PredicateKind::Ambiguous => { self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS) } - // FIXME: implement these predicates :) - ty::PredicateKind::ConstEvaluatable(_) | ty::PredicateKind::ConstEquate(_, _) => { + // FIXME: implement this predicate :) + ty::PredicateKind::ConstEvaluatable(_) => { self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) } + ty::PredicateKind::ConstEquate(_, _) => { + bug!("ConstEquate should not be emitted when `-Ztrait-solver=next` is active") + } ty::PredicateKind::TypeWellFormedFromEnv(..) => { bug!("TypeWellFormedFromEnv is only used for Chalk") } @@ -772,4 +775,21 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { } values } + + // Try to evaluate a const, or return `None` if the const is too generic. + // This doesn't mean the const isn't evaluatable, though, and should be treated + // as an ambiguity rather than no-solution. + pub(super) fn try_const_eval_resolve( + &self, + param_env: ty::ParamEnv<'tcx>, + unevaluated: ty::UnevaluatedConst<'tcx>, + ty: Ty<'tcx>, + ) -> Option<ty::Const<'tcx>> { + use rustc_middle::mir::interpret::ErrorHandled; + match self.infcx.try_const_eval_resolve(param_env, unevaluated, ty, None) { + Ok(ct) => Some(ct), + Err(ErrorHandled::Reported(e)) => Some(self.tcx().const_error(ty, e.into())), + Err(ErrorHandled::TooGeneric) => None, + } + } } diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs index 4a403196c7e..212327448c8 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -118,16 +118,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> { TypeError::Sorts(expected_found), ) } - ty::PredicateKind::ConstEquate(a, b) => { - let (a, b) = infcx.instantiate_binder_with_placeholders( - goal.predicate.kind().rebind((a, b)), - ); - let expected_found = ExpectedFound::new(true, a, b); - FulfillmentErrorCode::CodeConstEquateError( - expected_found, - TypeError::ConstMismatch(expected_found), - ) - } ty::PredicateKind::Clause(_) | ty::PredicateKind::WellFormed(_) | ty::PredicateKind::ObjectSafe(_) @@ -138,7 +128,8 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> { SelectionError::Unimplemented, ) } - ty::PredicateKind::TypeWellFormedFromEnv(_) => { + ty::PredicateKind::ConstEquate(..) + | ty::PredicateKind::TypeWellFormedFromEnv(_) => { bug!("unexpected goal: {goal:?}") } }, diff --git a/compiler/rustc_trait_selection/src/solve/project_goals.rs b/compiler/rustc_trait_selection/src/solve/project_goals.rs index 7d7dfa2c837..242f9ba8747 100644 --- a/compiler/rustc_trait_selection/src/solve/project_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/project_goals.rs @@ -22,25 +22,65 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { &mut self, goal: Goal<'tcx, ProjectionPredicate<'tcx>>, ) -> QueryResult<'tcx> { - match goal.predicate.projection_ty.kind(self.tcx()) { - ty::AliasKind::Projection => { + let def_id = goal.predicate.def_id(); + match self.tcx().def_kind(def_id) { + DefKind::AssocTy | DefKind::AssocConst => { // To only compute normalization once for each projection we only - // normalize if the expected term is an unconstrained inference variable. + // assemble normalization candidates if the expected term is an + // unconstrained inference variable. + // + // Why: For better cache hits, since if we have an unconstrained RHS then + // there are only as many cache keys as there are (canonicalized) alias + // types in each normalizes-to goal. This also weakens inference in a + // forwards-compatible way so we don't use the value of the RHS term to + // affect candidate assembly for projections. // // E.g. for `<T as Trait>::Assoc == u32` we recursively compute the goal // `exists<U> <T as Trait>::Assoc == U` and then take the resulting type for // `U` and equate it with `u32`. This means that we don't need a separate - // projection cache in the solver. + // projection cache in the solver, since we're piggybacking off of regular + // goal caching. if self.term_is_fully_unconstrained(goal) { - let candidates = self.assemble_and_evaluate_candidates(goal); - self.merge_candidates(candidates) + match self.tcx().associated_item(def_id).container { + ty::AssocItemContainer::TraitContainer => { + let candidates = self.assemble_and_evaluate_candidates(goal); + self.merge_candidates(candidates) + } + ty::AssocItemContainer::ImplContainer => { + bug!("IATs not supported here yet") + } + } } else { self.set_normalizes_to_hack_goal(goal); self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) } } - ty::AliasKind::Opaque => self.normalize_opaque_type(goal), - ty::AliasKind::Inherent => bug!("IATs not supported here yet"), + DefKind::AnonConst => self.normalize_anon_const(goal), + DefKind::OpaqueTy => self.normalize_opaque_type(goal), + kind => bug!("unknown DefKind {} in projection goal: {goal:#?}", kind.descr(def_id)), + } + } + + #[instrument(level = "debug", skip(self), ret)] + fn normalize_anon_const( + &mut self, + goal: Goal<'tcx, ty::ProjectionPredicate<'tcx>>, + ) -> QueryResult<'tcx> { + if let Some(normalized_const) = self.try_const_eval_resolve( + goal.param_env, + ty::UnevaluatedConst::new( + goal.predicate.projection_ty.def_id, + goal.predicate.projection_ty.substs, + ), + self.tcx() + .type_of(goal.predicate.projection_ty.def_id) + .no_bound_vars() + .expect("const ty should not rely on other generics"), + ) { + self.eq(goal.param_env, normalized_const, goal.predicate.term.ct().unwrap())?; + self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) + } else { + self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS) } } } @@ -173,17 +213,10 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> { ); // 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.type_of(assoc_def.item.def_id); - let term: ty::EarlyBinder<ty::Term<'tcx>> = if is_const { - let identity_substs = - ty::InternalSubsts::identity_for_item(tcx, assoc_def.item.def_id); - let did = 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 term = match assoc_def.item.kind { + ty::AssocKind::Type => tcx.type_of(assoc_def.item.def_id).map_bound(|ty| ty.into()), + ty::AssocKind::Const => bug!("associated const projection is not supported yet"), + ty::AssocKind::Fn => unreachable!("we should never project to a fn"), }; ecx.eq(goal.param_env, goal.predicate.term, term.subst(tcx, substs)) @@ -193,10 +226,14 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> { } fn consider_auto_trait_candidate( - _ecx: &mut EvalCtxt<'_, 'tcx>, + ecx: &mut EvalCtxt<'_, 'tcx>, goal: Goal<'tcx, Self>, ) -> QueryResult<'tcx> { - bug!("auto traits do not have associated types: {:?}", goal); + ecx.tcx().sess.delay_span_bug( + ecx.tcx().def_span(goal.predicate.def_id()), + "associated types not allowed on auto traits", + ); + Err(NoSolution) } fn consider_trait_alias_candidate( 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 1470dc452a1..01c74be7057 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -149,6 +149,12 @@ pub trait TypeErrCtxtExt<'tcx> { root_obligation: &PredicateObligation<'tcx>, error: &SelectionError<'tcx>, ); + + fn report_const_param_not_wf( + &self, + ty: Ty<'tcx>, + obligation: &PredicateObligation<'tcx>, + ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>; } impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { @@ -641,6 +647,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { span = obligation.cause.span; } } + if let ObligationCauseCode::CompareImplItemObligation { impl_item_def_id, trait_item_def_id, @@ -657,6 +664,13 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { return; } + // Report a const-param specific error + if let ObligationCauseCode::ConstParam(ty) = *obligation.cause.code().peel_derives() + { + self.report_const_param_not_wf(ty, &obligation).emit(); + return; + } + let bound_predicate = obligation.predicate.kind(); match bound_predicate.skip_binder() { ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => { @@ -1163,6 +1177,102 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { self.point_at_returns_when_relevant(&mut err, &obligation); err.emit(); } + + fn report_const_param_not_wf( + &self, + ty: Ty<'tcx>, + obligation: &PredicateObligation<'tcx>, + ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { + let span = obligation.cause.span; + + let mut diag = match ty.kind() { + _ if ty.has_param() => { + span_bug!(span, "const param tys cannot mention other generic parameters"); + } + ty::Float(_) => { + struct_span_err!( + self.tcx.sess, + span, + E0741, + "`{ty}` is forbidden as the type of a const generic parameter", + ) + } + ty::FnPtr(_) => { + struct_span_err!( + self.tcx.sess, + span, + E0741, + "using function pointers as const generic parameters is forbidden", + ) + } + ty::RawPtr(_) => { + struct_span_err!( + self.tcx.sess, + span, + E0741, + "using raw pointers as const generic parameters is forbidden", + ) + } + ty::Adt(def, _) => { + // We should probably see if we're *allowed* to derive `ConstParamTy` on the type... + let mut diag = struct_span_err!( + self.tcx.sess, + span, + E0741, + "`{ty}` must implement `ConstParamTy` to be used as the type of a const generic parameter", + ); + // Only suggest derive if this isn't a derived obligation, + // and the struct is local. + if let Some(span) = self.tcx.hir().span_if_local(def.did()) + && obligation.cause.code().parent().is_none() + { + if ty.is_structural_eq_shallow(self.tcx) { + diag.span_suggestion( + span, + "add `#[derive(ConstParamTy)]` to the struct", + "#[derive(ConstParamTy)]\n", + Applicability::MachineApplicable, + ); + } else { + // FIXME(adt_const_params): We should check there's not already an + // overlapping `Eq`/`PartialEq` impl. + diag.span_suggestion( + span, + "add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct", + "#[derive(ConstParamTy, PartialEq, Eq)]\n", + Applicability::MachineApplicable, + ); + } + } + diag + } + _ => { + struct_span_err!( + self.tcx.sess, + span, + E0741, + "`{ty}` can't be used as a const parameter type", + ) + } + }; + + let mut code = obligation.cause.code(); + let mut pred = obligation.predicate.to_opt_poly_trait_pred(); + while let Some((next_code, next_pred)) = code.parent() { + if let Some(pred) = pred { + let pred = self.instantiate_binder_with_placeholders(pred); + diag.note(format!( + "`{}` must implement `{}`, but it does not", + pred.self_ty(), + pred.print_modifiers_and_trait_path() + )); + } + code = next_code; + pred = next_pred; + } + + diag + } } trait InferCtxtPrivExt<'tcx> { 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 b5b8c7fe3ac..42038dbc3d8 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_errors::{ error_code, pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, - ErrorGuaranteed, MultiSpan, Style, + ErrorGuaranteed, MultiSpan, Style, SuggestionStyle, }; use rustc_hir as hir; use rustc_hir::def::DefKind; @@ -362,6 +362,15 @@ pub trait TypeErrCtxtExt<'tcx> { err: &mut Diagnostic, trait_pred: ty::PolyTraitPredicate<'tcx>, ); + + fn suggest_option_method_if_applicable( + &self, + failed_pred: ty::Predicate<'tcx>, + param_env: ty::ParamEnv<'tcx>, + err: &mut Diagnostic, + expr: &hir::Expr<'_>, + ); + fn note_function_argument_obligation( &self, body_id: LocalDefId, @@ -2655,7 +2664,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { | ObligationCauseCode::BinOp { .. } | ObligationCauseCode::AscribeUserTypeProvePredicate(..) | ObligationCauseCode::RustCall - | ObligationCauseCode::DropImpl => {} + | ObligationCauseCode::DropImpl + | ObligationCauseCode::ConstParam(_) => {} ObligationCauseCode::SliceOrArrayElem => { err.note("slice and array elements must have `Sized` type"); } @@ -3520,15 +3530,92 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { err.replace_span_with(path.ident.span, true); } } - if let Some(Node::Expr(hir::Expr { - kind: - hir::ExprKind::Call(hir::Expr { span, .. }, _) - | hir::ExprKind::MethodCall(hir::PathSegment { ident: Ident { span, .. }, .. }, ..), - .. - })) = hir.find(call_hir_id) + + if let Some(Node::Expr(expr)) = hir.find(call_hir_id) { + if let hir::ExprKind::Call(hir::Expr { span, .. }, _) + | hir::ExprKind::MethodCall( + hir::PathSegment { ident: Ident { span, .. }, .. }, + .., + ) = expr.kind + { + if Some(*span) != err.span.primary_span() { + err.span_label(*span, "required by a bound introduced by this call"); + } + } + + if let hir::ExprKind::MethodCall(_, expr, ..) = expr.kind { + self.suggest_option_method_if_applicable(failed_pred, param_env, err, expr); + } + } + } + + fn suggest_option_method_if_applicable( + &self, + failed_pred: ty::Predicate<'tcx>, + param_env: ty::ParamEnv<'tcx>, + err: &mut Diagnostic, + expr: &hir::Expr<'_>, + ) { + let tcx = self.tcx; + let infcx = self.infcx; + let Some(typeck_results) = self.typeck_results.as_ref() else { return }; + + // Make sure we're dealing with the `Option` type. + let Some(option_ty_adt) = typeck_results.expr_ty_adjusted(expr).ty_adt_def() else { return }; + if !tcx.is_diagnostic_item(sym::Option, option_ty_adt.did()) { + return; + } + + // Given the predicate `fn(&T): FnOnce<(U,)>`, extract `fn(&T)` and `(U,)`, + // then suggest `Option::as_deref(_mut)` if `U` can deref to `T` + if let ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate { trait_ref, .. })) + = failed_pred.kind().skip_binder() + && tcx.is_fn_trait(trait_ref.def_id) + && let [self_ty, found_ty] = trait_ref.substs.as_slice() + && let Some(fn_ty) = self_ty.as_type().filter(|ty| ty.is_fn()) + && let fn_sig @ ty::FnSig { + abi: abi::Abi::Rust, + c_variadic: false, + unsafety: hir::Unsafety::Normal, + .. + } = fn_ty.fn_sig(tcx).skip_binder() + + // Extract first param of fn sig with peeled refs, e.g. `fn(&T)` -> `T` + && let Some(&ty::Ref(_, target_ty, needs_mut)) = fn_sig.inputs().first().map(|t| t.kind()) + && !target_ty.has_escaping_bound_vars() + + // Extract first tuple element out of fn trait, e.g. `FnOnce<(U,)>` -> `U` + && let Some(ty::Tuple(tys)) = found_ty.as_type().map(Ty::kind) + && let &[found_ty] = tys.as_slice() + && !found_ty.has_escaping_bound_vars() + + // Extract `<U as Deref>::Target` assoc type and check that it is `T` + && let Some(deref_target_did) = tcx.lang_items().deref_target() + && let projection = tcx.mk_projection(deref_target_did, tcx.mk_substs(&[ty::GenericArg::from(found_ty)])) + && let Ok(deref_target) = tcx.try_normalize_erasing_regions(param_env, projection) + && deref_target == target_ty { - if Some(*span) != err.span.primary_span() { - err.span_label(*span, "required by a bound introduced by this call"); + let help = if let hir::Mutability::Mut = needs_mut + && let Some(deref_mut_did) = tcx.lang_items().deref_mut_trait() + && infcx + .type_implements_trait(deref_mut_did, iter::once(found_ty), param_env) + .must_apply_modulo_regions() + { + Some(("call `Option::as_deref_mut()` first", ".as_deref_mut()")) + } else if let hir::Mutability::Not = needs_mut { + Some(("call `Option::as_deref()` first", ".as_deref()")) + } else { + None + }; + + if let Some((msg, sugg)) = help { + err.span_suggestion_with_style( + expr.span.shrink_to_hi(), + msg, + sugg, + Applicability::MaybeIncorrect, + SuggestionStyle::ShowAlways + ); } } } diff --git a/compiler/rustc_trait_selection/src/traits/misc.rs b/compiler/rustc_trait_selection/src/traits/misc.rs index 2210ef975e6..e9cfd63e2ed 100644 --- a/compiler/rustc_trait_selection/src/traits/misc.rs +++ b/compiler/rustc_trait_selection/src/traits/misc.rs @@ -100,7 +100,8 @@ pub fn type_allowed_to_implement_const_param_ty<'tcx>( | ty::Str | ty::Array(..) | ty::Slice(_) - | ty::Ref(.., hir::Mutability::Not) => return Ok(()), + | ty::Ref(.., hir::Mutability::Not) + | ty::Tuple(_) => return Ok(()), &ty::Adt(adt, substs) => (adt, substs), diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index f7389bda159..c2f94cb6385 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -62,9 +62,7 @@ pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind; pub use self::specialize::{ specialization_graph, translate_substs, translate_substs_with_cause, OverlapError, }; -pub use self::structural_match::{ - search_for_adt_const_param_violation, search_for_structural_match_violation, -}; +pub use self::structural_match::search_for_structural_match_violation; pub use self::structural_normalize::StructurallyNormalizeExt; pub use self::util::elaborate; pub use self::util::{expand_trait_aliases, TraitAliasExpander}; diff --git a/compiler/rustc_trait_selection/src/traits/object_safety.rs b/compiler/rustc_trait_selection/src/traits/object_safety.rs index 9582479941b..048302187cf 100644 --- a/compiler/rustc_trait_selection/src/traits/object_safety.rs +++ b/compiler/rustc_trait_selection/src/traits/object_safety.rs @@ -526,7 +526,7 @@ fn virtual_call_violation_for_method<'tcx>( // #78372 tcx.sess.delay_span_bug( tcx.def_span(method.def_id), - format!("error: {}\n while computing layout for type {:?}", err, ty), + format!("error: {err}\n while computing layout for type {ty:?}"), ); None } diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 65af0bb1c4e..563cc257e03 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -672,7 +672,9 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx #[instrument(skip(self), level = "debug")] fn fold_const(&mut self, constant: ty::Const<'tcx>) -> ty::Const<'tcx> { let tcx = self.selcx.tcx(); - if tcx.lazy_normalization() || !needs_normalization(&constant, self.param_env.reveal()) { + if tcx.features().generic_const_exprs + || !needs_normalization(&constant, self.param_env.reveal()) + { constant } else { let constant = constant.super_fold_with(self); diff --git a/compiler/rustc_trait_selection/src/traits/structural_match.rs b/compiler/rustc_trait_selection/src/traits/structural_match.rs index e38ae9381c1..420f8c5dceb 100644 --- a/compiler/rustc_trait_selection/src/traits/structural_match.rs +++ b/compiler/rustc_trait_selection/src/traits/structural_match.rs @@ -34,24 +34,7 @@ pub fn search_for_structural_match_violation<'tcx>( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, ) -> Option<Ty<'tcx>> { - ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), adt_const_param: false }) - .break_value() -} - -/// This method traverses the structure of `ty`, trying to find any -/// types that are not allowed to be used in a const generic. -/// -/// This is either because the type does not implement `StructuralEq` -/// and `StructuralPartialEq`, or because the type is intentionally -/// not supported in const generics (such as floats and raw pointers, -/// which are allowed in match blocks). -pub fn search_for_adt_const_param_violation<'tcx>( - span: Span, - tcx: TyCtxt<'tcx>, - ty: Ty<'tcx>, -) -> Option<Ty<'tcx>> { - ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), adt_const_param: true }) - .break_value() + ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default() }).break_value() } /// This implements the traversal over the structure of a given type to try to @@ -65,11 +48,6 @@ struct Search<'tcx> { /// Tracks ADTs previously encountered during search, so that /// we will not recur on them again. seen: FxHashSet<hir::def_id::DefId>, - - // Additionally deny things that have been allowed in patterns, - // but are not allowed in adt const params, such as floats and - // fn ptrs. - adt_const_param: bool, } impl<'tcx> Search<'tcx> { @@ -124,41 +102,29 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Search<'tcx> { } ty::FnPtr(..) => { - if !self.adt_const_param { - return ControlFlow::Continue(()); - } else { - return ControlFlow::Break(ty); - } + return ControlFlow::Continue(()); } ty::RawPtr(..) => { - if !self.adt_const_param { - // structural-match ignores substructure of - // `*const _`/`*mut _`, so skip `super_visit_with`. - // - // For example, if you have: - // ``` - // struct NonStructural; - // #[derive(PartialEq, Eq)] - // struct T(*const NonStructural); - // const C: T = T(std::ptr::null()); - // ``` - // - // Even though `NonStructural` does not implement `PartialEq`, - // structural equality on `T` does not recur into the raw - // pointer. Therefore, one can still use `C` in a pattern. - return ControlFlow::Continue(()); - } else { - return ControlFlow::Break(ty); - } + // structural-match ignores substructure of + // `*const _`/`*mut _`, so skip `super_visit_with`. + // + // For example, if you have: + // ``` + // struct NonStructural; + // #[derive(PartialEq, Eq)] + // struct T(*const NonStructural); + // const C: T = T(std::ptr::null()); + // ``` + // + // Even though `NonStructural` does not implement `PartialEq`, + // structural equality on `T` does not recur into the raw + // pointer. Therefore, one can still use `C` in a pattern. + return ControlFlow::Continue(()); } ty::Float(_) => { - if !self.adt_const_param { - return ControlFlow::Continue(()); - } else { - return ControlFlow::Break(ty); - } + return ControlFlow::Continue(()); } ty::Array(..) | ty::Slice(_) | ty::Ref(..) | ty::Tuple(..) => { diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs index e2c9c62512e..537405b7eb9 100644 --- a/compiler/rustc_trait_selection/src/traits/util.rs +++ b/compiler/rustc_trait_selection/src/traits/util.rs @@ -295,7 +295,7 @@ pub fn future_trait_ref_and_outputs<'tcx>( pub fn impl_item_is_final(tcx: TyCtxt<'_>, assoc_item: &ty::AssocItem) -> bool { assoc_item.defaultness(tcx).is_final() - && tcx.impl_defaultness(assoc_item.container_id(tcx)).is_final() + && tcx.defaultness(assoc_item.container_id(tcx)).is_final() } pub enum TupleArgumentsFlag { diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 57b4183d336..658ab03c0f4 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -297,8 +297,8 @@ fn associated_type_for_impl_trait_in_trait( // Copy visility of the containing function. trait_assoc_ty.visibility(tcx.visibility(fn_def_id)); - // Copy impl_defaultness of the containing function. - trait_assoc_ty.impl_defaultness(tcx.impl_defaultness(fn_def_id)); + // Copy defaultness of the containing function. + trait_assoc_ty.defaultness(tcx.defaultness(fn_def_id)); // Copy type_of of the opaque. trait_assoc_ty.type_of(ty::EarlyBinder::bind(tcx.mk_opaque( @@ -393,8 +393,8 @@ fn associated_type_for_impl_trait_in_impl( // Copy visility of the containing function. impl_assoc_ty.visibility(tcx.visibility(impl_fn_def_id)); - // Copy impl_defaultness of the containing function. - impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id)); + // Copy defaultness of the containing function. + impl_assoc_ty.defaultness(tcx.defaultness(impl_fn_def_id)); // Copy generics_of the trait's associated item but the impl as the parent. // FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) resolves to the trait instead of the impl diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 36a20c78fcc..dd911239ac4 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -204,7 +204,7 @@ fn resolve_associated_item<'tcx>( } else { // All other methods are default methods of the `Future` trait. // (this assumes that `ImplSource::Future` is only used for methods on `Future`) - debug_assert!(tcx.impl_defaultness(trait_item_id).has_value()); + debug_assert!(tcx.defaultness(trait_item_id).has_value()); Some(Instance::new(trait_item_id, rcvr_substs)) } } diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 12ad8b0842f..fe2d1fba7fe 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -75,13 +75,13 @@ fn sized_constraint_for_ty<'tcx>( result } -fn impl_defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness { +fn defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness { match tcx.hir().get_by_def_id(def_id) { hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.defaultness, hir::Node::ImplItem(hir::ImplItem { defaultness, .. }) | hir::Node::TraitItem(hir::TraitItem { defaultness, .. }) => *defaultness, node => { - bug!("`impl_defaultness` called on {:?}", node); + bug!("`defaultness` called on {:?}", node); } } } @@ -186,6 +186,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { kind: hir::TraitItemKind::Const(..), .. }) | hir::Node::AnonConst(_) + | hir::Node::ConstBlock(_) | hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) | hir::Node::ImplItem(hir::ImplItem { kind: @@ -574,7 +575,7 @@ pub fn provide(providers: &mut Providers) { param_env_reveal_all_normalized, instance_def_size_estimate, issue33140_self_ty, - impl_defaultness, + defaultness, unsizing_params_for_adt, ..*providers }; diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index 744767aae44..c1cc892eb86 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -59,7 +59,7 @@ //! [`borrow`](`RefCell::borrow`), and a mutable borrow (`&mut T`) can be obtained with //! [`borrow_mut`](`RefCell::borrow_mut`). When these functions are called, they first verify that //! Rust's borrow rules will be satisfied: any number of immutable borrows are allowed or a -//! single immutable borrow is allowed, but never both. If a borrow is attempted that would violate +//! single mutable borrow is allowed, but never both. If a borrow is attempted that would violate //! these rules, the thread will panic. //! //! The corresponding [`Sync`] version of `RefCell<T>` is [`RwLock<T>`]. diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 2d2d5d49175..9a541ccaeac 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -205,6 +205,20 @@ pub trait StructuralPartialEq { // Empty. } +marker_impls! { + #[unstable(feature = "structural_match", issue = "31434")] + StructuralPartialEq for + usize, u8, u16, u32, u64, u128, + isize, i8, i16, i32, i64, i128, + bool, + char, + str /* Technically requires `[u8]: StructuralEq` */, + (), + {T, const N: usize} [T; N], + {T} [T], + {T: ?Sized} &T, +} + /// Required trait for constants used in pattern matches. /// /// Any type that derives `Eq` automatically implements this trait, *regardless* @@ -267,6 +281,7 @@ marker_impls! { bool, char, str /* Technically requires `[u8]: StructuralEq` */, + (), {T, const N: usize} [T; N], {T} [T], {T: ?Sized} &T, @@ -974,7 +989,8 @@ pub trait PointerLike {} #[lang = "const_param_ty"] #[unstable(feature = "adt_const_params", issue = "95174")] #[rustc_on_unimplemented(message = "`{Self}` can't be used as a const parameter type")] -pub trait ConstParamTy: StructuralEq {} +#[allow(multiple_supertrait_upcastable)] +pub trait ConstParamTy: StructuralEq + StructuralPartialEq {} /// Derive macro generating an impl of the trait `ConstParamTy`. #[rustc_builtin_macro] @@ -983,8 +999,7 @@ pub macro ConstParamTy($item:item) { /* compiler built-in */ } -// FIXME(generic_const_parameter_types): handle `ty::FnDef`/`ty::Closure` -// FIXME(generic_const_parameter_types): handle `ty::Tuple` +// FIXME(adt_const_params): handle `ty::FnDef`/`ty::Closure` marker_impls! { #[unstable(feature = "adt_const_params", issue = "95174")] ConstParamTy for @@ -998,6 +1013,11 @@ marker_impls! { {T: ?Sized + ConstParamTy} &T, } +// FIXME(adt_const_params): Add to marker_impls call above once not in bootstrap +#[unstable(feature = "adt_const_params", issue = "95174")] +#[cfg(not(bootstrap))] +impl ConstParamTy for () {} + /// A common trait implemented by all function pointers. #[unstable( feature = "fn_ptr_trait", diff --git a/library/core/src/mem/transmutability.rs b/library/core/src/mem/transmutability.rs index 87ae30619c6..a6f792ed0e3 100644 --- a/library/core/src/mem/transmutability.rs +++ b/library/core/src/mem/transmutability.rs @@ -1,3 +1,5 @@ +use crate::marker::ConstParamTy; + /// Are values of a type transmutable into values of another type? /// /// This trait is implemented on-the-fly by the compiler for types `Src` and `Self` when the bits of @@ -33,6 +35,9 @@ pub struct Assume { pub validity: bool, } +#[unstable(feature = "transmutability", issue = "99571")] +impl ConstParamTy for Assume {} + impl Assume { /// Do not assume that *you* have ensured any safety properties are met. #[unstable(feature = "transmutability", issue = "99571")] diff --git a/library/core/src/option.rs b/library/core/src/option.rs index ec1ef3cf43d..9b6ff76b240 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1138,7 +1138,7 @@ impl<T> Option<T> { /// Computes a default function result (if none), or /// applies a different function to the contained value (if any). /// - /// # Examples + /// # Basic examples /// /// ``` /// let k = 21; @@ -1149,6 +1149,25 @@ impl<T> Option<T> { /// let x: Option<&str> = None; /// assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42); /// ``` + /// + /// # Handling a Result-based fallback + /// + /// A somewhat common occurrence when dealing with optional values + /// in combination with [`Result<T, E>`] is the case where one wants to invoke + /// a fallible fallback if the option is not present. This example + /// parses a command line argument (if present), or the contents of a file to + /// an integer. However, unlike accessing the command line argument, reading + /// the file is fallible, so it must be wrapped with `Ok`. + /// + /// ```no_run + /// # fn main() -> Result<(), Box<dyn std::error::Error>> { + /// let v: u64 = std::env::args() + /// .nth(1) + /// .map_or_else(|| std::fs::read_to_string("/etc/someconfig.conf"), Ok)? + /// .parse()?; + /// # Ok(()) + /// # } + /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U @@ -1383,6 +1402,7 @@ impl<T> Option<T> { /// let item_2_0 = arr_2d.get(2).and_then(|row| row.get(0)); /// assert_eq!(item_2_0, None); /// ``` + #[doc(alias = "flatmap")] #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn and_then<U, F>(self, f: F) -> Option<U> diff --git a/library/core/src/tuple.rs b/library/core/src/tuple.rs index a1388dfeee6..ac8d04a8286 100644 --- a/library/core/src/tuple.rs +++ b/library/core/src/tuple.rs @@ -1,6 +1,9 @@ // See src/libstd/primitive_docs.rs for documentation. use crate::cmp::Ordering::{self, *}; +#[cfg(not(bootstrap))] +use crate::marker::ConstParamTy; +use crate::marker::{StructuralEq, StructuralPartialEq}; // Recursive macro for implementing n-ary tuple functions and operations // @@ -47,6 +50,28 @@ macro_rules! tuple_impls { maybe_tuple_doc! { $($T)+ @ + #[unstable(feature = "structural_match", issue = "31434")] + #[cfg(not(bootstrap))] + impl<$($T: ConstParamTy),+> ConstParamTy for ($($T,)+) + {} + } + + maybe_tuple_doc! { + $($T)+ @ + #[unstable(feature = "structural_match", issue = "31434")] + impl<$($T),+> StructuralPartialEq for ($($T,)+) + {} + } + + maybe_tuple_doc! { + $($T)+ @ + #[unstable(feature = "structural_match", issue = "31434")] + impl<$($T),+> StructuralEq for ($($T,)+) + {} + } + + maybe_tuple_doc! { + $($T)+ @ #[stable(feature = "rust1", since = "1.0.0")] impl<$($T: PartialOrd),+> PartialOrd for ($($T,)+) where diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 318a46d1b63..d53f1a2b2ff 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -240,6 +240,7 @@ #![feature(allocator_internals)] #![feature(allow_internal_unsafe)] #![feature(allow_internal_unstable)] +#![feature(c_str_literals)] #![feature(c_unwind)] #![feature(cfg_target_thread_local)] #![feature(concat_idents)] diff --git a/library/std/src/sys/unix/args.rs b/library/std/src/sys/unix/args.rs index 9ed4d9c1e0d..0efe2570d67 100644 --- a/library/std/src/sys/unix/args.rs +++ b/library/std/src/sys/unix/args.rs @@ -242,13 +242,15 @@ mod imp { let mut res = Vec::new(); unsafe { - let process_info_sel = sel_registerName("processInfo\0".as_ptr()); - let arguments_sel = sel_registerName("arguments\0".as_ptr()); - let utf8_sel = sel_registerName("UTF8String\0".as_ptr()); - let count_sel = sel_registerName("count\0".as_ptr()); - let object_at_sel = sel_registerName("objectAtIndex:\0".as_ptr()); - - let klass = objc_getClass("NSProcessInfo\0".as_ptr()); + let process_info_sel = + sel_registerName(c"processInfo".as_ptr() as *const libc::c_uchar); + let arguments_sel = sel_registerName(c"arguments".as_ptr() as *const libc::c_uchar); + let utf8_sel = sel_registerName(c"UTF8String".as_ptr() as *const libc::c_uchar); + let count_sel = sel_registerName(c"count".as_ptr() as *const libc::c_uchar); + let object_at_sel = + sel_registerName(c"objectAtIndex:".as_ptr() as *const libc::c_uchar); + + let klass = objc_getClass(c"NSProcessInfo".as_ptr() as *const libc::c_uchar); let info = objc_msgSend(klass, process_info_sel); let args = objc_msgSend(info, arguments_sel); diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 09e9ae2720f..0e5691d40d1 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -1063,7 +1063,7 @@ impl File { cfg_has_statx! { if let Some(ret) = unsafe { try_statx( fd, - b"\0" as *const _ as *const c_char, + c"".as_ptr() as *const c_char, libc::AT_EMPTY_PATH | libc::AT_STATX_SYNC_AS_STAT, libc::STATX_ALL, ) } { diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs index bb9e65e68e5..54e2f20b317 100644 --- a/library/std/src/sys/unix/mod.rs +++ b/library/std/src/sys/unix/mod.rs @@ -1,6 +1,5 @@ #![allow(missing_docs, nonstandard_style)] -use crate::ffi::CStr; use crate::io::ErrorKind; pub use self::rand::hashmap_random_keys; @@ -75,7 +74,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { // thread-id for the main thread and so renaming the main thread will rename the // process and we only want to enable this on platforms we've tested. if cfg!(target_os = "macos") { - thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0")); + thread::Thread::set_name(&c"main"); } unsafe fn sanitize_standard_fds() { @@ -121,7 +120,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { if pfd.revents & libc::POLLNVAL == 0 { continue; } - if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 { + if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 { // If the stream is closed but we failed to reopen it, abort the // process. Otherwise we wouldn't preserve the safety of // operations on the corresponding Rust object Stdin, Stdout, or @@ -151,7 +150,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { use libc::open64; for fd in 0..3 { if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF { - if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 { + if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 { // If the stream is closed but we failed to reopen it, abort the // process. Otherwise we wouldn't preserve the safety of // operations on the corresponding Rust object Stdin, Stdout, or diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 640648e8707..5f316b12b62 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -24,11 +24,11 @@ cfg_if::cfg_if! { if #[cfg(target_os = "fuchsia")] { // fuchsia doesn't have /dev/null } else if #[cfg(target_os = "redox")] { - const DEV_NULL: &str = "null:\0"; + const DEV_NULL: &CStr = c"null:"; } else if #[cfg(target_os = "vxworks")] { - const DEV_NULL: &str = "/null\0"; + const DEV_NULL: &CStr = c"/null"; } else { - const DEV_NULL: &str = "/dev/null\0"; + const DEV_NULL: &CStr = c"/dev/null"; } } @@ -474,8 +474,7 @@ impl Stdio { let mut opts = OpenOptions::new(); opts.read(readable); opts.write(!readable); - let path = unsafe { CStr::from_ptr(DEV_NULL.as_ptr() as *const _) }; - let fd = File::open_c(&path, &opts)?; + let fd = File::open_c(DEV_NULL, &opts)?; Ok((ChildStdio::Owned(fd.into_inner()), None)) } diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs index 612d43fe204..3721988b405 100644 --- a/library/std/src/sys/unix/process/process_unix.rs +++ b/library/std/src/sys/unix/process/process_unix.rs @@ -35,8 +35,26 @@ cfg_if::cfg_if! { if #[cfg(all(target_os = "nto", target_env = "nto71"))] { use crate::thread; use libc::{c_char, posix_spawn_file_actions_t, posix_spawnattr_t}; - // arbitrary number of tries: - const MAX_FORKSPAWN_TRIES: u32 = 4; + use crate::time::Duration; + use crate::sync::LazyLock; + // Get smallest amount of time we can sleep. + // Return a common value if it cannot be determined. + fn get_clock_resolution() -> Duration { + static MIN_DELAY: LazyLock<Duration, fn() -> Duration> = LazyLock::new(|| { + let mut mindelay = libc::timespec { tv_sec: 0, tv_nsec: 0 }; + if unsafe { libc::clock_getres(libc::CLOCK_MONOTONIC, &mut mindelay) } == 0 + { + Duration::from_nanos(mindelay.tv_nsec as u64) + } else { + Duration::from_millis(1) + } + }); + *MIN_DELAY + } + // Arbitrary minimum sleep duration for retrying fork/spawn + const MIN_FORKSPAWN_SLEEP: Duration = Duration::from_nanos(1); + // Maximum duration of sleeping before giving up and returning an error + const MAX_FORKSPAWN_SLEEP: Duration = Duration::from_millis(1000); } } @@ -163,12 +181,25 @@ impl Command { unsafe fn do_fork(&mut self) -> Result<(pid_t, pid_t), io::Error> { use crate::sys::os::errno; - let mut tries_left = MAX_FORKSPAWN_TRIES; + let mut delay = MIN_FORKSPAWN_SLEEP; + loop { let r = libc::fork(); - if r == -1 as libc::pid_t && tries_left > 0 && errno() as libc::c_int == libc::EBADF { - thread::yield_now(); - tries_left -= 1; + if r == -1 as libc::pid_t && errno() as libc::c_int == libc::EBADF { + if delay < get_clock_resolution() { + // We cannot sleep this short (it would be longer). + // Yield instead. + thread::yield_now(); + } else if delay < MAX_FORKSPAWN_SLEEP { + thread::sleep(delay); + } else { + return Err(io::const_io_error!( + ErrorKind::WouldBlock, + "forking returned EBADF too often", + )); + } + delay *= 2; + continue; } else { return cvt(r).map(|res| (res, -1)); } @@ -480,17 +511,28 @@ impl Command { attrp: *const posix_spawnattr_t, argv: *const *mut c_char, envp: *const *mut c_char, - ) -> i32 { - let mut tries_left = MAX_FORKSPAWN_TRIES; + ) -> io::Result<i32> { + let mut delay = MIN_FORKSPAWN_SLEEP; loop { match libc::posix_spawnp(pid, file, file_actions, attrp, argv, envp) { - libc::EBADF if tries_left > 0 => { - thread::yield_now(); - tries_left -= 1; + libc::EBADF => { + if delay < get_clock_resolution() { + // We cannot sleep this short (it would be longer). + // Yield instead. + thread::yield_now(); + } else if delay < MAX_FORKSPAWN_SLEEP { + thread::sleep(delay); + } else { + return Err(io::const_io_error!( + ErrorKind::WouldBlock, + "posix_spawnp returned EBADF too often", + )); + } + delay *= 2; continue; } r => { - return r; + return Ok(r); } } } @@ -620,14 +662,20 @@ impl Command { let spawn_fn = libc::posix_spawnp; #[cfg(target_os = "nto")] let spawn_fn = retrying_libc_posix_spawnp; - cvt_nz(spawn_fn( + + let spawn_res = spawn_fn( &mut p.pid, self.get_program_cstr().as_ptr(), file_actions.0.as_ptr(), attrs.0.as_ptr(), self.get_argv().as_ptr() as *const _, envp as *const _, - ))?; + ); + + #[cfg(target_os = "nto")] + let spawn_res = spawn_res?; + + cvt_nz(spawn_res)?; Ok(Some(p)) } } diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index 7307d9b2c86..878af5088d9 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -163,10 +163,9 @@ impl Thread { #[cfg(target_os = "netbsd")] pub fn set_name(name: &CStr) { unsafe { - let cname = CStr::from_bytes_with_nul_unchecked(b"%s\0".as_slice()); let res = libc::pthread_setname_np( libc::pthread_self(), - cname.as_ptr(), + c"%s".as_ptr(), name.as_ptr() as *mut libc::c_void, ); debug_assert_eq!(res, 0); diff --git a/library/std/src/sys/windows/c.rs b/library/std/src/sys/windows/c.rs index 2bc40c4748a..07b0610d463 100644 --- a/library/std/src/sys/windows/c.rs +++ b/library/std/src/sys/windows/c.rs @@ -317,7 +317,7 @@ pub unsafe fn NtWriteFile( // Functions that aren't available on every version of Windows that we support, // but we still use them and just provide some form of a fallback implementation. compat_fn_with_fallback! { - pub static KERNEL32: &CStr = ansi_str!("kernel32"); + pub static KERNEL32: &CStr = c"kernel32"; // >= Win10 1607 // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription @@ -350,7 +350,7 @@ compat_fn_optional! { } compat_fn_with_fallback! { - pub static NTDLL: &CStr = ansi_str!("ntdll"); + pub static NTDLL: &CStr = c"ntdll"; pub fn NtCreateKeyedEvent( KeyedEventHandle: LPHANDLE, diff --git a/library/std/src/sys/windows/compat.rs b/library/std/src/sys/windows/compat.rs index 4fe95d41116..649cc4bfdbf 100644 --- a/library/std/src/sys/windows/compat.rs +++ b/library/std/src/sys/windows/compat.rs @@ -228,9 +228,9 @@ macro_rules! compat_fn_optional { /// Load all needed functions from "api-ms-win-core-synch-l1-2-0". pub(super) fn load_synch_functions() { fn try_load() -> Option<()> { - const MODULE_NAME: &CStr = ansi_str!("api-ms-win-core-synch-l1-2-0"); - const WAIT_ON_ADDRESS: &CStr = ansi_str!("WaitOnAddress"); - const WAKE_BY_ADDRESS_SINGLE: &CStr = ansi_str!("WakeByAddressSingle"); + const MODULE_NAME: &CStr = c"api-ms-win-core-synch-l1-2-0"; + const WAIT_ON_ADDRESS: &CStr = c"WaitOnAddress"; + const WAKE_BY_ADDRESS_SINGLE: &CStr = c"WakeByAddressSingle"; // Try loading the library and all the required functions. // If any step fails, then they all fail. diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index bcc172b0fae..b11c8962203 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -1,6 +1,6 @@ #![allow(missing_docs, nonstandard_style)] -use crate::ffi::{CStr, OsStr, OsString}; +use crate::ffi::{OsStr, OsString}; use crate::io::ErrorKind; use crate::mem::MaybeUninit; use crate::os::windows::ffi::{OsStrExt, OsStringExt}; @@ -51,7 +51,7 @@ pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) { // Normally, `thread::spawn` will call `Thread::set_name` but since this thread already // exists, we have to call it ourselves. - thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0")); + thread::Thread::set_name(&c"main"); } // SAFETY: must be called only once during runtime cleanup. diff --git a/library/std/src/sys/windows/stdio.rs b/library/std/src/sys/windows/stdio.rs index 2e3e0859dc1..3fcaaa508e3 100644 --- a/library/std/src/sys/windows/stdio.rs +++ b/library/std/src/sys/windows/stdio.rs @@ -11,6 +11,9 @@ use crate::sys::cvt; use crate::sys::handle::Handle; use core::str::utf8_char_width; +#[cfg(test)] +mod tests; + // Don't cache handles but get them fresh for every read/write. This allows us to track changes to // the value over time (such as if a process calls `SetStdHandle` while it's running). See #40490. pub struct Stdin { @@ -383,6 +386,10 @@ fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> { debug_assert!(utf16.len() <= c::c_int::MAX as usize); debug_assert!(utf8.len() <= c::c_int::MAX as usize); + if utf16.is_empty() { + return Ok(0); + } + let result = unsafe { c::WideCharToMultiByte( c::CP_UTF8, // CodePage diff --git a/library/std/src/sys/windows/stdio/tests.rs b/library/std/src/sys/windows/stdio/tests.rs new file mode 100644 index 00000000000..1e53e0bee63 --- /dev/null +++ b/library/std/src/sys/windows/stdio/tests.rs @@ -0,0 +1,6 @@ +use super::utf16_to_utf8; + +#[test] +fn zero_size_read() { + assert_eq!(utf16_to_utf8(&[], &mut []).unwrap(), 0); +} diff --git a/src/bootstrap/Cargo.lock b/src/bootstrap/Cargo.lock index 311ac175160..8f8778efee7 100644 --- a/src/bootstrap/Cargo.lock +++ b/src/bootstrap/Cargo.lock @@ -58,6 +58,7 @@ dependencies = [ "once_cell", "opener", "pretty_assertions", + "semver", "serde", "serde_derive", "serde_json", @@ -646,6 +647,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] +name = "semver" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" + +[[package]] name = "serde" version = "1.0.160" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index 70ade776d7d..367c6190967 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -57,6 +57,7 @@ walkdir = "2" sysinfo = { version = "0.26.0", optional = true } clap = { version = "4.2.4", default-features = false, features = ["std", "usage", "help", "derive", "error-context"] } clap_complete = "4.2.2" +semver = "1.0.17" # Solaris doesn't support flock() and thus fd-lock is not option now [target.'cfg(not(target_os = "solaris"))'.dependencies] diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 43c859b0063..8764444aa61 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -571,7 +571,7 @@ impl<'a> ShouldRun<'a> { } } -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, ValueEnum)] +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] pub enum Kind { #[clap(alias = "b")] Build, @@ -642,7 +642,10 @@ impl Kind { Kind::Doc => "Documenting", Kind::Run => "Running", Kind::Suggest => "Suggesting", - _ => return format!("{self:?}"), + _ => { + let title_letter = self.as_str()[0..1].to_ascii_uppercase(); + return format!("{title_letter}{}ing", &self.as_str()[1..]); + } } .to_owned() } @@ -992,7 +995,7 @@ impl<'a> Builder<'a> { } pub fn sysroot(&self, compiler: Compiler) -> Interned<PathBuf> { - self.ensure(compile::Sysroot { compiler }) + self.ensure(compile::Sysroot::new(compiler)) } /// Returns the libdir where the standard library and other artifacts are @@ -1783,7 +1786,10 @@ impl<'a> Builder<'a> { cargo.env("RUSTC_TLS_MODEL_INITIAL_EXEC", "1"); } - if self.config.incremental { + // Ignore incremental modes except for stage0, since we're + // not guaranteeing correctness across builds if the compiler + // is changing under your feet. + if self.config.incremental && compiler.stage == 0 { cargo.env("CARGO_INCREMENTAL", "1"); } else { // Don't rely on any default setting for incr. comp. in Cargo diff --git a/src/bootstrap/clean.rs b/src/bootstrap/clean.rs index 0d9fd56b038..c1d867a0bd1 100644 --- a/src/bootstrap/clean.rs +++ b/src/bootstrap/clean.rs @@ -85,6 +85,10 @@ clean_crate_tree! { } fn clean_default(build: &Build, all: bool) { + if build.config.dry_run() { + return; + } + rm_rf("tmp".as_ref()); if all { diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 1c66c00eda7..48685f7a9d5 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -40,11 +40,18 @@ pub struct Std { /// /// This shouldn't be used from other steps; see the comment on [`Rustc`]. crates: Interned<Vec<String>>, + /// When using download-rustc, we need to use a new build of `std` for running unit tests of Std itself, + /// but we need to use the downloaded copy of std for linking to rustdoc. Allow this to be overriden by `builder.ensure` from other steps. + force_recompile: bool, } impl Std { pub fn new(compiler: Compiler, target: TargetSelection) -> Self { - Self { target, compiler, crates: Default::default() } + Self { target, compiler, crates: Default::default(), force_recompile: false } + } + + pub fn force_recompile(compiler: Compiler, target: TargetSelection) -> Self { + Self { target, compiler, crates: Default::default(), force_recompile: true } } } @@ -77,6 +84,7 @@ impl Step for Std { compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()), target: run.target, crates: make_run_crates(&run, "library"), + force_recompile: false, }); } @@ -89,11 +97,20 @@ impl Step for Std { let target = self.target; let compiler = self.compiler; - // When using `download-rustc`, we already have artifacts for the host available - // (they were copied in `impl Step for Sysroot`). Don't recompile them. - // NOTE: the ABI of the beta compiler is different from the ABI of the downloaded compiler, - // so its artifacts can't be reused. - if builder.download_rustc() && compiler.stage != 0 && target == builder.build.build { + // When using `download-rustc`, we already have artifacts for the host available. Don't + // recompile them. + if builder.download_rustc() && target == builder.build.build + // NOTE: the beta compiler may generate different artifacts than the downloaded compiler, so + // its artifacts can't be reused. + && compiler.stage != 0 + // This check is specific to testing std itself; see `test::Std` for more details. + && !self.force_recompile + { + cp_rustc_component_to_ci_sysroot( + builder, + compiler, + builder.config.ci_rust_std_contents(), + ); return; } @@ -428,6 +445,8 @@ struct StdLink { pub target: TargetSelection, /// Not actually used; only present to make sure the cache invalidation is correct. crates: Interned<Vec<String>>, + /// See [`Std::force_recompile`]. + force_recompile: bool, } impl StdLink { @@ -437,6 +456,7 @@ impl StdLink { target_compiler: std.compiler, target: std.target, crates: std.crates, + force_recompile: std.force_recompile, } } } @@ -460,8 +480,24 @@ impl Step for StdLink { let compiler = self.compiler; let target_compiler = self.target_compiler; let target = self.target; - let libdir = builder.sysroot_libdir(target_compiler, target); - let hostdir = builder.sysroot_libdir(target_compiler, compiler.host); + + // NOTE: intentionally does *not* check `target == builder.build` to avoid having to add the same check in `test::Crate`. + let (libdir, hostdir) = if self.force_recompile && builder.download_rustc() { + // NOTE: copies part of `sysroot_libdir` to avoid having to add a new `force_recompile` argument there too + let lib = builder.sysroot_libdir_relative(self.compiler); + let sysroot = builder.ensure(crate::compile::Sysroot { + compiler: self.compiler, + force_recompile: self.force_recompile, + }); + let libdir = sysroot.join(lib).join("rustlib").join(target.triple).join("lib"); + let hostdir = sysroot.join(lib).join("rustlib").join(compiler.host.triple).join("lib"); + (INTERNER.intern_path(libdir), INTERNER.intern_path(hostdir)) + } else { + let libdir = builder.sysroot_libdir(target_compiler, target); + let hostdir = builder.sysroot_libdir(target_compiler, compiler.host); + (libdir, hostdir) + }; + add_to_sysroot(builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target)); } } @@ -594,6 +630,25 @@ impl Step for StartupObjects { } } +fn cp_rustc_component_to_ci_sysroot( + builder: &Builder<'_>, + compiler: Compiler, + contents: Vec<String>, +) { + let sysroot = builder.ensure(Sysroot { compiler, force_recompile: false }); + + let ci_rustc_dir = builder.out.join(&*builder.build.build.triple).join("ci-rustc"); + for file in contents { + let src = ci_rustc_dir.join(&file); + let dst = sysroot.join(file); + if src.is_dir() { + t!(fs::create_dir_all(dst)); + } else { + builder.copy(&src, &dst); + } + } +} + #[derive(Debug, PartialOrd, Ord, Copy, Clone, PartialEq, Eq, Hash)] pub struct Rustc { pub target: TargetSelection, @@ -653,18 +708,11 @@ impl Step for Rustc { if builder.download_rustc() && compiler.stage != 0 { // Copy the existing artifacts instead of rebuilding them. // NOTE: this path is only taken for tools linking to rustc-dev (including ui-fulldeps tests). - let sysroot = builder.ensure(Sysroot { compiler }); - - let ci_rustc_dir = builder.out.join(&*builder.build.build.triple).join("ci-rustc"); - for file in builder.config.rustc_dev_contents() { - let src = ci_rustc_dir.join(&file); - let dst = sysroot.join(file); - if src.is_dir() { - t!(fs::create_dir_all(dst)); - } else { - builder.copy(&src, &dst); - } - } + cp_rustc_component_to_ci_sysroot( + builder, + compiler, + builder.config.ci_rustc_dev_contents(), + ); return; } @@ -1225,6 +1273,14 @@ pub fn compiler_file( #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct Sysroot { pub compiler: Compiler, + /// See [`Std::force_recompile`]. + force_recompile: bool, +} + +impl Sysroot { + pub(crate) fn new(compiler: Compiler) -> Self { + Sysroot { compiler, force_recompile: false } + } } impl Step for Sysroot { @@ -1247,6 +1303,8 @@ impl Step for Sysroot { let sysroot_dir = |stage| { if stage == 0 { host_dir.join("stage0-sysroot") + } else if self.force_recompile && stage == compiler.stage { + host_dir.join(format!("stage{stage}-test-sysroot")) } else if builder.download_rustc() && compiler.stage != builder.top_stage { host_dir.join("ci-rustc-sysroot") } else { @@ -1286,14 +1344,19 @@ impl Step for Sysroot { // 2. The sysroot is deleted and recreated between each invocation, so running `x test // ui-fulldeps && x test ui` can't cause failures. let mut filtered_files = Vec::new(); - // Don't trim directories or files that aren't loaded per-target; they can't cause conflicts. - let suffix = format!("lib/rustlib/{}/lib", compiler.host); - for path in builder.config.rustc_dev_contents() { - let path = Path::new(&path); - if path.parent().map_or(false, |parent| parent.ends_with(&suffix)) { - filtered_files.push(path.file_name().unwrap().to_owned()); + let mut add_filtered_files = |suffix, contents| { + for path in contents { + let path = Path::new(&path); + if path.parent().map_or(false, |parent| parent.ends_with(&suffix)) { + filtered_files.push(path.file_name().unwrap().to_owned()); + } } - } + }; + let suffix = format!("lib/rustlib/{}/lib", compiler.host); + add_filtered_files(suffix.as_str(), builder.config.ci_rustc_dev_contents()); + // NOTE: we can't copy std eagerly because `stage2-test-sysroot` needs to have only the + // newly compiled std, not the downloaded std. + add_filtered_files("lib", builder.config.ci_rust_std_contents()); let filtered_extensions = [OsStr::new("rmeta"), OsStr::new("rlib"), OsStr::new("so")]; let ci_rustc_dir = builder.ci_rustc_dir(builder.config.build); @@ -1411,7 +1474,8 @@ impl Step for Assemble { // If we're downloading a compiler from CI, we can use the same compiler for all stages other than 0. if builder.download_rustc() { - let sysroot = builder.ensure(Sysroot { compiler: target_compiler }); + let sysroot = + builder.ensure(Sysroot { compiler: target_compiler, force_recompile: false }); // Ensure that `libLLVM.so` ends up in the newly created target directory, // so that tools using `rustc_private` can use it. dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot); diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index 45ad1547eb7..8ea7e836375 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -25,6 +25,7 @@ use crate::flags::{Color, Flags, Warnings}; use crate::util::{exe, output, t}; use build_helper::detail_exit_macro; use once_cell::sync::OnceCell; +use semver::Version; use serde::{Deserialize, Deserializer}; use serde_derive::Deserialize; @@ -1114,10 +1115,14 @@ impl Config { config.out = crate::util::absolute(&config.out); } - config.initial_rustc = build.rustc.map(PathBuf::from).unwrap_or_else(|| { + config.initial_rustc = if let Some(rustc) = build.rustc { + config.check_build_rustc_version(&rustc); + PathBuf::from(rustc) + } else { config.download_beta_toolchain(); config.out.join(config.build.triple).join("stage0/bin/rustc") - }); + }; + config.initial_cargo = build .cargo .map(|cargo| { @@ -1779,6 +1784,42 @@ impl Config { self.rust_codegen_backends.get(0).cloned() } + pub fn check_build_rustc_version(&self, rustc_path: &str) { + if self.dry_run() { + return; + } + + // check rustc version is same or lower with 1 apart from the building one + let mut cmd = Command::new(rustc_path); + cmd.arg("--version"); + let rustc_output = output(&mut cmd) + .lines() + .next() + .unwrap() + .split(' ') + .nth(1) + .unwrap() + .split('-') + .next() + .unwrap() + .to_owned(); + let rustc_version = Version::parse(&rustc_output.trim()).unwrap(); + let source_version = + Version::parse(&fs::read_to_string(self.src.join("src/version")).unwrap().trim()) + .unwrap(); + if !(source_version == rustc_version + || (source_version.major == rustc_version.major + && source_version.minor == rustc_version.minor + 1)) + { + let prev_version = format!("{}.{}.x", source_version.major, source_version.minor - 1); + eprintln!( + "Unexpected rustc version: {}, we should use {}/{} to build source with {}", + rustc_version, prev_version, source_version, source_version + ); + detail_exit_macro!(1); + } + } + /// Returns the commit to download, or `None` if we shouldn't download CI artifacts. fn download_ci_rustc_commit(&self, download_rustc: Option<StringOrBool>) -> Option<String> { // If `download-rustc` is not set, default to rebuilding. diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index 3de85c91516..d6210ed59c4 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -680,7 +680,7 @@ impl Step for Rustc { let compiler = builder.compiler(stage, builder.config.build); builder.ensure(compile::Std::new(compiler, builder.config.build)); - let _guard = builder.msg( + let _guard = builder.msg_sysroot_tool( Kind::Doc, stage, &format!("compiler{}", crate_description(&self.crates)), @@ -834,6 +834,7 @@ macro_rules! tool_doc { cargo.arg("-Zskip-rustdoc-fingerprint"); // Only include compiler crates, no dependencies of those, such as `libc`. cargo.arg("--no-deps"); + cargo.arg("--lib"); $( cargo.arg("-p").arg($krate); )+ diff --git a/src/bootstrap/download.rs b/src/bootstrap/download.rs index 12780df2175..06f479808b9 100644 --- a/src/bootstrap/download.rs +++ b/src/bootstrap/download.rs @@ -271,11 +271,8 @@ impl Config { // `compile::Sysroot` needs to know the contents of the `rustc-dev` tarball to avoid adding // it to the sysroot unless it was explicitly requested. But parsing the 100 MB tarball is slow. // Cache the entries when we extract it so we only have to read it once. - let mut recorded_entries = if dst.ends_with("ci-rustc") && pattern == "rustc-dev" { - Some(BufWriter::new(t!(File::create(dst.join(".rustc-dev-contents"))))) - } else { - None - }; + let mut recorded_entries = + if dst.ends_with("ci-rustc") { recorded_entries(dst, pattern) } else { None }; for member in t!(tar.entries()) { let mut member = t!(member); @@ -332,6 +329,17 @@ impl Config { } } +fn recorded_entries(dst: &Path, pattern: &str) -> Option<BufWriter<File>> { + let name = if pattern == "rustc-dev" { + ".rustc-dev-contents" + } else if pattern.starts_with("rust-std") { + ".rust-std-contents" + } else { + return None; + }; + Some(BufWriter::new(t!(File::create(dst.join(name))))) +} + enum DownloadSource { CI, Dist, @@ -382,11 +390,20 @@ impl Config { Some(rustfmt_path) } - pub(crate) fn rustc_dev_contents(&self) -> Vec<String> { + pub(crate) fn ci_rust_std_contents(&self) -> Vec<String> { + self.ci_component_contents(".rust-std-contents") + } + + pub(crate) fn ci_rustc_dev_contents(&self) -> Vec<String> { + self.ci_component_contents(".rustc-dev-contents") + } + + fn ci_component_contents(&self, stamp_file: &str) -> Vec<String> { assert!(self.download_rustc()); let ci_rustc_dir = self.out.join(&*self.build.triple).join("ci-rustc"); - let rustc_dev_contents_file = t!(File::open(ci_rustc_dir.join(".rustc-dev-contents"))); - t!(BufReader::new(rustc_dev_contents_file).lines().collect()) + let stamp_file = ci_rustc_dir.join(stamp_file); + let contents_file = t!(File::open(&stamp_file), stamp_file.display().to_string()); + t!(BufReader::new(contents_file).lines().collect()) } pub(crate) fn download_ci_rustc(&self, commit: &str) { diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index a1aaee68c62..aa5d1bdd37f 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -1045,8 +1045,8 @@ impl Build { what: impl Display, target: TargetSelection, ) -> Option<gha::Group> { - let action = action.into(); - let msg = format!("{action:?}ing {what} for {target}"); + let action = action.into().description(); + let msg = format!("{action} {what} for {target}"); self.group(&msg) } @@ -1058,8 +1058,8 @@ impl Build { host: TargetSelection, target: TargetSelection, ) -> Option<gha::Group> { - let action = action.into(); - let msg = |fmt| format!("{action:?}ing {what} {fmt}"); + let action = action.into().description(); + let msg = |fmt| format!("{action} {what} {fmt}"); let msg = if host == target { msg(format_args!("(stage{stage} -> stage{}, {target})", stage + 1)) } else { diff --git a/src/bootstrap/setup.rs b/src/bootstrap/setup.rs index 40038df8332..c604c63a4dd 100644 --- a/src/bootstrap/setup.rs +++ b/src/bootstrap/setup.rs @@ -20,7 +20,7 @@ pub enum Profile { Codegen, Library, Tools, - User, + Dist, None, } @@ -31,6 +31,7 @@ static SETTINGS_HASHES: &[&str] = &[ "ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8", "56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922", "af1b5efe196aed007577899db9dae15d6dbc923d6fa42fa0934e68617ba9bbe0", + "3468fea433c25fff60be6b71e8a215a732a7b1268b6a83bf10d024344e140541", ]; static RUST_ANALYZER_SETTINGS: &str = include_str!("../etc/rust_analyzer_settings.json"); @@ -42,7 +43,7 @@ impl Profile { pub fn all() -> impl Iterator<Item = Self> { use Profile::*; // N.B. these are ordered by how they are displayed, not alphabetically - [Library, Compiler, Codegen, Tools, User, None].iter().copied() + [Library, Compiler, Codegen, Tools, Dist, None].iter().copied() } pub fn purpose(&self) -> String { @@ -52,7 +53,7 @@ impl Profile { Compiler => "Contribute to the compiler itself", Codegen => "Contribute to the compiler, and also modify LLVM or codegen", Tools => "Contribute to tools which depend on the compiler, but do not modify it directly (e.g. rustdoc, clippy, miri)", - User => "Install Rust from source", + Dist => "Install Rust from source", None => "Do not modify `config.toml`" } .to_string() @@ -72,7 +73,7 @@ impl Profile { Profile::Codegen => "codegen", Profile::Library => "library", Profile::Tools => "tools", - Profile::User => "user", + Profile::Dist => "dist", Profile::None => "none", } } @@ -86,7 +87,7 @@ impl FromStr for Profile { "lib" | "library" => Ok(Profile::Library), "compiler" => Ok(Profile::Compiler), "llvm" | "codegen" => Ok(Profile::Codegen), - "maintainer" | "user" => Ok(Profile::User), + "maintainer" | "dist" => Ok(Profile::Dist), "tools" | "tool" | "rustdoc" | "clippy" | "miri" | "rustfmt" | "rls" => { Ok(Profile::Tools) } @@ -159,7 +160,7 @@ pub fn setup(config: &Config, profile: Profile) { "test src/tools/rustfmt", ], Profile::Library => &["check", "build", "test library/std", "doc"], - Profile::User => &["dist", "build"], + Profile::Dist => &["dist", "build"], }; println!(); @@ -169,7 +170,7 @@ pub fn setup(config: &Config, profile: Profile) { println!("- `x.py {}`", cmd); } - if profile != Profile::User { + if profile != Profile::Dist { println!( "For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html" ); diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index 6e573c2284e..29e48481f0f 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -1772,6 +1772,14 @@ impl Step for BookTest { /// /// This uses the `rustdoc` that sits next to `compiler`. fn run(self, builder: &Builder<'_>) { + let host = self.compiler.host; + let _guard = builder.msg( + Kind::Test, + self.compiler.stage, + &format!("book {}", self.name), + host, + host, + ); // External docs are different from local because: // - Some books need pre-processing by mdbook before being tested. // - They need to save their state to toolstate. @@ -1963,7 +1971,7 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) -> } } - builder.info(&format!("doc tests for: {}", markdown.display())); + builder.verbose(&format!("doc tests for: {}", markdown.display())); let mut cmd = builder.rustdoc_cmd(compiler); builder.add_rust_test_threads(&mut cmd); // allow for unstable options such as new editions @@ -2196,7 +2204,8 @@ impl Step for Crate { let target = self.target; let mode = self.mode; - builder.ensure(compile::Std::new(compiler, target)); + // See [field@compile::Std::force_recompile]. + builder.ensure(compile::Std::force_recompile(compiler, target)); builder.ensure(RemoteCopyLibs { compiler, target }); // If we're not doing a full bootstrap but we're testing a stage2 @@ -2210,6 +2219,16 @@ impl Step for Crate { match mode { Mode::Std => { compile::std_cargo(builder, target, compiler.stage, &mut cargo); + // `std_cargo` actually does the wrong thing: it passes `--sysroot build/host/stage2`, + // but we want to use the force-recompile std we just built in `build/host/stage2-test-sysroot`. + // Override it. + if builder.download_rustc() { + let sysroot = builder + .out + .join(compiler.host.triple) + .join(format!("stage{}-test-sysroot", compiler.stage)); + cargo.env("RUSTC_SYSROOT", sysroot); + } } Mode::Rustc => { compile::rustc_cargo(builder, &mut cargo, target, compiler.stage); @@ -2261,6 +2280,11 @@ impl Step for CrateRustdoc { // isn't really necessary. builder.compiler_for(builder.top_stage, target, target) }; + // NOTE: normally `ensure(Rustc)` automatically runs `ensure(Std)` for us. However, when + // using `download-rustc`, the rustc_private artifacts may be in a *different sysroot* from + // the target rustdoc (`ci-rustc-sysroot` vs `stage2`). In that case, we need to ensure this + // explicitly to make sure it ends up in the stage2 sysroot. + builder.ensure(compile::Std::new(compiler, target)); builder.ensure(compile::Rustc::new(compiler, target)); let mut cargo = tool::prepare_tool_cargo( @@ -2312,7 +2336,13 @@ impl Step for CrateRustdoc { dylib_path.insert(0, PathBuf::from(&*libdir)); cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap()); - let _guard = builder.msg(builder.kind, compiler.stage, "rustdoc", compiler.host, target); + let _guard = builder.msg_sysroot_tool( + builder.kind, + compiler.stage, + "rustdoc", + compiler.host, + target, + ); run_cargo_test( cargo, &[], diff --git a/src/ci/docker/run.sh b/src/ci/docker/run.sh index 8bea8cd4c87..2fe3438e0fe 100755 --- a/src/ci/docker/run.sh +++ b/src/ci/docker/run.sh @@ -264,6 +264,7 @@ docker \ --env RUST_CI_OVERRIDE_RELEASE_CHANNEL \ --env CI_JOB_NAME="${CI_JOB_NAME-$IMAGE}" \ --env BASE_COMMIT="$BASE_COMMIT" \ + --env DIST_TRY_BUILD \ --init \ --rm \ rust-ci \ diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index fd619467fc1..d3cb6b6ed52 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -757,6 +757,7 @@ jobs: <<: *base-ci-job name: try - ${{ matrix.name }} env: + DIST_TRY_BUILD: 1 <<: [*shared-ci-variables, *prod-variables] if: github.event_name == 'push' && (github.ref == 'refs/heads/try' || github.ref == 'refs/heads/try-perf') && github.repository == 'rust-lang-ci/rust' strategy: diff --git a/src/ci/stage-build.py b/src/ci/stage-build.py index 066d3a198f2..4141296bd42 100644 --- a/src/ci/stage-build.py +++ b/src/ci/stage-build.py @@ -48,6 +48,11 @@ RUSTC_PGO_CRATES = [ LLVM_BOLT_CRATES = LLVM_PGO_CRATES + +def is_try_build() -> bool: + return os.environ.get("DIST_TRY_BUILD", "0") != "0" + + class Pipeline: # Paths def checkout_path(self) -> Path: @@ -851,6 +856,13 @@ def run(runner: BenchmarkRunner): build_args = sys.argv[1:] + # Skip components that are not needed for try builds to speed them up + if is_try_build(): + LOGGER.info("Skipping building of unimportant components for a try build") + for target in ("rust-docs", "rustc-docs", "rust-docs-json", "rust-analyzer", + "rustc-src", "clippy", "miri", "rustfmt"): + build_args.extend(["--exclude", target]) + timer = Timer() pipeline = create_pipeline() @@ -865,6 +877,7 @@ def run(runner: BenchmarkRunner): print_binary_sizes(pipeline) + if __name__ == "__main__": runner = DefaultBenchmarkRunner() run(runner) diff --git a/src/doc/rustc/src/platform-support/nto-qnx.md b/src/doc/rustc/src/platform-support/nto-qnx.md index 0d815c9b598..b376c4a84ac 100644 --- a/src/doc/rustc/src/platform-support/nto-qnx.md +++ b/src/doc/rustc/src/platform-support/nto-qnx.md @@ -164,18 +164,12 @@ export exclude_tests=' --exclude tests/run-make-fulldeps' env $build_env \ - ./x.py test -j 1 \ + ./x.py test \ $exclude_tests \ --stage 1 \ --target x86_64-pc-nto-qnx710 ``` -Currently, only one thread can be used when testing due to limitations in `libc::fork` and `libc::posix_spawnp`. -See [fork documentation](https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/f/fork.html) -(error section) for more information. -This can be achieved by using the `-j 1` parameter in the `x.py` call. -This issue is being researched and we will try to allow parallelism in the future. - ## Building Rust programs Rust does not yet ship pre-compiled artifacts for this target. diff --git a/src/etc/pre-push.sh b/src/etc/pre-push.sh index ff17931115c..0807e0492c1 100755 --- a/src/etc/pre-push.sh +++ b/src/etc/pre-push.sh @@ -14,4 +14,4 @@ ROOT_DIR="$(git rev-parse --show-toplevel)" echo "Running pre-push script $ROOT_DIR/x test tidy" cd "$ROOT_DIR" -CARGOFLAGS="--locked" ./x test tidy +./x test tidy --set build.locked-deps=true diff --git a/src/etc/rust_analyzer_settings.json b/src/etc/rust_analyzer_settings.json index dd01bfaa725..d9c4645f0b3 100644 --- a/src/etc/rust_analyzer_settings.json +++ b/src/etc/rust_analyzer_settings.json @@ -7,7 +7,14 @@ "check", "--json-output" ], - "rust-analyzer.linkedProjects": ["src/bootstrap/Cargo.toml", "Cargo.toml"], + "rust-analyzer.linkedProjects": [ + "Cargo.toml", + "src/tools/x/Cargo.toml", + "src/bootstrap/Cargo.toml", + "src/tools/rust-analyzer/Cargo.toml", + "compiler/rustc_codegen_cranelift/Cargo.toml", + "compiler/rustc_codegen_gcc/Cargo.toml" + ], "rust-analyzer.rustfmt.overrideCommand": [ "./build/host/rustfmt/bin/rustfmt", "--edition=2021" diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 5fd867189fd..b84a9cf369c 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1224,7 +1224,7 @@ pub(crate) fn clean_impl_item<'tcx>( } hir::ImplItemKind::Fn(ref sig, body) => { let m = clean_function(cx, sig, impl_.generics, FunctionArgs::Body(body)); - let defaultness = cx.tcx.impl_defaultness(impl_.owner_id); + let defaultness = cx.tcx.defaultness(impl_.owner_id); MethodItem(m, Some(defaultness)) } hir::ImplItemKind::Type(hir_ty) => { @@ -1258,7 +1258,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>( let provided = match assoc_item.container { ty::ImplContainer => true, - ty::TraitContainer => tcx.impl_defaultness(assoc_item.def_id).has_value(), + ty::TraitContainer => tcx.defaultness(assoc_item.def_id).has_value(), }; if provided { AssocConstItem(ty, ConstantKind::Extern { def_id: assoc_item.def_id }) @@ -1440,7 +1440,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>( } generics.where_predicates = where_predicates; - if tcx.impl_defaultness(assoc_item.def_id).has_value() { + if tcx.defaultness(assoc_item.def_id).has_value() { AssocTypeItem( Box::new(Typedef { type_: clean_middle_ty( diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 21f61acb2c5..156c8c06e76 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -1101,7 +1101,12 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: ); } -fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) { +fn item_trait_alias( + w: &mut impl fmt::Write, + cx: &mut Context<'_>, + it: &clean::Item, + t: &clean::TraitAlias, +) { wrap_item(w, |w| { write!( w, @@ -1111,16 +1116,17 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: & print_where_clause(&t.generics, cx, 0, Ending::Newline), bounds(&t.bounds, true, cx), attrs = render_attributes_in_pre(it, "", cx.tcx()), - ); + ) + .unwrap(); }); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); - + write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap(); // Render any items associated directly to this alias, as otherwise they // won't be visible anywhere in the docs. It would be nice to also show // associated items from the aliased type (see discussion in #32077), but // we need #14072 to make sense of the generics. write!(w, "{}", render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All)) + .unwrap(); } fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) { @@ -1451,11 +1457,11 @@ fn item_proc_macro( write!(w, "{}{}", buffer.into_inner(), document(cx, it, None, HeadingOffset::H2)).unwrap(); } -fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { +fn item_primitive(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item) { let def_id = it.item_id.expect_def_id(); - write!(w, "{}", document(cx, it, None, HeadingOffset::H2)); + write!(w, "{}", document(cx, it, None, HeadingOffset::H2)).unwrap(); if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) { - write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All)); + write!(w, "{}", render_assoc_items(cx, it, def_id, AssocItemRender::All)).unwrap(); } else { // We handle the "reference" primitive type on its own because we only want to list // implementations on generic types. @@ -1673,13 +1679,14 @@ fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>) bounds } -fn wrap_item<F>(w: &mut Buffer, f: F) +fn wrap_item<W, F>(w: &mut W, f: F) where - F: FnOnce(&mut Buffer), + W: fmt::Write, + F: FnOnce(&mut W), { - w.write_str(r#"<pre class="rust item-decl"><code>"#); + write!(w, r#"<pre class="rust item-decl"><code>"#).unwrap(); f(w); - w.write_str("</code></pre>"); + write!(w, "</code></pre>").unwrap(); } #[derive(PartialEq, Eq)] diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 2b0fef267e9..c31ba3304b0 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -205,7 +205,7 @@ impl UrlFragment { &UrlFragment::Item(def_id) => { let kind = match tcx.def_kind(def_id) { DefKind::AssocFn => { - if tcx.impl_defaultness(def_id).has_value() { + if tcx.defaultness(def_id).has_value() { "method." } else { "tymethod." diff --git a/src/tools/cargo b/src/tools/cargo -Subproject f7b95e31642e09c2b6eabb18ed75007dda6677a +Subproject b0fa79679e717cd077b7fc0fa4166f47107f1ba diff --git a/src/tools/clippy/.github/ISSUE_TEMPLATE/new_lint.yml b/src/tools/clippy/.github/ISSUE_TEMPLATE/new_lint.yml index 0b43d8d70c0..b49493edce1 100644 --- a/src/tools/clippy/.github/ISSUE_TEMPLATE/new_lint.yml +++ b/src/tools/clippy/.github/ISSUE_TEMPLATE/new_lint.yml @@ -12,29 +12,6 @@ body: description: What does this lint do? validations: required: true - - type: input - id: lint-name - attributes: - label: Lint Name - description: Please provide the lint name. - - type: dropdown - id: category - attributes: - label: Category - description: > - What category should this lint go into? If you're unsure you can select - multiple categories. You can find a category description in the - `README`. - multiple: true - options: - - correctness - - suspicious - - style - - complexity - - perf - - pedantic - - restriction - - cargo - type: textarea id: advantage attributes: diff --git a/src/tools/clippy/CHANGELOG.md b/src/tools/clippy/CHANGELOG.md index 79f2a47110b..8b609b47d81 100644 --- a/src/tools/clippy/CHANGELOG.md +++ b/src/tools/clippy/CHANGELOG.md @@ -6,7 +6,132 @@ document. ## Unreleased / Beta / In Rust Nightly -[149392b0...master](https://github.com/rust-lang/rust-clippy/compare/149392b0...master) +[83e42a23...master](https://github.com/rust-lang/rust-clippy/compare/83e42a23...master) + +## Rust 1.70 + +Current beta, released 2023-06-01 + +[149392b0...83e42a23](https://github.com/rust-lang/rust-clippy/compare/149392b0...83e42a23) + +### New Lints + +* [`large_futures`] + [#10414](https://github.com/rust-lang/rust-clippy/pull/10414) +* [`missing_assert_message`] + [#10362](https://github.com/rust-lang/rust-clippy/pull/10362) +* [`clear_with_drain`] + [#10528](https://github.com/rust-lang/rust-clippy/pull/10528) +* [`redundant_async_block`] + [#10448](https://github.com/rust-lang/rust-clippy/pull/10448) +* [`collection_is_never_read`] + [#10415](https://github.com/rust-lang/rust-clippy/pull/10415) +* [`let_with_type_underscore`] + [#10467](https://github.com/rust-lang/rust-clippy/pull/10467) +* [`tests_outside_test_module`] + [#10543](https://github.com/rust-lang/rust-clippy/pull/10543) +* [`allow_attributes`] + [#10481](https://github.com/rust-lang/rust-clippy/pull/10481) +* [`suspicious_doc_comments`] + [#10497](https://github.com/rust-lang/rust-clippy/pull/10497) +* [`unnecessary_box_returns`] + [#9102](https://github.com/rust-lang/rust-clippy/pull/9102) +* [`manual_main_separator_str`] + [#10483](https://github.com/rust-lang/rust-clippy/pull/10483) +* [`unnecessary_struct_initialization`] + [#10489](https://github.com/rust-lang/rust-clippy/pull/10489) +* [`manual_slice_size_calculation`] + [#10601](https://github.com/rust-lang/rust-clippy/pull/10601) +* [`lines_filter_map_ok`] + [#10534](https://github.com/rust-lang/rust-clippy/pull/10534) + +### Moves and Deprecations + +* Moved [`let_underscore_untyped`] to `restriction` + [#10442](https://github.com/rust-lang/rust-clippy/pull/10442) + +### Enhancements + +* [`extra_unused_type_parameters`]: No longer lints on public items if `avoid-breaking-exported-api` is set + [#10536](https://github.com/rust-lang/rust-clippy/pull/10536) +* [`len_without_is_empty`]: Now also detects `async` functions + [#10359](https://github.com/rust-lang/rust-clippy/pull/10359) +* [`arithmetic_side_effects`]: Now correctly handles divisions and modulo expressions if the right-hand-side + is unknown + [#10585](https://github.com/rust-lang/rust-clippy/pull/10585) +* [`nonminimal_bool`]: No longer ignores `#[allow]` attributes + [#10588](https://github.com/rust-lang/rust-clippy/pull/10588) +* [`uninit_vec`], [`uninit_assumed_init`]: Now uses a better heuristic + [#10520](https://github.com/rust-lang/rust-clippy/pull/10520) +* [`ifs_same_cond`]: Now also detects immutable method calls. + [#10350](https://github.com/rust-lang/rust-clippy/pull/10350) +* [`arithmetic_side_effects`]: No longer lints on right or left shifts with constant integers, as the + compiler warns about them + [#10309](https://github.com/rust-lang/rust-clippy/pull/10309) +* [`items_after_statements`]: `#[allow(items_after_statements)]` now works on items + [#10542](https://github.com/rust-lang/rust-clippy/pull/10542) +* [`significant_drop_tightening`]: Was optimized + [#10533](https://github.com/rust-lang/rust-clippy/pull/10533) + +### False Positive Fixes + +* [`single_component_path_imports`]: No longer lints if the import is used relative to `self` + [#10566](https://github.com/rust-lang/rust-clippy/pull/10566) +* [`derivable_impls`]: No longer suggests deriving `Default` on generics with implicit arguments + [#10399](https://github.com/rust-lang/rust-clippy/pull/10399) +* [`let_unit_value`]: No longer lints if the expression contains an `await` + [#10439](https://github.com/rust-lang/rust-clippy/pull/10439) +* [`double_must_use`]: Now ignores `async` functions + [#10589](https://github.com/rust-lang/rust-clippy/pull/10589) +* [`manual_clamp`]: No longer lints in constant context + [#10479](https://github.com/rust-lang/rust-clippy/pull/10479) +* [`almost_swapped`]: Now ignores external macros + [#10502](https://github.com/rust-lang/rust-clippy/pull/10502) +* [`nonminimal_bool`]: Now ignores macros + [#10527](https://github.com/rust-lang/rust-clippy/pull/10527) +* [`needless_return`]: No longer lints match statements with incompatible branches + [#10593](https://github.com/rust-lang/rust-clippy/pull/10593) +* [`use_self`]: Do not suggest using `Self` in const generic parameters + [#10375](https://github.com/rust-lang/rust-clippy/pull/10375) +* [`mem_replace_option_with_none`]: No longer lints on field expressions + [#10594](https://github.com/rust-lang/rust-clippy/pull/10594) +* [`items_after_statements`]: No longer lints on times from macros + [#10542](https://github.com/rust-lang/rust-clippy/pull/10542) +* [`print_literal`], [`write_literal`]: No longer lint strings coming from the `file!()` macro + [#10573](https://github.com/rust-lang/rust-clippy/pull/10573) +* [`uninit_vec`], [`uninit_assumed_init`]: Now check the types inside arrays and tuples + [#10553](https://github.com/rust-lang/rust-clippy/pull/10553) +* [`almost_swapped`]: No longer lints if a variable is assigned to itself + [#10499](https://github.com/rust-lang/rust-clippy/pull/10499) +* [`missing_docs_in_private_items`]: No longer lints on public items + [#10324](https://github.com/rust-lang/rust-clippy/pull/10324) + +### Suggestion Fixes/Improvements + +* [`extra_unused_type_parameters`]: The suggestion is now machine applicable + [#10536](https://github.com/rust-lang/rust-clippy/pull/10536) +* [`match_single_binding`]: Now adds a semicolon after the suggestion + [#10470](https://github.com/rust-lang/rust-clippy/pull/10470) +* [`missing_const_for_fn`]: Now includes a note if the change could break compatibility + [#10618](https://github.com/rust-lang/rust-clippy/pull/10618) +* [`cast_possible_truncation`]: Corrected suggestion for float and wildcard casts + [#10496](https://github.com/rust-lang/rust-clippy/pull/10496) +* [`transmutes_expressible_as_ptr_casts`]: The suggestion now includes parentheses when they are required + [#10454](https://github.com/rust-lang/rust-clippy/pull/10454) + +### ICE Fixes + +* [`needless_borrow`]: No longer panics on ambiguous projections + [#10403](https://github.com/rust-lang/rust-clippy/pull/10403) +* [`multiple_unsafe_ops_per_block`]: Fix ICE when calling a function-like object in an unsafe block + [#10405](https://github.com/rust-lang/rust-clippy/pull/10405) + +### Others + +* `clippy-driver` now searches parent directories for `clippy.toml` files + [#10592](https://github.com/rust-lang/rust-clippy/pull/10592) +* Fixed a deserialization error for the `array-size-threshold` config value + [#10423](https://github.com/rust-lang/rust-clippy/pull/10423) ## Rust 1.69 @@ -4838,6 +4963,7 @@ Released 2018-09-13 [`missing_docs_in_private_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items [`missing_enforced_import_renames`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_enforced_import_renames [`missing_errors_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc +[`missing_fields_in_debug`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_fields_in_debug [`missing_inline_in_public_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_inline_in_public_items [`missing_panics_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc [`missing_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc @@ -4874,6 +5000,7 @@ Released 2018-09-13 [`needless_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect [`needless_continue`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_continue [`needless_doctest_main`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main +[`needless_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_else [`needless_for_each`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_for_each [`needless_late_init`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_late_init [`needless_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes @@ -4949,6 +5076,7 @@ Released 2018-09-13 [`println_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#println_empty_string [`ptr_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg [`ptr_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr +[`ptr_cast_constness`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness [`ptr_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq [`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast [`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names diff --git a/src/tools/clippy/Cargo.toml b/src/tools/clippy/Cargo.toml index 3c72bb62ed1..ca8bf9fac91 100644 --- a/src/tools/clippy/Cargo.toml +++ b/src/tools/clippy/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy" -version = "0.1.71" +version = "0.1.72" description = "A bunch of helpful lints to avoid common pitfalls in Rust" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" @@ -30,16 +30,11 @@ termize = "0.1" compiletest_rs = { version = "0.10", features = ["tmp"] } tester = "0.9" regex = "1.5" -toml = "0.5" +toml = "0.7.3" walkdir = "2.3" # This is used by the `collect-metadata` alias. filetime = "0.2" -# A noop dependency that changes in the Rust repository, it's a bit of a hack. -# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust` -# for more information. -rustc-workspace-hack = "1.0" - # UI test dependencies clap = { version = "4.1.4", features = ["derive"] } clippy_utils = { path = "clippy_utils" } diff --git a/src/tools/clippy/book/src/configuration.md b/src/tools/clippy/book/src/configuration.md index 1304f6a8c2f..e8274bc4575 100644 --- a/src/tools/clippy/book/src/configuration.md +++ b/src/tools/clippy/book/src/configuration.md @@ -2,8 +2,14 @@ > **Note:** The configuration file is unstable and may be deprecated in the future. -Some lints can be configured in a TOML file named `clippy.toml` or `.clippy.toml`. It contains a -basic `variable = value` mapping e.g. +Some lints can be configured in a TOML file named `clippy.toml` or `.clippy.toml`, which is searched for in: + +1. The directory specified by the `CLIPPY_CONF_DIR` environment variable, or +2. The directory specified by the +[CARGO_MANIFEST_DIR](https://doc.rust-lang.org/cargo/reference/environment-variables.html) environment variable, or +3. The current directory. + +It contains a basic `variable = value` mapping e.g. ```toml avoid-breaking-exported-api = false diff --git a/src/tools/clippy/book/src/development/adding_lints.md b/src/tools/clippy/book/src/development/adding_lints.md index ccae8d37420..c26aa883eba 100644 --- a/src/tools/clippy/book/src/development/adding_lints.md +++ b/src/tools/clippy/book/src/development/adding_lints.md @@ -630,8 +630,14 @@ Before submitting your PR make sure you followed all the basic requirements: ## Adding configuration to a lint -Clippy supports the configuration of lints values using a `clippy.toml` file in -the workspace directory. Adding a configuration to a lint can be useful for +Clippy supports the configuration of lints values using a `clippy.toml` file which is searched for in: + +1. The directory specified by the `CLIPPY_CONF_DIR` environment variable, or +2. The directory specified by the +[CARGO_MANIFEST_DIR](https://doc.rust-lang.org/cargo/reference/environment-variables.html) environment variable, or +3. The current directory. + +Adding a configuration to a lint can be useful for thresholds or to constrain some behavior that can be seen as a false positive for some users. Adding a configuration is done in the following steps: diff --git a/src/tools/clippy/clippy_dev/src/main.rs b/src/tools/clippy/clippy_dev/src/main.rs index e2457e5a8a5..c03fbe9d275 100644 --- a/src/tools/clippy/clippy_dev/src/main.rs +++ b/src/tools/clippy/clippy_dev/src/main.rs @@ -5,6 +5,7 @@ use clap::{Arg, ArgAction, ArgMatches, Command}; use clippy_dev::{bless, dogfood, fmt, lint, new_lint, serve, setup, update_lints}; use indoc::indoc; +use std::convert::Infallible; fn main() { let matches = get_clap_config(); @@ -180,7 +181,8 @@ fn get_clap_config() -> ArgMatches { .short('n') .long("name") .help("Name of the new lint in snake case, ex: fn_too_long") - .required(true), + .required(true) + .value_parser(|name: &str| Ok::<_, Infallible>(name.replace('-', "_"))), Arg::new("category") .short('c') .long("category") diff --git a/src/tools/clippy/clippy_lints/Cargo.toml b/src/tools/clippy/clippy_lints/Cargo.toml index 98e69c7fd26..be9261a4704 100644 --- a/src/tools/clippy/clippy_lints/Cargo.toml +++ b/src/tools/clippy/clippy_lints/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy_lints" -version = "0.1.71" +version = "0.1.72" description = "A bunch of helpful lints to avoid common pitfalls in Rust" repository = "https://github.com/rust-lang/rust-clippy" readme = "README.md" @@ -20,15 +20,13 @@ quine-mc_cluskey = "0.2" regex-syntax = "0.7" serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", optional = true } -tempfile = { version = "3.2", optional = true } -toml = "0.5" +tempfile = { version = "3.3.0", optional = true } +toml = "0.7.3" unicode-normalization = "0.1" unicode-script = { version = "0.5", default-features = false } semver = "1.0" rustc-semver = "1.1" -# NOTE: cargo requires serde feat in its url dep -# see <https://github.com/rust-lang/rust/pull/63587#issuecomment-522343864> -url = { version = "2.2", features = ["serde"] } +url = "2.2" [features] deny-warnings = ["clippy_utils/deny-warnings"] diff --git a/src/tools/clippy/clippy_lints/src/allow_attributes.rs b/src/tools/clippy/clippy_lints/src/allow_attributes.rs index add73d0aeee..554efdc58e1 100644 --- a/src/tools/clippy/clippy_lints/src/allow_attributes.rs +++ b/src/tools/clippy/clippy_lints/src/allow_attributes.rs @@ -40,7 +40,7 @@ declare_clippy_lint! { /// a.len() /// } /// ``` - #[clippy::version = "1.69.0"] + #[clippy::version = "1.70.0"] pub ALLOW_ATTRIBUTES, restriction, "`#[allow]` will not trigger if a warning isn't found. `#[expect]` triggers if there are no warnings." diff --git a/src/tools/clippy/clippy_lints/src/booleans.rs b/src/tools/clippy/clippy_lints/src/booleans.rs index 455f0df7cd0..ff0102255a0 100644 --- a/src/tools/clippy/clippy_lints/src/booleans.rs +++ b/src/tools/clippy/clippy_lints/src/booleans.rs @@ -88,7 +88,6 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool { NonminimalBoolVisitor { cx }.visit_body(body); } } - struct NonminimalBoolVisitor<'a, 'tcx> { cx: &'a LateContext<'tcx>, } @@ -473,6 +472,10 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> { self.bool_expr(e); }, ExprKind::Unary(UnOp::Not, inner) => { + if let ExprKind::Unary(UnOp::Not, ex) = inner.kind && + !self.cx.typeck_results().node_types()[ex.hir_id].is_bool() { + return; + } if self.cx.typeck_results().node_types()[inner.hir_id].is_bool() { self.bool_expr(e); } diff --git a/src/tools/clippy/clippy_lints/src/casts/mod.rs b/src/tools/clippy/clippy_lints/src/casts/mod.rs index 0c175372aab..b90dab07a27 100644 --- a/src/tools/clippy/clippy_lints/src/casts/mod.rs +++ b/src/tools/clippy/clippy_lints/src/casts/mod.rs @@ -17,6 +17,7 @@ mod fn_to_numeric_cast; mod fn_to_numeric_cast_any; mod fn_to_numeric_cast_with_truncation; mod ptr_as_ptr; +mod ptr_cast_constness; mod unnecessary_cast; mod utils; @@ -363,7 +364,7 @@ declare_clippy_lint! { /// namely `*const T` to `*const U` and `*mut T` to `*mut U`. /// /// ### Why is this bad? - /// Though `as` casts between raw pointers is not terrible, `pointer::cast` is safer because + /// Though `as` casts between raw pointers are not terrible, `pointer::cast` is safer because /// it cannot accidentally change the pointer's mutability nor cast the pointer to other types like `usize`. /// /// ### Example @@ -388,6 +389,34 @@ declare_clippy_lint! { declare_clippy_lint! { /// ### What it does + /// Checks for `as` casts between raw pointers which change its constness, namely `*const T` to + /// `*mut T` and `*mut T` to `*const T`. + /// + /// ### Why is this bad? + /// Though `as` casts between raw pointers are not terrible, `pointer::cast_mut` and + /// `pointer::cast_const` are safer because they cannot accidentally cast the pointer to another + /// type. + /// + /// ### Example + /// ```rust + /// let ptr: *const u32 = &42_u32; + /// let mut_ptr = ptr as *mut u32; + /// let ptr = mut_ptr as *const u32; + /// ``` + /// Use instead: + /// ```rust + /// let ptr: *const u32 = &42_u32; + /// let mut_ptr = ptr.cast_mut(); + /// let ptr = mut_ptr.cast_const(); + /// ``` + #[clippy::version = "1.71.0"] + pub PTR_CAST_CONSTNESS, + pedantic, + "casting using `as` from and to raw pointers to change constness when specialized methods apply" +} + +declare_clippy_lint! { + /// ### What it does /// Checks for casts from an enum type to an integral type which will definitely truncate the /// value. /// @@ -652,6 +681,7 @@ impl_lint_pass!(Casts => [ FN_TO_NUMERIC_CAST_WITH_TRUNCATION, CHAR_LIT_AS_U8, PTR_AS_PTR, + PTR_CAST_CONSTNESS, CAST_ENUM_TRUNCATION, CAST_ENUM_CONSTRUCTOR, CAST_ABS_TO_UNSIGNED, @@ -685,6 +715,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts { return; } cast_slice_from_raw_parts::check(cx, expr, cast_expr, cast_to, &self.msrv); + ptr_cast_constness::check(cx, expr, cast_expr, cast_from, 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); diff --git a/src/tools/clippy/clippy_lints/src/casts/ptr_cast_constness.rs b/src/tools/clippy/clippy_lints/src/casts/ptr_cast_constness.rs new file mode 100644 index 00000000000..ab015f8822e --- /dev/null +++ b/src/tools/clippy/clippy_lints/src/casts/ptr_cast_constness.rs @@ -0,0 +1,44 @@ +use clippy_utils::msrvs::POINTER_CAST_CONSTNESS; +use clippy_utils::sugg::Sugg; +use clippy_utils::{diagnostics::span_lint_and_sugg, msrvs::Msrv}; +use if_chain::if_chain; +use rustc_errors::Applicability; +use rustc_hir::{Expr, Mutability}; +use rustc_lint::LateContext; +use rustc_middle::ty::{self, Ty, TypeAndMut}; + +use super::PTR_CAST_CONSTNESS; + +pub(super) fn check( + cx: &LateContext<'_>, + expr: &Expr<'_>, + cast_expr: &Expr<'_>, + cast_from: Ty<'_>, + cast_to: Ty<'_>, + msrv: &Msrv, +) { + if_chain! { + if msrv.meets(POINTER_CAST_CONSTNESS); + if let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, .. }) = cast_from.kind(); + if let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, .. }) = cast_to.kind(); + if matches!((from_mutbl, to_mutbl), + (Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)); + then { + let sugg = Sugg::hir(cx, cast_expr, "_"); + let constness = match *to_mutbl { + Mutability::Not => "const", + Mutability::Mut => "mut", + }; + + span_lint_and_sugg( + cx, + PTR_CAST_CONSTNESS, + expr.span, + "`as` casting between raw pointers while changing its constness", + &format!("try `pointer::cast_{constness}`, a safer alternative"), + format!("{}.cast_{constness}()", sugg.maybe_par()), + Applicability::MachineApplicable, + ); + } + } +} diff --git a/src/tools/clippy/clippy_lints/src/collection_is_never_read.rs b/src/tools/clippy/clippy_lints/src/collection_is_never_read.rs index 5e2eb5789f6..ac5ac542cf9 100644 --- a/src/tools/clippy/clippy_lints/src/collection_is_never_read.rs +++ b/src/tools/clippy/clippy_lints/src/collection_is_never_read.rs @@ -37,7 +37,7 @@ declare_clippy_lint! { /// println!("{sample}"); /// } /// ``` - #[clippy::version = "1.69.0"] + #[clippy::version = "1.70.0"] pub COLLECTION_IS_NEVER_READ, nursery, "a collection is never queried" diff --git a/src/tools/clippy/clippy_lints/src/declared_lints.rs b/src/tools/clippy/clippy_lints/src/declared_lints.rs index 212e809d4c5..15ff8be0fd9 100644 --- a/src/tools/clippy/clippy_lints/src/declared_lints.rs +++ b/src/tools/clippy/clippy_lints/src/declared_lints.rs @@ -89,6 +89,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::casts::FN_TO_NUMERIC_CAST_ANY_INFO, crate::casts::FN_TO_NUMERIC_CAST_WITH_TRUNCATION_INFO, crate::casts::PTR_AS_PTR_INFO, + crate::casts::PTR_CAST_CONSTNESS_INFO, crate::casts::UNNECESSARY_CAST_INFO, crate::checked_conversions::CHECKED_CONVERSIONS_INFO, crate::cognitive_complexity::COGNITIVE_COMPLEXITY_INFO, @@ -427,6 +428,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::missing_const_for_fn::MISSING_CONST_FOR_FN_INFO, crate::missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS_INFO, crate::missing_enforced_import_rename::MISSING_ENFORCED_IMPORT_RENAMES_INFO, + crate::missing_fields_in_debug::MISSING_FIELDS_IN_DEBUG_INFO, crate::missing_inline::MISSING_INLINE_IN_PUBLIC_ITEMS_INFO, crate::missing_trait_methods::MISSING_TRAIT_METHODS_INFO, crate::mixed_read_write_in_expression::DIVERGING_SUB_EXPRESSION_INFO, @@ -447,6 +449,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::needless_bool::NEEDLESS_BOOL_ASSIGN_INFO, crate::needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE_INFO, crate::needless_continue::NEEDLESS_CONTINUE_INFO, + crate::needless_else::NEEDLESS_ELSE_INFO, crate::needless_for_each::NEEDLESS_FOR_EACH_INFO, crate::needless_late_init::NEEDLESS_LATE_INIT_INFO, crate::needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS_INFO, diff --git a/src/tools/clippy/clippy_lints/src/default_constructed_unit_structs.rs b/src/tools/clippy/clippy_lints/src/default_constructed_unit_structs.rs index 9bd7a0dc0f3..fb037bbcbf3 100644 --- a/src/tools/clippy/clippy_lints/src/default_constructed_unit_structs.rs +++ b/src/tools/clippy/clippy_lints/src/default_constructed_unit_structs.rs @@ -1,4 +1,4 @@ -use clippy_utils::{diagnostics::span_lint_and_sugg, match_def_path, paths}; +use clippy_utils::{diagnostics::span_lint_and_sugg, is_ty_alias, match_def_path, paths}; use hir::{def::Res, ExprKind}; use rustc_errors::Applicability; use rustc_hir as hir; @@ -43,12 +43,23 @@ declare_clippy_lint! { } declare_lint_pass!(DefaultConstructedUnitStructs => [DEFAULT_CONSTRUCTED_UNIT_STRUCTS]); +fn is_alias(ty: hir::Ty<'_>) -> bool { + if let hir::TyKind::Path(ref qpath) = ty.kind { + is_ty_alias(qpath) + } else { + false + } +} + impl LateLintPass<'_> for DefaultConstructedUnitStructs { fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { if_chain!( // make sure we have a call to `Default::default` if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind; - if let ExprKind::Path(ref qpath@ hir::QPath::TypeRelative(_,_)) = fn_expr.kind; + if let ExprKind::Path(ref qpath @ hir::QPath::TypeRelative(base, _)) = fn_expr.kind; + // make sure this isn't a type alias: + // `<Foo as Bar>::Assoc` cannot be used as a constructor + if !is_alias(*base); if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id); if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD); // make sure we have a struct with no fields (unit struct) diff --git a/src/tools/clippy/clippy_lints/src/large_futures.rs b/src/tools/clippy/clippy_lints/src/large_futures.rs index 0ca31033b16..087c4a65250 100644 --- a/src/tools/clippy/clippy_lints/src/large_futures.rs +++ b/src/tools/clippy/clippy_lints/src/large_futures.rs @@ -38,7 +38,7 @@ declare_clippy_lint! { /// wait(fut).await; /// } /// ``` - #[clippy::version = "1.68.0"] + #[clippy::version = "1.70.0"] pub LARGE_FUTURES, pedantic, "large future may lead to unexpected stack overflows" diff --git a/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs b/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs index 32c6312e069..0a5901bce04 100644 --- a/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs +++ b/src/tools/clippy/clippy_lints/src/large_stack_arrays.rs @@ -38,7 +38,7 @@ impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]); impl<'tcx> LateLintPass<'tcx> for LargeStackArrays { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { - if let ExprKind::Repeat(_, _) = expr.kind + if let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind && let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind() && let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind() && let Ok(element_count) = element_count.try_to_target_usize(cx.tcx) diff --git a/src/tools/clippy/clippy_lints/src/let_with_type_underscore.rs b/src/tools/clippy/clippy_lints/src/let_with_type_underscore.rs index c01e3882d52..2f10e3d2581 100644 --- a/src/tools/clippy/clippy_lints/src/let_with_type_underscore.rs +++ b/src/tools/clippy/clippy_lints/src/let_with_type_underscore.rs @@ -17,7 +17,7 @@ declare_clippy_lint! { /// ```rust,ignore /// let my_number = 1; /// ``` - #[clippy::version = "1.69.0"] + #[clippy::version = "1.70.0"] pub LET_WITH_TYPE_UNDERSCORE, complexity, "unneeded underscore type (`_`) in a variable declaration" diff --git a/src/tools/clippy/clippy_lints/src/lib.rs b/src/tools/clippy/clippy_lints/src/lib.rs index fcca595c2bc..4a23edb58aa 100644 --- a/src/tools/clippy/clippy_lints/src/lib.rs +++ b/src/tools/clippy/clippy_lints/src/lib.rs @@ -202,6 +202,7 @@ mod missing_assert_message; mod missing_const_for_fn; mod missing_doc; mod missing_enforced_import_rename; +mod missing_fields_in_debug; mod missing_inline; mod missing_trait_methods; mod mixed_read_write_in_expression; @@ -217,6 +218,7 @@ mod needless_arbitrary_self_type; mod needless_bool; mod needless_borrowed_ref; mod needless_continue; +mod needless_else; mod needless_for_each; mod needless_late_init; mod needless_parens_on_range_literals; @@ -333,7 +335,7 @@ mod zero_sized_map_values; pub use crate::utils::conf::{lookup_conf_file, Conf}; use crate::utils::{ - conf::{format_error, metadata::get_configuration_metadata, TryConf}, + conf::{metadata::get_configuration_metadata, TryConf}, FindAll, }; @@ -369,23 +371,36 @@ pub fn read_conf(sess: &Session, path: &io::Result<(Option<PathBuf>, Vec<String> }, }; - let TryConf { conf, errors, warnings } = utils::conf::read(file_name); + let TryConf { conf, errors, warnings } = utils::conf::read(sess, file_name); // all conf errors are non-fatal, we just use the default conf in case of error for error in errors { - sess.err(format!( - "error reading Clippy's configuration file `{}`: {}", - file_name.display(), - format_error(error) - )); + if let Some(span) = error.span { + sess.span_err( + span, + format!("error reading Clippy's configuration file: {}", error.message), + ); + } else { + sess.err(format!( + "error reading Clippy's configuration file `{}`: {}", + file_name.display(), + error.message + )); + } } for warning in warnings { - sess.struct_warn(format!( - "error reading Clippy's configuration file `{}`: {}", - file_name.display(), - format_error(warning) - )) - .emit(); + if let Some(span) = warning.span { + sess.span_warn( + span, + format!("error reading Clippy's configuration file: {}", warning.message), + ); + } else { + sess.warn(format!( + "error reading Clippy's configuration file `{}`: {}", + file_name.display(), + warning.message + )); + } } conf @@ -990,6 +1005,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule)); store.register_early_pass(|| Box::new(ref_patterns::RefPatterns)); store.register_late_pass(|_| Box::new(default_constructed_unit_structs::DefaultConstructedUnitStructs)); + store.register_early_pass(|| Box::new(needless_else::NeedlessElse)); + store.register_late_pass(|_| Box::new(missing_fields_in_debug::MissingFieldsInDebug)); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/src/tools/clippy/clippy_lints/src/manual_let_else.rs b/src/tools/clippy/clippy_lints/src/manual_let_else.rs index 3f8b42ffe80..389b0a4a62d 100644 --- a/src/tools/clippy/clippy_lints/src/manual_let_else.rs +++ b/src/tools/clippy/clippy_lints/src/manual_let_else.rs @@ -77,53 +77,54 @@ impl<'tcx> LateLintPass<'tcx> for ManualLetElse { local.els.is_none() && local.ty.is_none() && init.span.ctxt() == stmt.span.ctxt() && - let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init) { - match if_let_or_match { - IfLetOrMatch::IfLet(if_let_expr, let_pat, if_then, if_else) => if_chain! { - if expr_is_simple_identity(let_pat, if_then); - if let Some(if_else) = if_else; - if expr_diverges(cx, if_else); - then { - emit_manual_let_else(cx, stmt.span, if_let_expr, local.pat, let_pat, if_else); - } - }, - IfLetOrMatch::Match(match_expr, arms, source) => { - if self.matches_behaviour == MatchLintBehaviour::Never { - return; - } - if source != MatchSource::Normal { - return; - } - // Any other number than two arms doesn't (necessarily) - // have a trivial mapping to let else. - if arms.len() != 2 { - return; - } - // Guards don't give us an easy mapping either - if arms.iter().any(|arm| arm.guard.is_some()) { - return; - } - let check_types = self.matches_behaviour == MatchLintBehaviour::WellKnownTypes; - let diverging_arm_opt = arms - .iter() - .enumerate() - .find(|(_, arm)| expr_diverges(cx, arm.body) && pat_allowed_for_else(cx, arm.pat, check_types)); - let Some((idx, diverging_arm)) = diverging_arm_opt else { return; }; - // If the non-diverging arm is the first one, its pattern can be reused in a let/else statement. - // However, if it arrives in second position, its pattern may cover some cases already covered - // by the diverging one. - // TODO: accept the non-diverging arm as a second position if patterns are disjointed. - if idx == 0 { - return; - } - let pat_arm = &arms[1 - idx]; - if !expr_is_simple_identity(pat_arm.pat, pat_arm.body) { - return; - } + let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init) + { + match if_let_or_match { + IfLetOrMatch::IfLet(if_let_expr, let_pat, if_then, if_else) => if_chain! { + if expr_is_simple_identity(let_pat, if_then); + if let Some(if_else) = if_else; + if expr_diverges(cx, if_else); + then { + emit_manual_let_else(cx, stmt.span, if_let_expr, local.pat, let_pat, if_else); + } + }, + IfLetOrMatch::Match(match_expr, arms, source) => { + if self.matches_behaviour == MatchLintBehaviour::Never { + return; + } + if source != MatchSource::Normal { + return; + } + // Any other number than two arms doesn't (necessarily) + // have a trivial mapping to let else. + if arms.len() != 2 { + return; + } + // Guards don't give us an easy mapping either + if arms.iter().any(|arm| arm.guard.is_some()) { + return; + } + let check_types = self.matches_behaviour == MatchLintBehaviour::WellKnownTypes; + let diverging_arm_opt = arms + .iter() + .enumerate() + .find(|(_, arm)| expr_diverges(cx, arm.body) && pat_allowed_for_else(cx, arm.pat, check_types)); + let Some((idx, diverging_arm)) = diverging_arm_opt else { return; }; + // If the non-diverging arm is the first one, its pattern can be reused in a let/else statement. + // However, if it arrives in second position, its pattern may cover some cases already covered + // by the diverging one. + // TODO: accept the non-diverging arm as a second position if patterns are disjointed. + if idx == 0 { + return; + } + let pat_arm = &arms[1 - idx]; + if !expr_is_simple_identity(pat_arm.pat, pat_arm.body) { + return; + } - emit_manual_let_else(cx, stmt.span, match_expr, local.pat, pat_arm.pat, diverging_arm.body); - }, - } + emit_manual_let_else(cx, stmt.span, match_expr, local.pat, pat_arm.pat, diverging_arm.body); + }, + } }; } @@ -145,10 +146,9 @@ fn emit_manual_let_else( "this could be rewritten as `let...else`", |diag| { // This is far from perfect, for example there needs to be: - // * mut additions for the bindings - // * renamings of the bindings for `PatKind::Or` + // * tracking for multi-binding cases: let (foo, bar) = if let (Some(foo), Ok(bar)) = ... + // * renamings of the bindings for many `PatKind`s like structs, slices, etc. // * unused binding collision detection with existing ones - // * putting patterns with at the top level | inside () // for this to be machine applicable. let mut app = Applicability::HasPlaceholders; let (sn_expr, _) = snippet_with_context(cx, expr.span, span.ctxt(), "", &mut app); @@ -159,28 +159,62 @@ fn emit_manual_let_else( } else { format!("{{ {sn_else} }}") }; - let sn_bl = match pat.kind { - PatKind::Or(..) => { - let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", &mut app); - format!("({sn_pat})") - }, - // Replace the variable name iff `TupleStruct` has one argument like `Variant(v)`. - PatKind::TupleStruct(ref w, args, ..) if args.len() == 1 => { - let sn_wrapper = cx.sess().source_map().span_to_snippet(w.span()).unwrap_or_default(); - let (sn_inner, _) = snippet_with_context(cx, local.span, span.ctxt(), "", &mut app); - format!("{sn_wrapper}({sn_inner})") - }, - _ => { - let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", &mut app); - sn_pat.into_owned() - }, - }; + let sn_bl = replace_in_pattern(cx, span, local, pat, &mut app); let sugg = format!("let {sn_bl} = {sn_expr} else {else_bl};"); diag.span_suggestion(span, "consider writing", sugg, app); }, ); } +// replaces the locals in the pattern +fn replace_in_pattern( + cx: &LateContext<'_>, + span: Span, + local: &Pat<'_>, + pat: &Pat<'_>, + app: &mut Applicability, +) -> String { + let mut bindings_count = 0; + pat.each_binding_or_first(&mut |_, _, _, _| bindings_count += 1); + // If the pattern creates multiple bindings, exit early, + // as otherwise we might paste the pattern to the positions of multiple bindings. + if bindings_count > 1 { + let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", app); + return sn_pat.into_owned(); + } + + match pat.kind { + PatKind::Binding(..) => { + let (sn_bdg, _) = snippet_with_context(cx, local.span, span.ctxt(), "", app); + return sn_bdg.to_string(); + }, + PatKind::Or(pats) => { + let patterns = pats + .iter() + .map(|pat| replace_in_pattern(cx, span, local, pat, app)) + .collect::<Vec<_>>(); + let or_pat = patterns.join(" | "); + return format!("({or_pat})"); + }, + // Replace the variable name iff `TupleStruct` has one argument like `Variant(v)`. + PatKind::TupleStruct(ref w, args, dot_dot_pos) => { + let mut args = args + .iter() + .map(|pat| replace_in_pattern(cx, span, local, pat, app)) + .collect::<Vec<_>>(); + if let Some(pos) = dot_dot_pos.as_opt_usize() { + args.insert(pos, "..".to_owned()); + } + let args = args.join(", "); + let sn_wrapper = cx.sess().source_map().span_to_snippet(w.span()).unwrap_or_default(); + return format!("{sn_wrapper}({args})"); + }, + _ => {}, + } + let (sn_pat, _) = snippet_with_context(cx, pat.span, span.ctxt(), "", app); + sn_pat.into_owned() +} + /// Check whether an expression is divergent. May give false negatives. fn expr_diverges(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { struct V<'cx, 'tcx> { diff --git a/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs b/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs index 42f1e2629d4..6424ac31d9f 100644 --- a/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs +++ b/src/tools/clippy/clippy_lints/src/matches/match_wild_err_arm.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_note; use clippy_utils::macros::{is_panic, root_macro_call}; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::visitors::is_local_used; -use clippy_utils::{is_wild, peel_blocks_with_stmt}; +use clippy_utils::{in_constant, is_wild, peel_blocks_with_stmt}; use rustc_hir::{Arm, Expr, PatKind}; use rustc_lint::LateContext; use rustc_span::symbol::{kw, sym}; @@ -10,6 +10,11 @@ use rustc_span::symbol::{kw, sym}; use super::MATCH_WILD_ERR_ARM; pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'tcx>]) { + // `unwrap`/`expect` is not (yet) const, so we want to allow this in const contexts for now + if in_constant(cx, ex.hir_id) { + return; + } + let ex_ty = cx.typeck_results().expr_ty(ex).peel_refs(); if is_type_diagnostic_item(cx, ex_ty, sym::Result) { for arm in arms { diff --git a/src/tools/clippy/clippy_lints/src/matches/mod.rs b/src/tools/clippy/clippy_lints/src/matches/mod.rs index 55ec9d4474f..0d91051632a 100644 --- a/src/tools/clippy/clippy_lints/src/matches/mod.rs +++ b/src/tools/clippy/clippy_lints/src/matches/mod.rs @@ -25,7 +25,7 @@ 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, tokenize_with_text}; +use clippy_utils::{higher, in_constant, is_direct_expn_of, is_span_match, tokenize_with_text}; use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat}; use rustc_lexer::TokenKind; use rustc_lint::{LateContext, LateLintPass, LintContext}; @@ -974,12 +974,16 @@ impl_lint_pass!(Matches => [ impl<'tcx> LateLintPass<'tcx> for Matches { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if in_external_macro(cx.sess(), expr.span) { + if is_direct_expn_of(expr.span, "matches").is_none() && in_external_macro(cx.sess(), expr.span) { return; } let from_expansion = expr.span.from_expansion(); if let ExprKind::Match(ex, arms, source) = expr.kind { + if is_direct_expn_of(expr.span, "matches").is_some() { + redundant_pattern_match::check_match(cx, expr, ex, arms); + } + if source == MatchSource::Normal && !is_span_match(cx, expr.span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/matches/redundant_pattern_match.rs b/src/tools/clippy/clippy_lints/src/matches/redundant_pattern_match.rs index e81e09da425..479cfd83512 100644 --- a/src/tools/clippy/clippy_lints/src/matches/redundant_pattern_match.rs +++ b/src/tools/clippy/clippy_lints/src/matches/redundant_pattern_match.rs @@ -1,10 +1,10 @@ use super::REDUNDANT_PATTERN_MATCHING; -use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; use clippy_utils::source::{snippet, walk_span_to_context}; use clippy_utils::sugg::Sugg; use clippy_utils::ty::{is_type_diagnostic_item, needs_ordered_drop}; use clippy_utils::visitors::any_temporaries_need_ordered_drop; -use clippy_utils::{higher, is_trait_method}; +use clippy_utils::{higher, is_expn_of, is_trait_method}; use if_chain::if_chain; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; @@ -190,24 +190,19 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind); if let Some(good_method) = found_good_method(cx, arms, node_pair) { - let span = expr.span.to(op.span); + let span = is_expn_of(expr.span, "matches").unwrap_or(expr.span.to(op.span)); let result_expr = match &op.kind { ExprKind::AddrOf(_, _, borrowed) => borrowed, _ => op, }; - span_lint_and_then( + span_lint_and_sugg( cx, REDUNDANT_PATTERN_MATCHING, - expr.span, + span, &format!("redundant pattern matching, consider using `{good_method}`"), - |diag| { - diag.span_suggestion( - span, - "try this", - format!("{}.{good_method}", snippet(cx, result_expr.span, "_")), - Applicability::MaybeIncorrect, // snippet - ); - }, + "try this", + format!("{}.{good_method}", snippet(cx, result_expr.span, "_")), + Applicability::MachineApplicable, ); } } diff --git a/src/tools/clippy/clippy_lints/src/methods/mod.rs b/src/tools/clippy/clippy_lints/src/methods/mod.rs index 9a594d964ab..88cbefbb5d3 100644 --- a/src/tools/clippy/clippy_lints/src/methods/mod.rs +++ b/src/tools/clippy/clippy_lints/src/methods/mod.rs @@ -3191,7 +3191,7 @@ declare_clippy_lint! { /// let mut v = vec![1, 2, 3]; /// v.clear(); /// ``` - #[clippy::version = "1.69.0"] + #[clippy::version = "1.70.0"] pub CLEAR_WITH_DRAIN, nursery, "calling `drain` in order to `clear` a container" diff --git a/src/tools/clippy/clippy_lints/src/missing_assert_message.rs b/src/tools/clippy/clippy_lints/src/missing_assert_message.rs index 2214a568d9c..4dbb79334ca 100644 --- a/src/tools/clippy/clippy_lints/src/missing_assert_message.rs +++ b/src/tools/clippy/clippy_lints/src/missing_assert_message.rs @@ -36,7 +36,7 @@ declare_clippy_lint! { /// assert!(service.ready, "`service.poll_ready()` must be called first to ensure that service is ready to receive requests"); /// } /// ``` - #[clippy::version = "1.69.0"] + #[clippy::version = "1.70.0"] pub MISSING_ASSERT_MESSAGE, restriction, "checks assertions without a custom panic message" diff --git a/src/tools/clippy/clippy_lints/src/missing_fields_in_debug.rs b/src/tools/clippy/clippy_lints/src/missing_fields_in_debug.rs new file mode 100644 index 00000000000..b6f0de7e504 --- /dev/null +++ b/src/tools/clippy/clippy_lints/src/missing_fields_in_debug.rs @@ -0,0 +1,234 @@ +use std::ops::ControlFlow; + +use clippy_utils::{ + diagnostics::span_lint_and_then, + is_path_lang_item, paths, + ty::match_type, + visitors::{for_each_expr, Visitable}, +}; +use rustc_ast::LitKind; +use rustc_data_structures::fx::FxHashSet; +use rustc_hir::Block; +use rustc_hir::{ + def::{DefKind, Res}, + Expr, ImplItemKind, LangItem, Node, +}; +use rustc_hir::{ExprKind, Impl, ItemKind, QPath, TyKind}; +use rustc_hir::{ImplItem, Item, VariantData}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_middle::ty::Ty; +use rustc_middle::ty::TypeckResults; +use rustc_session::{declare_lint_pass, declare_tool_lint}; +use rustc_span::{sym, Span, Symbol}; + +declare_clippy_lint! { + /// ### What it does + /// Checks for manual [`core::fmt::Debug`](https://doc.rust-lang.org/core/fmt/trait.Debug.html) implementations that do not use all fields. + /// + /// ### Why is this bad? + /// A common mistake is to forget to update manual `Debug` implementations when adding a new field + /// to a struct or a new variant to an enum. + /// + /// At the same time, it also acts as a style lint to suggest using [`core::fmt::DebugStruct::finish_non_exhaustive`](https://doc.rust-lang.org/core/fmt/struct.DebugStruct.html#method.finish_non_exhaustive) + /// for the times when the user intentionally wants to leave out certain fields (e.g. to hide implementation details). + /// + /// ### Known problems + /// This lint works based on the `DebugStruct` helper types provided by the `Formatter`, + /// so this won't detect `Debug` impls that use the `write!` macro. + /// Oftentimes there is more logic to a `Debug` impl if it uses `write!` macro, so it tries + /// to be on the conservative side and not lint in those cases in an attempt to prevent false positives. + /// + /// This lint also does not look through function calls, so calling a function does not consider fields + /// used inside of that function as used by the `Debug` impl. + /// + /// Lastly, it also ignores tuple structs as their `DebugTuple` formatter does not have a `finish_non_exhaustive` + /// method, as well as enums because their exhaustiveness is already checked by the compiler when matching on the enum, + /// making it much less likely to accidentally forget to update the `Debug` impl when adding a new variant. + /// + /// ### Example + /// ```rust + /// use std::fmt; + /// struct Foo { + /// data: String, + /// // implementation detail + /// hidden_data: i32 + /// } + /// impl fmt::Debug for Foo { + /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + /// formatter + /// .debug_struct("Foo") + /// .field("data", &self.data) + /// .finish() + /// } + /// } + /// ``` + /// Use instead: + /// ```rust + /// use std::fmt; + /// struct Foo { + /// data: String, + /// // implementation detail + /// hidden_data: i32 + /// } + /// impl fmt::Debug for Foo { + /// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + /// formatter + /// .debug_struct("Foo") + /// .field("data", &self.data) + /// .finish_non_exhaustive() + /// } + /// } + /// ``` + #[clippy::version = "1.70.0"] + pub MISSING_FIELDS_IN_DEBUG, + pedantic, + "missing fields in manual `Debug` implementation" +} +declare_lint_pass!(MissingFieldsInDebug => [MISSING_FIELDS_IN_DEBUG]); + +fn report_lints(cx: &LateContext<'_>, span: Span, span_notes: Vec<(Span, &'static str)>) { + span_lint_and_then( + cx, + MISSING_FIELDS_IN_DEBUG, + span, + "manual `Debug` impl does not include all fields", + |diag| { + for (span, note) in span_notes { + diag.span_note(span, note); + } + diag.help("consider including all fields in this `Debug` impl") + .help("consider calling `.finish_non_exhaustive()` if you intend to ignore fields"); + }, + ); +} + +/// Checks if we should lint in a block of code +/// +/// The way we check for this condition is by checking if there is +/// a call to `Formatter::debug_struct` but no call to `.finish_non_exhaustive()`. +fn should_lint<'tcx>( + cx: &LateContext<'tcx>, + typeck_results: &TypeckResults<'tcx>, + block: impl Visitable<'tcx>, +) -> bool { + // Is there a call to `DebugStruct::finish_non_exhaustive`? Don't lint if there is. + let mut has_finish_non_exhaustive = false; + // Is there a call to `DebugStruct::debug_struct`? Do lint if there is. + let mut has_debug_struct = false; + + for_each_expr(block, |expr| { + if let ExprKind::MethodCall(path, recv, ..) = &expr.kind { + let recv_ty = typeck_results.expr_ty(recv).peel_refs(); + + if path.ident.name == sym::debug_struct && match_type(cx, recv_ty, &paths::FORMATTER) { + has_debug_struct = true; + } else if path.ident.name == sym!(finish_non_exhaustive) && match_type(cx, recv_ty, &paths::DEBUG_STRUCT) { + has_finish_non_exhaustive = true; + } + } + ControlFlow::<!, _>::Continue(()) + }); + + !has_finish_non_exhaustive && has_debug_struct +} + +/// Checks if the given expression is a call to `DebugStruct::field` +/// and the first argument to it is a string literal and if so, returns it +/// +/// Example: `.field("foo", ....)` returns `Some("foo")` +fn as_field_call<'tcx>( + cx: &LateContext<'tcx>, + typeck_results: &TypeckResults<'tcx>, + expr: &Expr<'_>, +) -> Option<Symbol> { + if let ExprKind::MethodCall(path, recv, [debug_field, _], _) = &expr.kind + && let recv_ty = typeck_results.expr_ty(recv).peel_refs() + && match_type(cx, recv_ty, &paths::DEBUG_STRUCT) + && path.ident.name == sym::field + && let ExprKind::Lit(lit) = &debug_field.kind + && let LitKind::Str(sym, ..) = lit.node + { + Some(sym) + } else { + None + } +} + +/// Attempts to find unused fields assuming that the item is a struct +fn check_struct<'tcx>( + cx: &LateContext<'tcx>, + typeck_results: &TypeckResults<'tcx>, + block: &'tcx Block<'tcx>, + self_ty: Ty<'tcx>, + item: &'tcx Item<'tcx>, + data: &VariantData<'_>, +) { + // Is there a "direct" field access anywhere (i.e. self.foo)? + // We don't want to lint if there is not, because the user might have + // a newtype struct and use fields from the wrapped type only. + let mut has_direct_field_access = false; + let mut field_accesses = FxHashSet::default(); + + for_each_expr(block, |expr| { + if let ExprKind::Field(target, ident) = expr.kind + && let target_ty = typeck_results.expr_ty_adjusted(target).peel_refs() + && target_ty == self_ty + { + field_accesses.insert(ident.name); + has_direct_field_access = true; + } else if let Some(sym) = as_field_call(cx, typeck_results, expr) { + field_accesses.insert(sym); + } + ControlFlow::<!, _>::Continue(()) + }); + + let span_notes = data + .fields() + .iter() + .filter_map(|field| { + if field_accesses.contains(&field.ident.name) || is_path_lang_item(cx, field.ty, LangItem::PhantomData) { + None + } else { + Some((field.span, "this field is unused")) + } + }) + .collect::<Vec<_>>(); + + // only lint if there's also at least one direct field access to allow patterns + // where one might have a newtype struct and uses fields from the wrapped type + if !span_notes.is_empty() && has_direct_field_access { + report_lints(cx, item.span, span_notes); + } +} + +impl<'tcx> LateLintPass<'tcx> for MissingFieldsInDebug { + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx rustc_hir::Item<'tcx>) { + // is this an `impl Debug for X` block? + if let ItemKind::Impl(Impl { of_trait: Some(trait_ref), self_ty, items, .. }) = item.kind + && let Res::Def(DefKind::Trait, trait_def_id) = trait_ref.path.res + && let TyKind::Path(QPath::Resolved(_, self_path)) = &self_ty.kind + && cx.match_def_path(trait_def_id, &[sym::core, sym::fmt, sym::Debug]) + // don't trigger if this impl was derived + && !cx.tcx.has_attr(item.owner_id, sym::automatically_derived) + && !item.span.from_expansion() + // find `Debug::fmt` function + && let Some(fmt_item) = items.iter().find(|i| i.ident.name == sym::fmt) + && let ImplItem { kind: ImplItemKind::Fn(_, body_id), .. } = cx.tcx.hir().impl_item(fmt_item.id) + && let body = cx.tcx.hir().body(*body_id) + && let ExprKind::Block(block, _) = body.value.kind + // inspect `self` + && let self_ty = cx.tcx.type_of(self_path.res.def_id()).skip_binder().peel_refs() + && let Some(self_adt) = self_ty.ty_adt_def() + && let Some(self_def_id) = self_adt.did().as_local() + && let Some(Node::Item(self_item)) = cx.tcx.hir().find_by_def_id(self_def_id) + // NB: can't call cx.typeck_results() as we are not in a body + && let typeck_results = cx.tcx.typeck_body(*body_id) + && should_lint(cx, typeck_results, block) + { + // we intentionally only lint structs, see lint description + if let ItemKind::Struct(data, _) = &self_item.kind { + check_struct(cx, typeck_results, block, self_ty, item, data); + } + } + } +} diff --git a/src/tools/clippy/clippy_lints/src/missing_inline.rs b/src/tools/clippy/clippy_lints/src/missing_inline.rs index 5a459548153..a41d5a9ce8d 100644 --- a/src/tools/clippy/clippy_lints/src/missing_inline.rs +++ b/src/tools/clippy/clippy_lints/src/missing_inline.rs @@ -105,7 +105,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline { match tit_.kind { hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {}, hir::TraitItemKind::Fn(..) => { - if cx.tcx.impl_defaultness(tit.id.owner_id).has_value() { + if cx.tcx.defaultness(tit.id.owner_id).has_value() { // trait method with default body needs inline in case // an impl is not provided let desc = "a default trait method"; diff --git a/src/tools/clippy/clippy_lints/src/needless_else.rs b/src/tools/clippy/clippy_lints/src/needless_else.rs new file mode 100644 index 00000000000..4ff1bf7ffc0 --- /dev/null +++ b/src/tools/clippy/clippy_lints/src/needless_else.rs @@ -0,0 +1,61 @@ +use clippy_utils::source::snippet_opt; +use clippy_utils::{diagnostics::span_lint_and_sugg, source::trim_span}; +use rustc_ast::ast::{Expr, ExprKind}; +use rustc_errors::Applicability; +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; + +declare_clippy_lint! { + /// ### What it does + /// Checks for empty `else` branches. + /// + /// ### Why is this bad? + /// An empty else branch does nothing and can be removed. + /// + /// ### Example + /// ```rust + ///# fn check() -> bool { true } + /// if check() { + /// println!("Check successful!"); + /// } else { + /// } + /// ``` + /// Use instead: + /// ```rust + ///# fn check() -> bool { true } + /// if check() { + /// println!("Check successful!"); + /// } + /// ``` + #[clippy::version = "1.71.0"] + pub NEEDLESS_ELSE, + style, + "empty else branch" +} +declare_lint_pass!(NeedlessElse => [NEEDLESS_ELSE]); + +impl EarlyLintPass for NeedlessElse { + fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { + if let ExprKind::If(_, then_block, Some(else_clause)) = &expr.kind + && let ExprKind::Block(block, _) = &else_clause.kind + && !expr.span.from_expansion() + && !else_clause.span.from_expansion() + && block.stmts.is_empty() + && let Some(trimmed) = expr.span.trim_start(then_block.span) + && let span = trim_span(cx.sess().source_map(), trimmed) + && let Some(else_snippet) = snippet_opt(cx, span) + // Ignore else blocks that contain comments or #[cfg]s + && !else_snippet.contains(['/', '#']) + { + span_lint_and_sugg( + cx, + NEEDLESS_ELSE, + span, + "this else branch is empty", + "you can remove it", + String::new(), + Applicability::MachineApplicable, + ); + } + } +} diff --git a/src/tools/clippy/clippy_lints/src/nonstandard_macro_braces.rs b/src/tools/clippy/clippy_lints/src/nonstandard_macro_braces.rs index 6c909e5ed73..2d79a5c9008 100644 --- a/src/tools/clippy/clippy_lints/src/nonstandard_macro_braces.rs +++ b/src/tools/clippy/clippy_lints/src/nonstandard_macro_braces.rs @@ -241,7 +241,7 @@ impl<'de> Deserialize<'de> for MacroMatcher { V: de::MapAccess<'de>, { let mut name = None; - let mut brace: Option<&str> = None; + let mut brace: Option<String> = None; while let Some(key) = map.next_key()? { match key { Field::Name => { diff --git a/src/tools/clippy/clippy_lints/src/redundant_async_block.rs b/src/tools/clippy/clippy_lints/src/redundant_async_block.rs index a0f831764d0..05e52e6b38b 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_async_block.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_async_block.rs @@ -35,7 +35,7 @@ declare_clippy_lint! { /// }; /// let fut = f; /// ``` - #[clippy::version = "1.69.0"] + #[clippy::version = "1.70.0"] pub REDUNDANT_ASYNC_BLOCK, complexity, "`async { future.await }` can be replaced by `future`" diff --git a/src/tools/clippy/clippy_lints/src/regex.rs b/src/tools/clippy/clippy_lints/src/regex.rs index ef19c6f4617..674f8bf4c0f 100644 --- a/src/tools/clippy/clippy_lints/src/regex.rs +++ b/src/tools/clippy/clippy_lints/src/regex.rs @@ -177,7 +177,7 @@ fn check_set<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) { } fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) { - let mut parser = regex_syntax::ParserBuilder::new().unicode(true).utf8(!utf8).build(); + let mut parser = regex_syntax::ParserBuilder::new().unicode(true).utf8(utf8).build(); if let ExprKind::Lit(lit) = expr.kind { if let LitKind::Str(ref r, style) = lit.node { diff --git a/src/tools/clippy/clippy_lints/src/returns.rs b/src/tools/clippy/clippy_lints/src/returns.rs index df126d7617e..631ecf1428d 100644 --- a/src/tools/clippy/clippy_lints/src/returns.rs +++ b/src/tools/clippy/clippy_lints/src/returns.rs @@ -70,7 +70,7 @@ declare_clippy_lint! { "using a return statement like `return expr;` where an expression would suffice" } -#[derive(PartialEq, Eq, Clone)] +#[derive(PartialEq, Eq)] enum RetReplacement<'tcx> { Empty, Block, @@ -80,7 +80,7 @@ enum RetReplacement<'tcx> { } impl<'tcx> RetReplacement<'tcx> { - fn sugg_help(self) -> &'static str { + fn sugg_help(&self) -> &'static str { match self { Self::Empty | Self::Expr(..) => "remove `return`", Self::Block => "replace `return` with an empty block", @@ -88,10 +88,11 @@ impl<'tcx> RetReplacement<'tcx> { Self::IfSequence(..) => "remove `return` and wrap the sequence with parentheses", } } - fn applicability(&self) -> Option<Applicability> { + + fn applicability(&self) -> Applicability { match self { - Self::Expr(_, ap) | Self::IfSequence(_, ap) => Some(*ap), - _ => None, + Self::Expr(_, ap) | Self::IfSequence(_, ap) => *ap, + _ => Applicability::MachineApplicable, } } } @@ -271,7 +272,7 @@ fn check_final_expr<'tcx>( return; } - emit_return_lint(cx, ret_span, semi_spans, replacement); + emit_return_lint(cx, ret_span, semi_spans, &replacement); }, ExprKind::If(_, then, else_clause_opt) => { check_block_return(cx, &then.kind, peeled_drop_expr.span, semi_spans.clone()); @@ -306,20 +307,17 @@ fn expr_contains_conjunctive_ifs<'tcx>(expr: &'tcx Expr<'tcx>) -> bool { contains_if(expr, false) } -fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, semi_spans: Vec<Span>, replacement: RetReplacement<'_>) { +fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, semi_spans: Vec<Span>, replacement: &RetReplacement<'_>) { if ret_span.from_expansion() { return; } - let applicability = replacement.applicability().unwrap_or(Applicability::MachineApplicable); - let return_replacement = replacement.to_string(); - let sugg_help = replacement.sugg_help(); span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| { - diag.span_suggestion_hidden(ret_span, sugg_help, return_replacement, applicability); - // for each parent statement, we need to remove the semicolon - for semi_stmt_span in semi_spans { - diag.tool_only_span_suggestion(semi_stmt_span, "remove this semicolon", "", applicability); - } + let suggestions = std::iter::once((ret_span, replacement.to_string())) + .chain(semi_spans.into_iter().map(|span| (span, String::new()))) + .collect(); + + diag.multipart_suggestion_verbose(replacement.sugg_help(), suggestions, replacement.applicability()); }); } diff --git a/src/tools/clippy/clippy_lints/src/strings.rs b/src/tools/clippy/clippy_lints/src/strings.rs index 483f860a8b5..8658009eba4 100644 --- a/src/tools/clippy/clippy_lints/src/strings.rs +++ b/src/tools/clippy/clippy_lints/src/strings.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sug 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::{get_parent_expr, is_lint_allowed, is_path_diagnostic_item, method_calls}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::def_id::DefId; @@ -255,7 +255,8 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes { if_chain! { // Find std::str::converts::from_utf8 - if let Some(args) = match_function_call(cx, e, &paths::STR_FROM_UTF8); + if let ExprKind::Call(fun, args) = e.kind; + if is_path_diagnostic_item(cx, fun, sym::str_from_utf8); // Find string::as_bytes if let ExprKind::AddrOf(BorrowKind::Ref, _, args) = args[0].kind; diff --git a/src/tools/clippy/clippy_lints/src/unused_async.rs b/src/tools/clippy/clippy_lints/src/unused_async.rs index 55651a28be9..117dda09222 100644 --- a/src/tools/clippy/clippy_lints/src/unused_async.rs +++ b/src/tools/clippy/clippy_lints/src/unused_async.rs @@ -1,5 +1,5 @@ -use clippy_utils::diagnostics::span_lint_and_help; -use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor}; +use clippy_utils::diagnostics::span_lint_and_then; +use rustc_hir::intravisit::{walk_body, walk_expr, walk_fn, FnKind, Visitor}; use rustc_hir::{Body, Expr, ExprKind, FnDecl, YieldSource}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; @@ -42,6 +42,10 @@ declare_lint_pass!(UnusedAsync => [UNUSED_ASYNC]); struct AsyncFnVisitor<'a, 'tcx> { cx: &'a LateContext<'tcx>, found_await: bool, + /// Also keep track of `await`s in nested async blocks so we can mention + /// it in a note + await_in_async_block: Option<Span>, + async_depth: usize, } impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> { @@ -49,7 +53,11 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> { fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) { if let ExprKind::Yield(_, YieldSource::Await { .. }) = ex.kind { - self.found_await = true; + if self.async_depth == 1 { + self.found_await = true; + } else if self.await_in_async_block.is_none() { + self.await_in_async_block = Some(ex.span); + } } walk_expr(self, ex); } @@ -57,6 +65,20 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> { fn nested_visit_map(&mut self) -> Self::Map { self.cx.tcx.hir() } + + fn visit_body(&mut self, b: &'tcx Body<'tcx>) { + let is_async_block = matches!(b.generator_kind, Some(rustc_hir::GeneratorKind::Async(_))); + + if is_async_block { + self.async_depth += 1; + } + + walk_body(self, b); + + if is_async_block { + self.async_depth -= 1; + } + } } impl<'tcx> LateLintPass<'tcx> for UnusedAsync { @@ -70,16 +92,30 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync { def_id: LocalDefId, ) { if !span.from_expansion() && fn_kind.asyncness().is_async() { - let mut visitor = AsyncFnVisitor { cx, found_await: false }; + let mut visitor = AsyncFnVisitor { + cx, + found_await: false, + async_depth: 0, + await_in_async_block: None, + }; walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), def_id); if !visitor.found_await { - span_lint_and_help( + span_lint_and_then( cx, UNUSED_ASYNC, span, "unused `async` for function with no await statements", - None, - "consider removing the `async` from this function", + |diag| { + diag.help("consider removing the `async` from this function"); + + if let Some(span) = visitor.await_in_async_block { + diag.span_note( + span, + "`await` used in an async block, which does not require \ + the enclosing function to be `async`", + ); + } + }, ); } } diff --git a/src/tools/clippy/clippy_lints/src/utils/conf.rs b/src/tools/clippy/clippy_lints/src/utils/conf.rs index f6de66bb514..35f40830681 100644 --- a/src/tools/clippy/clippy_lints/src/utils/conf.rs +++ b/src/tools/clippy/clippy_lints/src/utils/conf.rs @@ -2,12 +2,15 @@ #![allow(clippy::module_name_repetitions)] +use rustc_session::Session; +use rustc_span::{BytePos, Pos, SourceFile, Span, SyntaxContext}; use serde::de::{Deserializer, IgnoredAny, IntoDeserializer, MapAccess, Visitor}; use serde::Deserialize; -use std::error::Error; +use std::fmt::{Debug, Display, Formatter}; +use std::ops::Range; use std::path::{Path, PathBuf}; use std::str::FromStr; -use std::{cmp, env, fmt, fs, io, iter}; +use std::{cmp, env, fmt, fs, io}; #[rustfmt::skip] const DEFAULT_DOC_VALID_IDENTS: &[&str] = &[ @@ -67,33 +70,70 @@ impl DisallowedPath { #[derive(Default)] pub struct TryConf { pub conf: Conf, - pub errors: Vec<Box<dyn Error>>, - pub warnings: Vec<Box<dyn Error>>, + pub errors: Vec<ConfError>, + pub warnings: Vec<ConfError>, } impl TryConf { - fn from_error(error: impl Error + 'static) -> Self { + fn from_toml_error(file: &SourceFile, error: &toml::de::Error) -> Self { + ConfError::from_toml(file, error).into() + } +} + +impl From<ConfError> for TryConf { + fn from(value: ConfError) -> Self { Self { conf: Conf::default(), - errors: vec![Box::new(error)], + errors: vec![value], warnings: vec![], } } } +impl From<io::Error> for TryConf { + fn from(value: io::Error) -> Self { + ConfError::from(value).into() + } +} + #[derive(Debug)] -struct ConfError(String); +pub struct ConfError { + pub message: String, + pub span: Option<Span>, +} + +impl ConfError { + fn from_toml(file: &SourceFile, error: &toml::de::Error) -> Self { + if let Some(span) = error.span() { + Self::spanned(file, error.message(), span) + } else { + Self { + message: error.message().to_string(), + span: None, + } + } + } -impl fmt::Display for ConfError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - <String as fmt::Display>::fmt(&self.0, f) + fn spanned(file: &SourceFile, message: impl Into<String>, span: Range<usize>) -> Self { + Self { + message: message.into(), + span: Some(Span::new( + file.start_pos + BytePos::from_usize(span.start), + file.start_pos + BytePos::from_usize(span.end), + SyntaxContext::root(), + None, + )), + } } } -impl Error for ConfError {} - -fn conf_error(s: impl Into<String>) -> Box<dyn Error> { - Box::new(ConfError(s.into())) +impl From<io::Error> for ConfError { + fn from(value: io::Error) -> Self { + Self { + message: value.to_string(), + span: None, + } + } } macro_rules! define_Conf { @@ -117,20 +157,14 @@ macro_rules! define_Conf { } } - impl<'de> Deserialize<'de> for TryConf { - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> { - deserializer.deserialize_map(ConfVisitor) - } - } - #[derive(Deserialize)] #[serde(field_identifier, rename_all = "kebab-case")] #[allow(non_camel_case_types)] enum Field { $($name,)* third_party, } - struct ConfVisitor; + struct ConfVisitor<'a>(&'a SourceFile); - impl<'de> Visitor<'de> for ConfVisitor { + impl<'de> Visitor<'de> for ConfVisitor<'_> { type Value = TryConf; fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -141,32 +175,38 @@ macro_rules! define_Conf { let mut errors = Vec::new(); let mut warnings = Vec::new(); $(let mut $name = None;)* - // could get `Field` here directly, but get `str` first for diagnostics - while let Some(name) = map.next_key::<&str>()? { - match Field::deserialize(name.into_deserializer())? { - $(Field::$name => { - $(warnings.push(conf_error(format!("deprecated field `{}`. {}", name, $dep)));)? - match map.next_value() { - Err(e) => errors.push(conf_error(e.to_string())), + // could get `Field` here directly, but get `String` first for diagnostics + while let Some(name) = map.next_key::<toml::Spanned<String>>()? { + match Field::deserialize(name.get_ref().as_str().into_deserializer()) { + Err(e) => { + let e: FieldError = e; + errors.push(ConfError::spanned(self.0, e.0, name.span())); + } + $(Ok(Field::$name) => { + $(warnings.push(ConfError::spanned(self.0, format!("deprecated field `{}`. {}", name.get_ref(), $dep), name.span()));)? + let raw_value = map.next_value::<toml::Spanned<toml::Value>>()?; + let value_span = raw_value.span(); + match <$ty>::deserialize(raw_value.into_inner()) { + Err(e) => errors.push(ConfError::spanned(self.0, e.to_string().replace('\n', " ").trim(), value_span)), Ok(value) => match $name { - Some(_) => errors.push(conf_error(format!("duplicate field `{}`", name))), + Some(_) => errors.push(ConfError::spanned(self.0, format!("duplicate field `{}`", name.get_ref()), name.span())), None => { $name = Some(value); // $new_conf is the same as one of the defined `$name`s, so // this variable is defined in line 2 of this function. $(match $new_conf { - Some(_) => errors.push(conf_error(concat!( + Some(_) => errors.push(ConfError::spanned(self.0, concat!( "duplicate field `", stringify!($new_conf), "` (provided as `", stringify!($name), "`)" - ))), + ), name.span())), None => $new_conf = $name.clone(), })? }, } } })* - // white-listed; ignore - Field::third_party => drop(map.next_value::<IgnoredAny>()) + // ignore contents of the third_party key + Ok(Field::third_party) => drop(map.next_value::<IgnoredAny>()) } } let conf = Conf { $($name: $name.unwrap_or_else(defaults::$name),)* }; @@ -486,7 +526,7 @@ pub fn lookup_conf_file() -> io::Result<(Option<PathBuf>, Vec<String>)> { const CONFIG_FILE_NAMES: [&str; 2] = [".clippy.toml", "clippy.toml"]; // Start looking for a config file in CLIPPY_CONF_DIR, or failing that, CARGO_MANIFEST_DIR. - // If neither of those exist, use ".". + // If neither of those exist, use ".". (Update documentation if this priority changes) let mut current = env::var_os("CLIPPY_CONF_DIR") .or_else(|| env::var_os("CARGO_MANIFEST_DIR")) .map_or_else(|| PathBuf::from("."), PathBuf::from) @@ -532,19 +572,19 @@ pub fn lookup_conf_file() -> io::Result<(Option<PathBuf>, Vec<String>)> { /// Read the `toml` configuration file. /// /// In case of error, the function tries to continue as much as possible. -pub fn read(path: &Path) -> TryConf { - let content = match fs::read_to_string(path) { - Err(e) => return TryConf::from_error(e), - Ok(content) => content, +pub fn read(sess: &Session, path: &Path) -> TryConf { + let file = match sess.source_map().load_file(path) { + Err(e) => return e.into(), + Ok(file) => file, }; - match toml::from_str::<TryConf>(&content) { + match toml::de::Deserializer::new(file.src.as_ref().unwrap()).deserialize_map(ConfVisitor(&file)) { Ok(mut conf) => { extend_vec_if_indicator_present(&mut conf.conf.doc_valid_idents, DEFAULT_DOC_VALID_IDENTS); extend_vec_if_indicator_present(&mut conf.conf.disallowed_names, DEFAULT_DISALLOWED_NAMES); conf }, - Err(e) => TryConf::from_error(e), + Err(e) => TryConf::from_toml_error(&file, &e), } } @@ -556,65 +596,42 @@ fn extend_vec_if_indicator_present(vec: &mut Vec<String>, default: &[&str]) { const SEPARATOR_WIDTH: usize = 4; -// Check whether the error is "unknown field" and, if so, list the available fields sorted and at -// least one per line, more if `CLIPPY_TERMINAL_WIDTH` is set and allows it. -pub fn format_error(error: Box<dyn Error>) -> String { - let s = error.to_string(); - - if_chain! { - if error.downcast::<toml::de::Error>().is_ok(); - if let Some((prefix, mut fields, suffix)) = parse_unknown_field_message(&s); - then { - use fmt::Write; - - fields.sort_unstable(); - - let (rows, column_widths) = calculate_dimensions(&fields); - - let mut msg = String::from(prefix); - for row in 0..rows { - writeln!(msg).unwrap(); - for (column, column_width) in column_widths.iter().copied().enumerate() { - let index = column * rows + row; - let field = fields.get(index).copied().unwrap_or_default(); - write!( - msg, - "{:SEPARATOR_WIDTH$}{field:column_width$}", - " " - ) - .unwrap(); - } - } - write!(msg, "\n{suffix}").unwrap(); - msg - } else { - s - } +#[derive(Debug)] +struct FieldError(String); + +impl std::error::Error for FieldError {} + +impl Display for FieldError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.pad(&self.0) } } -// `parse_unknown_field_message` will become unnecessary if -// https://github.com/alexcrichton/toml-rs/pull/364 is merged. -fn parse_unknown_field_message(s: &str) -> Option<(&str, Vec<&str>, &str)> { - // An "unknown field" message has the following form: - // unknown field `UNKNOWN`, expected one of `FIELD0`, `FIELD1`, ..., `FIELDN` at line X column Y - // ^^ ^^^^ ^^ - if_chain! { - if s.starts_with("unknown field"); - let slices = s.split("`, `").collect::<Vec<_>>(); - let n = slices.len(); - if n >= 2; - if let Some((prefix, first_field)) = slices[0].rsplit_once(" `"); - if let Some((last_field, suffix)) = slices[n - 1].split_once("` "); - then { - let fields = iter::once(first_field) - .chain(slices[1..n - 1].iter().copied()) - .chain(iter::once(last_field)) - .collect::<Vec<_>>(); - Some((prefix, fields, suffix)) - } else { - None +impl serde::de::Error for FieldError { + fn custom<T: Display>(msg: T) -> Self { + Self(msg.to_string()) + } + + fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self { + // List the available fields sorted and at least one per line, more if `CLIPPY_TERMINAL_WIDTH` is + // set and allows it. + use fmt::Write; + + let mut expected = expected.to_vec(); + expected.sort_unstable(); + + let (rows, column_widths) = calculate_dimensions(&expected); + + let mut msg = format!("unknown field `{field}`, expected one of"); + for row in 0..rows { + writeln!(msg).unwrap(); + for (column, column_width) in column_widths.iter().copied().enumerate() { + let index = column * rows + row; + let field = expected.get(index).copied().unwrap_or_default(); + write!(msg, "{:SEPARATOR_WIDTH$}{field:column_width$}", " ").unwrap(); + } } + Self(msg) } } diff --git a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs index a9089fba3c5..b6e4cd22789 100644 --- a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs +++ b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs @@ -65,8 +65,9 @@ declare_clippy_lint! { /// This can lead to confusing error messages at best and to unexpected behavior at worst. /// /// ### Exceptions - /// Wildcard imports are allowed from modules named `prelude`. Many crates (including the standard library) - /// provide modules named "prelude" specifically designed for wildcard import. + /// Wildcard imports are allowed from modules that their name contains `prelude`. Many crates + /// (including the standard library) provide modules named "prelude" specifically designed + /// for wildcard import. /// /// `use super::*` is allowed in test modules. This is defined as any module with "test" in the name. /// @@ -212,7 +213,9 @@ impl WildcardImports { // Allow "...prelude::..::*" imports. // Many crates have a prelude, and it is imported as a glob by design. fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool { - segments.iter().any(|ps| ps.ident.name == sym::prelude) + segments + .iter() + .any(|ps| ps.ident.name.as_str().contains(sym::prelude.as_str())) } // Allow "super::*" imports in tests. diff --git a/src/tools/clippy/clippy_utils/Cargo.toml b/src/tools/clippy/clippy_utils/Cargo.toml index 66a5079fa85..cfe686eb9b0 100644 --- a/src/tools/clippy/clippy_utils/Cargo.toml +++ b/src/tools/clippy/clippy_utils/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy_utils" -version = "0.1.71" +version = "0.1.72" edition = "2021" publish = false diff --git a/src/tools/clippy/clippy_utils/src/lib.rs b/src/tools/clippy/clippy_utils/src/lib.rs index 575c29a6b6f..8c883445a79 100644 --- a/src/tools/clippy/clippy_utils/src/lib.rs +++ b/src/tools/clippy/clippy_utils/src/lib.rs @@ -287,7 +287,7 @@ pub fn is_wild(pat: &Pat<'_>) -> bool { /// Checks if the given `QPath` belongs to a type alias. pub fn is_ty_alias(qpath: &QPath<'_>) -> bool { match *qpath { - QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias, ..)), + QPath::Resolved(_, path) => matches!(path.res, Res::Def(DefKind::TyAlias | DefKind::AssocTy, ..)), QPath::TypeRelative(ty, _) if let TyKind::Path(qpath) = ty.kind => { is_ty_alias(&qpath) }, _ => false, } diff --git a/src/tools/clippy/clippy_utils/src/msrvs.rs b/src/tools/clippy/clippy_utils/src/msrvs.rs index e05de2dc99c..6f102308f0b 100644 --- a/src/tools/clippy/clippy_utils/src/msrvs.rs +++ b/src/tools/clippy/clippy_utils/src/msrvs.rs @@ -20,7 +20,7 @@ macro_rules! msrv_aliases { // names may refer to stabilized feature flags or library items msrv_aliases! { 1,68,0 { PATH_MAIN_SEPARATOR_STR } - 1,65,0 { LET_ELSE } + 1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS } 1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE } 1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY } 1,55,0 { SEEK_REWIND } diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 0f0792fdaa9..38ee84fb76c 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -125,8 +125,6 @@ pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; pub const STR_BYTES: [&str; 4] = ["core", "str", "<impl str>", "bytes"]; pub const STR_CHARS: [&str; 4] = ["core", "str", "<impl str>", "chars"]; pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"]; -pub const STR_FROM_UTF8: [&str; 4] = ["core", "str", "converts", "from_utf8"]; -pub const STR_FROM_UTF8_UNCHECKED: [&str; 4] = ["core", "str", "converts", "from_utf8_unchecked"]; pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"]; pub const STR_STARTS_WITH: [&str; 4] = ["core", "str", "<impl str>", "starts_with"]; #[cfg(feature = "internal")] @@ -163,3 +161,5 @@ pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"]; pub const VEC_POP: [&str; 4] = ["alloc", "vec", "Vec", "pop"]; pub const OPTION_UNWRAP: [&str; 4] = ["core", "option", "Option", "unwrap"]; pub const OPTION_EXPECT: [&str; 4] = ["core", "option", "Option", "expect"]; +pub const FORMATTER: [&str; 3] = ["core", "fmt", "Formatter"]; +pub const DEBUG_STRUCT: [&str; 4] = ["core", "fmt", "builders", "DebugStruct"]; diff --git a/src/tools/clippy/clippy_utils/src/source.rs b/src/tools/clippy/clippy_utils/src/source.rs index 0f60290644a..582337b47e8 100644 --- a/src/tools/clippy/clippy_utils/src/source.rs +++ b/src/tools/clippy/clippy_utils/src/source.rs @@ -4,7 +4,7 @@ use rustc_data_structures::sync::Lrc; use rustc_errors::Applicability; -use rustc_hir::{Expr, ExprKind}; +use rustc_hir::{BlockCheckMode, Expr, ExprKind, UnsafeSource}; use rustc_lint::{LateContext, LintContext}; use rustc_session::Session; use rustc_span::source_map::{original_sp, SourceMap}; @@ -71,11 +71,16 @@ pub fn expr_block<T: LintContext>( app: &mut Applicability, ) -> String { let (code, from_macro) = snippet_block_with_context(cx, expr.span, outer, default, indent_relative_to, app); - if from_macro { - format!("{{ {code} }}") - } else if let ExprKind::Block(_, _) = expr.kind { + if !from_macro && + let ExprKind::Block(block, _) = expr.kind && + block.rules != BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) + { format!("{code}") } else { + // FIXME: add extra indent for the unsafe blocks: + // original code: unsafe { ... } + // result code: { unsafe { ... } } + // desired code: {\n unsafe { ... }\n} format!("{{ {code} }}") } } diff --git a/src/tools/clippy/declare_clippy_lint/Cargo.toml b/src/tools/clippy/declare_clippy_lint/Cargo.toml index 139102798c4..4dc906d00db 100644 --- a/src/tools/clippy/declare_clippy_lint/Cargo.toml +++ b/src/tools/clippy/declare_clippy_lint/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "declare_clippy_lint" -version = "0.1.71" +version = "0.1.72" edition = "2021" publish = false diff --git a/src/tools/clippy/lintcheck/Cargo.toml b/src/tools/clippy/lintcheck/Cargo.toml index 27d32f39003..a828d123704 100644 --- a/src/tools/clippy/lintcheck/Cargo.toml +++ b/src/tools/clippy/lintcheck/Cargo.toml @@ -22,7 +22,7 @@ rayon = "1.5.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.85" tar = "0.4" -toml = "0.5" +toml = "0.7.3" ureq = "2.2" walkdir = "2.3" diff --git a/src/tools/clippy/rust-toolchain b/src/tools/clippy/rust-toolchain index bc7fb711ed8..0d2e1eee643 100644 --- a/src/tools/clippy/rust-toolchain +++ b/src/tools/clippy/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2023-05-20" +channel = "nightly-2023-06-02" components = ["cargo", "llvm-tools", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"] diff --git a/src/tools/clippy/src/driver.rs b/src/tools/clippy/src/driver.rs index 59bf447a7cd..3c5b6e12b96 100644 --- a/src/tools/clippy/src/driver.rs +++ b/src/tools/clippy/src/driver.rs @@ -70,7 +70,7 @@ fn track_clippy_args(parse_sess: &mut ParseSess, args_env_var: &Option<String>) /// Track files that may be accessed at runtime in `file_depinfo` so that cargo will re-run clippy /// when any of them are modified -fn track_files(parse_sess: &mut ParseSess, conf_path_string: Option<String>) { +fn track_files(parse_sess: &mut ParseSess) { let file_depinfo = parse_sess.file_depinfo.get_mut(); // Used by `clippy::cargo` lints and to determine the MSRV. `cargo clippy` executes `clippy-driver` @@ -79,10 +79,7 @@ fn track_files(parse_sess: &mut ParseSess, conf_path_string: Option<String>) { file_depinfo.insert(Symbol::intern("Cargo.toml")); } - // `clippy.toml` - if let Some(path) = conf_path_string { - file_depinfo.insert(Symbol::intern(&path)); - } + // `clippy.toml` will be automatically tracked as it's loaded with `sess.source_map().load_file()` // During development track the `clippy-driver` executable so that cargo will re-run clippy whenever // it is rebuilt @@ -126,17 +123,11 @@ impl rustc_driver::Callbacks for ClippyCallbacks { #[allow(rustc::bad_opt_access)] fn config(&mut self, config: &mut interface::Config) { let conf_path = clippy_lints::lookup_conf_file(); - let conf_path_string = if let Ok((Some(path), _)) = &conf_path { - path.to_str().map(String::from) - } else { - None - }; - let previous = config.register_lints.take(); let clippy_args_var = self.clippy_args_var.take(); config.parse_sess_created = Some(Box::new(move |parse_sess| { track_clippy_args(parse_sess, &clippy_args_var); - track_files(parse_sess, conf_path_string); + track_files(parse_sess); })); config.register_lints = Some(Box::new(move |sess, lint_store| { // technically we're ~guaranteed that this is none but might as well call anything that diff --git a/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.lock b/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.lock index 7e96aa36feb..e4de82ad3b8 100644 --- a/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.lock +++ b/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "ansi_term" version = "0.11.0" @@ -10,71 +12,14 @@ dependencies = [ ] [[package]] -name = "bitflags" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" - -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - -[[package]] -name = "ctrlc" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "653abc99aa905f693d89df4797fadc08085baee379db92be9f2496cefe8a6f2c" -dependencies = [ - "kernel32-sys", - "nix", - "winapi 0.2.8", -] - -[[package]] -name = "kernel32-sys" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] - -[[package]] -name = "libc" -version = "0.2.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" - -[[package]] name = "multiple_crate_versions" version = "0.1.0" dependencies = [ "ansi_term", - "ctrlc", -] - -[[package]] -name = "nix" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2c5afeb0198ec7be8569d666644b574345aad2e95a53baf3a532da3e0f3fb32" -dependencies = [ - "bitflags", - "cfg-if", - "libc", - "void", + "winapi 0.2.8", ] [[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - -[[package]] name = "winapi" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -91,12 +36,6 @@ dependencies = [ ] [[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" - -[[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.toml b/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.toml index 4f97b011334..79317659ac0 100644 --- a/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.toml +++ b/src/tools/clippy/tests/ui-cargo/multiple_crate_versions/fail/Cargo.toml @@ -6,5 +6,5 @@ publish = false [workspace] [dependencies] -ctrlc = "=3.1.0" +winapi = "0.2" ansi_term = "=0.11.0" diff --git a/src/tools/clippy/tests/ui-internal/custom_ice_message.rs b/src/tools/clippy/tests/ui-internal/custom_ice_message.rs index 99ce7028390..9b0db660c99 100644 --- a/src/tools/clippy/tests/ui-internal/custom_ice_message.rs +++ b/src/tools/clippy/tests/ui-internal/custom_ice_message.rs @@ -3,7 +3,7 @@ //@normalize-stderr-test: "produce_ice.rs:\d*:\d*" -> "produce_ice.rs" //@normalize-stderr-test: "', .*clippy_lints" -> "', clippy_lints" //@normalize-stderr-test: "'rustc'" -> "'<unnamed>'" -//@normalize-stderr-test: "running on .*" -> "running on <target>" +//@normalize-stderr-test: "rustc 1\.\d+.* running on .*" -> "rustc <version> running on <target>" //@normalize-stderr-test: "(?ms)query stack during panic:\n.*end of query stack\n" -> "" #![deny(clippy::internal)] diff --git a/src/tools/clippy/tests/ui-internal/custom_ice_message.stderr b/src/tools/clippy/tests/ui-internal/custom_ice_message.stderr index 0fc385cd693..b9ea5a64de7 100644 --- a/src/tools/clippy/tests/ui-internal/custom_ice_message.stderr +++ b/src/tools/clippy/tests/ui-internal/custom_ice_message.stderr @@ -5,10 +5,9 @@ error: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new -note: rustc 1.71.0-nightly (521f4dae1 2023-05-19) running on <target> +note: rustc <version> running on <target> note: compiler flags: -C prefer-dynamic -Z ui-testing -note: Clippy version: foo - -thread panicked while panicking. aborting. +query stack during panic: +thread panicked while processing panic. aborting. diff --git a/src/tools/clippy/tests/ui-toml/bad_toml/conf_bad_toml.stderr b/src/tools/clippy/tests/ui-toml/bad_toml/conf_bad_toml.stderr index 28c1a568a63..5b7e8c0db74 100644 --- a/src/tools/clippy/tests/ui-toml/bad_toml/conf_bad_toml.stderr +++ b/src/tools/clippy/tests/ui-toml/bad_toml/conf_bad_toml.stderr @@ -1,4 +1,8 @@ -error: error reading Clippy's configuration file `$DIR/clippy.toml`: expected an equals, found an identifier at line 1 column 4 +error: error reading Clippy's configuration file: expected `.`, `=` + --> $DIR/clippy.toml:1:4 + | +LL | fn this_is_obviously(not: a, toml: file) { + | ^ error: aborting due to previous error diff --git a/src/tools/clippy/tests/ui-toml/bad_toml_type/conf_bad_type.stderr b/src/tools/clippy/tests/ui-toml/bad_toml_type/conf_bad_type.stderr index e3ec6019204..386e1135df9 100644 --- a/src/tools/clippy/tests/ui-toml/bad_toml_type/conf_bad_type.stderr +++ b/src/tools/clippy/tests/ui-toml/bad_toml_type/conf_bad_type.stderr @@ -1,4 +1,8 @@ -error: error reading Clippy's configuration file `$DIR/clippy.toml`: invalid type: integer `42`, expected a sequence for key `disallowed-names` +error: error reading Clippy's configuration file: invalid type: integer `42`, expected a sequence + --> $DIR/clippy.toml:1:20 + | +LL | disallowed-names = 42 + | ^^ error: aborting due to previous error diff --git a/src/tools/clippy/tests/ui-toml/conf_deprecated_key/conf_deprecated_key.stderr b/src/tools/clippy/tests/ui-toml/conf_deprecated_key/conf_deprecated_key.stderr index 630bad07c5b..123ad94dd09 100644 --- a/src/tools/clippy/tests/ui-toml/conf_deprecated_key/conf_deprecated_key.stderr +++ b/src/tools/clippy/tests/ui-toml/conf_deprecated_key/conf_deprecated_key.stderr @@ -1,6 +1,14 @@ -warning: error reading Clippy's configuration file `$DIR/clippy.toml`: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead +warning: error reading Clippy's configuration file: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead + --> $DIR/clippy.toml:2:1 + | +LL | cyclomatic-complexity-threshold = 2 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -warning: error reading Clippy's configuration file `$DIR/clippy.toml`: deprecated field `blacklisted-names`. Please use `disallowed-names` instead +warning: error reading Clippy's configuration file: deprecated field `blacklisted-names`. Please use `disallowed-names` instead + --> $DIR/clippy.toml:3:1 + | +LL | blacklisted-names = [ "..", "wibble" ] + | ^^^^^^^^^^^^^^^^^ error: the function has a cognitive complexity of (3/2) --> $DIR/conf_deprecated_key.rs:6:4 diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys/clippy.toml b/src/tools/clippy/tests/ui-toml/duplicated_keys/clippy.toml index 63a893cc6c7..55789afc1b7 100644 --- a/src/tools/clippy/tests/ui-toml/duplicated_keys/clippy.toml +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys/clippy.toml @@ -1,5 +1,2 @@ cognitive-complexity-threshold = 2 -# This is the deprecated name for the same key -cyclomatic-complexity-threshold = 3 -# Check we get duplication warning regardless of order cognitive-complexity-threshold = 4 diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys/duplicated_keys.stderr b/src/tools/clippy/tests/ui-toml/duplicated_keys/duplicated_keys.stderr index d99490a242d..54997735274 100644 --- a/src/tools/clippy/tests/ui-toml/duplicated_keys/duplicated_keys.stderr +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys/duplicated_keys.stderr @@ -1,8 +1,8 @@ -error: error reading Clippy's configuration file `$DIR/clippy.toml`: duplicate field `cognitive_complexity_threshold` (provided as `cyclomatic_complexity_threshold`) +error: error reading Clippy's configuration file: duplicate key `cognitive-complexity-threshold` in document root + --> $DIR/clippy.toml:2:1 + | +LL | cognitive-complexity-threshold = 4 + | ^ -error: error reading Clippy's configuration file `$DIR/clippy.toml`: duplicate field `cognitive-complexity-threshold` - -warning: error reading Clippy's configuration file `$DIR/clippy.toml`: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead - -error: aborting due to 2 previous errors; 1 warning emitted +error: aborting due to previous error diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/clippy.toml b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/clippy.toml new file mode 100644 index 00000000000..7932c43ebd2 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/clippy.toml @@ -0,0 +1,3 @@ +cognitive-complexity-threshold = 2 +# This is the deprecated name for the same key +cyclomatic-complexity-threshold = 3 diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/duplicated_keys.rs b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/duplicated_keys.rs new file mode 100644 index 00000000000..f328e4d9d04 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/duplicated_keys.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/duplicated_keys.stderr b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/duplicated_keys.stderr new file mode 100644 index 00000000000..2ae7848f183 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated/duplicated_keys.stderr @@ -0,0 +1,14 @@ +error: error reading Clippy's configuration file: duplicate field `cognitive_complexity_threshold` (provided as `cyclomatic_complexity_threshold`) + --> $DIR/clippy.toml:3:1 + | +LL | cyclomatic-complexity-threshold = 3 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: error reading Clippy's configuration file: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead + --> $DIR/clippy.toml:3:1 + | +LL | cyclomatic-complexity-threshold = 3 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error; 1 warning emitted + diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/clippy.toml b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/clippy.toml new file mode 100644 index 00000000000..53c634b727e --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/clippy.toml @@ -0,0 +1,4 @@ +# This is the deprecated name for cognitive-complexity-threshold +cyclomatic-complexity-threshold = 3 +# Check we get duplication warning regardless of order +cognitive-complexity-threshold = 4 diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/duplicated_keys.rs b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/duplicated_keys.rs new file mode 100644 index 00000000000..f328e4d9d04 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/duplicated_keys.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/duplicated_keys.stderr b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/duplicated_keys.stderr new file mode 100644 index 00000000000..53ad4271246 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/duplicated_keys_deprecated_2/duplicated_keys.stderr @@ -0,0 +1,14 @@ +error: error reading Clippy's configuration file: duplicate field `cognitive-complexity-threshold` + --> $DIR/clippy.toml:4:1 + | +LL | cognitive-complexity-threshold = 4 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: error reading Clippy's configuration file: deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead + --> $DIR/clippy.toml:2:1 + | +LL | cyclomatic-complexity-threshold = 3 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error; 1 warning emitted + diff --git a/src/tools/clippy/tests/ui-toml/ifs_same_cond/ifs_same_cond.rs b/src/tools/clippy/tests/ui-toml/ifs_same_cond/ifs_same_cond.rs index d623ac7e020..4882416c414 100644 --- a/src/tools/clippy/tests/ui-toml/ifs_same_cond/ifs_same_cond.rs +++ b/src/tools/clippy/tests/ui-toml/ifs_same_cond/ifs_same_cond.rs @@ -1,5 +1,5 @@ #![warn(clippy::ifs_same_cond)] -#![allow(clippy::if_same_then_else, clippy::comparison_chain)] +#![allow(clippy::if_same_then_else, clippy::comparison_chain, clippy::needless_else)] fn main() {} diff --git a/src/tools/clippy/tests/ui-toml/toml_unknown_key/clippy.toml b/src/tools/clippy/tests/ui-toml/toml_unknown_key/clippy.toml index 554b87cc50b..b77b4580051 100644 --- a/src/tools/clippy/tests/ui-toml/toml_unknown_key/clippy.toml +++ b/src/tools/clippy/tests/ui-toml/toml_unknown_key/clippy.toml @@ -1,6 +1,8 @@ # that one is an error foobar = 42 +# so is this one +barfoo = 53 -# that one is white-listed +# that one is ignored [third-party] clippy-feature = "nightly" diff --git a/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 44710b09648..b6038f031f3 100644 --- a/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -1,4 +1,4 @@ -error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of +error: error reading Clippy's configuration file: unknown field `foobar`, expected one of allow-dbg-in-tests allow-expect-in-tests allow-mixed-uninlined-format-args @@ -54,7 +54,71 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie vec-box-size-threshold verbose-bit-mask-threshold warn-on-all-wildcard-imports - at line 5 column 1 + --> $DIR/clippy.toml:2:1 + | +LL | foobar = 42 + | ^^^^^^ -error: aborting due to previous error +error: error reading Clippy's configuration file: unknown field `barfoo`, 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 + 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 + blacklisted-names + cargo-ignore-publish + cognitive-complexity-threshold + cyclomatic-complexity-threshold + disallowed-macros + disallowed-methods + disallowed-names + disallowed-types + doc-valid-idents + enable-raw-pointer-heuristic-for-send + enforced-import-renames + enum-variant-name-threshold + enum-variant-size-threshold + future-size-threshold + ignore-interior-mutability + large-error-threshold + literal-representation-threshold + matches-for-let-else + max-fn-params-bools + max-include-file-size + max-struct-bools + max-suggested-slice-pattern-length + max-trait-bounds + missing-docs-in-crate-items + msrv + pass-by-value-size-limit + semicolon-inside-block-ignore-singleline + semicolon-outside-block-ignore-multiline + single-char-binding-names-threshold + standard-macro-braces + suppress-restriction-lint-in-const + third-party + too-large-for-stack + too-many-arguments-threshold + too-many-lines-threshold + trivial-copy-size-limit + type-complexity-threshold + unnecessary-box-size + unreadable-literal-lint-fractions + upper-case-acronyms-aggressive + vec-box-size-threshold + verbose-bit-mask-threshold + warn-on-all-wildcard-imports + --> $DIR/clippy.toml:4:1 + | +LL | barfoo = 53 + | ^^^^^^ + +error: aborting due to 2 previous errors diff --git a/src/tools/clippy/tests/ui/auxiliary/wildcard_imports_helper.rs b/src/tools/clippy/tests/ui/auxiliary/wildcard_imports_helper.rs index d75cdd625f9..44f49c080cd 100644 --- a/src/tools/clippy/tests/ui/auxiliary/wildcard_imports_helper.rs +++ b/src/tools/clippy/tests/ui/auxiliary/wildcard_imports_helper.rs @@ -25,3 +25,9 @@ pub mod prelude { pub struct PreludeModAnywhere; } } + +pub mod extern_prelude { + pub mod v1 { + pub struct ExternPreludeModAnywhere; + } +} diff --git a/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.rs b/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.rs index 2d6055eb6c4..5780ea08937 100644 --- a/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.rs +++ b/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.rs @@ -1,6 +1,10 @@ #![deny(clippy::branches_sharing_code, clippy::if_same_then_else)] #![allow(dead_code)] -#![allow(clippy::mixed_read_write_in_expression, clippy::uninlined_format_args)] +#![allow( + clippy::mixed_read_write_in_expression, + clippy::uninlined_format_args, + clippy::needless_else +)] // This tests valid if blocks that shouldn't trigger the lint diff --git a/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.stderr b/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.stderr index ce7fff0122f..a7e72b780af 100644 --- a/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.stderr +++ b/src/tools/clippy/tests/ui/branches_sharing_code/valid_if_blocks.stderr @@ -1,5 +1,5 @@ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:105:14 + --> $DIR/valid_if_blocks.rs:109:14 | LL | if false { | ______________^ @@ -7,7 +7,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/valid_if_blocks.rs:106:12 + --> $DIR/valid_if_blocks.rs:110:12 | LL | } else { | ____________^ @@ -20,7 +20,7 @@ LL | #![deny(clippy::branches_sharing_code, clippy::if_same_then_else)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:116:15 + --> $DIR/valid_if_blocks.rs:120:15 | LL | if x == 0 { | _______________^ @@ -31,7 +31,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/valid_if_blocks.rs:120:12 + --> $DIR/valid_if_blocks.rs:124:12 | LL | } else { | ____________^ @@ -42,19 +42,19 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:127:23 + --> $DIR/valid_if_blocks.rs:131:23 | LL | let _ = if x == 6 { 7 } else { 7 }; | ^^^^^ | note: same as this - --> $DIR/valid_if_blocks.rs:127:34 + --> $DIR/valid_if_blocks.rs:131:34 | LL | let _ = if x == 6 { 7 } else { 7 }; | ^^^^^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:133:23 + --> $DIR/valid_if_blocks.rs:137:23 | LL | } else if x == 68 { | _______________________^ @@ -66,7 +66,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/valid_if_blocks.rs:138:12 + --> $DIR/valid_if_blocks.rs:142:12 | LL | } else { | ____________^ @@ -78,7 +78,7 @@ LL | | }; | |_____^ error: this `if` has identical blocks - --> $DIR/valid_if_blocks.rs:147:23 + --> $DIR/valid_if_blocks.rs:151:23 | LL | } else if x == 68 { | _______________________^ @@ -88,7 +88,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/valid_if_blocks.rs:150:12 + --> $DIR/valid_if_blocks.rs:154:12 | LL | } else { | ____________^ diff --git a/src/tools/clippy/tests/ui/crashes/ice-7410.rs b/src/tools/clippy/tests/ui/crashes/ice-7410.rs index ffe20ab1c31..a5373cdcae1 100644 --- a/src/tools/clippy/tests/ui/crashes/ice-7410.rs +++ b/src/tools/clippy/tests/ui/crashes/ice-7410.rs @@ -6,6 +6,7 @@ #![no_std] #![allow(clippy::if_same_then_else)] #![allow(clippy::redundant_pattern_matching)] +#![allow(clippy::needless_else)] use core::panic::PanicInfo; diff --git a/src/tools/clippy/tests/ui/default_constructed_unit_structs.fixed b/src/tools/clippy/tests/ui/default_constructed_unit_structs.fixed index e1012f38bba..ac5fe38ff44 100644 --- a/src/tools/clippy/tests/ui/default_constructed_unit_structs.fixed +++ b/src/tools/clippy/tests/ui/default_constructed_unit_structs.fixed @@ -101,6 +101,28 @@ struct EmptyStruct {} #[non_exhaustive] struct NonExhaustiveStruct; +mod issue_10755 { + struct Sqlite {} + + trait HasArguments<'q> { + type Arguments; + } + + impl<'q> HasArguments<'q> for Sqlite { + type Arguments = std::marker::PhantomData<&'q ()>; + } + + type SqliteArguments<'q> = <Sqlite as HasArguments<'q>>::Arguments; + + fn foo() { + // should not lint + // type alias cannot be used as a constructor + let _ = <Sqlite as HasArguments>::Arguments::default(); + + let _ = SqliteArguments::default(); + } +} + fn main() { // should lint let _ = PhantomData::<usize>; diff --git a/src/tools/clippy/tests/ui/default_constructed_unit_structs.rs b/src/tools/clippy/tests/ui/default_constructed_unit_structs.rs index c7b4313dbf0..de7f14ffbd9 100644 --- a/src/tools/clippy/tests/ui/default_constructed_unit_structs.rs +++ b/src/tools/clippy/tests/ui/default_constructed_unit_structs.rs @@ -101,6 +101,28 @@ struct EmptyStruct {} #[non_exhaustive] struct NonExhaustiveStruct; +mod issue_10755 { + struct Sqlite {} + + trait HasArguments<'q> { + type Arguments; + } + + impl<'q> HasArguments<'q> for Sqlite { + type Arguments = std::marker::PhantomData<&'q ()>; + } + + type SqliteArguments<'q> = <Sqlite as HasArguments<'q>>::Arguments; + + fn foo() { + // should not lint + // type alias cannot be used as a constructor + let _ = <Sqlite as HasArguments>::Arguments::default(); + + let _ = SqliteArguments::default(); + } +} + fn main() { // should lint let _ = PhantomData::<usize>::default(); diff --git a/src/tools/clippy/tests/ui/default_constructed_unit_structs.stderr b/src/tools/clippy/tests/ui/default_constructed_unit_structs.stderr index 61a32fb10e5..13abb9149da 100644 --- a/src/tools/clippy/tests/ui/default_constructed_unit_structs.stderr +++ b/src/tools/clippy/tests/ui/default_constructed_unit_structs.stderr @@ -13,25 +13,25 @@ LL | inner: PhantomData::default(), | ^^^^^^^^^^^ help: remove this call to `default` error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:106:33 + --> $DIR/default_constructed_unit_structs.rs:128:33 | LL | let _ = PhantomData::<usize>::default(); | ^^^^^^^^^^^ help: remove this call to `default` error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:107:42 + --> $DIR/default_constructed_unit_structs.rs:129:42 | LL | let _: PhantomData<i32> = PhantomData::default(); | ^^^^^^^^^^^ help: remove this call to `default` error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:108:55 + --> $DIR/default_constructed_unit_structs.rs:130:55 | LL | let _: PhantomData<i32> = std::marker::PhantomData::default(); | ^^^^^^^^^^^ help: remove this call to `default` error: use of `default` to create a unit struct - --> $DIR/default_constructed_unit_structs.rs:109:23 + --> $DIR/default_constructed_unit_structs.rs:131:23 | LL | let _ = UnitStruct::default(); | ^^^^^^^^^^^ help: remove this call to `default` diff --git a/src/tools/clippy/tests/ui/ifs_same_cond.rs b/src/tools/clippy/tests/ui/ifs_same_cond.rs index 9ce9a87626a..f62da157d1b 100644 --- a/src/tools/clippy/tests/ui/ifs_same_cond.rs +++ b/src/tools/clippy/tests/ui/ifs_same_cond.rs @@ -1,5 +1,5 @@ #![warn(clippy::ifs_same_cond)] -#![allow(clippy::if_same_then_else, clippy::comparison_chain)] // all empty blocks +#![allow(clippy::if_same_then_else, clippy::comparison_chain, clippy::needless_else)] // all empty blocks fn ifs_same_cond() { let a = 0; diff --git a/src/tools/clippy/tests/ui/large_stack_arrays.rs b/src/tools/clippy/tests/ui/large_stack_arrays.rs index 99787ffd3d3..3e9d5e6a4ca 100644 --- a/src/tools/clippy/tests/ui/large_stack_arrays.rs +++ b/src/tools/clippy/tests/ui/large_stack_arrays.rs @@ -18,6 +18,19 @@ pub static DOESNOTLINT2: [u8; 512_001] = { [x; 512_001] }; +fn issue_10741() { + #[derive(Copy, Clone)] + struct Large([u32; 100_000]); + + fn build() -> Large { + Large([0; 100_000]) + } + + let _x = [build(); 3]; + + let _y = [build(), build(), build()]; +} + fn main() { let bad = ( [0u32; 20_000_000], diff --git a/src/tools/clippy/tests/ui/large_stack_arrays.stderr b/src/tools/clippy/tests/ui/large_stack_arrays.stderr index 24e90094982..118d39566ab 100644 --- a/src/tools/clippy/tests/ui/large_stack_arrays.stderr +++ b/src/tools/clippy/tests/ui/large_stack_arrays.stderr @@ -1,14 +1,30 @@ error: allocating a local array larger than 512000 bytes - --> $DIR/large_stack_arrays.rs:23:9 + --> $DIR/large_stack_arrays.rs:29:14 + | +LL | let _x = [build(); 3]; + | ^^^^^^^^^^^^ + | + = help: consider allocating on the heap with `vec![build(); 3].into_boxed_slice()` + = note: `-D clippy::large-stack-arrays` implied by `-D warnings` + +error: allocating a local array larger than 512000 bytes + --> $DIR/large_stack_arrays.rs:31:14 + | +LL | let _y = [build(), build(), build()]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider allocating on the heap with `vec![build(), build(), build()].into_boxed_slice()` + +error: allocating a local array larger than 512000 bytes + --> $DIR/large_stack_arrays.rs:36:9 | LL | [0u32; 20_000_000], | ^^^^^^^^^^^^^^^^^^ | = help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()` - = note: `-D clippy::large-stack-arrays` implied by `-D warnings` error: allocating a local array larger than 512000 bytes - --> $DIR/large_stack_arrays.rs:24:9 + --> $DIR/large_stack_arrays.rs:37:9 | LL | [S { data: [0; 32] }; 5000], | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -16,7 +32,7 @@ LL | [S { data: [0; 32] }; 5000], = help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()` error: allocating a local array larger than 512000 bytes - --> $DIR/large_stack_arrays.rs:25:9 + --> $DIR/large_stack_arrays.rs:38:9 | LL | [Some(""); 20_000_000], | ^^^^^^^^^^^^^^^^^^^^^^ @@ -24,7 +40,7 @@ LL | [Some(""); 20_000_000], = help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()` error: allocating a local array larger than 512000 bytes - --> $DIR/large_stack_arrays.rs:26:9 + --> $DIR/large_stack_arrays.rs:39:9 | LL | [E::T(0); 5000], | ^^^^^^^^^^^^^^^ @@ -32,12 +48,12 @@ LL | [E::T(0); 5000], = help: consider allocating on the heap with `vec![E::T(0); 5000].into_boxed_slice()` error: allocating a local array larger than 512000 bytes - --> $DIR/large_stack_arrays.rs:27:9 + --> $DIR/large_stack_arrays.rs:40:9 | LL | [0u8; usize::MAX], | ^^^^^^^^^^^^^^^^^ | = help: consider allocating on the heap with `vec![0u8; usize::MAX].into_boxed_slice()` -error: aborting due to 5 previous errors +error: aborting due to 7 previous errors diff --git a/src/tools/clippy/tests/ui/manual_let_else.rs b/src/tools/clippy/tests/ui/manual_let_else.rs index 3996d775f55..351ea0e4f50 100644 --- a/src/tools/clippy/tests/ui/manual_let_else.rs +++ b/src/tools/clippy/tests/ui/manual_let_else.rs @@ -146,10 +146,20 @@ fn fire() { Variant::A(0, 0) } - // Should not be renamed let v = if let Variant::A(a, 0) = e() { a } else { return }; - // Should be renamed - let v = if let Variant::B(b) = e() { b } else { return }; + + // `mut v` is inserted into the pattern + let mut v = if let Variant::B(b) = e() { b } else { return }; + + // Nesting works + let nested = Ok(Some(e())); + let v = if let Ok(Some(Variant::B(b))) | Err(Some(Variant::A(b, _))) = nested { + b + } else { + return; + }; + // dot dot works + let v = if let Variant::A(.., a) = e() { a } else { return }; } fn not_fire() { diff --git a/src/tools/clippy/tests/ui/manual_let_else.stderr b/src/tools/clippy/tests/ui/manual_let_else.stderr index f6f56f7b00e..0e876797134 100644 --- a/src/tools/clippy/tests/ui/manual_let_else.stderr +++ b/src/tools/clippy/tests/ui/manual_let_else.stderr @@ -260,19 +260,42 @@ 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: this could be rewritten as `let...else` - --> $DIR/manual_let_else.rs:150:5 + --> $DIR/manual_let_else.rs:149:5 | LL | let v = if let Variant::A(a, 0) = e() { a } else { return }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Variant::A(a, 0) = e() else { return };` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Variant::A(v, 0) = e() else { return };` error: this could be rewritten as `let...else` --> $DIR/manual_let_else.rs:152:5 | -LL | let v = if let Variant::B(b) = e() { b } else { return }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Variant::B(v) = e() else { return };` +LL | let mut v = if let Variant::B(b) = e() { b } else { return }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Variant::B(mut v) = e() else { return };` error: this could be rewritten as `let...else` - --> $DIR/manual_let_else.rs:262:5 + --> $DIR/manual_let_else.rs:156:5 + | +LL | / let v = if let Ok(Some(Variant::B(b))) | Err(Some(Variant::A(b, _))) = nested { +LL | | b +LL | | } else { +LL | | return; +LL | | }; + | |______^ + | +help: consider writing + | +LL ~ let (Ok(Some(Variant::B(v))) | Err(Some(Variant::A(v, _)))) = nested else { +LL + return; +LL + }; + | + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:162:5 + | +LL | let v = if let Variant::A(.., a) = e() { a } else { return }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider writing: `let Variant::A(.., v) = e() else { return };` + +error: this could be rewritten as `let...else` + --> $DIR/manual_let_else.rs:272:5 | LL | / let _ = match ff { LL | | Some(value) => value, @@ -280,5 +303,5 @@ LL | | _ => macro_call!(), LL | | }; | |______^ help: consider writing: `let Some(_) = ff else { macro_call!() };` -error: aborting due to 20 previous errors +error: aborting due to 22 previous errors diff --git a/src/tools/clippy/tests/ui/manual_let_else_match.rs b/src/tools/clippy/tests/ui/manual_let_else_match.rs index 73b74679125..dfca3b023cd 100644 --- a/src/tools/clippy/tests/ui/manual_let_else_match.rs +++ b/src/tools/clippy/tests/ui/manual_let_else_match.rs @@ -68,7 +68,7 @@ fn fire() { let f = Variant::Bar(1); let _value = match f { - Variant::Bar(_) | Variant::Baz(_) => (), + Variant::Bar(v) | Variant::Baz(v) => v, _ => return, }; diff --git a/src/tools/clippy/tests/ui/manual_let_else_match.stderr b/src/tools/clippy/tests/ui/manual_let_else_match.stderr index bacc14dc967..13ed35bc1d5 100644 --- a/src/tools/clippy/tests/ui/manual_let_else_match.stderr +++ b/src/tools/clippy/tests/ui/manual_let_else_match.stderr @@ -58,10 +58,10 @@ 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 | | Variant::Bar(v) | Variant::Baz(v) => v, LL | | _ => return, LL | | }; - | |______^ help: consider writing: `let (Variant::Bar(_) | Variant::Baz(_)) = f else { return };` + | |______^ help: consider writing: `let (Variant::Bar(_value) | Variant::Baz(_value)) = f else { return };` error: this could be rewritten as `let...else` --> $DIR/manual_let_else_match.rs:76:5 diff --git a/src/tools/clippy/tests/ui/match_wild_err_arm.rs b/src/tools/clippy/tests/ui/match_wild_err_arm.rs index 823be65efe0..5a552e4ae51 100644 --- a/src/tools/clippy/tests/ui/match_wild_err_arm.rs +++ b/src/tools/clippy/tests/ui/match_wild_err_arm.rs @@ -1,7 +1,20 @@ #![feature(exclusive_range_pattern)] -#![allow(clippy::match_same_arms)] +#![allow(clippy::match_same_arms, dead_code)] #![warn(clippy::match_wild_err_arm)] +fn issue_10635() { + enum Error { + A, + B, + } + + // Don't trigger in const contexts. Const unwrap is not yet stable + const X: () = match Ok::<_, Error>(()) { + Ok(x) => x, + Err(_) => panic!(), + }; +} + fn match_wild_err_arm() { let x: Result<i32, &str> = Ok(3); diff --git a/src/tools/clippy/tests/ui/match_wild_err_arm.stderr b/src/tools/clippy/tests/ui/match_wild_err_arm.stderr index b016d682698..a9f54feacdb 100644 --- a/src/tools/clippy/tests/ui/match_wild_err_arm.stderr +++ b/src/tools/clippy/tests/ui/match_wild_err_arm.stderr @@ -1,5 +1,5 @@ error: `Err(_)` matches all errors - --> $DIR/match_wild_err_arm.rs:11:9 + --> $DIR/match_wild_err_arm.rs:24:9 | LL | Err(_) => panic!("err"), | ^^^^^^ @@ -8,7 +8,7 @@ LL | Err(_) => panic!("err"), = note: `-D clippy::match-wild-err-arm` implied by `-D warnings` error: `Err(_)` matches all errors - --> $DIR/match_wild_err_arm.rs:17:9 + --> $DIR/match_wild_err_arm.rs:30:9 | LL | Err(_) => panic!(), | ^^^^^^ @@ -16,7 +16,7 @@ LL | Err(_) => panic!(), = note: match each error separately or use the error output, or use `.expect(msg)` if the error case is unreachable error: `Err(_)` matches all errors - --> $DIR/match_wild_err_arm.rs:23:9 + --> $DIR/match_wild_err_arm.rs:36:9 | LL | Err(_) => { | ^^^^^^ @@ -24,7 +24,7 @@ LL | Err(_) => { = note: match each error separately or use the error output, or use `.expect(msg)` if the error case is unreachable error: `Err(_e)` matches all errors - --> $DIR/match_wild_err_arm.rs:31:9 + --> $DIR/match_wild_err_arm.rs:44:9 | LL | Err(_e) => panic!(), | ^^^^^^^ diff --git a/src/tools/clippy/tests/ui/missing_fields_in_debug.rs b/src/tools/clippy/tests/ui/missing_fields_in_debug.rs new file mode 100644 index 00000000000..c156d394ece --- /dev/null +++ b/src/tools/clippy/tests/ui/missing_fields_in_debug.rs @@ -0,0 +1,191 @@ +#![allow(unused)] +#![warn(clippy::missing_fields_in_debug)] + +use std::fmt; +use std::marker::PhantomData; +use std::ops::Deref; + +struct NamedStruct1Ignored { + data: u8, + hidden: u32, +} + +impl fmt::Debug for NamedStruct1Ignored { + // unused field: hidden + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .debug_struct("NamedStruct1Ignored") + .field("data", &self.data) + .finish() + } +} + +struct NamedStructMultipleIgnored { + data: u8, + hidden: u32, + hidden2: String, + hidden3: Vec<Vec<i32>>, + hidden4: ((((u8), u16), u32), u64), +} + +impl fmt::Debug for NamedStructMultipleIgnored { + // unused fields: hidden, hidden2, hidden4 + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .debug_struct("NamedStructMultipleIgnored") + .field("data", &self.data) + .field("hidden3", &self.hidden3) + .finish() + } +} + +struct Unit; + +// ok +impl fmt::Debug for Unit { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.debug_struct("Unit").finish() + } +} + +struct UnnamedStruct1Ignored(String); + +impl fmt::Debug for UnnamedStruct1Ignored { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.debug_tuple("UnnamedStruct1Ignored").finish() + } +} + +struct UnnamedStructMultipleIgnored(String, Vec<u8>, i32); + +// tuple structs are not linted +impl fmt::Debug for UnnamedStructMultipleIgnored { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .debug_tuple("UnnamedStructMultipleIgnored") + .field(&self.1) + .finish() + } +} + +struct NamedStructNonExhaustive { + a: u8, + b: String, +} + +// ok +impl fmt::Debug for NamedStructNonExhaustive { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .debug_struct("NamedStructNonExhaustive") + .field("a", &self.a) + .finish_non_exhaustive() // should not warn here + } +} + +struct MultiExprDebugImpl { + a: u8, + b: String, +} + +// ok +impl fmt::Debug for MultiExprDebugImpl { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = formatter.debug_struct("MultiExprDebugImpl"); + f.field("a", &self.a); + f.finish() + } +} + +#[derive(Debug)] +struct DerivedStruct { + a: u8, + b: i32, +} + +// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1166846953 + +struct Inner { + a: usize, + b: usize, +} + +struct HasInner { + inner: Inner, +} + +impl HasInner { + fn get(&self) -> &Inner { + &self.inner + } +} + +impl fmt::Debug for HasInner { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let inner = self.get(); + + f.debug_struct("HasInner") + .field("a", &inner.a) + .field("b", &inner.b) + .finish() + } +} + +// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1170306053 +struct Foo { + a: u8, + b: u8, +} + +impl fmt::Debug for Foo { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Foo").field("a", &self.a).field("b", &()).finish() + } +} + +// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1175473620 +mod comment1175473620 { + use super::*; + + struct Inner { + a: usize, + b: usize, + } + struct Wrapper(Inner); + + impl Deref for Wrapper { + type Target = Inner; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl fmt::Debug for Wrapper { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Wrapper") + .field("a", &self.a) + .field("b", &self.b) + .finish() + } + } +} + +// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1175488757 +// PhantomData is an exception and does not need to be included +struct WithPD { + a: u8, + b: u8, + c: PhantomData<String>, +} + +impl fmt::Debug for WithPD { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("WithPD") + .field("a", &self.a) + .field("b", &self.b) + .finish() + } +} + +fn main() {} diff --git a/src/tools/clippy/tests/ui/missing_fields_in_debug.stderr b/src/tools/clippy/tests/ui/missing_fields_in_debug.stderr new file mode 100644 index 00000000000..ef9d02abab7 --- /dev/null +++ b/src/tools/clippy/tests/ui/missing_fields_in_debug.stderr @@ -0,0 +1,73 @@ +error: manual `Debug` impl does not include all fields + --> $DIR/missing_fields_in_debug.rs:13:1 + | +LL | / impl fmt::Debug for NamedStruct1Ignored { +LL | | // unused field: hidden +LL | | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { +LL | | formatter +... | +LL | | } +LL | | } + | |_^ + | +note: this field is unused + --> $DIR/missing_fields_in_debug.rs:10:5 + | +LL | hidden: u32, + | ^^^^^^^^^^^ + = help: consider including all fields in this `Debug` impl + = help: consider calling `.finish_non_exhaustive()` if you intend to ignore fields + = note: `-D clippy::missing-fields-in-debug` implied by `-D warnings` + +error: manual `Debug` impl does not include all fields + --> $DIR/missing_fields_in_debug.rs:31:1 + | +LL | / impl fmt::Debug for NamedStructMultipleIgnored { +LL | | // unused fields: hidden, hidden2, hidden4 +LL | | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { +LL | | formatter +... | +LL | | } +LL | | } + | |_^ + | +note: this field is unused + --> $DIR/missing_fields_in_debug.rs:25:5 + | +LL | hidden: u32, + | ^^^^^^^^^^^ +note: this field is unused + --> $DIR/missing_fields_in_debug.rs:26:5 + | +LL | hidden2: String, + | ^^^^^^^^^^^^^^^ +note: this field is unused + --> $DIR/missing_fields_in_debug.rs:28:5 + | +LL | hidden4: ((((u8), u16), u32), u64), + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = help: consider including all fields in this `Debug` impl + = help: consider calling `.finish_non_exhaustive()` if you intend to ignore fields + +error: manual `Debug` impl does not include all fields + --> $DIR/missing_fields_in_debug.rs:92:1 + | +LL | / impl fmt::Debug for MultiExprDebugImpl { +LL | | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { +LL | | let mut f = formatter.debug_struct("MultiExprDebugImpl"); +LL | | f.field("a", &self.a); +LL | | f.finish() +LL | | } +LL | | } + | |_^ + | +note: this field is unused + --> $DIR/missing_fields_in_debug.rs:88:5 + | +LL | b: String, + | ^^^^^^^^^ + = help: consider including all fields in this `Debug` impl + = help: consider calling `.finish_non_exhaustive()` if you intend to ignore fields + +error: aborting due to 3 previous errors + diff --git a/src/tools/clippy/tests/ui/modulo_one.stderr b/src/tools/clippy/tests/ui/modulo_one.stderr index 04ecdef5e99..83a76f81d4e 100644 --- a/src/tools/clippy/tests/ui/modulo_one.stderr +++ b/src/tools/clippy/tests/ui/modulo_one.stderr @@ -2,7 +2,7 @@ error: this operation will panic at runtime --> $DIR/modulo_one.rs:11:5 | LL | i32::MIN % (-1); // also caught by rustc - | ^^^^^^^^^^^^^^^ attempt to compute the remainder of `i32::MIN % -1_i32`, which would overflow + | ^^^^^^^^^^^^^^^ attempt to compute `i32::MIN % -1_i32`, which would overflow | = note: `#[deny(unconditional_panic)]` on by default @@ -10,13 +10,13 @@ error: this operation will panic at runtime --> $DIR/modulo_one.rs:21:5 | LL | INT_MIN % NEG_ONE; // also caught by rustc - | ^^^^^^^^^^^^^^^^^ attempt to compute the remainder of `i64::MIN % -1_i64`, which would overflow + | ^^^^^^^^^^^^^^^^^ attempt to compute `i64::MIN % -1_i64`, which would overflow error: this operation will panic at runtime --> $DIR/modulo_one.rs:22:5 | LL | INT_MIN % STATIC_NEG_ONE; // ONLY caught by rustc - | ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute the remainder of `i64::MIN % -1_i64`, which would overflow + | ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `i64::MIN % -1_i64`, which would overflow error: any number modulo 1 will be 0 --> $DIR/modulo_one.rs:8:5 diff --git a/src/tools/clippy/tests/ui/needless_else.fixed b/src/tools/clippy/tests/ui/needless_else.fixed new file mode 100644 index 00000000000..06a16162790 --- /dev/null +++ b/src/tools/clippy/tests/ui/needless_else.fixed @@ -0,0 +1,57 @@ +//@run-rustfix +#![allow(unused)] +#![warn(clippy::needless_else)] +#![allow(clippy::suspicious_else_formatting)] + +macro_rules! mac { + ($test:expr) => { + if $test { + println!("Test successful!"); + } else { + } + }; +} + +macro_rules! empty_expansion { + () => {}; +} + +fn main() { + let b = std::hint::black_box(true); + + if b { + println!("Foobar"); + } + + if b { + println!("Foobar"); + } else { + // Do not lint because this comment might be important + } + + if b { + println!("Foobar"); + } else + /* Do not lint because this comment might be important */ + { + } + + // Do not lint because of the expression + let _ = if b { 1 } else { 2 }; + + // Do not lint because inside a macro + mac!(b); + + if b { + println!("Foobar"); + } else { + #[cfg(foo)] + "Do not lint cfg'd out code" + } + + if b { + println!("Foobar"); + } else { + empty_expansion!(); + } +} diff --git a/src/tools/clippy/tests/ui/needless_else.rs b/src/tools/clippy/tests/ui/needless_else.rs new file mode 100644 index 00000000000..728032c47a6 --- /dev/null +++ b/src/tools/clippy/tests/ui/needless_else.rs @@ -0,0 +1,58 @@ +//@run-rustfix +#![allow(unused)] +#![warn(clippy::needless_else)] +#![allow(clippy::suspicious_else_formatting)] + +macro_rules! mac { + ($test:expr) => { + if $test { + println!("Test successful!"); + } else { + } + }; +} + +macro_rules! empty_expansion { + () => {}; +} + +fn main() { + let b = std::hint::black_box(true); + + if b { + println!("Foobar"); + } else { + } + + if b { + println!("Foobar"); + } else { + // Do not lint because this comment might be important + } + + if b { + println!("Foobar"); + } else + /* Do not lint because this comment might be important */ + { + } + + // Do not lint because of the expression + let _ = if b { 1 } else { 2 }; + + // Do not lint because inside a macro + mac!(b); + + if b { + println!("Foobar"); + } else { + #[cfg(foo)] + "Do not lint cfg'd out code" + } + + if b { + println!("Foobar"); + } else { + empty_expansion!(); + } +} diff --git a/src/tools/clippy/tests/ui/needless_else.stderr b/src/tools/clippy/tests/ui/needless_else.stderr new file mode 100644 index 00000000000..ea693085164 --- /dev/null +++ b/src/tools/clippy/tests/ui/needless_else.stderr @@ -0,0 +1,12 @@ +error: this else branch is empty + --> $DIR/needless_else.rs:24:7 + | +LL | } else { + | _______^ +LL | | } + | |_____^ help: you can remove it + | + = note: `-D clippy::needless-else` implied by `-D warnings` + +error: aborting due to previous error + diff --git a/src/tools/clippy/tests/ui/needless_return.fixed b/src/tools/clippy/tests/ui/needless_return.fixed index d49ae5d8636..4dabf313963 100644 --- a/src/tools/clippy/tests/ui/needless_return.fixed +++ b/src/tools/clippy/tests/ui/needless_return.fixed @@ -7,7 +7,8 @@ clippy::if_same_then_else, clippy::single_match, clippy::needless_bool, - clippy::equatable_if_let + clippy::equatable_if_let, + clippy::needless_else )] #![warn(clippy::needless_return)] diff --git a/src/tools/clippy/tests/ui/needless_return.rs b/src/tools/clippy/tests/ui/needless_return.rs index 36763826174..542f562b314 100644 --- a/src/tools/clippy/tests/ui/needless_return.rs +++ b/src/tools/clippy/tests/ui/needless_return.rs @@ -7,7 +7,8 @@ clippy::if_same_then_else, clippy::single_match, clippy::needless_bool, - clippy::equatable_if_let + clippy::equatable_if_let, + clippy::needless_else )] #![warn(clippy::needless_return)] diff --git a/src/tools/clippy/tests/ui/needless_return.stderr b/src/tools/clippy/tests/ui/needless_return.stderr index 05f6038cd25..1d9d23d3008 100644 --- a/src/tools/clippy/tests/ui/needless_return.stderr +++ b/src/tools/clippy/tests/ui/needless_return.stderr @@ -1,390 +1,582 @@ error: unneeded `return` statement - --> $DIR/needless_return.rs:27:5 + --> $DIR/needless_return.rs:28:5 | LL | return true; | ^^^^^^^^^^^ | = note: `-D clippy::needless-return` implied by `-D warnings` - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:31:5 + --> $DIR/needless_return.rs:32:5 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:36:5 + --> $DIR/needless_return.rs:37:5 | LL | return true;;; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true;;; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:41:5 + --> $DIR/needless_return.rs:42:5 | LL | return true;; ; ; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true;; ; ; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:46:9 + --> $DIR/needless_return.rs:47:9 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:48:9 + --> $DIR/needless_return.rs:49:9 | LL | return false; | ^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return false; +LL + false + | error: unneeded `return` statement - --> $DIR/needless_return.rs:54:17 + --> $DIR/needless_return.rs:55:17 | LL | true => return false, | ^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL | true => false, + | ~~~~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:56:13 + --> $DIR/needless_return.rs:57:13 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:63:9 + --> $DIR/needless_return.rs:64:9 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:65:16 + --> $DIR/needless_return.rs:66:16 | LL | let _ = || return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL | let _ = || true; + | ~~~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:69:5 + --> $DIR/needless_return.rs:70:5 | LL | return the_answer!(); | ^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return the_answer!(); +LL + the_answer!() + | error: unneeded `return` statement - --> $DIR/needless_return.rs:72:21 + --> $DIR/needless_return.rs:73:21 | LL | fn test_void_fun() { | _____________________^ LL | | return; | |__________^ | - = help: remove `return` +help: remove `return` + | +LL - fn test_void_fun() { +LL - return; +LL + fn test_void_fun() { + | error: unneeded `return` statement - --> $DIR/needless_return.rs:77:11 + --> $DIR/needless_return.rs:78:11 | LL | if b { | ___________^ LL | | return; | |______________^ | - = help: remove `return` +help: remove `return` + | +LL - if b { +LL - return; +LL + if b { + | error: unneeded `return` statement - --> $DIR/needless_return.rs:79:13 + --> $DIR/needless_return.rs:80:13 | LL | } else { | _____________^ LL | | return; | |______________^ | - = help: remove `return` +help: remove `return` + | +LL - } else { +LL - return; +LL + } else { + | error: unneeded `return` statement - --> $DIR/needless_return.rs:87:14 + --> $DIR/needless_return.rs:88:14 | LL | _ => return, | ^^^^^^ | - = help: replace `return` with a unit value +help: replace `return` with a unit value + | +LL | _ => (), + | ~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:95:24 + --> $DIR/needless_return.rs:96:24 | LL | let _ = 42; | ________________________^ LL | | return; | |__________________^ | - = help: remove `return` +help: remove `return` + | +LL - let _ = 42; +LL - return; +LL + let _ = 42; + | error: unneeded `return` statement - --> $DIR/needless_return.rs:98:14 + --> $DIR/needless_return.rs:99:14 | LL | _ => return, | ^^^^^^ | - = help: replace `return` with a unit value +help: replace `return` with a unit value + | +LL | _ => (), + | ~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:111:9 + --> $DIR/needless_return.rs:112:9 | LL | return String::from("test"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return String::from("test"); +LL + String::from("test") + | error: unneeded `return` statement - --> $DIR/needless_return.rs:113:9 + --> $DIR/needless_return.rs:114:9 | LL | return String::new(); | ^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return String::new(); +LL + String::new() + | error: unneeded `return` statement - --> $DIR/needless_return.rs:135:32 + --> $DIR/needless_return.rs:136:32 | LL | bar.unwrap_or_else(|_| return) | ^^^^^^ | - = help: replace `return` with an empty block +help: replace `return` with an empty block + | +LL | bar.unwrap_or_else(|_| {}) + | ~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:139:21 + --> $DIR/needless_return.rs:140:21 | LL | let _ = || { | _____________________^ LL | | return; | |__________________^ | - = help: remove `return` +help: remove `return` + | +LL - let _ = || { +LL - return; +LL + let _ = || { + | error: unneeded `return` statement - --> $DIR/needless_return.rs:142:20 + --> $DIR/needless_return.rs:143:20 | LL | let _ = || return; | ^^^^^^ | - = help: replace `return` with an empty block +help: replace `return` with an empty block + | +LL | let _ = || {}; + | ~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:148:32 + --> $DIR/needless_return.rs:149:32 | LL | res.unwrap_or_else(|_| return Foo) | ^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL | res.unwrap_or_else(|_| Foo) + | ~~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:157:5 + --> $DIR/needless_return.rs:158:5 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:161:5 + --> $DIR/needless_return.rs:162:5 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:166:9 + --> $DIR/needless_return.rs:167:9 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:168:9 + --> $DIR/needless_return.rs:169:9 | LL | return false; | ^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return false; +LL + false + | error: unneeded `return` statement - --> $DIR/needless_return.rs:174:17 + --> $DIR/needless_return.rs:175:17 | LL | true => return false, | ^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL | true => false, + | ~~~~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:176:13 + --> $DIR/needless_return.rs:177:13 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:183:9 + --> $DIR/needless_return.rs:184:9 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return true; +LL + true + | error: unneeded `return` statement - --> $DIR/needless_return.rs:185:16 + --> $DIR/needless_return.rs:186:16 | LL | let _ = || return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL | let _ = || true; + | ~~~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:189:5 + --> $DIR/needless_return.rs:190:5 | LL | return the_answer!(); | ^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return the_answer!(); +LL + the_answer!() + | error: unneeded `return` statement - --> $DIR/needless_return.rs:192:33 + --> $DIR/needless_return.rs:193:33 | LL | async fn async_test_void_fun() { | _________________________________^ LL | | return; | |__________^ | - = help: remove `return` +help: remove `return` + | +LL - async fn async_test_void_fun() { +LL - return; +LL + async fn async_test_void_fun() { + | error: unneeded `return` statement - --> $DIR/needless_return.rs:197:11 + --> $DIR/needless_return.rs:198:11 | LL | if b { | ___________^ LL | | return; | |______________^ | - = help: remove `return` +help: remove `return` + | +LL - if b { +LL - return; +LL + if b { + | error: unneeded `return` statement - --> $DIR/needless_return.rs:199:13 + --> $DIR/needless_return.rs:200:13 | LL | } else { | _____________^ LL | | return; | |______________^ | - = help: remove `return` +help: remove `return` + | +LL - } else { +LL - return; +LL + } else { + | error: unneeded `return` statement - --> $DIR/needless_return.rs:207:14 + --> $DIR/needless_return.rs:208:14 | LL | _ => return, | ^^^^^^ | - = help: replace `return` with a unit value +help: replace `return` with a unit value + | +LL | _ => (), + | ~~ error: unneeded `return` statement - --> $DIR/needless_return.rs:220:9 + --> $DIR/needless_return.rs:221:9 | LL | return String::from("test"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return String::from("test"); +LL + String::from("test") + | error: unneeded `return` statement - --> $DIR/needless_return.rs:222:9 + --> $DIR/needless_return.rs:223:9 | LL | return String::new(); | ^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return String::new(); +LL + String::new() + | error: unneeded `return` statement - --> $DIR/needless_return.rs:238:5 + --> $DIR/needless_return.rs:239:5 | LL | return format!("Hello {}", "world!"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return format!("Hello {}", "world!"); +LL + format!("Hello {}", "world!") + | error: unneeded `return` statement - --> $DIR/needless_return.rs:250:9 + --> $DIR/needless_return.rs:251:9 | LL | return true; | ^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL ~ true +LL | } else { +LL | return false; +LL ~ } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:252:9 + --> $DIR/needless_return.rs:253:9 | LL | return false; | ^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL ~ false +LL ~ } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:259:13 + --> $DIR/needless_return.rs:260:13 | LL | return 10; | ^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL ~ 10 +LL | }, + ... +LL | }, +LL ~ } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:262:13 + --> $DIR/needless_return.rs:263:13 | LL | return 100; | ^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL ~ 100 +LL | }, +LL ~ } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:270:9 + --> $DIR/needless_return.rs:271:9 | LL | return 0; | ^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL ~ 0 +LL ~ } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:277:13 + --> $DIR/needless_return.rs:278:13 | LL | return *(x as *const isize); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL ~ *(x as *const isize) +LL | } else { +LL | return !*(x as *const isize); +LL ~ } +LL ~ } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:279:13 + --> $DIR/needless_return.rs:280:13 | LL | return !*(x as *const isize); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL ~ !*(x as *const isize) +LL ~ } +LL ~ } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:286:20 + --> $DIR/needless_return.rs:287:20 | LL | let _ = 42; | ____________________^ @@ -392,47 +584,73 @@ LL | | LL | | return; | |______________^ | - = help: remove `return` +help: remove `return` + | +LL - let _ = 42; +LL - +LL - return; +LL + let _ = 42; + | error: unneeded `return` statement - --> $DIR/needless_return.rs:293:20 + --> $DIR/needless_return.rs:294:20 | LL | let _ = 42; return; | ^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - let _ = 42; return; +LL + let _ = 42; + | error: unneeded `return` statement - --> $DIR/needless_return.rs:305:9 + --> $DIR/needless_return.rs:306:9 | LL | return Ok(format!("ok!")); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return Ok(format!("ok!")); +LL + Ok(format!("ok!")) + | error: unneeded `return` statement - --> $DIR/needless_return.rs:307:9 + --> $DIR/needless_return.rs:308:9 | LL | return Err(format!("err!")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return Err(format!("err!")); +LL + Err(format!("err!")) + | error: unneeded `return` statement - --> $DIR/needless_return.rs:313:9 + --> $DIR/needless_return.rs:314:9 | LL | return if true { 1 } else { 2 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` +help: remove `return` + | +LL - return if true { 1 } else { 2 }; +LL + if true { 1 } else { 2 } + | error: unneeded `return` statement - --> $DIR/needless_return.rs:317:9 + --> $DIR/needless_return.rs:318:9 | LL | return if b1 { 0 } else { 1 } | if b2 { 2 } else { 3 } | if b3 { 4 } else { 5 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: remove `return` and wrap the sequence with parentheses +help: remove `return` and wrap the sequence with parentheses + | +LL - return if b1 { 0 } else { 1 } | if b2 { 2 } else { 3 } | if b3 { 4 } else { 5 }; +LL + (if b1 { 0 } else { 1 } | if b2 { 2 } else { 3 } | if b3 { 4 } else { 5 }) + | error: aborting due to 52 previous errors diff --git a/src/tools/clippy/tests/ui/nonminimal_bool.rs b/src/tools/clippy/tests/ui/nonminimal_bool.rs index 80cc7c60f56..fec6b7713ee 100644 --- a/src/tools/clippy/tests/ui/nonminimal_bool.rs +++ b/src/tools/clippy/tests/ui/nonminimal_bool.rs @@ -110,3 +110,17 @@ fn issue_10435() { println!("{}", line!()); } } + +fn issue10836() { + struct Foo(bool); + impl std::ops::Not for Foo { + type Output = bool; + + fn not(self) -> Self::Output { + !self.0 + } + } + + // Should not lint + let _: bool = !!Foo(true); +} diff --git a/src/tools/clippy/tests/ui/ptr_cast_constness.fixed b/src/tools/clippy/tests/ui/ptr_cast_constness.fixed new file mode 100644 index 00000000000..24de573d083 --- /dev/null +++ b/src/tools/clippy/tests/ui/ptr_cast_constness.fixed @@ -0,0 +1,55 @@ +//@run-rustfix +//@aux-build:proc_macros.rs + +#![warn(clippy::ptr_cast_constness)] + +extern crate proc_macros; +use proc_macros::{external, inline_macros}; + +#[inline_macros] +fn main() { + let ptr: *const u32 = &42_u32; + let mut_ptr: *mut u32 = &mut 42_u32; + + let _ = ptr as *const i32; + let _ = mut_ptr as *mut i32; + + // Make sure the lint can handle the difference in their operator precedences. + unsafe { + let ptr_ptr: *const *const u32 = &ptr; + let _ = (*ptr_ptr).cast_mut(); + } + + let _ = ptr.cast_mut(); + let _ = mut_ptr.cast_const(); + + // Lint this, since pointer::cast_mut and pointer::cast_const have ?Sized + let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4]; + let _ = ptr_of_array as *const [u32]; + let _ = ptr_of_array as *const dyn std::fmt::Debug; + + // Make sure the lint is triggered inside a macro + let _ = inline!($ptr as *const i32); + + // Do not lint inside macros from external crates + let _ = external!($ptr as *const i32); +} + +#[clippy::msrv = "1.64"] +fn _msrv_1_64() { + let ptr: *const u32 = &42_u32; + let mut_ptr: *mut u32 = &mut 42_u32; + + // `pointer::cast_const` and `pointer::cast_mut` were stabilized in 1.65. Do not lint this + let _ = ptr as *mut i32; + let _ = mut_ptr as *const i32; +} + +#[clippy::msrv = "1.65"] +fn _msrv_1_65() { + let ptr: *const u32 = &42_u32; + let mut_ptr: *mut u32 = &mut 42_u32; + + let _ = ptr.cast_mut(); + let _ = mut_ptr.cast_const(); +} diff --git a/src/tools/clippy/tests/ui/ptr_cast_constness.rs b/src/tools/clippy/tests/ui/ptr_cast_constness.rs new file mode 100644 index 00000000000..63d973a9fca --- /dev/null +++ b/src/tools/clippy/tests/ui/ptr_cast_constness.rs @@ -0,0 +1,55 @@ +//@run-rustfix +//@aux-build:proc_macros.rs + +#![warn(clippy::ptr_cast_constness)] + +extern crate proc_macros; +use proc_macros::{external, inline_macros}; + +#[inline_macros] +fn main() { + let ptr: *const u32 = &42_u32; + let mut_ptr: *mut u32 = &mut 42_u32; + + let _ = ptr as *const i32; + let _ = mut_ptr as *mut i32; + + // Make sure the lint can handle the difference in their operator precedences. + unsafe { + let ptr_ptr: *const *const u32 = &ptr; + let _ = *ptr_ptr as *mut i32; + } + + let _ = ptr as *mut i32; + let _ = mut_ptr as *const i32; + + // Lint this, since pointer::cast_mut and pointer::cast_const have ?Sized + let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4]; + let _ = ptr_of_array as *const [u32]; + let _ = ptr_of_array as *const dyn std::fmt::Debug; + + // Make sure the lint is triggered inside a macro + let _ = inline!($ptr as *const i32); + + // Do not lint inside macros from external crates + let _ = external!($ptr as *const i32); +} + +#[clippy::msrv = "1.64"] +fn _msrv_1_64() { + let ptr: *const u32 = &42_u32; + let mut_ptr: *mut u32 = &mut 42_u32; + + // `pointer::cast_const` and `pointer::cast_mut` were stabilized in 1.65. Do not lint this + let _ = ptr as *mut i32; + let _ = mut_ptr as *const i32; +} + +#[clippy::msrv = "1.65"] +fn _msrv_1_65() { + let ptr: *const u32 = &42_u32; + let mut_ptr: *mut u32 = &mut 42_u32; + + let _ = ptr as *mut i32; + let _ = mut_ptr as *const i32; +} diff --git a/src/tools/clippy/tests/ui/ptr_cast_constness.stderr b/src/tools/clippy/tests/ui/ptr_cast_constness.stderr new file mode 100644 index 00000000000..43816c87c19 --- /dev/null +++ b/src/tools/clippy/tests/ui/ptr_cast_constness.stderr @@ -0,0 +1,34 @@ +error: `as` casting between raw pointers while changing its constness + --> $DIR/ptr_cast_constness.rs:20:17 + | +LL | let _ = *ptr_ptr as *mut i32; + | ^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `(*ptr_ptr).cast_mut()` + | + = note: `-D clippy::ptr-cast-constness` implied by `-D warnings` + +error: `as` casting between raw pointers while changing its constness + --> $DIR/ptr_cast_constness.rs:23:13 + | +LL | let _ = ptr as *mut i32; + | ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()` + +error: `as` casting between raw pointers while changing its constness + --> $DIR/ptr_cast_constness.rs:24:13 + | +LL | let _ = mut_ptr as *const i32; + | ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()` + +error: `as` casting between raw pointers while changing its constness + --> $DIR/ptr_cast_constness.rs:53:13 + | +LL | let _ = ptr as *mut i32; + | ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()` + +error: `as` casting between raw pointers while changing its constness + --> $DIR/ptr_cast_constness.rs:54:13 + | +LL | let _ = mut_ptr as *const i32; + | ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()` + +error: aborting due to 5 previous errors + diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed index bebdf89716f..481c9b263fb 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed @@ -2,7 +2,7 @@ // Issue #5746 #![warn(clippy::redundant_pattern_matching)] -#![allow(clippy::if_same_then_else, clippy::equatable_if_let)] +#![allow(clippy::if_same_then_else, clippy::equatable_if_let, clippy::needless_else)] use std::task::Poll::{Pending, Ready}; fn main() { diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs index 8fb6ed5f7ec..86e46d41e65 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs @@ -2,7 +2,7 @@ // Issue #5746 #![warn(clippy::redundant_pattern_matching)] -#![allow(clippy::if_same_then_else, clippy::equatable_if_let)] +#![allow(clippy::if_same_then_else, clippy::equatable_if_let, clippy::needless_else)] use std::task::Poll::{Pending, Ready}; fn main() { diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_option.fixed b/src/tools/clippy/tests/ui/redundant_pattern_matching_option.fixed index accdf1da9dd..dae931541d4 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_option.fixed +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_option.fixed @@ -47,6 +47,7 @@ fn main() { issue6067(); issue10726(); + issue10803(); let _ = if gen_opt().is_some() { 1 @@ -107,3 +108,14 @@ fn issue10726() { _ => false, }; } + +fn issue10803() { + let x = Some(42); + + let _ = x.is_some(); + + let _ = x.is_none(); + + // Don't lint + let _ = matches!(x, Some(16)); +} diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_option.rs b/src/tools/clippy/tests/ui/redundant_pattern_matching_option.rs index ec684bdf71c..3f2fa3f53ce 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_option.rs +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_option.rs @@ -56,6 +56,7 @@ fn main() { issue6067(); issue10726(); + issue10803(); let _ = if let Some(_) = gen_opt() { 1 @@ -134,3 +135,14 @@ fn issue10726() { _ => false, }; } + +fn issue10803() { + let x = Some(42); + + let _ = matches!(x, Some(_)); + + let _ = matches!(x, None); + + // Don't lint + let _ = matches!(x, Some(16)); +} diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_option.stderr b/src/tools/clippy/tests/ui/redundant_pattern_matching_option.stderr index a69eb390520..93760ce97a8 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_option.stderr +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_option.stderr @@ -77,49 +77,49 @@ LL | let _ = if let Some(_) = opt { true } else { false }; | -------^^^^^^^------ help: try this: `if opt.is_some()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_option.rs:60:20 + --> $DIR/redundant_pattern_matching_option.rs:61:20 | LL | let _ = if let Some(_) = gen_opt() { | -------^^^^^^^------------ help: try this: `if gen_opt().is_some()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:62:19 + --> $DIR/redundant_pattern_matching_option.rs:63:19 | LL | } else if let None = gen_opt() { | -------^^^^------------ help: try this: `if gen_opt().is_none()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_option.rs:68:12 + --> $DIR/redundant_pattern_matching_option.rs:69:12 | LL | if let Some(..) = gen_opt() {} | -------^^^^^^^^------------ help: try this: `if gen_opt().is_some()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_option.rs:83:12 + --> $DIR/redundant_pattern_matching_option.rs:84:12 | LL | if let Some(_) = Some(42) {} | -------^^^^^^^----------- help: try this: `if Some(42).is_some()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:85:12 + --> $DIR/redundant_pattern_matching_option.rs:86:12 | LL | if let None = None::<()> {} | -------^^^^------------- help: try this: `if None::<()>.is_none()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_option.rs:87:15 + --> $DIR/redundant_pattern_matching_option.rs:88:15 | LL | while let Some(_) = Some(42) {} | ----------^^^^^^^----------- help: try this: `while Some(42).is_some()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:89:15 + --> $DIR/redundant_pattern_matching_option.rs:90:15 | LL | while let None = None::<()> {} | ----------^^^^------------- help: try this: `while None::<()>.is_none()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_option.rs:91:5 + --> $DIR/redundant_pattern_matching_option.rs:92:5 | LL | / match Some(42) { LL | | Some(_) => true, @@ -128,7 +128,7 @@ LL | | }; | |_____^ help: try this: `Some(42).is_some()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:96:5 + --> $DIR/redundant_pattern_matching_option.rs:97:5 | LL | / match None::<()> { LL | | Some(_) => false, @@ -137,19 +137,19 @@ LL | | }; | |_____^ help: try this: `None::<()>.is_none()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:104:12 + --> $DIR/redundant_pattern_matching_option.rs:105:12 | LL | if let None = *(&None::<()>) {} | -------^^^^----------------- help: try this: `if (&None::<()>).is_none()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:105:12 + --> $DIR/redundant_pattern_matching_option.rs:106:12 | LL | if let None = *&None::<()> {} | -------^^^^--------------- help: try this: `if (&None::<()>).is_none()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_option.rs:111:5 + --> $DIR/redundant_pattern_matching_option.rs:112:5 | LL | / match x { LL | | Some(_) => true, @@ -158,7 +158,7 @@ LL | | }; | |_____^ help: try this: `x.is_some()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:116:5 + --> $DIR/redundant_pattern_matching_option.rs:117:5 | LL | / match x { LL | | None => true, @@ -167,7 +167,7 @@ LL | | }; | |_____^ help: try this: `x.is_none()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching_option.rs:121:5 + --> $DIR/redundant_pattern_matching_option.rs:122:5 | LL | / match x { LL | | Some(_) => false, @@ -176,7 +176,7 @@ LL | | }; | |_____^ help: try this: `x.is_none()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_option.rs:126:5 + --> $DIR/redundant_pattern_matching_option.rs:127:5 | LL | / match x { LL | | None => false, @@ -184,5 +184,17 @@ LL | | _ => true, LL | | }; | |_____^ help: try this: `x.is_some()` -error: aborting due to 26 previous errors +error: redundant pattern matching, consider using `is_some()` + --> $DIR/redundant_pattern_matching_option.rs:142:13 + | +LL | let _ = matches!(x, Some(_)); + | ^^^^^^^^^^^^^^^^^^^^ help: try this: `x.is_some()` + +error: redundant pattern matching, consider using `is_none()` + --> $DIR/redundant_pattern_matching_option.rs:144:13 + | +LL | let _ = matches!(x, None); + | ^^^^^^^^^^^^^^^^^ help: try this: `x.is_none()` + +error: aborting due to 28 previous errors diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_result.fixed b/src/tools/clippy/tests/ui/redundant_pattern_matching_result.fixed index e4032ae44b7..d77a2af7616 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_result.fixed +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_result.fixed @@ -44,6 +44,7 @@ fn main() { issue6067(); issue6065(); issue10726(); + issue10803(); let _ = if gen_res().is_ok() { 1 @@ -133,3 +134,17 @@ fn issue10726() { _ => true, }; } + +fn issue10803() { + let x: Result<i32, i32> = Ok(42); + + let _ = x.is_ok(); + + let _ = x.is_err(); + + // Don't lint + let _ = matches!(x, Ok(16)); + + // Don't lint + let _ = matches!(x, Err(16)); +} diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_result.rs b/src/tools/clippy/tests/ui/redundant_pattern_matching_result.rs index 39eb10df878..aa884ac6bb1 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_result.rs +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_result.rs @@ -56,6 +56,7 @@ fn main() { issue6067(); issue6065(); issue10726(); + issue10803(); let _ = if let Ok(_) = gen_res() { 1 @@ -163,3 +164,17 @@ fn issue10726() { _ => true, }; } + +fn issue10803() { + let x: Result<i32, i32> = Ok(42); + + let _ = matches!(x, Ok(_)); + + let _ = matches!(x, Err(_)); + + // Don't lint + let _ = matches!(x, Ok(16)); + + // Don't lint + let _ = matches!(x, Err(16)); +} diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_result.stderr b/src/tools/clippy/tests/ui/redundant_pattern_matching_result.stderr index 5893ae4dcc4..b462f7f41b9 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_result.stderr +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_result.stderr @@ -73,67 +73,67 @@ LL | let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false }; | -------^^^^^--------------------- help: try this: `if Ok::<usize, ()>(4).is_ok()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching_result.rs:60:20 + --> $DIR/redundant_pattern_matching_result.rs:61:20 | LL | let _ = if let Ok(_) = gen_res() { | -------^^^^^------------ help: try this: `if gen_res().is_ok()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching_result.rs:62:19 + --> $DIR/redundant_pattern_matching_result.rs:63:19 | LL | } else if let Err(_) = gen_res() { | -------^^^^^^------------ help: try this: `if gen_res().is_err()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_result.rs:85:19 + --> $DIR/redundant_pattern_matching_result.rs:86:19 | LL | while let Some(_) = r#try!(result_opt()) {} | ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_result.rs:86:16 + --> $DIR/redundant_pattern_matching_result.rs:87:16 | LL | if let Some(_) = r#try!(result_opt()) {} | -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_result.rs:92:12 + --> $DIR/redundant_pattern_matching_result.rs:93:12 | LL | if let Some(_) = m!() {} | -------^^^^^^^------- help: try this: `if m!().is_some()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching_result.rs:93:15 + --> $DIR/redundant_pattern_matching_result.rs:94:15 | LL | while let Some(_) = m!() {} | ----------^^^^^^^------- help: try this: `while m!().is_some()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching_result.rs:111:12 + --> $DIR/redundant_pattern_matching_result.rs:112:12 | LL | if let Ok(_) = Ok::<i32, i32>(42) {} | -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching_result.rs:113:12 + --> $DIR/redundant_pattern_matching_result.rs:114:12 | LL | if let Err(_) = Err::<i32, i32>(42) {} | -------^^^^^^---------------------- help: try this: `if Err::<i32, i32>(42).is_err()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching_result.rs:115:15 + --> $DIR/redundant_pattern_matching_result.rs:116:15 | LL | while let Ok(_) = Ok::<i32, i32>(10) {} | ----------^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_ok()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching_result.rs:117:15 + --> $DIR/redundant_pattern_matching_result.rs:118:15 | LL | while let Err(_) = Ok::<i32, i32>(10) {} | ----------^^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_err()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching_result.rs:119:5 + --> $DIR/redundant_pattern_matching_result.rs:120:5 | LL | / match Ok::<i32, i32>(42) { LL | | Ok(_) => true, @@ -142,7 +142,7 @@ LL | | }; | |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching_result.rs:124:5 + --> $DIR/redundant_pattern_matching_result.rs:125:5 | LL | / match Err::<i32, i32>(42) { LL | | Ok(_) => false, @@ -151,7 +151,7 @@ LL | | }; | |_____^ help: try this: `Err::<i32, i32>(42).is_err()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching_result.rs:134:5 + --> $DIR/redundant_pattern_matching_result.rs:135:5 | LL | / match x { LL | | Ok(_) => true, @@ -160,7 +160,7 @@ LL | | }; | |_____^ help: try this: `x.is_ok()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching_result.rs:139:5 + --> $DIR/redundant_pattern_matching_result.rs:140:5 | LL | / match x { LL | | Ok(_) => false, @@ -169,7 +169,7 @@ LL | | }; | |_____^ help: try this: `x.is_err()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching_result.rs:144:5 + --> $DIR/redundant_pattern_matching_result.rs:145:5 | LL | / match x { LL | | Err(_) => true, @@ -178,7 +178,7 @@ LL | | }; | |_____^ help: try this: `x.is_err()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching_result.rs:149:5 + --> $DIR/redundant_pattern_matching_result.rs:150:5 | LL | / match x { LL | | Err(_) => false, @@ -186,5 +186,17 @@ LL | | _ => true, LL | | }; | |_____^ help: try this: `x.is_ok()` -error: aborting due to 26 previous errors +error: redundant pattern matching, consider using `is_ok()` + --> $DIR/redundant_pattern_matching_result.rs:171:13 + | +LL | let _ = matches!(x, Ok(_)); + | ^^^^^^^^^^^^^^^^^^ help: try this: `x.is_ok()` + +error: redundant pattern matching, consider using `is_err()` + --> $DIR/redundant_pattern_matching_result.rs:173:13 + | +LL | let _ = matches!(x, Err(_)); + | ^^^^^^^^^^^^^^^^^^^ help: try this: `x.is_err()` + +error: aborting due to 28 previous errors diff --git a/src/tools/clippy/tests/ui/regex.rs b/src/tools/clippy/tests/ui/regex.rs index a5f79b139bc..1c8e47ab594 100644 --- a/src/tools/clippy/tests/ui/regex.rs +++ b/src/tools/clippy/tests/ui/regex.rs @@ -42,6 +42,11 @@ fn syntax_error() { let escaped_string_span = Regex::new("\\b\\c"); let aux_span = Regex::new("(?ixi)"); + + let should_not_lint = Regex::new("(?u)."); + let should_not_lint = BRegex::new("(?u)."); + let invalid_utf8_should_not_lint = BRegex::new("(?-u)."); + let invalid_utf8_should_lint = Regex::new("(?-u)."); } fn trivial_regex() { @@ -71,6 +76,8 @@ fn trivial_regex() { // non-trivial regexes let non_trivial_dot = Regex::new("a.b"); let non_trivial_dot_builder = RegexBuilder::new("a.b"); + let non_trivial_dot = Regex::new("."); + let non_trivial_dot = BRegex::new("."); let non_trivial_eq = Regex::new("^foo|bar$"); let non_trivial_starts_with = Regex::new("^foo|bar"); let non_trivial_ends_with = Regex::new("^foo|bar"); diff --git a/src/tools/clippy/tests/ui/regex.stderr b/src/tools/clippy/tests/ui/regex.stderr index 6b8a772e7f0..1e8a21283cd 100644 --- a/src/tools/clippy/tests/ui/regex.stderr +++ b/src/tools/clippy/tests/ui/regex.stderr @@ -99,8 +99,14 @@ error: regex syntax error: duplicate flag LL | let aux_span = Regex::new("(?ixi)"); | ^ ^ +error: regex syntax error: pattern can match invalid UTF-8 + --> $DIR/regex.rs:49:53 + | +LL | let invalid_utf8_should_lint = Regex::new("(?-u)."); + | ^ + error: trivial regex - --> $DIR/regex.rs:48:33 + --> $DIR/regex.rs:53:33 | LL | let trivial_eq = Regex::new("^foobar$"); | ^^^^^^^^^^ @@ -108,7 +114,7 @@ LL | let trivial_eq = Regex::new("^foobar$"); = help: consider using `==` on `str`s error: trivial regex - --> $DIR/regex.rs:50:48 + --> $DIR/regex.rs:55:48 | LL | let trivial_eq_builder = RegexBuilder::new("^foobar$"); | ^^^^^^^^^^ @@ -116,7 +122,7 @@ LL | let trivial_eq_builder = RegexBuilder::new("^foobar$"); = help: consider using `==` on `str`s error: trivial regex - --> $DIR/regex.rs:52:42 + --> $DIR/regex.rs:57:42 | LL | let trivial_starts_with = Regex::new("^foobar"); | ^^^^^^^^^ @@ -124,7 +130,7 @@ LL | let trivial_starts_with = Regex::new("^foobar"); = help: consider using `str::starts_with` error: trivial regex - --> $DIR/regex.rs:54:40 + --> $DIR/regex.rs:59:40 | LL | let trivial_ends_with = Regex::new("foobar$"); | ^^^^^^^^^ @@ -132,7 +138,7 @@ LL | let trivial_ends_with = Regex::new("foobar$"); = help: consider using `str::ends_with` error: trivial regex - --> $DIR/regex.rs:56:39 + --> $DIR/regex.rs:61:39 | LL | let trivial_contains = Regex::new("foobar"); | ^^^^^^^^ @@ -140,7 +146,7 @@ LL | let trivial_contains = Regex::new("foobar"); = help: consider using `str::contains` error: trivial regex - --> $DIR/regex.rs:58:39 + --> $DIR/regex.rs:63:39 | LL | let trivial_contains = Regex::new(NOT_A_REAL_REGEX); | ^^^^^^^^^^^^^^^^ @@ -148,7 +154,7 @@ LL | let trivial_contains = Regex::new(NOT_A_REAL_REGEX); = help: consider using `str::contains` error: trivial regex - --> $DIR/regex.rs:60:40 + --> $DIR/regex.rs:65:40 | LL | let trivial_backslash = Regex::new("a/.b"); | ^^^^^^^ @@ -156,7 +162,7 @@ LL | let trivial_backslash = Regex::new("a/.b"); = help: consider using `str::contains` error: trivial regex - --> $DIR/regex.rs:63:36 + --> $DIR/regex.rs:68:36 | LL | let trivial_empty = Regex::new(""); | ^^ @@ -164,7 +170,7 @@ LL | let trivial_empty = Regex::new(""); = help: the regex is unlikely to be useful as it is error: trivial regex - --> $DIR/regex.rs:65:36 + --> $DIR/regex.rs:70:36 | LL | let trivial_empty = Regex::new("^"); | ^^^ @@ -172,7 +178,7 @@ LL | let trivial_empty = Regex::new("^"); = help: the regex is unlikely to be useful as it is error: trivial regex - --> $DIR/regex.rs:67:36 + --> $DIR/regex.rs:72:36 | LL | let trivial_empty = Regex::new("^$"); | ^^^^ @@ -180,12 +186,12 @@ LL | let trivial_empty = Regex::new("^$"); = help: consider using `str::is_empty` error: trivial regex - --> $DIR/regex.rs:69:44 + --> $DIR/regex.rs:74:44 | LL | let binary_trivial_empty = BRegex::new("^$"); | ^^^^ | = help: consider using `str::is_empty` -error: aborting due to 23 previous errors +error: aborting due to 24 previous errors diff --git a/src/tools/clippy/tests/ui/rename.fixed b/src/tools/clippy/tests/ui/rename.fixed index f7854b89ee4..30f2bfc8c1a 100644 --- a/src/tools/clippy/tests/ui/rename.fixed +++ b/src/tools/clippy/tests/ui/rename.fixed @@ -28,6 +28,7 @@ #![allow(clippy::module_name_repetitions)] #![allow(clippy::recursive_format_impl)] #![allow(clippy::invisible_characters)] +#![allow(cast_ref_to_mut)] #![allow(suspicious_double_ref_op)] #![allow(cast_ref_to_mut)] #![allow(drop_bounds)] diff --git a/src/tools/clippy/tests/ui/rename.rs b/src/tools/clippy/tests/ui/rename.rs index fa347d395ef..3939914d422 100644 --- a/src/tools/clippy/tests/ui/rename.rs +++ b/src/tools/clippy/tests/ui/rename.rs @@ -28,6 +28,7 @@ #![allow(clippy::module_name_repetitions)] #![allow(clippy::recursive_format_impl)] #![allow(clippy::invisible_characters)] +#![allow(cast_ref_to_mut)] #![allow(suspicious_double_ref_op)] #![allow(cast_ref_to_mut)] #![allow(drop_bounds)] diff --git a/src/tools/clippy/tests/ui/rename.stderr b/src/tools/clippy/tests/ui/rename.stderr index 9dffe51e5d7..7290cf32e5b 100644 --- a/src/tools/clippy/tests/ui/rename.stderr +++ b/src/tools/clippy/tests/ui/rename.stderr @@ -1,5 +1,5 @@ error: lint `clippy::almost_complete_letter_range` has been renamed to `clippy::almost_complete_range` - --> $DIR/rename.rs:51:9 + --> $DIR/rename.rs:52:9 | LL | #![warn(clippy::almost_complete_letter_range)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::almost_complete_range` @@ -7,295 +7,295 @@ LL | #![warn(clippy::almost_complete_letter_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:52:9 + --> $DIR/rename.rs:53: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:53:9 + --> $DIR/rename.rs:54: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:54:9 + --> $DIR/rename.rs:55: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:55:9 + --> $DIR/rename.rs:56: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:56:9 + --> $DIR/rename.rs:57: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:57:9 + --> $DIR/rename.rs:58:9 | LL | #![warn(clippy::cyclomatic_complexity)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity` error: lint `clippy::derive_hash_xor_eq` has been renamed to `clippy::derived_hash_with_manual_eq` - --> $DIR/rename.rs:58:9 + --> $DIR/rename.rs:59:9 | LL | #![warn(clippy::derive_hash_xor_eq)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::derived_hash_with_manual_eq` error: lint `clippy::disallowed_method` has been renamed to `clippy::disallowed_methods` - --> $DIR/rename.rs:59:9 + --> $DIR/rename.rs:60: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:60:9 + --> $DIR/rename.rs:61: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:61:9 + --> $DIR/rename.rs:62: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:62:9 + --> $DIR/rename.rs:63: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:63:9 + --> $DIR/rename.rs:64:9 | LL | #![warn(clippy::if_let_some_result)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::match_result_ok` error: lint `clippy::integer_arithmetic` has been renamed to `clippy::arithmetic_side_effects` - --> $DIR/rename.rs:64:9 + --> $DIR/rename.rs:65:9 | LL | #![warn(clippy::integer_arithmetic)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::arithmetic_side_effects` error: lint `clippy::logic_bug` has been renamed to `clippy::overly_complex_bool_expr` - --> $DIR/rename.rs:65:9 + --> $DIR/rename.rs:66: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:66:9 + --> $DIR/rename.rs:67: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:67:9 + --> $DIR/rename.rs:68: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:68:9 + --> $DIR/rename.rs:69: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:69:9 + --> $DIR/rename.rs:70: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:70:9 + --> $DIR/rename.rs:71: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:71:9 + --> $DIR/rename.rs:72: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:72:9 + --> $DIR/rename.rs:73: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:73:9 + --> $DIR/rename.rs:74: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:74:9 + --> $DIR/rename.rs:75: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:75:9 + --> $DIR/rename.rs:76: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:76:9 + --> $DIR/rename.rs:77: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:77:9 + --> $DIR/rename.rs:78: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:78:9 + --> $DIR/rename.rs:79: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:79:9 + --> $DIR/rename.rs:80:9 | LL | #![warn(clippy::zero_width_space)] | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::invisible_characters` error: lint `clippy::cast_ref_to_mut` has been renamed to `cast_ref_to_mut` - --> $DIR/rename.rs:80:9 + --> $DIR/rename.rs:81:9 | LL | #![warn(clippy::cast_ref_to_mut)] | ^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `cast_ref_to_mut` error: lint `clippy::clone_double_ref` has been renamed to `suspicious_double_ref_op` - --> $DIR/rename.rs:81:9 + --> $DIR/rename.rs:82:9 | LL | #![warn(clippy::clone_double_ref)] | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `suspicious_double_ref_op` error: lint `clippy::drop_bounds` has been renamed to `drop_bounds` - --> $DIR/rename.rs:82:9 + --> $DIR/rename.rs:83:9 | LL | #![warn(clippy::drop_bounds)] | ^^^^^^^^^^^^^^^^^^^ help: use the new name: `drop_bounds` error: lint `clippy::drop_copy` has been renamed to `dropping_copy_types` - --> $DIR/rename.rs:83:9 + --> $DIR/rename.rs:84:9 | LL | #![warn(clippy::drop_copy)] | ^^^^^^^^^^^^^^^^^ help: use the new name: `dropping_copy_types` error: lint `clippy::drop_ref` has been renamed to `dropping_references` - --> $DIR/rename.rs:84:9 + --> $DIR/rename.rs:85:9 | LL | #![warn(clippy::drop_ref)] | ^^^^^^^^^^^^^^^^ help: use the new name: `dropping_references` error: lint `clippy::for_loop_over_option` has been renamed to `for_loops_over_fallibles` - --> $DIR/rename.rs:85:9 + --> $DIR/rename.rs:86: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:86:9 + --> $DIR/rename.rs:87: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:87:9 + --> $DIR/rename.rs:88:9 | LL | #![warn(clippy::for_loops_over_fallibles)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `for_loops_over_fallibles` error: lint `clippy::forget_copy` has been renamed to `forgetting_copy_types` - --> $DIR/rename.rs:88:9 + --> $DIR/rename.rs:89:9 | LL | #![warn(clippy::forget_copy)] | ^^^^^^^^^^^^^^^^^^^ help: use the new name: `forgetting_copy_types` error: lint `clippy::forget_ref` has been renamed to `forgetting_references` - --> $DIR/rename.rs:89:9 + --> $DIR/rename.rs:90:9 | LL | #![warn(clippy::forget_ref)] | ^^^^^^^^^^^^^^^^^^ help: use the new name: `forgetting_references` error: lint `clippy::into_iter_on_array` has been renamed to `array_into_iter` - --> $DIR/rename.rs:90:9 + --> $DIR/rename.rs:91: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:91:9 + --> $DIR/rename.rs:92: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:92:9 + --> $DIR/rename.rs:93:9 | LL | #![warn(clippy::invalid_ref)] | ^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_value` error: lint `clippy::invalid_utf8_in_unchecked` has been renamed to `invalid_from_utf8_unchecked` - --> $DIR/rename.rs:93:9 + --> $DIR/rename.rs:94:9 | LL | #![warn(clippy::invalid_utf8_in_unchecked)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_from_utf8_unchecked` error: lint `clippy::let_underscore_drop` has been renamed to `let_underscore_drop` - --> $DIR/rename.rs:94:9 + --> $DIR/rename.rs:95: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:95:9 + --> $DIR/rename.rs:96: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:96:9 + --> $DIR/rename.rs:97: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:97:9 + --> $DIR/rename.rs:98: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:98:9 + --> $DIR/rename.rs:99: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:99:9 + --> $DIR/rename.rs:100: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:100:9 + --> $DIR/rename.rs:101:9 | LL | #![warn(clippy::unused_label)] | ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unused_labels` diff --git a/src/tools/clippy/tests/ui/same_functions_in_if_condition.rs b/src/tools/clippy/tests/ui/same_functions_in_if_condition.rs index aea1507cc5b..08916398cbb 100644 --- a/src/tools/clippy/tests/ui/same_functions_in_if_condition.rs +++ b/src/tools/clippy/tests/ui/same_functions_in_if_condition.rs @@ -10,6 +10,8 @@ clippy::uninlined_format_args )] +use std::marker::ConstParamTy; + fn function() -> bool { true } @@ -96,7 +98,7 @@ fn main() { }; println!("{}", os); - #[derive(PartialEq, Eq)] + #[derive(PartialEq, Eq, ConstParamTy)] enum E { A, B, diff --git a/src/tools/clippy/tests/ui/same_functions_in_if_condition.stderr b/src/tools/clippy/tests/ui/same_functions_in_if_condition.stderr index aade3b1fa45..6aacc73b90d 100644 --- a/src/tools/clippy/tests/ui/same_functions_in_if_condition.stderr +++ b/src/tools/clippy/tests/ui/same_functions_in_if_condition.stderr @@ -1,11 +1,11 @@ error: this `if` has the same function call as a previous `if` - --> $DIR/same_functions_in_if_condition.rs:37:15 + --> $DIR/same_functions_in_if_condition.rs:39:15 | LL | } else if function() { | ^^^^^^^^^^ | note: same as this - --> $DIR/same_functions_in_if_condition.rs:36:8 + --> $DIR/same_functions_in_if_condition.rs:38:8 | LL | if function() { | ^^^^^^^^^^ @@ -16,61 +16,61 @@ LL | #![deny(clippy::same_functions_in_if_condition)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: this `if` has the same function call as a previous `if` - --> $DIR/same_functions_in_if_condition.rs:42:15 + --> $DIR/same_functions_in_if_condition.rs:44:15 | LL | } else if fn_arg(a) { | ^^^^^^^^^ | note: same as this - --> $DIR/same_functions_in_if_condition.rs:41:8 + --> $DIR/same_functions_in_if_condition.rs:43:8 | LL | if fn_arg(a) { | ^^^^^^^^^ error: this `if` has the same function call as a previous `if` - --> $DIR/same_functions_in_if_condition.rs:47:15 + --> $DIR/same_functions_in_if_condition.rs:49:15 | LL | } else if obj.method() { | ^^^^^^^^^^^^ | note: same as this - --> $DIR/same_functions_in_if_condition.rs:46:8 + --> $DIR/same_functions_in_if_condition.rs:48:8 | LL | if obj.method() { | ^^^^^^^^^^^^ error: this `if` has the same function call as a previous `if` - --> $DIR/same_functions_in_if_condition.rs:52:15 + --> $DIR/same_functions_in_if_condition.rs:54:15 | LL | } else if obj.method_arg(a) { | ^^^^^^^^^^^^^^^^^ | note: same as this - --> $DIR/same_functions_in_if_condition.rs:51:8 + --> $DIR/same_functions_in_if_condition.rs:53:8 | LL | if obj.method_arg(a) { | ^^^^^^^^^^^^^^^^^ error: this `if` has the same function call as a previous `if` - --> $DIR/same_functions_in_if_condition.rs:59:15 + --> $DIR/same_functions_in_if_condition.rs:61:15 | LL | } else if v.pop().is_none() { | ^^^^^^^^^^^^^^^^^ | note: same as this - --> $DIR/same_functions_in_if_condition.rs:57:8 + --> $DIR/same_functions_in_if_condition.rs:59:8 | LL | if v.pop().is_none() { | ^^^^^^^^^^^^^^^^^ error: this `if` has the same function call as a previous `if` - --> $DIR/same_functions_in_if_condition.rs:64:15 + --> $DIR/same_functions_in_if_condition.rs:66:15 | LL | } else if v.len() == 42 { | ^^^^^^^^^^^^^ | note: same as this - --> $DIR/same_functions_in_if_condition.rs:62:8 + --> $DIR/same_functions_in_if_condition.rs:64:8 | LL | if v.len() == 42 { | ^^^^^^^^^^^^^ diff --git a/src/tools/clippy/tests/ui/single_match.fixed b/src/tools/clippy/tests/ui/single_match.fixed new file mode 100644 index 00000000000..77a2cf3b991 --- /dev/null +++ b/src/tools/clippy/tests/ui/single_match.fixed @@ -0,0 +1,209 @@ +//@run-rustfix +#![warn(clippy::single_match)] +#![allow(unused, clippy::uninlined_format_args, clippy::redundant_pattern_matching)] +fn dummy() {} + +fn single_match() { + let x = Some(1u8); + + if let Some(y) = x { + println!("{:?}", y); + }; + + let x = Some(1u8); + if let Some(y) = x { println!("{:?}", y) } + + let z = (1u8, 1u8); + if let (2..=3, 7..=9) = z { dummy() }; + + // Not linted (pattern guards used) + match x { + Some(y) if y == 0 => println!("{:?}", y), + _ => (), + } + + // Not linted (no block with statements in the single arm) + match z { + (2..=3, 7..=9) => println!("{:?}", z), + _ => println!("nope"), + } +} + +enum Foo { + Bar, + Baz(u8), +} +use std::borrow::Cow; +use Foo::*; + +fn single_match_know_enum() { + let x = Some(1u8); + let y: Result<_, i8> = Ok(1i8); + + if let Some(y) = x { dummy() }; + + if let Ok(y) = y { dummy() }; + + let c = Cow::Borrowed(""); + + if let Cow::Borrowed(..) = c { dummy() }; + + let z = Foo::Bar; + // no warning + match z { + Bar => println!("42"), + Baz(_) => (), + } + + match z { + Baz(_) => println!("42"), + Bar => (), + } +} + +// issue #173 +fn if_suggestion() { + let x = "test"; + if x == "test" { println!() } + + #[derive(PartialEq, Eq)] + enum Foo { + A, + B, + C(u32), + } + + let x = Foo::A; + if x == Foo::A { println!() } + + const FOO_C: Foo = Foo::C(0); + if x == FOO_C { println!() } + + if x == Foo::A { println!() } + + let x = &x; + if x == &Foo::A { println!() } + + enum Bar { + A, + B, + } + impl PartialEq for Bar { + fn eq(&self, rhs: &Self) -> bool { + matches!((self, rhs), (Self::A, Self::A) | (Self::B, Self::B)) + } + } + impl Eq for Bar {} + + let x = Bar::A; + if let Bar::A = x { println!() } + + // issue #7038 + struct X; + let x = Some(X); + if let None = x { println!() }; +} + +// See: issue #8282 +fn ranges() { + enum E { + V, + } + let x = (Some(E::V), Some(42)); + + // Don't lint, because the `E` enum can be extended with additional fields later. Thus, the + // proposed replacement to `if let Some(E::V)` may hide non-exhaustive warnings that appeared + // because of `match` construction. + match x { + (Some(E::V), _) => {}, + (None, _) => {}, + } + + // lint + if let (Some(_), _) = x {} + + // lint + if let (Some(E::V), _) = x { todo!() } + + // lint + if let (.., Some(E::V), _) = (Some(42), Some(E::V), Some(42)) {} + + // Don't lint, see above. + match (Some(E::V), Some(E::V), Some(E::V)) { + (.., Some(E::V), _) => {}, + (.., None, _) => {}, + } + + // Don't lint, see above. + match (Some(E::V), Some(E::V), Some(E::V)) { + (Some(E::V), ..) => {}, + (None, ..) => {}, + } + + // Don't lint, see above. + match (Some(E::V), Some(E::V), Some(E::V)) { + (_, Some(E::V), ..) => {}, + (_, None, ..) => {}, + } +} + +fn skip_type_aliases() { + enum OptionEx { + Some(i32), + None, + } + enum ResultEx { + Err(i32), + Ok(i32), + } + + use OptionEx::{None, Some}; + use ResultEx::{Err, Ok}; + + // don't lint + match Err(42) { + Ok(_) => dummy(), + Err(_) => (), + }; + + // don't lint + match Some(1i32) { + Some(_) => dummy(), + None => (), + }; +} + +macro_rules! single_match { + ($num:literal) => { + match $num { + 15 => println!("15"), + _ => (), + } + }; +} + +fn main() { + single_match!(5); + + // Don't lint + let _ = match Some(0) { + #[cfg(feature = "foo")] + Some(10) => 11, + Some(x) => x, + _ => 0, + }; +} + +fn issue_10808(bar: Option<i32>) { + if let Some(v) = bar { unsafe { + let r = &v as *const i32; + println!("{}", *r); + } } + + if let Some(v) = bar { + unsafe { + let r = &v as *const i32; + println!("{}", *r); + } + } +} diff --git a/src/tools/clippy/tests/ui/single_match.rs b/src/tools/clippy/tests/ui/single_match.rs index d0c9b7b5663..8d0ab5b99ad 100644 --- a/src/tools/clippy/tests/ui/single_match.rs +++ b/src/tools/clippy/tests/ui/single_match.rs @@ -1,6 +1,6 @@ +//@run-rustfix #![warn(clippy::single_match)] -#![allow(clippy::uninlined_format_args)] - +#![allow(unused, clippy::uninlined_format_args, clippy::redundant_pattern_matching)] fn dummy() {} fn single_match() { @@ -244,3 +244,24 @@ fn main() { _ => 0, }; } + +fn issue_10808(bar: Option<i32>) { + match bar { + Some(v) => unsafe { + let r = &v as *const i32; + println!("{}", *r); + }, + _ => {}, + } + + match bar { + #[rustfmt::skip] + Some(v) => { + unsafe { + let r = &v as *const i32; + println!("{}", *r); + } + }, + _ => {}, + } +} diff --git a/src/tools/clippy/tests/ui/single_match.stderr b/src/tools/clippy/tests/ui/single_match.stderr index 7cecc1b7395..dad66e2ab2e 100644 --- a/src/tools/clippy/tests/ui/single_match.stderr +++ b/src/tools/clippy/tests/ui/single_match.stderr @@ -155,5 +155,47 @@ LL | | (..) => {}, LL | | } | |_____^ help: try this: `if let (.., Some(E::V), _) = (Some(42), Some(E::V), Some(42)) {}` -error: aborting due to 16 previous errors +error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` + --> $DIR/single_match.rs:249:5 + | +LL | / match bar { +LL | | Some(v) => unsafe { +LL | | let r = &v as *const i32; +LL | | println!("{}", *r); +LL | | }, +LL | | _ => {}, +LL | | } + | |_____^ + | +help: try this + | +LL ~ if let Some(v) = bar { unsafe { +LL + let r = &v as *const i32; +LL + println!("{}", *r); +LL + } } + | + +error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` + --> $DIR/single_match.rs:257:5 + | +LL | / match bar { +LL | | #[rustfmt::skip] +LL | | Some(v) => { +LL | | unsafe { +... | +LL | | _ => {}, +LL | | } + | |_____^ + | +help: try this + | +LL ~ if let Some(v) = bar { +LL + unsafe { +LL + let r = &v as *const i32; +LL + println!("{}", *r); +LL + } +LL + } + | + +error: aborting due to 18 previous errors diff --git a/src/tools/clippy/tests/ui/single_match_else.fixed b/src/tools/clippy/tests/ui/single_match_else.fixed new file mode 100644 index 00000000000..f88498655a4 --- /dev/null +++ b/src/tools/clippy/tests/ui/single_match_else.fixed @@ -0,0 +1,173 @@ +//@run-rustfix +//@aux-build: proc_macros.rs +#![warn(clippy::single_match_else)] +#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)] +extern crate proc_macros; +use proc_macros::with_span; + +enum ExprNode { + ExprAddrOf, + Butterflies, + Unicorns, +} + +static NODE: ExprNode = ExprNode::Unicorns; + +fn unwrap_addr() -> Option<&'static ExprNode> { + let _ = if let ExprNode::ExprAddrOf = ExprNode::Butterflies { Some(&NODE) } else { + let x = 5; + None + }; + + // Don't lint + with_span!(span match ExprNode::Butterflies { + ExprNode::ExprAddrOf => Some(&NODE), + _ => { + let x = 5; + None + }, + }) +} + +macro_rules! unwrap_addr { + ($expression:expr) => { + match $expression { + ExprNode::ExprAddrOf => Some(&NODE), + _ => { + let x = 5; + None + }, + } + }; +} + +#[rustfmt::skip] +fn main() { + unwrap_addr!(ExprNode::Unicorns); + + // + // don't lint single exprs/statements + // + + // don't lint here + match Some(1) { + Some(a) => println!("${:?}", a), + None => return, + } + + // don't lint here + match Some(1) { + Some(a) => println!("${:?}", a), + None => { + return + }, + } + + // don't lint here + match Some(1) { + Some(a) => println!("${:?}", a), + None => { + return; + }, + } + + // + // lint multiple exprs/statements "else" blocks + // + + // lint here + if let Some(a) = Some(1) { println!("${:?}", a) } else { + println!("else block"); + return + } + + // lint here + if let Some(a) = Some(1) { println!("${:?}", a) } else { + println!("else block"); + return; + } + + // lint here + use std::convert::Infallible; + if let Ok(a) = Result::<i32, Infallible>::Ok(1) { println!("${:?}", a) } else { + println!("else block"); + return; + } + + use std::borrow::Cow; + if let Cow::Owned(a) = Cow::from("moo") { println!("${:?}", a) } else { + println!("else block"); + return; + } +} + +fn issue_10808(bar: Option<i32>) { + if let Some(v) = bar { unsafe { + let r = &v as *const i32; + println!("{}", *r); + } } else { + println!("None1"); + println!("None2"); + } + + if let Some(v) = bar { + println!("Some"); + println!("{v}"); + } else { unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + } } + + if let Some(v) = bar { unsafe { + let r = &v as *const i32; + println!("{}", *r); + } } else { unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + } } + + if let Some(v) = bar { + unsafe { + let r = &v as *const i32; + println!("{}", *r); + } + } else { + println!("None"); + println!("None"); + } + + match bar { + Some(v) => { + println!("Some"); + println!("{v}"); + }, + #[rustfmt::skip] + None => { + unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + } + }, + } + + match bar { + #[rustfmt::skip] + Some(v) => { + unsafe { + let r = &v as *const i32; + println!("{}", *r); + } + }, + #[rustfmt::skip] + None => { + unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + } + }, + } +} diff --git a/src/tools/clippy/tests/ui/single_match_else.rs b/src/tools/clippy/tests/ui/single_match_else.rs index c8ac768b60f..b34b9553919 100644 --- a/src/tools/clippy/tests/ui/single_match_else.rs +++ b/src/tools/clippy/tests/ui/single_match_else.rs @@ -1,7 +1,7 @@ +//@run-rustfix //@aux-build: proc_macros.rs #![warn(clippy::single_match_else)] -#![allow(clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)] - +#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)] extern crate proc_macros; use proc_macros::with_span; @@ -115,3 +115,87 @@ fn main() { } } } + +fn issue_10808(bar: Option<i32>) { + match bar { + Some(v) => unsafe { + let r = &v as *const i32; + println!("{}", *r); + }, + None => { + println!("None1"); + println!("None2"); + }, + } + + match bar { + Some(v) => { + println!("Some"); + println!("{v}"); + }, + None => unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + }, + } + + match bar { + Some(v) => unsafe { + let r = &v as *const i32; + println!("{}", *r); + }, + None => unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + }, + } + + match bar { + #[rustfmt::skip] + Some(v) => { + unsafe { + let r = &v as *const i32; + println!("{}", *r); + } + }, + None => { + println!("None"); + println!("None"); + }, + } + + match bar { + Some(v) => { + println!("Some"); + println!("{v}"); + }, + #[rustfmt::skip] + None => { + unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + } + }, + } + + match bar { + #[rustfmt::skip] + Some(v) => { + unsafe { + let r = &v as *const i32; + println!("{}", *r); + } + }, + #[rustfmt::skip] + None => { + unsafe { + let v = 0; + let r = &v as *const i32; + println!("{}", *r); + } + }, + } +} diff --git a/src/tools/clippy/tests/ui/single_match_else.stderr b/src/tools/clippy/tests/ui/single_match_else.stderr index 62876a55dc6..228236f3bb8 100644 --- a/src/tools/clippy/tests/ui/single_match_else.stderr +++ b/src/tools/clippy/tests/ui/single_match_else.stderr @@ -100,5 +100,101 @@ LL + return; LL + } | -error: aborting due to 5 previous errors +error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` + --> $DIR/single_match_else.rs:120:5 + | +LL | / match bar { +LL | | Some(v) => unsafe { +LL | | let r = &v as *const i32; +LL | | println!("{}", *r); +... | +LL | | }, +LL | | } + | |_____^ + | +help: try this + | +LL ~ if let Some(v) = bar { unsafe { +LL + let r = &v as *const i32; +LL + println!("{}", *r); +LL + } } else { +LL + println!("None1"); +LL + println!("None2"); +LL + } + | + +error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` + --> $DIR/single_match_else.rs:131:5 + | +LL | / match bar { +LL | | Some(v) => { +LL | | println!("Some"); +LL | | println!("{v}"); +... | +LL | | }, +LL | | } + | |_____^ + | +help: try this + | +LL ~ if let Some(v) = bar { +LL + println!("Some"); +LL + println!("{v}"); +LL + } else { unsafe { +LL + let v = 0; +LL + let r = &v as *const i32; +LL + println!("{}", *r); +LL + } } + | + +error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` + --> $DIR/single_match_else.rs:143:5 + | +LL | / match bar { +LL | | Some(v) => unsafe { +LL | | let r = &v as *const i32; +LL | | println!("{}", *r); +... | +LL | | }, +LL | | } + | |_____^ + | +help: try this + | +LL ~ if let Some(v) = bar { unsafe { +LL + let r = &v as *const i32; +LL + println!("{}", *r); +LL + } } else { unsafe { +LL + let v = 0; +LL + let r = &v as *const i32; +LL + println!("{}", *r); +LL + } } + | + +error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let` + --> $DIR/single_match_else.rs:155:5 + | +LL | / match bar { +LL | | #[rustfmt::skip] +LL | | Some(v) => { +LL | | unsafe { +... | +LL | | }, +LL | | } + | |_____^ + | +help: try this + | +LL ~ if let Some(v) = bar { +LL + unsafe { +LL + let r = &v as *const i32; +LL + println!("{}", *r); +LL + } +LL + } else { +LL + println!("None"); +LL + println!("None"); +LL + } + | + +error: aborting due to 9 previous errors diff --git a/src/tools/clippy/tests/ui/suspicious_else_formatting.rs b/src/tools/clippy/tests/ui/suspicious_else_formatting.rs index e0153cdd8cd..4823d909208 100644 --- a/src/tools/clippy/tests/ui/suspicious_else_formatting.rs +++ b/src/tools/clippy/tests/ui/suspicious_else_formatting.rs @@ -1,7 +1,7 @@ //@aux-build:proc_macro_suspicious_else_formatting.rs #![warn(clippy::suspicious_else_formatting)] -#![allow(clippy::if_same_then_else, clippy::let_unit_value)] +#![allow(clippy::if_same_then_else, clippy::let_unit_value, clippy::needless_else)] extern crate proc_macro_suspicious_else_formatting; use proc_macro_suspicious_else_formatting::DeriveBadSpan; diff --git a/src/tools/clippy/tests/ui/unused_async.rs b/src/tools/clippy/tests/ui/unused_async.rs index 4ca7f29b34c..bfaa5dadfa5 100644 --- a/src/tools/clippy/tests/ui/unused_async.rs +++ b/src/tools/clippy/tests/ui/unused_async.rs @@ -3,6 +3,26 @@ use std::future::Future; use std::pin::Pin; +mod issue10800 { + #![allow(dead_code, unused_must_use, clippy::no_effect)] + + use std::future::ready; + + async fn async_block_await() { + async { + ready(()).await; + }; + } + + async fn normal_block_await() { + { + { + ready(()).await; + } + } + } +} + async fn foo() -> i32 { 4 } diff --git a/src/tools/clippy/tests/ui/unused_async.stderr b/src/tools/clippy/tests/ui/unused_async.stderr index cff3eccbd32..8ac2066a6b2 100644 --- a/src/tools/clippy/tests/ui/unused_async.stderr +++ b/src/tools/clippy/tests/ui/unused_async.stderr @@ -1,5 +1,23 @@ error: unused `async` for function with no await statements - --> $DIR/unused_async.rs:6:1 + --> $DIR/unused_async.rs:11:5 + | +LL | / async fn async_block_await() { +LL | | async { +LL | | ready(()).await; +LL | | }; +LL | | } + | |_____^ + | + = help: consider removing the `async` from this function +note: `await` used in an async block, which does not require the enclosing function to be `async` + --> $DIR/unused_async.rs:13:23 + | +LL | ready(()).await; + | ^^^^^ + = note: `-D clippy::unused-async` implied by `-D warnings` + +error: unused `async` for function with no await statements + --> $DIR/unused_async.rs:26:1 | LL | / async fn foo() -> i32 { LL | | 4 @@ -7,10 +25,9 @@ LL | | } | |_^ | = help: consider removing the `async` from this function - = note: `-D clippy::unused-async` implied by `-D warnings` error: unused `async` for function with no await statements - --> $DIR/unused_async.rs:17:5 + --> $DIR/unused_async.rs:37:5 | LL | / async fn unused(&self) -> i32 { LL | | 1 @@ -19,5 +36,5 @@ LL | | } | = help: consider removing the `async` from this function -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors diff --git a/src/tools/clippy/tests/ui/wildcard_imports.fixed b/src/tools/clippy/tests/ui/wildcard_imports.fixed index bd845361fa8..733bbcfbcef 100644 --- a/src/tools/clippy/tests/ui/wildcard_imports.fixed +++ b/src/tools/clippy/tests/ui/wildcard_imports.fixed @@ -24,6 +24,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::inner_extern_bar; use wildcard_imports_helper::{ExternA, extern_foo}; use std::io::prelude::*; +use wildcard_imports_helper::extern_prelude::v1::*; use wildcard_imports_helper::prelude::v1::*; struct ReadFoo; @@ -81,6 +82,7 @@ fn main() { let _ = inner_struct_mod::C; let _ = ExternA; let _ = PreludeModAnywhere; + let _ = ExternPreludeModAnywhere; double_struct_import_test!(); double_struct_import_test!(); diff --git a/src/tools/clippy/tests/ui/wildcard_imports.rs b/src/tools/clippy/tests/ui/wildcard_imports.rs index fb51f7bdfcc..4acdd374bde 100644 --- a/src/tools/clippy/tests/ui/wildcard_imports.rs +++ b/src/tools/clippy/tests/ui/wildcard_imports.rs @@ -24,6 +24,7 @@ use wildcard_imports_helper::inner::inner_for_self_import::*; use wildcard_imports_helper::*; use std::io::prelude::*; +use wildcard_imports_helper::extern_prelude::v1::*; use wildcard_imports_helper::prelude::v1::*; struct ReadFoo; @@ -81,6 +82,7 @@ fn main() { let _ = inner_struct_mod::C; let _ = ExternA; let _ = PreludeModAnywhere; + let _ = ExternPreludeModAnywhere; double_struct_import_test!(); double_struct_import_test!(); diff --git a/src/tools/clippy/tests/ui/wildcard_imports.stderr b/src/tools/clippy/tests/ui/wildcard_imports.stderr index 6b469cdfc44..235be2d5708 100644 --- a/src/tools/clippy/tests/ui/wildcard_imports.stderr +++ b/src/tools/clippy/tests/ui/wildcard_imports.stderr @@ -37,55 +37,55 @@ LL | use wildcard_imports_helper::*; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:95:13 + --> $DIR/wildcard_imports.rs:97:13 | LL | use crate::fn_mod::*; | ^^^^^^^^^^^^^^^^ help: try: `crate::fn_mod::foo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:101:75 + --> $DIR/wildcard_imports.rs:103:75 | LL | use wildcard_imports_helper::inner::inner_for_self_import::{self, *}; | ^ help: try: `inner_extern_foo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:102:13 + --> $DIR/wildcard_imports.rs:104:13 | LL | use wildcard_imports_helper::*; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternA, extern_foo}` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:113:20 + --> $DIR/wildcard_imports.rs:115:20 | LL | use self::{inner::*, inner2::*}; | ^^^^^^^^ help: try: `inner::inner_foo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:113:30 + --> $DIR/wildcard_imports.rs:115:30 | LL | use self::{inner::*, inner2::*}; | ^^^^^^^^^ help: try: `inner2::inner_bar` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:120:13 + --> $DIR/wildcard_imports.rs:122:13 | LL | use wildcard_imports_helper::*; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `wildcard_imports_helper::{ExternExportedEnum, ExternExportedStruct, extern_exported}` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:149:9 + --> $DIR/wildcard_imports.rs:151:9 | LL | use crate::in_fn_test::*; | ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:158:9 + --> $DIR/wildcard_imports.rs:160:9 | LL | use crate:: in_fn_test:: * ; | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate:: in_fn_test::exported` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:159:9 + --> $DIR/wildcard_imports.rs:161:9 | LL | use crate:: fn_mod:: | _________^ @@ -93,37 +93,37 @@ LL | | *; | |_________^ help: try: `crate:: fn_mod::foo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:170:13 + --> $DIR/wildcard_imports.rs:172:13 | LL | use super::*; | ^^^^^^^^ help: try: `super::foofoo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:205:17 + --> $DIR/wildcard_imports.rs:207:17 | LL | use super::*; | ^^^^^^^^ help: try: `super::insidefoo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:213:13 + --> $DIR/wildcard_imports.rs:215:13 | LL | use super_imports::*; | ^^^^^^^^^^^^^^^^ help: try: `super_imports::foofoo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:222:17 + --> $DIR/wildcard_imports.rs:224:17 | LL | use super::super::*; | ^^^^^^^^^^^^^^^ help: try: `super::super::foofoo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:231:13 + --> $DIR/wildcard_imports.rs:233:13 | LL | use super::super::super_imports::*; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `super::super::super_imports::foofoo` error: usage of wildcard import - --> $DIR/wildcard_imports.rs:239:13 + --> $DIR/wildcard_imports.rs:241:13 | LL | use super::*; | ^^^^^^^^ help: try: `super::foofoo` diff --git a/src/tools/miri/ci.sh b/src/tools/miri/ci.sh index b5b3b211b05..a8aae524e71 100755 --- a/src/tools/miri/ci.sh +++ b/src/tools/miri/ci.sh @@ -17,11 +17,11 @@ begingroup "Building Miri" echo "Installing release version of Miri" export RUSTFLAGS="-D warnings" export CARGO_INCREMENTAL=0 -./miri install # implicitly locked +export CARGO_EXTRA_FLAGS="--locked" +./miri install # Prepare debug build for direct `./miri` invocations echo "Building debug version of Miri" -export CARGO_EXTRA_FLAGS="--locked" ./miri check --no-default-features # make sure this can be built ./miri check --all-features # and this, too ./miri build --all-targets # the build that all the `./miri test` below will use @@ -39,8 +39,11 @@ function run_tests { ## ui test suite ./miri test if [ -z "${MIRI_TEST_TARGET+exists}" ]; then - # Only for host architecture: tests with optimizations (`-O` is what cargo passes, but crank MIR - # optimizations up all the way, too). + # Host-only tests: running these on all targets is unlikely to catch more problems and would + # cost a lot of CI time. + + # Tests with optimizations (`-O` is what cargo passes, but crank MIR optimizations up all the + # way, too). # Optimizations change diagnostics (mostly backtraces), so we don't check # them. Also error locations change so we don't run the failing tests. # We explicitly enable debug-assertions here, they are disabled by -O but we have tests @@ -51,6 +54,9 @@ function run_tests { for FILE in tests/many-seeds/*.rs; do MIRI_SEEDS=64 CARGO_EXTRA_FLAGS="$CARGO_EXTRA_FLAGS -q" ./miri many-seeds ./miri run "$FILE" done + + # Check that the benchmarks build and run, but without actually benchmarking. + HYPERFINE="bash -c" ./miri bench fi ## test-cargo-miri @@ -75,13 +81,6 @@ function run_tests { unset RUSTC MIRI rm -rf .cargo - # Ensure that our benchmarks all work, but only on Linux hosts. - if [ -z "${MIRI_TEST_TARGET+exists}" ] && [ "$HOST_TARGET" = x86_64-unknown-linux-gnu ] ; then - for BENCH in $(ls "bench-cargo-miri"); do - cargo miri run --manifest-path bench-cargo-miri/$BENCH/Cargo.toml - done - fi - endgroup } diff --git a/src/tools/miri/miri b/src/tools/miri/miri index 48a46a76a12..7cda995879c 100755 --- a/src/tools/miri/miri +++ b/src/tools/miri/miri @@ -77,7 +77,7 @@ if [ -z "$COMMAND" ]; then fi shift # macOS does not have a useful readlink/realpath so we have to use Python instead... -MIRIDIR=$(python3 -c 'import os, sys; print(os.path.dirname(os.path.realpath(sys.argv[1])))' "$0") +MIRIDIR=$(python3 -c 'import pathlib, sys; print(pathlib.Path(sys.argv[1]).resolve().parent.as_posix())' "$0") # Used for rustc syncs. JOSH_FILTER=":rev(75dd959a3a40eb5b4574f8d2e23aa6efbeb33573:prefix=src/tools/miri):/src/tools/miri" # Needed for `./miri bench`. @@ -184,6 +184,8 @@ many-seeds) exit 0 ;; bench) + # The hyperfine to use + HYPERFINE=${HYPERFINE:-hyperfine -w 1 -m 5 --shell=none} # Make sure we have an up-to-date Miri installed "$0" install # Run the requested benchmarks @@ -193,7 +195,7 @@ bench) BENCHES=("$@") fi for BENCH in "${BENCHES[@]}"; do - hyperfine -w 1 -m 5 --shell=none "cargo +$TOOLCHAIN miri run --manifest-path $MIRIDIR/bench-cargo-miri/$BENCH/Cargo.toml" + $HYPERFINE "cargo +$TOOLCHAIN miri run --manifest-path $MIRIDIR/bench-cargo-miri/$BENCH/Cargo.toml" done exit 0 ;; @@ -280,10 +282,9 @@ find_sysroot() { # Run command. case "$COMMAND" in install) - # "--locked" to respect the Cargo.lock file if it exists. # Install binaries to the miri toolchain's sysroot so they do not interact with other toolchains. - $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR" --force --locked --root "$SYSROOT" "$@" - $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR"/cargo-miri --force --locked --root "$SYSROOT" "$@" + $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR" --force --root "$SYSROOT" "$@" + $CARGO install $CARGO_EXTRA_FLAGS --path "$MIRIDIR"/cargo-miri --force --root "$SYSROOT" "$@" ;; check) # Check, and let caller control flags. diff --git a/src/tools/miri/rust-version b/src/tools/miri/rust-version index 0bff100dc14..ee815ae6f8f 100644 --- a/src/tools/miri/rust-version +++ b/src/tools/miri/rust-version @@ -1 +1 @@ -8b4b20836b832e91aa605a2faf5e2a55190202c8 +33c3d101280c8eb3cd8af421bfb56a8afcc3881d diff --git a/src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs b/src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs index 10873c46a6c..cea2cffa913 100644 --- a/src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs +++ b/src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs @@ -92,7 +92,7 @@ impl HistoryData { { // NOTE: `transition_range` is explicitly absent from the error message, it has no significance // to the user. The meaningful one is `access_range`. - self.events.push((Some(span.data()), format!("{this} then transitioned {transition} due to a {rel} {access_kind} at offsets {access_range:?}", rel = if is_foreign { "foreign" } else { "child" }))); + self.events.push((Some(span.data()), format!("{this} later transitioned to {endpoint} due to a {rel} {access_kind} at offsets {access_range:?}", endpoint = transition.endpoint(), rel = if is_foreign { "foreign" } else { "child" }))); self.events.push((None, format!("this corresponds to {}", transition.summary()))); } } @@ -212,12 +212,13 @@ impl History { /// Reconstruct the history relevant to `error_offset` by filtering /// only events whose range contains the offset we are interested in. - fn extract_relevant(&self, error_offset: u64) -> Self { + fn extract_relevant(&self, error_offset: u64, error_kind: TransitionError) -> Self { History { events: self .events .iter() .filter(|e| e.transition_range.contains(&error_offset)) + .filter(|e| e.transition.is_relevant(error_kind)) .cloned() .collect::<Vec<_>>(), created: self.created, @@ -303,7 +304,7 @@ impl TbError<'_> { history.extend(self.accessed_info.history.forget(), "accessed", false); } history.extend( - self.conflicting_info.history.extract_relevant(self.error_offset), + self.conflicting_info.history.extract_relevant(self.error_offset, self.error_kind), conflicting_tag_name, true, ); diff --git a/src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs b/src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs index 7e3e587db72..6b1e722b65e 100644 --- a/src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs +++ b/src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs @@ -1,6 +1,7 @@ use std::cmp::{Ordering, PartialOrd}; use std::fmt; +use crate::borrow_tracker::tree_borrows::diagnostics::TransitionError; use crate::borrow_tracker::tree_borrows::tree::AccessRelatedness; use crate::borrow_tracker::AccessKind; @@ -115,26 +116,31 @@ mod transition { /// Public interface to the state machine that controls read-write permissions. /// This is the "private `enum`" pattern. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Permission(PermissionPriv); +pub struct Permission { + inner: PermissionPriv, +} /// Transition from one permission to the next. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct PermTransition(PermissionPriv, PermissionPriv); +pub struct PermTransition { + from: PermissionPriv, + to: PermissionPriv, +} impl Permission { /// Default initial permission of the root of a new tree. pub fn new_root() -> Self { - Self(Active) + Self { inner: Active } } /// Default initial permission of a reborrowed mutable reference. pub fn new_unique_2phase(ty_is_freeze: bool) -> Self { - Self(Reserved { ty_is_freeze }) + Self { inner: Reserved { ty_is_freeze } } } /// Default initial permission of a reborrowed shared reference pub fn new_frozen() -> Self { - Self(Frozen) + Self { inner: Frozen } } /// Apply the transition to the inner PermissionPriv. @@ -144,9 +150,9 @@ impl Permission { old_perm: Self, protected: bool, ) -> Option<PermTransition> { - let old_state = old_perm.0; + let old_state = old_perm.inner; transition::perform_access(kind, rel_pos, old_state, protected) - .map(|new_state| PermTransition(old_state, new_state)) + .map(|new_state| PermTransition { from: old_state, to: new_state }) } } @@ -155,26 +161,27 @@ impl PermTransition { /// should be possible, but the same is not guaranteed by construction of /// transitions inferred by diagnostics. This checks that a transition /// reconstructed by diagnostics is indeed one that could happen. - fn is_possible(old: PermissionPriv, new: PermissionPriv) -> bool { - old <= new + fn is_possible(self) -> bool { + self.from <= self.to } - pub fn from(old: Permission, new: Permission) -> Option<Self> { - Self::is_possible(old.0, new.0).then_some(Self(old.0, new.0)) + pub fn from(from: Permission, to: Permission) -> Option<Self> { + let t = Self { from: from.inner, to: to.inner }; + t.is_possible().then_some(t) } pub fn is_noop(self) -> bool { - self.0 == self.1 + self.from == self.to } /// Extract result of a transition (checks that the starting point matches). pub fn applied(self, starting_point: Permission) -> Option<Permission> { - (starting_point.0 == self.0).then_some(Permission(self.1)) + (starting_point.inner == self.from).then_some(Permission { inner: self.to }) } /// Extract starting point of a transition pub fn started(self) -> Permission { - Permission(self.0) + Permission { inner: self.from } } /// Determines whether a transition that occured is compatible with the presence @@ -190,10 +197,9 @@ impl PermTransition { /// }; /// ``` pub fn is_allowed_by_protector(&self) -> bool { - let &Self(old, new) = self; - assert!(Self::is_possible(old, new)); - match (old, new) { - _ if old == new => true, + assert!(self.is_possible()); + match (self.from, self.to) { + _ if self.from == self.to => true, // It is always a protector violation to not be readable anymore (_, Disabled) => false, // In the case of a `Reserved` under a protector, both transitions @@ -204,16 +210,9 @@ impl PermTransition { (Reserved { .. }, Active) | (Reserved { .. }, Frozen) => true, // This pointer should have stayed writeable for the whole function (Active, Frozen) => false, - _ => unreachable!("Transition from {old:?} to {new:?} should never be possible"), + _ => unreachable!("Transition {} should never be possible", self), } } - - /// Composition function: get the transition that can be added after `app` to - /// produce `self`. - pub fn apply_start(self, app: Self) -> Option<Self> { - let new_start = app.applied(Permission(self.0))?; - Self::from(new_start, Permission(self.1)) - } } pub mod diagnostics { @@ -224,10 +223,10 @@ pub mod diagnostics { f, "{}", match self { - PermissionPriv::Reserved { .. } => "Reserved", - PermissionPriv::Active => "Active", - PermissionPriv::Frozen => "Frozen", - PermissionPriv::Disabled => "Disabled", + Reserved { .. } => "Reserved", + Active => "Active", + Frozen => "Frozen", + Disabled => "Disabled", } ) } @@ -235,13 +234,13 @@ pub mod diagnostics { impl fmt::Display for PermTransition { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "from {} to {}", self.0, self.1) + write!(f, "from {} to {}", self.from, self.to) } } impl fmt::Display for Permission { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) + write!(f, "{}", self.inner) } } @@ -251,7 +250,7 @@ pub mod diagnostics { // Make sure there are all of the same length as each other // and also as `diagnostics::DisplayFmtPermission.uninit` otherwise // alignment will be incorrect. - match self.0 { + match self.inner { Reserved { ty_is_freeze: true } => "Res", Reserved { ty_is_freeze: false } => "Re*", Active => "Act", @@ -269,9 +268,9 @@ pub mod diagnostics { /// to have write permissions, because that's what the diagnostics care about /// (otherwise `Reserved -> Frozen` would be considered a noop). pub fn summary(&self) -> &'static str { - assert!(Self::is_possible(self.0, self.1)); - match (self.0, self.1) { - (_, Active) => "an activation", + assert!(self.is_possible()); + match (self.from, self.to) { + (_, Active) => "the first write to a 2-phase borrowed mutable reference", (_, Frozen) => "a loss of write permissions", (Frozen, Disabled) => "a loss of read permissions", (_, Disabled) => "a loss of read and write permissions", @@ -279,6 +278,118 @@ pub mod diagnostics { unreachable!("Transition from {old:?} to {new:?} should never be possible"), } } + + /// Determines whether `self` is a relevant transition for the error `err`. + /// `self` will be a transition that happened to a tag some time before + /// that tag caused the error. + /// + /// Irrelevant events: + /// - modifications of write permissions when the error is related to read permissions + /// (on failed reads and protected `Frozen -> Disabled`, ignore `Reserved -> Active`, + /// `Reserved -> Frozen`, and `Active -> Frozen`) + /// - all transitions for attempts to deallocate strongly protected tags + /// + /// # Panics + /// + /// This function assumes that its arguments apply to the same location + /// and that they were obtained during a normal execution. It will panic otherwise. + /// - `err` cannot be a `ProtectedTransition(_)` of a noop transition, as those + /// never trigger protectors; + /// - all transitions involved in `self` and `err` should be increasing + /// (Reserved < Active < Frozen < Disabled); + /// - between `self` and `err` the permission should also be increasing, + /// so all permissions inside `err` should be greater than `self.1`; + /// - `Active` and `Reserved` cannot cause an error due to insufficient permissions, + /// so `err` cannot be a `ChildAccessForbidden(_)` of either of them; + pub(in super::super) fn is_relevant(&self, err: TransitionError) -> bool { + // NOTE: `super::super` is the visibility of `TransitionError` + assert!(self.is_possible()); + if self.is_noop() { + return false; + } + match err { + TransitionError::ChildAccessForbidden(insufficient) => { + // Show where the permission was gained then lost, + // but ignore unrelated permissions. + // This eliminates transitions like `Active -> Frozen` + // when the error is a failed `Read`. + match (self.to, insufficient.inner) { + (Frozen, Frozen) => true, + (Active, Frozen) => true, + (Disabled, Disabled) => true, + // A pointer being `Disabled` is a strictly stronger source of + // errors than it being `Frozen`. If we try to access a `Disabled`, + // then where it became `Frozen` (or `Active`) is the least of our concerns for now. + (Active | Frozen, Disabled) => false, + + // `Active` and `Reserved` have all permissions, so a + // `ChildAccessForbidden(Reserved | Active)` can never exist. + (_, Active) | (_, Reserved { .. }) => + unreachable!("this permission cannot cause an error"), + // No transition has `Reserved` as its `.to` unless it's a noop. + (Reserved { .. }, _) => unreachable!("self is a noop transition"), + // All transitions produced in normal executions (using `apply_access`) + // change permissions in the order `Reserved -> Active -> Frozen -> Disabled`. + // We assume that the error was triggered on the same location that + // the transition `self` applies to, so permissions found must be increasing + // in the order `self.from < self.to <= insufficient.inner` + (Disabled, Frozen) => + unreachable!("permissions between self and err must be increasing"), + } + } + TransitionError::ProtectedTransition(forbidden) => { + assert!(!forbidden.is_noop()); + // Show how we got to the starting point of the forbidden transition, + // but ignore what came before. + // This eliminates transitions like `Reserved -> Active` + // when the error is a `Frozen -> Disabled`. + match (self.to, forbidden.from, forbidden.to) { + // We absolutely want to know where it was activated. + (Active, Active, Frozen | Disabled) => true, + // And knowing where it became Frozen is also important. + (Frozen, Frozen, Disabled) => true, + // If the error is a transition `Frozen -> Disabled`, then we don't really + // care whether before that was `Reserved -> Active -> Frozen` or + // `Reserved -> Frozen` or even `Frozen` directly. + // The error will only show either + // - created as Frozen, then Frozen -> Disabled is forbidden + // - created as Reserved, later became Frozen, then Frozen -> Disabled is forbidden + // In both cases the `Reserved -> Active` part is inexistant or irrelevant. + (Active, Frozen, Disabled) => false, + + // `Reserved -> Frozen` does not trigger protectors. + (_, Reserved { .. }, Frozen) => + unreachable!("this transition cannot cause an error"), + // No transition has `Reserved` as its `.to` unless it's a noop. + (Reserved { .. }, _, _) => unreachable!("self is a noop transition"), + (_, Disabled, Disabled) | (_, Frozen, Frozen) | (_, Active, Active) => + unreachable!("err contains a noop transition"), + + // Permissions only evolve in the order `Reserved -> Active -> Frozen -> Disabled`, + // so permissions found must be increasing in the order + // `self.from < self.to <= forbidden.from < forbidden.to`. + (Disabled, Reserved { .. } | Active | Frozen, _) + | (Frozen, Reserved { .. } | Active, _) + | (Active, Reserved { .. }, _) => + unreachable!("permissions between self and err must be increasing"), + (_, Disabled, Reserved { .. } | Active | Frozen) + | (_, Frozen, Reserved { .. } | Active) + | (_, Active, Reserved { .. }) => + unreachable!("permissions within err must be increasing"), + } + } + // We don't care because protectors evolve independently from + // permissions. + TransitionError::ProtectedDealloc => false, + } + } + + /// Endpoint of a transition. + /// Meant only for diagnostics, use `applied` in non-diagnostics + /// code, which also checks that the starting point matches the current state. + pub fn endpoint(&self) -> Permission { + Permission { inner: self.to } + } } } diff --git a/src/tools/miri/src/diagnostics.rs b/src/tools/miri/src/diagnostics.rs index 14931baaadf..2a06bd871ef 100644 --- a/src/tools/miri/src/diagnostics.rs +++ b/src/tools/miri/src/diagnostics.rs @@ -3,6 +3,8 @@ use std::num::NonZeroU64; use log::trace; +use rustc_const_eval::ReportErrorExt; +use rustc_errors::DiagnosticMessage; use rustc_span::{source_map::DUMMY_SP, SpanData, Symbol}; use rustc_target::abi::{Align, Size}; @@ -83,7 +85,25 @@ impl fmt::Display for TerminationInfo { } } -impl MachineStopType for TerminationInfo {} +impl fmt::Debug for TerminationInfo { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{self}") + } +} + +impl MachineStopType for TerminationInfo { + fn diagnostic_message(&self) -> DiagnosticMessage { + self.to_string().into() + } + fn add_args( + self: Box<Self>, + _: &mut dyn FnMut( + std::borrow::Cow<'static, str>, + rustc_errors::DiagnosticArgValue<'static>, + ), + ) { + } +} /// Miri specific diagnostics pub enum NonHaltingDiagnostic { @@ -302,8 +322,31 @@ pub fn report_error<'tcx, 'mir>( let stacktrace = ecx.generate_stacktrace(); let (stacktrace, was_pruned) = prune_stacktrace(stacktrace, &ecx.machine); - e.print_backtrace(); - msg.insert(0, e.to_string()); + let (e, backtrace) = e.into_parts(); + backtrace.print_backtrace(); + + // We want to dump the allocation if this is `InvalidUninitBytes`. Since `add_args` consumes + // the `InterpError`, we extract the variables it before that. + let extra = match e { + UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some((alloc_id, access)))) => + Some((alloc_id, access)), + _ => None, + }; + + // FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the + // label and arguments from the InterpError. + let e = { + let handler = &ecx.tcx.sess.parse_sess.span_diagnostic; + let mut diag = ecx.tcx.sess.struct_allow(""); + let msg = e.diagnostic_message(); + e.add_args(handler, &mut diag); + let s = handler.eagerly_translate_to_string(msg, diag.args()); + diag.cancel(); + s + }; + + msg.insert(0, e); + report_msg( DiagLevel::Error, if let Some(title) = title { format!("{title}: {}", msg[0]) } else { msg[0].clone() }, @@ -332,15 +375,12 @@ pub fn report_error<'tcx, 'mir>( } // Extra output to help debug specific issues. - match e.kind() { - UndefinedBehavior(UndefinedBehaviorInfo::InvalidUninitBytes(Some((alloc_id, access)))) => { - eprintln!( - "Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:", - range = access.uninit, - ); - eprintln!("{:?}", ecx.dump_alloc(*alloc_id)); - } - _ => {} + if let Some((alloc_id, access)) = extra { + eprintln!( + "Uninitialized memory occurred at {alloc_id:?}{range:?}, in this allocation:", + range = access.uninit, + ); + eprintln!("{:?}", ecx.dump_alloc(alloc_id)); } None @@ -438,12 +478,15 @@ pub fn report_msg<'tcx>( // Add visual separator before backtrace. err.note(if extra_span { "BACKTRACE (of the first span):" } else { "BACKTRACE:" }); } + + let (mut err, handler) = err.into_diagnostic().unwrap(); + // Add backtrace for (idx, frame_info) in stacktrace.iter().enumerate() { let is_local = machine.is_local(frame_info); // No span for non-local frames and the first frame (which is the error site). if is_local && idx > 0 { - err.span_note(frame_info.span, frame_info.to_string()); + err.eager_subdiagnostic(handler, frame_info.as_note(machine.tcx)); } else { let sm = sess.source_map(); let span = sm.span_to_embeddable_string(frame_info.span); @@ -451,7 +494,7 @@ pub fn report_msg<'tcx>( } } - err.emit(); + handler.emit_diagnostic(&mut err); } impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> { diff --git a/src/tools/miri/src/eval.rs b/src/tools/miri/src/eval.rs index 43d8f221ce3..1e9d48be65e 100644 --- a/src/tools/miri/src/eval.rs +++ b/src/tools/miri/src/eval.rs @@ -422,8 +422,9 @@ pub fn eval_entry<'tcx>( let mut ecx = match create_ecx(tcx, entry_id, entry_type, &config) { Ok(v) => v, Err(err) => { - err.print_backtrace(); - panic!("Miri initialization error: {}", err.kind()) + let (kind, backtrace) = err.into_parts(); + backtrace.print_backtrace(); + panic!("Miri initialization error: {kind:?}") } }; diff --git a/src/tools/miri/src/helpers.rs b/src/tools/miri/src/helpers.rs index a2b49e6f219..befdddfa8c9 100644 --- a/src/tools/miri/src/helpers.rs +++ b/src/tools/miri/src/helpers.rs @@ -162,11 +162,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let instance = this.resolve_path(path, Namespace::ValueNS); let cid = GlobalId { instance, promoted: None }; // We don't give a span -- this isn't actually used directly by the program anyway. - let const_val = this - .eval_global(cid, None) - .unwrap_or_else(|err| panic!("failed to evaluate required Rust item: {path:?}\n{err}")); + let const_val = this.eval_global(cid, None).unwrap_or_else(|err| { + panic!("failed to evaluate required Rust item: {path:?}\n{err:?}") + }); this.read_scalar(&const_val.into()) - .unwrap_or_else(|err| panic!("failed to read required Rust item: {path:?}\n{err}")) + .unwrap_or_else(|err| panic!("failed to read required Rust item: {path:?}\n{err:?}")) } /// Helper function to get a `libc` constant as a `Scalar`. diff --git a/src/tools/miri/src/lib.rs b/src/tools/miri/src/lib.rs index 893a4dbd4c8..f711f01f323 100644 --- a/src/tools/miri/src/lib.rs +++ b/src/tools/miri/src/lib.rs @@ -43,6 +43,7 @@ extern crate rustc_apfloat; extern crate rustc_ast; +extern crate rustc_errors; #[macro_use] extern crate rustc_middle; extern crate rustc_const_eval; diff --git a/src/tools/miri/tests/compiletest.rs b/src/tools/miri/tests/compiletest.rs index fa06c4b6a12..9e6e3703153 100644 --- a/src/tools/miri/tests/compiletest.rs +++ b/src/tools/miri/tests/compiletest.rs @@ -51,18 +51,9 @@ fn test_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) -> let mut program = CommandBuilder::rustc(); program.program = miri_path(); - let in_rustc_test_suite = option_env!("RUSTC_STAGE").is_some(); - // Add some flags we always want. - if in_rustc_test_suite { - // Less aggressive warnings to make the rustc toolstate management less painful. - // (We often get warnings when e.g. a feature gets stabilized or some lint gets added/improved.) - program.args.push("-Astable-features".into()); - program.args.push("-Aunused".into()); - } else { - program.args.push("-Dwarnings".into()); - program.args.push("-Dunused".into()); - } + program.args.push("-Dwarnings".into()); + program.args.push("-Dunused".into()); if let Ok(extra_flags) = env::var("MIRIFLAGS") { for flag in extra_flags.split_whitespace() { program.args.push(flag.into()); @@ -277,7 +268,6 @@ fn run_dep_mode(target: String, mut args: impl Iterator<Item = OsString>) -> Res // the arguments to the interpreted prog cmd.arg("--"); cmd.args(args); - println!("{cmd:?}"); if cmd.spawn()?.wait()?.success() { Ok(()) } else { std::process::exit(1) } } diff --git a/src/tools/miri/tests/fail/intrinsics/copy_overlapping.rs b/src/tools/miri/tests/fail/intrinsics/copy_overlapping.rs index 3df881bd43c..9c73bdc17be 100644 --- a/src/tools/miri/tests/fail/intrinsics/copy_overlapping.rs +++ b/src/tools/miri/tests/fail/intrinsics/copy_overlapping.rs @@ -10,6 +10,6 @@ fn main() { unsafe { let a = data.as_mut_ptr(); let b = a.wrapping_offset(1) as *mut _; - copy_nonoverlapping(a, b, 2); //~ ERROR: copy_nonoverlapping called on overlapping ranges + copy_nonoverlapping(a, b, 2); //~ ERROR: `copy_nonoverlapping` called on overlapping ranges } } diff --git a/src/tools/miri/tests/fail/intrinsics/copy_overlapping.stderr b/src/tools/miri/tests/fail/intrinsics/copy_overlapping.stderr index cdb3da74ca9..13a76aae730 100644 --- a/src/tools/miri/tests/fail/intrinsics/copy_overlapping.stderr +++ b/src/tools/miri/tests/fail/intrinsics/copy_overlapping.stderr @@ -1,8 +1,8 @@ -error: Undefined Behavior: copy_nonoverlapping called on overlapping ranges +error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges --> $DIR/copy_overlapping.rs:LL:CC | LL | copy_nonoverlapping(a, b, 2); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ copy_nonoverlapping called on overlapping ranges + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges | = 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 diff --git a/src/tools/miri/tests/fail/intrinsics/ptr_offset_from_oob.stderr b/src/tools/miri/tests/fail/intrinsics/ptr_offset_from_oob.stderr index a31b929d7a7..0b9cda62b33 100644 --- a/src/tools/miri/tests/fail/intrinsics/ptr_offset_from_oob.stderr +++ b/src/tools/miri/tests/fail/intrinsics/ptr_offset_from_oob.stderr @@ -1,8 +1,8 @@ -error: Undefined Behavior: out-of-bounds offset_from: ALLOC has size 4, so pointer at offset 10 is out-of-bounds +error: Undefined Behavior: out-of-bounds `offset_from`: ALLOC has size 4, so pointer at offset 10 is out-of-bounds --> $DIR/ptr_offset_from_oob.rs:LL:CC | LL | unsafe { end_ptr.offset_from(end_ptr) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: ALLOC has size 4, so pointer at offset 10 is out-of-bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: ALLOC has size 4, so pointer at offset 10 is out-of-bounds | = 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 diff --git a/src/tools/miri/tests/fail/intrinsics/unchecked_shl.rs b/src/tools/miri/tests/fail/intrinsics/unchecked_shl.rs new file mode 100644 index 00000000000..4554d0cb82b --- /dev/null +++ b/src/tools/miri/tests/fail/intrinsics/unchecked_shl.rs @@ -0,0 +1,8 @@ +#![feature(unchecked_math)] + +fn main() { + unsafe { + let _n = 1i8.unchecked_shl(8); + //~^ ERROR: overflowing shift by 8 in `unchecked_shl` + } +} diff --git a/src/tools/miri/tests/fail/intrinsics/unchecked_shl.stderr b/src/tools/miri/tests/fail/intrinsics/unchecked_shl.stderr new file mode 100644 index 00000000000..264e9baf053 --- /dev/null +++ b/src/tools/miri/tests/fail/intrinsics/unchecked_shl.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: overflowing shift by 8 in `unchecked_shl` + --> $DIR/unchecked_shl.rs:LL:CC + | +LL | let _n = 1i8.unchecked_shl(8); + | ^^^^^^^^^^^^^^^^^^^^ overflowing shift by 8 in `unchecked_shl` + | + = 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 `main` at $DIR/unchecked_shl.rs:LL:CC + +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/intrinsics/overflowing-unchecked-rsh.rs b/src/tools/miri/tests/fail/intrinsics/unchecked_shr.rs index fe2e85be698..fe2e85be698 100644 --- a/src/tools/miri/tests/fail/intrinsics/overflowing-unchecked-rsh.rs +++ b/src/tools/miri/tests/fail/intrinsics/unchecked_shr.rs diff --git a/src/tools/miri/tests/fail/intrinsics/overflowing-unchecked-rsh.stderr b/src/tools/miri/tests/fail/intrinsics/unchecked_shr.stderr index 9c5d0d13108..378ff9734c1 100644 --- a/src/tools/miri/tests/fail/intrinsics/overflowing-unchecked-rsh.stderr +++ b/src/tools/miri/tests/fail/intrinsics/unchecked_shr.stderr @@ -1,5 +1,5 @@ error: Undefined Behavior: overflowing shift by 64 in `unchecked_shr` - --> $DIR/overflowing-unchecked-rsh.rs:LL:CC + --> $DIR/unchecked_shr.rs:LL:CC | LL | let _n = 1i64.unchecked_shr(64); | ^^^^^^^^^^^^^^^^^^^^^^ overflowing shift by 64 in `unchecked_shr` @@ -7,7 +7,7 @@ LL | let _n = 1i64.unchecked_shr(64); = 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 `main` at $DIR/overflowing-unchecked-rsh.rs:LL:CC + = note: inside `main` at $DIR/unchecked_shr.rs:LL:CC note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace diff --git a/src/tools/miri/tests/fail/tree-borrows/alternate-read-write.stderr b/src/tools/miri/tests/fail/tree-borrows/alternate-read-write.stderr index bb601fc8835..d82cb8288a3 100644 --- a/src/tools/miri/tests/fail/tree-borrows/alternate-read-write.stderr +++ b/src/tools/miri/tests/fail/tree-borrows/alternate-read-write.stderr @@ -17,13 +17,13 @@ help: the conflicting tag <TAG> was created here, in the initial state Reserved | LL | let y = unsafe { &mut *(x as *mut u8) }; | ^^^^^^^^^^^^^^^^^^^^ -help: the conflicting tag <TAG> then transitioned from Reserved to Active due to a child write access at offsets [0x0..0x1] +help: the conflicting tag <TAG> later transitioned to Active due to a child write access at offsets [0x0..0x1] --> $DIR/alternate-read-write.rs:LL:CC | LL | *y += 1; // Success | ^^^^^^^ - = help: this corresponds to an activation -help: the conflicting tag <TAG> then transitioned from Active to Frozen due to a foreign read access at offsets [0x0..0x1] + = help: this corresponds to the first write to a 2-phase borrowed mutable reference +help: the conflicting tag <TAG> later transitioned to Frozen due to a foreign read access at offsets [0x0..0x1] --> $DIR/alternate-read-write.rs:LL:CC | LL | let _val = *x; diff --git a/src/tools/miri/tests/fail/tree-borrows/error-range.stderr b/src/tools/miri/tests/fail/tree-borrows/error-range.stderr index bc829fd86d3..10c2e95ca2f 100644 --- a/src/tools/miri/tests/fail/tree-borrows/error-range.stderr +++ b/src/tools/miri/tests/fail/tree-borrows/error-range.stderr @@ -17,19 +17,7 @@ help: the conflicting tag <TAG> was created here, in the initial state Reserved | LL | let rmut = &mut *addr_of_mut!(data[0..6]); | ^^^^ -help: the conflicting tag <TAG> then transitioned from Reserved to Active due to a child write access at offsets [0x5..0x6] - --> $DIR/error-range.rs:LL:CC - | -LL | rmut[5] += 1; - | ^^^^^^^^^^^^ - = help: this corresponds to an activation -help: the conflicting tag <TAG> then transitioned from Active to Frozen due to a foreign read access at offsets [0x5..0x6] - --> $DIR/error-range.rs:LL:CC - | -LL | let _v = data[5]; - | ^^^^^^^ - = help: this corresponds to a loss of write permissions -help: the conflicting tag <TAG> then transitioned from Frozen to Disabled due to a foreign write access at offsets [0x5..0x6] +help: the conflicting tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x5..0x6] --> $DIR/error-range.rs:LL:CC | LL | data[5] = 1; diff --git a/src/tools/miri/tests/fail/tree-borrows/fragile-data-race.stderr b/src/tools/miri/tests/fail/tree-borrows/fragile-data-race.stderr index 79744964a8b..86fdf97ac41 100644 --- a/src/tools/miri/tests/fail/tree-borrows/fragile-data-race.stderr +++ b/src/tools/miri/tests/fail/tree-borrows/fragile-data-race.stderr @@ -17,7 +17,7 @@ help: the conflicting tag <TAG> was created here, in the initial state Reserved | LL | pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> { | ^ -help: the conflicting tag <TAG> then transitioned from Reserved to Frozen due to a foreign read access at offsets [0x0..0x1] +help: the conflicting tag <TAG> later transitioned to Frozen due to a foreign read access at offsets [0x0..0x1] --> RUSTLIB/core/src/ptr/mod.rs:LL:CC | LL | crate::intrinsics::read_via_copy(src) diff --git a/src/tools/miri/tests/fail/tree-borrows/read-to-local.stderr b/src/tools/miri/tests/fail/tree-borrows/read-to-local.stderr index 8c9c2f8f965..6deb4b43f6a 100644 --- a/src/tools/miri/tests/fail/tree-borrows/read-to-local.stderr +++ b/src/tools/miri/tests/fail/tree-borrows/read-to-local.stderr @@ -11,13 +11,13 @@ help: the accessed tag <TAG> was created here, in the initial state Reserved | LL | let mref = &mut root; | ^^^^^^^^^ -help: the accessed tag <TAG> then transitioned from Reserved to Active due to a child write access at offsets [0x0..0x1] +help: the accessed tag <TAG> later transitioned to Active due to a child write access at offsets [0x0..0x1] --> $DIR/read-to-local.rs:LL:CC | LL | *ptr = 0; // Write | ^^^^^^^^ - = help: this corresponds to an activation -help: the accessed tag <TAG> then transitioned from Active to Frozen due to a foreign read access at offsets [0x0..0x1] + = help: this corresponds to the first write to a 2-phase borrowed mutable reference +help: the accessed tag <TAG> later transitioned to Frozen due to a foreign read access at offsets [0x0..0x1] --> $DIR/read-to-local.rs:LL:CC | LL | assert_eq!(root, 0); // Parent Read diff --git a/src/tools/miri/tests/fail/tree-borrows/strongly-protected.stderr b/src/tools/miri/tests/fail/tree-borrows/strongly-protected.stderr index 071b216ff98..97088d5854c 100644 --- a/src/tools/miri/tests/fail/tree-borrows/strongly-protected.stderr +++ b/src/tools/miri/tests/fail/tree-borrows/strongly-protected.stderr @@ -17,12 +17,6 @@ help: the strongly protected tag <TAG> was created here, in the initial state Re | LL | fn inner(x: &mut i32, f: fn(&mut i32)) { | ^ -help: the strongly protected tag <TAG> then transitioned from Reserved to Active due to a child write access at offsets [0x0..0x4] - --> $DIR/strongly-protected.rs:LL:CC - | -LL | drop(unsafe { Box::from_raw(raw) }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = help: this corresponds to an activation = note: BACKTRACE (of the first span): = note: inside `std::alloc::dealloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC = note: inside `<std::alloc::Global as std::alloc::Allocator>::deallocate` at RUSTLIB/alloc/src/alloc.rs:LL:CC diff --git a/src/tools/miri/tests/fail/tree-borrows/write-during-2phase.stderr b/src/tools/miri/tests/fail/tree-borrows/write-during-2phase.stderr index f6285bdcf16..898f6108ccb 100644 --- a/src/tools/miri/tests/fail/tree-borrows/write-during-2phase.stderr +++ b/src/tools/miri/tests/fail/tree-borrows/write-during-2phase.stderr @@ -18,7 +18,7 @@ LL | | *inner = 42; LL | | n LL | | }); | |______^ -help: the accessed tag <TAG> then transitioned from Reserved to Disabled due to a foreign write access at offsets [0x0..0x8] +help: the accessed tag <TAG> later transitioned to Disabled due to a foreign write access at offsets [0x0..0x8] --> $DIR/write-during-2phase.rs:LL:CC | LL | *inner = 42; diff --git a/src/tools/miri/tests/pass/shims/fs.rs b/src/tools/miri/tests/pass/shims/fs.rs index 3258a2be460..af245aa89aa 100644 --- a/src/tools/miri/tests/pass/shims/fs.rs +++ b/src/tools/miri/tests/pass/shims/fs.rs @@ -31,7 +31,7 @@ fn main() { } fn host_to_target_path(path: String) -> PathBuf { - use std::ffi::{c_char, CStr, CString}; + use std::ffi::{CStr, CString}; let path = CString::new(path).unwrap(); let mut out = Vec::with_capacity(1024); diff --git a/src/tools/rust-installer/Cargo.toml b/src/tools/rust-installer/Cargo.toml index 97734f048ab..85e979f07bf 100644 --- a/src/tools/rust-installer/Cargo.toml +++ b/src/tools/rust-installer/Cargo.toml @@ -17,7 +17,6 @@ tar = "0.4.38" walkdir = "2" xz2 = "0.1.4" num_cpus = "1" -remove_dir_all = "0.5" [dependencies.clap] features = ["derive"] diff --git a/src/tools/rust-installer/src/util.rs b/src/tools/rust-installer/src/util.rs index 674617c657c..6cac314b68d 100644 --- a/src/tools/rust-installer/src/util.rs +++ b/src/tools/rust-installer/src/util.rs @@ -82,7 +82,7 @@ pub fn open_file<P: AsRef<Path>>(path: P) -> Result<fs::File> { /// Wraps `remove_dir_all` with a nicer error message. pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<()> { - remove_dir_all::remove_dir_all(path.as_ref()) + fs::remove_dir_all(path.as_ref()) .with_context(|| format!("failed to remove dir '{}'", path.as_ref().display()))?; Ok(()) } diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index db2b7910b71..9979dfbd56a 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -132,7 +132,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "crossbeam-epoch", "crossbeam-utils", "crypto-common", - "cstr", "datafrog", "derive_more", "digest", @@ -168,12 +167,14 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "instant", "intl-memoizer", "intl_pluralrules", + "io-lifetimes", "itertools", "itoa", "jobserver", "lazy_static", "libc", "libloading", + "linux-raw-sys", "litemap", "lock_api", "log", @@ -211,12 +212,12 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "regex", "regex-automata", "regex-syntax", - "remove_dir_all", "rustc-demangle", "rustc-hash", "rustc-rayon", "rustc-rayon-core", "rustc_version", + "rustix", "ruzstd", // via object in thorin-dwp "ryu", "scoped-tls", @@ -280,6 +281,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "winapi-util", "winapi-x86_64-pc-windows-gnu", "windows", + "windows-sys", "windows-targets", "windows_aarch64_gnullvm", "windows_aarch64_msvc", diff --git a/src/tools/x/Cargo.lock b/src/tools/x/Cargo.lock index 723d6cb25ed..09e5c750749 100644 --- a/src/tools/x/Cargo.lock +++ b/src/tools/x/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "x" -version = "0.1.0" +version = "0.1.1" diff --git a/tests/codegen/box-maybe-uninit-llvm14.rs b/tests/codegen/box-maybe-uninit-llvm14.rs index b0c88f76c43..c9f88fb3fe4 100644 --- a/tests/codegen/box-maybe-uninit-llvm14.rs +++ b/tests/codegen/box-maybe-uninit-llvm14.rs @@ -31,4 +31,4 @@ pub fn box_uninitialized2() -> Box<MaybeUninit<[usize; 1024 * 1024]>> { // Hide the LLVM 15+ `allocalign` attribute in the declaration of __rust_alloc // from the CHECK-NOT above. We don't check the attributes here because we can't rely // on all of them being set until LLVM 15. -// CHECK: declare noalias{{.*}} @__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+.*}} noundef) +// CHECK: declare {{(dso_local )?}}noalias{{.*}} @__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+.*}} noundef) diff --git a/tests/codegen/box-maybe-uninit.rs b/tests/codegen/box-maybe-uninit.rs index 2f88966996a..5c08b5832ad 100644 --- a/tests/codegen/box-maybe-uninit.rs +++ b/tests/codegen/box-maybe-uninit.rs @@ -28,6 +28,6 @@ pub fn box_uninitialized2() -> Box<MaybeUninit<[usize; 1024 * 1024]>> { // Hide the `allocalign` attribute in the declaration of __rust_alloc // from the CHECK-NOT above, and also verify the attributes got set reasonably. -// CHECK: declare noalias noundef ptr @__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+}} allocalign noundef) unnamed_addr [[RUST_ALLOC_ATTRS:#[0-9]+]] +// CHECK: declare {{(dso_local )?}}noalias noundef ptr @__rust_alloc(i{{[0-9]+}} noundef, i{{[0-9]+}} allocalign noundef) unnamed_addr [[RUST_ALLOC_ATTRS:#[0-9]+]] -// CHECK-DAG: attributes [[RUST_ALLOC_ATTRS]] = { {{.*}} allockind("alloc,uninitialized,aligned") allocsize(0) uwtable "alloc-family"="__rust_alloc" {{.*}} } +// CHECK-DAG: attributes [[RUST_ALLOC_ATTRS]] = { {{.*}} allockind("alloc,uninitialized,aligned") allocsize(0) {{(uwtable )?}}"alloc-family"="__rust_alloc" {{.*}} } diff --git a/tests/codegen/call-metadata.rs b/tests/codegen/call-metadata.rs index 1c30c08d3b2..07cc0c96371 100644 --- a/tests/codegen/call-metadata.rs +++ b/tests/codegen/call-metadata.rs @@ -6,7 +6,7 @@ #![crate_type = "lib"] pub fn test() { - // CHECK: call noundef i8 @some_true(), !range [[R0:![0-9]+]] + // CHECK: call noundef i8 @some_true(){{( #[0-9]+)?}}, !range [[R0:![0-9]+]] // CHECK: [[R0]] = !{i8 0, i8 3} some_true(); } diff --git a/tests/codegen/debug-column.rs b/tests/codegen/debug-column.rs index e61642b8e1b..f3b19a2eb2f 100644 --- a/tests/codegen/debug-column.rs +++ b/tests/codegen/debug-column.rs @@ -6,11 +6,11 @@ fn main() { unsafe { // Column numbers are 1-based. Regression test for #65437. - // CHECK: call void @giraffe(), !dbg [[A:!.*]] + // CHECK: call void @giraffe(){{( #[0-9]+)?}}, !dbg [[A:!.*]] giraffe(); // Column numbers use byte offests. Regression test for #67360 - // CHECK: call void @turtle(), !dbg [[B:!.*]] + // CHECK: call void @turtle(){{( #[0-9]+)?}}, !dbg [[B:!.*]] /* ż */ turtle(); // CHECK: [[A]] = !DILocation(line: 10, column: 9, diff --git a/tests/codegen/drop.rs b/tests/codegen/drop.rs index 99402827158..3615ef47b53 100644 --- a/tests/codegen/drop.rs +++ b/tests/codegen/drop.rs @@ -1,4 +1,5 @@ // ignore-wasm32-bare compiled with panic=abort by default +// needs-unwind - this test verifies the amount of drop calls when unwinding is used // compile-flags: -C no-prepopulate-passes #![crate_type = "lib"] diff --git a/tests/codegen/external-no-mangle-statics.rs b/tests/codegen/external-no-mangle-statics.rs index c6ecb7aa96a..48023a2a901 100644 --- a/tests/codegen/external-no-mangle-statics.rs +++ b/tests/codegen/external-no-mangle-statics.rs @@ -6,72 +6,72 @@ // `#[no_mangle]`d static variables always have external linkage, i.e., no `internal` in their // definitions -// CHECK: @A = local_unnamed_addr constant +// CHECK: @A = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] static A: u8 = 0; -// CHECK: @B = local_unnamed_addr global +// CHECK: @B = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] static mut B: u8 = 0; -// CHECK: @C = local_unnamed_addr constant +// CHECK: @C = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] pub static C: u8 = 0; -// CHECK: @D = local_unnamed_addr global +// CHECK: @D = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] pub static mut D: u8 = 0; mod private { - // CHECK: @E = local_unnamed_addr constant + // CHECK: @E = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] static E: u8 = 0; - // CHECK: @F = local_unnamed_addr global + // CHECK: @F = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] static mut F: u8 = 0; - // CHECK: @G = local_unnamed_addr constant + // CHECK: @G = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] pub static G: u8 = 0; - // CHECK: @H = local_unnamed_addr global + // CHECK: @H = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] pub static mut H: u8 = 0; } const HIDDEN: () = { - // CHECK: @I = local_unnamed_addr constant + // CHECK: @I = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] static I: u8 = 0; - // CHECK: @J = local_unnamed_addr global + // CHECK: @J = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] static mut J: u8 = 0; - // CHECK: @K = local_unnamed_addr constant + // CHECK: @K = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] pub static K: u8 = 0; - // CHECK: @L = local_unnamed_addr global + // CHECK: @L = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] pub static mut L: u8 = 0; }; fn x() { - // CHECK: @M = local_unnamed_addr constant + // CHECK: @M = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] static M: fn() = x; - // CHECK: @N = local_unnamed_addr global + // CHECK: @N = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] static mut N: u8 = 0; - // CHECK: @O = local_unnamed_addr constant + // CHECK: @O = {{(dso_local )?}}local_unnamed_addr constant #[no_mangle] pub static O: u8 = 0; - // CHECK: @P = local_unnamed_addr global + // CHECK: @P = {{(dso_local )?}}local_unnamed_addr global #[no_mangle] pub static mut P: u8 = 0; } diff --git a/tests/codegen/issues/issue-105386-ub-in-debuginfo.rs b/tests/codegen/issues/issue-105386-ub-in-debuginfo.rs index 2ee4d7cca0e..6e0eacfe400 100644 --- a/tests/codegen/issues/issue-105386-ub-in-debuginfo.rs +++ b/tests/codegen/issues/issue-105386-ub-in-debuginfo.rs @@ -1,4 +1,5 @@ -// compile-flags: --crate-type=lib -O -Cdebuginfo=2 -Cno-prepopulate-passes +// compile-flags: --crate-type=lib -O -Cdebuginfo=2 -Cno-prepopulate-passes -Zmir-enable-passes=-ScalarReplacementOfAggregates +// MIR SROA will decompose the closure // min-llvm-version: 15.0 # this test uses opaque pointer notation #![feature(stmt_expr_attributes)] @@ -15,8 +16,8 @@ pub fn outer_function(x: S, y: S) -> usize { // Check that we do not attempt to load from the spilled arg before it is assigned to // when generating debuginfo. // CHECK-LABEL: @outer_function -// CHECK: [[spill:%.*]] = alloca %"[closure@{{.*.rs}}:9:23: 9:25]" -// CHECK-NOT: [[ptr_tmp:%.*]] = getelementptr inbounds %"[closure@{{.*.rs}}:9:23: 9:25]", ptr [[spill]] +// CHECK: [[spill:%.*]] = alloca %"[closure@{{.*.rs}}:10:23: 10:25]" +// CHECK-NOT: [[ptr_tmp:%.*]] = getelementptr inbounds %"[closure@{{.*.rs}}:10:23: 10:25]", ptr [[spill]] // CHECK-NOT: [[load:%.*]] = load ptr, ptr // CHECK: call void @llvm.lifetime.start{{.*}}({{.*}}, ptr [[spill]]) // CHECK: [[inner:%.*]] = getelementptr inbounds %"{{.*}}", ptr [[spill]] diff --git a/tests/codegen/issues/issue-86106.rs b/tests/codegen/issues/issue-86106.rs index c0be7fab2f3..be5034dcfbd 100644 --- a/tests/codegen/issues/issue-86106.rs +++ b/tests/codegen/issues/issue-86106.rs @@ -7,7 +7,7 @@ #![crate_type = "lib"] -// CHECK-LABEL: define void @string_new +// CHECK-LABEL: define {{(dso_local )?}}void @string_new #[no_mangle] pub fn string_new() -> String { // CHECK: store ptr inttoptr @@ -17,7 +17,7 @@ pub fn string_new() -> String { String::new() } -// CHECK-LABEL: define void @empty_to_string +// CHECK-LABEL: define {{(dso_local )?}}void @empty_to_string #[no_mangle] pub fn empty_to_string() -> String { // CHECK: store ptr inttoptr diff --git a/tests/codegen/link_section.rs b/tests/codegen/link_section.rs index 88b8692b0ac..2b26b604ad3 100644 --- a/tests/codegen/link_section.rs +++ b/tests/codegen/link_section.rs @@ -3,7 +3,7 @@ #![crate_type = "lib"] -// CHECK: @VAR1 = constant <{ [4 x i8] }> <{ [4 x i8] c"\01\00\00\00" }>, section ".test_one" +// CHECK: @VAR1 = {{(dso_local )?}}constant <{ [4 x i8] }> <{ [4 x i8] c"\01\00\00\00" }>, section ".test_one" #[no_mangle] #[link_section = ".test_one"] #[cfg(target_endian = "little")] @@ -19,17 +19,17 @@ pub enum E { B(f32) } -// CHECK: @VAR2 = constant {{.*}}, section ".test_two" +// CHECK: @VAR2 = {{(dso_local )?}}constant {{.*}}, section ".test_two" #[no_mangle] #[link_section = ".test_two"] pub static VAR2: E = E::A(666); -// CHECK: @VAR3 = constant {{.*}}, section ".test_three" +// CHECK: @VAR3 = {{(dso_local )?}}constant {{.*}}, section ".test_three" #[no_mangle] #[link_section = ".test_three"] pub static VAR3: E = E::B(1.); -// CHECK: define void @fn1() {{.*}} section ".test_four" { +// CHECK: define {{(dso_local )?}}void @fn1() {{.*}} section ".test_four" { #[no_mangle] #[link_section = ".test_four"] pub fn fn1() {} diff --git a/tests/codegen/mir-inlined-line-numbers.rs b/tests/codegen/mir-inlined-line-numbers.rs index 19d83f0eee7..d13527b9521 100644 --- a/tests/codegen/mir-inlined-line-numbers.rs +++ b/tests/codegen/mir-inlined-line-numbers.rs @@ -19,7 +19,7 @@ pub fn example() { } // CHECK-LABEL: @example -// CHECK: tail call void @bar(), !dbg [[DBG_ID:![0-9]+]] +// CHECK: tail call void @bar(){{( #[0-9]+)?}}, !dbg [[DBG_ID:![0-9]+]] // CHECK: [[DBG_ID]] = !DILocation(line: 7, // CHECK-SAME: inlinedAt: [[INLINE_ID:![0-9]+]]) // CHECK: [[INLINE_ID]] = !DILocation(line: 18, diff --git a/tests/codegen/naked-noinline.rs b/tests/codegen/naked-noinline.rs index c0ac69f4ed7..5cfb500c0ef 100644 --- a/tests/codegen/naked-noinline.rs +++ b/tests/codegen/naked-noinline.rs @@ -12,7 +12,7 @@ use std::arch::asm; pub unsafe extern "C" fn f() { // Check that f has naked and noinline attributes. // - // CHECK: define void @f() unnamed_addr [[ATTR:#[0-9]+]] + // CHECK: define {{(dso_local )?}}void @f() unnamed_addr [[ATTR:#[0-9]+]] // CHECK-NEXT: start: // CHECK-NEXT: call void asm asm!("", options(noreturn)); @@ -22,7 +22,7 @@ pub unsafe extern "C" fn f() { pub unsafe fn g() { // Check that call to f is not inlined. // - // CHECK-LABEL: define void @g() + // CHECK-LABEL: define {{(dso_local )?}}void @g() // CHECK-NEXT: start: // CHECK-NEXT: call void @f() f(); diff --git a/tests/codegen/personality_lifetimes.rs b/tests/codegen/personality_lifetimes.rs index 2104022f578..9ff7a9b3e88 100644 --- a/tests/codegen/personality_lifetimes.rs +++ b/tests/codegen/personality_lifetimes.rs @@ -1,5 +1,6 @@ // ignore-msvc // ignore-wasm32-bare compiled with panic=abort by default +// needs-unwind // compile-flags: -O -C no-prepopulate-passes diff --git a/tests/codegen/ptr-read-metadata.rs b/tests/codegen/ptr-read-metadata.rs index e1e3272662c..73d1db6df27 100644 --- a/tests/codegen/ptr-read-metadata.rs +++ b/tests/codegen/ptr-read-metadata.rs @@ -9,7 +9,7 @@ use std::mem::MaybeUninit; -// CHECK-LABEL: define noundef i8 @copy_byte( +// CHECK-LABEL: define {{(dso_local )?}}noundef i8 @copy_byte( #[no_mangle] pub unsafe fn copy_byte(p: *const u8) -> u8 { // CHECK-NOT: load @@ -19,7 +19,7 @@ pub unsafe fn copy_byte(p: *const u8) -> u8 { *p } -// CHECK-LABEL: define noundef i8 @read_byte( +// CHECK-LABEL: define {{(dso_local )?}}noundef i8 @read_byte( #[no_mangle] pub unsafe fn read_byte(p: *const u8) -> u8 { // CHECK-NOT: load @@ -29,7 +29,7 @@ pub unsafe fn read_byte(p: *const u8) -> u8 { p.read() } -// CHECK-LABEL: define i8 @read_byte_maybe_uninit( +// CHECK-LABEL: define {{(dso_local )?}}i8 @read_byte_maybe_uninit( #[no_mangle] pub unsafe fn read_byte_maybe_uninit(p: *const MaybeUninit<u8>) -> MaybeUninit<u8> { // CHECK-NOT: load @@ -39,7 +39,7 @@ pub unsafe fn read_byte_maybe_uninit(p: *const MaybeUninit<u8>) -> MaybeUninit<u p.read() } -// CHECK-LABEL: define noundef i8 @read_byte_assume_init( +// CHECK-LABEL: define {{(dso_local )?}}noundef i8 @read_byte_assume_init( #[no_mangle] pub unsafe fn read_byte_assume_init(p: &MaybeUninit<u8>) -> u8 { // CHECK-NOT: load @@ -49,7 +49,7 @@ pub unsafe fn read_byte_assume_init(p: &MaybeUninit<u8>) -> u8 { p.assume_init_read() } -// CHECK-LABEL: define noundef i32 @copy_char( +// CHECK-LABEL: define {{(dso_local )?}}noundef i32 @copy_char( #[no_mangle] pub unsafe fn copy_char(p: *const char) -> char { // CHECK-NOT: load @@ -60,7 +60,7 @@ pub unsafe fn copy_char(p: *const char) -> char { *p } -// CHECK-LABEL: define noundef i32 @read_char( +// CHECK-LABEL: define {{(dso_local )?}}noundef i32 @read_char( #[no_mangle] pub unsafe fn read_char(p: *const char) -> char { // CHECK-NOT: load @@ -71,7 +71,7 @@ pub unsafe fn read_char(p: *const char) -> char { p.read() } -// CHECK-LABEL: define i32 @read_char_maybe_uninit( +// CHECK-LABEL: define {{(dso_local )?}}i32 @read_char_maybe_uninit( #[no_mangle] pub unsafe fn read_char_maybe_uninit(p: *const MaybeUninit<char>) -> MaybeUninit<char> { // CHECK-NOT: load @@ -82,7 +82,7 @@ pub unsafe fn read_char_maybe_uninit(p: *const MaybeUninit<char>) -> MaybeUninit p.read() } -// CHECK-LABEL: define noundef i32 @read_char_assume_init( +// CHECK-LABEL: define {{(dso_local )?}}noundef i32 @read_char_assume_init( #[no_mangle] pub unsafe fn read_char_assume_init(p: &MaybeUninit<char>) -> char { // CHECK-NOT: load diff --git a/tests/codegen/sanitizer-cfi-emit-type-metadata-id-itanium-cxx-abi.rs b/tests/codegen/sanitizer-cfi-emit-type-metadata-id-itanium-cxx-abi.rs index ebb26cd35c0..472d921ace0 100644 --- a/tests/codegen/sanitizer-cfi-emit-type-metadata-id-itanium-cxx-abi.rs +++ b/tests/codegen/sanitizer-cfi-emit-type-metadata-id-itanium-cxx-abi.rs @@ -587,6 +587,6 @@ pub fn foo149(_: Type14<Bar>, _: Type14<Bar>, _: Type14<Bar>) { } // CHECK: ![[TYPE144]] = !{i64 0, !"_ZTSFvu3refIvEE"} // CHECK: ![[TYPE145]] = !{i64 0, !"_ZTSFvu3refIvES_E"} // CHECK: ![[TYPE146]] = !{i64 0, !"_ZTSFvu3refIvES_S_E"} -// CHECK: ![[TYPE147]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3BarE -// CHECK: ![[TYPE148]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3BarS_E -// CHECK: ![[TYPE149]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3BarS_S_E +// CHECK: ![[TYPE147]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3BarE"} +// CHECK: ![[TYPE148]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3BarS_E"} +// CHECK: ![[TYPE149]] = !{i64 0, !"_ZTSFvu{{[0-9]+}}NtC{{[[:print:]]+}}_51sanitizer_cfi_emit_type_metadata_id_itanium_cxx_abi3BarS_S_E"} diff --git a/tests/codegen/tuple-layout-opt.rs b/tests/codegen/tuple-layout-opt.rs index 35f76085145..309fe1d5ec9 100644 --- a/tests/codegen/tuple-layout-opt.rs +++ b/tests/codegen/tuple-layout-opt.rs @@ -6,31 +6,31 @@ #![crate_type="lib"] type ScalarZstLast = (u128, ()); -// CHECK: define i128 @test_ScalarZstLast(i128 %_1) +// CHECK: define {{(dso_local )?}}i128 @test_ScalarZstLast(i128 %_1) #[no_mangle] pub fn test_ScalarZstLast(_: ScalarZstLast) -> ScalarZstLast { loop {} } type ScalarZstFirst = ((), u128); -// CHECK: define i128 @test_ScalarZstFirst(i128 %_1) +// CHECK: define {{(dso_local )?}}i128 @test_ScalarZstFirst(i128 %_1) #[no_mangle] pub fn test_ScalarZstFirst(_: ScalarZstFirst) -> ScalarZstFirst { loop {} } type ScalarPairZstLast = (u8, u128, ()); -// CHECK: define { i128, i8 } @test_ScalarPairZstLast(i128 %_1.0, i8 %_1.1) +// CHECK: define {{(dso_local )?}}{ i128, i8 } @test_ScalarPairZstLast(i128 %_1.0, i8 %_1.1) #[no_mangle] pub fn test_ScalarPairZstLast(_: ScalarPairZstLast) -> ScalarPairZstLast { loop {} } type ScalarPairZstFirst = ((), u8, u128); -// CHECK: define { i8, i128 } @test_ScalarPairZstFirst(i8 %_1.0, i128 %_1.1) +// CHECK: define {{(dso_local )?}}{ i8, i128 } @test_ScalarPairZstFirst(i8 %_1.0, i128 %_1.1) #[no_mangle] pub fn test_ScalarPairZstFirst(_: ScalarPairZstFirst) -> ScalarPairZstFirst { loop {} } type ScalarPairLotsOfZsts = ((), u8, (), u128, ()); -// CHECK: define { i128, i8 } @test_ScalarPairLotsOfZsts(i128 %_1.0, i8 %_1.1) +// CHECK: define {{(dso_local )?}}{ i128, i8 } @test_ScalarPairLotsOfZsts(i128 %_1.0, i8 %_1.1) #[no_mangle] pub fn test_ScalarPairLotsOfZsts(_: ScalarPairLotsOfZsts) -> ScalarPairLotsOfZsts { loop {} } type ScalarPairLottaNesting = (((), ((), u8, (), u128, ())), ()); -// CHECK: define { i128, i8 } @test_ScalarPairLottaNesting(i128 %_1.0, i8 %_1.1) +// CHECK: define {{(dso_local )?}}{ i128, i8 } @test_ScalarPairLottaNesting(i128 %_1.0, i8 %_1.1) #[no_mangle] pub fn test_ScalarPairLottaNesting(_: ScalarPairLottaNesting) -> ScalarPairLottaNesting { loop {} } diff --git a/tests/codegen/union-abi.rs b/tests/codegen/union-abi.rs index c18f2a49fc3..8481ca8ccfa 100644 --- a/tests/codegen/union-abi.rs +++ b/tests/codegen/union-abi.rs @@ -17,60 +17,60 @@ pub struct i64x4(i64, i64, i64, i64); #[derive(Copy, Clone)] pub union UnionI64x4{ a:(), b: i64x4 } -// CHECK: define void @test_UnionI64x4({{<4 x i64>\*|ptr}} {{.*}} %_1) +// CHECK: define {{(dso_local )?}}void @test_UnionI64x4({{<4 x i64>\*|ptr}} {{.*}} %_1) #[no_mangle] pub fn test_UnionI64x4(_: UnionI64x4) { loop {} } pub union UnionI64x4_{ a: i64x4, b: (), c:i64x4, d: Unhab, e: ((),()), f: UnionI64x4 } -// CHECK: define void @test_UnionI64x4_({{<4 x i64>\*|ptr}} {{.*}} %_1) +// CHECK: define {{(dso_local )?}}void @test_UnionI64x4_({{<4 x i64>\*|ptr}} {{.*}} %_1) #[no_mangle] pub fn test_UnionI64x4_(_: UnionI64x4_) { loop {} } pub union UnionI64x4I64{ a: i64x4, b: i64 } -// CHECK: define void @test_UnionI64x4I64({{%UnionI64x4I64\*|ptr}} {{.*}} %_1) +// CHECK: define {{(dso_local )?}}void @test_UnionI64x4I64({{%UnionI64x4I64\*|ptr}} {{.*}} %_1) #[no_mangle] pub fn test_UnionI64x4I64(_: UnionI64x4I64) { loop {} } pub union UnionI64x4Tuple{ a: i64x4, b: (i64, i64, i64, i64) } -// CHECK: define void @test_UnionI64x4Tuple({{%UnionI64x4Tuple\*|ptr}} {{.*}} %_1) +// CHECK: define {{(dso_local )?}}void @test_UnionI64x4Tuple({{%UnionI64x4Tuple\*|ptr}} {{.*}} %_1) #[no_mangle] pub fn test_UnionI64x4Tuple(_: UnionI64x4Tuple) { loop {} } pub union UnionF32{a:f32} -// CHECK: define float @test_UnionF32(float %_1) +// CHECK: define {{(dso_local )?}}float @test_UnionF32(float %_1) #[no_mangle] pub fn test_UnionF32(_: UnionF32) -> UnionF32 { loop {} } pub union UnionF32F32{a:f32, b:f32} -// CHECK: define float @test_UnionF32F32(float %_1) +// CHECK: define {{(dso_local )?}}float @test_UnionF32F32(float %_1) #[no_mangle] pub fn test_UnionF32F32(_: UnionF32F32) -> UnionF32F32 { loop {} } pub union UnionF32U32{a:f32, b:u32} -// CHECK: define i32 @test_UnionF32U32(i32{{( %0)?}}) +// CHECK: define {{(dso_local )?}}i32 @test_UnionF32U32(i32{{( %0)?}}) #[no_mangle] pub fn test_UnionF32U32(_: UnionF32U32) -> UnionF32U32 { loop {} } pub union UnionU128{a:u128} -// CHECK: define i128 @test_UnionU128(i128 %_1) +// CHECK: define {{(dso_local )?}}i128 @test_UnionU128(i128 %_1) #[no_mangle] pub fn test_UnionU128(_: UnionU128) -> UnionU128 { loop {} } #[repr(C)] pub union CUnionU128{a:u128} -// CHECK: define void @test_CUnionU128({{%CUnionU128\*|ptr}} {{.*}} %_1) +// CHECK: define {{(dso_local )?}}void @test_CUnionU128({{%CUnionU128\*|ptr}} {{.*}} %_1) #[no_mangle] pub fn test_CUnionU128(_: CUnionU128) { loop {} } pub union UnionBool { b:bool } -// CHECK: define noundef zeroext i1 @test_UnionBool(i8 %b) +// CHECK: define {{(dso_local )?}}noundef zeroext i1 @test_UnionBool(i8 %b) #[no_mangle] pub fn test_UnionBool(b: UnionBool) -> bool { unsafe { b.b } } // CHECK: %0 = trunc i8 %b to i1 diff --git a/tests/codegen/unwind-abis/c-unwind-abi.rs b/tests/codegen/unwind-abis/c-unwind-abi.rs index e258dbcacd2..fa5b6bad75c 100644 --- a/tests/codegen/unwind-abis/c-unwind-abi.rs +++ b/tests/codegen/unwind-abis/c-unwind-abi.rs @@ -1,4 +1,5 @@ // compile-flags: -C opt-level=0 +// needs-unwind // Test that `nounwind` attributes are correctly applied to exported `C` and `C-unwind` extern // functions. `C-unwind` functions MUST NOT have this attribute. We disable optimizations above diff --git a/tests/codegen/unwind-abis/cdecl-unwind-abi.rs b/tests/codegen/unwind-abis/cdecl-unwind-abi.rs index 19a7228839a..64746d32175 100644 --- a/tests/codegen/unwind-abis/cdecl-unwind-abi.rs +++ b/tests/codegen/unwind-abis/cdecl-unwind-abi.rs @@ -1,4 +1,5 @@ // compile-flags: -C opt-level=0 +// needs-unwind // Test that `nounwind` attributes are correctly applied to exported `cdecl` and // `cdecl-unwind` extern functions. `cdecl-unwind` functions MUST NOT have this attribute. We diff --git a/tests/codegen/unwind-abis/nounwind-on-stable-panic-unwind.rs b/tests/codegen/unwind-abis/nounwind-on-stable-panic-unwind.rs index c1c5bbdda34..dc3911cd4eb 100644 --- a/tests/codegen/unwind-abis/nounwind-on-stable-panic-unwind.rs +++ b/tests/codegen/unwind-abis/nounwind-on-stable-panic-unwind.rs @@ -1,5 +1,6 @@ // compile-flags: -C opt-level=0 // ignore-wasm32-bare compiled with panic=abort by default +// needs-unwind #![crate_type = "lib"] diff --git a/tests/codegen/unwind-abis/system-unwind-abi.rs b/tests/codegen/unwind-abis/system-unwind-abi.rs index 2591c1d4814..f274a33b099 100644 --- a/tests/codegen/unwind-abis/system-unwind-abi.rs +++ b/tests/codegen/unwind-abis/system-unwind-abi.rs @@ -1,4 +1,5 @@ // compile-flags: -C opt-level=0 +// needs-unwind // Test that `nounwind` attributes are correctly applied to exported `system` and `system-unwind` // extern functions. `system-unwind` functions MUST NOT have this attribute. We disable diff --git a/tests/codegen/unwind-extern-exports.rs b/tests/codegen/unwind-extern-exports.rs index 6ac3c079f81..4e1e719d5cd 100644 --- a/tests/codegen/unwind-extern-exports.rs +++ b/tests/codegen/unwind-extern-exports.rs @@ -1,5 +1,6 @@ // compile-flags: -C opt-level=0 // ignore-wasm32-bare compiled with panic=abort by default +// needs-unwind #![crate_type = "lib"] #![feature(c_unwind)] diff --git a/tests/codegen/unwind-extern-imports.rs b/tests/codegen/unwind-extern-imports.rs index e33e3e80521..260dcc628cc 100644 --- a/tests/codegen/unwind-extern-imports.rs +++ b/tests/codegen/unwind-extern-imports.rs @@ -1,5 +1,6 @@ // compile-flags: -C no-prepopulate-passes // ignore-wasm32-bare compiled with panic=abort by default +// needs-unwind #![crate_type = "lib"] #![feature(c_unwind)] diff --git a/tests/codegen/vec-shrink-panik.rs b/tests/codegen/vec-shrink-panik.rs index 88b7edff260..606d68ff3ab 100644 --- a/tests/codegen/vec-shrink-panik.rs +++ b/tests/codegen/vec-shrink-panik.rs @@ -5,6 +5,7 @@ // [new]min-llvm-version: 17 // compile-flags: -O // ignore-debug: the debug assertions get in the way +// needs-unwind #![crate_type = "lib"] #![feature(shrink_to)] diff --git a/tests/incremental/const-generics/hash-tyvid-regression-1.rs b/tests/incremental/const-generics/hash-tyvid-regression-1.rs index 5ff7b19d894..06d67423451 100644 --- a/tests/incremental/const-generics/hash-tyvid-regression-1.rs +++ b/tests/incremental/const-generics/hash-tyvid-regression-1.rs @@ -1,8 +1,20 @@ // revisions: cfail #![feature(generic_const_exprs, adt_const_params)] #![allow(incomplete_features)] + +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] +struct NonZeroUsize(usize); + +impl NonZeroUsize { + const fn get(self) -> usize { + self.0 + } +} + // regression test for #77650 -fn c<T, const N: std::num::NonZeroUsize>() +fn c<T, const N: NonZeroUsize>() where [T; N.get()]: Sized, { diff --git a/tests/incremental/const-generics/hash-tyvid-regression-2.rs b/tests/incremental/const-generics/hash-tyvid-regression-2.rs index 5cdd43cd782..33f226ff611 100644 --- a/tests/incremental/const-generics/hash-tyvid-regression-2.rs +++ b/tests/incremental/const-generics/hash-tyvid-regression-2.rs @@ -1,11 +1,23 @@ // revisions: cfail #![feature(generic_const_exprs, adt_const_params)] #![allow(incomplete_features)] + +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] +struct NonZeroUsize(usize); + +impl NonZeroUsize { + const fn get(self) -> usize { + self.0 + } +} + // regression test for #77650 -struct C<T, const N: core::num::NonZeroUsize>([T; N.get()]) +struct C<T, const N: NonZeroUsize>([T; N.get()]) where [T; N.get()]: Sized; -impl<'a, const N: core::num::NonZeroUsize, A, B: PartialEq<A>> PartialEq<&'a [A]> for C<B, N> +impl<'a, const N: NonZeroUsize, A, B: PartialEq<A>> PartialEq<&'a [A]> for C<B, N> where [B; N.get()]: Sized, { diff --git a/tests/incremental/const-generics/try_unify_abstract_const_regression_tests/issue-77708-3.rs b/tests/incremental/const-generics/try_unify_abstract_const_regression_tests/issue-77708-3.rs index fc114f224a2..f1c108fed11 100644 --- a/tests/incremental/const-generics/try_unify_abstract_const_regression_tests/issue-77708-3.rs +++ b/tests/incremental/const-generics/try_unify_abstract_const_regression_tests/issue-77708-3.rs @@ -2,7 +2,18 @@ #![feature(generic_const_exprs, adt_const_params)] #![allow(incomplete_features)] -use std::{convert::TryFrom, num::NonZeroUsize}; +use std::{convert::TryFrom}; + +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] +struct NonZeroUsize(usize); + +impl NonZeroUsize { + const fn get(self) -> usize { + self.0 + } +} struct A<const N: NonZeroUsize>([u8; N.get()]) where diff --git a/tests/incremental/hashes/match_expressions.rs b/tests/incremental/hashes/match_expressions.rs index 4429df6833e..ecb19480d65 100644 --- a/tests/incremental/hashes/match_expressions.rs +++ b/tests/incremental/hashes/match_expressions.rs @@ -225,8 +225,9 @@ pub fn change_mutability_of_binding_in_pattern(x: u32) -> u32 { } } +// Ignore optimized_mir in cfail2, the only change to optimized MIR is a span. #[cfg(not(any(cfail1,cfail4)))] -#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,optimized_mir,typeck")] +#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,typeck")] #[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail5", except="hir_owner_nodes,optimized_mir,typeck")] #[rustc_clean(cfg="cfail6")] diff --git a/tests/mir-opt/inline/cycle.main.Inline.diff b/tests/mir-opt/inline/cycle.main.Inline.diff index fdf6337a9be..7a39aae1baf 100644 --- a/tests/mir-opt/inline/cycle.main.Inline.diff +++ b/tests/mir-opt/inline/cycle.main.Inline.diff @@ -11,6 +11,14 @@ + let mut _3: &fn() {g}; // in scope 1 at $DIR/cycle.rs:6:5: 6:6 + let _4: (); // in scope 1 at $DIR/cycle.rs:6:5: 6:8 + scope 2 (inlined <fn() {g} as Fn<()>>::call - shim(fn() {g})) { // at $DIR/cycle.rs:6:5: 6:8 ++ scope 3 (inlined g) { // at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ scope 4 (inlined f::<fn() {main}>) { // at $DIR/cycle.rs:12:5: 12:12 ++ debug g => main; // in scope 4 at $DIR/cycle.rs:5:6: 5:7 ++ let _6: (); // in scope 4 at $DIR/cycle.rs:6:5: 6:8 ++ scope 5 (inlined <fn() {main} as Fn<()>>::call - shim(fn() {main})) { // at $DIR/cycle.rs:6:5: 6:8 ++ } ++ } ++ } + } + } @@ -30,7 +38,11 @@ + _3 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6 + StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + _5 = const (); // scope 1 at $DIR/cycle.rs:6:5: 6:8 -+ _4 = move (*_3)() -> [return: bb4, unwind: bb2]; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ StorageLive(_6); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ _6 = main() -> [return: bb4, unwind: bb2]; // scope 5 at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ // mir::Constant ++ // + span: no-location ++ // + literal: Const { ty: fn() {main}, val: Value(<ZST>) } } bb1: { @@ -50,6 +62,7 @@ + } + + bb4: { ++ StorageDead(_6); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL + StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:7: 6:8 + drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2 diff --git a/tests/mir-opt/inline/exponential_runtime.main.Inline.diff b/tests/mir-opt/inline/exponential_runtime.main.Inline.diff index 30af8661dec..7fad6aba82e 100644 --- a/tests/mir-opt/inline/exponential_runtime.main.Inline.diff +++ b/tests/mir-opt/inline/exponential_runtime.main.Inline.diff @@ -12,6 +12,26 @@ + let _5: (); // in scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 + let _6: (); // in scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25 + let _7: (); // in scope 2 at $DIR/exponential_runtime.rs:64:9: 64:25 ++ scope 3 (inlined <() as E>::call) { // at $DIR/exponential_runtime.rs:62:9: 62:25 ++ let _8: (); // in scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25 ++ let _9: (); // in scope 3 at $DIR/exponential_runtime.rs:51:9: 51:25 ++ let _10: (); // in scope 3 at $DIR/exponential_runtime.rs:52:9: 52:25 ++ scope 4 (inlined <() as D>::call) { // at $DIR/exponential_runtime.rs:50:9: 50:25 ++ let _11: (); // in scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25 ++ let _12: (); // in scope 4 at $DIR/exponential_runtime.rs:39:9: 39:25 ++ let _13: (); // in scope 4 at $DIR/exponential_runtime.rs:40:9: 40:25 ++ scope 5 (inlined <() as C>::call) { // at $DIR/exponential_runtime.rs:38:9: 38:25 ++ let _14: (); // in scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25 ++ let _15: (); // in scope 5 at $DIR/exponential_runtime.rs:27:9: 27:25 ++ let _16: (); // in scope 5 at $DIR/exponential_runtime.rs:28:9: 28:25 ++ scope 6 (inlined <() as B>::call) { // at $DIR/exponential_runtime.rs:26:9: 26:25 ++ let _17: (); // in scope 6 at $DIR/exponential_runtime.rs:14:9: 14:25 ++ let _18: (); // in scope 6 at $DIR/exponential_runtime.rs:15:9: 15:25 ++ let _19: (); // in scope 6 at $DIR/exponential_runtime.rs:16:9: 16:25 ++ } ++ } ++ } ++ } + } + } @@ -24,12 +44,24 @@ + StorageLive(_5); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 + StorageLive(_6); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 + StorageLive(_7); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 -+ _5 = <() as E>::call() -> bb4; // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 ++ StorageLive(_8); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 ++ StorageLive(_9); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 ++ StorageLive(_10); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 ++ StorageLive(_11); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25 ++ StorageLive(_12); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25 ++ StorageLive(_13); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25 ++ StorageLive(_14); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25 ++ StorageLive(_15); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25 ++ StorageLive(_16); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25 ++ StorageLive(_17); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25 ++ StorageLive(_18); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25 ++ StorageLive(_19); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25 ++ _17 = <() as A>::call() -> bb12; // scope 6 at $DIR/exponential_runtime.rs:14:9: 14:25 // mir::Constant - // + span: $DIR/exponential_runtime.rs:87:5: 87:20 - // + literal: Const { ty: fn() {<() as G>::call}, val: Value(<ZST>) } -+ // + span: $DIR/exponential_runtime.rs:62:9: 62:23 -+ // + literal: Const { ty: fn() {<() as E>::call}, val: Value(<ZST>) } ++ // + span: $DIR/exponential_runtime.rs:14:9: 14:23 ++ // + literal: Const { ty: fn() {<() as A>::call}, val: Value(<ZST>) } } bb1: { @@ -59,6 +91,9 @@ + } + + bb4: { ++ StorageDead(_10); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 ++ StorageDead(_9); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 ++ StorageDead(_8); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 + _6 = <() as E>::call() -> bb5; // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25 + // mir::Constant + // + span: $DIR/exponential_runtime.rs:63:9: 63:23 @@ -70,6 +105,71 @@ + // mir::Constant + // + span: $DIR/exponential_runtime.rs:64:9: 64:23 + // + literal: Const { ty: fn() {<() as E>::call}, val: Value(<ZST>) } ++ } ++ ++ bb6: { ++ StorageDead(_13); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25 ++ StorageDead(_12); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25 ++ StorageDead(_11); // scope 3 at $DIR/exponential_runtime.rs:50:9: 50:25 ++ _9 = <() as D>::call() -> bb7; // scope 3 at $DIR/exponential_runtime.rs:51:9: 51:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:51:9: 51:23 ++ // + literal: Const { ty: fn() {<() as D>::call}, val: Value(<ZST>) } ++ } ++ ++ bb7: { ++ _10 = <() as D>::call() -> bb4; // scope 3 at $DIR/exponential_runtime.rs:52:9: 52:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:52:9: 52:23 ++ // + literal: Const { ty: fn() {<() as D>::call}, val: Value(<ZST>) } ++ } ++ ++ bb8: { ++ StorageDead(_16); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25 ++ StorageDead(_15); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25 ++ StorageDead(_14); // scope 4 at $DIR/exponential_runtime.rs:38:9: 38:25 ++ _12 = <() as C>::call() -> bb9; // scope 4 at $DIR/exponential_runtime.rs:39:9: 39:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:39:9: 39:23 ++ // + literal: Const { ty: fn() {<() as C>::call}, val: Value(<ZST>) } ++ } ++ ++ bb9: { ++ _13 = <() as C>::call() -> bb6; // scope 4 at $DIR/exponential_runtime.rs:40:9: 40:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:40:9: 40:23 ++ // + literal: Const { ty: fn() {<() as C>::call}, val: Value(<ZST>) } ++ } ++ ++ bb10: { ++ StorageDead(_19); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25 ++ StorageDead(_18); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25 ++ StorageDead(_17); // scope 5 at $DIR/exponential_runtime.rs:26:9: 26:25 ++ _15 = <() as B>::call() -> bb11; // scope 5 at $DIR/exponential_runtime.rs:27:9: 27:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:27:9: 27:23 ++ // + literal: Const { ty: fn() {<() as B>::call}, val: Value(<ZST>) } ++ } ++ ++ bb11: { ++ _16 = <() as B>::call() -> bb8; // scope 5 at $DIR/exponential_runtime.rs:28:9: 28:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:28:9: 28:23 ++ // + literal: Const { ty: fn() {<() as B>::call}, val: Value(<ZST>) } ++ } ++ ++ bb12: { ++ _18 = <() as A>::call() -> bb13; // scope 6 at $DIR/exponential_runtime.rs:15:9: 15:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:15:9: 15:23 ++ // + literal: Const { ty: fn() {<() as A>::call}, val: Value(<ZST>) } ++ } ++ ++ bb13: { ++ _19 = <() as A>::call() -> bb10; // scope 6 at $DIR/exponential_runtime.rs:16:9: 16:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:16:9: 16:23 ++ // + literal: Const { ty: fn() {<() as A>::call}, val: Value(<ZST>) } } } diff --git a/tests/mir-opt/inline/inline_cycle.two.Inline.diff b/tests/mir-opt/inline/inline_cycle.two.Inline.diff index c8f58111da7..48f0bd10301 100644 --- a/tests/mir-opt/inline/inline_cycle.two.Inline.diff +++ b/tests/mir-opt/inline/inline_cycle.two.Inline.diff @@ -10,6 +10,14 @@ + debug f => _2; // in scope 1 at $DIR/inline_cycle.rs:54:22: 54:23 + let _3: (); // in scope 1 at $DIR/inline_cycle.rs:55:5: 55:8 + scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) { // at $DIR/inline_cycle.rs:55:5: 55:8 ++ scope 3 (inlined f) { // at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ scope 4 (inlined call::<fn() {f}>) { // at $DIR/inline_cycle.rs:60:5: 60:12 ++ debug f => f; // in scope 4 at $DIR/inline_cycle.rs:54:22: 54:23 ++ let _5: (); // in scope 4 at $DIR/inline_cycle.rs:55:5: 55:8 ++ scope 5 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) { // at $DIR/inline_cycle.rs:55:5: 55:8 ++ } ++ } ++ } + } + } @@ -27,10 +35,15 @@ + StorageLive(_3); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 + StorageLive(_4); // scope 1 at $DIR/inline_cycle.rs:55:5: 55:8 + _4 = const (); // scope 1 at $DIR/inline_cycle.rs:55:5: 55:8 -+ _3 = move _2() -> bb1; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ StorageLive(_5); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ _5 = f() -> bb1; // scope 5 at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ // mir::Constant ++ // + span: no-location ++ // + literal: Const { ty: fn() {f}, val: Value(<ZST>) } } bb1: { ++ StorageDead(_5); // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL + StorageDead(_4); // scope 1 at $DIR/inline_cycle.rs:55:5: 55:8 + StorageDead(_3); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 + StorageDead(_2); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 diff --git a/tests/mir-opt/inline/inline_cycle_generic.main.Inline.diff b/tests/mir-opt/inline/inline_cycle_generic.main.Inline.diff index 9429ca59364..8696e624b22 100644 --- a/tests/mir-opt/inline/inline_cycle_generic.main.Inline.diff +++ b/tests/mir-opt/inline/inline_cycle_generic.main.Inline.diff @@ -6,18 +6,20 @@ let _1: (); // in scope 0 at $DIR/inline_cycle_generic.rs:+1:5: +1:24 + scope 1 (inlined <C as Call>::call) { // at $DIR/inline_cycle_generic.rs:10:5: 10:24 + scope 2 (inlined <B<A> as Call>::call) { // at $DIR/inline_cycle_generic.rs:39:9: 39:31 ++ scope 3 (inlined <A as Call>::call) { // at $DIR/inline_cycle_generic.rs:32:9: 32:28 ++ } + } + } bb0: { StorageLive(_1); // scope 0 at $DIR/inline_cycle_generic.rs:+1:5: +1:24 - _1 = <C as Call>::call() -> bb1; // scope 0 at $DIR/inline_cycle_generic.rs:+1:5: +1:24 -+ _1 = <A as Call>::call() -> bb1; // scope 2 at $DIR/inline_cycle_generic.rs:32:9: 32:28 ++ _1 = <B<C> as Call>::call() -> bb1; // scope 3 at $DIR/inline_cycle_generic.rs:24:9: 24:31 // mir::Constant - // + span: $DIR/inline_cycle_generic.rs:10:5: 10:22 - // + literal: Const { ty: fn() {<C as Call>::call}, val: Value(<ZST>) } -+ // + span: $DIR/inline_cycle_generic.rs:32:9: 32:26 -+ // + literal: Const { ty: fn() {<A as Call>::call}, val: Value(<ZST>) } ++ // + span: $DIR/inline_cycle_generic.rs:24:9: 24:29 ++ // + literal: Const { ty: fn() {<B<C> as Call>::call}, val: Value(<ZST>) } } bb1: { diff --git a/tests/mir-opt/inline/inline_diverging.h.Inline.diff b/tests/mir-opt/inline/inline_diverging.h.Inline.diff index 255451e8670..78cd47c5f4b 100644 --- a/tests/mir-opt/inline/inline_diverging.h.Inline.diff +++ b/tests/mir-opt/inline/inline_diverging.h.Inline.diff @@ -20,6 +20,8 @@ + } + } + scope 4 (inlined <fn() -> ! {sleep} as Fn<()>>::call - shim(fn() -> ! {sleep})) { // at $DIR/inline_diverging.rs:27:13: 27:16 ++ scope 5 (inlined sleep) { // at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ } + } + } @@ -40,44 +42,11 @@ + _3 = &_2; // scope 1 at $DIR/inline_diverging.rs:27:13: 27:14 + StorageLive(_8); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 + _8 = const (); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 -+ _4 = move (*_3)() -> [return: bb6, unwind: bb4]; // scope 4 at $SRC_DIR/core/src/ops/function.rs:LL:COL ++ goto -> bb1; // scope 5 at $DIR/inline_diverging.rs:39:5: 39:12 + } + + bb1: { -+ StorageDead(_5); // scope 2 at $DIR/inline_diverging.rs:28:15: 28:16 -+ StorageLive(_7); // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7 -+ _7 = move _4; // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7 -+ _1 = (move _7, move _6); // scope 3 at $DIR/inline_diverging.rs:29:5: 29:11 -+ StorageDead(_7); // scope 3 at $DIR/inline_diverging.rs:29:10: 29:11 -+ StorageDead(_4); // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2 -+ drop(_2) -> bb2; // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2 -+ } -+ -+ bb2: { -+ unreachable; // scope 0 at $DIR/inline_diverging.rs:30:2: 30:2 -+ } -+ -+ bb3 (cleanup): { -+ drop(_4) -> [return: bb4, unwind terminate]; // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2 -+ } -+ -+ bb4 (cleanup): { -+ drop(_2) -> [return: bb5, unwind terminate]; // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2 -+ } -+ -+ bb5 (cleanup): { -+ resume; // scope 1 at $DIR/inline_diverging.rs:26:1: 30:2 -+ } -+ -+ bb6: { -+ StorageDead(_8); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 -+ StorageDead(_3); // scope 1 at $DIR/inline_diverging.rs:27:15: 27:16 -+ StorageLive(_5); // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14 -+ _5 = &_2; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14 -+ _6 = <fn() -> ! {sleep} as Fn<()>>::call(move _5, const ()) -> [return: bb1, unwind: bb3]; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:16 -+ // mir::Constant -+ // + span: $DIR/inline_diverging.rs:28:13: 28:14 -+ // + literal: Const { ty: for<'a> extern "rust-call" fn(&'a fn() -> ! {sleep}, ()) -> <fn() -> ! {sleep} as FnOnce<()>>::Output {<fn() -> ! {sleep} as Fn<()>>::call}, val: Value(<ZST>) } ++ goto -> bb1; // scope 5 at $DIR/inline_diverging.rs:39:5: 39:12 } } diff --git a/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.diff b/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.diff index d76cd0e2bb8..e82e3f18811 100644 --- a/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.diff +++ b/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.Inline.diff @@ -11,35 +11,33 @@ + debug self => _3; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug rhs => _4; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _5: u16; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _6: (u32,); // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _7: u32; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL + scope 2 { + scope 3 (inlined core::num::<impl u16>::unchecked_shl::conv) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug x => _7; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _8: std::option::Option<u16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _9: std::result::Result<u16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ debug x => _4; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ let mut _6: std::option::Option<u16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ let mut _7: std::result::Result<u16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + scope 4 { + scope 5 (inlined <u32 as TryInto<u16>>::try_into) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug self => _7; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL ++ debug self => _4; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + scope 6 (inlined convert::num::<impl TryFrom<u32> for u16>::try_from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL -+ debug u => _7; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ let mut _10: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ let mut _11: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ let mut _12: u16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ debug u => _4; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ let mut _8: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ let mut _9: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ let mut _10: u16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + } + } + scope 7 (inlined Result::<u16, TryFromIntError>::ok) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug self => _9; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ let mut _13: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ let _14: u16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ debug self => _7; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ let mut _11: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ let _12: u16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + scope 8 { -+ debug x => _14; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL ++ debug x => _12; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + } + } + scope 9 (inlined #[track_caller] Option::<u16>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug self => _8; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ let mut _15: &std::option::Option<u16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ let mut _16: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ debug self => _6; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ let mut _13: &std::option::Option<u16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ let mut _14: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + scope 10 { + debug val => _5; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL + } @@ -52,7 +50,7 @@ + } + } + scope 12 (inlined Option::<u16>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL -+ debug self => _15; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL ++ debug self => _13; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL + } + } + } @@ -70,18 +68,14 @@ - // + span: $DIR/unchecked_shifts.rs:11:7: 11:20 - // + literal: Const { ty: unsafe fn(u16, u32) -> u16 {core::num::<impl u16>::unchecked_shl}, val: Value(<ZST>) } + StorageLive(_5); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_6); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _6 = (_4,); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_7); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _7 = move (_6.0: u32); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageLive(_11); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _11 = const 65535_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _10 = Gt(_7, move _11); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageDead(_11); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ switchInt(move _10) -> [0: bb3, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageLive(_7); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageLive(_8); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_9); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _9 = const 65535_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _8 = Gt(_4, move _9); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageDead(_9); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ switchInt(move _8) -> [0: bb3, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } bb1: { @@ -92,7 +86,7 @@ + } + + bb2: { -+ _9 = Result::<u16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _7 = Result::<u16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + // mir::Constant + // + span: no-location + // + literal: Const { ty: TryFromIntError, val: Value(<ZST>) } @@ -100,22 +94,22 @@ + } + + bb3: { -+ StorageLive(_12); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _12 = _7 as u16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _9 = Result::<u16, TryFromIntError>::Ok(move _12); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageDead(_12); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _10 = _4 as u16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _7 = Result::<u16, TryFromIntError>::Ok(move _10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageDead(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + goto -> bb4; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + } + + bb4: { -+ StorageDead(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageLive(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _13 = discriminant(_9); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ switchInt(move _13) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ StorageDead(_8); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ _11 = discriminant(_7); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ switchInt(move _11) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + } + + bb5: { -+ _8 = Option::<u16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ _6 = Option::<u16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + goto -> bb8; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + } + @@ -124,25 +118,23 @@ + } + + bb7: { -+ _14 = move ((_9 as Ok).0: u16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ _8 = Option::<u16>::Some(move _14); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL ++ _12 = move ((_7 as Ok).0: u16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ _6 = Option::<u16>::Some(move _12); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + goto -> bb8; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + } + + bb8: { -+ StorageDead(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_15); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _16 = discriminant(_8); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ switchInt(move _16) -> [1: bb9, otherwise: bb6]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ StorageDead(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageDead(_7); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageLive(_13); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ _14 = discriminant(_6); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ switchInt(move _14) -> [1: bb9, otherwise: bb6]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + } + + bb9: { -+ _5 = move ((_8 as Some).0: u16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ StorageDead(_15); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_7); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_6); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ _5 = move ((_6 as Some).0: u16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ StorageDead(_13); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageDead(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _0 = unchecked_shl::<u16>(_3, move _5) -> [return: bb1, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL diff --git a/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.PreCodegen.after.mir b/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.PreCodegen.after.mir index f4b2416eaab..8fa4fdaa49a 100644 --- a/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.PreCodegen.after.mir +++ b/tests/mir-opt/inline/unchecked_shifts.unchecked_shl_unsigned_smaller.PreCodegen.after.mir @@ -7,38 +7,36 @@ fn unchecked_shl_unsigned_smaller(_1: u16, _2: u32) -> u16 { scope 1 (inlined core::num::<impl u16>::unchecked_shl) { // at $DIR/unchecked_shifts.rs:11:7: 11:23 debug self => _1; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL debug rhs => _2; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL - let mut _3: (u32,); // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _4: u32; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _13: u16; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _11: u16; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL scope 2 { scope 3 (inlined core::num::<impl u16>::unchecked_shl::conv) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug x => _4; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _8: std::result::Result<u16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _11: std::option::Option<u16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + debug x => _2; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _6: std::result::Result<u16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _9: std::option::Option<u16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL scope 4 { scope 5 (inlined <u32 as TryInto<u16>>::try_into) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug self => _4; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + debug self => _2; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL scope 6 (inlined convert::num::<impl TryFrom<u32> for u16>::try_from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL - debug u => _4; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - let mut _5: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - let mut _6: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - let mut _7: u16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + debug u => _2; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + let mut _3: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + let mut _4: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + let mut _5: u16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } } scope 7 (inlined Result::<u16, TryFromIntError>::ok) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug self => _8; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - let mut _9: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - let _10: u16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + debug self => _6; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + let mut _7: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + let _8: u16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL scope 8 { - debug x => _10; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + debug x => _8; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL } } scope 9 (inlined #[track_caller] Option::<u16>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug self => _11; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - let mut _12: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - let mut _14: &std::option::Option<u16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + debug self => _9; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + let mut _10: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + let mut _12: &std::option::Option<u16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL scope 10 { - debug val => _13; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL + debug val => _11; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL } scope 11 { scope 13 (inlined unreachable_unchecked) { // at $SRC_DIR/core/src/option.rs:LL:COL @@ -49,7 +47,7 @@ fn unchecked_shl_unsigned_smaller(_1: u16, _2: u32) -> u16 { } } scope 12 (inlined Option::<u16>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL - debug self => _14; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL + debug self => _12; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL } } } @@ -58,31 +56,27 @@ fn unchecked_shl_unsigned_smaller(_1: u16, _2: u32) -> u16 { } bb0: { - StorageLive(_13); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_3); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _3 = (_2,); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_4); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _4 = move (_3.0: u32); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_11); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_6); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageLive(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _5 = const 65535_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _6 = Gt(_4, move _5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageDead(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - switchInt(move _6) -> [0: bb1, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_11); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_4); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_3); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _3 = const 65535_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _4 = Gt(_2, move _3); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageDead(_3); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + switchInt(move _4) -> [0: bb1, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } bb1: { - StorageLive(_7); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _7 = _4 as u16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _8 = Result::<u16, TryFromIntError>::Ok(move _7); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageDead(_7); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _5 = _2 as u16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _6 = Result::<u16, TryFromIntError>::Ok(move _5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageDead(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL goto -> bb3; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } bb2: { - _8 = Result::<u16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _6 = Result::<u16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL // mir::Constant // + span: no-location // + literal: Const { ty: TryFromIntError, val: Value(<ZST>) } @@ -90,45 +84,43 @@ fn unchecked_shl_unsigned_smaller(_1: u16, _2: u32) -> u16 { } bb3: { - StorageDead(_6); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageLive(_10); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _9 = discriminant(_8); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - switchInt(move _9) -> [0: bb4, 1: bb5, otherwise: bb9]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + StorageDead(_4); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _7 = discriminant(_6); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + switchInt(move _7) -> [0: bb4, 1: bb5, otherwise: bb9]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL } bb4: { - _10 = move ((_8 as Ok).0: u16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - _11 = Option::<u16>::Some(move _10); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + _8 = move ((_6 as Ok).0: u16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + _9 = Option::<u16>::Some(move _8); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL goto -> bb6; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL } bb5: { - _11 = Option::<u16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + _9 = Option::<u16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL goto -> bb6; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL } bb6: { - StorageDead(_10); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL StorageDead(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _12 = discriminant(_11); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - switchInt(move _12) -> [1: bb7, otherwise: bb9]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + StorageDead(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _10 = discriminant(_9); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + switchInt(move _10) -> [1: bb7, otherwise: bb9]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL } bb7: { - _13 = move ((_11 as Some).0: u16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - StorageDead(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageDead(_11); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageDead(_4); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageDead(_3); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _0 = unchecked_shl::<u16>(_1, move _13) -> [return: bb8, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + _11 = move ((_9 as Some).0: u16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + StorageDead(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageDead(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _0 = unchecked_shl::<u16>(_1, move _11) -> [return: bb8, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(u16, u16) -> u16 {unchecked_shl::<u16>}, val: Value(<ZST>) } } bb8: { - StorageDead(_13); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageDead(_11); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL return; // scope 0 at $DIR/unchecked_shifts.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.Inline.diff b/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.Inline.diff index f3d3e6090bb..f20c7da4747 100644 --- a/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.Inline.diff +++ b/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.Inline.diff @@ -11,35 +11,33 @@ + debug self => _3; // in scope 1 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL + debug rhs => _4; // in scope 1 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL + let mut _5: i16; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _6: (u32,); // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _7: u32; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL + scope 2 { + scope 3 (inlined core::num::<impl i16>::unchecked_shr::conv) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug x => _7; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _8: std::option::Option<i16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ let mut _9: std::result::Result<i16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ debug x => _4; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ let mut _6: std::option::Option<i16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ let mut _7: std::result::Result<i16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + scope 4 { + scope 5 (inlined <u32 as TryInto<i16>>::try_into) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug self => _7; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL ++ debug self => _4; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + scope 6 (inlined convert::num::<impl TryFrom<u32> for i16>::try_from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL -+ debug u => _7; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ let mut _10: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ let mut _11: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ let mut _12: i16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ debug u => _4; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ let mut _8: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ let mut _9: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ let mut _10: i16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + } + } + scope 7 (inlined Result::<i16, TryFromIntError>::ok) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug self => _9; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ let mut _13: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ let _14: i16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ debug self => _7; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ let mut _11: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ let _12: i16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + scope 8 { -+ debug x => _14; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL ++ debug x => _12; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + } + } + scope 9 (inlined #[track_caller] Option::<i16>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ debug self => _8; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ let mut _15: &std::option::Option<i16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ let mut _16: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ debug self => _6; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ let mut _13: &std::option::Option<i16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ let mut _14: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + scope 10 { + debug val => _5; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL + } @@ -52,7 +50,7 @@ + } + } + scope 12 (inlined Option::<i16>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL -+ debug self => _15; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL ++ debug self => _13; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL + } + } + } @@ -70,18 +68,14 @@ - // + span: $DIR/unchecked_shifts.rs:17:7: 17:20 - // + literal: Const { ty: unsafe fn(i16, u32) -> i16 {core::num::<impl i16>::unchecked_shr}, val: Value(<ZST>) } + StorageLive(_5); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_6); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _6 = (_4,); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_7); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _7 = move (_6.0: u32); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageLive(_11); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _11 = const 32767_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _10 = Gt(_7, move _11); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageDead(_11); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ switchInt(move _10) -> [0: bb3, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageLive(_7); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageLive(_8); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_9); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _9 = const 32767_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _8 = Gt(_4, move _9); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageDead(_9); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ switchInt(move _8) -> [0: bb3, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } bb1: { @@ -92,7 +86,7 @@ + } + + bb2: { -+ _9 = Result::<i16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _7 = Result::<i16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + // mir::Constant + // + span: no-location + // + literal: Const { ty: TryFromIntError, val: Value(<ZST>) } @@ -100,22 +94,22 @@ + } + + bb3: { -+ StorageLive(_12); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _12 = _7 as i16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ _9 = Result::<i16, TryFromIntError>::Ok(move _12); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageDead(_12); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _10 = _4 as i16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ _7 = Result::<i16, TryFromIntError>::Ok(move _10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageDead(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + goto -> bb4; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + } + + bb4: { -+ StorageDead(_10); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL -+ StorageLive(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _13 = discriminant(_9); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ switchInt(move _13) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ StorageDead(_8); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL ++ StorageLive(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ _11 = discriminant(_7); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ switchInt(move _11) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + } + + bb5: { -+ _8 = Option::<i16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ _6 = Option::<i16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + goto -> bb8; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + } + @@ -124,25 +118,23 @@ + } + + bb7: { -+ _14 = move ((_9 as Ok).0: i16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL -+ _8 = Option::<i16>::Some(move _14); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL ++ _12 = move ((_7 as Ok).0: i16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL ++ _6 = Option::<i16>::Some(move _12); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + goto -> bb8; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + } + + bb8: { -+ StorageDead(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageLive(_15); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ _16 = discriminant(_8); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ switchInt(move _16) -> [1: bb9, otherwise: bb6]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ StorageDead(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageDead(_7); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageLive(_13); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ _14 = discriminant(_6); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ switchInt(move _14) -> [1: bb9, otherwise: bb6]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + } + + bb9: { -+ _5 = move ((_8 as Some).0: i16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL -+ StorageDead(_15); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_7); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL -+ StorageDead(_6); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ _5 = move ((_6 as Some).0: i16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL ++ StorageDead(_13); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL ++ StorageDead(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _0 = unchecked_shr::<i16>(_3, move _5) -> [return: bb1, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/num/int_macros.rs:LL:COL diff --git a/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.PreCodegen.after.mir b/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.PreCodegen.after.mir index 67f0fe8932f..7f737abb936 100644 --- a/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.PreCodegen.after.mir +++ b/tests/mir-opt/inline/unchecked_shifts.unchecked_shr_signed_smaller.PreCodegen.after.mir @@ -7,38 +7,36 @@ fn unchecked_shr_signed_smaller(_1: i16, _2: u32) -> i16 { scope 1 (inlined core::num::<impl i16>::unchecked_shr) { // at $DIR/unchecked_shifts.rs:17:7: 17:23 debug self => _1; // in scope 1 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL debug rhs => _2; // in scope 1 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL - let mut _3: (u32,); // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _4: u32; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _13: i16; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _11: i16; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL scope 2 { scope 3 (inlined core::num::<impl i16>::unchecked_shr::conv) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug x => _4; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _8: std::result::Result<i16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL - let mut _11: std::option::Option<i16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + debug x => _2; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _6: std::result::Result<i16, std::num::TryFromIntError>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _9: std::option::Option<i16>; // in scope 3 at $SRC_DIR/core/src/num/mod.rs:LL:COL scope 4 { scope 5 (inlined <u32 as TryInto<i16>>::try_into) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug self => _4; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + debug self => _2; // in scope 5 at $SRC_DIR/core/src/convert/mod.rs:LL:COL scope 6 (inlined convert::num::<impl TryFrom<u32> for i16>::try_from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL - debug u => _4; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - let mut _5: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - let mut _6: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - let mut _7: i16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + debug u => _2; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + let mut _3: u32; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + let mut _4: bool; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + let mut _5: i16; // in scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } } scope 7 (inlined Result::<i16, TryFromIntError>::ok) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug self => _8; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - let mut _9: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - let _10: i16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + debug self => _6; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + let mut _7: isize; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + let _8: i16; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL scope 8 { - debug x => _10; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + debug x => _8; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL } } scope 9 (inlined #[track_caller] Option::<i16>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL - debug self => _11; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - let mut _12: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - let mut _14: &std::option::Option<i16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + debug self => _9; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + let mut _10: isize; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + let mut _12: &std::option::Option<i16>; // in scope 9 at $SRC_DIR/core/src/option.rs:LL:COL scope 10 { - debug val => _13; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL + debug val => _11; // in scope 10 at $SRC_DIR/core/src/option.rs:LL:COL } scope 11 { scope 13 (inlined unreachable_unchecked) { // at $SRC_DIR/core/src/option.rs:LL:COL @@ -49,7 +47,7 @@ fn unchecked_shr_signed_smaller(_1: i16, _2: u32) -> i16 { } } scope 12 (inlined Option::<i16>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL - debug self => _14; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL + debug self => _12; // in scope 12 at $SRC_DIR/core/src/option.rs:LL:COL } } } @@ -58,31 +56,27 @@ fn unchecked_shr_signed_smaller(_1: i16, _2: u32) -> i16 { } bb0: { - StorageLive(_13); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_3); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _3 = (_2,); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_4); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _4 = move (_3.0: u32); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_11); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_6); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageLive(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _5 = const 32767_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _6 = Gt(_4, move _5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageDead(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - switchInt(move _6) -> [0: bb1, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_11); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_4); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_3); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _3 = const 32767_u32; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _4 = Gt(_2, move _3); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageDead(_3); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + switchInt(move _4) -> [0: bb1, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } bb1: { - StorageLive(_7); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _7 = _4 as i16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - _8 = Result::<i16, TryFromIntError>::Ok(move _7); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageDead(_7); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _5 = _2 as i16 (IntToInt); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _6 = Result::<i16, TryFromIntError>::Ok(move _5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageDead(_5); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL goto -> bb3; // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL } bb2: { - _8 = Result::<i16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + _6 = Result::<i16, TryFromIntError>::Err(const TryFromIntError(())); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL // mir::Constant // + span: no-location // + literal: Const { ty: TryFromIntError, val: Value(<ZST>) } @@ -90,45 +84,43 @@ fn unchecked_shr_signed_smaller(_1: i16, _2: u32) -> i16 { } bb3: { - StorageDead(_6); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL - StorageLive(_10); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _9 = discriminant(_8); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - switchInt(move _9) -> [0: bb4, 1: bb5, otherwise: bb9]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + StorageDead(_4); // scope 6 at $SRC_DIR/core/src/convert/num.rs:LL:COL + StorageLive(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _7 = discriminant(_6); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + switchInt(move _7) -> [0: bb4, 1: bb5, otherwise: bb9]; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL } bb4: { - _10 = move ((_8 as Ok).0: i16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - _11 = Option::<i16>::Some(move _10); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + _8 = move ((_6 as Ok).0: i16); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + _9 = Option::<i16>::Some(move _8); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL goto -> bb6; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL } bb5: { - _11 = Option::<i16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + _9 = Option::<i16>::None; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL goto -> bb6; // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL } bb6: { - StorageDead(_10); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL StorageDead(_8); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageLive(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _12 = discriminant(_11); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - switchInt(move _12) -> [1: bb7, otherwise: bb9]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + StorageDead(_6); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _10 = discriminant(_9); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + switchInt(move _10) -> [1: bb7, otherwise: bb9]; // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL } bb7: { - _13 = move ((_11 as Some).0: i16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL - StorageDead(_14); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageDead(_11); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageDead(_4); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - StorageDead(_3); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL - _0 = unchecked_shr::<i16>(_1, move _13) -> [return: bb8, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL + _11 = move ((_9 as Some).0: i16); // scope 9 at $SRC_DIR/core/src/option.rs:LL:COL + StorageDead(_12); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageDead(_9); // scope 4 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _0 = unchecked_shr::<i16>(_1, move _11) -> [return: bb8, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/num/int_macros.rs:LL:COL // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(i16, i16) -> i16 {unchecked_shr::<i16>}, val: Value(<ZST>) } } bb8: { - StorageDead(_13); // scope 2 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL + StorageDead(_11); // scope 2 at $SRC_DIR/core/src/num/int_macros.rs:LL:COL return; // scope 0 at $DIR/unchecked_shifts.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/lower_intrinsics.align_of.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.align_of.LowerIntrinsics.diff index dd742d87a29..3530f4a807f 100644 --- a/tests/mir-opt/lower_intrinsics.align_of.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.align_of.LowerIntrinsics.diff @@ -7,7 +7,7 @@ bb0: { - _0 = std::intrinsics::min_align_of::<T>() -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:42 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:21:5: 21:40 +- // + span: $DIR/lower_intrinsics.rs:27:5: 27:40 - // + literal: Const { ty: extern "rust-intrinsic" fn() -> usize {std::intrinsics::min_align_of::<T>}, val: Value(<ZST>) } + _0 = AlignOf(T); // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:42 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:42 diff --git a/tests/mir-opt/lower_intrinsics.assume.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.assume.LowerIntrinsics.diff index e95c3f9bcc7..158ce62e209 100644 --- a/tests/mir-opt/lower_intrinsics.assume.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.assume.LowerIntrinsics.diff @@ -11,7 +11,7 @@ StorageLive(_1); // scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:38 - _1 = std::intrinsics::assume(const true) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:38 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:106:9: 106:32 +- // + span: $DIR/lower_intrinsics.rs:112:9: 112:32 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(bool) {std::intrinsics::assume}, val: Value(<ZST>) } + assume(const true); // scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:38 + goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:38 diff --git a/tests/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff index 1f03b7b0baf..6fc9616d855 100644 --- a/tests/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff @@ -31,7 +31,7 @@ _3 = &(*_4); // scope 0 at $DIR/lower_intrinsics.rs:+1:42: +1:44 - _2 = discriminant_value::<T>(move _3) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:45 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:82:5: 82:41 +- // + span: $DIR/lower_intrinsics.rs:88:5: 88:41 - // + literal: Const { ty: for<'a> extern "rust-intrinsic" fn(&'a T) -> <T as DiscriminantKind>::Discriminant {discriminant_value::<T>}, val: Value(<ZST>) } + _2 = discriminant((*_3)); // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:45 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:45 @@ -46,13 +46,13 @@ StorageLive(_7); // scope 0 at $DIR/lower_intrinsics.rs:+2:42: +2:44 _19 = const _; // scope 0 at $DIR/lower_intrinsics.rs:+2:42: +2:44 // mir::Constant - // + span: $DIR/lower_intrinsics.rs:83:42: 83:44 + // + span: $DIR/lower_intrinsics.rs:89:42: 89:44 // + literal: Const { ty: &i32, val: Unevaluated(discriminant, [T], Some(promoted[2])) } _7 = &(*_19); // scope 0 at $DIR/lower_intrinsics.rs:+2:42: +2:44 _6 = &(*_7); // scope 0 at $DIR/lower_intrinsics.rs:+2:42: +2:44 - _5 = discriminant_value::<i32>(move _6) -> [return: bb2, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+2:5: +2:45 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:83:5: 83:41 +- // + span: $DIR/lower_intrinsics.rs:89:5: 89:41 - // + literal: Const { ty: for<'a> extern "rust-intrinsic" fn(&'a i32) -> <i32 as DiscriminantKind>::Discriminant {discriminant_value::<i32>}, val: Value(<ZST>) } + _5 = discriminant((*_6)); // scope 0 at $DIR/lower_intrinsics.rs:+2:5: +2:45 + goto -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:+2:5: +2:45 @@ -67,13 +67,13 @@ StorageLive(_11); // scope 0 at $DIR/lower_intrinsics.rs:+3:42: +3:45 _18 = const _; // scope 0 at $DIR/lower_intrinsics.rs:+3:42: +3:45 // mir::Constant - // + span: $DIR/lower_intrinsics.rs:84:42: 84:45 + // + span: $DIR/lower_intrinsics.rs:90:42: 90:45 // + literal: Const { ty: &(), val: Unevaluated(discriminant, [T], Some(promoted[1])) } _11 = &(*_18); // scope 0 at $DIR/lower_intrinsics.rs:+3:42: +3:45 _10 = &(*_11); // scope 0 at $DIR/lower_intrinsics.rs:+3:42: +3:45 - _9 = discriminant_value::<()>(move _10) -> [return: bb3, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+3:5: +3:46 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:84:5: 84:41 +- // + span: $DIR/lower_intrinsics.rs:90:5: 90:41 - // + literal: Const { ty: for<'a> extern "rust-intrinsic" fn(&'a ()) -> <() as DiscriminantKind>::Discriminant {discriminant_value::<()>}, val: Value(<ZST>) } + _9 = discriminant((*_10)); // scope 0 at $DIR/lower_intrinsics.rs:+3:5: +3:46 + goto -> bb3; // scope 0 at $DIR/lower_intrinsics.rs:+3:5: +3:46 @@ -88,13 +88,13 @@ StorageLive(_15); // scope 0 at $DIR/lower_intrinsics.rs:+4:42: +4:47 _17 = const _; // scope 0 at $DIR/lower_intrinsics.rs:+4:42: +4:47 // mir::Constant - // + span: $DIR/lower_intrinsics.rs:85:42: 85:47 + // + span: $DIR/lower_intrinsics.rs:91:42: 91:47 // + literal: Const { ty: &E, val: Unevaluated(discriminant, [T], Some(promoted[0])) } _15 = &(*_17); // scope 0 at $DIR/lower_intrinsics.rs:+4:42: +4:47 _14 = &(*_15); // scope 0 at $DIR/lower_intrinsics.rs:+4:42: +4:47 - _13 = discriminant_value::<E>(move _14) -> [return: bb4, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+4:5: +4:48 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:85:5: 85:41 +- // + span: $DIR/lower_intrinsics.rs:91:5: 91:41 - // + literal: Const { ty: for<'a> extern "rust-intrinsic" fn(&'a E) -> <E as DiscriminantKind>::Discriminant {discriminant_value::<E>}, val: Value(<ZST>) } + _13 = discriminant((*_14)); // scope 0 at $DIR/lower_intrinsics.rs:+4:5: +4:48 + goto -> bb4; // scope 0 at $DIR/lower_intrinsics.rs:+4:5: +4:48 diff --git a/tests/mir-opt/lower_intrinsics.f_copy_nonoverlapping.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.f_copy_nonoverlapping.LowerIntrinsics.diff index a54e9a9320e..5b870ccf5ee 100644 --- a/tests/mir-opt/lower_intrinsics.f_copy_nonoverlapping.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.f_copy_nonoverlapping.LowerIntrinsics.diff @@ -49,7 +49,7 @@ StorageDead(_9); // scope 3 at $DIR/lower_intrinsics.rs:+4:90: +4:91 - _3 = copy_nonoverlapping::<i32>(move _4, move _8, const 0_usize) -> [return: bb1, unwind unreachable]; // scope 3 at $DIR/lower_intrinsics.rs:+4:9: +4:95 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:99:9: 99:28 +- // + span: $DIR/lower_intrinsics.rs:105:9: 105:28 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const i32, *mut i32, usize) {copy_nonoverlapping::<i32>}, val: Value(<ZST>) } + copy_nonoverlapping(dst = move _8, src = move _4, count = const 0_usize); // scope 3 at $DIR/lower_intrinsics.rs:+4:9: +4:95 + goto -> bb1; // scope 3 at $DIR/lower_intrinsics.rs:+4:9: +4:95 diff --git a/tests/mir-opt/lower_intrinsics.forget.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.forget.LowerIntrinsics.diff index bfb000ccdb5..582a79f48d8 100644 --- a/tests/mir-opt/lower_intrinsics.forget.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.forget.LowerIntrinsics.diff @@ -11,7 +11,7 @@ _2 = move _1; // scope 0 at $DIR/lower_intrinsics.rs:+1:30: +1:31 - _0 = std::intrinsics::forget::<T>(move _2) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:32 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:26:5: 26:29 +- // + span: $DIR/lower_intrinsics.rs:32:5: 32:29 - // + literal: Const { ty: extern "rust-intrinsic" fn(T) {std::intrinsics::forget::<T>}, val: Value(<ZST>) } + _0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:32 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:32 diff --git a/tests/mir-opt/lower_intrinsics.non_const.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.non_const.LowerIntrinsics.diff index 64d82907c7e..81ad97077b4 100644 --- a/tests/mir-opt/lower_intrinsics.non_const.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.non_const.LowerIntrinsics.diff @@ -13,7 +13,7 @@ StorageLive(_1); // scope 0 at $DIR/lower_intrinsics.rs:+2:9: +2:18 _1 = std::intrinsics::size_of::<T>; // scope 0 at $DIR/lower_intrinsics.rs:+2:21: +2:51 // mir::Constant - // + span: $DIR/lower_intrinsics.rs:37:21: 37:51 + // + span: $DIR/lower_intrinsics.rs:43:21: 43:51 // + literal: Const { ty: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>}, val: Value(<ZST>) } StorageLive(_2); // scope 1 at $DIR/lower_intrinsics.rs:+3:5: +3:14 _2 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+3:5: +3:14 diff --git a/tests/mir-opt/lower_intrinsics.option_payload.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.option_payload.LowerIntrinsics.diff index b022e2ba42b..edc66e2c75c 100644 --- a/tests/mir-opt/lower_intrinsics.option_payload.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.option_payload.LowerIntrinsics.diff @@ -24,7 +24,7 @@ _4 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+2:55: +2:56 - _3 = option_payload_ptr::<usize>(move _4) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+2:18: +2:57 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:137:18: 137:54 +- // + span: $DIR/lower_intrinsics.rs:143:18: 143:54 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const Option<usize>) -> *const usize {option_payload_ptr::<usize>}, val: Value(<ZST>) } + _3 = &raw const (((*_4) as Some).0: usize); // scope 1 at $DIR/lower_intrinsics.rs:+2:18: +2:57 + goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+2:18: +2:57 @@ -37,7 +37,7 @@ _6 = &raw const (*_2); // scope 2 at $DIR/lower_intrinsics.rs:+3:55: +3:56 - _5 = option_payload_ptr::<String>(move _6) -> [return: bb2, unwind unreachable]; // scope 2 at $DIR/lower_intrinsics.rs:+3:18: +3:57 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:138:18: 138:54 +- // + span: $DIR/lower_intrinsics.rs:144:18: 144:54 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const Option<String>) -> *const String {option_payload_ptr::<String>}, val: Value(<ZST>) } + _5 = &raw const (((*_6) as Some).0: std::string::String); // scope 2 at $DIR/lower_intrinsics.rs:+3:18: +3:57 + goto -> bb2; // scope 2 at $DIR/lower_intrinsics.rs:+3:18: +3:57 diff --git a/tests/mir-opt/lower_intrinsics.ptr_offset.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.ptr_offset.LowerIntrinsics.diff index 60a1dd0ba7d..1760efe77d9 100644 --- a/tests/mir-opt/lower_intrinsics.ptr_offset.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.ptr_offset.LowerIntrinsics.diff @@ -15,7 +15,7 @@ _4 = _2; // scope 0 at $DIR/lower_intrinsics.rs:+1:33: +1:34 - _0 = offset::<*const i32, isize>(move _3, move _4) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:35 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:144:5: 144:29 +- // + span: $DIR/lower_intrinsics.rs:150:5: 150:29 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const i32, isize) -> *const i32 {offset::<*const i32, isize>}, val: Value(<ZST>) } + _0 = Offset(move _3, move _4); // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:35 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:35 diff --git a/tests/mir-opt/lower_intrinsics.read_via_copy_primitive.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.read_via_copy_primitive.LowerIntrinsics.diff index 5805df48f54..8583766348a 100644 --- a/tests/mir-opt/lower_intrinsics.read_via_copy_primitive.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.read_via_copy_primitive.LowerIntrinsics.diff @@ -13,7 +13,7 @@ _2 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 - _0 = read_via_copy::<i32>(move _2) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:119:14: 119:45 +- // + span: $DIR/lower_intrinsics.rs:125:14: 125:45 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const i32) -> i32 {read_via_copy::<i32>}, val: Value(<ZST>) } + _0 = (*_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 + goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 diff --git a/tests/mir-opt/lower_intrinsics.read_via_copy_uninhabited.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.read_via_copy_uninhabited.LowerIntrinsics.diff index 95b2ec49d80..f64bc9dcf62 100644 --- a/tests/mir-opt/lower_intrinsics.read_via_copy_uninhabited.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.read_via_copy_uninhabited.LowerIntrinsics.diff @@ -13,7 +13,7 @@ _2 = &raw const (*_1); // scope 1 at $DIR/lower_intrinsics.rs:+1:46: +1:47 - _0 = read_via_copy::<Never>(move _2) -> unwind unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:124:14: 124:45 +- // + span: $DIR/lower_intrinsics.rs:130:14: 130:45 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*const Never) -> Never {read_via_copy::<Never>}, val: Value(<ZST>) } + unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:48 } diff --git a/tests/mir-opt/lower_intrinsics.rs b/tests/mir-opt/lower_intrinsics.rs index 0ca88a42e3f..30b5c78e647 100644 --- a/tests/mir-opt/lower_intrinsics.rs +++ b/tests/mir-opt/lower_intrinsics.rs @@ -11,6 +11,12 @@ pub fn wrapping(a: i32, b: i32) { let _z = core::intrinsics::wrapping_mul(a, b); } +// EMIT_MIR lower_intrinsics.unchecked.LowerIntrinsics.diff +pub unsafe fn unchecked(a: i32, b: i32) { + let _x = core::intrinsics::unchecked_div(a, b); + let _y = core::intrinsics::unchecked_rem(a, b); +} + // EMIT_MIR lower_intrinsics.size_of.LowerIntrinsics.diff pub fn size_of<T>() -> usize { core::intrinsics::size_of::<T>() diff --git a/tests/mir-opt/lower_intrinsics.size_of.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.size_of.LowerIntrinsics.diff index 27e8accea8b..a880df6a5c2 100644 --- a/tests/mir-opt/lower_intrinsics.size_of.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.size_of.LowerIntrinsics.diff @@ -7,7 +7,7 @@ bb0: { - _0 = std::intrinsics::size_of::<T>() -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:37 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:16:5: 16:35 +- // + span: $DIR/lower_intrinsics.rs:22:5: 22:35 - // + literal: Const { ty: extern "rust-intrinsic" fn() -> usize {std::intrinsics::size_of::<T>}, val: Value(<ZST>) } + _0 = SizeOf(T); // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:37 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:5: +1:37 diff --git a/tests/mir-opt/lower_intrinsics.transmute_inhabited.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.transmute_inhabited.LowerIntrinsics.diff index 1b3b7685185..cde7c64c57a 100644 --- a/tests/mir-opt/lower_intrinsics.transmute_inhabited.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.transmute_inhabited.LowerIntrinsics.diff @@ -13,7 +13,7 @@ _2 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+1:34: +1:35 - _0 = transmute::<std::cmp::Ordering, i8>(move _2) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:36 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:43:14: 43:33 +- // + span: $DIR/lower_intrinsics.rs:49:14: 49:33 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(std::cmp::Ordering) -> i8 {transmute::<std::cmp::Ordering, i8>}, val: Value(<ZST>) } + _0 = move _2 as i8 (Transmute); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:36 + goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:36 diff --git a/tests/mir-opt/lower_intrinsics.transmute_ref_dst.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.transmute_ref_dst.LowerIntrinsics.diff index c6a7d2287e7..6fc0f3d3e3f 100644 --- a/tests/mir-opt/lower_intrinsics.transmute_ref_dst.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.transmute_ref_dst.LowerIntrinsics.diff @@ -13,7 +13,7 @@ _2 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+1:34: +1:35 - _0 = transmute::<&T, *const T>(move _2) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:36 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:53:14: 53:33 +- // + span: $DIR/lower_intrinsics.rs:59:14: 59:33 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(&T) -> *const T {transmute::<&T, *const T>}, val: Value(<ZST>) } + _0 = move _2 as *const T (Transmute); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:36 + goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:36 diff --git a/tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.diff index aa5d9619d10..e6887a382a2 100644 --- a/tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.transmute_to_box_uninhabited.LowerIntrinsics.diff @@ -12,7 +12,7 @@ StorageLive(_1); // scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:10 - _1 = transmute::<usize, Box<Never>>(const 1_usize) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:25: +1:52 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:70:25: 70:44 +- // + span: $DIR/lower_intrinsics.rs:76:25: 76:44 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(usize) -> Box<Never> {transmute::<usize, Box<Never>>}, val: Value(<ZST>) } + _1 = const 1_usize as std::boxed::Box<Never> (Transmute); // scope 0 at $DIR/lower_intrinsics.rs:+1:25: +1:52 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:25: +1:52 diff --git a/tests/mir-opt/lower_intrinsics.transmute_to_mut_uninhabited.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.transmute_to_mut_uninhabited.LowerIntrinsics.diff index 5fafd45fe85..b2a44b7c561 100644 --- a/tests/mir-opt/lower_intrinsics.transmute_to_mut_uninhabited.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.transmute_to_mut_uninhabited.LowerIntrinsics.diff @@ -12,7 +12,7 @@ StorageLive(_1); // scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:10 - _1 = transmute::<usize, &mut Never>(const 1_usize) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:25: +1:52 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:64:25: 64:44 +- // + span: $DIR/lower_intrinsics.rs:70:25: 70:44 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(usize) -> &mut Never {transmute::<usize, &mut Never>}, val: Value(<ZST>) } + _1 = const 1_usize as &mut Never (Transmute); // scope 0 at $DIR/lower_intrinsics.rs:+1:25: +1:52 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:25: +1:52 diff --git a/tests/mir-opt/lower_intrinsics.transmute_to_ref_uninhabited.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.transmute_to_ref_uninhabited.LowerIntrinsics.diff index 08dead13211..c49d3aeff70 100644 --- a/tests/mir-opt/lower_intrinsics.transmute_to_ref_uninhabited.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.transmute_to_ref_uninhabited.LowerIntrinsics.diff @@ -12,7 +12,7 @@ StorageLive(_1); // scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:10 - _1 = transmute::<usize, &Never>(const 1_usize) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:21: +1:48 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:58:21: 58:40 +- // + span: $DIR/lower_intrinsics.rs:64:21: 64:40 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(usize) -> &Never {transmute::<usize, &Never>}, val: Value(<ZST>) } + _1 = const 1_usize as &Never (Transmute); // scope 0 at $DIR/lower_intrinsics.rs:+1:21: +1:48 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:21: +1:48 diff --git a/tests/mir-opt/lower_intrinsics.transmute_uninhabited.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.transmute_uninhabited.LowerIntrinsics.diff index f0b76127dd5..06759d74a32 100644 --- a/tests/mir-opt/lower_intrinsics.transmute_uninhabited.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.transmute_uninhabited.LowerIntrinsics.diff @@ -13,7 +13,7 @@ _2 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+1:47: +1:48 - _0 = transmute::<(), Never>(move _2) -> unwind unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:49 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:48:14: 48:46 +- // + span: $DIR/lower_intrinsics.rs:54:14: 54:46 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(()) -> Never {transmute::<(), Never>}, val: Value(<ZST>) } + _0 = move _2 as Never (Transmute); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:49 + unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:49 diff --git a/tests/mir-opt/lower_intrinsics.unchecked.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.unchecked.LowerIntrinsics.diff new file mode 100644 index 00000000000..9bb43d850eb --- /dev/null +++ b/tests/mir-opt/lower_intrinsics.unchecked.LowerIntrinsics.diff @@ -0,0 +1,60 @@ +- // MIR for `unchecked` before LowerIntrinsics ++ // MIR for `unchecked` after LowerIntrinsics + + fn unchecked(_1: i32, _2: i32) -> () { + debug a => _1; // in scope 0 at $DIR/lower_intrinsics.rs:+0:25: +0:26 + debug b => _2; // in scope 0 at $DIR/lower_intrinsics.rs:+0:33: +0:34 + let mut _0: (); // return place in scope 0 at $DIR/lower_intrinsics.rs:+0:41: +0:41 + let _3: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:11 + let mut _4: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:46: +1:47 + let mut _5: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:49: +1:50 + let mut _7: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+2:46: +2:47 + let mut _8: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+2:49: +2:50 + scope 1 { + debug _x => _3; // in scope 1 at $DIR/lower_intrinsics.rs:+1:9: +1:11 + let _6: i32; // in scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:11 + scope 2 { + debug _y => _6; // in scope 2 at $DIR/lower_intrinsics.rs:+2:9: +2:11 + } + } + + bb0: { + StorageLive(_3); // scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:11 + StorageLive(_4); // scope 0 at $DIR/lower_intrinsics.rs:+1:46: +1:47 + _4 = _1; // scope 0 at $DIR/lower_intrinsics.rs:+1:46: +1:47 + StorageLive(_5); // scope 0 at $DIR/lower_intrinsics.rs:+1:49: +1:50 + _5 = _2; // scope 0 at $DIR/lower_intrinsics.rs:+1:49: +1:50 +- _3 = unchecked_div::<i32>(move _4, move _5) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:51 +- // mir::Constant +- // + span: $DIR/lower_intrinsics.rs:16:14: 16:45 +- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(i32, i32) -> i32 {unchecked_div::<i32>}, val: Value(<ZST>) } ++ _3 = Div(move _4, move _5); // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:51 ++ goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:51 + } + + bb1: { + StorageDead(_5); // scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51 + StorageDead(_4); // scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51 + StorageLive(_6); // scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:11 + StorageLive(_7); // scope 1 at $DIR/lower_intrinsics.rs:+2:46: +2:47 + _7 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+2:46: +2:47 + StorageLive(_8); // scope 1 at $DIR/lower_intrinsics.rs:+2:49: +2:50 + _8 = _2; // scope 1 at $DIR/lower_intrinsics.rs:+2:49: +2:50 +- _6 = unchecked_rem::<i32>(move _7, move _8) -> [return: bb2, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:51 +- // mir::Constant +- // + span: $DIR/lower_intrinsics.rs:17:14: 17:45 +- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(i32, i32) -> i32 {unchecked_rem::<i32>}, val: Value(<ZST>) } ++ _6 = Rem(move _7, move _8); // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:51 ++ goto -> bb2; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:51 + } + + bb2: { + StorageDead(_8); // scope 1 at $DIR/lower_intrinsics.rs:+2:50: +2:51 + StorageDead(_7); // scope 1 at $DIR/lower_intrinsics.rs:+2:50: +2:51 + _0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:+0:41: +3:2 + StorageDead(_6); // scope 1 at $DIR/lower_intrinsics.rs:+3:1: +3:2 + StorageDead(_3); // scope 0 at $DIR/lower_intrinsics.rs:+3:1: +3:2 + return; // scope 0 at $DIR/lower_intrinsics.rs:+3:2: +3:2 + } + } + diff --git a/tests/mir-opt/lower_intrinsics.unreachable.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.unreachable.LowerIntrinsics.diff index 28e45909c33..83c9c508bc0 100644 --- a/tests/mir-opt/lower_intrinsics.unreachable.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.unreachable.LowerIntrinsics.diff @@ -13,7 +13,7 @@ StorageLive(_2); // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:45 - _2 = std::intrinsics::unreachable() -> unwind unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:45 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:31:14: 31:43 +- // + span: $DIR/lower_intrinsics.rs:37:14: 37:43 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn() -> ! {std::intrinsics::unreachable}, val: Value(<ZST>) } + unreachable; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:45 } diff --git a/tests/mir-opt/lower_intrinsics.with_overflow.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.with_overflow.LowerIntrinsics.diff index 9cf4fbb88f3..4ae4466a600 100644 --- a/tests/mir-opt/lower_intrinsics.with_overflow.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.with_overflow.LowerIntrinsics.diff @@ -32,7 +32,7 @@ _5 = _2; // scope 0 at $DIR/lower_intrinsics.rs:+1:53: +1:54 - _3 = add_with_overflow::<i32>(move _4, move _5) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:112:14: 112:49 +- // + span: $DIR/lower_intrinsics.rs:118:14: 118:49 - // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {add_with_overflow::<i32>}, val: Value(<ZST>) } + _3 = CheckedAdd(move _4, move _5); // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55 + goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55 @@ -48,7 +48,7 @@ _8 = _2; // scope 1 at $DIR/lower_intrinsics.rs:+2:53: +2:54 - _6 = sub_with_overflow::<i32>(move _7, move _8) -> [return: bb2, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:113:14: 113:49 +- // + span: $DIR/lower_intrinsics.rs:119:14: 119:49 - // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {sub_with_overflow::<i32>}, val: Value(<ZST>) } + _6 = CheckedSub(move _7, move _8); // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55 + goto -> bb2; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55 @@ -64,7 +64,7 @@ _11 = _2; // scope 2 at $DIR/lower_intrinsics.rs:+3:53: +3:54 - _9 = mul_with_overflow::<i32>(move _10, move _11) -> [return: bb3, unwind unreachable]; // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:114:14: 114:49 +- // + span: $DIR/lower_intrinsics.rs:120:14: 120:49 - // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {mul_with_overflow::<i32>}, val: Value(<ZST>) } + _9 = CheckedMul(move _10, move _11); // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55 + goto -> bb3; // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55 diff --git a/tests/mir-opt/lower_intrinsics.write_via_move_string.LowerIntrinsics.diff b/tests/mir-opt/lower_intrinsics.write_via_move_string.LowerIntrinsics.diff index 38d99f661dc..2eabd7f626b 100644 --- a/tests/mir-opt/lower_intrinsics.write_via_move_string.LowerIntrinsics.diff +++ b/tests/mir-opt/lower_intrinsics.write_via_move_string.LowerIntrinsics.diff @@ -17,7 +17,7 @@ _4 = move _2; // scope 1 at $DIR/lower_intrinsics.rs:+1:50: +1:51 - _0 = write_via_move::<String>(move _3, move _4) -> [return: bb1, unwind unreachable]; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:52 - // mir::Constant -- // + span: $DIR/lower_intrinsics.rs:129:14: 129:46 +- // + span: $DIR/lower_intrinsics.rs:135:14: 135:46 - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(*mut String, String) {write_via_move::<String>}, val: Value(<ZST>) } + (*_3) = move _4; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:52 + goto -> bb1; // scope 1 at $DIR/lower_intrinsics.rs:+1:14: +1:52 diff --git a/tests/mir-opt/pre-codegen/chained_comparison.bitand.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/chained_comparison.bitand.PreCodegen.after.mir new file mode 100644 index 00000000000..40ddf82f4cf --- /dev/null +++ b/tests/mir-opt/pre-codegen/chained_comparison.bitand.PreCodegen.after.mir @@ -0,0 +1,84 @@ +// MIR for `bitand` after PreCodegen + +fn bitand(_1: &Blueprint, _2: &Blueprint) -> bool { + debug a => _1; // in scope 0 at $DIR/chained_comparison.rs:+0:15: +0:16 + debug b => _2; // in scope 0 at $DIR/chained_comparison.rs:+0:30: +0:31 + let mut _0: bool; // return place in scope 0 at $DIR/chained_comparison.rs:+0:48: +0:52 + let mut _3: u32; // in scope 0 at $DIR/chained_comparison.rs:+1:6: +1:22 + let mut _4: u32; // in scope 0 at $DIR/chained_comparison.rs:+1:26: +1:42 + let mut _5: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +1:43 + let mut _6: u32; // in scope 0 at $DIR/chained_comparison.rs:+2:12: +2:21 + let mut _7: u32; // in scope 0 at $DIR/chained_comparison.rs:+2:25: +2:34 + let mut _8: bool; // in scope 0 at $DIR/chained_comparison.rs:+2:11: +2:35 + let mut _9: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +2:35 + let mut _10: u32; // in scope 0 at $DIR/chained_comparison.rs:+3:12: +3:28 + let mut _11: u32; // in scope 0 at $DIR/chained_comparison.rs:+3:32: +3:48 + let mut _12: bool; // in scope 0 at $DIR/chained_comparison.rs:+3:11: +3:49 + let mut _13: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +3:49 + let mut _14: u32; // in scope 0 at $DIR/chained_comparison.rs:+4:12: +4:25 + let mut _15: u32; // in scope 0 at $DIR/chained_comparison.rs:+4:29: +4:42 + let mut _16: bool; // in scope 0 at $DIR/chained_comparison.rs:+4:11: +4:43 + let mut _17: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +4:43 + let mut _18: u32; // in scope 0 at $DIR/chained_comparison.rs:+5:12: +5:21 + let mut _19: u32; // in scope 0 at $DIR/chained_comparison.rs:+5:25: +5:34 + let mut _20: bool; // in scope 0 at $DIR/chained_comparison.rs:+5:11: +5:35 + + bb0: { + StorageLive(_17); // scope 0 at $DIR/chained_comparison.rs:+1:5: +4:43 + StorageLive(_13); // scope 0 at $DIR/chained_comparison.rs:+1:5: +3:49 + StorageLive(_9); // scope 0 at $DIR/chained_comparison.rs:+1:5: +2:35 + StorageLive(_5); // scope 0 at $DIR/chained_comparison.rs:+1:5: +1:43 + StorageLive(_3); // scope 0 at $DIR/chained_comparison.rs:+1:6: +1:22 + _3 = ((*_1).0: u32); // scope 0 at $DIR/chained_comparison.rs:+1:6: +1:22 + StorageLive(_4); // scope 0 at $DIR/chained_comparison.rs:+1:26: +1:42 + _4 = ((*_2).0: u32); // scope 0 at $DIR/chained_comparison.rs:+1:26: +1:42 + _5 = Eq(move _3, move _4); // scope 0 at $DIR/chained_comparison.rs:+1:5: +1:43 + StorageDead(_4); // scope 0 at $DIR/chained_comparison.rs:+1:42: +1:43 + StorageDead(_3); // scope 0 at $DIR/chained_comparison.rs:+1:42: +1:43 + StorageLive(_8); // scope 0 at $DIR/chained_comparison.rs:+2:11: +2:35 + StorageLive(_6); // scope 0 at $DIR/chained_comparison.rs:+2:12: +2:21 + _6 = ((*_1).1: u32); // scope 0 at $DIR/chained_comparison.rs:+2:12: +2:21 + StorageLive(_7); // scope 0 at $DIR/chained_comparison.rs:+2:25: +2:34 + _7 = ((*_2).1: u32); // scope 0 at $DIR/chained_comparison.rs:+2:25: +2:34 + _8 = Eq(move _6, move _7); // scope 0 at $DIR/chained_comparison.rs:+2:11: +2:35 + StorageDead(_7); // scope 0 at $DIR/chained_comparison.rs:+2:34: +2:35 + StorageDead(_6); // scope 0 at $DIR/chained_comparison.rs:+2:34: +2:35 + _9 = BitAnd(move _5, move _8); // scope 0 at $DIR/chained_comparison.rs:+1:5: +2:35 + StorageDead(_8); // scope 0 at $DIR/chained_comparison.rs:+2:34: +2:35 + StorageDead(_5); // scope 0 at $DIR/chained_comparison.rs:+2:34: +2:35 + StorageLive(_12); // scope 0 at $DIR/chained_comparison.rs:+3:11: +3:49 + StorageLive(_10); // scope 0 at $DIR/chained_comparison.rs:+3:12: +3:28 + _10 = ((*_1).2: u32); // scope 0 at $DIR/chained_comparison.rs:+3:12: +3:28 + StorageLive(_11); // scope 0 at $DIR/chained_comparison.rs:+3:32: +3:48 + _11 = ((*_2).2: u32); // scope 0 at $DIR/chained_comparison.rs:+3:32: +3:48 + _12 = Eq(move _10, move _11); // scope 0 at $DIR/chained_comparison.rs:+3:11: +3:49 + StorageDead(_11); // scope 0 at $DIR/chained_comparison.rs:+3:48: +3:49 + StorageDead(_10); // scope 0 at $DIR/chained_comparison.rs:+3:48: +3:49 + _13 = BitAnd(move _9, move _12); // scope 0 at $DIR/chained_comparison.rs:+1:5: +3:49 + StorageDead(_12); // scope 0 at $DIR/chained_comparison.rs:+3:48: +3:49 + StorageDead(_9); // scope 0 at $DIR/chained_comparison.rs:+3:48: +3:49 + StorageLive(_16); // scope 0 at $DIR/chained_comparison.rs:+4:11: +4:43 + StorageLive(_14); // scope 0 at $DIR/chained_comparison.rs:+4:12: +4:25 + _14 = ((*_1).3: u32); // scope 0 at $DIR/chained_comparison.rs:+4:12: +4:25 + StorageLive(_15); // scope 0 at $DIR/chained_comparison.rs:+4:29: +4:42 + _15 = ((*_2).3: u32); // scope 0 at $DIR/chained_comparison.rs:+4:29: +4:42 + _16 = Eq(move _14, move _15); // scope 0 at $DIR/chained_comparison.rs:+4:11: +4:43 + StorageDead(_15); // scope 0 at $DIR/chained_comparison.rs:+4:42: +4:43 + StorageDead(_14); // scope 0 at $DIR/chained_comparison.rs:+4:42: +4:43 + _17 = BitAnd(move _13, move _16); // scope 0 at $DIR/chained_comparison.rs:+1:5: +4:43 + StorageDead(_16); // scope 0 at $DIR/chained_comparison.rs:+4:42: +4:43 + StorageDead(_13); // scope 0 at $DIR/chained_comparison.rs:+4:42: +4:43 + StorageLive(_20); // scope 0 at $DIR/chained_comparison.rs:+5:11: +5:35 + StorageLive(_18); // scope 0 at $DIR/chained_comparison.rs:+5:12: +5:21 + _18 = ((*_1).4: u32); // scope 0 at $DIR/chained_comparison.rs:+5:12: +5:21 + StorageLive(_19); // scope 0 at $DIR/chained_comparison.rs:+5:25: +5:34 + _19 = ((*_2).4: u32); // scope 0 at $DIR/chained_comparison.rs:+5:25: +5:34 + _20 = Eq(move _18, move _19); // scope 0 at $DIR/chained_comparison.rs:+5:11: +5:35 + StorageDead(_19); // scope 0 at $DIR/chained_comparison.rs:+5:34: +5:35 + StorageDead(_18); // scope 0 at $DIR/chained_comparison.rs:+5:34: +5:35 + _0 = BitAnd(move _17, move _20); // scope 0 at $DIR/chained_comparison.rs:+1:5: +5:35 + StorageDead(_20); // scope 0 at $DIR/chained_comparison.rs:+5:34: +5:35 + StorageDead(_17); // scope 0 at $DIR/chained_comparison.rs:+5:34: +5:35 + return; // scope 0 at $DIR/chained_comparison.rs:+6:2: +6:2 + } +} diff --git a/tests/mir-opt/pre-codegen/chained_comparison.naive.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/chained_comparison.naive.PreCodegen.after.mir new file mode 100644 index 00000000000..26ce7fda4ed --- /dev/null +++ b/tests/mir-opt/pre-codegen/chained_comparison.naive.PreCodegen.after.mir @@ -0,0 +1,127 @@ +// MIR for `naive` after PreCodegen + +fn naive(_1: &Blueprint, _2: &Blueprint) -> bool { + debug a => _1; // in scope 0 at $DIR/chained_comparison.rs:+0:14: +0:15 + debug b => _2; // in scope 0 at $DIR/chained_comparison.rs:+0:29: +0:30 + let mut _0: bool; // return place in scope 0 at $DIR/chained_comparison.rs:+0:47: +0:51 + let mut _3: u32; // in scope 0 at $DIR/chained_comparison.rs:+1:6: +1:22 + let mut _4: u32; // in scope 0 at $DIR/chained_comparison.rs:+1:26: +1:42 + let mut _5: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +1:43 + let mut _6: u32; // in scope 0 at $DIR/chained_comparison.rs:+2:13: +2:22 + let mut _7: u32; // in scope 0 at $DIR/chained_comparison.rs:+2:26: +2:35 + let mut _8: bool; // in scope 0 at $DIR/chained_comparison.rs:+2:12: +2:36 + let mut _9: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +2:36 + let mut _10: u32; // in scope 0 at $DIR/chained_comparison.rs:+3:13: +3:29 + let mut _11: u32; // in scope 0 at $DIR/chained_comparison.rs:+3:33: +3:49 + let mut _12: bool; // in scope 0 at $DIR/chained_comparison.rs:+3:12: +3:50 + let mut _13: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +3:50 + let mut _14: u32; // in scope 0 at $DIR/chained_comparison.rs:+4:13: +4:26 + let mut _15: u32; // in scope 0 at $DIR/chained_comparison.rs:+4:30: +4:43 + let mut _16: bool; // in scope 0 at $DIR/chained_comparison.rs:+4:12: +4:44 + let mut _17: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:5: +4:44 + let mut _18: u32; // in scope 0 at $DIR/chained_comparison.rs:+5:13: +5:22 + let mut _19: u32; // in scope 0 at $DIR/chained_comparison.rs:+5:26: +5:35 + let mut _20: bool; // in scope 0 at $DIR/chained_comparison.rs:+5:12: +5:36 + + bb0: { + StorageLive(_17); // scope 0 at $DIR/chained_comparison.rs:+1:5: +4:44 + StorageLive(_13); // scope 0 at $DIR/chained_comparison.rs:+1:5: +3:50 + StorageLive(_9); // scope 0 at $DIR/chained_comparison.rs:+1:5: +2:36 + StorageLive(_5); // scope 0 at $DIR/chained_comparison.rs:+1:5: +1:43 + StorageLive(_3); // scope 0 at $DIR/chained_comparison.rs:+1:6: +1:22 + _3 = ((*_1).0: u32); // scope 0 at $DIR/chained_comparison.rs:+1:6: +1:22 + StorageLive(_4); // scope 0 at $DIR/chained_comparison.rs:+1:26: +1:42 + _4 = ((*_2).0: u32); // scope 0 at $DIR/chained_comparison.rs:+1:26: +1:42 + _5 = Eq(move _3, move _4); // scope 0 at $DIR/chained_comparison.rs:+1:5: +1:43 + StorageDead(_4); // scope 0 at $DIR/chained_comparison.rs:+1:42: +1:43 + StorageDead(_3); // scope 0 at $DIR/chained_comparison.rs:+1:42: +1:43 + switchInt(move _5) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/chained_comparison.rs:+1:5: +2:36 + } + + bb1: { + StorageDead(_8); // scope 0 at $DIR/chained_comparison.rs:+2:35: +2:36 + StorageDead(_5); // scope 0 at $DIR/chained_comparison.rs:+2:35: +2:36 + goto -> bb3; // scope 0 at $DIR/chained_comparison.rs:+1:5: +2:36 + } + + bb2: { + StorageLive(_8); // scope 0 at $DIR/chained_comparison.rs:+2:12: +2:36 + StorageLive(_6); // scope 0 at $DIR/chained_comparison.rs:+2:13: +2:22 + _6 = ((*_1).1: u32); // scope 0 at $DIR/chained_comparison.rs:+2:13: +2:22 + StorageLive(_7); // scope 0 at $DIR/chained_comparison.rs:+2:26: +2:35 + _7 = ((*_2).1: u32); // scope 0 at $DIR/chained_comparison.rs:+2:26: +2:35 + _8 = Eq(move _6, move _7); // scope 0 at $DIR/chained_comparison.rs:+2:12: +2:36 + StorageDead(_7); // scope 0 at $DIR/chained_comparison.rs:+2:35: +2:36 + StorageDead(_6); // scope 0 at $DIR/chained_comparison.rs:+2:35: +2:36 + _9 = move _8; // scope 0 at $DIR/chained_comparison.rs:+1:5: +2:36 + StorageDead(_8); // scope 0 at $DIR/chained_comparison.rs:+2:35: +2:36 + StorageDead(_5); // scope 0 at $DIR/chained_comparison.rs:+2:35: +2:36 + switchInt(move _9) -> [0: bb3, otherwise: bb4]; // scope 0 at $DIR/chained_comparison.rs:+1:5: +3:50 + } + + bb3: { + StorageDead(_12); // scope 0 at $DIR/chained_comparison.rs:+3:49: +3:50 + StorageDead(_9); // scope 0 at $DIR/chained_comparison.rs:+3:49: +3:50 + goto -> bb5; // scope 0 at $DIR/chained_comparison.rs:+1:5: +3:50 + } + + bb4: { + StorageLive(_12); // scope 0 at $DIR/chained_comparison.rs:+3:12: +3:50 + StorageLive(_10); // scope 0 at $DIR/chained_comparison.rs:+3:13: +3:29 + _10 = ((*_1).2: u32); // scope 0 at $DIR/chained_comparison.rs:+3:13: +3:29 + StorageLive(_11); // scope 0 at $DIR/chained_comparison.rs:+3:33: +3:49 + _11 = ((*_2).2: u32); // scope 0 at $DIR/chained_comparison.rs:+3:33: +3:49 + _12 = Eq(move _10, move _11); // scope 0 at $DIR/chained_comparison.rs:+3:12: +3:50 + StorageDead(_11); // scope 0 at $DIR/chained_comparison.rs:+3:49: +3:50 + StorageDead(_10); // scope 0 at $DIR/chained_comparison.rs:+3:49: +3:50 + _13 = move _12; // scope 0 at $DIR/chained_comparison.rs:+1:5: +3:50 + StorageDead(_12); // scope 0 at $DIR/chained_comparison.rs:+3:49: +3:50 + StorageDead(_9); // scope 0 at $DIR/chained_comparison.rs:+3:49: +3:50 + switchInt(move _13) -> [0: bb5, otherwise: bb6]; // scope 0 at $DIR/chained_comparison.rs:+1:5: +4:44 + } + + bb5: { + StorageDead(_16); // scope 0 at $DIR/chained_comparison.rs:+4:43: +4:44 + StorageDead(_13); // scope 0 at $DIR/chained_comparison.rs:+4:43: +4:44 + goto -> bb7; // scope 0 at $DIR/chained_comparison.rs:+1:5: +4:44 + } + + bb6: { + StorageLive(_16); // scope 0 at $DIR/chained_comparison.rs:+4:12: +4:44 + StorageLive(_14); // scope 0 at $DIR/chained_comparison.rs:+4:13: +4:26 + _14 = ((*_1).3: u32); // scope 0 at $DIR/chained_comparison.rs:+4:13: +4:26 + StorageLive(_15); // scope 0 at $DIR/chained_comparison.rs:+4:30: +4:43 + _15 = ((*_2).3: u32); // scope 0 at $DIR/chained_comparison.rs:+4:30: +4:43 + _16 = Eq(move _14, move _15); // scope 0 at $DIR/chained_comparison.rs:+4:12: +4:44 + StorageDead(_15); // scope 0 at $DIR/chained_comparison.rs:+4:43: +4:44 + StorageDead(_14); // scope 0 at $DIR/chained_comparison.rs:+4:43: +4:44 + _17 = move _16; // scope 0 at $DIR/chained_comparison.rs:+1:5: +4:44 + StorageDead(_16); // scope 0 at $DIR/chained_comparison.rs:+4:43: +4:44 + StorageDead(_13); // scope 0 at $DIR/chained_comparison.rs:+4:43: +4:44 + switchInt(move _17) -> [0: bb7, otherwise: bb8]; // scope 0 at $DIR/chained_comparison.rs:+1:5: +5:36 + } + + bb7: { + _0 = const false; // scope 0 at $DIR/chained_comparison.rs:+1:5: +5:36 + goto -> bb9; // scope 0 at $DIR/chained_comparison.rs:+1:5: +5:36 + } + + bb8: { + StorageLive(_20); // scope 0 at $DIR/chained_comparison.rs:+5:12: +5:36 + StorageLive(_18); // scope 0 at $DIR/chained_comparison.rs:+5:13: +5:22 + _18 = ((*_1).4: u32); // scope 0 at $DIR/chained_comparison.rs:+5:13: +5:22 + StorageLive(_19); // scope 0 at $DIR/chained_comparison.rs:+5:26: +5:35 + _19 = ((*_2).4: u32); // scope 0 at $DIR/chained_comparison.rs:+5:26: +5:35 + _20 = Eq(move _18, move _19); // scope 0 at $DIR/chained_comparison.rs:+5:12: +5:36 + StorageDead(_19); // scope 0 at $DIR/chained_comparison.rs:+5:35: +5:36 + StorageDead(_18); // scope 0 at $DIR/chained_comparison.rs:+5:35: +5:36 + _0 = move _20; // scope 0 at $DIR/chained_comparison.rs:+1:5: +5:36 + goto -> bb9; // scope 0 at $DIR/chained_comparison.rs:+1:5: +5:36 + } + + bb9: { + StorageDead(_20); // scope 0 at $DIR/chained_comparison.rs:+5:35: +5:36 + StorageDead(_17); // scope 0 at $DIR/chained_comparison.rs:+5:35: +5:36 + return; // scope 0 at $DIR/chained_comparison.rs:+6:2: +6:2 + } +} diff --git a/tests/mir-opt/pre-codegen/chained_comparison.returning.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/chained_comparison.returning.PreCodegen.after.mir new file mode 100644 index 00000000000..7560e1e26ce --- /dev/null +++ b/tests/mir-opt/pre-codegen/chained_comparison.returning.PreCodegen.after.mir @@ -0,0 +1,126 @@ +// MIR for `returning` after PreCodegen + +fn returning(_1: &Blueprint, _2: &Blueprint) -> bool { + debug a => _1; // in scope 0 at $DIR/chained_comparison.rs:+0:18: +0:19 + debug b => _2; // in scope 0 at $DIR/chained_comparison.rs:+0:33: +0:34 + let mut _0: bool; // return place in scope 0 at $DIR/chained_comparison.rs:+0:51: +0:55 + let mut _3: u32; // in scope 0 at $DIR/chained_comparison.rs:+1:8: +1:24 + let mut _4: u32; // in scope 0 at $DIR/chained_comparison.rs:+1:28: +1:44 + let mut _5: bool; // in scope 0 at $DIR/chained_comparison.rs:+1:8: +1:44 + let mut _6: u32; // in scope 0 at $DIR/chained_comparison.rs:+4:8: +4:17 + let mut _7: u32; // in scope 0 at $DIR/chained_comparison.rs:+4:21: +4:30 + let mut _8: bool; // in scope 0 at $DIR/chained_comparison.rs:+4:8: +4:30 + let mut _9: u32; // in scope 0 at $DIR/chained_comparison.rs:+7:8: +7:24 + let mut _10: u32; // in scope 0 at $DIR/chained_comparison.rs:+7:28: +7:44 + let mut _11: bool; // in scope 0 at $DIR/chained_comparison.rs:+7:8: +7:44 + let mut _12: u32; // in scope 0 at $DIR/chained_comparison.rs:+10:8: +10:21 + let mut _13: u32; // in scope 0 at $DIR/chained_comparison.rs:+10:25: +10:38 + let mut _14: bool; // in scope 0 at $DIR/chained_comparison.rs:+10:8: +10:38 + let mut _15: u32; // in scope 0 at $DIR/chained_comparison.rs:+13:8: +13:17 + let mut _16: u32; // in scope 0 at $DIR/chained_comparison.rs:+13:21: +13:30 + let mut _17: bool; // in scope 0 at $DIR/chained_comparison.rs:+13:8: +13:30 + + bb0: { + StorageLive(_5); // scope 0 at $DIR/chained_comparison.rs:+1:8: +1:44 + StorageLive(_3); // scope 0 at $DIR/chained_comparison.rs:+1:8: +1:24 + _3 = ((*_1).0: u32); // scope 0 at $DIR/chained_comparison.rs:+1:8: +1:24 + StorageLive(_4); // scope 0 at $DIR/chained_comparison.rs:+1:28: +1:44 + _4 = ((*_2).0: u32); // scope 0 at $DIR/chained_comparison.rs:+1:28: +1:44 + _5 = Ne(move _3, move _4); // scope 0 at $DIR/chained_comparison.rs:+1:8: +1:44 + StorageDead(_4); // scope 0 at $DIR/chained_comparison.rs:+1:43: +1:44 + StorageDead(_3); // scope 0 at $DIR/chained_comparison.rs:+1:43: +1:44 + switchInt(move _5) -> [0: bb1, otherwise: bb10]; // scope 0 at $DIR/chained_comparison.rs:+1:8: +1:44 + } + + bb1: { + StorageDead(_5); // scope 0 at $DIR/chained_comparison.rs:+3:5: +3:6 + StorageLive(_8); // scope 0 at $DIR/chained_comparison.rs:+4:8: +4:30 + StorageLive(_6); // scope 0 at $DIR/chained_comparison.rs:+4:8: +4:17 + _6 = ((*_1).1: u32); // scope 0 at $DIR/chained_comparison.rs:+4:8: +4:17 + StorageLive(_7); // scope 0 at $DIR/chained_comparison.rs:+4:21: +4:30 + _7 = ((*_2).1: u32); // scope 0 at $DIR/chained_comparison.rs:+4:21: +4:30 + _8 = Ne(move _6, move _7); // scope 0 at $DIR/chained_comparison.rs:+4:8: +4:30 + StorageDead(_7); // scope 0 at $DIR/chained_comparison.rs:+4:29: +4:30 + StorageDead(_6); // scope 0 at $DIR/chained_comparison.rs:+4:29: +4:30 + switchInt(move _8) -> [0: bb2, otherwise: bb9]; // scope 0 at $DIR/chained_comparison.rs:+4:8: +4:30 + } + + bb2: { + StorageDead(_8); // scope 0 at $DIR/chained_comparison.rs:+6:5: +6:6 + StorageLive(_11); // scope 0 at $DIR/chained_comparison.rs:+7:8: +7:44 + StorageLive(_9); // scope 0 at $DIR/chained_comparison.rs:+7:8: +7:24 + _9 = ((*_1).2: u32); // scope 0 at $DIR/chained_comparison.rs:+7:8: +7:24 + StorageLive(_10); // scope 0 at $DIR/chained_comparison.rs:+7:28: +7:44 + _10 = ((*_2).2: u32); // scope 0 at $DIR/chained_comparison.rs:+7:28: +7:44 + _11 = Ne(move _9, move _10); // scope 0 at $DIR/chained_comparison.rs:+7:8: +7:44 + StorageDead(_10); // scope 0 at $DIR/chained_comparison.rs:+7:43: +7:44 + StorageDead(_9); // scope 0 at $DIR/chained_comparison.rs:+7:43: +7:44 + switchInt(move _11) -> [0: bb3, otherwise: bb8]; // scope 0 at $DIR/chained_comparison.rs:+7:8: +7:44 + } + + bb3: { + StorageDead(_11); // scope 0 at $DIR/chained_comparison.rs:+9:5: +9:6 + StorageLive(_14); // scope 0 at $DIR/chained_comparison.rs:+10:8: +10:38 + StorageLive(_12); // scope 0 at $DIR/chained_comparison.rs:+10:8: +10:21 + _12 = ((*_1).3: u32); // scope 0 at $DIR/chained_comparison.rs:+10:8: +10:21 + StorageLive(_13); // scope 0 at $DIR/chained_comparison.rs:+10:25: +10:38 + _13 = ((*_2).3: u32); // scope 0 at $DIR/chained_comparison.rs:+10:25: +10:38 + _14 = Ne(move _12, move _13); // scope 0 at $DIR/chained_comparison.rs:+10:8: +10:38 + StorageDead(_13); // scope 0 at $DIR/chained_comparison.rs:+10:37: +10:38 + StorageDead(_12); // scope 0 at $DIR/chained_comparison.rs:+10:37: +10:38 + switchInt(move _14) -> [0: bb4, otherwise: bb7]; // scope 0 at $DIR/chained_comparison.rs:+10:8: +10:38 + } + + bb4: { + StorageDead(_14); // scope 0 at $DIR/chained_comparison.rs:+12:5: +12:6 + StorageLive(_17); // scope 0 at $DIR/chained_comparison.rs:+13:8: +13:30 + StorageLive(_15); // scope 0 at $DIR/chained_comparison.rs:+13:8: +13:17 + _15 = ((*_1).4: u32); // scope 0 at $DIR/chained_comparison.rs:+13:8: +13:17 + StorageLive(_16); // scope 0 at $DIR/chained_comparison.rs:+13:21: +13:30 + _16 = ((*_2).4: u32); // scope 0 at $DIR/chained_comparison.rs:+13:21: +13:30 + _17 = Ne(move _15, move _16); // scope 0 at $DIR/chained_comparison.rs:+13:8: +13:30 + StorageDead(_16); // scope 0 at $DIR/chained_comparison.rs:+13:29: +13:30 + StorageDead(_15); // scope 0 at $DIR/chained_comparison.rs:+13:29: +13:30 + switchInt(move _17) -> [0: bb5, otherwise: bb6]; // scope 0 at $DIR/chained_comparison.rs:+13:8: +13:30 + } + + bb5: { + StorageDead(_17); // scope 0 at $DIR/chained_comparison.rs:+15:5: +15:6 + _0 = const true; // scope 0 at $DIR/chained_comparison.rs:+16:5: +16:9 + goto -> bb11; // scope 0 at $DIR/chained_comparison.rs:+17:2: +17:2 + } + + bb6: { + _0 = const false; // scope 0 at $DIR/chained_comparison.rs:+14:16: +14:21 + StorageDead(_17); // scope 0 at $DIR/chained_comparison.rs:+15:5: +15:6 + goto -> bb11; // scope 0 at no-location + } + + bb7: { + _0 = const false; // scope 0 at $DIR/chained_comparison.rs:+11:16: +11:21 + StorageDead(_14); // scope 0 at $DIR/chained_comparison.rs:+12:5: +12:6 + goto -> bb11; // scope 0 at no-location + } + + bb8: { + _0 = const false; // scope 0 at $DIR/chained_comparison.rs:+8:16: +8:21 + StorageDead(_11); // scope 0 at $DIR/chained_comparison.rs:+9:5: +9:6 + goto -> bb11; // scope 0 at no-location + } + + bb9: { + _0 = const false; // scope 0 at $DIR/chained_comparison.rs:+5:16: +5:21 + StorageDead(_8); // scope 0 at $DIR/chained_comparison.rs:+6:5: +6:6 + goto -> bb11; // scope 0 at no-location + } + + bb10: { + _0 = const false; // scope 0 at $DIR/chained_comparison.rs:+2:16: +2:21 + StorageDead(_5); // scope 0 at $DIR/chained_comparison.rs:+3:5: +3:6 + goto -> bb11; // scope 0 at no-location + } + + bb11: { + return; // scope 0 at $DIR/chained_comparison.rs:+17:2: +17:2 + } +} diff --git a/tests/mir-opt/pre-codegen/chained_comparison.rs b/tests/mir-opt/pre-codegen/chained_comparison.rs new file mode 100644 index 00000000000..f7879140f81 --- /dev/null +++ b/tests/mir-opt/pre-codegen/chained_comparison.rs @@ -0,0 +1,51 @@ +// compile-flags: -O -Zmir-opt-level=2 -Cdebuginfo=2 +// ignore-debug + +#![crate_type = "lib"] + +pub struct Blueprint { + pub fuel_tank_size: u32, + pub payload: u32, + pub wheel_diameter: u32, + pub wheel_width: u32, + pub storage: u32, +} + +pub fn naive(a: &Blueprint, b: &Blueprint) -> bool { + (a.fuel_tank_size == b.fuel_tank_size) + && (a.payload == b.payload) + && (a.wheel_diameter == b.wheel_diameter) + && (a.wheel_width == b.wheel_width) + && (a.storage == b.storage) +} + +pub fn bitand(a: &Blueprint, b: &Blueprint) -> bool { + (a.fuel_tank_size == b.fuel_tank_size) + & (a.payload == b.payload) + & (a.wheel_diameter == b.wheel_diameter) + & (a.wheel_width == b.wheel_width) + & (a.storage == b.storage) +} + +pub fn returning(a: &Blueprint, b: &Blueprint) -> bool { + if a.fuel_tank_size != b.fuel_tank_size { + return false; + } + if a.payload != b.payload { + return false; + } + if a.wheel_diameter != b.wheel_diameter { + return false; + } + if a.wheel_width != b.wheel_width { + return false; + } + if a.storage != b.storage { + return false; + } + true +} + +// EMIT_MIR chained_comparison.naive.PreCodegen.after.mir +// EMIT_MIR chained_comparison.bitand.PreCodegen.after.mir +// EMIT_MIR chained_comparison.returning.PreCodegen.after.mir diff --git a/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir new file mode 100644 index 00000000000..dff3cbbe76d --- /dev/null +++ b/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir @@ -0,0 +1,144 @@ +// MIR for `checked_shl` after PreCodegen + +fn checked_shl(_1: u32, _2: u32) -> Option<u32> { + debug x => _1; // in scope 0 at $DIR/checked_ops.rs:+0:20: +0:21 + debug rhs => _2; // in scope 0 at $DIR/checked_ops.rs:+0:28: +0:31 + let mut _0: std::option::Option<u32>; // return place in scope 0 at $DIR/checked_ops.rs:+0:41: +0:52 + scope 1 (inlined core::num::<impl u32>::checked_shl) { // at $DIR/checked_ops.rs:16:7: 16:23 + debug self => _1; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug rhs => _2; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _11: u32; // in scope 1 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _12: bool; // in scope 1 at $SRC_DIR/core/src/num/mod.rs:LL:COL + scope 2 { + debug a => _11; // in scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug b => _10; // in scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + } + scope 3 (inlined core::num::<impl u32>::overflowing_shl) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug self => _1; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug rhs => _2; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _9: u32; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _10: bool; // in scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + scope 4 (inlined core::num::<impl u32>::wrapping_shl) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug self => _1; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug rhs => _2; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _3: u32; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _4: u32; // in scope 4 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + scope 5 { + scope 6 (inlined core::num::<impl u32>::unchecked_shl) { // at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug self => _1; // in scope 6 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug rhs => _4; // in scope 6 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + let mut _8: u32; // in scope 6 at $SRC_DIR/core/src/num/mod.rs:LL:COL + scope 7 { + scope 8 (inlined core::num::<impl u32>::unchecked_shl::conv) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL + debug x => _4; // in scope 8 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _5: std::result::Result<u32, std::convert::Infallible>; // in scope 8 at $SRC_DIR/core/src/num/mod.rs:LL:COL + let mut _7: std::option::Option<u32>; // in scope 8 at $SRC_DIR/core/src/num/mod.rs:LL:COL + scope 9 { + scope 10 (inlined <u32 as TryInto<u32>>::try_into) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL + debug self => _4; // in scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + scope 11 (inlined <u32 as TryFrom<u32>>::try_from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL + debug value => _4; // in scope 11 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + scope 12 (inlined <u32 as Into<u32>>::into) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL + debug self => _4; // in scope 12 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + scope 13 (inlined <u32 as From<u32>>::from) { // at $SRC_DIR/core/src/convert/mod.rs:LL:COL + debug t => _4; // in scope 13 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + } + } + } + } + scope 14 (inlined Result::<u32, Infallible>::ok) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL + debug self => _5; // in scope 14 at $SRC_DIR/core/src/result.rs:LL:COL + let _6: u32; // in scope 14 at $SRC_DIR/core/src/result.rs:LL:COL + scope 15 { + debug x => _6; // in scope 15 at $SRC_DIR/core/src/result.rs:LL:COL + } + } + scope 16 (inlined #[track_caller] Option::<u32>::unwrap_unchecked) { // at $SRC_DIR/core/src/num/mod.rs:LL:COL + debug self => _7; // in scope 16 at $SRC_DIR/core/src/option.rs:LL:COL + let mut _13: &std::option::Option<u32>; // in scope 16 at $SRC_DIR/core/src/option.rs:LL:COL + scope 17 { + debug val => _8; // in scope 17 at $SRC_DIR/core/src/option.rs:LL:COL + } + scope 18 { + scope 20 (inlined unreachable_unchecked) { // at $SRC_DIR/core/src/option.rs:LL:COL + scope 21 { + scope 22 (inlined unreachable_unchecked::runtime) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL + } + } + } + } + scope 19 (inlined Option::<u32>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL + debug self => _13; // in scope 19 at $SRC_DIR/core/src/option.rs:LL:COL + } + } + } + } + } + } + } + } + } + } + + bb0: { + StorageLive(_10); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23 + StorageLive(_11); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23 + StorageLive(_9); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageLive(_4); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageLive(_3); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + _3 = const 31_u32; // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + _4 = BitAnd(_2, move _3); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageDead(_3); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageLive(_8); // scope 7 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_7); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_5); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _5 = Result::<u32, Infallible>::Ok(_4); // scope 11 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + StorageLive(_6); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _6 = move ((_5 as Ok).0: u32); // scope 14 at $SRC_DIR/core/src/result.rs:LL:COL + _7 = Option::<u32>::Some(move _6); // scope 15 at $SRC_DIR/core/src/result.rs:LL:COL + StorageDead(_6); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageDead(_5); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageLive(_13); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _8 = move ((_7 as Some).0: u32); // scope 16 at $SRC_DIR/core/src/option.rs:LL:COL + StorageDead(_13); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + StorageDead(_7); // scope 9 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _9 = unchecked_shl::<u32>(_1, move _8) -> [return: bb1, unwind unreachable]; // scope 7 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(u32, u32) -> u32 {unchecked_shl::<u32>}, val: Value(<ZST>) } + } + + bb1: { + StorageDead(_8); // scope 7 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageDead(_4); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + _10 = Ge(_2, const _); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + _11 = move _9; // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageDead(_9); // scope 3 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageLive(_12); // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL + _12 = unlikely(_10) -> [return: bb2, unwind unreachable]; // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/num/mod.rs:LL:COL + // + literal: Const { ty: extern "rust-intrinsic" fn(bool) -> bool {unlikely}, val: Value(<ZST>) } + } + + bb2: { + switchInt(move _12) -> [0: bb3, otherwise: bb4]; // scope 2 at $SRC_DIR/core/src/num/mod.rs:LL:COL + } + + bb3: { + _0 = Option::<u32>::Some(_11); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + goto -> bb5; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + } + + bb4: { + _0 = Option::<u32>::None; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + goto -> bb5; // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + } + + bb5: { + StorageDead(_12); // scope 2 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageDead(_11); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23 + StorageDead(_10); // scope 0 at $DIR/checked_ops.rs:+1:7: +1:23 + return; // scope 0 at $DIR/checked_ops.rs:+2:2: +2:2 + } +} diff --git a/tests/mir-opt/pre-codegen/checked_ops.rs b/tests/mir-opt/pre-codegen/checked_ops.rs new file mode 100644 index 00000000000..dee43b0c6f8 --- /dev/null +++ b/tests/mir-opt/pre-codegen/checked_ops.rs @@ -0,0 +1,17 @@ +// compile-flags: -O -Zmir-opt-level=2 -Cdebuginfo=2 +// needs-unwind +// ignore-debug +// only-x86_64 + +#![crate_type = "lib"] +#![feature(step_trait)] + +// EMIT_MIR checked_ops.step_forward.PreCodegen.after.mir +pub fn step_forward(x: u32, n: usize) -> u32 { + std::iter::Step::forward(x, n) +} + +// EMIT_MIR checked_ops.checked_shl.PreCodegen.after.mir +pub fn checked_shl(x: u32, rhs: u32) -> Option<u32> { + x.checked_shl(rhs) +} diff --git a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir new file mode 100644 index 00000000000..78f68e8ffe6 --- /dev/null +++ b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir @@ -0,0 +1,64 @@ +// MIR for `step_forward` after PreCodegen + +fn step_forward(_1: u32, _2: usize) -> u32 { + debug x => _1; // in scope 0 at $DIR/checked_ops.rs:+0:21: +0:22 + debug n => _2; // in scope 0 at $DIR/checked_ops.rs:+0:29: +0:30 + let mut _0: u32; // return place in scope 0 at $DIR/checked_ops.rs:+0:42: +0:45 + scope 1 (inlined <u32 as Step>::forward) { // at $DIR/checked_ops.rs:11:5: 11:35 + debug start => _1; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug n => _2; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let _3: std::option::Option<u32>; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _4: &std::option::Option<u32>; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _7: bool; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _8: u32; // in scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + scope 2 { + } + scope 3 (inlined Option::<u32>::is_none) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _4; // in scope 3 at $SRC_DIR/core/src/option.rs:LL:COL + let mut _6: bool; // in scope 3 at $SRC_DIR/core/src/option.rs:LL:COL + scope 4 (inlined Option::<u32>::is_some) { // at $SRC_DIR/core/src/option.rs:LL:COL + debug self => _4; // in scope 4 at $SRC_DIR/core/src/option.rs:LL:COL + let mut _5: isize; // in scope 4 at $SRC_DIR/core/src/option.rs:LL:COL + } + } + scope 5 (inlined core::num::<impl u32>::wrapping_add) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _1; // in scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + debug rhs => _8; // in scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + } + } + + bb0: { + StorageLive(_7); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_4); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_3); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _3 = <u32 as Step>::forward_checked(_1, _2) -> bb1; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL + // + literal: Const { ty: fn(u32, usize) -> Option<u32> {<u32 as Step>::forward_checked}, val: Value(<ZST>) } + } + + bb1: { + _4 = &_3; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_6); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL + _5 = discriminant((*_4)); // scope 4 at $SRC_DIR/core/src/option.rs:LL:COL + _6 = Eq(_5, const 1_isize); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _7 = Not(move _6); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL + StorageDead(_6); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL + StorageDead(_3); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_4); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + switchInt(move _7) -> [0: bb3, otherwise: bb2]; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb2: { + assert(!const true, "attempt to compute `{} + {}`, which would overflow", const _, const 1_u32) -> bb3; // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb3: { + StorageDead(_7); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_8); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _8 = _2 as u32 (IntToInt); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _0 = Add(_1, _8); // scope 5 at $SRC_DIR/core/src/num/uint_macros.rs:LL:COL + StorageDead(_8); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + return; // scope 0 at $DIR/checked_ops.rs:+2:2: +2:2 + } +} diff --git a/tests/mir-opt/pre-codegen/loops.filter_mapped.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/loops.filter_mapped.PreCodegen.after.mir new file mode 100644 index 00000000000..dce9feddfc5 --- /dev/null +++ b/tests/mir-opt/pre-codegen/loops.filter_mapped.PreCodegen.after.mir @@ -0,0 +1,102 @@ +// MIR for `filter_mapped` after PreCodegen + +fn filter_mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> Option<U>) -> () { + debug iter => _1; // in scope 0 at $DIR/loops.rs:+0:28: +0:32 + debug f => _2; // in scope 0 at $DIR/loops.rs:+0:59: +0:60 + let mut _0: (); // return place in scope 0 at $DIR/loops.rs:+0:87: +0:87 + let mut _3: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32 + let mut _4: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32 + let mut _5: std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32 + let mut _6: &mut std::iter::FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32 + let mut _9: std::option::Option<U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:32 + let mut _10: isize; // in scope 0 at $DIR/loops.rs:+1:5: +3:6 + let _12: (); // in scope 0 at $DIR/loops.rs:+1:14: +1:32 + scope 1 { + debug iter => _5; // in scope 1 at $DIR/loops.rs:+1:14: +1:32 + let _11: U; // in scope 1 at $DIR/loops.rs:+1:9: +1:10 + scope 2 { + debug x => _11; // in scope 2 at $DIR/loops.rs:+1:9: +1:10 + } + scope 4 (inlined <FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> as Iterator>::next) { // at $DIR/loops.rs:20:14: 20:32 + debug self => _6; // in scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + let mut _7: &mut impl Iterator<Item = T>; // in scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + let mut _8: &mut impl Fn(T) -> Option<U>; // in scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + } + } + scope 3 (inlined <FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> as IntoIterator>::into_iter) { // at $DIR/loops.rs:20:14: 20:32 + debug self => _3; // in scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + } + + bb0: { + StorageLive(_4); // scope 0 at $DIR/loops.rs:+1:14: +1:32 + StorageLive(_3); // scope 0 at $DIR/loops.rs:+1:14: +1:32 + _3 = <impl Iterator<Item = T> as Iterator>::filter_map::<U, impl Fn(T) -> Option<U>>(move _1, move _2) -> bb1; // scope 0 at $DIR/loops.rs:+1:14: +1:32 + // mir::Constant + // + span: $DIR/loops.rs:20:19: 20:29 + // + literal: Const { ty: fn(impl Iterator<Item = T>, impl Fn(T) -> Option<U>) -> FilterMap<impl Iterator<Item = T>, impl Fn(T) -> Option<U>> {<impl Iterator<Item = T> as Iterator>::filter_map::<U, impl Fn(T) -> Option<U>>}, val: Value(<ZST>) } + } + + bb1: { + _4 = move _3; // scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + StorageDead(_3); // scope 0 at $DIR/loops.rs:+1:31: +1:32 + StorageLive(_5); // scope 0 at $DIR/loops.rs:+1:14: +1:32 + _5 = move _4; // scope 0 at $DIR/loops.rs:+1:14: +1:32 + goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb2: { + StorageLive(_9); // scope 1 at $DIR/loops.rs:+1:14: +1:32 + _6 = &mut _5; // scope 1 at $DIR/loops.rs:+1:14: +1:32 + StorageLive(_7); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + _7 = &mut ((*_6).0: impl Iterator<Item = T>); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + StorageLive(_8); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + _8 = &mut ((*_6).1: impl Fn(T) -> Option<U>); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + _9 = <impl Iterator<Item = T> as Iterator>::find_map::<U, &mut impl Fn(T) -> Option<U>>(move _7, move _8) -> [return: bb3, unwind: bb9]; // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + // + literal: Const { ty: for<'a> fn(&'a mut impl Iterator<Item = T>, &mut impl Fn(T) -> Option<U>) -> Option<U> {<impl Iterator<Item = T> as Iterator>::find_map::<U, &mut impl Fn(T) -> Option<U>>}, val: Value(<ZST>) } + } + + bb3: { + StorageDead(_8); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + StorageDead(_7); // scope 4 at $SRC_DIR/core/src/iter/adapters/filter_map.rs:LL:COL + _10 = discriminant(_9); // scope 1 at $DIR/loops.rs:+1:14: +1:32 + switchInt(move _10) -> [0: bb4, 1: bb6, otherwise: bb8]; // scope 1 at $DIR/loops.rs:+1:14: +1:32 + } + + bb4: { + StorageDead(_9); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + drop(_5) -> bb5; // scope 0 at $DIR/loops.rs:+3:5: +3:6 + } + + bb5: { + StorageDead(_5); // scope 0 at $DIR/loops.rs:+3:5: +3:6 + StorageDead(_4); // scope 0 at $DIR/loops.rs:+3:5: +3:6 + return; // scope 0 at $DIR/loops.rs:+4:2: +4:2 + } + + bb6: { + _11 = move ((_9 as Some).0: U); // scope 1 at $DIR/loops.rs:+1:9: +1:10 + _12 = opaque::<U>(move _11) -> [return: bb7, unwind: bb9]; // scope 2 at $DIR/loops.rs:+2:9: +2:18 + // mir::Constant + // + span: $DIR/loops.rs:21:9: 21:15 + // + literal: Const { ty: fn(U) {opaque::<U>}, val: Value(<ZST>) } + } + + bb7: { + StorageDead(_9); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb8: { + unreachable; // scope 1 at $DIR/loops.rs:+1:14: +1:32 + } + + bb9 (cleanup): { + drop(_5) -> [return: bb10, unwind terminate]; // scope 0 at $DIR/loops.rs:+3:5: +3:6 + } + + bb10 (cleanup): { + resume; // scope 0 at $DIR/loops.rs:+0:1: +4:2 + } +} diff --git a/tests/mir-opt/pre-codegen/loops.int_range.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/loops.int_range.PreCodegen.after.mir new file mode 100644 index 00000000000..86e0a62b6f9 --- /dev/null +++ b/tests/mir-opt/pre-codegen/loops.int_range.PreCodegen.after.mir @@ -0,0 +1,124 @@ +// MIR for `int_range` after PreCodegen + +fn int_range(_1: usize, _2: usize) -> () { + debug start => _1; // in scope 0 at $DIR/loops.rs:+0:18: +0:23 + debug end => _2; // in scope 0 at $DIR/loops.rs:+0:32: +0:35 + let mut _0: (); // return place in scope 0 at $DIR/loops.rs:+0:44: +0:44 + let mut _3: std::ops::Range<usize>; // in scope 0 at $DIR/loops.rs:+1:14: +1:24 + let mut _4: std::ops::Range<usize>; // in scope 0 at $DIR/loops.rs:+1:14: +1:24 + let mut _5: &mut std::ops::Range<usize>; // in scope 0 at $DIR/loops.rs:+1:14: +1:24 + let mut _11: std::option::Option<usize>; // in scope 0 at $DIR/loops.rs:+1:14: +1:24 + let mut _14: isize; // in scope 0 at $DIR/loops.rs:+1:5: +3:6 + let _16: (); // in scope 0 at $DIR/loops.rs:+1:14: +1:24 + scope 1 { + debug iter => _4; // in scope 1 at $DIR/loops.rs:+1:14: +1:24 + let _15: usize; // in scope 1 at $DIR/loops.rs:+1:9: +1:10 + scope 2 { + debug i => _15; // in scope 2 at $DIR/loops.rs:+1:9: +1:10 + } + scope 4 (inlined iter::range::<impl Iterator for std::ops::Range<usize>>::next) { // at $DIR/loops.rs:8:14: 8:24 + debug self => _5; // in scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL + scope 5 (inlined <std::ops::Range<usize> as iter::range::RangeIteratorImpl>::spec_next) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _5; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _6: &usize; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _7: &usize; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _10: bool; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let _12: usize; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _13: usize; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + scope 6 { + debug old => _12; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + scope 7 { + } + } + scope 8 (inlined cmp::impls::<impl PartialOrd for usize>::lt) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _6; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _7; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _8: usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _9: usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + } + } + } + } + scope 3 (inlined <std::ops::Range<usize> as IntoIterator>::into_iter) { // at $DIR/loops.rs:8:14: 8:24 + debug self => _3; // in scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + } + + bb0: { + _3 = std::ops::Range::<usize> { start: _1, end: _2 }; // scope 0 at $DIR/loops.rs:+1:14: +1:24 + StorageLive(_4); // scope 0 at $DIR/loops.rs:+1:14: +1:24 + _4 = move _3; // scope 0 at $DIR/loops.rs:+1:14: +1:24 + goto -> bb1; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb1: { + StorageLive(_11); // scope 1 at $DIR/loops.rs:+1:14: +1:24 + _5 = &mut _4; // scope 1 at $DIR/loops.rs:+1:14: +1:24 + StorageLive(_12); // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_10); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_6); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _6 = &((*_5).0: usize); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_7); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _7 = &((*_5).1: usize); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_8); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + _8 = (*_6); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_9); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + _9 = (*_7); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + _10 = Lt(move _8, move _9); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_9); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_8); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_7); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_6); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + switchInt(move _10) -> [0: bb2, otherwise: bb3]; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb2: { + _11 = Option::<usize>::None; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb5; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb3: { + _12 = ((*_5).0: usize); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_13); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _13 = <usize as Step>::forward_unchecked(_12, const 1_usize) -> bb4; // scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL + // + literal: Const { ty: unsafe fn(usize, usize) -> usize {<usize as Step>::forward_unchecked}, val: Value(<ZST>) } + } + + bb4: { + ((*_5).0: usize) = move _13; // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_13); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _11 = Option::<usize>::Some(_12); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb5; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb5: { + StorageDead(_10); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_12); // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _14 = discriminant(_11); // scope 1 at $DIR/loops.rs:+1:14: +1:24 + switchInt(move _14) -> [0: bb6, 1: bb7, otherwise: bb9]; // scope 1 at $DIR/loops.rs:+1:14: +1:24 + } + + bb6: { + StorageDead(_11); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + StorageDead(_4); // scope 0 at $DIR/loops.rs:+3:5: +3:6 + return; // scope 0 at $DIR/loops.rs:+4:2: +4:2 + } + + bb7: { + _15 = ((_11 as Some).0: usize); // scope 1 at $DIR/loops.rs:+1:9: +1:10 + _16 = opaque::<usize>(_15) -> bb8; // scope 2 at $DIR/loops.rs:+2:9: +2:18 + // mir::Constant + // + span: $DIR/loops.rs:9:9: 9:15 + // + literal: Const { ty: fn(usize) {opaque::<usize>}, val: Value(<ZST>) } + } + + bb8: { + StorageDead(_11); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + goto -> bb1; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb9: { + unreachable; // scope 1 at $DIR/loops.rs:+1:14: +1:24 + } +} diff --git a/tests/mir-opt/pre-codegen/loops.mapped.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/loops.mapped.PreCodegen.after.mir new file mode 100644 index 00000000000..bf1380b30ff --- /dev/null +++ b/tests/mir-opt/pre-codegen/loops.mapped.PreCodegen.after.mir @@ -0,0 +1,91 @@ +// MIR for `mapped` after PreCodegen + +fn mapped(_1: impl Iterator<Item = T>, _2: impl Fn(T) -> U) -> () { + debug iter => _1; // in scope 0 at $DIR/loops.rs:+0:21: +0:25 + debug f => _2; // in scope 0 at $DIR/loops.rs:+0:52: +0:53 + let mut _0: (); // return place in scope 0 at $DIR/loops.rs:+0:72: +0:72 + let mut _3: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25 + let mut _4: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25 + let mut _5: std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25 + let mut _6: &mut std::iter::Map<impl Iterator<Item = T>, impl Fn(T) -> U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25 + let mut _7: std::option::Option<U>; // in scope 0 at $DIR/loops.rs:+1:14: +1:25 + let mut _8: isize; // in scope 0 at $DIR/loops.rs:+1:5: +3:6 + let _10: (); // in scope 0 at $DIR/loops.rs:+1:14: +1:25 + scope 1 { + debug iter => _5; // in scope 1 at $DIR/loops.rs:+1:14: +1:25 + let _9: U; // in scope 1 at $DIR/loops.rs:+1:9: +1:10 + scope 2 { + debug x => _9; // in scope 2 at $DIR/loops.rs:+1:9: +1:10 + } + } + scope 3 (inlined <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as IntoIterator>::into_iter) { // at $DIR/loops.rs:14:14: 14:25 + debug self => _3; // in scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + } + + bb0: { + StorageLive(_4); // scope 0 at $DIR/loops.rs:+1:14: +1:25 + StorageLive(_3); // scope 0 at $DIR/loops.rs:+1:14: +1:25 + _3 = <impl Iterator<Item = T> as Iterator>::map::<U, impl Fn(T) -> U>(move _1, move _2) -> bb1; // scope 0 at $DIR/loops.rs:+1:14: +1:25 + // mir::Constant + // + span: $DIR/loops.rs:14:19: 14:22 + // + literal: Const { ty: fn(impl Iterator<Item = T>, impl Fn(T) -> U) -> Map<impl Iterator<Item = T>, impl Fn(T) -> U> {<impl Iterator<Item = T> as Iterator>::map::<U, impl Fn(T) -> U>}, val: Value(<ZST>) } + } + + bb1: { + _4 = move _3; // scope 3 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + StorageDead(_3); // scope 0 at $DIR/loops.rs:+1:24: +1:25 + StorageLive(_5); // scope 0 at $DIR/loops.rs:+1:14: +1:25 + _5 = move _4; // scope 0 at $DIR/loops.rs:+1:14: +1:25 + goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb2: { + StorageLive(_7); // scope 1 at $DIR/loops.rs:+1:14: +1:25 + _6 = &mut _5; // scope 1 at $DIR/loops.rs:+1:14: +1:25 + _7 = <Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next(_6) -> [return: bb3, unwind: bb9]; // scope 1 at $DIR/loops.rs:+1:14: +1:25 + // mir::Constant + // + span: $DIR/loops.rs:14:14: 14:25 + // + literal: Const { ty: for<'a> fn(&'a mut Map<impl Iterator<Item = T>, impl Fn(T) -> U>) -> Option<<Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::Item> {<Map<impl Iterator<Item = T>, impl Fn(T) -> U> as Iterator>::next}, val: Value(<ZST>) } + } + + bb3: { + _8 = discriminant(_7); // scope 1 at $DIR/loops.rs:+1:14: +1:25 + switchInt(move _8) -> [0: bb4, 1: bb6, otherwise: bb8]; // scope 1 at $DIR/loops.rs:+1:14: +1:25 + } + + bb4: { + StorageDead(_7); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + drop(_5) -> bb5; // scope 0 at $DIR/loops.rs:+3:5: +3:6 + } + + bb5: { + StorageDead(_5); // scope 0 at $DIR/loops.rs:+3:5: +3:6 + StorageDead(_4); // scope 0 at $DIR/loops.rs:+3:5: +3:6 + return; // scope 0 at $DIR/loops.rs:+4:2: +4:2 + } + + bb6: { + _9 = move ((_7 as Some).0: U); // scope 1 at $DIR/loops.rs:+1:9: +1:10 + _10 = opaque::<U>(move _9) -> [return: bb7, unwind: bb9]; // scope 2 at $DIR/loops.rs:+2:9: +2:18 + // mir::Constant + // + span: $DIR/loops.rs:15:9: 15:15 + // + literal: Const { ty: fn(U) {opaque::<U>}, val: Value(<ZST>) } + } + + bb7: { + StorageDead(_7); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb8: { + unreachable; // scope 1 at $DIR/loops.rs:+1:14: +1:25 + } + + bb9 (cleanup): { + drop(_5) -> [return: bb10, unwind terminate]; // scope 0 at $DIR/loops.rs:+3:5: +3:6 + } + + bb10 (cleanup): { + resume; // scope 0 at $DIR/loops.rs:+0:1: +4:2 + } +} diff --git a/tests/mir-opt/pre-codegen/loops.rs b/tests/mir-opt/pre-codegen/loops.rs new file mode 100644 index 00000000000..67f549a511c --- /dev/null +++ b/tests/mir-opt/pre-codegen/loops.rs @@ -0,0 +1,37 @@ +// compile-flags: -O -Zmir-opt-level=2 -g +// needs-unwind +// ignore-debug + +#![crate_type = "lib"] + +pub fn int_range(start: usize, end: usize) { + for i in start..end { + opaque(i) + } +} + +pub fn mapped<T, U>(iter: impl Iterator<Item = T>, f: impl Fn(T) -> U) { + for x in iter.map(f) { + opaque(x) + } +} + +pub fn filter_mapped<T, U>(iter: impl Iterator<Item = T>, f: impl Fn(T) -> Option<U>) { + for x in iter.filter_map(f) { + opaque(x) + } +} + +pub fn vec_move(mut v: Vec<impl Sized>) { + for x in v { + opaque(x) + } +} + +#[inline(never)] +fn opaque(_: impl Sized) {} + +// EMIT_MIR loops.int_range.PreCodegen.after.mir +// EMIT_MIR loops.mapped.PreCodegen.after.mir +// EMIT_MIR loops.filter_mapped.PreCodegen.after.mir +// EMIT_MIR loops.vec_move.PreCodegen.after.mir diff --git a/tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir new file mode 100644 index 00000000000..6cd5a66de00 --- /dev/null +++ b/tests/mir-opt/pre-codegen/loops.vec_move.PreCodegen.after.mir @@ -0,0 +1,83 @@ +// MIR for `vec_move` after PreCodegen + +fn vec_move(_1: Vec<impl Sized>) -> () { + debug v => _1; // in scope 0 at $DIR/loops.rs:+0:17: +0:22 + let mut _0: (); // return place in scope 0 at $DIR/loops.rs:+0:41: +0:41 + let mut _2: std::vec::IntoIter<impl Sized>; // in scope 0 at $DIR/loops.rs:+1:14: +1:15 + let mut _3: std::vec::IntoIter<impl Sized>; // in scope 0 at $DIR/loops.rs:+1:14: +1:15 + let mut _4: &mut std::vec::IntoIter<impl Sized>; // in scope 0 at $DIR/loops.rs:+1:14: +1:15 + let mut _5: std::option::Option<impl Sized>; // in scope 0 at $DIR/loops.rs:+1:14: +1:15 + let mut _6: isize; // in scope 0 at $DIR/loops.rs:+1:5: +3:6 + let _8: (); // in scope 0 at $DIR/loops.rs:+1:14: +1:15 + scope 1 { + debug iter => _3; // in scope 1 at $DIR/loops.rs:+1:14: +1:15 + let _7: impl Sized; // in scope 1 at $DIR/loops.rs:+1:9: +1:10 + scope 2 { + debug x => _7; // in scope 2 at $DIR/loops.rs:+1:9: +1:10 + } + } + + bb0: { + StorageLive(_2); // scope 0 at $DIR/loops.rs:+1:14: +1:15 + _2 = <Vec<impl Sized> as IntoIterator>::into_iter(move _1) -> bb1; // scope 0 at $DIR/loops.rs:+1:14: +1:15 + // mir::Constant + // + span: $DIR/loops.rs:26:14: 26:15 + // + literal: Const { ty: fn(Vec<impl Sized>) -> <Vec<impl Sized> as IntoIterator>::IntoIter {<Vec<impl Sized> as IntoIterator>::into_iter}, val: Value(<ZST>) } + } + + bb1: { + StorageLive(_3); // scope 0 at $DIR/loops.rs:+1:14: +1:15 + _3 = move _2; // scope 0 at $DIR/loops.rs:+1:14: +1:15 + goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb2: { + StorageLive(_5); // scope 1 at $DIR/loops.rs:+1:14: +1:15 + _4 = &mut _3; // scope 1 at $DIR/loops.rs:+1:14: +1:15 + _5 = <std::vec::IntoIter<impl Sized> as Iterator>::next(_4) -> [return: bb3, unwind: bb9]; // scope 1 at $DIR/loops.rs:+1:14: +1:15 + // mir::Constant + // + span: $DIR/loops.rs:26:14: 26:15 + // + literal: Const { ty: for<'a> fn(&'a mut std::vec::IntoIter<impl Sized>) -> Option<<std::vec::IntoIter<impl Sized> as Iterator>::Item> {<std::vec::IntoIter<impl Sized> as Iterator>::next}, val: Value(<ZST>) } + } + + bb3: { + _6 = discriminant(_5); // scope 1 at $DIR/loops.rs:+1:14: +1:15 + switchInt(move _6) -> [0: bb4, 1: bb6, otherwise: bb8]; // scope 1 at $DIR/loops.rs:+1:14: +1:15 + } + + bb4: { + StorageDead(_5); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + drop(_3) -> bb5; // scope 0 at $DIR/loops.rs:+3:5: +3:6 + } + + bb5: { + StorageDead(_3); // scope 0 at $DIR/loops.rs:+3:5: +3:6 + StorageDead(_2); // scope 0 at $DIR/loops.rs:+3:5: +3:6 + return; // scope 0 at $DIR/loops.rs:+4:2: +4:2 + } + + bb6: { + _7 = move ((_5 as Some).0: impl Sized); // scope 1 at $DIR/loops.rs:+1:9: +1:10 + _8 = opaque::<impl Sized>(move _7) -> [return: bb7, unwind: bb9]; // scope 2 at $DIR/loops.rs:+2:9: +2:18 + // mir::Constant + // + span: $DIR/loops.rs:27:9: 27:15 + // + literal: Const { ty: fn(impl Sized) {opaque::<impl Sized>}, val: Value(<ZST>) } + } + + bb7: { + StorageDead(_5); // scope 1 at $DIR/loops.rs:+3:5: +3:6 + goto -> bb2; // scope 1 at $DIR/loops.rs:+1:5: +3:6 + } + + bb8: { + unreachable; // scope 1 at $DIR/loops.rs:+1:14: +1:15 + } + + bb9 (cleanup): { + drop(_3) -> [return: bb10, unwind terminate]; // scope 0 at $DIR/loops.rs:+3:5: +3:6 + } + + bb10 (cleanup): { + resume; // scope 0 at $DIR/loops.rs:+0:1: +4:2 + } +} diff --git a/tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.mir index 50e0538c133..0cf9643dfc2 100644 --- a/tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/mem_replace.mem_replace.PreCodegen.after.mir @@ -15,20 +15,18 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 { scope 7 (inlined std::ptr::write::<u32>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL debug dst => _4; // in scope 7 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL debug src => _2; // in scope 7 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - let mut _6: *mut u32; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 8 { scope 9 (inlined std::ptr::write::runtime::<u32>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug dst => _6; // in scope 9 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug dst => _4; // in scope 9 at $SRC_DIR/core/src/intrinsics.rs:LL:COL } } } } scope 4 (inlined std::ptr::read::<u32>) { // at $SRC_DIR/core/src/mem/mod.rs:LL:COL debug src => _3; // in scope 4 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - let mut _5: *const u32; // in scope 4 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 5 { scope 6 (inlined std::ptr::read::runtime::<u32>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug src => _5; // in scope 6 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug src => _3; // in scope 6 at $SRC_DIR/core/src/intrinsics.rs:LL:COL } } } @@ -38,15 +36,11 @@ fn mem_replace(_1: &mut u32, _2: u32) -> u32 { bb0: { StorageLive(_3); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL _3 = &raw const (*_1); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL - StorageLive(_5); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL _0 = (*_3); // scope 5 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - StorageDead(_5); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL StorageDead(_3); // scope 2 at $SRC_DIR/core/src/mem/mod.rs:LL:COL StorageLive(_4); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL _4 = &raw mut (*_1); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL - StorageLive(_6); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL (*_4) = _2; // scope 8 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - StorageDead(_6); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL StorageDead(_4); // scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL return; // scope 0 at $DIR/mem_replace.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/pre-codegen/range_iter.forward_loop.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/range_iter.forward_loop.PreCodegen.after.mir index 693939e75f4..06a4e35f1f9 100644 --- a/tests/mir-opt/pre-codegen/range_iter.forward_loop.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/range_iter.forward_loop.PreCodegen.after.mir @@ -8,16 +8,16 @@ fn forward_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () { let mut _4: std::ops::Range<u32>; // in scope 0 at $DIR/range_iter.rs:+1:14: +1:24 let mut _5: std::ops::Range<u32>; // in scope 0 at $DIR/range_iter.rs:+1:14: +1:24 let mut _6: &mut std::ops::Range<u32>; // in scope 0 at $DIR/range_iter.rs:+1:14: +1:24 - let mut _10: std::option::Option<u32>; // in scope 0 at $DIR/range_iter.rs:+1:14: +1:24 - let mut _13: isize; // in scope 0 at $DIR/range_iter.rs:+1:5: +3:6 - let mut _15: &impl Fn(u32); // in scope 0 at $DIR/range_iter.rs:+2:9: +2:10 - let mut _16: (u32,); // in scope 0 at $DIR/range_iter.rs:+2:9: +2:13 - let _17: (); // in scope 0 at $DIR/range_iter.rs:+1:14: +1:24 + let mut _12: std::option::Option<u32>; // in scope 0 at $DIR/range_iter.rs:+1:14: +1:24 + let mut _15: isize; // in scope 0 at $DIR/range_iter.rs:+1:5: +3:6 + let mut _17: &impl Fn(u32); // in scope 0 at $DIR/range_iter.rs:+2:9: +2:10 + let mut _18: (u32,); // in scope 0 at $DIR/range_iter.rs:+2:9: +2:13 + let _19: (); // in scope 0 at $DIR/range_iter.rs:+1:14: +1:24 scope 1 { debug iter => _5; // in scope 1 at $DIR/range_iter.rs:+1:14: +1:24 - let _14: u32; // in scope 1 at $DIR/range_iter.rs:+1:9: +1:10 + let _16: u32; // in scope 1 at $DIR/range_iter.rs:+1:9: +1:10 scope 2 { - debug x => _14; // in scope 2 at $DIR/range_iter.rs:+1:9: +1:10 + debug x => _16; // in scope 2 at $DIR/range_iter.rs:+1:9: +1:10 } scope 4 (inlined iter::range::<impl Iterator for std::ops::Range<u32>>::next) { // at $DIR/range_iter.rs:21:14: 21:24 debug self => _6; // in scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL @@ -25,14 +25,20 @@ fn forward_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () { debug self => _6; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL let mut _7: &u32; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL let mut _8: &u32; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - let mut _9: bool; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - let _11: u32; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - let mut _12: u32; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _11: bool; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let _13: u32; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _14: u32; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL scope 6 { - debug old => _11; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug old => _13; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL scope 7 { } } + scope 8 (inlined cmp::impls::<impl PartialOrd for u32>::lt) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _7; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _8; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _9: u32; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _10: u32; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + } } } } @@ -48,92 +54,92 @@ fn forward_loop(_1: u32, _2: u32, _3: impl Fn(u32)) -> () { } bb1: { - StorageLive(_10); // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 + StorageLive(_12); // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 _6 = &mut _5; // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 - StorageLive(_11); // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageLive(_9); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_13); // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_11); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL StorageLive(_7); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL _7 = &((*_6).0: u32); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL StorageLive(_8); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL _8 = &((*_6).1: u32); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _9 = <u32 as PartialOrd>::lt(move _7, move _8) -> [return: bb2, unwind: bb12]; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - // mir::Constant - // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL - // + literal: Const { ty: for<'a, 'b> fn(&'a u32, &'b u32) -> bool {<u32 as PartialOrd>::lt}, val: Value(<ZST>) } - } - - bb2: { + StorageLive(_9); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + _9 = (*_7); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_10); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + _10 = (*_8); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + _11 = Lt(move _9, move _10); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_10); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_9); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL StorageDead(_8); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL StorageDead(_7); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - switchInt(move _9) -> [0: bb3, otherwise: bb4]; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + switchInt(move _11) -> [0: bb2, otherwise: bb3]; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL } - bb3: { - _10 = Option::<u32>::None; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - goto -> bb6; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + bb2: { + _12 = Option::<u32>::None; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb5; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL } - bb4: { - _11 = ((*_6).0: u32); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageLive(_12); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _12 = <u32 as Step>::forward_unchecked(_11, const 1_usize) -> [return: bb5, unwind: bb12]; // scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL + bb3: { + _13 = ((*_6).0: u32); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_14); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _14 = <u32 as Step>::forward_unchecked(_13, const 1_usize) -> [return: bb4, unwind: bb11]; // scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL // + literal: Const { ty: unsafe fn(u32, usize) -> u32 {<u32 as Step>::forward_unchecked}, val: Value(<ZST>) } } - bb5: { - ((*_6).0: u32) = move _12; // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageDead(_12); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _10 = Option::<u32>::Some(_11); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL - goto -> bb6; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + bb4: { + ((*_6).0: u32) = move _14; // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_14); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _12 = Option::<u32>::Some(_13); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb5; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL } - bb6: { - StorageDead(_9); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageDead(_11); // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _13 = discriminant(_10); // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 - switchInt(move _13) -> [0: bb7, 1: bb9, otherwise: bb11]; // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 + bb5: { + StorageDead(_11); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_13); // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _15 = discriminant(_12); // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 + switchInt(move _15) -> [0: bb6, 1: bb8, otherwise: bb10]; // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 } - bb7: { - StorageDead(_10); // scope 1 at $DIR/range_iter.rs:+3:5: +3:6 + bb6: { + StorageDead(_12); // scope 1 at $DIR/range_iter.rs:+3:5: +3:6 StorageDead(_5); // scope 0 at $DIR/range_iter.rs:+3:5: +3:6 - drop(_3) -> bb8; // scope 0 at $DIR/range_iter.rs:+4:1: +4:2 + drop(_3) -> bb7; // scope 0 at $DIR/range_iter.rs:+4:1: +4:2 } - bb8: { + bb7: { return; // scope 0 at $DIR/range_iter.rs:+4:2: +4:2 } - bb9: { - _14 = ((_10 as Some).0: u32); // scope 1 at $DIR/range_iter.rs:+1:9: +1:10 - StorageLive(_15); // scope 2 at $DIR/range_iter.rs:+2:9: +2:10 - _15 = &_3; // scope 2 at $DIR/range_iter.rs:+2:9: +2:10 - StorageLive(_16); // scope 2 at $DIR/range_iter.rs:+2:9: +2:13 - _16 = (_14,); // scope 2 at $DIR/range_iter.rs:+2:9: +2:13 - _17 = <impl Fn(u32) as Fn<(u32,)>>::call(move _15, move _16) -> [return: bb10, unwind: bb12]; // scope 2 at $DIR/range_iter.rs:+2:9: +2:13 + bb8: { + _16 = ((_12 as Some).0: u32); // scope 1 at $DIR/range_iter.rs:+1:9: +1:10 + StorageLive(_17); // scope 2 at $DIR/range_iter.rs:+2:9: +2:10 + _17 = &_3; // scope 2 at $DIR/range_iter.rs:+2:9: +2:10 + StorageLive(_18); // scope 2 at $DIR/range_iter.rs:+2:9: +2:13 + _18 = (_16,); // scope 2 at $DIR/range_iter.rs:+2:9: +2:13 + _19 = <impl Fn(u32) as Fn<(u32,)>>::call(move _17, move _18) -> [return: bb9, unwind: bb11]; // scope 2 at $DIR/range_iter.rs:+2:9: +2:13 // mir::Constant // + span: $DIR/range_iter.rs:22:9: 22:10 // + literal: Const { ty: for<'a> extern "rust-call" fn(&'a impl Fn(u32), (u32,)) -> <impl Fn(u32) as FnOnce<(u32,)>>::Output {<impl Fn(u32) as Fn<(u32,)>>::call}, val: Value(<ZST>) } } - bb10: { - StorageDead(_16); // scope 2 at $DIR/range_iter.rs:+2:12: +2:13 - StorageDead(_15); // scope 2 at $DIR/range_iter.rs:+2:12: +2:13 - StorageDead(_10); // scope 1 at $DIR/range_iter.rs:+3:5: +3:6 + bb9: { + StorageDead(_18); // scope 2 at $DIR/range_iter.rs:+2:12: +2:13 + StorageDead(_17); // scope 2 at $DIR/range_iter.rs:+2:12: +2:13 + StorageDead(_12); // scope 1 at $DIR/range_iter.rs:+3:5: +3:6 goto -> bb1; // scope 1 at $DIR/range_iter.rs:+1:5: +3:6 } - bb11: { + bb10: { unreachable; // scope 1 at $DIR/range_iter.rs:+1:14: +1:24 } - bb12 (cleanup): { - drop(_3) -> [return: bb13, unwind terminate]; // scope 0 at $DIR/range_iter.rs:+4:1: +4:2 + bb11 (cleanup): { + drop(_3) -> [return: bb12, unwind terminate]; // scope 0 at $DIR/range_iter.rs:+4:1: +4:2 } - bb13 (cleanup): { + bb12 (cleanup): { resume; // scope 0 at $DIR/range_iter.rs:+0:1: +4:2 } } diff --git a/tests/mir-opt/pre-codegen/range_iter.range_iter_next.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/range_iter.range_iter_next.PreCodegen.after.mir index 668ec31c6c1..f15722deee0 100644 --- a/tests/mir-opt/pre-codegen/range_iter.range_iter_next.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/range_iter.range_iter_next.PreCodegen.after.mir @@ -9,60 +9,66 @@ fn range_iter_next(_1: &mut std::ops::Range<u32>) -> Option<u32> { debug self => _1; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL let mut _2: &u32; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL let mut _3: &u32; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - let mut _4: bool; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - let _5: u32; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - let mut _6: u32; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _6: bool; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let _7: u32; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _8: u32; // in scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL scope 3 { - debug old => _5; // in scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug old => _7; // in scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL scope 4 { } } + scope 5 (inlined cmp::impls::<impl PartialOrd for u32>::lt) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _2; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _3; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _4: u32; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _5: u32; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + } } } bb0: { - StorageLive(_5); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageLive(_4); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_7); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_6); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL StorageLive(_2); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL _2 = &((*_1).0: u32); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL StorageLive(_3); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL _3 = &((*_1).1: u32); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _4 = <u32 as PartialOrd>::lt(move _2, move _3) -> bb1; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - // mir::Constant - // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL - // + literal: Const { ty: for<'a, 'b> fn(&'a u32, &'b u32) -> bool {<u32 as PartialOrd>::lt}, val: Value(<ZST>) } - } - - bb1: { + StorageLive(_4); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + _4 = (*_2); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_5); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + _5 = (*_3); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + _6 = Lt(move _4, move _5); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_5); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_4); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL StorageDead(_3); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL StorageDead(_2); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - switchInt(move _4) -> [0: bb2, otherwise: bb3]; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + switchInt(move _6) -> [0: bb1, otherwise: bb2]; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL } - bb2: { + bb1: { _0 = Option::<u32>::None; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - goto -> bb5; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb4; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL } - bb3: { - _5 = ((*_1).0: u32); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageLive(_6); // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _6 = <u32 as Step>::forward_unchecked(_5, const 1_usize) -> bb4; // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL + bb2: { + _7 = ((*_1).0: u32); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_8); // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _8 = <u32 as Step>::forward_unchecked(_7, const 1_usize) -> bb3; // scope 4 at $SRC_DIR/core/src/iter/range.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL // + literal: Const { ty: unsafe fn(u32, usize) -> u32 {<u32 as Step>::forward_unchecked}, val: Value(<ZST>) } } - bb4: { - ((*_1).0: u32) = move _6; // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageDead(_6); // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _0 = Option::<u32>::Some(_5); // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL - goto -> bb5; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + bb3: { + ((*_1).0: u32) = move _8; // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_8); // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _0 = Option::<u32>::Some(_7); // scope 3 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb4; // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL } - bb5: { - StorageDead(_4); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL - StorageDead(_5); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL + bb4: { + StorageDead(_6); // scope 2 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_7); // scope 1 at $SRC_DIR/core/src/iter/range.rs:LL:COL return; // scope 0 at $DIR/range_iter.rs:+2:2: +2:2 } } diff --git a/tests/mir-opt/pre-codegen/simple_option_map.ezmap.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/simple_option_map.ezmap.PreCodegen.after.mir index 089b0c23e2c..73b5678ce04 100644 --- a/tests/mir-opt/pre-codegen/simple_option_map.ezmap.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/simple_option_map.ezmap.PreCodegen.after.mir @@ -3,24 +3,21 @@ fn ezmap(_1: Option<i32>) -> Option<i32> { debug x => _1; // in scope 0 at $DIR/simple_option_map.rs:+0:14: +0:15 let mut _0: std::option::Option<i32>; // return place in scope 0 at $DIR/simple_option_map.rs:+0:33: +0:44 - let mut _5: i32; // in scope 0 at $DIR/simple_option_map.rs:11:25: 11:29 scope 1 (inlined map::<i32, i32, [closure@$DIR/simple_option_map.rs:18:12: 18:15]>) { // at $DIR/simple_option_map.rs:18:5: 18:22 debug slf => _1; // in scope 1 at $DIR/simple_option_map.rs:6:17: 6:20 debug f => const ZeroSized: [closure@$DIR/simple_option_map.rs:18:12: 18:15]; // in scope 1 at $DIR/simple_option_map.rs:6:33: 6:34 let mut _2: isize; // in scope 1 at $DIR/simple_option_map.rs:11:9: 11:16 let _3: i32; // in scope 1 at $DIR/simple_option_map.rs:11:14: 11:15 - let mut _4: (i32,); // in scope 1 at $DIR/simple_option_map.rs:11:25: 11:29 - let mut _6: i32; // in scope 1 at $DIR/simple_option_map.rs:11:25: 11:29 + let mut _4: i32; // in scope 1 at $DIR/simple_option_map.rs:11:25: 11:29 scope 2 { debug x => _3; // in scope 2 at $DIR/simple_option_map.rs:11:14: 11:15 scope 3 (inlined ezmap::{closure#0}) { // at $DIR/simple_option_map.rs:11:25: 11:29 - debug n => _5; // in scope 3 at $DIR/simple_option_map.rs:+1:13: +1:14 + debug n => _3; // in scope 3 at $DIR/simple_option_map.rs:+1:13: +1:14 } } } bb0: { - StorageLive(_3); // scope 0 at $DIR/simple_option_map.rs:+1:5: +1:22 _2 = discriminant(_1); // scope 1 at $DIR/simple_option_map.rs:10:11: 10:14 switchInt(move _2) -> [0: bb1, 1: bb2, otherwise: bb4]; // scope 1 at $DIR/simple_option_map.rs:10:5: 10:14 } @@ -32,21 +29,14 @@ fn ezmap(_1: Option<i32>) -> Option<i32> { bb2: { _3 = ((_1 as Some).0: i32); // scope 1 at $DIR/simple_option_map.rs:11:14: 11:15 - StorageLive(_6); // scope 2 at $DIR/simple_option_map.rs:11:25: 11:29 StorageLive(_4); // scope 2 at $DIR/simple_option_map.rs:11:25: 11:29 - _4 = (move _3,); // scope 2 at $DIR/simple_option_map.rs:11:25: 11:29 - StorageLive(_5); // scope 2 at $DIR/simple_option_map.rs:11:25: 11:29 - _5 = move (_4.0: i32); // scope 2 at $DIR/simple_option_map.rs:11:25: 11:29 - _6 = Add(_5, const 1_i32); // scope 3 at $DIR/simple_option_map.rs:+1:16: +1:21 - StorageDead(_5); // scope 2 at $DIR/simple_option_map.rs:11:25: 11:29 - StorageDead(_4); // scope 2 at $DIR/simple_option_map.rs:11:28: 11:29 - _0 = Option::<i32>::Some(move _6); // scope 2 at $DIR/simple_option_map.rs:11:20: 11:30 - StorageDead(_6); // scope 2 at $DIR/simple_option_map.rs:11:29: 11:30 + _4 = Add(_3, const 1_i32); // scope 3 at $DIR/simple_option_map.rs:+1:16: +1:21 + _0 = Option::<i32>::Some(move _4); // scope 2 at $DIR/simple_option_map.rs:11:20: 11:30 + StorageDead(_4); // scope 2 at $DIR/simple_option_map.rs:11:29: 11:30 goto -> bb3; // scope 1 at $DIR/simple_option_map.rs:14:1: 14:2 } bb3: { - StorageDead(_3); // scope 0 at $DIR/simple_option_map.rs:+1:5: +1:22 return; // scope 0 at $DIR/simple_option_map.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/pre-codegen/slice_filter.rs b/tests/mir-opt/pre-codegen/slice_filter.rs new file mode 100644 index 00000000000..aba951acdd0 --- /dev/null +++ b/tests/mir-opt/pre-codegen/slice_filter.rs @@ -0,0 +1,15 @@ +// compile-flags: -O -Zmir-opt-level=2 -Cdebuginfo=2 +// ignore-debug: standard library debug assertions add a panic that breaks this optimization + +#![crate_type = "lib"] + +pub fn variant_a(input: &[(usize, usize, usize, usize)]) -> usize { + input.iter().filter(|(a, b, c, d)| a <= c && d <= b || c <= a && b <= d).count() +} + +pub fn variant_b(input: &[(usize, usize, usize, usize)]) -> usize { + input.iter().filter(|&&(a, b, c, d)| a <= c && d <= b || c <= a && b <= d).count() +} + +// EMIT_MIR slice_filter.variant_a-{closure#0}.PreCodegen.after.mir +// EMIT_MIR slice_filter.variant_b-{closure#0}.PreCodegen.after.mir diff --git a/tests/mir-opt/pre-codegen/slice_filter.variant_a-{closure#0}.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_filter.variant_a-{closure#0}.PreCodegen.after.mir new file mode 100644 index 00000000000..91c8f299fdb --- /dev/null +++ b/tests/mir-opt/pre-codegen/slice_filter.variant_a-{closure#0}.PreCodegen.after.mir @@ -0,0 +1,228 @@ +// MIR for `variant_a::{closure#0}` after PreCodegen + +fn variant_a::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:7:25: 7:39], _2: &&(usize, usize, usize, usize)) -> bool { + let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:40: +0:40 + let mut _3: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 + let _4: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 + let mut _5: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 + let _6: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 + let mut _7: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 + let _8: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 + let mut _9: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 + let _10: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 + let mut _11: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:41 + let _12: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:45: +0:46 + let mut _13: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:45: +0:46 + let mut _18: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:46 + let mut _19: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:50: +0:51 + let _20: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:55: +0:56 + let mut _21: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:55: +0:56 + let mut _26: bool; // in scope 0 at $DIR/slice_filter.rs:+0:50: +0:56 + let mut _27: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:56 + let mut _28: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:61 + let _29: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:65: +0:66 + let mut _30: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:65: +0:66 + let mut _35: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:66 + let mut _36: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:76 + let mut _37: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:70: +0:71 + let _38: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 + let mut _39: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 + let mut _44: bool; // in scope 0 at $DIR/slice_filter.rs:+0:70: +0:76 + scope 1 { + debug a => _4; // in scope 1 at $DIR/slice_filter.rs:+0:27: +0:28 + debug b => _6; // in scope 1 at $DIR/slice_filter.rs:+0:30: +0:31 + debug c => _8; // in scope 1 at $DIR/slice_filter.rs:+0:33: +0:34 + debug d => _10; // in scope 1 at $DIR/slice_filter.rs:+0:36: +0:37 + scope 2 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:7:40: 7:46 + debug self => _11; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _13; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _14: &usize; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _15: &usize; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL + scope 3 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL + debug self => _14; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _15; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _16: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _17: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + } + } + scope 4 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:7:60: 7:66 + debug self => _28; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _30; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _31: &usize; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _32: &usize; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL + scope 5 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL + debug self => _31; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _32; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _33: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _34: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + } + } + scope 6 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:7:50: 7:56 + debug self => _19; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _21; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _22: &usize; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _23: &usize; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL + scope 7 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL + debug self => _22; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _23; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _24: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _25: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + } + } + scope 8 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:7:70: 7:76 + debug self => _37; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _39; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _40: &usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _41: &usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + scope 9 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL + debug self => _40; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _41; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _42: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _43: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + } + } + } + + bb0: { + StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 + _3 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 + _4 = &((*_3).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 + StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 + _5 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 + _6 = &((*_5).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 + StorageLive(_8); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 + _7 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 + _8 = &((*_7).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 + StorageLive(_10); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 + _9 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 + _10 = &((*_9).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 + StorageLive(_27); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 + StorageLive(_18); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:46 + StorageLive(_11); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:41 + _11 = &_4; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:41 + StorageLive(_13); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 + StorageLive(_12); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 + _12 = _8; // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 + _13 = &_12; // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 + _14 = deref_copy (*_11); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL + _15 = deref_copy (*_13); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_16); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + _16 = (*_14); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_17); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + _17 = (*_15); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + _18 = Le(move _16, move _17); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_17); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_16); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 + StorageDead(_13); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 + StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 + switchInt(move _18) -> [0: bb1, otherwise: bb2]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 + } + + bb1: { + StorageDead(_26); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + StorageDead(_18); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 + } + + bb2: { + StorageLive(_26); // scope 1 at $DIR/slice_filter.rs:+0:50: +0:56 + StorageLive(_19); // scope 1 at $DIR/slice_filter.rs:+0:50: +0:51 + _19 = &_10; // scope 1 at $DIR/slice_filter.rs:+0:50: +0:51 + StorageLive(_21); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + StorageLive(_20); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + _20 = _6; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + _21 = &_20; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + _22 = deref_copy (*_19); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL + _23 = deref_copy (*_21); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_24); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + _24 = (*_22); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_25); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + _25 = (*_23); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + _26 = Le(move _24, move _25); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_25); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_24); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_20); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + StorageDead(_21); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + StorageDead(_19); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + _27 = move _26; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 + StorageDead(_26); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + StorageDead(_18); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 + switchInt(move _27) -> [0: bb3, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 + } + + bb3: { + StorageLive(_36); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 + StorageLive(_35); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:66 + StorageLive(_28); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:61 + _28 = &_8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:61 + StorageLive(_30); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 + StorageLive(_29); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 + _29 = _4; // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 + _30 = &_29; // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 + _31 = deref_copy (*_28); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL + _32 = deref_copy (*_30); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_33); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + _33 = (*_31); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_34); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + _34 = (*_32); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + _35 = Le(move _33, move _34); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_34); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_33); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_29); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 + StorageDead(_30); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 + StorageDead(_28); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 + switchInt(move _35) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 + } + + bb4: { + _36 = const false; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 + goto -> bb6; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 + } + + bb5: { + StorageLive(_44); // scope 1 at $DIR/slice_filter.rs:+0:70: +0:76 + StorageLive(_37); // scope 1 at $DIR/slice_filter.rs:+0:70: +0:71 + _37 = &_6; // scope 1 at $DIR/slice_filter.rs:+0:70: +0:71 + StorageLive(_39); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageLive(_38); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + _38 = _10; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + _39 = &_38; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + _40 = deref_copy (*_37); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + _41 = deref_copy (*_39); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_42); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + _42 = (*_40); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_43); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + _43 = (*_41); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + _44 = Le(move _42, move _43); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_43); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_42); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_38); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_39); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_37); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + _36 = move _44; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 + goto -> bb6; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 + } + + bb6: { + StorageDead(_44); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_35); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + _0 = move _36; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 + goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 + } + + bb7: { + _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 + goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 + } + + bb8: { + StorageDead(_36); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_27); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_10); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_8); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 + StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 + return; // scope 0 at $DIR/slice_filter.rs:+0:76: +0:76 + } +} diff --git a/tests/mir-opt/pre-codegen/slice_filter.variant_b-{closure#0}.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_filter.variant_b-{closure#0}.PreCodegen.after.mir new file mode 100644 index 00000000000..9f5fe95a8b4 --- /dev/null +++ b/tests/mir-opt/pre-codegen/slice_filter.variant_b-{closure#0}.PreCodegen.after.mir @@ -0,0 +1,92 @@ +// MIR for `variant_b::{closure#0}` after PreCodegen + +fn variant_b::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:11:25: 11:41], _2: &&(usize, usize, usize, usize)) -> bool { + let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:42: +0:42 + let mut _3: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 + let _4: usize; // in scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 + let mut _5: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 + let _6: usize; // in scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 + let mut _7: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 + let _8: usize; // in scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 + let mut _9: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 + let _10: usize; // in scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 + let mut _11: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:48 + let mut _12: bool; // in scope 0 at $DIR/slice_filter.rs:+0:52: +0:58 + let mut _13: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:58 + let mut _14: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:68 + let mut _15: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:78 + let mut _16: bool; // in scope 0 at $DIR/slice_filter.rs:+0:72: +0:78 + scope 1 { + debug a => _4; // in scope 1 at $DIR/slice_filter.rs:+0:29: +0:30 + debug b => _6; // in scope 1 at $DIR/slice_filter.rs:+0:32: +0:33 + debug c => _8; // in scope 1 at $DIR/slice_filter.rs:+0:35: +0:36 + debug d => _10; // in scope 1 at $DIR/slice_filter.rs:+0:38: +0:39 + } + + bb0: { + _3 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 + _4 = ((*_3).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 + _5 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 + _6 = ((*_5).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 + _7 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 + _8 = ((*_7).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 + _9 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 + _10 = ((*_9).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 + StorageLive(_13); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 + StorageLive(_11); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 + _11 = Le(_4, _8); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 + switchInt(move _11) -> [0: bb1, otherwise: bb2]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 + } + + bb1: { + StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 + StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 + goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 + } + + bb2: { + StorageLive(_12); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 + _12 = Le(_10, _6); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 + _13 = move _12; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 + StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 + StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 + switchInt(move _13) -> [0: bb3, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 + } + + bb3: { + StorageLive(_15); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 + StorageLive(_14); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 + _14 = Le(_8, _4); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 + switchInt(move _14) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 + } + + bb4: { + _15 = const false; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 + goto -> bb6; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 + } + + bb5: { + StorageLive(_16); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 + _16 = Le(_6, _10); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 + _15 = move _16; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 + goto -> bb6; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 + } + + bb6: { + StorageDead(_16); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 + StorageDead(_14); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 + _0 = move _15; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 + goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 + } + + bb7: { + _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 + goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 + } + + bb8: { + StorageDead(_15); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 + StorageDead(_13); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 + return; // scope 0 at $DIR/slice_filter.rs:+0:78: +0:78 + } +} diff --git a/tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.mir index b05d44f4d60..6c306280536 100644 --- a/tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.mir @@ -21,19 +21,17 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> { debug self => _2; // in scope 4 at $SRC_DIR/core/src/slice/index.rs:LL:COL debug slice => _6; // in scope 4 at $SRC_DIR/core/src/slice/index.rs:LL:COL let mut _7: *mut u32; // in scope 4 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let mut _10: usize; // in scope 4 at $SRC_DIR/core/src/intrinsics.rs:LL:COL - let mut _11: *mut [u32]; // in scope 4 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 5 { debug this => _2; // in scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL scope 6 { scope 7 (inlined <usize as SliceIndex<[T]>>::get_unchecked_mut::runtime::<u32>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug this => _10; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug slice => _11; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug this => _2; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug slice => _6; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 8 (inlined ptr::mut_ptr::<impl *mut [u32]>::len) { // at $SRC_DIR/core/src/slice/index.rs:LL:COL - debug self => _11; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - let mut _12: *const [u32]; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _6; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + let mut _10: *const [u32]; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 9 (inlined std::ptr::metadata::<[u32]>) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug ptr => _12; // in scope 9 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + debug ptr => _10; // in scope 9 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL scope 10 { } } @@ -81,14 +79,10 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> { StorageLive(_6); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL _6 = &raw mut (*_1); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL StorageLive(_10); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_11); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_12); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL StorageLive(_7); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL _7 = _6 as *mut u32 (PtrToPtr); // scope 11 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL _8 = Offset(_7, _2); // scope 13 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL StorageDead(_7); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageDead(_12); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageDead(_11); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL StorageDead(_10); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL StorageDead(_6); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL _9 = &mut (*_8); // scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL diff --git a/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.mir index 6d9ec5d9a27..727ccc1de53 100644 --- a/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/slice_index.slice_get_unchecked_mut_range.PreCodegen.after.mir @@ -4,65 +4,63 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) -> debug slice => _1; // in scope 0 at $DIR/slice_index.rs:+0:45: +0:50 debug index => _2; // in scope 0 at $DIR/slice_index.rs:+0:64: +0:69 let mut _0: &mut [u32]; // return place in scope 0 at $DIR/slice_index.rs:+0:88: +0:98 + let mut _3: usize; // in scope 0 at $DIR/slice_index.rs:+1:29: +1:34 + let mut _4: usize; // in scope 0 at $DIR/slice_index.rs:+1:29: +1:34 scope 1 (inlined core::slice::<impl [u32]>::get_unchecked_mut::<std::ops::Range<usize>>) { // at $DIR/slice_index.rs:26:11: 26:35 debug self => _1; // in scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - debug index => _2; // in scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - let mut _3: *mut [u32]; // in scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - let mut _15: *mut [u32]; // in scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + debug index => std::ops::Range<usize>{ .0 => _3, .1 => _4, }; // in scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + let mut _5: *mut [u32]; // in scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + let mut _14: *mut [u32]; // in scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL scope 2 { scope 3 (inlined <std::ops::Range<usize> as SliceIndex<[u32]>>::get_unchecked_mut) { // at $SRC_DIR/core/src/slice/mod.rs:LL:COL - debug self => _2; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - debug slice => _3; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let mut _4: usize; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let mut _5: usize; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL + debug self => std::ops::Range<usize>{ .0 => _3, .1 => _4, }; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL + debug slice => _5; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL let mut _7: *mut u32; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let mut _8: usize; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let mut _9: *mut u32; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let mut _10: usize; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let _16: std::ops::Range<usize>; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL - let mut _17: std::ops::Range<usize>; // in scope 3 at $SRC_DIR/core/src/intrinsics.rs:LL:COL - let mut _18: *mut [u32]; // in scope 3 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + let mut _8: *mut u32; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL + let mut _9: usize; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL + let _16: usize; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL + let _17: usize; // in scope 3 at $SRC_DIR/core/src/slice/index.rs:LL:COL scope 4 { - debug this => _16; // in scope 4 at $SRC_DIR/core/src/slice/index.rs:LL:COL + debug this => std::ops::Range<usize>{ .0 => _16, .1 => _17, }; // in scope 4 at $SRC_DIR/core/src/slice/index.rs:LL:COL scope 5 { let _6: usize; // in scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL scope 6 { debug new_len => _6; // in scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL scope 11 (inlined ptr::mut_ptr::<impl *mut [u32]>::as_mut_ptr) { // at $SRC_DIR/core/src/slice/index.rs:LL:COL - debug self => _3; // in scope 11 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _5; // in scope 11 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL } scope 12 (inlined ptr::mut_ptr::<impl *mut u32>::add) { // at $SRC_DIR/core/src/slice/index.rs:LL:COL debug self => _7; // in scope 12 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug count => _8; // in scope 12 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug count => _3; // in scope 12 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 13 { } } scope 14 (inlined slice_from_raw_parts_mut::<u32>) { // at $SRC_DIR/core/src/slice/index.rs:LL:COL - debug data => _9; // in scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - debug len => _10; // in scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - let mut _11: *mut (); // in scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL + debug data => _8; // in scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL + debug len => _9; // in scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL + let mut _10: *mut (); // in scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL scope 15 (inlined ptr::mut_ptr::<impl *mut u32>::cast::<()>) { // at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - debug self => _9; // in scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _8; // in scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL } scope 16 (inlined std::ptr::from_raw_parts_mut::<[u32]>) { // at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - debug data_address => _11; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - debug metadata => _10; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - let mut _12: *const (); // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - let mut _13: std::ptr::metadata::PtrComponents<[u32]>; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - let mut _14: std::ptr::metadata::PtrRepr<[u32]>; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + debug data_address => _10; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + debug metadata => _9; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + let mut _11: *const (); // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + let mut _12: std::ptr::metadata::PtrComponents<[u32]>; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + let mut _13: std::ptr::metadata::PtrRepr<[u32]>; // in scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL scope 17 { } } } } scope 7 (inlined <std::ops::Range<usize> as SliceIndex<[T]>>::get_unchecked_mut::runtime::<u32>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug this => _17; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug slice => _18; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug this => std::ops::Range<usize>{ .0 => _16, .1 => _17, }; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug slice => _5; // in scope 7 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 8 (inlined ptr::mut_ptr::<impl *mut [u32]>::len) { // at $SRC_DIR/core/src/slice/index.rs:LL:COL - debug self => _18; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - let mut _19: *const [u32]; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _5; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + let mut _15: *const [u32]; // in scope 8 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 9 (inlined std::ptr::metadata::<[u32]>) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug ptr => _19; // in scope 9 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + debug ptr => _15; // in scope 9 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL scope 10 { } } @@ -75,60 +73,51 @@ fn slice_get_unchecked_mut_range(_1: &mut [u32], _2: std::ops::Range<usize>) -> } bb0: { + _3 = move (_2.0: usize); // scope 0 at $DIR/slice_index.rs:+1:29: +1:34 + _4 = move (_2.1: usize); // scope 0 at $DIR/slice_index.rs:+1:29: +1:34 + StorageLive(_14); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + StorageLive(_5); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + _5 = &raw mut (*_1); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL StorageLive(_15); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - StorageLive(_3); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - _3 = &raw mut (*_1); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL StorageLive(_16); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL StorageLive(_17); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - StorageLive(_18); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - StorageLive(_19); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL StorageLive(_6); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_4); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - _4 = (_2.1: usize); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_5); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - _5 = (_2.0: usize); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - _6 = unchecked_sub::<usize>(move _4, move _5) -> [return: bb1, unwind unreachable]; // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL + _6 = unchecked_sub::<usize>(_4, _3) -> [return: bb1, unwind unreachable]; // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/slice/index.rs:LL:COL // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(usize, usize) -> usize {unchecked_sub::<usize>}, val: Value(<ZST>) } } bb1: { - StorageDead(_5); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageDead(_4); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_9); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_7); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - _7 = _3 as *mut u32 (PtrToPtr); // scope 11 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL StorageLive(_8); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - _8 = (_2.0: usize); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - _9 = Offset(_7, _8); // scope 13 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - StorageDead(_8); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL + StorageLive(_7); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL + _7 = _5 as *mut u32 (PtrToPtr); // scope 11 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + _8 = Offset(_7, _3); // scope 13 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL StorageDead(_7); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_10); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - _10 = _6; // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageLive(_11); // scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - _11 = _9 as *mut () (PtrToPtr); // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - StorageLive(_14); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + StorageLive(_9); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL + _9 = _6; // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL + StorageLive(_10); // scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL + _10 = _8 as *mut () (PtrToPtr); // scope 15 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL StorageLive(_13); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL StorageLive(_12); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - _12 = _11 as *const () (Pointer(MutToConstPointer)); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - _13 = ptr::metadata::PtrComponents::<[u32]> { data_address: move _12, metadata: _10 }; // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + StorageLive(_11); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + _11 = _10 as *const () (Pointer(MutToConstPointer)); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + _12 = ptr::metadata::PtrComponents::<[u32]> { data_address: move _11, metadata: _9 }; // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + StorageDead(_11); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + _13 = ptr::metadata::PtrRepr::<[u32]> { const_ptr: move _12 }; // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL StorageDead(_12); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - _14 = ptr::metadata::PtrRepr::<[u32]> { const_ptr: move _13 }; // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - StorageDead(_13); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - _15 = (_14.1: *mut [u32]); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - StorageDead(_14); // scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL - StorageDead(_11); // scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL - StorageDead(_10); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL + _14 = (_13.1: *mut [u32]); // scope 17 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + StorageDead(_13); // scope 16 at $SRC_DIR/core/src/ptr/metadata.rs:LL:COL + StorageDead(_10); // scope 14 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL StorageDead(_9); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL + StorageDead(_8); // scope 6 at $SRC_DIR/core/src/slice/index.rs:LL:COL StorageDead(_6); // scope 5 at $SRC_DIR/core/src/slice/index.rs:LL:COL - StorageDead(_19); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - StorageDead(_18); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL StorageDead(_17); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL StorageDead(_16); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - StorageDead(_3); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - _0 = &mut (*_15); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL - StorageDead(_15); // scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + StorageDead(_15); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + StorageDead(_5); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + _0 = &mut (*_14); // scope 2 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + StorageDead(_14); // scope 1 at $SRC_DIR/core/src/slice/mod.rs:LL:COL return; // scope 0 at $DIR/slice_index.rs:+2:2: +2:2 } } diff --git a/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.mir new file mode 100644 index 00000000000..4dd11c1e529 --- /dev/null +++ b/tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.mir @@ -0,0 +1,213 @@ +// MIR for `enumerated_loop` after PreCodegen + +fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () { + debug slice => _1; // in scope 0 at $DIR/slice_iter.rs:+0:31: +0:36 + debug f => _2; // in scope 0 at $DIR/slice_iter.rs:+0:47: +0:48 + let mut _0: (); // return place in scope 0 at $DIR/slice_iter.rs:+0:70: +0:70 + let mut _13: std::slice::Iter<'_, T>; // in scope 0 at $DIR/slice_iter.rs:+1:19: +1:31 + let mut _14: std::iter::Enumerate<std::slice::Iter<'_, T>>; // in scope 0 at $DIR/slice_iter.rs:+1:19: +1:43 + let mut _15: std::iter::Enumerate<std::slice::Iter<'_, T>>; // in scope 0 at $DIR/slice_iter.rs:+1:19: +1:43 + let mut _16: &mut std::iter::Enumerate<std::slice::Iter<'_, T>>; // in scope 0 at $DIR/slice_iter.rs:+1:19: +1:43 + let mut _17: std::option::Option<(usize, &T)>; // in scope 0 at $DIR/slice_iter.rs:+1:19: +1:43 + let mut _18: isize; // in scope 0 at $DIR/slice_iter.rs:+1:5: +3:6 + let mut _21: &impl Fn(usize, &T); // in scope 0 at $DIR/slice_iter.rs:+2:9: +2:10 + let mut _22: (usize, &T); // in scope 0 at $DIR/slice_iter.rs:+2:9: +2:16 + let _23: (); // in scope 0 at $DIR/slice_iter.rs:+1:19: +1:43 + scope 1 { + debug iter => _15; // in scope 1 at $DIR/slice_iter.rs:+1:19: +1:43 + let _19: usize; // in scope 1 at $DIR/slice_iter.rs:+1:10: +1:11 + let _20: &T; // in scope 1 at $DIR/slice_iter.rs:+1:13: +1:14 + scope 2 { + debug i => _19; // in scope 2 at $DIR/slice_iter.rs:+1:10: +1:11 + debug x => _20; // in scope 2 at $DIR/slice_iter.rs:+1:13: +1:14 + } + } + scope 3 (inlined core::slice::<impl [T]>::iter) { // at $DIR/slice_iter.rs:42:25: 42:31 + debug self => _1; // in scope 3 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + scope 4 (inlined std::slice::Iter::<'_, T>::new) { // at $SRC_DIR/core/src/slice/mod.rs:LL:COL + debug slice => _1; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + let _4: *const T; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + let mut _5: bool; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + let mut _6: usize; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + let mut _8: usize; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + let mut _9: *mut T; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + let mut _11: std::ptr::NonNull<T>; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + let mut _12: *const T; // in scope 4 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + scope 5 { + debug ptr => _4; // in scope 5 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + scope 6 { + let _7: *const T; // in scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + scope 7 { + debug end => _7; // in scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + scope 13 (inlined NonNull::<T>::new_unchecked) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL + debug ptr => _9; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL + let mut _10: *const T; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL + scope 14 { + scope 15 (inlined NonNull::<T>::new_unchecked::runtime::<T>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug ptr => _9; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + scope 16 (inlined ptr::mut_ptr::<impl *mut T>::is_null) { // at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL + debug self => _9; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + let mut _24: *mut u8; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + scope 17 { + scope 18 (inlined ptr::mut_ptr::<impl *mut T>::is_null::runtime_impl) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug ptr => _24; // in scope 18 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + scope 19 (inlined ptr::mut_ptr::<impl *mut u8>::addr) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _24; // in scope 19 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + scope 20 { + scope 21 (inlined ptr::mut_ptr::<impl *mut u8>::cast::<()>) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _24; // in scope 21 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + } + } + } + } + } + } + } + } + } + } + scope 9 (inlined invalid::<T>) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL + debug addr => _8; // in scope 9 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL + scope 10 { + } + } + scope 11 (inlined ptr::const_ptr::<impl *const T>::add) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL + debug self => _4; // in scope 11 at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + debug count => _6; // in scope 11 at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + scope 12 { + } + } + } + } + scope 8 (inlined core::slice::<impl [T]>::as_ptr) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL + debug self => _1; // in scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + let mut _3: *const [T]; // in scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + } + } + } + scope 22 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) { // at $DIR/slice_iter.rs:42:32: 42:43 + debug self => _13; // in scope 22 at $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + scope 23 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) { // at $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + debug iter => _13; // in scope 23 at $SRC_DIR/core/src/iter/adapters/enumerate.rs:LL:COL + } + } + scope 24 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) { // at $DIR/slice_iter.rs:42:19: 42:43 + debug self => _14; // in scope 24 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + } + + bb0: { + StorageLive(_13); // scope 0 at $DIR/slice_iter.rs:+1:19: +1:31 + StorageLive(_4); // scope 3 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + StorageLive(_3); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + _3 = &raw const (*_1); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + _4 = move _3 as *const T (PtrToPtr); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + StorageDead(_3); // scope 8 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + StorageLive(_7); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageLive(_5); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _5 = const _; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + switchInt(move _5) -> [0: bb1, otherwise: bb2]; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + } + + bb1: { + StorageLive(_6); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _6 = Len((*_1)); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _7 = Offset(_4, _6); // scope 12 at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL + StorageDead(_6); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + goto -> bb3; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + } + + bb2: { + StorageLive(_8); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _8 = Len((*_1)); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _7 = _8 as *const T (Transmute); // scope 10 at $SRC_DIR/core/src/ptr/mod.rs:LL:COL + StorageDead(_8); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + goto -> bb3; // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + } + + bb3: { + StorageDead(_5); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageLive(_11); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageLive(_9); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _9 = _4 as *mut T (PtrToPtr); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageLive(_10); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageLive(_24); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _10 = _9 as *const T (Pointer(MutToConstPointer)); // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL + _11 = NonNull::<T> { pointer: _10 }; // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL + StorageDead(_24); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageDead(_10); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageDead(_9); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageLive(_12); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _12 = _7; // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + _13 = std::slice::Iter::<'_, T> { ptr: move _11, end: move _12, _marker: const ZeroSized: PhantomData<&T> }; // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + // mir::Constant + // + span: no-location + // + literal: Const { ty: PhantomData<&T>, val: Value(<ZST>) } + // adt + // + user_ty: UserType(1) + StorageDead(_12); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageDead(_11); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageDead(_7); // scope 6 at $SRC_DIR/core/src/slice/iter.rs:LL:COL + StorageDead(_4); // scope 3 at $SRC_DIR/core/src/slice/mod.rs:LL:COL + _14 = Enumerate::<std::slice::Iter<'_, T>> { iter: move _13, count: const 0_usize }; // scope 23 at $SRC_DIR/core/src/iter/adapters/enumerate.rs:LL:COL + StorageDead(_13); // scope 0 at $DIR/slice_iter.rs:+1:42: +1:43 + StorageLive(_15); // scope 0 at $DIR/slice_iter.rs:+1:19: +1:43 + _15 = move _14; // scope 0 at $DIR/slice_iter.rs:+1:19: +1:43 + goto -> bb4; // scope 1 at $DIR/slice_iter.rs:+1:5: +3:6 + } + + bb4: { + StorageLive(_17); // scope 1 at $DIR/slice_iter.rs:+1:19: +1:43 + _16 = &mut _15; // scope 1 at $DIR/slice_iter.rs:+1:19: +1:43 + _17 = <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next(_16) -> [return: bb5, unwind: bb11]; // scope 1 at $DIR/slice_iter.rs:+1:19: +1:43 + // mir::Constant + // + span: $DIR/slice_iter.rs:42:19: 42:43 + // + literal: Const { ty: for<'a> fn(&'a mut Enumerate<std::slice::Iter<'_, T>>) -> Option<<Enumerate<std::slice::Iter<'_, T>> as Iterator>::Item> {<Enumerate<std::slice::Iter<'_, T>> as Iterator>::next}, val: Value(<ZST>) } + } + + bb5: { + _18 = discriminant(_17); // scope 1 at $DIR/slice_iter.rs:+1:19: +1:43 + switchInt(move _18) -> [0: bb6, 1: bb8, otherwise: bb10]; // scope 1 at $DIR/slice_iter.rs:+1:19: +1:43 + } + + bb6: { + StorageDead(_17); // scope 1 at $DIR/slice_iter.rs:+3:5: +3:6 + StorageDead(_15); // scope 0 at $DIR/slice_iter.rs:+3:5: +3:6 + drop(_2) -> bb7; // scope 0 at $DIR/slice_iter.rs:+4:1: +4:2 + } + + bb7: { + return; // scope 0 at $DIR/slice_iter.rs:+4:2: +4:2 + } + + bb8: { + _19 = (((_17 as Some).0: (usize, &T)).0: usize); // scope 1 at $DIR/slice_iter.rs:+1:10: +1:11 + _20 = (((_17 as Some).0: (usize, &T)).1: &T); // scope 1 at $DIR/slice_iter.rs:+1:13: +1:14 + StorageLive(_21); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:10 + _21 = &_2; // scope 2 at $DIR/slice_iter.rs:+2:9: +2:10 + StorageLive(_22); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:16 + _22 = (_19, _20); // scope 2 at $DIR/slice_iter.rs:+2:9: +2:16 + _23 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _21, move _22) -> [return: bb9, unwind: bb11]; // scope 2 at $DIR/slice_iter.rs:+2:9: +2:16 + // mir::Constant + // + span: $DIR/slice_iter.rs:43:9: 43:10 + // + literal: Const { ty: for<'a> extern "rust-call" fn(&'a impl Fn(usize, &T), (usize, &T)) -> <impl Fn(usize, &T) as FnOnce<(usize, &T)>>::Output {<impl Fn(usize, &T) as Fn<(usize, &T)>>::call}, val: Value(<ZST>) } + } + + bb9: { + StorageDead(_22); // scope 2 at $DIR/slice_iter.rs:+2:15: +2:16 + StorageDead(_21); // scope 2 at $DIR/slice_iter.rs:+2:15: +2:16 + StorageDead(_17); // scope 1 at $DIR/slice_iter.rs:+3:5: +3:6 + goto -> bb4; // scope 1 at $DIR/slice_iter.rs:+1:5: +3:6 + } + + bb10: { + unreachable; // scope 1 at $DIR/slice_iter.rs:+1:19: +1:43 + } + + bb11 (cleanup): { + drop(_2) -> [return: bb12, unwind terminate]; // scope 0 at $DIR/slice_iter.rs:+4:1: +4:2 + } + + bb12 (cleanup): { + resume; // scope 0 at $DIR/slice_iter.rs:+0:1: +4:2 + } +} diff --git a/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir index 0cf1d68d18a..0c18fb84bcd 100644 --- a/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/slice_iter.forward_loop.PreCodegen.after.mir @@ -39,21 +39,20 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () { scope 13 (inlined NonNull::<T>::new_unchecked) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL debug ptr => _9; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL let mut _10: *const T; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL - let mut _22: *mut T; // in scope 13 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 14 { scope 15 (inlined NonNull::<T>::new_unchecked::runtime::<T>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug ptr => _22; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug ptr => _9; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 16 (inlined ptr::mut_ptr::<impl *mut T>::is_null) { // at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL - debug self => _22; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - let mut _23: *mut u8; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _9; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + let mut _22: *mut u8; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 17 { scope 18 (inlined ptr::mut_ptr::<impl *mut T>::is_null::runtime_impl) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug ptr => _23; // in scope 18 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug ptr => _22; // in scope 18 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 19 (inlined ptr::mut_ptr::<impl *mut u8>::addr) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug self => _23; // in scope 19 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _22; // in scope 19 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 20 { scope 21 (inlined ptr::mut_ptr::<impl *mut u8>::cast::<()>) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug self => _23; // in scope 21 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _22; // in scope 21 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL } } } @@ -122,10 +121,8 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () { _9 = _4 as *mut T (PtrToPtr); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageLive(_10); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageLive(_22); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL - StorageLive(_23); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL _10 = _9 as *const T (Pointer(MutToConstPointer)); // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL _11 = NonNull::<T> { pointer: _10 }; // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL - StorageDead(_23); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageDead(_22); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageDead(_10); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageDead(_9); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL diff --git a/tests/mir-opt/pre-codegen/slice_iter.range_loop.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_iter.range_loop.PreCodegen.after.mir new file mode 100644 index 00000000000..870496f14ea --- /dev/null +++ b/tests/mir-opt/pre-codegen/slice_iter.range_loop.PreCodegen.after.mir @@ -0,0 +1,161 @@ +// MIR for `range_loop` after PreCodegen + +fn range_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () { + debug slice => _1; // in scope 0 at $DIR/slice_iter.rs:+0:26: +0:31 + debug f => _2; // in scope 0 at $DIR/slice_iter.rs:+0:42: +0:43 + let mut _0: (); // return place in scope 0 at $DIR/slice_iter.rs:+0:65: +0:65 + let mut _3: usize; // in scope 0 at $DIR/slice_iter.rs:+1:17: +1:28 + let mut _4: std::ops::Range<usize>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + let mut _5: std::ops::Range<usize>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + let mut _6: &mut std::ops::Range<usize>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + let mut _12: std::option::Option<usize>; // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + let mut _15: isize; // in scope 0 at $DIR/slice_iter.rs:+1:5: +4:6 + let mut _17: usize; // in scope 0 at $DIR/slice_iter.rs:+2:18: +2:26 + let mut _18: bool; // in scope 0 at $DIR/slice_iter.rs:+2:18: +2:26 + let mut _20: &impl Fn(usize, &T); // in scope 0 at $DIR/slice_iter.rs:+3:9: +3:10 + let mut _21: (usize, &T); // in scope 0 at $DIR/slice_iter.rs:+3:9: +3:16 + let _22: (); // in scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + scope 1 { + debug iter => _5; // in scope 1 at $DIR/slice_iter.rs:+1:14: +1:28 + let _16: usize; // in scope 1 at $DIR/slice_iter.rs:+1:9: +1:10 + scope 2 { + debug i => _16; // in scope 2 at $DIR/slice_iter.rs:+1:9: +1:10 + let _19: &T; // in scope 2 at $DIR/slice_iter.rs:+2:13: +2:14 + scope 3 { + debug x => _19; // in scope 3 at $DIR/slice_iter.rs:+2:13: +2:14 + } + } + scope 5 (inlined iter::range::<impl Iterator for std::ops::Range<usize>>::next) { // at $DIR/slice_iter.rs:49:14: 49:28 + debug self => _6; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + scope 6 (inlined <std::ops::Range<usize> as iter::range::RangeIteratorImpl>::spec_next) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _6; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _7: &usize; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _8: &usize; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _11: bool; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let _13: usize; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + let mut _14: usize; // in scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + scope 7 { + debug old => _13; // in scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL + scope 8 { + } + } + scope 9 (inlined cmp::impls::<impl PartialOrd for usize>::lt) { // at $SRC_DIR/core/src/iter/range.rs:LL:COL + debug self => _7; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + debug other => _8; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _9: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + let mut _10: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + } + } + } + } + scope 4 (inlined <std::ops::Range<usize> as IntoIterator>::into_iter) { // at $DIR/slice_iter.rs:49:14: 49:28 + debug self => _4; // in scope 4 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + } + + bb0: { + StorageLive(_3); // scope 0 at $DIR/slice_iter.rs:+1:17: +1:28 + _3 = Len((*_1)); // scope 0 at $DIR/slice_iter.rs:+1:17: +1:28 + _4 = std::ops::Range::<usize> { start: const 0_usize, end: move _3 }; // scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + StorageDead(_3); // scope 0 at $DIR/slice_iter.rs:+1:27: +1:28 + StorageLive(_5); // scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + _5 = move _4; // scope 0 at $DIR/slice_iter.rs:+1:14: +1:28 + goto -> bb1; // scope 1 at $DIR/slice_iter.rs:+1:5: +4:6 + } + + bb1: { + StorageLive(_12); // scope 1 at $DIR/slice_iter.rs:+1:14: +1:28 + _6 = &mut _5; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:28 + StorageLive(_13); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_11); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_7); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _7 = &((*_6).0: usize); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_8); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _8 = &((*_6).1: usize); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_9); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + _9 = (*_7); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageLive(_10); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + _10 = (*_8); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + _11 = Lt(move _9, move _10); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_10); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_9); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL + StorageDead(_8); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_7); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + switchInt(move _11) -> [0: bb2, otherwise: bb3]; // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb2: { + _12 = Option::<usize>::None; // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb5; // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb3: { + _13 = ((*_6).0: usize); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageLive(_14); // scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _14 = <usize as Step>::forward_unchecked(_13, const 1_usize) -> [return: bb4, unwind: bb12]; // scope 8 at $SRC_DIR/core/src/iter/range.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL + // + literal: Const { ty: unsafe fn(usize, usize) -> usize {<usize as Step>::forward_unchecked}, val: Value(<ZST>) } + } + + bb4: { + ((*_6).0: usize) = move _14; // scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_14); // scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _12 = Option::<usize>::Some(_13); // scope 7 at $SRC_DIR/core/src/iter/range.rs:LL:COL + goto -> bb5; // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + } + + bb5: { + StorageDead(_11); // scope 6 at $SRC_DIR/core/src/iter/range.rs:LL:COL + StorageDead(_13); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _15 = discriminant(_12); // scope 1 at $DIR/slice_iter.rs:+1:14: +1:28 + switchInt(move _15) -> [0: bb6, 1: bb8, otherwise: bb11]; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:28 + } + + bb6: { + StorageDead(_12); // scope 1 at $DIR/slice_iter.rs:+4:5: +4:6 + StorageDead(_5); // scope 0 at $DIR/slice_iter.rs:+4:5: +4:6 + drop(_2) -> bb7; // scope 0 at $DIR/slice_iter.rs:+5:1: +5:2 + } + + bb7: { + return; // scope 0 at $DIR/slice_iter.rs:+5:2: +5:2 + } + + bb8: { + _16 = ((_12 as Some).0: usize); // scope 1 at $DIR/slice_iter.rs:+1:9: +1:10 + _17 = Len((*_1)); // scope 2 at $DIR/slice_iter.rs:+2:18: +2:26 + _18 = Lt(_16, _17); // scope 2 at $DIR/slice_iter.rs:+2:18: +2:26 + assert(move _18, "index out of bounds: the length is {} but the index is {}", move _17, _16) -> [success: bb9, unwind: bb12]; // scope 2 at $DIR/slice_iter.rs:+2:18: +2:26 + } + + bb9: { + _19 = &(*_1)[_16]; // scope 2 at $DIR/slice_iter.rs:+2:17: +2:26 + StorageLive(_20); // scope 3 at $DIR/slice_iter.rs:+3:9: +3:10 + _20 = &_2; // scope 3 at $DIR/slice_iter.rs:+3:9: +3:10 + StorageLive(_21); // scope 3 at $DIR/slice_iter.rs:+3:9: +3:16 + _21 = (_16, _19); // scope 3 at $DIR/slice_iter.rs:+3:9: +3:16 + _22 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _20, move _21) -> [return: bb10, unwind: bb12]; // scope 3 at $DIR/slice_iter.rs:+3:9: +3:16 + // mir::Constant + // + span: $DIR/slice_iter.rs:51:9: 51:10 + // + literal: Const { ty: for<'a> extern "rust-call" fn(&'a impl Fn(usize, &T), (usize, &T)) -> <impl Fn(usize, &T) as FnOnce<(usize, &T)>>::Output {<impl Fn(usize, &T) as Fn<(usize, &T)>>::call}, val: Value(<ZST>) } + } + + bb10: { + StorageDead(_21); // scope 3 at $DIR/slice_iter.rs:+3:15: +3:16 + StorageDead(_20); // scope 3 at $DIR/slice_iter.rs:+3:15: +3:16 + StorageDead(_12); // scope 1 at $DIR/slice_iter.rs:+4:5: +4:6 + goto -> bb1; // scope 1 at $DIR/slice_iter.rs:+1:5: +4:6 + } + + bb11: { + unreachable; // scope 1 at $DIR/slice_iter.rs:+1:14: +1:28 + } + + bb12 (cleanup): { + drop(_2) -> [return: bb13, unwind terminate]; // scope 0 at $DIR/slice_iter.rs:+5:1: +5:2 + } + + bb13 (cleanup): { + resume; // scope 0 at $DIR/slice_iter.rs:+0:1: +5:2 + } +} diff --git a/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.mir index 4fde50c6fe4..1aa05cbeb97 100644 --- a/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/slice_iter.reverse_loop.PreCodegen.after.mir @@ -44,21 +44,20 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () { scope 13 (inlined NonNull::<T>::new_unchecked) { // at $SRC_DIR/core/src/slice/iter.rs:LL:COL debug ptr => _9; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL let mut _10: *const T; // in scope 13 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL - let mut _24: *mut T; // in scope 13 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 14 { scope 15 (inlined NonNull::<T>::new_unchecked::runtime::<T>) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL - debug ptr => _24; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL + debug ptr => _9; // in scope 15 at $SRC_DIR/core/src/intrinsics.rs:LL:COL scope 16 (inlined ptr::mut_ptr::<impl *mut T>::is_null) { // at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL - debug self => _24; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - let mut _25: *mut u8; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _9; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + let mut _24: *mut u8; // in scope 16 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 17 { scope 18 (inlined ptr::mut_ptr::<impl *mut T>::is_null::runtime_impl) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug ptr => _25; // in scope 18 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug ptr => _24; // in scope 18 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 19 (inlined ptr::mut_ptr::<impl *mut u8>::addr) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug self => _25; // in scope 19 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _24; // in scope 19 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL scope 20 { scope 21 (inlined ptr::mut_ptr::<impl *mut u8>::cast::<()>) { // at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL - debug self => _25; // in scope 21 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL + debug self => _24; // in scope 21 at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL } } } @@ -134,10 +133,8 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () { _9 = _4 as *mut T (PtrToPtr); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageLive(_10); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageLive(_24); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL - StorageLive(_25); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL _10 = _9 as *const T (Pointer(MutToConstPointer)); // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL _11 = NonNull::<T> { pointer: _10 }; // scope 14 at $SRC_DIR/core/src/ptr/non_null.rs:LL:COL - StorageDead(_25); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageDead(_24); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageDead(_10); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL StorageDead(_9); // scope 7 at $SRC_DIR/core/src/slice/iter.rs:LL:COL diff --git a/tests/mir-opt/pre-codegen/slice_iter.rs b/tests/mir-opt/pre-codegen/slice_iter.rs index ca423ca55e6..a1cd85e753f 100644 --- a/tests/mir-opt/pre-codegen/slice_iter.rs +++ b/tests/mir-opt/pre-codegen/slice_iter.rs @@ -36,3 +36,18 @@ pub fn reverse_loop<'a, T>(slice: &'a [T], f: impl Fn(&T)) { f(x) } } + +// EMIT_MIR slice_iter.enumerated_loop.PreCodegen.after.mir +pub fn enumerated_loop<'a, T>(slice: &'a [T], f: impl Fn(usize, &T)) { + for (i, x) in slice.iter().enumerate() { + f(i, x) + } +} + +// EMIT_MIR slice_iter.range_loop.PreCodegen.after.mir +pub fn range_loop<'a, T>(slice: &'a [T], f: impl Fn(usize, &T)) { + for i in 0..slice.len() { + let x = &slice[i]; + f(i, x) + } +} diff --git a/tests/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff b/tests/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff index bdf1de468b3..7f0e50a23f9 100644 --- a/tests/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff +++ b/tests/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff @@ -9,70 +9,61 @@ let mut _4: std::result::Result<i32, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:9 let mut _5: isize; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 let _6: std::result::Result<std::convert::Infallible, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 - let mut _7: !; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 - let mut _8: std::result::Result<std::convert::Infallible, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 - let _9: i32; // in scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 + let mut _7: std::result::Result<std::convert::Infallible, i32>; // in scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 + let _8: i32; // in scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 scope 1 { debug residual => _6; // in scope 1 at $DIR/separate_const_switch.rs:+1:9: +1:10 scope 2 { scope 8 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<Infallible, i32>>>::from_residual) { // at $DIR/separate_const_switch.rs:25:8: 25:10 - debug residual => _8; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL - let _14: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL - let mut _15: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + debug residual => _6; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + let _13: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + let mut _14: i32; // in scope 8 at $SRC_DIR/core/src/result.rs:LL:COL scope 9 { - debug e => _14; // in scope 9 at $SRC_DIR/core/src/result.rs:LL:COL + debug e => _13; // in scope 9 at $SRC_DIR/core/src/result.rs:LL:COL scope 10 (inlined <i32 as From<i32>>::from) { // at $SRC_DIR/core/src/result.rs:LL:COL - debug t => _14; // in scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL + debug t => _13; // in scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL } } } } } scope 3 { - debug val => _9; // in scope 3 at $DIR/separate_const_switch.rs:+1:8: +1:10 + debug val => _8; // in scope 3 at $DIR/separate_const_switch.rs:+1:8: +1:10 scope 4 { } } scope 5 (inlined <Result<i32, i32> as Try>::branch) { // at $DIR/separate_const_switch.rs:25:8: 25:10 - debug self => _4; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL - let mut _10: isize; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + debug self => _1; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + let mut _9: isize; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + let _10: i32; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL let _11: i32; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL - let _12: i32; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL - let mut _13: std::result::Result<std::convert::Infallible, i32>; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + let mut _12: std::result::Result<std::convert::Infallible, i32>; // in scope 5 at $SRC_DIR/core/src/result.rs:LL:COL scope 6 { - debug v => _11; // in scope 6 at $SRC_DIR/core/src/result.rs:LL:COL + debug v => _10; // in scope 6 at $SRC_DIR/core/src/result.rs:LL:COL } scope 7 { - debug e => _12; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + debug e => _11; // in scope 7 at $SRC_DIR/core/src/result.rs:LL:COL } } bb0: { - StorageLive(_2); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 StorageLive(_3); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 - StorageLive(_4); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:9 - _4 = _1; // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:9 + StorageLive(_10); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 StorageLive(_11); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 - StorageLive(_12); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 - _10 = discriminant(_4); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL - switchInt(move _10) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + _9 = discriminant(_1); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + switchInt(move _9) -> [0: bb7, 1: bb5, otherwise: bb6]; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL } bb1: { - StorageDead(_12); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 StorageDead(_11); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 - StorageDead(_4); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 + StorageDead(_10); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 _5 = discriminant(_3); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 switchInt(move _5) -> [0: bb2, 1: bb4, otherwise: bb3]; // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 } bb2: { - StorageLive(_9); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 - _9 = ((_3 as Continue).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 - _2 = _9; // scope 4 at $DIR/separate_const_switch.rs:+1:8: +1:10 - StorageDead(_9); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 - _0 = Result::<i32, i32>::Ok(move _2); // scope 0 at $DIR/separate_const_switch.rs:+1:5: +1:11 - StorageDead(_2); // scope 0 at $DIR/separate_const_switch.rs:+1:10: +1:11 + _8 = ((_3 as Continue).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+1:8: +1:10 + _0 = Result::<i32, i32>::Ok(_8); // scope 0 at $DIR/separate_const_switch.rs:+1:5: +1:11 StorageDead(_3); // scope 0 at $DIR/separate_const_switch.rs:+2:1: +2:2 return; // scope 0 at $DIR/separate_const_switch.rs:+2:2: +2:2 } @@ -82,30 +73,19 @@ } bb4: { - StorageLive(_6); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 _6 = ((_3 as Break).0: std::result::Result<std::convert::Infallible, i32>); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 - StorageLive(_8); // scope 2 at $DIR/separate_const_switch.rs:+1:9: +1:10 - _8 = _6; // scope 2 at $DIR/separate_const_switch.rs:+1:9: +1:10 - StorageLive(_14); // scope 2 at $DIR/separate_const_switch.rs:+1:8: +1:10 - _14 = move ((_8 as Err).0: i32); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL - StorageLive(_15); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL - _15 = move _14; // scope 10 at $SRC_DIR/core/src/convert/mod.rs:LL:COL - _0 = Result::<i32, i32>::Err(move _15); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL - StorageDead(_15); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL - StorageDead(_14); // scope 2 at $DIR/separate_const_switch.rs:+1:8: +1:10 - StorageDead(_8); // scope 2 at $DIR/separate_const_switch.rs:+1:9: +1:10 - StorageDead(_6); // scope 0 at $DIR/separate_const_switch.rs:+1:9: +1:10 - StorageDead(_2); // scope 0 at $DIR/separate_const_switch.rs:+1:10: +1:11 + _13 = ((_6 as Err).0: i32); // scope 8 at $SRC_DIR/core/src/result.rs:LL:COL + _0 = Result::<i32, i32>::Err(move _13); // scope 9 at $SRC_DIR/core/src/result.rs:LL:COL StorageDead(_3); // scope 0 at $DIR/separate_const_switch.rs:+2:1: +2:2 return; // scope 0 at $DIR/separate_const_switch.rs:+2:2: +2:2 } bb5: { - _12 = move ((_4 as Err).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL - StorageLive(_13); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - _13 = Result::<Infallible, i32>::Err(move _12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - _3 = ControlFlow::<Result<Infallible, i32>, i32>::Break(move _13); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL - StorageDead(_13); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + _11 = ((_1 as Err).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + StorageLive(_12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + _12 = Result::<Infallible, i32>::Err(move _11); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + _3 = ControlFlow::<Result<Infallible, i32>, i32>::Break(move _12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL + StorageDead(_12); // scope 7 at $SRC_DIR/core/src/result.rs:LL:COL goto -> bb1; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL } @@ -114,8 +94,8 @@ } bb7: { - _11 = move ((_4 as Ok).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL - _3 = ControlFlow::<Result<Infallible, i32>, i32>::Continue(move _11); // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL + _10 = ((_1 as Ok).0: i32); // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL + _3 = ControlFlow::<Result<Infallible, i32>, i32>::Continue(move _10); // scope 6 at $SRC_DIR/core/src/result.rs:LL:COL goto -> bb1; // scope 5 at $SRC_DIR/core/src/result.rs:LL:COL } } diff --git a/tests/mir-opt/separate_const_switch.too_complex.SeparateConstSwitch.diff b/tests/mir-opt/separate_const_switch.too_complex.SeparateConstSwitch.diff index b5e0a66d83f..f86a96dec41 100644 --- a/tests/mir-opt/separate_const_switch.too_complex.SeparateConstSwitch.diff +++ b/tests/mir-opt/separate_const_switch.too_complex.SeparateConstSwitch.diff @@ -34,13 +34,8 @@ } bb1: { - StorageLive(_6); // scope 0 at $DIR/separate_const_switch.rs:+8:17: +8:18 _6 = ((_1 as Err).0: usize); // scope 0 at $DIR/separate_const_switch.rs:+8:17: +8:18 - StorageLive(_7); // scope 2 at $DIR/separate_const_switch.rs:+8:42: +8:43 - _7 = _6; // scope 2 at $DIR/separate_const_switch.rs:+8:42: +8:43 - _2 = ControlFlow::<usize, i32>::Break(move _7); // scope 2 at $DIR/separate_const_switch.rs:+8:23: +8:44 - StorageDead(_7); // scope 2 at $DIR/separate_const_switch.rs:+8:43: +8:44 - StorageDead(_6); // scope 0 at $DIR/separate_const_switch.rs:+8:43: +8:44 + _2 = ControlFlow::<usize, i32>::Break(_6); // scope 2 at $DIR/separate_const_switch.rs:+8:23: +8:44 goto -> bb4; // scope 0 at $DIR/separate_const_switch.rs:+8:43: +8:44 } @@ -49,13 +44,8 @@ } bb3: { - StorageLive(_4); // scope 0 at $DIR/separate_const_switch.rs:+7:16: +7:17 _4 = ((_1 as Ok).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+7:16: +7:17 - StorageLive(_5); // scope 1 at $DIR/separate_const_switch.rs:+7:44: +7:45 - _5 = _4; // scope 1 at $DIR/separate_const_switch.rs:+7:44: +7:45 - _2 = ControlFlow::<usize, i32>::Continue(move _5); // scope 1 at $DIR/separate_const_switch.rs:+7:22: +7:46 - StorageDead(_5); // scope 1 at $DIR/separate_const_switch.rs:+7:45: +7:46 - StorageDead(_4); // scope 0 at $DIR/separate_const_switch.rs:+7:45: +7:46 + _2 = ControlFlow::<usize, i32>::Continue(_4); // scope 1 at $DIR/separate_const_switch.rs:+7:22: +7:46 goto -> bb4; // scope 0 at $DIR/separate_const_switch.rs:+7:45: +7:46 } @@ -73,13 +63,8 @@ } bb6: { - StorageLive(_9); // scope 0 at $DIR/separate_const_switch.rs:+11:31: +11:32 _9 = ((_2 as Continue).0: i32); // scope 0 at $DIR/separate_const_switch.rs:+11:31: +11:32 - StorageLive(_10); // scope 3 at $DIR/separate_const_switch.rs:+11:42: +11:43 - _10 = _9; // scope 3 at $DIR/separate_const_switch.rs:+11:42: +11:43 - _0 = Option::<i32>::Some(move _10); // scope 3 at $DIR/separate_const_switch.rs:+11:37: +11:44 - StorageDead(_10); // scope 3 at $DIR/separate_const_switch.rs:+11:43: +11:44 - StorageDead(_9); // scope 0 at $DIR/separate_const_switch.rs:+11:43: +11:44 + _0 = Option::<i32>::Some(_9); // scope 3 at $DIR/separate_const_switch.rs:+11:37: +11:44 goto -> bb7; // scope 0 at $DIR/separate_const_switch.rs:+11:43: +11:44 } diff --git a/tests/mir-opt/slice_filter.rs b/tests/mir-opt/slice_filter.rs deleted file mode 100644 index be32f40f132..00000000000 --- a/tests/mir-opt/slice_filter.rs +++ /dev/null @@ -1,20 +0,0 @@ -fn main() { - let input = vec![]; - let _variant_a_result = variant_a(&input); - let _variant_b_result = variant_b(&input); -} - -pub fn variant_a(input: &[(usize, usize, usize, usize)]) -> usize { - input.iter().filter(|(a, b, c, d)| a <= c && d <= b || c <= a && b <= d).count() -} - -pub fn variant_b(input: &[(usize, usize, usize, usize)]) -> usize { - input.iter().filter(|&&(a, b, c, d)| a <= c && d <= b || c <= a && b <= d).count() -} - -// EMIT_MIR slice_filter.variant_a-{closure#0}.ReferencePropagation.diff -// EMIT_MIR slice_filter.variant_a-{closure#0}.CopyProp.diff -// EMIT_MIR slice_filter.variant_a-{closure#0}.DestinationPropagation.diff -// EMIT_MIR slice_filter.variant_b-{closure#0}.CopyProp.diff -// EMIT_MIR slice_filter.variant_b-{closure#0}.ReferencePropagation.diff -// EMIT_MIR slice_filter.variant_b-{closure#0}.DestinationPropagation.diff diff --git a/tests/mir-opt/slice_filter.variant_a-{closure#0}.CopyProp.diff b/tests/mir-opt/slice_filter.variant_a-{closure#0}.CopyProp.diff deleted file mode 100644 index 60e5056c7a9..00000000000 --- a/tests/mir-opt/slice_filter.variant_a-{closure#0}.CopyProp.diff +++ /dev/null @@ -1,279 +0,0 @@ -- // MIR for `variant_a::{closure#0}` before CopyProp -+ // MIR for `variant_a::{closure#0}` after CopyProp - - fn variant_a::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:8:25: 8:39], _2: &&(usize, usize, usize, usize)) -> bool { - let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:40: +0:40 - let _3: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 - let _4: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 - let _5: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 - let _6: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 - let mut _7: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:56 - let mut _8: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:46 - let mut _9: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:41 - let mut _10: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:45: +0:46 - let _11: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:45: +0:46 - let mut _12: bool; // in scope 0 at $DIR/slice_filter.rs:+0:50: +0:56 - let mut _13: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:50: +0:51 - let mut _14: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:55: +0:56 - let _15: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:55: +0:56 - let mut _16: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:76 - let mut _17: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:66 - let mut _18: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:61 - let mut _19: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:65: +0:66 - let _20: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:65: +0:66 - let mut _21: bool; // in scope 0 at $DIR/slice_filter.rs:+0:70: +0:76 - let mut _22: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:70: +0:71 - let mut _23: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - let _24: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - let mut _25: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _26: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _27: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _28: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _31: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _32: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _37: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _38: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _43: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _44: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _49: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _50: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 1 { - debug a => _3; // in scope 1 at $DIR/slice_filter.rs:+0:27: +0:28 - debug b => _4; // in scope 1 at $DIR/slice_filter.rs:+0:30: +0:31 - debug c => _5; // in scope 1 at $DIR/slice_filter.rs:+0:33: +0:34 - debug d => _6; // in scope 1 at $DIR/slice_filter.rs:+0:36: +0:37 - scope 2 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:40: 8:46 - debug self => _9; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => _10; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _29: &usize; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _30: &usize; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 3 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _31; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _32; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => _29; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => _30; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _33: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _34: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 4 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:60: 8:66 - debug self => _18; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => _19; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _35: &usize; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _36: &usize; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 5 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _37; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _38; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => _35; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => _36; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _39: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _40: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 6 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:50: 8:56 - debug self => _13; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => _14; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _41: &usize; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _42: &usize; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 7 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _43; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _44; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => _41; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => _42; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _45: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _46: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 8 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:70: 8:76 - debug self => _22; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => _23; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _47: &usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _48: &usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 9 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _49; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _50; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => _47; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => _48; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _51: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _52: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - } - - bb0: { - StorageLive(_3); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 - _25 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 - _3 = &((*_25).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 - StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 - _26 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 - _4 = &((*_26).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 - StorageLive(_5); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 - _27 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 - _5 = &((*_27).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 - StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 - _28 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 - _6 = &((*_28).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 - StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - StorageLive(_8); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:46 - StorageLive(_9); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:41 - _9 = &_3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:41 - StorageLive(_10); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - StorageLive(_11); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - _11 = _5; // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - _10 = &_11; // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - _29 = deref_copy (*_9); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - _30 = deref_copy (*_10); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_31); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _31 = _29; // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_32); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _32 = _30; // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_33); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _33 = (*_31); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _33 = (*_29); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_34); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _34 = (*_32); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _34 = (*_30); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - _8 = Le(move _33, move _34); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_34); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_33); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_32); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_31); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - StorageDead(_10); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - StorageDead(_9); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - switchInt(move _8) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - } - - bb1: { - _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - - bb2: { - StorageLive(_16); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - StorageLive(_17); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:66 - StorageLive(_18); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:61 - _18 = &_5; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:61 - StorageLive(_19); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - StorageLive(_20); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - _20 = _3; // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - _19 = &_20; // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - _35 = deref_copy (*_18); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - _36 = deref_copy (*_19); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_37); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _37 = _35; // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_38); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _38 = _36; // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_39); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _39 = (*_37); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _39 = (*_35); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_40); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _40 = (*_38); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _40 = (*_36); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - _17 = Le(move _39, move _40); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_40); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_39); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_38); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_37); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_20); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - StorageDead(_19); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - StorageDead(_18); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - switchInt(move _17) -> [0: bb6, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb3: { - StorageDead(_16); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_5); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_3); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - return; // scope 0 at $DIR/slice_filter.rs:+0:76: +0:76 - } - - bb4: { - _7 = const false; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - goto -> bb2; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - } - - bb5: { - StorageLive(_12); // scope 1 at $DIR/slice_filter.rs:+0:50: +0:56 - StorageLive(_13); // scope 1 at $DIR/slice_filter.rs:+0:50: +0:51 - _13 = &_6; // scope 1 at $DIR/slice_filter.rs:+0:50: +0:51 - StorageLive(_14); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageLive(_15); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - _15 = _4; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - _14 = &_15; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - _41 = deref_copy (*_13); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - _42 = deref_copy (*_14); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_43); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _43 = _41; // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_44); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _44 = _42; // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_45); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _45 = (*_43); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _45 = (*_41); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_46); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _46 = (*_44); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _46 = (*_42); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - _12 = Le(move _45, move _46); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_46); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_45); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_44); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_43); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_15); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_14); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_13); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - _7 = move _12; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - switchInt(move _7) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - - bb6: { - _16 = const false; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb7: { - StorageLive(_21); // scope 1 at $DIR/slice_filter.rs:+0:70: +0:76 - StorageLive(_22); // scope 1 at $DIR/slice_filter.rs:+0:70: +0:71 - _22 = &_4; // scope 1 at $DIR/slice_filter.rs:+0:70: +0:71 - StorageLive(_23); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageLive(_24); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - _24 = _6; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - _23 = &_24; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - _47 = deref_copy (*_22); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - _48 = deref_copy (*_23); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_49); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _49 = _47; // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageLive(_50); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _50 = _48; // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_51); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _51 = (*_49); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _51 = (*_47); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_52); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _52 = (*_50); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _52 = (*_48); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - _21 = Le(move _51, move _52); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_52); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_51); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_50); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_49); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_24); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_23); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_22); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - _16 = move _21; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb8: { - StorageDead(_21); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_17); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - _0 = move _16; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - } - diff --git a/tests/mir-opt/slice_filter.variant_a-{closure#0}.DestinationPropagation.diff b/tests/mir-opt/slice_filter.variant_a-{closure#0}.DestinationPropagation.diff deleted file mode 100644 index afdcf57815f..00000000000 --- a/tests/mir-opt/slice_filter.variant_a-{closure#0}.DestinationPropagation.diff +++ /dev/null @@ -1,165 +0,0 @@ -- // MIR for `variant_a::{closure#0}` before DestinationPropagation -+ // MIR for `variant_a::{closure#0}` after DestinationPropagation - - fn variant_a::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:8:25: 8:39], _2: &&(usize, usize, usize, usize)) -> bool { - let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:40: +0:40 - let mut _3: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:56 - let mut _4: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:46 - let mut _5: bool; // in scope 0 at $DIR/slice_filter.rs:+0:50: +0:56 - let mut _6: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:76 - let mut _7: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:66 - let mut _8: bool; // in scope 0 at $DIR/slice_filter.rs:+0:70: +0:76 - let mut _9: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _10: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _11: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _12: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - scope 1 { - debug a => &((*_9).0: usize); // in scope 1 at $DIR/slice_filter.rs:+0:27: +0:28 - debug b => &((*_10).1: usize); // in scope 1 at $DIR/slice_filter.rs:+0:30: +0:31 - debug c => &((*_11).2: usize); // in scope 1 at $DIR/slice_filter.rs:+0:33: +0:34 - debug d => &((*_12).3: usize); // in scope 1 at $DIR/slice_filter.rs:+0:36: +0:37 - scope 2 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:40: 8:46 - debug self => &&((*_9).0: usize); // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &&((*_11).2: usize); // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 3 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL - debug self => &((*_9).0: usize); // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &((*_11).2: usize); // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _13: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _14: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 4 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:60: 8:66 - debug self => &&((*_11).2: usize); // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &&((*_9).0: usize); // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 5 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL - debug self => &((*_11).2: usize); // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &((*_9).0: usize); // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _15: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _16: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 6 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:50: 8:56 - debug self => &&((*_12).3: usize); // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &&((*_10).1: usize); // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 7 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL - debug self => &((*_12).3: usize); // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &((*_10).1: usize); // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _17: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _18: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 8 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:70: 8:76 - debug self => &&((*_10).1: usize); // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &&((*_12).3: usize); // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 9 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL - debug self => &((*_10).1: usize); // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - debug other => &((*_12).3: usize); // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _19: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _20: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - } - - bb0: { - _9 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 - _10 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 - _11 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 - _12 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 -- StorageLive(_3); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - StorageLive(_4); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:46 - StorageLive(_13); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - _13 = ((*_9).0: usize); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_14); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - _14 = ((*_11).2: usize); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - _4 = Le(move _13, move _14); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_14); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_13); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - switchInt(move _4) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - } - - bb1: { - _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - - bb2: { -- StorageLive(_6); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:66 - StorageLive(_15); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - _15 = ((*_11).2: usize); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_16); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - _16 = ((*_9).0: usize); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - _7 = Le(move _15, move _16); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_16); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_15); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - switchInt(move _7) -> [0: bb6, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb3: { -- StorageDead(_6); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageDead(_3); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - return; // scope 0 at $DIR/slice_filter.rs:+0:76: +0:76 - } - - bb4: { -- StorageDead(_5); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_4); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - goto -> bb2; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - } - - bb5: { -- StorageLive(_5); // scope 1 at $DIR/slice_filter.rs:+0:50: +0:56 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:50: +0:56 - StorageLive(_17); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - _17 = ((*_12).3: usize); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_18); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - _18 = ((*_10).1: usize); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - _5 = Le(move _17, move _18); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_18); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_17); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _3 = move _5; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 -- StorageDead(_5); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_4); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -- switchInt(move _3) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 -+ switchInt(move _5) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - - bb6: { -- _6 = const false; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 -+ _0 = const false; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb7: { -- StorageLive(_8); // scope 1 at $DIR/slice_filter.rs:+0:70: +0:76 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:70: +0:76 - StorageLive(_19); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - _19 = ((*_10).1: usize); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_20); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - _20 = ((*_12).3: usize); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _8 = Le(move _19, move _20); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _0 = Le(move _19, move _20); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_20); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_19); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _6 = move _8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb8: { -- StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- _0 = move _6; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - } - diff --git a/tests/mir-opt/slice_filter.variant_a-{closure#0}.ReferencePropagation.diff b/tests/mir-opt/slice_filter.variant_a-{closure#0}.ReferencePropagation.diff deleted file mode 100644 index 2534eeef432..00000000000 --- a/tests/mir-opt/slice_filter.variant_a-{closure#0}.ReferencePropagation.diff +++ /dev/null @@ -1,267 +0,0 @@ -- // MIR for `variant_a::{closure#0}` before ReferencePropagation -+ // MIR for `variant_a::{closure#0}` after ReferencePropagation - - fn variant_a::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:8:25: 8:39], _2: &&(usize, usize, usize, usize)) -> bool { - let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:40: +0:40 - let _3: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 - let _4: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 - let _5: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 - let _6: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 - let mut _7: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:56 - let mut _8: bool; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:46 - let mut _9: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:40: +0:41 - let mut _10: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:45: +0:46 - let _11: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:45: +0:46 - let mut _12: bool; // in scope 0 at $DIR/slice_filter.rs:+0:50: +0:56 - let mut _13: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:50: +0:51 - let mut _14: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:55: +0:56 - let _15: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:55: +0:56 - let mut _16: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:76 - let mut _17: bool; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:66 - let mut _18: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:60: +0:61 - let mut _19: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:65: +0:66 - let _20: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:65: +0:66 - let mut _21: bool; // in scope 0 at $DIR/slice_filter.rs:+0:70: +0:76 - let mut _22: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:70: +0:71 - let mut _23: &&usize; // in scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - let _24: &usize; // in scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - let mut _25: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _26: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _27: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _28: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:38 - let mut _31: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _32: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _37: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _38: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _43: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _44: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _49: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _50: &usize; // in scope 0 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 1 { -- debug a => _3; // in scope 1 at $DIR/slice_filter.rs:+0:27: +0:28 -- debug b => _4; // in scope 1 at $DIR/slice_filter.rs:+0:30: +0:31 -- debug c => _5; // in scope 1 at $DIR/slice_filter.rs:+0:33: +0:34 -- debug d => _6; // in scope 1 at $DIR/slice_filter.rs:+0:36: +0:37 -+ debug a => &((*_25).0: usize); // in scope 1 at $DIR/slice_filter.rs:+0:27: +0:28 -+ debug b => &((*_26).1: usize); // in scope 1 at $DIR/slice_filter.rs:+0:30: +0:31 -+ debug c => &((*_27).2: usize); // in scope 1 at $DIR/slice_filter.rs:+0:33: +0:34 -+ debug d => &((*_28).3: usize); // in scope 1 at $DIR/slice_filter.rs:+0:36: +0:37 - scope 2 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:40: 8:46 -- debug self => _9; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _10; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &&((*_25).0: usize); // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &&((*_27).2: usize); // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _29: &usize; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _30: &usize; // in scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 3 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _29; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _30; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &((*_25).0: usize); // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &((*_27).2: usize); // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _33: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _34: usize; // in scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 4 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:60: 8:66 -- debug self => _18; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _19; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &&((*_27).2: usize); // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &&((*_25).0: usize); // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _35: &usize; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _36: &usize; // in scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 5 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _35; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _36; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &((*_27).2: usize); // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &((*_25).0: usize); // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _39: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _40: usize; // in scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 6 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:50: 8:56 -- debug self => _13; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _14; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &&((*_28).3: usize); // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &&((*_26).1: usize); // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _41: &usize; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _42: &usize; // in scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 7 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _41; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _42; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &((*_28).3: usize); // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &((*_26).1: usize); // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _45: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _46: usize; // in scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - scope 8 (inlined cmp::impls::<impl PartialOrd for &usize>::le) { // at $DIR/slice_filter.rs:8:70: 8:76 -- debug self => _22; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _23; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &&((*_26).1: usize); // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &&((*_28).3: usize); // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _47: &usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _48: &usize; // in scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - scope 9 (inlined cmp::impls::<impl PartialOrd for usize>::le) { // at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug self => _47; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- debug other => _48; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug self => &((*_26).1: usize); // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ debug other => &((*_28).3: usize); // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _51: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - let mut _52: usize; // in scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - } - } - } - - bb0: { -- StorageLive(_3); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 - _25 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 -- _3 = &((*_25).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:27: +0:28 -- StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 - _26 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 -- _4 = &((*_26).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:30: +0:31 -- StorageLive(_5); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 - _27 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 -- _5 = &((*_27).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:33: +0:34 -- StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 - _28 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 -- _6 = &((*_28).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:36: +0:37 - StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - StorageLive(_8); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:46 -- StorageLive(_9); // scope 1 at $DIR/slice_filter.rs:+0:40: +0:41 -- _9 = &_3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:41 -- StorageLive(_10); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 -- StorageLive(_11); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 -- _11 = _5; // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 -- _10 = &_11; // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 -- _29 = deref_copy (*_9); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _30 = deref_copy (*_10); // scope 2 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_33); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _33 = (*_29); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _33 = ((*_25).0: usize); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_34); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _34 = (*_30); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _34 = ((*_27).2: usize); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - _8 = Le(move _33, move _34); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_34); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_33); // scope 3 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 -- StorageDead(_10); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 -- StorageDead(_9); // scope 1 at $DIR/slice_filter.rs:+0:45: +0:46 - switchInt(move _8) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - } - - bb1: { - _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - - bb2: { - StorageLive(_16); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - StorageLive(_17); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:66 -- StorageLive(_18); // scope 1 at $DIR/slice_filter.rs:+0:60: +0:61 -- _18 = &_5; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:61 -- StorageLive(_19); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 -- StorageLive(_20); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 -- _20 = _3; // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 -- _19 = &_20; // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 -- _35 = deref_copy (*_18); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _36 = deref_copy (*_19); // scope 4 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_39); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _39 = (*_35); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _39 = ((*_27).2: usize); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_40); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _40 = (*_36); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _40 = ((*_25).0: usize); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - _17 = Le(move _39, move _40); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_40); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_39); // scope 5 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_20); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 -- StorageDead(_19); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 -- StorageDead(_18); // scope 1 at $DIR/slice_filter.rs:+0:65: +0:66 - switchInt(move _17) -> [0: bb6, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb3: { - StorageDead(_16); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageDead(_5); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageDead(_3); // scope 0 at $DIR/slice_filter.rs:+0:75: +0:76 - return; // scope 0 at $DIR/slice_filter.rs:+0:76: +0:76 - } - - bb4: { - _7 = const false; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - goto -> bb2; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - } - - bb5: { - StorageLive(_12); // scope 1 at $DIR/slice_filter.rs:+0:50: +0:56 -- StorageLive(_13); // scope 1 at $DIR/slice_filter.rs:+0:50: +0:51 -- _13 = &_6; // scope 1 at $DIR/slice_filter.rs:+0:50: +0:51 -- StorageLive(_14); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -- StorageLive(_15); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -- _15 = _4; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -- _14 = &_15; // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -- _41 = deref_copy (*_13); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _42 = deref_copy (*_14); // scope 6 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_45); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _45 = (*_41); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _45 = ((*_28).3: usize); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_46); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _46 = (*_42); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _46 = ((*_26).1: usize); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - _12 = Le(move _45, move _46); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_46); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_45); // scope 7 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_15); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -- StorageDead(_14); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 -- StorageDead(_13); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - _7 = move _12; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:56 - StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:55: +0:56 - switchInt(move _7) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - - bb6: { - _16 = const false; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb7: { - StorageLive(_21); // scope 1 at $DIR/slice_filter.rs:+0:70: +0:76 -- StorageLive(_22); // scope 1 at $DIR/slice_filter.rs:+0:70: +0:71 -- _22 = &_4; // scope 1 at $DIR/slice_filter.rs:+0:70: +0:71 -- StorageLive(_23); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageLive(_24); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- _24 = _6; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- _23 = &_24; // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- _47 = deref_copy (*_22); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _48 = deref_copy (*_23); // scope 8 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_51); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _51 = (*_47); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _51 = ((*_26).1: usize); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageLive(_52); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- _52 = (*_48); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -+ _52 = ((*_28).3: usize); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - _21 = Le(move _51, move _52); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_52); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL - StorageDead(_51); // scope 9 at $SRC_DIR/core/src/cmp.rs:LL:COL -- StorageDead(_24); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageDead(_23); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 -- StorageDead(_22); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - _16 = move _21; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:60: +0:76 - } - - bb8: { - StorageDead(_21); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - StorageDead(_17); // scope 1 at $DIR/slice_filter.rs:+0:75: +0:76 - _0 = move _16; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:40: +0:76 - } - } - diff --git a/tests/mir-opt/slice_filter.variant_b-{closure#0}.CopyProp.diff b/tests/mir-opt/slice_filter.variant_b-{closure#0}.CopyProp.diff deleted file mode 100644 index 5e4bdbdfa2e..00000000000 --- a/tests/mir-opt/slice_filter.variant_b-{closure#0}.CopyProp.diff +++ /dev/null @@ -1,139 +0,0 @@ -- // MIR for `variant_b::{closure#0}` before CopyProp -+ // MIR for `variant_b::{closure#0}` after CopyProp - - fn variant_b::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:12:25: 12:41], _2: &&(usize, usize, usize, usize)) -> bool { - let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:42: +0:42 - let _3: usize; // in scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - let _4: usize; // in scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - let _5: usize; // in scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - let _6: usize; // in scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - let mut _7: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:58 - let mut _8: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:48 - let mut _9: usize; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:43 - let mut _10: usize; // in scope 0 at $DIR/slice_filter.rs:+0:47: +0:48 - let mut _11: bool; // in scope 0 at $DIR/slice_filter.rs:+0:52: +0:58 - let mut _12: usize; // in scope 0 at $DIR/slice_filter.rs:+0:52: +0:53 - let mut _13: usize; // in scope 0 at $DIR/slice_filter.rs:+0:57: +0:58 - let mut _14: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:78 - let mut _15: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:68 - let mut _16: usize; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:63 - let mut _17: usize; // in scope 0 at $DIR/slice_filter.rs:+0:67: +0:68 - let mut _18: bool; // in scope 0 at $DIR/slice_filter.rs:+0:72: +0:78 - let mut _19: usize; // in scope 0 at $DIR/slice_filter.rs:+0:72: +0:73 - let mut _20: usize; // in scope 0 at $DIR/slice_filter.rs:+0:77: +0:78 - let mut _21: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _22: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _23: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _24: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - scope 1 { - debug a => _3; // in scope 1 at $DIR/slice_filter.rs:+0:29: +0:30 - debug b => _4; // in scope 1 at $DIR/slice_filter.rs:+0:32: +0:33 - debug c => _5; // in scope 1 at $DIR/slice_filter.rs:+0:35: +0:36 - debug d => _6; // in scope 1 at $DIR/slice_filter.rs:+0:38: +0:39 - } - - bb0: { -- StorageLive(_3); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - _21 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - _3 = ((*_21).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 -- StorageLive(_4); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - _22 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - _4 = ((*_22).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 -- StorageLive(_5); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - _23 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - _5 = ((*_23).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 -- StorageLive(_6); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - _24 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - _6 = ((*_24).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - StorageLive(_8); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 -- StorageLive(_9); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:43 -- _9 = _3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:43 -- StorageLive(_10); // scope 1 at $DIR/slice_filter.rs:+0:47: +0:48 -- _10 = _5; // scope 1 at $DIR/slice_filter.rs:+0:47: +0:48 -- _8 = Le(move _9, move _10); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 -- StorageDead(_10); // scope 1 at $DIR/slice_filter.rs:+0:47: +0:48 -- StorageDead(_9); // scope 1 at $DIR/slice_filter.rs:+0:47: +0:48 -+ _8 = Le(_3, _5); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 - switchInt(move _8) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - } - - bb1: { - _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - - bb2: { - StorageLive(_14); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - StorageLive(_15); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 -- StorageLive(_16); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:63 -- _16 = _5; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:63 -- StorageLive(_17); // scope 1 at $DIR/slice_filter.rs:+0:67: +0:68 -- _17 = _3; // scope 1 at $DIR/slice_filter.rs:+0:67: +0:68 -- _15 = Le(move _16, move _17); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 -- StorageDead(_17); // scope 1 at $DIR/slice_filter.rs:+0:67: +0:68 -- StorageDead(_16); // scope 1 at $DIR/slice_filter.rs:+0:67: +0:68 -+ _15 = Le(_5, _3); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 - switchInt(move _15) -> [0: bb6, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb3: { - StorageDead(_14); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -- StorageDead(_6); // scope 0 at $DIR/slice_filter.rs:+0:77: +0:78 -- StorageDead(_5); // scope 0 at $DIR/slice_filter.rs:+0:77: +0:78 -- StorageDead(_4); // scope 0 at $DIR/slice_filter.rs:+0:77: +0:78 -- StorageDead(_3); // scope 0 at $DIR/slice_filter.rs:+0:77: +0:78 - return; // scope 0 at $DIR/slice_filter.rs:+0:78: +0:78 - } - - bb4: { - _7 = const false; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - goto -> bb2; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - } - - bb5: { - StorageLive(_11); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 -- StorageLive(_12); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:53 -- _12 = _6; // scope 1 at $DIR/slice_filter.rs:+0:52: +0:53 -- StorageLive(_13); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 -- _13 = _4; // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 -- _11 = Le(move _12, move _13); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 -- StorageDead(_13); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 -- StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 -+ _11 = Le(_6, _4); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 - _7 = move _11; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - switchInt(move _7) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - - bb6: { - _14 = const false; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb7: { - StorageLive(_18); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 -- StorageLive(_19); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:73 -- _19 = _4; // scope 1 at $DIR/slice_filter.rs:+0:72: +0:73 -- StorageLive(_20); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -- _20 = _6; // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -- _18 = Le(move _19, move _20); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 -- StorageDead(_20); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -- StorageDead(_19); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -+ _18 = Le(_4, _6); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 - _14 = move _18; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb8: { - StorageDead(_18); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - StorageDead(_15); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - _0 = move _14; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - } - diff --git a/tests/mir-opt/slice_filter.variant_b-{closure#0}.DestinationPropagation.diff b/tests/mir-opt/slice_filter.variant_b-{closure#0}.DestinationPropagation.diff deleted file mode 100644 index 45af6600cd4..00000000000 --- a/tests/mir-opt/slice_filter.variant_b-{closure#0}.DestinationPropagation.diff +++ /dev/null @@ -1,109 +0,0 @@ -- // MIR for `variant_b::{closure#0}` before DestinationPropagation -+ // MIR for `variant_b::{closure#0}` after DestinationPropagation - - fn variant_b::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:12:25: 12:41], _2: &&(usize, usize, usize, usize)) -> bool { - let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:42: +0:42 - let _3: usize; // in scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - let _4: usize; // in scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - let _5: usize; // in scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - let _6: usize; // in scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - let mut _7: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:58 - let mut _8: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:48 - let mut _9: bool; // in scope 0 at $DIR/slice_filter.rs:+0:52: +0:58 - let mut _10: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:78 - let mut _11: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:68 - let mut _12: bool; // in scope 0 at $DIR/slice_filter.rs:+0:72: +0:78 - let mut _13: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _14: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _15: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _16: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - scope 1 { - debug a => _3; // in scope 1 at $DIR/slice_filter.rs:+0:29: +0:30 - debug b => _4; // in scope 1 at $DIR/slice_filter.rs:+0:32: +0:33 - debug c => _5; // in scope 1 at $DIR/slice_filter.rs:+0:35: +0:36 - debug d => _6; // in scope 1 at $DIR/slice_filter.rs:+0:38: +0:39 - } - - bb0: { - _13 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - _3 = ((*_13).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - _14 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - _4 = ((*_14).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - _15 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - _5 = ((*_15).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - _16 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - _6 = ((*_16).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 -- StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - StorageLive(_8); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 - _8 = Le(_3, _5); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 - switchInt(move _8) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - } - - bb1: { - _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - - bb2: { -- StorageLive(_10); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - StorageLive(_11); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 - _11 = Le(_5, _3); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 - switchInt(move _11) -> [0: bb6, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb3: { -- StorageDead(_10); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -- StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - return; // scope 0 at $DIR/slice_filter.rs:+0:78: +0:78 - } - - bb4: { -- StorageDead(_9); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - goto -> bb2; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - } - - bb5: { -- StorageLive(_9); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 - _9 = Le(_6, _4); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 -- _7 = move _9; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 -- StorageDead(_9); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 -- switchInt(move _7) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 -+ switchInt(move _9) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - - bb6: { -- _10 = const false; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 -+ _0 = const false; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb7: { -- StorageLive(_12); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 -- _12 = Le(_4, _6); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 -- _10 = move _12; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 -+ _0 = Le(_4, _6); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb8: { -- StorageDead(_12); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 -- _0 = move _10; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 -+ nop; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - } - diff --git a/tests/mir-opt/slice_filter.variant_b-{closure#0}.ReferencePropagation.diff b/tests/mir-opt/slice_filter.variant_b-{closure#0}.ReferencePropagation.diff deleted file mode 100644 index d1241c6b024..00000000000 --- a/tests/mir-opt/slice_filter.variant_b-{closure#0}.ReferencePropagation.diff +++ /dev/null @@ -1,103 +0,0 @@ -- // MIR for `variant_b::{closure#0}` before ReferencePropagation -+ // MIR for `variant_b::{closure#0}` after ReferencePropagation - - fn variant_b::{closure#0}(_1: &mut [closure@$DIR/slice_filter.rs:12:25: 12:41], _2: &&(usize, usize, usize, usize)) -> bool { - let mut _0: bool; // return place in scope 0 at $DIR/slice_filter.rs:+0:42: +0:42 - let _3: usize; // in scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - let _4: usize; // in scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - let _5: usize; // in scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - let _6: usize; // in scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - let mut _7: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:58 - let mut _8: bool; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:48 - let mut _9: usize; // in scope 0 at $DIR/slice_filter.rs:+0:42: +0:43 - let mut _10: usize; // in scope 0 at $DIR/slice_filter.rs:+0:47: +0:48 - let mut _11: bool; // in scope 0 at $DIR/slice_filter.rs:+0:52: +0:58 - let mut _12: usize; // in scope 0 at $DIR/slice_filter.rs:+0:52: +0:53 - let mut _13: usize; // in scope 0 at $DIR/slice_filter.rs:+0:57: +0:58 - let mut _14: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:78 - let mut _15: bool; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:68 - let mut _16: usize; // in scope 0 at $DIR/slice_filter.rs:+0:62: +0:63 - let mut _17: usize; // in scope 0 at $DIR/slice_filter.rs:+0:67: +0:68 - let mut _18: bool; // in scope 0 at $DIR/slice_filter.rs:+0:72: +0:78 - let mut _19: usize; // in scope 0 at $DIR/slice_filter.rs:+0:72: +0:73 - let mut _20: usize; // in scope 0 at $DIR/slice_filter.rs:+0:77: +0:78 - let mut _21: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _22: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _23: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - let mut _24: &(usize, usize, usize, usize); // in scope 0 at $DIR/slice_filter.rs:+0:26: +0:40 - scope 1 { - debug a => _3; // in scope 1 at $DIR/slice_filter.rs:+0:29: +0:30 - debug b => _4; // in scope 1 at $DIR/slice_filter.rs:+0:32: +0:33 - debug c => _5; // in scope 1 at $DIR/slice_filter.rs:+0:35: +0:36 - debug d => _6; // in scope 1 at $DIR/slice_filter.rs:+0:38: +0:39 - } - - bb0: { - _21 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - _3 = ((*_21).0: usize); // scope 0 at $DIR/slice_filter.rs:+0:29: +0:30 - _22 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - _4 = ((*_22).1: usize); // scope 0 at $DIR/slice_filter.rs:+0:32: +0:33 - _23 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - _5 = ((*_23).2: usize); // scope 0 at $DIR/slice_filter.rs:+0:35: +0:36 - _24 = deref_copy (*_2); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - _6 = ((*_24).3: usize); // scope 0 at $DIR/slice_filter.rs:+0:38: +0:39 - StorageLive(_7); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - StorageLive(_8); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 - _8 = Le(_3, _5); // scope 1 at $DIR/slice_filter.rs:+0:42: +0:48 - switchInt(move _8) -> [0: bb4, otherwise: bb5]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - } - - bb1: { - _0 = const true; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - - bb2: { - StorageLive(_14); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - StorageLive(_15); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 - _15 = Le(_5, _3); // scope 1 at $DIR/slice_filter.rs:+0:62: +0:68 - switchInt(move _15) -> [0: bb6, otherwise: bb7]; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb3: { - StorageDead(_14); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - StorageDead(_7); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - return; // scope 0 at $DIR/slice_filter.rs:+0:78: +0:78 - } - - bb4: { - _7 = const false; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - goto -> bb2; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - } - - bb5: { - StorageLive(_11); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 - _11 = Le(_6, _4); // scope 1 at $DIR/slice_filter.rs:+0:52: +0:58 - _7 = move _11; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:58 - StorageDead(_11); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - StorageDead(_8); // scope 1 at $DIR/slice_filter.rs:+0:57: +0:58 - switchInt(move _7) -> [0: bb2, otherwise: bb1]; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - - bb6: { - _14 = const false; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb7: { - StorageLive(_18); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 - _18 = Le(_4, _6); // scope 1 at $DIR/slice_filter.rs:+0:72: +0:78 - _14 = move _18; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - goto -> bb8; // scope 1 at $DIR/slice_filter.rs:+0:62: +0:78 - } - - bb8: { - StorageDead(_18); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - StorageDead(_15); // scope 1 at $DIR/slice_filter.rs:+0:77: +0:78 - _0 = move _14; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - goto -> bb3; // scope 1 at $DIR/slice_filter.rs:+0:42: +0:78 - } - } - diff --git a/tests/rustdoc-gui/theme-in-history.goml b/tests/rustdoc-gui/theme-in-history.goml index 8fcd0ecd309..42c5b5e6e69 100644 --- a/tests/rustdoc-gui/theme-in-history.goml +++ b/tests/rustdoc-gui/theme-in-history.goml @@ -7,7 +7,7 @@ set-local-storage: { } // We reload the page so the local storage settings are being used. reload: -assert-css: ("body", { "background-color": "rgb(53, 53, 53)" }) +assert-css: ("body", { "background-color": "#353535" }) assert-local-storage: { "rustdoc-theme": "dark" } // Now we go to the settings page. @@ -15,7 +15,7 @@ go-to: "file://" + |DOC_PATH| + "/settings.html" wait-for: "#settings" // We change the theme to "light". click: "#theme-light" -wait-for-css: ("body", { "background-color": "rgb(255, 255, 255)" }) +wait-for-css: ("body", { "background-color": "white" }) assert-local-storage: { "rustdoc-theme": "light" } // We go back in history. @@ -23,5 +23,5 @@ history-go-back: // Confirm that we're not on the settings page. assert-false: "#settings" // Check that the current theme is still "light". -assert-css: ("body", { "background-color": "rgb(255, 255, 255)" }) +assert-css: ("body", { "background-color": "white" }) assert-local-storage: { "rustdoc-theme": "light" } diff --git a/tests/rustdoc/double-hyphen-to-dash.rs b/tests/rustdoc/double-hyphen-to-dash.rs new file mode 100644 index 00000000000..66905f90cfc --- /dev/null +++ b/tests/rustdoc/double-hyphen-to-dash.rs @@ -0,0 +1,9 @@ +// This test ensures that `--` (double-hyphen) is correctly converted into `–` (dash). + +#![crate_name = "foo"] + +// @has 'foo/index.html' '//*[@class="desc docblock-short"]' '–' +// @has 'foo/struct.Bar.html' '//*[@class="docblock"]' '–' + +/// -- +pub struct Bar; diff --git a/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.rs b/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.rs index 39e34d73f9a..ffbefce48d3 100644 --- a/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.rs +++ b/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.rs @@ -538,7 +538,7 @@ struct LabelWithTrailingPath { #[diag(no_crate_example, code = "E0123")] struct LabelWithTrailingNameValue { #[label(no_crate_label, foo = "...")] - //~^ ERROR invalid nested attribute + //~^ ERROR only `no_span` is a valid nested attribute span: Span, } @@ -546,7 +546,7 @@ struct LabelWithTrailingNameValue { #[diag(no_crate_example, code = "E0123")] struct LabelWithTrailingList { #[label(no_crate_label, foo("..."))] - //~^ ERROR invalid nested attribute + //~^ ERROR only `no_span` is a valid nested attribute span: Span, } diff --git a/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr b/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr index 801e4b5793c..1398f9c96bf 100644 --- a/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr +++ b/tests/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr @@ -243,7 +243,7 @@ error: invalid nested attribute LL | #[suggestion(nonsense = "bar")] | ^^^^^^^^ | - = help: only `style`, `code` and `applicability` are valid nested attributes + = help: only `no_span`, `style`, `code` and `applicability` are valid nested attributes error: suggestion without `code = "..."` --> $DIR/diagnostic-derive.rs:234:5 @@ -257,7 +257,7 @@ error: invalid nested attribute LL | #[suggestion(msg = "bar")] | ^^^ | - = help: only `style`, `code` and `applicability` are valid nested attributes + = help: only `no_span`, `style`, `code` and `applicability` are valid nested attributes error: suggestion without `code = "..."` --> $DIR/diagnostic-derive.rs:243:5 @@ -335,13 +335,13 @@ error: a diagnostic slug must be the first argument to the attribute LL | #[label(no_crate_label, foo)] | ^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/diagnostic-derive.rs:540:29 | LL | #[label(no_crate_label, foo = "...")] | ^^^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/diagnostic-derive.rs:548:29 | LL | #[label(no_crate_label, foo("..."))] diff --git a/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.rs b/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.rs index 1bfbb60015d..38af5b0f9fb 100644 --- a/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.rs +++ b/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.rs @@ -85,7 +85,7 @@ struct F { #[derive(Subdiagnostic)] #[label(bug = "...")] -//~^ ERROR invalid nested attribute +//~^ ERROR only `no_span` is a valid nested attribute //~| ERROR diagnostic slug must be first argument struct G { #[primary_span] @@ -104,7 +104,7 @@ struct H { #[derive(Subdiagnostic)] #[label(slug = 4)] -//~^ ERROR invalid nested attribute +//~^ ERROR only `no_span` is a valid nested attribute //~| ERROR diagnostic slug must be first argument struct J { #[primary_span] @@ -114,7 +114,7 @@ struct J { #[derive(Subdiagnostic)] #[label(slug("..."))] -//~^ ERROR invalid nested attribute +//~^ ERROR only `no_span` is a valid nested attribute //~| ERROR diagnostic slug must be first argument struct K { #[primary_span] @@ -143,7 +143,7 @@ struct M { #[derive(Subdiagnostic)] #[label(no_crate_example, code = "...")] -//~^ ERROR invalid nested attribute +//~^ ERROR only `no_span` is a valid nested attribute struct N { #[primary_span] span: Span, @@ -152,7 +152,7 @@ struct N { #[derive(Subdiagnostic)] #[label(no_crate_example, applicability = "machine-applicable")] -//~^ ERROR invalid nested attribute +//~^ ERROR only `no_span` is a valid nested attribute struct O { #[primary_span] span: Span, @@ -224,7 +224,7 @@ enum T { enum U { #[label(code = "...")] //~^ ERROR diagnostic slug must be first argument of a `#[label(...)]` attribute - //~| ERROR invalid nested attribute + //~| ERROR only `no_span` is a valid nested attribute A { #[primary_span] span: Span, diff --git a/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.stderr b/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.stderr index fca4f506890..5ddc8edd745 100644 --- a/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.stderr +++ b/tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.stderr @@ -26,7 +26,7 @@ error: `#[label = ...]` is not a valid attribute LL | #[label = "..."] | ^^^^^^^^^^^^^^^^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/subdiagnostic-derive.rs:87:9 | LL | #[label(bug = "...")] @@ -44,7 +44,7 @@ error: unexpected literal in nested attribute, expected ident LL | #[label("...")] | ^^^^^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/subdiagnostic-derive.rs:106:9 | LL | #[label(slug = 4)] @@ -56,7 +56,7 @@ error: diagnostic slug must be first argument of a `#[label(...)]` attribute LL | #[label(slug = 4)] | ^^^^^^^^^^^^^^^^^^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/subdiagnostic-derive.rs:116:9 | LL | #[label(slug("..."))] @@ -74,13 +74,13 @@ error: unexpected end of input, unexpected token in nested attribute, expected i LL | #[label()] | ^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/subdiagnostic-derive.rs:145:27 | LL | #[label(no_crate_example, code = "...")] | ^^^^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/subdiagnostic-derive.rs:154:27 | LL | #[label(no_crate_example, applicability = "machine-applicable")] @@ -116,7 +116,7 @@ error: `#[bar(...)]` is not a valid attribute LL | #[bar("...")] | ^^^^^^^^^^^^^ -error: invalid nested attribute +error: only `no_span` is a valid nested attribute --> $DIR/subdiagnostic-derive.rs:225:13 | LL | #[label(code = "...")] @@ -312,7 +312,7 @@ error: invalid nested attribute LL | #[multipart_suggestion(no_crate_example, code = "...", applicability = "machine-applicable")] | ^^^^ | - = help: only `style` and `applicability` are valid nested attributes + = help: only `no_span`, `style` and `applicability` are valid nested attributes error: multipart suggestion without any `#[suggestion_part(...)]` fields --> $DIR/subdiagnostic-derive.rs:540:1 diff --git a/tests/ui/auto-traits/issue-23080-2.stderr b/tests/ui/auto-traits/issue-23080-2.current.stderr index fed485612da..a57c6d9b0cb 100644 --- a/tests/ui/auto-traits/issue-23080-2.stderr +++ b/tests/ui/auto-traits/issue-23080-2.current.stderr @@ -1,5 +1,5 @@ error[E0380]: auto traits cannot have associated items - --> $DIR/issue-23080-2.rs:5:10 + --> $DIR/issue-23080-2.rs:8:10 | LL | unsafe auto trait Trait { | ----- auto traits cannot have associated items diff --git a/tests/ui/auto-traits/issue-23080-2.next.stderr b/tests/ui/auto-traits/issue-23080-2.next.stderr new file mode 100644 index 00000000000..a57c6d9b0cb --- /dev/null +++ b/tests/ui/auto-traits/issue-23080-2.next.stderr @@ -0,0 +1,11 @@ +error[E0380]: auto traits cannot have associated items + --> $DIR/issue-23080-2.rs:8:10 + | +LL | unsafe auto trait Trait { + | ----- auto traits cannot have associated items +LL | type Output; + | -----^^^^^^- help: remove these associated items + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0380`. diff --git a/tests/ui/auto-traits/issue-23080-2.rs b/tests/ui/auto-traits/issue-23080-2.rs index cb4cf6de1ef..882b8f39384 100644 --- a/tests/ui/auto-traits/issue-23080-2.rs +++ b/tests/ui/auto-traits/issue-23080-2.rs @@ -1,3 +1,6 @@ +// revisions: current next +//[next] compile-flags: -Ztrait-solver=next + #![feature(auto_traits)] #![feature(negative_impls)] diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr b/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr index de5704ee429..48910b82a10 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr @@ -11,6 +11,10 @@ note: required by a bound in `check` | LL | fn check(_: impl std::marker::ConstParamTy) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check` +help: use parentheses to call this function + | +LL | check(main()); + | ++ error[E0277]: `[closure@$DIR/const_param_ty_bad.rs:8:11: 8:13]` can't be used as a const parameter type --> $DIR/const_param_ty_bad.rs:8:11 @@ -25,6 +29,10 @@ note: required by a bound in `check` | LL | fn check(_: impl std::marker::ConstParamTy) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check` +help: use parentheses to call this closure + | +LL | check(|| {}()); + | ++ error[E0277]: `fn()` can't be used as a const parameter type --> $DIR/const_param_ty_bad.rs:9:11 @@ -39,6 +47,10 @@ note: required by a bound in `check` | LL | fn check(_: impl std::marker::ConstParamTy) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check` +help: use parentheses to call this function pointer + | +LL | check(main as fn()()); + | ++ error[E0277]: `&mut ()` can't be used as a const parameter type --> $DIR/const_param_ty_bad.rs:10:11 @@ -48,11 +60,17 @@ LL | check(&mut ()); | | | required by a bound introduced by this call | + = note: `ConstParamTy` is implemented for `&()`, but not for `&mut ()` note: required by a bound in `check` --> $DIR/const_param_ty_bad.rs:4:18 | LL | fn check(_: impl std::marker::ConstParamTy) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check` +help: consider removing the leading `&`-reference + | +LL - check(&mut ()); +LL + check(()); + | error[E0277]: `*mut ()` can't be used as a const parameter type --> $DIR/const_param_ty_bad.rs:11:11 @@ -62,6 +80,7 @@ LL | check(&mut () as *mut ()); | | | required by a bound introduced by this call | + = help: the trait `ConstParamTy` is implemented for `()` note: required by a bound in `check` --> $DIR/const_param_ty_bad.rs:4:18 | @@ -76,6 +95,7 @@ LL | check(&() as *const ()); | | | required by a bound introduced by this call | + = help: the trait `ConstParamTy` is implemented for `()` note: required by a bound in `check` --> $DIR/const_param_ty_bad.rs:4:18 | diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_good.rs b/tests/ui/const-generics/adt_const_params/const_param_ty_good.rs index 87ae83dd966..100ab332a40 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_good.rs +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_good.rs @@ -49,5 +49,7 @@ fn main() { check::<D<u8>>(); check::<D<[&[bool]; 8]>>(); - // FIXME: test tuples + check::<()>(); + check::<(i32,)>(); + check::<(D<u8>, D<i32>)>(); } diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.rs b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.rs index 37986de481f..08f7c5cb542 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.rs +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.rs @@ -9,9 +9,11 @@ struct CantParam(ImplementsConstParamTy); impl std::marker::ConstParamTy for CantParam {} //~^ error: the type `CantParam` does not `#[derive(Eq)]` +//~| error: the type `CantParam` does not `#[derive(PartialEq)]` #[derive(std::marker::ConstParamTy)] //~^ error: the type `CantParamDerive` does not `#[derive(Eq)]` +//~| error: the type `CantParamDerive` does not `#[derive(PartialEq)]` struct CantParamDerive(ImplementsConstParamTy); fn check<T: std::marker::ConstParamTy>() {} diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.stderr b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.stderr index 52701d55914..43c5b96dc7c 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.stderr +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.stderr @@ -1,3 +1,12 @@ +error[E0277]: the type `CantParam` does not `#[derive(PartialEq)]` + --> $DIR/const_param_ty_impl_no_structural_eq.rs:10:36 + | +LL | impl std::marker::ConstParamTy for CantParam {} + | ^^^^^^^^^ the trait `StructuralPartialEq` is not implemented for `CantParam` + | +note: required by a bound in `ConstParamTy` + --> $SRC_DIR/core/src/marker.rs:LL:COL + error[E0277]: the type `CantParam` does not `#[derive(Eq)]` --> $DIR/const_param_ty_impl_no_structural_eq.rs:10:36 | @@ -7,8 +16,18 @@ LL | impl std::marker::ConstParamTy for CantParam {} note: required by a bound in `ConstParamTy` --> $SRC_DIR/core/src/marker.rs:LL:COL +error[E0277]: the type `CantParamDerive` does not `#[derive(PartialEq)]` + --> $DIR/const_param_ty_impl_no_structural_eq.rs:14:10 + | +LL | #[derive(std::marker::ConstParamTy)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralPartialEq` is not implemented for `CantParamDerive` + | +note: required by a bound in `ConstParamTy` + --> $SRC_DIR/core/src/marker.rs:LL:COL + = note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the type `CantParamDerive` does not `#[derive(Eq)]` - --> $DIR/const_param_ty_impl_no_structural_eq.rs:13:10 + --> $DIR/const_param_ty_impl_no_structural_eq.rs:14:10 | LL | #[derive(std::marker::ConstParamTy)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralEq` is not implemented for `CantParamDerive` @@ -17,6 +36,6 @@ note: required by a bound in `ConstParamTy` --> $SRC_DIR/core/src/marker.rs:LL:COL = note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 2 previous errors +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.rs b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.rs index d70377a20c1..c04e96c569b 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.rs +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.rs @@ -14,6 +14,7 @@ impl Eq for Union {} impl std::marker::StructuralEq for Union {} impl std::marker::ConstParamTy for Union {} +//~^ ERROR the type `Union` does not `#[derive(PartialEq)]` #[derive(std::marker::ConstParamTy)] //~^ ERROR this trait cannot be derived for unions diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.stderr b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.stderr index 29370304605..985b933c40c 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.stderr +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.stderr @@ -1,8 +1,18 @@ error: this trait cannot be derived for unions - --> $DIR/const_param_ty_impl_union.rs:18:10 + --> $DIR/const_param_ty_impl_union.rs:19:10 | LL | #[derive(std::marker::ConstParamTy)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to previous error +error[E0277]: the type `Union` does not `#[derive(PartialEq)]` + --> $DIR/const_param_ty_impl_union.rs:16:36 + | +LL | impl std::marker::ConstParamTy for Union {} + | ^^^^^ the trait `StructuralPartialEq` is not implemented for `Union` + | +note: required by a bound in `ConstParamTy` + --> $SRC_DIR/core/src/marker.rs:LL:COL + +error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/const-generics/const-param-with-additional-obligations.rs b/tests/ui/const-generics/const-param-with-additional-obligations.rs new file mode 100644 index 00000000000..f53cf85cdd3 --- /dev/null +++ b/tests/ui/const-generics/const-param-with-additional-obligations.rs @@ -0,0 +1,17 @@ +#![feature(adt_const_params)] +#![allow(incomplete_features)] + +use std::marker::ConstParamTy; + +#[derive(Eq, PartialEq)] +struct Foo<T>(T); + +trait Other {} + +impl<T> ConstParamTy for Foo<T> where T: Other + ConstParamTy {} + +fn foo<const N: Foo<u8>>() {} +//~^ ERROR `Foo<u8>` must implement `ConstParamTy` to be used as the type of a const generic parameter +//~| NOTE `u8` must implement `Other`, but it does not + +fn main() {} diff --git a/tests/ui/const-generics/const-param-with-additional-obligations.stderr b/tests/ui/const-generics/const-param-with-additional-obligations.stderr new file mode 100644 index 00000000000..f7ec4d57401 --- /dev/null +++ b/tests/ui/const-generics/const-param-with-additional-obligations.stderr @@ -0,0 +1,11 @@ +error[E0741]: `Foo<u8>` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/const-param-with-additional-obligations.rs:13:17 + | +LL | fn foo<const N: Foo<u8>>() {} + | ^^^^^^^ + | + = note: `u8` must implement `Other`, but it does not + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0741`. diff --git a/tests/ui/const-generics/float-generic.adt_const_params.stderr b/tests/ui/const-generics/float-generic.adt_const_params.stderr index fef5ef0d1fa..6fe5390471d 100644 --- a/tests/ui/const-generics/float-generic.adt_const_params.stderr +++ b/tests/ui/const-generics/float-generic.adt_const_params.stderr @@ -3,8 +3,6 @@ error[E0741]: `f32` is forbidden as the type of a const generic parameter | LL | fn foo<const F: f32>() {} | ^^^ - | - = note: floats do not derive `Eq` or `Ord`, which are required for const parameters error: aborting due to previous error diff --git a/tests/ui/const-generics/forbid-non-structural_match-types.rs b/tests/ui/const-generics/forbid-non-structural_match-types.rs index 6ae9d5cfbb5..4fec2a9f32f 100644 --- a/tests/ui/const-generics/forbid-non-structural_match-types.rs +++ b/tests/ui/const-generics/forbid-non-structural_match-types.rs @@ -1,13 +1,15 @@ #![feature(adt_const_params)] #![allow(incomplete_features)] -#[derive(PartialEq, Eq)] +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] struct A; struct B<const X: A>; // ok struct C; -struct D<const X: C>; //~ ERROR `C` must be annotated with `#[derive(PartialEq, Eq)]` +struct D<const X: C>; //~ ERROR `C` must implement `ConstParamTy` to be used as the type of a const generic parameter fn main() {} diff --git a/tests/ui/const-generics/forbid-non-structural_match-types.stderr b/tests/ui/const-generics/forbid-non-structural_match-types.stderr index 81b9bdfbd60..0efb9e9d3c2 100644 --- a/tests/ui/const-generics/forbid-non-structural_match-types.stderr +++ b/tests/ui/const-generics/forbid-non-structural_match-types.stderr @@ -1,8 +1,14 @@ -error[E0741]: `C` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter - --> $DIR/forbid-non-structural_match-types.rs:11:19 +error[E0741]: `C` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/forbid-non-structural_match-types.rs:13:19 | LL | struct D<const X: C>; - | ^ `C` doesn't derive both `PartialEq` and `Eq` + | ^ + | +help: add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct + | +LL + #[derive(ConstParamTy, PartialEq, Eq)] +LL | struct C; + | error: aborting due to previous error diff --git a/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.full.stderr b/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.full.stderr index 1d10dfdf10c..c478718b4cc 100644 --- a/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.full.stderr +++ b/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.full.stderr @@ -7,7 +7,7 @@ LL | struct ArithArrayLen<const N: usize>([u32; 0 + N]); = help: try adding a `where` bound using this expression: `where [(); 0 + N]:` error: overly complex generic constant - --> $DIR/array-size-in-generic-struct-param.rs:19:15 + --> $DIR/array-size-in-generic-struct-param.rs:23:15 | LL | arr: [u8; CFG.arr_size], | ^^^^^^^^^^^^ field access is not supported in generic constants diff --git a/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.min.stderr b/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.min.stderr index 18e9135d072..956e9c9c988 100644 --- a/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.min.stderr +++ b/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.min.stderr @@ -8,7 +8,7 @@ LL | struct ArithArrayLen<const N: usize>([u32; 0 + N]); = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions error: generic parameters may not be used in const operations - --> $DIR/array-size-in-generic-struct-param.rs:19:15 + --> $DIR/array-size-in-generic-struct-param.rs:23:15 | LL | arr: [u8; CFG.arr_size], | ^^^ cannot perform const operation using `CFG` @@ -17,7 +17,7 @@ LL | arr: [u8; CFG.arr_size], = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions error: `Config` is forbidden as the type of a const generic parameter - --> $DIR/array-size-in-generic-struct-param.rs:17:21 + --> $DIR/array-size-in-generic-struct-param.rs:21:21 | LL | struct B<const CFG: Config> { | ^^^^^^ diff --git a/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.rs b/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.rs index 7d3fe413c17..33ca6dcb304 100644 --- a/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.rs +++ b/tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.rs @@ -9,7 +9,11 @@ struct ArithArrayLen<const N: usize>([u32; 0 + N]); //[full]~^ ERROR unconstrained generic constant //[min]~^^ ERROR generic parameters may not be used in const operations +#[cfg(full)] +use std::marker::ConstParamTy; + #[derive(PartialEq, Eq)] +#[cfg_attr(full, derive(ConstParamTy))] struct Config { arr_size: usize, } diff --git a/tests/ui/const-generics/generic_const_exprs/issue-80742.stderr b/tests/ui/const-generics/generic_const_exprs/issue-80742.stderr index 6aa8ee13b79..dc3a400cbaa 100644 --- a/tests/ui/const-generics/generic_const_exprs/issue-80742.stderr +++ b/tests/ui/const-generics/generic_const_exprs/issue-80742.stderr @@ -1,4 +1,4 @@ -error[E0080]: evaluation of `Inline::<dyn std::fmt::Debug>::{constant#0}` failed +error[E0080]: evaluation of `Inline::<dyn Debug>::{constant#0}` failed --> $SRC_DIR/core/src/mem/mod.rs:LL:COL | = note: size_of called on unsized type `dyn Debug` @@ -35,7 +35,7 @@ help: consider relaxing the type parameter's implicit `Sized` bound LL | impl<T: ?Sized> Inline<T> | ++++++++ -error[E0080]: evaluation of `Inline::<dyn std::fmt::Debug>::{constant#0}` failed +error[E0080]: evaluation of `Inline::<dyn Debug>::{constant#0}` failed --> $SRC_DIR/core/src/mem/mod.rs:LL:COL | = note: size_of called on unsized type `dyn Debug` diff --git a/tests/ui/const-generics/invalid-enum.rs b/tests/ui/const-generics/invalid-enum.rs index cb6d05349db..fcfad300c44 100644 --- a/tests/ui/const-generics/invalid-enum.rs +++ b/tests/ui/const-generics/invalid-enum.rs @@ -1,7 +1,9 @@ #![feature(adt_const_params)] #![allow(incomplete_features)] -#[derive(PartialEq, Eq)] +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] enum CompileFlag { A, B, diff --git a/tests/ui/const-generics/invalid-enum.stderr b/tests/ui/const-generics/invalid-enum.stderr index 0d3643f6f89..7e8a632b34f 100644 --- a/tests/ui/const-generics/invalid-enum.stderr +++ b/tests/ui/const-generics/invalid-enum.stderr @@ -1,5 +1,5 @@ error[E0573]: expected type, found variant `CompileFlag::A` - --> $DIR/invalid-enum.rs:21:12 + --> $DIR/invalid-enum.rs:23:12 | LL | test_1::<CompileFlag::A>(); | ^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | test_1::<CompileFlag::A>(); | help: try using the variant's enum: `CompileFlag` error[E0573]: expected type, found variant `CompileFlag::A` - --> $DIR/invalid-enum.rs:25:15 + --> $DIR/invalid-enum.rs:27:15 | LL | test_2::<_, CompileFlag::A>(0); | ^^^^^^^^^^^^^^ @@ -17,7 +17,7 @@ LL | test_2::<_, CompileFlag::A>(0); | help: try using the variant's enum: `CompileFlag` error[E0573]: expected type, found variant `CompileFlag::A` - --> $DIR/invalid-enum.rs:29:18 + --> $DIR/invalid-enum.rs:31:18 | LL | let _: Example<CompileFlag::A, _> = Example { x: 0 }; | ^^^^^^^^^^^^^^ @@ -26,7 +26,7 @@ LL | let _: Example<CompileFlag::A, _> = Example { x: 0 }; | help: try using the variant's enum: `CompileFlag` error[E0747]: unresolved item provided when a constant was expected - --> $DIR/invalid-enum.rs:29:18 + --> $DIR/invalid-enum.rs:31:18 | LL | let _: Example<CompileFlag::A, _> = Example { x: 0 }; | ^^^^^^^^^^^^^^ @@ -37,7 +37,7 @@ LL | let _: Example<{ CompileFlag::A }, _> = Example { x: 0 }; | + + error[E0747]: type provided when a constant was expected - --> $DIR/invalid-enum.rs:33:18 + --> $DIR/invalid-enum.rs:35:18 | LL | let _: Example<Example::ASSOC_FLAG, _> = Example { x: 0 }; | ^^^^^^^^^^^^^^^^^^^ @@ -48,7 +48,7 @@ LL | let _: Example<{ Example::ASSOC_FLAG }, _> = Example { x: 0 }; | + + error[E0747]: unresolved item provided when a constant was expected - --> $DIR/invalid-enum.rs:21:12 + --> $DIR/invalid-enum.rs:23:12 | LL | test_1::<CompileFlag::A>(); | ^^^^^^^^^^^^^^ @@ -59,7 +59,7 @@ LL | test_1::<{ CompileFlag::A }>(); | + + error[E0747]: unresolved item provided when a constant was expected - --> $DIR/invalid-enum.rs:25:15 + --> $DIR/invalid-enum.rs:27:15 | LL | test_2::<_, CompileFlag::A>(0); | ^^^^^^^^^^^^^^ diff --git a/tests/ui/const-generics/issue-66451.rs b/tests/ui/const-generics/issue-66451.rs index 3335f7d5984..c8d5515e987 100644 --- a/tests/ui/const-generics/issue-66451.rs +++ b/tests/ui/const-generics/issue-66451.rs @@ -1,13 +1,15 @@ #![feature(adt_const_params)] #![allow(incomplete_features)] -#[derive(Debug, PartialEq, Eq)] +use std::marker::ConstParamTy; + +#[derive(Debug, PartialEq, Eq, ConstParamTy)] struct Foo { value: i32, nested: &'static Bar<i32>, } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, ConstParamTy)] struct Bar<T>(T); struct Test<const F: Foo>; diff --git a/tests/ui/const-generics/issue-66451.stderr b/tests/ui/const-generics/issue-66451.stderr index e0cb0b661ff..946d5148667 100644 --- a/tests/ui/const-generics/issue-66451.stderr +++ b/tests/ui/const-generics/issue-66451.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/issue-66451.rs:27:10 + --> $DIR/issue-66451.rs:29:10 | LL | let y: Test<{ | ____________- diff --git a/tests/ui/const-generics/issue-80471.rs b/tests/ui/const-generics/issue-80471.rs index d0af8a5eaa8..fa6f1fde435 100644 --- a/tests/ui/const-generics/issue-80471.rs +++ b/tests/ui/const-generics/issue-80471.rs @@ -8,6 +8,6 @@ enum Nat { } fn foo<const N: Nat>() {} -//~^ ERROR `Box<Nat>` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter +//~^ ERROR `Nat` must implement `ConstParamTy` to be used as the type of a const generic parameter fn main() {} diff --git a/tests/ui/const-generics/issue-80471.stderr b/tests/ui/const-generics/issue-80471.stderr index b89706710bc..3b7143de543 100644 --- a/tests/ui/const-generics/issue-80471.stderr +++ b/tests/ui/const-generics/issue-80471.stderr @@ -7,11 +7,17 @@ LL | #![feature(adt_const_params)] = note: see issue #95174 <https://github.com/rust-lang/rust/issues/95174> for more information = note: `#[warn(incomplete_features)]` on by default -error[E0741]: `Box<Nat>` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter +error[E0741]: `Nat` must implement `ConstParamTy` to be used as the type of a const generic parameter --> $DIR/issue-80471.rs:10:17 | LL | fn foo<const N: Nat>() {} | ^^^ + | +help: add `#[derive(ConstParamTy)]` to the struct + | +LL + #[derive(ConstParamTy)] +LL | enum Nat { + | error: aborting due to previous error; 1 warning emitted diff --git a/tests/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr b/tests/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr index 16fabd1e88f..e2d8c5ca0e1 100644 --- a/tests/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr +++ b/tests/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr @@ -1,8 +1,10 @@ -error[E0741]: `(dyn A + 'static)` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter +error[E0741]: `&'static (dyn A + 'static)` can't be used as a const parameter type --> $DIR/issue-63322-forbid-dyn.rs:9:18 | LL | fn test<const T: &'static dyn A>() { | ^^^^^^^^^^^^^^ + | + = note: `(dyn A + 'static)` must implement `ConstParamTy`, but it does not error: aborting due to previous error diff --git a/tests/ui/const-generics/issues/issue-63322-forbid-dyn.rs b/tests/ui/const-generics/issues/issue-63322-forbid-dyn.rs index 116c3fcfb21..8bc35ab3d37 100644 --- a/tests/ui/const-generics/issues/issue-63322-forbid-dyn.rs +++ b/tests/ui/const-generics/issues/issue-63322-forbid-dyn.rs @@ -7,7 +7,7 @@ struct B; impl A for B {} fn test<const T: &'static dyn A>() { - //[full]~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` to be used + //[full]~^ ERROR `&'static (dyn A + 'static)` can't be used as a const parameter type //[min]~^^ ERROR `&'static (dyn A + 'static)` is forbidden unimplemented!() } diff --git a/tests/ui/const-generics/issues/issue-71381.full.stderr b/tests/ui/const-generics/issues/issue-71381.full.stderr index 962eaf75b98..b6460e0017f 100644 --- a/tests/ui/const-generics/issues/issue-71381.full.stderr +++ b/tests/ui/const-generics/issues/issue-71381.full.stderr @@ -14,19 +14,6 @@ LL | const FN: unsafe extern "C" fn(Args), | = note: type parameters may not be used in the type of const parameters -error[E0741]: using function pointers as const generic parameters is forbidden - --> $DIR/issue-71381.rs:14:61 - | -LL | pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error[E0741]: using function pointers as const generic parameters is forbidden - --> $DIR/issue-71381.rs:23:19 - | -LL | const FN: unsafe extern "C" fn(Args), - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors -Some errors have detailed explanations: E0741, E0770. -For more information about an error, try `rustc --explain E0741`. +For more information about this error, try `rustc --explain E0770`. diff --git a/tests/ui/const-generics/issues/issue-71381.rs b/tests/ui/const-generics/issues/issue-71381.rs index 66f819dbe06..8a878efb42a 100644 --- a/tests/ui/const-generics/issues/issue-71381.rs +++ b/tests/ui/const-generics/issues/issue-71381.rs @@ -12,8 +12,8 @@ unsafe extern "C" fn pass(args: PassArg) { impl Test { pub fn call_me<Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args)>(&self) { - //~^ ERROR: using function pointers as const generic parameters is forbidden - //~| ERROR: the type of const parameters must not depend on other generic parameters + //[min]~^ ERROR: using function pointers as const generic parameters is forbidden + //~^^ ERROR: the type of const parameters must not depend on other generic parameters self.0 = Self::trampiline::<Args, IDX, FN> as _ } @@ -21,8 +21,8 @@ impl Test { Args: Sized, const IDX: usize, const FN: unsafe extern "C" fn(Args), - //~^ ERROR: using function pointers as const generic parameters is forbidden - //~| ERROR: the type of const parameters must not depend on other generic parameters + //[min]~^ ERROR: using function pointers as const generic parameters is forbidden + //~^^ ERROR: the type of const parameters must not depend on other generic parameters >( args: Args, ) { @@ -31,6 +31,6 @@ impl Test { } fn main() { - let x = Test(); + let x = Test(std::ptr::null()); x.call_me::<PassArg, 30, pass>() } diff --git a/tests/ui/const-generics/issues/issue-71611.full.stderr b/tests/ui/const-generics/issues/issue-71611.full.stderr index e109459f2be..b55f410a023 100644 --- a/tests/ui/const-generics/issues/issue-71611.full.stderr +++ b/tests/ui/const-generics/issues/issue-71611.full.stderr @@ -6,13 +6,6 @@ LL | fn func<A, const F: fn(inner: A)>(outer: A) { | = note: type parameters may not be used in the type of const parameters -error[E0741]: using function pointers as const generic parameters is forbidden - --> $DIR/issue-71611.rs:5:21 - | -LL | fn func<A, const F: fn(inner: A)>(outer: A) { - | ^^^^^^^^^^^^ - -error: aborting due to 2 previous errors +error: aborting due to previous error -Some errors have detailed explanations: E0741, E0770. -For more information about an error, try `rustc --explain E0741`. +For more information about this error, try `rustc --explain E0770`. diff --git a/tests/ui/const-generics/issues/issue-71611.rs b/tests/ui/const-generics/issues/issue-71611.rs index fbb91ca18aa..c917f66818b 100644 --- a/tests/ui/const-generics/issues/issue-71611.rs +++ b/tests/ui/const-generics/issues/issue-71611.rs @@ -3,8 +3,8 @@ #![cfg_attr(full, allow(incomplete_features))] fn func<A, const F: fn(inner: A)>(outer: A) { - //~^ ERROR: using function pointers as const generic parameters is forbidden - //~| ERROR: the type of const parameters must not depend on other generic parameters + //[min]~^ ERROR: using function pointers as const generic parameters is forbidden + //~^^ ERROR: the type of const parameters must not depend on other generic parameters F(outer); } diff --git a/tests/ui/const-generics/issues/issue-74255.min.stderr b/tests/ui/const-generics/issues/issue-74255.min.stderr index b462d84487e..bbcf8682b71 100644 --- a/tests/ui/const-generics/issues/issue-74255.min.stderr +++ b/tests/ui/const-generics/issues/issue-74255.min.stderr @@ -1,5 +1,5 @@ error: `IceEnum` is forbidden as the type of a const generic parameter - --> $DIR/issue-74255.rs:14:31 + --> $DIR/issue-74255.rs:18:31 | LL | fn ice_struct_fn<const I: IceEnum>() {} | ^^^^^^^ diff --git a/tests/ui/const-generics/issues/issue-74255.rs b/tests/ui/const-generics/issues/issue-74255.rs index 0e523926fb0..60b2fd37c44 100644 --- a/tests/ui/const-generics/issues/issue-74255.rs +++ b/tests/ui/const-generics/issues/issue-74255.rs @@ -3,7 +3,11 @@ #![cfg_attr(full, feature(adt_const_params))] #![cfg_attr(full, allow(incomplete_features))] +#[cfg(full)] +use std::marker::ConstParamTy; + #[derive(PartialEq, Eq)] +#[cfg_attr(full, derive(ConstParamTy))] enum IceEnum { Variant } diff --git a/tests/ui/const-generics/issues/issue-74950.min.stderr b/tests/ui/const-generics/issues/issue-74950.min.stderr index 729ecc2022c..c37ee93d420 100644 --- a/tests/ui/const-generics/issues/issue-74950.min.stderr +++ b/tests/ui/const-generics/issues/issue-74950.min.stderr @@ -1,5 +1,5 @@ error: `Inner` is forbidden as the type of a const generic parameter - --> $DIR/issue-74950.rs:17:23 + --> $DIR/issue-74950.rs:20:23 | LL | struct Outer<const I: Inner>; | ^^^^^ @@ -8,7 +8,7 @@ LL | struct Outer<const I: Inner>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `Inner` is forbidden as the type of a const generic parameter - --> $DIR/issue-74950.rs:17:23 + --> $DIR/issue-74950.rs:20:23 | LL | struct Outer<const I: Inner>; | ^^^^^ @@ -17,7 +17,7 @@ LL | struct Outer<const I: Inner>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `Inner` is forbidden as the type of a const generic parameter - --> $DIR/issue-74950.rs:17:23 + --> $DIR/issue-74950.rs:20:23 | LL | struct Outer<const I: Inner>; | ^^^^^ @@ -26,7 +26,7 @@ LL | struct Outer<const I: Inner>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `Inner` is forbidden as the type of a const generic parameter - --> $DIR/issue-74950.rs:17:23 + --> $DIR/issue-74950.rs:20:23 | LL | struct Outer<const I: Inner>; | ^^^^^ @@ -35,7 +35,7 @@ LL | struct Outer<const I: Inner>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `Inner` is forbidden as the type of a const generic parameter - --> $DIR/issue-74950.rs:17:23 + --> $DIR/issue-74950.rs:20:23 | LL | struct Outer<const I: Inner>; | ^^^^^ diff --git a/tests/ui/const-generics/issues/issue-74950.rs b/tests/ui/const-generics/issues/issue-74950.rs index 3e1ca4735db..43bb322656b 100644 --- a/tests/ui/const-generics/issues/issue-74950.rs +++ b/tests/ui/const-generics/issues/issue-74950.rs @@ -3,8 +3,11 @@ #![cfg_attr(full, feature(adt_const_params))] #![cfg_attr(full, allow(incomplete_features))] +#[cfg(full)] +use std::marker::ConstParamTy; #[derive(PartialEq, Eq)] +#[cfg_attr(full, derive(ConstParamTy))] struct Inner; // Note: We emit the error 5 times if we don't deduplicate: diff --git a/tests/ui/const-generics/issues/issue-87076.rs b/tests/ui/const-generics/issues/issue-87076.rs index 8a567678b82..a32c1f965f8 100644 --- a/tests/ui/const-generics/issues/issue-87076.rs +++ b/tests/ui/const-generics/issues/issue-87076.rs @@ -3,7 +3,9 @@ #![feature(adt_const_params)] #![allow(incomplete_features)] -#[derive(PartialEq, Eq)] +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] pub struct UnitDims { pub time: u8, pub length: u8, diff --git a/tests/ui/const-generics/issues/issue-97278.rs b/tests/ui/const-generics/issues/issue-97278.rs index da0a9776fd4..8e7a1fcd995 100644 --- a/tests/ui/const-generics/issues/issue-97278.rs +++ b/tests/ui/const-generics/issues/issue-97278.rs @@ -9,6 +9,6 @@ enum Bar { } fn test<const BAR: Bar>() {} -//~^ ERROR `Arc<i32>` must be annotated with `#[derive(PartialEq, Eq)]` +//~^ ERROR `Bar` must implement `ConstParamTy` to be used as the type of a const generic parameter fn main() {} diff --git a/tests/ui/const-generics/issues/issue-97278.stderr b/tests/ui/const-generics/issues/issue-97278.stderr index ff13cb505ab..31e92f840e1 100644 --- a/tests/ui/const-generics/issues/issue-97278.stderr +++ b/tests/ui/const-generics/issues/issue-97278.stderr @@ -1,8 +1,14 @@ -error[E0741]: `Arc<i32>` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter +error[E0741]: `Bar` must implement `ConstParamTy` to be used as the type of a const generic parameter --> $DIR/issue-97278.rs:11:20 | LL | fn test<const BAR: Bar>() {} | ^^^ + | +help: add `#[derive(ConstParamTy)]` to the struct + | +LL + #[derive(ConstParamTy)] +LL | enum Bar { + | error: aborting due to previous error diff --git a/tests/ui/const-generics/issues/issue-99641.rs b/tests/ui/const-generics/issues/issue-99641.rs index fae6d3fc41f..dd075a6ad05 100644 --- a/tests/ui/const-generics/issues/issue-99641.rs +++ b/tests/ui/const-generics/issues/issue-99641.rs @@ -3,10 +3,10 @@ fn main() { pub struct Color<const WHITE: (fn(),)>; - //~^ ERROR using function pointers + //~^ ERROR `(fn(),)` can't be used as a const parameter type impl<const WHITE: (fn(),)> Color<WHITE> { - //~^ ERROR using function pointers + //~^ ERROR `(fn(),)` can't be used as a const parameter type pub fn new() -> Self { Color::<WHITE> } diff --git a/tests/ui/const-generics/issues/issue-99641.stderr b/tests/ui/const-generics/issues/issue-99641.stderr index 349ebba08d5..800aec3ef2c 100644 --- a/tests/ui/const-generics/issues/issue-99641.stderr +++ b/tests/ui/const-generics/issues/issue-99641.stderr @@ -1,14 +1,18 @@ -error[E0741]: using function pointers as const generic parameters is forbidden +error[E0741]: `(fn(),)` can't be used as a const parameter type --> $DIR/issue-99641.rs:5:35 | LL | pub struct Color<const WHITE: (fn(),)>; | ^^^^^^^ + | + = note: `fn()` must implement `ConstParamTy`, but it does not -error[E0741]: using function pointers as const generic parameters is forbidden +error[E0741]: `(fn(),)` can't be used as a const parameter type --> $DIR/issue-99641.rs:8:23 | LL | impl<const WHITE: (fn(),)> Color<WHITE> { | ^^^^^^^ + | + = note: `fn()` must implement `ConstParamTy`, but it does not error: aborting due to 2 previous errors diff --git a/tests/ui/const-generics/overlapping_impls.rs b/tests/ui/const-generics/overlapping_impls.rs index e599eadd8cf..2ce6c4a823c 100644 --- a/tests/ui/const-generics/overlapping_impls.rs +++ b/tests/ui/const-generics/overlapping_impls.rs @@ -2,7 +2,8 @@ #![allow(incomplete_features)] #![feature(adt_const_params)] #![feature(generic_const_exprs)] -use std::marker::PhantomData; + +use std::marker::{ConstParamTy, PhantomData}; struct Foo<const I: i32, const J: i32> {} @@ -22,7 +23,7 @@ pub struct Foo2<const P: Protocol, T> { _marker: PhantomData<T>, } -#[derive(PartialEq, Eq)] +#[derive(PartialEq, Eq, ConstParamTy)] pub enum Protocol { Variant1, Variant2, diff --git a/tests/ui/const-generics/std/const-generics-range.full.stderr b/tests/ui/const-generics/std/const-generics-range.full.stderr new file mode 100644 index 00000000000..5bf48ad7385 --- /dev/null +++ b/tests/ui/const-generics/std/const-generics-range.full.stderr @@ -0,0 +1,39 @@ +error[E0741]: `std::ops::Range<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/const-generics-range.rs:8:24 + | +LL | struct _Range<const R: std::ops::Range<usize>>; + | ^^^^^^^^^^^^^^^^^^^^^^ + +error[E0741]: `RangeFrom<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/const-generics-range.rs:13:28 + | +LL | struct _RangeFrom<const R: std::ops::RangeFrom<usize>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0741]: `RangeFull` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/const-generics-range.rs:18:28 + | +LL | struct _RangeFull<const R: std::ops::RangeFull>; + | ^^^^^^^^^^^^^^^^^^^ + +error[E0741]: `RangeInclusive<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/const-generics-range.rs:24:33 + | +LL | struct _RangeInclusive<const R: std::ops::RangeInclusive<usize>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0741]: `RangeTo<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/const-generics-range.rs:29:26 + | +LL | struct _RangeTo<const R: std::ops::RangeTo<usize>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0741]: `RangeToInclusive<usize>` must implement `ConstParamTy` to be used as the type of a const generic parameter + --> $DIR/const-generics-range.rs:34:35 + | +LL | struct _RangeToInclusive<const R: std::ops::RangeToInclusive<usize>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0741`. diff --git a/tests/ui/const-generics/std/const-generics-range.min.stderr b/tests/ui/const-generics/std/const-generics-range.min.stderr index d4b2ad6fd0c..53fca6e884a 100644 --- a/tests/ui/const-generics/std/const-generics-range.min.stderr +++ b/tests/ui/const-generics/std/const-generics-range.min.stderr @@ -1,5 +1,5 @@ error: `std::ops::Range<usize>` is forbidden as the type of a const generic parameter - --> $DIR/const-generics-range.rs:7:24 + --> $DIR/const-generics-range.rs:8:24 | LL | struct _Range<const R: std::ops::Range<usize>>; | ^^^^^^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | struct _Range<const R: std::ops::Range<usize>>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `RangeFrom<usize>` is forbidden as the type of a const generic parameter - --> $DIR/const-generics-range.rs:12:28 + --> $DIR/const-generics-range.rs:13:28 | LL | struct _RangeFrom<const R: std::ops::RangeFrom<usize>>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -17,7 +17,7 @@ LL | struct _RangeFrom<const R: std::ops::RangeFrom<usize>>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `RangeFull` is forbidden as the type of a const generic parameter - --> $DIR/const-generics-range.rs:17:28 + --> $DIR/const-generics-range.rs:18:28 | LL | struct _RangeFull<const R: std::ops::RangeFull>; | ^^^^^^^^^^^^^^^^^^^ @@ -26,7 +26,7 @@ LL | struct _RangeFull<const R: std::ops::RangeFull>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `RangeInclusive<usize>` is forbidden as the type of a const generic parameter - --> $DIR/const-generics-range.rs:23:33 + --> $DIR/const-generics-range.rs:24:33 | LL | struct _RangeInclusive<const R: std::ops::RangeInclusive<usize>>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -35,7 +35,7 @@ LL | struct _RangeInclusive<const R: std::ops::RangeInclusive<usize>>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `RangeTo<usize>` is forbidden as the type of a const generic parameter - --> $DIR/const-generics-range.rs:28:26 + --> $DIR/const-generics-range.rs:29:26 | LL | struct _RangeTo<const R: std::ops::RangeTo<usize>>; | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -44,7 +44,7 @@ LL | struct _RangeTo<const R: std::ops::RangeTo<usize>>; = help: more complex types are supported with `#![feature(adt_const_params)]` error: `RangeToInclusive<usize>` is forbidden as the type of a const generic parameter - --> $DIR/const-generics-range.rs:33:35 + --> $DIR/const-generics-range.rs:34:35 | LL | struct _RangeToInclusive<const R: std::ops::RangeToInclusive<usize>>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/const-generics/std/const-generics-range.rs b/tests/ui/const-generics/std/const-generics-range.rs index 46c06f312b9..bda59f3ec45 100644 --- a/tests/ui/const-generics/std/const-generics-range.rs +++ b/tests/ui/const-generics/std/const-generics-range.rs @@ -1,4 +1,5 @@ -// [full] check-pass +// [full] known-bug: unknown + // revisions: full min #![cfg_attr(full, feature(adt_const_params))] #![cfg_attr(full, allow(incomplete_features))] diff --git a/tests/ui/const-ptr/forbidden_slices.stderr b/tests/ui/const-ptr/forbidden_slices.stderr index 817cfb0acf9..22c3dfa64fe 100644 --- a/tests/ui/const-ptr/forbidden_slices.stderr +++ b/tests/ui/const-ptr/forbidden_slices.stderr @@ -54,11 +54,11 @@ error[E0080]: it is undefined behavior to use this value LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) }; | ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { HEX_DUMP } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/forbidden_slices.rs:30:1 @@ -98,7 +98,7 @@ LL | from_raw_parts(ptr, 1) error[E0080]: could not evaluate static initializer --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | - = note: out-of-bounds offset_from: null pointer is a dangling pointer (it has no provenance) + = note: out-of-bounds `offset_from`: null pointer is a dangling pointer (it has no provenance) | note: inside `ptr::const_ptr::<impl *const u32>::sub_ptr` --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -156,11 +156,11 @@ error[E0080]: it is undefined behavior to use this value LL | pub static R5: &[u8] = unsafe { | ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { HEX_DUMP } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/forbidden_slices.rs:63:1 diff --git a/tests/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr b/tests/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr index 8f3b3d5f700..23ba2c2f535 100644 --- a/tests/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr +++ b/tests/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr @@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/alloc_intrinsic_errors.rs:9:17 | LL | let _ = intrinsics::const_allocate(4, 3) as *mut i32; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ align has to be a power of 2, `3` is not a power of 2 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid align passed to `const_allocate`: 3 is not a power of 2 | note: inside `foo` --> $DIR/alloc_intrinsic_errors.rs:9:17 diff --git a/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.rs b/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.rs index f746f27000f..9ae906bbb73 100644 --- a/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.rs +++ b/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.rs @@ -4,11 +4,11 @@ use std::intrinsics; const FOO: *const i32 = foo(); -//~^ ERROR untyped pointers are not allowed in constant +//~^ ERROR unsupported untyped pointer in constant const fn foo() -> &'static i32 { let t = unsafe { - let i = intrinsics::const_allocate(4, 4) as * mut i32; + let i = intrinsics::const_allocate(4, 4) as *mut i32; *i = 20; i }; diff --git a/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.stderr b/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.stderr index 00ab0dfc557..2103f842bd5 100644 --- a/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.stderr +++ b/tests/ui/consts/const-eval/heap/alloc_intrinsic_nontransient_fail.stderr @@ -1,8 +1,10 @@ -error: untyped pointers are not allowed in constant +error: unsupported untyped pointer in constant --> $DIR/alloc_intrinsic_nontransient_fail.rs:6:1 | LL | const FOO: *const i32 = foo(); | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: memory only reachable via raw pointers is not supported error: aborting due to previous error diff --git a/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.rs b/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.rs index 77871c394b7..1354b3c33b3 100644 --- a/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.rs +++ b/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.rs @@ -3,7 +3,7 @@ #![feature(const_mut_refs)] use std::intrinsics; -const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32}; -//~^ error: untyped pointers are not allowed in constant +const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32 }; +//~^ error: unsupported untyped pointer in constant fn main() {} diff --git a/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.stderr b/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.stderr index 36002b850b7..b6276647350 100644 --- a/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.stderr +++ b/tests/ui/consts/const-eval/heap/alloc_intrinsic_untyped.stderr @@ -1,8 +1,10 @@ -error: untyped pointers are not allowed in constant +error: unsupported untyped pointer in constant --> $DIR/alloc_intrinsic_untyped.rs:6:1 | -LL | const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32}; +LL | const BAR: *mut i32 = unsafe { intrinsics::const_allocate(4, 4) as *mut i32 }; | ^^^^^^^^^^^^^^^^^^^ + | + = note: memory only reachable via raw pointers is not supported error: aborting due to previous error diff --git a/tests/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.stderr b/tests/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.stderr index 650b409b190..4c23957a1f8 100644 --- a/tests/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.stderr +++ b/tests/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.stderr @@ -20,7 +20,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/dealloc_intrinsic_incorrect_layout.rs:25:5 | LL | intrinsics::const_deallocate(ptr, 4, 3); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ align has to be a power of 2, `3` is not a power of 2 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid align passed to `const_deallocate`: 3 is not a power of 2 error: aborting due to 4 previous errors diff --git a/tests/ui/consts/const-eval/raw-bytes.32bit.stderr b/tests/ui/consts/const-eval/raw-bytes.32bit.stderr index a93b561e5be..c0ddaceea4c 100644 --- a/tests/ui/consts/const-eval/raw-bytes.32bit.stderr +++ b/tests/ui/consts/const-eval/raw-bytes.32bit.stderr @@ -35,7 +35,7 @@ error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:44:1 | LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type Never + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type `Never` | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: 1, align: 1) { @@ -290,11 +290,11 @@ error[E0080]: it is undefined behavior to use this value LL | const MYSTR_NO_INIT_ISSUE83182: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: 8, align: 4) { ╾ALLOC_ID╼ 01 00 00 00 │ ╾──╼.... } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:152:1 @@ -529,11 +529,11 @@ error[E0080]: it is undefined behavior to use this value LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, mem::size_of::<&u32>()) }; | ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: 8, align: 4) { ╾ALLOC_ID╼ 04 00 00 00 │ ╾──╼.... } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:221:1 @@ -574,11 +574,11 @@ error[E0080]: it is undefined behavior to use this value LL | pub static R5: &[u8] = unsafe { | ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: 8, align: 4) { ╾ALLOC_ID╼ 04 00 00 00 │ ╾──╼.... } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:242:1 diff --git a/tests/ui/consts/const-eval/raw-bytes.64bit.stderr b/tests/ui/consts/const-eval/raw-bytes.64bit.stderr index a32d4863a38..20c905878e0 100644 --- a/tests/ui/consts/const-eval/raw-bytes.64bit.stderr +++ b/tests/ui/consts/const-eval/raw-bytes.64bit.stderr @@ -35,7 +35,7 @@ error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:44:1 | LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type Never + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type `Never` | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: 1, align: 1) { @@ -290,11 +290,11 @@ error[E0080]: it is undefined behavior to use this value LL | const MYSTR_NO_INIT_ISSUE83182: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: 16, align: 8) { ╾ALLOC_ID╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........ } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:152:1 @@ -529,11 +529,11 @@ error[E0080]: it is undefined behavior to use this value LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, mem::size_of::<&u32>()) }; | ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: 16, align: 8) { ╾ALLOC_ID╼ 08 00 00 00 00 00 00 00 │ ╾──────╼........ } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:221:1 @@ -574,11 +574,11 @@ error[E0080]: it is undefined behavior to use this value LL | pub static R5: &[u8] = unsafe { | ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: 16, align: 8) { ╾ALLOC_ID╼ 08 00 00 00 00 00 00 00 │ ╾──────╼........ } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: it is undefined behavior to use this value --> $DIR/raw-bytes.rs:242:1 diff --git a/tests/ui/consts/const-eval/ub-enum.32bit.stderr b/tests/ui/consts/const-eval/ub-enum.32bit.stderr index 3ad1ac974c8..1810600b785 100644 --- a/tests/ui/consts/const-eval/ub-enum.32bit.stderr +++ b/tests/ui/consts/const-eval/ub-enum.32bit.stderr @@ -86,7 +86,7 @@ error[E0080]: it is undefined behavior to use this value --> $DIR/ub-enum.rs:83:1 | LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type Never + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type `Never` | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { @@ -108,7 +108,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/ub-enum.rs:96:77 | LL | const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe { mem::transmute(0u64) }; - | ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Ok)>.0.1: encountered a value of uninhabited type Never + | ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Ok)>.0.1: encountered a value of uninhabited type `Never` error[E0080]: evaluation of constant value failed --> $DIR/ub-enum.rs:98:77 diff --git a/tests/ui/consts/const-eval/ub-enum.64bit.stderr b/tests/ui/consts/const-eval/ub-enum.64bit.stderr index a66706d1af9..fb40babb0b9 100644 --- a/tests/ui/consts/const-eval/ub-enum.64bit.stderr +++ b/tests/ui/consts/const-eval/ub-enum.64bit.stderr @@ -86,7 +86,7 @@ error[E0080]: it is undefined behavior to use this value --> $DIR/ub-enum.rs:83:1 | LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type Never + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(D)>.0: encountered a value of uninhabited type `Never` | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { @@ -108,7 +108,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/ub-enum.rs:96:77 | LL | const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe { mem::transmute(0u64) }; - | ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Ok)>.0.1: encountered a value of uninhabited type Never + | ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Ok)>.0.1: encountered a value of uninhabited type `Never` error[E0080]: evaluation of constant value failed --> $DIR/ub-enum.rs:98:77 diff --git a/tests/ui/consts/const-eval/ub-uninhabit.stderr b/tests/ui/consts/const-eval/ub-uninhabit.stderr index 733975fc0e9..f1ad0f04d3d 100644 --- a/tests/ui/consts/const-eval/ub-uninhabit.stderr +++ b/tests/ui/consts/const-eval/ub-uninhabit.stderr @@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/ub-uninhabit.rs:16:35 | LL | const BAD_BAD_BAD: Bar = unsafe { MaybeUninit { uninit: () }.init }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a value of uninhabited type Bar + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a value of uninhabited type `Bar` error[E0080]: it is undefined behavior to use this value --> $DIR/ub-uninhabit.rs:19:1 @@ -19,7 +19,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/ub-uninhabit.rs:22:42 | LL | const BAD_BAD_ARRAY: [Bar; 1] = unsafe { MaybeUninit { uninit: () }.init }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered a value of uninhabited type Bar + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered a value of uninhabited type `Bar` error: aborting due to 3 previous errors diff --git a/tests/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr b/tests/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr index 74bc6317c80..b423edbdcec 100644 --- a/tests/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr +++ b/tests/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr @@ -28,7 +28,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/validate_uninhabited_zsts.rs:21:42 | LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; - | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered a value of uninhabited type empty::Void + | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered a value of uninhabited type `Void` warning: the type `empty::Empty` does not permit zero-initialization --> $DIR/validate_uninhabited_zsts.rs:21:42 diff --git a/tests/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr b/tests/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr index 74bc6317c80..b423edbdcec 100644 --- a/tests/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr +++ b/tests/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr @@ -28,7 +28,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/validate_uninhabited_zsts.rs:21:42 | LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; - | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered a value of uninhabited type empty::Void + | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered a value of uninhabited type `Void` warning: the type `empty::Empty` does not permit zero-initialization --> $DIR/validate_uninhabited_zsts.rs:21:42 diff --git a/tests/ui/consts/issue-64506.stderr b/tests/ui/consts/issue-64506.stderr index 31a5b1df837..2fe84245b3e 100644 --- a/tests/ui/consts/issue-64506.stderr +++ b/tests/ui/consts/issue-64506.stderr @@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/issue-64506.rs:16:22 | LL | let x = unsafe { Foo { b: () }.a }; - | ^^^^^^^^^^^^^^^ constructing invalid value at .inner: encountered a value of uninhabited type AnonPipe + | ^^^^^^^^^^^^^^^ constructing invalid value at .inner: encountered a value of uninhabited type `AnonPipe` error: aborting due to previous error diff --git a/tests/ui/consts/issue-83182.stderr b/tests/ui/consts/issue-83182.stderr index 1d578f910c0..ca4e0f7aa02 100644 --- a/tests/ui/consts/issue-83182.stderr +++ b/tests/ui/consts/issue-83182.stderr @@ -4,11 +4,11 @@ error[E0080]: it is undefined behavior to use this value LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { HEX_DUMP } + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error: aborting due to previous error diff --git a/tests/ui/consts/issue-miri-1910.stderr b/tests/ui/consts/issue-miri-1910.stderr index fb758d406b5..67797e6fb5a 100644 --- a/tests/ui/consts/issue-miri-1910.stderr +++ b/tests/ui/consts/issue-miri-1910.stderr @@ -3,8 +3,6 @@ error[E0080]: evaluation of constant value failed | = note: unable to turn pointer into raw bytes | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported note: inside `std::ptr::read::<u8>` --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL note: inside `ptr::const_ptr::<impl *const u8>::read` @@ -14,6 +12,8 @@ note: inside `C` | LL | (&foo as *const _ as *const u8).add(one_and_a_half_pointers).read(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error: aborting due to previous error diff --git a/tests/ui/consts/miri_unleashed/assoc_const.stderr b/tests/ui/consts/miri_unleashed/assoc_const.stderr index 8e22cb74bf5..d97097d352a 100644 --- a/tests/ui/consts/miri_unleashed/assoc_const.stderr +++ b/tests/ui/consts/miri_unleashed/assoc_const.stderr @@ -1,4 +1,4 @@ -error[E0080]: evaluation of `<std::string::String as Bar<std::vec::Vec<u32>, std::string::String>>::F` failed +error[E0080]: evaluation of `<String as Bar<Vec<u32>, String>>::F` failed --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL | = note: calling non-const function `<Vec<u32> as Drop>::drop` diff --git a/tests/ui/consts/miri_unleashed/raw_mutable_const.rs b/tests/ui/consts/miri_unleashed/raw_mutable_const.rs index 5f8ec4e6e29..adb1f8bf3ec 100644 --- a/tests/ui/consts/miri_unleashed/raw_mutable_const.rs +++ b/tests/ui/consts/miri_unleashed/raw_mutable_const.rs @@ -3,6 +3,6 @@ use std::cell::UnsafeCell; const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _; -//~^ ERROR: untyped pointers are not allowed in constant +//~^ ERROR: unsupported untyped pointer in constant fn main() {} diff --git a/tests/ui/consts/miri_unleashed/raw_mutable_const.stderr b/tests/ui/consts/miri_unleashed/raw_mutable_const.stderr index f8dc11d695f..5acdcdd95e8 100644 --- a/tests/ui/consts/miri_unleashed/raw_mutable_const.stderr +++ b/tests/ui/consts/miri_unleashed/raw_mutable_const.stderr @@ -1,8 +1,10 @@ -error: untyped pointers are not allowed in constant +error: unsupported untyped pointer in constant --> $DIR/raw_mutable_const.rs:5:1 | LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: memory only reachable via raw pointers is not supported warning: skipping const checks | diff --git a/tests/ui/consts/missing_span_in_backtrace.stderr b/tests/ui/consts/missing_span_in_backtrace.stderr index e6d3d51990d..fcfb9fbb3f8 100644 --- a/tests/ui/consts/missing_span_in_backtrace.stderr +++ b/tests/ui/consts/missing_span_in_backtrace.stderr @@ -3,8 +3,6 @@ error[E0080]: evaluation of constant value failed | = note: unable to copy parts of a pointer from memory at ALLOC_ID | - = help: this code performed an operation that depends on the underlying bytes representing a pointer - = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported note: inside `std::ptr::read::<MaybeUninit<MaybeUninit<u8>>>` --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL note: inside `mem::swap_simple::<MaybeUninit<MaybeUninit<u8>>>` @@ -22,6 +20,8 @@ note: inside `X` 20 | | mem::size_of::<&i32>(), 21 | | ); | |_________^ + = help: this code performed an operation that depends on the underlying bytes representing a pointer + = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error: aborting due to previous error diff --git a/tests/ui/consts/offset_from_ub.stderr b/tests/ui/consts/offset_from_ub.stderr index 6530084a585..97ff6efdd79 100644 --- a/tests/ui/consts/offset_from_ub.stderr +++ b/tests/ui/consts/offset_from_ub.stderr @@ -27,31 +27,31 @@ error[E0080]: evaluation of constant value failed --> $DIR/offset_from_ub.rs:37:14 | LL | unsafe { ptr_offset_from(ptr, ptr) } - | ^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: null pointer is a dangling pointer (it has no provenance) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: null pointer is a dangling pointer (it has no provenance) error[E0080]: evaluation of constant value failed --> $DIR/offset_from_ub.rs:44:14 | LL | unsafe { ptr_offset_from(ptr2, ptr1) } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: 0x8[noalloc] is a dangling pointer (it has no provenance) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: 0x8[noalloc] is a dangling pointer (it has no provenance) error[E0080]: evaluation of constant value failed --> $DIR/offset_from_ub.rs:53:14 | LL | unsafe { ptr_offset_from(end_ptr, start_ptr) } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: alloc17 has size 4, so pointer to 10 bytes starting at offset 0 is out-of-bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: alloc17 has size 4, so pointer to 10 bytes starting at offset 0 is out-of-bounds error[E0080]: evaluation of constant value failed --> $DIR/offset_from_ub.rs:62:14 | LL | unsafe { ptr_offset_from(start_ptr, end_ptr) } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: alloc20 has size 4, so pointer to 10 bytes starting at offset 0 is out-of-bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: alloc20 has size 4, so pointer to 10 bytes starting at offset 0 is out-of-bounds error[E0080]: evaluation of constant value failed --> $DIR/offset_from_ub.rs:70:14 | LL | unsafe { ptr_offset_from(end_ptr, end_ptr) } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds offset_from: alloc23 has size 4, so pointer at offset 10 is out-of-bounds + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds `offset_from`: alloc23 has size 4, so pointer at offset 10 is out-of-bounds error[E0080]: evaluation of constant value failed --> $DIR/offset_from_ub.rs:79:14 @@ -86,7 +86,7 @@ LL | unsafe { ptr_offset_from_unsigned(ptr2, ptr1) } error[E0080]: evaluation of constant value failed --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | - = note: out-of-bounds offset_from: null pointer is a dangling pointer (it has no provenance) + = note: out-of-bounds `offset_from`: null pointer is a dangling pointer (it has no provenance) | note: inside `ptr::const_ptr::<impl *const u8>::offset_from` --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL @@ -99,7 +99,7 @@ LL | unsafe { ptr2.offset_from(ptr1) } error[E0080]: evaluation of constant value failed --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | - = note: out-of-bounds offset_from: null pointer is a dangling pointer (it has no provenance) + = note: out-of-bounds `offset_from`: null pointer is a dangling pointer (it has no provenance) | note: inside `ptr::const_ptr::<impl *const u8>::offset_from` --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL diff --git a/tests/ui/consts/raw-ptr-const.rs b/tests/ui/consts/raw-ptr-const.rs index b9c542d03d5..fc774be54df 100644 --- a/tests/ui/consts/raw-ptr-const.rs +++ b/tests/ui/consts/raw-ptr-const.rs @@ -3,6 +3,6 @@ // could also be allowed. const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _; -//~^ ERROR untyped pointers are not allowed in constant +//~^ ERROR unsupported untyped pointer in constant fn main() {} diff --git a/tests/ui/consts/raw-ptr-const.stderr b/tests/ui/consts/raw-ptr-const.stderr index f7b53433b69..82f782fab7f 100644 --- a/tests/ui/consts/raw-ptr-const.stderr +++ b/tests/ui/consts/raw-ptr-const.stderr @@ -1,8 +1,10 @@ -error: untyped pointers are not allowed in constant +error: unsupported untyped pointer in constant --> $DIR/raw-ptr-const.rs:5:1 | LL | const CONST_RAW: *const Vec<i32> = &Vec::new() as *const _; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: memory only reachable via raw pointers is not supported error: aborting due to previous error diff --git a/tests/ui/consts/refs_check_const_eq-issue-88384.rs b/tests/ui/consts/refs_check_const_eq-issue-88384.rs index 1496b28bd3e..fb0405b651c 100644 --- a/tests/ui/consts/refs_check_const_eq-issue-88384.rs +++ b/tests/ui/consts/refs_check_const_eq-issue-88384.rs @@ -8,10 +8,10 @@ struct CompileTimeSettings{ } struct Foo<const T: CompileTimeSettings>; -//~^ ERROR using function pointers as const generic parameters is forbidden +//~^ ERROR `CompileTimeSettings` must implement `ConstParamTy` to be used as the type of a const generic parameter impl<const T: CompileTimeSettings> Foo<T> { - //~^ ERROR using function pointers as const generic parameters is forbidden + //~^ ERROR `CompileTimeSettings` must implement `ConstParamTy` to be used as the type of a const generic parameter fn call_hooks(){ } } diff --git a/tests/ui/consts/refs_check_const_eq-issue-88384.stderr b/tests/ui/consts/refs_check_const_eq-issue-88384.stderr index 3855b5f2a68..c490cd053e7 100644 --- a/tests/ui/consts/refs_check_const_eq-issue-88384.stderr +++ b/tests/ui/consts/refs_check_const_eq-issue-88384.stderr @@ -7,17 +7,29 @@ LL | #![feature(adt_const_params)] = note: see issue #95174 <https://github.com/rust-lang/rust/issues/95174> for more information = note: `#[warn(incomplete_features)]` on by default -error[E0741]: using function pointers as const generic parameters is forbidden +error[E0741]: `CompileTimeSettings` must implement `ConstParamTy` to be used as the type of a const generic parameter --> $DIR/refs_check_const_eq-issue-88384.rs:10:21 | LL | struct Foo<const T: CompileTimeSettings>; | ^^^^^^^^^^^^^^^^^^^ + | +help: add `#[derive(ConstParamTy)]` to the struct + | +LL + #[derive(ConstParamTy)] +LL | struct CompileTimeSettings{ + | -error[E0741]: using function pointers as const generic parameters is forbidden +error[E0741]: `CompileTimeSettings` must implement `ConstParamTy` to be used as the type of a const generic parameter --> $DIR/refs_check_const_eq-issue-88384.rs:13:15 | LL | impl<const T: CompileTimeSettings> Foo<T> { | ^^^^^^^^^^^^^^^^^^^ + | +help: add `#[derive(ConstParamTy)]` to the struct + | +LL + #[derive(ConstParamTy)] +LL | struct CompileTimeSettings{ + | error: aborting due to 2 previous errors; 1 warning emitted diff --git a/tests/ui/inline-const/elided-lifetime-being-infer-vars.rs b/tests/ui/inline-const/elided-lifetime-being-infer-vars.rs new file mode 100644 index 00000000000..5661db4a253 --- /dev/null +++ b/tests/ui/inline-const/elided-lifetime-being-infer-vars.rs @@ -0,0 +1,11 @@ +// check-pass + +#![feature(inline_const)] + +fn main() { + let _my_usize = const { + let a = 10_usize; + let b: &'_ usize = &a; + *b + }; +} diff --git a/tests/ui/issues/issue-62375.stderr b/tests/ui/issues/issue-62375.stderr index cd632e64fe5..f6d7968c0c4 100644 --- a/tests/ui/issues/issue-62375.stderr +++ b/tests/ui/issues/issue-62375.stderr @@ -11,8 +11,11 @@ note: an implementation of `PartialEq<fn(()) -> A {A::Value}>` might be missing | LL | enum A { | ^^^^^^ must implement `PartialEq<fn(()) -> A {A::Value}>` -note: the trait `PartialEq` must be implemented - --> $SRC_DIR/core/src/cmp.rs:LL:COL +help: consider annotating `A` with `#[derive(PartialEq)]` + | +LL + #[derive(PartialEq)] +LL | enum A { + | help: use parentheses to construct this tuple variant | LL | a == A::Value(/* () */); diff --git a/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr b/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr index 5087807b6c7..159a54873cc 100644 --- a/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr +++ b/tests/ui/lint/trivial-casts-featuring-type-ascription.stderr @@ -4,7 +4,7 @@ error: trivial numeric cast: `i32` as `i32` LL | let lugubrious = 12i32 as i32; | ^^^^^^^^^^^^ | - = help: cast can be replaced by coercion; this might require type ascription or a temporary variable + = help: cast can be replaced by coercion; this might require a temporary variable note: the lint level is defined here --> $DIR/trivial-casts-featuring-type-ascription.rs:1:24 | @@ -17,7 +17,7 @@ error: trivial cast: `&u32` as `*const u32` LL | let _ = haunted as *const u32; | ^^^^^^^^^^^^^^^^^^^^^ | - = help: cast can be replaced by coercion; this might require type ascription or a temporary variable + = help: cast can be replaced by coercion; this might require a temporary variable note: the lint level is defined here --> $DIR/trivial-casts-featuring-type-ascription.rs:1:9 | diff --git a/tests/ui/mir/thir-constparam-temp.rs b/tests/ui/mir/thir-constparam-temp.rs index cdc5910b36c..7eedc325d60 100644 --- a/tests/ui/mir/thir-constparam-temp.rs +++ b/tests/ui/mir/thir-constparam-temp.rs @@ -3,7 +3,9 @@ #![feature(adt_const_params)] #![allow(incomplete_features)] -#[derive(PartialEq, Eq)] +use std::marker::ConstParamTy; + +#[derive(PartialEq, Eq, ConstParamTy)] struct Yikes; impl Yikes { diff --git a/tests/ui/mir/thir-constparam-temp.stderr b/tests/ui/mir/thir-constparam-temp.stderr index b77d67e084f..d50747e5434 100644 --- a/tests/ui/mir/thir-constparam-temp.stderr +++ b/tests/ui/mir/thir-constparam-temp.stderr @@ -1,5 +1,5 @@ warning: taking a mutable reference to a `const` item - --> $DIR/thir-constparam-temp.rs:14:5 + --> $DIR/thir-constparam-temp.rs:16:5 | LL | YIKES.mut_self() | ^^^^^^^^^^^^^^^^ @@ -7,12 +7,12 @@ LL | YIKES.mut_self() = note: each usage of a `const` item creates a new temporary = note: the mutable reference will refer to this temporary, not the original `const` item note: mutable reference created due to call to this method - --> $DIR/thir-constparam-temp.rs:10:5 + --> $DIR/thir-constparam-temp.rs:12:5 | LL | fn mut_self(&mut self) {} | ^^^^^^^^^^^^^^^^^^^^^^ note: `const` item defined here - --> $DIR/thir-constparam-temp.rs:13:8 + --> $DIR/thir-constparam-temp.rs:15:8 | LL | fn foo<const YIKES: Yikes>() { | ^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/mismatched_types/suggest-option-asderef-unfixable.rs b/tests/ui/mismatched_types/suggest-option-asderef-unfixable.rs new file mode 100644 index 00000000000..cc9ba5514fe --- /dev/null +++ b/tests/ui/mismatched_types/suggest-option-asderef-unfixable.rs @@ -0,0 +1,40 @@ +fn produces_string() -> Option<String> { + Some("my cool string".to_owned()) +} + +fn takes_str_but_too_many_refs(_: &&str) -> Option<()> { + Some(()) +} + +fn no_args() -> Option<()> { + Some(()) +} + +fn generic_ref<T>(_: &T) -> Option<()> { + Some(()) +} + +extern "C" fn takes_str_but_wrong_abi(_: &str) -> Option<()> { + Some(()) +} + +unsafe fn takes_str_but_unsafe(_: &str) -> Option<()> { + Some(()) +} + +struct TypeWithoutDeref; + +fn main() { + let _ = produces_string().and_then(takes_str_but_too_many_refs); + //~^ ERROR type mismatch in function arguments + let _ = produces_string().and_then(takes_str_but_wrong_abi); + //~^ ERROR expected a `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` + let _ = produces_string().and_then(takes_str_but_unsafe); + //~^ ERROR expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}` + let _ = produces_string().and_then(no_args); + //~^ ERROR function is expected to take 1 argument, but it takes 0 arguments + let _ = produces_string().and_then(generic_ref); + //~^ ERROR type mismatch in function arguments + let _ = Some(TypeWithoutDeref).and_then(takes_str_but_too_many_refs); + //~^ ERROR type mismatch in function arguments +} diff --git a/tests/ui/mismatched_types/suggest-option-asderef-unfixable.stderr b/tests/ui/mismatched_types/suggest-option-asderef-unfixable.stderr new file mode 100644 index 00000000000..079909eb48d --- /dev/null +++ b/tests/ui/mismatched_types/suggest-option-asderef-unfixable.stderr @@ -0,0 +1,96 @@ +error[E0631]: type mismatch in function arguments + --> $DIR/suggest-option-asderef-unfixable.rs:28:40 + | +LL | fn takes_str_but_too_many_refs(_: &&str) -> Option<()> { + | ------------------------------------------------------ found signature defined here +... +LL | let _ = produces_string().and_then(takes_str_but_too_many_refs); + | -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(String) -> _` + found function signature `for<'a, 'b> fn(&'a &'b str) -> _` +note: required by a bound in `Option::<T>::and_then` + --> $SRC_DIR/core/src/option.rs:LL:COL + +error[E0277]: expected a `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` + --> $DIR/suggest-option-asderef-unfixable.rs:30:40 + | +LL | let _ = produces_string().and_then(takes_str_but_wrong_abi); + | -------- ^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce<(String,)>` closure, found `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` + | | + | required by a bound introduced by this call + | + = help: the trait `FnOnce<(String,)>` is not implemented for fn item `for<'a> extern "C" fn(&'a str) -> Option<()> {takes_str_but_wrong_abi}` +note: required by a bound in `Option::<T>::and_then` + --> $SRC_DIR/core/src/option.rs:LL:COL + +error[E0277]: expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}` + --> $DIR/suggest-option-asderef-unfixable.rs:32:40 + | +LL | let _ = produces_string().and_then(takes_str_but_unsafe); + | -------- ^^^^^^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }` + | | + | required by a bound introduced by this call + | + = help: the trait `FnOnce<(String,)>` is not implemented for fn item `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}` + = note: unsafe function cannot be called generically without an unsafe block +note: required by a bound in `Option::<T>::and_then` + --> $SRC_DIR/core/src/option.rs:LL:COL + +error[E0593]: function is expected to take 1 argument, but it takes 0 arguments + --> $DIR/suggest-option-asderef-unfixable.rs:34:40 + | +LL | fn no_args() -> Option<()> { + | -------------------------- takes 0 arguments +... +LL | let _ = produces_string().and_then(no_args); + | -------- ^^^^^^^ expected function that takes 1 argument + | | + | required by a bound introduced by this call + | +note: required by a bound in `Option::<T>::and_then` + --> $SRC_DIR/core/src/option.rs:LL:COL + +error[E0631]: type mismatch in function arguments + --> $DIR/suggest-option-asderef-unfixable.rs:36:40 + | +LL | fn generic_ref<T>(_: &T) -> Option<()> { + | -------------------------------------- found signature defined here +... +LL | let _ = produces_string().and_then(generic_ref); + | -------- ^^^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(String) -> _` + found function signature `for<'a> fn(&'a _) -> _` +note: required by a bound in `Option::<T>::and_then` + --> $SRC_DIR/core/src/option.rs:LL:COL +help: do not borrow the argument + | +LL - fn generic_ref<T>(_: &T) -> Option<()> { +LL + fn generic_ref<T>(_: T) -> Option<()> { + | + +error[E0631]: type mismatch in function arguments + --> $DIR/suggest-option-asderef-unfixable.rs:38:45 + | +LL | fn takes_str_but_too_many_refs(_: &&str) -> Option<()> { + | ------------------------------------------------------ found signature defined here +... +LL | let _ = Some(TypeWithoutDeref).and_then(takes_str_but_too_many_refs); + | -------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(TypeWithoutDeref) -> _` + found function signature `for<'a, 'b> fn(&'a &'b str) -> _` +note: required by a bound in `Option::<T>::and_then` + --> $SRC_DIR/core/src/option.rs:LL:COL + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0277, E0593, E0631. +For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/mismatched_types/suggest-option-asderef.fixed b/tests/ui/mismatched_types/suggest-option-asderef.fixed new file mode 100644 index 00000000000..08805999341 --- /dev/null +++ b/tests/ui/mismatched_types/suggest-option-asderef.fixed @@ -0,0 +1,30 @@ +// run-rustfix + +fn produces_string() -> Option<String> { + Some("my cool string".to_owned()) +} + +fn takes_str(_: &str) -> Option<()> { + Some(()) +} + +fn takes_str_mut(_: &mut str) -> Option<()> { + Some(()) +} + +fn generic<T>(_: T) -> Option<()> { + Some(()) +} + +fn main() { + let _: Option<()> = produces_string().as_deref().and_then(takes_str); + //~^ ERROR type mismatch in function arguments + //~| HELP call `Option::as_deref()` first + let _: Option<Option<()>> = produces_string().as_deref().map(takes_str); + //~^ ERROR type mismatch in function arguments + //~| HELP call `Option::as_deref()` first + let _: Option<Option<()>> = produces_string().as_deref_mut().map(takes_str_mut); + //~^ ERROR type mismatch in function arguments + //~| HELP call `Option::as_deref_mut()` first + let _ = produces_string().and_then(generic); +} diff --git a/tests/ui/mismatched_types/suggest-option-asderef.rs b/tests/ui/mismatched_types/suggest-option-asderef.rs new file mode 100644 index 00000000000..3cfb2ffa828 --- /dev/null +++ b/tests/ui/mismatched_types/suggest-option-asderef.rs @@ -0,0 +1,30 @@ +// run-rustfix + +fn produces_string() -> Option<String> { + Some("my cool string".to_owned()) +} + +fn takes_str(_: &str) -> Option<()> { + Some(()) +} + +fn takes_str_mut(_: &mut str) -> Option<()> { + Some(()) +} + +fn generic<T>(_: T) -> Option<()> { + Some(()) +} + +fn main() { + let _: Option<()> = produces_string().and_then(takes_str); + //~^ ERROR type mismatch in function arguments + //~| HELP call `Option::as_deref()` first + let _: Option<Option<()>> = produces_string().map(takes_str); + //~^ ERROR type mismatch in function arguments + //~| HELP call `Option::as_deref()` first + let _: Option<Option<()>> = produces_string().map(takes_str_mut); + //~^ ERROR type mismatch in function arguments + //~| HELP call `Option::as_deref_mut()` first + let _ = produces_string().and_then(generic); +} diff --git a/tests/ui/mismatched_types/suggest-option-asderef.stderr b/tests/ui/mismatched_types/suggest-option-asderef.stderr new file mode 100644 index 00000000000..46da19d2bf4 --- /dev/null +++ b/tests/ui/mismatched_types/suggest-option-asderef.stderr @@ -0,0 +1,63 @@ +error[E0631]: type mismatch in function arguments + --> $DIR/suggest-option-asderef.rs:20:52 + | +LL | fn takes_str(_: &str) -> Option<()> { + | ----------------------------------- found signature defined here +... +LL | let _: Option<()> = produces_string().and_then(takes_str); + | -------- ^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(String) -> _` + found function signature `for<'a> fn(&'a str) -> _` +note: required by a bound in `Option::<T>::and_then` + --> $SRC_DIR/core/src/option.rs:LL:COL +help: call `Option::as_deref()` first + | +LL | let _: Option<()> = produces_string().as_deref().and_then(takes_str); + | +++++++++++ + +error[E0631]: type mismatch in function arguments + --> $DIR/suggest-option-asderef.rs:23:55 + | +LL | fn takes_str(_: &str) -> Option<()> { + | ----------------------------------- found signature defined here +... +LL | let _: Option<Option<()>> = produces_string().map(takes_str); + | --- ^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(String) -> _` + found function signature `for<'a> fn(&'a str) -> _` +note: required by a bound in `Option::<T>::map` + --> $SRC_DIR/core/src/option.rs:LL:COL +help: call `Option::as_deref()` first + | +LL | let _: Option<Option<()>> = produces_string().as_deref().map(takes_str); + | +++++++++++ + +error[E0631]: type mismatch in function arguments + --> $DIR/suggest-option-asderef.rs:26:55 + | +LL | fn takes_str_mut(_: &mut str) -> Option<()> { + | ------------------------------------------- found signature defined here +... +LL | let _: Option<Option<()>> = produces_string().map(takes_str_mut); + | --- ^^^^^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(String) -> _` + found function signature `for<'a> fn(&'a mut str) -> _` +note: required by a bound in `Option::<T>::map` + --> $SRC_DIR/core/src/option.rs:LL:COL +help: call `Option::as_deref_mut()` first + | +LL | let _: Option<Option<()>> = produces_string().as_deref_mut().map(takes_str_mut); + | +++++++++++++++ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0631`. diff --git a/tests/ui/numbers-arithmetic/issue-8460-const.noopt.stderr b/tests/ui/numbers-arithmetic/issue-8460-const.noopt.stderr index c4abcb78411..2fba94d0740 100644 --- a/tests/ui/numbers-arithmetic/issue-8460-const.noopt.stderr +++ b/tests/ui/numbers-arithmetic/issue-8460-const.noopt.stderr @@ -76,37 +76,37 @@ error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:35:36 | LL | assert!(thread::spawn(move|| { isize::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^^^ attempt to compute the remainder of `isize::MIN % -1_isize`, which would overflow + | ^^^^^^^^^^^^^^^ attempt to compute `isize::MIN % -1_isize`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:37:36 | LL | assert!(thread::spawn(move|| { i8::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^ attempt to compute the remainder of `i8::MIN % -1_i8`, which would overflow + | ^^^^^^^^^^^^ attempt to compute `i8::MIN % -1_i8`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:39:36 | LL | assert!(thread::spawn(move|| { i16::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i16::MIN % -1_i16`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i16::MIN % -1_i16`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:41:36 | LL | assert!(thread::spawn(move|| { i32::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i32::MIN % -1_i32`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i32::MIN % -1_i32`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:43:36 | LL | assert!(thread::spawn(move|| { i64::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i64::MIN % -1_i64`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i64::MIN % -1_i64`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:45:36 | LL | assert!(thread::spawn(move|| { i128::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^^ attempt to compute the remainder of `i128::MIN % -1_i128`, which would overflow + | ^^^^^^^^^^^^^^ attempt to compute `i128::MIN % -1_i128`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:47:36 diff --git a/tests/ui/numbers-arithmetic/issue-8460-const.opt.stderr b/tests/ui/numbers-arithmetic/issue-8460-const.opt.stderr index c4abcb78411..2fba94d0740 100644 --- a/tests/ui/numbers-arithmetic/issue-8460-const.opt.stderr +++ b/tests/ui/numbers-arithmetic/issue-8460-const.opt.stderr @@ -76,37 +76,37 @@ error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:35:36 | LL | assert!(thread::spawn(move|| { isize::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^^^ attempt to compute the remainder of `isize::MIN % -1_isize`, which would overflow + | ^^^^^^^^^^^^^^^ attempt to compute `isize::MIN % -1_isize`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:37:36 | LL | assert!(thread::spawn(move|| { i8::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^ attempt to compute the remainder of `i8::MIN % -1_i8`, which would overflow + | ^^^^^^^^^^^^ attempt to compute `i8::MIN % -1_i8`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:39:36 | LL | assert!(thread::spawn(move|| { i16::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i16::MIN % -1_i16`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i16::MIN % -1_i16`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:41:36 | LL | assert!(thread::spawn(move|| { i32::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i32::MIN % -1_i32`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i32::MIN % -1_i32`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:43:36 | LL | assert!(thread::spawn(move|| { i64::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i64::MIN % -1_i64`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i64::MIN % -1_i64`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:45:36 | LL | assert!(thread::spawn(move|| { i128::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^^ attempt to compute the remainder of `i128::MIN % -1_i128`, which would overflow + | ^^^^^^^^^^^^^^ attempt to compute `i128::MIN % -1_i128`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:47:36 diff --git a/tests/ui/numbers-arithmetic/issue-8460-const.opt_with_overflow_checks.stderr b/tests/ui/numbers-arithmetic/issue-8460-const.opt_with_overflow_checks.stderr index c4abcb78411..2fba94d0740 100644 --- a/tests/ui/numbers-arithmetic/issue-8460-const.opt_with_overflow_checks.stderr +++ b/tests/ui/numbers-arithmetic/issue-8460-const.opt_with_overflow_checks.stderr @@ -76,37 +76,37 @@ error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:35:36 | LL | assert!(thread::spawn(move|| { isize::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^^^ attempt to compute the remainder of `isize::MIN % -1_isize`, which would overflow + | ^^^^^^^^^^^^^^^ attempt to compute `isize::MIN % -1_isize`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:37:36 | LL | assert!(thread::spawn(move|| { i8::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^ attempt to compute the remainder of `i8::MIN % -1_i8`, which would overflow + | ^^^^^^^^^^^^ attempt to compute `i8::MIN % -1_i8`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:39:36 | LL | assert!(thread::spawn(move|| { i16::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i16::MIN % -1_i16`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i16::MIN % -1_i16`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:41:36 | LL | assert!(thread::spawn(move|| { i32::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i32::MIN % -1_i32`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i32::MIN % -1_i32`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:43:36 | LL | assert!(thread::spawn(move|| { i64::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^ attempt to compute the remainder of `i64::MIN % -1_i64`, which would overflow + | ^^^^^^^^^^^^^ attempt to compute `i64::MIN % -1_i64`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:45:36 | LL | assert!(thread::spawn(move|| { i128::MIN % -1; }).join().is_err()); - | ^^^^^^^^^^^^^^ attempt to compute the remainder of `i128::MIN % -1_i128`, which would overflow + | ^^^^^^^^^^^^^^ attempt to compute `i128::MIN % -1_i128`, which would overflow error: this operation will panic at runtime --> $DIR/issue-8460-const.rs:47:36 diff --git a/tests/ui/offset-of/offset-of-dst-field.rs b/tests/ui/offset-of/offset-of-dst-field.rs index 3b8dc0b84a4..e393b159e64 100644 --- a/tests/ui/offset-of/offset-of-dst-field.rs +++ b/tests/ui/offset-of/offset-of-dst-field.rs @@ -36,6 +36,8 @@ fn main() { offset_of!(Alpha, z); //~ ERROR the size for values of type offset_of!(Beta, z); //~ ERROR the size for values of type offset_of!(Gamma, z); //~ ERROR the size for values of type + offset_of!((u8, dyn Trait), 0); // ok + offset_of!((u8, dyn Trait), 1); //~ ERROR the size for values of type } fn delta() { diff --git a/tests/ui/offset-of/offset-of-dst-field.stderr b/tests/ui/offset-of/offset-of-dst-field.stderr index 128c783d5dd..4eaceaa9358 100644 --- a/tests/ui/offset-of/offset-of-dst-field.stderr +++ b/tests/ui/offset-of/offset-of-dst-field.stderr @@ -25,8 +25,17 @@ LL | offset_of!(Gamma, z); = help: the trait `Sized` is not implemented for `Extern` = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time + --> $DIR/offset-of-dst-field.rs:40:5 + | +LL | offset_of!((u8, dyn Trait), 1); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Trait` + = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the size for values of type `Extern` cannot be known at compilation time - --> $DIR/offset-of-dst-field.rs:43:5 + --> $DIR/offset-of-dst-field.rs:45:5 | LL | offset_of!(Delta<Extern>, z); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time @@ -35,7 +44,7 @@ LL | offset_of!(Delta<Extern>, z); = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time - --> $DIR/offset-of-dst-field.rs:44:5 + --> $DIR/offset-of-dst-field.rs:46:5 | LL | offset_of!(Delta<dyn Trait>, z); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time @@ -44,7 +53,7 @@ LL | offset_of!(Delta<dyn Trait>, z); = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the size for values of type `[u8]` cannot be known at compilation time - --> $DIR/offset-of-dst-field.rs:42:5 + --> $DIR/offset-of-dst-field.rs:44:5 | LL | offset_of!(Delta<Alpha>, z); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time @@ -58,7 +67,7 @@ LL | struct Alpha { = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the size for values of type `T` cannot be known at compilation time - --> $DIR/offset-of-dst-field.rs:48:5 + --> $DIR/offset-of-dst-field.rs:50:5 | LL | fn generic_with_maybe_sized<T: ?Sized>() -> usize { | - this type parameter needs to be `std::marker::Sized` @@ -72,6 +81,6 @@ LL - fn generic_with_maybe_sized<T: ?Sized>() -> usize { LL + fn generic_with_maybe_sized<T>() -> usize { | -error: aborting due to 7 previous errors +error: aborting due to 8 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/offset-of/offset-of-tuple.rs b/tests/ui/offset-of/offset-of-tuple.rs new file mode 100644 index 00000000000..4077538b77f --- /dev/null +++ b/tests/ui/offset-of/offset-of-tuple.rs @@ -0,0 +1,10 @@ +#![feature(offset_of)] +#![feature(builtin_syntax)] + +fn main() { + core::mem::offset_of!((u8, u8), _0); //~ ERROR no field `_0` + core::mem::offset_of!((u8, u8), +1); //~ ERROR no rules expected + core::mem::offset_of!((u8, u8), -1); //~ ERROR no rules expected + builtin # offset_of((u8, u8), _0); //~ ERROR no field `_0` + builtin # offset_of((u8, u8), +1); //~ ERROR expected identifier +} diff --git a/tests/ui/offset-of/offset-of-tuple.stderr b/tests/ui/offset-of/offset-of-tuple.stderr new file mode 100644 index 00000000000..cc9ce0f3455 --- /dev/null +++ b/tests/ui/offset-of/offset-of-tuple.stderr @@ -0,0 +1,37 @@ +error: expected identifier, found `+` + --> $DIR/offset-of-tuple.rs:9:35 + | +LL | builtin # offset_of((u8, u8), +1); + | ^ expected identifier + +error: no rules expected the token `1` + --> $DIR/offset-of-tuple.rs:6:38 + | +LL | core::mem::offset_of!((u8, u8), +1); + | ^ no rules expected this token in macro call + | + = note: while trying to match sequence start + +error: no rules expected the token `1` + --> $DIR/offset-of-tuple.rs:7:38 + | +LL | core::mem::offset_of!((u8, u8), -1); + | ^ no rules expected this token in macro call + | + = note: while trying to match sequence start + +error[E0609]: no field `_0` on type `(u8, u8)` + --> $DIR/offset-of-tuple.rs:5:37 + | +LL | core::mem::offset_of!((u8, u8), _0); + | ^^ + +error[E0609]: no field `_0` on type `(u8, u8)` + --> $DIR/offset-of-tuple.rs:8:35 + | +LL | builtin # offset_of((u8, u8), _0); + | ^^ + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0609`. diff --git a/tests/ui/offset-of/offset-of-unsized.rs b/tests/ui/offset-of/offset-of-unsized.rs index 666387e615e..49c8328da5c 100644 --- a/tests/ui/offset-of/offset-of-unsized.rs +++ b/tests/ui/offset-of/offset-of-unsized.rs @@ -1,5 +1,6 @@ // build-pass -// regression test for #112051 +// regression test for #112051, not in `offset-of-dst` as the issue is in codegen, +// and isn't triggered in the presence of typeck errors #![feature(offset_of)] diff --git a/tests/ui/sanitize/issue-111184-generator-witness.rs b/tests/ui/sanitize/issue-111184-generator-witness.rs new file mode 100644 index 00000000000..8f4118057ce --- /dev/null +++ b/tests/ui/sanitize/issue-111184-generator-witness.rs @@ -0,0 +1,17 @@ +// Regression test for issue 111184, where ty::GeneratorWitness were not expected to occur in +// encode_ty and caused the compiler to ICE. +// +// needs-sanitizer-cfi +// compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi --edition=2021 +// no-prefer-dynamic +// only-x86_64-unknown-linux-gnu +// run-pass + +use std::future::Future; + +async fn foo() {} +fn bar<T>(_: impl Future<Output = T>) {} + +fn main() { + bar(foo()); +} diff --git a/tests/ui/statics/uninhabited-static.stderr b/tests/ui/statics/uninhabited-static.stderr index 35fdcae6a59..9260930473f 100644 --- a/tests/ui/statics/uninhabited-static.stderr +++ b/tests/ui/statics/uninhabited-static.stderr @@ -47,7 +47,7 @@ error[E0080]: could not evaluate static initializer --> $DIR/uninhabited-static.rs:12:31 | LL | static VOID2: Void = unsafe { std::mem::transmute(()) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a value of uninhabited type Void + | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a value of uninhabited type `Void` warning: the type `Void` does not permit zero-initialization --> $DIR/uninhabited-static.rs:12:31 @@ -66,7 +66,7 @@ error[E0080]: could not evaluate static initializer --> $DIR/uninhabited-static.rs:16:32 | LL | static NEVER2: Void = unsafe { std::mem::transmute(()) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a value of uninhabited type Void + | ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a value of uninhabited type `Void` warning: the type `Void` does not permit zero-initialization --> $DIR/uninhabited-static.rs:16:32 diff --git a/tests/ui/symbol-names/const-generics-structural-demangling.rs b/tests/ui/symbol-names/const-generics-structural-demangling.rs index df09ba494a7..947fddf3f31 100644 --- a/tests/ui/symbol-names/const-generics-structural-demangling.rs +++ b/tests/ui/symbol-names/const-generics-structural-demangling.rs @@ -1,14 +1,13 @@ // build-fail // compile-flags: -C symbol-mangling-version=v0 --crate-name=c -// NOTE(eddyb) we need `core` for `core::option::Option`, normalize away its -// disambiguator hash, which can/should change (including between stage{1,2}). -// normalize-stderr-test: "core\[[0-9a-f]+\]" -> "core[HASH]" // normalize-stderr-test: "c\[[0-9a-f]+\]" -> "c[HASH]" #![feature(adt_const_params, decl_macro, rustc_attrs)] #![allow(incomplete_features)] +use std::marker::ConstParamTy; + pub struct RefByte<const RB: &'static u8>; #[rustc_symbol_name] @@ -43,25 +42,31 @@ pub struct TupleByteBool<const TBB: (u8, bool)>; //~| ERROR demangling-alt(<c::TupleByteBool<{(1, false)}>>) impl TupleByteBool<{(1, false)}> {} -pub struct OptionUsize<const OU: Option<usize>>; +#[derive(PartialEq, Eq, ConstParamTy)] +pub enum MyOption<T> { + Some(T), + None, +} + +pub struct OptionUsize<const OU: MyOption<usize>>; // HACK(eddyb) the full mangling is only in `.stderr` because we can normalize // the `core` disambiguator hash away there, but not here. #[rustc_symbol_name] //~^ ERROR symbol-name //~| ERROR demangling -//~| ERROR demangling-alt(<c::OptionUsize<{core::option::Option::<usize>::None}>>) -impl OptionUsize<{None}> {} +//~| ERROR demangling-alt(<c::OptionUsize<{c::MyOption::<usize>::None}>>) +impl OptionUsize<{MyOption::None}> {} // HACK(eddyb) the full mangling is only in `.stderr` because we can normalize // the `core` disambiguator hash away there, but not here. #[rustc_symbol_name] //~^ ERROR symbol-name //~| ERROR demangling -//~| ERROR demangling-alt(<c::OptionUsize<{core::option::Option::<usize>::Some(0)}>>) -impl OptionUsize<{Some(0)}> {} +//~| ERROR demangling-alt(<c::OptionUsize<{c::MyOption::<usize>::Some(0)}>>) +impl OptionUsize<{MyOption::Some(0)}> {} -#[derive(PartialEq, Eq)] +#[derive(PartialEq, Eq, ConstParamTy)] pub struct Foo { s: &'static str, ch: char, @@ -78,7 +83,7 @@ impl Foo_<{Foo { s: "abc", ch: 'x', slice: &[1, 2, 3] }}> {} // NOTE(eddyb) this tests specifically the use of disambiguators in field names, // using macros 2.0 hygiene to create a `struct` with conflicting field names. macro duplicate_field_name_test($x:ident) { - #[derive(PartialEq, Eq)] + #[derive(PartialEq, Eq, ConstParamTy)] pub struct Bar { $x: u8, x: u16, diff --git a/tests/ui/symbol-names/const-generics-structural-demangling.stderr b/tests/ui/symbol-names/const-generics-structural-demangling.stderr index a4c997477ee..96dea154d05 100644 --- a/tests/ui/symbol-names/const-generics-structural-demangling.stderr +++ b/tests/ui/symbol-names/const-generics-structural-demangling.stderr @@ -1,131 +1,131 @@ error: symbol-name(_RMCsCRATE_HASH_1cINtB<REF>_7RefByteKRh7b_E) - --> $DIR/const-generics-structural-demangling.rs:14:1 + --> $DIR/const-generics-structural-demangling.rs:13:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling(<c[HASH]::RefByte<{&123u8}>>) - --> $DIR/const-generics-structural-demangling.rs:14:1 + --> $DIR/const-generics-structural-demangling.rs:13:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling-alt(<c::RefByte<{&123}>>) - --> $DIR/const-generics-structural-demangling.rs:14:1 + --> $DIR/const-generics-structural-demangling.rs:13:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: symbol-name(_RMs_CsCRATE_HASH_1cINtB<REF>_6RefZstKRAEE) - --> $DIR/const-generics-structural-demangling.rs:24:1 + --> $DIR/const-generics-structural-demangling.rs:23:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling(<c[HASH]::RefZst<{&[]}>>) - --> $DIR/const-generics-structural-demangling.rs:24:1 + --> $DIR/const-generics-structural-demangling.rs:23:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling-alt(<c::RefZst<{&[]}>>) - --> $DIR/const-generics-structural-demangling.rs:24:1 + --> $DIR/const-generics-structural-demangling.rs:23:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB<REF>_11Array3BytesKAh1_h2_h3_EE) - --> $DIR/const-generics-structural-demangling.rs:32:1 + --> $DIR/const-generics-structural-demangling.rs:31:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling(<c[HASH]::Array3Bytes<{[1u8, 2u8, 3u8]}>>) - --> $DIR/const-generics-structural-demangling.rs:32:1 + --> $DIR/const-generics-structural-demangling.rs:31:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling-alt(<c::Array3Bytes<{[1, 2, 3]}>>) - --> $DIR/const-generics-structural-demangling.rs:32:1 + --> $DIR/const-generics-structural-demangling.rs:31:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB<REF>_13TupleByteBoolKTh1_b0_EE) - --> $DIR/const-generics-structural-demangling.rs:40:1 + --> $DIR/const-generics-structural-demangling.rs:39:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling(<c[HASH]::TupleByteBool<{(1u8, false)}>>) - --> $DIR/const-generics-structural-demangling.rs:40:1 + --> $DIR/const-generics-structural-demangling.rs:39:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling-alt(<c::TupleByteBool<{(1, false)}>>) - --> $DIR/const-generics-structural-demangling.rs:40:1 + --> $DIR/const-generics-structural-demangling.rs:39:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeKVNtINtNtCsCRATE_HASH_4core6option6OptionjE4NoneUE) - --> $DIR/const-generics-structural-demangling.rs:50:1 +error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeKVNtINtB<REF>_8MyOptionjE4NoneUE) + --> $DIR/const-generics-structural-demangling.rs:55:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: demangling(<c[HASH]::OptionUsize<{core[HASH]::option::Option::<usize>::None}>>) - --> $DIR/const-generics-structural-demangling.rs:50:1 +error: demangling(<c[HASH]::OptionUsize<{c[HASH]::MyOption::<usize>::None}>>) + --> $DIR/const-generics-structural-demangling.rs:55:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: demangling-alt(<c::OptionUsize<{core::option::Option::<usize>::None}>>) - --> $DIR/const-generics-structural-demangling.rs:50:1 +error: demangling-alt(<c::OptionUsize<{c::MyOption::<usize>::None}>>) + --> $DIR/const-generics-structural-demangling.rs:55:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeKVNtINtNtCsCRATE_HASH_4core6option6OptionjE4SomeTj0_EE) - --> $DIR/const-generics-structural-demangling.rs:58:1 +error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeKVNtINtB<REF>_8MyOptionjE4SomeTj0_EE) + --> $DIR/const-generics-structural-demangling.rs:63:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: demangling(<c[HASH]::OptionUsize<{core[HASH]::option::Option::<usize>::Some(0usize)}>>) - --> $DIR/const-generics-structural-demangling.rs:58:1 +error: demangling(<c[HASH]::OptionUsize<{c[HASH]::MyOption::<usize>::Some(0usize)}>>) + --> $DIR/const-generics-structural-demangling.rs:63:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: demangling-alt(<c::OptionUsize<{core::option::Option::<usize>::Some(0)}>>) - --> $DIR/const-generics-structural-demangling.rs:58:1 +error: demangling-alt(<c::OptionUsize<{c::MyOption::<usize>::Some(0)}>>) + --> $DIR/const-generics-structural-demangling.rs:63:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: symbol-name(_RMs4_CsCRATE_HASH_1cINtB<REF>_4Foo_KVNtB<REF>_3FooS1sRe616263_2chc78_5sliceRAh1_h2_h3_EEE) - --> $DIR/const-generics-structural-demangling.rs:72:1 + --> $DIR/const-generics-structural-demangling.rs:77:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling(<c[HASH]::Foo_<{c[HASH]::Foo { s: "abc", ch: 'x', slice: &[1u8, 2u8, 3u8] }}>>) - --> $DIR/const-generics-structural-demangling.rs:72:1 + --> $DIR/const-generics-structural-demangling.rs:77:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ error: demangling-alt(<c::Foo_<{c::Foo { s: "abc", ch: 'x', slice: &[1, 2, 3] }}>>) - --> $DIR/const-generics-structural-demangling.rs:72:1 + --> $DIR/const-generics-structural-demangling.rs:77:1 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs9_CsCRATE_HASH_1cINtB<REF>_4Bar_KVNtB<REF>_3BarS1xh7b_s_1xt1000_EE) - --> $DIR/const-generics-structural-demangling.rs:88:5 +error: symbol-name(_RMsf_CsCRATE_HASH_1cINtB<REF>_4Bar_KVNtB<REF>_3BarS1xh7b_s_1xt1000_EE) + --> $DIR/const-generics-structural-demangling.rs:93:5 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ @@ -136,7 +136,7 @@ LL | duplicate_field_name_test!(x); = note: this error originates in the macro `duplicate_field_name_test` (in Nightly builds, run with -Z macro-backtrace for more info) error: demangling(<c[HASH]::Bar_<{c[HASH]::Bar { x: 123u8, x: 4096u16 }}>>) - --> $DIR/const-generics-structural-demangling.rs:88:5 + --> $DIR/const-generics-structural-demangling.rs:93:5 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ @@ -147,7 +147,7 @@ LL | duplicate_field_name_test!(x); = note: this error originates in the macro `duplicate_field_name_test` (in Nightly builds, run with -Z macro-backtrace for more info) error: demangling-alt(<c::Bar_<{c::Bar { x: 123, x: 4096 }}>>) - --> $DIR/const-generics-structural-demangling.rs:88:5 + --> $DIR/const-generics-structural-demangling.rs:93:5 | LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/traits/new-solver/array-default.rs b/tests/ui/traits/new-solver/array-default.rs new file mode 100644 index 00000000000..5077137b09b --- /dev/null +++ b/tests/ui/traits/new-solver/array-default.rs @@ -0,0 +1,8 @@ +// compile-flags: -Ztrait-solver=next +// check-pass + +fn has_default<const N: usize>() where [(); N]: Default {} + +fn main() { + has_default::<1>(); +} diff --git a/tests/ui/traits/new-solver/structural-resolve-field.rs b/tests/ui/traits/new-solver/structural-resolve-field.rs index c492d927696..01899c9ad64 100644 --- a/tests/ui/traits/new-solver/structural-resolve-field.rs +++ b/tests/ui/traits/new-solver/structural-resolve-field.rs @@ -1,35 +1,13 @@ // compile-flags: -Ztrait-solver=next // check-pass +#[derive(Default)] struct Foo { x: i32, } -impl MyDefault for Foo { - fn my_default() -> Self { - Self { - x: 0, - } - } -} - -trait MyDefault { - fn my_default() -> Self; -} - -impl MyDefault for [Foo; 0] { - fn my_default() -> Self { - [] - } -} -impl MyDefault for [Foo; 1] { - fn my_default() -> Self { - [Foo::my_default(); 1] - } -} - fn main() { - let mut xs = <[Foo; 1]>::my_default(); + let mut xs = <[Foo; 1]>::default(); xs[0].x = 1; (&mut xs[0]).x = 2; } diff --git a/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.fails.stderr b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.fails.stderr new file mode 100644 index 00000000000..072ac32a5de --- /dev/null +++ b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.fails.stderr @@ -0,0 +1,18 @@ +error[E0277]: the trait bound `(): Trait<1>` is not satisfied + --> $DIR/unevaluated-const-impl-trait-ref.rs:20:13 + | +LL | needs::<1>(); + | ^ the trait `Trait<1>` is not implemented for `()` + | + = help: the following other types implement trait `Trait<N>`: + <() as Trait<0>> + <() as Trait<2>> +note: required by a bound in `needs` + --> $DIR/unevaluated-const-impl-trait-ref.rs:10:38 + | +LL | fn needs<const N: usize>() where (): Trait<N> {} + | ^^^^^^^^ required by this bound in `needs` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.rs b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.rs new file mode 100644 index 00000000000..26c595bc974 --- /dev/null +++ b/tests/ui/traits/new-solver/unevaluated-const-impl-trait-ref.rs @@ -0,0 +1,22 @@ +// compile-flags: -Ztrait-solver=next +// revisions: works fails +//[works] check-pass + +trait Trait<const N: usize> {} + +impl Trait<{ 1 - 1 }> for () {} +impl Trait<{ 1 + 1 }> for () {} + +fn needs<const N: usize>() where (): Trait<N> {} + +#[cfg(works)] +fn main() { + needs::<0>(); + needs::<2>(); +} + +#[cfg(fails)] +fn main() { + needs::<1>(); + //[fails]~^ ERROR the trait bound `(): Trait<1>` is not satisfied +} diff --git a/tests/ui/typeck/derive-sugg-arg-arity.rs b/tests/ui/typeck/derive-sugg-arg-arity.rs new file mode 100644 index 00000000000..094c93a8535 --- /dev/null +++ b/tests/ui/typeck/derive-sugg-arg-arity.rs @@ -0,0 +1,8 @@ +pub struct A; + +fn main() { + match () { + _ => match A::partial_cmp() {}, + //~^ ERROR the function or associated item `partial_cmp` exists for struct `A`, but its trait bounds were not satisfied + } +} diff --git a/tests/ui/typeck/derive-sugg-arg-arity.stderr b/tests/ui/typeck/derive-sugg-arg-arity.stderr new file mode 100644 index 00000000000..5b4c4817198 --- /dev/null +++ b/tests/ui/typeck/derive-sugg-arg-arity.stderr @@ -0,0 +1,31 @@ +error[E0599]: the function or associated item `partial_cmp` exists for struct `A`, but its trait bounds were not satisfied + --> $DIR/derive-sugg-arg-arity.rs:5:23 + | +LL | pub struct A; + | ------------ + | | + | function or associated item `partial_cmp` not found for this struct + | doesn't satisfy `A: Iterator` + | doesn't satisfy `A: PartialOrd<_>` +... +LL | _ => match A::partial_cmp() {}, + | ^^^^^^^^^^^ function or associated item cannot be called on `A` due to unsatisfied trait bounds + | + = note: the following trait bounds were not satisfied: + `A: PartialOrd<_>` + which is required by `&A: PartialOrd<&_>` + `A: PartialOrd<_>` + which is required by `&mut A: PartialOrd<&mut _>` + `A: Iterator` + which is required by `&mut A: Iterator` +note: the trait `Iterator` must be implemented + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL +help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]` + | +LL + #[derive(PartialEq, PartialOrd)] +LL | pub struct A; + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. diff --git a/tests/ui/typeck/return-dyn-type-mismatch-2.rs b/tests/ui/typeck/return-dyn-type-mismatch-2.rs new file mode 100644 index 00000000000..328f154dcbc --- /dev/null +++ b/tests/ui/typeck/return-dyn-type-mismatch-2.rs @@ -0,0 +1,11 @@ +trait Trait<T> {} + +fn foo<T>() -> dyn Trait<T> +where + dyn Trait<T>: Sized, // pesky sized predicate +{ + 42 + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/typeck/return-dyn-type-mismatch-2.stderr b/tests/ui/typeck/return-dyn-type-mismatch-2.stderr new file mode 100644 index 00000000000..9c368e83834 --- /dev/null +++ b/tests/ui/typeck/return-dyn-type-mismatch-2.stderr @@ -0,0 +1,15 @@ +error[E0308]: mismatched types + --> $DIR/return-dyn-type-mismatch-2.rs:7:5 + | +LL | fn foo<T>() -> dyn Trait<T> + | ------------ expected `(dyn Trait<T> + 'static)` because of return type +... +LL | 42 + | ^^ expected `dyn Trait`, found integer + | + = note: expected trait object `(dyn Trait<T> + 'static)` + found type `{integer}` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/typeck/return-dyn-type-mismatch.rs b/tests/ui/typeck/return-dyn-type-mismatch.rs new file mode 100644 index 00000000000..93718f70f41 --- /dev/null +++ b/tests/ui/typeck/return-dyn-type-mismatch.rs @@ -0,0 +1,21 @@ +pub trait TestTrait { + type MyType; + + fn func() -> Option<Self> + where + Self: Sized; +} + +impl<T> dyn TestTrait<MyType = T> +where + Self: Sized, // pesky sized predicate +{ + fn other_func() -> dyn TestTrait<MyType = T> { + match Self::func() { + None => None, + //~^ ERROR mismatched types + } + } +} + +fn main() {} diff --git a/tests/ui/typeck/return-dyn-type-mismatch.stderr b/tests/ui/typeck/return-dyn-type-mismatch.stderr new file mode 100644 index 00000000000..9d0a609d87f --- /dev/null +++ b/tests/ui/typeck/return-dyn-type-mismatch.stderr @@ -0,0 +1,15 @@ +error[E0308]: mismatched types + --> $DIR/return-dyn-type-mismatch.rs:15:21 + | +LL | fn other_func() -> dyn TestTrait<MyType = T> { + | ------------------------- expected `(dyn TestTrait<MyType = T> + 'static)` because of return type +LL | match Self::func() { +LL | None => None, + | ^^^^ expected `dyn TestTrait`, found `Option<_>` + | + = note: expected trait object `(dyn TestTrait<MyType = T> + 'static)` + found enum `Option<_>` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/triagebot.toml b/triagebot.toml index c160c83cc95..1bfd48f07cc 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -372,6 +372,14 @@ cc = ["@GuillaumeGomez"] message = "Some changes might have occurred in exhaustiveness checking" cc = ["@Nadrieril"] +[mentions."library/portable-simd"] +message = """ +Portable SIMD is developed in its own repository. If possible, consider \ +making this change to [rust-lang/portable-simd](https://github.com/rust-lang/portable-simd) \ +instead. +""" +cc = ["@calebzulawski"] + [mentions."src/librustdoc/clean/types.rs"] cc = ["@camelid"] @@ -387,6 +395,10 @@ cc = [ message = "Some changes occurred in HTML/CSS themes." cc = ["@GuillaumeGomez"] +[mentions."tests/rustdoc-gui/"] +message = "Some changes occurred in GUI tests." +cc = ["@GuillaumeGomez"] + [mentions."src/librustdoc/html/static/css/themes/ayu.css"] message = "A change occurred in the Ayu theme." cc = ["@Cldfire"] |
