Skip to content

Commit ac50d61

Browse files
committed
instance: polymorphize FnDef substs
This commit extends previous polymorphization of substs to polymorphize `FnDef`. Signed-off-by: David Wood <[email protected]>
1 parent 4ccaf6f commit ac50d61

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

src/librustc_middle/ty/flags.rs

+2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ impl FlagComputation {
196196
}
197197

198198
&ty::FnDef(_, substs) => {
199+
self.add_flags(TypeFlags::MAY_POLYMORPHIZE);
200+
199201
self.add_substs(substs);
200202
}
201203

src/librustc_middle/ty/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
150150
self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
151151
}
152152

153-
/// Does this value contain closures or generators such that it may require
153+
/// Does this value contain closures, generators or functions such that it may require
154154
/// polymorphization?
155155
fn may_polymorphize(&self) -> bool {
156156
self.has_type_flags(TypeFlags::MAY_POLYMORPHIZE)

src/librustc_middle/ty/instance.rs

+8
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,14 @@ fn polymorphize<'tcx>(
512512
self.tcx.mk_closure(def_id, polymorphized_substs)
513513
}
514514
}
515+
ty::FnDef(def_id, substs) => {
516+
let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
517+
if substs == polymorphized_substs {
518+
ty
519+
} else {
520+
self.tcx.mk_fn_def(def_id, polymorphized_substs)
521+
}
522+
}
515523
ty::Generator(def_id, substs, movability) => {
516524
let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
517525
if substs == polymorphized_substs {

src/librustc_middle/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ bitflags! {
576576
/// replaced later, in a way that would change the results of `impl` specialization?
577577
const STILL_FURTHER_SPECIALIZABLE = 1 << 17;
578578

579-
/// Does this value contain closures or generators such that it may require
579+
/// Does this value contain closures, generators or functions such that it may require
580580
/// polymorphization?
581581
const MAY_POLYMORPHIZE = 1 << 18;
582582
}

src/test/codegen-units/polymorphization/pr-75255.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,33 @@
33

44
#![crate_type = "rlib"]
55

6-
// Test that only one copy of `Iter::map` is generated.
6+
// Test that only one copy of `Iter::map` and `iter::repeat` are generated.
7+
8+
fn unused<T>() -> u64 {
9+
42
10+
}
711

812
fn foo<T>() {
913
let x = [1, 2, 3, std::mem::size_of::<T>()];
1014
x.iter().map(|_| ());
1115
}
1216

17+
//~ 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]
18+
//~ 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]
19+
20+
fn bar<T>() {
21+
std::iter::repeat(unused::<T>);
22+
}
23+
24+
//~ MONO_ITEM fn core::iter[0]::sources[0]::repeat[0]<fn() -> u64> @@ pr_75255-cgu.1[Internal]
25+
1326
pub fn dispatch() {
1427
foo::<String>();
1528
foo::<Vec<String>>();
16-
}
1729

18-
//~ 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]
19-
//~ 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]
30+
bar::<String>();
31+
bar::<Vec<String>>();
32+
}
2033

2134
// These are all the items that aren't relevant to the test.
2235
//~ 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() {
3548
//~ MONO_ITEM fn pr_75255::dispatch[0] @@ pr_75255-cgu.1[External]
3649
//~ MONO_ITEM fn pr_75255::foo[0]<alloc::string[0]::String[0]> @@ pr_75255-cgu.1[Internal]
3750
//~ MONO_ITEM fn pr_75255::foo[0]<alloc::vec[0]::Vec[0]<alloc::string[0]::String[0]>> @@ pr_75255-cgu.1[Internal]
51+
//~ MONO_ITEM fn pr_75255::bar[0]<alloc::string[0]::String[0]> @@ pr_75255-cgu.1[Internal]
52+
//~ MONO_ITEM fn pr_75255::bar[0]<alloc::vec[0]::Vec[0]<alloc::string[0]::String[0]>> @@ pr_75255-cgu.1[Internal]

0 commit comments

Comments
 (0)