Skip to content

Commit ce61d40

Browse files
committed
Replace every Vec in Target(Options) with it's Cow equivalent
1 parent ccff48f commit ce61d40

31 files changed

+117
-53
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,10 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
674674

675675
linker::disable_localization(&mut cmd);
676676

677-
for &(ref k, ref v) in &sess.target.link_env {
677+
for &(ref k, ref v) in sess.target.link_env.iter() {
678678
cmd.env(k.as_ref(), v.as_ref());
679679
}
680-
for k in &sess.target.link_env_remove {
680+
for k in sess.target.link_env_remove.iter() {
681681
cmd.env_remove(k.as_ref());
682682
}
683683

compiler/rustc_serialize/src/json.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,15 @@ impl<A: ToJson> ToJson for Vec<A> {
22472247
}
22482248
}
22492249

2250+
impl<'a, A: ToJson> ToJson for Cow<'a, [A]>
2251+
where
2252+
[A]: ToOwned,
2253+
{
2254+
fn to_json(&self) -> Json {
2255+
Json::Array(self.iter().map(|elt| elt.to_json()).collect())
2256+
}
2257+
}
2258+
22502259
impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
22512260
fn to_json(&self) -> Json {
22522261
let mut d = BTreeMap::new();

compiler/rustc_session/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
956956
ret.reserve(7); // the minimum number of insertions
957957
// Target bindings.
958958
ret.insert((sym::target_os, Some(Symbol::intern(os))));
959-
for fam in &sess.target.families {
959+
for fam in sess.target.families.iter() {
960960
ret.insert((sym::target_family, Some(Symbol::intern(fam))));
961961
if fam == "windows" {
962962
ret.insert((sym::windows, None));

compiler/rustc_target/src/spec/aarch64_apple_darwin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn target() -> Target {
99
base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::THREAD;
1010

1111
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), "arm64".into()]);
12-
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
12+
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
1313

1414
// Clang automatically chooses a more specific target based on
1515
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work

compiler/rustc_target/src/spec/apple_base.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::{borrow::Cow, env};
22

33
use crate::spec::{FramePointer, LldFlavor, SplitDebuginfo, TargetOptions};
44

5+
use super::cvs;
6+
57
pub fn opts(os: &'static str) -> TargetOptions {
68
// ELF TLS is only available in macOS 10.7+. If you try to compile for 10.6
79
// either the linker will complain if it is used or the binary will end up
@@ -26,7 +28,7 @@ pub fn opts(os: &'static str) -> TargetOptions {
2628
dynamic_linking: true,
2729
linker_is_gnu: false,
2830
executables: true,
29-
families: vec!["unix".into()],
31+
families: cvs!["unix"],
3032
is_like_osx: true,
3133
dwarf_version: Some(2),
3234
frame_pointer: FramePointer::Always,
@@ -51,7 +53,7 @@ pub fn opts(os: &'static str) -> TargetOptions {
5153
// this environment variable too in recent versions.
5254
//
5355
// For some more info see the commentary on #47086
54-
link_env: vec![("ZERO_AR_DATE".into(), "1".into())],
56+
link_env: Cow::Borrowed(&[(Cow::Borrowed("ZERO_AR_DATE"), Cow::Borrowed("1"))]),
5557

5658
..Default::default()
5759
}

compiler/rustc_target/src/spec/apple_sdk_base.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::spec::TargetOptions;
1+
use crate::{spec::cvs, spec::TargetOptions};
22
use std::borrow::Cow;
33

44
use Arch::*;
@@ -36,12 +36,12 @@ fn target_cpu(arch: Arch) -> &'static str {
3636
}
3737
}
3838

39-
fn link_env_remove(arch: Arch) -> Vec<Cow<'static, str>> {
39+
fn link_env_remove(arch: Arch) -> Cow<'static, [Cow<'static, str>]> {
4040
match arch {
4141
Armv7 | Armv7s | Arm64 | I386 | X86_64 | Arm64_sim => {
42-
vec!["MACOSX_DEPLOYMENT_TARGET".into()]
42+
cvs!["MACOSX_DEPLOYMENT_TARGET"]
4343
}
44-
X86_64_macabi | Arm64_macabi => vec!["IPHONEOS_DEPLOYMENT_TARGET".into()],
44+
X86_64_macabi | Arm64_macabi => cvs!["IPHONEOS_DEPLOYMENT_TARGET"],
4545
}
4646
}
4747

compiler/rustc_target/src/spec/armv6k_nintendo_3ds.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::spec::{LinkArgs, LinkerFlavor, RelocModel, Target, TargetOptions};
22

3+
use super::cvs;
4+
35
/// A base target for Nintendo 3DS devices using the devkitARM toolchain.
46
///
57
/// Requires the devkitARM toolchain for 3DS targets on the host system.
@@ -30,7 +32,7 @@ pub fn target() -> Target {
3032
linker_flavor: LinkerFlavor::Gcc,
3133
cpu: "mpcore".into(),
3234
executables: true,
33-
families: vec!["unix".into()],
35+
families: cvs!["unix"],
3436
linker: Some("arm-none-eabi-gcc".into()),
3537
relocation_model: RelocModel::Static,
3638
features: "+vfp2".into(),

compiler/rustc_target/src/spec/dragonfly_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::spec::{RelroLevel, TargetOptions};
22

3+
use super::cvs;
4+
35
pub fn opts() -> TargetOptions {
46
TargetOptions {
57
os: "dragonfly".into(),
68
dynamic_linking: true,
79
executables: true,
8-
families: vec!["unix".into()],
10+
families: cvs!["unix"],
911
has_rpath: true,
1012
position_independent_executables: true,
1113
relro_level: RelroLevel::Full,

compiler/rustc_target/src/spec/freebsd_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::spec::{RelroLevel, TargetOptions};
22

3+
use super::cvs;
4+
35
pub fn opts() -> TargetOptions {
46
TargetOptions {
57
os: "freebsd".into(),
68
dynamic_linking: true,
79
executables: true,
8-
families: vec!["unix".into()],
10+
families: cvs!["unix"],
911
has_rpath: true,
1012
position_independent_executables: true,
1113
relro_level: RelroLevel::Full,

compiler/rustc_target/src/spec/fuchsia_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::spec::{crt_objects, LinkArgs, LinkOutputKind, LinkerFlavor, LldFlavor, TargetOptions};
22

3+
use super::cvs;
4+
35
pub fn opts() -> TargetOptions {
46
let mut pre_link_args = LinkArgs::new();
57
pre_link_args.insert(
@@ -25,7 +27,7 @@ pub fn opts() -> TargetOptions {
2527
linker: Some("rust-lld".into()),
2628
dynamic_linking: true,
2729
executables: true,
28-
families: vec!["unix".into()],
30+
families: cvs!["unix"],
2931
is_like_fuchsia: true,
3032
pre_link_args,
3133
pre_link_objects: crt_objects::new(&[

compiler/rustc_target/src/spec/haiku_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::spec::{RelroLevel, TargetOptions};
22

3+
use super::cvs;
4+
35
pub fn opts() -> TargetOptions {
46
TargetOptions {
57
os: "haiku".into(),
68
dynamic_linking: true,
79
executables: true,
8-
families: vec!["unix".into()],
10+
families: cvs!["unix"],
911
relro_level: RelroLevel::Full,
1012
..Default::default()
1113
}

compiler/rustc_target/src/spec/i686_apple_darwin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub fn target() -> Target {
55
base.cpu = "yonah".into();
66
base.max_atomic_width = Some(64);
77
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".into()]);
8-
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
8+
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
99
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
1010
base.stack_probes = StackProbeType::Call;
1111
base.frame_pointer = FramePointer::Always;

compiler/rustc_target/src/spec/illumos_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::spec::{FramePointer, LinkArgs, LinkerFlavor, TargetOptions};
22
use std::default::Default;
33

4+
use super::cvs;
5+
46
pub fn opts() -> TargetOptions {
57
let mut late_link_args = LinkArgs::new();
68
late_link_args.insert(
@@ -31,7 +33,7 @@ pub fn opts() -> TargetOptions {
3133
dynamic_linking: true,
3234
executables: true,
3335
has_rpath: true,
34-
families: vec!["unix".into()],
36+
families: cvs!["unix"],
3537
is_like_solaris: true,
3638
linker_is_gnu: false,
3739
limit_rdylib_exports: false, // Linker doesn't support this

compiler/rustc_target/src/spec/l4re_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::spec::{LinkerFlavor, PanicStrategy, TargetOptions};
22
use std::default::Default;
33

4+
use super::cvs;
5+
46
pub fn opts() -> TargetOptions {
57
TargetOptions {
68
os: "l4re".into(),
@@ -10,7 +12,7 @@ pub fn opts() -> TargetOptions {
1012
panic_strategy: PanicStrategy::Abort,
1113
linker: Some("l4-bender".into()),
1214
linker_is_gnu: false,
13-
families: vec!["unix".into()],
15+
families: cvs!["unix"],
1416
..Default::default()
1517
}
1618
}

compiler/rustc_target/src/spec/linux_base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::spec::{RelroLevel, TargetOptions};
22

3+
use super::cvs;
4+
35
pub fn opts() -> TargetOptions {
46
TargetOptions {
57
os: "linux".into(),
68
dynamic_linking: true,
79
executables: true,
8-
families: vec!["unix".into()],
10+
families: cvs!["unix"],
911
has_rpath: true,
1012
position_independent_executables: true,
1113
relro_level: RelroLevel::Full,

compiler/rustc_target/src/spec/mipsel_sony_psp.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, RelocModel};
22
use crate::spec::{Target, TargetOptions};
33

4+
use super::cvs;
5+
46
// The PSP has custom linker requirements.
57
const LINKER_SCRIPT: &str = include_str!("./mipsel_sony_psp_linker_script.ld");
68

@@ -27,7 +29,7 @@ pub fn target() -> Target {
2729
features: "+single-float".into(),
2830

2931
// PSP does not support trap-on-condition instructions.
30-
llvm_args: vec!["-mno-check-zero-division".into()],
32+
llvm_args: cvs!["-mno-check-zero-division"],
3133
pre_link_args,
3234
link_script: Some(LINKER_SCRIPT.into()),
3335
..Default::default()

compiler/rustc_target/src/spec/mod.rs

+32-13
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,25 @@ supported_targets! {
10271027
("mips64-openwrt-linux-musl", mips64_openwrt_linux_musl),
10281028
}
10291029

1030+
/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
1031+
// FIXME(Urgau): Figure out why the obvious form `["".into()].into()` doesn't work.
1032+
macro_rules! cvs {
1033+
() => {
1034+
::std::borrow::Cow::Borrowed(&[])
1035+
};
1036+
($($x:expr),+ $(,)?) => {
1037+
{
1038+
::std::borrow::Cow::Borrowed(&[
1039+
$(
1040+
::std::borrow::Cow::Borrowed($x),
1041+
)*
1042+
])
1043+
}
1044+
};
1045+
}
1046+
1047+
pub(crate) use cvs;
1048+
10301049
/// Warnings encountered when parsing the target `json`.
10311050
///
10321051
/// Includes fields that weren't recognized and fields that don't have the expected type.
@@ -1160,12 +1179,12 @@ pub struct TargetOptions {
11601179
pub link_script: Option<Cow<'static, str>>,
11611180

11621181
/// Environment variables to be set for the linker invocation.
1163-
pub link_env: Vec<(Cow<'static, str>, Cow<'static, str>)>,
1182+
pub link_env: Cow<'static, [(Cow<'static, str>, Cow<'static, str>)]>,
11641183
/// Environment variables to be removed for the linker invocation.
1165-
pub link_env_remove: Vec<Cow<'static, str>>,
1184+
pub link_env_remove: Cow<'static, [Cow<'static, str>]>,
11661185

11671186
/// Extra arguments to pass to the external assembler (when used)
1168-
pub asm_args: Vec<Cow<'static, str>>,
1187+
pub asm_args: Cow<'static, [Cow<'static, str>]>,
11691188

11701189
/// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
11711190
/// to "generic".
@@ -1211,7 +1230,7 @@ pub struct TargetOptions {
12111230
/// Common options are: "unix", "windows". Defaults to no families.
12121231
///
12131232
/// See <https://doc.rust-lang.org/reference/conditional-compilation.html#target_family>.
1214-
pub families: Vec<Cow<'static, str>>,
1233+
pub families: Cow<'static, [Cow<'static, str>]>,
12151234
/// Whether the target toolchain's ABI supports returning small structs as an integer.
12161235
pub abi_return_struct_as_int: bool,
12171236
/// Whether the target toolchain is like macOS's. Only useful for compiling against iOS/macOS,
@@ -1371,7 +1390,7 @@ pub struct TargetOptions {
13711390

13721391
/// If set, have the linker export exactly these symbols, instead of using
13731392
/// the usual logic to figure this out from the crate itself.
1374-
pub override_export_symbols: Option<Vec<Cow<'static, str>>>,
1393+
pub override_export_symbols: Option<Cow<'static, [Cow<'static, str>]>>,
13751394

13761395
/// Determines how or whether the MergeFunctions LLVM pass should run for
13771396
/// this target. Either "disabled", "trampolines", or "aliases".
@@ -1391,7 +1410,7 @@ pub struct TargetOptions {
13911410
pub relax_elf_relocations: bool,
13921411

13931412
/// Additional arguments to pass to LLVM, similar to the `-C llvm-args` codegen option.
1394-
pub llvm_args: Vec<Cow<'static, str>>,
1413+
pub llvm_args: Cow<'static, [Cow<'static, str>]>,
13951414

13961415
/// Whether to use legacy .ctors initialization hooks rather than .init_array. Defaults
13971416
/// to false (uses .init_array).
@@ -1449,7 +1468,7 @@ impl Default for TargetOptions {
14491468
pre_link_args: LinkArgs::new(),
14501469
post_link_args: LinkArgs::new(),
14511470
link_script: None,
1452-
asm_args: Vec::new(),
1471+
asm_args: Cow::Borrowed(&[]),
14531472
cpu: "generic".into(),
14541473
features: Cow::from(""),
14551474
dynamic_linking: false,
@@ -1466,7 +1485,7 @@ impl Default for TargetOptions {
14661485
exe_suffix: Cow::from(""),
14671486
staticlib_prefix: "lib".into(),
14681487
staticlib_suffix: ".a".into(),
1469-
families: Vec::new(),
1488+
families: cvs![],
14701489
abi_return_struct_as_int: false,
14711490
is_like_osx: false,
14721491
is_like_solaris: false,
@@ -1492,8 +1511,8 @@ impl Default for TargetOptions {
14921511
late_link_args: LinkArgs::new(),
14931512
late_link_args_dynamic: LinkArgs::new(),
14941513
late_link_args_static: LinkArgs::new(),
1495-
link_env: Vec::new(),
1496-
link_env_remove: Vec::new(),
1514+
link_env: Cow::Borrowed(&[]),
1515+
link_env_remove: Cow::Borrowed(&[]),
14971516
archive_format: "gnu".into(),
14981517
main_needs_argc_argv: true,
14991518
allow_asm: true,
@@ -1526,7 +1545,7 @@ impl Default for TargetOptions {
15261545
mcount: "mcount".into(),
15271546
llvm_abiname: "".into(),
15281547
relax_elf_relocations: false,
1529-
llvm_args: vec![],
1548+
llvm_args: cvs![],
15301549
use_ctors_section: false,
15311550
eh_frame_header: true,
15321551
has_thumb_interworking: false,
@@ -1978,7 +1997,7 @@ impl Target {
19781997
if p.len() == 2 {
19791998
let k = p[0].to_string();
19801999
let v = p[1].to_string();
1981-
base.$key_name.push((k.into(), v.into()));
2000+
base.$key_name.to_mut().push((k.into(), v.into()));
19822001
}
19832002
}
19842003
}
@@ -2004,7 +2023,7 @@ impl Target {
20042023
.map(|a| a.as_string().unwrap().to_string().into())
20052024
.collect();
20062025
} else if let Some(v) = Json::as_string(&value) {
2007-
base.$key_name = vec![v.to_string().into()];
2026+
base.$key_name = vec![v.to_string().into()].into();
20082027
}
20092028
}
20102029
} );

compiler/rustc_target/src/spec/msp430_none_elf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::cvs;
12
use crate::spec::{PanicStrategy, RelocModel, Target, TargetOptions};
23

34
pub fn target() -> Target {
@@ -15,7 +16,7 @@ pub fn target() -> Target {
1516
// workaround this LLVM generates assembly files which then we feed
1617
// to gcc to get object files. For this reason we have a hard
1718
// dependency on this specific gcc.
18-
asm_args: vec!["-mcpu=msp430".into()],
19+
asm_args: cvs!["-mcpu=msp430"],
1920
linker: Some("msp430-elf-gcc".into()),
2021
linker_is_gnu: false,
2122

0 commit comments

Comments
 (0)