From 6d2f8af1db238e0a2af73470be950bee65a692e6 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Mon, 8 Nov 2021 00:55:00 -0500 Subject: [PATCH 01/14] Permit const assertions in stdlib --- library/core/src/panicking.rs | 1 + library/core/src/time.rs | 4 ++-- .../const_panic_stability.e2018.stderr | 16 ++++++++++++++++ .../const_panic_stability.e2021.stderr | 13 +++++++++++++ .../consts/const-eval/const_panic_stability.rs | 17 +++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/consts/const-eval/const_panic_stability.e2018.stderr create mode 100644 src/test/ui/consts/const-eval/const_panic_stability.e2021.stderr create mode 100644 src/test/ui/consts/const-eval/const_panic_stability.rs diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 29124c87e1bc5..eedea6562bd4d 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -36,6 +36,7 @@ use crate::panic::{Location, PanicInfo}; #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] #[cfg_attr(feature = "panic_immediate_abort", inline)] #[track_caller] +#[rustc_const_unstable(feature = "core_panic", issue = "none")] #[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators pub const fn panic(expr: &'static str) -> ! { // Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially diff --git a/library/core/src/time.rs b/library/core/src/time.rs index a054d72a8800a..7330c86a11a29 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -727,7 +727,7 @@ impl Duration { pub const fn from_secs_f64(secs: f64) -> Duration { match Duration::try_from_secs_f64(secs) { Ok(v) => v, - Err(e) => crate::panicking::panic(e.description()), + Err(e) => panic!("{}", e.description()), } } @@ -788,7 +788,7 @@ impl Duration { pub const fn from_secs_f32(secs: f32) -> Duration { match Duration::try_from_secs_f32(secs) { Ok(v) => v, - Err(e) => crate::panicking::panic(e.description()), + Err(e) => panic!("{}", e.description()), } } diff --git a/src/test/ui/consts/const-eval/const_panic_stability.e2018.stderr b/src/test/ui/consts/const-eval/const_panic_stability.e2018.stderr new file mode 100644 index 0000000000000..94cf64fff1999 --- /dev/null +++ b/src/test/ui/consts/const-eval/const_panic_stability.e2018.stderr @@ -0,0 +1,16 @@ +warning: panic message is not a string literal + --> $DIR/const_panic_stability.rs:14:12 + | +LL | panic!({ "foo" }); + | ^^^^^^^^^ + | + = note: `#[warn(non_fmt_panics)]` on by default + = note: this usage of panic!() is deprecated; it will be a hard error in Rust 2021 + = note: for more information, see +help: add a "{}" format string to Display the message + | +LL | panic!("{}", { "foo" }); + | +++++ + +warning: 1 warning emitted + diff --git a/src/test/ui/consts/const-eval/const_panic_stability.e2021.stderr b/src/test/ui/consts/const-eval/const_panic_stability.e2021.stderr new file mode 100644 index 0000000000000..9e8179181fd09 --- /dev/null +++ b/src/test/ui/consts/const-eval/const_panic_stability.e2021.stderr @@ -0,0 +1,13 @@ +error: format argument must be a string literal + --> $DIR/const_panic_stability.rs:14:12 + | +LL | panic!({ "foo" }); + | ^^^^^^^^^ + | +help: you might be missing a string literal to format with + | +LL | panic!("{}", { "foo" }); + | +++++ + +error: aborting due to previous error + diff --git a/src/test/ui/consts/const-eval/const_panic_stability.rs b/src/test/ui/consts/const-eval/const_panic_stability.rs new file mode 100644 index 0000000000000..1aee6f27e2752 --- /dev/null +++ b/src/test/ui/consts/const-eval/const_panic_stability.rs @@ -0,0 +1,17 @@ +// revisions: e2018 e2021 +//[e2018] edition:2018 +//[e2021] edition:2021 +//[e2018] check-pass +#![crate_type = "lib"] +#![stable(feature = "foo", since = "1.0.0")] +#![feature(staged_api)] + +#[stable(feature = "foo", since = "1.0.0")] +#[rustc_const_stable(feature = "foo", since = "1.0.0")] +const fn foo() { + assert!(false); + assert!(false, "foo"); + panic!({ "foo" }); + //[e2018]~^ WARNING panic message is not a string literal + //[e2021]~^^ ERROR format argument must be a string literal +} From 958de5a93864b90e7f90d7a5d0ce082c34232b59 Mon Sep 17 00:00:00 2001 From: Gus Wynn Date: Wed, 6 Oct 2021 13:19:39 -0700 Subject: [PATCH 02/14] warn on must_use use on async fn's --- compiler/rustc_passes/src/check_attr.rs | 32 +++++++++++++++ src/test/ui/lint/unused/unused-async.rs | 43 +++++++++++++++++++++ src/test/ui/lint/unused/unused-async.stderr | 26 +++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/test/ui/lint/unused/unused-async.rs create mode 100644 src/test/ui/lint/unused/unused-async.stderr diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 596d13d2d9acb..633a90dd0ec7c 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -112,6 +112,7 @@ impl CheckAttrVisitor<'tcx> { self.check_default_method_body_is_const(attr, span, target) } sym::must_not_suspend => self.check_must_not_suspend(&attr, span, target), + sym::must_use => self.check_must_use(hir_id, &attr, span, target), sym::rustc_const_unstable | sym::rustc_const_stable | sym::unstable @@ -1046,6 +1047,37 @@ impl CheckAttrVisitor<'tcx> { is_valid } + /// Warns against some misuses of `#[must_use]` + fn check_must_use( + &self, + hir_id: HirId, + attr: &Attribute, + span: &Span, + _target: Target, + ) -> bool { + let node = self.tcx.hir().get(hir_id); + if let Some(fn_node) = node.fn_kind() { + if let rustc_hir::IsAsync::Async = fn_node.asyncness() { + self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| { + lint.build( + "`must_use` attribute on `async` functions \ + applies to the anonymous `Future` returned by the \ + function, not the value within.", + ) + .span_label( + *span, + "this attribute does nothing, the `Future`s \ + returned by async functions are already `must_use`", + ) + .emit(); + }); + } + } + + // For now, its always valid + true + } + /// Checks if `#[must_not_suspend]` is applied to a function. Returns `true` if valid. fn check_must_not_suspend(&self, attr: &Attribute, span: &Span, target: Target) -> bool { match target { diff --git a/src/test/ui/lint/unused/unused-async.rs b/src/test/ui/lint/unused/unused-async.rs new file mode 100644 index 0000000000000..7d17af1157379 --- /dev/null +++ b/src/test/ui/lint/unused/unused-async.rs @@ -0,0 +1,43 @@ +// edition:2018 +// run-pass +#![allow(dead_code)] + +#[must_use] +//~^ WARNING `must_use` +async fn test() -> i32 { + 1 +} + + +struct Wowee {} + +impl Wowee { + #[must_use] + //~^ WARNING `must_use` + async fn test_method() -> i32 { + 1 + } +} + +/* FIXME(guswynn) update this test when async-fn-in-traits works +trait Doer { + #[must_use] + async fn test_trait_method() -> i32; + WARNING must_use + async fn test_other_trait() -> i32; +} + +impl Doer for Wowee { + async fn test_trait_method() -> i32 { + 1 + } + #[must_use] + async fn test_other_trait() -> i32 { + WARNING must_use + 1 + } +} +*/ + +fn main() { +} diff --git a/src/test/ui/lint/unused/unused-async.stderr b/src/test/ui/lint/unused/unused-async.stderr new file mode 100644 index 0000000000000..e470b6dac9473 --- /dev/null +++ b/src/test/ui/lint/unused/unused-async.stderr @@ -0,0 +1,26 @@ +warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within. + --> $DIR/unused-async.rs:5:1 + | +LL | #[must_use] + | ^^^^^^^^^^^ +LL | +LL | / async fn test() -> i32 { +LL | | 1 +LL | | } + | |_- this attribute does nothing, the `Future`s returned by async functions are already `must_use` + | + = note: `#[warn(unused_attributes)]` on by default + +warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within. + --> $DIR/unused-async.rs:15:5 + | +LL | #[must_use] + | ^^^^^^^^^^^ +LL | +LL | / async fn test_method() -> i32 { +LL | | 1 +LL | | } + | |_____- this attribute does nothing, the `Future`s returned by async functions are already `must_use` + +warning: 2 warnings emitted + From c15b55ace6ae247fa1ca0001d8840c974644a817 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 10 Nov 2021 19:25:54 +0100 Subject: [PATCH 03/14] Add Vec::retain_mut --- library/alloc/src/vec/mod.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index d52c78eedf3fa..4989244b50e27 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1444,6 +1444,34 @@ impl Vec { pub fn retain(&mut self, mut f: F) where F: FnMut(&T) -> bool, + { + self.retain_mut(|elem| f(elem)); + } + + /// Retains only the elements specified by the predicate, passing a mutable reference to it. + /// + /// In other words, remove all elements `e` such that `f(&mut e)` returns `false`. + /// This method operates in place, visiting each element exactly once in the + /// original order, and preserves the order of the retained elements. + /// + /// # Examples + /// + /// ``` + /// #![feature(vec_retain_mut)] + /// + /// let mut vec = vec![1, 2, 3, 4]; + /// vec.retain_mut(|x| if *x > 3 { + /// false + /// } else { + /// *x += 1; + /// true + /// }); + /// assert_eq!(vec, [2, 3, 4]); + /// ``` + #[unstable(feature = "vec_retain_mut", issue = "90829")] + pub fn retain_mut(&mut self, mut f: F) + where + F: FnMut(&mut T) -> bool, { let original_len = self.len(); // Avoid double drop if the drop guard is not executed, @@ -1496,7 +1524,7 @@ impl Vec { g: &mut BackshiftOnDrop<'_, T, A>, ) -> bool where - F: FnMut(&T) -> bool, + F: FnMut(&mut T) -> bool, { // SAFETY: Unchecked element must be valid. let cur = unsafe { &mut *g.v.as_mut_ptr().add(g.processed_len) }; From 130b9e9e3b058e76e43788fac67538c064fc8a97 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Sat, 13 Nov 2021 16:43:43 +1100 Subject: [PATCH 04/14] Improve diagnostics when a static lifetime is expected --- .../nice_region_error/named_anon_conflict.rs | 65 +++++++------------ ...ds-static-cant-capture-borrowed.nll.stderr | 13 +++- ...ure-bounds-static-cant-capture-borrowed.rs | 2 +- ...bounds-static-cant-capture-borrowed.stderr | 18 ++++- .../generator-region-requirements.nll.stderr | 11 ++++ .../generator-region-requirements.rs | 2 +- .../generator-region-requirements.stderr | 14 ++-- ...ojection-type-lifetime-mismatch.nll.stderr | 26 ++++++++ .../projection-type-lifetime-mismatch.rs | 6 +- .../projection-type-lifetime-mismatch.stderr | 26 +++++--- src/test/ui/issues/issue-46983.nll.stderr | 10 +++ src/test/ui/issues/issue-46983.rs | 2 +- src/test/ui/issues/issue-46983.stderr | 8 ++- ...expected-return-static-indirect.nll.stderr | 24 +++++++ ...e-90600-expected-return-static-indirect.rs | 14 ++++ ...600-expected-return-static-indirect.stderr | 14 ++++ ...region-lbr-anon-does-not-outlive-static.rs | 2 +- ...on-lbr-anon-does-not-outlive-static.stderr | 7 +- src/test/ui/nll/guarantor-issue-46974.rs | 2 +- src/test/ui/nll/guarantor-issue-46974.stderr | 10 +-- .../regions-static-bound.ll.nll.stderr | 28 -------- .../regions-static-bound.migrate.nll.stderr | 23 ------- .../regions-static-bound.migrate.stderr | 29 --------- .../regions/regions-static-bound.nll.stderr | 31 ++++++--- src/test/ui/regions/regions-static-bound.rs | 12 +--- .../ui/regions/regions-static-bound.stderr | 46 +++++++++++++ 26 files changed, 269 insertions(+), 176 deletions(-) create mode 100644 src/test/ui/generator/generator-region-requirements.nll.stderr create mode 100644 src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.nll.stderr create mode 100644 src/test/ui/issues/issue-46983.nll.stderr create mode 100644 src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.nll.stderr create mode 100644 src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs create mode 100644 src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr delete mode 100644 src/test/ui/regions/regions-static-bound.ll.nll.stderr delete mode 100644 src/test/ui/regions/regions-static-bound.migrate.nll.stderr delete mode 100644 src/test/ui/regions/regions-static-bound.migrate.stderr create mode 100644 src/test/ui/regions/regions-static-bound.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs index 0878f8550da35..eb1c80ecb018c 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs @@ -3,8 +3,6 @@ use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type; use crate::infer::error_reporting::nice_region_error::NiceRegionError; use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder}; -use rustc_hir::intravisit::Visitor; -use rustc_hir::FnRetTy; use rustc_middle::ty; impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { @@ -48,19 +46,24 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { return None; // inapplicable }; + // Suggesting to add a `'static` lifetime to a parameter is nearly always incorrect, + // and can steer users down the wrong path. + if *named == ty::ReStatic { + return None; + } + debug!("try_report_named_anon_conflict: named = {:?}", named); debug!("try_report_named_anon_conflict: anon_param_info = {:?}", anon_param_info); debug!("try_report_named_anon_conflict: region_info = {:?}", region_info); - let (param, new_ty, new_ty_span, br, is_first, scope_def_id, is_impl_item) = ( - anon_param_info.param, - anon_param_info.param_ty, - anon_param_info.param_ty_span, - anon_param_info.bound_region, - anon_param_info.is_first, - region_info.def_id, - region_info.is_impl_item, - ); + let param = anon_param_info.param; + let new_ty = anon_param_info.param_ty; + let new_ty_span = anon_param_info.param_ty_span; + let br = anon_param_info.bound_region; + let is_first = anon_param_info.is_first; + let scope_def_id = region_info.def_id; + let is_impl_item = region_info.is_impl_item; + match br { ty::BrAnon(_) => {} _ => { @@ -75,26 +78,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { return None; } - if let Some((_, fndecl)) = find_anon_type(self.tcx(), anon, &br) { - if self.is_self_anon(is_first, scope_def_id) { - return None; - } - - if let FnRetTy::Return(ty) = &fndecl.output { - let mut v = ty::TraitObjectVisitor(vec![], self.tcx().hir()); - v.visit_ty(ty); - - debug!("try_report_named_anon_conflict: ret ty {:?}", ty); - if sub == &ty::ReStatic - && v.0.into_iter().any(|t| t.span.desugaring_kind().is_none()) - { - // If the failure is due to a `'static` requirement coming from a `dyn` or - // `impl` Trait that *isn't* caused by `async fn` desugaring, handle this case - // better in `static_impl_trait`. - debug!("try_report_named_anon_conflict: impl Trait + 'static"); - return None; - } - } + if find_anon_type(self.tcx(), anon, &br).is_some() + && self.is_self_anon(is_first, scope_def_id) + { + return None; } let (error_var, span_label_var) = match param.pat.simple_ident() { @@ -114,16 +101,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { ); diag.span_label(span, format!("lifetime `{}` required", named)); - // Suggesting `'static` is nearly always incorrect, and can steer users - // down the wrong path. - if *named != ty::ReStatic { - diag.span_suggestion( - new_ty_span, - &format!("add explicit lifetime `{}` to {}", named, span_label_var), - new_ty.to_string(), - Applicability::Unspecified, - ); - } + diag.span_suggestion( + new_ty_span, + &format!("add explicit lifetime `{}` to {}", named, span_label_var), + new_ty.to_string(), + Applicability::Unspecified, + ); Some(diag) } diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr index e7d9664ec505e..af3810e91aeb0 100644 --- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr +++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr @@ -1,11 +1,18 @@ -error[E0621]: explicit lifetime required in the type of `x` +error[E0521]: borrowed data escapes outside of function --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5 | +LL | fn foo(x: &()) { + | - - let's call the lifetime of this reference `'1` + | | + | `x` is a reference that is only valid in the function body LL | / bar(|| { LL | | LL | | let _ = x; LL | | }) - | |______^ lifetime `'static` required + | | ^ + | | | + | |______`x` escapes the function body here + | argument requires that `'1` must outlive `'static` error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:9 @@ -31,5 +38,5 @@ LL | bar(move || { error: aborting due to 2 previous errors -Some errors have detailed explanations: E0373, E0621. +Some errors have detailed explanations: E0373, E0521. For more information about an error, try `rustc --explain E0373`. diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.rs b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.rs index 4fa5d54431c60..cbdc8b7deef38 100644 --- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.rs +++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.rs @@ -3,7 +3,7 @@ fn bar(blk: F) where F: FnOnce() + 'static { fn foo(x: &()) { bar(|| { - //~^ ERROR explicit lifetime required in the type of `x` [E0621] + //~^ ERROR `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] let _ = x; }) } diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr index a9add6184f13e..d761abdfc6a34 100644 --- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr +++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr @@ -1,9 +1,21 @@ -error[E0621]: explicit lifetime required in the type of `x` +error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:9 + | +LL | fn foo(x: &()) { + | --- this data with an anonymous lifetime `'_`... +LL | bar(|| { + | _________^ +LL | | +LL | | let _ = x; +LL | | }) + | |_____^ ...is captured here... + | +note: ...and is required to live as long as `'static` here --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5 | LL | bar(|| { - | ^^^ lifetime `'static` required + | ^^^ error: aborting due to previous error -For more information about this error, try `rustc --explain E0621`. +For more information about this error, try `rustc --explain E0759`. diff --git a/src/test/ui/generator/generator-region-requirements.nll.stderr b/src/test/ui/generator/generator-region-requirements.nll.stderr new file mode 100644 index 0000000000000..b4530cfda2b54 --- /dev/null +++ b/src/test/ui/generator/generator-region-requirements.nll.stderr @@ -0,0 +1,11 @@ +error: lifetime may not live long enough + --> $DIR/generator-region-requirements.rs:13:51 + | +LL | fn dangle(x: &mut i32) -> &'static mut i32 { + | - let's call the lifetime of this reference `'1` +... +LL | GeneratorState::Complete(c) => return c, + | ^ returning this value requires that `'1` must outlive `'static` + +error: aborting due to previous error + diff --git a/src/test/ui/generator/generator-region-requirements.rs b/src/test/ui/generator/generator-region-requirements.rs index 5f0a6bb09b784..cec68509a66e6 100644 --- a/src/test/ui/generator/generator-region-requirements.rs +++ b/src/test/ui/generator/generator-region-requirements.rs @@ -6,11 +6,11 @@ fn dangle(x: &mut i32) -> &'static mut i32 { let mut g = || { yield; x + //~^ ERROR `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] }; loop { match Pin::new(&mut g).resume(()) { GeneratorState::Complete(c) => return c, - //~^ ERROR explicit lifetime required GeneratorState::Yielded(_) => (), } } diff --git a/src/test/ui/generator/generator-region-requirements.stderr b/src/test/ui/generator/generator-region-requirements.stderr index de90a599e76b7..b6b9db22426eb 100644 --- a/src/test/ui/generator/generator-region-requirements.stderr +++ b/src/test/ui/generator/generator-region-requirements.stderr @@ -1,9 +1,15 @@ -error[E0621]: explicit lifetime required in the type of `x` - --> $DIR/generator-region-requirements.rs:12:51 +error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/generator-region-requirements.rs:8:9 | +LL | fn dangle(x: &mut i32) -> &'static mut i32 { + | -------- this data with an anonymous lifetime `'_`... +... +LL | x + | ^ ...is captured here... +... LL | GeneratorState::Complete(c) => return c, - | ^ lifetime `'static` required + | - ...and is required to live as long as `'static` here error: aborting due to previous error -For more information about this error, try `rustc --explain E0621`. +For more information about this error, try `rustc --explain E0759`. diff --git a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.nll.stderr b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.nll.stderr new file mode 100644 index 0000000000000..4620aa34e84d9 --- /dev/null +++ b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.nll.stderr @@ -0,0 +1,26 @@ +error: lifetime may not live long enough + --> $DIR/projection-type-lifetime-mismatch.rs:17:5 + | +LL | fn f(x: &impl for<'a> X = &'a ()>) -> &'static () { + | - let's call the lifetime of this reference `'1` +LL | x.m() + | ^^^^^ returning this value requires that `'1` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/projection-type-lifetime-mismatch.rs:22:5 + | +LL | fn g X = &'a ()>>(x: &T) -> &'static () { + | - let's call the lifetime of this reference `'1` +LL | x.m() + | ^^^^^ returning this value requires that `'1` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/projection-type-lifetime-mismatch.rs:27:5 + | +LL | fn h(x: &()) -> &'static () { + | - let's call the lifetime of this reference `'1` +LL | x.m() + | ^^^^^ returning this value requires that `'1` must outlive `'static` + +error: aborting due to 3 previous errors + diff --git a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.rs b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.rs index bcbcfc1899637..9b04fe233208e 100644 --- a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.rs +++ b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.rs @@ -15,17 +15,17 @@ impl X for () { fn f(x: &impl for<'a> X = &'a ()>) -> &'static () { x.m() - //~^ ERROR explicit lifetime required + //~^ ERROR `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] } fn g X = &'a ()>>(x: &T) -> &'static () { x.m() - //~^ ERROR explicit lifetime required + //~^ ERROR `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] } fn h(x: &()) -> &'static () { x.m() - //~^ ERROR explicit lifetime required + //~^ ERROR `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] } fn main() { diff --git a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr index 315bef16c5f13..1ffd205652f66 100644 --- a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr +++ b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr @@ -1,21 +1,27 @@ -error[E0621]: explicit lifetime required in the type of `x` - --> $DIR/projection-type-lifetime-mismatch.rs:17:5 +error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/projection-type-lifetime-mismatch.rs:17:7 | +LL | fn f(x: &impl for<'a> X = &'a ()>) -> &'static () { + | ------------------------------- this data with an anonymous lifetime `'_`... LL | x.m() - | ^^^^^ lifetime `'static` required + | --^-- ...is captured and required to live as long as `'static` here -error[E0621]: explicit lifetime required in the type of `x` - --> $DIR/projection-type-lifetime-mismatch.rs:22:5 +error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/projection-type-lifetime-mismatch.rs:22:7 | +LL | fn g X = &'a ()>>(x: &T) -> &'static () { + | -- this data with an anonymous lifetime `'_`... LL | x.m() - | ^^^^^ lifetime `'static` required + | --^-- ...is captured and required to live as long as `'static` here -error[E0621]: explicit lifetime required in the type of `x` - --> $DIR/projection-type-lifetime-mismatch.rs:27:5 +error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/projection-type-lifetime-mismatch.rs:27:7 | +LL | fn h(x: &()) -> &'static () { + | --- this data with an anonymous lifetime `'_`... LL | x.m() - | ^^^^^ lifetime `'static` required + | --^-- ...is captured and required to live as long as `'static` here error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0621`. +For more information about this error, try `rustc --explain E0759`. diff --git a/src/test/ui/issues/issue-46983.nll.stderr b/src/test/ui/issues/issue-46983.nll.stderr new file mode 100644 index 0000000000000..38a219bbd7b52 --- /dev/null +++ b/src/test/ui/issues/issue-46983.nll.stderr @@ -0,0 +1,10 @@ +error: lifetime may not live long enough + --> $DIR/issue-46983.rs:2:5 + | +LL | fn foo(x: &u32) -> &'static u32 { + | - let's call the lifetime of this reference `'1` +LL | &*x + | ^^^ returning this value requires that `'1` must outlive `'static` + +error: aborting due to previous error + diff --git a/src/test/ui/issues/issue-46983.rs b/src/test/ui/issues/issue-46983.rs index c1fd7729bdefb..87ed892894479 100644 --- a/src/test/ui/issues/issue-46983.rs +++ b/src/test/ui/issues/issue-46983.rs @@ -1,6 +1,6 @@ fn foo(x: &u32) -> &'static u32 { &*x - //~^ ERROR explicit lifetime required in the type of `x` [E0621] + //~^ ERROR `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] } fn main() {} diff --git a/src/test/ui/issues/issue-46983.stderr b/src/test/ui/issues/issue-46983.stderr index d328329edad22..77fb130f5192e 100644 --- a/src/test/ui/issues/issue-46983.stderr +++ b/src/test/ui/issues/issue-46983.stderr @@ -1,9 +1,11 @@ -error[E0621]: explicit lifetime required in the type of `x` +error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement --> $DIR/issue-46983.rs:2:5 | +LL | fn foo(x: &u32) -> &'static u32 { + | ---- this data with an anonymous lifetime `'_`... LL | &*x - | ^^^ lifetime `'static` required + | ^^^ ...is captured and required to live as long as `'static` here error: aborting due to previous error -For more information about this error, try `rustc --explain E0621`. +For more information about this error, try `rustc --explain E0759`. diff --git a/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.nll.stderr b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.nll.stderr new file mode 100644 index 0000000000000..99e1e7217b45c --- /dev/null +++ b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.nll.stderr @@ -0,0 +1,24 @@ +error[E0597]: `foo` does not live long enough + --> $DIR/issue-90600-expected-return-static-indirect.rs:7:32 + | +LL | let refcell = RefCell::new(&mut foo); + | ^^^^^^^^ borrowed value does not live long enough +LL | +LL | let read = &refcell as &RefCell; + | -------- cast requires that `foo` is borrowed for `'static` +... +LL | } + | - `foo` dropped here while still borrowed + +error: lifetime may not live long enough + --> $DIR/issue-90600-expected-return-static-indirect.rs:9:16 + | +LL | fn inner(mut foo: &[u8]) { + | - let's call the lifetime of this reference `'1` +... +LL | let read = &refcell as &RefCell; + | ^^^^^^^^ cast requires that `'1` must outlive `'static` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0597`. diff --git a/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs new file mode 100644 index 0000000000000..39996bbf43b3c --- /dev/null +++ b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs @@ -0,0 +1,14 @@ +use std::cell::RefCell; +use std::io::Read; + +fn main() {} + +fn inner(mut foo: &[u8]) { + let refcell = RefCell::new(&mut foo); + //~^ ERROR `foo` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] + let read = &refcell as &RefCell; + + read_thing(read); +} + +fn read_thing(refcell: &RefCell) {} diff --git a/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr new file mode 100644 index 0000000000000..3f65d3af725cf --- /dev/null +++ b/src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr @@ -0,0 +1,14 @@ +error[E0759]: `foo` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/issue-90600-expected-return-static-indirect.rs:7:32 + | +LL | fn inner(mut foo: &[u8]) { + | ----- this data with an anonymous lifetime `'_`... +LL | let refcell = RefCell::new(&mut foo); + | ^^^^^^^^ ...is captured here... +... +LL | read_thing(read); + | ---- ...and is required to live as long as `'static` here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0759`. diff --git a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.rs b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.rs index ac2943fc3a59b..c8f226f5238e9 100644 --- a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.rs +++ b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.rs @@ -7,7 +7,7 @@ fn foo(x: &u32) -> &'static u32 { &*x - //~^ ERROR explicit lifetime required in the type of `x` + //~^ ERROR lifetime may not live long enough } fn main() { } diff --git a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr index 4c302d935db96..7034492cee09d 100644 --- a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr +++ b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr @@ -1,9 +1,10 @@ -error[E0621]: explicit lifetime required in the type of `x` +error: lifetime may not live long enough --> $DIR/region-lbr-anon-does-not-outlive-static.rs:9:5 | +LL | fn foo(x: &u32) -> &'static u32 { + | - let's call the lifetime of this reference `'1` LL | &*x - | ^^^ lifetime `ReStatic` required + | ^^^ returning this value requires that `'1` must outlive `'static` error: aborting due to previous error -For more information about this error, try `rustc --explain E0621`. diff --git a/src/test/ui/nll/guarantor-issue-46974.rs b/src/test/ui/nll/guarantor-issue-46974.rs index d0af468cff6cd..87ed0e642e998 100644 --- a/src/test/ui/nll/guarantor-issue-46974.rs +++ b/src/test/ui/nll/guarantor-issue-46974.rs @@ -12,7 +12,7 @@ fn foo(s: &mut (i32,)) -> i32 { fn bar(s: &Box<(i32,)>) -> &'static i32 { // FIXME(#46983): error message should be better - &s.0 //~ ERROR explicit lifetime required in the type of `s` [E0621] + &s.0 //~ ERROR lifetime may not live long enough } fn main() { diff --git a/src/test/ui/nll/guarantor-issue-46974.stderr b/src/test/ui/nll/guarantor-issue-46974.stderr index eabc3105c0293..8245aadf82684 100644 --- a/src/test/ui/nll/guarantor-issue-46974.stderr +++ b/src/test/ui/nll/guarantor-issue-46974.stderr @@ -9,13 +9,15 @@ LL | *s = (2,); LL | *x | -- borrow later used here -error[E0621]: explicit lifetime required in the type of `s` +error: lifetime may not live long enough --> $DIR/guarantor-issue-46974.rs:15:5 | +LL | fn bar(s: &Box<(i32,)>) -> &'static i32 { + | - let's call the lifetime of this reference `'1` +LL | // FIXME(#46983): error message should be better LL | &s.0 - | ^^^^ lifetime `'static` required + | ^^^^ returning this value requires that `'1` must outlive `'static` error: aborting due to 2 previous errors -Some errors have detailed explanations: E0506, E0621. -For more information about an error, try `rustc --explain E0506`. +For more information about this error, try `rustc --explain E0506`. diff --git a/src/test/ui/regions/regions-static-bound.ll.nll.stderr b/src/test/ui/regions/regions-static-bound.ll.nll.stderr deleted file mode 100644 index d6cec03e0ff2e..0000000000000 --- a/src/test/ui/regions/regions-static-bound.ll.nll.stderr +++ /dev/null @@ -1,28 +0,0 @@ -error: lifetime may not live long enough - --> $DIR/regions-static-bound.rs:9:5 - | -LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { - | -- lifetime `'a` defined here -LL | t //[ll]~ ERROR E0312 - | ^ returning this value requires that `'a` must outlive `'static` - -error[E0621]: explicit lifetime required in the type of `u` - --> $DIR/regions-static-bound.rs:14:5 - | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()` -LL | static_id(&u); //[ll]~ ERROR explicit lifetime required in the type of `u` [E0621] - | ^^^^^^^^^^^^^ lifetime `'static` required - -error[E0621]: explicit lifetime required in the type of `v` - --> $DIR/regions-static-bound.rs:16:5 - | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()` -... -LL | static_id_indirect(&v); //[ll]~ ERROR explicit lifetime required in the type of `v` [E0621] - | ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0621`. diff --git a/src/test/ui/regions/regions-static-bound.migrate.nll.stderr b/src/test/ui/regions/regions-static-bound.migrate.nll.stderr deleted file mode 100644 index a280c6f0a02d2..0000000000000 --- a/src/test/ui/regions/regions-static-bound.migrate.nll.stderr +++ /dev/null @@ -1,23 +0,0 @@ -error: lifetime may not live long enough - --> $DIR/regions-static-bound.rs:9:5 - | -LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { - | -- lifetime `'a` defined here -LL | t - | ^ returning this value requires that `'a` must outlive `'static` - -error[E0621]: explicit lifetime required in the type of `u` - --> $DIR/regions-static-bound.rs:14:5 - | -LL | static_id(&u); - | ^^^^^^^^^^^^^ lifetime `'static` required - -error[E0621]: explicit lifetime required in the type of `v` - --> $DIR/regions-static-bound.rs:16:5 - | -LL | static_id_indirect(&v); - | ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0621`. diff --git a/src/test/ui/regions/regions-static-bound.migrate.stderr b/src/test/ui/regions/regions-static-bound.migrate.stderr deleted file mode 100644 index 8f11e148220d6..0000000000000 --- a/src/test/ui/regions/regions-static-bound.migrate.stderr +++ /dev/null @@ -1,29 +0,0 @@ -error[E0312]: lifetime of reference outlives lifetime of borrowed content... - --> $DIR/regions-static-bound.rs:9:5 - | -LL | t - | ^ - | - = note: ...the reference is valid for the static lifetime... -note: ...but the borrowed content is only valid for the lifetime `'a` as defined here - --> $DIR/regions-static-bound.rs:8:24 - | -LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { - | ^^ - -error[E0621]: explicit lifetime required in the type of `u` - --> $DIR/regions-static-bound.rs:14:5 - | -LL | static_id(&u); - | ^^^^^^^^^ lifetime `'static` required - -error[E0621]: explicit lifetime required in the type of `v` - --> $DIR/regions-static-bound.rs:16:5 - | -LL | static_id_indirect(&v); - | ^^^^^^^^^^^^^^^^^^ lifetime `'static` required - -error: aborting due to 3 previous errors - -Some errors have detailed explanations: E0312, E0621. -For more information about an error, try `rustc --explain E0312`. diff --git a/src/test/ui/regions/regions-static-bound.nll.stderr b/src/test/ui/regions/regions-static-bound.nll.stderr index a280c6f0a02d2..699638c7ef9b2 100644 --- a/src/test/ui/regions/regions-static-bound.nll.stderr +++ b/src/test/ui/regions/regions-static-bound.nll.stderr @@ -1,23 +1,38 @@ error: lifetime may not live long enough - --> $DIR/regions-static-bound.rs:9:5 + --> $DIR/regions-static-bound.rs:6:5 | LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { | -- lifetime `'a` defined here LL | t | ^ returning this value requires that `'a` must outlive `'static` -error[E0621]: explicit lifetime required in the type of `u` - --> $DIR/regions-static-bound.rs:14:5 +error[E0521]: borrowed data escapes outside of function + --> $DIR/regions-static-bound.rs:10:5 | +LL | fn error(u: &(), v: &()) { + | - - let's call the lifetime of this reference `'1` + | | + | `u` is a reference that is only valid in the function body LL | static_id(&u); - | ^^^^^^^^^^^^^ lifetime `'static` required + | ^^^^^^^^^^^^^ + | | + | `u` escapes the function body here + | argument requires that `'1` must outlive `'static` -error[E0621]: explicit lifetime required in the type of `v` - --> $DIR/regions-static-bound.rs:16:5 +error[E0521]: borrowed data escapes outside of function + --> $DIR/regions-static-bound.rs:11:5 | +LL | fn error(u: &(), v: &()) { + | - - let's call the lifetime of this reference `'2` + | | + | `v` is a reference that is only valid in the function body +LL | static_id(&u); LL | static_id_indirect(&v); - | ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required + | ^^^^^^^^^^^^^^^^^^^^^^ + | | + | `v` escapes the function body here + | argument requires that `'2` must outlive `'static` error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0621`. +For more information about this error, try `rustc --explain E0521`. diff --git a/src/test/ui/regions/regions-static-bound.rs b/src/test/ui/regions/regions-static-bound.rs index 1db54881d3e03..a977a8b36d056 100644 --- a/src/test/ui/regions/regions-static-bound.rs +++ b/src/test/ui/regions/regions-static-bound.rs @@ -1,20 +1,14 @@ -// revisions: migrate nll -//[nll] compile-flags:-Zborrowck=mir - fn static_id<'a,'b>(t: &'a ()) -> &'static () where 'a: 'static { t } fn static_id_indirect<'a,'b>(t: &'a ()) -> &'static () where 'a: 'b, 'b: 'static { t } fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { - t //[migrate]~ ERROR E0312 - //[nll]~^ ERROR lifetime may not live long enough + t //~ ERROR E0312 } fn error(u: &(), v: &()) { - static_id(&u); //[migrate]~ ERROR explicit lifetime required in the type of `u` [E0621] - //[nll]~^ ERROR explicit lifetime required in the type of `u` [E0621] - static_id_indirect(&v); //[migrate]~ ERROR explicit lifetime required in the type of `v` [E0621] - //[nll]~^ ERROR explicit lifetime required in the type of `v` [E0621] + static_id(&u); //~ ERROR `u` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] + static_id_indirect(&v); //~ ERROR `v` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] } fn main() {} diff --git a/src/test/ui/regions/regions-static-bound.stderr b/src/test/ui/regions/regions-static-bound.stderr new file mode 100644 index 0000000000000..51fe16ca9da03 --- /dev/null +++ b/src/test/ui/regions/regions-static-bound.stderr @@ -0,0 +1,46 @@ +error[E0312]: lifetime of reference outlives lifetime of borrowed content... + --> $DIR/regions-static-bound.rs:6:5 + | +LL | t + | ^ + | + = note: ...the reference is valid for the static lifetime... +note: ...but the borrowed content is only valid for the lifetime `'a` as defined here + --> $DIR/regions-static-bound.rs:5:24 + | +LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { + | ^^ + +error[E0759]: `u` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/regions-static-bound.rs:10:5 + | +LL | fn error(u: &(), v: &()) { + | --- this data with an anonymous lifetime `'_`... +LL | static_id(&u); + | ^^^^^^^^^ -- ...is captured here... + | +note: ...and is required to live as long as `'static` here + --> $DIR/regions-static-bound.rs:10:5 + | +LL | static_id(&u); + | ^^^^^^^^^ + +error[E0759]: `v` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement + --> $DIR/regions-static-bound.rs:11:5 + | +LL | fn error(u: &(), v: &()) { + | --- this data with an anonymous lifetime `'_`... +LL | static_id(&u); +LL | static_id_indirect(&v); + | ^^^^^^^^^^^^^^^^^^ -- ...is captured here... + | +note: ...and is required to live as long as `'static` here + --> $DIR/regions-static-bound.rs:11:5 + | +LL | static_id_indirect(&v); + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0312, E0759. +For more information about an error, try `rustc --explain E0312`. From 24acf8602969215469ac26a2c0042385096345d9 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 13 Nov 2021 23:56:22 +0100 Subject: [PATCH 05/14] Fix span for non-satisfied trivial trait bounds The spans for "trait bound not satisfied" errors in trivial trait bounds referenced the entire item (fn, impl, struct) before. Now they only reference the obligation itself (`String: Copy`) Address #90869 --- compiler/rustc_infer/src/traits/util.rs | 17 +++++ .../rustc_trait_selection/src/traits/mod.rs | 3 +- compiler/rustc_typeck/src/check/wfcheck.rs | 25 +++++- .../ui/const-generics/issues/issue-67185-2.rs | 9 +-- .../issues/issue-67185-2.stderr | 60 ++++++--------- src/test/ui/cross/cross-fn-cache-hole.rs | 4 +- src/test/ui/cross/cross-fn-cache-hole.stderr | 12 +-- .../feature-gate-trivial_bounds.stderr | 76 +++++++------------ 8 files changed, 102 insertions(+), 104 deletions(-) diff --git a/compiler/rustc_infer/src/traits/util.rs b/compiler/rustc_infer/src/traits/util.rs index c839f824d1c9c..92f74af4eb3eb 100644 --- a/compiler/rustc_infer/src/traits/util.rs +++ b/compiler/rustc_infer/src/traits/util.rs @@ -5,6 +5,7 @@ use crate::traits::{Obligation, ObligationCause, PredicateObligation}; use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; use rustc_middle::ty::{self, ToPredicate, TyCtxt, WithConstness}; use rustc_span::symbol::Ident; +use rustc_span::Span; pub fn anonymize_predicate<'tcx>( tcx: TyCtxt<'tcx>, @@ -97,6 +98,22 @@ pub fn elaborate_predicates<'tcx>( elaborate_obligations(tcx, obligations) } +pub fn elaborate_predicates_with_span<'tcx>( + tcx: TyCtxt<'tcx>, + predicates: impl Iterator, Span)>, +) -> Elaborator<'tcx> { + let obligations = predicates + .map(|(predicate, span)| { + predicate_obligation( + predicate, + ty::ParamEnv::empty(), + ObligationCause::dummy_with_span(span), + ) + }) + .collect(); + elaborate_obligations(tcx, obligations) +} + pub fn elaborate_obligations<'tcx>( tcx: TyCtxt<'tcx>, mut obligations: Vec>, diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index d4a586b0124a2..91671994c5ace 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -65,7 +65,8 @@ pub use self::specialize::{specialization_graph, translate_substs, OverlapError} pub use self::structural_match::search_for_structural_match_violation; pub use self::structural_match::NonStructuralMatchTy; pub use self::util::{ - elaborate_obligations, elaborate_predicates, elaborate_trait_ref, elaborate_trait_refs, + elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span, + elaborate_trait_ref, elaborate_trait_refs, }; pub use self::util::{expand_trait_aliases, TraitAliasExpander}; pub use self::util::{ diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index c1adc2894ccfc..0050ac99cb19a 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -1641,19 +1641,38 @@ fn report_bivariance(tcx: TyCtxt<'_>, param: &rustc_hir::GenericParam<'_>) { /// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that /// aren't true. -fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, span: Span, id: hir::HirId) { +fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirId) { let empty_env = ty::ParamEnv::empty(); let def_id = fcx.tcx.hir().local_def_id(id); - let predicates = fcx.tcx.predicates_of(def_id).predicates.iter().map(|(p, _)| *p); + let predicates_with_span = + fcx.tcx.predicates_of(def_id).predicates.iter().map(|(p, span)| (*p, *span)); // Check elaborated bounds. - let implied_obligations = traits::elaborate_predicates(fcx.tcx, predicates); + let implied_obligations = traits::elaborate_predicates_with_span(fcx.tcx, predicates_with_span); for obligation in implied_obligations { let pred = obligation.predicate; // Match the existing behavior. if pred.is_global(fcx.tcx) && !pred.has_late_bound_regions() { let pred = fcx.normalize_associated_types_in(span, pred); + let hir_node = fcx.tcx.hir().find(id); + + // only use the span of the predicate clause (#90869) + + if let Some(hir::Generics { where_clause, .. }) = + hir_node.and_then(|node| node.generics()) + { + let obligation_span = obligation.cause.span(fcx.tcx); + + span = where_clause + .predicates + .iter() + // There seems to be no better way to find out which predicate we are in + .find(|pred| pred.span().contains(obligation_span)) + .map(|pred| pred.span()) + .unwrap_or(obligation_span); + } + let obligation = traits::Obligation::new( traits::ObligationCause::new(span, id, traits::TrivialBound), empty_env, diff --git a/src/test/ui/const-generics/issues/issue-67185-2.rs b/src/test/ui/const-generics/issues/issue-67185-2.rs index c1a04e2014749..18bb6f6bc1edc 100644 --- a/src/test/ui/const-generics/issues/issue-67185-2.rs +++ b/src/test/ui/const-generics/issues/issue-67185-2.rs @@ -9,11 +9,10 @@ trait Bar {} impl Bar for [u16; 4] {} impl Bar for [[u16; 3]; 3] {} -trait Foo //~ ERROR the trait bound `[u16; 3]: Bar` is not satisfied [E0277] - //~^ ERROR the trait bound `[[u16; 3]; 2]: Bar` is not satisfied [E0277] - where - [::Quaks; 2]: Bar, - ::Quaks: Bar, +trait Foo +where + [::Quaks; 2]: Bar, //~ ERROR the trait bound `[[u16; 3]; 2]: Bar` is not satisfied [E0277] + ::Quaks: Bar, //~ ERROR the trait bound `[u16; 3]: Bar` is not satisfied [E0277] { } diff --git a/src/test/ui/const-generics/issues/issue-67185-2.stderr b/src/test/ui/const-generics/issues/issue-67185-2.stderr index 7167bea94bbb6..89aa3d395e25b 100644 --- a/src/test/ui/const-generics/issues/issue-67185-2.stderr +++ b/src/test/ui/const-generics/issues/issue-67185-2.stderr @@ -1,14 +1,8 @@ error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied - --> $DIR/issue-67185-2.rs:12:1 + --> $DIR/issue-67185-2.rs:15:5 | -LL | / trait Foo -LL | | -LL | | where -LL | | [::Quaks; 2]: Bar, -LL | | ::Quaks: Bar, -LL | | { -LL | | } - | |_^ the trait `Bar` is not implemented for `[u16; 3]` +LL | ::Quaks: Bar, + | ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `[u16; 3]` | = help: the following implementations were found: <[[u16; 3]; 3] as Bar> @@ -17,16 +11,10 @@ LL | | } = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied - --> $DIR/issue-67185-2.rs:12:1 + --> $DIR/issue-67185-2.rs:14:5 | -LL | / trait Foo -LL | | -LL | | where -LL | | [::Quaks; 2]: Bar, -LL | | ::Quaks: Bar, -LL | | { -LL | | } - | |_^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` +LL | [::Quaks; 2]: Bar, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` | = help: the following implementations were found: <[[u16; 3]; 3] as Bar> @@ -35,7 +23,7 @@ LL | | } = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied - --> $DIR/issue-67185-2.rs:22:6 + --> $DIR/issue-67185-2.rs:21:6 | LL | impl Foo for FooImpl {} | ^^^ the trait `Bar` is not implemented for `[u16; 3]` @@ -44,16 +32,16 @@ LL | impl Foo for FooImpl {} <[[u16; 3]; 3] as Bar> <[u16; 4] as Bar> note: required by a bound in `Foo` - --> $DIR/issue-67185-2.rs:16:29 + --> $DIR/issue-67185-2.rs:15:25 | LL | trait Foo | --- required by a bound in this ... -LL | ::Quaks: Bar, - | ^^^ required by this bound in `Foo` +LL | ::Quaks: Bar, + | ^^^ required by this bound in `Foo` error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied - --> $DIR/issue-67185-2.rs:22:6 + --> $DIR/issue-67185-2.rs:21:6 | LL | impl Foo for FooImpl {} | ^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` @@ -62,16 +50,16 @@ LL | impl Foo for FooImpl {} <[[u16; 3]; 3] as Bar> <[u16; 4] as Bar> note: required by a bound in `Foo` - --> $DIR/issue-67185-2.rs:15:34 + --> $DIR/issue-67185-2.rs:14:30 | LL | trait Foo | --- required by a bound in this -... -LL | [::Quaks; 2]: Bar, - | ^^^ required by this bound in `Foo` +LL | where +LL | [::Quaks; 2]: Bar, + | ^^^ required by this bound in `Foo` error[E0277]: the trait bound `[[u16; 3]; 2]: Bar` is not satisfied - --> $DIR/issue-67185-2.rs:26:14 + --> $DIR/issue-67185-2.rs:25:14 | LL | fn f(_: impl Foo) {} | ^^^ the trait `Bar` is not implemented for `[[u16; 3]; 2]` @@ -80,16 +68,16 @@ LL | fn f(_: impl Foo) {} <[[u16; 3]; 3] as Bar> <[u16; 4] as Bar> note: required by a bound in `Foo` - --> $DIR/issue-67185-2.rs:15:34 + --> $DIR/issue-67185-2.rs:14:30 | LL | trait Foo | --- required by a bound in this -... -LL | [::Quaks; 2]: Bar, - | ^^^ required by this bound in `Foo` +LL | where +LL | [::Quaks; 2]: Bar, + | ^^^ required by this bound in `Foo` error[E0277]: the trait bound `[u16; 3]: Bar` is not satisfied - --> $DIR/issue-67185-2.rs:26:14 + --> $DIR/issue-67185-2.rs:25:14 | LL | fn f(_: impl Foo) {} | ^^^ the trait `Bar` is not implemented for `[u16; 3]` @@ -98,13 +86,13 @@ LL | fn f(_: impl Foo) {} <[[u16; 3]; 3] as Bar> <[u16; 4] as Bar> note: required by a bound in `Foo` - --> $DIR/issue-67185-2.rs:16:29 + --> $DIR/issue-67185-2.rs:15:25 | LL | trait Foo | --- required by a bound in this ... -LL | ::Quaks: Bar, - | ^^^ required by this bound in `Foo` +LL | ::Quaks: Bar, + | ^^^ required by this bound in `Foo` error: aborting due to 6 previous errors diff --git a/src/test/ui/cross/cross-fn-cache-hole.rs b/src/test/ui/cross/cross-fn-cache-hole.rs index 249c6474c9420..c38a5001ac86d 100644 --- a/src/test/ui/cross/cross-fn-cache-hole.rs +++ b/src/test/ui/cross/cross-fn-cache-hole.rs @@ -12,8 +12,8 @@ trait Bar { } // We don't always check where clauses for sanity, but in this case // wfcheck does report an error here: -fn vacuous() //~ ERROR the trait bound `i32: Bar` is not satisfied - where i32: Foo +fn vacuous() + where i32: Foo //~ ERROR the trait bound `i32: Bar` is not satisfied { // ... the original intention was to check that we don't use that // vacuous where clause (which could never be satisfied) to accept diff --git a/src/test/ui/cross/cross-fn-cache-hole.stderr b/src/test/ui/cross/cross-fn-cache-hole.stderr index 0d325bb7ec102..7e15562b0816b 100644 --- a/src/test/ui/cross/cross-fn-cache-hole.stderr +++ b/src/test/ui/cross/cross-fn-cache-hole.stderr @@ -1,14 +1,8 @@ error[E0277]: the trait bound `i32: Bar` is not satisfied - --> $DIR/cross-fn-cache-hole.rs:15:1 + --> $DIR/cross-fn-cache-hole.rs:16:11 | -LL | / fn vacuous() -LL | | where i32: Foo -LL | | { -LL | | // ... the original intention was to check that we don't use that -... | -LL | | require::(); -LL | | } - | |_^ the trait `Bar` is not implemented for `i32` +LL | where i32: Foo + | ^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `i32` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable diff --git a/src/test/ui/feature-gates/feature-gate-trivial_bounds.stderr b/src/test/ui/feature-gates/feature-gate-trivial_bounds.stderr index 78904b383f49a..28c49c33bf6b5 100644 --- a/src/test/ui/feature-gates/feature-gate-trivial_bounds.stderr +++ b/src/test/ui/feature-gates/feature-gate-trivial_bounds.stderr @@ -1,87 +1,71 @@ error[E0277]: the trait bound `i32: Foo` is not satisfied - --> $DIR/feature-gate-trivial_bounds.rs:10:1 + --> $DIR/feature-gate-trivial_bounds.rs:10:14 | LL | enum E where i32: Foo { V } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `i32` + | ^^^^^^^^ the trait `Foo` is not implemented for `i32` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `i32: Foo` is not satisfied - --> $DIR/feature-gate-trivial_bounds.rs:12:1 + --> $DIR/feature-gate-trivial_bounds.rs:12:16 | LL | struct S where i32: Foo; - | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `i32` + | ^^^^^^^^ the trait `Foo` is not implemented for `i32` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `i32: Foo` is not satisfied - --> $DIR/feature-gate-trivial_bounds.rs:14:1 + --> $DIR/feature-gate-trivial_bounds.rs:14:15 | LL | trait T where i32: Foo {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `i32` + | ^^^^^^^^ the trait `Foo` is not implemented for `i32` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `i32: Foo` is not satisfied - --> $DIR/feature-gate-trivial_bounds.rs:16:1 + --> $DIR/feature-gate-trivial_bounds.rs:16:15 | LL | union U where i32: Foo { f: i32 } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `i32` + | ^^^^^^^^ the trait `Foo` is not implemented for `i32` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `i32: Foo` is not satisfied - --> $DIR/feature-gate-trivial_bounds.rs:20:1 + --> $DIR/feature-gate-trivial_bounds.rs:20:23 | -LL | / impl Foo for () where i32: Foo { -LL | | fn test(&self) { -LL | | 3i32.test(); -LL | | Foo::test(&4i32); -LL | | generic_function(5i32); -LL | | } -LL | | } - | |_^ the trait `Foo` is not implemented for `i32` +LL | impl Foo for () where i32: Foo { + | ^^^^^^^^ the trait `Foo` is not implemented for `i32` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `i32: Foo` is not satisfied - --> $DIR/feature-gate-trivial_bounds.rs:28:1 + --> $DIR/feature-gate-trivial_bounds.rs:28:14 | -LL | / fn f() where i32: Foo -LL | | { -LL | | let s = S; -LL | | 3i32.test(); -LL | | Foo::test(&4i32); -LL | | generic_function(5i32); -LL | | } - | |_^ the trait `Foo` is not implemented for `i32` +LL | fn f() where i32: Foo + | ^^^^^^^^ the trait `Foo` is not implemented for `i32` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the trait bound `String: Neg` is not satisfied - --> $DIR/feature-gate-trivial_bounds.rs:36:1 + --> $DIR/feature-gate-trivial_bounds.rs:36:38 | -LL | / fn use_op(s: String) -> String where String: ::std::ops::Neg { -LL | | -s -LL | | } - | |_^ the trait `Neg` is not implemented for `String` +LL | fn use_op(s: String) -> String where String: ::std::ops::Neg { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Neg` is not implemented for `String` | = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: `i32` is not an iterator - --> $DIR/feature-gate-trivial_bounds.rs:40:1 + --> $DIR/feature-gate-trivial_bounds.rs:40:20 | -LL | / fn use_for() where i32: Iterator { -LL | | for _ in 2i32 {} -LL | | } - | |_^ `i32` is not an iterator +LL | fn use_for() where i32: Iterator { + | ^^^^^^^^^^^^^ `i32` is not an iterator | = help: the trait `Iterator` is not implemented for `i32` = note: if you want to iterate between `start` until a value `end`, use the exclusive range syntax `start..end` or the inclusive range syntax `start..=end` @@ -89,22 +73,20 @@ LL | | } = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the size for values of type `str` cannot be known at compilation time - --> $DIR/feature-gate-trivial_bounds.rs:52:1 + --> $DIR/feature-gate-trivial_bounds.rs:52:32 | LL | struct TwoStrs(str, str) where str: Sized; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | ^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `str` = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time - --> $DIR/feature-gate-trivial_bounds.rs:55:1 + --> $DIR/feature-gate-trivial_bounds.rs:55:26 | -LL | / fn unsized_local() where Dst: Sized { -LL | | let x: Dst = *(Box::new(Dst { x: 1 }) as Box>); -LL | | } - | |_^ doesn't have a size known at compile-time +LL | fn unsized_local() where Dst: Sized { + | ^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: within `Dst<(dyn A + 'static)>`, the trait `Sized` is not implemented for `(dyn A + 'static)` note: required because it appears within the type `Dst<(dyn A + 'static)>` @@ -116,12 +98,10 @@ LL | struct Dst { = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable error[E0277]: the size for values of type `str` cannot be known at compilation time - --> $DIR/feature-gate-trivial_bounds.rs:59:1 + --> $DIR/feature-gate-trivial_bounds.rs:59:30 | -LL | / fn return_str() -> str where str: Sized { -LL | | *"Sized".to_string().into_boxed_str() -LL | | } - | |_^ doesn't have a size known at compile-time +LL | fn return_str() -> str where str: Sized { + | ^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `str` = help: see issue #48214 From f980f813e189b8568eab93311fcc4effa2cffec3 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Sun, 14 Nov 2021 22:04:25 +1100 Subject: [PATCH 06/14] Improve ManuallyDrop suggestion --- compiler/rustc_typeck/src/check/check.rs | 15 ++++++++++--- .../feature-gate-untagged_unions.stderr | 14 ++++++------- src/test/ui/union/issue-41073.stderr | 7 +++---- src/test/ui/union/union-custom-drop.stderr | 7 +++---- .../union-with-drop-fields.mirunsafeck.stderr | 21 ++++++++----------- ...union-with-drop-fields.thirunsafeck.stderr | 21 ++++++++----------- 6 files changed, 42 insertions(+), 43 deletions(-) diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index f0a77cb39a622..335814f8627b3 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -371,8 +371,13 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b let param_env = tcx.param_env(item_def_id); for field in fields { let field_ty = field.ty(tcx, substs); - // We are currently checking the type this field came from, so it must be local. - let field_span = tcx.hir().span_if_local(field.did).unwrap(); + let (field_span, ty_span) = + // We are currently checking the type this field came from, so it must be local. + if let Node::Field(field) = tcx.hir().get_if_local(field.did).unwrap() { + (field.span, field.ty.span) + } else { + unreachable!("mir field has to correspond to hir field"); + }; if field_ty.needs_drop(tcx, param_env) { struct_span_err!( tcx.sess, @@ -380,7 +385,11 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b E0740, "unions may not contain fields that need dropping" ) - .span_note(field_span, "`std::mem::ManuallyDrop` can be used to wrap the type") + .multipart_suggestion_verbose( + "wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped", + vec![(ty_span, format!("std::mem::ManuallyDrop<{}>", field_ty))], + Applicability::MaybeIncorrect, + ) .emit(); return false; } diff --git a/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr b/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr index ed973871b3f06..a9ccb83558773 100644 --- a/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr +++ b/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr @@ -13,11 +13,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: String, | ^^^^^^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/feature-gate-untagged_unions.rs:16:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: String, - | ^^^^^^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0740]: unions may not contain fields that need dropping --> $DIR/feature-gate-untagged_unions.rs:24:5 @@ -25,11 +24,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: T, | ^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/feature-gate-untagged_unions.rs:24:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: T, - | ^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 3 previous errors diff --git a/src/test/ui/union/issue-41073.stderr b/src/test/ui/union/issue-41073.stderr index 2e9598b227124..04b4286cc4b63 100644 --- a/src/test/ui/union/issue-41073.stderr +++ b/src/test/ui/union/issue-41073.stderr @@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: A, | ^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/issue-41073.rs:4:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: A, - | ^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/union/union-custom-drop.stderr b/src/test/ui/union/union-custom-drop.stderr index ee2333f905fb4..0c59048f7fedb 100644 --- a/src/test/ui/union/union-custom-drop.stderr +++ b/src/test/ui/union/union-custom-drop.stderr @@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping LL | bar: Bar, | ^^^^^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/union-custom-drop.rs:7:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | bar: Bar, - | ^^^^^^^^ +LL | bar: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr b/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr index 2062fb7473faa..8f3d30cd7ee46 100644 --- a/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr +++ b/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr @@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: String, | ^^^^^^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/union-with-drop-fields.rs:11:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: String, - | ^^^^^^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:19:5 @@ -16,11 +15,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: S, | ^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/union-with-drop-fields.rs:19:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: S, - | ^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:24:5 @@ -28,11 +26,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: T, | ^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/union-with-drop-fields.rs:24:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: T, - | ^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 3 previous errors diff --git a/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr b/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr index 2062fb7473faa..8f3d30cd7ee46 100644 --- a/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr +++ b/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr @@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: String, | ^^^^^^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/union-with-drop-fields.rs:11:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: String, - | ^^^^^^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:19:5 @@ -16,11 +15,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: S, | ^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/union-with-drop-fields.rs:19:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: S, - | ^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:24:5 @@ -28,11 +26,10 @@ error[E0740]: unions may not contain fields that need dropping LL | a: T, | ^^^^ | -note: `std::mem::ManuallyDrop` can be used to wrap the type - --> $DIR/union-with-drop-fields.rs:24:5 +help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | -LL | a: T, - | ^^^^ +LL | a: std::mem::ManuallyDrop, + | ~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to 3 previous errors From 50ec47aa06e96a195707fb5ee92fcd32299ca272 Mon Sep 17 00:00:00 2001 From: Andreas Jonson Date: Sun, 14 Nov 2021 14:01:30 +0100 Subject: [PATCH 07/14] Remove workaround for the forward progress handling in LLVM --- compiler/rustc_codegen_gcc/src/intrinsic/mod.rs | 6 +----- compiler/rustc_codegen_llvm/src/context.rs | 1 - compiler/rustc_codegen_llvm/src/intrinsic.rs | 9 --------- compiler/rustc_codegen_ssa/src/mir/block.rs | 11 ----------- compiler/rustc_codegen_ssa/src/traits/intrinsic.rs | 4 ---- 5 files changed, 1 insertion(+), 30 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/intrinsic/mod.rs b/compiler/rustc_codegen_gcc/src/intrinsic/mod.rs index 64bd586662d38..f3a2382ef32d9 100644 --- a/compiler/rustc_codegen_gcc/src/intrinsic/mod.rs +++ b/compiler/rustc_codegen_gcc/src/intrinsic/mod.rs @@ -316,7 +316,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> { extended_asm.add_input_operand(None, "r", result.llval); extended_asm.add_clobber("memory"); extended_asm.set_volatile_flag(true); - + // We have copied the value to `result` already. return; } @@ -363,10 +363,6 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> { cond } - fn sideeffect(&mut self) { - // TODO(antoyo) - } - fn type_test(&mut self, _pointer: Self::Value, _typeid: Self::Value) -> Self::Value { // Unsupported. self.context.new_rvalue_from_int(self.int_type, 0) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 1dba264a9614a..613a8df891ce4 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -597,7 +597,6 @@ impl CodegenCx<'b, 'tcx> { ifn!("llvm.trap", fn() -> void); ifn!("llvm.debugtrap", fn() -> void); ifn!("llvm.frameaddress", fn(t_i32) -> i8p); - ifn!("llvm.sideeffect", fn() -> void); ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32); ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64); diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 924bb803b368f..a7e34b080594b 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -392,15 +392,6 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> { self.call_intrinsic("llvm.expect.i1", &[cond, self.const_bool(expected)]) } - fn sideeffect(&mut self) { - // This kind of check would make a ton of sense in the caller, but currently the only - // caller of this function is in `rustc_codegen_ssa`, which is agnostic to whether LLVM - // codegen backend being used, and so is unable to check the LLVM version. - if unsafe { llvm::LLVMRustVersionMajor() } < 12 { - self.call_intrinsic("llvm.sideeffect", &[]); - } - } - fn type_test(&mut self, pointer: Self::Value, typeid: Self::Value) -> Self::Value { // Test the called operand using llvm.type.test intrinsic. The LowerTypeTests link-time // optimization pass replaces calls to this intrinsic with code to test type membership. diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index a9471f7b77160..c8f388bfa1d5a 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -980,17 +980,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } mir::TerminatorKind::Goto { target } => { - if bb == target { - // This is an unconditional branch back to this same basic block. That means we - // have something like a `loop {}` statement. LLVM versions before 12.0 - // miscompile this because they assume forward progress. For older versions - // try to handle just this specific case which comes up commonly in practice - // (e.g., in embedded code). - // - // NB: the `sideeffect` currently checks for the LLVM version used internally. - bx.sideeffect(); - } - helper.funclet_br(self, &mut bx, target); } diff --git a/compiler/rustc_codegen_ssa/src/traits/intrinsic.rs b/compiler/rustc_codegen_ssa/src/traits/intrinsic.rs index 78bf22ef9f2e2..02be6cd360c72 100644 --- a/compiler/rustc_codegen_ssa/src/traits/intrinsic.rs +++ b/compiler/rustc_codegen_ssa/src/traits/intrinsic.rs @@ -20,10 +20,6 @@ pub trait IntrinsicCallMethods<'tcx>: BackendTypes { fn abort(&mut self); fn assume(&mut self, val: Self::Value); fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value; - /// Emits a forced side effect. - /// - /// Currently has any effect only when LLVM versions prior to 12.0 are used as the backend. - fn sideeffect(&mut self); /// Trait method used to test whether a given pointer is associated with a type identifier. fn type_test(&mut self, pointer: Self::Value, typeid: Self::Value) -> Self::Value; /// Trait method used to inject `va_start` on the "spoofed" `VaListImpl` in From 62acf7f96def600de3239cb93b62d07e9b514276 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Mon, 15 Nov 2021 14:47:36 +1100 Subject: [PATCH 08/14] feedback --- compiler/rustc_typeck/src/check/check.rs | 17 +++++++++-------- .../feature-gate-untagged_unions.stderr | 4 ++-- src/test/ui/union/issue-41073.stderr | 2 +- src/test/ui/union/union-custom-drop.stderr | 2 +- .../union-with-drop-fields.mirunsafeck.stderr | 6 +++--- .../union-with-drop-fields.thirunsafeck.stderr | 6 +++--- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index 335814f8627b3..bb1d9744e66fe 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -371,14 +371,12 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b let param_env = tcx.param_env(item_def_id); for field in fields { let field_ty = field.ty(tcx, substs); - let (field_span, ty_span) = - // We are currently checking the type this field came from, so it must be local. - if let Node::Field(field) = tcx.hir().get_if_local(field.did).unwrap() { - (field.span, field.ty.span) - } else { - unreachable!("mir field has to correspond to hir field"); - }; if field_ty.needs_drop(tcx, param_env) { + let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) { + // We are currently checking the type this field came from, so it must be local. + Some(Node::Field(field)) => (field.span, field.ty.span), + _ => unreachable!("mir field has to correspond to hir field"), + }; struct_span_err!( tcx.sess, field_span, @@ -387,7 +385,10 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b ) .multipart_suggestion_verbose( "wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped", - vec![(ty_span, format!("std::mem::ManuallyDrop<{}>", field_ty))], + vec![ + (ty_span.shrink_to_lo(), format!("std::mem::ManuallyDrop<")), + (ty_span.shrink_to_hi(), ">".into()), + ], Applicability::MaybeIncorrect, ) .emit(); diff --git a/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr b/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr index a9ccb83558773..0967cb7ba8bd2 100644 --- a/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr +++ b/src/test/ui/feature-gates/feature-gate-untagged_unions.stderr @@ -16,7 +16,7 @@ LL | a: String, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error[E0740]: unions may not contain fields that need dropping --> $DIR/feature-gate-untagged_unions.rs:24:5 @@ -27,7 +27,7 @@ LL | a: T, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error: aborting due to 3 previous errors diff --git a/src/test/ui/union/issue-41073.stderr b/src/test/ui/union/issue-41073.stderr index 04b4286cc4b63..8edf4db441b9c 100644 --- a/src/test/ui/union/issue-41073.stderr +++ b/src/test/ui/union/issue-41073.stderr @@ -7,7 +7,7 @@ LL | a: A, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error: aborting due to previous error diff --git a/src/test/ui/union/union-custom-drop.stderr b/src/test/ui/union/union-custom-drop.stderr index 0c59048f7fedb..65ca5fd931d67 100644 --- a/src/test/ui/union/union-custom-drop.stderr +++ b/src/test/ui/union/union-custom-drop.stderr @@ -7,7 +7,7 @@ LL | bar: Bar, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | bar: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error: aborting due to previous error diff --git a/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr b/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr index 8f3d30cd7ee46..f5e9681735c6f 100644 --- a/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr +++ b/src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr @@ -7,7 +7,7 @@ LL | a: String, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:19:5 @@ -18,7 +18,7 @@ LL | a: S, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:24:5 @@ -29,7 +29,7 @@ LL | a: T, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error: aborting due to 3 previous errors diff --git a/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr b/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr index 8f3d30cd7ee46..f5e9681735c6f 100644 --- a/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr +++ b/src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr @@ -7,7 +7,7 @@ LL | a: String, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:19:5 @@ -18,7 +18,7 @@ LL | a: S, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error[E0740]: unions may not contain fields that need dropping --> $DIR/union-with-drop-fields.rs:24:5 @@ -29,7 +29,7 @@ LL | a: T, help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped | LL | a: std::mem::ManuallyDrop, - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | +++++++++++++++++++++++ + error: aborting due to 3 previous errors From de05d3ec31f22b1f3afaf8b5ceddf224492c4aaa Mon Sep 17 00:00:00 2001 From: 5225225 <5225225@mailbox.org> Date: Sat, 13 Nov 2021 11:14:17 +0000 Subject: [PATCH 09/14] Print full char literal on error if any are non-printing --- .../src/lexer/unescape_error_reporting.rs | 10 ++++++++++ .../char/whitespace-character-literal.rs | 9 +++++++++ .../char/whitespace-character-literal.stderr | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/test/ui/parser/char/whitespace-character-literal.rs create mode 100644 src/test/ui/parser/char/whitespace-character-literal.stderr diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index 569f186a72766..0f6594a2a7f32 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -82,6 +82,16 @@ pub(crate) fn emit_unescape_error( Applicability::MachineApplicable, ); } + } else { + if lit.chars().filter(|x| x.is_whitespace() || x.is_control()).count() >= 1 { + handler.span_note( + span, + &format!( + "there are non-printing characters, the full sequence is `{}`", + lit.escape_default(), + ), + ); + } } if !has_help { diff --git a/src/test/ui/parser/char/whitespace-character-literal.rs b/src/test/ui/parser/char/whitespace-character-literal.rs new file mode 100644 index 0000000000000..ecb5c3cf49eda --- /dev/null +++ b/src/test/ui/parser/char/whitespace-character-literal.rs @@ -0,0 +1,9 @@ +// This tests that the error generated when a character literal has multiple +// characters in it contains a note about non-printing characters. + +fn main() { + // x + let _hair_space_around = ' x​'; + //~^ ERROR: character literal may only contain one codepoint + //~| NOTE: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}` +} diff --git a/src/test/ui/parser/char/whitespace-character-literal.stderr b/src/test/ui/parser/char/whitespace-character-literal.stderr new file mode 100644 index 0000000000000..a12088ce77d11 --- /dev/null +++ b/src/test/ui/parser/char/whitespace-character-literal.stderr @@ -0,0 +1,18 @@ +error: character literal may only contain one codepoint + --> $DIR/whitespace-character-literal.rs:6:30 + | +LL | let _hair_space_around = ' x​'; + | ^^^^ + | +note: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}` + --> $DIR/whitespace-character-literal.rs:6:31 + | +LL | let _hair_space_around = ' x​'; + | ^^ +help: if you meant to write a `str` literal, use double quotes + | +LL | let _hair_space_around = " x​"; + | ~~~~ + +error: aborting due to previous error + From 52199c93bb065db6e4ca5b95977212a30c646b5f Mon Sep 17 00:00:00 2001 From: 5225225 <5225225@mailbox.org> Date: Sat, 13 Nov 2021 12:46:22 +0000 Subject: [PATCH 10/14] Suggest removing the non-printing characters --- .../src/lexer/unescape_error_reporting.rs | 17 ++++++++++++++++- .../parser/char/whitespace-character-literal.rs | 3 ++- .../char/whitespace-character-literal.stderr | 13 ++++++------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index 0f6594a2a7f32..aa7ab4a953ca2 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -7,6 +7,10 @@ use rustc_errors::{pluralize, Applicability, Handler}; use rustc_lexer::unescape::{EscapeError, Mode}; use rustc_span::{BytePos, Span}; +fn printing(ch: char) -> bool { + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) != 0 && !ch.is_whitespace() +} + pub(crate) fn emit_unescape_error( handler: &Handler, // interior part of the literal, without quotes @@ -83,7 +87,11 @@ pub(crate) fn emit_unescape_error( ); } } else { - if lit.chars().filter(|x| x.is_whitespace() || x.is_control()).count() >= 1 { + let printable: Vec = lit.chars().filter(|x| printing(*x)).collect(); + + if let [ch] = printable.as_slice() { + has_help = true; + handler.span_note( span, &format!( @@ -91,6 +99,13 @@ pub(crate) fn emit_unescape_error( lit.escape_default(), ), ); + + handler.span_suggestion( + span, + "consider removing the non-printing characters", + ch.to_string(), + Applicability::MaybeIncorrect, + ); } } diff --git a/src/test/ui/parser/char/whitespace-character-literal.rs b/src/test/ui/parser/char/whitespace-character-literal.rs index ecb5c3cf49eda..de5e09204b462 100644 --- a/src/test/ui/parser/char/whitespace-character-literal.rs +++ b/src/test/ui/parser/char/whitespace-character-literal.rs @@ -2,8 +2,9 @@ // characters in it contains a note about non-printing characters. fn main() { - // x let _hair_space_around = ' x​'; //~^ ERROR: character literal may only contain one codepoint //~| NOTE: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}` + //~| HELP: consider removing the non-printing characters + //~| SUGGESTION: x } diff --git a/src/test/ui/parser/char/whitespace-character-literal.stderr b/src/test/ui/parser/char/whitespace-character-literal.stderr index a12088ce77d11..aa4fe4cf01f64 100644 --- a/src/test/ui/parser/char/whitespace-character-literal.stderr +++ b/src/test/ui/parser/char/whitespace-character-literal.stderr @@ -1,18 +1,17 @@ +['x'] error: character literal may only contain one codepoint - --> $DIR/whitespace-character-literal.rs:6:30 + --> $DIR/whitespace-character-literal.rs:5:30 | LL | let _hair_space_around = ' x​'; - | ^^^^ + | ^--^ + | | + | help: consider removing the non-printing characters: `x` | note: there are non-printing characters, the full sequence is `\u{200a}x\u{200b}` - --> $DIR/whitespace-character-literal.rs:6:31 + --> $DIR/whitespace-character-literal.rs:5:31 | LL | let _hair_space_around = ' x​'; | ^^ -help: if you meant to write a `str` literal, use double quotes - | -LL | let _hair_space_around = " x​"; - | ~~~~ error: aborting due to previous error From 17b5e2d167e8dec14117f0722c72444d2e3a5b2e Mon Sep 17 00:00:00 2001 From: 5225225 <5225225@mailbox.org> Date: Sat, 13 Nov 2021 13:25:19 +0000 Subject: [PATCH 11/14] Remove debug output from test stderr --- src/test/ui/parser/char/whitespace-character-literal.stderr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/ui/parser/char/whitespace-character-literal.stderr b/src/test/ui/parser/char/whitespace-character-literal.stderr index aa4fe4cf01f64..d73de41a8099b 100644 --- a/src/test/ui/parser/char/whitespace-character-literal.stderr +++ b/src/test/ui/parser/char/whitespace-character-literal.stderr @@ -1,4 +1,3 @@ -['x'] error: character literal may only contain one codepoint --> $DIR/whitespace-character-literal.rs:5:30 | From 09e59c2875e351cacd2f9cb653fee383acf02121 Mon Sep 17 00:00:00 2001 From: 5225225 <5225225@mailbox.org> Date: Sat, 13 Nov 2021 15:08:20 +0000 Subject: [PATCH 12/14] Inline printable function --- .../src/lexer/unescape_error_reporting.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index aa7ab4a953ca2..7f68112a427ba 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -7,10 +7,6 @@ use rustc_errors::{pluralize, Applicability, Handler}; use rustc_lexer::unescape::{EscapeError, Mode}; use rustc_span::{BytePos, Span}; -fn printing(ch: char) -> bool { - unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0) != 0 && !ch.is_whitespace() -} - pub(crate) fn emit_unescape_error( handler: &Handler, // interior part of the literal, without quotes @@ -87,7 +83,13 @@ pub(crate) fn emit_unescape_error( ); } } else { - let printable: Vec = lit.chars().filter(|x| printing(*x)).collect(); + let printable: Vec = lit + .chars() + .filter(|&x| { + unicode_width::UnicodeWidthChar::width(x).unwrap_or(0) != 0 + && !x.is_whitespace() + }) + .collect(); if let [ch] = printable.as_slice() { has_help = true; From eee29b0b95aedd9568df63a49340ca6efbf4bbb0 Mon Sep 17 00:00:00 2001 From: 5225225 <5225225@mailbox.org> Date: Tue, 16 Nov 2021 08:08:31 +0000 Subject: [PATCH 13/14] Increase tidy limit for parser by 1 --- src/tools/tidy/src/ui_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index cc058d538f388..681b2486d07cc 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -9,7 +9,7 @@ const ENTRY_LIMIT: usize = 1000; // FIXME: The following limits should be reduced eventually. const ROOT_ENTRY_LIMIT: usize = 1102; const ISSUES_ENTRY_LIMIT: usize = 2310; -const PARSER_LIMIT: usize = 1004; +const PARSER_LIMIT: usize = 1005; fn check_entries(path: &Path, bad: &mut bool) { let dirs = walkdir::WalkDir::new(&path.join("test/ui")) From 83ce771c0f3af07803423918a63ac4f958ca6ea9 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Tue, 16 Nov 2021 09:43:10 -0500 Subject: [PATCH 14/14] Update compiler/rustc_passes/src/check_attr.rs Co-authored-by: Yuki Okushi --- compiler/rustc_passes/src/check_attr.rs | 2 +- src/test/ui/lint/unused/unused-async.stderr | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 633a90dd0ec7c..09d883aad05d4 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -1062,7 +1062,7 @@ impl CheckAttrVisitor<'tcx> { lint.build( "`must_use` attribute on `async` functions \ applies to the anonymous `Future` returned by the \ - function, not the value within.", + function, not the value within", ) .span_label( *span, diff --git a/src/test/ui/lint/unused/unused-async.stderr b/src/test/ui/lint/unused/unused-async.stderr index e470b6dac9473..6bbc9e2bf0088 100644 --- a/src/test/ui/lint/unused/unused-async.stderr +++ b/src/test/ui/lint/unused/unused-async.stderr @@ -1,4 +1,4 @@ -warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within. +warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within --> $DIR/unused-async.rs:5:1 | LL | #[must_use] @@ -11,7 +11,7 @@ LL | | } | = note: `#[warn(unused_attributes)]` on by default -warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within. +warning: `must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within --> $DIR/unused-async.rs:15:5 | LL | #[must_use]