Skip to content

Commit f7b05af

Browse files
committed
Auto merge of #64269 - Centril:rollup-y4dm32c, r=Centril
Rollup of 5 pull requests Successful merges: - #64052 (Rename test locals to work around LLDB bug) - #64066 (Support "soft" feature-gating using a lint) - #64177 (resolve: Do not afraid to set current module to enums and traits) - #64229 (Reduce span to function name in unreachable calls) - #64255 (Add methods for converting `bool` to `Option<T>`) Failed merges: r? @ghost
2 parents 43a5ff4 + cd3cb28 commit f7b05af

File tree

30 files changed

+185
-59
lines changed

30 files changed

+185
-59
lines changed

src/libcore/bool.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! impl bool {}
2+
3+
#[cfg(not(boostrap_stdarch_ignore_this))]
4+
#[lang = "bool"]
5+
impl bool {
6+
/// Returns `Some(t)` if the `bool` is `true`, or `None` otherwise.
7+
///
8+
/// # Examples
9+
///
10+
/// ```
11+
/// #![feature(bool_to_option)]
12+
///
13+
/// assert_eq!(false.then(0), None);
14+
/// assert_eq!(true.then(0), Some(0));
15+
/// ```
16+
#[unstable(feature = "bool_to_option", issue = "64260")]
17+
#[inline]
18+
pub fn then<T>(self, t: T) -> Option<T> {
19+
if self {
20+
Some(t)
21+
} else {
22+
None
23+
}
24+
}
25+
26+
/// Returns `Some(f())` if the `bool` is `true`, or `None` otherwise.
27+
///
28+
/// # Examples
29+
///
30+
/// ```
31+
/// #![feature(bool_to_option)]
32+
///
33+
/// assert_eq!(false.then_with(|| 0), None);
34+
/// assert_eq!(true.then_with(|| 0), Some(0));
35+
/// ```
36+
#[unstable(feature = "bool_to_option", issue = "64260")]
37+
#[inline]
38+
pub fn then_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
39+
if self {
40+
Some(f())
41+
} else {
42+
None
43+
}
44+
}
45+
}

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ pub mod task;
227227
pub mod alloc;
228228

229229
// note: does not need to be public
230+
mod bool;
230231
mod tuple;
231232
mod unit;
232233

src/libcore/macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,8 +1236,10 @@ pub(crate) mod builtin {
12361236
pub macro test($item:item) { /* compiler built-in */ }
12371237

12381238
/// Attribute macro applied to a function to turn it into a benchmark test.
1239-
#[unstable(feature = "test", issue = "50297",
1240-
reason = "`bench` is a part of custom test frameworks which are unstable")]
1239+
#[cfg_attr(not(boostrap_stdarch_ignore_this), unstable(soft, feature = "test", issue = "50297",
1240+
reason = "`bench` is a part of custom test frameworks which are unstable"))]
1241+
#[cfg_attr(boostrap_stdarch_ignore_this, unstable(feature = "test", issue = "50297",
1242+
reason = "`bench` is a part of custom test frameworks which are unstable"))]
12411243
#[allow_internal_unstable(test, rustc_attrs)]
12421244
#[rustc_builtin_macro]
12431245
pub macro bench($item:item) { /* compiler built-in */ }

src/libcore/tests/bool.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[test]
2+
fn test_bool_to_option() {
3+
assert_eq!(false.then(0), None);
4+
assert_eq!(true.then(0), Some(0));
5+
assert_eq!(false.then_with(|| 0), None);
6+
assert_eq!(true.then_with(|| 0), Some(0));
7+
}

src/libcore/tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(bool_to_option)]
12
#![feature(bound_cloned)]
23
#![feature(box_syntax)]
34
#![feature(cell_update)]
@@ -40,6 +41,7 @@ mod any;
4041
mod array;
4142
mod ascii;
4243
mod atomic;
44+
mod bool;
4345
mod cell;
4446
mod char;
4547
mod clone;

