From 8621285e3beb8e7e4a80b7d369e62b12084f9bcc Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 10 Jun 2023 22:41:59 +0000 Subject: [PATCH 1/9] reword message to be less vague --- .../src/traits/error_reporting/suggestions.rs | 4 ++-- .../anonymous-higher-ranked-lifetime.stderr | 22 +++++++++---------- tests/ui/closures/multiple-fn-bounds.stderr | 2 +- ...osure-arg-type-mismatch-issue-45727.stderr | 4 ++-- .../closure-arg-type-mismatch.stderr | 2 +- .../ui/mismatched_types/issue-36053-2.stderr | 2 +- ...uggest-option-asderef-inference-var.stderr | 2 +- .../suggest-option-asderef.fixed | 2 +- .../suggest-option-asderef.rs | 2 +- .../suggest-option-asderef.stderr | 2 +- .../late-bound-in-borrow-closure-sugg.stderr | 2 +- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 43b63762ba32d..7efef2364a60c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -4029,7 +4029,7 @@ fn hint_missing_borrow<'tcx>( if !to_borrow.is_empty() { err.multipart_suggestion_verbose( - "consider borrowing the argument", + "consider adjusting the signature so it borrows its argument", to_borrow, Applicability::MaybeIncorrect, ); @@ -4037,7 +4037,7 @@ fn hint_missing_borrow<'tcx>( if !remove_borrow.is_empty() { err.multipart_suggestion_verbose( - "do not borrow the argument", + "consider adjusting the signature so it does not borrow its argument", remove_borrow, Applicability::MaybeIncorrect, ); diff --git a/tests/ui/anonymous-higher-ranked-lifetime.stderr b/tests/ui/anonymous-higher-ranked-lifetime.stderr index c023d1b159056..9e3ffa6cce89a 100644 --- a/tests/ui/anonymous-higher-ranked-lifetime.stderr +++ b/tests/ui/anonymous-higher-ranked-lifetime.stderr @@ -13,7 +13,7 @@ note: required by a bound in `f1` | LL | fn f1(_: F) where F: Fn(&(), &()) {} | ^^^^^^^^^^^^ required by this bound in `f1` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | f1(|_: &(), _: &()| {}); | + + @@ -33,7 +33,7 @@ note: required by a bound in `f2` | LL | fn f2(_: F) where F: for<'a> Fn(&'a (), &()) {} | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f2` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | f2(|_: &(), _: &()| {}); | + + @@ -53,7 +53,7 @@ note: required by a bound in `f3` | LL | fn f3<'a, F>(_: F) where F: Fn(&'a (), &()) {} | ^^^^^^^^^^^^^^^ required by this bound in `f3` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | f3(|_: &(), _: &()| {}); | + + @@ -73,7 +73,7 @@ note: required by a bound in `f4` | LL | fn f4(_: F) where F: for<'r> Fn(&(), &'r ()) {} | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f4` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | f4(|_: &(), _: &()| {}); | + + @@ -93,7 +93,7 @@ note: required by a bound in `f5` | LL | fn f5(_: F) where F: for<'r> Fn(&'r (), &'r ()) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f5` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | f5(|_: &(), _: &()| {}); | + + @@ -113,7 +113,7 @@ note: required by a bound in `g1` | LL | fn g1(_: F) where F: Fn(&(), Box) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g1` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | g1(|_: &(), _: ()| {}); | + @@ -133,7 +133,7 @@ note: required by a bound in `g2` | LL | fn g2(_: F) where F: Fn(&(), fn(&())) {} | ^^^^^^^^^^^^^^^^ required by this bound in `g2` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | g2(|_: &(), _: ()| {}); | + @@ -153,7 +153,7 @@ note: required by a bound in `g3` | LL | fn g3(_: F) where F: for<'s> Fn(&'s (), Box) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g3` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | g3(|_: &(), _: ()| {}); | + @@ -173,7 +173,7 @@ note: required by a bound in `g4` | LL | fn g4(_: F) where F: Fn(&(), for<'r> fn(&'r ())) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g4` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | g4(|_: &(), _: ()| {}); | + @@ -193,7 +193,7 @@ note: required by a bound in `h1` | LL | fn h1(_: F) where F: Fn(&(), Box, &(), fn(&(), &())) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h1` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | h1(|_: &(), _: (), _: &(), _: ()| {}); | + + @@ -213,7 +213,7 @@ note: required by a bound in `h2` | LL | fn h2(_: F) where F: for<'t0> Fn(&(), Box, &'t0 (), fn(&(), &())) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h2` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | h2(|_: &(), _: (), _: &(), _: ()| {}); | + + diff --git a/tests/ui/closures/multiple-fn-bounds.stderr b/tests/ui/closures/multiple-fn-bounds.stderr index 32a1edb0024c0..d94c902f7214d 100644 --- a/tests/ui/closures/multiple-fn-bounds.stderr +++ b/tests/ui/closures/multiple-fn-bounds.stderr @@ -18,7 +18,7 @@ note: required by a bound in `foo` | LL | fn foo bool + Fn(char) -> bool>(f: F) { | ^^^^^^^^^^^^^^^^ required by this bound in `foo` -help: do not borrow the argument +help: consider adjusting the signature so it does not borrow its argument | LL | foo(move |char| v); | ~~~~ diff --git a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.stderr b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.stderr index fb8af4bb7dd29..452cba6b4de70 100644 --- a/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.stderr +++ b/tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.stderr @@ -10,7 +10,7 @@ LL | let _ = (-10..=10).find(|x: i32| x.signum() == 0); found closure signature `fn(i32) -> _` note: required by a bound in `find` --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | let _ = (-10..=10).find(|x: &i32| x.signum() == 0); | + @@ -27,7 +27,7 @@ LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0); found closure signature `for<'a, 'b, 'c> fn(&'a &'b &'c i32) -> _` note: required by a bound in `find` --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL -help: do not borrow the argument +help: consider adjusting the signature so it does not borrow its argument | LL - let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0); LL + let _ = (-10..=10).find(|x: &i32| x.signum() == 0); diff --git a/tests/ui/mismatched_types/closure-arg-type-mismatch.stderr b/tests/ui/mismatched_types/closure-arg-type-mismatch.stderr index 811ff0533f012..760e3327b776a 100644 --- a/tests/ui/mismatched_types/closure-arg-type-mismatch.stderr +++ b/tests/ui/mismatched_types/closure-arg-type-mismatch.stderr @@ -10,7 +10,7 @@ LL | a.iter().map(|_: (u32, u32)| 45); found closure signature `fn((u32, u32)) -> _` note: required by a bound in `map` --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | a.iter().map(|_: &(u32, u32)| 45); | + diff --git a/tests/ui/mismatched_types/issue-36053-2.stderr b/tests/ui/mismatched_types/issue-36053-2.stderr index a6764a1dc6d31..4d230ce9a7ab5 100644 --- a/tests/ui/mismatched_types/issue-36053-2.stderr +++ b/tests/ui/mismatched_types/issue-36053-2.stderr @@ -10,7 +10,7 @@ LL | once::<&str>("str").fuse().filter(|a: &str| true).count(); found closure signature `for<'a> fn(&'a str) -> _` note: required by a bound in `filter` --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | once::<&str>("str").fuse().filter(|a: &&str| true).count(); | + diff --git a/tests/ui/mismatched_types/suggest-option-asderef-inference-var.stderr b/tests/ui/mismatched_types/suggest-option-asderef-inference-var.stderr index 71c4729e31038..5c4c13266d030 100644 --- a/tests/ui/mismatched_types/suggest-option-asderef-inference-var.stderr +++ b/tests/ui/mismatched_types/suggest-option-asderef-inference-var.stderr @@ -13,7 +13,7 @@ LL | let _has_inference_vars: Option = Some(0).map(deref_int); found function signature `for<'a> fn(&'a i32) -> _` note: required by a bound in `Option::::map` --> $SRC_DIR/core/src/option.rs:LL:COL -help: do not borrow the argument +help: consider adjusting the signature so it does not borrow its argument | LL - fn deref_int(a: &i32) -> i32 { LL + fn deref_int(a: i32) -> i32 { diff --git a/tests/ui/mismatched_types/suggest-option-asderef.fixed b/tests/ui/mismatched_types/suggest-option-asderef.fixed index 5c42ece3c5d09..fc488b790b3d8 100644 --- a/tests/ui/mismatched_types/suggest-option-asderef.fixed +++ b/tests/ui/mismatched_types/suggest-option-asderef.fixed @@ -17,7 +17,7 @@ fn generic(_: T) -> Option<()> { } fn generic_ref(_: T) -> Option<()> { - //~^ HELP do not borrow the argument + //~^ HELP consider adjusting the signature so it does not borrow its argument Some(()) } diff --git a/tests/ui/mismatched_types/suggest-option-asderef.rs b/tests/ui/mismatched_types/suggest-option-asderef.rs index a5278b8fb1618..28f46808a3923 100644 --- a/tests/ui/mismatched_types/suggest-option-asderef.rs +++ b/tests/ui/mismatched_types/suggest-option-asderef.rs @@ -17,7 +17,7 @@ fn generic(_: T) -> Option<()> { } fn generic_ref(_: &T) -> Option<()> { - //~^ HELP do not borrow the argument + //~^ HELP consider adjusting the signature so it does not borrow its argument Some(()) } diff --git a/tests/ui/mismatched_types/suggest-option-asderef.stderr b/tests/ui/mismatched_types/suggest-option-asderef.stderr index 01341603dde3f..bfea086735065 100644 --- a/tests/ui/mismatched_types/suggest-option-asderef.stderr +++ b/tests/ui/mismatched_types/suggest-option-asderef.stderr @@ -73,7 +73,7 @@ LL | let _ = produces_string().and_then(generic_ref); found function signature `for<'a> fn(&'a _) -> _` note: required by a bound in `Option::::and_then` --> $SRC_DIR/core/src/option.rs:LL:COL -help: do not borrow the argument +help: consider adjusting the signature so it does not borrow its argument | LL - fn generic_ref(_: &T) -> Option<()> { LL + fn generic_ref(_: T) -> Option<()> { diff --git a/tests/ui/suggestions/late-bound-in-borrow-closure-sugg.stderr b/tests/ui/suggestions/late-bound-in-borrow-closure-sugg.stderr index 6820af1fd45c3..a03d4e8ce9f70 100644 --- a/tests/ui/suggestions/late-bound-in-borrow-closure-sugg.stderr +++ b/tests/ui/suggestions/late-bound-in-borrow-closure-sugg.stderr @@ -16,7 +16,7 @@ note: required by a bound in `Trader::<'a>::set_closure` | LL | pub fn set_closure(&mut self, function: impl Fn(&mut Trader) + 'a) { | ^^^^^^^^^^^^^^^ required by this bound in `Trader::<'a>::set_closure` -help: consider borrowing the argument +help: consider adjusting the signature so it borrows its argument | LL | let closure = |trader : &mut Trader| { | ++++ From 0f7ef1a202b32f783b3e0e05d318208c0c83b8d0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 10 Jun 2023 22:36:28 +0000 Subject: [PATCH 2/9] Adjust inner span of implicit self ref argument --- compiler/rustc_ast/src/ast.rs | 7 +++- .../dropck/explicit-drop-bounds.bad1.stderr | 4 +- .../dropck/explicit-drop-bounds.bad2.stderr | 4 +- .../min_specialization/issue-79224.stderr | 4 +- tests/ui/typeck/mismatched-map-under-self.rs | 17 ++++++++ .../typeck/mismatched-map-under-self.stderr | 42 +++++++++++++++++++ 6 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 tests/ui/typeck/mismatched-map-under-self.rs create mode 100644 tests/ui/typeck/mismatched-map-under-self.stderr diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index a398fd80119be..cd4373e6d330e 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2353,7 +2353,12 @@ impl Param { /// Builds a `Param` object from `ExplicitSelf`. pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param { let span = eself.span.to(eself_ident.span); - let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span, tokens: None }); + let infer_ty = P(Ty { + id: DUMMY_NODE_ID, + kind: TyKind::ImplicitSelf, + span: eself_ident.span, + tokens: None, + }); let (mutbl, ty) = match eself.node { SelfKind::Explicit(ty, mutbl) => (mutbl, ty), SelfKind::Value(mutbl) => (mutbl, infer_ty), diff --git a/tests/ui/dropck/explicit-drop-bounds.bad1.stderr b/tests/ui/dropck/explicit-drop-bounds.bad1.stderr index 3b506c7e7ec13..3ef11e2c0bbd1 100644 --- a/tests/ui/dropck/explicit-drop-bounds.bad1.stderr +++ b/tests/ui/dropck/explicit-drop-bounds.bad1.stderr @@ -15,10 +15,10 @@ LL | [T; 1]: Copy, T: std::marker::Copy // But `[T; 1]: Copy` does not imply | ~~~~~~~~~~~~~~~~~~~~~~ error[E0277]: the trait bound `T: Copy` is not satisfied - --> $DIR/explicit-drop-bounds.rs:32:13 + --> $DIR/explicit-drop-bounds.rs:32:18 | LL | fn drop(&mut self) {} - | ^^^^^^^^^ the trait `Copy` is not implemented for `T` + | ^^^^ the trait `Copy` is not implemented for `T` | note: required by a bound in `DropMe` --> $DIR/explicit-drop-bounds.rs:7:18 diff --git a/tests/ui/dropck/explicit-drop-bounds.bad2.stderr b/tests/ui/dropck/explicit-drop-bounds.bad2.stderr index 832af3e521a9d..8138b86ddea46 100644 --- a/tests/ui/dropck/explicit-drop-bounds.bad2.stderr +++ b/tests/ui/dropck/explicit-drop-bounds.bad2.stderr @@ -15,10 +15,10 @@ LL | impl Drop for DropMe | +++++++++++++++++++ error[E0277]: the trait bound `T: Copy` is not satisfied - --> $DIR/explicit-drop-bounds.rs:40:13 + --> $DIR/explicit-drop-bounds.rs:40:18 | LL | fn drop(&mut self) {} - | ^^^^^^^^^ the trait `Copy` is not implemented for `T` + | ^^^^ the trait `Copy` is not implemented for `T` | note: required by a bound in `DropMe` --> $DIR/explicit-drop-bounds.rs:7:18 diff --git a/tests/ui/specialization/min_specialization/issue-79224.stderr b/tests/ui/specialization/min_specialization/issue-79224.stderr index 505baa23ca336..9a4d557a152c6 100644 --- a/tests/ui/specialization/min_specialization/issue-79224.stderr +++ b/tests/ui/specialization/min_specialization/issue-79224.stderr @@ -11,10 +11,10 @@ LL | impl Display for Cow<'_, B> { | +++++++++++++++++++ error[E0277]: the trait bound `B: Clone` is not satisfied - --> $DIR/issue-79224.rs:20:12 + --> $DIR/issue-79224.rs:20:13 | LL | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - | ^^^^^ the trait `Clone` is not implemented for `B` + | ^^^^ the trait `Clone` is not implemented for `B` | = note: required for `B` to implement `ToOwned` help: consider further restricting this bound diff --git a/tests/ui/typeck/mismatched-map-under-self.rs b/tests/ui/typeck/mismatched-map-under-self.rs new file mode 100644 index 0000000000000..bedb7a1907d61 --- /dev/null +++ b/tests/ui/typeck/mismatched-map-under-self.rs @@ -0,0 +1,17 @@ +pub trait Insertable { + type Values; + + fn values(&self) -> Self::Values; +} + +impl Insertable for Option { + type Values = (); + + fn values(self) -> Self::Values { + //~^ ERROR method `values` has an incompatible type for trait + self.map(Insertable::values).unwrap_or_default() + //~^ ERROR type mismatch in function arguments + } +} + +fn main() {} diff --git a/tests/ui/typeck/mismatched-map-under-self.stderr b/tests/ui/typeck/mismatched-map-under-self.stderr new file mode 100644 index 0000000000000..65ef5456268f3 --- /dev/null +++ b/tests/ui/typeck/mismatched-map-under-self.stderr @@ -0,0 +1,42 @@ +error[E0053]: method `values` has an incompatible type for trait + --> $DIR/mismatched-map-under-self.rs:10:15 + | +LL | fn values(self) -> Self::Values { + | ^^^^ + | | + | expected `&Option`, found `Option` + | help: change the self-receiver type to match the trait: `&self` + | +note: type in trait + --> $DIR/mismatched-map-under-self.rs:4:15 + | +LL | fn values(&self) -> Self::Values; + | ^^^^^ + = note: expected signature `fn(&Option)` + found signature `fn(Option)` + +error[E0631]: type mismatch in function arguments + --> $DIR/mismatched-map-under-self.rs:12:18 + | +LL | fn values(&self) -> Self::Values; + | --------------------------------- found signature defined here +... +LL | self.map(Insertable::values).unwrap_or_default() + | --- ^^^^^^^^^^^^^^^^^^ expected due to this + | | + | required by a bound introduced by this call + | + = note: expected function signature `fn(T) -> _` + found function signature `for<'a> fn(&'a _) -> _` +note: required by a bound in `Option::::map` + --> $SRC_DIR/core/src/option.rs:LL:COL +help: consider adjusting the signature so it does not borrow its argument + | +LL - fn values(&self) -> Self::Values; +LL + fn values(self) -> Self::Values; + | + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0053, E0631. +For more information about an error, try `rustc --explain E0053`. From d1ab6c2ae3fa0726d5f8b5a92b66faa09c72b893 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 10 Jun 2023 22:51:00 +0000 Subject: [PATCH 3/9] Do not suggest adjusting trait signature on type mismatch --- .../src/traits/error_reporting/suggestions.rs | 4 ++++ tests/ui/typeck/mismatched-map-under-self.stderr | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 7efef2364a60c..73a45322c4fe0 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3959,6 +3959,10 @@ fn hint_missing_borrow<'tcx>( found_node: Node<'_>, err: &mut Diagnostic, ) { + if matches!(found_node, Node::TraitItem(..)) { + return; + } + let found_args = match found.kind() { ty::FnPtr(f) => infcx.instantiate_binder_with_placeholders(*f).inputs().iter(), kind => { diff --git a/tests/ui/typeck/mismatched-map-under-self.stderr b/tests/ui/typeck/mismatched-map-under-self.stderr index 65ef5456268f3..51491407c4938 100644 --- a/tests/ui/typeck/mismatched-map-under-self.stderr +++ b/tests/ui/typeck/mismatched-map-under-self.stderr @@ -30,11 +30,6 @@ LL | self.map(Insertable::values).unwrap_or_default() found function signature `for<'a> fn(&'a _) -> _` note: required by a bound in `Option::::map` --> $SRC_DIR/core/src/option.rs:LL:COL -help: consider adjusting the signature so it does not borrow its argument - | -LL - fn values(&self) -> Self::Values; -LL + fn values(self) -> Self::Values; - | error: aborting due to 2 previous errors From 8745ae21f9acd17fb3e3d1f9556280e652e996a0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 28 Jun 2023 18:08:21 +0000 Subject: [PATCH 4/9] convert to fluent, make plurals work --- compiler/rustc_trait_selection/messages.ftl | 10 ++++++ compiler/rustc_trait_selection/src/errors.rs | 36 ++++++++++++++++++- .../src/traits/error_reporting/suggestions.rs | 13 ++----- .../anonymous-higher-ranked-lifetime.stderr | 14 ++++---- 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_trait_selection/messages.ftl b/compiler/rustc_trait_selection/messages.ftl index 217ba71b63108..f57f1bad15dca 100644 --- a/compiler/rustc_trait_selection/messages.ftl +++ b/compiler/rustc_trait_selection/messages.ftl @@ -1,3 +1,13 @@ +trait_selection_adjust_signature_borrow = consider adjusting the signature so it borrows its {$len -> + [one] argument + *[other] arguments + } + +trait_selection_adjust_signature_remove_borrow = consider adjusting the signature so it does not borrow its {$len -> + [one] argument + *[other] arguments + } + trait_selection_dump_vtable_entries = vtable entries for `{$trait_ref}`: {$entries} trait_selection_empty_on_clause_in_rustc_on_unimplemented = empty `on`-clause in `#[rustc_on_unimplemented]` diff --git a/compiler/rustc_trait_selection/src/errors.rs b/compiler/rustc_trait_selection/src/errors.rs index 54e22cc3d7fe2..dde9e9c9ac694 100644 --- a/compiler/rustc_trait_selection/src/errors.rs +++ b/compiler/rustc_trait_selection/src/errors.rs @@ -1,5 +1,8 @@ use crate::fluent_generated as fluent; -use rustc_errors::{ErrorGuaranteed, Handler, IntoDiagnostic}; +use rustc_errors::{ + AddToDiagnostic, Applicability, Diagnostic, ErrorGuaranteed, Handler, IntoDiagnostic, + SubdiagnosticMessage, +}; use rustc_macros::Diagnostic; use rustc_middle::ty::{self, PolyTraitRef, Ty}; use rustc_span::{Span, Symbol}; @@ -97,3 +100,34 @@ pub struct InherentProjectionNormalizationOverflow { pub span: Span, pub ty: String, } + +pub enum AdjustSignatureBorrow { + Borrow { to_borrow: Vec<(Span, String)> }, + RemoveBorrow { remove_borrow: Vec<(Span, String)> }, +} + +impl AddToDiagnostic for AdjustSignatureBorrow { + fn add_to_diagnostic_with(self, diag: &mut Diagnostic, _: F) + where + F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, + { + match self { + AdjustSignatureBorrow::Borrow { to_borrow } => { + diag.set_arg("len", to_borrow.len()); + diag.multipart_suggestion_verbose( + fluent::trait_selection_adjust_signature_borrow, + to_borrow, + Applicability::MaybeIncorrect, + ); + } + AdjustSignatureBorrow::RemoveBorrow { remove_borrow } => { + diag.set_arg("len", remove_borrow.len()); + diag.multipart_suggestion_verbose( + fluent::trait_selection_adjust_signature_remove_borrow, + remove_borrow, + Applicability::MaybeIncorrect, + ); + } + } + } +} diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 73a45322c4fe0..798155428b474 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -5,6 +5,7 @@ use super::{ PredicateObligation, }; +use crate::errors; use crate::infer::InferCtxt; use crate::traits::{NormalizeExt, ObligationCtxt}; @@ -4032,19 +4033,11 @@ fn hint_missing_borrow<'tcx>( } if !to_borrow.is_empty() { - err.multipart_suggestion_verbose( - "consider adjusting the signature so it borrows its argument", - to_borrow, - Applicability::MaybeIncorrect, - ); + err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow }); } if !remove_borrow.is_empty() { - err.multipart_suggestion_verbose( - "consider adjusting the signature so it does not borrow its argument", - remove_borrow, - Applicability::MaybeIncorrect, - ); + err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow }); } } diff --git a/tests/ui/anonymous-higher-ranked-lifetime.stderr b/tests/ui/anonymous-higher-ranked-lifetime.stderr index 9e3ffa6cce89a..e441cbdc866a1 100644 --- a/tests/ui/anonymous-higher-ranked-lifetime.stderr +++ b/tests/ui/anonymous-higher-ranked-lifetime.stderr @@ -13,7 +13,7 @@ note: required by a bound in `f1` | LL | fn f1(_: F) where F: Fn(&(), &()) {} | ^^^^^^^^^^^^ required by this bound in `f1` -help: consider adjusting the signature so it borrows its argument +help: consider adjusting the signature so it borrows its arguments | LL | f1(|_: &(), _: &()| {}); | + + @@ -33,7 +33,7 @@ note: required by a bound in `f2` | LL | fn f2(_: F) where F: for<'a> Fn(&'a (), &()) {} | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f2` -help: consider adjusting the signature so it borrows its argument +help: consider adjusting the signature so it borrows its arguments | LL | f2(|_: &(), _: &()| {}); | + + @@ -53,7 +53,7 @@ note: required by a bound in `f3` | LL | fn f3<'a, F>(_: F) where F: Fn(&'a (), &()) {} | ^^^^^^^^^^^^^^^ required by this bound in `f3` -help: consider adjusting the signature so it borrows its argument +help: consider adjusting the signature so it borrows its arguments | LL | f3(|_: &(), _: &()| {}); | + + @@ -73,7 +73,7 @@ note: required by a bound in `f4` | LL | fn f4(_: F) where F: for<'r> Fn(&(), &'r ()) {} | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f4` -help: consider adjusting the signature so it borrows its argument +help: consider adjusting the signature so it borrows its arguments | LL | f4(|_: &(), _: &()| {}); | + + @@ -93,7 +93,7 @@ note: required by a bound in `f5` | LL | fn f5(_: F) where F: for<'r> Fn(&'r (), &'r ()) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f5` -help: consider adjusting the signature so it borrows its argument +help: consider adjusting the signature so it borrows its arguments | LL | f5(|_: &(), _: &()| {}); | + + @@ -193,7 +193,7 @@ note: required by a bound in `h1` | LL | fn h1(_: F) where F: Fn(&(), Box, &(), fn(&(), &())) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h1` -help: consider adjusting the signature so it borrows its argument +help: consider adjusting the signature so it borrows its arguments | LL | h1(|_: &(), _: (), _: &(), _: ()| {}); | + + @@ -213,7 +213,7 @@ note: required by a bound in `h2` | LL | fn h2(_: F) where F: for<'t0> Fn(&(), Box, &'t0 (), fn(&(), &())) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h2` -help: consider adjusting the signature so it borrows its argument +help: consider adjusting the signature so it borrows its arguments | LL | h2(|_: &(), _: (), _: &(), _: ()| {}); | + + From c0156f1b2c42baa7a19308f940dddab0f7d384ac Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 22 Jul 2023 15:29:42 +0800 Subject: [PATCH 5/9] Fix rustc-args passing issue in bootstrap --- src/bootstrap/builder.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 32eb4e68b08db..1707dafb11aa8 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -2043,6 +2043,13 @@ impl<'a> Builder<'a> { rustflags.arg("-Zinline-mir"); } + // set rustc args passed from command line + let rustc_args = + self.config.cmd.rustc_args().iter().map(|s| s.to_string()).collect::>(); + if !rustc_args.is_empty() { + cargo.env("RUSTFLAGS", &rustc_args.join(" ")); + } + Cargo { command: cargo, rustflags, rustdocflags, allow_features } } From b8701ff9d3cb22e6d3ec4c8a1e167c0a2366f3a8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 22 Jul 2023 07:59:14 +0000 Subject: [PATCH 6/9] Remove Scope::Elision. --- .../src/collect/resolve_bound_vars.rs | 59 +++++++------------ 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index 44f116ef2da6c..5e261f8038726 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -137,12 +137,6 @@ enum Scope<'a> { s: ScopeRef<'a>, }, - /// A scope which either determines unspecified lifetimes or errors - /// on them (e.g., due to ambiguity). - Elision { - s: ScopeRef<'a>, - }, - /// Use a specific lifetime (if `Some`) or leave it unset (to be /// inferred in a function body or potentially error outside one), /// for the default choice of lifetime in a trait object type. @@ -211,7 +205,6 @@ impl<'a> fmt::Debug for TruncatedScopeDebug<'a> { Scope::Body { id, s: _ } => { f.debug_struct("Body").field("id", id).field("s", &"..").finish() } - Scope::Elision { s: _ } => f.debug_struct("Elision").field("s", &"..").finish(), Scope::ObjectLifetimeDefault { lifetime, s: _ } => f .debug_struct("ObjectLifetimeDefault") .field("lifetime", lifetime) @@ -325,9 +318,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { break (vec![], BinderScopeType::Normal); } - Scope::Elision { s, .. } - | Scope::ObjectLifetimeDefault { s, .. } - | Scope::AnonConstBoundary { s } => { + Scope::ObjectLifetimeDefault { s, .. } | Scope::AnonConstBoundary { s } => { scope = s; } @@ -526,16 +517,12 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { | hir::ItemKind::Macro(..) | hir::ItemKind::Mod(..) | hir::ItemKind::ForeignMod { .. } + | hir::ItemKind::Static(..) + | hir::ItemKind::Const(..) | hir::ItemKind::GlobalAsm(..) => { // These sorts of items have no lifetime parameters at all. intravisit::walk_item(self, item); } - hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => { - // No lifetime parameters, but implied 'static. - self.with(Scope::Elision { s: self.scope }, |this| { - intravisit::walk_item(this, item) - }); - } hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin: hir::OpaqueTyOrigin::TyAlias { .. }, .. @@ -727,12 +714,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { // Elided lifetimes are not allowed in non-return // position impl Trait let scope = Scope::TraitRefBoundary { s: self.scope }; - self.with(scope, |this| { - let scope = Scope::Elision { s: this.scope }; - this.with(scope, |this| { - intravisit::walk_item(this, opaque_ty); - }) - }); + self.with(scope, |this| intravisit::walk_item(this, opaque_ty)); return; } @@ -1293,8 +1275,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { scope = s; } - Scope::Elision { s, .. } - | Scope::ObjectLifetimeDefault { s, .. } + Scope::ObjectLifetimeDefault { s, .. } | Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } | Scope::AnonConstBoundary { s } => { @@ -1357,7 +1338,6 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { Scope::Root { .. } => break, Scope::Binder { s, .. } | Scope::Body { s, .. } - | Scope::Elision { s, .. } | Scope::ObjectLifetimeDefault { s, .. } | Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } @@ -1409,8 +1389,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { scope = s; } - Scope::Elision { s, .. } - | Scope::ObjectLifetimeDefault { s, .. } + Scope::ObjectLifetimeDefault { s, .. } | Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } => { scope = s; @@ -1483,7 +1462,6 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { Scope::Root { .. } => break, Scope::Binder { s, .. } | Scope::Body { s, .. } - | Scope::Elision { s, .. } | Scope::ObjectLifetimeDefault { s, .. } | Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } @@ -1564,7 +1542,6 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { Scope::Body { .. } => break true, Scope::Binder { s, .. } - | Scope::Elision { s, .. } | Scope::ObjectLifetimeDefault { s, .. } | Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } @@ -1832,14 +1809,20 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { output: Option<&'tcx hir::Ty<'tcx>>, in_closure: bool, ) { - self.with(Scope::Elision { s: self.scope }, |this| { - for input in inputs { - this.visit_ty(input); - } - if !in_closure && let Some(output) = output { - this.visit_ty(output); - } - }); + self.with( + Scope::ObjectLifetimeDefault { + lifetime: Some(ResolvedArg::StaticLifetime), + s: self.scope, + }, + |this| { + for input in inputs { + this.visit_ty(input); + } + if !in_closure && let Some(output) = output { + this.visit_ty(output); + } + }, + ); if in_closure && let Some(output) = output { self.visit_ty(output); } @@ -1859,7 +1842,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { scope = s; } - Scope::Root { .. } | Scope::Elision { .. } => break ResolvedArg::StaticLifetime, + Scope::Root { .. } => break ResolvedArg::StaticLifetime, Scope::Body { .. } | Scope::ObjectLifetimeDefault { lifetime: None, .. } => return, From ffa4b6f422aabfc425f61526d51e2c42300b130f Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 22 Jul 2023 13:02:59 +0200 Subject: [PATCH 7/9] Add regression test for issue #113941 - naive layout isn't refined --- tests/ui/layout/issue-113941.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/ui/layout/issue-113941.rs diff --git a/tests/ui/layout/issue-113941.rs b/tests/ui/layout/issue-113941.rs new file mode 100644 index 0000000000000..7a54e28b35034 --- /dev/null +++ b/tests/ui/layout/issue-113941.rs @@ -0,0 +1,13 @@ +// build-pass +// revisions: normal randomize-layout +// [randomize-layout]compile-flags: -Zrandomize-layout + +enum Void {} + +pub struct Struct([*const (); 0], Void); + +pub enum Enum { + Variant(Struct), +} + +fn main() {} From d4ce83c38585a033069983a2990caad14911553b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 22 Jul 2023 14:01:23 +0200 Subject: [PATCH 8/9] Migrate GUI colors test to original CSS color format --- tests/rustdoc-gui/item-decl-colors.goml | 49 +++++++++++++------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/tests/rustdoc-gui/item-decl-colors.goml b/tests/rustdoc-gui/item-decl-colors.goml index 5732dd8eea26d..a777842735c24 100644 --- a/tests/rustdoc-gui/item-decl-colors.goml +++ b/tests/rustdoc-gui/item-decl-colors.goml @@ -20,6 +20,7 @@ define-function: ( block { go-to: "file://" + |DOC_PATH| + "/test_docs/struct.WithGenerics.html" show-text: true + set-local-storage: {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"} reload: assert-css: (".item-decl .code-attribute", {"color": |attr_color|}, ALL) @@ -40,41 +41,41 @@ call-function: ( "check-colors", { "theme": "ayu", - "attr_color": "rgb(153, 153, 153)", - "trait_color": "rgb(57, 175, 215)", - "struct_color": "rgb(255, 160, 165)", - "enum_color": "rgb(255, 160, 165)", - "primitive_color": "rgb(255, 160, 165)", - "constant_color": "rgb(57, 175, 215)", - "fn_color": "rgb(253, 214, 135)", - "assoc_type_color": "rgb(57, 175, 215)", + "attr_color": "#999", + "trait_color": "#39afd7", + "struct_color": "#ffa0a5", + "enum_color": "#ffa0a5", + "primitive_color": "#ffa0a5", + "constant_color": "#39afd7", + "fn_color": "#fdd687", + "assoc_type_color": "#39afd7", }, ) call-function: ( "check-colors", { "theme": "dark", - "attr_color": "rgb(153, 153, 153)", - "trait_color": "rgb(183, 140, 242)", - "struct_color": "rgb(45, 191, 184)", - "enum_color": "rgb(45, 191, 184)", - "primitive_color": "rgb(45, 191, 184)", - "constant_color": "rgb(210, 153, 29)", - "fn_color": "rgb(43, 171, 99)", - "assoc_type_color": "rgb(210, 153, 29)", + "attr_color": "#999", + "trait_color": "#b78cf2", + "struct_color": "#2dbfb8", + "enum_color": "#2dbfb8", + "primitive_color": "#2dbfb8", + "constant_color": "#d2991d", + "fn_color": "#2bab63", + "assoc_type_color": "#d2991d", }, ) call-function: ( "check-colors", { "theme": "light", - "attr_color": "rgb(153, 153, 153)", - "trait_color": "rgb(110, 79, 201)", - "struct_color": "rgb(173, 55, 138)", - "enum_color": "rgb(173, 55, 138)", - "primitive_color": "rgb(173, 55, 138)", - "constant_color": "rgb(56, 115, 173)", - "fn_color": "rgb(173, 124, 55)", - "assoc_type_color": "rgb(56, 115, 173)", + "attr_color": "#999", + "trait_color": "#6e4fc9", + "struct_color": "#ad378a", + "enum_color": "#ad378a", + "primitive_color": "#ad378a", + "constant_color": "#3873ad", + "fn_color": "#ad7c37", + "assoc_type_color": "#3873ad", }, ) From e32011209d185a12575c90929c13211936a3f421 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 20 Jul 2023 20:02:56 +0000 Subject: [PATCH 9/9] Get rid of subst-relate incompleteness in new solver --- .../src/solve/alias_relate.rs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_trait_selection/src/solve/alias_relate.rs b/compiler/rustc_trait_selection/src/solve/alias_relate.rs index 73362d823065a..3c7f91e6ca9be 100644 --- a/compiler/rustc_trait_selection/src/solve/alias_relate.rs +++ b/compiler/rustc_trait_selection/src/solve/alias_relate.rs @@ -66,24 +66,27 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { Invert::Yes, )); // Relate via args - let subst_relate_response = self - .assemble_subst_relate_candidate(param_env, alias_lhs, alias_rhs, direction); - candidates.extend(subst_relate_response); + candidates.extend( + self.assemble_subst_relate_candidate( + param_env, alias_lhs, alias_rhs, direction, + ), + ); debug!(?candidates); if let Some(merged) = self.try_merge_responses(&candidates) { Ok(merged) } else { - // When relating two aliases and we have ambiguity, we prefer - // relating the generic arguments of the aliases over normalizing - // them. This is necessary for inference during typeck. + // When relating two aliases and we have ambiguity, if both + // aliases can be normalized to something, we prefer + // "bidirectionally normalizing" both of them within the same + // candidate. + // + // See . // // As this is incomplete, we must not do so during coherence. match self.solver_mode() { SolverMode::Normal => { - if let Ok(subst_relate_response) = subst_relate_response { - Ok(subst_relate_response) - } else if let Ok(bidirectional_normalizes_to_response) = self + if let Ok(bidirectional_normalizes_to_response) = self .assemble_bidirectional_normalizes_to_candidate( param_env, lhs, rhs, direction, )