Skip to content

Commit 91fdcb2

Browse files
committed
Use EmitterWithNote and allow warnings, adjust wording for --cfg/--check-cfg error
1 parent 841ed1a commit 91fdcb2

23 files changed

+50
-61
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -534,28 +534,24 @@ impl Emitter for HumanEmitter {
534534
}
535535
}
536536

537-
/// An emitter that does nothing when emitting a non-error diagnostic.
538-
/// Error diagnostics are forwarded to `error_emitter` to avoid silent
539-
/// failures of rustc, as witnessed e.g. in issue #89358.
540-
pub struct ErrorOnlyEmitter {
541-
pub error_emitter: Box<dyn Emitter + DynSend>,
542-
pub error_note: String,
537+
/// An emitter that adds a note to each diagnostic.
538+
pub struct EmitterWithNote {
539+
pub emitter: Box<dyn Emitter + DynSend>,
540+
pub note: String,
543541
}
544542

545-
impl Emitter for ErrorOnlyEmitter {
543+
impl Emitter for EmitterWithNote {
546544
fn source_map(&self) -> Option<&SourceMap> {
547545
None
548546
}
549547

550548
fn emit_diagnostic(&mut self, mut diag: DiagInner, registry: &Registry) {
551-
if diag.is_error() {
552-
diag.sub(Level::Note, self.error_note.clone(), MultiSpan::new());
553-
self.error_emitter.emit_diagnostic(diag, registry);
554-
}
549+
diag.sub(Level::Note, self.note.clone(), MultiSpan::new());
550+
self.emitter.emit_diagnostic(diag, registry);
555551
}
556552

557553
fn translator(&self) -> &Translator {
558-
self.error_emitter.translator()
554+
self.emitter.translator()
559555
}
560556
}
561557

compiler/rustc_interface/src/interface.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,17 @@ pub struct Compiler {
5353
pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
5454
cfgs.into_iter()
5555
.map(|s| {
56-
let psess = ParseSess::with_error_emitter(
56+
let psess = ParseSess::emitter_with_note(
5757
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
58-
format!("this error occurred on the command line: `--cfg={s}`"),
58+
format!("this occurred on the command line: `--cfg={s}`"),
5959
);
6060
let filename = FileName::cfg_spec_source_code(&s);
6161

6262
macro_rules! error {
6363
($reason: expr) => {
6464
#[allow(rustc::untranslatable_diagnostic)]
6565
#[allow(rustc::diagnostic_outside_of_impl)]
66-
dcx.fatal(format!(
67-
"invalid `--cfg` argument: `{}` ({})",
68-
s, $reason,
69-
));
66+
dcx.fatal(format!("invalid `--cfg` argument: `{}` ({})", s, $reason,));
7067
};
7168
}
7269

@@ -88,7 +85,7 @@ pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
8885
let ident = meta_item.ident().expect("multi-segment cfg key");
8986

9087
if !ident.name.can_be_raw() {
91-
error!(format!("argument key must be an identifier, but `{}` cannot be a raw identifier", ident.name));
88+
error!("argument key must be an identifier");
9289
}
9390

9491
return (ident.name, meta_item.value_str());
@@ -98,9 +95,9 @@ pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
9895
Ok(..) => {}
9996
Err(err) => {
10097
err.cancel();
101-
},
98+
}
10299
}
103-
},
100+
}
104101
Err(errs) => errs.into_iter().for_each(|err| {
105102
err.cancel();
106103
}),
@@ -129,9 +126,9 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
129126
let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() };
130127

131128
for s in specs {
132-
let psess = ParseSess::with_error_emitter(
129+
let psess = ParseSess::emitter_with_note(
133130
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
134-
format!("this error occurred on the command line: `--check-cfg={s}`"),
131+
format!("this occurred on the command line: `--check-cfg={s}`"),
135132
);
136133
let filename = FileName::cfg_spec_source_code(&s);
137134

@@ -226,10 +223,7 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
226223
}
227224

228225
if !ident.name.can_be_raw() {
229-
error!(format!(
230-
"argument key must be an identifier, but `{}` cannot be a raw identifier",
231-
ident.name
232-
));
226+
error!("`cfg()` names must be identifiers");
233227
}
234228

235229
names.push(ident);

compiler/rustc_session/src/parse.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::attr::AttrIdGenerator;
88
use rustc_ast::node_id::NodeId;
99
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
1010
use rustc_data_structures::sync::{AppendOnlyVec, Lock};
11-
use rustc_errors::emitter::{ErrorOnlyEmitter, HumanEmitter, stderr_destination};
11+
use rustc_errors::emitter::{EmitterWithNote, HumanEmitter, stderr_destination};
1212
use rustc_errors::translation::Translator;
1313
use rustc_errors::{
1414
BufferedEarlyLint, ColorConfig, DecorateDiagCompat, Diag, DiagCtxt, DiagCtxtHandle,
@@ -315,13 +315,12 @@ impl ParseSess {
315315
}
316316
}
317317

318-
pub fn with_error_emitter(locale_resources: Vec<&'static str>, error_note: String) -> Self {
318+
pub fn emitter_with_note(locale_resources: Vec<&'static str>, note: String) -> Self {
319319
let translator = Translator::with_fallback_bundle(locale_resources, false);
320320
let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
321321
let emitter =
322322
Box::new(HumanEmitter::new(stderr_destination(ColorConfig::Auto), translator));
323-
let dcx = DiagCtxt::new(Box::new(ErrorOnlyEmitter { error_emitter: emitter, error_note }))
324-
.disable_warnings();
323+
let dcx = DiagCtxt::new(Box::new(EmitterWithNote { emitter, note }));
325324
ParseSess::with_dcx(dcx, sm)
326325
}
327326

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: invalid `--cfg` argument: `crate` (argument key must be an identifier, but `crate` cannot be a raw identifier)
1+
error: invalid `--cfg` argument: `crate` (argument key must be an identifier)
22

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: `crate` cannot be a raw identifier
22
|
3-
= note: this error occurred on the command line: `--cfg=r#crate`
3+
= note: this occurred on the command line: `--cfg=r#crate`
44

5-
error: invalid `--cfg` argument: `r#crate` (argument key must be an identifier, but `crate` cannot be a raw identifier)
5+
error: invalid `--cfg` argument: `r#crate` (argument key must be an identifier)
66

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: `self` cannot be a raw identifier
22
|
3-
= note: this error occurred on the command line: `--cfg=r#self`
3+
= note: this occurred on the command line: `--cfg=r#self`
44

5-
error: invalid `--cfg` argument: `r#self` (argument key must be an identifier, but `self` cannot be a raw identifier)
5+
error: invalid `--cfg` argument: `r#self` (argument key must be an identifier)
66

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: `Self` cannot be a raw identifier
22
|
3-
= note: this error occurred on the command line: `--cfg=r#Self`
3+
= note: this occurred on the command line: `--cfg=r#Self`
44

5-
error: invalid `--cfg` argument: `r#Self` (argument key must be an identifier, but `Self` cannot be a raw identifier)
5+
error: invalid `--cfg` argument: `r#Self` (argument key must be an identifier)
66

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: `super` cannot be a raw identifier
22
|
3-
= note: this error occurred on the command line: `--cfg=r#super`
3+
= note: this occurred on the command line: `--cfg=r#super`
44

5-
error: invalid `--cfg` argument: `r#super` (argument key must be an identifier, but `super` cannot be a raw identifier)
5+
error: invalid `--cfg` argument: `r#super` (argument key must be an identifier)
66

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: `_` cannot be a raw identifier
22
|
3-
= note: this error occurred on the command line: `--cfg=r#_`
3+
= note: this occurred on the command line: `--cfg=r#_`
44

5-
error: invalid `--cfg` argument: `r#_` (argument key must be an identifier, but `_` cannot be a raw identifier)
5+
error: invalid `--cfg` argument: `r#_` (argument key must be an identifier)
66

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: invalid `--cfg` argument: `self` (argument key must be an identifier, but `self` cannot be a raw identifier)
1+
error: invalid `--cfg` argument: `self` (argument key must be an identifier)
22

0 commit comments

Comments
 (0)