Skip to content

Commit b424882

Browse files
committed
Auto merge of #5861 - alexcrichton:build-script-edition, r=ehuss
Fix the edition build scripts are compiled with Previously build scripts were accidentally and unconditionally compiled with the 2015 edition, but they should instead use the edition of the `[package]` itself. Closes #5860
2 parents 135ef53 + 152eca5 commit b424882

File tree

4 files changed

+108
-30
lines changed

4 files changed

+108
-30
lines changed

src/cargo/core/manifest.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,29 @@ compact_debug! {
251251
match &self.kind {
252252
TargetKind::Lib(kinds) => {
253253
(
254-
Target::lib_target(&self.name, kinds.clone(), src.clone()),
254+
Target::lib_target(
255+
&self.name,
256+
kinds.clone(),
257+
src.clone(),
258+
Edition::Edition2015,
259+
),
255260
format!("lib_target({:?}, {:?}, {:?})",
256261
self.name, kinds, src),
257262
)
258263
}
259264
TargetKind::CustomBuild => {
260265
(
261-
Target::custom_build_target(&self.name, src.clone()),
266+
Target::custom_build_target(
267+
&self.name,
268+
src.clone(),
269+
Edition::Edition2015,
270+
),
262271
format!("custom_build_target({:?}, {:?})",
263272
self.name, src),
264273
)
265274
}
266275
_ => (
267-
Target::with_path(src.clone()),
276+
Target::with_path(src.clone(), Edition::Edition2015),
268277
format!("with_path({:?})", src),
269278
),
270279
}
@@ -493,7 +502,7 @@ impl VirtualManifest {
493502
}
494503

495504
impl Target {
496-
fn with_path(src_path: PathBuf) -> Target {
505+
fn with_path(src_path: PathBuf, edition: Edition) -> Target {
497506
assert!(
498507
src_path.is_absolute(),
499508
"`{}` is not absolute",
@@ -508,45 +517,55 @@ impl Target {
508517
doctest: false,
509518
harness: true,
510519
for_host: false,
511-
edition: Edition::Edition2015,
520+
edition,
512521
tested: true,
513522
benched: true,
514523
}
515524
}
516525

517-
pub fn lib_target(name: &str, crate_targets: Vec<LibKind>, src_path: PathBuf) -> Target {
526+
pub fn lib_target(
527+
name: &str,
528+
crate_targets: Vec<LibKind>,
529+
src_path: PathBuf,
530+
edition: Edition,
531+
) -> Target {
518532
Target {
519533
kind: TargetKind::Lib(crate_targets),
520534
name: name.to_string(),
521535
doctest: true,
522536
doc: true,
523-
..Target::with_path(src_path)
537+
..Target::with_path(src_path, edition)
524538
}
525539
}
526540

527541
pub fn bin_target(
528542
name: &str,
529543
src_path: PathBuf,
530544
required_features: Option<Vec<String>>,
545+
edition: Edition,
531546
) -> Target {
532547
Target {
533548
kind: TargetKind::Bin,
534549
name: name.to_string(),
535550
required_features,
536551
doc: true,
537-
..Target::with_path(src_path)
552+
..Target::with_path(src_path, edition)
538553
}
539554
}
540555

541556
/// Builds a `Target` corresponding to the `build = "build.rs"` entry.
542-
pub fn custom_build_target(name: &str, src_path: PathBuf) -> Target {
557+
pub fn custom_build_target(
558+
name: &str,
559+
src_path: PathBuf,
560+
edition: Edition,
561+
) -> Target {
543562
Target {
544563
kind: TargetKind::CustomBuild,
545564
name: name.to_string(),
546565
for_host: true,
547566
benched: false,
548567
tested: false,
549-
..Target::with_path(src_path)
568+
..Target::with_path(src_path, edition)
550569
}
551570
}
552571

@@ -555,6 +574,7 @@ impl Target {
555574
crate_targets: Vec<LibKind>,
556575
src_path: PathBuf,
557576
required_features: Option<Vec<String>>,
577+
edition: Edition,
558578
) -> Target {
559579
let kind = if crate_targets.is_empty() {
560580
TargetKind::ExampleBin
@@ -568,35 +588,37 @@ impl Target {
568588
required_features,
569589
tested: false,
570590
benched: false,
571-
..Target::with_path(src_path)
591+
..Target::with_path(src_path, edition)
572592
}
573593
}
574594

575595
pub fn test_target(
576596
name: &str,
577597
src_path: PathBuf,
578598
required_features: Option<Vec<String>>,
599+
edition: Edition,
579600
) -> Target {
580601
Target {
581602
kind: TargetKind::Test,
582603
name: name.to_string(),
583604
required_features,
584605
benched: false,
585-
..Target::with_path(src_path)
606+
..Target::with_path(src_path, edition)
586607
}
587608
}
588609

589610
pub fn bench_target(
590611
name: &str,
591612
src_path: PathBuf,
592613
required_features: Option<Vec<String>>,
614+
edition: Edition,
593615
) -> Target {
594616
Target {
595617
kind: TargetKind::Bench,
596618
name: name.to_string(),
597619
required_features,
598620
tested: false,
599-
..Target::with_path(src_path)
621+
..Target::with_path(src_path, edition)
600622
}
601623
}
602624

src/cargo/util/toml/targets.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub fn targets(
107107
targets.push(Target::custom_build_target(
108108
&name,
109109
package_root.join(custom_build),
110+
edition,
110111
));
111112
}
112113

@@ -189,8 +190,8 @@ fn clean_lib(
189190
(None, _, _) => vec![LibKind::Lib],
190191
};
191192

192-
let mut target = Target::lib_target(&lib.name(), crate_types, path);
193-
configure(features, lib, &mut target, edition)?;
193+
let mut target = Target::lib_target(&lib.name(), crate_types, path, edition);
194+
configure(features, lib, &mut target)?;
194195
Ok(Some(target))
195196
}
196197

@@ -270,8 +271,13 @@ fn clean_bins(
270271
Err(e) => bail!("{}", e),
271272
};
272273

273-
let mut target = Target::bin_target(&bin.name(), path, bin.required_features.clone());
274-
configure(features, bin, &mut target, edition)?;
274+
let mut target = Target::bin_target(
275+
&bin.name(),
276+
path,
277+
bin.required_features.clone(),
278+
edition,
279+
);
280+
configure(features, bin, &mut target)?;
275281
result.push(target);
276282
}
277283
return Ok(result);
@@ -332,8 +338,9 @@ fn clean_examples(
332338
crate_types,
333339
path,
334340
toml.required_features.clone(),
341+
edition,
335342
);
336-
configure(features, &toml, &mut target, edition)?;
343+
configure(features, &toml, &mut target)?;
337344
result.push(target);
338345
}
339346

@@ -366,8 +373,13 @@ fn clean_tests(
366373

367374
let mut result = Vec::new();
368375
for (path, toml) in targets {
369-
let mut target = Target::test_target(&toml.name(), path, toml.required_features.clone());
370-
configure(features, &toml, &mut target, edition)?;
376+
let mut target = Target::test_target(
377+
&toml.name(),
378+
path,
379+
toml.required_features.clone(),
380+
edition,
381+
);
382+
configure(features, &toml, &mut target)?;
371383
result.push(target);
372384
}
373385
Ok(result)
@@ -420,8 +432,13 @@ fn clean_benches(
420432

421433
let mut result = Vec::new();
422434
for (path, toml) in targets {
423-
let mut target = Target::bench_target(&toml.name(), path, toml.required_features.clone());
424-
configure(features, &toml, &mut target, edition)?;
435+
let mut target = Target::bench_target(
436+
&toml.name(),
437+
path,
438+
toml.required_features.clone(),
439+
edition,
440+
);
441+
configure(features, &toml, &mut target)?;
425442
result.push(target);
426443
}
427444

@@ -697,7 +714,6 @@ fn configure(
697714
features: &Features,
698715
toml: &TomlTarget,
699716
target: &mut Target,
700-
edition: Edition,
701717
) -> CargoResult<()> {
702718
let t2 = target.clone();
703719
target
@@ -710,14 +726,11 @@ fn configure(
710726
(None, None) => t2.for_host(),
711727
(Some(true), _) | (_, Some(true)) => true,
712728
(Some(false), _) | (_, Some(false)) => false,
713-
})
714-
.set_edition(match toml.edition.clone() {
715-
None => edition,
716-
Some(s) => {
717-
features.require(Feature::edition()).chain_err(|| "editions are unstable")?;
718-
s.parse().chain_err(|| "failed to parse the `edition` key")?
719-
},
720729
});
730+
if let Some(edition) = toml.edition.clone() {
731+
features.require(Feature::edition()).chain_err(|| "editions are unstable")?;
732+
target.set_edition(edition.parse().chain_err(|| "failed to parse the `edition` key")?);
733+
}
721734
Ok(())
722735
}
723736

tests/testsuite/edition.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use support::{basic_lib_manifest, is_nightly, execs, project};
2+
use support::ChannelChanger;
3+
use support::hamcrest::assert_that;
4+
5+
#[test]
6+
fn edition_works_for_build_script() {
7+
if !is_nightly() {
8+
return
9+
}
10+
11+
let p = project()
12+
.file(
13+
"Cargo.toml",
14+
r#"
15+
cargo-features = ['edition']
16+
[package]
17+
name = 'foo'
18+
version = '0.1.0'
19+
edition = '2018'
20+
21+
[build-dependencies]
22+
a = { path = 'a' }
23+
"#,
24+
)
25+
.file("src/lib.rs", "")
26+
.file(
27+
"build.rs",
28+
r#"
29+
fn main() {
30+
a::foo();
31+
}
32+
"#,
33+
)
34+
.file("a/Cargo.toml", &basic_lib_manifest("a"))
35+
.file("a/src/lib.rs", "pub fn foo() {}")
36+
.build();
37+
38+
assert_that(
39+
p.cargo("build -v").masquerade_as_nightly_cargo(),
40+
execs().with_status(0),
41+
);
42+
}

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ mod death;
4747
mod dep_info;
4848
mod directory;
4949
mod doc;
50+
mod edition;
5051
mod features;
5152
mod fetch;
5253
mod fix;

0 commit comments

Comments
 (0)