diff options
| author | Michael Goulet <michael@errs.io> | 2025-01-08 00:46:03 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2025-01-14 18:47:23 +0000 |
| commit | 3cd75812c818c5e7855f1be8f6a754c9f7a9f0f8 (patch) | |
| tree | d6b249747e31a25e7041aa2e74d0519d9483ff0e | |
| parent | 11bc805369b7e2aa641e885c137b2c86ce701e2c (diff) | |
| download | rust-3cd75812c818c5e7855f1be8f6a754c9f7a9f0f8.tar.gz rust-3cd75812c818c5e7855f1be8f6a754c9f7a9f0f8.zip | |
Normalize field before checking PhantomData in coerce/dispatch impl validation
| -rw-r--r-- | compiler/rustc_hir_analysis/src/coherence/builtin.rs | 23 | ||||
| -rw-r--r-- | tests/ui/self/phantomdata-in-coerce-and-dispatch-impls.rs | 25 |
2 files changed, 45 insertions, 3 deletions
diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs index a661e588b95..b43a808ccdc 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs @@ -260,13 +260,22 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<() .iter() .filter(|field| { // Ignore PhantomData fields - if tcx.type_of(field.did).instantiate_identity().is_phantom_data() { + let unnormalized_ty = tcx.type_of(field.did).instantiate_identity(); + if tcx + .try_normalize_erasing_regions( + ty::TypingEnv::non_body_analysis(tcx, def_a.did()), + unnormalized_ty, + ) + .unwrap_or(unnormalized_ty) + .is_phantom_data() + { return false; } let ty_a = field.ty(tcx, args_a); let ty_b = field.ty(tcx, args_b); + // FIXME: We could do normalization here, but is it really worth it? if ty_a == ty_b { // Allow 1-ZSTs that don't mention type params. // @@ -469,8 +478,16 @@ pub(crate) fn coerce_unsized_info<'tcx>( .filter_map(|(i, f)| { let (a, b) = (f.ty(tcx, args_a), f.ty(tcx, args_b)); - if tcx.type_of(f.did).instantiate_identity().is_phantom_data() { - // Ignore PhantomData fields + // Ignore PhantomData fields + let unnormalized_ty = tcx.type_of(f.did).instantiate_identity(); + if tcx + .try_normalize_erasing_regions( + ty::TypingEnv::non_body_analysis(tcx, def_a.did()), + unnormalized_ty, + ) + .unwrap_or(unnormalized_ty) + .is_phantom_data() + { return None; } diff --git a/tests/ui/self/phantomdata-in-coerce-and-dispatch-impls.rs b/tests/ui/self/phantomdata-in-coerce-and-dispatch-impls.rs new file mode 100644 index 00000000000..9c7e33830f5 --- /dev/null +++ b/tests/ui/self/phantomdata-in-coerce-and-dispatch-impls.rs @@ -0,0 +1,25 @@ +//@ check-pass + +#![feature(coerce_unsized, dispatch_from_dyn, unsize)] + +use std::marker::Unsize; +use std::ops::{CoerceUnsized, DispatchFromDyn}; +use std::marker::PhantomData; + +trait Mirror { + type Assoc; +} +impl<T> Mirror for T { + type Assoc = T; +} + +struct W<T: 'static> { + t: &'static T, + f: <PhantomData<T> as Mirror>::Assoc, +} + +impl<T, U> CoerceUnsized<W<U>> for W<T> where T: Unsize<U> {} + +impl<T, U> DispatchFromDyn<W<U>> for W<T> where T: Unsize<U> {} + +fn main() {} |
