build_safely 0.5.4

build script helpers for working with nightly
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#![expect(clippy::test_attr_in_doctest)]
//! Checking for experimental or stabilised features is prone to subtle errors which create issues
//! for downstream users and verbose when done properly. This provides extensions to the amazing
//! [autocfg::AutoCfg] (re-exported via our prelude to make your life easier) to safely identify the
//! availability of nightly features & handle the future stabilisation process without additional
//! effort on your part. All while respecting any `allow-features` whitelists.
//!
//! For a list of known features with dedicated probes see [UnstableFeature]
//!
//! # Usage
//!
//! ## In `build.rs`
//!
//! ```rust, no_run
//! use build_safely::prelude::*;
//!
//! fn main() -> Result<()> {
//!     // get a new AutoCfg or provide a valuable error
//!     // rather than panicing
//!     let ac = AutoCfg::new()?;
//!
//!     // check to see if the downstream crate has defined
//!     // `unstable.allow-features` in `.cargo/config.toml`.
//!     // It is mandatory to perform this check and pass the
//!     // result to any calls to `emit_unstable_feature`
//!     let allowed_features = cargo_allowed_features()?;
//!
//!     // We want to make use of `assert_matches` if it is available
//!     ac.emit_unstable_feature(assert_matches, &allowed_features);
//!     //                       ^^^^^^^^^^^^^^ - enum variant to avoid typos
//!
//!     Ok(())
//! }
//! ```
//!
//! ## In `lib.rs` / `main.rs`
//!
//! ```rust
//! // only enable unstable feature if it is available and has not yet been stabilised
//! #![cfg_attr(unstable_assert_matches, feature(assert_matches))]
//!
//! #[cfg(test)]
//! // Do these tests if `assert_matches` is available
//! #[cfg(has_assert_matches)]
//! mod tests {
//!     // `assert_matches` was moved in early 2026 before stabilisation
//!     #[cfg(assert_matches_location = "root")]
//!     use std::assert_matches;
//!
//!     // in earlier nightly compilers `assert_matches` was in a separate module
//!     #[cfg(assert_matches_location = "module")]
//!     use std::assert_matches::assert_matches;
//!
//!     #[test]
//!     fn has() {
//!         assert_matches!(Some(5), Some(n) if n == 5);
//!     }
//! }
//!
//! #[cfg(test)]
//! // Do these tests if `assert_matches` is not available
//! #[cfg(not(has_assert_matches))]
//! mod tests {
//!     #[test]
//!     fn has_not() {
//!         assert_eq!(Some(5), Some(5));
//!     }
//! }
//! ```
//!
//! # Note to downstream crates
//!
//! If you (transiently) depend on a crate which uses `build_safely` and have implemented a
//! whitelist of `allowed-features`.
//!
//! Due to limitations in the information provided by cargo:
//!
//! - This will obtain config.toml files based upon `OUT_DIR`. If this is not under the project
//!   root, you can override by providing an alternative path via the environment variable
//!   `BUILD_SAFELY_CARGO_CONFIG_DIR`. See cargo's documentation on config file hierarchical structure
//!   for more details.
//! - This will not respect additional entries passed at the command line via
//!   `cargo --config unstable.allow-features=[...]`

use std::{
    fmt::Debug,
    path::Path,
    process::{Command, Output},
};

/// re-exported from autocfg
///
pub use autocfg::AutoCfg;
use derive_more::Display;

use crate::{BuildError, Result, get_var};
use probes::{has, make_probe, unstable};

