From d9531a0d93f63a8e67e56419e0aadf25a0edd43a Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Wed, 7 Jun 2023 21:00:56 +0100 Subject: [PATCH 01/10] Remove wrongly emitted `.eh_frame` in `-Cpanic=abort` --- compiler/rustc_mir_transform/src/check_alignment.rs | 7 ++++++- tests/run-make/panic-abort-eh_frame/Makefile | 10 ++++++++++ tests/run-make/panic-abort-eh_frame/foo.rs | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/run-make/panic-abort-eh_frame/Makefile create mode 100644 tests/run-make/panic-abort-eh_frame/foo.rs diff --git a/compiler/rustc_mir_transform/src/check_alignment.rs b/compiler/rustc_mir_transform/src/check_alignment.rs index 1fe8ea0789286..ef64f70fdf349 100644 --- a/compiler/rustc_mir_transform/src/check_alignment.rs +++ b/compiler/rustc_mir_transform/src/check_alignment.rs @@ -9,6 +9,7 @@ use rustc_middle::mir::{ }; use rustc_middle::ty::{Ty, TyCtxt, TypeAndMut}; use rustc_session::Session; +use rustc_target::spec::PanicStrategy; pub struct CheckAlignment; @@ -236,7 +237,11 @@ fn insert_alignment_check<'tcx>( required: Operand::Copy(alignment), found: Operand::Copy(addr), }), - unwind: UnwindAction::Terminate, + unwind: if tcx.sess.panic_strategy() == PanicStrategy::Unwind { + UnwindAction::Terminate + } else { + UnwindAction::Unreachable + }, }, }); } diff --git a/tests/run-make/panic-abort-eh_frame/Makefile b/tests/run-make/panic-abort-eh_frame/Makefile new file mode 100644 index 0000000000000..1cb7bf575cbdf --- /dev/null +++ b/tests/run-make/panic-abort-eh_frame/Makefile @@ -0,0 +1,10 @@ +# only-linux +# +# This test ensures that `panic=abort` code (without `C-unwind`, that is) should not have any +# unwinding related `.eh_frame` sections emitted. + +include ../tools.mk + +all: + $(RUSTC) foo.rs --crate-type=lib --emit=obj=$(TMPDIR)/foo.o -Cpanic=abort + objdump --dwarf=frames $(TMPDIR)/foo.o | $(CGREP) -v 'DW_CFA' diff --git a/tests/run-make/panic-abort-eh_frame/foo.rs b/tests/run-make/panic-abort-eh_frame/foo.rs new file mode 100644 index 0000000000000..e185352945538 --- /dev/null +++ b/tests/run-make/panic-abort-eh_frame/foo.rs @@ -0,0 +1,10 @@ +#![no_std] + +#[panic_handler] +fn handler(_: &core::panic::PanicInfo<'_>) -> ! { + loop {} +} + +pub unsafe fn oops(x: *const u32) -> u32 { + *x +} From 1e36f7e9ae2a99139cf383193141e1429a20faf1 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 11 Jun 2023 06:08:44 +0000 Subject: [PATCH 02/10] `suspicious_double_ref_op`: don't lint on `.borrow()` --- compiler/rustc_lint/src/noop_method_call.rs | 33 +++++++++++---------- tests/ui/lint/issue-112489.rs | 17 +++++++++++ 2 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 tests/ui/lint/issue-112489.rs diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs index d054966459d85..04bb34983af7e 100644 --- a/compiler/rustc_lint/src/noop_method_call.rs +++ b/compiler/rustc_lint/src/noop_method_call.rs @@ -76,22 +76,22 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { // We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow` // traits and ignore any other method call. - let did = match cx.typeck_results().type_dependent_def(expr.hir_id) { - // Verify we are dealing with a method/associated function. - Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) { - // Check that we're dealing with a trait method for one of the traits we care about. - Some(trait_id) - if matches!( - cx.tcx.get_diagnostic_name(trait_id), - Some(sym::Borrow | sym::Clone | sym::Deref) - ) => - { - did - } - _ => return, - }, - _ => return, + + let Some((DefKind::AssocFn, did)) = + cx.typeck_results().type_dependent_def(expr.hir_id) + else { + return; + }; + + let Some(trait_id) = cx.tcx.trait_of_item(did) else { return }; + + if !matches!( + cx.tcx.get_diagnostic_name(trait_id), + Some(sym::Borrow | sym::Clone | sym::Deref) + ) { + return; }; + let substs = cx .tcx .normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id)); @@ -129,6 +129,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span }, ); } else { + if op == "borrow" { + return; + } cx.emit_spanned_lint( SUSPICIOUS_DOUBLE_REF_OP, span, diff --git a/tests/ui/lint/issue-112489.rs b/tests/ui/lint/issue-112489.rs new file mode 100644 index 0000000000000..559edf0e4f23c --- /dev/null +++ b/tests/ui/lint/issue-112489.rs @@ -0,0 +1,17 @@ +// check-pass +use std::borrow::Borrow; + +struct S; + +trait T: Sized { + fn foo(self) {} +} + +impl T for S {} +impl T for &S {} + +fn main() { + let s = S; + s.borrow().foo(); + s.foo(); +} From 1caed5167369cf026f4159b4011cbd55f5c39ccd Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 11 Jun 2023 08:18:56 +0000 Subject: [PATCH 03/10] do not use stringly typed diagnostics --- compiler/rustc_lint/messages.ftl | 12 ++++---- compiler/rustc_lint/src/lints.rs | 12 +++++--- compiler/rustc_lint/src/noop_method_call.rs | 33 +++++++++++---------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 5e13ee7b8a41d..91c04c67f80b4 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -478,13 +478,11 @@ lint_requested_level = requested on the command line with `{$level} {$lint_name} lint_supertrait_as_deref_target = `{$t}` implements `Deref` with supertrait `{$target_principal}` as target .label = target type is set here -lint_suspicious_double_ref_op = - using `.{$call}()` on a double reference, which returns `{$ty}` instead of {$op -> - *[should_not_happen] [{$op}] - [deref] dereferencing - [borrow] borrowing - [clone] cloning - } the inner type +lint_suspicious_double_ref_clone = + using `.clone()` on a double reference, which returns `{$ty}` instead of cloning the inner type + +lint_suspicious_double_ref_deref = + using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type lint_trivial_untranslatable_diag = diagnostic with static strings only diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index e990c771bdf50..ad75dd3754a33 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1231,11 +1231,15 @@ pub struct NoopMethodCallDiag<'a> { } #[derive(LintDiagnostic)] -#[diag(lint_suspicious_double_ref_op)] -pub struct SuspiciousDoubleRefDiag<'a> { - pub call: Symbol, +#[diag(lint_suspicious_double_ref_deref)] +pub struct SuspiciousDoubleRefDerefDiag<'a> { + pub ty: Ty<'a>, +} + +#[derive(LintDiagnostic)] +#[diag(lint_suspicious_double_ref_clone)] +pub struct SuspiciousDoubleRefCloneDiag<'a> { pub ty: Ty<'a>, - pub op: &'static str, } // pass_by_value.rs diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs index 04bb34983af7e..d56c35bb677a5 100644 --- a/compiler/rustc_lint/src/noop_method_call.rs +++ b/compiler/rustc_lint/src/noop_method_call.rs @@ -1,5 +1,7 @@ use crate::context::LintContext; -use crate::lints::{NoopMethodCallDiag, SuspiciousDoubleRefDiag}; +use crate::lints::{ + NoopMethodCallDiag, SuspiciousDoubleRefCloneDiag, SuspiciousDoubleRefDerefDiag, +}; use crate::LateContext; use crate::LateLintPass; use rustc_hir::def::DefKind; @@ -102,13 +104,6 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { // (Re)check that it implements the noop diagnostic. let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return }; - let op = match name { - sym::noop_method_borrow => "borrow", - sym::noop_method_clone => "clone", - sym::noop_method_deref => "deref", - _ => return, - }; - let receiver_ty = cx.typeck_results().expr_ty(receiver); let expr_ty = cx.typeck_results().expr_ty_adjusted(expr); let arg_adjustments = cx.typeck_results().expr_adjustments(receiver); @@ -129,14 +124,22 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span }, ); } else { - if op == "borrow" { - return; + match name { + // If `type_of(x) == T` and `x.borrow()` is used to get `&T`, + // then that should be allowed + sym::noop_method_borrow => return, + sym::noop_method_clone => cx.emit_spanned_lint( + SUSPICIOUS_DOUBLE_REF_OP, + span, + SuspiciousDoubleRefCloneDiag { ty: expr_ty }, + ), + sym::noop_method_deref => cx.emit_spanned_lint( + SUSPICIOUS_DOUBLE_REF_OP, + span, + SuspiciousDoubleRefDerefDiag { ty: expr_ty }, + ), + _ => return, } - cx.emit_spanned_lint( - SUSPICIOUS_DOUBLE_REF_OP, - span, - SuspiciousDoubleRefDiag { call: call.ident.name, ty: expr_ty, op }, - ) } } } From 72b3b58efc6ed4bab93ba98586b62750abbdda79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Sun, 11 Jun 2023 23:44:28 +0800 Subject: [PATCH 04/10] Extend `unused_must_use` to cover block exprs --- compiler/rustc_arena/src/lib.rs | 2 +- compiler/rustc_lint/src/lints.rs | 53 ++++++++--- compiler/rustc_lint/src/unused.rs | 94 ++++++++++++++++--- library/core/src/primitive_docs.rs | 2 +- library/core/tests/ptr.rs | 2 +- library/std/src/primitive_docs.rs | 2 +- .../tests/ui/transmute_ptr_to_ref.fixed | 3 +- .../clippy/tests/ui/transmute_ptr_to_ref.rs | 3 +- .../tests/ui/transmute_ptr_to_ref.stderr | 48 +++++----- .../storage_dead_dangling.rs | 2 +- .../storage_dead_dangling.stderr | 4 +- .../intrinsics/uninit_uninhabited_type.rs | 2 +- .../intrinsics/uninit_uninhabited_type.stderr | 4 +- .../miri/tests/fail/intrinsics/zero_fn_ptr.rs | 2 +- .../tests/fail/intrinsics/zero_fn_ptr.stderr | 4 +- tests/ui/issues/issue-1460.stderr | 4 +- .../ui/lint/unused/must-use-block-expr.fixed | 36 +++++++ tests/ui/lint/unused/must-use-block-expr.rs | 36 +++++++ .../ui/lint/unused/must-use-block-expr.stderr | 51 ++++++++++ 19 files changed, 289 insertions(+), 65 deletions(-) create mode 100644 tests/ui/lint/unused/must-use-block-expr.fixed create mode 100644 tests/ui/lint/unused/must-use-block-expr.rs create mode 100644 tests/ui/lint/unused/must-use-block-expr.stderr diff --git a/compiler/rustc_arena/src/lib.rs b/compiler/rustc_arena/src/lib.rs index 6e15f06a76de0..ba47ebd68cbf5 100644 --- a/compiler/rustc_arena/src/lib.rs +++ b/compiler/rustc_arena/src/lib.rs @@ -67,7 +67,7 @@ struct ArenaChunk { unsafe impl<#[may_dangle] T> Drop for ArenaChunk { fn drop(&mut self) { - unsafe { Box::from_raw(self.storage.as_mut()) }; + unsafe { drop(Box::from_raw(self.storage.as_mut())) } } } diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index e990c771bdf50..9bcf672672a97 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1534,8 +1534,29 @@ pub struct UnusedOp<'a> { pub op: &'a str, #[label] pub label: Span, - #[suggestion(style = "verbose", code = "let _ = ", applicability = "maybe-incorrect")] - pub suggestion: Span, + #[subdiagnostic] + pub suggestion: UnusedOpSuggestion, +} + +#[derive(Subdiagnostic)] +pub enum UnusedOpSuggestion { + #[suggestion( + lint_suggestion, + style = "verbose", + code = "let _ = ", + applicability = "maybe-incorrect" + )] + NormalExpr { + #[primary_span] + span: Span, + }, + #[multipart_suggestion(lint_suggestion, style = "verbose", applicability = "maybe-incorrect")] + BlockTailExpr { + #[suggestion_part(code = "let _ = ")] + before_span: Span, + #[suggestion_part(code = ";")] + after_span: Span, + }, } #[derive(LintDiagnostic)] @@ -1578,15 +1599,25 @@ pub struct UnusedDef<'a, 'b> { } #[derive(Subdiagnostic)] -#[suggestion( - lint_suggestion, - style = "verbose", - code = "let _ = ", - applicability = "maybe-incorrect" -)] -pub struct UnusedDefSuggestion { - #[primary_span] - pub span: Span, + +pub enum UnusedDefSuggestion { + #[suggestion( + lint_suggestion, + style = "verbose", + code = "let _ = ", + applicability = "maybe-incorrect" + )] + NormalExpr { + #[primary_span] + span: Span, + }, + #[multipart_suggestion(lint_suggestion, style = "verbose", applicability = "maybe-incorrect")] + BlockTailExpr { + #[suggestion_part(code = "let _ = ")] + before_span: Span, + #[suggestion_part(code = ";")] + after_span: Span, + }, } // Needed because of def_path_str diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 04df23c736b0d..9861610612fb0 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -1,7 +1,8 @@ use crate::lints::{ PathStatementDrop, PathStatementDropSub, PathStatementNoEffect, UnusedAllocationDiag, UnusedAllocationMutDiag, UnusedClosure, UnusedDef, UnusedDefSuggestion, UnusedDelim, - UnusedDelimSuggestion, UnusedGenerator, UnusedImportBracesDiag, UnusedOp, UnusedResult, + UnusedDelimSuggestion, UnusedGenerator, UnusedImportBracesDiag, UnusedOp, UnusedOpSuggestion, + UnusedResult, }; use crate::Lint; use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; @@ -93,7 +94,15 @@ declare_lint_pass!(UnusedResults => [UNUSED_MUST_USE, UNUSED_RESULTS]); impl<'tcx> LateLintPass<'tcx> for UnusedResults { fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) { - let hir::StmtKind::Semi(expr) = s.kind else { return; }; + let hir::StmtKind::Semi(mut expr) = s.kind else { return; }; + + let mut expr_is_from_block = false; + while let hir::ExprKind::Block(blk, ..) = expr.kind + && let hir::Block { expr: Some(e), .. } = blk + { + expr = e; + expr_is_from_block = true; + } if let hir::ExprKind::Ret(..) = expr.kind { return; @@ -113,6 +122,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { expr.span, "output of future returned by ", "", + expr_is_from_block, ) { // We have a bare `foo().await;` on an opaque type from an async function that was @@ -125,13 +135,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { let must_use_result = is_ty_must_use(cx, ty, &expr, expr.span); let type_lint_emitted_or_suppressed = match must_use_result { Some(path) => { - emit_must_use_untranslated(cx, &path, "", "", 1, false); + emit_must_use_untranslated(cx, &path, "", "", 1, false, expr_is_from_block); true } None => false, }; - let fn_warned = check_fn_must_use(cx, expr); + let fn_warned = check_fn_must_use(cx, expr, expr_is_from_block); if !fn_warned && type_lint_emitted_or_suppressed { // We don't warn about unused unit or uninhabited types. @@ -176,7 +186,14 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { UnusedOp { op: must_use_op, label: expr.span, - suggestion: expr.span.shrink_to_lo(), + suggestion: if expr_is_from_block { + UnusedOpSuggestion::BlockTailExpr { + before_span: expr.span.shrink_to_lo(), + after_span: expr.span.shrink_to_hi(), + } + } else { + UnusedOpSuggestion::NormalExpr { span: expr.span.shrink_to_lo() } + }, }, ); op_warned = true; @@ -186,7 +203,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { cx.emit_spanned_lint(UNUSED_RESULTS, s.span, UnusedResult { ty }); } - fn check_fn_must_use(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { + fn check_fn_must_use( + cx: &LateContext<'_>, + expr: &hir::Expr<'_>, + expr_is_from_block: bool, + ) -> bool { let maybe_def_id = match expr.kind { hir::ExprKind::Call(ref callee, _) => { match callee.kind { @@ -207,7 +228,14 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { _ => None, }; if let Some(def_id) = maybe_def_id { - check_must_use_def(cx, def_id, expr.span, "return value of ", "") + check_must_use_def( + cx, + def_id, + expr.span, + "return value of ", + "", + expr_is_from_block, + ) } else { false } @@ -350,6 +378,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { span: Span, descr_pre_path: &str, descr_post_path: &str, + expr_is_from_block: bool, ) -> bool { is_def_must_use(cx, def_id, span) .map(|must_use_path| { @@ -360,6 +389,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { descr_post_path, 1, false, + expr_is_from_block, ) }) .is_some() @@ -373,6 +403,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { descr_post: &str, plural_len: usize, is_inner: bool, + expr_is_from_block: bool, ) { let plural_suffix = pluralize!(plural_len); @@ -380,21 +411,51 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { MustUsePath::Suppressed => {} MustUsePath::Boxed(path) => { let descr_pre = &format!("{}boxed ", descr_pre); - emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len, true); + emit_must_use_untranslated( + cx, + path, + descr_pre, + descr_post, + plural_len, + true, + expr_is_from_block, + ); } MustUsePath::Opaque(path) => { let descr_pre = &format!("{}implementer{} of ", descr_pre, plural_suffix); - emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len, true); + emit_must_use_untranslated( + cx, + path, + descr_pre, + descr_post, + plural_len, + true, + expr_is_from_block, + ); } MustUsePath::TraitObject(path) => { let descr_post = &format!(" trait object{}{}", plural_suffix, descr_post); - emit_must_use_untranslated(cx, path, descr_pre, descr_post, plural_len, true); + emit_must_use_untranslated( + cx, + path, + descr_pre, + descr_post, + plural_len, + true, + expr_is_from_block, + ); } MustUsePath::TupleElement(elems) => { for (index, path) in elems { let descr_post = &format!(" in tuple element {}", index); emit_must_use_untranslated( - cx, path, descr_pre, descr_post, plural_len, true, + cx, + path, + descr_pre, + descr_post, + plural_len, + true, + expr_is_from_block, ); } } @@ -407,6 +468,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { descr_post, plural_len.saturating_add(usize::try_from(*len).unwrap_or(usize::MAX)), true, + expr_is_from_block, ); } MustUsePath::Closure(span) => { @@ -433,8 +495,14 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { cx, def_id: *def_id, note: *reason, - suggestion: (!is_inner) - .then_some(UnusedDefSuggestion { span: span.shrink_to_lo() }), + suggestion: (!is_inner).then_some(if expr_is_from_block { + UnusedDefSuggestion::BlockTailExpr { + before_span: span.shrink_to_lo(), + after_span: span.shrink_to_hi(), + } + } else { + UnusedDefSuggestion::NormalExpr { span: span.shrink_to_lo() } + }), }, ); } diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index 8266e899011cb..80289ca08c3fc 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -308,7 +308,7 @@ mod prim_never {} /// /// ```no_run /// // Undefined behaviour -/// unsafe { char::from_u32_unchecked(0x110000) }; +/// let _ = unsafe { char::from_u32_unchecked(0x110000) }; /// ``` /// /// USVs are also the exact set of values that may be encoded in UTF-8. Because diff --git a/library/core/tests/ptr.rs b/library/core/tests/ptr.rs index c02cd99cc4477..ee885adfeee61 100644 --- a/library/core/tests/ptr.rs +++ b/library/core/tests/ptr.rs @@ -1001,7 +1001,7 @@ fn nonnull_tagged_pointer_with_provenance() { assert_eq!(p.tag(), 3); assert_eq!(unsafe { *p.pointer().as_ptr() }, 10); - unsafe { Box::from_raw(p.pointer().as_ptr()) }; + unsafe { drop(Box::from_raw(p.pointer().as_ptr())) }; /// A non-null pointer type which carries several bits of metadata and maintains provenance. #[repr(transparent)] diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 8266e899011cb..80289ca08c3fc 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -308,7 +308,7 @@ mod prim_never {} /// /// ```no_run /// // Undefined behaviour -/// unsafe { char::from_u32_unchecked(0x110000) }; +/// let _ = unsafe { char::from_u32_unchecked(0x110000) }; /// ``` /// /// USVs are also the exact set of values that may be encoded in UTF-8. Because diff --git a/src/tools/clippy/tests/ui/transmute_ptr_to_ref.fixed b/src/tools/clippy/tests/ui/transmute_ptr_to_ref.fixed index 575dadde90630..ac55ab5a8e2d1 100644 --- a/src/tools/clippy/tests/ui/transmute_ptr_to_ref.fixed +++ b/src/tools/clippy/tests/ui/transmute_ptr_to_ref.fixed @@ -2,6 +2,7 @@ #![warn(clippy::transmute_ptr_to_ref)] #![allow(clippy::match_single_binding)] +#![allow(unused_must_use)] unsafe fn _ptr_to_ref(p: *const T, m: *mut T, o: *const U, om: *mut U) { let _: &T = &*p; @@ -38,7 +39,7 @@ fn _issue1231() { type Bar<'a> = &'a u8; let raw = 42 as *const i32; - unsafe { &*(raw as *const u8) }; + let _ = unsafe { &*(raw as *const u8) }; } unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'b u32 { diff --git a/src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs b/src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs index 4238ff804780e..901a3e90dbecc 100644 --- a/src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs +++ b/src/tools/clippy/tests/ui/transmute_ptr_to_ref.rs @@ -2,6 +2,7 @@ #![warn(clippy::transmute_ptr_to_ref)] #![allow(clippy::match_single_binding)] +#![allow(unused_must_use)] unsafe fn _ptr_to_ref(p: *const T, m: *mut T, o: *const U, om: *mut U) { let _: &T = std::mem::transmute(p); @@ -38,7 +39,7 @@ fn _issue1231() { type Bar<'a> = &'a u8; let raw = 42 as *const i32; - unsafe { std::mem::transmute::<_, Bar>(raw) }; + let _ = unsafe { std::mem::transmute::<_, Bar>(raw) }; } unsafe fn _issue8924<'a, 'b, 'c>(x: *const &'a u32, y: *const &'b u32) -> &'c &'b u32 { diff --git a/src/tools/clippy/tests/ui/transmute_ptr_to_ref.stderr b/src/tools/clippy/tests/ui/transmute_ptr_to_ref.stderr index b3e6c09d2d7a1..68007edc41028 100644 --- a/src/tools/clippy/tests/ui/transmute_ptr_to_ref.stderr +++ b/src/tools/clippy/tests/ui/transmute_ptr_to_ref.stderr @@ -1,5 +1,5 @@ error: transmute from a pointer type (`*const T`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:7:17 + --> $DIR/transmute_ptr_to_ref.rs:8:17 | LL | let _: &T = std::mem::transmute(p); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*p` @@ -7,127 +7,127 @@ LL | let _: &T = std::mem::transmute(p); = note: `-D clippy::transmute-ptr-to-ref` implied by `-D warnings` error: transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:10:21 + --> $DIR/transmute_ptr_to_ref.rs:11:21 | LL | let _: &mut T = std::mem::transmute(m); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *m` error: transmute from a pointer type (`*mut T`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:13:17 + --> $DIR/transmute_ptr_to_ref.rs:14:17 | LL | let _: &T = std::mem::transmute(m); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*m` error: transmute from a pointer type (`*mut T`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:16:21 + --> $DIR/transmute_ptr_to_ref.rs:17:21 | LL | let _: &mut T = std::mem::transmute(p as *mut T); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(p as *mut T)` error: transmute from a pointer type (`*const U`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:19:17 + --> $DIR/transmute_ptr_to_ref.rs:20:17 | LL | let _: &T = std::mem::transmute(o); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(o as *const T)` error: transmute from a pointer type (`*mut U`) to a reference type (`&mut T`) - --> $DIR/transmute_ptr_to_ref.rs:22:21 + --> $DIR/transmute_ptr_to_ref.rs:23:21 | LL | let _: &mut T = std::mem::transmute(om); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(om as *mut T)` error: transmute from a pointer type (`*mut U`) to a reference type (`&T`) - --> $DIR/transmute_ptr_to_ref.rs:25:17 + --> $DIR/transmute_ptr_to_ref.rs:26:17 | LL | let _: &T = std::mem::transmute(om); | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(om as *const T)` error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, u8>`) - --> $DIR/transmute_ptr_to_ref.rs:35:32 + --> $DIR/transmute_ptr_to_ref.rs:36:32 | LL | let _: &Foo = unsafe { std::mem::transmute::<_, &Foo<_>>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*raw.cast::>()` error: transmute from a pointer type (`*const i32`) to a reference type (`&_issue1231::Foo<'_, &u8>`) - --> $DIR/transmute_ptr_to_ref.rs:37:33 + --> $DIR/transmute_ptr_to_ref.rs:38:33 | LL | let _: &Foo<&u8> = unsafe { std::mem::transmute::<_, &Foo<&_>>(raw) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*raw.cast::>()` error: transmute from a pointer type (`*const i32`) to a reference type (`&u8`) - --> $DIR/transmute_ptr_to_ref.rs:41:14 + --> $DIR/transmute_ptr_to_ref.rs:42:22 | -LL | unsafe { std::mem::transmute::<_, Bar>(raw) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const u8)` +LL | let _ = unsafe { std::mem::transmute::<_, Bar>(raw) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(raw as *const u8)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:46:14 + --> $DIR/transmute_ptr_to_ref.rs:47:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:47:14 + --> $DIR/transmute_ptr_to_ref.rs:48:14 | LL | 1 => std::mem::transmute(y), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*y.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:48:14 + --> $DIR/transmute_ptr_to_ref.rs:49:14 | LL | 2 => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&'b u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:49:14 + --> $DIR/transmute_ptr_to_ref.rs:50:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(y), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*y.cast::<&'b u32>()` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:57:19 + --> $DIR/transmute_ptr_to_ref.rs:58:19 | LL | let _: &u32 = std::mem::transmute(a); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:58:19 + --> $DIR/transmute_ptr_to_ref.rs:59:19 | LL | let _: &u32 = std::mem::transmute::<_, &u32>(a); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a.cast::()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:60:14 + --> $DIR/transmute_ptr_to_ref.rs:61:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&u32>()` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:61:14 + --> $DIR/transmute_ptr_to_ref.rs:62:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*x.cast::<&'b u32>()` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:69:19 + --> $DIR/transmute_ptr_to_ref.rs:70:19 | LL | let _: &u32 = std::mem::transmute(a); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*a` error: transmute from a pointer type (`*const u32`) to a reference type (`&u32`) - --> $DIR/transmute_ptr_to_ref.rs:70:19 + --> $DIR/transmute_ptr_to_ref.rs:71:19 | LL | let _: &u32 = std::mem::transmute::<_, &u32>(a); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(a as *const u32)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:72:14 + --> $DIR/transmute_ptr_to_ref.rs:73:14 | LL | 0 => std::mem::transmute(x), | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(x as *const () as *const &u32)` error: transmute from a pointer type (`*const &u32`) to a reference type (`&&u32`) - --> $DIR/transmute_ptr_to_ref.rs:73:14 + --> $DIR/transmute_ptr_to_ref.rs:74:14 | LL | _ => std::mem::transmute::<_, &&'b u32>(x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(x as *const () as *const &'b u32)` diff --git a/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.rs b/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.rs index 03113585d1464..366930a831c88 100644 --- a/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.rs +++ b/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.rs @@ -10,7 +10,7 @@ fn fill(v: &mut i32) { } fn evil() { - unsafe { &mut *(LEAK as *mut i32) }; //~ ERROR: is a dangling pointer + let _ = unsafe { &mut *(LEAK as *mut i32) }; //~ ERROR: is a dangling pointer } fn main() { diff --git a/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.stderr b/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.stderr index 2ba8116cadc24..6c41add60ef4a 100644 --- a/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.stderr +++ b/src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.stderr @@ -1,8 +1,8 @@ error: Undefined Behavior: dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance) --> $DIR/storage_dead_dangling.rs:LL:CC | -LL | unsafe { &mut *(LEAK as *mut i32) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance) +LL | let _ = unsafe { &mut *(LEAK as *mut i32) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance) | = 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/uninit_uninhabited_type.rs b/src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.rs index e606d8b283c8a..59781f023661c 100644 --- a/src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.rs +++ b/src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.rs @@ -2,6 +2,6 @@ #[allow(deprecated, invalid_value)] fn main() { - unsafe { std::mem::uninitialized::() }; + let _ = unsafe { std::mem::uninitialized::() }; //~^ ERROR: attempted to instantiate uninhabited type `!` } diff --git a/src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr b/src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr index 150128ba2a41c..f2cc343032627 100644 --- a/src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr +++ b/src/tools/miri/tests/fail/intrinsics/uninit_uninhabited_type.stderr @@ -1,8 +1,8 @@ error: abnormal termination: aborted execution: attempted to instantiate uninhabited type `!` --> $DIR/uninit_uninhabited_type.rs:LL:CC | -LL | unsafe { std::mem::uninitialized::() }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!` +LL | let _ = unsafe { std::mem::uninitialized::() }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!` | = note: inside `main` at $DIR/uninit_uninhabited_type.rs:LL:CC diff --git a/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.rs b/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.rs index 6d9ae14c5c479..e9c6e464e88cf 100644 --- a/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.rs +++ b/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.rs @@ -1,5 +1,5 @@ #[allow(deprecated, invalid_value)] fn main() { - unsafe { std::mem::zeroed::() }; + let _ = unsafe { std::mem::zeroed::() }; //~^ ERROR: attempted to zero-initialize type `fn()`, which is invalid } diff --git a/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr b/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr index 9d44ba9f746ad..77d5822804315 100644 --- a/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr +++ b/src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr @@ -1,8 +1,8 @@ error: abnormal termination: aborted execution: attempted to zero-initialize type `fn()`, which is invalid --> $DIR/zero_fn_ptr.rs:LL:CC | -LL | unsafe { std::mem::zeroed::() }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `fn()`, which is invalid +LL | let _ = unsafe { std::mem::zeroed::() }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `fn()`, which is invalid | = note: inside `main` at $DIR/zero_fn_ptr.rs:LL:CC diff --git a/tests/ui/issues/issue-1460.stderr b/tests/ui/issues/issue-1460.stderr index eb7661fad56db..d4a8c8955e233 100644 --- a/tests/ui/issues/issue-1460.stderr +++ b/tests/ui/issues/issue-1460.stderr @@ -1,8 +1,8 @@ warning: unused closure that must be used - --> $DIR/issue-1460.rs:6:5 + --> $DIR/issue-1460.rs:6:6 | LL | {|i: u32| if 1 == i { }}; - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^ | = note: closures are lazy and do nothing unless called = note: `#[warn(unused_must_use)]` on by default diff --git a/tests/ui/lint/unused/must-use-block-expr.fixed b/tests/ui/lint/unused/must-use-block-expr.fixed new file mode 100644 index 0000000000000..642012812bd2a --- /dev/null +++ b/tests/ui/lint/unused/must-use-block-expr.fixed @@ -0,0 +1,36 @@ +// run-rustfix +// check-pass + +#![warn(unused_must_use)] + +#[must_use] +fn foo() -> i32 { + 42 +} + +fn bar() { + { + let _ = foo(); + //~^ WARN unused return value + } +} + +fn baz() { + { + let _ = foo(); + //~^ WARN unused return value + }; +} + +fn main() { + bar(); + baz(); + { + let _ = 1 + 2; + //~^ WARN unused arithmetic operation + } + { + let _ = 1 + 2; + //~^ WARN unused arithmetic operation + }; +} diff --git a/tests/ui/lint/unused/must-use-block-expr.rs b/tests/ui/lint/unused/must-use-block-expr.rs new file mode 100644 index 0000000000000..e0a680aa07d08 --- /dev/null +++ b/tests/ui/lint/unused/must-use-block-expr.rs @@ -0,0 +1,36 @@ +// run-rustfix +// check-pass + +#![warn(unused_must_use)] + +#[must_use] +fn foo() -> i32 { + 42 +} + +fn bar() { + { + foo(); + //~^ WARN unused return value + } +} + +fn baz() { + { + foo() + //~^ WARN unused return value + }; +} + +fn main() { + bar(); + baz(); + { + 1 + 2; + //~^ WARN unused arithmetic operation + } + { + 1 + 2 + //~^ WARN unused arithmetic operation + }; +} diff --git a/tests/ui/lint/unused/must-use-block-expr.stderr b/tests/ui/lint/unused/must-use-block-expr.stderr new file mode 100644 index 0000000000000..d821beb1d92e7 --- /dev/null +++ b/tests/ui/lint/unused/must-use-block-expr.stderr @@ -0,0 +1,51 @@ +warning: unused return value of `foo` that must be used + --> $DIR/must-use-block-expr.rs:13:9 + | +LL | foo(); + | ^^^^^ + | +note: the lint level is defined here + --> $DIR/must-use-block-expr.rs:4:9 + | +LL | #![warn(unused_must_use)] + | ^^^^^^^^^^^^^^^ +help: use `let _ = ...` to ignore the resulting value + | +LL | let _ = foo(); + | +++++++ + +warning: unused return value of `foo` that must be used + --> $DIR/must-use-block-expr.rs:20:9 + | +LL | foo() + | ^^^^^ + | +help: use `let _ = ...` to ignore the resulting value + | +LL | let _ = foo(); + | +++++++ + + +warning: unused arithmetic operation that must be used + --> $DIR/must-use-block-expr.rs:29:9 + | +LL | 1 + 2; + | ^^^^^ the arithmetic operation produces a value + | +help: use `let _ = ...` to ignore the resulting value + | +LL | let _ = 1 + 2; + | +++++++ + +warning: unused arithmetic operation that must be used + --> $DIR/must-use-block-expr.rs:33:9 + | +LL | 1 + 2 + | ^^^^^ the arithmetic operation produces a value + | +help: use `let _ = ...` to ignore the resulting value + | +LL | let _ = 1 + 2; + | +++++++ + + +warning: 4 warnings emitted + From ee7e717322f83d4b055e15b9948c07dd59118a5c Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Wed, 14 Jun 2023 08:03:35 +0000 Subject: [PATCH 05/10] tweak suggestion for argument-position `impl ?Sized` --- compiler/rustc_middle/src/ty/diagnostics.rs | 41 +++++++++++++++------ tests/ui/trait-bounds/apit-unsized.rs | 4 ++ tests/ui/trait-bounds/apit-unsized.stderr | 41 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 tests/ui/trait-bounds/apit-unsized.rs create mode 100644 tests/ui/trait-bounds/apit-unsized.stderr diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 9c948dba1e438..1c80692c306eb 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -14,8 +14,8 @@ use rustc_errors::{Applicability, Diagnostic, DiagnosticArgValue, IntoDiagnostic use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::DefId; -use rustc_hir::WherePredicate; -use rustc_span::Span; +use rustc_hir::{PredicateOrigin, WherePredicate}; +use rustc_span::{BytePos, Span}; use rustc_type_ir::sty::TyKind::*; impl<'tcx> IntoDiagnosticArg for Ty<'tcx> { @@ -156,10 +156,11 @@ enum SuggestChangingConstraintsMessage<'a> { RestrictBoundFurther, RestrictType { ty: &'a str }, RestrictTypeFurther { ty: &'a str }, - RemovingQSized, + RemoveMaybeUnsized, + ReplaceMaybeUnsizedWithSized, } -fn suggest_removing_unsized_bound( +fn suggest_changing_unsized_bound( generics: &hir::Generics<'_>, suggestions: &mut Vec<(Span, String, SuggestChangingConstraintsMessage<'_>)>, param: &hir::GenericParam<'_>, @@ -183,12 +184,25 @@ fn suggest_removing_unsized_bound( if poly.trait_ref.trait_def_id() != def_id { continue; } - let sp = generics.span_for_bound_removal(where_pos, pos); - suggestions.push(( - sp, - String::new(), - SuggestChangingConstraintsMessage::RemovingQSized, - )); + if predicate.origin == PredicateOrigin::ImplTrait && predicate.bounds.len() == 1 { + // For `impl ?Sized` with no other bounds, suggest `impl Sized` instead. + let bound_span = bound.span(); + if bound_span.can_be_used_for_suggestions() { + let question_span = bound_span.with_hi(bound_span.lo() + BytePos(1)); + suggestions.push(( + question_span, + String::new(), + SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized, + )); + } + } else { + let sp = generics.span_for_bound_removal(where_pos, pos); + suggestions.push(( + sp, + String::new(), + SuggestChangingConstraintsMessage::RemoveMaybeUnsized, + )); + } } } } @@ -245,7 +259,7 @@ pub fn suggest_constraining_type_params<'a>( param.span, format!("this type parameter needs to be `{}`", constraint), ); - suggest_removing_unsized_bound(generics, &mut suggestions, param, def_id); + suggest_changing_unsized_bound(generics, &mut suggestions, param, def_id); } } @@ -395,9 +409,12 @@ pub fn suggest_constraining_type_params<'a>( SuggestChangingConstraintsMessage::RestrictTypeFurther { ty } => { Cow::from(format!("consider further restricting type parameter `{}`", ty)) } - SuggestChangingConstraintsMessage::RemovingQSized => { + SuggestChangingConstraintsMessage::RemoveMaybeUnsized => { Cow::from("consider removing the `?Sized` bound to make the type parameter `Sized`") } + SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized => { + Cow::from("consider replacing `?Sized` with `Sized`") + } }; err.span_suggestion_verbose(span, msg, suggestion, applicability); diff --git a/tests/ui/trait-bounds/apit-unsized.rs b/tests/ui/trait-bounds/apit-unsized.rs new file mode 100644 index 0000000000000..469d6a6345ed9 --- /dev/null +++ b/tests/ui/trait-bounds/apit-unsized.rs @@ -0,0 +1,4 @@ +fn foo(_: impl Iterator + ?Sized) {} //~ ERROR [E0277] +fn bar(_: impl ?Sized) {} //~ ERROR [E0277] + +fn main() {} diff --git a/tests/ui/trait-bounds/apit-unsized.stderr b/tests/ui/trait-bounds/apit-unsized.stderr new file mode 100644 index 0000000000000..7a4a2274d4498 --- /dev/null +++ b/tests/ui/trait-bounds/apit-unsized.stderr @@ -0,0 +1,41 @@ +error[E0277]: the size for values of type `impl Iterator + ?Sized` cannot be known at compilation time + --> $DIR/apit-unsized.rs:1:8 + | +LL | fn foo(_: impl Iterator + ?Sized) {} + | ^ ---------------------------------- this type parameter needs to be `std::marker::Sized` + | | + | doesn't have a size known at compile-time + | + = help: unsized fn params are gated as an unstable feature +help: consider removing the `?Sized` bound to make the type parameter `Sized` + | +LL - fn foo(_: impl Iterator + ?Sized) {} +LL + fn foo(_: impl Iterator) {} + | +help: function arguments must have a statically known size, borrowed types always have a known size + | +LL | fn foo(_: &impl Iterator + ?Sized) {} + | + + +error[E0277]: the size for values of type `impl ?Sized` cannot be known at compilation time + --> $DIR/apit-unsized.rs:2:8 + | +LL | fn bar(_: impl ?Sized) {} + | ^ ----------- this type parameter needs to be `std::marker::Sized` + | | + | doesn't have a size known at compile-time + | + = help: unsized fn params are gated as an unstable feature +help: consider replacing `?Sized` with `Sized` + | +LL - fn bar(_: impl ?Sized) {} +LL + fn bar(_: impl Sized) {} + | +help: function arguments must have a statically known size, borrowed types always have a known size + | +LL | fn bar(_: &impl ?Sized) {} + | + + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. From b6a3f126c01d792c448f7b341352f591404bc19a Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Wed, 14 Jun 2023 19:22:03 +0200 Subject: [PATCH 06/10] change `std::marker::Sized` to just `Sized` --- compiler/rustc_middle/src/ty/diagnostics.rs | 7 ++--- .../const-argument-if-length.full.stderr | 2 +- .../const-argument-if-length.min.stderr | 2 +- .../dst/dst-object-from-unsized-type.stderr | 4 +-- .../issue-88287.stderr | 2 +- tests/ui/offset-of/offset-of-dst-field.stderr | 2 +- tests/ui/packed/issue-27060-2.stderr | 2 +- ...adt-param-with-implicit-sized-bound.stderr | 2 +- ...ltiline-trait-bound-in-where-clause.stderr | 6 ++-- tests/ui/trait-bounds/apit-unsized.stderr | 4 +-- tests/ui/trait-bounds/unsized-bound.stderr | 30 ++++++++----------- tests/ui/traits/suggest-where-clause.stderr | 4 +-- tests/ui/union/union-sized-field.stderr | 6 ++-- tests/ui/unsized/unsized-bare-typaram.stderr | 2 +- tests/ui/unsized/unsized-enum.stderr | 2 +- tests/ui/unsized/unsized-enum2.stderr | 8 ++--- tests/ui/unsized/unsized-fn-arg.stderr | 2 +- .../unsized-inherent-impl-self-type.stderr | 2 +- tests/ui/unsized/unsized-struct.stderr | 4 +-- .../unsized-trait-impl-self-type.stderr | 2 +- .../unsized-trait-impl-trait-arg.stderr | 2 +- tests/ui/unsized/unsized3.stderr | 12 ++++---- tests/ui/unsized/unsized5.stderr | 8 ++--- tests/ui/unsized/unsized6.stderr | 26 ++++++++-------- tests/ui/unsized/unsized7.stderr | 2 +- 25 files changed, 69 insertions(+), 76 deletions(-) diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 1c80692c306eb..d89baa9c88d83 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -252,13 +252,10 @@ pub fn suggest_constraining_type_params<'a>( { let mut sized_constraints = constraints.extract_if(|(_, def_id)| *def_id == tcx.lang_items().sized_trait()); - if let Some((constraint, def_id)) = sized_constraints.next() { + if let Some((_, def_id)) = sized_constraints.next() { applicability = Applicability::MaybeIncorrect; - err.span_label( - param.span, - format!("this type parameter needs to be `{}`", constraint), - ); + err.span_label(param.span, "this type parameter needs to be `Sized`"); suggest_changing_unsized_bound(generics, &mut suggestions, param, def_id); } } diff --git a/tests/ui/const-generics/const-argument-if-length.full.stderr b/tests/ui/const-generics/const-argument-if-length.full.stderr index 2ceba59cf05c7..7997026dfe498 100644 --- a/tests/ui/const-generics/const-argument-if-length.full.stderr +++ b/tests/ui/const-generics/const-argument-if-length.full.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/const-argument-if-length.rs:15:12 | LL | pub struct AtLeastByte { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | value: T, | ^ doesn't have a size known at compile-time | diff --git a/tests/ui/const-generics/const-argument-if-length.min.stderr b/tests/ui/const-generics/const-argument-if-length.min.stderr index f85e60f63fb3b..3ba9ffebd4de6 100644 --- a/tests/ui/const-generics/const-argument-if-length.min.stderr +++ b/tests/ui/const-generics/const-argument-if-length.min.stderr @@ -11,7 +11,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/const-argument-if-length.rs:15:12 | LL | pub struct AtLeastByte { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | value: T, | ^ doesn't have a size known at compile-time | diff --git a/tests/ui/dst/dst-object-from-unsized-type.stderr b/tests/ui/dst/dst-object-from-unsized-type.stderr index d5e464aed4ba7..cbb7dc5e9f421 100644 --- a/tests/ui/dst/dst-object-from-unsized-type.stderr +++ b/tests/ui/dst/dst-object-from-unsized-type.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/dst-object-from-unsized-type.rs:8:23 | LL | fn test1(t: &T) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | let u: &dyn Foo = t; | ^ doesn't have a size known at compile-time | @@ -17,7 +17,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/dst-object-from-unsized-type.rs:13:23 | LL | fn test2(t: &T) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | let v: &dyn Foo = t as &dyn Foo; | ^ doesn't have a size known at compile-time | diff --git a/tests/ui/generic-associated-types/issue-88287.stderr b/tests/ui/generic-associated-types/issue-88287.stderr index 1b84cce622929..d77076a28fab6 100644 --- a/tests/ui/generic-associated-types/issue-88287.stderr +++ b/tests/ui/generic-associated-types/issue-88287.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `A` cannot be known at compilation tim --> $DIR/issue-88287.rs:34:9 | LL | type SearchFutureTy<'f, A, B: 'f> - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | async move { todo!() } | ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time diff --git a/tests/ui/offset-of/offset-of-dst-field.stderr b/tests/ui/offset-of/offset-of-dst-field.stderr index 4eaceaa935817..3f613d947e488 100644 --- a/tests/ui/offset-of/offset-of-dst-field.stderr +++ b/tests/ui/offset-of/offset-of-dst-field.stderr @@ -70,7 +70,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/offset-of-dst-field.rs:50:5 | LL | fn generic_with_maybe_sized() -> usize { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | offset_of!(Delta, z) | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | diff --git a/tests/ui/packed/issue-27060-2.stderr b/tests/ui/packed/issue-27060-2.stderr index 0836ceaecd198..cf5f4e530dc4e 100644 --- a/tests/ui/packed/issue-27060-2.stderr +++ b/tests/ui/packed/issue-27060-2.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/issue-27060-2.rs:3:11 | LL | pub struct Bad { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | data: T, | ^ doesn't have a size known at compile-time | diff --git a/tests/ui/suggestions/adt-param-with-implicit-sized-bound.stderr b/tests/ui/suggestions/adt-param-with-implicit-sized-bound.stderr index b77c8c7fd5bcc..d136f5ff6543f 100644 --- a/tests/ui/suggestions/adt-param-with-implicit-sized-bound.stderr +++ b/tests/ui/suggestions/adt-param-with-implicit-sized-bound.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/adt-param-with-implicit-sized-bound.rs:25:9 | LL | struct Struct5{ - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | _t: X, | ^^^^ doesn't have a size known at compile-time | diff --git a/tests/ui/suggestions/removal-of-multiline-trait-bound-in-where-clause.stderr b/tests/ui/suggestions/removal-of-multiline-trait-bound-in-where-clause.stderr index 6071b10d387c2..eb74679d66049 100644 --- a/tests/ui/suggestions/removal-of-multiline-trait-bound-in-where-clause.stderr +++ b/tests/ui/suggestions/removal-of-multiline-trait-bound-in-where-clause.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn foo(foo: Wrapper) | - ^^^^^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Wrapper` --> $DIR/removal-of-multiline-trait-bound-in-where-clause.rs:1:16 @@ -33,7 +33,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn bar(foo: Wrapper) | - ^^^^^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Wrapper` --> $DIR/removal-of-multiline-trait-bound-in-where-clause.rs:1:16 @@ -58,7 +58,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn qux(foo: Wrapper) | - ^^^^^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Wrapper` --> $DIR/removal-of-multiline-trait-bound-in-where-clause.rs:1:16 diff --git a/tests/ui/trait-bounds/apit-unsized.stderr b/tests/ui/trait-bounds/apit-unsized.stderr index 7a4a2274d4498..0f2dc52599f63 100644 --- a/tests/ui/trait-bounds/apit-unsized.stderr +++ b/tests/ui/trait-bounds/apit-unsized.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `impl Iterator + ?Sized` c --> $DIR/apit-unsized.rs:1:8 | LL | fn foo(_: impl Iterator + ?Sized) {} - | ^ ---------------------------------- this type parameter needs to be `std::marker::Sized` + | ^ ---------------------------------- this type parameter needs to be `Sized` | | | doesn't have a size known at compile-time | @@ -21,7 +21,7 @@ error[E0277]: the size for values of type `impl ?Sized` cannot be known at compi --> $DIR/apit-unsized.rs:2:8 | LL | fn bar(_: impl ?Sized) {} - | ^ ----------- this type parameter needs to be `std::marker::Sized` + | ^ ----------- this type parameter needs to be `Sized` | | | doesn't have a size known at compile-time | diff --git a/tests/ui/trait-bounds/unsized-bound.stderr b/tests/ui/trait-bounds/unsized-bound.stderr index da27ba1c58dbe..4d45bffabce5e 100644 --- a/tests/ui/trait-bounds/unsized-bound.stderr +++ b/tests/ui/trait-bounds/unsized-bound.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `B` cannot be known at compilation tim LL | impl Trait<(A, B)> for (A, B) where A: ?Sized, B: ?Sized, {} | - ^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = note: required because it appears within the type `(A, B)` note: required by a bound in `Trait` @@ -28,7 +28,7 @@ error[E0277]: the size for values of type `A` cannot be known at compilation tim LL | impl Trait<(A, B)> for (A, B) where A: ?Sized, B: ?Sized, {} | - ^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = note: only the last element of a tuple may have a dynamically sized type help: consider removing the `?Sized` bound to make the type parameter `Sized` @@ -43,7 +43,7 @@ error[E0277]: the size for values of type `C` cannot be known at compilation tim LL | impl Trait<(A, B, C)> for (A, B, C) where A: ?Sized, {} | - ^^^^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = note: required because it appears within the type `(A, B, C)` note: required by a bound in `Trait` @@ -65,9 +65,7 @@ error[E0277]: the size for values of type `A` cannot be known at compilation tim --> $DIR/unsized-bound.rs:5:52 | LL | impl Trait<(A, B, C)> for (A, B, C) where A: ?Sized, {} - | - ^^^^^^^^^ doesn't have a size known at compile-time - | | - | this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ^^^^^^^^^ doesn't have a size known at compile-time | = note: only the last element of a tuple may have a dynamically sized type help: consider removing the `?Sized` bound to make the type parameter `Sized` @@ -80,9 +78,7 @@ error[E0277]: the size for values of type `B` cannot be known at compilation tim --> $DIR/unsized-bound.rs:5:52 | LL | impl Trait<(A, B, C)> for (A, B, C) where A: ?Sized, {} - | - ^^^^^^^^^ doesn't have a size known at compile-time - | | - | this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ^^^^^^^^^ doesn't have a size known at compile-time | = note: only the last element of a tuple may have a dynamically sized type help: consider removing the `?Sized` bound to make the type parameter `Sized` @@ -97,7 +93,7 @@ error[E0277]: the size for values of type `B` cannot be known at compilation tim LL | impl Trait2<(A, B)> for (A, B) {} | - ^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = note: required because it appears within the type `(A, B)` note: required by a bound in `Trait2` @@ -121,7 +117,7 @@ error[E0277]: the size for values of type `A` cannot be known at compilation tim LL | impl Trait2<(A, B)> for (A, B) {} | - ^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = note: only the last element of a tuple may have a dynamically sized type help: consider removing the `?Sized` bound to make the type parameter `Sized` @@ -136,7 +132,7 @@ error[E0277]: the size for values of type `A` cannot be known at compilation tim LL | impl Trait3 for A where A: ?Sized {} | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Trait3` --> $DIR/unsized-bound.rs:13:14 @@ -159,7 +155,7 @@ error[E0277]: the size for values of type `A` cannot be known at compilation tim LL | impl Trait4 for A {} | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Trait4` --> $DIR/unsized-bound.rs:16:14 @@ -182,7 +178,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | impl Trait5 for X where X: ?Sized {} | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Trait5` --> $DIR/unsized-bound.rs:19:14 @@ -205,7 +201,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | impl Trait6 for X {} | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Trait6` --> $DIR/unsized-bound.rs:22:14 @@ -228,7 +224,7 @@ error[E0277]: the size for values of type `Y` cannot be known at compilation tim LL | impl Trait7 for X where Y: ?Sized {} | - ^^^^^^^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Trait7` --> $DIR/unsized-bound.rs:25:17 @@ -251,7 +247,7 @@ error[E0277]: the size for values of type `Y` cannot be known at compilation tim LL | impl Trait8 for X {} | - ^^^^^^^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Trait8` --> $DIR/unsized-bound.rs:28:17 diff --git a/tests/ui/traits/suggest-where-clause.stderr b/tests/ui/traits/suggest-where-clause.stderr index 44e63b78cce85..f3a4c68903377 100644 --- a/tests/ui/traits/suggest-where-clause.stderr +++ b/tests/ui/traits/suggest-where-clause.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `U` cannot be known at compilation tim --> $DIR/suggest-where-clause.rs:7:20 | LL | fn check() { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | // suggest a where-clause, if needed LL | mem::size_of::(); | ^ doesn't have a size known at compile-time @@ -19,7 +19,7 @@ error[E0277]: the size for values of type `U` cannot be known at compilation tim --> $DIR/suggest-where-clause.rs:10:20 | LL | fn check() { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | mem::size_of::>(); | ^^^^^^^ doesn't have a size known at compile-time diff --git a/tests/ui/union/union-sized-field.stderr b/tests/ui/union/union-sized-field.stderr index bf1ff9c8bc154..0a79f8bba01a0 100644 --- a/tests/ui/union/union-sized-field.stderr +++ b/tests/ui/union/union-sized-field.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/union-sized-field.rs:4:12 | LL | union Foo { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | value: ManuallyDrop, | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | @@ -28,7 +28,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/union-sized-field.rs:9:12 | LL | struct Foo2 { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | value: ManuallyDrop, | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | @@ -54,7 +54,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim --> $DIR/union-sized-field.rs:15:11 | LL | enum Foo3 { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | Value(ManuallyDrop), | ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | diff --git a/tests/ui/unsized/unsized-bare-typaram.stderr b/tests/ui/unsized/unsized-bare-typaram.stderr index 1eff14be8e19a..daef984404a79 100644 --- a/tests/ui/unsized/unsized-bare-typaram.stderr +++ b/tests/ui/unsized/unsized-bare-typaram.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn foo() { bar::() } | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `bar` --> $DIR/unsized-bare-typaram.rs:1:8 diff --git a/tests/ui/unsized/unsized-enum.stderr b/tests/ui/unsized/unsized-enum.stderr index 5f2e224308f3a..9e6408e814367 100644 --- a/tests/ui/unsized/unsized-enum.stderr +++ b/tests/ui/unsized/unsized-enum.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn foo2() { not_sized::>() } | - ^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Foo` --> $DIR/unsized-enum.rs:4:10 diff --git a/tests/ui/unsized/unsized-enum2.stderr b/tests/ui/unsized/unsized-enum2.stderr index 00b80327c9b77..71cf782120e57 100644 --- a/tests/ui/unsized/unsized-enum2.stderr +++ b/tests/ui/unsized/unsized-enum2.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `W` cannot be known at compilation tim --> $DIR/unsized-enum2.rs:23:8 | LL | enum E { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | // parameter LL | VA(W), | ^ doesn't have a size known at compile-time @@ -27,7 +27,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized-enum2.rs:25:11 | LL | enum E { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | VB{x: X}, | ^ doesn't have a size known at compile-time @@ -52,7 +52,7 @@ error[E0277]: the size for values of type `Y` cannot be known at compilation tim --> $DIR/unsized-enum2.rs:27:15 | LL | enum E { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | VC(isize, Y), | ^ doesn't have a size known at compile-time @@ -77,7 +77,7 @@ error[E0277]: the size for values of type `Z` cannot be known at compilation tim --> $DIR/unsized-enum2.rs:29:21 | LL | enum E { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | VD{u: isize, x: Z}, | ^ doesn't have a size known at compile-time diff --git a/tests/ui/unsized/unsized-fn-arg.stderr b/tests/ui/unsized/unsized-fn-arg.stderr index 404fa5291b3e2..0f6fadde19a05 100644 --- a/tests/ui/unsized/unsized-fn-arg.stderr +++ b/tests/ui/unsized/unsized-fn-arg.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn f(t: T) {} | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = help: unsized fn params are gated as an unstable feature help: consider removing the `?Sized` bound to make the type parameter `Sized` diff --git a/tests/ui/unsized/unsized-inherent-impl-self-type.stderr b/tests/ui/unsized/unsized-inherent-impl-self-type.stderr index a952aa063d10b..9e5ad92eb04cd 100644 --- a/tests/ui/unsized/unsized-inherent-impl-self-type.stderr +++ b/tests/ui/unsized/unsized-inherent-impl-self-type.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | impl S5 { | - ^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `S5` --> $DIR/unsized-inherent-impl-self-type.rs:5:11 diff --git a/tests/ui/unsized/unsized-struct.stderr b/tests/ui/unsized/unsized-struct.stderr index dff1b0a5112a4..4e7cb09f0ccaf 100644 --- a/tests/ui/unsized/unsized-struct.stderr +++ b/tests/ui/unsized/unsized-struct.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn foo2() { not_sized::>() } | - ^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `Foo` --> $DIR/unsized-struct.rs:4:12 @@ -30,7 +30,7 @@ error[E0277]: the size for values of type `T` cannot be known at compilation tim LL | fn bar2() { is_sized::>() } | - ^^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required because it appears within the type `Bar` --> $DIR/unsized-struct.rs:11:8 diff --git a/tests/ui/unsized/unsized-trait-impl-self-type.stderr b/tests/ui/unsized/unsized-trait-impl-self-type.stderr index f6ba9a80cb1c2..4955d463fc26f 100644 --- a/tests/ui/unsized/unsized-trait-impl-self-type.stderr +++ b/tests/ui/unsized/unsized-trait-impl-self-type.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | impl T3 for S5 { | - ^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `S5` --> $DIR/unsized-trait-impl-self-type.rs:8:11 diff --git a/tests/ui/unsized/unsized-trait-impl-trait-arg.stderr b/tests/ui/unsized/unsized-trait-impl-trait-arg.stderr index f81487d5231ab..8761c293af465 100644 --- a/tests/ui/unsized/unsized-trait-impl-trait-arg.stderr +++ b/tests/ui/unsized/unsized-trait-impl-trait-arg.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | impl T2 for S4 { | - ^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `T2` --> $DIR/unsized-trait-impl-trait-arg.rs:4:10 diff --git a/tests/ui/unsized/unsized3.stderr b/tests/ui/unsized/unsized3.stderr index 9ad1ac6b4df61..3ef9a875358c0 100644 --- a/tests/ui/unsized/unsized3.stderr +++ b/tests/ui/unsized/unsized3.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized3.rs:7:13 | LL | fn f1(x: &X) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f2::(x); | ------- ^ doesn't have a size known at compile-time | | @@ -27,7 +27,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized3.rs:18:13 | LL | fn f3(x: &X) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f4::(x); | ------- ^ doesn't have a size known at compile-time | | @@ -52,7 +52,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized3.rs:33:8 | LL | fn f8(x1: &S, x2: &S) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f5(x1); | -- ^^ doesn't have a size known at compile-time | | @@ -82,7 +82,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized3.rs:40:5 | LL | fn f9(x1: Box>) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f5(&(*x1, 34)); | ^^ doesn't have a size known at compile-time | @@ -102,7 +102,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized3.rs:45:9 | LL | fn f10(x1: Box>) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f5(&(32, *x1)); | ^^^^^^^^^ doesn't have a size known at compile-time | @@ -123,7 +123,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized3.rs:45:8 | LL | fn f10(x1: Box>) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f5(&(32, *x1)); | -- ^^^^^^^^^^ doesn't have a size known at compile-time | | diff --git a/tests/ui/unsized/unsized5.stderr b/tests/ui/unsized/unsized5.stderr index 03ed0c4574ab9..53e7fc17ef9f3 100644 --- a/tests/ui/unsized/unsized5.stderr +++ b/tests/ui/unsized/unsized5.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized5.rs:4:9 | LL | struct S1 { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f1: X, | ^ doesn't have a size known at compile-time | @@ -26,7 +26,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized5.rs:10:8 | LL | struct S2 { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | f: isize, LL | g: X, | ^ doesn't have a size known at compile-time @@ -87,7 +87,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized5.rs:25:8 | LL | enum E { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | V1(X, isize), | ^ doesn't have a size known at compile-time | @@ -111,7 +111,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized5.rs:29:12 | LL | enum F { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | V2{f1: X, f: isize}, | ^ doesn't have a size known at compile-time | diff --git a/tests/ui/unsized/unsized6.stderr b/tests/ui/unsized/unsized6.stderr index 18ac1ea187515..56e7f60f9ff08 100644 --- a/tests/ui/unsized/unsized6.stderr +++ b/tests/ui/unsized/unsized6.stderr @@ -2,7 +2,7 @@ error[E0277]: the size for values of type `Y` cannot be known at compilation tim --> $DIR/unsized6.rs:9:9 | LL | fn f1(x: &X) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | let y: Y; | ^ doesn't have a size known at compile-time @@ -23,7 +23,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:7:12 | LL | fn f1(x: &X) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | let _: W; // <-- this is OK, no bindings created, no initializer. LL | let _: (isize, (X, isize)); | ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time @@ -39,7 +39,7 @@ error[E0277]: the size for values of type `Z` cannot be known at compilation tim --> $DIR/unsized6.rs:11:12 | LL | fn f1(x: &X) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | let y: (isize, (Z, usize)); | ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time @@ -55,7 +55,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:15:9 | LL | fn f2(x: &X) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | let y: X; | ^ doesn't have a size known at compile-time | @@ -75,7 +75,7 @@ error[E0277]: the size for values of type `Y` cannot be known at compilation tim --> $DIR/unsized6.rs:17:12 | LL | fn f2(x: &X) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | let y: (isize, (Y, isize)); | ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time @@ -91,7 +91,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:22:9 | LL | fn f3(x1: Box, x2: Box, x3: Box) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | let y: X = *x1; | ^ doesn't have a size known at compile-time | @@ -111,7 +111,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:24:9 | LL | fn f3(x1: Box, x2: Box, x3: Box) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | let y = *x2; | ^ doesn't have a size known at compile-time @@ -128,7 +128,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:26:10 | LL | fn f3(x1: Box, x2: Box, x3: Box) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | let (y, z) = (*x3, 4); | ^ doesn't have a size known at compile-time @@ -145,7 +145,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:30:9 | LL | fn f4(x1: Box, x2: Box, x3: Box) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` LL | let y: X = *x1; | ^ doesn't have a size known at compile-time | @@ -165,7 +165,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:32:9 | LL | fn f4(x1: Box, x2: Box, x3: Box) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | let y = *x2; | ^ doesn't have a size known at compile-time @@ -182,7 +182,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim --> $DIR/unsized6.rs:34:10 | LL | fn f4(x1: Box, x2: Box, x3: Box) { - | - this type parameter needs to be `std::marker::Sized` + | - this type parameter needs to be `Sized` ... LL | let (y, z) = (*x3, 4); | ^ doesn't have a size known at compile-time @@ -201,7 +201,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | fn g1(x: X) {} | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = help: unsized fn params are gated as an unstable feature help: consider removing the `?Sized` bound to make the type parameter `Sized` @@ -220,7 +220,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | fn g2(x: X) {} | - ^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | = help: unsized fn params are gated as an unstable feature help: consider removing the `?Sized` bound to make the type parameter `Sized` diff --git a/tests/ui/unsized/unsized7.stderr b/tests/ui/unsized/unsized7.stderr index 1555b9df4f8d4..c313a2724c038 100644 --- a/tests/ui/unsized/unsized7.stderr +++ b/tests/ui/unsized/unsized7.stderr @@ -4,7 +4,7 @@ error[E0277]: the size for values of type `X` cannot be known at compilation tim LL | impl T1 for S3 { | - ^^^^^ doesn't have a size known at compile-time | | - | this type parameter needs to be `std::marker::Sized` + | this type parameter needs to be `Sized` | note: required by a bound in `T1` --> $DIR/unsized7.rs:7:10 From c75e6e0f6c55d11ef06767cb42f617ae5e5b3870 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Thu, 15 Jun 2023 12:49:49 +0000 Subject: [PATCH 07/10] normalize closure output before relation --- .../src/type_check/input_output.rs | 16 +----- .../issue-112604-closure-output-normalize.rs | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 tests/ui/nll/issue-112604-closure-output-normalize.rs diff --git a/compiler/rustc_borrowck/src/type_check/input_output.rs b/compiler/rustc_borrowck/src/type_check/input_output.rs index a06d4bcc6c7ae..eec886b7be48f 100644 --- a/compiler/rustc_borrowck/src/type_check/input_output.rs +++ b/compiler/rustc_borrowck/src/type_check/input_output.rs @@ -124,21 +124,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { // Return types are a bit more complex. They may contain opaque `impl Trait` types. let mir_output_ty = body.local_decls[RETURN_PLACE].ty; let output_span = body.local_decls[RETURN_PLACE].source_info.span; - if let Err(terr) = self.eq_types( - normalized_output_ty, - mir_output_ty, - Locations::All(output_span), - ConstraintCategory::BoringNoLocation, - ) { - span_mirbug!( - self, - Location::START, - "equate_inputs_and_outputs: `{:?}=={:?}` failed with `{:?}`", - normalized_output_ty, - mir_output_ty, - terr - ); - }; + self.equate_normalized_input_or_output(normalized_output_ty, mir_output_ty, output_span); } #[instrument(skip(self), level = "debug")] diff --git a/tests/ui/nll/issue-112604-closure-output-normalize.rs b/tests/ui/nll/issue-112604-closure-output-normalize.rs new file mode 100644 index 0000000000000..e4c954eeb33d2 --- /dev/null +++ b/tests/ui/nll/issue-112604-closure-output-normalize.rs @@ -0,0 +1,49 @@ +//check-pass + +use higher_kinded_types::*; +mod higher_kinded_types { + pub(crate) trait HKT { + type Of<'lt>; + } + + pub(crate) trait WithLifetime<'lt> { + type T; + } + + impl WithLifetime<'any>> HKT for T { + type Of<'lt> = >::T; + } +} + +trait Trait { + type Gat<'lt>; +} + +impl Trait for () { + type Gat<'lt> = (); +} + +/// Same as `Trait`, but using HKTs rather than GATs +trait HTrait { + type Hat: ?Sized + HKT; +} + +impl HTrait for T { + type Hat = dyn for<'lt> WithLifetime<'lt, T = T::Gat<'lt>>; +} + +impl Trait for Box> { + type Gat<'lt> = Hat::Of<'lt>; +} + +fn existential() -> impl for<'a> Trait = ()> {} + +fn dyn_hoops( + _: T, +) -> Box WithLifetime<'a, T = T::Gat<'a>>>> { + loop {} +} + +fn main() { + let _ = || -> _ { dyn_hoops(existential()) }; +} From c560dbc95d708fa6762725f44ef38b86b5af9a16 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 15 Jun 2023 17:38:15 +0200 Subject: [PATCH 08/10] Migrate GUI colors test to original CSS color format --- tests/rustdoc-gui/headers-color.goml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/rustdoc-gui/headers-color.goml b/tests/rustdoc-gui/headers-color.goml index 7d83833a8bd0c..a7ac94c4943cc 100644 --- a/tests/rustdoc-gui/headers-color.goml +++ b/tests/rustdoc-gui/headers-color.goml @@ -42,29 +42,29 @@ call-function: ( "check-colors", { "theme": "ayu", - "color": "rgb(197, 197, 197)", - "code_header_color": "rgb(230, 225, 207)", + "color": "#c5c5c5", + "code_header_color": "#e6e1cf", "focus_background_color": "rgba(255, 236, 164, 0.06)", - "headings_color": "rgb(57, 175, 215)", + "headings_color": "#39afd7", }, ) call-function: ( "check-colors", { "theme": "dark", - "color": "rgb(221, 221, 221)", - "code_header_color": "rgb(221, 221, 221)", - "focus_background_color": "rgb(73, 74, 61)", - "headings_color": "rgb(210, 153, 29)", + "color": "#ddd", + "code_header_color": "#ddd", + "focus_background_color": "#494a3d", + "headings_color": "#d2991d", }, ) call-function: ( "check-colors", { "theme": "light", - "color": "rgb(0, 0, 0)", - "code_header_color": "rgb(0, 0, 0)", - "focus_background_color": "rgb(253, 255, 211)", - "headings_color": "rgb(56, 115, 173)", + "color": "black", + "code_header_color": "black", + "focus_background_color": "#fdffd3", + "headings_color": "#3873ad", }, ) From 71db99935a7dd98dfd9253dac55e58f098f56996 Mon Sep 17 00:00:00 2001 From: David Koloski Date: Thu, 15 Jun 2023 12:17:40 -0400 Subject: [PATCH 09/10] Add support for test tmpdir to fuchsia test runner Also format the script to keep the code nice. --- src/ci/docker/scripts/fuchsia-test-runner.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ci/docker/scripts/fuchsia-test-runner.py b/src/ci/docker/scripts/fuchsia-test-runner.py index ecef56f56f1d5..78a8a6662ea64 100755 --- a/src/ci/docker/scripts/fuchsia-test-runner.py +++ b/src/ci/docker/scripts/fuchsia-test-runner.py @@ -171,7 +171,6 @@ def ffx_isolate_dir(self): def home_dir(self): return os.path.join(self.tmp_dir(), "user-home") - def start_ffx_isolation(self): # Most of this is translated directly from ffx's isolate library os.mkdir(self.ffx_isolate_dir()) @@ -424,7 +423,7 @@ def start(self): ) # Create lockfiles - open(self.pm_lockfile_path(), 'a').close() + open(self.pm_lockfile_path(), "a").close() # Write to file self.write_to_file() @@ -458,6 +457,7 @@ def start(self): ], use: [ {{ storage: "data", path: "/data" }}, + {{ storage: "tmp", path: "/tmp" }}, {{ protocol: [ "fuchsia.process.Launcher" ] }}, {{ protocol: [ "fuchsia.posix.socket.Provider" ] }} ], @@ -571,6 +571,9 @@ def log(msg): if os.getenv("RUST_BACKTRACE") == None: env_vars += f'\n "RUST_BACKTRACE=0",' + # Use /tmp as the test temporary directory + env_vars += f'\n "RUST_TEST_TMPDIR=/tmp",' + cml.write( self.CML_TEMPLATE.format(env_vars=env_vars, exe_name=exe_name) ) @@ -642,7 +645,7 @@ def log(msg): log("Publishing package to repo...") # Publish package to repo - with open(self.pm_lockfile_path(), 'w') as pm_lockfile: + with open(self.pm_lockfile_path(), "w") as pm_lockfile: fcntl.lockf(pm_lockfile.fileno(), fcntl.LOCK_EX) subprocess.check_call( [ @@ -1045,9 +1048,7 @@ def print_help(args): ) debug_parser.set_defaults(func=debug) - syslog_parser = subparsers.add_parser( - "syslog", help="prints the device syslog" - ) + syslog_parser = subparsers.add_parser("syslog", help="prints the device syslog") syslog_parser.set_defaults(func=syslog) args = parser.parse_args() From 465e4d9c9cd13484ef2185b7957c53026104794e Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Thu, 15 Jun 2023 19:27:31 +0200 Subject: [PATCH 10/10] Fix comment for ptr alignment checks in codegen --- compiler/rustc_codegen_ssa/src/mir/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index e0cb26d3ba866..a4a8aad87269d 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -616,7 +616,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { AssertKind::MisalignedPointerDereference { ref required, ref found } => { let required = self.codegen_operand(bx, required).immediate(); let found = self.codegen_operand(bx, found).immediate(); - // It's `fn panic_bounds_check(index: usize, len: usize)`, + // It's `fn panic_misaligned_pointer_dereference(required: usize, found: usize)`, // and `#[track_caller]` adds an implicit third argument. (LangItem::PanicMisalignedPointerDereference, vec![required, found, location]) }