Skip to content

Commit c457abe

Browse files
committed
migrate lib_features.rs to translateable diagnostics
1 parent 40d5f00 commit c457abe

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,9 @@ passes_unrecognized_field =
420420
421421
passes_layout =
422422
layout error: {$layout_error}
423+
424+
passes_feature_stable_twice =
425+
feature `{$feature}` is declared stable since {$since}, but was previously declared stable since {$prev_since}
426+
427+
passes_feature_previously_declared =
428+
feature `{$feature}` is declared {$declared}, but was previously declared {$prev_declared}

compiler/rustc_passes/src/errors.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,3 +832,23 @@ pub struct Layout {
832832
pub span: Span,
833833
pub layout_error: String,
834834
}
835+
836+
#[derive(Diagnostic)]
837+
#[diag(passes::feature_stable_twice, code = "E0711")]
838+
pub struct FeatureStableTwice {
839+
#[primary_span]
840+
pub span: Span,
841+
pub feature: Symbol,
842+
pub since: Symbol,
843+
pub prev_since: Symbol,
844+
}
845+
846+
#[derive(Diagnostic)]
847+
#[diag(passes::feature_previously_declared, code = "E0711")]
848+
pub struct FeaturePreviouslyDeclared<'a, 'b> {
849+
#[primary_span]
850+
pub span: Span,
851+
pub feature: Symbol,
852+
pub declared: &'a str,
853+
pub prev_declared: &'b str,
854+
}

compiler/rustc_passes/src/lib_features.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
use rustc_ast::{Attribute, MetaItemKind};
88
use rustc_attr::{rust_version_symbol, VERSION_PLACEHOLDER};
9-
use rustc_errors::struct_span_err;
109
use rustc_hir::intravisit::Visitor;
1110
use rustc_middle::hir::nested_filter;
1211
use rustc_middle::middle::lib_features::LibFeatures;
@@ -15,6 +14,8 @@ use rustc_middle::ty::TyCtxt;
1514
use rustc_span::symbol::Symbol;
1615
use rustc_span::{sym, Span};
1716

17+
use crate::errors::{FeaturePreviouslyDeclared, FeatureStableTwice};
18+
1819
fn new_lib_features() -> LibFeatures {
1920
LibFeatures { stable: Default::default(), unstable: Default::default() }
2021
}
@@ -92,14 +93,12 @@ impl<'tcx> LibFeatureCollector<'tcx> {
9293
(Some(since), _, false) => {
9394
if let Some((prev_since, _)) = self.lib_features.stable.get(&feature) {
9495
if *prev_since != since {
95-
self.span_feature_error(
96+
self.tcx.sess.emit_err(FeatureStableTwice {
9697
span,
97-
&format!(
98-
"feature `{}` is declared stable since {}, \
99-
but was previously declared stable since {}",
100-
feature, since, prev_since,
101-
),
102-
);
98+
feature,
99+
since,
100+
prev_since: *prev_since,
101+
});
103102
return;
104103
}
105104
}
@@ -110,22 +109,17 @@ impl<'tcx> LibFeatureCollector<'tcx> {
110109
self.lib_features.unstable.insert(feature, span);
111110
}
112111
(Some(_), _, true) | (None, true, _) => {
113-
self.span_feature_error(
112+
let declared = if since.is_some() { "stable" } else { "unstable" };
113+
let prev_declared = if since.is_none() { "stable" } else { "unstable" };
114+
self.tcx.sess.emit_err(FeaturePreviouslyDeclared {
114115
span,
115-
&format!(
116-
"feature `{}` is declared {}, but was previously declared {}",
117-
feature,
118-
if since.is_some() { "stable" } else { "unstable" },
119-
if since.is_none() { "stable" } else { "unstable" },
120-
),
121-
);
116+
feature,
117+
declared,
118+
prev_declared,
119+
});
122120
}
123121
}
124122
}
125-
126-
fn span_feature_error(&self, span: Span, msg: &str) {
127-
struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg).emit();
128-
}
129123
}
130124

131125
impl<'tcx> Visitor<'tcx> for LibFeatureCollector<'tcx> {

0 commit comments

Comments
 (0)