Skip to content

Commit 54645e8

Browse files
committed
set up rustc_metadata for SessionDiagnostics, port dependency_format.rs
1 parent 4fd4de7 commit 54645e8

File tree

5 files changed

+103
-53
lines changed

5 files changed

+103
-53
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
metadata_rlib_required =
2+
crate `{$crate_name}` required to be available in rlib format, but was not found in this form
3+
4+
metadata_lib_required =
5+
crate `{$crate_name}` required to be available in {$kind} format, but was not found in this form
6+
7+
metadata_crate_dep_multiple =
8+
cannot satisfy dependencies so `{$crate_name}` only shows up once
9+
.help = having upstream crates all available in one format will likely make this go away
10+
11+
metadata_two_panic_runtimes =
12+
cannot link together two panic runtimes: {$prev_name} and {$cur_name}
13+
14+
metadata_bad_panic_strategy =
15+
the linked panic runtime `{$runtime}` is not compiled with this crate's panic strategy `{$strategy}`
16+
17+
metadata_required_panic_strategy =
18+
the crate `{$crate_name}` requires panic strategy `{$found_strategy}` which is incompatible with this crate's strategy of `{$desired_strategy}`
19+
20+
metadata_incompatible_panic_in_drop_strategy =
21+
the crate `{$crate_name}` is compiled with the panic-in-drop strategy `{$found_strategy}` which is incompatible with this crate's strategy of `{$desired_strategy}`