/// Known features with `unstable_...` & dedicated probes for `has_...`.
///
/// If the feature you want is not in this list you can use `Other` to get `unstable_...`
/// but please also raise a PR (or open an issue) to add a custom probe for `has_...`.
#[allow(non_camel_case_types, reason = "shadowing feature naming")]
#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum UnstableFeature {
    /// ## Provides cfg flags for feature [`adt_const_params`](https://github.com/rust-lang/rust/issues/95174)
    /// - `#![cfg_attr(unstable_adt_const_params, feature(adt_const_params))]`
    /// - `#[cfg(has_adt_const_params)]`
    adt_const_params,
    /// ## Provides cfg flags:
    /// - `#![cfg_attr(unstable_assert_matches, feature(assert_matches))]`
    /// - `#[cfg(has_assert_matches)]`
    /// - ```rust, ignore
    ///   #[cfg(assert_matches_location = "root")]
    ///   use std::assert_matches;
    ///   ```
    /// - ```rust, ignore
    ///   #[cfg(assert_matches_location = "module")]
    ///   use std::assert_matches::assert_matches;
    ///   ```
    assert_matches,
    /// ## Provides cfg flags for feature [`bool_to_result`](https://github.com/rust-lang/rust/issues/142748)
    /// - `#![cfg_attr(unstable_bool_to_result, feature(bool_to_result))]`
    /// - `#[cfg(has_bool_to_result)]`
    /// - this gates [`bool::ok_or`] & [`bool::ok_or_else`]
    /// - **Stable since 1.98.0**
    bool_to_result,
    /// ## Provides cfg flags for feature [`can_vector`](https://github.com/rust-lang/rust/issues/69941)
    /// - `#![cfg_attr(unstable_can_vector, feature(can_vector))]`
    /// - `#[cfg(has_can_vector)]`
    /// - this gates [`std::io::Read::is_read_vectored`] & [`std::io::Write::is_write_vectored`]
    can_vector,
    /// ## Provides cfg flags for feature [`doc_notable_trait`](https://github.com/rust-lang/rust/issues/45040)
    /// - `#![cfg_attr(unstable_doc_notable_trait, feature(doc_notable_trait))]`
    /// - `#[cfg(has_doc_notable_trait)]`
    doc_notable_trait,
    /// ## Provides cfg flags:
    /// - `#![cfg_attr(unstable_iterator_try_collect, feature(iterator_try_collect))]`
    /// - `#[cfg(has_iterator_try_collect)]`
    iterator_try_collect,
    /// ## Provides cfg flags:
    /// - `#![cfg_attr(unstable_never_type, feature(never_type))]`
    /// - `#[cfg(has_never_type)]`
    never_type,
    /// ## Provides cfg flags:
    /// - `#![cfg_attr(unstable_proc_macro_diagnostic, feature(proc_macro_diagnostic))]`
    /// - `#[cfg(has_proc_macro_diagnostic)]`
    proc_macro_diagnostic,
    /// ## Provides cfg flags for feature [`strip_circumfix`](https://github.com/rust-lang/rust/issues/147946)
    /// - `#![cfg_attr(unstable_strip_circumfix, feature(strip_circumfix))]`
    /// - `#[cfg(has_strip_circumfix)]`
    strip_circumfix,
    /// ## Provides cfg flags:
    /// - `#![cfg_attr(unstable_try_trait_v2, feature(try_trait_v2))]`
    /// - `#[cfg(has_try_trait_v2)]`
    try_trait_v2,
    /// ## Provides cfg flags:
    /// - `#![cfg_attr(unstable_try_trait_v2_residual, feature(try_trait_v2_residual))]`
    /// - `#[cfg(has_try_trait_v2_residual)]`
    try_trait_v2_residual,
    /// ## Provides cfg flags for feature [`unsized_const_params`](https://github.com/rust-lang/rust/issues/128028)
    /// - `#![cfg_attr(unstable_unsized_const_params, feature(unsized_const_params))]`
    /// - `#[cfg(has_unsized_const_params)]`
    /// - `#![cfg_attr(unstable_adt_const_params, feature(adt_const_params))]`
    /// - Note: `unsized_const_params` requires `adt_const_params` to be enabled
    unsized_const_params,
    /// ## Provides cfg flags for feature [`write_all_vectored`](https://github.com/rust-lang/rust/issues/70436)
    /// - `#![cfg_attr(unstable_write_all_vectored, feature(write_all_vectored))]`
    /// - `#[cfg(has_write_all_vectored)]`
    /// - this gates [`std::io::Write::write_all_vectored`]
    write_all_vectored,
    /// only provides `unstable_...` - please raise a PR to add a custom probe for `has_...`
    OtherFeature(String),
}

impl UnstableFeature {
    // This is not pub or trait From to avoid risk of typos
    fn from(feature: &str) -> Self {
        match feature {
            "adt_const_params" => Self::adt_const_params,
            "assert_matches" => Self::assert_matches,
            "bool_to_result" => Self::bool_to_result,
            "can_vector" => Self::can_vector,
            "doc_notable_trait" => Self::doc_notable_trait,
            "iterator_try_collect" => Self::iterator_try_collect,
            "never_type" => Self::never_type,
            "proc_macro_diagnostic" => Self::proc_macro_diagnostic,
            "strip_circumfix" => Self::strip_circumfix,
            "try_trait_v2" => Self::try_trait_v2,
            "try_trait_v2_residual" => Self::try_trait_v2_residual,
            "unsized_const_params" => Self::unsized_const_params,
            "write_all_vectored" => Self::write_all_vectored,
            _ => Self::OtherFeature(feature.to_string()),
        }
    }
}

