Skip to content

Commit 6007641

Browse files
committed
gating diagnostics -> rustc_session::parse
1 parent 1756313 commit 6007641

File tree

6 files changed

+77
-76
lines changed

6 files changed

+77
-76
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,6 +3866,7 @@ dependencies = [
38663866
"log",
38673867
"num_cpus",
38683868
"rustc_data_structures",
3869+
"rustc_error_codes",
38693870
"rustc_errors",
38703871
"rustc_feature",
38713872
"rustc_fs_util",

src/librustc_session/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ path = "lib.rs"
1010

1111
[dependencies]
1212
log = "0.4"
13+
rustc_error_codes = { path = "../librustc_error_codes" }
1314
rustc_errors = { path = "../librustc_errors" }
1415
rustc_feature = { path = "../librustc_feature" }
1516
rustc_target = { path = "../librustc_target" }

src/librustc_session/parse.rs

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::node_id::NodeId;
66

77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
88
use rustc_data_structures::sync::{Lock, Lrc, Once};
9-
use rustc_errors::{
10-
emitter::SilentEmitter, Applicability, ColorConfig, DiagnosticBuilder, Handler,
11-
};
12-
use rustc_feature::UnstableFeatures;
9+
use rustc_error_codes::E0658;
10+
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
11+
use rustc_errors::{error_code, Applicability, DiagnosticBuilder};
12+
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
1313
use rustc_span::edition::Edition;
1414
use rustc_span::hygiene::ExpnId;
1515
use rustc_span::source_map::{FilePathMapping, SourceMap};
@@ -62,6 +62,70 @@ impl GatedSpans {
6262
}
6363
}
6464

65+
#[derive(Debug, Copy, Clone, PartialEq)]
66+
pub enum GateStrength {
67+
/// A hard error. (Most feature gates should use this.)
68+
Hard,
69+
/// Only a warning. (Use this only as backwards-compatibility demands.)
70+
Soft,
71+
}
72+
73+
pub fn feature_err<'a>(
74+
sess: &'a ParseSess,
75+
feature: Symbol,
76+
span: impl Into<MultiSpan>,
77+
explain: &str,
78+
) -> DiagnosticBuilder<'a> {
79+
feature_err_issue(sess, feature, span, GateIssue::Language, explain)
80+
}
81+
82+
pub fn feature_err_issue<'a>(
83+
sess: &'a ParseSess,
84+
feature: Symbol,
85+
span: impl Into<MultiSpan>,
86+
issue: GateIssue,
87+
explain: &str,
88+
) -> DiagnosticBuilder<'a> {
89+
leveled_feature_err(sess, feature, span, issue, explain, GateStrength::Hard)
90+
}
91+
92+
pub fn leveled_feature_err<'a>(
93+
sess: &'a ParseSess,
94+
feature: Symbol,
95+
span: impl Into<MultiSpan>,
96+
issue: GateIssue,
97+
explain: &str,
98+
level: GateStrength,
99+
) -> DiagnosticBuilder<'a> {
100+
let diag = &sess.span_diagnostic;
101+
102+
let mut err = match level {
103+
GateStrength::Hard => diag.struct_span_err_with_code(span, explain, error_code!(E0658)),
104+
GateStrength::Soft => diag.struct_span_warn(span, explain),
105+
};
106+
107+
if let Some(n) = find_feature_issue(feature, issue) {
108+
err.note(&format!(
109+
"for more information, see https://github.com/rust-lang/rust/issues/{}",
110+
n,
111+
));
112+
}
113+
114+
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
115+
if sess.unstable_features.is_nightly_build() {
116+
err.help(&format!("add `#![feature({})]` to the crate attributes to enable", feature));
117+
}
118+
119+
// If we're on stable and only emitting a "soft" warning, add a note to
120+
// clarify that the feature isn't "on" (rather than being on but
121+
// warning-worthy).
122+
if !sess.unstable_features.is_nightly_build() && level == GateStrength::Soft {
123+
err.help("a nightly build of the compiler is required to enable this feature");
124+
}
125+
126+
err
127+
}
128+
65129
/// Info about a parsing session.
66130
pub struct ParseSess {
67131
pub span_diagnostic: Handler,

src/libsyntax/attr/builtin.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
33
use super::{mark_used, MetaItemKind};
44
use crate::ast::{self, Attribute, MetaItem, NestedMetaItem};
5-
use crate::feature_gate::feature_err;
65
use crate::print::pprust;
7-
use crate::sess::ParseSess;
6+
use crate::sess::{feature_err, ParseSess};
87

98
use rustc_errors::{struct_span_err, Applicability, Handler};
109
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};

