From b92c2f792c33fbb180358997c5368f336f8912d2 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Sun, 16 Apr 2023 14:13:29 -0700 Subject: [PATCH] fix incorrect param env in dead code lint --- compiler/rustc_passes/src/dead.rs | 9 +++-- library/core/tests/mem.rs | 2 +- .../dead-code/offset-of-correct-param-env.rs | 40 +++++++++++++++++++ .../dead-code/offset-of.rs} | 0 .../dead-code/offset-of.stderr} | 0 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tests/ui/lint/dead-code/offset-of-correct-param-env.rs rename tests/ui/{liveness/liveness-offset-of.rs => lint/dead-code/offset-of.rs} (100%) rename tests/ui/{liveness/liveness-offset-of.stderr => lint/dead-code/offset-of.stderr} (100%) diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 82b55df106c..170b0b91e57 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -242,7 +242,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { let &(container, ref indices) = data.get(expr.hir_id).expect("no offset_of_data for offset_of"); - let mut last_did = expr.hir_id.owner.to_def_id(); + let body_did = self.typeck_results().hir_owner.to_def_id(); + let param_env = self.tcx.param_env(body_did); + let mut current_ty = container; for &index in indices { @@ -253,15 +255,14 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { self.insert_def_id(field.did); let field_ty = field.ty(self.tcx, subst); - last_did = field.did; current_ty = - self.tcx.normalize_erasing_regions(self.tcx.param_env(field.did), field_ty); + self.tcx.normalize_erasing_regions(param_env, field_ty); } // we don't need to mark tuple fields as live, // but we may need to mark subfields ty::Tuple(tys) => { current_ty = self.tcx.normalize_erasing_regions( - self.tcx.param_env(last_did), + param_env, tys[index.as_usize()], ); } diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index 2351406ddd2..b2a7df6b2e9 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -458,4 +458,4 @@ fn offset_of_addr() { assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, y), ptr::addr_of!(base.y).addr()); assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.0), ptr::addr_of!(base.z.0).addr()); assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.1), ptr::addr_of!(base.z.1).addr()); -} +} \ No newline at end of file diff --git a/tests/ui/lint/dead-code/offset-of-correct-param-env.rs b/tests/ui/lint/dead-code/offset-of-correct-param-env.rs new file mode 100644 index 00000000000..b7444049a88 --- /dev/null +++ b/tests/ui/lint/dead-code/offset-of-correct-param-env.rs @@ -0,0 +1,40 @@ +// check-pass + +#![feature(offset_of)] +#![deny(dead_code)] + +// This struct contains a projection that can only be normalized after getting the field type. +struct A { + a: ::EquateParamTo, +} + +// This is the inner struct that we want to get. +struct MyFieldIsNotDead { + not_dead: u8, +} + +// These are some helpers. +// Inside the param env of `test`, we want to make it so that it considers T=MyFieldIsNotDead. +struct GenericIsEqual(T); +trait Project { + type EquateParamTo; +} +impl Project for GenericIsEqual { + type EquateParamTo = T; +} + +fn test() -> usize +where + GenericIsEqual: Project, +{ + // The first field of the A that we construct here is `> as Project>::EquateParamTo`. + // Typeck normalizes this and figures that the not_dead field is totally fine and accessible. + // But importantly, the normalization ends up with T, which, as we've declared in our param env is MyFieldDead. + // When we're in the param env of the `a` field, the where bound above is not in scope, so we don't know what T is - it's generic. + // We cannot access a field on T. Boom! + std::mem::offset_of!(A>, a.not_dead) +} + +fn main() { + test::(); +} \ No newline at end of file diff --git a/tests/ui/liveness/liveness-offset-of.rs b/tests/ui/lint/dead-code/offset-of.rs similarity index 100% rename from tests/ui/liveness/liveness-offset-of.rs rename to tests/ui/lint/dead-code/offset-of.rs diff --git a/tests/ui/liveness/liveness-offset-of.stderr b/tests/ui/lint/dead-code/offset-of.stderr similarity index 100% rename from tests/ui/liveness/liveness-offset-of.stderr rename to tests/ui/lint/dead-code/offset-of.stderr