1
Fork 0

Remove map_layouts.

As per the `FIXME` comment, it's an abstraction that makes the code
harder to read.
This commit is contained in:
Nicholas Nethercote 2023-10-06 11:20:02 +11:00
parent 409193654d
commit 449b84cb99

View file

@ -32,26 +32,6 @@ where
) -> Self { ) -> Self {
Self { src, dst, scope, assume, context } Self { src, dst, scope, assume, context }
} }
// FIXME(bryangarza): Delete this when all usages are removed
pub(crate) fn map_layouts<F, M>(
self,
f: F,
) -> Result<MaybeTransmutableQuery<M, C>, Answer<<C as QueryContext>::Ref>>
where
F: FnOnce(
L,
L,
<C as QueryContext>::Scope,
&C,
) -> Result<(M, M), Answer<<C as QueryContext>::Ref>>,
{
let Self { src, dst, scope, assume, context } = self;
let (src, dst) = f(src, dst, scope, &context)?;
Ok(MaybeTransmutableQuery { src, dst, scope, assume, context })
}
} }
// FIXME: Nix this cfg, so we can write unit tests independently of rustc // FIXME: Nix this cfg, so we can write unit tests independently of rustc
@ -107,42 +87,42 @@ where
#[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))] #[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))]
pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Ref> { pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Ref> {
let assume_visibility = self.assume.safety; let assume_visibility = self.assume.safety;
// FIXME(bryangarza): Refactor this code to get rid of `map_layouts`
let query_or_answer = self.map_layouts(|src, dst, scope, context| {
// Remove all `Def` nodes from `src`, without checking their visibility.
let src = src.prune(&|def| true);
trace!(?src, "pruned src"); let Self { src, dst, scope, assume, context } = self;
// Remove all `Def` nodes from `dst`, additionally... // Remove all `Def` nodes from `src`, without checking their visibility.
let dst = if assume_visibility { let src = src.prune(&|def| true);
// ...if visibility is assumed, don't check their visibility.
dst.prune(&|def| true)
} else {
// ...otherwise, prune away all unreachable paths through the `Dst` layout.
dst.prune(&|def| context.is_accessible_from(def, scope))
};
trace!(?dst, "pruned dst"); trace!(?src, "pruned src");
// Convert `src` from a tree-based representation to an NFA-based representation. // Remove all `Def` nodes from `dst`, additionally...
// If the conversion fails because `src` is uninhabited, conclude that the transmutation let dst = if assume_visibility {
// is acceptable, because instances of the `src` type do not exist. // ...if visibility is assumed, don't check their visibility.
let src = Nfa::from_tree(src).map_err(|Uninhabited| Answer::Yes)?; dst.prune(&|def| true)
} else {
// ...otherwise, prune away all unreachable paths through the `Dst` layout.
dst.prune(&|def| context.is_accessible_from(def, scope))
};
// Convert `dst` from a tree-based representation to an NFA-based representation. trace!(?dst, "pruned dst");
// If the conversion fails because `src` is uninhabited, conclude that the transmutation
// is unacceptable, because instances of the `dst` type do not exist.
let dst =
Nfa::from_tree(dst).map_err(|Uninhabited| Answer::No(Reason::DstIsPrivate))?;
Ok((src, dst)) // Convert `src` from a tree-based representation to an NFA-based representation.
}); // If the conversion fails because `src` is uninhabited, conclude that the transmutation
// is acceptable, because instances of the `src` type do not exist.
let src = match Nfa::from_tree(src) {
Ok(src) => src,
Err(Uninhabited) => return Answer::Yes,
};
match query_or_answer { // Convert `dst` from a tree-based representation to an NFA-based representation.
Ok(query) => query.answer(), // If the conversion fails because `src` is uninhabited, conclude that the transmutation
Err(answer) => answer, // is unacceptable, because instances of the `dst` type do not exist.
} let dst = match Nfa::from_tree(dst) {
Ok(dst) => dst,
Err(Uninhabited) => return Answer::No(Reason::DstIsPrivate),
};
MaybeTransmutableQuery { src, dst, scope, assume, context }.answer()
} }
} }
@ -156,14 +136,10 @@ where
#[inline(always)] #[inline(always)]
#[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))] #[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))]
pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Ref> { pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Ref> {
// FIXME(bryangarza): Refactor this code to get rid of `map_layouts` let Self { src, dst, scope, assume, context } = self;
let query_or_answer = self let src = Dfa::from_nfa(src);
.map_layouts(|src, dst, scope, context| Ok((Dfa::from_nfa(src), Dfa::from_nfa(dst)))); let dst = Dfa::from_nfa(dst);
MaybeTransmutableQuery { src, dst, scope, assume, context }.answer()
match query_or_answer {
Ok(query) => query.answer(),
Err(answer) => answer,
}
} }
} }