compiler/rustc_error_messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fluent_messages! {
4646
infer => "../locales/en-US/infer.ftl",
4747
lint => "../locales/en-US/lint.ftl",
4848
monomorphize => "../locales/en-US/monomorphize.ftl",
49+
metadata => "../locales/en-US/metadata.ftl",
4950
parser => "../locales/en-US/parser.ftl",
5051
passes => "../locales/en-US/passes.ftl",
5152
plugin_impl => "../locales/en-US/plugin_impl.ftl",

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
//! than finding a number of solutions (there are normally quite a few).
5353
5454
use crate::creader::CStore;
55+
use crate::errors::{
56+
BadPanicStrategy, CrateDepMultiple, IncompatiblePanicInDropStrategy, LibRequired,
57+
RequiredPanicStrategy, RlibRequired, TwoPanicRuntimes,
58+
};
5559

5660
use rustc_data_structures::fx::FxHashMap;
5761
use rustc_hir::def_id::CrateNum;
@@ -136,11 +140,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
136140
if src.rlib.is_some() {
137141
continue;
138142
}
139-
sess.err(&format!(
140-
"crate `{}` required to be available in rlib format, \
141-
but was not found in this form",
142-
tcx.crate_name(cnum)
143-
));
143+
sess.emit_err(RlibRequired { crate_name: tcx.crate_name(cnum).to_string() });
144144
}
145145
return Vec::new();
146146
}
@@ -224,12 +224,10 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
224224
Linkage::Static => "rlib",
225225
_ => "dylib",
226226
};
227-
sess.err(&format!(
228-
"crate `{}` required to be available in {} format, \
229-
but was not found in this form",
230-
tcx.crate_name(cnum),
231-
kind
232-
));
227+
sess.emit_err(LibRequired {
228+
crate_name: tcx.crate_name(cnum).to_string(),
229+
kind: kind.to_string(),
230+
});
233231
}
234232
}
235233
}
@@ -254,16 +252,7 @@ fn add_library(
254252
// can be refined over time.
255253
if link2 != link || link == RequireStatic {
256254
tcx.sess
257-
.struct_err(&format!(
258-
"cannot satisfy dependencies so `{}` only \
259-
shows up once",
260-
tcx.crate_name(cnum)
261-
))
262-
.help(
263-
"having upstream crates all available in one format \
264-
will likely make this go away",
265-
)
266-
.emit();
255+
.emit_err(CrateDepMultiple { crate_name: tcx.crate_name(cnum).to_string() });
267256
}
268257
}
269258
None => {
@@ -358,13 +347,9 @@ fn verify_ok(tcx: TyCtxt<'_>, list: &[Linkage]) {
358347

359348
if tcx.is_panic_runtime(cnum) {
360349
if let Some((prev, _)) = panic_runtime {
361-
let prev_name = tcx.crate_name(prev);
362-
let cur_name = tcx.crate_name(cnum);
363-
sess.err(&format!(
364-
"cannot link together two \
365-
panic runtimes: {} and {}",
366-
prev_name, cur_name
367-
));
350+
let prev_name = tcx.crate_name(prev).to_string();
351+
let cur_name = tcx.crate_name(cnum).to_string();
352+
sess.emit_err(TwoPanicRuntimes { prev_name, cur_name });
368353
}
369354
panic_runtime = Some((
370355
cnum,
@@ -384,13 +369,10 @@ fn verify_ok(tcx: TyCtxt<'_>, list: &[Linkage]) {
384369
// First up, validate that our selected panic runtime is indeed exactly
385370
// our same strategy.
386371
if found_strategy != desired_strategy {
387-
sess.err(&format!(
388-
"the linked panic runtime `{}` is \
389-
not compiled with this crate's \
390-
panic strategy `{}`",
391-
tcx.crate_name(runtime_cnum),
392-
desired_strategy.desc()
393-
));
372+
sess.emit_err(BadPanicStrategy {
373+
runtime: tcx.crate_name(runtime_cnum).to_string(),
374+
strategy: desired_strategy.desc().to_string(),
375+
});
394376
}
395377

396378
// Next up, verify that all other crates are compatible with this panic
@@ -407,28 +389,19 @@ fn verify_ok(tcx: TyCtxt<'_>, list: &[Linkage]) {
407389
}
408390

409391
if let Some(found_strategy) = tcx.required_panic_strategy(cnum) && desired_strategy != found_strategy {
410-
sess.err(&format!(
411-
"the crate `{}` requires \
412-
panic strategy `{}` which is \
413-
incompatible with this crate's \
414-
strategy of `{}`",
415-
tcx.crate_name(cnum),
416-
found_strategy.desc(),
417-
desired_strategy.desc()
418-
));
392+
sess.emit_err(RequiredPanicStrategy {
393+
crate_name: tcx.crate_name(cnum).to_string(),
394+
found_strategy: found_strategy.desc().to_string(),
395+
desired_strategy: desired_strategy.desc().to_string() });
419396
}
420397

421398
let found_drop_strategy = tcx.panic_in_drop_strategy(cnum);
422399
if tcx.sess.opts.unstable_opts.panic_in_drop != found_drop_strategy {
423-
sess.err(&format!(
424-
"the crate `{}` is compiled with the \
425-
panic-in-drop strategy `{}` which is \
426-
incompatible with this crate's \
427-
strategy of `{}`",
428-
tcx.crate_name(cnum),
429-
found_drop_strategy.desc(),
430-
tcx.sess.opts.unstable_opts.panic_in_drop.desc()
431-
));
400+
sess.emit_err(IncompatiblePanicInDropStrategy {
401+
crate_name: tcx.crate_name(cnum).to_string(),
402+
found_strategy: found_drop_strategy.desc().to_string(),
403+
desired_strategy: tcx.sess.opts.unstable_opts.panic_in_drop.desc().to_string(),
404+
});
432405
}
433406
}
434407
}

compiler/rustc_metadata/src/errors.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// use rustc_errors::ErrorGuaranteed;
2+
use rustc_macros::SessionDiagnostic;
3+
4+
#[derive(SessionDiagnostic)]
5+
#[diag(metadata::rlib_required)]
6+
pub struct RlibRequired {
7+
pub crate_name: String,
8+
}
9+
10+
#[derive(SessionDiagnostic)]
11+
#[diag(metadata::lib_required)]
12+
pub struct LibRequired {
13+
pub crate_name: String,
14+
pub kind: String,
15+
}
16+
17+
#[derive(SessionDiagnostic)]
18+
#[diag(metadata::crate_dep_multiple)]
19+
#[help]
20+
pub struct CrateDepMultiple {
21+
pub crate_name: String,
22+
}
23+
24+
#[derive(SessionDiagnostic)]
25+
#[diag(metadata::two_panic_runtimes)]
26+
pub struct TwoPanicRuntimes {
27+
pub prev_name: String,
28+
pub cur_name: String,
29+
}
30+
31+
#[derive(SessionDiagnostic)]
32+
#[diag(metadata::bad_panic_strategy)]
33+
pub struct BadPanicStrategy {
34+
pub runtime: String,
35+
pub strategy: String,
36+
}
37+
38+
#[derive(SessionDiagnostic)]
39+
#[diag(metadata::required_panic_strategy)]
40+
pub struct RequiredPanicStrategy {
41+
pub crate_name: String,
42+
pub found_strategy: String,
43+
pub desired_strategy: String,
44+
}
45+
46+
#[derive(SessionDiagnostic)]
47+
#[diag(metadata::incompatible_panic_in_drop_strategy)]
48+
pub struct IncompatiblePanicInDropStrategy {
49+
pub crate_name: String,
50+
pub found_strategy: String,
51+
pub desired_strategy: String,
52+
}

compiler/rustc_metadata/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#![feature(never_type)]
1717
#![recursion_limit = "256"]
1818
#![allow(rustc::potential_query_instability)]
19+
#![deny(rustc::untranslatable_diagnostic)]
20+
#![deny(rustc::diagnostic_outside_of_impl)]
1921

2022
extern crate proc_macro;
2123

@@ -34,6 +36,7 @@ mod native_libs;
3436
mod rmeta;
3537

3638
pub mod creader;
39+
pub mod errors;
3740
pub mod fs;
3841
pub mod locator;
3942

0 commit comments

Comments
 (0)