Skip to content

Commit

Permalink
Rollup merge of rust-lang#68018 - petrochenkov:nosoft, r=Centril
Browse files Browse the repository at this point in the history
feature_gate: Remove `GateStrength`

The "soft feature gating" from `feature_gate/check.rs` is unused, and even if it were used, hardcoded warning is not a good solution and [deny-by-default lint](rust-lang#64266) would be a better way to do this.

cc rust-lang#67806 (comment)
r? @Centril
  • Loading branch information
Centril authored Jan 11, 2020
2 parents 1691ee2 + 8e35c4f commit 71a719c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 74 deletions.
47 changes: 10 additions & 37 deletions src/librustc_ast_passes/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,25 @@ use rustc_span::Span;
use syntax::ast::{self, AssocTyConstraint, AssocTyConstraintKind, NodeId};
use syntax::ast::{GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData};
use syntax::attr;
use syntax::sess::{feature_err, leveled_feature_err, GateStrength, ParseSess};
use syntax::sess::{feature_err, feature_err_issue, ParseSess};
use syntax::visit::{self, FnKind, Visitor};

use log::debug;

macro_rules! gate_feature_fn {
($cx: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $level: expr) => {{
let (cx, has_feature, span, name, explain, level) =
(&*$cx, $has_feature, $span, $name, $explain, $level);
($cx: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{
let (cx, has_feature, span, name, explain) = (&*$cx, $has_feature, $span, $name, $explain);
let has_feature: bool = has_feature(&$cx.features);
debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
if !has_feature && !span.allows_unstable($name) {
leveled_feature_err(cx.parse_sess, name, span, GateIssue::Language, explain, level)
.emit();
feature_err_issue(cx.parse_sess, name, span, GateIssue::Language, explain).emit();
}
}};
}

macro_rules! gate_feature {
macro_rules! gate_feature_post {
($cx: expr, $feature: ident, $span: expr, $explain: expr) => {
gate_feature_fn!(
$cx,
|x: &Features| x.$feature,
$span,
sym::$feature,
$explain,
GateStrength::Hard
)
};
($cx: expr, $feature: ident, $span: expr, $explain: expr, $level: expr) => {
gate_feature_fn!($cx, |x: &Features| x.$feature, $span, sym::$feature, $explain, $level)
gate_feature_fn!($cx, |x: &Features| x.$feature, $span, sym::$feature, $explain)
};
}

Expand All @@ -51,21 +39,6 @@ struct PostExpansionVisitor<'a> {
features: &'a Features,
}

macro_rules! gate_feature_post {
($cx: expr, $feature: ident, $span: expr, $explain: expr) => {{
let (cx, span) = ($cx, $span);
if !span.allows_unstable(sym::$feature) {
gate_feature!(cx, $feature, span, $explain)
}
}};
($cx: expr, $feature: ident, $span: expr, $explain: expr, $level: expr) => {{
let (cx, span) = ($cx, $span);
if !span.allows_unstable(sym::$feature) {
gate_feature!(cx, $feature, span, $explain, $level)
}
}};
}

impl<'a> PostExpansionVisitor<'a> {
fn check_abi(&self, abi: ast::StrLit) {
let ast::StrLit { symbol_unescaped, span, .. } = abi;
Expand Down Expand Up @@ -257,15 +230,15 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
// Check feature gates for built-in attributes.
if let Some((.., AttributeGate::Gated(_, name, descr, has_feature))) = attr_info {
gate_feature_fn!(self, has_feature, attr.span, name, descr, GateStrength::Hard);
gate_feature_fn!(self, has_feature, attr.span, name, descr);
}
// Check unstable flavors of the `#[doc]` attribute.
if attr.check_name(sym::doc) {
for nested_meta in attr.meta_item_list().unwrap_or_default() {
macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
$(if nested_meta.check_name(sym::$name) {
let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
gate_feature!(self, $feature, attr.span, msg);
gate_feature_post!(self, $feature, attr.span, msg);
})*
}}

Expand Down Expand Up @@ -666,7 +639,7 @@ pub fn check_crate(
macro_rules! gate_all {
($gate:ident, $msg:literal) => {
for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
gate_feature!(&visitor, $gate, *span, $msg);
gate_feature_post!(&visitor, $gate, *span, $msg);
}
};
}
Expand All @@ -688,7 +661,7 @@ pub fn check_crate(
// disabling these uses of early feature-gatings.
if false {
for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
gate_feature!(&visitor, $gate, *span, $msg);
gate_feature_post!(&visitor, $gate, *span, $msg);
}
}
};
Expand Down
38 changes: 1 addition & 37 deletions src/librustc_session/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ impl GatedSpans {
}
}

/// The strenght of a feature gate.
/// Either it is a `Hard` error, or only a `Soft` warning.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum GateStrength {
/// A hard error. (Most feature gates should use this.)
Hard,
/// Only a warning. (Use this only as backwards-compatibility demands.)
Soft,
}

/// Construct a diagnostic for a language feature error due to the given `span`.
/// The `feature`'s `Symbol` is the one you used in `active.rs` and `rustc_span::symbols`.
pub fn feature_err<'a>(
Expand All @@ -94,26 +84,7 @@ pub fn feature_err_issue<'a>(
issue: GateIssue,
explain: &str,
) -> DiagnosticBuilder<'a> {
leveled_feature_err(sess, feature, span, issue, explain, GateStrength::Hard)
}

/// Construct a diagnostic for a feature gate error / warning.
///
/// You should typically just use `feature_err` instead.
pub fn leveled_feature_err<'a>(
sess: &'a ParseSess,
feature: Symbol,
span: impl Into<MultiSpan>,
issue: GateIssue,
explain: &str,
level: GateStrength,
) -> DiagnosticBuilder<'a> {
let diag = &sess.span_diagnostic;

let mut err = match level {
GateStrength::Hard => diag.struct_span_err_with_code(span, explain, error_code!(E0658)),
GateStrength::Soft => diag.struct_span_warn(span, explain),
};
let mut err = sess.span_diagnostic.struct_span_err_with_code(span, explain, error_code!(E0658));

if let Some(n) = find_feature_issue(feature, issue) {
err.note(&format!(
Expand All @@ -127,13 +98,6 @@ pub fn leveled_feature_err<'a>(
err.help(&format!("add `#![feature({})]` to the crate attributes to enable", feature));
}

// If we're on stable and only emitting a "soft" warning, add a note to
// clarify that the feature isn't "on" (rather than being on but
// warning-worthy).
if !sess.unstable_features.is_nightly_build() && level == GateStrength::Soft {
err.help("a nightly build of the compiler is required to enable this feature");
}

err
}

Expand Down

0 comments on commit 71a719c

Please sign in to comment.