src/librustc/ich/impls_syntax.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ for ::syntax::attr::StabilityLevel {
115115
hasher: &mut StableHasher<W>) {
116116
mem::discriminant(self).hash_stable(hcx, hasher);
117117
match *self {
118-
::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue } => {
118+
::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue, ref is_soft } => {
119119
reason.hash_stable(hcx, hasher);
120120
issue.hash_stable(hcx, hasher);
121+
is_soft.hash_stable(hcx, hasher);
121122
}
122123
::syntax::attr::StabilityLevel::Stable { ref since } => {
123124
since.hash_stable(hcx, hasher);

src/librustc/lint/builtin.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ declare_lint! {
395395
"reservation of a two-phased borrow conflicts with other shared borrows"
396396
}
397397

398+
declare_lint! {
399+
pub SOFT_UNSTABLE,
400+
Deny,
401+
"a feature gate that doesn't break dependent crates"
402+
}
403+
398404
declare_lint_pass! {
399405
/// Does nothing as a lint pass, but registers some `Lint`s
400406
/// that are used by other parts of the compiler.
@@ -460,6 +466,7 @@ declare_lint_pass! {
460466
NESTED_IMPL_TRAIT,
461467
MUTABLE_BORROW_RESERVATION_CONFLICT,
462468
INDIRECT_STRUCTURAL_MATCH,
469+
SOFT_UNSTABLE,
463470
]
464471
}
465472

src/librustc/middle/lang_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> LanguageItems {
244244

245245
language_item_table! {
246246
// Variant name, Name, Method name, Target;
247+
BoolImplItem, "bool", bool_impl, Target::Impl;
247248
CharImplItem, "char", char_impl, Target::Impl;
248249
StrImplItem, "str", str_impl, Target::Impl;
249250
SliceImplItem, "slice", slice_impl, Target::Impl;

src/librustc/middle/stability.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ impl<'tcx> Index<'tcx> {
438438
level: attr::StabilityLevel::Unstable {
439439
reason: Some(Symbol::intern(reason)),
440440
issue: 27812,
441+
is_soft: false,
441442
},
442443
feature: sym::rustc_private,
443444
rustc_depr: None,
@@ -480,7 +481,7 @@ pub fn provide(providers: &mut Providers<'_>) {
480481
}
481482

482483
pub fn report_unstable(
483-
sess: &Session, feature: Symbol, reason: Option<Symbol>, issue: u32, span: Span
484+
sess: &Session, feature: Symbol, reason: Option<Symbol>, issue: u32, is_soft: bool, span: Span
484485
) {
485486
let msg = match reason {
486487
Some(r) => format!("use of unstable library feature '{}': {}", feature, r),
@@ -505,7 +506,13 @@ pub fn report_unstable(
505506
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
506507
let fresh = sess.one_time_diagnostics.borrow_mut().insert(error_id);
507508
if fresh {
508-
emit_feature_err(&sess.parse_sess, feature, span, GateIssue::Library(Some(issue)), &msg);
509+
if is_soft {
510+
sess.buffer_lint(lint::builtin::SOFT_UNSTABLE, CRATE_NODE_ID, span, &msg);
511+
} else {
512+
emit_feature_err(
513+
&sess.parse_sess, feature, span, GateIssue::Library(Some(issue)), &msg
514+
);
515+
}
509516
}
510517
}
511518

@@ -621,6 +628,7 @@ pub enum EvalResult {
621628
feature: Symbol,
622629
reason: Option<Symbol>,
623630
issue: u32,
631+
is_soft: bool,
624632
},
625633
/// The item does not have the `#[stable]` or `#[unstable]` marker assigned.
626634
Unmarked,
@@ -720,7 +728,9 @@ impl<'tcx> TyCtxt<'tcx> {
720728
}
721729

722730
match stability {
723-
Some(&Stability { level: attr::Unstable { reason, issue }, feature, .. }) => {
731+
Some(&Stability {
732+
level: attr::Unstable { reason, issue, is_soft }, feature, ..
733+
}) => {
724734
if span.allows_unstable(feature) {
725735
debug!("stability: skipping span={:?} since it is internal", span);
726736
return EvalResult::Allow;
@@ -744,7 +754,7 @@ impl<'tcx> TyCtxt<'tcx> {
744754
}
745755
}
746756

747-
EvalResult::Deny { feature, reason, issue }
757+
EvalResult::Deny { feature, reason, issue, is_soft }
748758
}
749759
Some(_) => {
750760
// Stable APIs are always ok to call and deprecated APIs are
@@ -767,8 +777,8 @@ impl<'tcx> TyCtxt<'tcx> {
767777
pub fn check_stability(self, def_id: DefId, id: Option<HirId>, span: Span) {
768778
match self.eval_stability(def_id, id, span) {
769779
EvalResult::Allow => {}
770-
EvalResult::Deny { feature, reason, issue } =>
771-
report_unstable(self.sess, feature, reason, issue, span),
780+
EvalResult::Deny { feature, reason, issue, is_soft } =>
781+
report_unstable(self.sess, feature, reason, issue, is_soft, span),
772782
EvalResult::Unmarked => {
773783
// The API could be uncallable for other reasons, for example when a private module
774784
// was referenced.

src/librustc_lint/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,12 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
435435
id: LintId::of(INDIRECT_STRUCTURAL_MATCH),
436436
reference: "issue #62411 <https://github.com/rust-lang/rust/issues/62411>",
437437
edition: None,
438-
}
438+
},
439+
FutureIncompatibleInfo {
440+
id: LintId::of(SOFT_UNSTABLE),
441+
reference: "issue #64266 <https://github.com/rust-lang/rust/issues/64266>",
442+
edition: None,
443+
},
439444
]);
440445

441446
// Register renamed and removed lints.

0 commit comments

Comments
 (0)