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
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),
),
}
}
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",
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)
}
}
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)
}
}
crate_targets: Vec<LibKind>,
src_path: PathBuf,
required_features: Option<Vec<String>>,
+ edition: Edition,
) -> Target {
let kind = if crate_targets.is_empty() {
TargetKind::ExampleBin
required_features,
tested: false,
benched: false,
- ..Target::with_path(src_path)
+ ..Target::with_path(src_path, edition)
}
}
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)
}
}
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)
}
}
targets.push(Target::custom_build_target(
&name,
package_root.join(custom_build),
+ edition,
));
}
(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))
}
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);
crate_types,
path,
toml.required_features.clone(),
+ edition,
);
- configure(features, &toml, &mut target, edition)?;
+ configure(features, &toml, &mut target)?;
result.push(target);
}
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)
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);
}
features: &Features,
toml: &TomlTarget,
target: &mut Target,
- edition: Edition,
) -> CargoResult<()> {
let t2 = target.clone();
target
(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(())
}
--- /dev/null
+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),
+ );
+}
mod dep_info;
mod directory;
mod doc;
+mod edition;
mod features;
mod fetch;
mod fix;