Test and implement reachability for trait objects and generic parameters of functions
This commit is contained in:
parent
8332b47cae
commit
746e4eff26
@ -4400,6 +4400,7 @@ dependencies = [
|
||||
"rustc_lexer",
|
||||
"rustc_macros",
|
||||
"rustc_middle",
|
||||
"rustc_privacy",
|
||||
"rustc_session",
|
||||
"rustc_span",
|
||||
"rustc_target",
|
||||
|
@ -18,6 +18,7 @@ rustc_index = { path = "../rustc_index" }
|
||||
rustc_lexer = { path = "../rustc_lexer" }
|
||||
rustc_macros = { path = "../rustc_macros" }
|
||||
rustc_middle = { path = "../rustc_middle" }
|
||||
rustc_privacy = { path = "../rustc_privacy" }
|
||||
rustc_session = { path = "../rustc_session" }
|
||||
rustc_span = { path = "../rustc_span" }
|
||||
rustc_target = { path = "../rustc_target" }
|
||||
|
@ -16,7 +16,8 @@
|
||||
use rustc_middle::middle::privacy::{self, Level};
|
||||
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc};
|
||||
use rustc_middle::query::Providers;
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
use rustc_middle::ty::{self, ExistentialTraitRef, TyCtxt};
|
||||
use rustc_privacy::DefIdVisitor;
|
||||
use rustc_session::config::CrateType;
|
||||
use rustc_target::spec::abi::Abi;
|
||||
|
||||
@ -272,13 +273,22 @@ fn propagate_from_alloc(&mut self, alloc: ConstAllocation<'tcx>) {
|
||||
self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
|
||||
}
|
||||
GlobalAlloc::Function(instance) => {
|
||||
// Manually visit to actually see the instance's `DefId`. Type visitors won't see it
|
||||
self.propagate_item(Res::Def(
|
||||
self.tcx.def_kind(instance.def_id()),
|
||||
instance.def_id(),
|
||||
))
|
||||
// TODO: walk generic args
|
||||
));
|
||||
self.visit(instance.args);
|
||||
}
|
||||
GlobalAlloc::VTable(ty, trait_ref) => {
|
||||
self.visit(ty);
|
||||
// Manually visit to actually see the trait's `DefId`. Type visitors won't see it
|
||||
if let Some(trait_ref) = trait_ref {
|
||||
let ExistentialTraitRef { def_id, args } = trait_ref.skip_binder();
|
||||
self.visit_def_id(def_id, "", &"");
|
||||
self.visit(args);
|
||||
}
|
||||
}
|
||||
GlobalAlloc::VTable(ty, trait_ref) => todo!("{ty:?}, {trait_ref:?}"),
|
||||
GlobalAlloc::Memory(alloc) => self.propagate_from_alloc(alloc),
|
||||
}
|
||||
}
|
||||
@ -318,6 +328,23 @@ fn propagate_item(&mut self, res: Res) {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> DefIdVisitor<'tcx> for ReachableContext<'tcx> {
|
||||
type Result = ();
|
||||
|
||||
fn tcx(&self) -> TyCtxt<'tcx> {
|
||||
self.tcx
|
||||
}
|
||||
|
||||
fn visit_def_id(
|
||||
&mut self,
|
||||
def_id: DefId,
|
||||
_kind: &str,
|
||||
_descr: &dyn std::fmt::Display,
|
||||
) -> Self::Result {
|
||||
self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
|
||||
}
|
||||
}
|
||||
|
||||
fn check_item<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
id: hir::ItemId,
|
||||
|
@ -67,7 +67,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
/// First, it doesn't have overridable `fn visit_trait_ref`, so we have to catch trait `DefId`s
|
||||
/// manually. Second, it doesn't visit some type components like signatures of fn types, or traits
|
||||
/// in `impl Trait`, see individual comments in `DefIdVisitorSkeleton::visit_ty`.
|
||||
trait DefIdVisitor<'tcx> {
|
||||
pub trait DefIdVisitor<'tcx> {
|
||||
type Result: VisitorResult = ();
|
||||
const SHALLOW: bool = false;
|
||||
const SKIP_ASSOC_TYS: bool = false;
|
||||
@ -98,7 +98,7 @@ fn visit_clauses(&mut self, clauses: &[(ty::Clause<'tcx>, Span)]) -> Self::Resul
|
||||
}
|
||||
}
|
||||
|
||||
struct DefIdVisitorSkeleton<'v, 'tcx, V: ?Sized> {
|
||||
pub struct DefIdVisitorSkeleton<'v, 'tcx, V: ?Sized> {
|
||||
def_id_visitor: &'v mut V,
|
||||
visited_opaque_tys: FxHashSet<DefId>,
|
||||
dummy: PhantomData<TyCtxt<'tcx>>,
|
||||
|
@ -1,6 +1,8 @@
|
||||
pub static V: &u32 = &X;
|
||||
pub static F: fn() = f;
|
||||
pub static G: fn() = G0;
|
||||
pub static H: &(dyn Fn() + Sync) = &h;
|
||||
pub static I: fn() = Helper(j).mk();
|
||||
|
||||
static X: u32 = 42;
|
||||
static G0: fn() = g;
|
||||
@ -12,3 +14,22 @@ pub fn v() -> *const u32 {
|
||||
fn f() {}
|
||||
|
||||
fn g() {}
|
||||
|
||||
fn h() {}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Helper<T: Copy>(T);
|
||||
|
||||
impl<T: Copy + FnOnce()> Helper<T> {
|
||||
const fn mk(self) -> fn() {
|
||||
i::<T>
|
||||
}
|
||||
}
|
||||
|
||||
fn i<T: FnOnce()>() {
|
||||
assert_eq!(std::mem::size_of::<T>(), 0);
|
||||
// unsafe to work around the lack of a `Default` impl for function items
|
||||
unsafe { (std::mem::transmute_copy::<(), T>(&()))() }
|
||||
}
|
||||
|
||||
fn j() {}
|
||||
|
@ -6,6 +6,8 @@
|
||||
static V: &u32 = aux::V;
|
||||
static F: fn() = aux::F;
|
||||
static G: fn() = aux::G;
|
||||
static H: &(dyn Fn() + Sync) = aux::H;
|
||||
static I: fn() = aux::I;
|
||||
|
||||
fn v() -> *const u32 {
|
||||
V
|
||||
@ -15,4 +17,6 @@ fn main() {
|
||||
assert_eq!(aux::v(), crate::v());
|
||||
F();
|
||||
G();
|
||||
H();
|
||||
I();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user