mod probes {
    use super::{AutoCfg, UnstableFeature};

    /// Prefix with:
    /// - #![allow(stable_features)] (only if allowed)
    /// - #![feature()] (only if allowed)
    /// - #![allow(unused)] (always)
    pub fn make_probe(feature: &UnstableFeature, allowed: bool, probe: &str) -> String {
        let mut _probe = String::with_capacity(256);
        if allowed {
            _probe.push('\n');
            _probe.push_str("#![allow(stable_features)]");
            _probe.push('\n');

            _probe.push_str("#![feature(");
            _probe.push_str(&feature.to_string());
            _probe.push_str(")]");
            _probe.push('\n');
        };
        _probe.push_str("#![allow(unused)]");
        _probe.push('\n');
        _probe.push_str(probe);
        _probe
    }

    /// Register `#[cfg(has_feature)]` & set based on the probe
    pub fn has(ac: &AutoCfg, feature: &UnstableFeature, allowed: bool, probe: &str) -> bool {
        let cfg = format!("has_{feature}");
        autocfg::emit_possibility(&cfg);
        let code = make_probe(feature, allowed, probe);
        if ac.probe_raw(&code).is_ok() {
            autocfg::emit(&cfg);
            true
        } else {
            false
        }
    }

    /// Register `#[cfg(has_feature)]` & run a default probe
    pub fn unstable(
        ac: &AutoCfg,
        feature: &UnstableFeature,
        allowed: bool,
        extra_lines: Option<&str>,
    ) -> bool {
        let cfg = format!("unstable_{feature}");
        autocfg::emit_possibility(&cfg);

        let mut code = format!(
            r#"
#![deny(stable_features)]
#![feature({feature})]
#![allow(unused)]
"#
        );
        if let Some(extra_lines) = extra_lines {
            code.push('\n');
            code.push_str(extra_lines);
            code.push('\n');
        }

        if allowed && ac.probe_raw(&code).is_ok() {
            autocfg::emit(&cfg);
            true
        } else {
            false
        }
    }

    pub mod adt_const_params {
        pub const AVAILABLE: &str = r#"
use std::marker::ConstParamTy;
#[derive(ConstParamTy, PartialEq, Eq)]
struct Increment(i32);
struct Counter<const INC: Increment>(i32);
"#;
    }

    pub mod assert_matches {
        pub const AVAILABLE: &str = r#"
use std::assert_matches;
"#;
        pub const ROOT: &str = r#"
use std::assert_matches;

fn main() {
    assert_matches!(Some(4), Some(_));
}
"#;
        // was stabilised in root - so no need to remove feature from this probe
        pub const MODULE: &str = r#"
#![allow(stable_features)]
#![feature(assert_matches)]
use std::assert_matches::assert_matches;

fn main() {
    assert_matches!(Some(4), Some(_));
}
"#;
    }

    pub mod bool_to_result {
        pub const AVAILABLE: &str = r#"
fn main() {
    let _ = true.ok_or(());
}
"#;
    }

    pub mod can_vector {
        pub const AVAILABLE: &str = r#"
use std::io::Read;
fn main() {
    std::io::empty().is_read_vectored();
}
"#;
    }

    pub mod doc_notable_trait {
        pub const AVAILABLE: &str = r#"
#[doc(notable_trait)]
trait Foo {}
"#;
    }

    pub mod iterator_try_collect {
        // vec! not array: https://internals.rust-lang.org/t/code-compiles-on-playground-but-fails-when-passed-via-stdin-to-rustc/24393
        pub const AVAILABLE: &str = r#"
fn try_collect() {
    let _: Option<Vec<_>> = std::iter::Iterator::try_collect(&mut vec![Some(1)].into_iter());
}
"#;
    }

    pub mod never_type {
        pub const AVAILABLE: &str = r#"
type Bang = !;
"#;
    }

    pub mod proc_macro_diagnostic {
        pub const AVAILABLE: &str = r#"
extern crate proc_macro;
use proc_macro::Diagnostic;      
"#;
    }

