about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-04-14 14:37:34 +0000
committerbors <bors@rust-lang.org>2022-04-14 14:37:34 +0000
commite7575f9670f3c837def3d186ae09366c75c7632e (patch)
tree088b32e82cc36e2f3a55abf6b05554335d8075a4
parente371eeb778c293cc85ce396cec5d6372fe1ef8b7 (diff)
parent3624f1ceaf66af9a1f325eaaeac6866a47302efd (diff)
downloadrust-e7575f9670f3c837def3d186ae09366c75c7632e.tar.gz
rust-e7575f9670f3c837def3d186ae09366c75c7632e.zip
Auto merge of #95315 - compiler-errors:pointee-fix, r=pnkfelix
when checking pointee metadata, canonicalize the `Sized` check

Use `infcx.predicate_must_hold_modulo_regions` with a `Sized` obligation instead of just calling `ty.is_sized`, because the latter does not canonicalize region and type vars (and in the test case I added in this PR, there's a region var in the `ParamEnv`).

Fixes #95311
-rw-r--r--compiler/rustc_trait_selection/src/traits/project.rs12
-rw-r--r--src/test/ui/traits/issue-95311.rs19
2 files changed, 30 insertions, 1 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs
index 4734a843a9c..8ba390c71db 100644
--- a/compiler/rustc_trait_selection/src/traits/project.rs
+++ b/compiler/rustc_trait_selection/src/traits/project.rs
@@ -19,6 +19,7 @@ use super::{Normalized, NormalizedTy, ProjectionCacheEntry, ProjectionCacheKey};
 use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
 use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
 use crate::traits::error_reporting::InferCtxtExt as _;
+use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
 use crate::traits::select::ProjectionMatchesProjection;
 use rustc_data_structures::sso::SsoHashSet;
 use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -1562,7 +1563,16 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
                     // type parameters, opaques, and unnormalized projections have pointer
                     // metadata if they're known (e.g. by the param_env) to be sized
                     ty::Param(_) | ty::Projection(..) | ty::Opaque(..)
-                        if tail.is_sized(selcx.tcx().at(obligation.cause.span), obligation.param_env) =>
+                        if selcx.infcx().predicate_must_hold_modulo_regions(
+                            &obligation.with(
+                                ty::Binder::dummy(ty::TraitRef::new(
+                                    selcx.tcx().require_lang_item(LangItem::Sized, None),
+                                    selcx.tcx().mk_substs_trait(self_ty, &[]),
+                                ))
+                                .without_const()
+                                .to_predicate(selcx.tcx()),
+                            ),
+                        ) =>
                     {
                         true
                     }
diff --git a/src/test/ui/traits/issue-95311.rs b/src/test/ui/traits/issue-95311.rs
new file mode 100644
index 00000000000..9d40d254ad9
--- /dev/null
+++ b/src/test/ui/traits/issue-95311.rs
@@ -0,0 +1,19 @@
+// check-pass
+
+// Test to check that pointee trait doesn't let region variables escape into the cache
+
+#![feature(ptr_metadata)]
+
+trait Bar: Sized + 'static {}
+
+struct Foo<B: Bar> {
+    marker: std::marker::PhantomData<B>,
+}
+
+impl<B: Bar> Foo<B> {
+    fn foo<T: ?Sized>(value: &T) {
+        std::ptr::metadata(value);
+    }
+}
+
+fn main() {}