src/libsyntax/feature_gate/check.rs

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
use crate::ast::{self, AssocTyConstraint, AssocTyConstraintKind, NodeId};
22
use crate::ast::{GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData};
33
use crate::attr;
4-
use crate::sess::ParseSess;
4+
use crate::sess::{feature_err, leveled_feature_err, GateStrength, ParseSess};
55
use crate::visit::{self, FnKind, Visitor};
66

77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_error_codes::*;
9-
use rustc_errors::{error_code, struct_span_err, Applicability, DiagnosticBuilder, Handler};
10-
use rustc_feature::{find_feature_issue, GateIssue};
9+
use rustc_errors::{error_code, struct_span_err, Applicability, Handler};
1110
use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP};
12-
use rustc_feature::{Feature, Features, State as FeatureState, UnstableFeatures};
11+
use rustc_feature::{Feature, Features, GateIssue, State as FeatureState, UnstableFeatures};
1312
use rustc_feature::{
1413
ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES, STABLE_REMOVED_FEATURES,
1514
};
1615
use rustc_span::edition::{Edition, ALL_EDITIONS};
1716
use rustc_span::source_map::Spanned;
1817
use rustc_span::symbol::{sym, Symbol};
19-
use rustc_span::{MultiSpan, Span, DUMMY_SP};
18+
use rustc_span::{Span, DUMMY_SP};
2019

2120
use log::debug;
2221

@@ -53,70 +52,6 @@ pub fn check_attribute(attr: &ast::Attribute, parse_sess: &ParseSess, features:
5352
PostExpansionVisitor { parse_sess, features }.visit_attribute(attr)
5453
}
5554

56-
#[derive(Debug, Copy, Clone, PartialEq)]
57-
pub enum GateStrength {
58-
/// A hard error. (Most feature gates should use this.)
59-
Hard,
60-
/// Only a warning. (Use this only as backwards-compatibility demands.)
61-
Soft,
62-
}
63-
64-
pub fn feature_err<'a>(
65-
sess: &'a ParseSess,
66-
feature: Symbol,
67-
span: impl Into<MultiSpan>,
68-
explain: &str,
69-
) -> DiagnosticBuilder<'a> {
70-
feature_err_issue(sess, feature, span, GateIssue::Language, explain)
71-
}
72-
73-
pub fn feature_err_issue<'a>(
74-
sess: &'a ParseSess,
75-
feature: Symbol,
76-
span: impl Into<MultiSpan>,
77-
issue: GateIssue,
78-
explain: &str,
79-
) -> DiagnosticBuilder<'a> {
80-
leveled_feature_err(sess, feature, span, issue, explain, GateStrength::Hard)
81-
}
82-
83-
fn leveled_feature_err<'a>(
84-
sess: &'a ParseSess,
85-
feature: Symbol,
86-
span: impl Into<MultiSpan>,
87-
issue: GateIssue,
88-
explain: &str,
89-
level: GateStrength,
90-
) -> DiagnosticBuilder<'a> {
91-
let diag = &sess.span_diagnostic;
92-
93-
let mut err = match level {
94-
GateStrength::Hard => diag.struct_span_err_with_code(span, explain, error_code!(E0658)),
95-
GateStrength::Soft => diag.struct_span_warn(span, explain),
96-
};
97-
98-
if let Some(n) = find_feature_issue(feature, issue) {
99-
err.note(&format!(
100-
"for more information, see https://github.com/rust-lang/rust/issues/{}",
101-
n,
102-
));
103-
}
104-
105-
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
106-
if sess.unstable_features.is_nightly_build() {
107-
err.help(&format!("add `#![feature({})]` to the crate attributes to enable", feature));
108-
}
109-
110-
// If we're on stable and only emitting a "soft" warning, add a note to
111-
// clarify that the feature isn't "on" (rather than being on but
112-
// warning-worthy).
113-
if !sess.unstable_features.is_nightly_build() && level == GateStrength::Soft {
114-
err.help("a nightly build of the compiler is required to enable this feature");
115-
}
116-
117-
err
118-
}
119-
12055
struct PostExpansionVisitor<'a> {
12156
parse_sess: &'a ParseSess,
12257
features: &'a Features,

src/libsyntax/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ pub mod entry;
7777
pub mod expand;
7878
pub mod feature_gate {
7979
mod check;
80-
pub use check::{check_attribute, check_crate, feature_err, feature_err_issue, get_features};
80+
pub use check::{check_attribute, check_crate, get_features};
81+
pub use rustc_session::parse::{feature_err, feature_err_issue};
8182
}
8283
pub mod mut_visit;
8384
pub mod ptr;

0 commit comments

Comments
 (0)