    pub mod strip_circumfix {
        pub const AVAILABLE: &str = r#"
fn main() {
    let s = "foo";
    let _ = s.strip_circumfix("f", "o");
}
"#;
    }

    pub mod try_trait_v2 {
        pub const AVAILABLE: &str = r#"
use std::ops::Try;
"#;
    }

    pub mod try_trait_v2_residual {
        pub const AVAILABLE: &str = r#"
use std::ops::Residual;
"#;
    }

    pub mod unsized_const_params {
        // requires: feature(adt_const_params), allow(incomplete_features)
        // #![allow(stable_features)] may be duplicated by make_probe
        pub const AVAILABLE: &str = r#"
#![allow(clippy::duplicated_attributes)]
#![allow(stable_features)]
#![allow(incomplete_features)]
#![feature(adt_const_params)]
struct Foo<const N: &'static str>;
"#;
    }

    pub mod write_all_vectored {
        pub const AVAILABLE: &str = r#"
use std::io::{empty, Write, IoSlice};
fn main() {
    let buf: [u8;_] = [0];
    let slice = IoSlice::new(&buf);
    empty().write_all_vectored(&mut [slice]);
}
"#;
    }
}

/// Adds [`AutoCfg::emit_unstable_feature`](Nightly::emit_unstable_feature)
pub trait Nightly {
    /// Offers at least 2 cfg flags for all [known features](UnstableFeature)
    ///
    /// # Feature enablement: `cfg(unstable_...)`
    ///
    /// - To be used at top-level crate via `#![cfg_attr(unstable_foo, feature(foo))]`
    ///
    /// # Cfg-gating: `cfg(has_...)`
    ///
    /// - **Do not rely on `#[cfg(not(unstable_foo))]` to suggest that `feature(foo)` is stable!**
    /// - There are 3 reasons that `#[cfg(unstable_foo)]` could be `false`:
    ///     1. The build is using `stable`/`beta` or the feature is not on the `allow-features` whitelist
    ///     2. The feature has been stabilised
    ///     3. The compiler is from before the feature was implemented
    /// - All [known features](UnstableFeature) have a `#[cfg(has_...)]` for this purpose.
    ///
    /// # Note
    ///
    /// - You must pass a set of [AllowedFeatures], created by calling [cargo_allowed_features]
    /// - If you need to test that a feature is available in order to cfg-gate your code and it is not
    ///   on the list of [known features](UnstableFeature), please raise a PR with a suggested probe.
    /// - Returns `true` if `has_...` has been set. This means `OtherFeature` will always return `false`
    fn emit_unstable_feature(
        &self,
        feature: UnstableFeature,
        allowed_features: &AllowedFeatures,
    ) -> bool;

    /// Calls [`emit_unstable_feature`](Nightly::emit_unstable_feature) for all given features,
    /// additionally setting `has_{bundlename}` & returning `true` if all features are available.
    ///
    /// # Note
    ///
    /// - This will always return false if any of the features are
    ///   [`OtherFeature`](UnstableFeature::OtherFeature)
    fn emit_unstable_feature_bundle<F: IntoIterator<Item = UnstableFeature>>(
        &self,
        features: F,
        allowed_features: &AllowedFeatures,
        bundle_name: &str,
    ) -> bool;
}

