]> git.proxmox.com Git - cargo.git/commitdiff
Fix the edition build scripts are compiled with
authorAlex Crichton <alex@alexcrichton.com>
Sat, 4 Aug 2018 15:44:16 +0000 (08:44 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sat, 4 Aug 2018 15:44:16 +0000 (08:44 -0700)
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

src/cargo/core/manifest.rs
src/cargo/util/toml/targets.rs
tests/testsuite/edition.rs [new file with mode: 0644]
tests/testsuite/main.rs

index 06732ac2956e34c8fa927349514c5993e33e7a1a..82a9c6527a39a63a33c4e03811735fd551507614 100644 (file)
@@ -251,20 +251,29 @@ compact_debug! {
                 match &self.kind {
                     TargetKind::Lib(kinds) => {
                         (
-                            Target::lib_target(&self.name, kinds.clone(), src.clone()),
+                            Target::lib_target(
+                                &self.name,
+                                kinds.clone(),
+                                src.clone(),
+                                Edition::Edition2015,
+                            ),
                             format!("lib_target({:?}, {:?}, {:?})",
                                     self.name, kinds, src),
                         )
                     }
                     TargetKind::CustomBuild => {
                         (
-                            Target::custom_build_target(&self.name, src.clone()),
+                            Target::custom_build_target(
+                                &self.name,
+                                src.clone(),
+                                Edition::Edition2015,
+                            ),
                             format!("custom_build_target({:?}, {:?})",
                                     self.name, src),
                         )
                     }
                     _ => (
-                        Target::with_path(src.clone()),
+                        Target::with_path(src.clone(), Edition::Edition2015),
                         format!("with_path({:?})", src),
                     ),
                 }
@@ -493,7 +502,7 @@ impl VirtualManifest {
 }
 
 impl Target {
-    fn with_path(src_path: PathBuf) -> Target {
+    fn with_path(src_path: PathBuf, edition: Edition) -> Target {
         assert!(
             src_path.is_absolute(),
             "`{}` is not absolute",
@@ -508,19 +517,24 @@ impl Target {
             doctest: false,
             harness: true,
             for_host: false,
-            edition: Edition::Edition2015,
+            edition,
             tested: true,
             benched: true,
         }
     }
 
-    pub fn lib_target(name: &str, crate_targets: Vec<LibKind>, src_path: PathBuf) -> Target {
+    pub fn lib_target(
+        name: &str,
+        crate_targets: Vec<LibKind>,
+        src_path: PathBuf,
+        edition: Edition,
+    ) -> Target {
         Target {
             kind: TargetKind::Lib(crate_targets),
             name: name.to_string(),
             doctest: true,
             doc: true,
-            ..Target::with_path(src_path)
+            ..Target::with_path(src_path, edition)
         }
     }
 
@@ -528,25 +542,30 @@ impl Target {
         name: &str,
         src_path: PathBuf,
         required_features: Option<Vec<String>>,
+        edition: Edition,
     ) -> Target {
         Target {
             kind: TargetKind::Bin,
             name: name.to_string(),
             required_features,
             doc: true,
-            ..Target::with_path(src_path)
+            ..Target::with_path(src_path, edition)
         }
     }
 
     /// Builds a `Target` corresponding to the `build = "build.rs"` entry.
-    pub fn custom_build_target(name: &str, src_path: PathBuf) -> Target {
+    pub fn custom_build_target(
+        name: &str,
+        src_path: PathBuf,
+        edition: Edition,
+    ) -> Target {
         Target {
             kind: TargetKind::CustomBuild,
             name: name.to_string(),
             for_host: true,
             benched: false,
             tested: false,
-            ..Target::with_path(src_path)
+            ..Target::with_path(src_path, edition)
         }
     }
 
@@ -555,6 +574,7 @@ impl Target {
         crate_targets: Vec<LibKind>,
         src_path: PathBuf,
         required_features: Option<Vec<String>>,
+        edition: Edition,
     ) -> Target {
         let kind = if crate_targets.is_empty() {
             TargetKind::ExampleBin
@@ -568,7 +588,7 @@ impl Target {
             required_features,
             tested: false,
             benched: false,
-            ..Target::with_path(src_path)
+            ..Target::with_path(src_path, edition)
         }
     }
 
@@ -576,13 +596,14 @@ impl Target {
         name: &str,
         src_path: PathBuf,
         required_features: Option<Vec<String>>,
+        edition: Edition,
     ) -> Target {
         Target {
             kind: TargetKind::Test,
             name: name.to_string(),
             required_features,
             benched: false,
-            ..Target::with_path(src_path)
+            ..Target::with_path(src_path, edition)
         }
     }
 
@@ -590,13 +611,14 @@ impl Target {
         name: &str,
         src_path: PathBuf,
         required_features: Option<Vec<String>>,
+        edition: Edition,
     ) -> Target {
         Target {
             kind: TargetKind::Bench,
             name: name.to_string(),
             required_features,
             tested: false,
-            ..Target::with_path(src_path)
+            ..Target::with_path(src_path, edition)
         }
     }
 
index e6e3017b60a13c0c1c95a56dedc0e8479893c395..3fcc4f44a016a6ad8dc8faa51d16e336e6f5fc9d 100644 (file)
@@ -107,6 +107,7 @@ pub fn targets(
         targets.push(Target::custom_build_target(
             &name,
             package_root.join(custom_build),
+            edition,
         ));
     }
 
@@ -189,8 +190,8 @@ fn clean_lib(
         (None, _, _) => vec![LibKind::Lib],
     };
 
-    let mut target = Target::lib_target(&lib.name(), crate_types, path);
-    configure(features, lib, &mut target, edition)?;
+    let mut target = Target::lib_target(&lib.name(), crate_types, path, edition);
+    configure(features, lib, &mut target)?;
     Ok(Some(target))
 }
 
@@ -270,8 +271,13 @@ fn clean_bins(
             Err(e) => bail!("{}", e),
         };
 
-        let mut target = Target::bin_target(&bin.name(), path, bin.required_features.clone());
-        configure(features, bin, &mut target, edition)?;
+        let mut target = Target::bin_target(
+            &bin.name(),
+            path,
+            bin.required_features.clone(),
+            edition,
+        );
+        configure(features, bin, &mut target)?;
         result.push(target);
     }
     return Ok(result);
@@ -332,8 +338,9 @@ fn clean_examples(
             crate_types,
             path,
             toml.required_features.clone(),
+            edition,
         );
-        configure(features, &toml, &mut target, edition)?;
+        configure(features, &toml, &mut target)?;
         result.push(target);
     }
 
@@ -366,8 +373,13 @@ fn clean_tests(
 
     let mut result = Vec::new();
     for (path, toml) in targets {
-        let mut target = Target::test_target(&toml.name(), path, toml.required_features.clone());
-        configure(features, &toml, &mut target, edition)?;
+        let mut target = Target::test_target(
+            &toml.name(),
+            path,
+            toml.required_features.clone(),
+            edition,
+        );
+        configure(features, &toml, &mut target)?;
         result.push(target);
     }
     Ok(result)
@@ -420,8 +432,13 @@ fn clean_benches(
 
     let mut result = Vec::new();
     for (path, toml) in targets {
-        let mut target = Target::bench_target(&toml.name(), path, toml.required_features.clone());
-        configure(features, &toml, &mut target, edition)?;
+        let mut target = Target::bench_target(
+            &toml.name(),
+            path,
+            toml.required_features.clone(),
+            edition,
+        );
+        configure(features, &toml, &mut target)?;
         result.push(target);
     }
 
@@ -697,7 +714,6 @@ fn configure(
     features: &Features,
     toml: &TomlTarget,
     target: &mut Target,
-    edition: Edition,
 ) -> CargoResult<()> {
     let t2 = target.clone();
     target
@@ -710,14 +726,11 @@ fn configure(
             (None, None) => t2.for_host(),
             (Some(true), _) | (_, Some(true)) => true,
             (Some(false), _) | (_, Some(false)) => false,
-        })
-        .set_edition(match toml.edition.clone() {
-            None => edition,
-            Some(s) => {
-                features.require(Feature::edition()).chain_err(|| "editions are unstable")?;
-                s.parse().chain_err(|| "failed to parse the `edition` key")?
-            },
         });
+    if let Some(edition) = toml.edition.clone() {
+        features.require(Feature::edition()).chain_err(|| "editions are unstable")?;
+        target.set_edition(edition.parse().chain_err(|| "failed to parse the `edition` key")?);
+    }
     Ok(())
 }
 
diff --git a/tests/testsuite/edition.rs b/tests/testsuite/edition.rs
new file mode 100644 (file)
index 0000000..c392609
--- /dev/null
@@ -0,0 +1,42 @@
+use support::{basic_lib_manifest, is_nightly, execs, project};
+use support::ChannelChanger;
+use support::hamcrest::assert_that;
+
+#[test]
+fn edition_works_for_build_script() {
+    if !is_nightly() {
+        return
+    }
+
+    let p = project()
+        .file(
+            "Cargo.toml",
+            r#"
+                cargo-features = ['edition']
+                [package]
+                name = 'foo'
+                version = '0.1.0'
+                edition = '2018'
+
+                [build-dependencies]
+                a = { path = 'a' }
+            "#,
+        )
+        .file("src/lib.rs", "")
+        .file(
+            "build.rs",
+            r#"
+                fn main() {
+                    a::foo();
+                }
+            "#,
+        )
+        .file("a/Cargo.toml", &basic_lib_manifest("a"))
+        .file("a/src/lib.rs", "pub fn foo() {}")
+        .build();
+
+    assert_that(
+        p.cargo("build -v").masquerade_as_nightly_cargo(),
+        execs().with_status(0),
+    );
+}
index 7aae7bf0e62614c43c0f764d90b93dbcc98ab2af..6b89f29800674ff9886f2152371ca00ba5a46fbb 100644 (file)
@@ -47,6 +47,7 @@ mod death;
 mod dep_info;
 mod directory;
 mod doc;
+mod edition;
 mod features;
 mod fetch;
 mod fix;