Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #64269

Merged
merged 15 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/libcore/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! impl bool {}

#[cfg(not(boostrap_stdarch_ignore_this))]
#[lang = "bool"]
impl bool {
/// Returns `Some(t)` if the `bool` is `true`, or `None` otherwise.
///
/// # Examples
///
/// ```
/// #![feature(bool_to_option)]
///
/// assert_eq!(false.then(0), None);
/// assert_eq!(true.then(0), Some(0));
/// ```
#[unstable(feature = "bool_to_option", issue = "64260")]
#[inline]
pub fn then<T>(self, t: T) -> Option<T> {
if self {
Some(t)
} else {
None
}
}

/// Returns `Some(f())` if the `bool` is `true`, or `None` otherwise.
///
/// # Examples
///
/// ```
/// #![feature(bool_to_option)]
///
/// assert_eq!(false.then_with(|| 0), None);
/// assert_eq!(true.then_with(|| 0), Some(0));
/// ```
#[unstable(feature = "bool_to_option", issue = "64260")]
#[inline]
pub fn then_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
if self {
Some(f())
} else {
None
}
}
}
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub mod task;
pub mod alloc;

// note: does not need to be public
mod bool;
mod tuple;
mod unit;

Expand Down
6 changes: 4 additions & 2 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,8 +1236,10 @@ pub(crate) mod builtin {
pub macro test($item:item) { /* compiler built-in */ }

/// Attribute macro applied to a function to turn it into a benchmark test.
#[unstable(feature = "test", issue = "50297",
reason = "`bench` is a part of custom test frameworks which are unstable")]
#[cfg_attr(not(boostrap_stdarch_ignore_this), unstable(soft, feature = "test", issue = "50297",
reason = "`bench` is a part of custom test frameworks which are unstable"))]
#[cfg_attr(boostrap_stdarch_ignore_this, unstable(feature = "test", issue = "50297",
reason = "`bench` is a part of custom test frameworks which are unstable"))]
#[allow_internal_unstable(test, rustc_attrs)]
#[rustc_builtin_macro]
pub macro bench($item:item) { /* compiler built-in */ }
Expand Down
7 changes: 7 additions & 0 deletions src/libcore/tests/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[test]
fn test_bool_to_option() {
assert_eq!(false.then(0), None);
assert_eq!(true.then(0), Some(0));
assert_eq!(false.then_with(|| 0), None);
assert_eq!(true.then_with(|| 0), Some(0));
}
2 changes: 2 additions & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(bool_to_option)]
#![feature(bound_cloned)]
#![feature(box_syntax)]
#![feature(cell_update)]
Expand Down Expand Up @@ -40,6 +41,7 @@ mod any;
mod array;
mod ascii;
mod atomic;
mod bool;
mod cell;
mod char;
mod clone;
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ for ::syntax::attr::StabilityLevel {
hasher: &mut StableHasher<W>) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue } => {
::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue, ref is_soft } => {
reason.hash_stable(hcx, hasher);
issue.hash_stable(hcx, hasher);
is_soft.hash_stable(hcx, hasher);
}
::syntax::attr::StabilityLevel::Stable { ref since } => {
since.hash_stable(hcx, hasher);
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ declare_lint! {
"reservation of a two-phased borrow conflicts with other shared borrows"
}

declare_lint! {
pub SOFT_UNSTABLE,
Deny,
"a feature gate that doesn't break dependent crates"
}

declare_lint_pass! {
/// Does nothing as a lint pass, but registers some `Lint`s
/// that are used by other parts of the compiler.
Expand Down Expand Up @@ -460,6 +466,7 @@ declare_lint_pass! {
NESTED_IMPL_TRAIT,
MUTABLE_BORROW_RESERVATION_CONFLICT,
INDIRECT_STRUCTURAL_MATCH,
SOFT_UNSTABLE,
]
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> LanguageItems {

language_item_table! {
// Variant name, Name, Method name, Target;
BoolImplItem, "bool", bool_impl, Target::Impl;
CharImplItem, "char", char_impl, Target::Impl;
StrImplItem, "str", str_impl, Target::Impl;
SliceImplItem, "slice", slice_impl, Target::Impl;
Expand Down
22 changes: 16 additions & 6 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ impl<'tcx> Index<'tcx> {
level: attr::StabilityLevel::Unstable {
reason: Some(Symbol::intern(reason)),
issue: 27812,
is_soft: false,
},
feature: sym::rustc_private,
rustc_depr: None,
Expand Down Expand Up @@ -480,7 +481,7 @@ pub fn provide(providers: &mut Providers<'_>) {
}

pub fn report_unstable(
sess: &Session, feature: Symbol, reason: Option<Symbol>, issue: u32, span: Span
sess: &Session, feature: Symbol, reason: Option<Symbol>, issue: u32, is_soft: bool, span: Span
) {
let msg = match reason {
Some(r) => format!("use of unstable library feature '{}': {}", feature, r),
Expand All @@ -505,7 +506,13 @@ pub fn report_unstable(
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
let fresh = sess.one_time_diagnostics.borrow_mut().insert(error_id);
if fresh {
emit_feature_err(&sess.parse_sess, feature, span, GateIssue::Library(Some(issue)), &msg);
if is_soft {
sess.buffer_lint(lint::builtin::SOFT_UNSTABLE, CRATE_NODE_ID, span, &msg);
} else {
emit_feature_err(
&sess.parse_sess, feature, span, GateIssue::Library(Some(issue)), &msg
);
}
}
}

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

match stability {
Some(&Stability { level: attr::Unstable { reason, issue }, feature, .. }) => {
Some(&Stability {
level: attr::Unstable { reason, issue, is_soft }, feature, ..
}) => {
if span.allows_unstable(feature) {
debug!("stability: skipping span={:?} since it is internal", span);
return EvalResult::Allow;
Expand All @@ -744,7 +754,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

EvalResult::Deny { feature, reason, issue }
EvalResult::Deny { feature, reason, issue, is_soft }
}
Some(_) => {
// Stable APIs are always ok to call and deprecated APIs are
Expand All @@ -767,8 +777,8 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn check_stability(self, def_id: DefId, id: Option<HirId>, span: Span) {
match self.eval_stability(def_id, id, span) {
EvalResult::Allow => {}
EvalResult::Deny { feature, reason, issue } =>
report_unstable(self.sess, feature, reason, issue, span),
EvalResult::Deny { feature, reason, issue, is_soft } =>
report_unstable(self.sess, feature, reason, issue, is_soft, span),
EvalResult::Unmarked => {
// The API could be uncallable for other reasons, for example when a private module
// was referenced.
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,12 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
id: LintId::of(INDIRECT_STRUCTURAL_MATCH),
reference: "issue #62411 <https://github.com/rust-lang/rust/issues/62411>",
edition: None,
}
},
FutureIncompatibleInfo {
id: LintId::of(SOFT_UNSTABLE),
reference: "issue #64266 <https://github.com/rust-lang/rust/issues/64266>",
edition: None,
},
]);

// Register renamed and removed lints.
Expand Down
11 changes: 4 additions & 7 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
expansion,
item.span);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.parent_scope.module = module;

for variant in &(*enum_definition).variants {
self.build_reduced_graph_for_variant(variant, module, vis);
self.build_reduced_graph_for_variant(variant, vis);
}
}

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

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

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

self.parent_scope.module = parent.parent.unwrap(); // nearest normal ancestor
visit::walk_trait_item(self, item);
self.parent_scope.module = parent;
}

fn visit_token(&mut self, t: Token) {
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,11 @@ impl<'a> ModuleData<'a> {
}

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

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

if let ModuleKind::Block(..) = module.kind {
return Some(module.parent.unwrap());
return Some(module.parent.unwrap().nearest_item_scope());
}

None
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,10 @@ impl<'a> Resolver<'a> {
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, path: &ast::Path) {
let span = path.span;
if let Some(stability) = &ext.stability {
if let StabilityLevel::Unstable { reason, issue } = stability.level {
if let StabilityLevel::Unstable { reason, issue, is_soft } = stability.level {
let feature = stability.feature;
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
stability::report_unstable(self.session, feature, reason, issue, span);
stability::report_unstable(self.session, feature, reason, issue, is_soft, span);
}
}
if let Some(depr) = &stability.rustc_depr {
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Warn for non-block expressions with diverging children.
match expr.node {
ExprKind::Block(..) | ExprKind::Loop(..) | ExprKind::Match(..) => {},
ExprKind::Call(ref callee, _) =>
self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
ExprKind::MethodCall(_, ref span, _) =>
self.warn_if_unreachable(expr.hir_id, *span, "call"),
_ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
}

Expand Down
4 changes: 4 additions & 0 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
ty::Param(p) => {
self.assemble_inherent_candidates_from_param(p);
}
ty::Bool => {
let lang_def_id = lang_items.bool_impl();
self.assemble_inherent_impl_for_primitive(lang_def_id);
}
ty::Char => {
let lang_def_id = lang_items.char_impl();
self.assemble_inherent_impl_for_primitive(lang_def_id);
Expand Down
8 changes: 8 additions & 0 deletions src/librustc_typeck/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
ty::Dynamic(ref data, ..) if data.principal_def_id().is_some() => {
self.check_def_id(item, data.principal_def_id().unwrap());
}
ty::Bool => {
self.check_primitive_impl(def_id,
lang_items.bool_impl(),
None,
"bool",
"bool",
item.span);
}
ty::Char => {
self.check_primitive_impl(def_id,
lang_items.char_impl(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3977,7 +3977,7 @@ fn build_deref_target_impls(cx: &DocContext<'_>,
F32 => tcx.lang_items().f32_impl(),
F64 => tcx.lang_items().f64_impl(),
Char => tcx.lang_items().char_impl(),
Bool => None,
Bool => tcx.lang_items().bool_impl(),
Str => tcx.lang_items().str_impl(),
Slice => tcx.lang_items().slice_impl(),
Array => tcx.lang_items().slice_impl(),
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
"f32" => tcx.lang_items().f32_impl(),
"f64" => tcx.lang_items().f64_impl(),
"str" => tcx.lang_items().str_impl(),
"bool" => tcx.lang_items().bool_impl(),
"char" => tcx.lang_items().char_impl(),
_ => None,
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/passes/collect_trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
lang_items.f64_impl(),
lang_items.f32_runtime_impl(),
lang_items.f64_runtime_impl(),
lang_items.bool_impl(),
lang_items.char_impl(),
lang_items.str_impl(),
lang_items.slice_impl(),
Expand Down
Loading