gating diagnostics -> rustc_session::parse

This commit is contained in:
Mazdak Farrokhzad 2020-01-02 11:41:57 +01:00
parent 1756313117
commit 6007641d21
6 changed files with 77 additions and 76 deletions

View File

@ -3866,6 +3866,7 @@ dependencies = [
"log",
"num_cpus",
"rustc_data_structures",
"rustc_error_codes",
"rustc_errors",
"rustc_feature",
"rustc_fs_util",

View File

@ -10,6 +10,7 @@ path = "lib.rs"
[dependencies]
log = "0.4"
rustc_error_codes = { path = "../librustc_error_codes" }
rustc_errors = { path = "../librustc_errors" }
rustc_feature = { path = "../librustc_feature" }
rustc_target = { path = "../librustc_target" }

View File

@ -6,10 +6,10 @@ use crate::node_id::NodeId;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::{Lock, Lrc, Once};
use rustc_errors::{
emitter::SilentEmitter, Applicability, ColorConfig, DiagnosticBuilder, Handler,
};
use rustc_feature::UnstableFeatures;
use rustc_error_codes::E0658;
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
use rustc_errors::{error_code, Applicability, DiagnosticBuilder};
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
use rustc_span::edition::Edition;
use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{FilePathMapping, SourceMap};
@ -62,6 +62,70 @@ impl GatedSpans {
}
}
#[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,
}
pub fn feature_err<'a>(
sess: &'a ParseSess,
feature: Symbol,
span: impl Into<MultiSpan>,
explain: &str,
) -> DiagnosticBuilder<'a> {
feature_err_issue(sess, feature, span, GateIssue::Language, explain)
}
pub fn feature_err_issue<'a>(
sess: &'a ParseSess,
feature: Symbol,
span: impl Into<MultiSpan>,
issue: GateIssue,
explain: &str,
) -> DiagnosticBuilder<'a> {
leveled_feature_err(sess, feature, span, issue, explain, GateStrength::Hard)
}
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),
};
if let Some(n) = find_feature_issue(feature, issue) {
err.note(&format!(
"for more information, see https://github.com/rust-lang/rust/issues/{}",
n,
));
}
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
if sess.unstable_features.is_nightly_build() {
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
}
/// Info about a parsing session.
pub struct ParseSess {
pub span_diagnostic: Handler,

View File

@ -2,9 +2,8 @@
use super::{mark_used, MetaItemKind};
use crate::ast::{self, Attribute, MetaItem, NestedMetaItem};
use crate::feature_gate::feature_err;
use crate::print::pprust;
use crate::sess::ParseSess;
use crate::sess::{feature_err, ParseSess};
use rustc_errors::{struct_span_err, Applicability, Handler};
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};

View File

@ -1,22 +1,21 @@
use crate::ast::{self, AssocTyConstraint, AssocTyConstraintKind, NodeId};
use crate::ast::{GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData};
use crate::attr;
use crate::sess::ParseSess;
use crate::sess::{feature_err, leveled_feature_err, GateStrength, ParseSess};
use crate::visit::{self, FnKind, Visitor};
use rustc_data_structures::fx::FxHashMap;
use rustc_error_codes::*;
use rustc_errors::{error_code, struct_span_err, Applicability, DiagnosticBuilder, Handler};
use rustc_feature::{find_feature_issue, GateIssue};
use rustc_errors::{error_code, struct_span_err, Applicability, Handler};
use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP};
use rustc_feature::{Feature, Features, State as FeatureState, UnstableFeatures};
use rustc_feature::{Feature, Features, GateIssue, State as FeatureState, UnstableFeatures};
use rustc_feature::{
ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES, STABLE_REMOVED_FEATURES,
};
use rustc_span::edition::{Edition, ALL_EDITIONS};
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::{MultiSpan, Span, DUMMY_SP};
use rustc_span::{Span, DUMMY_SP};
use log::debug;
@ -53,70 +52,6 @@ pub fn check_attribute(attr: &ast::Attribute, parse_sess: &ParseSess, features:
PostExpansionVisitor { parse_sess, features }.visit_attribute(attr)
}
#[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,
}
pub fn feature_err<'a>(
sess: &'a ParseSess,
feature: Symbol,
span: impl Into<MultiSpan>,
explain: &str,
) -> DiagnosticBuilder<'a> {
feature_err_issue(sess, feature, span, GateIssue::Language, explain)
}
pub fn feature_err_issue<'a>(
sess: &'a ParseSess,
feature: Symbol,
span: impl Into<MultiSpan>,
issue: GateIssue,
explain: &str,
) -> DiagnosticBuilder<'a> {
leveled_feature_err(sess, feature, span, issue, explain, GateStrength::Hard)
}
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),
};
if let Some(n) = find_feature_issue(feature, issue) {
err.note(&format!(
"for more information, see https://github.com/rust-lang/rust/issues/{}",
n,
));
}
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
if sess.unstable_features.is_nightly_build() {
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
}
struct PostExpansionVisitor<'a> {
parse_sess: &'a ParseSess,
features: &'a Features,

View File

@ -77,7 +77,8 @@ pub mod entry;
pub mod expand;
pub mod feature_gate {
mod check;
pub use check::{check_attribute, check_crate, feature_err, feature_err_issue, get_features};
pub use check::{check_attribute, check_crate, get_features};
pub use rustc_session::parse::{feature_err, feature_err_issue};
}
pub mod mut_visit;
pub mod ptr;