From 547cb2722b3d7e5a437194bcd5f18f0c213ebdbd Mon Sep 17 00:00:00 2001 From: Takayuki Maeda <41065217+TaKO8Ki@users.noreply.github.com> Date: Thu, 21 Apr 2022 16:47:01 +0900 Subject: [PATCH 1/4] make `E0117` error clear --- compiler/rustc_typeck/src/coherence/orphan.rs | 11 +++++++++-- src/test/ui/coherence/coherence-cow.re_a.stderr | 2 +- src/test/ui/coherence/coherence-cow.re_b.stderr | 2 +- src/test/ui/coherence/coherence-cow.re_c.stderr | 2 +- src/test/ui/coherence/coherence-impls-copy.stderr | 2 +- src/test/ui/coherence/coherence-orphan.stderr | 4 ++-- .../ui/coherence/coherence-overlapping-pairs.stderr | 2 +- .../coherence-pair-covered-uncovered-1.stderr | 2 +- .../coherence/coherence-pair-covered-uncovered.stderr | 2 +- src/test/ui/coherence/coherence-vec-local-2.stderr | 2 +- src/test/ui/coherence/coherence-vec-local.stderr | 2 +- .../ui/coherence/coherence_local_err_struct.stderr | 2 +- src/test/ui/coherence/impl-foreign-for-foreign.stderr | 2 +- .../impl-foreign-for-foreign[foreign].stderr | 6 +++--- .../impl-foreign-for-fundamental[foreign].stderr | 4 ++-- .../impl-foreign[foreign]-for-foreign.stderr | 2 +- ...l-foreign[fundemental[foreign]]-for-foreign.stderr | 6 +++--- .../coherence/impl[t]-foreign-for-foreign[t].stderr | 4 ++-- src/test/ui/error-codes/E0117.stderr | 2 +- .../const-and-non-const-impl.rs | 2 +- .../const-and-non-const-impl.stderr | 2 +- ...ck-default-trait-impl-cross-crate-coherence.stderr | 2 +- 22 files changed, 37 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_typeck/src/coherence/orphan.rs b/compiler/rustc_typeck/src/coherence/orphan.rs index 77a5374482917..2fc425a6fc414 100644 --- a/compiler/rustc_typeck/src/coherence/orphan.rs +++ b/compiler/rustc_typeck/src/coherence/orphan.rs @@ -50,6 +50,7 @@ fn orphan_check_impl(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua tcx, sp, tr.path.span, + trait_ref.self_ty(), impl_.self_ty.span, &impl_.generics, err, @@ -201,18 +202,24 @@ fn emit_orphan_check_error<'tcx>( tcx: TyCtxt<'tcx>, sp: Span, trait_span: Span, + self_ty: Ty<'tcx>, self_ty_span: Span, generics: &hir::Generics<'tcx>, err: traits::OrphanCheckErr<'tcx>, ) -> Result { Err(match err { traits::OrphanCheckErr::NonLocalInputType(tys) => { + let msg = match self_ty.kind() { + ty::Adt(..) => "can be implemented for types defined outside of the crate", + ty::Param(_) => "can have blanket implementations defined in this trait", + _ if self_ty.is_primitive() => "can be implemented for primitive types", + _ => "can be implemented for arbitrary types", + }; let mut err = struct_span_err!( tcx.sess, sp, E0117, - "only traits defined in the current crate can be implemented for \ - arbitrary types" + "only traits defined in the current crate {msg}" ); err.span_label(sp, "impl doesn't use only types from inside the current crate"); for (ty, is_target_ty) in &tys { diff --git a/src/test/ui/coherence/coherence-cow.re_a.stderr b/src/test/ui/coherence/coherence-cow.re_a.stderr index 0cf2a406da443..fe4b5b4107895 100644 --- a/src/test/ui/coherence/coherence-cow.re_a.stderr +++ b/src/test/ui/coherence/coherence-cow.re_a.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-cow.rs:18:1 | LL | impl Remote for Pair> { } diff --git a/src/test/ui/coherence/coherence-cow.re_b.stderr b/src/test/ui/coherence/coherence-cow.re_b.stderr index b523db4da23ea..da4ede3251ed8 100644 --- a/src/test/ui/coherence/coherence-cow.re_b.stderr +++ b/src/test/ui/coherence/coherence-cow.re_b.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-cow.rs:22:1 | LL | impl Remote for Pair,T> { } diff --git a/src/test/ui/coherence/coherence-cow.re_c.stderr b/src/test/ui/coherence/coherence-cow.re_c.stderr index bd635fc2e8c29..d1a20c0ca101b 100644 --- a/src/test/ui/coherence/coherence-cow.re_c.stderr +++ b/src/test/ui/coherence/coherence-cow.re_c.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-cow.rs:26:1 | LL | impl Remote for Pair,U> { } diff --git a/src/test/ui/coherence/coherence-impls-copy.stderr b/src/test/ui/coherence/coherence-impls-copy.stderr index a7d6968a2967c..b3ca354c633aa 100644 --- a/src/test/ui/coherence/coherence-impls-copy.stderr +++ b/src/test/ui/coherence/coherence-impls-copy.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/coherence-impls-copy.rs:5:1 | LL | impl Copy for i32 {} diff --git a/src/test/ui/coherence/coherence-orphan.stderr b/src/test/ui/coherence/coherence-orphan.stderr index 52d2cc88cbe7f..01f166a21f768 100644 --- a/src/test/ui/coherence/coherence-orphan.stderr +++ b/src/test/ui/coherence/coherence-orphan.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/coherence-orphan.rs:10:1 | LL | impl TheTrait for isize { } @@ -10,7 +10,7 @@ LL | impl TheTrait for isize { } | = note: define and implement a trait or new type instead -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-orphan.rs:17:1 | LL | impl !Send for Vec { } diff --git a/src/test/ui/coherence/coherence-overlapping-pairs.stderr b/src/test/ui/coherence/coherence-overlapping-pairs.stderr index c1a02681c1343..15c92dfeb07d7 100644 --- a/src/test/ui/coherence/coherence-overlapping-pairs.stderr +++ b/src/test/ui/coherence/coherence-overlapping-pairs.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-overlapping-pairs.rs:8:1 | LL | impl Remote for lib::Pair { } diff --git a/src/test/ui/coherence/coherence-pair-covered-uncovered-1.stderr b/src/test/ui/coherence/coherence-pair-covered-uncovered-1.stderr index b18bf44dbdf28..03d7871238128 100644 --- a/src/test/ui/coherence/coherence-pair-covered-uncovered-1.stderr +++ b/src/test/ui/coherence/coherence-pair-covered-uncovered-1.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/coherence-pair-covered-uncovered-1.rs:12:1 | LL | impl Remote1>> for i32 { } diff --git a/src/test/ui/coherence/coherence-pair-covered-uncovered.stderr b/src/test/ui/coherence/coherence-pair-covered-uncovered.stderr index 34fdf64ea109f..73dfe2f572ae3 100644 --- a/src/test/ui/coherence/coherence-pair-covered-uncovered.stderr +++ b/src/test/ui/coherence/coherence-pair-covered-uncovered.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-pair-covered-uncovered.rs:8:1 | LL | impl Remote for Pair> { } diff --git a/src/test/ui/coherence/coherence-vec-local-2.stderr b/src/test/ui/coherence/coherence-vec-local-2.stderr index 567b6a6c17fda..95fdf172ec255 100644 --- a/src/test/ui/coherence/coherence-vec-local-2.stderr +++ b/src/test/ui/coherence/coherence-vec-local-2.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-vec-local-2.rs:11:1 | LL | impl Remote for Vec> { } diff --git a/src/test/ui/coherence/coherence-vec-local.stderr b/src/test/ui/coherence/coherence-vec-local.stderr index 38464f12a21d0..4835e771abd3b 100644 --- a/src/test/ui/coherence/coherence-vec-local.stderr +++ b/src/test/ui/coherence/coherence-vec-local.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence-vec-local.rs:11:1 | LL | impl Remote for Vec { } diff --git a/src/test/ui/coherence/coherence_local_err_struct.stderr b/src/test/ui/coherence/coherence_local_err_struct.stderr index 8c310b318a7af..afc6fc45d0e03 100644 --- a/src/test/ui/coherence/coherence_local_err_struct.stderr +++ b/src/test/ui/coherence/coherence_local_err_struct.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/coherence_local_err_struct.rs:14:1 | LL | impl lib::MyCopy for lib::MyStruct { } diff --git a/src/test/ui/coherence/impl-foreign-for-foreign.stderr b/src/test/ui/coherence/impl-foreign-for-foreign.stderr index fe7c9b93f5401..93f7a6fdc25d3 100644 --- a/src/test/ui/coherence/impl-foreign-for-foreign.stderr +++ b/src/test/ui/coherence/impl-foreign-for-foreign.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign-for-foreign.rs:10:1 | LL | impl Remote for i32 { diff --git a/src/test/ui/coherence/impl-foreign-for-foreign[foreign].stderr b/src/test/ui/coherence/impl-foreign-for-foreign[foreign].stderr index bdf19cf00a7ce..e24537bce229a 100644 --- a/src/test/ui/coherence/impl-foreign-for-foreign[foreign].stderr +++ b/src/test/ui/coherence/impl-foreign-for-foreign[foreign].stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign-for-foreign[foreign].rs:10:1 | LL | impl Remote1> for i32 { @@ -10,7 +10,7 @@ LL | impl Remote1> for i32 { | = note: define and implement a trait or new type instead -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign-for-foreign[foreign].rs:14:1 | LL | impl Remote1> for f64 { @@ -22,7 +22,7 @@ LL | impl Remote1> for f64 { | = note: define and implement a trait or new type instead -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign-for-foreign[foreign].rs:18:1 | LL | impl Remote1> for f32 { diff --git a/src/test/ui/coherence/impl-foreign-for-fundamental[foreign].stderr b/src/test/ui/coherence/impl-foreign-for-fundamental[foreign].stderr index 20dc955ffe42b..55ea4409e6f36 100644 --- a/src/test/ui/coherence/impl-foreign-for-fundamental[foreign].stderr +++ b/src/test/ui/coherence/impl-foreign-for-fundamental[foreign].stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/impl-foreign-for-fundamental[foreign].rs:10:1 | LL | impl Remote for Box { @@ -10,7 +10,7 @@ LL | impl Remote for Box { | = note: define and implement a trait or new type instead -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/impl-foreign-for-fundamental[foreign].rs:14:1 | LL | impl Remote for Box> { diff --git a/src/test/ui/coherence/impl-foreign[foreign]-for-foreign.stderr b/src/test/ui/coherence/impl-foreign[foreign]-for-foreign.stderr index 5552d82579371..65b3aa394a85f 100644 --- a/src/test/ui/coherence/impl-foreign[foreign]-for-foreign.stderr +++ b/src/test/ui/coherence/impl-foreign[foreign]-for-foreign.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign[foreign]-for-foreign.rs:10:1 | LL | impl Remote1 for f64 { diff --git a/src/test/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr b/src/test/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr index c1e2fdaf5e302..8e77c13e1116a 100644 --- a/src/test/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr +++ b/src/test/ui/coherence/impl-foreign[fundemental[foreign]]-for-foreign.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign[fundemental[foreign]]-for-foreign.rs:11:1 | LL | impl Remote1> for i32 { @@ -11,7 +11,7 @@ LL | impl Remote1> for i32 { | = note: define and implement a trait or new type instead -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign[fundemental[foreign]]-for-foreign.rs:15:1 | LL | impl Remote1>> for f64 { @@ -24,7 +24,7 @@ LL | impl Remote1>> for f64 { | = note: define and implement a trait or new type instead -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/impl-foreign[fundemental[foreign]]-for-foreign.rs:19:1 | LL | impl Remote1>> for f32 { diff --git a/src/test/ui/coherence/impl[t]-foreign-for-foreign[t].stderr b/src/test/ui/coherence/impl[t]-foreign-for-foreign[t].stderr index 7f8ec83b24a8b..92346c29198ce 100644 --- a/src/test/ui/coherence/impl[t]-foreign-for-foreign[t].stderr +++ b/src/test/ui/coherence/impl[t]-foreign-for-foreign[t].stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/impl[t]-foreign-for-foreign[t].rs:11:1 | LL | impl Remote for Rc { @@ -9,7 +9,7 @@ LL | impl Remote for Rc { | = note: define and implement a trait or new type instead -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/impl[t]-foreign-for-foreign[t].rs:16:1 | LL | impl Remote for Arc { diff --git a/src/test/ui/error-codes/E0117.stderr b/src/test/ui/error-codes/E0117.stderr index cdbafff2a202b..76d9f5cc0e52a 100644 --- a/src/test/ui/error-codes/E0117.stderr +++ b/src/test/ui/error-codes/E0117.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/E0117.rs:1:1 | LL | impl Drop for u32 {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.rs b/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.rs index 961968186de24..dd50993176673 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.rs @@ -3,7 +3,7 @@ pub struct Int(i32); impl const std::ops::Add for i32 { //~ ERROR type annotations needed - //~^ ERROR only traits defined in the current crate can be implemented for arbitrary types + //~^ ERROR only traits defined in the current crate can be implemented for primitive types type Output = Self; fn add(self, rhs: Self) -> Self { diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.stderr b/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.stderr index 154a6a35a44ef..9fd82196e79c1 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/const-and-non-const-impl.stderr @@ -1,4 +1,4 @@ -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for primitive types --> $DIR/const-and-non-const-impl.rs:5:1 | LL | impl const std::ops::Add for i32 { diff --git a/src/test/ui/typeck/typeck-default-trait-impl-cross-crate-coherence.stderr b/src/test/ui/typeck/typeck-default-trait-impl-cross-crate-coherence.stderr index cf5c15df7051c..fc3778b796745 100644 --- a/src/test/ui/typeck/typeck-default-trait-impl-cross-crate-coherence.stderr +++ b/src/test/ui/typeck/typeck-default-trait-impl-cross-crate-coherence.stderr @@ -26,7 +26,7 @@ error[E0321]: cross-crate traits with a default impl, like `DefaultedTrait`, can LL | impl DefaultedTrait for Box { } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait for type in another crate -error[E0117]: only traits defined in the current crate can be implemented for arbitrary types +error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate --> $DIR/typeck-default-trait-impl-cross-crate-coherence.rs:22:1 | LL | impl DefaultedTrait for lib::Something { } From 9ff5b7ee41cd53737865328c7a210316078e2c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Thu, 21 Apr 2022 00:00:00 +0000 Subject: [PATCH 2/4] Update `validate_uninhabited_zsts.rs` test after MIR building changes to ensure that it still tests validation, instead of failing earlier on during evaluation. --- .../validate_uninhabited_zsts.32bit.stderr | 35 +++++++++++-------- .../validate_uninhabited_zsts.64bit.stderr | 35 +++++++++++-------- .../const-eval/validate_uninhabited_zsts.rs | 19 ++++++---- 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/test/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr b/src/test/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr index bbb8511a65410..65ab1b02b3587 100644 --- a/src/test/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr +++ b/src/test/ui/consts/const-eval/validate_uninhabited_zsts.32bit.stderr @@ -7,14 +7,17 @@ LL | unsafe { std::mem::transmute(()) } | transmuting to uninhabited type | inside `foo` at $DIR/validate_uninhabited_zsts.rs:4:14 ... -LL | const FOO: [Empty; 3] = [foo(); 3]; - | ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:13:26 +LL | const FOO: [empty::Empty; 3] = [foo(); 3]; + | ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:20:33 -error[E0080]: evaluation of constant value failed - --> $DIR/validate_uninhabited_zsts.rs:16:35 +error[E0080]: it is undefined behavior to use this value + --> $DIR/validate_uninhabited_zsts.rs:23:1 + | +LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at [0].0: encountered a value of uninhabited type empty::Void | -LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; - | ^^^^^^^^^^^^^^^^^^^^^^^ transmuting to uninhabited type + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 0, align: 1) {} warning: the type `!` does not permit zero-initialization --> $DIR/validate_uninhabited_zsts.rs:4:14 @@ -28,16 +31,20 @@ LL | unsafe { std::mem::transmute(()) } = note: `#[warn(invalid_value)]` on by default = note: the `!` type has no valid value -warning: the type `Empty` does not permit zero-initialization - --> $DIR/validate_uninhabited_zsts.rs:16:35 +warning: the type `empty::Empty` does not permit zero-initialization + --> $DIR/validate_uninhabited_zsts.rs:23:42 + | +LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | | + | this code causes undefined behavior when executed + | help: use `MaybeUninit` instead, and only call `assume_init` after initialization is done | -LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; - | ^^^^^^^^^^^^^^^^^^^^^^^ - | | - | this code causes undefined behavior when executed - | help: use `MaybeUninit` instead, and only call `assume_init` after initialization is done +note: enums with no variants have no valid value (in this struct field) + --> $DIR/validate_uninhabited_zsts.rs:16:22 | - = note: enums with no variants have no valid value +LL | pub struct Empty(Void); + | ^^^^ error: aborting due to 2 previous errors; 2 warnings emitted diff --git a/src/test/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr b/src/test/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr index bbb8511a65410..65ab1b02b3587 100644 --- a/src/test/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr +++ b/src/test/ui/consts/const-eval/validate_uninhabited_zsts.64bit.stderr @@ -7,14 +7,17 @@ LL | unsafe { std::mem::transmute(()) } | transmuting to uninhabited type | inside `foo` at $DIR/validate_uninhabited_zsts.rs:4:14 ... -LL | const FOO: [Empty; 3] = [foo(); 3]; - | ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:13:26 +LL | const FOO: [empty::Empty; 3] = [foo(); 3]; + | ----- inside `FOO` at $DIR/validate_uninhabited_zsts.rs:20:33 -error[E0080]: evaluation of constant value failed - --> $DIR/validate_uninhabited_zsts.rs:16:35 +error[E0080]: it is undefined behavior to use this value + --> $DIR/validate_uninhabited_zsts.rs:23:1 + | +LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at [0].0: encountered a value of uninhabited type empty::Void | -LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; - | ^^^^^^^^^^^^^^^^^^^^^^^ transmuting to uninhabited type + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 0, align: 1) {} warning: the type `!` does not permit zero-initialization --> $DIR/validate_uninhabited_zsts.rs:4:14 @@ -28,16 +31,20 @@ LL | unsafe { std::mem::transmute(()) } = note: `#[warn(invalid_value)]` on by default = note: the `!` type has no valid value -warning: the type `Empty` does not permit zero-initialization - --> $DIR/validate_uninhabited_zsts.rs:16:35 +warning: the type `empty::Empty` does not permit zero-initialization + --> $DIR/validate_uninhabited_zsts.rs:23:42 + | +LL | const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | | + | this code causes undefined behavior when executed + | help: use `MaybeUninit` instead, and only call `assume_init` after initialization is done | -LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; - | ^^^^^^^^^^^^^^^^^^^^^^^ - | | - | this code causes undefined behavior when executed - | help: use `MaybeUninit` instead, and only call `assume_init` after initialization is done +note: enums with no variants have no valid value (in this struct field) + --> $DIR/validate_uninhabited_zsts.rs:16:22 | - = note: enums with no variants have no valid value +LL | pub struct Empty(Void); + | ^^^^ error: aborting due to 2 previous errors; 2 warnings emitted diff --git a/src/test/ui/consts/const-eval/validate_uninhabited_zsts.rs b/src/test/ui/consts/const-eval/validate_uninhabited_zsts.rs index 990d5a308238f..96f3312758292 100644 --- a/src/test/ui/consts/const-eval/validate_uninhabited_zsts.rs +++ b/src/test/ui/consts/const-eval/validate_uninhabited_zsts.rs @@ -6,16 +6,23 @@ const fn foo() -> ! { //~| WARN the type `!` does not permit zero-initialization [invalid_value] } -#[derive(Clone, Copy)] -enum Empty { } +// Type defined in a submodule, so that it is not "visibly" +// uninhabited (which would change interpreter behavior). +pub mod empty { + #[derive(Clone, Copy)] + enum Void {} + + #[derive(Clone, Copy)] + pub struct Empty(Void); +} #[warn(const_err)] -const FOO: [Empty; 3] = [foo(); 3]; +const FOO: [empty::Empty; 3] = [foo(); 3]; #[warn(const_err)] -const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; -//~^ ERROR evaluation of constant value failed -//~| WARN the type `Empty` does not permit zero-initialization +const BAR: [empty::Empty; 3] = [unsafe { std::mem::transmute(()) }; 3]; +//~^ ERROR it is undefined behavior to use this value +//~| WARN the type `empty::Empty` does not permit zero-initialization fn main() { FOO; From f5a8ee4dd84b4d3b6b53adedbd2b9ac3b0d09df8 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda <41065217+TaKO8Ki@users.noreply.github.com> Date: Fri, 22 Apr 2022 11:25:42 +0900 Subject: [PATCH 3/4] remove an error for type params --- compiler/rustc_typeck/src/coherence/orphan.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/rustc_typeck/src/coherence/orphan.rs b/compiler/rustc_typeck/src/coherence/orphan.rs index 2fc425a6fc414..19e68f0b14f4f 100644 --- a/compiler/rustc_typeck/src/coherence/orphan.rs +++ b/compiler/rustc_typeck/src/coherence/orphan.rs @@ -211,7 +211,6 @@ fn emit_orphan_check_error<'tcx>( traits::OrphanCheckErr::NonLocalInputType(tys) => { let msg = match self_ty.kind() { ty::Adt(..) => "can be implemented for types defined outside of the crate", - ty::Param(_) => "can have blanket implementations defined in this trait", _ if self_ty.is_primitive() => "can be implemented for primitive types", _ => "can be implemented for arbitrary types", }; From 343523cbf1820df9c58f4fc9c952a0289178c95a Mon Sep 17 00:00:00 2001 From: SparrowLii Date: Fri, 22 Apr 2022 20:05:39 +0800 Subject: [PATCH 4/4] Make the lifetime accurate which is used in the region constraints part --- compiler/rustc_borrowck/src/constraints/graph.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_borrowck/src/constraints/graph.rs b/compiler/rustc_borrowck/src/constraints/graph.rs index 4ceca60e23cee..c19a39c393f79 100644 --- a/compiler/rustc_borrowck/src/constraints/graph.rs +++ b/compiler/rustc_borrowck/src/constraints/graph.rs @@ -190,7 +190,7 @@ impl<'s, 'tcx, D: ConstraintGraphDirecton> RegionGraph<'s, 'tcx, D> { /// Given a region `R`, iterate over all regions `R1` such that /// there exists a constraint `R: R1`. - crate fn outgoing_regions(&self, region_sup: RegionVid) -> Successors<'_, 'tcx, D> { + crate fn outgoing_regions(&self, region_sup: RegionVid) -> Successors<'s, 'tcx, D> { Successors { edges: self.constraint_graph.outgoing_edges(region_sup, self.set, self.static_region), } @@ -225,10 +225,7 @@ impl<'s, 'tcx, D: ConstraintGraphDirecton> graph::WithSuccessors for RegionGraph } } -impl<'s, 'graph, 'tcx, D: ConstraintGraphDirecton> graph::GraphSuccessors<'graph> - for RegionGraph<'s, 'tcx, D> -{ +impl<'s, 'tcx, D: ConstraintGraphDirecton> graph::GraphSuccessors<'_> for RegionGraph<'s, 'tcx, D> { type Item = RegionVid; - // FIXME - why can't this be `'graph, 'tcx` - type Iter = Successors<'graph, 'graph, D>; + type Iter = Successors<'s, 'tcx, D>; }