Skip to content

Commit feb385b

Browse files
authored
Merge pull request #77 from cuviper/edition
Add methods to get/set the edition
2 parents 706fa58 + f332b4d commit feb385b

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub struct AutoCfg {
9191
rustc_version: Version,
9292
target: Option<OsString>,
9393
no_std: bool,
94+
edition: Option<String>,
9495
rustflags: Vec<String>,
9596
uuid: u64,
9697
}
@@ -203,6 +204,7 @@ impl AutoCfg {
203204
rustc_version: rustc_version,
204205
target: target,
205206
no_std: false,
207+
edition: None,
206208
uuid: new_uuid(),
207209
};
208210

@@ -246,6 +248,36 @@ impl AutoCfg {
246248
self.no_std = no_std;
247249
}
248250

251+
/// Returns the `--edition` string that is currently being passed to `rustc`, if any,
252+
/// as configured by the [`set_edition`][Self::set_edition] method.
253+
pub fn edition(&self) -> Option<&str> {
254+
match self.edition {
255+
Some(ref edition) => Some(&**edition),
256+
None => None,
257+
}
258+
}
259+
260+
/// Sets the `--edition` string that will be passed to `rustc`,
261+
/// or `None` to leave the compiler at its default edition.
262+
///
263+
/// See also [The Rust Edition Guide](https://doc.rust-lang.org/edition-guide/).
264+
///
265+
/// **Warning:** Setting an unsupported edition will likely cause **all** subsequent probes to
266+
/// fail! As of this writing, the known editions and their minimum Rust versions are:
267+
///
268+
/// | Edition | Version |
269+
/// | ------- | ---------- |
270+
/// | 2015 | 1.27.0[^1] |
271+
/// | 2018 | 1.31.0 |
272+
/// | 2021 | 1.56.0 |
273+
/// | 2024 | 1.85.0 |
274+
///
275+
/// [^1]: Prior to 1.27.0, Rust was effectively 2015 Edition by default, but the concept hadn't
276+
/// been established yet, so the explicit `--edition` flag wasn't supported either.
277+
pub fn set_edition(&mut self, edition: Option<String>) {
278+
self.edition = edition;
279+
}
280+
249281
/// Tests whether the current `rustc` reports a version greater than
250282
/// or equal to "`major`.`minor`".
251283
pub fn probe_rustc_version(&self, major: usize, minor: usize) -> bool {
@@ -282,6 +314,10 @@ impl AutoCfg {
282314
.arg(&self.out_dir)
283315
.arg("--emit=llvm-ir");
284316

317+
if let Some(edition) = self.edition.as_ref() {
318+
command.arg("--edition").arg(edition);
319+
}
320+
285321
if let Some(target) = self.target.as_ref() {
286322
command.arg("--target").arg(target);
287323
}

tests/tests.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,41 @@ fn probe_cleanup() {
149149
// is empty -- i.e. the probe should have removed any output files.
150150
std::fs::remove_dir(&dir).unwrap();
151151
}
152+
153+
#[test]
154+
fn editions() {
155+
let mut ac = autocfg_for_test();
156+
assert!(ac.edition().is_none());
157+
assert!(ac.probe_raw("").is_ok());
158+
159+
for (edition, minor) in vec![(2015, 27), (2018, 31), (2021, 56), (2024, 85)] {
160+
let edition = edition.to_string();
161+
ac.set_edition(Some(edition.clone()));
162+
assert_eq!(ac.edition(), Some(&*edition));
163+
assert_min(&ac, 1, minor, ac.probe_raw("").is_ok());
164+
}
165+
166+
ac.set_edition(Some("invalid".into()));
167+
assert_eq!(ac.edition(), Some("invalid"));
168+
assert!(ac.probe_raw("").is_err());
169+
170+
ac.set_edition(None);
171+
assert!(ac.edition().is_none());
172+
assert!(ac.probe_raw("").is_ok());
173+
}
174+
175+
#[test]
176+
fn edition_keyword_try() {
177+
let mut ac = autocfg_for_test();
178+
179+
if ac.probe_rustc_version(1, 27) {
180+
ac.set_edition(Some(2015.to_string()));
181+
}
182+
assert!(ac.probe_expression("{ let try = 0; try }"));
183+
184+
if ac.probe_rustc_version(1, 31) {
185+
ac.set_edition(Some(2018.to_string()));
186+
assert!(!ac.probe_expression("{ let try = 0; try }"));
187+
assert!(ac.probe_expression("{ let r#try = 0; r#try }"));
188+
}
189+
}

0 commit comments

Comments
 (0)