impl Nightly for AutoCfg {
    fn emit_unstable_feature(
        &self,
        feature: UnstableFeature,
        allowed_features: &AllowedFeatures,
    ) -> bool {
        // show in `cargo build -vv`
        dbg!(&feature);

        let ac = self;
        let allowed = allowed_features.includes(&feature);
        match feature {
            UnstableFeature::adt_const_params => {
                unstable(ac, &feature, allowed, None);
                has(ac, &feature, allowed, probes::adt_const_params::AVAILABLE)
            }
            UnstableFeature::assert_matches => {
                unstable(self, &feature, allowed, None);
                autocfg::emit_possibility("assert_matches_location, values(\"root\", \"module\")");
                if self
                    .probe_raw(&make_probe(&feature, allowed, probes::assert_matches::ROOT))
                    .is_ok()
                {
                    autocfg::emit("assert_matches_location=\"root\"");
                } else if allowed && self.probe_raw(probes::assert_matches::MODULE).is_ok() {
                    //    ^^^^^^^ assert_matches was stabilised in root
                    autocfg::emit("assert_matches_location=\"module\"");
                }
                has(ac, &feature, allowed, probes::assert_matches::AVAILABLE)
            }
            UnstableFeature::bool_to_result => {
                unstable(ac, &feature, allowed, None);
                has(ac, &feature, allowed, probes::bool_to_result::AVAILABLE)
            }
            UnstableFeature::can_vector => {
                unstable(ac, &feature, allowed, None);
                has(ac, &feature, allowed, probes::can_vector::AVAILABLE)
            }
            UnstableFeature::doc_notable_trait => {
                unstable(ac, &feature, allowed, None);
                has(ac, &feature, allowed, probes::doc_notable_trait::AVAILABLE)
            }
            UnstableFeature::iterator_try_collect => {
                unstable(self, &feature, allowed, None);
                has(
                    ac,
                    &feature,
                    allowed,
                    probes::iterator_try_collect::AVAILABLE,
                )
            }
            UnstableFeature::never_type => {
                unstable(self, &feature, allowed, None);
                has(ac, &feature, allowed, probes::never_type::AVAILABLE)
            }
            UnstableFeature::proc_macro_diagnostic => {
                unstable(ac, &feature, allowed, Some("extern crate proc_macro;"));
                has(
                    ac,
                    &feature,
                    allowed,
                    probes::proc_macro_diagnostic::AVAILABLE,
                )
            }
            UnstableFeature::strip_circumfix => {
                unstable(ac, &feature, allowed, None);
                has(ac, &feature, allowed, probes::strip_circumfix::AVAILABLE)
            }
            UnstableFeature::try_trait_v2 => {
                unstable(self, &feature, allowed, None);
                has(ac, &feature, allowed, probes::try_trait_v2::AVAILABLE)
            }
            UnstableFeature::try_trait_v2_residual => {
                unstable(self, &feature, allowed, None);
                has(
                    ac,
                    &feature,
                    allowed,
                    probes::try_trait_v2_residual::AVAILABLE,
                )
            }
            UnstableFeature::unsized_const_params => {
                let extra_lines = if unstable(
                    ac,
                    &UnstableFeature::adt_const_params,
                    allowed_features.includes(&UnstableFeature::adt_const_params),
                    None,
                ) {
                    Some("#![feature(adt_const_params)]\n#![allow(incomplete_features)]")
                } else {
                    None
                };
                unstable(ac, &feature, allowed, extra_lines);
                has(
                    ac,
                    &feature,
                    allowed,
                    probes::unsized_const_params::AVAILABLE,
                )
            }
            UnstableFeature::write_all_vectored => {
                unstable(ac, &feature, allowed, None);
                has(ac, &feature, allowed, probes::write_all_vectored::AVAILABLE)
            }
            UnstableFeature::OtherFeature(_) => {
                unstable(self, &feature, allowed, None);
                false
            }
        }
    }

    fn emit_unstable_feature_bundle<F: IntoIterator<Item = UnstableFeature>>(
        &self,
        features: F,
        allowed_features: &AllowedFeatures,
        bundle_name: &str,
    ) -> bool {
        let cfg = format!("has_{bundle_name}");
        autocfg::emit_possibility(&cfg);

        let mut has = true;
        for feature in features.into_iter() {
            has = self.emit_unstable_feature(feature, allowed_features) && has;
        }

        if has {
            autocfg::emit(&cfg);
        }

        has
    }
}

/// Check whether cargo will accept unstable flags. You probably never need to run this
/// yourself and should prefer to simply call [`cargo_allowed_features`].
pub fn cargo_unstable() -> Result<bool> {
    let mut cmd = Command::new(get_var("CARGO")?);
    cmd.args([
        "-Zunstable-options",
        "--config",
        "unstable.allow-features=[\"unstable-options\"]",
        "help",
    ]);
    let output = cmd
        .output()
        .map_err(|err| BuildError::Other(err.to_string()))?;
    Ok(output.status.success())
}

fn cargo_config<P: AsRef<Path>>(
    current_dir: &Option<P>,
    added_unstable_options: bool,
) -> Result<Output> {
    let mut cargo_config_get = Command::new(get_var("CARGO")?);
    if let Some(dir) = &current_dir {
        cargo_config_get.current_dir(dir);
    }
    cargo_config_get.arg("-Zunstable-options");
    if added_unstable_options {
        cargo_config_get.args(["--config", "unstable.allow-features=[\"unstable-options\"]"]);
    }
    cargo_config_get.args(["config", "get"]);

    // show in `cargo build -vv`
    dbg!(&cargo_config_get);

    cargo_config_get
        .output()
        .map_err(|err| BuildError::Other(err.to_string()))
}

