Skip to content

Commit

Permalink
instance: polymorphize FnDef substs
Browse files Browse the repository at this point in the history
This commit extends previous polymorphization of substs to polymorphize
`FnDef`.

Signed-off-by: David Wood <david@davidtw.co>
  • Loading branch information
davidtwco committed Aug 7, 2020
1 parent 4ccaf6f commit ac50d61
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/librustc_middle/ty/flags.rs
Expand Up @@ -196,6 +196,8 @@ impl FlagComputation {
}

&ty::FnDef(_, substs) => {
self.add_flags(TypeFlags::MAY_POLYMORPHIZE);

self.add_substs(substs);
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/fold.rs
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/librustc_middle/ty/instance.rs
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/mod.rs
Expand Up @@ -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;
}
Expand Down
23 changes: 19 additions & 4 deletions src/test/codegen-units/polymorphization/pr-75255.rs
Expand Up @@ -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]
Expand All @@ -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]

0 comments on commit ac50d61

Please sign in to comment.