instance: polymorphize FnDef substs

This commit extends previous polymorphization of substs to polymorphize
`FnDef`.

Signed-off-by: David Wood <david@davidtw.co>
This commit is contained in:
David Wood 2020-08-07 17:57:21 +01:00
parent 4ccaf6f38c
commit ac50d61785
No known key found for this signature in database
GPG Key ID: 2592E76C87381FD9
5 changed files with 31 additions and 6 deletions

View File

@ -196,6 +196,8 @@ impl FlagComputation {
}
&ty::FnDef(_, substs) => {
self.add_flags(TypeFlags::MAY_POLYMORPHIZE);
self.add_substs(substs);
}

View File

@ -150,7 +150,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
}
/// Does this value contain closures or generators such that it may require
/// Does this value contain closures, generators or functions such that it may require
/// polymorphization?
fn may_polymorphize(&self) -> bool {
self.has_type_flags(TypeFlags::MAY_POLYMORPHIZE)

View File

@ -512,6 +512,14 @@ fn polymorphize<'tcx>(
self.tcx.mk_closure(def_id, polymorphized_substs)
}
}
ty::FnDef(def_id, substs) => {
let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
if substs == polymorphized_substs {
ty
} else {
self.tcx.mk_fn_def(def_id, polymorphized_substs)
}
}
ty::Generator(def_id, substs, movability) => {
let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
if substs == polymorphized_substs {

View File

@ -576,7 +576,7 @@ bitflags! {
/// replaced later, in a way that would change the results of `impl` specialization?
const STILL_FURTHER_SPECIALIZABLE = 1 << 17;
/// Does this value contain closures or generators such that it may require
/// Does this value contain closures, generators or functions such that it may require
/// polymorphization?
const MAY_POLYMORPHIZE = 1 << 18;
}

View File

@ -3,20 +3,33 @@
#![crate_type = "rlib"]
// Test that only one copy of `Iter::map` is generated.
// Test that only one copy of `Iter::map` and `iter::repeat` are generated.
fn unused<T>() -> u64 {
42
}
fn foo<T>() {
let x = [1, 2, 3, std::mem::size_of::<T>()];
x.iter().map(|_| ());
}
//~ MONO_ITEM fn core::iter[0]::adapters[0]::{{impl}}[29]::new[0]<core::slice[0]::Iter[0]<usize>, pr_75255::foo[0]::{{closure}}[0]<T>> @@ pr_75255-cgu.0[External]
//~ MONO_ITEM fn core::iter[0]::traits[0]::iterator[0]::Iterator[0]::map[0]<core::slice[0]::Iter[0]<usize>, (), pr_75255::foo[0]::{{closure}}[0]<T>> @@ pr_75255-cgu.1[Internal]
fn bar<T>() {
std::iter::repeat(unused::<T>);
}
//~ MONO_ITEM fn core::iter[0]::sources[0]::repeat[0]<fn() -> u64> @@ pr_75255-cgu.1[Internal]
pub fn dispatch() {
foo::<String>();
foo::<Vec<String>>();
}
//~ MONO_ITEM fn core::iter[0]::adapters[0]::{{impl}}[29]::new[0]<core::slice[0]::Iter[0]<usize>, pr_75255::foo[0]::{{closure}}[0]<T>> @@ pr_75255-cgu.0[External]
//~ MONO_ITEM fn core::iter[0]::traits[0]::iterator[0]::Iterator[0]::map[0]<core::slice[0]::Iter[0]<usize>, (), pr_75255::foo[0]::{{closure}}[0]<T>> @@ pr_75255-cgu.1[Internal]
bar::<String>();
bar::<Vec<String>>();
}
// These are all the items that aren't relevant to the test.
//~ MONO_ITEM fn core::mem[0]::size_of[0]<alloc::string[0]::String[0]> @@ pr_75255-cgu.1[Internal]
@ -35,3 +48,5 @@ pub fn dispatch() {
//~ MONO_ITEM fn pr_75255::dispatch[0] @@ pr_75255-cgu.1[External]
//~ MONO_ITEM fn pr_75255::foo[0]<alloc::string[0]::String[0]> @@ pr_75255-cgu.1[Internal]
//~ MONO_ITEM fn pr_75255::foo[0]<alloc::vec[0]::Vec[0]<alloc::string[0]::String[0]>> @@ pr_75255-cgu.1[Internal]
//~ MONO_ITEM fn pr_75255::bar[0]<alloc::string[0]::String[0]> @@ pr_75255-cgu.1[Internal]
//~ MONO_ITEM fn pr_75255::bar[0]<alloc::vec[0]::Vec[0]<alloc::string[0]::String[0]>> @@ pr_75255-cgu.1[Internal]