-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
safely transmute<&List<Ty<'tcx>>, &List<GenericArg<'tcx>>>
#93505
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9c1ab8
safely `transmute<&List<Ty<'tcx>>, &List<GenericArg<'tcx>>>`
lcnr 1245131
use `List<Ty<'tcx>>` for tuples
lcnr 880343c
update clippy
lcnr 7d5d6c0
update rustdoc
lcnr 758f4e7
optimize `TypeFoldable` for 2 element tuples
lcnr c909b6d
add comment to `Lift` impls
lcnr 80f56cd
review
lcnr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,7 +101,6 @@ pub struct CtxtInterners<'tcx> { | |
// Specifically use a speedy hash algorithm for these hash sets, since | ||
// they're accessed quite often. | ||
type_: InternedSet<'tcx, TyS<'tcx>>, | ||
type_list: InternedSet<'tcx, List<Ty<'tcx>>>, | ||
substs: InternedSet<'tcx, InternalSubsts<'tcx>>, | ||
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>, | ||
region: InternedSet<'tcx, RegionKind>, | ||
|
@@ -129,7 +128,6 @@ impl<'tcx> CtxtInterners<'tcx> { | |
CtxtInterners { | ||
arena, | ||
type_: Default::default(), | ||
type_list: Default::default(), | ||
substs: Default::default(), | ||
region: Default::default(), | ||
poly_existential_predicates: Default::default(), | ||
|
@@ -1657,6 +1655,8 @@ macro_rules! nop_lift { | |
type Lifted = $lifted; | ||
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> { | ||
if tcx.interners.$set.contains_pointer_to(&InternedInSet(self.0.0)) { | ||
// SAFETY: `self` is interned and therefore valid | ||
// for the entire lifetime of the `TyCtxt`. | ||
Some(unsafe { mem::transmute(self) }) | ||
} else { | ||
None | ||
|
@@ -1666,6 +1666,25 @@ macro_rules! nop_lift { | |
}; | ||
} | ||
|
||
// Can't use the macros as we have reuse the `substs` here. | ||
// | ||
// See `intern_type_list` for more info. | ||
impl<'a, 'tcx> Lift<'tcx> for &'a List<Ty<'a>> { | ||
type Lifted = &'tcx List<Ty<'tcx>>; | ||
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> { | ||
if self.is_empty() { | ||
return Some(List::empty()); | ||
} | ||
if tcx.interners.substs.contains_pointer_to(&InternedInSet(self.as_substs())) { | ||
// SAFETY: `self` is interned and therefore valid | ||
// for the entire lifetime of the `TyCtxt`. | ||
Some(unsafe { mem::transmute::<&'a List<Ty<'a>>, &'tcx List<Ty<'tcx>>>(self) }) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
macro_rules! nop_list_lift { | ||
($set:ident; $ty:ty => $lifted:ty) => { | ||
impl<'a, 'tcx> Lift<'tcx> for &'a List<$ty> { | ||
|
@@ -1690,7 +1709,6 @@ nop_lift! {const_; Const<'a> => Const<'tcx>} | |
nop_lift_old! {const_allocation; &'a Allocation => &'tcx Allocation} | ||
nop_lift! {predicate; Predicate<'a> => Predicate<'tcx>} | ||
|
||
nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>} | ||
nop_list_lift! {poly_existential_predicates; ty::Binder<'a, ExistentialPredicate<'a>> => ty::Binder<'tcx, ExistentialPredicate<'tcx>>} | ||
nop_list_lift! {predicates; Predicate<'a> => Predicate<'tcx>} | ||
nop_list_lift! {canonical_var_infos; CanonicalVarInfo<'a> => CanonicalVarInfo<'tcx>} | ||
|
@@ -2189,7 +2207,6 @@ macro_rules! slice_interners { | |
} | ||
|
||
slice_interners!( | ||
type_list: _intern_type_list(Ty<'tcx>), | ||
substs: _intern_substs(GenericArg<'tcx>), | ||
canonical_var_infos: _intern_canonical_var_infos(CanonicalVarInfo<'tcx>), | ||
poly_existential_predicates: | ||
|
@@ -2259,7 +2276,7 @@ impl<'tcx> TyCtxt<'tcx> { | |
) -> PolyFnSig<'tcx> { | ||
sig.map_bound(|s| { | ||
let params_iter = match s.inputs()[0].kind() { | ||
ty::Tuple(params) => params.into_iter().map(|k| k.expect_ty()), | ||
ty::Tuple(params) => params.into_iter(), | ||
_ => bug!(), | ||
}; | ||
self.mk_fn_sig(params_iter, s.output(), s.c_variadic, unsafety, abi::Abi::Rust) | ||
|
@@ -2421,15 +2438,11 @@ impl<'tcx> TyCtxt<'tcx> { | |
|
||
#[inline] | ||
pub fn intern_tup(self, ts: &[Ty<'tcx>]) -> Ty<'tcx> { | ||
let kinds: Vec<_> = ts.iter().map(|&t| GenericArg::from(t)).collect(); | ||
self.mk_ty(Tuple(self.intern_substs(&kinds))) | ||
self.mk_ty(Tuple(self.intern_type_list(&ts))) | ||
} | ||
|
||
pub fn mk_tup<I: InternAs<[Ty<'tcx>], Ty<'tcx>>>(self, iter: I) -> I::Output { | ||
iter.intern_with(|ts| { | ||
let kinds: Vec<_> = ts.iter().map(|&t| GenericArg::from(t)).collect(); | ||
self.mk_ty(Tuple(self.intern_substs(&kinds))) | ||
}) | ||
iter.intern_with(|ts| self.mk_ty(Tuple(self.intern_type_list(&ts)))) | ||
} | ||
|
||
#[inline] | ||
|
@@ -2611,7 +2624,19 @@ impl<'tcx> TyCtxt<'tcx> { | |
} | ||
|
||
pub fn intern_type_list(self, ts: &[Ty<'tcx>]) -> &'tcx List<Ty<'tcx>> { | ||
if ts.is_empty() { List::empty() } else { self._intern_type_list(ts) } | ||
if ts.is_empty() { | ||
List::empty() | ||
} else { | ||
// Actually intern type lists as lists of `GenericArg`s. | ||
// | ||
// Transmuting from `Ty<'tcx>` to `GenericArg<'tcx>` is sound | ||
// as explained in ty_slice_as_generic_arg`. With this, | ||
// we guarantee that even when transmuting between `List<Ty<'tcx>>` | ||
// and `List<GenericArg<'tcx>>`, the uniqueness requirement for | ||
// lists is upheld. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heh, this is...a cute pun. |
||
let substs = self._intern_substs(ty::subst::ty_slice_as_generic_args(ts)); | ||
substs.try_as_type_list().unwrap() | ||
} | ||
} | ||
|
||
pub fn intern_substs(self, ts: &[GenericArg<'tcx>]) -> &'tcx List<GenericArg<'tcx>> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the transmute inside it, in instances such as this one, I personally like to spell out the aliases explicitly:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i generally agree, though, considering that all other
Lift
impls also don't do that i am going to keep the code as is for now.