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

30 files changed

+185
-59
lines changed

src/libcore/bool.rs

+45
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

+1
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

+4-2
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

+7
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

+2
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

+2-1
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

+7
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

+1
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

+16-6
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

+6-1
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.

src/librustc_resolve/build_reduced_graph.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
728728
expansion,
729729
item.span);
730730
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
731+
self.parent_scope.module = module;
731732

732733
for variant in &(*enum_definition).variants {
733-
self.build_reduced_graph_for_variant(variant, module, vis);
734+
self.build_reduced_graph_for_variant(variant, vis);
734735
}
735736
}
736737

@@ -818,10 +819,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
818819

819820
// Constructs the reduced graph for one variant. Variants exist in the
820821
// type and value namespaces.
821-
fn build_reduced_graph_for_variant(&mut self,
822-
variant: &Variant,
823-
parent: Module<'a>,
824-
vis: ty::Visibility) {
822+
fn build_reduced_graph_for_variant(&mut self, variant: &Variant, vis: ty::Visibility) {
823+
let parent = self.parent_scope.module;
825824
let expn_id = self.parent_scope.expansion;
826825
let ident = variant.ident;
827826

@@ -1253,9 +1252,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12531252
let expansion = self.parent_scope.expansion;
12541253
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
12551254

1256-
self.parent_scope.module = parent.parent.unwrap(); // nearest normal ancestor
12571255
visit::walk_trait_item(self, item);
1258-
self.parent_scope.module = parent;
12591256
}
12601257

12611258
fn visit_token(&mut self, t: Token) {

src/librustc_resolve/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,11 @@ impl<'a> ModuleData<'a> {
535535
}
536536

537537
fn nearest_item_scope(&'a self) -> Module<'a> {
538-
if self.is_trait() { self.parent.unwrap() } else { self }
538+
match self.kind {
539+
ModuleKind::Def(DefKind::Enum, ..) | ModuleKind::Def(DefKind::Trait, ..) =>
540+
self.parent.expect("enum or trait module without a parent"),
541+
_ => self,
542+
}
539543
}
540544

541545
fn is_ancestor_of(&self, mut other: &Self) -> bool {
@@ -1637,7 +1641,7 @@ impl<'a> Resolver<'a> {
16371641
}
16381642

16391643
if let ModuleKind::Block(..) = module.kind {
1640-
return Some(module.parent.unwrap());
1644+
return Some(module.parent.unwrap().nearest_item_scope());
16411645
}
16421646

16431647
None

src/librustc_resolve/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -774,10 +774,10 @@ impl<'a> Resolver<'a> {
774774
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, path: &ast::Path) {
775775
let span = path.span;
776776
if let Some(stability) = &ext.stability {
777-
if let StabilityLevel::Unstable { reason, issue } = stability.level {
777+
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
778778
let feature = stability.feature;
779779
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
780-
stability::report_unstable(self.session, feature, reason, issue, span);
780+
stability::report_unstable(self.session, feature, reason, issue, is_soft, span);
781781
}
782782
}
783783
if let Some(depr) = &stability.rustc_depr {

src/librustc_typeck/check/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
161161
// Warn for non-block expressions with diverging children.
162162
match expr.node {
163163
ExprKind::Block(..) | ExprKind::Loop(..) | ExprKind::Match(..) => {},
164+
ExprKind::Call(ref callee, _) =>
165+
self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
166+
ExprKind::MethodCall(_, ref span, _) =>
167+
self.warn_if_unreachable(expr.hir_id, *span, "call"),
164168
_ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
165169
}
166170

src/librustc_typeck/check/method/probe.rs

+4
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
578578
ty::Param(p) => {
579579
self.assemble_inherent_candidates_from_param(p);
580580
}
581+
ty::Bool => {
582+
let lang_def_id = lang_items.bool_impl();
583+
self.assemble_inherent_impl_for_primitive(lang_def_id);
584+
}
581585
ty::Char => {
582586
let lang_def_id = lang_items.char_impl();
583587
self.assemble_inherent_impl_for_primitive(lang_def_id);

src/librustc_typeck/coherence/inherent_impls.rs

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
6767
ty::Dynamic(ref data, ..) if data.principal_def_id().is_some() => {
6868
self.check_def_id(item, data.principal_def_id().unwrap());
6969
}
70+
ty::Bool => {
71+
self.check_primitive_impl(def_id,
72+
lang_items.bool_impl(),
73+
None,
74+
"bool",
75+
"bool",
76+
item.span);
77+
}
7078
ty::Char => {
7179
self.check_primitive_impl(def_id,
7280
lang_items.char_impl(),

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3977,7 +3977,7 @@ fn build_deref_target_impls(cx: &DocContext<'_>,
39773977
F32 => tcx.lang_items().f32_impl(),
39783978
F64 => tcx.lang_items().f64_impl(),
39793979
Char => tcx.lang_items().char_impl(),
3980-
Bool => None,
3980+
Bool => tcx.lang_items().bool_impl(),
39813981
Str => tcx.lang_items().str_impl(),
39823982
Slice => tcx.lang_items().slice_impl(),
39833983
Array => tcx.lang_items().slice_impl(),

src/librustdoc/passes/collect_intra_doc_links.rs

+1
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
679679
"f32" => tcx.lang_items().f32_impl(),
680680
"f64" => tcx.lang_items().f64_impl(),
681681
"str" => tcx.lang_items().str_impl(),
682+
"bool" => tcx.lang_items().bool_impl(),
682683
"char" => tcx.lang_items().char_impl(),
683684
_ => None,
684685
}

src/librustdoc/passes/collect_trait_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
5353
lang_items.f64_impl(),
5454
lang_items.f32_runtime_impl(),
5555
lang_items.f64_runtime_impl(),
56+
lang_items.bool_impl(),
5657
lang_items.char_impl(),
5758
lang_items.str_impl(),
5859
lang_items.slice_impl(),

0 commit comments

Comments
 (0)