/// Identify which experimental features are allowed for this build.
///
/// This works fine on any channel and respects whitelists (`unstable.allowed-features`) in all
/// relevant cargo config.toml files.
///
/// ## Note to downstream crates
///
/// Due to limitations in the information provided by cargo:
///
/// - This will obtain config.toml files based upon `OUT_DIR`. If this is not under the project
///   root, you can override by providing an alternative path via the environment variable
///   `BUILD_SAFELY_CARGO_CONFIG_DIR`. See cargo's documentation on config file hierarchical structure
///   for more details.
/// - This will not respect additional entries passed at the command line via
///   `cargo --config unstable.allow-features=[...]`
pub fn cargo_allowed_features() -> Result<AllowedFeatures> {
    println!("cargo::rerun-if-env-changed=BUILD_SAFELY_CARGO_CONFIG_DIR");
    let cwd = std::env::var("BUILD_SAFELY_CARGO_CONFIG_DIR")
        .or_else(|_| std::env::var("OUT_DIR"))
        .ok();
    _cargo_allowed_features(cwd)
}

fn _cargo_allowed_features<P: AsRef<Path> + Debug>(
    current_dir: Option<P>,
) -> Result<AllowedFeatures> {
    if !cargo_unstable()? {
        // show in `cargo build -vv`
        dbg!("cargo won't accept `-Z` - so we're on a not-unstable toolchain");

        let allowed_features = AllowedFeatures(_AllowedFeatures::None);

        // show in `cargo build -vv`
        dbg!(&allowed_features);
        return Ok(allowed_features);
    }

    let mut added_unstable_options = false;
    let mut output = cargo_config(&current_dir, added_unstable_options)?;

    if !output.status.success() {
        // Maybe there is a restricted list which doesn't include unstable-options
        added_unstable_options = true;
        output = cargo_config(&current_dir, added_unstable_options)?;

        if !output.status.success() {
            // Nope something else went wrong!
            return Err(BuildError::Other(format!(
                "cargo config failed with error {code}: {stderr}",
                code = output.status,
                stderr = String::from_utf8_lossy(&output.stderr)
            )));
        }
    };

    let cargo_config = String::from_utf8_lossy(&output.stdout);

    let allowed_features = match cargo_config
        .lines()
        .find(|line| line.starts_with("unstable.allow-features"))
    {
        None => AllowedFeatures(_AllowedFeatures::All),
        Some(features) => {
            // default output format is toml
            let features: Vec<_> = features
                .strip_prefix("unstable.allow-features = [")
                .ok_or_else(|| {
                    BuildError::Other(format!(
                        "invalid cargo config output: {}",
                        String::from_utf8_lossy(&output.stdout)
                    ))
                })?
                .strip_suffix("]")
                .ok_or_else(|| {
                    BuildError::Other(format!(
                        "invalid cargo config output: {}",
                        String::from_utf8_lossy(&output.stdout)
                    ))
                })?
                .replace("\"", "")
                .split(",")
                .map(str::trim)
                .filter(|feature| !added_unstable_options || *feature != "unstable-options")
                .map(UnstableFeature::from)
                .collect();
            if features.is_empty() {
                AllowedFeatures(_AllowedFeatures::None)
            } else {
                AllowedFeatures(_AllowedFeatures::Some(features))
            }
        }
    };

    // show in `cargo build -vv`
    dbg!(&allowed_features);

    Ok(allowed_features)
}

/// The set of allowed experimental features for the current build. The only way to create this
/// is via a call to [cargo_allowed_features] - this is deliberate, to ensure that people who have
/// decided to restrict the experimental features they use to a whitelist are respected.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AllowedFeatures(_AllowedFeatures);

