From e9215bb7a97d61b5368f58fa299474a8606655d4 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 22 May 2022 21:42:01 -0700 Subject: [PATCH 1/6] Do writeback of child expressions before parent expression --- compiler/rustc_typeck/src/check/writeback.rs | 3 +- .../type-check/unknown_type_for_closure.rs | 18 ++++++++++-- .../unknown_type_for_closure.stderr | 29 +++++++++++++++++-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_typeck/src/check/writeback.rs b/compiler/rustc_typeck/src/check/writeback.rs index 16096ea3d74..4fe5b26dc05 100644 --- a/compiler/rustc_typeck/src/check/writeback.rs +++ b/compiler/rustc_typeck/src/check/writeback.rs @@ -263,8 +263,6 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> { self.fix_scalar_builtin_expr(e); self.fix_index_builtin_expr(e); - self.visit_node_id(e.span, e.hir_id); - match e.kind { hir::ExprKind::Closure(_, _, body, _, _) => { let body = self.fcx.tcx.hir().body(body); @@ -291,6 +289,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> { _ => {} } + self.visit_node_id(e.span, e.hir_id); intravisit::walk_expr(self, e); } diff --git a/src/test/ui/type/type-check/unknown_type_for_closure.rs b/src/test/ui/type/type-check/unknown_type_for_closure.rs index 0dbf82453a2..0089d86e340 100644 --- a/src/test/ui/type/type-check/unknown_type_for_closure.rs +++ b/src/test/ui/type/type-check/unknown_type_for_closure.rs @@ -1,3 +1,17 @@ -fn main() { - let x = |_| { }; //~ ERROR type annotations needed +fn infer_in_arg() { + let x = |b: Vec<_>| {}; //~ ERROR E0282 } + +fn empty_pattern() { + let x = |_| {}; //~ ERROR type annotations needed +} + +fn infer_ty() { + let x = |k: _| {}; //~ ERROR type annotations needed +} + +fn ambig_return() { + let x = || -> Vec<_> { Vec::new() }; //~ ERROR type annotations needed for the closure `fn() -> Vec<_>` +} + +fn main() {} diff --git a/src/test/ui/type/type-check/unknown_type_for_closure.stderr b/src/test/ui/type/type-check/unknown_type_for_closure.stderr index 5971f56c97f..c3accad5f25 100644 --- a/src/test/ui/type/type-check/unknown_type_for_closure.stderr +++ b/src/test/ui/type/type-check/unknown_type_for_closure.stderr @@ -1,9 +1,32 @@ -error[E0282]: type annotations needed +error[E0282]: type annotations needed for `Vec<_>` --> $DIR/unknown_type_for_closure.rs:2:14 | -LL | let x = |_| { }; +LL | let x = |b: Vec<_>| {}; | ^ consider giving this closure parameter a type -error: aborting due to previous error +error[E0282]: type annotations needed + --> $DIR/unknown_type_for_closure.rs:6:14 + | +LL | let x = |_| {}; + | ^ consider giving this closure parameter a type + +error[E0282]: type annotations needed + --> $DIR/unknown_type_for_closure.rs:10:14 + | +LL | let x = |k: _| {}; + | ^ consider giving this closure parameter a type + +error[E0282]: type annotations needed for the closure `fn() -> Vec<_>` + --> $DIR/unknown_type_for_closure.rs:14:28 + | +LL | let x = || -> Vec<_> { Vec::new() }; + | ^^^^^^^^ cannot infer type for type parameter `T` + | +help: give this closure an explicit return type without `_` placeholders + | +LL | let x = || -> Vec<_> { Vec::new() }; + | ~~~~~~ + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0282`. From c82a3706f7f4560ae0e742a19a854b3cf755fc62 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 23 May 2022 20:27:06 +0300 Subject: [PATCH 2/6] rustc: Fix ICE in native library error reporting --- compiler/rustc_metadata/src/native_libs.rs | 9 +++++---- .../ui/native-library-link-flags/modifiers-override-3.rs | 7 +++++++ .../modifiers-override-3.stderr | 4 ++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/native-library-link-flags/modifiers-override-3.rs create mode 100644 src/test/ui/native-library-link-flags/modifiers-override-3.stderr diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 8d044be195a..95892d83414 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -418,10 +418,11 @@ impl<'tcx> Collector<'tcx> { // involved or not, library reordering and kind overriding without // explicit `:rename` in particular. if lib.has_modifiers() || passed_lib.has_modifiers() { - self.tcx.sess.span_err( - self.tcx.def_span(lib.foreign_module.unwrap()), - "overriding linking modifiers from command line is not supported" - ); + let msg = "overriding linking modifiers from command line is not supported"; + match lib.foreign_module { + Some(def_id) => self.tcx.sess.span_err(self.tcx.def_span(def_id), msg), + None => self.tcx.sess.err(msg), + }; } if passed_lib.kind != NativeLibKind::Unspecified { lib.kind = passed_lib.kind; diff --git a/src/test/ui/native-library-link-flags/modifiers-override-3.rs b/src/test/ui/native-library-link-flags/modifiers-override-3.rs new file mode 100644 index 00000000000..b28c53c6b0a --- /dev/null +++ b/src/test/ui/native-library-link-flags/modifiers-override-3.rs @@ -0,0 +1,7 @@ +// Regression test for issue #97299, one command line library with modifiers +// overrides another command line library with modifiers. + +// compile-flags:-lstatic:+whole-archive=foo -lstatic:+whole-archive=foo +// error-pattern: overriding linking modifiers from command line is not supported + +fn main() {} diff --git a/src/test/ui/native-library-link-flags/modifiers-override-3.stderr b/src/test/ui/native-library-link-flags/modifiers-override-3.stderr new file mode 100644 index 00000000000..365e5618100 --- /dev/null +++ b/src/test/ui/native-library-link-flags/modifiers-override-3.stderr @@ -0,0 +1,4 @@ +error: overriding linking modifiers from command line is not supported + +error: aborting due to previous error + From e2e425e8d2e89bdca278be732e063a5c48412382 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 24 May 2022 13:00:46 +0200 Subject: [PATCH 3/6] give correct error message on structural match violation --- .../src/thir/pattern/const_to_pat.rs | 18 +++++------ .../rustc_trait_selection/src/traits/mod.rs | 2 +- .../src/traits/structural_match.rs | 32 +++++++++++++------ compiler/rustc_typeck/src/check/wfcheck.rs | 24 ++++++++------ 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index 880f86aff5d..f694e009ab9 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -121,27 +121,27 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option { traits::search_for_structural_match_violation(self.span, self.tcx(), ty).map(|non_sm_ty| { - with_no_trimmed_paths!(match non_sm_ty { - traits::NonStructuralMatchTy::Adt(adt) => self.adt_derive_msg(adt), - traits::NonStructuralMatchTy::Dynamic => { + with_no_trimmed_paths!(match non_sm_ty.kind { + traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt), + traits::NonStructuralMatchTyKind::Dynamic => { "trait objects cannot be used in patterns".to_string() } - traits::NonStructuralMatchTy::Opaque => { + traits::NonStructuralMatchTyKind::Opaque => { "opaque types cannot be used in patterns".to_string() } - traits::NonStructuralMatchTy::Closure => { + traits::NonStructuralMatchTyKind::Closure => { "closures cannot be used in patterns".to_string() } - traits::NonStructuralMatchTy::Generator => { + traits::NonStructuralMatchTyKind::Generator => { "generators cannot be used in patterns".to_string() } - traits::NonStructuralMatchTy::Param => { + traits::NonStructuralMatchTyKind::Param => { bug!("use of a constant whose type is a parameter inside a pattern") } - traits::NonStructuralMatchTy::Projection => { + traits::NonStructuralMatchTyKind::Projection => { bug!("use of a constant whose type is a projection inside a pattern") } - traits::NonStructuralMatchTy::Foreign => { + traits::NonStructuralMatchTyKind::Foreign => { bug!("use of a value of a foreign type inside a pattern") } }) diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 81819534e8b..dcfdff68640 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -62,7 +62,7 @@ pub use self::specialize::specialization_graph::FutureCompatOverlapError; pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind; 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::structural_match::{NonStructuralMatchTy, NonStructuralMatchTyKind}; pub use self::util::{ elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span, elaborate_trait_ref, elaborate_trait_refs, diff --git a/compiler/rustc_trait_selection/src/traits/structural_match.rs b/compiler/rustc_trait_selection/src/traits/structural_match.rs index 67e3bf80486..5465395768c 100644 --- a/compiler/rustc_trait_selection/src/traits/structural_match.rs +++ b/compiler/rustc_trait_selection/src/traits/structural_match.rs @@ -11,7 +11,13 @@ use rustc_span::Span; use std::ops::ControlFlow; #[derive(Debug)] -pub enum NonStructuralMatchTy<'tcx> { +pub struct NonStructuralMatchTy<'tcx> { + pub ty: Ty<'tcx>, + pub kind: NonStructuralMatchTyKind<'tcx>, +} + +#[derive(Debug)] +pub enum NonStructuralMatchTyKind<'tcx> { Adt(AdtDef<'tcx>), Param, Dynamic, @@ -137,25 +143,32 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> { let (adt_def, substs) = match *ty.kind() { ty::Adt(adt_def, substs) => (adt_def, substs), ty::Param(_) => { - return ControlFlow::Break(NonStructuralMatchTy::Param); + let kind = NonStructuralMatchTyKind::Param; + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } ty::Dynamic(..) => { - return ControlFlow::Break(NonStructuralMatchTy::Dynamic); + let kind = NonStructuralMatchTyKind::Dynamic; + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } ty::Foreign(_) => { - return ControlFlow::Break(NonStructuralMatchTy::Foreign); + let kind = NonStructuralMatchTyKind::Foreign; + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } ty::Opaque(..) => { - return ControlFlow::Break(NonStructuralMatchTy::Opaque); + let kind = NonStructuralMatchTyKind::Opaque; + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } ty::Projection(..) => { - return ControlFlow::Break(NonStructuralMatchTy::Projection); + let kind = NonStructuralMatchTyKind::Projection; + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } ty::Closure(..) => { - return ControlFlow::Break(NonStructuralMatchTy::Closure); + let kind = NonStructuralMatchTyKind::Closure; + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } ty::Generator(..) | ty::GeneratorWitness(..) => { - return ControlFlow::Break(NonStructuralMatchTy::Generator); + let kind = NonStructuralMatchTyKind::Generator; + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } ty::RawPtr(..) => { // structural-match ignores substructure of @@ -215,7 +228,8 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> { if !self.type_marked_structural(ty) { debug!("Search found ty: {:?}", ty); - return ControlFlow::Break(NonStructuralMatchTy::Adt(adt_def)); + let kind = NonStructuralMatchTyKind::Adt(adt_def); + return ControlFlow::Break(NonStructuralMatchTy { ty, kind }); } // structural-match does not care about the diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index 50966868ec7..5c0c5b24ecd 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -827,7 +827,9 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) { ); } - if traits::search_for_structural_match_violation(param.span, tcx, ty).is_some() { + if let Some(non_structural_match_ty) = + traits::search_for_structural_match_violation(param.span, tcx, ty) + { // We use the same error code in both branches, because this is really the same // issue: we just special-case the message for type parameters to make it // clearer. @@ -853,19 +855,23 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) { ) .emit(); } else { - struct_span_err!( + let mut diag = struct_span_err!( tcx.sess, hir_ty.span, E0741, "`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \ the type of a const parameter", - ty, - ) - .span_label( - hir_ty.span, - format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"), - ) - .emit(); + non_structural_match_ty.ty, + ); + + if ty == non_structural_match_ty.ty { + diag.span_label( + hir_ty.span, + format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"), + ); + } + + diag.emit(); } } } else { From 86e8bbe4fdf547ad6bbc664282ba29f3ff4be5a6 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 24 May 2022 13:01:11 +0200 Subject: [PATCH 4/6] add and update tests --- .../issues/issue-63322-forbid-dyn.full.stderr | 4 ++-- src/test/ui/const-generics/issues/issue-97278.rs | 14 ++++++++++++++ .../ui/const-generics/issues/issue-97278.stderr | 9 +++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/const-generics/issues/issue-97278.rs create mode 100644 src/test/ui/const-generics/issues/issue-97278.stderr diff --git a/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr b/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr index e1c20e6ae78..16fabd1e88f 100644 --- a/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr +++ b/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.full.stderr @@ -1,8 +1,8 @@ -error[E0741]: `&'static (dyn A + 'static)` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter +error[E0741]: `(dyn A + 'static)` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter --> $DIR/issue-63322-forbid-dyn.rs:9:18 | LL | fn test() { - | ^^^^^^^^^^^^^^ `&'static (dyn A + 'static)` doesn't derive both `PartialEq` and `Eq` + | ^^^^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/const-generics/issues/issue-97278.rs b/src/test/ui/const-generics/issues/issue-97278.rs new file mode 100644 index 00000000000..da0a9776fd4 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-97278.rs @@ -0,0 +1,14 @@ +#![feature(adt_const_params)] +#![allow(incomplete_features)] + +use std::sync::Arc; + +#[derive(PartialEq, Eq)] +enum Bar { + Bar(Arc) +} + +fn test() {} +//~^ ERROR `Arc` must be annotated with `#[derive(PartialEq, Eq)]` + +fn main() {} diff --git a/src/test/ui/const-generics/issues/issue-97278.stderr b/src/test/ui/const-generics/issues/issue-97278.stderr new file mode 100644 index 00000000000..ff13cb505ab --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-97278.stderr @@ -0,0 +1,9 @@ +error[E0741]: `Arc` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter + --> $DIR/issue-97278.rs:11:20 + | +LL | fn test() {} + | ^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0741`. From cca9e63d70ce23d2c042a02fe4ac212ef967784f Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Wed, 25 May 2022 22:14:20 +0900 Subject: [PATCH 5/6] Add regression test for #82830 --- src/test/ui/traits/issue-82830.rs | 16 ++++++++++++++++ src/test/ui/traits/issue-82830.stderr | 15 +++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/test/ui/traits/issue-82830.rs create mode 100644 src/test/ui/traits/issue-82830.stderr diff --git a/src/test/ui/traits/issue-82830.rs b/src/test/ui/traits/issue-82830.rs new file mode 100644 index 00000000000..c8289b2e30b --- /dev/null +++ b/src/test/ui/traits/issue-82830.rs @@ -0,0 +1,16 @@ +trait A { + type B; +} + +type MaybeBox = >>::B; +struct P { + t: MaybeBox

, //~ ERROR: overflow evaluating the requirement `P: Sized` +} + +impl A for P { + type B = N; +} + +fn main() { + let t: MaybeBox

; +} diff --git a/src/test/ui/traits/issue-82830.stderr b/src/test/ui/traits/issue-82830.stderr new file mode 100644 index 00000000000..f863143c738 --- /dev/null +++ b/src/test/ui/traits/issue-82830.stderr @@ -0,0 +1,15 @@ +error[E0275]: overflow evaluating the requirement `P: Sized` + --> $DIR/issue-82830.rs:7:8 + | +LL | t: MaybeBox

, + | ^^^^^^^^^^^ + | +note: required because of the requirements on the impl of `A>` for `P` + --> $DIR/issue-82830.rs:10:12 + | +LL | impl A for P { + | ^^^^^^^ ^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. From 611948b9689d43ec43d77bbbc9e95df683fef853 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Wed, 25 May 2022 22:25:37 +0900 Subject: [PATCH 6/6] Fix a typo on Struct `Substructure` --- compiler/rustc_builtin_macros/src/deriving/generic/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 0fd23fd281e..0832fdad8b8 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -257,7 +257,7 @@ pub struct Substructure<'a> { pub type_ident: Ident, /// ident of the method pub method_ident: Ident, - /// dereferenced access to any [`Self_`] or [`Ptr(Self_, _)][ptr]` arguments + /// dereferenced access to any [`Self_`] or [`Ptr(Self_, _)`][ptr] arguments /// /// [`Self_`]: ty::Ty::Self_ /// [ptr]: ty::Ty::Ptr