Skip to content

Commit 2eefb88

Browse files
committed
WIP3
1 parent a49198e commit 2eefb88

File tree

6 files changed

+111
-91
lines changed

6 files changed

+111
-91
lines changed

compiler/rustc_target/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1313
#![doc(rust_logo)]
1414
#![feature(assert_matches)]
15+
#![feature(fn_traits)]
1516
#![feature(iter_intersperse)]
1617
#![feature(let_chains)]
1718
#![feature(min_exhaustive_patterns)]
1819
#![feature(rustc_attrs)]
1920
#![feature(rustdoc_internals)]
21+
#![feature(unboxed_closures)]
2022
// tidy-alphabetical-end
2123

2224
use std::path::{Path, PathBuf};

compiler/rustc_target/src/spec/base/aix.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::abi::Endian;
22
use crate::spec::{crt_objects, cvs, Cc, CodeModel, LinkOutputKind, LinkerFlavor};
3-
use crate::spec::{MaybeLazy, TargetOptions};
3+
use crate::spec::TargetOptions;
44

55
pub fn opts() -> TargetOptions {
66
TargetOptions {
@@ -23,12 +23,10 @@ pub fn opts() -> TargetOptions {
2323
is_like_aix: true,
2424
default_dwarf_version: 3,
2525
function_sections: true,
26-
pre_link_objects: MaybeLazy::lazy(|| {
27-
crt_objects::new(&[
28-
(LinkOutputKind::DynamicNoPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
29-
(LinkOutputKind::DynamicPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
30-
])
31-
}),
26+
pre_link_objects: crt_objects::new(&[
27+
(LinkOutputKind::DynamicNoPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
28+
(LinkOutputKind::DynamicPicExe, &["/usr/lib/crt0_64.o", "/usr/lib/crti_64.o"]),
29+
]),
3230
dll_suffix: ".a".into(),
3331
..Default::default()
3432
}

compiler/rustc_target/src/spec/base/fuchsia.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ pub fn opts() -> TargetOptions {
3434
dynamic_linking: true,
3535
families: cvs!["unix"],
3636
pre_link_args,
37-
pre_link_objects: MaybeLazy::lazy(|| {
38-
crt_objects::new(&[
39-
(LinkOutputKind::DynamicNoPicExe, &["Scrt1.o"]),
40-
(LinkOutputKind::DynamicPicExe, &["Scrt1.o"]),
41-
(LinkOutputKind::StaticNoPicExe, &["Scrt1.o"]),
42-
(LinkOutputKind::StaticPicExe, &["Scrt1.o"]),
43-
])
44-
}),
37+
pre_link_objects: crt_objects::new(&[
38+
(LinkOutputKind::DynamicNoPicExe, &["Scrt1.o"]),
39+
(LinkOutputKind::DynamicPicExe, &["Scrt1.o"]),
40+
(LinkOutputKind::StaticNoPicExe, &["Scrt1.o"]),
41+
(LinkOutputKind::StaticPicExe, &["Scrt1.o"]),
42+
]),
4543
position_independent_executables: true,
4644
has_thread_local: true,
4745
frame_pointer: FramePointer::NonLeaf,

compiler/rustc_target/src/spec/crt_objects.rs

+59-55
Original file line numberDiff line numberDiff line change
@@ -44,89 +44,93 @@ use crate::spec::{LinkOutputKind, MaybeLazy};
4444
use std::borrow::Cow;
4545
use std::collections::BTreeMap;
4646

47-
pub type CrtObjectsData = BTreeMap<LinkOutputKind, Vec<Cow<'static, str>>>;
48-
pub type CrtObjects = MaybeLazy<CrtObjectsData>;
47+
type Tralala = &'static [(LinkOutputKind, &'static [&'static str])];
48+
pub struct Trulala(Tralala);
4949

50-
pub(super) fn new(obj_table: &[(LinkOutputKind, &[&'static str])]) -> CrtObjectsData {
51-
obj_table.iter().map(|(z, k)| (*z, k.iter().map(|b| (*b).into()).collect())).collect()
50+
impl FnOnce<()> for Trulala {
51+
type Output = CrtObjectsData;
52+
extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
53+
self.0.iter().map(|(z, k)| (*z, k.iter().map(|b| (*b).into()).collect())).collect()
54+
}
5255
}
5356

54-
pub(super) fn all(obj: &'static str) -> CrtObjectsData {
55-
new(&[
56-
(LinkOutputKind::DynamicNoPicExe, &[obj]),
57-
(LinkOutputKind::DynamicPicExe, &[obj]),
58-
(LinkOutputKind::StaticNoPicExe, &[obj]),
59-
(LinkOutputKind::StaticPicExe, &[obj]),
60-
(LinkOutputKind::DynamicDylib, &[obj]),
61-
(LinkOutputKind::StaticDylib, &[obj]),
62-
])
57+
pub type CrtObjectsData = BTreeMap<LinkOutputKind, Vec<Cow<'static, str>>>;
58+
pub type CrtObjects = MaybeLazy<CrtObjectsData, Trulala>;
59+
60+
pub(super) fn new(obj_table: Tralala) -> CrtObjects {
61+
MaybeLazy::lazy2(Trulala(obj_table))
6362
}
6463

65-
pub(super) fn pre_musl_self_contained() -> CrtObjects {
66-
MaybeLazy::lazy(|| {
64+
macro_rules! all {
65+
($obj: literal) => {
6766
new(&[
68-
(LinkOutputKind::DynamicNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
69-
(LinkOutputKind::DynamicPicExe, &["Scrt1.o", "crti.o", "crtbeginS.o"]),
70-
(LinkOutputKind::StaticNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
71-
(LinkOutputKind::StaticPicExe, &["rcrt1.o", "crti.o", "crtbeginS.o"]),
72-
(LinkOutputKind::DynamicDylib, &["crti.o", "crtbeginS.o"]),
73-
(LinkOutputKind::StaticDylib, &["crti.o", "crtbeginS.o"]),
67+
(LinkOutputKind::DynamicNoPicExe, &[$obj]),
68+
(LinkOutputKind::DynamicPicExe, &[$obj]),
69+
(LinkOutputKind::StaticNoPicExe, &[$obj]),
70+
(LinkOutputKind::StaticPicExe, &[$obj]),
71+
(LinkOutputKind::DynamicDylib, &[$obj]),
72+
(LinkOutputKind::StaticDylib, &[$obj]),
7473
])
75-
})
74+
};
75+
}
76+
77+
pub(super) fn pre_musl_self_contained() -> CrtObjects {
78+
new(&[
79+
(LinkOutputKind::DynamicNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
80+
(LinkOutputKind::DynamicPicExe, &["Scrt1.o", "crti.o", "crtbeginS.o"]),
81+
(LinkOutputKind::StaticNoPicExe, &["crt1.o", "crti.o", "crtbegin.o"]),
82+
(LinkOutputKind::StaticPicExe, &["rcrt1.o", "crti.o", "crtbeginS.o"]),
83+
(LinkOutputKind::DynamicDylib, &["crti.o", "crtbeginS.o"]),
84+
(LinkOutputKind::StaticDylib, &["crti.o", "crtbeginS.o"]),
85+
])
7686
}
7787

7888
pub(super) fn post_musl_self_contained() -> CrtObjects {
79-
MaybeLazy::lazy(|| {
80-
new(&[
81-
(LinkOutputKind::DynamicNoPicExe, &["crtend.o", "crtn.o"]),
82-
(LinkOutputKind::DynamicPicExe, &["crtendS.o", "crtn.o"]),
83-
(LinkOutputKind::StaticNoPicExe, &["crtend.o", "crtn.o"]),
84-
(LinkOutputKind::StaticPicExe, &["crtendS.o", "crtn.o"]),
85-
(LinkOutputKind::DynamicDylib, &["crtendS.o", "crtn.o"]),
86-
(LinkOutputKind::StaticDylib, &["crtendS.o", "crtn.o"]),
87-
])
88-
})
89+
new(&[
90+
(LinkOutputKind::DynamicNoPicExe, &["crtend.o", "crtn.o"]),
91+
(LinkOutputKind::DynamicPicExe, &["crtendS.o", "crtn.o"]),
92+
(LinkOutputKind::StaticNoPicExe, &["crtend.o", "crtn.o"]),
93+
(LinkOutputKind::StaticPicExe, &["crtendS.o", "crtn.o"]),
94+
(LinkOutputKind::DynamicDylib, &["crtendS.o", "crtn.o"]),
95+
(LinkOutputKind::StaticDylib, &["crtendS.o", "crtn.o"]),
96+
])
8997
}
9098

9199
pub(super) fn pre_mingw_self_contained() -> CrtObjects {
92-
MaybeLazy::lazy(|| {
93-
new(&[
94-
(LinkOutputKind::DynamicNoPicExe, &["crt2.o", "rsbegin.o"]),
95-
(LinkOutputKind::DynamicPicExe, &["crt2.o", "rsbegin.o"]),
96-
(LinkOutputKind::StaticNoPicExe, &["crt2.o", "rsbegin.o"]),
97-
(LinkOutputKind::StaticPicExe, &["crt2.o", "rsbegin.o"]),
98-
(LinkOutputKind::DynamicDylib, &["dllcrt2.o", "rsbegin.o"]),
99-
(LinkOutputKind::StaticDylib, &["dllcrt2.o", "rsbegin.o"]),
100-
])
101-
})
100+
new(&[
101+
(LinkOutputKind::DynamicNoPicExe, &["crt2.o", "rsbegin.o"]),
102+
(LinkOutputKind::DynamicPicExe, &["crt2.o", "rsbegin.o"]),
103+
(LinkOutputKind::StaticNoPicExe, &["crt2.o", "rsbegin.o"]),
104+
(LinkOutputKind::StaticPicExe, &["crt2.o", "rsbegin.o"]),
105+
(LinkOutputKind::DynamicDylib, &["dllcrt2.o", "rsbegin.o"]),
106+
(LinkOutputKind::StaticDylib, &["dllcrt2.o", "rsbegin.o"]),
107+
])
102108
}
103109

104110
pub(super) fn post_mingw_self_contained() -> CrtObjects {
105-
MaybeLazy::lazy(|| all("rsend.o"))
111+
all!("rsend.o")
106112
}
107113

108114
pub(super) fn pre_mingw() -> CrtObjects {
109-
MaybeLazy::lazy(|| all("rsbegin.o"))
115+
all!("rsbegin.o")
110116
}
111117

112118
pub(super) fn post_mingw() -> CrtObjects {
113-
MaybeLazy::lazy(|| all("rsend.o"))
119+
all!("rsend.o")
114120
}
115121

116122
pub(super) fn pre_wasi_self_contained() -> CrtObjects {
117123
// Use crt1-command.o instead of crt1.o to enable support for new-style
118124
// commands. See https://reviews.llvm.org/D81689 for more info.
119-
MaybeLazy::lazy(|| {
120-
new(&[
121-
(LinkOutputKind::DynamicNoPicExe, &["crt1-command.o"]),
122-
(LinkOutputKind::DynamicPicExe, &["crt1-command.o"]),
123-
(LinkOutputKind::StaticNoPicExe, &["crt1-command.o"]),
124-
(LinkOutputKind::StaticPicExe, &["crt1-command.o"]),
125-
(LinkOutputKind::WasiReactorExe, &["crt1-reactor.o"]),
126-
])
127-
})
125+
new(&[
126+
(LinkOutputKind::DynamicNoPicExe, &["crt1-command.o"]),
127+
(LinkOutputKind::DynamicPicExe, &["crt1-command.o"]),
128+
(LinkOutputKind::StaticNoPicExe, &["crt1-command.o"]),
129+
(LinkOutputKind::StaticPicExe, &["crt1-command.o"]),
130+
(LinkOutputKind::WasiReactorExe, &["crt1-reactor.o"]),
131+
])
128132
}
129133

130134
pub(super) fn post_wasi_self_contained() -> CrtObjects {
131-
MaybeLazy::lazy(|| new(&[]))
135+
new(&[])
132136
}

compiler/rustc_target/src/spec/maybe_lazy.rs

+35-17
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::fmt::{Debug, Display};
55
use std::ops::Deref;
66
use std::sync::LazyLock;
77

8-
enum MaybeLazyInner<T: 'static + ToOwned + ?Sized> {
9-
Lazy(LazyLock<T::Owned>),
8+
enum MaybeLazyInner<T: 'static + ToOwned + ?Sized, F> {
9+
Lazy(LazyLock<T::Owned, F>),
1010
Cow(Cow<'static, T>),
1111
}
1212

@@ -16,16 +16,16 @@ enum MaybeLazyInner<T: 'static + ToOwned + ?Sized> {
1616
/// They can all be constructed from the [`MaybeLazy::borrowed`], [`MaybeLazy::owned`] and
1717
/// [`MaybeLazy::lazy`] methods.
1818
#[repr(transparent)]
19-
pub struct MaybeLazy<T: 'static + ToOwned + ?Sized> {
19+
pub struct MaybeLazy<T: 'static + ToOwned + ?Sized, F = fn() -> <T as ToOwned>::Owned> {
2020
// Inner state.
2121
//
2222
// Not to be inlined since we may want in the future to
2323
// make this struct usable to statics and we might need to
2424
// workaround const-eval limitation (particulary around drop).
25-
inner: MaybeLazyInner<T>,
25+
inner: MaybeLazyInner<T, F>,
2626
}
2727

28-
impl<T: 'static + ?Sized + ToOwned> MaybeLazy<T> {
28+
impl<T: 'static + ?Sized + ToOwned, F: FnOnce() -> T::Owned> MaybeLazy<T, F> {
2929
/// Create a [`MaybeLazy`] from an borrowed `T`.
3030
#[inline]
3131
pub const fn borrowed(a: &'static T) -> Self {
@@ -38,17 +38,25 @@ impl<T: 'static + ?Sized + ToOwned> MaybeLazy<T> {
3838
MaybeLazy { inner: MaybeLazyInner::Cow(Cow::Owned(a)) }
3939
}
4040

41+
pub const fn lazy2(a: F) -> Self {
42+
MaybeLazy { inner: MaybeLazyInner::Lazy(LazyLock::new(a)) }
43+
}
44+
}
45+
46+
impl<T: 'static + ?Sized + ToOwned> MaybeLazy<T> {
4147
/// Create a [`MaybeLazy`] that is lazy by taking a function pointer.
4248
///
4349
/// This function pointer cannot *ever* take a closure. User can potentially
4450
/// workaround that by using closure-to-fnptr or `const` items.
4551
#[inline]
4652
pub const fn lazy(a: fn() -> T::Owned) -> Self {
47-
MaybeLazy { inner: MaybeLazyInner::Lazy(LazyLock::new(a)) }
53+
Self::lazy2(a)
4854
}
4955
}
5056

51-
impl<T: 'static + ?Sized + ToOwned<Owned: Clone>> Clone for MaybeLazy<T> {
57+
impl<T: 'static + ?Sized + ToOwned<Owned: Clone>, F: FnOnce() -> T::Owned> Clone
58+
for MaybeLazy<T, F>
59+
{
5260
#[inline]
5361
fn clone(&self) -> Self {
5462
MaybeLazy {
@@ -68,7 +76,9 @@ impl<T: 'static + ?Sized + ToOwned<Owned: Default>> Default for MaybeLazy<T> {
6876
}
6977

7078
// Debug and Display below are implemented in terms of this Deref
71-
impl<T: 'static + ?Sized + ToOwned<Owned: Borrow<T>>> Deref for MaybeLazy<T> {
79+
impl<T: 'static + ?Sized + ToOwned<Owned: Borrow<T>>, F: FnOnce() -> T::Owned> Deref
80+
for MaybeLazy<T, F>
81+
{
7282
type Target = T;
7383

7484
#[inline]
@@ -80,46 +90,54 @@ impl<T: 'static + ?Sized + ToOwned<Owned: Borrow<T>>> Deref for MaybeLazy<T> {
8090
}
8191
}
8292

83-
impl<T: 'static + ?Sized + ToOwned<Owned: Debug> + Debug> Debug for MaybeLazy<T> {
93+
impl<T: 'static + ?Sized + ToOwned<Owned: Debug> + Debug, F: FnOnce() -> T::Owned> Debug
94+
for MaybeLazy<T, F>
95+
{
8496
#[inline]
8597
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8698
Debug::fmt(&**self, fmt)
8799
}
88100
}
89101

90-
impl<T: 'static + ?Sized + ToOwned<Owned: Display> + Display> Display for MaybeLazy<T> {
102+
impl<T: 'static + ?Sized + ToOwned<Owned: Display> + Display, F: FnOnce() -> T::Owned> Display
103+
for MaybeLazy<T, F>
104+
{
91105
#[inline]
92106
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93107
Display::fmt(&**self, fmt)
94108
}
95109
}
96110

97-
impl<T: 'static + ?Sized + ToOwned> AsRef<T> for MaybeLazy<T> {
111+
impl<T: 'static + ?Sized + ToOwned, F: FnOnce() -> T::Owned> AsRef<T> for MaybeLazy<T, F> {
98112
#[inline]
99113
fn as_ref(&self) -> &T {
100114
&**self
101115
}
102116
}
103117

104-
impl<B: ?Sized + PartialEq<C> + ToOwned, C: ?Sized + ToOwned> PartialEq<MaybeLazy<C>>
105-
for MaybeLazy<B>
118+
impl<
119+
B: ?Sized + PartialEq<C> + ToOwned,
120+
C: ?Sized + ToOwned,
121+
F1: FnOnce() -> B::Owned,
122+
F2: FnOnce() -> C::Owned,
123+
> PartialEq<MaybeLazy<C, F2>> for MaybeLazy<B, F1>
106124
{
107125
#[inline]
108-
fn eq(&self, other: &MaybeLazy<C>) -> bool {
126+
fn eq(&self, other: &MaybeLazy<C, F2>) -> bool {
109127
PartialEq::eq(&**self, &**other)
110128
}
111129
}
112130

113-
impl PartialEq<&str> for MaybeLazy<str> {
131+
impl<F: FnOnce() -> String> PartialEq<&str> for MaybeLazy<str, F> {
114132
#[inline]
115133
fn eq(&self, other: &&str) -> bool {
116134
&**self == *other
117135
}
118136
}
119137

120-
impl From<&'static str> for MaybeLazy<str> {
138+
impl<F: FnOnce() -> String> From<&'static str> for MaybeLazy<str, F> {
121139
#[inline]
122-
fn from(s: &'static str) -> MaybeLazy<str> {
140+
fn from(s: &'static str) -> MaybeLazy<str, F> {
123141
MaybeLazy::borrowed(s)
124142
}
125143
}

compiler/rustc_target/src/spec/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2496,10 +2496,10 @@ impl Default for TargetOptions {
24962496
static_position_independent_executables: false,
24972497
plt_by_default: true,
24982498
relro_level: RelroLevel::None,
2499-
pre_link_objects: MaybeLazy::lazy(Default::default),
2500-
post_link_objects: MaybeLazy::lazy(Default::default),
2501-
pre_link_objects_self_contained: MaybeLazy::lazy(Default::default),
2502-
post_link_objects_self_contained: MaybeLazy::lazy(Default::default),
2499+
pre_link_objects: crt_objects::new(&[]),
2500+
post_link_objects: crt_objects::new(&[]),
2501+
pre_link_objects_self_contained: crt_objects::new(&[]),
2502+
post_link_objects_self_contained: crt_objects::new(&[]),
25032503
link_self_contained: LinkSelfContainedDefault::False,
25042504
pre_link_args: MaybeLazy::lazy(LinkArgs::new),
25052505
pre_link_args_json: LinkArgsCli::new(),

0 commit comments

Comments
 (0)