impl AllowedFeatures {
    /// Not public as this doesn't consider any restrictions made via `RUSTFLAGS`, those
    /// features will be disabled for all calls to rustc when running probes.
    fn includes(&self, feature: &UnstableFeature) -> bool {
        match &self.0 {
            _AllowedFeatures::None => false,
            _AllowedFeatures::All => true,
            _AllowedFeatures::Some(features) => features.iter().find(|f| *f == feature).is_some(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// private to make it impossible to manually construct an [AllowedFeatures] from outside this crate
enum _AllowedFeatures {
    None,
    All,
    Some(Vec<UnstableFeature>),
}

#[cfg(test)]
mod tests {
    use std::{
        assert_matches,
        fs::{self, File},
        io::Write,
    };

    use tempfile::TempDir;

    use super::UnstableFeature::*;
    use super::*;

    #[test]
    fn no_config_toml() {
        let tmp = TempDir::new().expect("tempdir");
        let allowed = _cargo_allowed_features(Some(&tmp));
        if cargo_unstable().expect("cargo_unstable") {
            assert_matches!(allowed, Ok(AllowedFeatures(_AllowedFeatures::All)));
            assert!(allowed.unwrap().includes(&try_trait_v2));
        } else {
            assert_matches!(allowed, Ok(AllowedFeatures(_AllowedFeatures::None)));
            assert!(!allowed.unwrap().includes(&try_trait_v2));
        }
    }

    #[test]
    fn allowed_features() {
        let tmp = TempDir::new().expect("tempdir");
        let config_location = tmp.path().join(".cargo");
        fs::create_dir(&config_location).expect(".cargo created");
        dbg!(&config_location);
        let mut config =
            File::create_new(config_location.join("config.toml")).expect("create config.toml");
        writeln!(
            config,
            "unstable.allow-features = [\"try_trait_v2\", \"unstable-options\"]"
        )
        .expect("added to config");

        let allowed = _cargo_allowed_features(Some(&tmp)).unwrap();
        if cargo_unstable().expect("cargo_unstable") {
            assert_matches!(
                allowed,
                AllowedFeatures(_AllowedFeatures::Some(ref features))
                if features == &vec![try_trait_v2, OtherFeature("unstable-options".to_string())]
            );
            assert!(allowed.includes(&try_trait_v2));
            assert!(allowed.includes(&OtherFeature("unstable-options".to_string())));
        } else {
            assert_matches!(allowed, AllowedFeatures(_AllowedFeatures::None));
            assert!(!allowed.includes(&try_trait_v2));
            assert!(!allowed.includes(&OtherFeature("unstable-options".to_string())));
        }
    }

    #[test]
    fn allowed_features_no_unstable_options() {
        let tmp = TempDir::new().expect("tempdir");
        let config_location = tmp.path().join(".cargo");
        fs::create_dir(&config_location).expect(".cargo created");
        dbg!(&config_location);
        let mut config =
            File::create_new(config_location.join("config.toml")).expect("create config.toml");
        writeln!(config, "unstable.allow-features = [\"try_trait_v2\"]").expect("added to config");

        let allowed = _cargo_allowed_features(Some(&tmp)).unwrap();
        if cargo_unstable().expect("cargo_unstable") {
            assert_matches!(
                allowed,
                AllowedFeatures(_AllowedFeatures::Some(ref features))
                if features == &vec![try_trait_v2]
            );
            assert!(allowed.includes(&try_trait_v2));
        } else {
            assert_matches!(allowed, AllowedFeatures(_AllowedFeatures::None));
            assert!(!allowed.includes(&try_trait_v2));
        }
    }

    #[test]
    fn all_forbidden() {
        let tmp = TempDir::new().expect("tempdir");
        let config_location = tmp.path().join(".cargo");
        fs::create_dir(&config_location).expect(".cargo created");
        dbg!(&config_location);
        let mut config =
            File::create_new(config_location.join("config.toml")).expect("create config.toml");
        writeln!(config, "unstable.allow-features = []").expect("added to config");

        let allowed = _cargo_allowed_features(Some(&tmp));
        assert_matches!(allowed, Ok(AllowedFeatures(_AllowedFeatures::None)));
    }

    #[test]
    fn make_assert_matches_probe() {
        let expected = r#"
#![allow(stable_features)]
#![feature(assert_matches)]
#![allow(unused)]

use std::assert_matches;
"#;
        let probe = r#"
use std::assert_matches;
"#;

        assert_eq!(probes::make_probe(&assert_matches, true, probe), expected);
    }

    #[test]
    fn unstable_feature_display() {
        assert_eq!(
            "foo",
            format!("{}", UnstableFeature::OtherFeature("foo".to_string()))
        );
        assert_eq!("try_trait_v2", format!("{}", UnstableFeature::try_trait_v2))
    }
}