From 7cdb23b98a58a4e73ab1e4ca5e3bb7d04645bdd7 Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Wed, 17 May 2023 22:10:36 +0200 Subject: [PATCH] don't skip inference for type in `offset_of!` --- compiler/rustc_hir_typeck/src/writeback.rs | 10 +--------- .../rustc_mir_build/src/build/expr/as_rvalue.rs | 7 +++---- library/core/tests/mem.rs | 5 +++++ tests/ui/offset-of/offset-of-arg-count.rs | 2 +- tests/ui/offset-of/offset-of-dst-field.rs | 1 + tests/ui/offset-of/offset-of-dst-field.stderr | 13 +++++++++++-- tests/ui/offset-of/offset-of-inference.rs | 11 +++++++++++ tests/ui/offset-of/offset-of-inference.stderr | 9 +++++++++ 8 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 tests/ui/offset-of/offset-of-inference.rs create mode 100644 tests/ui/offset-of/offset-of-inference.stderr diff --git a/compiler/rustc_hir_typeck/src/writeback.rs b/compiler/rustc_hir_typeck/src/writeback.rs index eed3c65eccc0e..a4c6dd4332a60 100644 --- a/compiler/rustc_hir_typeck/src/writeback.rs +++ b/compiler/rustc_hir_typeck/src/writeback.rs @@ -692,15 +692,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { fcx_typeck_results.offset_of_data().items_in_stable_order() { let hir_id = hir::HirId { owner: common_hir_owner, local_id }; - - if cfg!(debug_assertions) && container.has_infer() { - span_bug!( - hir_id.to_span(self.fcx.tcx), - "writeback: `{:?}` has inference variables", - container - ); - }; - + let container = self.resolve(container, &hir_id); self.typeck_results.offset_of_data_mut().insert(hir_id, (container, indices.clone())); } } diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 0105a265ffbab..c385b00692ff4 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -481,10 +481,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { })))) } - ExprKind::OffsetOf { container, fields } => block.and(Rvalue::NullaryOp( - NullOp::OffsetOf(fields), - this.tcx.erase_regions(container), - )), + ExprKind::OffsetOf { container, fields } => { + block.and(Rvalue::NullaryOp(NullOp::OffsetOf(fields), container)) + } ExprKind::Literal { .. } | ExprKind::NamedConst { .. } diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index 0a9850146e5a3..aee9c89b59544 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -394,6 +394,8 @@ fn offset_of() { z: T } + trait Trait {} + // Ensure that this type of generics works fn offs_of_z() -> usize { offset_of!(Generic, z) @@ -401,6 +403,9 @@ fn offset_of() { assert_eq!(offset_of!(Generic, z), 8); assert_eq!(offs_of_z::(), 8); + + // Ensure that it works with the implicit lifetime in `Box`. + assert_eq!(offset_of!(Generic>, z), 8); } #[test] diff --git a/tests/ui/offset-of/offset-of-arg-count.rs b/tests/ui/offset-of/offset-of-arg-count.rs index 92a205f14d9d1..31de45bc7567f 100644 --- a/tests/ui/offset-of/offset-of-arg-count.rs +++ b/tests/ui/offset-of/offset-of-arg-count.rs @@ -13,7 +13,7 @@ fn main() { offset_of!(S, f..); //~ ERROR no rules expected the token offset_of!(S, f..,); //~ ERROR no rules expected the token offset_of!(Lt<'static>, bar); // issue #111657 - + offset_of!(Lt<'_>, bar); // issue #111678 } struct S { f: u8, } diff --git a/tests/ui/offset-of/offset-of-dst-field.rs b/tests/ui/offset-of/offset-of-dst-field.rs index 89e73b8c6b84d..3b8dc0b84a460 100644 --- a/tests/ui/offset-of/offset-of-dst-field.rs +++ b/tests/ui/offset-of/offset-of-dst-field.rs @@ -41,6 +41,7 @@ fn main() { fn delta() { offset_of!(Delta, z); //~ ERROR the size for values of type offset_of!(Delta, z); //~ ERROR the size for values of type + offset_of!(Delta, z); //~ ERROR the size for values of type } fn generic_with_maybe_sized() -> usize { diff --git a/tests/ui/offset-of/offset-of-dst-field.stderr b/tests/ui/offset-of/offset-of-dst-field.stderr index 992eab3d4bdf0..128c783d5dd2d 100644 --- a/tests/ui/offset-of/offset-of-dst-field.stderr +++ b/tests/ui/offset-of/offset-of-dst-field.stderr @@ -34,6 +34,15 @@ LL | offset_of!(Delta, z); = help: the trait `Sized` is not implemented for `Extern` = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time + --> $DIR/offset-of-dst-field.rs:44:5 + | +LL | offset_of!(Delta, z); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Trait` + = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the size for values of type `[u8]` cannot be known at compilation time --> $DIR/offset-of-dst-field.rs:42:5 | @@ -49,7 +58,7 @@ LL | struct Alpha { = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the size for values of type `T` cannot be known at compilation time - --> $DIR/offset-of-dst-field.rs:47:5 + --> $DIR/offset-of-dst-field.rs:48:5 | LL | fn generic_with_maybe_sized() -> usize { | - this type parameter needs to be `std::marker::Sized` @@ -63,6 +72,6 @@ LL - fn generic_with_maybe_sized() -> usize { LL + fn generic_with_maybe_sized() -> usize { | -error: aborting due to 6 previous errors +error: aborting due to 7 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/offset-of/offset-of-inference.rs b/tests/ui/offset-of/offset-of-inference.rs new file mode 100644 index 0000000000000..ba87574eae047 --- /dev/null +++ b/tests/ui/offset-of/offset-of-inference.rs @@ -0,0 +1,11 @@ +// Test that inference types in `offset_of!` don't ICE. + +#![feature(offset_of)] + +struct Foo { + x: T, +} + +fn main() { + let _ = core::mem::offset_of!(Foo<_>, x); //~ ERROR: type annotations needed +} diff --git a/tests/ui/offset-of/offset-of-inference.stderr b/tests/ui/offset-of/offset-of-inference.stderr new file mode 100644 index 0000000000000..2a520f6f90694 --- /dev/null +++ b/tests/ui/offset-of/offset-of-inference.stderr @@ -0,0 +1,9 @@ +error[E0282]: type annotations needed + --> $DIR/offset-of-inference.rs:10:35 + | +LL | let _ = core::mem::offset_of!(Foo<_>, x); + | ^^^^^^ cannot infer type + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`.